{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import itertools \n", "import pandas as pd\n", "import numpy as np\n", "# from pandas_risk import *\n", "from time import time\n", "import os\n", "\n", "attr = ['gender','race','zip','year_of_birth']\n", "comb_attr = [\n", " ['zip' ,'gender', 'birth_datetime', 'race'], \n", " ['zip', 'gender', 'year_of_birth', 'race'], \n", " ['gender','race','zip'],\n", " ['race','year_of_birth','zip']\n", "]\n", " " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "SQL_CONTROLLED=\"SELECT * FROM deid_risk.basic_risk60k\"\n", "dfc = pd.read_gbq(SQL_CONTROLLED,private_key='/home/steve/dev/google-cloud-sdk/accounts/curation-test.json')\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def risk(**args):\n", " Yi = args['data']\n", " Yi = Yi.fillna(' ')\n", " sizes = args['prop'] if 'prop' in args else np.arange(5,100,5)\n", " FLAG = args['flag'] if 'flag' in args else 'UNFLAGGED'\n", " N = args['num_runs']\n", " if 'cols' in args :\n", " columns = args['cols']\n", " else:\n", " columns = list(set(Yi.columns.tolist()) - set(['person_id']))\n", " p = pd.DataFrame()\n", " y_i= pd.DataFrame({\"group_size\":Yi.groupby(columns,as_index=False).size()}).reset_index()\n", " for index in sizes :\n", " for n in np.repeat(index,N):\n", " \n", " # we will randomly sample n% rows from the dataset\n", " i = np.random.choice(Yi.shape[0],((Yi.shape[0] * n)/100),replace=False)\n", " x_i= pd.DataFrame(Yi).loc[i] \n", " risk = x_i.deid.risk(id='person_id',quasi_id = columns)\n", " x_i = pd.DataFrame({\"group_size\":x_i.groupby(columns,as_index=False).size()}).reset_index()\n", "\n", "\n", " r = pd.merge(x_i,y_i,on=columns,how='inner')\n", " if r.shape[0] == 0 :\n", " continue\n", " r['marketer'] = r.apply(lambda row: (row.group_size_x / np.float64(row.group_size_y)) /np.sum(x_i.group_size) ,axis=1)\n", " r['sample %'] = np.repeat(n,r.shape[0])\n", " r['tier'] = np.repeat(FLAG,r.shape[0])\n", " r['sample marketer'] = np.repeat(risk['marketer'].values[0],r.shape[0])\n", " # r['patient_count'] = np.repeat(r.shape[0],r.shape[0])\n", " r = r.groupby(['sample %','tier','sample marketer'],as_index=False).sum()[['sample %','marketer','sample marketer','tier']]\n", " p = p.append(r)\n", " p.index = np.arange(p.shape[0]).astype(np.int64)\n", " return p\n", " \n", " " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from pandas_risk import *\n", "o = pd.DataFrame()\n", "PATH=\"out/experiment-phase-2.xlsx\"\n", "writer = pd.ExcelWriter(PATH,engine='xlsxwriter')\n", "comb_attr = [\n", " ['zip' ,'gender', 'birth_datetime', 'race'], \n", " ['zip', 'gender', 'year_of_birth', 'race'], \n", " ['gender','race','zip'],\n", " ['race','year_of_birth','zip']\n", "]\n", "\n", "for cols in comb_attr :\n", " o = risk(data=dfc,cols=cols,flag='CONTROLLED',num_runs=5)\n", " #\n", " # adding the policy\n", " x = [1* dfc.columns.isin(cols) for i in range(o.shape[0])]\n", " o = o.join(pd.DataFrame(x,columns = dfc.columns))\n", " #\n", " # Write this to excel notebook\n", " o.to_excel(writer,\"-\".join(cols))\n", "# break\n", " \n", "\n", "# p = p.rename(columns={'marketer_x':'sample marketer'})\n", "# p.index = np.arange(p.shape[0]).astype(np.int64)\n", "\n", "writer.save()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | person_id | \n", "year_of_birth | \n", "month_of_birth | \n", "day_of_birth | \n", "birth_datetime | \n", "race_concept_id | \n", "ethnicity_concept_id | \n", "location_id | \n", "care_site_id | \n", "person_source_value | \n", "... | \n", "gender_source_concept_id | \n", "race_source_value | \n", "ethnicity_source_value | \n", "sex_at_birth | \n", "birth_date | \n", "race | \n", "zip | \n", "city | \n", "state | \n", "gender | \n", "
---|
0 rows × 21 columns
\n", "