jkushwaha commited on
Commit
6ad27e4
·
verified ·
1 Parent(s): c66e292

Create final_date_insertion_code.py

Browse files
Files changed (1) hide show
  1. final_date_insertion_code.py +79 -0
final_date_insertion_code.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+
3
+ def get_encounter_dates(df_list, pif_key):
4
+ encounter_dates = []
5
+ for df in df_list:
6
+ df['encounter_date'].fillna('', inplace=True)
7
+ df_date = df.loc[df['pif_key'].astype(str) == str(pif_key), 'encounter_date'].values
8
+ if len(df_date) > 0:
9
+ encounter_dates.extend(df_date)
10
+ return encounter_dates
11
+
12
+ def get_latest_date(encounter_dates):
13
+ if encounter_dates:
14
+ return max(encounter_dates)
15
+ return ''
16
+
17
+ def create_date_insert_dict(ingested_date):
18
+ return {
19
+ 'attribute_name': 'report_date',
20
+ 'attribute_method': 'cv',
21
+ 'attribute_normalized_prediction': '',
22
+ 'attribute_prediction': str(ingested_date),
23
+ 'attribute_version': 'v2_090523',
24
+ 'attribute_vocab': '',
25
+ 'attribute_code': '',
26
+ 'date_of_service': ''
27
+ }
28
+
29
+ def add_logging_entry(logging_df, pif_key, json_report_date_exists, encounter_dates, ingested_date, multiple_date, old_date):
30
+ logging_df = logging_df.append({
31
+ 'pif_key': pif_key,
32
+ 'json_report_date_exists': json_report_date_exists,
33
+ 'encounter_dates': encounter_dates,
34
+ 'ingested_date': ingested_date,
35
+ 'multiple_date': multiple_date,
36
+ 'old_date': old_date
37
+ }, ignore_index=True)
38
+ return logging_df
39
+
40
+ def date_dict(df_list, pif_key):
41
+ encounter_dates = get_encounter_dates(df_list, pif_key)
42
+ ingested_date = get_latest_date(encounter_dates)
43
+ return create_date_insert_dict(ingested_date), encounter_dates, ingested_date
44
+
45
+ def report_date_insertion(dict_list, df_list, logging_df):
46
+ col_names = {col['attribute_name'] for col in dict_list}
47
+ pif_key = next((col['attribute_prediction'] for col in dict_list if col['attribute_name'] == 'pif_key'), None)
48
+
49
+ if 'report_date' not in col_names and pif_key is not None:
50
+ date_insert_dict, encounter_dates, ingested_date = date_dict(df_list, pif_key)
51
+ dict_list.insert(1, date_insert_dict)
52
+ logging_df = add_logging_entry(logging_df, pif_key, False, encounter_dates, ingested_date, len(encounter_dates) > 1, 'missing' if not ingested_date else '')
53
+
54
+ elif 'report_date' in col_names and pif_key is not None:
55
+ date_insert_dict, encounter_dates, ingested_date = date_dict(df_list, pif_key)
56
+ if ingested_date:
57
+ for report_date_idx, tm in enumerate(dict_list):
58
+ if tm['attribute_name'] == 'report_date':
59
+ old_date = tm['attribute_prediction']
60
+ break
61
+ dict_list.pop(report_date_idx)
62
+ dict_list.insert(1, date_insert_dict)
63
+ logging_df = add_logging_entry(logging_df, pif_key, True, encounter_dates, ingested_date, len(encounter_dates) > 1, old_date)
64
+ else:
65
+ for report_date_idx, tm in enumerate(dict_list):
66
+ if tm['attribute_name'] == 'report_date':
67
+ old_date = tm['attribute_prediction']
68
+ break
69
+ logging_df = add_logging_entry(logging_df, pif_key, True, None, '', False, old_date)
70
+
71
+ return dict_list, logging_df
72
+
73
+ def json_report_date_insertion(json_data, df_list, logging_df):
74
+ for biomarker_detail in json_data['patient_level']['biomarkers']['details']:
75
+ for attribute in biomarker_detail['attribute']:
76
+ attribute_details = attribute['attribute_details']
77
+ attribute['attribute_details'], logging_df = report_date_insertion(attribute_details, df_list, logging_df)
78
+
79
+ return json_data, logging_df