Skip to content

Commit

Permalink
adding generators for contrast-specific nifti files
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed May 29, 2024
1 parent 3123725 commit d91a267
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 14 deletions.
96 changes: 83 additions & 13 deletions extras/fileformats/extras/medimage/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
NiftiGz,
Nifti1,
NiftiGzX,
NiftiGzXBvec,
NiftiXBvec,
NiftiX,
T1w,
Mri,
Fmri,
Dmri,
Brain,
)
import medimages4tests.dummy.nifti
import medimages4tests.mri.neuro.t1w
import medimages4tests.mri.neuro.dwi
import medimages4tests.mri.neuro.bold


@FileSet.read_metadata.register
Expand Down Expand Up @@ -67,17 +71,15 @@ def t1w_nifti_gz_x_generate_sample_data(
nifti: NiftiGzX[T1w],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
return medimages4tests.mri.neuro.t1w.get_image(out_dir=generator.dest_dir)
return _get_t1w_nifti_gz_x(generator)


@FileSet.generate_sample_data.register
def t1w_nifti_x_generate_sample_data(
nifti: NiftiX[T1w],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
nifti_gz_x = NiftiGzX(
medimages4tests.mri.neuro.t1w.get_image(out_dir=generator.dest_dir)
)
nifti_gz_x = NiftiGzX(_get_t1w_nifti_gz_x(generator))
return NiftiX.convert(nifti_gz_x)


Expand All @@ -86,17 +88,15 @@ def t1w_brain_nifti_gz_x_generate_sample_data(
nifti: NiftiGzX[T1w, Brain],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
return medimages4tests.mri.neuro.t1w.get_image(out_dir=generator.dest_dir)
return _get_t1w_nifti_gz_x(generator)


@FileSet.generate_sample_data.register
def t1w_brain_nifti_x_generate_sample_data(
nifti: NiftiX[T1w, Brain],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
nifti_gz_x = NiftiGzX(
medimages4tests.mri.neuro.t1w.get_image(out_dir=generator.dest_dir)
)
nifti_gz_x = NiftiGzX(_get_t1w_nifti_gz_x(generator))
return NiftiX.convert(nifti_gz_x)


Expand All @@ -105,15 +105,85 @@ def fmri_brain_nifti_gz_x_generate_sample_data(
nifti: NiftiGzX[Fmri, Brain],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
return medimages4tests.mri.neuro.t1w.get_image(out_dir=generator.dest_dir)
return _get_fmri_nifti_gz_x(generator)


@FileSet.generate_sample_data.register
def fmri_brain_nifti_x_generate_sample_data(
nifti: NiftiX[Fmri, Brain],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
nifti_gz_x = NiftiGzX(
medimages4tests.mri.neuro.t1w.get_image(out_dir=generator.dest_dir)
)
nifti_gz_x = NiftiGzX(_get_fmri_nifti_gz_x(generator))
return NiftiX.convert(nifti_gz_x)


@FileSet.generate_sample_data.register
def fmri_nifti_gz_x_generate_sample_data(
nifti: NiftiGzX[Fmri],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
return _get_fmri_nifti_gz_x(generator)


@FileSet.generate_sample_data.register
def fmri_nifti_x_generate_sample_data(
nifti: NiftiX[Fmri],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
nifti_gz_x = NiftiGzX(_get_fmri_nifti_gz_x(generator))
return NiftiX.convert(nifti_gz_x)


@FileSet.generate_sample_data.register
def dmri_brain_nifti_gz_x_generate_sample_data(
nifti: NiftiGzXBvec[Dmri, Brain],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
return _get_dmri_nifti_gz_x(generator)


@FileSet.generate_sample_data.register
def dmri_brain_nifti_x_generate_sample_data(
nifti: NiftiXBvec[Dmri, Brain],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
nifti_gz_x = NiftiGzX(_get_dmri_nifti_gz_x(generator))
return NiftiX.convert(nifti_gz_x)


@FileSet.generate_sample_data.register
def dmri_nifti_gz_x_generate_sample_data(
nifti: NiftiGzXBvec[Dmri],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
return _get_dmri_nifti_gz_x(generator)


@FileSet.generate_sample_data.register
def dmri_nifti_x_generate_sample_data(
nifti: NiftiXBvec[Dmri],
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
nifti_gz_x = NiftiGzXBvec(_get_dmri_nifti_gz_x(generator))
return NiftiX.convert(nifti_gz_x)


def _get_t1w_nifti_gz_x(generator: SampleFileGenerator) -> ty.Iterable[Path]:
sample = generator.seed if generator.seed else "ds004130-ON01016"
return medimages4tests.mri.neuro.t1w.get_image(
out_dir=generator.dest_dir, sample=sample
)


def _get_fmri_nifti_gz_x(generator: SampleFileGenerator) -> ty.Iterable[Path]:
sample = generator.seed if generator.seed else "ds002014-01"
return medimages4tests.mri.neuro.bold.get_image(
out_dir=generator.dest_dir, sample=sample
)


def _get_dmri_nifti_gz_x(generator: SampleFileGenerator) -> ty.Iterable[Path]:
sample = generator.seed if generator.seed else "ds004024-CON031"
return medimages4tests.mri.neuro.dwi.get_image(
out_dir=generator.dest_dir, sample=sample
)
16 changes: 16 additions & 0 deletions extras/fileformats/extras/medimage/tests/test_generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from fileformats.medimage import NiftiGzX, NiftiGzXBvec, T1w, Dmri, Fmri


def test_t1w_generator():
img = NiftiGzX[T1w].sample()
assert len(img.dims()) == 3


def test_fmri_generator():
img = NiftiGzX[Fmri].sample()
assert len(img.dims()) == 3


def test_dmri_generator():
img = NiftiGzXBvec[Dmri].sample()
assert len(img.dims()) == 4
1 change: 1 addition & 0 deletions fileformats/medimage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
Mri,
Pet,
Dti,
Dmri,
Fmri,
Cr,
Ct,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .. import CrossSectionalProcedure
from fileformats.medimage.contents.imaging.modality import MagneticResonanceImaging
from .....imaging.modality import MagneticResonanceImaging


class MrProcedure(CrossSectionalProcedure, MagneticResonanceImaging):
Expand Down
File renamed without changes.

0 comments on commit d91a267

Please sign in to comment.