Skip to content

Commit

Permalink
updated to use validated_property decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Sep 16, 2024
1 parent c524bab commit a68e448
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 49 deletions.
1 change: 1 addition & 0 deletions .codespell-ignorewords
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nd
te
ptd
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ repos:
--non-interactive,
]
exclude: tests
additional_dependencies: [pytest, fileformats, pydicom, nibabel, attrs]
additional_dependencies:
- fileformats>=0.13.0
- pytest
- pydicom
- nibabel
- attrs
3 changes: 0 additions & 3 deletions fileformats/medimage/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import typing
import typing as ty
import logging
from fileformats.core import extra, FileSet, mtime_cached_property
Expand Down Expand Up @@ -31,14 +30,12 @@

class MedicalImage(WithClassifiers, FileSet):

iana_mime: ty.Optional[str] = None
INCLUDE_HDR_KEYS: ty.Optional[ty.Tuple[str, ...]] = None
IGNORE_HDR_KEYS: ty.Optional[ty.Tuple[str, ...]] = None
binary = True
classifiers_attr_name = "image_contents"
image_contents = ()
allowed_classifiers = (ContentsClassifier,)
multiple_classifiers = True
exclusive_classifiers = (ImagingModality, AnatomicalEntity, Derivative)

@extra
Expand Down
17 changes: 7 additions & 10 deletions fileformats/medimage/diffusion.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import typing
from fileformats.core import extra
from fileformats.core import extra, validated_property
from fileformats.core.typing import TypeAlias
from fileformats.core.mixin import WithAdjacentFiles
from fileformats.generic import File
from fileformats.generic import BinaryFile
from .nifti import NiftiGzX, NiftiGz, Nifti1, NiftiX


Expand All @@ -14,10 +14,7 @@
) # In Py<3.9 this is problematic "numpy.typing.NDArray[numpy.floating[typing.Any]]"


class DwiEncoding(File):

iana_mime: typing.Optional[str] = None

class DwiEncoding:
@extra
def read_array(self) -> EncodingArrayType:
"Both the gradient direction and weighting combined into a single Nx4 array"
Expand All @@ -35,7 +32,7 @@ def b_values(self) -> EncodingArrayType:
return self.array()[:, 3]


class Bval(File):
class Bval(BinaryFile):

ext = ".bval"

Expand All @@ -44,19 +41,19 @@ def read_array(self) -> EncodingArrayType:
raise NotImplementedError


class Bvec(WithAdjacentFiles, DwiEncoding):
class Bvec(WithAdjacentFiles, DwiEncoding, BinaryFile):
"""FSL-style diffusion encoding, in two separate files"""

ext = ".bvec"

@property
@validated_property
def b_values_file(self) -> Bval:
return Bval(self.select_by_ext(Bval))


# NIfTI file format gzipped with BIDS side car
class WithBvec(WithAdjacentFiles):
@property
@validated_property
def encoding(self) -> Bvec:
return Bvec(self.select_by_ext(Bvec)) # type: ignore[attr-defined]

Expand Down
11 changes: 6 additions & 5 deletions fileformats/medimage/misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fileformats.application import Gzip
from fileformats.generic import File
from fileformats.generic import BinaryFile
from fileformats.core import validated_property
from fileformats.core.mixin import WithSeparateHeader, WithMagicVersion
from .base import MedicalImage
from fileformats.core.exceptions import FormatMismatchError
Expand All @@ -10,18 +11,18 @@
# ==================


class AnalyzeHeader(File):
class AnalyzeHeader(BinaryFile):

ext = ".hdr"


class Analyze(WithSeparateHeader, MedicalImage, File):
class Analyze(WithSeparateHeader, MedicalImage, BinaryFile):

ext = ".img"
header_type = AnalyzeHeader


