Skip to content

Commit

Permalink
Merge pull request #2 from Australian-Imaging-Service/openneuro-download
Browse files Browse the repository at this point in the history
Openneuro download
  • Loading branch information
tclose authored Oct 25, 2022
2 parents 6164ba8 + 2fa2d18 commit 5d2c6bb
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 24 deletions.
21 changes: 19 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,29 @@ or include in your package's ``test_requires``.
Usage
-----

Create a pytest fixture consisting of a dummy image with field-map metadata in DICOM format

.. code-block:: python
# Import medimages4tests generator functions
from medimages4tests.dicom.mri.fmap.ge.discovery_mr888.dv26_0_r05_2008a import sample_image
from medimages4tests.dummy.dicom.mri.fmap.ge.discovery_mr888.dv26_0_r05_2008a import get_image
# Return generated images in pytest fixtures (or alternative test framework)
@pytest.fixture()
def ge_dicom_fmap():
return sample_image()
return get_image()
Create a dummy NIfTI image

.. code-block:: python
import numpy
# Import `get_image` function
from medimages4tests.dummy.nifti import get_image
# Create dummy nifti image of 10x10x10 containing all ones
@pytest.fixture()
def ones_nifti():
return get_image(
data=numpy.ones((10, 10, 10))
)
4 changes: 2 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from importlib import import_module
import pytest

base_dicom_dir = Path(__file__).parent / 'medimages4tests' / 'dicom'
base_dicom_dir = Path(__file__).parent / 'medimages4tests' / 'dummy' / 'dicom'
dicom_modules = ['__'.join(p.relative_to(base_dicom_dir).with_suffix('').parts)
for p in base_dicom_dir.glob('**/*.py')
if p.stem != 'base']
Expand All @@ -17,7 +17,7 @@ def work_dir():

@pytest.fixture(params=dicom_modules)
def dicom_module(request):
module_path = 'medimages4tests.dicom.' + '.'.join(request.param.split('__'))
module_path = 'medimages4tests.dummy.dicom.' + '.'.join(request.param.split('__'))
return import_module(module_path)


Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import shutil
from copy import copy
import pydicom.dataset
from .. import base_cache_dir
from ... import base_cache_dir

cache_dir = base_cache_dir / "dicom"
cache_dir = base_cache_dir / "dummy" / "dicom"
dicom_pkg_dir = Path(__file__).parent


Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

from medimages4tests.dicom.base import generate_dicom, default_dicom_dir
from medimages4tests.dummy.dicom.base import generate_dicom, default_dicom_dir


def sample_image(out_dir=default_dicom_dir(__file__)):
def get_image(out_dir=default_dicom_dir(__file__)):
return generate_dicom(out_dir, num_vols, constant_hdr,
collated_data, varying_hdr)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from medimages4tests.dicom.base import generate_dicom, default_dicom_dir
from medimages4tests.dummy.dicom.base import generate_dicom, default_dicom_dir


def sample_image(out_dir=default_dicom_dir(__file__)):
def get_image(out_dir=default_dicom_dir(__file__)):
return generate_dicom(out_dir, num_vols, constant_hdr,
collated_data, varying_hdr)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

from medimages4tests.dicom.base import generate_dicom, default_dicom_dir
from medimages4tests.dummy.dicom.base import generate_dicom, default_dicom_dir


def sample_image(out_dir=default_dicom_dir(__file__)):
def get_image(out_dir=default_dicom_dir(__file__)):
return generate_dicom(out_dir, num_vols, constant_hdr,
collated_data, varying_hdr)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from medimages4tests.dicom.base import generate_dicom, default_dicom_dir
from medimages4tests.dummy.dicom.base import generate_dicom, default_dicom_dir


def sample_image(out_dir=default_dicom_dir(__file__)):
def get_image(out_dir=default_dicom_dir(__file__)):
return generate_dicom(out_dir, num_vols, constant_hdr,
collated_data, varying_hdr)

Expand Down
8 changes: 6 additions & 2 deletions medimages4tests/nifti.py → medimages4tests/dummy/nifti.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import tempfile
from pathlib import Path
import gzip
import shutil
import numpy as np
import nibabel as nb


