From 270ef94653db30ff0b02733816f755b0296f99da Mon Sep 17 00:00:00 2001 From: Dan Levitas Date: Thu, 25 Jul 2024 16:50:28 +0000 Subject: [PATCH] [ENH] Provide abridged ezBIDS information for telemetry --- handler/bids.sh | 4 ++ handler/ezBIDS_core/ezBIDS_core.py | 2 +- handler/telemetry.py | 84 ++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 handler/telemetry.py diff --git a/handler/bids.sh b/handler/bids.sh index dd44de6..79bec9b 100755 --- a/handler/bids.sh +++ b/handler/bids.sh @@ -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/ \ No newline at end of file diff --git a/handler/ezBIDS_core/ezBIDS_core.py b/handler/ezBIDS_core/ezBIDS_core.py index cbd5c4b..ed0f813 100755 --- a/handler/ezBIDS_core/ezBIDS_core.py +++ b/handler/ezBIDS_core/ezBIDS_core.py @@ -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")) diff --git a/handler/telemetry.py b/handler/telemetry.py new file mode 100755 index 0000000..5092250 --- /dev/null +++ b/handler/telemetry.py @@ -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)