# SmartVA-Analyze 1.1 accepts a csv file as input
# and expects a column for every field name in the "Guide for data entry.xlsx" spreadsheet

df = pd.DataFrame(index=[0], columns=cb.index.unique())

# SmartVA-Analyze 1.1 also requires a handful of columns that are not in the Guide
df['child_3_10'] = np.nan
df['agedays']    = np.nan
df['child_5_7e'] = np.nan
df['child_5_6e'] = np.nan
df['adult_2_9a'] = np.nan

df.loc[0,'sid'] = 'example'

# if we save this dataframe as a csv, we can run it through SmartVA-Analyze 1.1

fname = 'example_1.csv'
df.to_csv(fname, index=False)

# here are the results of running this example through SmartVA-Analyze 1.1
pd.read_csv('neonate-predictions.csv')

"""
Explanation: Minimal example
Generate a .csv file that is accepted as input to SmartVA-Analyze 1.1
End of explanation
"""


hypothetical_data = pd.DataFrame(index=range(5))

hypothetical_data['sex'] = ['M', 'M', 'F', 'M', 'F']
hypothetical_data['age'] = [35, 45, 75, 67, 91]

hypothetical_data['injury'] = ['rti', 'fall', '', '', '']
hypothetical_data['heart_disease'] = ['N', 'N', 'Y', 'Y', 'Y']
hypothetical_data['chest_pain'] = ['N', 'N', 'Y', 'N', '']

hypothetical_data

# SmartVA-Analyze 1.1 accepts a csv file as input
# and expects a column for every field name in the "Guide for data entry.xlsx" spreadsheet

df = pd.DataFrame(index=hypothetical_data.index, columns=cb.index.unique())

# SmartVA-Analyze 1.1 also requires a handful of columns that are not in the Guide
df['child_3_10'] = np.nan
df['agedays']    = np.nan
df['child_5_7e'] = np.nan
df['child_5_6e'] = np.nan
df['adult_2_9a'] = np.nan

# to find the coding of specific variables, look in the Guide, and 
# as necessary refer to the numbers in paper form for the PHMRC Shortened Questionnaire
# http://www.healthdata.org/sites/default/files/files/Tools/SmartVA/2015/PHMRC%20Shortened%20VAI_all-modules_2015.zip

# set id
df['sid'] = hypothetical_data.index

# set sex
df['gen_5_2'] = hypothetical_data['sex'].map({'M': '1', 'F': '2'})

# set age
df['gen_5_4'] = 1  # units are years
df['gen_5_4a'] = hypothetical_data['age'].astype(int)


# good place to save work and confirm that it runs through SmartVA
fname = 'example_2.csv'
df.to_csv(fname, index=False)

# here are the results of running this example
pd.read_csv('adult-predictions.csv')

# map injuries to appropriate codes
# suffered injury?
df['adult_5_1'] = hypothetical_data['injury'].map({'rti':'1', 'fall':'1', '':'0'})
# injury type
df['adult_5_2'] = hypothetical_data['injury'].map({'rti':'1', 'fall':'2'})

# _another_ good place to save work and confirm that it runs through SmartVA
fname = 'example_3.csv'
df.to_csv(fname, index=False)

# here are the results of running this example
pd.read_csv('adult-predictions.csv')

# map heart disease (to column adult_1_1i, see Guide)
df['adult_1_1i'] = hypothetical_data['heart_disease'].map({'Y':'1', 'N':'0'})

# map chest pain (to column adult_2_43, see Guide)
df['adult_2_43'] = hypothetical_data['chest_pain'].map({'Y':'1', 'N':'0', '':'9'})

# and that completes the work for a simple, hypothetical mapping
fname = 'example_4.csv'
df.to_csv(fname, index=False)

# have a look at the non-empty entries in the mapped database:
df.T.dropna()

# here are the results of running this example
pd.read_csv('adult-predictions.csv')

"""
Explanation: Example of simple, hypothetical mapping
If we have data on a set of verbal autopsies (VAs) that did not use the PHMRC Shortened Questionnaire, we must map them to the expected format.  This is a simple, hypothetical example for a set of VAs that asked only about injuries, hypertension, chest pain:
End of explanation
"""


