From 38ba4516948b5c3877beb2ad5831971e890b0cc9 Mon Sep 17 00:00:00 2001 From: Jeremie Fouquet Date: Wed, 19 Jun 2024 15:56:07 -0400 Subject: [PATCH 1/5] Use new BIDS tags to create nifti file names --- brkraw/scripts/brkraw.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/brkraw/scripts/brkraw.py b/brkraw/scripts/brkraw.py index a27c96e..6385f3f 100644 --- a/brkraw/scripts/brkraw.py +++ b/brkraw/scripts/brkraw.py @@ -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!') @@ -702,6 +702,18 @@ def completeFieldsCreateFolders (df, filtered_dset, dset, multi_session, root_pa if pd.notnull(row.rec): if bids_validation(df, i, 'rec', row.rec, 2): fname = '{}_rec-{}'.format(fname, row.rec) + 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): From ea7105f7189a4712ee141bc339e64143b810c80e Mon Sep 17 00:00:00 2001 From: Jeremie Fouquet Date: Fri, 21 Jun 2024 13:17:35 -0400 Subject: [PATCH 2/5] Correct BIDS tags order for rec and dir --- brkraw/scripts/brkraw.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/brkraw/scripts/brkraw.py b/brkraw/scripts/brkraw.py index 6385f3f..daaa51e 100644 --- a/brkraw/scripts/brkraw.py +++ b/brkraw/scripts/brkraw.py @@ -696,12 +696,12 @@ 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.flip): if bids_validation(df, i, 'flip', row.flip, 2): fname = '{}_flip-{}'.format(fname, row.flip) From 6a02a07e71509f30a88e910baeaaaf09c6178352 Mon Sep 17 00:00:00 2001 From: Jeremie Fouquet Date: Fri, 21 Jun 2024 13:56:02 -0400 Subject: [PATCH 3/5] feat: change where user-defined run number is inserted --- brkraw/scripts/brkraw.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/brkraw/scripts/brkraw.py b/brkraw/scripts/brkraw.py index daaa51e..753a101 100644 --- a/brkraw/scripts/brkraw.py +++ b/brkraw/scripts/brkraw.py @@ -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 ' @@ -702,6 +702,9 @@ def completeFieldsCreateFolders (df, filtered_dset, dset, multi_session, root_pa 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 pd.notnull(row.flip): if bids_validation(df, i, 'flip', row.flip, 2): fname = '{}_flip-{}'.format(fname, row.flip) From 894eaa21e69991213bd2faa49c3300f7b39eb67a Mon Sep 17 00:00:00 2001 From: Jeremie Fouquet Date: Fri, 21 Jun 2024 22:38:00 -0400 Subject: [PATCH 4/5] fix: correct the position of the echo tag --- brkraw/lib/utils.py | 4 +++- brkraw/scripts/brkraw.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/brkraw/lib/utils.py b/brkraw/lib/utils.py index 847798b..ae4a900 100644 --- a/brkraw/lib/utils.py +++ b/brkraw/lib/utils.py @@ -404,9 +404,11 @@ def build_bids_json(dset, row, fname, json_path, slope=False, offset=False): crop = None 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: diff --git a/brkraw/scripts/brkraw.py b/brkraw/scripts/brkraw.py index 753a101..77d2b85 100644 --- a/brkraw/scripts/brkraw.py +++ b/brkraw/scripts/brkraw.py @@ -705,6 +705,8 @@ def completeFieldsCreateFolders (df, filtered_dset, dset, multi_session, root_pa 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) From d4ffb492911b544c3e73e8a4f974010c4403a7bf Mon Sep 17 00:00:00 2001 From: Jeremie Fouquet Date: Tue, 25 Jun 2024 12:50:06 -0400 Subject: [PATCH 5/5] fix: add modality to multi-echo BIDS file name --- brkraw/lib/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brkraw/lib/utils.py b/brkraw/lib/utils.py index ae4a900..28094ca 100644 --- a/brkraw/lib/utils.py +++ b/brkraw/lib/utils.py @@ -402,6 +402,7 @@ 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) @@ -416,7 +417,6 @@ def build_bids_json(dset, row, fname, json_path, slope=False, offset=False): 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)