Skip to content

Commit

Permalink
Fix timepoints determination
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsmkn committed Jul 19, 2023
1 parent 10712d1 commit c6398c3
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# History

## 0.13.1 (2023-07-19)

* Fix timepoints determination

## 0.13.0 (2023-07-17)

* Increased maximum number of segments to 64
Expand Down
8 changes: 5 additions & 3 deletions panimg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import shutil
from enum import Enum
from functools import cached_property
from pathlib import Path
from typing import Any, Dict, FrozenSet, List, NamedTuple, Optional, Set, Tuple
from uuid import UUID, uuid4
Expand Down Expand Up @@ -230,8 +231,9 @@ def window_width(self) -> Optional[float]:

@property
def timepoints(self) -> Optional[int]:
if self.image.GetDimension() == 4:
return int(self.image.GetSize()[-1])
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])
else:
return None

Expand Down Expand Up @@ -262,7 +264,7 @@ def add_value_range_meta_data(cls, image: Image): # noqa: B902, N805

return image

@property
@cached_property
def segments(self) -> Optional[FrozenSet[int]]:
if (
self.image.GetNumberOfComponentsPerPixel() != 1
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "panimg"
version = "0.13.0"
version = "0.13.1"
description = "Conversion of medical images to MHA and TIFF."
license = "Apache-2.0"
authors = ["James Meakin <[email protected]>"]
Expand Down
Binary file modified tests/resources/image10x11x12x13.mha
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/resources/image10x11x12x13.mhd
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ NDims = 4
BinaryData = True
BinaryDataByteOrderMSB = False
CompressedData = True
CompressedDataSize = 39
CompressedDataSize = 40
TransformMatrix = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Offset = -131 -99 -917 0
CenterOfRotation = 0 0 0 0
ElementSpacing = 0.429 0.429 0.5 1
DimSize = 10 11 12 13
AnatomicalOrientation = RAI
AnatomicalOrientation = ????
ElementType = MET_UCHAR
ElementDataFile = image10x11x12x13.zraw
Binary file modified tests/resources/image10x11x12x13.zraw
Binary file not shown.
1 change: 1 addition & 0 deletions tests/test_background_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_staged_4d_mha_and_4d_mhd_upload(images, tmpdir_factory):
assert len(result.new_images) == 1

image = result.new_images.pop()

assert image.timepoints == 13
assert image.depth == 12
assert image.height == 11
Expand Down
4 changes: 2 additions & 2 deletions tests/test_mhd.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def test_parse_header_valid_4d_mhd():
"BinaryData": "True",
"BinaryDataByteOrderMSB": "False",
"CompressedData": "True",
"CompressedDataSize": "39",
"CompressedDataSize": "40",
"TransformMatrix": "1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1",
"Offset": "-131 -99 -917 0",
"CenterOfRotation": "0 0 0 0",
"AnatomicalOrientation": "RAI",
"AnatomicalOrientation": "????",
"ElementSpacing": "0.429 0.429 0.5 1",
"DimSize": "10 11 12 13",
"ElementType": "MET_UCHAR",
Expand Down
17 changes: 15 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def test_built_image_extra_metadata_defaults(tmpdir, caplog):
),
( # 4D image
"image10x11x12x13.mha",
0,
0,
2,
2,
),
],
)
Expand Down Expand Up @@ -245,3 +245,16 @@ def test_invalid_4d(tmp_path_factory):
)
]
}


def test_4d_segmentation_none_timepoints(tmp_path_factory):
result = _build_files(
builder=image_builders.image_builder_mhd,
files={RESOURCE_PATH / "segments" / "4D_1_1_1_5_UInt8.mha"},
output_directory=tmp_path_factory.mktemp("output"),
)

new_image = result.new_images.pop()

assert new_image.segments == frozenset({1, 2, 3, 4, 5})
assert new_image.timepoints is None

0 comments on commit c6398c3

Please sign in to comment.