In [1]:
import reader
data = reader.Data()
In [2]:
data['all_person_data'].recent_DR.value_counts()
Out[2]:
In [3]:
import datetime
In [4]:
d = data['all_encounter_data'][['Enc_Date', 'Person_Nbr']].copy()
d['Date'] = d.Enc_Date.map(lambda x: datetime.datetime(x.year, x.month, x.day).strftime('%Y-%m-%d'))
d['Enc_Nbr'] = d.index
d.head()
Out[4]:
In [5]:
d.shape[0]
Out[5]:
In [7]:
import pandas as pd
sno = pd.read_csv('snocodeTally.csv')
snocode = {k:list(v.Class)[0] for k,v in sno.groupby('SNOMED code')}
In [8]:
d1 = data['SNOMED_problem_list']
d1['Date'] = d1.Date_Created.map(lambda x: datetime.datetime(x.year, x.month, x.day).strftime('%Y-%m-%d'))
d1.head()
Out[8]:
In [9]:
d1['sno_diagnosis'] = d1.Concept_ID.map(lambda x: snocode.get(x, float('nan')))
d1 = d1[d1.sno_diagnosis.notnull()]
d1.shape[0]
Out[9]:
In [10]:
d2 = d.merge(d1, left_on = ['Person_Nbr', 'Date'], right_on = ['Person_Nbr','Date'], how = 'outer')
#d2 = d2.drop([], 1)
d2.head()
Out[10]:
In [11]:
d2.shape[0]
Out[11]:
In [33]:
d2['sno_diagnosis'] = d2.Concept_ID.map(lambda x: snocode.get(x, float('nan')))
temp = d2[d2.sno_diagnosis.notnull()]
temp[temp.Enc_Nbr.isnull()].head()
Out[33]:
In [34]:
print(temp[temp.Enc_Nbr.isnull()].shape[0]/float(temp.shape[0]))
print(temp[temp.Enc_Nbr.isnull()].shape[0])
print(temp.shape[0])
In [14]:
d3 = d2[d2.sno_diagnosis.notnull()][['Person_Nbr', 'Enc_Nbr', 'sno_diagnosis']].copy()
d3.head()
Out[14]:
In [15]:
d4 = data['all_encounter_data'][['Person_Nbr', 'DR_diagnosis', 'Enc_Date']].copy()
d4['Enc_Nbr'] = d4.index
mapping = {
'no_DR': 0,
'mNPDR': 1,
'MNPDR': 1,
'SNPDR': 2,
'PDR':2
}
d4['vision_class'] = d4.DR_diagnosis.map(lambda x: mapping.get(x))
d4.head()
Out[15]:
In [16]:
d4 = d4.merge(d3, left_on = ['Person_Nbr', 'Enc_Nbr'], right_on = ['Person_Nbr', 'Enc_Nbr'], how='left')
d4.head()
Out[16]:
In [17]:
temp = d4[d4.sno_diagnosis.notnull()]
print(temp[temp.vision_class!=temp.sno_diagnosis].shape[0]/float(temp.shape[0]))
print(temp[temp.vision_class!=temp.sno_diagnosis].shape[0])
print(temp.shape[0])
In [18]:
import math
d4.sno_diagnosis = d4.sno_diagnosis.map(lambda x: 0 if math.isnan(x) else x)
In [19]:
d5 = data['all_person_data'][['worst_DR','recent_DR']].copy()
d5['worst_vis_icd'] = d5.worst_DR.map(lambda x: mapping[x])
d5['recent_vis_icd'] = d5.recent_DR.map(lambda x: mapping[x])
In [20]:
d5['worst_vis_sno'] = d4.groupby('Person_Nbr')['sno_diagnosis'].max()
In [21]:
import numpy as np
def recent_DR(groupbyblock):
templist = groupbyblock.sort_values(['Enc_Date'],ascending=False)['sno_diagnosis'].values
temp = np.where(templist!=0)[0]
if len(temp) > 0:
return templist[temp[0]]
else:
return 0
d5['recent_vis_sno'] = d4.groupby('Person_Nbr').apply(lambda x: recent_DR(x))
In [22]:
d5.head()
Out[22]:
In [23]:
d5.shape[0]
Out[23]:
In [24]:
temp = d5[d5.worst_vis_icd==0]
temp[temp.worst_vis_icd!=temp.worst_vis_sno].shape[0]
Out[24]:
In [25]:
temp = d5[d5.recent_vis_icd==0]
temp[temp.recent_vis_icd!=temp.recent_vis_sno].shape[0]
Out[25]:
In [26]:
temp = d5[d5.worst_vis_icd!=0]
temp = temp[temp.worst_vis_sno!=0]
temp[temp.worst_vis_icd!=temp.worst_vis_sno].shape[0]
Out[26]:
In [27]:
temp = d5[d5.recent_vis_icd!=0]
temp = temp[temp.recent_vis_sno!=0]
temp[temp.recent_vis_icd!=temp.recent_vis_sno].shape[0]
Out[27]:
In [28]:
d5[d5.worst_vis_icd!=0].shape[0]
Out[28]:
In [29]:
d5[d5.recent_vis_icd!=0].shape[0]
Out[29]:
In [30]:
d5[d5.worst_vis_sno!=0].shape[0]
Out[30]:
In [31]:
d5[d5.recent_vis_sno!=0].shape[0]
Out[31]:
In [ ]: