whylabs/whylogs-python

View on GitHub
python/examples/integrations/Guest Session.ipynb

Summary

Maintainability
Test Coverage
{
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "%pip install 'whylogs>=1.5.0'"
      ],
      "metadata": {
        "id": "JmfTzxUYEXKd"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-MgRNCmLEVqW"
      },
      "outputs": [],
      "source": [
        "from typing import Any\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "\n",
        "def load_testing_data() -> pd.DataFrame:  # TODO remove after user testing\n",
        "    data = pd.read_csv(\"https://guest-session-testing-public.s3.us-west-2.amazonaws.com/adult_income_m.csv\")\n",
        "\n",
        "    def convert_random_values(value: Any) -> Any:\n",
        "        if isinstance(value, int) and np.random.random() < 1 / 100:\n",
        "            return str(value)\n",
        "        return value\n",
        "\n",
        "    data[\"capital-gain\"] = data[\"capital-loss\"].apply(convert_random_values)\n",
        "    data[\"capital-loss\"] = data[\"capital-loss\"].apply(convert_random_values)\n",
        "    return data\n",
        "\n",
        "\n",
        "df = load_testing_data()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1Fr5cb15EVqX",
        "outputId": "af93648e-2624-444c-e582-cb43fc093684"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Initializing session with config /home/anthony/.config/whylogs/config.ini\n",
            "\n",
            "✅ Using session type: WHYLABS_ANONYMOUS\n",
            " ⤷ session id: session-6LpLjnAE\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "<whylogs.api.whylabs.session.session.GuestSession at 0x7fc674559c70>"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import whylogs as why\n",
        "\n",
        "why.init(upload_on_log=True, allow_local=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "OBWMvrR1EVqY",
        "outputId": "cefe37ec-8a7a-4ba1-c742-0d2e8b1a6bac"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "✅ Aggregated 48842 rows into profile foo\n",
            "\n",
            "Visualize and explore this profile with one-click\n",
            "🔍 https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-zv5Qm5zwJw0XEzpo&sessionToken=session-6LpLjnAE\n"
          ]
        }
      ],
      "source": [
        "profile = why.log(df, name=\"foo\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "F6IQU_ErEVqZ",
        "outputId": "b3e96b05-4a88-4d5c-fbf2-86e8b46c6199"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "✅ Aggregated 48842 rows into profile \n",
            "\n",
            "Visualize and explore this profile with one-click\n",
            "🔍 https://hub.whylabsapp.com/resources/model-1/profiles?profile=1691712000000&sessionToken=session-6LpLjnAE\n"
          ]
        }
      ],
      "source": [
        "# Upload the same data as a batch profile by leaving out the name\n",
        "profile = why.log(df)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "QJravKnAEVqZ",
        "outputId": "ec5da4c3-f10e-43fa-fc76-1e62a955d0b3"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "✅ Aggregated 48842 lines into profile 'foo', 48842 lines into profile 'bar'\n",
            "\n",
            "Visualize and explore the profiles with one-click\n",
            "🔍 https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-aj7Q52Zszb0VhjeW&profile=ref-6awZJWQI347XFBgD&sessionToken=session-6LpLjnAE\n",
            "\n",
            "Or view each profile individually\n",
            " ⤷ https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-aj7Q52Zszb0VhjeW&sessionToken=session-6LpLjnAE\n",
            " ⤷ https://hub.whylabsapp.com/resources/model-1/profiles?profile=ref-6awZJWQI347XFBgD&sessionToken=session-6LpLjnAE\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "<whylogs.api.logger.result_set.ViewResultSet at 0x7fc5dc310d30>"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "why.log(multiple={'foo': df, 'bar': df})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3t_bDbpUEVqa"
      },
      "source": [
        "# Switch to an autheneticated session"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "RZFDwX51EVqb",
        "outputId": "22c44314-7a9e-4bb5-fe2d-77b579bb0200"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Initializing session with config /home/anthony/.config/whylogs/config.ini\n",
            "\n",
            "✅ Using session type: WHYLABS\n",
            " ⤷ org id: org-JpsdM6\n",
            " ⤷ api key: MPq7Hg002z\n",
            " ⤷ default dataset: model-62\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "<whylogs.api.whylabs.session.session.ApiKeySession at 0x7fc6765e76a0>"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "why.init(reinit=True, allow_anonymous=False, upload_on_log=True, whylabs_api_key=\"MPq7Hg002z.Na5VweqsJfu5ArGILjQTlGAyPyOhtOnEVEtqY2b5PXNGJLZLjHscT:org-JpsdM6\", default_dataset_id=\"model-62\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2VtoH4P2EVqb",
        "outputId": "07aa17bf-9789-497c-e17a-4e2c80a788cf"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "✅ Aggregated 48842 rows into profile real_dataset\n",
            "\n",
            "Visualize and explore this profile with one-click\n",
            "🔍 https://hub.whylabsapp.com/resources/model-62/profiles?profile=ref-WvU6X5tH0Nrkh4a3\n"
          ]
        }
      ],
      "source": [
        "profile = why.log(df, name=\"real_dataset\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PVoD08mKEVqc"
      },
      "source": [
        "## Or upload via the whylabs writer\n",
        "This will use the session for credentials as well, it just won't have all of the fancy output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ETiMfgW6EVqc",
        "outputId": "f33cd8d0-bc57-4619-b9fb-e93b7da63d5f"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[(True, 'log-KCaCKErR8Gi7TooV')]"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "profile.writer('whylabs').write()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "149THEmpEVqd",
        "outputId": "f705b31a-487a-47c9-cca0-7e25d865aaa6"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "[(True, 'ref-vdBRFKAO8y9J2C7M')]"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# as a reference profile\n",
        "profile.writer('whylabs').option(reference_profile_name=\"authenticated_ref\").write()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".venv",
      "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.10.11"
    },
    "orig_nbformat": 4,
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}