nirs4all.controllers.models.stacking.multilevel module
Multi-Level Stacking Validator (Phase 7).
This module provides validation and level detection for multi-level stacking, where meta-models can use predictions from other meta-models as sources.
The validator ensures: 1. No circular dependencies exist in the stacking hierarchy 2. Stacking levels don’t exceed configured maximum 3. Level detection works correctly for AUTO mode 4. Source models from appropriate levels are selected
- Stacking Hierarchy:
Level 0: Base models (PLS, RF, XGBoost, Neural Networks, etc.) Level 1: First meta-models (stack on Level 0 only) Level 2: Second meta-models (stack on Level 0 + Level 1) Level 3: Third meta-models (stack on Level 0 + Level 1 + Level 2)
Example
>>> validator = MultiLevelValidator(prediction_store)
>>> result = validator.validate_sources(
... meta_model_name="FinalMeta",
... source_candidates=candidates,
... context=context
... )
>>> if not result.is_valid:
... raise CircularDependencyError(...)
- class nirs4all.controllers.models.stacking.multilevel.LevelValidationResult(is_valid: bool = True, detected_level: int = 1, source_levels: Dict[str, int]=<factory>, circular_dependencies: List[List[str]] = <factory>, warnings: List[str] = <factory>, errors: List[str] = <factory>)[source]
Bases:
objectResult of multi-level stacking validation.
- class nirs4all.controllers.models.stacking.multilevel.ModelLevelInfo(model_name: str, level: int, is_meta_model: bool, source_models: List[str] = <factory>, step_idx: int = 0)[source]
Bases:
objectInformation about a model’s stacking level.
- class nirs4all.controllers.models.stacking.multilevel.MultiLevelValidator(prediction_store: Predictions, max_level: int = 3, log_warnings: bool = True)[source]
Bases:
objectValidates multi-level stacking configurations.
Ensures that stacking hierarchies are valid, detects circular dependencies, and computes appropriate stacking levels for meta-models.
- prediction_store
Predictions storage for analyzing model metadata.
- max_level
Maximum allowed stacking level.
- log_warnings
Whether to emit Python warnings.
- META_MODEL_PATTERNS = {'MetaModel', 'StackingClassifier', 'StackingRegressor'}
- detect_level(source_candidates: List[ModelCandidate], context: ExecutionContext) int[source]
Detect the appropriate stacking level based on source models.
- Parameters:
source_candidates – List of candidate source models.
context – Execution context.
- Returns:
Detected stacking level (1 if no meta-model sources, 2+ otherwise).
- filter_by_level(candidates: List[ModelCandidate], context: ExecutionContext, max_source_level: int | None = None, exclude_meta_models: bool = False) List[ModelCandidate][source]
Filter source candidates by stacking level.
- Parameters:
candidates – List of candidate source models.
context – Execution context.
max_source_level – Maximum allowed source level (None = no limit).
exclude_meta_models – If True, exclude all meta-models from sources.
- Returns:
Filtered list of candidates.
- get_all_levels(context: ExecutionContext) Dict[str, int][source]
Get levels for all models in the prediction store.
- Parameters:
context – Execution context.
- Returns:
Dict mapping model names to their stacking levels.
- validate_sources(meta_model_name: str, source_candidates: List[ModelCandidate], context: ExecutionContext, allow_meta_sources: bool = True) LevelValidationResult[source]
Validate source models for a meta-model.
Checks for circular dependencies and computes the appropriate stacking level based on source model levels.
- Parameters:
meta_model_name – Name of the meta-model being validated.
source_candidates – List of candidate source models.
context – Execution context.
allow_meta_sources – Whether to allow other meta-models as sources.
- Returns:
LevelValidationResult with validation status and detected level.
- nirs4all.controllers.models.stacking.multilevel.detect_stacking_level(prediction_store: Predictions, source_candidates: List[ModelCandidate], context: ExecutionContext) int[source]
Convenience function for detecting stacking level.
- Parameters:
prediction_store – Predictions storage.
source_candidates – List of candidate source models.
context – Execution context.
- Returns:
Detected stacking level.
- nirs4all.controllers.models.stacking.multilevel.validate_multi_level_stacking(prediction_store: Predictions, meta_model_name: str, source_candidates: List[ModelCandidate], context: ExecutionContext, max_level: int = 3, allow_meta_sources: bool = True) LevelValidationResult[source]
Convenience function for validating multi-level stacking.
- Parameters:
prediction_store – Predictions storage.
meta_model_name – Name of the meta-model.
source_candidates – List of candidate source models.
context – Execution context.
max_level – Maximum allowed stacking level.
allow_meta_sources – Whether to allow meta-model sources.
- Returns:
LevelValidationResult with validation status.