Skip to content

Commit 4b52c01

Browse files
fix(bids): apply CodeRabbit feedback
- Update setup.py to include nibabel in BIDS extra - Update docs to clarify nibabel is included - Add nibabel availability check in _info() - Move os import to module level - Update test skipif to check both pybids and nibabel
1 parent 4d52df6 commit 4b52c01

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

docs/source/bids_dataset.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<Tip>
66

7-
To use the BIDS loader, you need to install the `bids` extra:
7+
To use the BIDS loader, you need to install the `bids` extra (which installs `pybids` and `nibabel`):
88

99
```bash
1010
pip install datasets[bids]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@
210210

211211
NIBABEL_REQUIRE = ["nibabel>=5.3.2", "ipyniivue==2.4.2"]
212212

213-
PYBIDS_REQUIRE = ["pybids>=0.21.0"]
213+
PYBIDS_REQUIRE = ["pybids>=0.21.0"] + NIBABEL_REQUIRE
214214

215215
EXTRAS_REQUIRE = {
216216
"audio": AUDIO_REQUIRE,

src/datasets/packaged_modules/bids/bids.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
from dataclasses import dataclass
34
from typing import Optional
45

@@ -28,6 +29,8 @@ class Bids(datasets.GeneratorBasedBuilder):
2829
def _info(self):
2930
if not config.PYBIDS_AVAILABLE:
3031
raise ImportError("To load BIDS datasets, please install pybids: pip install pybids")
32+
if not config.NIBABEL_AVAILABLE:
33+
raise ImportError("To load BIDS datasets, please install nibabel: pip install nibabel")
3134

3235
return datasets.DatasetInfo(
3336
features=datasets.Features(
@@ -46,8 +49,6 @@ def _info(self):
4649
)
4750

4851
def _split_generators(self, dl_manager):
49-
import os
50-
5152
from bids import BIDSLayout
5253

5354
if not self.config.data_dir:

tests/packaged_modules/test_bids.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def test_bids_module_imports():
7575

7676
def test_bids_requires_pybids(monkeypatch):
7777
"""Test helpful error when pybids not installed."""
78-
import datasets.config
7978
from datasets.packaged_modules.bids.bids import Bids
8079

8180
monkeypatch.setattr(datasets.config, "PYBIDS_AVAILABLE", False)
@@ -84,7 +83,10 @@ def test_bids_requires_pybids(monkeypatch):
8483
Bids()
8584

8685

87-
@pytest.mark.skipif(not datasets.config.PYBIDS_AVAILABLE, reason="pybids not installed")
86+
@pytest.mark.skipif(
87+
not datasets.config.PYBIDS_AVAILABLE or not datasets.config.NIBABEL_AVAILABLE,
88+
reason="pybids or nibabel not installed",
89+
)
8890
def test_bids_loads_single_subject(minimal_bids_dataset):
8991
from datasets import load_dataset
9092

@@ -100,7 +102,10 @@ def test_bids_loads_single_subject(minimal_bids_dataset):
100102
assert sample["session"] is None
101103

102104

103-
@pytest.mark.skipif(not datasets.config.PYBIDS_AVAILABLE, reason="pybids not installed")
105+
@pytest.mark.skipif(
106+
not datasets.config.PYBIDS_AVAILABLE or not datasets.config.NIBABEL_AVAILABLE,
107+
reason="pybids or nibabel not installed",
108+
)
104109
def test_bids_multi_subject(multi_subject_bids):
105110
from datasets import load_dataset
106111

0 commit comments

Comments
 (0)