This guide explains how to generate model classes and perform static validation in the Beaver project.
# Basic code generation with validation
python beaver_cli.py generate --input examples/model.bvr
# Generate with comprehensive checking
python beaver_cli.py generate --input examples/model.bvr --check-syntax --verbose
# Preview what will be generated
python beaver_cli.py generate --input examples/model.bvr --dry-run# Validate without generating code
python beaver_cli.py validate --input examples/model.bvr
# Verbose validation with detailed feedback
python beaver_cli.py validate --input examples/model.bvr --verbose# Analyze all examples
python beaver_cli.py analyze --directory examples
# Analyze specific file
python beaver_cli.py analyze --input examples/model.bvr
# Get JSON output for programmatic use
python beaver_cli.py analyze --directory examples --output jsonFeatures:
- ✅ Static validation before code generation
- ✅ Syntax checking of generated code
- ✅ Compilation testing
- ✅ Dry-run mode for previewing
- ✅ Verbose logging
- ✅ Comprehensive error reporting
Usage:
python beaver/gen_enhanced.py --metamodel examples/model.bvr --generated_file_name output.py --check-syntax --verboseOptions:
--validate-only: Only perform validation, skip code generation--skip-validation: Skip static validation (not recommended)--check-syntax: Validate Python syntax of generated code--dry-run: Show preview without creating files--verbose: Enable detailed output
Features:
- ✅ Validates model classes exist in River library
- ✅ Checks parameter compatibility
- ✅ Validates model combinations in pipelines
- ✅ Provides improvement suggestions
- ✅ Categorizes issues by severity (Error/Warning/Info)
Validation Categories:
- Model Existence: Checks if River classes are available
- Parameter Validation: Verifies parameter names and types
- Pipeline Compatibility: Ensures models work together
- Best Practices: Suggests improvements
Features:
- ✅ Analyzes complexity of model configurations
- ✅ Provides improvement suggestions
- ✅ Generates detailed reports
- ✅ Supports batch analysis of multiple files
- ✅ JSON output for integration
Metrics Tracked:
- Model count and types
- Data source complexity
- Pipeline structure
- Parameter usage
- Feature engineering complexity
Features:
- ✅ Single entry point for all operations
- ✅ Consistent command structure
- ✅ Built-in help and examples
- ✅ Error handling and user feedback
The validator checks that your models are properly defined:
# ✅ GOOD: Valid River model
algorithm <ALMAClassifier> alma_model
params:
lr = 0.1
# ❌ BAD: Non-existent class
algorithm <NonExistentClassifier> bad_model
params:
invalid_param = 1Parameters are validated against River documentation:
# ✅ GOOD: Valid parameters
algorithm <KMeans> clustering_model
params:
n_clusters = 5
halflife = 0.5
# ❌ BAD: Invalid parameter
algorithm <KMeans> bad_clustering
params:
wrong_parameter = 5The system checks pipeline compatibility:
# ✅ GOOD: Complete pipeline
preprocessor <StandardScaler> scaler
algorithm <LogisticRegression> classifier
metric <Accuracy> accuracy_metric
# ⚠️ WARNING: Missing preprocessor
algorithm <LogisticRegression> classifier_only-
❌ ERROR: Must be fixed before code generation
- Non-existent model classes
- Invalid parameter names
- Critical syntax issues
-
⚠️ WARNING: Should be reviewed but won't block generation- Parameter type mismatches
- Missing preprocessing steps
- Suboptimal configurations
-
ℹ️ INFO: Suggestions for improvement
- Performance optimizations
- Best practice recommendations
- Code quality improvements
🔍 Model Validation Report
==================================================
❌ ERRORS (1):
• [model2] Parameter 'wrong_param' not valid for ALMAClassifier
💡 Valid parameters: lr, alpha, B, C
⚠️ WARNINGS (2):
• [model3] Parameter 'lr' expects float, got str
• [pipeline] No preprocessing steps found
💡 Consider adding StandardScaler or other preprocessors
ℹ️ INFO (1):
• [model4] Using default parameters
💡 Consider tuning parameters for better performance
You can extend the validator with custom rules:
from beaver.validator import ModelValidator, ValidationIssue, ValidationLevel
class CustomValidator(ModelValidator):
def validate_custom_rule(self, model):
# Add your custom validation logic
if model.name.startswith('test_'):
self.issues.append(ValidationIssue(
level=ValidationLevel.WARNING,
message="Model name suggests this is a test model",
model_name=model.name,
suggestion="Use descriptive model names in production"
))Process multiple files programmatically:
from beaver.analyzer import ModelAnalyzer
from pathlib import Path
analyzer = ModelAnalyzer()
results = {}
for file_path in Path('examples').glob('*.bvr'):
results[file_path.name] = analyzer.analyze_file(str(file_path))
# Generate summary report
for filename, result in results.items():
if result['status'] == 'success':
print(f"{filename}: {result['metrics']['complexity_score']} complexity")Add validation to your CI pipeline:
# .github/workflows/validate-models.yml
name: Validate Beaver Models
on: [push, pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.9"
- name: Install dependencies
run: pip install -r requirements.txt
- name: Validate models
run: |
for file in examples/*.bvr; do
python beaver_cli.py validate --input "$file" --verbose
done-
"Module not found" errors
- Ensure River is installed:
pip install river - Check for typos in model class names
- Ensure River is installed:
-
"Parameter not valid" errors
- Check River documentation for correct parameter names
- Verify parameter types match expected values
-
Generation fails after validation passes
- Check template syntax in Jinja files
- Verify all referenced models are defined
- Verbose Mode: Add
--verboseto any command for detailed output - Dry Run: Use
--dry-runto preview without making changes - Check Examples: Run
python beaver_cli.py examplesto see available examples - Extended Help: Run
python beaver_cli.py helpfor comprehensive guidance
- Use Descriptive Names:
fraud_detectorinstead ofmodel1 - Include Parameters: Don't rely only on defaults
- Add Preprocessing: Most models benefit from data preprocessing
- Define Metrics: Always include appropriate evaluation metrics
- Validate Early: Run validation before complex configurations
- Fix Errors First: Address all errors before warnings
- Test Generated Code: Use
--check-syntaxto catch issues early - Review Suggestions: Consider warnings and info messages
- Start Simple: Begin with basic models and add complexity gradually
- Validate Frequently: Run validation after each change
- Use Analysis: Regularly analyze your configurations for improvements
- Document Changes: Keep track of model parameter tuning
beaver/validator.py: Core validation logicbeaver/gen_enhanced.py: Enhanced code generatorbeaver/analyzer.py: Model analysis and suggestionsbeaver_cli.py: Unified command-line interfacebeaver/templates/models_macros.jinja: Updated Jinja templatesexamples/: Sample .bvr files for testing