Skip to content

Commit

Permalink
Merge pull request #1331 from FCP-INDI/develop
Browse files Browse the repository at this point in the history
Merge 1.7.0-development branch into Master
  • Loading branch information
sgiavasis authored Jul 21, 2020
2 parents eb267de + 5f11b14 commit f1fa5c1
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions CPAC/utils/bids_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json


def bids_decode_fname(file_path, dbg=False):
def bids_decode_fname(file_path, dbg=False, raise_error=True):
import re

f_dict = {}
Expand Down Expand Up @@ -50,6 +50,7 @@ def bids_decode_fname(file_path, dbg=False):
fname = fname.split(".")[0]
# convert the filename string into a dictionary to pull out the other
# key value pairs

for key_val_pair in fname.split("_"):
# if the chunk has the shape key-val store key: val in f_dict
if "-" in key_val_pair:
Expand All @@ -58,14 +59,29 @@ def bids_decode_fname(file_path, dbg=False):
else:
f_dict["scantype"] = key_val_pair.split(".")[0]

if not f_dict["scantype"]:
raise ValueError("Filename (%s) does not appear to contain" % (fname) +
" scan type, does it conform to the BIDS format?")

if 'bold' in f_dict["scantype"] and not f_dict["task"]:
raise ValueError("Filename (%s) is a BOLD file, but " % (fname) +
"doesn't contain a task, does it conform to the" +
" BIDS format?")
if "scantype" not in f_dict:
msg = "Filename ({0}) does not appear to contain" \
" scan type, does it conform to the BIDS format?".format(fname)
if raise_error:
raise ValueError(msg)
else:
print(msg)
elif not f_dict["scantype"]:
msg = "Filename ({0}) does not appear to contain" \
" scan type, does it conform to the BIDS format?".format(fname)
if raise_error:
raise ValueError(msg)
else:
print(msg)
else:
if 'bold' in f_dict["scantype"] and not f_dict["task"]:
msg = "Filename ({0}) is a BOLD file, but " \
"doesn't contain a task, does it conform to the" \
" BIDS format?".format(fname)
if raise_error:
raise ValueError(msg)
else:
print(msg)

return f_dict

Expand Down Expand Up @@ -137,7 +153,7 @@ def bids_retrieve_params(bids_config_dict, f_dict, dbg=False):
return params


def bids_parse_sidecar(config_dict, dbg=False):
def bids_parse_sidecar(config_dict, dbg=False, raise_error=True):
# type: (dict, bool) -> dict
"""
Uses the BIDS principle of inheritance to build a data structure that
Expand Down Expand Up @@ -188,7 +204,7 @@ def bids_parse_sidecar(config_dict, dbg=False):
print("processing %s" % (cp))

# decode the filepath into its various components as defined by BIDS
f_dict = bids_decode_fname(cp)
f_dict = bids_decode_fname(cp, raise_error=raise_error)

# handling inheritance is a complete pain, we will try to handle it by
# build the key from the bottom up, starting with the most
Expand Down Expand Up @@ -322,7 +338,8 @@ def gen_bids_outputs_sublist(base_path, paths_list, key_list, creds_path):
return sublist


def bids_gen_cpac_sublist(bids_dir, paths_list, config_dict, creds_path, dbg=False):
def bids_gen_cpac_sublist(bids_dir, paths_list, config_dict, creds_path, dbg=False,
raise_error=True):
"""
Generates a CPAC formatted subject list from information contained in a
BIDS formatted set of data.
Expand Down Expand Up @@ -358,7 +375,7 @@ def bids_gen_cpac_sublist(bids_dir, paths_list, config_dict, creds_path, dbg=Fal
# otherwise parse the information in the sidecar json files into a dict
# we can use to extract data for our nifti files
if config_dict:
bids_config_dict = bids_parse_sidecar(config_dict)
bids_config_dict = bids_parse_sidecar(config_dict, raise_error=raise_error)

subdict = {}

Expand All @@ -368,7 +385,7 @@ def bids_gen_cpac_sublist(bids_dir, paths_list, config_dict, creds_path, dbg=Fal

if f.endswith(".nii") or f.endswith(".nii.gz"):

f_dict = bids_decode_fname(p)
f_dict = bids_decode_fname(p, raise_error=raise_error)

if config_dict:
t_params = bids_retrieve_params(bids_config_dict,
Expand Down Expand Up @@ -514,7 +531,7 @@ def collect_bids_files_configs(bids_dir, aws_input_creds=''):
from indi_aws import fetch_creds
bucket = fetch_creds.return_bucket(aws_input_creds, bucket_name)

print("gathering files from S3 bucket (%s) for %s" % (bucket, prefix))
print(f"gathering files from S3 bucket ({bucket}) for {prefix}")

for s3_obj in bucket.objects.filter(Prefix=prefix):
for suf in suffixes:
Expand Down

0 comments on commit f1fa5c1

Please sign in to comment.