in functions/source/job-creation/job_creation.py [0:0]
def get_table(dataframe):
"""
Generate the tabular data for the input manitest file.
Parameters
----------
dataframe : pd.DataFrame
One patient data from the csv.
Returns
-------
table : dictionary
key: encounter_date, value: {field_name: value}
Format
------
'table': {
'encounter_date': {
'field': 'value',
},
}
Note
----
Because of the payload limit of the input manifest file,
the table size is restricted (max_record_number)
"""
dataframe['collection_dt_tm'] = pd.to_datetime(
dataframe['collection_dt_tm'])
max_record_number = 11
# Sort by collection dates
dataframe.sort_values(['collection_dt_tm'], inplace=True)
if len(dataframe) > max_record_number:
dataframe = dataframe.loc[:max_record_number]
dataframe.loc[max_record_number, :] = ""
dataframe.loc[max_record_number, 'name_first'] = \
'Patient has too many collection dates,\
please check Cerner to for more details!'
# Generate a label for collection count, if there are multiple collections
# on the same day
count = 0
collection_date = ''
for index in dataframe.index:
if index < max_record_number - 1:
print(f'index :{index}')
print(dataframe['collection_dt_tm'])
if collection_date == dataframe.loc[index, 'collection_dt_tm']:
count += 1
else:
count = 1
collection_date = dataframe.loc[index, 'collection_dt_tm']
dataframe.loc[index, 'collection_count'] = \
'{collection_date}_{number}'.format(
collection_date=pd.to_datetime(dataframe.loc[
index, 'collection_dt_tm']).strftime('%Y-%m-%d'),
number=count,
)
else:
dataframe.loc[index, 'collection_count'] = "DateNotAvailable"
dataframe = dataframe.set_index('collection_count')
# Transform into a dictionary
table_dict = dataframe.astype(str).T.to_dict()
table = {}
for collection_date in table_dict:
table[collection_date] = []
for fieldname, humanname in get_table_fields():
try:
table[collection_date].append(
[humanname, table_dict[collection_date][fieldname]]
)
except KeyError:
pass
return table