Skip to content

Commit f89b7be

Browse files
committed
Update cubids.py
1 parent 3c67ea4 commit f89b7be

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

cubids/cubids.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,48 @@ def add_file_collections(self):
439439

440440
self.reset_bids_layout()
441441

442+
def rename_subject(self, old_subject, new_subject):
443+
"""Rename a subject in the dataset.
444+
445+
This method renames a subject in the dataset and updates all associated files.
446+
447+
Parameters
448+
----------
449+
old_subject : :obj:`str`
450+
The old subject ID.
451+
new_subject : :obj:`str`
452+
The new subject ID.
453+
"""
454+
# check if force_unlock is set
455+
if self.force_unlock:
456+
# CHANGE TO SUBPROCESS.CALL IF NOT BLOCKING
457+
subprocess.run(["datalad", "unlock"], cwd=self.path)
458+
459+
# get all files in the dataset
460+
files = self.layout.get(subject=old_subject, extension=[".nii", ".nii.gz"])
461+
for file in files:
462+
# get the filename
463+
filename = file.filename
464+
# get the entities
465+
entities = file.entities
466+
# rename the subject
467+
entities["subject"] = new_subject
468+
# build the new path
469+
new_path = utils.build_path(
470+
filepath=filename,
471+
out_entities=entities,
472+
out_dir=self.path,
473+
schema=self.schema,
474+
)
475+
# rename the file
476+
os.rename(file.path, new_path)
477+
478+
if self.use_datalad:
479+
self.datalad_save(message="Renamed subject")
480+
481+
self.reset_bids_layout()
482+
483+
442484
def apply_tsv_changes(self, summary_tsv, files_tsv, new_prefix, raise_on_error=True):
443485
"""Apply changes documented in the edited summary tsv and generate the new tsv files.
444486

0 commit comments

Comments
 (0)