dicube 0.1.4__cp39-cp39-macosx_11_0_arm64.whl → 0.2.2__cp39-cp39-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,37 +31,37 @@ def derive_pixel_header_from_array(
31
31
  dtype = str(image.dtype)
32
32
  if image.dtype in (np.uint16, np.uint8, np.uint32):
33
33
  return image, PixelDataHeader(
34
- RESCALE_SLOPE=1,
35
- RESCALE_INTERCEPT=0,
36
- PIXEL_DTYPE=dtype,
37
- ORIGINAL_PIXEL_DTYPE=dtype,
34
+ RescaleSlope=1,
35
+ RescaleIntercept=0,
36
+ PixelDtype=dtype,
37
+ OriginalPixelDtype=dtype,
38
38
  )
39
39
  elif image.dtype == np.int16:
40
40
  min_val = int(np.min(image))
41
41
  image = (image - min_val).astype("uint16")
42
42
  return image, PixelDataHeader(
43
- RESCALE_SLOPE=1,
44
- RESCALE_INTERCEPT=min_val,
45
- PIXEL_DTYPE="uint16",
46
- ORIGINAL_PIXEL_DTYPE=dtype,
43
+ RescaleSlope=1,
44
+ RescaleIntercept=min_val,
45
+ PixelDtype="uint16",
46
+ OriginalPixelDtype=dtype,
47
47
  )
48
48
  elif image.dtype == np.int8:
49
49
  min_val = int(np.min(image))
50
50
  image = (image - min_val).astype("uint8")
51
51
  return image, PixelDataHeader(
52
- RESCALE_SLOPE=1,
53
- RESCALE_INTERCEPT=min_val,
54
- PIXEL_DTYPE="uint8",
55
- ORIGINAL_PIXEL_DTYPE=dtype,
52
+ RescaleSlope=1,
53
+ RescaleIntercept=min_val,
54
+ PixelDtype="uint8",
55
+ OriginalPixelDtype=dtype,
56
56
  )
57
57
  elif image.dtype == np.int32:
58
58
  min_val = int(np.min(image))
59
59
  image = (image - min_val).astype("uint32")
60
60
  return image, PixelDataHeader(
61
- RESCALE_SLOPE=1,
62
- RESCALE_INTERCEPT=min_val,
63
- PIXEL_DTYPE="uint32",
64
- ORIGINAL_PIXEL_DTYPE=dtype,
61
+ RescaleSlope=1,
62
+ RescaleIntercept=min_val,
63
+ PixelDtype="uint32",
64
+ OriginalPixelDtype=dtype,
65
65
  )
66
66
  elif image.dtype in (np.float16, np.float32, np.float64):
67
67
  if preferred_dtype == "uint8":
@@ -78,10 +78,10 @@ def derive_pixel_header_from_array(
78
78
  # Set all pixels to 0, slope=0, intercept=min_val
79
79
  # When reading back: i*slope+intercept = min_val
80
80
  header = PixelDataHeader(
81
- RESCALE_SLOPE=1.0,
82
- RESCALE_INTERCEPT=float(min_val),
83
- PIXEL_DTYPE=preferred_dtype,
84
- ORIGINAL_PIXEL_DTYPE=dtype,
81
+ RescaleSlope=1.0,
82
+ RescaleIntercept=float(min_val),
83
+ PixelDtype=preferred_dtype,
84
+ OriginalPixelDtype=dtype,
85
85
  )
86
86
  raw_image = np.zeros_like(image, dtype=preferred_dtype)
87
87
  return raw_image, header
@@ -92,12 +92,12 @@ def derive_pixel_header_from_array(
92
92
  preferred_dtype
93
93
  )
94
94
  header = PixelDataHeader(
95
- RESCALE_SLOPE=slope,
96
- RESCALE_INTERCEPT=intercept,
97
- PIXEL_DTYPE=preferred_dtype,
98
- ORIGINAL_PIXEL_DTYPE=dtype,
99
- MAX_VAL=max_val,
100
- MIN_VAL=min_val,
95
+ RescaleSlope=slope,
96
+ RescaleIntercept=intercept,
97
+ PixelDtype=preferred_dtype,
98
+ OriginalPixelDtype=dtype,
99
+ MaxVal=max_val,
100
+ MinVal=min_val,
101
101
  )
102
102
  return raw_image, header
103
103
  else:
@@ -132,10 +132,128 @@ def get_float_data(
132
132
 
133
133
  # Note: Output may be positive or negative depending on original dtype and slope/intercept
134
134
  output_img = raw_image.astype(dtype)
135
- if pixel_header.RESCALE_SLOPE is not None:
136
- slope = np.array(pixel_header.RESCALE_SLOPE).astype(dtype)
137
- output_img *= slope
138
- if pixel_header.RESCALE_INTERCEPT is not None:
139
- intercept = np.array(pixel_header.RESCALE_INTERCEPT).astype(dtype)
140
- output_img += intercept
141
- return output_img
135
+ if pixel_header.RescaleSlope is not None:
136
+ slope = np.array(pixel_header.RescaleSlope).astype(dtype)
137
+ if slope != 1.0:
138
+ output_img *= slope
139
+ if pixel_header.RescaleIntercept is not None:
140
+ intercept = np.array(pixel_header.RescaleIntercept).astype(dtype)
141
+ if intercept != 0.0:
142
+ output_img += intercept
143
+ return output_img
144
+
145
+
146
+ def determine_optimal_nifti_dtype(
147
+ image: np.ndarray, pixel_header: PixelDataHeader
148
+ ) -> Tuple[np.ndarray, str]:
149
+ """Determine the optimal data type for saving to NIfTI and return the converted data.
150
+
151
+ This function selects the most appropriate data type for NIfTI export based on the value range
152
+ of the raw image and the rescale slope/intercept. It minimizes unnecessary data conversion and
153
+ only applies scaling or offset if needed.
154
+
155
+ Args:
156
+ image (np.ndarray): The raw image data (integer type guaranteed).
157
+ pixel_header (PixelDataHeader): Pixel header containing rescale information.
158
+
159
+ Returns:
160
+ Tuple[np.ndarray, str]:
161
+ - The image data converted to the optimal type for NIfTI export.
162
+ - The name of the chosen data type as a string.
163
+
164
+ Raises:
165
+ ValueError: If the data cannot be represented in any supported NIfTI type.
166
+
167
+ Example:
168
+ >>> arr = np.array([0, 100, 200], dtype=np.uint16)
169
+ >>> header = PixelDataHeader(RescaleSlope=1.0, RescaleIntercept=0.0, OriginalPixelDtype="uint16", PixelDtype="uint16")
170
+ >>> data, dtype_name = determine_optimal_nifti_dtype(arr, header)
171
+ >>> print(data.dtype, dtype_name)
172
+ uint8 uint8
173
+ """
174
+ # 获取slope和intercept
175
+ slope = pixel_header.RescaleSlope if pixel_header.RescaleSlope is not None else 1.0
176
+ intercept = pixel_header.RescaleIntercept if pixel_header.RescaleIntercept is not None else 0.0
177
+
178
+ # 直接从原始数据计算值域
179
+ raw_min = float(image.min())
180
+ raw_max = float(image.max())
181
+
182
+ # 计算应用slope和intercept后的值域
183
+ min_val = raw_min * slope + intercept
184
+ max_val = raw_max * slope + intercept
185
+
186
+ # 如果斜率为负,需要交换min和max
187
+ if slope < 0:
188
+ min_val, max_val = max_val, min_val
189
+
190
+ # 检查原始数据类型
191
+ original_dtype = pixel_header.OriginalPixelDtype
192
+ is_signed_original = original_dtype in ("int8", "int16", "int32", "int64")
193
+
194
+ # 检查slope和intercept是否为整数值
195
+ has_integer_transform = (
196
+ np.isclose(slope % 1, 0) and
197
+ np.isclose(intercept % 1, 0)
198
+ )
199
+
200
+ # 准备最终数据 - 仅在确定dtype后执行一次转换
201
+ result_dtype = None
202
+ result_dtype_name = None
203
+
204
+ # 如果slope和intercept都是整数,则可以使用整数类型
205
+ if has_integer_transform:
206
+ # 尊重原始数据类型的符号属性
207
+ if is_signed_original or min_val < 0:
208
+ # 有符号整数
209
+ if min_val >= -128 and max_val <= 127:
210
+ result_dtype = np.int8
211
+ result_dtype_name = "int8"
212
+ elif min_val >= -32768 and max_val <= 32767:
213
+ result_dtype = np.int16
214
+ result_dtype_name = "int16"
215
+ elif min_val >= -2147483648 and max_val <= 2147483647:
216
+ result_dtype = np.int32
217
+ result_dtype_name = "int32"
218
+ elif max_val <= 2147483647: # 值域在int32范围内,但原始类型是int32
219
+ result_dtype = np.int32
220
+ result_dtype_name = "int32"
221
+ else:
222
+ # 无符号整数
223
+ if max_val <= 255:
224
+ result_dtype = np.uint8
225
+ result_dtype_name = "uint8"
226
+ elif max_val <= 65535:
227
+ result_dtype = np.uint16
228
+ result_dtype_name = "uint16"
229
+ elif max_val <= 4294967295:
230
+ result_dtype = np.uint32
231
+ result_dtype_name = "uint32"
232
+
233
+ # 如果没有找到合适的整数类型,使用浮点类型
234
+ if result_dtype is None:
235
+ if np.issubdtype(image.dtype, np.float64) or min_val < -3.4e38 or max_val > 3.4e38:
236
+ result_dtype = np.float64
237
+ result_dtype_name = "float64"
238
+ else:
239
+ result_dtype = np.float32
240
+ result_dtype_name = "float32"
241
+
242
+ if has_integer_transform:
243
+ intercept = int(intercept)
244
+ else:
245
+ intercept = np.array(intercept,dtype=result_dtype)
246
+
247
+ if slope == 1.0:
248
+ # 只要加法
249
+ return image.astype(result_dtype) + intercept, result_dtype_name
250
+ else:
251
+ # 需要乘法,生成最终数据
252
+ if result_dtype in (np.float32, np.float64):
253
+ # 浮点类型,直接使用浮点运算
254
+ result = image.astype(result_dtype) * slope + intercept
255
+ else:
256
+ # 整数类型,先做浮点运算再转换
257
+ result = (image.astype(np.float32) * slope + intercept).astype(result_dtype)
258
+
259
+ return result, result_dtype_name
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: dicube
3
- Version: 0.1.4
3
+ Version: 0.2.2
4
4
  Summary: Medical Image Storage Library with DICOM compatibility
5
5
  Author: Fangzhou Liao
6
6
  License: MIT
@@ -38,6 +38,7 @@ Requires-Dist: mypy>=1.0.0; extra == "dev"
38
38
  Requires-Dist: build>=0.8.0; extra == "dev"
39
39
  Requires-Dist: pylibjpeg>=2.0; extra == "dev"
40
40
  Requires-Dist: pylibjpeg-openjpeg>=2.0; extra == "dev"
41
+ Requires-Dist: nibabel>=3.2.0; extra == "dev"
41
42
  Provides-Extra: all
42
43
  Requires-Dist: pybind11>=2.10.0; extra == "all"
43
44
  Requires-Dist: nibabel>=3.2.0; extra == "all"
@@ -1,4 +1,7 @@
1
- dicube/__init__.py,sha256=QRyaQXqrzwSo8utXhGsbToE_lCEsjlzdzwFwVHINS8I,4189
1
+ dicube-0.2.2.dist-info/RECORD,,
2
+ dicube-0.2.2.dist-info/WHEEL,sha256=Bp5NfjQuMM_4-YV1MlnhqEfxPV5ySj3kUtyINz66rJc,112
3
+ dicube-0.2.2.dist-info/METADATA,sha256=nBhXpfkqIUhGO2LUB-HmTwmDIolxcvd-cr8tW4CJNzM,10627
4
+ dicube/__init__.py,sha256=TK2-JGOB_aPa6ilHw4WUc9l5Whc3Fb-tlZSPzbf5HIw,4886
2
5
  dicube/exceptions.py,sha256=YrwBD93oWI4yhvFfO-SOPCNMQ4z_1FSVL2gCVSnx-LU,6689
3
6
  dicube/validation.py,sha256=Edmx3yKhRShdzvM7uRK6N9j58GNV8rUv_k9w43JcaMs,12478
4
7
  dicube/codecs/__init__.py,sha256=NNus-iAv4JXsNH1kALf5vnLYnESFBoYaN72yJ7kQoqk,3754
@@ -8,22 +11,19 @@ dicube/codecs/jph/ojph_complete.cpython-38-darwin.so,sha256=Bv7L0i6T1u8AUa7uKHpx
8
11
  dicube/codecs/jph/ojph_decode_complete.cpython-38-darwin.so,sha256=HHzNB5C3xZ0yhBA_tnzvAHlRdxZ7SBzbwbgjRWvNQ_g,448712
9
12
  dicube/codecs/jph/ojph_decode_complete.cpython-39-darwin.so,sha256=OMAfYyAQra6xF_Zv5Y-LGBkg68MYpTK4qwB8lWqlf6U,448968
10
13
  dicube/codecs/jph/codec.py,sha256=d4bAoVAatSrWiC1k7gQLzWODZZTGo_1xeRRXO0NAtqo,5460
11
- dicube/core/io.py,sha256=9Wg9dmVCnZpXQH5fKiS152sKDyy4vOO7ChYpgRL-vzE,13763
14
+ dicube/core/io.py,sha256=g8uKbcX7zz9LCq65spBeeodZEf6l4HSF9jzl60HZVyw,15629
12
15
  dicube/core/__init__.py,sha256=tU9vaPHbTkF8V78T8D6werr0nC7k6bMPIlatEIEdHU4,656
13
- dicube/core/pixel_header.py,sha256=M-zu5hFG0BWj0chWgxyGFAQP7oH7r-1iN44acKOLm7k,3929
14
- dicube/core/image.py,sha256=5XafyshOQbife6M8_G3vmrensjc1jTjyeUkAfAOCKiI,14182
16
+ dicube/core/pixel_header.py,sha256=x3-gNiyL4lFRQ6c1TVdvsm2mG36xCQx78hq4vjYkqJI,4005
17
+ dicube/core/image.py,sha256=DAyBwKlWeCU6oDWsmdSdAzVW3IieHNLUINIAhS_ZtRg,14177
15
18
  dicube/utils/__init__.py,sha256=m3pYJfuk4hISVes_cfG2eDjTyGF-y40xbxV07IOjbi0,149
16
19
  dicube/storage/__init__.py,sha256=q36sJqdI9acURBXjOW_g3QiyHw0XLGFCukMsQ-qioZc,465
17
- dicube/storage/dcb_file.py,sha256=Gsb_LXHAm7DqAdrh4Kgo0rrj1mUuz7sY6tVt8HiKXyI,30379
18
- dicube/storage/pixel_utils.py,sha256=thyYO-YNtWEvg1ROPLC-zPWqa37O0vJ8e26h3aF6dXM,5136
19
- dicube/dicom/dcb_streaming.py,sha256=6HUmk5QEdg8WhkaiiUDjdX8yMwQxABjmaXaDjxwQuQU,9042
20
+ dicube/storage/dcb_file.py,sha256=GfOx2C6fexKojxwzak3_Q0K3gRBHB_UTMNVBY4NPRzg,31232
21
+ dicube/storage/pixel_utils.py,sha256=-xMMnjSMzYk6WRvLAPktMELhSXJPmLBnt5wxKgOw5zg,9759
22
+ dicube/dicom/dcb_streaming.py,sha256=Acsdusc6jIKuu1kd-VuDOoteZGdpt3UmwsxF-HQYZtM,9088
20
23
  dicube/dicom/dicom_meta.py,sha256=Iv-kEcV39vhK2qgSqGpqDhgg28RuUm8ddzSdpToIVPc,26813
21
- dicube/dicom/dicom_io.py,sha256=P-arcQu-CUB0Uf5daLku_Wf-3D3NpY3PiWHaosoe3dA,4941
24
+ dicube/dicom/dicom_io.py,sha256=EBa72RGHfifFKygBwCWf6r2YCTNxE9onO8d65ws_5VU,4917
22
25
  dicube/dicom/dicom_status.py,sha256=NXi-sj3mhyaFGfpechewTjhI201vziVsyfLotsLr3Fs,10117
23
26
  dicube/dicom/__init__.py,sha256=9b7liSXx5vlpEYAr59GZGkcUw16kULELTX5SlLfcXPw,371
24
27
  dicube/dicom/merge_utils.py,sha256=6flzoK_6gzpvv4WSTaWq4SjVdr1gh3gtUPMsRfKDSNM,9312
25
28
  dicube/dicom/space_from_meta.py,sha256=__B7UTmvgV1N-LMJg9O_ObHgNWa39mbRF4ZSkQXm3Vs,2420
26
29
  dicube/dicom/dicom_tags.py,sha256=sOjrK9SoAhGqynF0e3YH70j8wbtvwGRezV-tM0wp40Q,3945
27
- dicube-0.1.4.dist-info/RECORD,,
28
- dicube-0.1.4.dist-info/WHEEL,sha256=Bp5NfjQuMM_4-YV1MlnhqEfxPV5ySj3kUtyINz66rJc,112
29
- dicube-0.1.4.dist-info/METADATA,sha256=lLPPqXaJcIOIHhGuU3buJPhmi3GCMeKi5KW0FhlY0nk,10581
File without changes