Skip to content

Commit 507776c

Browse files
committed
simplified segment data triplet setter
1 parent cd391fe commit 507776c

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

src/pydcmqi/segimage.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ def get_min_max_values(image: sitk.Image) -> Tuple[float, float]:
2323
return filter.GetMinimum(), filter.GetMaximum()
2424

2525

26-
def _path(path: Union[str, Path]) -> Path:
26+
def _path(path: str | Path) -> Path:
2727
if isinstance(path, str):
2828
return Path(path)
29-
elif isinstance(path, Path):
29+
if isinstance(path, Path):
3030
return path
31-
else:
32-
raise ValueError("Invalid path type.")
31+
raise ValueError("Invalid path type.")
3332

3433

3534
# --== class definitions ==--
@@ -261,6 +260,13 @@ def segmentAlgorithmType(self) -> str:
261260
def segmentAlgorithmType(self, segmentAlgorithmType: str) -> None:
262261
self._data["SegmentAlgorithmType"] = segmentAlgorithmType
263262

263+
def _triplet_setter(self, key: str, value: Union[Tuple[str, str, str], Triplet]):
264+
if isinstance(value, tuple):
265+
value = Triplet.fromTuple(value)
266+
assert isinstance(value, Triplet)
267+
self._data[key] = value.asdict()
268+
269+
264270
@property
265271
def segmentedPropertyCategory(self) -> Triplet:
266272
return self._triplet_factory("SegmentedPropertyCategoryCodeSequence")
@@ -269,10 +275,7 @@ def segmentedPropertyCategory(self) -> Triplet:
269275
def segmentedPropertyCategory(
270276
self, value: Union[Tuple[str, str, str], Triplet]
271277
) -> None:
272-
if isinstance(value, tuple):
273-
value = Triplet.fromTuple(value)
274-
assert isinstance(value, Triplet)
275-
self._data["SegmentedPropertyCategoryCodeSequence"] = value.asdict()
278+
self._triplet_setter("SegmentedPropertyCategoryCodeSequence", value)
276279

277280
@property
278281
def segmentedPropertyType(self) -> Triplet:
@@ -282,10 +285,7 @@ def segmentedPropertyType(self) -> Triplet:
282285
def segmentedPropertyType(
283286
self, value: Union[Tuple[str, str, str], Triplet]
284287
) -> None:
285-
if isinstance(value, tuple):
286-
value = Triplet.fromTuple(value)
287-
assert isinstance(value, Triplet)
288-
self._data["SegmentedPropertyTypeCodeSequence"] = value.asdict()
288+
self._triplet_setter("SegmentedPropertyTypeCodeSequence", value)
289289

290290
@property
291291
def segmentedPropertyTypeModifier(self) -> Triplet:
@@ -295,10 +295,7 @@ def segmentedPropertyTypeModifier(self) -> Triplet:
295295
def segmentedPropertyTypeModifier(
296296
self, value: Union[Tuple[str, str, str], Triplet]
297297
) -> None:
298-
if isinstance(value, tuple):
299-
value = Triplet.fromTuple(value)
300-
assert isinstance(value, Triplet)
301-
self._data["SegmentedPropertyTypeModifierCodeSequence"] = value.asdict()
298+
self._triplet_setter("SegmentedPropertyTypeModifierCodeSequence", value)
302299

303300
@property
304301
def hasSegmentedPropertyTypeModifier(self) -> bool:
@@ -310,10 +307,7 @@ def anatomicRegion(self) -> Triplet:
310307

311308
@anatomicRegion.setter
312309
def anatomicRegion(self, value: Union[Tuple[str, str, str], Triplet]) -> None:
313-
if isinstance(value, tuple):
314-
value = Triplet.fromTuple(value)
315-
assert isinstance(value, Triplet)
316-
self._data["AnatomicRegionSequence"] = value.asdict()
310+
self._triplet_setter("AnatomicRegionSequence", value)
317311

318312
@property
319313
def hasAnatomicRegion(self) -> bool:
@@ -327,10 +321,7 @@ def anatomicRegionModifier(self) -> Triplet:
327321
def anatomicRegionModifier(
328322
self, value: Union[Tuple[str, str, str], Triplet]
329323
) -> None:
330-
if isinstance(value, tuple):
331-
value = Triplet.fromTuple(value)
332-
assert isinstance(value, Triplet)
333-
self._data["AnatomicRegionModifierSequence"] = value.asdict()
324+
self._triplet_setter("AnatomicRegionModifierSequence", value)
334325

335326
@property
336327
def hasAnatomicRegionModifier(self) -> bool:

0 commit comments

Comments
 (0)