This repository was archived by the owner on Aug 21, 2018. It is now read-only.
forked from jlgraner/bxh2bids
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbxh_pick_fields.py
More file actions
101 lines (86 loc) · 4.11 KB
/
bxh_pick_fields.py
File metadata and controls
101 lines (86 loc) · 4.11 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
import os
import logging
import json, xmltodict
#This is meant to pull information from a bxh file and put it into a python
#dictionary that can then be written out into a BIDS image sidecar json file.
#
#In order to do this, there needs to be a link between the fields expected
#in the BIDS sidecar and the information in the bxh file. This link is in the
#form of the passed "json_file". This file must look like:
#
#{
# "ManufacturersModelName": ["bxh", "acquisitiondata", "scannermodelname"],
# "MagneticFieldStrength": ["bxh", "acquisitiondata", "magneticfield"],
# "Manufacturer": ["bxh", "acquisitiondata", "scannermanufacturer"],
# "DeviceSerialNumber": ["bxh", "acquisitiondata", "scannerserialnumber"],
# "SoftwareVersions": ["bxh", "acquisitiondata", "softwareversions"],
# "ReceiveCoilName": ["bxh", "acquisitiondata", "receivecoilname"],
# "PulseSequenceType": ["bxh", "acquisitiondata", "description"],
# "EffectiveEchoSpacing": ["bxh", "acquisitiondata", "echospacing"],
# "EchoTime": ["bxh", "acquisitiondata", "te"],
# "InversionTime": ["bxh", "acquisitiondata", "ti"],
# "FlipAngle": ["bxh", "acquisitiondata", "flipangle"],
# "InstitutionName": ["bxh", "acquisitiondata", "institution"]
# }
#
#That is, each field name is the field name of the item expected in the BIDS
#sidecare file and the values are lists of keys that will locate the associated
#values in a bxh file.
#
#bxh_pick() can receive this link in one of two formats:
# 1) The full path/name of a bxh file, which it will then read in as a dictionary
# using xmltodict (NOTE: bxh files are in XML format).
# 2) A python dictionary of the contents of the bxh file.
def bxh_pick(json_file, bxh_file=None, bxh_as_dict=None):
logging.info('-STARTING: bxh_pick-')
if bxh_file is not None:
logging.info('Trying to load passed bxh file.')
if not os.path.exists(bxh_file):
logging.error('Passed bxh file cannot be found!')
logging.error('Looked for file: '+str(bxh_file))
raise RuntimeError('Passed bxh file could not be found.')
with open(bxh_file) as fd:
bxh_dict = xmltodict.parse(fd.read())
else:
if bxh_as_dict is None:
raise RuntimeError('Both bxh_file and bxh_as_dict are None!')
bxh_dict = bxh_as_dict
out_dict = {}
#Check to make sure the passed file exists
if not os.path.exists(json_file):
logging.error('Cannot find passed json file!')
raise RuntimeError('Looked for and could not find: '+str(json_file))
with open(json_file) as fd:
template = json.loads(fd.read())
#template will be a dictionary in which the keys are
#field names to be written to a BIDS sidecar json file
#and the values are the field names in which the corresponding
#values are saved in the bxh file.
for key in template.keys():
try:
this_item = bxh_dict
bxh_field_location = template[key]['location']
bids_style = template[key]['BIDSstyle']
#Go through the elements in the list of bxh fields and keep "digging"
#until we get to the final value.
for element in bxh_field_location:
this_item = this_item[element]
#The keys in the input json file need to be the same as those expected
#in the BIDS sidecar file.
if bids_style == 'string':
out_dict[key] = str(this_item)
elif bids_style == 'float':
out_dict[key] = float(this_item)
elif bids_style == 'int':
out_dict[key] = int(this_item)
else:
logging.warning('Unkown BIDS type found in field link json!')
logging.warning('File: '+str(json_file))
logging.warning('Item Key: '+str(key))
logging.warning('BIDSstyle: '+str(bids_style))
logging.warning('Just using original type!')
out_dict[key] = this_item
except KeyError:
logging.info('Field not found in bxh: '+str(key))
logging.info('-FINISHED: bxh_pick-')
return out_dict