Skip to content

Commit

Permalink
Minor tweak (use a set instead of a list as datasource.targets)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Jan 18, 2024
1 parent e3eb818 commit 58caa9d
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@


class DataSource:
def __init__(self, provenance: Union[str, Path]='', plugins: dict=None, dataformat: str='', datatype: str='', subprefix: str='', sesprefix: str='', targets: List[Path] = ()):
def __init__(self, provenance: Union[str, Path]='', plugins: dict=None, dataformat: str='', datatype: str='', subprefix: str='', sesprefix: str='', targets: set[Path] = ()):
"""
A source data type (e.g. DICOM or PAR) that can be converted to BIDS by the plugins
Expand All @@ -76,7 +76,7 @@ def __init__(self, provenance: Union[str, Path]='', plugins: dict=None, dataform
self.is_datasource()
self.subprefix = subprefix
self.sesprefix = sesprefix
self.targets = list(targets)
self.targets = set(targets)
self._cache = {}

def resubprefix(self) -> str:
Expand Down Expand Up @@ -1987,7 +1987,7 @@ def rename_runless_to_run1(runs: List[dict], scans_table: pd.DataFrame) -> None:

# Change target from run-less to run-1
run['datasource'].targets.remove(target)
run['datasource'].targets.append((outfolder/run1_bidsname).with_suffix(suffixes))
run['datasource'].targets.add((outfolder/run1_bidsname).with_suffix(suffixes))


def updatemetadata(datasource: DataSource, targetmeta: Path, usermeta: dict, extensions: list, sourcemeta: Path = Path()) -> dict:
Expand Down
4 changes: 2 additions & 2 deletions bidscoin/plugins/dcm2niix2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
if not list(outfolder.glob(f"{bidsname}.*nii*")): continue

jsonfiles.update(outfolder.glob(f"{bidsname}.json")) # add existing created json files: bidsname.json
datasource.targets += outfolder.glob(f"{bidsname}.*[!json]") # Add files created using this run-item (except sidecars)
datasource.targets.update(outfolder.glob(f"{bidsname}.*[!json]")) # Add files created using this run-item (except sidecars)

# Handle the ABCD GE pepolar sequence
extrafile = list(outfolder.glob(f"{bidsname}a.nii*"))
Expand Down Expand Up @@ -428,7 +428,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
if newbidsfile.is_file():
LOGGER.warning(f"Overwriting existing {newbidsfile} file -- check your results carefully!")
dcm2niixfile.replace(newbidsfile)
datasource.targets.append(newbidsfile)
datasource.targets.add(newbidsfile)

# Rename all associated files (i.e. the json-, bval- and bvec-files)
oldjsonfile = dcm2niixfile.with_suffix('').with_suffix('.json')
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/plugins/nibabel2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> None:

# Save the sourcefile as a BIDS NIfTI file
nib.save(nib.load(sourcefile), bidsfile)
datasource.targets.append(bidsfile)
datasource.targets.add(bidsfile)

# Load / copy over the source meta-data
sidecar = bidsfile.with_suffix('').with_suffix('.json')
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/plugins/spec2nii2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def bidscoiner_plugin(session: Path, bidsmap: dict, bidsses: Path) -> Union[None
if not list(outfolder.glob(f"{bidsname}.nii*")): continue

# Add files created using this bidsmap run-item (except sidecars)
datasource.targets += outfolder.glob(f"{bidsname}.*[!json]")
datasource.targets.add(outfolder.glob(f"{bidsname}.*[!json]"))

# Load / copy over and adapt the newly produced json sidecar-file (NB: assumes every NIfTI-file comes with a json-file)
metadata = bids.updatemetadata(datasource, sidecar, run['meta'], options['meta'])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def test_rename_runless_to_run1(tmp_path):
outfile = (outfolder/file_name).with_suffix(suffix)
outfile.touch()
if suffix == '.nii.gz':
run['datasource'].targets.append(outfile)
run['datasource'].targets.add(outfile)
matched_runs.append(run)

# Create the scans table
Expand Down

0 comments on commit 58caa9d

Please sign in to comment.