Skip to content

Commit

Permalink
Merge pull request #24 from ArcanaFramework/syntax-updates
Browse files Browse the repository at this point in the history
update to match changes in fileformats base
  • Loading branch information
tclose authored Feb 22, 2024
2 parents fa39819 + 169ed14 commit 7ba3534
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 32 deletions.
6 changes: 6 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
set_input_validator(True)
from fileformats.medimage.dicom import DicomDir

try:
import pydra.tasks.mrtrix3.v3_0
except ImportError:
OLD_MRTRIX_VERSION = True
else:
OLD_MRTRIX_VERSION = False

# Set DEBUG logging for unittests

Expand Down
29 changes: 15 additions & 14 deletions extras/fileformats/extras/medimage/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pydicom
import numpy as np
from fileformats.core import FileSet
from fileformats.core.utils import gen_filename
from fileformats.core.utils import SampleFileGenerator
from fileformats.medimage import MedicalImage, DicomCollection, DicomDir, DicomSeries
import medimages4tests.dummy.dicom.mri.t1w.siemens.skyra.syngo_d13c

Expand Down Expand Up @@ -42,15 +42,13 @@ def dicom_series_number(collection: DicomCollection) -> str:


@FileSet.generate_sample_data.register
def dicom_dir_generate_sample_data(dcmdir: DicomDir, dest_dir: Path, seed: ty.Union[int, Random] = 0, stem: ty.Optional[str] = None) -> ty.Iterable[Path]:
def dicom_dir_generate_sample_data(
dcmdir: DicomDir,
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
dcm_dir = medimages4tests.dummy.dicom.mri.t1w.siemens.skyra.syngo_d13c.get_image()
# Set series number to random value to make it different
if isinstance(seed, Random):
rng = seed
else:
rng = Random(seed)
series_number = rng.randint(1, SERIES_NUMBER_RANGE)
dest = Path(dest_dir) / gen_filename(seed_or_rng=seed, stem=stem)
series_number = generator.rng.randint(1, SERIES_NUMBER_RANGE)
dest = generator.generate_fspath(DicomDir)
dest.mkdir()
for dcm_file in dcm_dir.iterdir():
dcm = pydicom.dcmread(dcm_file)
Expand All @@ -60,13 +58,16 @@ def dicom_dir_generate_sample_data(dcmdir: DicomDir, dest_dir: Path, seed: ty.Un


@FileSet.generate_sample_data.register
def dicom_series_generate_sample_data(dcm_series: DicomSeries, dest_dir: Path, seed: ty.Union[int, Random] = 0, stem: ty.Optional[str] = None) -> ty.Iterable[Path]:
rng = Random(seed)
dicom_dir = dicom_dir_generate_sample_data(dcm_series, dest_dir=mkdtemp(), seed=rng, stem=None)[0]
stem = gen_filename(rng, stem=stem)
def dicom_series_generate_sample_data(
dcm_series: DicomSeries,
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
dicom_dir: Path = dicom_dir_generate_sample_data(dcm_series, generator=generator)[0]
stem = generator.generate_fspath().stem
fspaths = []
for i, dicom_file in enumerate(dicom_dir.iterdir(), start=1):
fspaths.append(dicom_file.rename(dest_dir / f"{stem}-{i}.dcm"))
fspaths.append(dicom_file.rename(generator.dest_dir / f"{stem}-{i}.dcm"))
dicom_dir.rmdir()
return fspaths


Expand Down
22 changes: 13 additions & 9 deletions extras/fileformats/extras/medimage/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import nibabel
import numpy as np
from fileformats.core import FileSet
from fileformats.core.utils import gen_filename
from fileformats.core.utils import SampleFileGenerator
from fileformats.medimage import MedicalImage, Nifti, NiftiGz, Nifti1, NiftiGzX, NiftiX
import medimages4tests.dummy.nifti

Expand Down Expand Up @@ -33,33 +33,37 @@ def nifti_dims(nifti: Nifti) -> ty.Tuple[int, int, int]:

@FileSet.generate_sample_data.register
def nifti_generate_sample_data(
nifti: Nifti1, dest_dir: Path, seed: ty.Union[int, Random] = 0, stem: ty.Optional[str] = None
nifti: Nifti1,
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
return medimages4tests.dummy.nifti.get_image(
out_file=dest_dir / gen_filename(seed, file_type=Nifti1, stem=stem)
out_file=generator.generate_fspath(file_type=Nifti1)
)


@FileSet.generate_sample_data.register
def nifti_gz_generate_sample_data(
nifti: NiftiGz, dest_dir: Path, seed: ty.Union[int, Random] = 0, stem: ty.Optional[str] = None
nifti: NiftiGz,
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
return medimages4tests.dummy.nifti.get_image(
out_file=dest_dir / gen_filename(seed, file_type=NiftiGz, stem=stem),
out_file=generator.generate_fspath(file_type=NiftiGz),
compressed=True,
)


@FileSet.generate_sample_data.register
def nifti_gz_x_generate_sample_data(
nifti: NiftiGzX, dest_dir: Path, seed: ty.Union[int, Random] = 0, stem: ty.Optional[str] = None
nifti: NiftiGzX,
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
return medimages4tests.mri.neuro.t1w.get_image()
return medimages4tests.mri.neuro.t1w.get_image(out_dir=generator.dest_dir)


@FileSet.generate_sample_data.register
def nifti_x_generate_sample_data(
nifti: NiftiX, dest_dir: Path, seed: ty.Union[int, Random] = 0, stem: ty.Optional[str] = None
nifti: NiftiX,
generator: SampleFileGenerator,
) -> ty.Iterable[Path]:
nifti_gz_x = NiftiGzX(medimages4tests.mri.neuro.t1w.get_image())
nifti_gz_x = NiftiGzX(medimages4tests.mri.neuro.t1w.get_image(out_dir=generator.dest_dir))
return NiftiX.convert(nifti_gz_x)
12 changes: 8 additions & 4 deletions extras/fileformats/extras/medimage/tests/test_dicom_metadata.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import pytest
from fileformats.medimage import DicomSeries, DicomDir
from conftest import OLD_MRTRIX_VERSION


@pytest.mark.xfail(condition=OLD_MRTRIX_VERSION, reason="Old MRtrix version")
def test_dicom_series_metadata(tmp_path):
series = DicomSeries.sample(tmp_path)

# Check series number is not a list
assert not isinstance(series["SeriesNumber"], list)
assert not isinstance(series.metadata["SeriesNumber"], list)
# check the SOP Instance ID has been converted into a list
assert isinstance(series["SOPInstanceUID"], list)
assert isinstance(series.metadata["SOPInstanceUID"], list)


@pytest.mark.xfail(condition=OLD_MRTRIX_VERSION, reason="Old MRtrix version")
def test_dicom_dir_metadata(tmp_path):
series = DicomDir.sample(tmp_path)

# Check series number is not a list
assert not isinstance(series["SeriesNumber"], list)
assert not isinstance(series.metadata["SeriesNumber"], list)
# check the SOP Instance ID has been converted into a list
assert isinstance(series["SOPInstanceUID"], list)
assert isinstance(series.metadata["SOPInstanceUID"], list)
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from fileformats.medimage import (
NiftiGzX,
NiftiGzXBvec,
NiftiBvec,
Analyze,
)
from logging import getLogger
from conftest import OLD_MRTRIX_VERSION


logger = getLogger("fileformats")
Expand All @@ -27,6 +27,7 @@ def test_dicom_to_nifti_select_echo(dummy_magfmap_dicom):
assert nifti_gz_x_e2.metadata["EchoNumber"] == 2


@pytest.mark.xfail(condition=OLD_MRTRIX_VERSION, reason="Old MRtrix version")
def test_dicom_to_nifti_select_suffix(dummy_mixedfmap_dicom):

nifti_gz_x_ph = NiftiGzX.convert(dummy_mixedfmap_dicom, file_postfix="_ph")
Expand All @@ -42,6 +43,7 @@ def test_dicom_to_nifti_select_suffix(dummy_mixedfmap_dicom):
assert list(nifti_gz_x_real.dims()) == [256, 256, 60]


@pytest.mark.xfail(condition=OLD_MRTRIX_VERSION, reason="Old MRtrix version")
def test_dicom_to_nifti_with_extract_volume(dummy_dwi_dicom):

nifti_gz_x_e1 = NiftiGzX.convert(dummy_dwi_dicom, extract_volume=30)
Expand All @@ -57,6 +59,7 @@ def test_dicom_to_nifti_with_jq_edit(dummy_t1w_dicom):
assert nifti_gz_x.metadata["EchoTime"] == 2.07


@pytest.mark.xfail(condition=OLD_MRTRIX_VERSION, reason="Old MRtrix version")
def test_dicom_to_niftix_with_fslgrad(dummy_dwi_dicom):

logger.debug("Performing FSL grad conversion")
Expand All @@ -74,12 +77,13 @@ def test_dicom_to_niftix_with_fslgrad(dummy_dwi_dicom):
assert all(abs(1 - m) < 1e5 for m in bvec_mags)


# @pytest.mark.skip("Mrtrix isn't installed in test environment yet")
@pytest.mark.xfail(condition=OLD_MRTRIX_VERSION, reason="Old MRtrix version")
def test_dicom_to_nifti_as_4d(dummy_t1w_dicom):

nifti_gz_x_e1 = NiftiGzX.convert(dummy_t1w_dicom, to_4d=True)
assert nifti_gz_x_e1.metadata["dim"][0] == 4


@pytest.mark.xfail(condition=OLD_MRTRIX_VERSION, reason="Old MRtrix version")
def test_dicom_to_analyze(dummy_t1w_dicom):
Analyze.convert(dummy_t1w_dicom)
2 changes: 1 addition & 1 deletion fileformats/medimage/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def from_paths(
for dicom in dicoms:
dicom.select_metadata(selected_keys)
series_dict[
(str(dicom["StudyInstanceUID"]), str(dicom["SeriesNumber"]))
(str(dicom.metadata["StudyInstanceUID"]), str(dicom.metadata["SeriesNumber"]))
].append(dicom)
return set([cls(s) for s in series_dict.values()]), remaining

Expand Down
4 changes: 2 additions & 2 deletions fileformats/medimage/tests/test_dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def test_dicom_series_metadata(tmp_path):
series = DicomSeries.sample(tmp_path)

# Check series number is not a list
assert not isinstance(series["SeriesNumber"], list)
assert not isinstance(series.metadata["SeriesNumber"], list)
# check the SOP Instance ID has been converted into a list
assert isinstance(series["SOPInstanceUID"], list)
assert isinstance(series.metadata["SOPInstanceUID"], list)

0 comments on commit 7ba3534

Please sign in to comment.