|
| 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