{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Writing profiles\n", "\n", "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/whylabs/whylogs/blob/1.0.x/python/examples/integrations/Writing_Profiles.ipynb)\n", "\n", "Hello there! If you've come to this tutorial, perhaps you are wondering what can you do after you generated your first (or maybe not the first) profile. Well, a good practice is to store these profiles as *lightweight* files, which is one of the cool features `whylogs` brings to the table.\n", "\n", "Here we will check different flavors of writing, and you can check which one of these will meet your current needs. Shall we? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating simple profiles\n", "\n", "In order for us to get started, let's take a very simple example dataset and profile it." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "data = {\n", " \"col_1\": [1.0, 2.2, 0.1, 1.2],\n", " \"col_2\": [\"some\", \"text\", \"column\", \"example\"],\n", " \"col_3\": [4, 2, 3, 5]\n", "}\n", "\n", "df = pd.DataFrame(data)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
col_1col_2col_3
01.0some4
12.2text2
20.1column3
31.2example5
\n", "
" ], "text/plain": [ " col_1 col_2 col_3\n", "0 1.0 some 4\n", "1 2.2 text 2\n", "2 0.1 column 3\n", "3 1.2 example 5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import whylogs as why\n", "\n", "profile_results = why.log(df)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "whylogs.api.logger.result_set.ProfileResultSet" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(profile_results)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now we can check its collected metrics by transforming it into a `DatasetProfileView`" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
counts/ncounts/nulltypes/integraltypes/fractionaltypes/booleantypes/stringtypes/objectcardinality/estcardinality/upper_1cardinality/lower_1...distribution/ndistribution/maxdistribution/mindistribution/q_10distribution/q_25distribution/mediandistribution/q_75distribution/q_90ints/maxints/min
column
col_240000404.04.00024.0...NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
col_140040004.04.00024.0...4.02.20.10.11.01.22.22.2NaNNaN
col_340400004.04.00024.0...4.05.02.02.03.04.05.05.05.02.0
\n", "

3 rows × 24 columns

\n", "
" ], "text/plain": [ " counts/n counts/null types/integral types/fractional \\\n", "column \n", "col_2 4 0 0 0 \n", "col_1 4 0 0 4 \n", "col_3 4 0 4 0 \n", "\n", " types/boolean types/string types/object cardinality/est \\\n", "column \n", "col_2 0 4 0 4.0 \n", "col_1 0 0 0 4.0 \n", "col_3 0 0 0 4.0 \n", "\n", " cardinality/upper_1 cardinality/lower_1 ... distribution/n \\\n", "column ... \n", "col_2 4.0002 4.0 ... NaN \n", "col_1 4.0002 4.0 ... 4.0 \n", "col_3 4.0002 4.0 ... 4.0 \n", "\n", " distribution/max distribution/min distribution/q_10 \\\n", "column \n", "col_2 NaN NaN NaN \n", "col_1 2.2 0.1 0.1 \n", "col_3 5.0 2.0 2.0 \n", "\n", " distribution/q_25 distribution/median distribution/q_75 \\\n", "column \n", "col_2 NaN NaN NaN \n", "col_1 1.0 1.2 2.2 \n", "col_3 3.0 4.0 5.0 \n", "\n", " distribution/q_90 ints/max ints/min \n", "column \n", "col_2 NaN NaN NaN \n", "col_1 2.2 NaN NaN \n", "col_3 5.0 5.0 2.0 \n", "\n", "[3 rows x 24 columns]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "profile_view = profile_results.view()\n", "profile_view.to_pandas()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cool! So now that we have a proper profile created, let's see how we can persist is as a file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Local writer\n", "\n", "The first and most straight-forward way of persisting a `whylogs` profile as a file is to write it directly to your disk. Our API makes it possible with the following commands. You could either write it from the `ProfileResultSet`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "profile_results.writer(\"local\").write(dest=\"my_profile.bin\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want, you can also not specify a `dest`, but an optional `base_dir`, which will write your profile with its timestamp to this base directory you want. Let's see how:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "profile_results.writer(\"local\").option(base_dir=\"my_directory\").write()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or from the `DatasetProfileView` directly, with a `path`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "profile_view.write(path=\"my_profile.bin\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And if it couldn't get any more convenient, you can also use the **same logic for logging** to also write your profile, like:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "why.write(profile=profile_view, base_dir=\"my_directory/my_profile.bin\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['profile_2022-05-26 20:04:07.484778.bin', 'my_profile.bin']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os \n", "os.listdir(\"./my_directory\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And that's it! Now you can go ahead and decide where and how to store these profiles, for further inspection and guaranteeing your data and ML model pipelines are generating useful and quality data for your end users." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## s3 Writer\n", "\n", "From an enterprise perspective, it can be interesting to use `s3` buckets to store your profiles instead of manually deciding what to do with them from your local machine. And that is why we have created an integration to do just that! \n", "\n", "To try to maintain this example simple enough, we won't use an actual cloud-based storage, but we will mock one with the `moto` library. This way, you can test this anywhere without worrying about credentials too much :) In order to keep `whylogs` as light as possible, and allow users to extend as they need, we have made `s3` an extra dependency.\n", "\n", "So let's get started by creating this mocked `s3` bucket.\n", "\n", "P.S.: if you haven't installed the `whylogs[s3]` extra dependency already, uncomment and run the cell below." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# ! pip install \"whylogs[s3]\"" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "s3.Bucket(name='my_great_bucket')" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import boto3\n", "from moto import mock_s3\n", "from moto.s3.responses import DEFAULT_REGION_NAME\n", "\n", "BUCKET_NAME = \"my_great_bucket\"\n", "\n", "\n", "mocks3 = mock_s3()\n", "mocks3.start()\n", "resource = boto3.resource(\"s3\", region_name=DEFAULT_REGION_NAME)\n", "resource.create_bucket(Bucket=BUCKET_NAME)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have created our `s3` bucket we will already be able to communicate with the mocked storage object. A good practice here is to declare your access credentials as environment variables. For a production setting, this won't be persisted into code, but this will give you a sense of how to safely use our `s3` writer." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import os \n", "\n", "os.environ[\"AWS_ACCESS_KEY_ID\"] = \"my_key_id\"\n", "os.environ[\"AWS_SECRET_ACCESS_KEY\"] = \"my_access_key\"" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "profile_results.writer(\"s3\").option(bucket_name=BUCKET_NAME).write()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And you've done it! Seems too good to be true. How would I know if the profiles are there? 🤔 Well, let's investigate them." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "s3_client = boto3.client(\"s3\")\n", "objects = s3_client.list_objects(Bucket=BUCKET_NAME)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'my_great_bucket'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objects.get(\"Name\", [])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'Key': 'profile_2022-05-26 20:04:07.484778.bin',\n", " 'LastModified': datetime.datetime(2022, 5, 26, 20, 4, 8, tzinfo=tzutc()),\n", " 'ETag': '\"ae976af8f52532e3ab58bfe089d2bc44\"',\n", " 'Size': 1115,\n", " 'StorageClass': 'STANDARD',\n", " 'Owner': {'DisplayName': 'webfile',\n", " 'ID': '75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a'}}]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objects.get(\"Contents\", [])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And there we have it, our local `s3` bucket has our profile written! \n", "If we want to put our profile into a special \"directory\" - often referred to as prefix - we can do the following instead:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "profile_results.writer(\"s3\").option(\n", " bucket_name=BUCKET_NAME, \n", " object_name=f\"my_prefix/somewhere/profile_{profile_view.creation_timestamp}.bin\"\n", " ).write()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'Key': 'my_prefix/somewhere/profile_2022-05-26 20:04:07.484778.bin',\n", " 'LastModified': datetime.datetime(2022, 5, 26, 20, 4, 8, tzinfo=tzutc()),\n", " 'ETag': '\"ae976af8f52532e3ab58bfe089d2bc44\"',\n", " 'Size': 1115,\n", " 'StorageClass': 'STANDARD',\n", " 'Owner': {'DisplayName': 'webfile',\n", " 'ID': '75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a'}},\n", " {'Key': 'profile_2022-05-26 20:04:07.484778.bin',\n", " 'LastModified': datetime.datetime(2022, 5, 26, 20, 4, 8, tzinfo=tzutc()),\n", " 'ETag': '\"ae976af8f52532e3ab58bfe089d2bc44\"',\n", " 'Size': 1115,\n", " 'StorageClass': 'STANDARD',\n", " 'Owner': {'DisplayName': 'webfile',\n", " 'ID': '75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a'}}]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "objects = s3_client.list_objects(Bucket=BUCKET_NAME)\n", "objects.get(\"Contents\", [])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Wrapping up the s3 objects\n", "\n", "Now let's close our connection to our mocked `s3` object." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "mocks3.stop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And that's it, you have just written a profile to an s3 bucket! If you want to check other integrations that we've made, please make sure to check out our [other examples](https://github.com/whylabs/whylogs/tree/1.0.x/python/examples) page.\n", "\n", "Hopefully this tutorial will help you get started to save your profiles and make sure to keep your Data and ML Pipelines always Robust and Responsible :)" ] } ], "metadata": { "interpreter": { "hash": "621bf3dad1a07b07b7449bd6c8cc04f495dddaf4dee6bfb51531763f476f51b9" }, "kernelspec": { "display_name": "Python 3.9.12 ('.venv': poetry)", "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.9.13" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }