Skip to content

Commit

Permalink
Read BIDS datasets using pydra-bids tasks (#791)
Browse files Browse the repository at this point in the history
* Add pydra-bids to dependencies

* Fix invalid escape sequence in regexp

* Remove erroneous return

* Read BIDS datasets with BIDSDataReader

* Remove datatype attribute from BIDS queries

It is not supported by `pydra-bids` and could be computed from the suffix
anyway.

* Ugrade pydra-bids to version 0.0.3

* Fix name of BIDS dataset reader task

* Run formatter
  • Loading branch information
ghisvail authored Nov 18, 2022
1 parent 5f516e8 commit f28eb89
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 38 deletions.
4 changes: 2 additions & 2 deletions clinica/pydra/engine_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def run(wf: Workflow) -> Result:
with Submitter(plugin="cf") as submitter:
submitter(wf)
except Exception as e:
path = re.search("\/.*\.pklz", str(e))
path = re.search(r"/.*\.pklz", str(e))
if path:
print(read_error(path.group(0)))
return print(str(e))
print(str(e))

return wf.result(return_inputs=False)

Expand Down
13 changes: 5 additions & 8 deletions clinica/pydra/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def bids_reader(query: BIDSQuery, input_dir: PathLike):
Parameters
----------
query : BIDSQuery
Input to BIDSDataGrabber (c.f https://nipype.readthedocs.io/en/latest/api/generated/nipype.interfaces.io.html#bidsdatagrabber)
BIDS dataset reader query.
input_dir : PathLike
The BIDS input directory.
Expand All @@ -125,14 +125,11 @@ def bids_reader(query: BIDSQuery, input_dir: PathLike):
Nipype1Task
The task used for reading files from BIDS.
"""
bids_data_grabber = nio.BIDSDataGrabber(output_query=query.query)
bids_reader_task = Nipype1Task(
name="bids_reader_task",
interface=bids_data_grabber,
base_dir=input_dir,
output_query=query.query,
from pydra.tasks.bids import BIDSDatasetReader

return BIDSDatasetReader(output_query=query.query).to_task(
name="bids_reader_task", dataset_path=input_dir
)
return bids_reader_task


def caps_reader(query: CAPSQuery, input_dir: PathLike):
Expand Down
8 changes: 4 additions & 4 deletions clinica/pydra/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def format_query(self, input_query: Optional[Dict] = None) -> Dict:
{"label" : {query dict} or list of query dicts}
example: {"T1w": {"datatype": "anat", "suffix": "T1w", "extension": [".nii.gz"]}}
example: {"T1w": {"suffix": "T1w", "extension": [".nii.gz"]}}
Parameters
----------
Expand Down Expand Up @@ -150,12 +150,12 @@ class BIDSQuery(Query):
>>> len(q)
1
>>> q.query
{'T1w': {'datatype': 'anat', 'suffix': 'T1w', 'extension': ['.nii.gz']}}
{'T1w': {'suffix': 'T1w', 'extension': ['.nii.gz']}}
"""

_default_queries = {
"T1w": {"datatype": "anat", "suffix": "T1w", "extension": [".nii.gz"]},
"pet": {"datatype": "pet", "suffix": "pet", "extension": [".nii.gz"]},
"T1w": {"suffix": "T1w", "extension": [".nii.gz"]},
"pet": {"suffix": "pet", "extension": [".nii.gz"]},
}

def parse_query(self, query: Dict) -> Dict:
Expand Down
39 changes: 29 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ joblib = "^1.2.0"
attrs = ">=20.1.0"
cattrs = "^1.9.0"
brainstat = "^0.3.6"
pydra-bids = "^0.0.3"

[tool.poetry.group.dev.dependencies]
black = "*"
Expand Down
4 changes: 1 addition & 3 deletions test/unittests/pydra/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def smooth_image(input_image: PathLike) -> PurePath:
def bids_query():
from clinica.pydra.query import BIDSQuery

return BIDSQuery(
{"T1w": {"datatype": "anat", "suffix": "T1w", "extension": [".nii.gz"]}}
)
return BIDSQuery({"T1w": {"suffix": "T1w", "extension": [".nii.gz"]}})


@pytest.fixture
Expand Down
7 changes: 3 additions & 4 deletions test/unittests/pydra/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@


def test_bids_reader_instantiation(tmp_path):
from nipype.interfaces.io import BIDSDataGrabber
from pydra.engine.task import FunctionTask

from clinica.pydra.interfaces import bids_reader

task = bids_reader(BIDSQuery(), tmp_path)
assert isinstance(task, Nipype1Task)
assert isinstance(task, FunctionTask)
assert task.name == "bids_reader_task"
assert task.inputs.base_dir == tmp_path
assert isinstance(task._interface, BIDSDataGrabber)
assert task.inputs.dataset_path == tmp_path


def test_bids_reader(tmp_path):
Expand Down
9 changes: 2 additions & 7 deletions test/unittests/pydra/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ def test_bids_query():
assert len(q) == 0

q = BIDSQuery({"T1w": {}})
assert q.query == {
"T1w": {"datatype": "anat", "suffix": "T1w", "extension": [".nii.gz"]}
}
assert q.query == {"T1w": {"suffix": "T1w", "extension": [".nii.gz"]}}
assert len(q) == 1

q = BIDSQuery({"T1w": {"foo": "bar"}})
assert q.query == {
"T1w": {
"datatype": "anat",
"suffix": "T1w",
"extension": [".nii.gz"],
"foo": "bar",
Expand All @@ -32,9 +29,7 @@ def test_bids_query():
assert len(q) == 1

q = BIDSQuery({"T1w": {}, "foo": {"bar": "baz"}})
assert q.query == {
"T1w": {"datatype": "anat", "suffix": "T1w", "extension": [".nii.gz"]}
}
assert q.query == {"T1w": {"suffix": "T1w", "extension": [".nii.gz"]}}
assert len(q) == 1


Expand Down

0 comments on commit f28eb89

Please sign in to comment.