whylabs/whylogs-python

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

Summary

Maintainability
Test Coverage
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "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": 2,
   "metadata": {},
   "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()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "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": 4,
   "metadata": {},
   "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": 5,
   "metadata": {},
   "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": {},
   "source": [
    "# Switch to an autheneticated session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "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, whylabs_api_key=\"MPq7Hg002z.Na5VweqsJfu5ArGILjQTlGAyPyOhtOnEVEtqY2b5PXNGJLZLjHscT:org-JpsdM6\", default_dataset_id=\"model-62\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "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": {},
   "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": 8,
   "metadata": {},
   "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": 9,
   "metadata": {},
   "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
 },
 "nbformat": 4,
 "nbformat_minor": 2
}