From a988db273f950b707a8149dfcfa52eacbe07df52 Mon Sep 17 00:00:00 2001 From: miriam-groeneveld Date: Wed, 4 Oct 2023 17:12:40 +0200 Subject: [PATCH] fix writing pyramid levels fix mypy errors --- panimg/contrib/wsi_dcm_to_tiff/dcm_to_tiff.py | 23 ++++++++++--------- panimg/models.py | 9 +++++--- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/panimg/contrib/wsi_dcm_to_tiff/dcm_to_tiff.py b/panimg/contrib/wsi_dcm_to_tiff/dcm_to_tiff.py index f1427d3..a2ce711 100644 --- a/panimg/contrib/wsi_dcm_to_tiff/dcm_to_tiff.py +++ b/panimg/contrib/wsi_dcm_to_tiff/dcm_to_tiff.py @@ -228,6 +228,7 @@ def dcm_to_tiff(input_dir, output_path): with TiffWriter(output_path, bigtiff=True) as tif: tile_size = image.levels[0].default_instance.tile_size.width + test_tile = np.array(image.read_tile(0, (0, 0))) def tiler(getter, level, cols, rows): for row in range(rows): @@ -235,29 +236,27 @@ def tiler(getter, level, cols, rows): im = np.array(getter(level, (col, row))) yield im - for level in range(0, len(image.levels.levels)): - cols = int(math.ceil(image.levels[level].size.width / tile_size)) - rows = int(math.ceil(image.levels[level].size.height / tile_size)) + for level in image.levels: + cols = int(math.ceil(level.size.width / tile_size)) + rows = int(math.ceil(level.size.height / tile_size)) - test_tile = np.array(image.read_tile(0, (0, 0))) dtype = test_tile.dtype shape = ( - image.levels[level].size.height, - image.levels[level].size.width, + level.size.height, + level.size.width, 3, ) - level_tiler = tiler(image.read_tile, level, cols, rows) + level_tiler = tiler(image.read_tile, level.level, cols, rows) extratags = [(274, 3, 1, 1, False)] # Orientation TOPLEFT resolution = ( - int(10 / image.levels[level].pixel_spacing.width), - int(10 / image.levels[level].pixel_spacing.height), - "CENTIMETER", + int(10 / level.pixel_spacing.width), + int(10 / level.pixel_spacing.height), ) - subfiletype = 1 if level != 0 else 0 + subfiletype = 1 if level.level != 0 else 0 tif.write( level_tiler, @@ -268,6 +267,8 @@ def tiler(getter, level, cols, rows): compression="jpeg", subsampling=(1, 1), resolution=resolution, + resolutionunit="CENTIMETER", description="Converted from DICOM", subfiletype=subfiletype, + extratags=extratags, ) diff --git a/panimg/models.py b/panimg/models.py index 1da3842..e48f6bb 100644 --- a/panimg/models.py +++ b/panimg/models.py @@ -10,8 +10,9 @@ import numpy as np import SimpleITK -from pydantic import BaseModel, ConfigDict, field_validator +from pydantic import BaseModel, ConfigDict from pydantic.dataclasses import dataclass +from pydantic.functional_validators import field_validator from SimpleITK import GetArrayViewFromImage, Image, WriteImage from panimg.exceptions import ValidationError @@ -185,7 +186,9 @@ class SimpleITKImage(BaseModel): spacing_valid: bool eye_choice: EyeChoice = EyeChoice.NOT_APPLICABLE - model_config = ConfigDict(arbitrary_types_allowed=True, frozen=True) + model_config: ConfigDict = ConfigDict( + arbitrary_types_allowed=True, frozen=True + ) @property def width(self) -> int: @@ -408,7 +411,7 @@ class TIFFImage(BaseModel): eye_choice: EyeChoice = EyeChoice.NOT_APPLICABLE segments: Optional[FrozenSet[int]] = None - model_config = ConfigDict(frozen=True) + model_config: ConfigDict = ConfigDict(frozen=True) def save(self, output_directory: Path) -> Tuple[PanImg, Set[PanImgFile]]: pk = uuid4()