Skip to content
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

Use new BIDS tags to create nifti file names #171

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
6 changes: 4 additions & 2 deletions brkraw/lib/utils.py
Original file line number Diff line number Diff line change
@@ -402,19 +402,21 @@ def build_bids_json(dset, row, fname, json_path, slope=False, offset=False):
crop = [int(row.Start), int(row.End)]
else:
crop = None
fname = '{}_{}'.format(fname, row.modality)
if dset.is_multi_echo(row.ScanID, row.RecoID): # multi_echo
nii_objs = dset.get_niftiobj(row.ScanID, row.RecoID, crop=crop, slope=slope, offset=offset)
n_echo = len(nii_objs)
n_digit = len(str(n_echo))
for echo, nii in enumerate(nii_objs):
# caught a bug here for multiple echo, changed fname to currentFileName
currentFileName = '{}_echo-{}_{}'.format(fname, echo + 1, row.modality)
currentFileName = fname.replace('echo.index',str(echo + 1).zfill(n_digit))
output_path = os.path.join(row.Dir, currentFileName)
nii.to_filename('{}.nii.gz'.format(output_path))
if json_path:
ref = get_bids_ref_obj(json_path, row)
dset.save_json(row.ScanID, row.RecoID, currentFileName, dir=row.Dir,
metadata=ref, condition=['me', echo])
else:
fname = '{}_{}'.format(fname, row.modality)
dset.save_as(row.ScanID, row.RecoID, fname, dir=row.Dir, crop=crop, slope=slope, offset=offset)
if re.search('dwi', row.modality, re.IGNORECASE):
# DTI parameter (FSL style)
35 changes: 26 additions & 9 deletions brkraw/scripts/brkraw.py
Original file line number Diff line number Diff line change
@@ -391,11 +391,11 @@ def main():

# [220202] make compatible with csv, tsv and xlsx
if 'xlsx' in datasheet_ext:
df = pd.read_excel(datasheet, dtype={'SubjID': str, 'SessID': str, 'run': str})
df = pd.read_excel(datasheet, dtype={'SubjID': str, 'SessID': str, 'run': str, 'inv': str, 'flip': str})
elif 'csv' in datasheet_ext:
df = pd.read_csv(datasheet, dtype={'SubjID': str, 'SessID': str, 'run': str}, index_col=None, header=0, sep=',')
df = pd.read_csv(datasheet, dtype={'SubjID': str, 'SessID': str, 'run': str, 'inv': str, 'flip': str}, index_col=None, header=0, sep=',')
elif 'tsv' in datasheet_ext:
df = pd.read_csv(datasheet, dtype={'SubjID': str, 'SessID': str, 'run': str}, index_col=None, header=0, sep='\t')
df = pd.read_csv(datasheet, dtype={'SubjID': str, 'SessID': str, 'run': str, 'inv': str, 'flip': str}, index_col=None, header=0, sep='\t')
else:
print(f'{datasheet_ext} if not supported format.')
raise InvalidApproach('Invalid input for datasheet!')
@@ -470,11 +470,11 @@ def main():
if len(md_df) > 1:
conflict_tested = []
for j, sub_row in md_df.iterrows():
# Try to append a run number if it was not already specified
# The run tag might not appear at the right position with this
# strategy
if pd.isnull(sub_row.run):
fname = '{}_run-{}'.format(sub_row.FileName, str(j+1).zfill(2))
else:
_ = bids_validation(df, i, 'run', sub_row.run, 3, dtype=int)
fname = '{}_run-{}'.format(sub_row.FileName, str(sub_row.run).zfill(2)) # [20210822] format error
if fname in conflict_tested:
raise ValueConflictInField('ScanID:[{}] Conflict error. '
'The [run] index value must be unique '
@@ -696,12 +696,29 @@ def completeFieldsCreateFolders (df, filtered_dset, dset, multi_session, root_pa
if pd.notnull(row.ce):
if bids_validation(df, i, 'ce', row.ce, 5):
fname = '{}_ce-{}'.format(fname, row.ce)
if pd.notnull(row.dir):
if bids_validation(df, i, 'dir', row.dir, 2):
fname = '{}_dir-{}'.format(fname, row.dir)
if pd.notnull(row.rec):
if bids_validation(df, i, 'rec', row.rec, 2):
fname = '{}_rec-{}'.format(fname, row.rec)
if pd.notnull(row.dir):
if bids_validation(df, i, 'dir', row.dir, 2):
fname = '{}_dir-{}'.format(fname, row.dir)
if pd.notnull(row.run):
if bids_validation(df, i, 'run', row.run, 2):
fname = '{}_run-{}'.format(fname, row.run)
if dset.is_multi_echo(row.ScanID, row.RecoID):
fname = '{}_echo-echo.index'.format(fname)
if pd.notnull(row.flip):
if bids_validation(df, i, 'flip', row.flip, 2):
fname = '{}_flip-{}'.format(fname, row.flip)
if pd.notnull(row.inv):
if bids_validation(df, i, 'inv', row.inv, 2):
fname = '{}_inv-{}'.format(fname, row.inv)
if pd.notnull(row.mt):
if bids_validation(df, i, 'mt', row.mt, 3):
fname = '{}_mt-{}'.format(fname, row.mt)
if pd.notnull(row.part):
if bids_validation(df, i, 'part', row.part, 5):
fname = '{}_part-{}'.format(fname, row.part)
filtered_dset.loc[i, 'FileName'] = fname
filtered_dset.loc[i, 'Dir'] = dtype_path
if pd.isnull(row.modality):