Skip to content

Commit 64c517c

Browse files
committed
Refactor MedicalImage and MedicalImageFolder
classes
1 parent 6d85be4 commit 64c517c

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

cyclops/data/features/medical_image.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import os
55
import tempfile
6-
from dataclasses import dataclass, field
6+
from dataclasses import dataclass
77
from io import BytesIO
88
from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Tuple, Union
99

@@ -54,47 +54,57 @@ class MedicalImage(Image): # type: ignore
5454
5555
Parameters
5656
----------
57-
decode : bool, optional, default=True
58-
Whether to decode the image. If False, the image will be returned as a
59-
dictionary in the format `{"path": image_path, "bytes": image_bytes}`.
6057
reader : Union[str, ImageReader], optional, default="ITKReader"
6158
The MONAI image reader to use.
6259
suffix : str, optional, default=".jpg"
6360
The suffix to use when decoding bytes to image.
61+
decode : bool, optional, default=True
62+
Whether to decode the image. If False, the image will be returned as a
63+
dictionary in the format `{"path": image_path, "bytes": image_bytes}`.
6464
6565
"""
6666

67-
if any(
68-
module is None
69-
for module in (
70-
monai_image_writer,
71-
monai_compose,
72-
monai_array_io,
73-
monai_array_util,
74-
)
75-
):
76-
raise ImportError(
77-
"The `MedicalImage` feature requires MONAI to be installed. "
78-
"Please install it with `pip install monai`.",
79-
)
80-
81-
reader: Union[str, monai_image_reader.ImageReader] = "ITKReader"
82-
suffix: str = ".jpg" # used when decoding/encoding bytes to image
83-
_loader = monai_compose.Compose(
84-
[
85-
monai_array_io.LoadImage(
86-
reader=reader,
87-
simple_keys=True,
88-
dtype=None,
89-
image_only=True,
90-
),
91-
monai_array_util.ToNumpy(),
92-
],
93-
)
94-
# Automatically constructed
9567
dtype: ClassVar[str] = "dict"
9668
pa_type: ClassVar[Any] = pa.struct({"bytes": pa.binary(), "path": pa.string()})
97-
_type: str = field(default="MedicalImage", init=False, repr=False)
69+
70+
def __init__(
71+
self,
72+
reader: Union[str, monai_image_reader.ImageReader] = "ITKReader",
73+
suffix: str = ".jpg",
74+
decode: bool = True,
75+
id_: Optional[str] = None,
76+
):
77+
super().__init__(decode=decode, id=id_)
78+
79+
if any(
80+
module is None
81+
for module in (
82+
monai_image_writer,
83+
monai_compose,
84+
monai_array_io,
85+
monai_array_util,
86+
)
87+
):
88+
raise ImportError(
89+
"The `MedicalImage` feature requires MONAI to be installed. "
90+
"Please install it with `pip install monai`.",
91+
)
92+
93+
self.reader: Union[str, monai_image_reader.ImageReader] = "ITKReader"
94+
self.suffix: str = suffix # used when decoding/encoding bytes to image
95+
self._loader = monai_compose.Compose(
96+
[
97+
monai_array_io.LoadImage(
98+
reader=reader,
99+
simple_keys=True,
100+
dtype=None,
101+
image_only=False,
102+
),
103+
monai_array_util.ToNumpy(),
104+
],
105+
)
106+
# Automatically constructed
107+
self._type: str = "MedicalImage"
98108

99109
def encode_example(
100110
self,
@@ -278,5 +288,5 @@ def _encode_ndarray(
278288
return {"path": None, "bytes": temp_file_bytes}
279289

280290

281-
# add the `MedicalImage` feature to the `features` module
291+
# add the `MedicalImage` feature to the `features` module namespace
282292
features.MedicalImage = MedicalImage

cyclops/data/packaged_loading_scripts/medical_imagefolder/medical_imagefolder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MedicalImageFolderConfig(
3333
class MedicalImageFolder(folder_based_builder.FolderBasedBuilder): # type: ignore
3434
"""MedicalImageFolder."""
3535

36-
BASE_FEATURE = MedicalImage()
36+
BASE_FEATURE = MedicalImage
3737
BASE_COLUMN_NAME = "image"
3838
BUILDER_CONFIG_CLASS = MedicalImageFolderConfig
3939
EXTENSIONS: List[str] # definition at the bottom of the script

0 commit comments

Comments
 (0)