Skip to content

Commit

Permalink
Merge pull request #124 from PennLINC/apply-refactor
Browse files Browse the repository at this point in the history
Apply refactor
  • Loading branch information
scovitz authored Jun 23, 2021
2 parents aad21cb + 265efc1 commit 49412ad
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions bond/bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, data_root, use_datalad=False, acq_group_level='subject',
self.datalad_handle = None
self.old_filenames = [] # files whose key groups changed
self.new_filenames = [] # new filenames for files to change
self.IF_rename_paths = [] # fmap jsons with rename intended fors
self.grouping_config = load_config(grouping_config)
self.acq_group_level = acq_group_level

Expand Down Expand Up @@ -290,7 +291,8 @@ def apply_csv_changes(self, summary_csv, files_csv, new_prefix,

if self.use_datalad:
self.datalad_handle.run(cmd=["bash", new_prefix +
"_full_cmd.sh"])
"_full_cmd.sh"],
inputs=self.IF_rename_paths)
else:
subprocess.run(["bash", new_prefix + "_full_cmd.sh"],
stdout=subprocess.PIPE,
Expand Down Expand Up @@ -406,11 +408,17 @@ def change_filename(self, filepath, entities):

if '_task-' in filepath:
old_events = filepath.replace(scan_end, '_events.tsv')
old_ejson = filepath.replace(scan_end, '_events.json')
if Path(old_events).exists():
self.old_filenames.append(old_events)
new_scan_end = '_' + suffix + old_ext
new_events = new_path.replace(new_scan_end, '_events.tsv')
self.new_filenames.append(new_events)
if Path(old_ejson).exists():
self.old_filenames.append(old_ejson)
new_scan_end = '_' + suffix + old_ext
new_ejson = new_path.replace(new_scan_end, '_events.json')
self.new_filenames.append(new_ejson)

old_physio = filepath.replace(scan_end, '_physio.tsv.gz')
if Path(old_physio).exists():
Expand All @@ -420,8 +428,9 @@ def change_filename(self, filepath, entities):
self.new_filenames.append(new_physio)

# RENAME INTENDED FORS!
for path in Path(self.path).rglob("sub-*/*/fmap/*.json"):

ses_path = self.path + '/' + sub + '/' + ses
for path in Path(ses_path).rglob("fmap/*.json"):
self.IF_rename_paths.append(str(path))
json_file = self.layout.get_file(str(path))
data = json_file.get_dict()

Expand Down Expand Up @@ -577,8 +586,20 @@ def _purge_associations(self, scans):
to_remove.append(filepath)
if '/dwi/' in str(path):
# add the bval and bvec if there
to_remove.append(img_to_new_ext(str(path), '.bval'))
to_remove.append(img_to_new_ext(str(path), '.bvec'))
if Path(img_to_new_ext(str(path), '.bval')).exists():
to_remove.append(img_to_new_ext(str(path), '.bval'))
if Path(img_to_new_ext(str(path), '.bvec')).exists():
to_remove.append(img_to_new_ext(str(path), '.bvec'))
if '/func/' in str(path):
# add tsvs
tsv = img_to_new_ext(str(path), '.tsv').replace(
'_bold', '_events')
if Path(tsv).exists():
print(tsv)
to_remove.append(tsv)
# add tsv json (if exists)
if Path(tsv.replace('.tsv', '.json')).exists():
to_remove.append(tsv.replace('.tsv', '.json'))
to_remove += scans

# create rm commands for all files that need to be purged
Expand Down

0 comments on commit 49412ad

Please sign in to comment.