Skip to content

Commit

Permalink
[ENH] Provide abridged ezBIDS information for telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Levitas committed Jul 25, 2024
1 parent c2ca609 commit 270ef94
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
4 changes: 4 additions & 0 deletions handler/bids.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ bids-validator --json "$rootDir" > $root/validator.json || true
echo "Copying finalized.json file to ezBIDS_template.json"
cp -r $root/finalized.json $root/ezBIDS_template.json

# Create telemetry-specific files
echo "Creating ezBIDS telemetry files"
./telemetry.py $root

# Telemetry (to hard-coded pet2bids server)
curl -H 'Content-Type: application/json' -d @$root/validator.json -X POST http://52.87.154.236/telemetry/
2 changes: 1 addition & 1 deletion handler/ezBIDS_core/ezBIDS_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ def correct_pe(pe_direction, ornt, correction):
-------
proper_pe_direction : string
pe_direction, in "ijk" format.
correction: boolean
"""
# axes = (("R", "L"), ("A", "P"), ("S", "I"))
Expand Down
84 changes: 84 additions & 0 deletions handler/telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python3

"""
The purpose of this code is to filter BIDS information (data type, suffix, and entity labels) discerned from the
ezBIDS Core. This will be passed to the telemetry service, so that comparisons between what ezBIDS guessed vs the
final product (with user modifications) can be made.
@author: dlevitas
"""

import os
import sys
import json

# Begin
DATA_DIR = sys.argv[1]
os.chdir(DATA_DIR)

# Create telemetry information from the ezBIDS Core json (ezBIDS guesses) as the finalized json (including user edits)


def gather_telemetry(dtype):
if dtype == 'core':
json_path = f'{DATA_DIR}/ezBIDS_core.json'
output_file = "ezBIDS_core_telemetry.json"
error_message = 'There is no ezBIDS_core.json file, indicating failure during the ezBIDS Core processing. \
Please contact support for assistance'
else:
json_path = f'{DATA_DIR}/finalized.json'
output_file = "ezBIDS_finalized_telemetry.json"
error_message = 'There is no ezBIDS finalized file, indicating either failure during ezBIDS or inability to \
finalize dataset. Please contact support for assistance'

if os.path.isfile(json_path):
json_data = open(json_path)
json_data = json.load(json_data, strict=False)

for obj in json_data['objects']:

subject_idx = obj['subject_idx']
session_idx = obj['session_idx']
series_idx = obj['series_idx']

idx = str(subject_idx) + str(session_idx) + str(series_idx)

seq_file_name = json_data['series'][series_idx]['nifti_path']

ezBIDS_type = json_data['series'][series_idx]['type']
if '/' in ezBIDS_type:
data_type = ezBIDS_type.split('/')[0]
suffix = ezBIDS_type.split('/')[-1]
else:
data_type = 'exclude'
suffix = 'exclude'

rationale = json_data['series'][series_idx]['message']

entities = json_data['series'][series_idx]['entities']
known_entities = {}
for entity_key in entities:
if entities[entity_key] != '':
entity_val = entities[entity_key]
known_entities[entity_key] = entity_val

ezBIDS_telemetry_info_list.append([idx, seq_file_name, data_type, suffix, rationale, known_entities])

else:
print(error_message)

if len(ezBIDS_telemetry_info_list) > 1:
header = ezBIDS_telemetry_info_list[0]
rows = ezBIDS_telemetry_info_list[1:]
ezBIDS_telemetry_info_json = []
for row in rows:
ezBIDS_telemetry_info_json.append(dict(zip(header, row)))

with open(output_file, "w") as fp:
json.dump(ezBIDS_telemetry_info_json, fp, indent=3)


# Execute functionality
for dtype in ['core', 'finalized']:
ezBIDS_telemetry_info_list = [['idx', 'seq_file_name', 'data_type', 'suffix', 'rationale', 'known_entities']]
gather_telemetry(dtype)

0 comments on commit 270ef94

Please sign in to comment.