Skip to content

Commit 270ef94

Browse files
author
Dan Levitas
committed
[ENH] Provide abridged ezBIDS information for telemetry
1 parent c2ca609 commit 270ef94

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

handler/bids.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ bids-validator --json "$rootDir" > $root/validator.json || true
3333
echo "Copying finalized.json file to ezBIDS_template.json"
3434
cp -r $root/finalized.json $root/ezBIDS_template.json
3535

36+
# Create telemetry-specific files
37+
echo "Creating ezBIDS telemetry files"
38+
./telemetry.py $root
39+
3640
# Telemetry (to hard-coded pet2bids server)
3741
curl -H 'Content-Type: application/json' -d @$root/validator.json -X POST http://52.87.154.236/telemetry/

handler/ezBIDS_core/ezBIDS_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ def correct_pe(pe_direction, ornt, correction):
788788
-------
789789
proper_pe_direction : string
790790
pe_direction, in "ijk" format.
791-
791+
792792
correction: boolean
793793
"""
794794
# axes = (("R", "L"), ("A", "P"), ("S", "I"))

handler/telemetry.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
The purpose of this code is to filter BIDS information (data type, suffix, and entity labels) discerned from the
5+
ezBIDS Core. This will be passed to the telemetry service, so that comparisons between what ezBIDS guessed vs the
6+
final product (with user modifications) can be made.
7+
8+
@author: dlevitas
9+
"""
10+
11+
import os
12+
import sys
13+
import json
14+
15+
# Begin
16+
DATA_DIR = sys.argv[1]
17+
os.chdir(DATA_DIR)
18+
19+
# Create telemetry information from the ezBIDS Core json (ezBIDS guesses) as the finalized json (including user edits)
20+
21+
22+
def gather_telemetry(dtype):
23+
if dtype == 'core':
24+
json_path = f'{DATA_DIR}/ezBIDS_core.json'
25+
output_file = "ezBIDS_core_telemetry.json"
26+
error_message = 'There is no ezBIDS_core.json file, indicating failure during the ezBIDS Core processing. \
27+
Please contact support for assistance'
28+
else:
29+
json_path = f'{DATA_DIR}/finalized.json'
30+
output_file = "ezBIDS_finalized_telemetry.json"
31+
error_message = 'There is no ezBIDS finalized file, indicating either failure during ezBIDS or inability to \
32+
finalize dataset. Please contact support for assistance'
33+
34+
if os.path.isfile(json_path):
35+
json_data = open(json_path)
36+
json_data = json.load(json_data, strict=False)
37+
38+
for obj in json_data['objects']:
39+
40+
subject_idx = obj['subject_idx']
41+
session_idx = obj['session_idx']
42+
series_idx = obj['series_idx']
43+
44+
idx = str(subject_idx) + str(session_idx) + str(series_idx)
45+
46+
seq_file_name = json_data['series'][series_idx]['nifti_path']
47+
48+
ezBIDS_type = json_data['series'][series_idx]['type']
49+
if '/' in ezBIDS_type:
50+
data_type = ezBIDS_type.split('/')[0]
51+
suffix = ezBIDS_type.split('/')[-1]
52+
else:
53+
data_type = 'exclude'
54+
suffix = 'exclude'
55+
56+
rationale = json_data['series'][series_idx]['message']
57+
58+
entities = json_data['series'][series_idx]['entities']
59+
known_entities = {}
60+
for entity_key in entities:
61+
if entities[entity_key] != '':
62+
entity_val = entities[entity_key]
63+
known_entities[entity_key] = entity_val
64+
65+
ezBIDS_telemetry_info_list.append([idx, seq_file_name, data_type, suffix, rationale, known_entities])
66+
67+
else:
68+
print(error_message)
69+
70+
if len(ezBIDS_telemetry_info_list) > 1:
71+
header = ezBIDS_telemetry_info_list[0]
72+
rows = ezBIDS_telemetry_info_list[1:]
73+
ezBIDS_telemetry_info_json = []
74+
for row in rows:
75+
ezBIDS_telemetry_info_json.append(dict(zip(header, row)))
76+
77+
with open(output_file, "w") as fp:
78+
json.dump(ezBIDS_telemetry_info_json, fp, indent=3)
79+
80+
81+
# Execute functionality
82+
for dtype in ['core', 'finalized']:
83+
ezBIDS_telemetry_info_list = [['idx', 'seq_file_name', 'data_type', 'suffix', 'rationale', 'known_entities']]
84+
gather_telemetry(dtype)

0 commit comments

Comments
 (0)