{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Local skore Project\n\nThis example shows how to use :class:`~skore.Project` in **local** mode: store\nreports on your machine and inspect them. A key point is that\n:meth:`~skore.Project.summarize` returns a :class:`~skore.project._summary.Summary`,\nwhich is a :class:`pandas.DataFrame`. In Jupyter you get an interactive widget, but\nyou can always inspect and filter the summary as a DataFrame if you prefer.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create a local project and store reports\n\nWe use a temporary directory as the workspace so the example is self-contained.\nIn practice you can omit ``workspace`` to use the default (e.g. a ``skore/``\ndirectory in your user cache).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pathlib import Path\nfrom tempfile import TemporaryDirectory\n\nfrom skore import Project\n\ntmp_dir = TemporaryDirectory()\ntmp_path = Path(tmp_dir.name)\nproject = Project(\"example-project\", workspace=tmp_path)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from sklearn.datasets import load_breast_cancer\nfrom sklearn.linear_model import LogisticRegression\nfrom skrub import tabular_pipeline\n\nX, y = load_breast_cancer(return_X_y=True, as_frame=True)\nestimator = tabular_pipeline(LogisticRegression(max_iter=1_000))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom sklearn.base import clone\nfrom skore import evaluate\n\nfor regularization in np.logspace(-7, 7, 31):\n    report = evaluate(\n        clone(estimator).set_params(logisticregression__C=regularization),\n        X,\n        y,\n        splitter=0.2,\n        pos_label=1,\n    )\n    project.put(f\"lr-regularization-{regularization:.1e}\", report)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Summarize: you get a DataFrame\n\n:meth:`~skore.Project.summarize` returns a :class:`~skore.project._summary.Summary`,\nwhich subclasses :class:`pandas.DataFrame`. In a Jupyter environment it renders\nan interactive parallel-coordinates widget by default.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "summary = project.summarize()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To see the normal DataFrame table instead of the widget (e.g. in scripts or\nwhen you prefer the table), wrap the summary in :class:`pandas.DataFrame`:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n\npandas_summary = pd.DataFrame(summary)\npandas_summary"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Basically, our summary contains metadata related to various information that we need\nto quickly help filtering the reports.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "summary.info()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Filter reports by metric (e.g. keep only those above a given accuracy) and\nwork with the result as a table.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "summary.query(\"log_loss < 0.1\")[\"key\"].tolist()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Use :meth:`~skore.project._summary.Summary.reports` to load the corresponding\nreports from the project (optionally after filtering the summary).\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "reports = summary.query(\"log_loss < 0.1\").reports(return_as=\"comparison\")\nlen(reports.reports_)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Since we got a :class:`~skore.ComparisonReport`, we can use the metrics accessor\nto summarize the metrics across the reports.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "reports.metrics.summarize().frame()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "reports.metrics.roc().plot(subplot_by=None)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "project.delete(\"example-project\", workspace=tmp_path)\ntmp_dir.cleanup()"
      ]
    }
  ],
  "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
}