Skip to content

Commit

Permalink
debugged extras hooks signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Dec 6, 2023
1 parent cdbe8bb commit 9a707fb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion extras/fileformats/extras/medimage/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


@MedicalImage.read_array.register
def dicom_read_array(collection: DicomCollection):
def dicom_read_array(collection: DicomCollection) -> np.ndarray:
image_stack = []
for dcm_file in collection.contents:
image_stack.append(pydicom.dcmread(dcm_file).pixel_array)
Expand Down
6 changes: 3 additions & 3 deletions extras/fileformats/extras/medimage/diffusion.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import numpy as np
from fileformats.medimage.diffusion import DwiEncoding, Bval, Bvec
from fileformats.medimage import DwiEncoding, Bval, Bvec


@Bval.read_array.register
def bval_read_array(bval: Bval):
def bval_read_array(bval: Bval) -> np.ndarray: # noqa
return np.asarray([float(ln) for ln in bval.read_contents().split()])


@DwiEncoding.read_array.register
def bvec_read_array(bvec: Bvec):
def bvec_read_array(bvec: Bvec) -> np.ndarray: # noqa
bvals = bvec.b_values_file.read_array()
directions = np.asarray(
[[float(x) for x in ln.split()] for ln in bvec.read_contents().splitlines()]
Expand Down
13 changes: 7 additions & 6 deletions extras/fileformats/extras/medimage/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,33 @@
import typing as ty
from random import Random
import nibabel
import numpy as np
from fileformats.core import FileSet
from fileformats.core.utils import gen_filename
from fileformats.medimage import MedicalImage, Nifti, NiftiGz, Nifti1, NiftiGzX, NiftiX
import medimages4tests.dummy.nifti


@FileSet.read_metadata.register
def nifti_read_metadata(nifti: Nifti):
def nifti_read_metadata(nifti: Nifti) -> ty.Mapping[str, ty.Any]:
return dict(nibabel.load(nifti.fspath).header)


@MedicalImage.read_array.register
def nifti_data_array(nifti: Nifti):
def nifti_data_array(nifti: Nifti) -> np.ndarray: # noqa
return nibabel.load(nifti.fspath).get_data()


@MedicalImage.vox_sizes.register
def nifti_vox_sizes(nifti: Nifti):
def nifti_vox_sizes(nifti: Nifti) -> ty.Tuple[float, float, float]:
# FIXME: This won't work for 4-D files
return nifti.metadata["pixdim"][1:4]
return tuple(float(d) for d in nifti.metadata["pixdim"][1:4])


@MedicalImage.dims.register
def nifti_dims(nifti: Nifti):
def nifti_dims(nifti: Nifti) -> ty.Tuple[int, int, int]:
# FIXME: This won't work for 4-D files
return nifti.metadata["dim"][1:4]
return tuple(int(d) for d in nifti.metadata["dim"][1:4])


@FileSet.generate_sample_data.register
Expand Down
2 changes: 1 addition & 1 deletion fileformats/medimage/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MedicalImage(FileSet):
binary = True

@hook.extra
def read_array(self):
def read_array(self) -> "numpy.ndarray": # noqa
"""
Returns the binary data of the image in a numpy array
"""
Expand Down
10 changes: 5 additions & 5 deletions fileformats/medimage/diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ class DwiEncoding(File):
iana_mime: ty.Optional[str] = None

@hook.extra
def read_array(self):
def read_array(self) -> "numpy.ndarray": # noqa
"Both the gradient direction and weighting combined into a single Nx4 array"
raise NotImplementedError

@property
def array(self):
def array(self) -> "numpy.ndarray": # noqa
return self.read_array()

@property
def directions(self):
def directions(self) -> "numpy.ndarray": # noqa
"gradient direction and weighting combined into a single Nx4 array"
return self.array[:, :3]

@property
def b_values(self):
def b_values(self) -> "numpy.ndarray": # noqa
"the b-value weighting"
return self.array[:, 3]

Expand All @@ -34,7 +34,7 @@ class Bval(File):
ext = ".bval"

@hook.extra
def read_array(self):
def read_array(self) -> "numpy.ndarray": # noqa
raise NotImplementedError


Expand Down

0 comments on commit 9a707fb

Please sign in to comment.