""" MLflow experiment tracking module: Handles run initialization, hyperparameter logging, multi-class evaluation metrics, confusion matrix artifacts, and model pipeline tracking. """ import os import mlflow import mlflow.sklearn from typing import Dict, Any EXPERIMENT_NAME = "Email_Threat_Classification_SOC" # Environment configuration os.environ["MLFLOW_ALLOW_FILE_STORE"] = "true" os.environ["MLFLOW_DISABLE_AGENT_HINT"] = "1" def setup_mlflow(tracking_dir: str = None) -> str: """ Configures MLflow tracking with an SQLite database backend and artifact store. """ if tracking_dir is None: tracking_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "mlruns")) os.makedirs(tracking_dir, exist_ok=True) db_file = os.path.join(tracking_dir, "mlflow.db") db_path = db_file.replace("\\", "/") uri = f"sqlite:///{db_path}" # Ensure existing experiment and run artifact locations match the local environment if os.path.exists(db_file): try: import sqlite3 conn = sqlite3.connect(db_file) cur = conn.cursor() current_exp1_dir = os.path.join(tracking_dir, "1").replace("\\", "/") current_exp0_dir = os.path.join(tracking_dir, "0").replace("\\", "/") cur.execute("UPDATE experiments SET artifact_location = ? WHERE experiment_id IN ('1', 1)", (f"file:{current_exp1_dir}",)) cur.execute("UPDATE experiments SET artifact_location = ? WHERE experiment_id IN ('0', 0)", (f"file:{current_exp0_dir}",)) for row in cur.execute("SELECT run_uuid, artifact_uri FROM runs").fetchall(): r_uuid, art_uri = row if art_uri and "mlruns/1/" in art_uri: new_uri = f"file:{current_exp1_dir}/{r_uuid}/artifacts" cur.execute("UPDATE runs SET artifact_uri = ? WHERE run_uuid = ?", (new_uri, r_uuid)) conn.commit() conn.close() except Exception: pass mlflow.set_tracking_uri(uri) mlflow.set_experiment(EXPERIMENT_NAME) return uri def log_experiment_run( run_name: str, model_type: str, params: Dict[str, Any], metrics: Dict[str, float], artifacts: Dict[str, str] = None, pipeline=None, tags: Dict[str, str] = None ) -> str: """ Logs an atomic experiment run with parameters, metrics, figures, and models. """ with mlflow.start_run(run_name=run_name) as run: # Standard cyber defense tags default_tags = { "model_type": model_type, "domain": "Cybersecurity / Email Threat Intelligence", "framework": "scikit-learn / xgboost", "classes": "Legitimate, Spam, Suspicious" } if tags: default_tags.update(tags) mlflow.set_tags(default_tags) # Log parameters flat_params = {} for k, v in params.items(): flat_params[k] = str(v) flat_params["model_algorithm"] = model_type mlflow.log_params(flat_params) # Log metrics for metric_name, metric_val in metrics.items(): if isinstance(metric_val, (int, float)): mlflow.log_metric(metric_name, float(metric_val)) # Log artifact files (confusion matrix images, reports) if artifacts: for art_name, art_path in artifacts.items(): if os.path.exists(art_path): mlflow.log_artifact(art_path, artifact_path="evaluation_artifacts") # Log model pipeline artifact cleanly if pipeline is not None: try: import tempfile import joblib with tempfile.TemporaryDirectory() as tmp_dir: model_path = os.path.join(tmp_dir, "model_pipeline.joblib") joblib.dump(pipeline, model_path) mlflow.log_artifact(model_path, artifact_path="model_pipeline") except Exception as e: print(f"[Notice] Model artifact logging notice: {e}") run_id = run.info.run_id return run_id def get_experiment_summary() -> list: """ Retrieves summary table of all logged runs in the experiment. """ experiment = mlflow.get_experiment_by_name(EXPERIMENT_NAME) if not experiment: return [] runs = mlflow.search_runs(experiment_ids=[experiment.experiment_id]) return runs.to_dict(orient="records")