{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Store and retrieve reports on Skore Hub\n\nThis example shows how to use :class:`~skore.Project` in **hub** mode: store\nreports remotely 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\n## Examples\n\nTo run this example and push in your own Skore Hub workspace and project, you can run\nthis example with the following command:\n\n```bash\nWORKSPACE=<workspace> PROJECT=<project> python plot_skore_hub_project.py\n```\nIn this gallery, we are going to push the different reports into a public\nworkspace.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "`skore` can communicate with Skore Hub which serves two main purposes: storing and\nretrieving any reports that you created and a user-friendly interface for you to\nexplore and compare models.\n\nFirst, we need to login to Skore Hub such that later we can push our reports to it.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from skore import login\n\nlogin(mode=\"hub\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To illustrate the integration with Skore Hub, we use a binary classification task\nwhere the goal is to predict whether a patient has a tumor or not.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport skrub\nfrom sklearn.datasets import load_breast_cancer\n\nX, y = load_breast_cancer(return_X_y=True, as_frame=True)\nlabels = np.array([\"no tumor\", \"tumor\"], dtype=object)\ny = labels[y]\nskrub.TableReport(X)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Store reports on Skore Hub\n\nOn this problem, we use a logistic regression classifier with skrub's\n:func:`~skrub.tabular_pipeline` to preprocess the data if needed.\n\nTo send several reports to Skore Hub, we send models with different regularization\nparameters.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from numpy import logspace\nfrom sklearn.linear_model import LogisticRegression\nfrom skore import Project, evaluate\n\nproject = Project(f\"{WORKSPACE}/{PROJECT}\", mode=\"hub\")\n\nfor regularization in logspace(-3, 3, 5):\n    project.put(\n        f\"lr-regularization-{regularization:.1e}\",\n        evaluate(\n            skrub.tabular_pipeline(LogisticRegression(C=regularization)),\n            X,\n            y,\n            splitter=0.2,\n            pos_label=\"tumor\",\n        ),\n    )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Retrieve report stored on Skore Hub\n\nRetrieving a report on Skore Hub is similar to retrieving a report in local mode.\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.2\")[\"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.2\").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": "markdown",
      "metadata": {},
      "source": [
        "### Conclusion\n\nSkore Hub provides a user-friendly interface for you to explore and compare models.\nYou can easily store reports created using Skore.\n\n"
      ]
    }
  ],
  "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
}