Skip to content

Commit

Permalink
fix writing pyramid levels
Browse files Browse the repository at this point in the history
fix mypy errors
  • Loading branch information
miriam-groeneveld committed Oct 4, 2023
1 parent 4a2d4fb commit a988db2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
23 changes: 12 additions & 11 deletions panimg/contrib/wsi_dcm_to_tiff/dcm_to_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,36 +228,35 @@ 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):
for col in range(cols):
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,
Expand All @@ -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,
)
9 changes: 6 additions & 3 deletions panimg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a988db2

Please sign in to comment.