def sample_image(
out_file: Path,
def get_image(
out_file: Path = None,
data: np.ndarray = None,
vox_sizes=(1.0, 1.0, 1.0),
qform=(1, 2, 3, 1),
compressed=False,
) -> Path:
"""Create a random Nifti file to satisfy BIDS parsers"""
if out_file is None:
out_file = Path(tempfile.mkdtemp()) / "sample.nii"

if data is None:
data = np.random.randint(0, 1, size=[10, 10, 10])

Expand Down
Empty file added medimages4tests/mri/__init__.py
Empty file.
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions medimages4tests/mri/neuro/t1w.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from medimages4tests import base_cache_dir
from medimages4tests.utils import retrieve_from_openneuro, OpenneuroSpec


cache_dir = base_cache_dir / "mri" / "neuro" / "t1w"


SAMPLES = {
"ds004130-ON01016": OpenneuroSpec(
dataset="ds004130",
tag="1.0.0",
path="sub-ON01016/anat/sub-ON01016_acq-fspgr_run-01_T1w",
)
}


def get_image(sample="ds004130-ON01016"):
return retrieve_from_openneuro(SAMPLES[sample], cache_dir / sample)
34 changes: 34 additions & 0 deletions medimages4tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from tempfile import mkdtemp
import shutil
from pathlib import Path
import openneuro
import attrs


@attrs.define
class OpenneuroSpec:

dataset: str
tag: str
path: Path


def retrieve_from_openneuro(
sample, cache_path, suffixes=(".nii.gz", ".json"), force_download=False
):
if not cache_path.parent.exists():
cache_path.parent.mkdir(parents=True)
out_path = cache_path.with_suffix(suffixes[0])
if not out_path.exists() or force_download:
tmpdir = Path(mkdtemp())
openneuro.download(
dataset=sample.dataset,
tag=sample.tag,
target_dir=tmpdir,
include=[sample.path],
)
for ext in suffixes:
shutil.copyfile(
(tmpdir / sample.path).with_suffix(ext), cache_path.with_suffix(ext)
)
return out_path
4 changes: 2 additions & 2 deletions scripts/import_dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def generate_python_code(dpath: Path, image_type: str):
collated_data=json.dumps(collated_data))


FILE_TEMPLATE = """from medimages4tests.dicom.base import generate_dicom, default_dicom_dir
FILE_TEMPLATE = """from medimages4tests.dummy.dicom.base import generate_dicom, default_dicom_dir
def sample_image(out_dir=default_dicom_dir(__file__)):
def get_image(out_dir=default_dicom_dir(__file__)):
return generate_dicom(out_dir, num_vols, constant_hdr,
collated_data, varying_hdr)
Expand Down
23 changes: 22 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,25 @@ VCS = git
style = pep440
versionfile_source = medimages4tests/_version.py
versionfile_build = medimages4tests/_version.py
tag_prefix =
tag_prefix =


[flake8]
doctests = True
exclude =
**/__init__.py
*build/
docs/sphinxext/
docs/tools/
docs/conf.py
arcana/_version.py
versioneer.py
docs/source/conf.py
max-line-length = 88
select = C,E,F,W,B,B950
extend-ignore = E203,E501
per-file-ignores =
setup.py:F401

[codespell]
ignore-words = .codespell-ignorewords
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
long_description=open('README.rst').read(),
install_requires=[
'pydicom>=1.0.2',
"nibabel>=4.0.1"],
"nibabel>=4.0.1",
"openneuro-py>=2022.2.0",
"attrs>=21.4.0"],
extras_require={
'test': [
'pytest>=5.4.3',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_dicom_creation(dicom_module):
out_dir = Path(tempfile.mkdtemp())

# Generate DICOM files
dicom_module.sample_image(out_dir)
dicom_module.get_image(out_dir)

assert len(list(out_dir.glob('*.dcm'))) == dicom_module.num_vols

Expand Down
6 changes: 3 additions & 3 deletions tests/test_nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import shutil
import nibabel as nb
import numpy as np
from medimages4tests.nifti import sample_image
from medimages4tests.dummy.nifti import get_image


def test_nifti(work_dir):

nifti_fpath = sample_image(work_dir / "sample.nii")
nifti_fpath = get_image(work_dir / "sample.nii")

nifti = nb.load(nifti_fpath)

Expand All @@ -16,7 +16,7 @@ def test_nifti(work_dir):

def test_nifti_compressed(work_dir):

gz_fpath = sample_image(work_dir / "sample.nii.gz", compressed=True)
gz_fpath = get_image(work_dir / "sample.nii.gz", compressed=True)
uncompressed_fpath = work_dir / "nifti.nii"

with gzip.open(gz_fpath, 'rb') as f_in:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_openneuro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import nibabel as nb
from medimages4tests.mri.neuro.t1w import get_image


def test_openneuro_retrieve():

nifti_fpath = get_image()

nifti = nb.load(nifti_fpath)

assert nifti.shape == (204, 256, 256)

0 comments on commit 5d2c6bb

Please sign in to comment.