|
3 | 3 | import logging
|
4 | 4 | import os
|
5 | 5 | import tempfile
|
6 |
| -from dataclasses import dataclass, field |
| 6 | +from dataclasses import dataclass |
7 | 7 | from io import BytesIO
|
8 | 8 | from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Tuple, Union
|
9 | 9 |
|
@@ -54,47 +54,57 @@ class MedicalImage(Image): # type: ignore
|
54 | 54 |
|
55 | 55 | Parameters
|
56 | 56 | ----------
|
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}`. |
60 | 57 | reader : Union[str, ImageReader], optional, default="ITKReader"
|
61 | 58 | The MONAI image reader to use.
|
62 | 59 | suffix : str, optional, default=".jpg"
|
63 | 60 | 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}`. |
64 | 64 |
|
65 | 65 | """
|
66 | 66 |
|
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 |
95 | 67 | dtype: ClassVar[str] = "dict"
|
96 | 68 | 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" |
98 | 108 |
|
99 | 109 | def encode_example(
|
100 | 110 | self,
|
@@ -278,5 +288,5 @@ def _encode_ndarray(
|
278 | 288 | return {"path": None, "bytes": temp_file_bytes}
|
279 | 289 |
|
280 | 290 |
|
281 |
| -# add the `MedicalImage` feature to the `features` module |
| 291 | +# add the `MedicalImage` feature to the `features` module namespace |
282 | 292 | features.MedicalImage = MedicalImage
|
0 commit comments