-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathform_creator.py
More file actions
90 lines (75 loc) · 3.33 KB
/
form_creator.py
File metadata and controls
90 lines (75 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import csv
def parse_set_net_form(csv_writer, csv_reader):
last_form_field = None
last_topic_field = None
n = 1
for row in csv_reader:
# print(row.keys())
topic_field = row.get('Topic').strip()
variable_field = row.get('Variable').strip()
response_field = row.get('Response options')
if len(variable_field) == 0:
continue
if variable_field == "Pregnancy ID":
answers = []
notes = row.get('Response options')
form_field = "All"
else:
answers = response_field.split('\n')
notes = ''
form_field = row.get('\ufeffForm').strip()
question_type = "TEXT"
if len(answers) > 1:
question_type = "MC"
if variable_field == "SET-NET pathogen/disease":
question_type = "MS"
else:
notes = response_field
if response_field == "MM/DD/YYYY" or response_field.lower() == 'date' or 'date of' in response_field:
question_type = 'DATE'
final_answers = []
final_answers_str = ''
if len(answers) > 1:
for a in answers:
low_a = a.lower()
if low_a == 'etc.' or low_a == 'etc' or low_a == 'text' or low_a == 'text_field' or a.strip == '':
continue
final_answers.append(a)
if len(final_answers) > 1:
final_answers_str = '"' + '","'.join(final_answers) + '"'
else:
notes = answers
else:
notes = answers
if not form_field or len(form_field) == 0:
form_field = last_form_field
if not topic_field or len(topic_field) == 0:
topic_field = last_topic_field
# group = '{}: {}'.format(form_field, topic_field)
group = form_field
if n == 17:
variable_field = 'Type 1 or type 2 diabetes (not gestational diabetes)'
out_row = [str(n), variable_field, final_answers_str, group, question_type, '', '',
'', '', '', '', '',
'', '', '', '', '',
'', '', notes]
csv_writer.writerow(out_row)
print(out_row)
last_form_field = form_field
last_topic_field = topic_field
n += 1
def convert_csv(input_file, output_file, form_type='set_net'):
# print(form_type)
with open(output_file, mode='w') as out_file:
csv_writer = csv.writer(out_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(["#", "question_name", "answers", "group", "type", "evidence_bundle", "DoneBy",
"feature_name", "fhir_resource_type", "code_system", "codes", "valueset_oid",
"nlp_task_type", "text_terms", "value_min", "value_max", "value_enum_set",
"value_extractor_ast", "logic", "notes"])
with open(input_file) as in_file:
csv_reader = csv.DictReader(in_file, delimiter=',')
if form_type == 'set_net':
parse_set_net_form(csv_writer, csv_reader)
if __name__ == "__main__":
convert_csv('/Users/charityhilton/Box/CDC_MotherBaby_TRANCHE2/set_net_variables.csv',
'/Users/charityhilton/Box/CDC_MotherBaby_TRANCHE2/set_net_form.csv')