Skip to content

Mlfow tutoirial#60

Open
Farid841 wants to merge 2 commits into
astrolabsoftware:mainfrom
Farid841:mlfow-tutoirial
Open

Mlfow tutoirial#60
Farid841 wants to merge 2 commits into
astrolabsoftware:mainfrom
Farid841:mlfow-tutoirial

Conversation

@Farid841

@Farid841 Farid841 commented Jul 9, 2026

Copy link
Copy Markdown

Add two Jupyter notebook tutorials showing how to use MLflow with Fink: install and run an MLflow server locally, log a full training run (params, metrics, model, artifacts), then rerun only the best run to the remote Fink MLflow server.

Currently, there is no optimal method for sending a specific execution with models or artifacts.
import export mlflow can be explore in the futur

Copilot AI review requested due to automatic review settings July 9, 2026 17:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new “Fink AI” tutorial area under ztf/ focused on using MLflow for local experiment tracking and re-running a selected “best” run against the remote Fink MLflow server, including logging preprocessing code and dependency metadata for deployment/reproducibility.

Changes:

  • Introduces two MLflow-focused Jupyter notebook tutorials (local setup + remote re-run workflow).
  • Adds preprocessing/inference helper scripts intended to be logged as MLflow artifacts.
  • Adds a small tutorial README plus a local requirements.txt for the tutorial environment.

Reviewed changes

Copilot reviewed 6 out of 18 changed files in this pull request and generated 14 comments.

Show a summary per file
File Description
ztf/fink ai/requirements.txt Adds a tutorial-specific dependency list for the Fink AI/MLflow walkthrough.
ztf/fink ai/README.md Documents the two-notebook workflow and prerequisites for the MLflow tutorials.
ztf/fink ai/processor.py Adds an example inference entrypoint wrapping preprocessing + model inference.
ztf/fink ai/preprocessing.py Adds end-to-end alert preprocessing utilities + a pre_processing entrypoint for remote use.
ztf/fink ai/01_mlflow_local_setup_and_first_run.ipynb Tutorial 1 notebook for running MLflow locally and logging runs/artifacts.
ztf/fink ai/02_send_run_to_remote_server.ipynb Tutorial 2 notebook for switching MLflow tracking URI and re-running remotely.
ztf/fink ai/data/init.py Adds a data package marker for tutorial resources.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +203 to +206
# Now we create a DataFrame X that contains all the features
X = pd.DataFrame(vra_features).join(pd.DataFrame(lc_features_g_series)
).join(pd.DataFrame(lc_features_g_series),
rsuffix='r_')
Comment on lines +154 to +156
def make_X(clean_data: pd.DataFrame,
fink_lc_features: list = fink_lc_features_to_keep
) -> pd.DataFrame:
Comment on lines +71 to +78
def run_sherlock(alert_data:pd.DataFrame):
# TODO: add description of what run_sherlock does for me.
"""Run Sherlock on the alert data processed by process_data."""

if "LASAIR_TOKEN" not in os.environ:
alert_data['sherl_class'] = np.nan
alert_data['sep_arcsec'] = np.nan
return alert_data
Comment on lines +16 to +18
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s.%(funcName)s: %(message)s")
Comment on lines +232 to +239
alerts_df = pd.DataFrame([data])
clean_df = raw2clean(alerts_df)
curated_df = run_sherlock(clean_df)
X, meta = make_X(curated_df)

