Skip to content

Commit

Permalink
Merge branch 'master' of github.com:INCF/pybids into fix/metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgorgo committed Apr 29, 2017
2 parents 27808dc + 3631057 commit 47d8a26
Showing 1 changed file with 36 additions and 20 deletions.
56 changes: 36 additions & 20 deletions bids/grabbids/bids_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,28 @@ def get_metadata(self, path, **kwargs):

return merged_param_dict

def get_fieldmap(self, path):
def get_fieldmap(self, path, return_list=False):
fieldmaps = self._get_fieldmaps(path)

if return_list:
return fieldmaps
else:
if len(fieldmaps) == 1:
return fieldmaps[0]
elif len(fieldmaps) > 1:
raise ValueError("More than one fieldmap found, but the "
"'return_list' argument was set to False. "
"Either ensure that there is only one "
"fieldmap for this image, or set the "
"'return_list' argument to True and handle "
"the result as a list.")
else: # len(fieldmaps) == 0
return None

def _get_fieldmaps(self, path):
sub = os.path.split(path)[1].split("_")[0].split("sub-")[1]
fieldmap_set = {}
type_ = '(phase1|phase2|phasediff|epi|fieldmap)'
fieldmap_set = []
type_ = '(phase1|phasediff|epi|fieldmap)'
for file in self.get(subject=sub, type=type_,
extensions=['nii.gz', 'nii']):
metadata = self.get_metadata(file.filename)
Expand All @@ -59,34 +77,32 @@ def get_fieldmap(self, path):
else:
intended_for = [metadata["IntendedFor"]]
if any([path.endswith(suffix) for suffix in intended_for]):
cur_fieldmap = {}
if file.type == "phasediff":
fieldmap_set = {"phasediff": file.filename,
cur_fieldmap = {"phasediff": file.filename,
"magnitude1": file.filename.replace(
"phasediff", "magnitude1"),
"magnitude2": file.filename.replace(
"phasediff", "magnitude2"),
"type": "phasediff"}
break
elif file.type == "phase1":
fieldmap_set["phase1"] = file.filename
fieldmap_set["magnitude1"] = \
cur_fieldmap["phase1"] = file.filename
cur_fieldmap["magnitude1"] = \
file.filename.replace("phase1", "magnitude1")
fieldmap_set["type"] = "phase"
elif file.type == "phase2":
fieldmap_set["phase2"] = file.filename
fieldmap_set["magnitude2"] = \
file.filename.replace("phase2", "magnitude2")
fieldmap_set["type"] = "phase"
cur_fieldmap["phase2"] = \
file.filename.replace("phase1", "phase2")
cur_fieldmap["magnitude2"] = \
file.filename.replace("phase1", "magnitude2")
cur_fieldmap["type"] = "phase"
elif file.type == "epi":
if "epi" not in fieldmap_set.keys():
fieldmap_set["epi"] = []
fieldmap_set["epi"].append(file.filename)
fieldmap_set["type"] = "epi"
cur_fieldmap["epi"] = file.filename
cur_fieldmap["type"] = "epi"
elif file.type == "fieldmap":
fieldmap_set["fieldmap"] = file.filename
fieldmap_set["magnitude"] = \
cur_fieldmap["fieldmap"] = file.filename
cur_fieldmap["magnitude"] = \
file.filename.replace("fieldmap", "magnitude")
fieldmap_set["type"] = "fieldmap"
cur_fieldmap["type"] = "fieldmap"
fieldmap_set.append(cur_fieldmap)
return fieldmap_set

def find_match(self, target, source=None):
Expand Down

0 comments on commit 47d8a26

Please sign in to comment.