[13b491]: / code / check_T2star_metadata.py

Download this file

134 lines (122 with data), 4.6 kB

  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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import glob
import os
import json
import argparse
import pandas as pd
def get_parser():
parser = argparse.ArgumentParser(description='Check if AcquisitionDuration, RepetitionTime, VolumeTiming, '
'SliceTiming are available in the JSON metadata for *_T2star.json ')
parser.add_argument('--input', help='Path to data-multi-subject')
parser.add_argument('--output', help='CSV output file.')
return parser
def fetch_acquisition_duration(fname_json):
"""
Fetch the value of AcquisitionDuration in the JSON file of the same basename as fname_nifti
Return: float: Acquisition duration in seconds
"""
# Open JSON file
with open(fname_json) as f:
dict_json = json.load(f)
if 'AcquisitionDuration' in dict_json:
return float(dict_json['AcquisitionDuration'])
else:
raise ReferenceError
def fetch_slice_timing(fname_json):
"""
Fetch the value of SliceTiming in the JSON file of the same basename as fname_nifti
Return: float: SliceTiming in seconds
"""
# Open JSON file
with open(fname_json) as f:
dict_json = json.load(f)
if 'SliceTiming' in dict_json:
return (dict_json['SliceTiming'])
else:
raise ReferenceError
def fetch_volume_timing(fname_json):
"""
Fetch the value of VolumeTiming in the JSON file of the same basename as fname_nifti
Return: float: VolumeTiming in seconds
"""
# Open JSON file
with open(fname_json) as f:
dict_json = json.load(f)
if 'VolumeTiming' in dict_json:
return (dict_json['VolumeTiming'])
else:
raise ReferenceError
def fetch_repetitiontime(fname_json):
"""
Fetch the value of RepetitionTime in the JSON file of the same basename as fname_nifti
Return: float: RepetitionTime in seconds
"""
# Open JSON file
with open(fname_json) as f:
dict_json = json.load(f)
if 'RepetitionTime' in dict_json:
return (dict_json['RepetitionTime'])
else:
raise ReferenceError
def fetch_acqparam(fname_json, acq_param):
"""
Fetch the value of Acquisition Parameter in the JSON file of the same basename as fname_nifti
Return: Acquisition Parameter
"""
# Open JSON file
with open(fname_json) as f:
dict_json = json.load(f)
if acq_param in dict_json:
return (dict_json[acq_param])
else:
raise ReferenceError
def main(argv=None):
parser = get_parser()
args = parser.parse_args(argv)
os.chdir(args.input)
rows=[]
for file in glob.glob("./*/*/*_T2star.json"):
print(file)
row = []
row.append((os.path.split(file)[1]).replace("_T2star.json",""))
try:
repetitiontime = fetch_repetitiontime(file)
row.append(repetitiontime)
except ReferenceError:
print("Field 'RepetitionTime' was not found in the JSON sidecar.")
row.append("N")
try:
acq_duration = fetch_acquisition_duration(file)
row.append(acq_duration)
except ReferenceError:
print("Field 'AcquisitionDuration' was not found in the JSON sidecar.")
row.append("N")
try:
rep_timing = fetch_volume_timing(file)
row.append(rep_timing)
except ReferenceError:
print("Field 'VolumeTiming' was not found in the JSON sidecar.")
row.append("N")
try:
slice_timing = fetch_slice_timing(file)
row.append(slice_timing)
except ReferenceError:
print("Field 'SliceTiming' was not found in the JSON sidecar.")
row.append("N")
try:
acq_matrix = fetch_acqparam(file,'AcquisitionMatrixPE')
row.append(acq_matrix)
except ReferenceError:
print("Field 'AcquisitionMatrixPE' was not found in the JSON sidecar.")
row.append("N")
try:
acq_matrix = fetch_acqparam(file, 'ReconMatrixPE')
row.append(acq_matrix)
except ReferenceError:
print("Field 'ReconMatrixPE' was not found in the JSON sidecar.")
row.append("N")
rows.append(row)
df = pd.DataFrame(rows, columns=["Subject", "RepetitionTime", "AcquisitionDuration", "VolumeTiming", "SliceTiming", "AcquisitionMatrixPE", "ReconMatrixPE"])
df = df.sort_values(by=['Subject'])
df.to_csv(args.output, index=False)
if __name__ == "__main__":
main()