result = X.iloc[0].to_dict()
result['candid'] = X.index[0]
result['objectId'] = meta.iloc[0]['objectId']
" })\n",
"\n",
"runs_df = pd.DataFrame(run_data)\n",
"display(runs_df.sort_values)\n"
Comment on lines +18 to +24
"In your terminal (or add to your `.bashrc`/`.zshrc`):\n",
"\n",
"```bash\n",
"export MLFLOW_TRACKING_USERNAME=\"your_username\"\n",
"export MLFLOW_TRACKING_PASSWORD=\"your_password\"\n",
"export MLFLOW_TRACKING_URI=\"https://mlflow-dev.fink-broker.org\"\n",
"```\n",
Comment on lines +42 to +52
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MLFLOW_TRACKING_USERNAME is NOT set - please set it before continuing!\n",
"MLFLOW_TRACKING_PASSWORD is NOT set - please set it before continuing!\n",
"MLFLOW_TRACKING_URI is NOT set - please set it before continuing!\n"
]
}
],
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Preprocessing dependencies (see the imports at the top of preprocessing.py)\ndependencies = [\"pandas\", \"numpy\", \"fink-client\", \"lasair\"]\ndependencies_path = \"requirements.txt\"\nwith open(dependencies_path, \"w\") as f:\n f.write(\"\\n\".join(dependencies) + \"\\n\")\n\nprint(f\"Dependencies written to {dependencies_path}:\")\nprint(open(dependencies_path).read())"
Comment on lines +125 to +126
"cell_type": "code",
"source": "print(\"Starting run on REMOTE server...\\n\")\n\nwith mlflow.start_run(run_name=f\"remote_LR_{PARAMS['learning_rate']}\"):\n\n # ==========================================\n # 1. TRAIN MODEL (same as before)\n # ==========================================\n print(\"Training model...\")\n model = HistGradientBoostingClassifier(**PARAMS)\n model.fit(X.values, y)\n y_pred = model.predict(X.values)\n print(\"Model trained!\\n\")\n\n # ==========================================\n # 2. LOG PARAMETERS (same as before)\n # ==========================================\n print(\"Logging parameters...\")\n mlflow.log_params(PARAMS)\n\n # ==========================================\n # 3. LOG MODEL (same as before)\n # ==========================================\n print(\"Logging model...\")\n signature = infer_signature(X, y_pred)\n mlflow.sklearn.log_model(\n model,\n name=\"model\",\n signature=signature,\n input_example=X.iloc[:1],\n )\n\n # ==========================================\n # 4. LOG METRICS (same as before)\n # ==========================================\n print(\"Logging metrics...\")\n mlflow.log_metric(\"accuracy\", accuracy_score(y, y_pred))\n mlflow.log_metric(\"precision\", precision_score(y, y_pred, zero_division=0))\n mlflow.log_metric(\"recall\", recall_score(y, y_pred, zero_division=0))\n mlflow.log_metric(\"f1_score\", f1_score(y, y_pred, zero_division=0))\n\n # ==========================================\n # 5. LOG DATA (optional - be selective!)\n # ==========================================\n print(\"Logging training data...\")\n mlflow.log_table(X, \"X_train.parquet\")\n mlflow.log_table(y, \"y_train.parquet\")\n\n # ==========================================\n # 6. LOG METADATA (same as before)\n # ==========================================\n print(\"Logging metadata...\")\n meta_info = {\n \"params\": PARAMS,\n \"data_info\": {\n \"n_samples\": X.shape[0],\n \"n_features\": X.shape[1]\n },\n \"notes\": \"Run sent from local to remote server\"\n }\n with open(\"meta.json\", \"w\") as f:\n json.dump(meta_info, f, indent=2)\n mlflow.log_artifact(\"meta.json\")\n\n # ==========================================\n # 7. LOG PREPROCESSING CODE — CRITICAL FOR REMOTE 🆕\n # ==========================================\n # The remote server (Fink) needs your preprocessing code to transform\n # new incoming data the same way you transformed your training data.\n print(\"Logging preprocessing code...\")\n mlflow.log_artifact(\"preprocessing.py\", artifact_path=\"code\")\n\n # ==========================================\n # 8. LOG REQUIREMENTS — CRITICAL FOR REMOTE 🆕\n # ==========================================\n # Tells the remote server which Python packages your preprocessing\n # code needs (MLflow handles the model's own dependencies automatically).\n print(\"Logging dependencies...\")\n mlflow.log_artifact(dependencies_path)\n\n print(\"\\nRun completed successfully on REMOTE server!\")\n print(f\"View at: {REMOTE_URI}\")",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants