Skip to content

Commit

Permalink
Fix pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsmkn committed Jan 6, 2025
1 parent b804072 commit 828a018
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
4 changes: 2 additions & 2 deletions panimg/image_builders/metaio_mhd_mha.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections import defaultdict
from collections.abc import Iterator, Mapping
from pathlib import Path
from typing import DefaultDict, Union
from typing import DefaultDict

from panimg.exceptions import UnconsumedFilesException, ValidationError
from panimg.image_builders.metaio_utils import load_sitk_image, parse_mh_header
Expand Down Expand Up @@ -57,7 +57,7 @@ def detect_mhd_file(headers: dict[str, str], path: Path) -> bool:
raise ValueError("Data container of mhd file is missing")
return True

def detect_mha_file(headers: Mapping[str, Union[str, None]]) -> bool:
def detect_mha_file(headers: Mapping[str, str | None]) -> bool:
data_file = headers.get(element_data_file_key, None)
return data_file == "LOCAL"

Expand Down
2 changes: 1 addition & 1 deletion panimg/image_builders/oct.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _create_itk_fundus_image(
)


def _extract_slice_size(*, img: Union[FDS, FDA]) -> OctDimensions:
def _extract_slice_size(*, img: FDS | FDA) -> OctDimensions:
slice_size_meta_data = Struct(
"unknown" / PaddedString(12, "utf16"),
"xmm" / Float64l,
Expand Down
16 changes: 8 additions & 8 deletions panimg/image_builders/tiff.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import re
from collections import defaultdict
from collections.abc import Iterator
from collections.abc import Callable, Iterator
from dataclasses import dataclass, field
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Callable, DefaultDict, Optional
from typing import DefaultDict
from uuid import UUID, uuid4

import tifffile
Expand Down Expand Up @@ -42,12 +42,12 @@ class GrandChallengeTiffFile:
image_width: int = 0
image_height: int = 0
resolution_levels: int = 0
color_space: Optional[ColorSpace] = None
color_space: ColorSpace | None = None
voxel_width_mm: float = 0
voxel_height_mm: float = 0
associated_files: list[Path] = field(default_factory=list)
min_voxel_value: Optional[int] = None
max_voxel_value: Optional[int] = None
min_voxel_value: int | None = None
max_voxel_value: int | None = None

def validate(self) -> None:
if not self.image_width:
Expand Down Expand Up @@ -76,7 +76,7 @@ def validate(self) -> None:
)

