Skip to content

Commit

Permalink
Re-Refactoring of IO helper functions (bidscoin.plugins -> `bidscoi…
Browse files Browse the repository at this point in the history
…n.utilities`)
  • Loading branch information
marcelzwiers committed Dec 8, 2024
1 parent 13e414b commit b1707bf
Show file tree
Hide file tree
Showing 13 changed files with 664 additions and 669 deletions.
4 changes: 1 addition & 3 deletions bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
from fnmatch import fnmatch
from pathlib import Path
from typing import Union, Any, Iterable, NewType
from pydicom import config
from importlib.util import find_spec
if find_spec('bidscoin') is None:
import sys
sys.path.append(str(Path(__file__).parents[1]))
from bidscoin import bcoin, schemafolder, templatefolder, lsdirs, is_hidden, __version__, DEBUG
from bidscoin import bcoin, schemafolder, templatefolder, is_hidden, __version__
from bidscoin.plugins import EventsParser
from ruamel.yaml import YAML
yaml = YAML()
yaml.representer.ignore_aliases = lambda *data: True # Expand aliases (https://stackoverflow.com/questions/58091449/disabling-alias-for-yaml-file-in-python)
config.INVALID_KEY_BEHAVIOR = 'IGNORE'

# Define custom data types (replace with proper classes or TypeAlias of Python >= 3.10)
Plugin = NewType('Plugin', dict[str, Any])
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidscoiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if find_spec('bidscoin') is None:
sys.path.append(str(Path(__file__).parents[1]))
from bidscoin import bcoin, bids, lsdirs, bidsversion, trackusage, __version__, DEBUG
from bidscoin.plugins import unpack
from bidscoin.utilities import unpack


def bidscoiner(sourcefolder: str, bidsfolder: str, participant: list=(), force: bool=False, bidsmap: str='bidsmap.yaml', cluster: str='') -> None:
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/bidsmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
sys.path.append(str(Path(__file__).parents[1]))
from bidscoin import bcoin, lsdirs, trackusage, check_version, __version__
from bidscoin.bids import BidsMap
from bidscoin.plugins import unpack
from bidscoin.utilities import unpack

_, uptodate, versionmessage = check_version()

Expand Down
551 changes: 1 addition & 550 deletions bidscoin/plugins/__init__.py

Large diffs are not rendered by default.

19 changes: 6 additions & 13 deletions bidscoin/plugins/dcm2niix2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from typing import Union
from pathlib import Path
from bidscoin import bids, run_command, lsdirs, due, Doi
from bidscoin.utilities import physio
from bidscoin.utilities import physio, is_dicomfile, is_parfile, get_dicomfield, get_parfield, get_dicomfile, get_parfiles
from bidscoin.bids import BidsMap, DataFormat, Plugin, Plugins
from bidscoin.plugins import PluginInterface, is_dicomfile, is_parfile, get_dicomfield, get_parfield, get_dicomfile, get_parfiles
from bidscoin.plugins import PluginInterface
try:
from nibabel.testing import data_path
except ImportError:
Expand Down Expand Up @@ -92,7 +92,6 @@ def has_support(self, file: Path, dataformat: Union[DataFormat, str]='') -> str:

return ''


