<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 There are encounters have more than one diagnosis of DR.
Step1:  With the multi-diagnosis encounter records and group all the encounters into person file, there are 555 people have 2 diagnosis, 98 people have 3 and 10 people have 4. But we can't tell if the diagnosis comes with a series of encounters or just in one encounter.
Step2:  So I select the encounters that have only 1 diagnosis and ignore the multi-diagnosis situations. There are 449 people have more than 1 diagnosis, and these diagnosis must come with a encounter sequence. But the encounters I have ignored may influece the people amount here.
Step3:  The multi_diagnosis encounters may mislead our decision with 340 people, out of 663 people who have multiple diagnosis in total.
Step4:  There are overlap between the sets I extracted above. 
Among 3180 people who is extracted in the previous step to own only one diagnosis throughout their entire record, 127 also have multi-diagnosis encounters at the same time. Shall we delete the multi_diagnosis ones from their records? I don't think it is a good idea...
Step5:  87 people only have multi-diagnosis encounters, they have no single-diagnosis encounters.
Step6:  Among 449 people who is extracted in the pervious step to own a diagnosis sequence, 126 have multi-diagnosis encounters.
Step7:  My conclusion
Step8:  For people who have pure diagnosis sequence, how is their situation changing over time?
<ASSISTANT_TASK:>
Python Code:
from collections import Counter
Counter(df[['mNPDR', 'MNPDR', 'SNPDR', 'PDR']].sum(axis=1))
Explanation: There are encounters have more than one diagnosis of DR.
End of explanation
tmp={k:v[['mNPDR', 'MNPDR', 'SNPDR', 'PDR']].sum(axis=0) for k,v in df.groupby('Person_Nbr')}
tmp1 = {k:len(v[~v.isin([0])].index.tolist()) for k,v in tmp.items()}
Counter(tmp1.values())
Explanation: With the multi-diagnosis encounter records and group all the encounters into person file, there are 555 people have 2 diagnosis, 98 people have 3 and 10 people have 4. But we can't tell if the diagnosis comes with a series of encounters or just in one encounter.
End of explanation
df1=df[df[['mNPDR', 'MNPDR', 'SNPDR', 'PDR']].sum(axis=1).isin([1])]
temp={k:v[['mNPDR', 'MNPDR', 'SNPDR', 'PDR']].sum(axis=0) for k,v in df1.groupby('Person_Nbr')}
len(temp)
temp[863]
temp1 = {k:len(v[~v.isin([0])].index.tolist()) for k,v in temp.items()}
Counter(temp1.values())
Explanation: So I select the encounters that have only 1 diagnosis and ignore the multi-diagnosis situations. There are 449 people have more than 1 diagnosis, and these diagnosis must come with a encounter sequence. But the encounters I have ignored may influece the people amount here.
End of explanation
df2=df[df[['mNPDR', 'MNPDR', 'SNPDR', 'PDR']].sum(axis=1).isin([2,3])]
temp2={k:v[['mNPDR', 'MNPDR', 'SNPDR', 'PDR']].sum(axis=0) for k,v in df2.groupby('Person_Nbr')}
len(temp2)
temp2[863]
temp3 = {k:len(v[~v.isin([0])].index.tolist()) for k,v in temp2.items()}
Counter(temp3.values())
Explanation: The multi_diagnosis encounters may mislead our decision with 340 people, out of 663 people who have multiple diagnosis in total.
End of explanation
len(set([k for k,v in temp1.items() if v==1])&set(temp3.keys()))
Explanation: There are overlap between the sets I extracted above. 
Among 3180 people who is extracted in the previous step to own only one diagnosis throughout their entire record, 127 also have multi-diagnosis encounters at the same time. Shall we delete the multi_diagnosis ones from their records? I don't think it is a good idea...
End of explanation
len([k for k in temp3.keys() if k not in temp1.keys()])
Explanation: 87 people only have multi-diagnosis encounters, they have no single-diagnosis encounters.
End of explanation
len(set([k for k,v in temp1.items() if v>1]))
len([k1 for k1 in set([k for k,v in temp1.items() if v>1]) if k1 not in temp3.keys()])
len(set([k for k,v in temp1.items() if v>1])&set(temp3.keys()))
len([k1 for k1 in temp3.keys() if k1 not in set([k for k,v in temp1.items() if v>1])])
Explanation: Among 449 people who is extracted in the pervious step to own a diagnosis sequence, 126 have multi-diagnosis encounters.
End of explanation
# Example of pure diagnosis sequence
df[df.Person_Nbr.isin([639475])]
# Example of mixture of diagnosis sequence and multi-diagnosis encounters
df[df.Person_Nbr.isin([186925])]
# Example of people who have both multi-diagnosis encounters and single-diagnosis encounters
df[df.Person_Nbr.isin([138753])]
# Example of people who have only multi-diagnosis encounters
df[df.Person_Nbr.isin([476849])]
Explanation: My conclusion:
3716 people have DR diagnosis. 663 of 3716 have multiple DR diagnosis throughout their entire record
-323 of 663 have a pure diagnosis sequence.
(This is the amount of people we can definitely use to study the diagnosis changing.)
-126 of 663 have both diagnosis sequence and multi-diagnosis encounters
(With some deletion/cleaning, this amount of people could be used by our purpose.)
-214 of 663  have multi-diagnosis encounters
--127 of 214 have both multi-diagnosis encounters and single-diagnosis encounters
--87 people have only multi-diagnosis encounters
(These people can't be used in my view, they have no difference with the single-diagnosis patients.)
Of course all above may or may not have encounters without DR diagnosis
End of explanation
index = [k1 for k1 in set([k for k,v in temp1.items() if v>1]) if k1 not in temp3.keys()]
df3 = df[df[['mNPDR', 'MNPDR', 'SNPDR', 'PDR']].sum(axis=1).isin([0,1])]
df3 = df3[df3.Person_Nbr.isin(index)]
df3.head()
import pprint
d={}
for k,v in df3.groupby('Person_Nbr'):
    l=[]
    t = v.sort_values(by=['Enc_Date'], ascending=True)
    for k in t.index:
        t1=t.loc[k,]
        if len(t1[t1==True])>0:
            l.append({t.loc[k,'Enc_Date']: (t1[t1==True].index[0])})
        else:
            l.append({t.loc[k,'Enc_Date']: 'unknown'})
    d[k]=l
    
d.items()[0]
dictionary={'mNPDR':1, 'MNPDR':2, 'SNPDR':3, 'PDR':4}
def f(dic):
    l=[]
    t=[]
    for idx, v in enumerate(dic):
        t.append(v.keys()[0])
        if v.values()[0]=='unknown':
            if idx!=0:
                l.append(l[idx-1])
            else:
                l.append(0)
        else:
            l.append(dictionary[v.values()[0]])
    return (t,l)
import matplotlib.pyplot as plt
import matplotlib.dates 
i=0
for k,v in d.items():
    t,l = f(v)
    plt.plot(t,l)
    i+=1
    if i>9:
        break
plt.yticks(range(0,5), ['unknown', 'mNPDR', 'MNPDR', 'SNPDR', 'PDR'])
plt.ylabel('Severity of DR')
plt.xlabel('Year')
#plt.savefig('hhh.jpg')
plt.show()
Explanation: For people who have pure diagnosis sequence, how is their situation changing over time?
End of explanation
<END_TASK>