8000 WIP: adding Philips XML/REC support by grlee77 · Pull Request #683 · nipy/nibabel · GitHub
[go: up one dir, main page]

Skip to content

WIP: adding Philips XML/REC support #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix bug in conversion of Boolean entries from string such as 'N' and 'Y'
prior to this commit, both would get stored as true because bool('N') == True
  • Loading branch information
grlee77 committed Oct 22, 2018
commit 4068548b786c88528033e658edf63db6ba1d9fa2
25 changes: 20 additions & 5 deletions nibabel/xmlrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ class XMLRECError(Exception):

# Dict for converting XML/REC types to appropriate python types
# could convert the Enumeration strings to int, but enums_dict is incomplete

def _xml_str_to_bool(s):
if isinstance(s, bytes):
s = s.decode()
if s == 'Y 8000 ':
return True
else:
return False

# and the strings are more easily interpretable
xml_type_dict = {'Float': np.float32,
'Double': np.float64,
Expand Down Expand Up @@ -62,12 +71,15 @@ def _process_gen_dict_xml(xml_root):
a = e.attrib
if 'Name' in a:
entry_type = xml_type_dict[a['Type']]
if entry_type in ['S16', 'S32', 'S128']:
entry_type = str
if entry_type == bool:
# convert string such as 'N' or 'Y' to boolean
cast = _xml_str_to_bool
else:
cast = entry_type
if 'ArraySize' in a:
val = [entry_type(i) for i in e.text.strip().split()]
val = [cast(i) for i in e.text.strip().split()]
else:
val = entry_type(e.text)
val = cast(e.text)
general_info[a['Name']] = val
return general_info

Expand Down Expand Up @@ -166,6 +178,9 @@ def _get_val(entry_dtype, text):
val = text[:32]
elif entry_dtype == '|S128':
val = text[:128]
elif entry_dtype == bool:
# convert string 'Y' or 'N' to boolean
val = _xml_str_to_bool(text)
else:
val = entry_dtype(text)
return val
Expand Down Expand Up @@ -371,7 +386,7 @@ def get_bvals_bvecs(self):
Array of b vectors, shape (n_directions, 3), or None if not a
diffusion acquisition.
"""
if self.general_info['Diffusion'] == 0:
if not self.general_info['Diffusion']:
return None, None
reorder = self.get_sorted_slice_indices()
if len(self.get_data_shape()) == 3:
Expand Down
0