def get_attribute(self, dataformat: Union[DataFormat, str], sourcefile: Path, attribute: str, options) -> Union[str, int]:
"""
This plugin supports reading attributes from DICOM and PAR dataformats
Expand All @@ -109,7 +108,6 @@ def get_attribute(self, dataformat: Union[DataFormat, str], sourcefile: Path, at
if dataformat == 'PAR':
return get_parfield(attribute, sourcefile)


def bidsmapper(self, session: Path, bidsmap_new: BidsMap, bidsmap_old: BidsMap, template: BidsMap) -> None:
"""
The goal of this plugin function is to identify all the different runs in the session and update the
Expand Down Expand Up @@ -190,7 +188,6 @@ def bidsmapper(self, session: Path, bidsmap_new: BidsMap, bidsmap_old: BidsMap,
else:
LOGGER.bcdebug(f"Existing/duplicate sample: {run.datasource}")


@due.dcite(Doi('10.1016/j.jneumeth.2016.03.001'), description='dcm2niix: DICOM to NIfTI converter', tags=['reference-implementation'])
def bidscoiner(self, session: Path, bidsmap: BidsMap, bidsses: Path) -> Union[None, dict]:
"""
Expand All @@ -206,9 +203,8 @@ def bidscoiner(self, session: Path, bidsmap: BidsMap, bidsses: Path) -> Union[No
"""

# Get the subject identifiers and the BIDS root folder from the bidsses folder
subid = bidsses.name if bidsses.name.startswith('sub-') else bidsses.parent.name
sesid = bidsses.name if bidsses.name.startswith('ses-') else ''
bidsfolder = bidsses.parent.parent if sesid else bidsses.parent
subid = bidsses.name if bidsses.name.startswith('sub-') else bidsses.parent.name
sesid = bidsses.name if bidsses.name.startswith('ses-') else ''

# Get started and see what dataformat we have
options = Plugin(bidsmap.plugins['dcm2niix2bids'])
Expand All @@ -222,13 +218,10 @@ def bidscoiner(self, session: Path, bidsmap: BidsMap, bidsses: Path) -> Union[No

# Make a list of all the data sources/runs
sources: list[Path] = []
manufacturer = 'UNKNOWN'
if dataformat == 'DICOM':
sources = lsdirs(session, '**/*')
manufacturer = datasource.attributes('Manufacturer')
sources = lsdirs(session, '**/*')
elif dataformat == 'PAR':
sources = get_parfiles(session)
manufacturer = 'Philips Medical Systems'
sources = get_parfiles(session)
else:
LOGGER.error(f"Unsupported dataformat '{dataformat}'")

Expand Down
3 changes: 0 additions & 3 deletions bidscoin/plugins/nibabel2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def test(self, options: Plugin=OPTIONS) -> int:

return 0


def has_support(self, file: Path, dataformat: Union[DataFormat, str]='') -> str:
"""
This plugin function assesses whether a sourcefile is of a supported dataformat
Expand All @@ -81,7 +80,6 @@ def has_support(self, file: Path, dataformat: Union[DataFormat, str]='') -> str:

return ''


def get_attribute(self, dataformat: Union[DataFormat, str], sourcefile: Path, attribute: str, options: Plugin) -> Union[str, int, float, list]:
"""
This plugin supports reading attributes from DICOM and PAR dataformats
Expand All @@ -107,7 +105,6 @@ def get_attribute(self, dataformat: Union[DataFormat, str], sourcefile: Path, at

return value


def bidscoiner(self, session: Path, bidsmap: BidsMap, bidsses: Path) -> None:
"""
The bidscoiner plugin to convert the session Nibabel source-files into BIDS-valid NIfTI-files in the
Expand Down
5 changes: 2 additions & 3 deletions bidscoin/plugins/spec2nii2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from pathlib import Path
from bidscoin import run_command, is_hidden, bids, due, Doi
from bidscoin.bids import BidsMap, DataFormat, Plugin
from bidscoin.plugins import PluginInterface, is_dicomfile, get_twixfield, get_sparfield, get_p7field
from bidscoin.plugins import PluginInterface
from bidscoin.utilities import is_dicomfile, get_twixfield, get_sparfield, get_p7field

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,7 +46,6 @@ def test(self, options: Plugin=OPTIONS) -> int:
# Test the spec2nii installation
return run_command(f"{options.get('command',OPTIONS['command'])} -v")


def has_support(self, file: Path, dataformat: Union[DataFormat, str]='') -> str:
"""
This plugin function assesses whether a sourcefile is of a supported dataformat
Expand All @@ -68,7 +68,6 @@ def has_support(self, file: Path, dataformat: Union[DataFormat, str]='') -> str:

return ''


def get_attribute(self, dataformat: Union[DataFormat, str], sourcefile: Path, attribute: str, options: Plugin) -> str:
"""
This plugin function reads attributes from the supported sourcefile
Expand Down
Loading

0 comments on commit b1707bf

Please sign in to comment.