class Mgh(WithMagicVersion, File):
class Mgh(WithMagicVersion, BinaryFile):
"""
FreeSurfer 4-dimensional brain images
Expand All @@ -31,7 +32,7 @@ class Mgh(WithMagicVersion, File):
ext = ".mgh"
magic_pattern = rb"(....)" # First integer is the version string

@property
@validated_property
def _is_supported_version(self) -> None:
assert isinstance(self.version, str)
if int(self.version) != 1:
Expand Down
11 changes: 5 additions & 6 deletions fileformats/medimage/nifti.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import typing as ty
from fileformats.generic import File
from fileformats.generic import BinaryFile
from fileformats.core import validated_property
from fileformats.core.mixin import WithSideCars, WithMagicNumber, WithAdjacentFiles
from fileformats.application import Json
from fileformats.application.archive import BaseGzip
from .base import MedicalImage


class Nifti(MedicalImage, File):
class Nifti(MedicalImage, BinaryFile):

ext: str = ".nii"
iana_mime: ty.Optional[str] = None


class WithBids(WithSideCars):

primary_type = Nifti
side_car_types = (Json,)

@property
@validated_property
def json_file(self) -> Json:
return Json(self.select_by_ext(Json)) # type: ignore[attr-defined]

Expand Down Expand Up @@ -61,6 +60,6 @@ class NiftiWithDataFile(WithAdjacentFiles, Nifti1):
magic_number = "6E693100"
alternate_exts = (".hdr",)

@property
@validated_property
def data_file(self) -> NiftiDataFile:
return NiftiDataFile(self.select_by_ext(NiftiDataFile))
6 changes: 3 additions & 3 deletions fileformats/medimage/raw/mri/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from fileformats.generic import File
from fileformats.generic import BinaryFile


class Kspace(File):
class Kspace(BinaryFile):

binary = True
# iana_mime = None
pass


class Rda(File):
class Rda(BinaryFile):
"""MRS format"""

ext = ".rda"
Expand Down
17 changes: 3 additions & 14 deletions fileformats/medimage/raw/pet/base.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
from fileformats.generic import File
from fileformats.generic import BinaryFile


class PetRawData(File):

binary = True
# iana_mime = None
pass
class PetRawData(BinaryFile):
"""Base class for raw PET data files"""


class PetListMode(PetRawData):
"raw projection data"
# iana_mime = None
pass


class PetSinogram(PetRawData):
"histogrammed projection data in a reconstruction-friendly format"
# iana_mime = None
pass


class PetCountRate(PetRawData):
"number of prompt/random/single events per unit time"
# iana_mime = None
pass


class PetNormalisation(PetRawData):
"normalisation scan or the current cross calibration factor"
# iana_mime = None
pass
6 changes: 3 additions & 3 deletions fileformats/medimage/raw/pet/siemens.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import io
from fileformats.generic import TypedSet
from fileformats.core.decorators import mtime_cached_property
from fileformats.core import mtime_cached_property, validated_property
from fileformats.core.mixin import WithMagicNumber
from .base import (
PetRawData,
Expand Down Expand Up @@ -32,10 +32,10 @@ class Vnd_Siemens_Biograph128Vision_Vr20b_PetRawData(WithMagicNumber, PetRawData
def dicom_header_size(self) -> int:
with self.open() as f:
f.seek(self.dcm_hdr_size_int_offset, io.SEEK_END)
dcm_hdr_size_bytes: bytes = f.read(self.sizeof_dcm_hdr_size_int) # type: ignore[assignment]
dcm_hdr_size_bytes: bytes = f.read(self.sizeof_dcm_hdr_size_int)

Check warning on line 35 in fileformats/medimage/raw/pet/siemens.py

View check run for this annotation

Codecov / codecov/patch

fileformats/medimage/raw/pet/siemens.py#L35

Added line #L35 was not covered by tests
return int.from_bytes(dcm_hdr_size_bytes, "little")

@property
@validated_property
def dicom_header_offset(self) -> int:
dcm_hdr_size: int = self.dicom_header_size
return -dcm_hdr_size + self.dcm_hdr_size_int_offset
Expand Down
4 changes: 1 addition & 3 deletions fileformats/medimage/surface.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import typing as ty
from fileformats.core import FileSet
from fileformats.application import Xml


class SurfaceMesh(FileSet):

iana_mime: ty.Optional[str] = None
...


class Gifti(SurfaceMesh, Xml):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ doctests = true
per-file-ignores = ["__init__.py:F401"]
max-line-length = 88
select = "C,E,F,W,B,B950"
extend-ignore = ['E203', 'E501', 'E129', 'W503']
extend-ignore = ['E203', 'E501', 'E129', 'W503', 'E701']

0 comments on commit a68e448

Please sign in to comment.