From 36a3d42c767d61c8424717dc5ee5adac4ec9f41d Mon Sep 17 00:00:00 2001 From: Chris Gorgolewski Date: Wed, 26 Apr 2017 18:13:41 -0700 Subject: [PATCH 1/3] account for the situation where multiple fieldmaps are present --- bids/grabbids/bids_layout.py | 44 ++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/bids/grabbids/bids_layout.py b/bids/grabbids/bids_layout.py index 9123280ec..3e17a940d 100644 --- a/bids/grabbids/bids_layout.py +++ b/bids/grabbids/bids_layout.py @@ -47,9 +47,17 @@ def get_metadata(self, path, **kwargs): return merged_param_dict def get_fieldmap(self, path): + fieldmaps = self.get_fieldmaps(path) + + if len(fieldmaps) == 1: + return fieldmaps[0] + else: + return fieldmaps + + 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) @@ -59,34 +67,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): From 137730157a15740503cad2a62fee520b4d41b0f2 Mon Sep 17 00:00:00 2001 From: Chris Gorgolewski Date: Thu, 27 Apr 2017 14:14:38 -0700 Subject: [PATCH 2/3] exceptional commit --- bids/grabbids/bids_layout.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/bids/grabbids/bids_layout.py b/bids/grabbids/bids_layout.py index 3e17a940d..78ef471ab 100644 --- a/bids/grabbids/bids_layout.py +++ b/bids/grabbids/bids_layout.py @@ -46,15 +46,24 @@ def get_metadata(self, path, **kwargs): return merged_param_dict - def get_fieldmap(self, path): - fieldmaps = self.get_fieldmaps(path) + def get_fieldmap(self, path, all=False): + fieldmaps = self._get_fieldmaps(path) - if len(fieldmaps) == 1: - return fieldmaps[0] - else: + if all: return fieldmaps - - def get_fieldmaps(self, path): + else: + if len(fieldmaps) == 1: + return fieldmaps[0] + elif len(fieldmaps) > 1: + raise ValueError("More than one fieldmap found, but 'all' " + "argument was set to False. Either ensure that " + "there is only one fieldmap for this image, " + "or set the 'all' argument to True and handle " + "the result as a list.") + else: # len == 0 + return fieldmaps + + def _get_fieldmaps(self, path): sub = os.path.split(path)[1].split("_")[0].split("sub-")[1] fieldmap_set = [] type_ = '(phase1|phasediff|epi|fieldmap)' From 9c0ad452562ecfd4900c3eda18438ff739cbdf56 Mon Sep 17 00:00:00 2001 From: Chris Gorgolewski Date: Thu, 27 Apr 2017 14:33:55 -0700 Subject: [PATCH 3/3] all encompassing fix for the exceptional commit --- bids/grabbids/bids_layout.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/bids/grabbids/bids_layout.py b/bids/grabbids/bids_layout.py index 78ef471ab..b175bc0b7 100644 --- a/bids/grabbids/bids_layout.py +++ b/bids/grabbids/bids_layout.py @@ -46,22 +46,23 @@ def get_metadata(self, path, **kwargs): return merged_param_dict - def get_fieldmap(self, path, all=False): + def get_fieldmap(self, path, return_list=False): fieldmaps = self._get_fieldmaps(path) - if all: + 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 'all' " - "argument was set to False. Either ensure that " - "there is only one fieldmap for this image, " - "or set the 'all' argument to True and handle " + 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 == 0 - return fieldmaps + else: # len(fieldmaps) == 0 + return None def _get_fieldmaps(self, path): sub = os.path.split(path)[1].split("_")[0].split("sub-")[1]