-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparse_json.py
executable file
·91 lines (64 loc) · 1.87 KB
/
parse_json.py
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
#!/usr/bin/env python
from os import environ, getcwd, chdir
environ['OPENBLAS_NUM_THREADS']='16'
from glob import glob
import json,sys
import pandas as pd
from os.path import abspath
import yaml
if len(sys.argv)<3 or sys.argv[1]=='-h' or sys.argv[1]=='--help':
print(f"""A command line program for making REDCap-like reports.
Usage:
{__file__} /path/to/event_var.yaml /path/to/Network/PHOENIX/PROTECTED
{__file__} /path/to/event_var.yaml /path/to/Network/PHOENIX/PROTECTED \"*/raw/*/surveys/*.Pronet.json\"
The last template is optional.
Sample yaml file:
screening_arm_1:
- chrguid_guid
- chrhealth_alle
baseline_arm_1:
- chrdemo_age_mos_chr
baseline_arm_2:
- chrdemo_age_mos_hc
""")
exit(0)
with open(sys.argv[1]) as f:
rows=yaml.safe_load(f)
dir_bak=getcwd()
chdir(sys.argv[2])
template='*/raw/*/surveys/*.Pronet.json'
if len(sys.argv)==4:
template=sys.argv[3]
files=glob(template)
L=len(files)
values={}
for event,vars in rows.items():
values[event]={}
for v in vars:
values[event][v]=['']*L
for i,file in enumerate(files):
with open(file) as f:
dict1=json.load(f)
print(file)
for event,vars in rows.items():
print('\t',event)
for v in vars:
print('\t\t',v)
for d in dict1:
if d['redcap_event_name']==event:
try:
values[event][v][i]=d['chric_record_id']+' '+d[v]
break
except:
print('\t','\033[0;31m Variable absent in json \033[0m')
print('\n')
print('\n\n')
for event,vars in rows.items():
for v in vars:
print('Report of',event,v)
print('=====================')
for i in range(L):
if values[event][v][i]:
print(values[event][v][i])
print('=====================\n')
chdir(dir_bak)