# -*- coding: utf-8 -*-
"""
Created on Mon Aug 15 12:31:05 2016

@author: nn31
"""

import pandas as pd
import numpy as np
import pickle
import re
import scipy.stats


#read in cmmi data
cmmi = pd.read_csv("/Volumes/DCCRP_projects/CMMI-Pro62342/data/QDACT 05-03-2016.csv",
                   parse_dates=['AssessmentDate','AdmissionDate','DischargeDate','PalliativeDischargeDate'])
cmmi.sort_values(by=['internalid','AssessmentDate'],ascending=[1,1],inplace=True)    
                   
dd = pickle.load(open("/Users/nn31/Dropbox/40-githubRrepos/qdact-basic-analysis/notebooks/python_scripts/02_data_dictionary_dict.p", "rb" ))

#let's get a list of the symptom variables
variables = list(dd.keys())
variables.sort()
pattern = r'\b' + re.escape('ESAS')
symptoms = [variables[i] for i, x in enumerate(variables) if re.search(pattern, x)]

symptoms = ['ESASAnxiety', 'ESASAppetite', 'ESASConstipation', 'ESASDepression', 'ESASDrowsiness', 'ESASNausea', 'ESASPain', 'ESASShortnessOfBreath', 'ESASTiredness', 'ESASWellBeing']

#These variables all need to be transformed into a 5-level nominal variable
def createNominalVar(x):
    if x==0.0:
        return('None')
    elif ((x>0.0) & (x<=4.0)):
        return('Mild')
    elif ((x>=5.0) & (x<=7.0)):
        return('Moderate')
    elif ((x>=8.0) & (x<=10.0)):
        return('Severe')
    elif (x==994.0):
        return('Unable to Respond')
    else:
        return(np.nan)
        
t = cmmi['ESASAnxiety'].apply(createNominalVar)

for x in symptoms:
    cmmi[x+'_5level']=cmmi[x].apply(createNominalVar)


def cont(pandasDF,rowVar,colVar,label):
    grouped = pandasDF[[colVar,rowVar]].groupby(colVar)[rowVar]
    groupednm = pandasDF[[colVar,rowVar]].dropna().groupby(colVar)[rowVar]
    preSize = pd.DataFrame({'pre':grouped.size()})
    posSize = pd.DataFrame({'post':groupednm.size()})
    numbers = pd.merge(preSize,posSize,left_index=True,right_index=True)
    descriptors = groupednm.quantile([.25, .5, .75]).unstack()
    output = pd.merge(numbers,descriptors,left_index=True,right_index=True)
    values_per_group = [col for col_name, col in groupednm]
    results = scipy.stats.kruskal(*values_per_group)
    output['label'] = output[[0.5,0.25,0.75,'post']].apply(lambda x : str(x[0]) + 
    ' (' + str(x[1]) + '-' + str(x[2]) + ') n:' + str(x[3]), axis=1)
    final = pd.DataFrame({label:output['label']}).transpose()
    final['pvalue'] = results.pvalue
    return(final)

def cat(pandasDF,rowVar,colVar,label,displayRowText=None):
    cross = pd.crosstab(pandasDF[rowVar], pandasDF[colVar]).transpose()
    cross_pct = cross.apply(lambda r: r/r.sum(), axis=1)
    col_tot   = cross.apply(lambda r: r.sum(), axis=1)
    merge1 = pd.merge(cross,cross_pct,left_index=True,right_index=True)
    pval = scipy.stats.chi2_contingency(cross)[1]
    all_dat = pd.merge(merge1,pd.DataFrame({'tot':col_tot}),left_index=True,right_index=True)
    final=pd.DataFrame()
    for i,x in enumerate(cross.columns.tolist()):
        show=x
        temp = all_dat[[show + '_x',show + '_y','tot']]
        output = pd.DataFrame({label + show:temp.apply(lambda x: str("{0:.0f}".format(x[0])) + '/' + str("{0:.0f}".format(x[2])) + ' (' + "{0:.2f}%".format(x[1] * 100) + ')',axis=1 )}).transpose()
        output['pvalue'] = pval
        if i==0:
            final=output
        else:
            final=pd.concat([final,output])
    return(final)


#Need to set-to missing
cmmi['PrimaryDiagnosis_miss'] = cmmi['PrimaryDiagnosis'].apply(lambda x: np.nan if (x in [998,999]) else x)
internalIDGroup = cmmi.groupby('internalid')
fv = internalIDGroup.agg(lambda x: x.iloc[0])


  
x1 = cat(fv,'ESASAnxiety_5level',        'PrimaryDiagnosis_miss',label='Anxiety - ')
x2 = cat(fv,'ESASAppetite_5level',      'PrimaryDiagnosis_miss',label='Appetite - ')
x3 = cat(fv,'ESASConstipation_5level',  'PrimaryDiagnosis_miss',label='Constipation - ')
x4 = cat(fv,'ESASDepression_5level',    'PrimaryDiagnosis_miss',label='Depression - ')
x5 = cat(fv,'ESASDrowsiness_5level',    'PrimaryDiagnosis_miss',label='Drowsiness - ')
x6 = cat(fv,'ESASNausea_5level',        'PrimaryDiagnosis_miss',label='Nausea - ')
x7 = cat(fv,'ESASPain_5level',          'PrimaryDiagnosis_miss',label='Pain - ')
x8 = cat(fv,'ESASShortnessOfBreath_5level','PrimaryDiagnosis_miss',label='Shortness - ')
x9 = cat(fv,'ESASTiredness_5level',     'PrimaryDiagnosis_miss',label='Tiredness - ')
x10 = cat(fv,'ESASWellBeing_5level',     'PrimaryDiagnosis_miss',label='Well Being - ')
x11 = cont(fv,'PPSScore',          'PrimaryDiagnosis_miss',label='PPSScore - ')

frames = [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11]
table = pd.concat(frames)

counts = fv['PrimaryDiagnosis_miss'].value_counts(dropna=False)

#Now, let's change the column headers
new_header = table.columns.tolist()
for i,x in enumerate(new_header):
    if x in dd.get('PrimaryDiagnosis').get('codes'):
        N = counts.tolist()[counts.index.tolist().index(x)]
        indx = dd.get('PrimaryDiagnosis').get('codes').index(x)
        text = dd.get('PrimaryDiagnosis').get('codes_fmt')[indx] +  " N=(" + str(N) + ")"
        new_header[i] = text
table.columns = new_header

table.to_csv("/Users/nn31/Dropbox/40-githubRrepos/qdact-basic-analysis/notebooks/python_scripts/11_primarydiagnosis_tables_catv2.csv")   