{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# MLflow skore Project\n\nThis example shows how to persist reports in MLflow using :class:`~skore.Project`\nin ``mode=\"mlflow\"``: log reports as MLflow runs and inspect them. It uses a\n:class:`~skore.CrossValidationReport`, but the same approach applies to\n:class:`~skore.EstimatorReport`.\n\nTo run this example against your own MLflow tracking server, use:\n\n```bash\nTRACKING_URI=<tracking_uri> PROJECT=<project> python plot_skore_mlflow_project.py\n```\nTo try it locally, start an MLflow server with ``uvx mlflow server`` and set\n``TRACKING_URI=http://127.0.0.1:5000``.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First, let us build one report to persist:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.datasets import load_iris\nfrom sklearn.ensemble import HistGradientBoostingClassifier\nfrom skore import Project, evaluate\n\nX, y = load_iris(return_X_y=True, as_frame=True)\n\nestimator = HistGradientBoostingClassifier()\nreport = evaluate(estimator, X, y, splitter=5)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then, we can push the report to the MLflow backend:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import io\n\n\n# MLflow/Alembic emits verbose DB initialization logs; silence them so the\n# example page focuses on skore usage rather than backend startup details.\nwith redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()):\n    # This creates an MLflow experiment with name `PROJECT`:\n    project = Project(\n        PROJECT,\n        mode=\"mlflow\",\n        tracking_uri=TRACKING_URI,\n    )\nproject.put(\"hgb-baseline\", report)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Note that mlflow warns us about saving models with `pickle`. Future versions of skore\nmight rely on [skops](https://skops.readthedocs.io)_ for model serialization,\nwhich will make these warnings disappear.\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Like for other types of projects (local, hub), you can access the summary\nand its DataFrame version:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n\nsummary = project.summarize()\npandas_summary = pd.DataFrame(summary).reset_index()\npandas_summary[[\"id\", \"key\", \"report_type\", \"learner\", \"ml_task\", \"dataset\"]]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The \"id\" column corresponds to the MLflow run ID, so you can access the MLflow\nrun this way:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import mlflow\n\n(run_id,) = pandas_summary[\"id\"]\n\nmlflow_run = mlflow.get_run(run_id)\nmlflow_run.data.metrics"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "But most importantly, this ID lets you load saved reports:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "loaded_report = project.get(run_id)\nloaded_report.metrics.summarize().frame()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}