@property
def segments(self) -> Optional[frozenset[int]]:
def segments(self) -> frozenset[int] | None:
if self.min_voxel_value is None or self.max_voxel_value is None:
return None
if (
Expand Down Expand Up @@ -209,7 +209,7 @@ def _extract_tags(
return gc_file


def _get_color_space(*, color_space_string) -> Optional[ColorSpace]:
def _get_color_space(*, color_space_string) -> ColorSpace | None:
color_space_string = color_space_string.upper()

if color_space_string == "MINISBLACK":
Expand Down Expand Up @@ -319,7 +319,7 @@ def _get_vms_files(vms_file: Path):
def _convert(
*,
files: list[Path],
associated_files_getter: Optional[Callable[[Path], list[Path]]],
associated_files_getter: Callable[[Path], list[Path]] | None,
converter,
output_directory: Path,
file_errors: dict[Path, list[str]],
Expand Down
44 changes: 22 additions & 22 deletions panimg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enum import Enum
from functools import cached_property
from pathlib import Path
from typing import Any, NamedTuple, Optional
from typing import Any, NamedTuple
from uuid import UUID, uuid4

import numpy as np
Expand Down Expand Up @@ -132,35 +132,35 @@ class PanImg:
name: str
width: int
height: int
depth: Optional[int]
voxel_width_mm: Optional[float]
voxel_height_mm: Optional[float]
voxel_depth_mm: Optional[float]
timepoints: Optional[int]
resolution_levels: Optional[int]
window_center: Optional[float]
window_width: Optional[float]
depth: int | None
voxel_width_mm: float | None
voxel_height_mm: float | None
voxel_depth_mm: float | None
timepoints: int | None
resolution_levels: int | None
window_center: float | None
window_width: float | None
color_space: ColorSpace
eye_choice: EyeChoice
patient_id: str = ""
patient_name: str = ""
patient_birth_date: Optional[datetime.date] = None
patient_birth_date: datetime.date | None = None
patient_age: str = ""
patient_sex: PatientSex = PatientSex.EMPTY
study_date: Optional[datetime.date] = None
study_date: datetime.date | None = None
study_instance_uid: str = ""
series_instance_uid: str = ""
study_description: str = ""
series_description: str = ""
segments: Optional[frozenset[int]] = None
segments: frozenset[int] | None = None


@dataclass(frozen=True)
class PanImgFile:
image_id: UUID
image_type: ImageType
file: Path
directory: Optional[Path] = None
directory: Path | None = None


@dataclass
Expand Down Expand Up @@ -196,7 +196,7 @@ def height(self) -> int:
return int(self.image.GetHeight())

@property
def depth(self) -> Optional[int]:
def depth(self) -> int | None:
try:
depth = int(self.image.GetDepth())
except (RuntimeError, ValueError):
Expand All @@ -212,7 +212,7 @@ def _extract_first_float(value: str) -> float:
return float(value)

@property
def window_center(self) -> Optional[float]:
def window_center(self) -> float | None:
try:
return self._extract_first_float(
self.image.GetMetaData("WindowCenter")
Expand All @@ -221,7 +221,7 @@ def window_center(self) -> Optional[float]:
return None

@property
def window_width(self) -> Optional[float]:
def window_width(self) -> float | None:
try:
return self._extract_first_float(
self.image.GetMetaData("WindowWidth")
Expand All @@ -230,7 +230,7 @@ def window_width(self) -> Optional[float]:
return None

@property
def timepoints(self) -> Optional[int]:
def timepoints(self) -> int | None:
if self.image.GetDimension() == 4 and self.segments is None:
# Only 4D files that are non-segmentations have timepoints
return int(self.image.GetSize()[3])
Expand Down Expand Up @@ -265,7 +265,7 @@ def add_value_range_meta_data(cls, image: Image): # noqa: B902, N805
return image

@cached_property
def segments(self) -> Optional[frozenset[int]]:
def segments(self) -> frozenset[int] | None:
if (
self.image.GetNumberOfComponentsPerPixel() != 1
or self.image.GetPixelIDValue() not in MASK_TYPE_PIXEL_IDS
Expand Down Expand Up @@ -305,21 +305,21 @@ def color_space(self) -> ColorSpace:
return ITK_COLOR_SPACE_MAP[self.image.GetNumberOfComponentsPerPixel()]

@property
def voxel_width_mm(self) -> Optional[float]:
def voxel_width_mm(self) -> float | None:
if self.spacing_valid:
return float(self.image.GetSpacing()[0])
else:
return None

@property
def voxel_height_mm(self) -> Optional[float]:
def voxel_height_mm(self) -> float | None:
if self.spacing_valid:
return float(self.image.GetSpacing()[1])
else:
return None

@property
def voxel_depth_mm(self) -> Optional[float]:
def voxel_depth_mm(self) -> float | None:
if self.spacing_valid:
try:
return float(self.image.GetSpacing()[2])
Expand Down Expand Up @@ -406,7 +406,7 @@ class TIFFImage(BaseModel):
resolution_levels: int
color_space: ColorSpace
eye_choice: EyeChoice = EyeChoice.NOT_APPLICABLE
segments: Optional[frozenset[int]] = None
segments: frozenset[int] | None = None

model_config = ConfigDict(frozen=True)

Expand Down
6 changes: 3 additions & 3 deletions panimg/panimg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import defaultdict
from collections.abc import Iterable
from pathlib import Path
from typing import DefaultDict, Optional
from typing import DefaultDict

from panimg.exceptions import UnconsumedFilesException
from panimg.image_builders import DEFAULT_IMAGE_BUILDERS
Expand All @@ -17,8 +17,8 @@ def convert(
*,
input_directory: Path,
output_directory: Path,
builders: Optional[Iterable[ImageBuilder]] = None,
post_processors: Optional[Iterable[PostProcessor]] = None,
builders: Iterable[ImageBuilder] | None = None,
post_processors: Iterable[PostProcessor] | None = None,
recurse_subdirectories: bool = True,
) -> PanImgResult:
new_images: set[PanImg] = set()
Expand Down
4 changes: 2 additions & 2 deletions panimg/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Iterator
from pathlib import Path
from typing import Protocol, Union
from typing import Protocol

from panimg.models import (
PanImgFile,
Expand All @@ -13,7 +13,7 @@
class ImageBuilder(Protocol):
def __call__( # noqa: E704
self, *, files: set[Path]
) -> Union[Iterator[SimpleITKImage], Iterator[TIFFImage]]: ...
) -> Iterator[SimpleITKImage] | Iterator[TIFFImage]: ...


class PostProcessor(Protocol):
Expand Down
1 change: 1 addition & 0 deletions tests/test_background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def test_staged_mhd_upload_with_additional_headers(
for original, new in zip(
extract_header_listing(key, original_headers),
extract_header_listing(key, headers),
strict=True,
):
assert original == new, key
else:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_mhd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import shutil
import zlib
from pathlib import Path
from typing import Union

import pytest
import SimpleITK
Expand Down Expand Up @@ -198,8 +197,8 @@ def test_load_sitk_image_with_additional_meta_data(tmpdir, test_img: str):
)
def test_load_sitk_image_with_various_window_formats(
test_img: str,
center: Union[float, list[float]],
width: Union[float, list[float]],
center: float | list[float],
width: float | list[float],
):
src = MHD_WINDOW_DIR / test_img
sitk_image = load_sitk_image(src)
Expand Down

0 comments on commit 828a018

Please sign in to comment.