import datetime

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()

d.shape[0]

import pandas as pd
sno = pd.read_csv('snocodeTally.csv')
snocode = {k:list(v.Class)[0] for k,v in sno.groupby('SNOMED code')}

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()

d1['sno_diagnosis'] = d1.Concept_ID.map(lambda x: snocode.get(x, float('nan')))
d1 = d1[d1.sno_diagnosis.notnull()]
d1.shape[0]

"""
Explanation: Tried to merge the snomed_problem_list with all_encounter_data by date.
End of explanation
"""


d2 = d.merge(d1, left_on = ['Person_Nbr', 'Date'], right_on = ['Person_Nbr','Date'], how = 'outer')
#d2 = d2.drop([], 1)
d2.head()

d2.shape[0]

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()

"""
Explanation: But the problem is not every record in snomed table has a linked encounter number with just date as the key.
End of explanation
"""


print(temp[temp.Enc_Nbr.isnull()].shape[0]/float(temp.shape[0]))
print(temp[temp.Enc_Nbr.isnull()].shape[0])
print(temp.shape[0])

"""
Explanation: 65% snomed diagnosed records have no corresponding encounter data.
End of explanation
"""


d3 = d2[d2.sno_diagnosis.notnull()][['Person_Nbr', 'Enc_Nbr', 'sno_diagnosis']].copy()
d3.head()

"""
Explanation: Extract the encounters that are diagnosed as no_DR, no_vision_threatening and vision_threatening with SNOMED codes
End of explanation
"""


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()

"""
Explanation: Transfer the original DR-diagnosis to vision-related class
End of explanation
"""


d4 = d4.merge(d3, left_on = ['Person_Nbr', 'Enc_Nbr'], right_on = ['Person_Nbr', 'Enc_Nbr'], how='left')
d4.head()

"""
Explanation: Merge snomed-diagnosed encounters to the original encoutners by Person_Nbr and Enc_Nbr
End of explanation
"""


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])

"""
Explanation: 14% encounters that have both records got a different diagnosis
End of explanation
"""


import math
d4.sno_diagnosis = d4.sno_diagnosis.map(lambda x: 0 if math.isnan(x) else x)

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])

d5['worst_vis_sno'] = d4.groupby('Person_Nbr')['sno_diagnosis'].max()

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))

d5.head()

d5.shape[0]

"""
Explanation: Transfer the encounter-wise profile to person-wise profile
Map all the NaN value of snomed diagnosis in encounter profile to 0
End of explanation
"""


temp = d5[d5.worst_vis_icd==0]
temp[temp.worst_vis_icd!=temp.worst_vis_sno].shape[0]

temp = d5[d5.recent_vis_icd==0]
temp[temp.recent_vis_icd!=temp.recent_vis_sno].shape[0]

"""
Explanation: 101 patients that diagnosed as no_DR got a different diagnosis with snomed (in both worst and recent case)
End of explanation
"""


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]

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]

"""
Explanation: Less than 100 patients diagnosed as some kind of vision class got a different diagnosis with snomed
End of explanation
"""


d5[d5.worst_vis_icd!=0].shape[0]

d5[d5.recent_vis_icd!=0].shape[0]

d5[d5.worst_vis_sno!=0].shape[0]

d5[d5.recent_vis_sno!=0].shape[0]

"""
Explanation: With ICD we had in total 4030 patients with DR, and with SNOMED we had in total 1053 patients with DR
End of explanation
"""


