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

Update tmp.py

Browse files
Files changed (1) hide show
  1. tmp.py +90 -10
tmp.py CHANGED
@@ -1,14 +1,94 @@
1
- import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- # Sample DataFrame
4
- data = {'Date': [202205270000.0, '', 202206010000.0]}
5
 
6
- df = pd.DataFrame(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Define a lambda function to convert numeric date to string date
9
- convert_to_date = lambda x: pd.to_datetime(str(int(x)), format='%Y%m%d', errors='coerce').strftime('%Y-%m-%d') if x != '' else ''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Apply the lambda function to the DataFrame column
12
- df['Date'] = df['Date'].apply(convert_to_date)
13
-
14
- print(df)
 
 
 
 
 
1
+ def date_dict(df_list, pif_key):
2
+ encounter_dates = []
3
+ for df in df_list:
4
+ df['encounter_date'].fillna('', inplace=True)
5
+ df_date = df.loc[df['pif_key'].astype(str) == str(pif_key), 'encounter_date'].values
6
+ if len(df_date) > 0:
7
+ encounter_dates.extend(df_date)
8
+ if encounter_dates:
9
+ ingested_date = max(encounter_dates)
10
+ else:
11
+ ingested_date = ''
12
+ date_insert_dict = {
13
+ 'attribute_name': 'report_date',
14
+ 'attribute_method': 'cv',
15
+ 'attribute_normalized_prediction': '',
16
+ 'attribute_prediction': str(ingested_date),
17
+ 'attribute_version': 'v2_090523',
18
+ 'attribute_vocab': '',
19
+ 'attribute_code': '',
20
+ 'date_of_service': ''
21
+ }
22
+ return date_insert_dict, encounter_dates, ingested_date
23
 
 
 
24
 
25
+ def report_date_insertion(dict_list, df_list, logging_df):
26
+ col_names = {col['attribute_name'] for col in dict_list}
27
+ if 'report_date' not in col_names:
28
+ pif_key = next((col['attribute_prediction'] for col in dict_list if col['attribute_name'] == 'pif_key'), None)
29
+ if pif_key is not None:
30
+ date_insert_dict, encounter_dates, ingested_date = date_dict(df_list, pif_key)
31
+ dict_list.insert(1, date_insert_dict)
32
+ if ingested_date:
33
+ if len(encounter_dates)>1:
34
+ logging_df = logging_df.append({'pif_key': pif_key,
35
+ 'json_report_date_exists': False,
36
+ 'encounter_dates': encounter_dates,
37
+ 'ingested_date': ingested_date,
38
+ 'multiple_date': True, 'old_date': 'missing'}, ignore_index=True)
39
+ else:
40
+ logging_df = logging_df.append({'pif_key': pif_key,
41
+ 'json_report_date_exists': False,
42
+ 'encounter_dates': encounter_dates,
43
+ 'ingested_date': ingested_date,
44
+ 'multiple_date': False, 'old_date': 'missing'}, ignore_index=True)
45
+ else:
46
+ logging_df = logging_df.append({'pif_key': pif_key,
47
+ 'json_report_date_exists': False,
48
+ 'encounter_dates': None,
49
+ 'ingested_date': '',
50
+ 'multiple_date': False, 'old_date': 'missing'}, ignore_index=True)
51
 
52
+ elif 'report_date' in col_names:
53
+ pif_key = next((col['attribute_prediction'] for col in dict_list if col['attribute_name'] == 'pif_key'), None)
54
+ if 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
+ if len(encounter_dates)>1:
64
+ logging_df = logging_df.append({'pif_key': pif_key,
65
+ 'json_report_date_exists': True,
66
+ 'encounter_dates': encounter_dates,
67
+ 'ingested_date': ingested_date,
68
+ 'multiple_date': True, 'old_date': old_date}, ignore_index=True)
69
+ else:
70
+ logging_df = logging_df.append({'pif_key': pif_key,
71
+ 'json_report_date_exists': True,
72
+ 'encounter_dates': encounter_dates,
73
+ 'ingested_date': ingested_date,
74
+ 'multiple_date': False, 'old_date': old_date}, ignore_index=True)
75
+ else:
76
+ for report_date_idx, tm in enumerate(dict_list):
77
+ if tm['attribute_name']=='report_date':
78
+ old_date = tm['attribute_prediction']
79
+ break
80
+ logging_df = logging_df.append({'pif_key': pif_key,
81
+ 'json_report_date_exists': True,
82
+ 'encounter_dates': None,
83
+ 'ingested_date': '',
84
+ 'multiple_date': False, 'old_date': old_date}, ignore_index=True)
85
+ return dict_list, logging_df
86
 
87
+ def json_report_date_insertion(json_data, df_list, logging_df):
88
+ for biomarker_detail in json_data['patient_level']['biomarkers']['details']:
89
+ for attribute in biomarker_detail['attribute']:
90
+ attribute_details = attribute['attribute_details']
91
+ dict_list, logging_df = report_date_insertion(attribute_details, df_list, logging_df)
92
+ attribute['attribute_details'] = dict_list
93
+
94
+ return json_data, logging_df