Skip to content

Commit d772fe0

Browse files
committed
added openneuro download of t1w
1 parent 8cdc6a4 commit d772fe0

File tree

6 files changed

+72
-37
lines changed

6 files changed

+72
-37
lines changed

README.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,29 @@ or include in your package's ``test_requires``.
3030
Usage
3131
-----
3232

33+
Create a pytest fixture consisting of a dummy image with field-map metadata in DICOM format
34+
3335
.. code-block:: python
3436
3537
# Import medimages4tests generator functions
36-
from medimages4tests.dicom.mri.fmap.ge.discovery_mr888.dv26_0_r05_2008a import sample_image
38+
from medimages4tests.dummy.dicom.mri.fmap.ge.discovery_mr888.dv26_0_r05_2008a import get_image
3739
3840
# Return generated images in pytest fixtures (or alternative test framework)
3941
@pytest.fixture()
4042
def ge_dicom_fmap():
41-
return sample_image()
43+
return get_image()
44+
45+
Create a dummy NIfTI image
46+
47+
.. code-block:: python
48+
49+
import numpy
50+
# Import `get_image` function
51+
from medimages4tests.dummy.nifti import get_image
52+
53+
# Create dummy nifti image of 10x10x10 containing all ones
54+
@pytest.fixture()
55+
def ones_nifti():
56+
return get_image(
57+
data=numpy.ones((10, 10, 10))
58+
)

medimages4tests/dummy/nifti.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import tempfile
12
from pathlib import Path
23
import gzip
34
import shutil
@@ -6,13 +7,16 @@
67

78

89
def get_image(
9-
out_file: Path,
10+
out_file: Path = None,
1011
data: np.ndarray = None,
1112
vox_sizes=(1.0, 1.0, 1.0),
1213
qform=(1, 2, 3, 1),
1314
compressed=False,
1415
) -> Path:
1516
"""Create a random Nifti file to satisfy BIDS parsers"""
17+
if out_file is None:
18+
out_file = Path(tempfile.mkdtemp()) / "sample.nii"
19+
1620
if data is None:
1721
data = np.random.randint(0, 1, size=[10, 10, 10])
1822

medimages4tests/mri/neuro/base.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +0,0 @@
1-
from pathlib import Path
2-
import attrs
3-
4-
5-
@attrs.define
6-
class OpenneuroSpec:
7-
8-
dataset: str
9-
tag: str
10-
path: Path

medimages4tests/mri/neuro/t1w.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
from tempfile import mkdtemp
2-
import shutil
3-
from pathlib import Path
4-
import openneuro
51
from medimages4tests import base_cache_dir
6-
from ..neuro import OpenneuroSpec
2+
from medimages4tests.utils import retrieve_from_openneuro, OpenneuroSpec
73

84

95
cache_dir = base_cache_dir / "mri" / "neuro" / "t1w"
@@ -18,22 +14,5 @@
1814
}
1915

2016

21-
def get_image(sample_name):
22-
sample = SAMPLES[sample_name]
23-
if cache_dir.exists():
24-
cache_dir.mkdir(parents=True)
25-
out_path = (cache_dir / sample_name).with_suffix(".nii.gz")
26-
if not out_path.exists():
27-
tmpdir = Path(mkdtemp())
28-
openneuro.download(
29-
dataset=sample.dataset,
30-
tag=sample.tag,
31-
target_dir=tmpdir,
32-
include=[sample.path],
33-
)
34-
for ext in (".nii.gz", ".json"):
35-
shutil.copyfile(
36-
(tmpdir / sample.path).with_suffix(ext)
37-
(cache_dir / sample_name).with_suffix(ext)
38-
)
39-
return out_path
17+
def get_image(sample="ds004130-ON01016"):
18+
return retrieve_from_openneuro(SAMPLES[sample], cache_dir / sample)

medimages4tests/utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from tempfile import mkdtemp
2+
import shutil
3+
from pathlib import Path
4+
import openneuro
5+
import attrs
6+
7+
8+
@attrs.define
9+
class OpenneuroSpec:
10+
11+
dataset: str
12+
tag: str
13+
path: Path
14+
15+
16+
def retrieve_from_openneuro(
17+
sample, cache_path, suffixes=(".nii.gz", ".json"), force_download=False
18+
):
19+
if not cache_path.parent.exists():
20+
cache_path.parent.mkdir(parents=True)
21+
out_path = cache_path.with_suffix(suffixes[0])
22+
if not out_path.exists() or force_download:
23+
tmpdir = Path(mkdtemp())
24+
openneuro.download(
25+
dataset=sample.dataset,
26+
tag=sample.tag,
27+
target_dir=tmpdir,
28+
include=[sample.path],
29+
)
30+
for ext in suffixes:
31+
shutil.copyfile(
32+
(tmpdir / sample.path).with_suffix(ext), cache_path.with_suffix(ext)
33+
)
34+
return out_path

tests/test_openneuro.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import nibabel as nb
2+
from medimages4tests.mri.neuro.t1w import get_image
3+
4+
5+
def test_openneuro_retrieve():
6+
7+
nifti_fpath = get_image()
8+
9+
nifti = nb.load(nifti_fpath)
10+
11+
assert nifti.shape == (204, 256, 256)

0 commit comments

Comments
 (0)