churnkit 0.75.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/00_start_here.ipynb +647 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01_data_discovery.ipynb +1165 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01a_a_temporal_text_deep_dive.ipynb +961 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01a_temporal_deep_dive.ipynb +1690 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01b_temporal_quality.ipynb +679 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01c_temporal_patterns.ipynb +3305 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/01d_event_aggregation.ipynb +1463 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/02_column_deep_dive.ipynb +1430 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/02a_text_columns_deep_dive.ipynb +854 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/03_quality_assessment.ipynb +1639 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/04_relationship_analysis.ipynb +1890 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/05_multi_dataset.ipynb +1457 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/06_feature_opportunities.ipynb +1624 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/07_modeling_readiness.ipynb +780 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/08_baseline_experiments.ipynb +979 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/09_business_alignment.ipynb +572 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/10_spec_generation.ipynb +1179 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/11_scoring_validation.ipynb +1418 -0
- churnkit-0.75.0a1.data/data/share/churnkit/exploration_notebooks/12_view_documentation.ipynb +151 -0
- churnkit-0.75.0a1.dist-info/METADATA +229 -0
- churnkit-0.75.0a1.dist-info/RECORD +302 -0
- churnkit-0.75.0a1.dist-info/WHEEL +4 -0
- churnkit-0.75.0a1.dist-info/entry_points.txt +2 -0
- churnkit-0.75.0a1.dist-info/licenses/LICENSE +202 -0
- customer_retention/__init__.py +37 -0
- customer_retention/analysis/__init__.py +0 -0
- customer_retention/analysis/auto_explorer/__init__.py +62 -0
- customer_retention/analysis/auto_explorer/exploration_manager.py +470 -0
- customer_retention/analysis/auto_explorer/explorer.py +258 -0
- customer_retention/analysis/auto_explorer/findings.py +291 -0
- customer_retention/analysis/auto_explorer/layered_recommendations.py +485 -0
- customer_retention/analysis/auto_explorer/recommendation_builder.py +148 -0
- customer_retention/analysis/auto_explorer/recommendations.py +418 -0
- customer_retention/analysis/business/__init__.py +26 -0
- customer_retention/analysis/business/ab_test_designer.py +144 -0
- customer_retention/analysis/business/fairness_analyzer.py +166 -0
- customer_retention/analysis/business/intervention_matcher.py +121 -0
- customer_retention/analysis/business/report_generator.py +222 -0
- customer_retention/analysis/business/risk_profile.py +199 -0
- customer_retention/analysis/business/roi_analyzer.py +139 -0
- customer_retention/analysis/diagnostics/__init__.py +20 -0
- customer_retention/analysis/diagnostics/calibration_analyzer.py +133 -0
- customer_retention/analysis/diagnostics/cv_analyzer.py +144 -0
- customer_retention/analysis/diagnostics/error_analyzer.py +107 -0
- customer_retention/analysis/diagnostics/leakage_detector.py +394 -0
- customer_retention/analysis/diagnostics/noise_tester.py +140 -0
- customer_retention/analysis/diagnostics/overfitting_analyzer.py +190 -0
- customer_retention/analysis/diagnostics/segment_analyzer.py +122 -0
- customer_retention/analysis/discovery/__init__.py +8 -0
- customer_retention/analysis/discovery/config_generator.py +49 -0
- customer_retention/analysis/discovery/discovery_flow.py +19 -0
- customer_retention/analysis/discovery/type_inferencer.py +147 -0
- customer_retention/analysis/interpretability/__init__.py +13 -0
- customer_retention/analysis/interpretability/cohort_analyzer.py +185 -0
- customer_retention/analysis/interpretability/counterfactual.py +175 -0
- customer_retention/analysis/interpretability/individual_explainer.py +141 -0
- customer_retention/analysis/interpretability/pdp_generator.py +103 -0
- customer_retention/analysis/interpretability/shap_explainer.py +106 -0
- customer_retention/analysis/jupyter_save_hook.py +28 -0
- customer_retention/analysis/notebook_html_exporter.py +136 -0
- customer_retention/analysis/notebook_progress.py +60 -0
- customer_retention/analysis/plotly_preprocessor.py +154 -0
- customer_retention/analysis/recommendations/__init__.py +54 -0
- customer_retention/analysis/recommendations/base.py +158 -0
- customer_retention/analysis/recommendations/cleaning/__init__.py +11 -0
- customer_retention/analysis/recommendations/cleaning/consistency.py +107 -0
- customer_retention/analysis/recommendations/cleaning/deduplicate.py +94 -0
- customer_retention/analysis/recommendations/cleaning/impute.py +67 -0
- customer_retention/analysis/recommendations/cleaning/outlier.py +71 -0
- customer_retention/analysis/recommendations/datetime/__init__.py +3 -0
- customer_retention/analysis/recommendations/datetime/extract.py +149 -0
- customer_retention/analysis/recommendations/encoding/__init__.py +3 -0
- customer_retention/analysis/recommendations/encoding/categorical.py +114 -0
- customer_retention/analysis/recommendations/pipeline.py +74 -0
- customer_retention/analysis/recommendations/registry.py +76 -0
- customer_retention/analysis/recommendations/selection/__init__.py +3 -0
- customer_retention/analysis/recommendations/selection/drop_column.py +56 -0
- customer_retention/analysis/recommendations/transform/__init__.py +4 -0
- customer_retention/analysis/recommendations/transform/power.py +94 -0
- customer_retention/analysis/recommendations/transform/scale.py +112 -0
- customer_retention/analysis/visualization/__init__.py +15 -0
- customer_retention/analysis/visualization/chart_builder.py +2619 -0
- customer_retention/analysis/visualization/console.py +122 -0
- customer_retention/analysis/visualization/display.py +171 -0
- customer_retention/analysis/visualization/number_formatter.py +36 -0
- customer_retention/artifacts/__init__.py +3 -0
- customer_retention/artifacts/fit_artifact_registry.py +146 -0
- customer_retention/cli.py +93 -0
- customer_retention/core/__init__.py +0 -0
- customer_retention/core/compat/__init__.py +193 -0
- customer_retention/core/compat/detection.py +99 -0
- customer_retention/core/compat/ops.py +48 -0
- customer_retention/core/compat/pandas_backend.py +57 -0
- customer_retention/core/compat/spark_backend.py +75 -0
- customer_retention/core/components/__init__.py +11 -0
- customer_retention/core/components/base.py +79 -0
- customer_retention/core/components/components/__init__.py +13 -0
- customer_retention/core/components/components/deployer.py +26 -0
- customer_retention/core/components/components/explainer.py +26 -0
- customer_retention/core/components/components/feature_eng.py +33 -0
- customer_retention/core/components/components/ingester.py +34 -0
- customer_retention/core/components/components/profiler.py +34 -0
- customer_retention/core/components/components/trainer.py +38 -0
- customer_retention/core/components/components/transformer.py +36 -0
- customer_retention/core/components/components/validator.py +37 -0
- customer_retention/core/components/enums.py +33 -0
- customer_retention/core/components/orchestrator.py +94 -0
- customer_retention/core/components/registry.py +59 -0
- customer_retention/core/config/__init__.py +39 -0
- customer_retention/core/config/column_config.py +95 -0
- customer_retention/core/config/experiments.py +71 -0
- customer_retention/core/config/pipeline_config.py +117 -0
- customer_retention/core/config/source_config.py +83 -0
- customer_retention/core/utils/__init__.py +28 -0
- customer_retention/core/utils/leakage.py +85 -0
- customer_retention/core/utils/severity.py +53 -0
- customer_retention/core/utils/statistics.py +90 -0
- customer_retention/generators/__init__.py +0 -0
- customer_retention/generators/notebook_generator/__init__.py +167 -0
- customer_retention/generators/notebook_generator/base.py +55 -0
- customer_retention/generators/notebook_generator/cell_builder.py +49 -0
- customer_retention/generators/notebook_generator/config.py +47 -0
- customer_retention/generators/notebook_generator/databricks_generator.py +48 -0
- customer_retention/generators/notebook_generator/local_generator.py +48 -0
- customer_retention/generators/notebook_generator/project_init.py +174 -0
- customer_retention/generators/notebook_generator/runner.py +150 -0
- customer_retention/generators/notebook_generator/script_generator.py +110 -0
- customer_retention/generators/notebook_generator/stages/__init__.py +19 -0
- customer_retention/generators/notebook_generator/stages/base_stage.py +86 -0
- customer_retention/generators/notebook_generator/stages/s01_ingestion.py +100 -0
- customer_retention/generators/notebook_generator/stages/s02_profiling.py +95 -0
- customer_retention/generators/notebook_generator/stages/s03_cleaning.py +180 -0
- customer_retention/generators/notebook_generator/stages/s04_transformation.py +165 -0
- customer_retention/generators/notebook_generator/stages/s05_feature_engineering.py +115 -0
- customer_retention/generators/notebook_generator/stages/s06_feature_selection.py +97 -0
- customer_retention/generators/notebook_generator/stages/s07_model_training.py +176 -0
- customer_retention/generators/notebook_generator/stages/s08_deployment.py +81 -0
- customer_retention/generators/notebook_generator/stages/s09_monitoring.py +112 -0
- customer_retention/generators/notebook_generator/stages/s10_batch_inference.py +642 -0
- customer_retention/generators/notebook_generator/stages/s11_feature_store.py +348 -0
- customer_retention/generators/orchestration/__init__.py +23 -0
- customer_retention/generators/orchestration/code_generator.py +196 -0
- customer_retention/generators/orchestration/context.py +147 -0
- customer_retention/generators/orchestration/data_materializer.py +188 -0
- customer_retention/generators/orchestration/databricks_exporter.py +411 -0
- customer_retention/generators/orchestration/doc_generator.py +311 -0
- customer_retention/generators/pipeline_generator/__init__.py +26 -0
- customer_retention/generators/pipeline_generator/findings_parser.py +727 -0
- customer_retention/generators/pipeline_generator/generator.py +142 -0
- customer_retention/generators/pipeline_generator/models.py +166 -0
- customer_retention/generators/pipeline_generator/renderer.py +2125 -0
- customer_retention/generators/spec_generator/__init__.py +37 -0
- customer_retention/generators/spec_generator/databricks_generator.py +433 -0
- customer_retention/generators/spec_generator/generic_generator.py +373 -0
- customer_retention/generators/spec_generator/mlflow_pipeline_generator.py +685 -0
- customer_retention/generators/spec_generator/pipeline_spec.py +298 -0
- customer_retention/integrations/__init__.py +0 -0
- customer_retention/integrations/adapters/__init__.py +13 -0
- customer_retention/integrations/adapters/base.py +10 -0
- customer_retention/integrations/adapters/factory.py +25 -0
- customer_retention/integrations/adapters/feature_store/__init__.py +6 -0
- customer_retention/integrations/adapters/feature_store/base.py +57 -0
- customer_retention/integrations/adapters/feature_store/databricks.py +94 -0
- customer_retention/integrations/adapters/feature_store/feast_adapter.py +97 -0
- customer_retention/integrations/adapters/feature_store/local.py +75 -0
- customer_retention/integrations/adapters/mlflow/__init__.py +6 -0
- customer_retention/integrations/adapters/mlflow/base.py +32 -0
- customer_retention/integrations/adapters/mlflow/databricks.py +54 -0
- customer_retention/integrations/adapters/mlflow/experiment_tracker.py +161 -0
- customer_retention/integrations/adapters/mlflow/local.py +50 -0
- customer_retention/integrations/adapters/storage/__init__.py +5 -0
- customer_retention/integrations/adapters/storage/base.py +33 -0
- customer_retention/integrations/adapters/storage/databricks.py +76 -0
- customer_retention/integrations/adapters/storage/local.py +59 -0
- customer_retention/integrations/feature_store/__init__.py +47 -0
- customer_retention/integrations/feature_store/definitions.py +215 -0
- customer_retention/integrations/feature_store/manager.py +744 -0
- customer_retention/integrations/feature_store/registry.py +412 -0
- customer_retention/integrations/iteration/__init__.py +28 -0
- customer_retention/integrations/iteration/context.py +212 -0
- customer_retention/integrations/iteration/feedback_collector.py +184 -0
- customer_retention/integrations/iteration/orchestrator.py +168 -0
- customer_retention/integrations/iteration/recommendation_tracker.py +341 -0
- customer_retention/integrations/iteration/signals.py +212 -0
- customer_retention/integrations/llm_context/__init__.py +4 -0
- customer_retention/integrations/llm_context/context_builder.py +201 -0
- customer_retention/integrations/llm_context/prompts.py +100 -0
- customer_retention/integrations/streaming/__init__.py +103 -0
- customer_retention/integrations/streaming/batch_integration.py +149 -0
- customer_retention/integrations/streaming/early_warning_model.py +227 -0
- customer_retention/integrations/streaming/event_schema.py +214 -0
- customer_retention/integrations/streaming/online_store_writer.py +249 -0
- customer_retention/integrations/streaming/realtime_scorer.py +261 -0
- customer_retention/integrations/streaming/trigger_engine.py +293 -0
- customer_retention/integrations/streaming/window_aggregator.py +393 -0
- customer_retention/stages/__init__.py +0 -0
- customer_retention/stages/cleaning/__init__.py +9 -0
- customer_retention/stages/cleaning/base.py +28 -0
- customer_retention/stages/cleaning/missing_handler.py +160 -0
- customer_retention/stages/cleaning/outlier_handler.py +204 -0
- customer_retention/stages/deployment/__init__.py +28 -0
- customer_retention/stages/deployment/batch_scorer.py +106 -0
- customer_retention/stages/deployment/champion_challenger.py +299 -0
- customer_retention/stages/deployment/model_registry.py +182 -0
- customer_retention/stages/deployment/retraining_trigger.py +245 -0
- customer_retention/stages/features/__init__.py +73 -0
- customer_retention/stages/features/behavioral_features.py +266 -0
- customer_retention/stages/features/customer_segmentation.py +505 -0
- customer_retention/stages/features/feature_definitions.py +265 -0
- customer_retention/stages/features/feature_engineer.py +551 -0
- customer_retention/stages/features/feature_manifest.py +340 -0
- customer_retention/stages/features/feature_selector.py +239 -0
- customer_retention/stages/features/interaction_features.py +160 -0
- customer_retention/stages/features/temporal_features.py +243 -0
- customer_retention/stages/ingestion/__init__.py +9 -0
- customer_retention/stages/ingestion/load_result.py +32 -0
- customer_retention/stages/ingestion/loaders.py +195 -0
- customer_retention/stages/ingestion/source_registry.py +130 -0
- customer_retention/stages/modeling/__init__.py +31 -0
- customer_retention/stages/modeling/baseline_trainer.py +139 -0
- customer_retention/stages/modeling/cross_validator.py +125 -0
- customer_retention/stages/modeling/data_splitter.py +205 -0
- customer_retention/stages/modeling/feature_scaler.py +99 -0
- customer_retention/stages/modeling/hyperparameter_tuner.py +107 -0
- customer_retention/stages/modeling/imbalance_handler.py +282 -0
- customer_retention/stages/modeling/mlflow_logger.py +95 -0
- customer_retention/stages/modeling/model_comparator.py +149 -0
- customer_retention/stages/modeling/model_evaluator.py +138 -0
- customer_retention/stages/modeling/threshold_optimizer.py +131 -0
- customer_retention/stages/monitoring/__init__.py +37 -0
- customer_retention/stages/monitoring/alert_manager.py +328 -0
- customer_retention/stages/monitoring/drift_detector.py +201 -0
- customer_retention/stages/monitoring/performance_monitor.py +242 -0
- customer_retention/stages/preprocessing/__init__.py +5 -0
- customer_retention/stages/preprocessing/transformer_manager.py +284 -0
- customer_retention/stages/profiling/__init__.py +256 -0
- customer_retention/stages/profiling/categorical_distribution.py +269 -0
- customer_retention/stages/profiling/categorical_target_analyzer.py +274 -0
- customer_retention/stages/profiling/column_profiler.py +527 -0
- customer_retention/stages/profiling/distribution_analysis.py +483 -0
- customer_retention/stages/profiling/drift_detector.py +310 -0
- customer_retention/stages/profiling/feature_capacity.py +507 -0
- customer_retention/stages/profiling/pattern_analysis_config.py +513 -0
- customer_retention/stages/profiling/profile_result.py +212 -0
- customer_retention/stages/profiling/quality_checks.py +1632 -0
- customer_retention/stages/profiling/relationship_detector.py +256 -0
- customer_retention/stages/profiling/relationship_recommender.py +454 -0
- customer_retention/stages/profiling/report_generator.py +520 -0
- customer_retention/stages/profiling/scd_analyzer.py +151 -0
- customer_retention/stages/profiling/segment_analyzer.py +632 -0
- customer_retention/stages/profiling/segment_aware_outlier.py +265 -0
- customer_retention/stages/profiling/target_level_analyzer.py +217 -0
- customer_retention/stages/profiling/temporal_analyzer.py +388 -0
- customer_retention/stages/profiling/temporal_coverage.py +488 -0
- customer_retention/stages/profiling/temporal_feature_analyzer.py +692 -0
- customer_retention/stages/profiling/temporal_feature_engineer.py +703 -0
- customer_retention/stages/profiling/temporal_pattern_analyzer.py +636 -0
- customer_retention/stages/profiling/temporal_quality_checks.py +278 -0
- customer_retention/stages/profiling/temporal_target_analyzer.py +241 -0
- customer_retention/stages/profiling/text_embedder.py +87 -0
- customer_retention/stages/profiling/text_processor.py +115 -0
- customer_retention/stages/profiling/text_reducer.py +60 -0
- customer_retention/stages/profiling/time_series_profiler.py +303 -0
- customer_retention/stages/profiling/time_window_aggregator.py +376 -0
- customer_retention/stages/profiling/type_detector.py +382 -0
- customer_retention/stages/profiling/window_recommendation.py +288 -0
- customer_retention/stages/temporal/__init__.py +166 -0
- customer_retention/stages/temporal/access_guard.py +180 -0
- customer_retention/stages/temporal/cutoff_analyzer.py +235 -0
- customer_retention/stages/temporal/data_preparer.py +178 -0
- customer_retention/stages/temporal/point_in_time_join.py +134 -0
- customer_retention/stages/temporal/point_in_time_registry.py +148 -0
- customer_retention/stages/temporal/scenario_detector.py +163 -0
- customer_retention/stages/temporal/snapshot_manager.py +259 -0
- customer_retention/stages/temporal/synthetic_coordinator.py +66 -0
- customer_retention/stages/temporal/timestamp_discovery.py +531 -0
- customer_retention/stages/temporal/timestamp_manager.py +255 -0
- customer_retention/stages/transformation/__init__.py +13 -0
- customer_retention/stages/transformation/binary_handler.py +85 -0
- customer_retention/stages/transformation/categorical_encoder.py +245 -0
- customer_retention/stages/transformation/datetime_transformer.py +97 -0
- customer_retention/stages/transformation/numeric_transformer.py +181 -0
- customer_retention/stages/transformation/pipeline.py +257 -0
- customer_retention/stages/validation/__init__.py +60 -0
- customer_retention/stages/validation/adversarial_scoring_validator.py +205 -0
- customer_retention/stages/validation/business_sense_gate.py +173 -0
- customer_retention/stages/validation/data_quality_gate.py +235 -0
- customer_retention/stages/validation/data_validators.py +511 -0
- customer_retention/stages/validation/feature_quality_gate.py +183 -0
- customer_retention/stages/validation/gates.py +117 -0
- customer_retention/stages/validation/leakage_gate.py +352 -0
- customer_retention/stages/validation/model_validity_gate.py +213 -0
- customer_retention/stages/validation/pipeline_validation_runner.py +264 -0
- customer_retention/stages/validation/quality_scorer.py +544 -0
- customer_retention/stages/validation/rule_generator.py +57 -0
- customer_retention/stages/validation/scoring_pipeline_validator.py +446 -0
- customer_retention/stages/validation/timeseries_detector.py +769 -0
- customer_retention/transforms/__init__.py +47 -0
- customer_retention/transforms/artifact_store.py +50 -0
- customer_retention/transforms/executor.py +157 -0
- customer_retention/transforms/fitted.py +92 -0
- customer_retention/transforms/ops.py +148 -0
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any, Dict, List, Optional
|
|
6
|
+
|
|
7
|
+
import yaml
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class RecommendationStatus(Enum):
|
|
11
|
+
PENDING = "pending"
|
|
12
|
+
APPLIED = "applied"
|
|
13
|
+
SKIPPED = "skipped"
|
|
14
|
+
FAILED = "failed"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class RecommendationType(Enum):
|
|
18
|
+
CLEANING = "cleaning"
|
|
19
|
+
TRANSFORM = "transform"
|
|
20
|
+
FEATURE = "feature"
|
|
21
|
+
ENCODING = "encoding"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class TrackedRecommendation:
|
|
26
|
+
recommendation_id: str
|
|
27
|
+
recommendation_type: RecommendationType
|
|
28
|
+
source_column: str
|
|
29
|
+
action: str
|
|
30
|
+
description: str
|
|
31
|
+
status: RecommendationStatus = RecommendationStatus.PENDING
|
|
32
|
+
applied_in_iteration: Optional[str] = None
|
|
33
|
+
skip_reason: Optional[str] = None
|
|
34
|
+
failure_reason: Optional[str] = None
|
|
35
|
+
outcome_impact: Optional[float] = None
|
|
36
|
+
created_at: datetime = field(default_factory=datetime.now)
|
|
37
|
+
updated_at: Optional[datetime] = None
|
|
38
|
+
|
|
39
|
+
@staticmethod
|
|
40
|
+
def generate_id(rec_type: RecommendationType, column: str, action: str) -> str:
|
|
41
|
+
return f"{rec_type.value}_{column}_{action}"
|
|
42
|
+
|
|
43
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
44
|
+
return {
|
|
45
|
+
"recommendation_id": self.recommendation_id,
|
|
46
|
+
"recommendation_type": self.recommendation_type.value,
|
|
47
|
+
"source_column": self.source_column,
|
|
48
|
+
"action": self.action,
|
|
49
|
+
"description": self.description,
|
|
50
|
+
"status": self.status.value,
|
|
51
|
+
"applied_in_iteration": self.applied_in_iteration,
|
|
52
|
+
"skip_reason": self.skip_reason,
|
|
53
|
+
"failure_reason": self.failure_reason,
|
|
54
|
+
"outcome_impact": self.outcome_impact,
|
|
55
|
+
"created_at": self.created_at.isoformat(),
|
|
56
|
+
"updated_at": self.updated_at.isoformat() if self.updated_at else None
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@classmethod
|
|
60
|
+
def from_dict(cls, data: Dict[str, Any]) -> "TrackedRecommendation":
|
|
61
|
+
return cls(
|
|
62
|
+
recommendation_id=data["recommendation_id"],
|
|
63
|
+
recommendation_type=RecommendationType(data["recommendation_type"]),
|
|
64
|
+
source_column=data["source_column"],
|
|
65
|
+
action=data["action"],
|
|
66
|
+
description=data.get("description", ""),
|
|
67
|
+
status=RecommendationStatus(data.get("status", "pending")),
|
|
68
|
+
applied_in_iteration=data.get("applied_in_iteration"),
|
|
69
|
+
skip_reason=data.get("skip_reason"),
|
|
70
|
+
failure_reason=data.get("failure_reason"),
|
|
71
|
+
outcome_impact=data.get("outcome_impact"),
|
|
72
|
+
created_at=datetime.fromisoformat(data["created_at"]) if "created_at" in data else datetime.now(),
|
|
73
|
+
updated_at=datetime.fromisoformat(data["updated_at"]) if data.get("updated_at") else None
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class RecommendationTracker:
|
|
78
|
+
PATTERN_SECTIONS = ["trend", "seasonality", "cohort", "recency", "categorical"]
|
|
79
|
+
|
|
80
|
+
def __init__(self, storage_path: str):
|
|
81
|
+
self.storage_path = Path(storage_path)
|
|
82
|
+
self.recommendations: Dict[str, TrackedRecommendation] = {}
|
|
83
|
+
|
|
84
|
+
def add(self, recommendation: TrackedRecommendation) -> None:
|
|
85
|
+
self.recommendations[recommendation.recommendation_id] = recommendation
|
|
86
|
+
|
|
87
|
+
def _create_tracked_recommendation(
|
|
88
|
+
self, rec_type: RecommendationType, source_col: str, action: str, description: str
|
|
89
|
+
) -> TrackedRecommendation:
|
|
90
|
+
rec_id = TrackedRecommendation.generate_id(rec_type, source_col, action)
|
|
91
|
+
tracked = TrackedRecommendation(
|
|
92
|
+
recommendation_id=rec_id,
|
|
93
|
+
recommendation_type=rec_type,
|
|
94
|
+
source_column=source_col,
|
|
95
|
+
action=action,
|
|
96
|
+
description=description
|
|
97
|
+
)
|
|
98
|
+
self.add(tracked)
|
|
99
|
+
return tracked
|
|
100
|
+
|
|
101
|
+
def add_from_cleaning(self, cleaning_rec) -> TrackedRecommendation:
|
|
102
|
+
action = f"{cleaning_rec.issue_type}_{cleaning_rec.strategy}"
|
|
103
|
+
return self._create_tracked_recommendation(
|
|
104
|
+
RecommendationType.CLEANING, cleaning_rec.column_name, action, cleaning_rec.description
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def add_from_transform(self, transform_rec) -> TrackedRecommendation:
|
|
108
|
+
return self._create_tracked_recommendation(
|
|
109
|
+
RecommendationType.TRANSFORM, transform_rec.column_name,
|
|
110
|
+
transform_rec.transform_type, transform_rec.reason
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
def add_from_feature(self, feature_rec) -> TrackedRecommendation:
|
|
114
|
+
return self._create_tracked_recommendation(
|
|
115
|
+
RecommendationType.FEATURE, feature_rec.source_column,
|
|
116
|
+
feature_rec.feature_name, feature_rec.description
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
def _add_feature_list(
|
|
120
|
+
self, rec_dict: Dict[str, Any], default_action: str, source_fn=None
|
|
121
|
+
) -> List[TrackedRecommendation]:
|
|
122
|
+
features = rec_dict.get("features", [])
|
|
123
|
+
action = rec_dict.get("action", default_action)
|
|
124
|
+
reason = rec_dict.get("reason", "")
|
|
125
|
+
description = f"{action}: {reason}"
|
|
126
|
+
tracked_list = []
|
|
127
|
+
for feature in features:
|
|
128
|
+
source = source_fn(feature) if source_fn else default_action.split("_")[0]
|
|
129
|
+
tracked_list.append(
|
|
130
|
+
self._create_tracked_recommendation(RecommendationType.FEATURE, source, feature, description)
|
|
131
|
+
)
|
|
132
|
+
return tracked_list
|
|
133
|
+
|
|
134
|
+
def add_from_recency(self, rec_dict: Dict[str, Any]) -> List[TrackedRecommendation]:
|
|
135
|
+
return self._add_feature_list(rec_dict, "recency_feature", source_fn=lambda _: "recency")
|
|
136
|
+
|
|
137
|
+
def add_from_categorical(self, rec_dict: Dict[str, Any]) -> List[TrackedRecommendation]:
|
|
138
|
+
return self._add_feature_list(
|
|
139
|
+
rec_dict, "categorical_feature",
|
|
140
|
+
source_fn=lambda f: f.replace("_is_high_risk", "") if "_is_high_risk" in f else f,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
def _should_add_recommendation(
|
|
144
|
+
self, rec: TrackedRecommendation, seen_ids: set, tracked: List[TrackedRecommendation]
|
|
145
|
+
) -> bool:
|
|
146
|
+
if rec.recommendation_id in seen_ids:
|
|
147
|
+
return False
|
|
148
|
+
seen_ids.add(rec.recommendation_id)
|
|
149
|
+
self.add(rec)
|
|
150
|
+
tracked.append(rec)
|
|
151
|
+
return True
|
|
152
|
+
|
|
153
|
+
def add_from_temporal_findings(self, findings: Any) -> List[TrackedRecommendation]:
|
|
154
|
+
tracked: List[TrackedRecommendation] = []
|
|
155
|
+
seen_ids: set = set()
|
|
156
|
+
pattern_meta = findings.metadata.get("temporal_patterns", {}) if findings.metadata else {}
|
|
157
|
+
|
|
158
|
+
def add_if_new(rec: TrackedRecommendation) -> bool:
|
|
159
|
+
return self._should_add_recommendation(rec, seen_ids, tracked)
|
|
160
|
+
|
|
161
|
+
self._process_pattern_sections(pattern_meta, add_if_new)
|
|
162
|
+
self._process_temporal_features(pattern_meta, add_if_new)
|
|
163
|
+
self._process_sparkline_recommendations(pattern_meta, add_if_new)
|
|
164
|
+
self._process_effect_size_recommendations(pattern_meta, add_if_new)
|
|
165
|
+
self._process_predictive_power_recommendations(pattern_meta, add_if_new)
|
|
166
|
+
return tracked
|
|
167
|
+
|
|
168
|
+
def _process_section_recommendations(self, pattern_meta: Dict, section: str, add_if_new, skip_actions: Optional[List[str]] = None) -> None:
|
|
169
|
+
for rec in pattern_meta.get(section, {}).get("recommendations", []):
|
|
170
|
+
if skip_actions and rec.get("action") in skip_actions:
|
|
171
|
+
continue
|
|
172
|
+
features = rec.get("features", [])
|
|
173
|
+
if not features:
|
|
174
|
+
continue
|
|
175
|
+
action = rec.get("action", f"add_{section}_feature")
|
|
176
|
+
reason = rec.get("reason", f"From {section} analysis")
|
|
177
|
+
priority = rec.get("priority", "medium")
|
|
178
|
+
for feature in features:
|
|
179
|
+
rec_id = TrackedRecommendation.generate_id(RecommendationType.FEATURE, section, feature)
|
|
180
|
+
add_if_new(TrackedRecommendation(
|
|
181
|
+
recommendation_id=rec_id, recommendation_type=RecommendationType.FEATURE,
|
|
182
|
+
source_column=section, action=feature, description=f"[{priority}] {action}: {reason}",
|
|
183
|
+
))
|
|
184
|
+
|
|
185
|
+
def _process_pattern_sections(self, pattern_meta: Dict, add_if_new) -> None:
|
|
186
|
+
for section in self.PATTERN_SECTIONS:
|
|
187
|
+
self._process_section_recommendations(pattern_meta, section, add_if_new, ["skip_cohort_features"])
|
|
188
|
+
|
|
189
|
+
def _process_temporal_features(self, pattern_meta: Dict, add_if_new) -> None:
|
|
190
|
+
for section in ["velocity", "momentum", "lag"]:
|
|
191
|
+
for rec in pattern_meta.get(section, {}).get("recommendations", []):
|
|
192
|
+
features = rec.get("features", [])
|
|
193
|
+
if not features:
|
|
194
|
+
continue
|
|
195
|
+
action = rec.get("action", f"add_{section}_feature")
|
|
196
|
+
description = rec.get("description", f"From {section} analysis")
|
|
197
|
+
source_col = rec.get("source_column", section)
|
|
198
|
+
int_priority = rec.get("priority", 2)
|
|
199
|
+
priority_str = self._get_priority_label(int_priority)
|
|
200
|
+
effect_size = rec.get("effect_size")
|
|
201
|
+
effect_info = f" (d={effect_size:.2f})" if effect_size else ""
|
|
202
|
+
for feature in features:
|
|
203
|
+
rec_id = TrackedRecommendation.generate_id(RecommendationType.FEATURE, source_col, feature)
|
|
204
|
+
add_if_new(TrackedRecommendation(
|
|
205
|
+
recommendation_id=rec_id, recommendation_type=RecommendationType.FEATURE,
|
|
206
|
+
source_column=source_col, action=feature,
|
|
207
|
+
description=f"[{priority_str}] {action}: {description}{effect_info}",
|
|
208
|
+
))
|
|
209
|
+
|
|
210
|
+
def _process_sparkline_recommendations(self, pattern_meta: Dict, add_if_new) -> None:
|
|
211
|
+
for rec in pattern_meta.get("sparkline", {}).get("recommendations", []):
|
|
212
|
+
features = rec.get("features", []) or ([rec.get("feature")] if rec.get("feature") else [])
|
|
213
|
+
if not features:
|
|
214
|
+
continue
|
|
215
|
+
action = rec.get("action", "sparkline_feature")
|
|
216
|
+
reason = rec.get("reason", "From sparkline analysis")
|
|
217
|
+
priority = rec.get("priority", "medium")
|
|
218
|
+
for feature in features:
|
|
219
|
+
rec_id = TrackedRecommendation.generate_id(RecommendationType.FEATURE, "sparkline", feature)
|
|
220
|
+
add_if_new(TrackedRecommendation(
|
|
221
|
+
recommendation_id=rec_id, recommendation_type=RecommendationType.FEATURE,
|
|
222
|
+
source_column="sparkline", action=feature, description=f"[{priority}] {action}: {reason}",
|
|
223
|
+
))
|
|
224
|
+
|
|
225
|
+
def _process_effect_size_recommendations(self, pattern_meta: Dict, add_if_new) -> None:
|
|
226
|
+
for rec in pattern_meta.get("effect_size", {}).get("recommendations", []):
|
|
227
|
+
feature = rec.get("feature", "")
|
|
228
|
+
if not feature or rec.get("action") == "consider_dropping":
|
|
229
|
+
continue
|
|
230
|
+
effect_d = rec.get("effect_size", 0)
|
|
231
|
+
priority = rec.get("priority", "medium")
|
|
232
|
+
reason = rec.get("reason", f"Effect size d={effect_d:.2f}")
|
|
233
|
+
rec_id = TrackedRecommendation.generate_id(RecommendationType.FEATURE, "effect_size", feature)
|
|
234
|
+
add_if_new(TrackedRecommendation(
|
|
235
|
+
recommendation_id=rec_id, recommendation_type=RecommendationType.FEATURE,
|
|
236
|
+
source_column="effect_size", action=feature, description=f"[{priority}] prioritize: {reason}",
|
|
237
|
+
))
|
|
238
|
+
|
|
239
|
+
def _process_predictive_power_recommendations(self, pattern_meta: Dict, add_if_new) -> None:
|
|
240
|
+
for rec in pattern_meta.get("predictive_power", {}).get("recommendations", []):
|
|
241
|
+
feature = rec.get("feature", "")
|
|
242
|
+
if not feature:
|
|
243
|
+
continue
|
|
244
|
+
iv, ks = rec.get("iv", 0), rec.get("ks", 0)
|
|
245
|
+
priority = rec.get("priority", "medium")
|
|
246
|
+
rec_id = TrackedRecommendation.generate_id(RecommendationType.FEATURE, "predictive_power", feature)
|
|
247
|
+
add_if_new(TrackedRecommendation(
|
|
248
|
+
recommendation_id=rec_id, recommendation_type=RecommendationType.FEATURE,
|
|
249
|
+
source_column="predictive_power", action=feature,
|
|
250
|
+
description=f"[{priority}] include: IV={iv:.3f}, KS={ks:.3f}",
|
|
251
|
+
))
|
|
252
|
+
|
|
253
|
+
@staticmethod
|
|
254
|
+
def _get_priority_label(int_priority: int) -> str:
|
|
255
|
+
return "high" if int_priority == 1 else "medium"
|
|
256
|
+
|
|
257
|
+
def get(self, recommendation_id: str) -> Optional[TrackedRecommendation]:
|
|
258
|
+
return self.recommendations.get(recommendation_id)
|
|
259
|
+
|
|
260
|
+
def _update_recommendation_status(self, recommendation_id: str, status: RecommendationStatus, **kwargs) -> None:
|
|
261
|
+
rec = self.get(recommendation_id)
|
|
262
|
+
if rec:
|
|
263
|
+
rec.status = status
|
|
264
|
+
rec.updated_at = datetime.now()
|
|
265
|
+
for attr, value in kwargs.items():
|
|
266
|
+
setattr(rec, attr, value)
|
|
267
|
+
|
|
268
|
+
def mark_applied(self, recommendation_id: str, iteration_id: str) -> None:
|
|
269
|
+
self._update_recommendation_status(
|
|
270
|
+
recommendation_id, RecommendationStatus.APPLIED, applied_in_iteration=iteration_id
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
def mark_skipped(self, recommendation_id: str, reason: str) -> None:
|
|
274
|
+
self._update_recommendation_status(
|
|
275
|
+
recommendation_id, RecommendationStatus.SKIPPED, skip_reason=reason
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
def mark_failed(self, recommendation_id: str, reason: str) -> None:
|
|
279
|
+
self._update_recommendation_status(
|
|
280
|
+
recommendation_id, RecommendationStatus.FAILED, failure_reason=reason
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
def set_outcome_impact(self, recommendation_id: str, impact: float) -> None:
|
|
284
|
+
rec = self.get(recommendation_id)
|
|
285
|
+
if rec:
|
|
286
|
+
rec.outcome_impact = impact
|
|
287
|
+
rec.updated_at = datetime.now()
|
|
288
|
+
|
|
289
|
+
def _get_by_status(self, status: RecommendationStatus) -> List[TrackedRecommendation]:
|
|
290
|
+
return [r for r in self.recommendations.values() if r.status == status]
|
|
291
|
+
|
|
292
|
+
def get_pending(self) -> List[TrackedRecommendation]:
|
|
293
|
+
return self._get_by_status(RecommendationStatus.PENDING)
|
|
294
|
+
|
|
295
|
+
def get_applied(self) -> List[TrackedRecommendation]:
|
|
296
|
+
return self._get_by_status(RecommendationStatus.APPLIED)
|
|
297
|
+
|
|
298
|
+
def get_skipped(self) -> List[TrackedRecommendation]:
|
|
299
|
+
return self._get_by_status(RecommendationStatus.SKIPPED)
|
|
300
|
+
|
|
301
|
+
def get_failed(self) -> List[TrackedRecommendation]:
|
|
302
|
+
return self._get_by_status(RecommendationStatus.FAILED)
|
|
303
|
+
|
|
304
|
+
def get_high_impact(self, threshold: float = 0.1) -> List[TrackedRecommendation]:
|
|
305
|
+
high_impact = [
|
|
306
|
+
r for r in self.recommendations.values()
|
|
307
|
+
if r.outcome_impact is not None and r.outcome_impact >= threshold
|
|
308
|
+
]
|
|
309
|
+
high_impact.sort(key=lambda x: x.outcome_impact or 0, reverse=True)
|
|
310
|
+
return high_impact
|
|
311
|
+
|
|
312
|
+
def get_by_type(self, rec_type: RecommendationType) -> List[TrackedRecommendation]:
|
|
313
|
+
return [r for r in self.recommendations.values()
|
|
314
|
+
if r.recommendation_type == rec_type]
|
|
315
|
+
|
|
316
|
+
def get_summary(self) -> Dict[str, int]:
|
|
317
|
+
return {
|
|
318
|
+
"total": len(self.recommendations),
|
|
319
|
+
"pending": len(self.get_pending()),
|
|
320
|
+
"applied": len(self.get_applied()),
|
|
321
|
+
"skipped": len(self.get_skipped()),
|
|
322
|
+
"failed": len(self.get_failed()),
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
def save(self) -> None:
|
|
326
|
+
self.storage_path.parent.mkdir(parents=True, exist_ok=True)
|
|
327
|
+
data = {
|
|
328
|
+
"recommendations": [r.to_dict() for r in self.recommendations.values()]
|
|
329
|
+
}
|
|
330
|
+
with open(self.storage_path, "w") as f:
|
|
331
|
+
yaml.dump(data, f, default_flow_style=False, sort_keys=False)
|
|
332
|
+
|
|
333
|
+
def load(self) -> None:
|
|
334
|
+
if not self.storage_path.exists():
|
|
335
|
+
return
|
|
336
|
+
with open(self.storage_path, "r") as f:
|
|
337
|
+
data = yaml.safe_load(f)
|
|
338
|
+
if data and "recommendations" in data:
|
|
339
|
+
for rec_data in data["recommendations"]:
|
|
340
|
+
rec = TrackedRecommendation.from_dict(rec_data)
|
|
341
|
+
self.recommendations[rec.recommendation_id] = rec
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple
|
|
5
|
+
|
|
6
|
+
from customer_retention.core.compat import DataFrame
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from .context import IterationTrigger
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class IterationSignal(Enum):
|
|
13
|
+
DRIFT_CRITICAL = "drift_critical"
|
|
14
|
+
DRIFT_WARNING = "drift_warning"
|
|
15
|
+
PERFORMANCE_CRITICAL = "performance_critical"
|
|
16
|
+
PERFORMANCE_WARNING = "performance_warning"
|
|
17
|
+
DATA_QUALITY_ISSUE = "data_quality_issue"
|
|
18
|
+
SCHEDULED_RETRAIN = "scheduled_retrain"
|
|
19
|
+
MANUAL_TRIGGER = "manual_trigger"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class SignalEvent:
|
|
24
|
+
signal_type: IterationSignal
|
|
25
|
+
source: str
|
|
26
|
+
severity: str
|
|
27
|
+
details: Dict[str, Any]
|
|
28
|
+
recommended_action: str
|
|
29
|
+
timestamp: datetime = field(default_factory=datetime.now)
|
|
30
|
+
|
|
31
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
32
|
+
return {
|
|
33
|
+
"signal_type": self.signal_type.value,
|
|
34
|
+
"source": self.source,
|
|
35
|
+
"severity": self.severity,
|
|
36
|
+
"details": self.details,
|
|
37
|
+
"recommended_action": self.recommended_action,
|
|
38
|
+
"timestamp": self.timestamp.isoformat()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def from_dict(cls, data: Dict[str, Any]) -> "SignalEvent":
|
|
43
|
+
return cls(
|
|
44
|
+
signal_type=IterationSignal(data["signal_type"]),
|
|
45
|
+
source=data["source"],
|
|
46
|
+
severity=data["severity"],
|
|
47
|
+
details=data["details"],
|
|
48
|
+
recommended_action=data["recommended_action"],
|
|
49
|
+
timestamp=datetime.fromisoformat(data["timestamp"]) if "timestamp" in data else datetime.now()
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class SignalAggregator:
|
|
54
|
+
def __init__(self, drift_detector=None, performance_monitor=None):
|
|
55
|
+
self.drift_detector = drift_detector
|
|
56
|
+
self.performance_monitor = performance_monitor
|
|
57
|
+
self._pending_signals: List[SignalEvent] = []
|
|
58
|
+
|
|
59
|
+
def check_drift_signals(self, current_data: DataFrame) -> List[SignalEvent]:
|
|
60
|
+
if self.drift_detector is None:
|
|
61
|
+
return []
|
|
62
|
+
|
|
63
|
+
signals = []
|
|
64
|
+
try:
|
|
65
|
+
drift_result = self.drift_detector.detect_drift(current_data, method="psi")
|
|
66
|
+
|
|
67
|
+
for feature_result in drift_result.feature_results:
|
|
68
|
+
if not feature_result.drift_detected:
|
|
69
|
+
continue
|
|
70
|
+
|
|
71
|
+
from customer_retention.core.components.enums import Severity
|
|
72
|
+
if feature_result.severity == Severity.CRITICAL:
|
|
73
|
+
signal_type = IterationSignal.DRIFT_CRITICAL
|
|
74
|
+
severity = "critical"
|
|
75
|
+
action = "retrain"
|
|
76
|
+
else:
|
|
77
|
+
signal_type = IterationSignal.DRIFT_WARNING
|
|
78
|
+
severity = "warning"
|
|
79
|
+
action = "monitor"
|
|
80
|
+
|
|
81
|
+
signals.append(SignalEvent(
|
|
82
|
+
signal_type=signal_type,
|
|
83
|
+
source="drift_detector",
|
|
84
|
+
severity=severity,
|
|
85
|
+
details={
|
|
86
|
+
"feature": feature_result.feature_name,
|
|
87
|
+
"metric": feature_result.metric_name,
|
|
88
|
+
"value": feature_result.metric_value
|
|
89
|
+
},
|
|
90
|
+
recommended_action=action
|
|
91
|
+
))
|
|
92
|
+
except Exception:
|
|
93
|
+
pass
|
|
94
|
+
|
|
95
|
+
self._pending_signals.extend(signals)
|
|
96
|
+
return signals
|
|
97
|
+
|
|
98
|
+
def check_performance_signals(self, current_metrics: Dict[str, float]) -> List[SignalEvent]:
|
|
99
|
+
if self.performance_monitor is None:
|
|
100
|
+
return []
|
|
101
|
+
|
|
102
|
+
signals = []
|
|
103
|
+
result = self.performance_monitor.compare_metrics(current_metrics)
|
|
104
|
+
|
|
105
|
+
from customer_retention.stages.monitoring.performance_monitor import PerformanceStatus
|
|
106
|
+
if result.status == PerformanceStatus.CRITICAL:
|
|
107
|
+
signals.append(SignalEvent(
|
|
108
|
+
signal_type=IterationSignal.PERFORMANCE_CRITICAL,
|
|
109
|
+
source="performance_monitor",
|
|
110
|
+
severity="critical",
|
|
111
|
+
details={
|
|
112
|
+
"current_metrics": current_metrics,
|
|
113
|
+
"baseline_metrics": result.baseline_metrics,
|
|
114
|
+
"comparison": result.comparison
|
|
115
|
+
},
|
|
116
|
+
recommended_action="retrain"
|
|
117
|
+
))
|
|
118
|
+
elif result.status == PerformanceStatus.WARNING:
|
|
119
|
+
signals.append(SignalEvent(
|
|
120
|
+
signal_type=IterationSignal.PERFORMANCE_WARNING,
|
|
121
|
+
source="performance_monitor",
|
|
122
|
+
severity="warning",
|
|
123
|
+
details={
|
|
124
|
+
"current_metrics": current_metrics,
|
|
125
|
+
"baseline_metrics": result.baseline_metrics,
|
|
126
|
+
"comparison": result.comparison
|
|
127
|
+
},
|
|
128
|
+
recommended_action="investigate"
|
|
129
|
+
))
|
|
130
|
+
|
|
131
|
+
self._pending_signals.extend(signals)
|
|
132
|
+
return signals
|
|
133
|
+
|
|
134
|
+
def add_manual_signal(self, reason: str, details: Dict[str, Any]) -> SignalEvent:
|
|
135
|
+
event = SignalEvent(
|
|
136
|
+
signal_type=IterationSignal.MANUAL_TRIGGER,
|
|
137
|
+
source="user",
|
|
138
|
+
severity="info",
|
|
139
|
+
details={"reason": reason, **details},
|
|
140
|
+
recommended_action="retrain"
|
|
141
|
+
)
|
|
142
|
+
self._pending_signals.append(event)
|
|
143
|
+
return event
|
|
144
|
+
|
|
145
|
+
def add_scheduled_signal(self, schedule_name: str) -> SignalEvent:
|
|
146
|
+
event = SignalEvent(
|
|
147
|
+
signal_type=IterationSignal.SCHEDULED_RETRAIN,
|
|
148
|
+
source="scheduler",
|
|
149
|
+
severity="info",
|
|
150
|
+
details={"schedule": schedule_name},
|
|
151
|
+
recommended_action="retrain"
|
|
152
|
+
)
|
|
153
|
+
self._pending_signals.append(event)
|
|
154
|
+
return event
|
|
155
|
+
|
|
156
|
+
def check_all_signals(self, current_data: Optional[DataFrame] = None,
|
|
157
|
+
current_metrics: Optional[Dict[str, float]] = None) -> List[SignalEvent]:
|
|
158
|
+
all_signals = []
|
|
159
|
+
|
|
160
|
+
if current_data is not None:
|
|
161
|
+
all_signals.extend(self.check_drift_signals(current_data))
|
|
162
|
+
|
|
163
|
+
if current_metrics is not None:
|
|
164
|
+
all_signals.extend(self.check_performance_signals(current_metrics))
|
|
165
|
+
|
|
166
|
+
return all_signals
|
|
167
|
+
|
|
168
|
+
def get_pending_signals(self) -> List[SignalEvent]:
|
|
169
|
+
return self._pending_signals.copy()
|
|
170
|
+
|
|
171
|
+
def clear_signals(self) -> None:
|
|
172
|
+
self._pending_signals.clear()
|
|
173
|
+
|
|
174
|
+
def should_trigger_iteration(self) -> Tuple[bool, Optional["IterationTrigger"]]:
|
|
175
|
+
from .context import IterationTrigger
|
|
176
|
+
|
|
177
|
+
if not self._pending_signals:
|
|
178
|
+
return False, None
|
|
179
|
+
|
|
180
|
+
for signal in self._pending_signals:
|
|
181
|
+
if signal.signal_type == IterationSignal.DRIFT_CRITICAL:
|
|
182
|
+
return True, IterationTrigger.DRIFT_DETECTED
|
|
183
|
+
if signal.signal_type == IterationSignal.PERFORMANCE_CRITICAL:
|
|
184
|
+
return True, IterationTrigger.PERFORMANCE_DROP
|
|
185
|
+
|
|
186
|
+
for signal in self._pending_signals:
|
|
187
|
+
if signal.signal_type == IterationSignal.MANUAL_TRIGGER:
|
|
188
|
+
return True, IterationTrigger.MANUAL
|
|
189
|
+
if signal.signal_type == IterationSignal.SCHEDULED_RETRAIN:
|
|
190
|
+
return True, IterationTrigger.SCHEDULED
|
|
191
|
+
|
|
192
|
+
critical_count = sum(1 for s in self._pending_signals if "critical" in s.severity.lower())
|
|
193
|
+
warning_count = sum(1 for s in self._pending_signals if "warning" in s.severity.lower())
|
|
194
|
+
|
|
195
|
+
if critical_count > 0:
|
|
196
|
+
return True, IterationTrigger.DRIFT_DETECTED
|
|
197
|
+
if warning_count >= 3:
|
|
198
|
+
return True, IterationTrigger.DRIFT_DETECTED
|
|
199
|
+
|
|
200
|
+
return False, None
|
|
201
|
+
|
|
202
|
+
def get_signal_summary(self) -> Dict[str, Any]:
|
|
203
|
+
return {
|
|
204
|
+
"total": len(self._pending_signals),
|
|
205
|
+
"critical": sum(1 for s in self._pending_signals if "critical" in s.severity.lower()),
|
|
206
|
+
"warning": sum(1 for s in self._pending_signals if "warning" in s.severity.lower()),
|
|
207
|
+
"info": sum(1 for s in self._pending_signals if "info" in s.severity.lower()),
|
|
208
|
+
"signals_by_type": {
|
|
209
|
+
signal_type.value: sum(1 for s in self._pending_signals if s.signal_type == signal_type)
|
|
210
|
+
for signal_type in IterationSignal
|
|
211
|
+
}
|
|
212
|
+
}
|