Skip to content

Commit

Permalink
Merge pull request #62 from PennLINC/fix-bval-bvec
Browse files Browse the repository at this point in the history
now checking for .tsv .bval and .bvec in renaming
  • Loading branch information
scovitz authored Dec 17, 2020
2 parents cc971e8 + 090182b commit 511ac80
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
40 changes: 28 additions & 12 deletions bond/bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ def apply_csv_changes(self, summary_csv, files_csv, new_prefix,
(files_df[["ParamGroup", "KeyGroup"]] == source_id).all(1)]

# Get a source json file
source_json = img_to_json(source_files.iloc[0].FilePath)
source_json = img_to_new_ext(source_files.iloc[0].FilePath,
'.json')
for dest_nii in dest_files.FilePath:
dest_json = img_to_json(dest_nii)
dest_json = img_to_new_ext(dest_nii, '.json')
if Path(dest_json).exists() and Path(source_json).exists():
merge_commands.append(
'bids-sidecar-merge %s %s'
Expand Down Expand Up @@ -253,7 +254,7 @@ def change_filename(self, filepath, entities):
modality = large.replace(small, '')

# detect the subject/session string and keep it together
# front_stem is the string of subject/session paris
# front_stem is the string of subject/session pairs
# these two entities don't change with the key group
front_stem = ""
cntr = 0
Expand Down Expand Up @@ -291,13 +292,21 @@ def change_filename(self, filepath, entities):
self.old_filenames.append(str(path))
self.new_filenames.append(new_path)

# now also rename json file
json_file = img_to_json(filepath)

if Path(json_file).exists():
new_json_path = new_path_front + "_" + new_filename + ".json"
self.old_filenames.append(json_file)
self.new_filenames.append(new_json_path)
# now also rename files with same stem diff extension
extensions = ['.json', '.bval', '.bvec', '.tsv', '.tsv.gz']
for ext in extensions:
ext_file = img_to_new_ext(filepath, ext)

# check if ext_file exists in the bids dir
if Path(ext_file).exists():
# need to remove suffix for .tsv and .tsv.gz files
if ext == '.tsv':
new_filename = new_filename.rpartition('_')[0] + '_events'
if ext == '.tsv.gz':
new_filename = new_filename.rpartition('_')[0] + '_physio'
new_ext_path = new_path_front + "_" + new_filename + ext
self.old_filenames.append(ext_file)
self.new_filenames.append(new_ext_path)

def _cache_fieldmaps(self):
"""Searches all fieldmaps and creates a lookup for each file.
Expand Down Expand Up @@ -665,5 +674,12 @@ def _order_columns(df):
return df[new_columns]


def img_to_json(img_path):
return img_path.replace(".nii.gz", "").replace(".nii", "") + ".json"
def img_to_new_ext(img_path, new_ext):
# handle .tsv edge case
if new_ext == '.tsv':
# take out suffix
return img_path.rpartition('_')[0] + '_events' + new_ext
if new_ext == '.tsv.gz':
return img_path.rpartition('_')[0] + '_physio' + new_ext
else:
return img_path.replace(".nii.gz", "").replace(".nii", "") + new_ext
38 changes: 38 additions & 0 deletions tests/test_bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from copy import deepcopy
import hashlib
import json
from pathlib import Path
from pkg_resources import resource_filename as pkgrf
import pytest
from bond import BOnD
Expand Down Expand Up @@ -366,6 +367,13 @@ def test_apply_csv_changes(tmp_path):
# make sure files you wanted to rename exist in the bids dir

data_root = get_data(tmp_path)

# add extension files
path_to_img = str(data_root / "complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_magnitude1.nii.gz")
_add_ext_files(path_to_img)
assert Path(data_root /
"complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v4_physio.tsv.gz").exists() == True

complete_bond = BOnD(data_root / "complete", use_datalad=True)
complete_bond.datalad_save()

Expand All @@ -387,11 +395,24 @@ def test_apply_csv_changes(tmp_path):
assert og_content == mod1_content

# edit the csv, add a RenameKeyGroup
# make sure extension files also get renamed

_edit_csv(str(tmp_path / "originals_summary.csv"))
complete_bond.apply_csv_changes(str(tmp_path / "originals_summary.csv"),
str(tmp_path / "originals_files.csv"),
str(tmp_path / "modified2"))

# check files df to make sure extension files also got renmaed
mod_files = tmp_path / "modified2_files.csv"
assert Path(data_root /
"complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v5_magnitude1.bval").exists() == True
assert Path(data_root /
"complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v5_magnitude1.bvec").exists() == True
assert Path(data_root /
"complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v5_events.tsv").exists() == True
assert Path(data_root /
"complete/sub-01/ses-phdiff/fmap/sub-01_ses-phdiff_acq-v5_physio.tsv.gz").exists() == True

mod2_path = tmp_path / "modified2_summary.csv"
with mod2_path.open("r") as f:
mod2_content = "".join(f.readlines())
Expand All @@ -412,6 +433,7 @@ def test_apply_csv_changes(tmp_path):
deleted_content = "".join(f.readlines())
assert deleted_keyparam not in deleted_content


def _add_deletion(summary_csv):
df = pd.read_csv(summary_csv)
df.loc[3, 'MergeInto'] = 0
Expand All @@ -430,6 +452,22 @@ def _edit_csv(summary_csv):
writer = csv.writer(open(summary_csv, 'w'))
writer.writerows(lines)

def _add_ext_files(img_path):
# add and save extension files in
exts = ['.bval', '.bvec', '.tsv', '.tsv.gz']
for ext in exts:
ext_file = img_path.replace(".nii.gz", "").replace(".nii", "") + ext

if ext == '.tsv':
no_suffix = ext_file.rpartition('_')[0]
ext_file = no_suffix + '_events' + ext
if ext == '.tsv.gz':
no_suffix = ext_file.rpartition('_')[0]
ext_file = no_suffix + '_physio' + ext
# save ext file in img_path's parent dir
Path(ext_file).touch()


# add new key group name to RenameKeyGroup column


Expand Down

0 comments on commit 511ac80

Please sign in to comment.