Skip to content

Commit

Permalink
Merge pull request #23 from tclose/mif-gz-converter
Browse files Browse the repository at this point in the history
added converter between mrtrix and mrtrix-gz
  • Loading branch information
tclose authored Sep 10, 2024
2 parents 4cff451 + 7a822f8 commit 08d2287
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 27 deletions.
9 changes: 8 additions & 1 deletion related-packages/fileformats-extras/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
import tempfile
import pytest
from fileformats.medimage import DicomDir
from fileformats.medimage import DicomDir, Nifti

# Set DEBUG logging for unittests

Expand Down Expand Up @@ -43,3 +43,10 @@ def dummy_dwi_dicom():
import medimages4tests.dummy.dicom.mri.dwi.siemens.skyra.syngo_d13c as module

return DicomDir(module.get_image())


@pytest.fixture(scope="session")
def dummy_nifti():
import medimages4tests.dummy.nifti as module

return Nifti(module.get_image())
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fileformats.medimage_mrtrix3 import (
ImageFormat as MrtrixImage,
ImageHeader as MrtrixImageHeader,
ImageFormatGz as MrtrixImageGz,
)

try:
Expand All @@ -16,6 +17,18 @@
in_out_file_kwargs = {}


@converter(
source_format=MedicalImage,
target_format=MrtrixImageGz,
out_ext=MrtrixImageGz.ext,
**in_out_file_kwargs,
)
@converter(
source_format=MedicalImage,
target_format=MrtrixImageHeader,
out_ext=MrtrixImageHeader.ext,
**in_out_file_kwargs,
)
@converter(
source_format=MedicalImage,
target_format=MrtrixImage,
Expand All @@ -40,25 +53,25 @@ def mrconvert(name, out_ext: str, **kwargs):
return MrConvert(name=name, out_file="out" + out_ext, **kwargs)


@converter(
source_format=MedicalImage,
target_format=MrtrixImageHeader,
out_ext=MrtrixImageHeader.ext,
**in_out_file_kwargs,
)
def mrconvert2(name, out_ext: str, **kwargs):
"""Initiate an MRConvert task with the output file extension set
# @converter(
# source_format=MedicalImage,
# target_format=MrtrixImageHeader,
# out_ext=MrtrixImageHeader.ext,
# **in_out_file_kwargs,
# )
# def mrconvert2(name, out_ext: str, **kwargs):
# """Initiate an MRConvert task with the output file extension set

Parameters
----------
name : str
name of the converter task
out_ext : str
extension of the output file, used by MRConvert to determine the desired format
# Parameters
# ----------
# name : str
# name of the converter task
# out_ext : str
# extension of the output file, used by MRConvert to determine the desired format

Returns
-------
pydra.ShellCommandTask
the converter task
"""
return MrConvert(name=name, out_file="out" + out_ext, **kwargs)
# Returns
# -------
# pydra.ShellCommandTask
# the converter task
# """
# return MrConvert(name=name, out_file="out" + out_ext, **kwargs)
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import os
import sys
from unittest import mock
import typing as ty
from pathlib import Path
import numpy as np
import numpy.typing
from medimages4tests.dummy.nifti import get_image as get_dummy_nifti
from fileformats.core import FileSet, SampleFileGenerator, extra_implementation
from fileformats.medimage import MedicalImage, Nifti1
from fileformats.medimage_mrtrix3 import ImageFormat

if sys.version_info >= (3, 9):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias


DataArrayType: TypeAlias = (
"numpy.typing.NDArray[ty.Union[np.floating[ty.Any], np.integer[ty.Any]]]"
)


@extra_implementation(FileSet.generate_sample_data)
def generate_mrtrix_sample_data(
Expand All @@ -21,7 +33,7 @@ def generate_mrtrix_sample_data(


@extra_implementation(MedicalImage.read_array)
def mrtrix_read_array(mif: ImageFormat) -> np.ndarray:
def mrtrix_read_array(mif: ImageFormat) -> DataArrayType:
raise NotImplementedError(
"Need to work out how to use the metadata to read the array in the correct order"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fileformats.medimage import NiftiBvec
from fileformats.medimage_mrtrix3 import ImageFormat, ImageHeader
from fileformats.medimage_mrtrix3 import ImageFormat, ImageHeader, ImageFormatGz


# @pytest.mark.xfail(reason="not sure what the reason is at this stage, might be bug in Pydra")
Expand All @@ -15,3 +15,9 @@ def test_dicom_to_mrtrix_image(dummy_dwi_dicom):

def test_dicom_to_mrtrix_image_header(dummy_dwi_dicom):
ImageHeader.convert(dummy_dwi_dicom)


def test_mif_to_mifgz(dummy_nifti):
mif = ImageFormat.convert(dummy_nifti)
mif_gz = ImageFormatGz.convert(mif)
ImageFormat.convert(mif_gz)
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@
ImageHeaderB,
)
from .track import Tracks


__all__ = [
"__version__",
"ImageIn",
"ImageOut",
"BFile",
"Tracks",
"NiftiB",
"NiftiGzB",
"NiftiGzXB",
"NiftiXB",
"ImageFormatB",
"ImageFormatGzB",
"ImageHeaderB",
"ImageFormat",
"ImageFormatGz",
"ImageHeader",
"ImageDataFile",
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import typing as ty
from fileformats.core import FileSet, extra_implementation
from fileformats.generic import File
from fileformats.application import Gzip
from fileformats.application.archive import BaseGzip
from fileformats.core.mixin import WithMagicNumber
from fileformats.core.exceptions import FormatMismatchError
import fileformats.medimage
Expand Down Expand Up @@ -61,8 +61,9 @@ def data_file(self):
return self


class ImageFormatGz(Gzip[ImageFormat]):
class ImageFormatGz(fileformats.medimage.MedicalImage, BaseGzip):

archived_type = ImageFormat
iana_mime = "application/x-mrtrix-image-format-gz"
ext = ".mif.gz"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import typing as ty
from fileformats.application import Dicom
from fileformats.medimage import (
Expand All @@ -10,6 +11,10 @@
Mgh,
MghGz,
Analyze,
NiftiBvec,
NiftiGzBvec,
NiftiXBvec,
NiftiGzXBvec,
)
from .image import ImageFormat, ImageHeader, ImageFormatGz
from .dwi import (
Expand All @@ -22,8 +27,13 @@
ImageHeaderB,
)

if sys.version_info >= (3, 9):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias

ImageIn = ty.Union[

ImageIn: TypeAlias = ty.Union[
ImageFormat,
ImageFormatGz,
ImageHeader,
Expand All @@ -37,12 +47,16 @@
NiftiX,
Nifti1,
Nifti2,
NiftiB,
NiftiGzB,
NiftiGzXB,
NiftiXB,
Mgh,
MghGz,
Analyze,
]

ImageOut = ty.Union[
ImageOut: TypeAlias = ty.Union[
ImageFormat,
ImageFormatGz,
ImageHeader,
Expand All @@ -54,6 +68,14 @@
NiftiX,
Nifti1,
Nifti2,
NiftiB,
NiftiGzB,
NiftiGzXB,
NiftiXB,
NiftiBvec,
NiftiGzBvec,
NiftiXBvec,
NiftiGzXBvec,
Mgh,
MghGz,
Analyze,
Expand Down

0 comments on commit 08d2287

Please sign in to comment.