dicube 0.1.4__cp310-cp310-win_amd64.whl → 0.2.2__cp310-cp310-win_amd64.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"
@@ -0,0 +1,31 @@
1
+ dicube/__init__.py,sha256=XI_Zeif8FOQ-i_G7LabwVFpubzDniXSFb-8H_WdYW8c,5059
2
+ dicube/codecs/__init__.py,sha256=Tqw2vmQML-pcaNtnyTtj4W4kBqGhV2FTiVAT5bW5FgA,3905
3
+ dicube/codecs/jph/__init__.py,sha256=pWYkTNWYeGZx3khsJBAKRdB2Vgyr0BX5aLDKaanfe_o,407
4
+ dicube/codecs/jph/codec.py,sha256=BMZM3ReemQheraSIYn19aqH-1NrbjQTPAx3iCTms7KU,5620
5
+ dicube/codecs/jph/ojph_complete.cp310-win_amd64.pyd,sha256=5WeheXwI28J6Xh8MltyoUsxcjFJSCiZWe9W-qKvOaqI,601600
6
+ dicube/codecs/jph/ojph_complete.cp38-win_amd64.pyd,sha256=5A8LW4oiuxFKTgM8g5QXcgFA1Jp8gT0-5Ag2n3JIWWw,601600
7
+ dicube/codecs/jph/ojph_complete.cp39-win_amd64.pyd,sha256=p8dd-h1i6sOMWsn_KFIAMwCN3wDCcMrFUQk6dRLDNbI,601600
8
+ dicube/codecs/jph/ojph_decode_complete.cp310-win_amd64.pyd,sha256=ZmnsCcu2FASt6QXpPVPUsyGw3FKesqR7H8FaFU4MsFQ,639488
9
+ dicube/codecs/jph/ojph_decode_complete.cp38-win_amd64.pyd,sha256=ZsXsgMf-j47XI4gk0XiiyeLdLddvwM_PyYUjrqbrXj0,639488
10
+ dicube/codecs/jph/ojph_decode_complete.cp39-win_amd64.pyd,sha256=nCm7fjGl6gmOiTj1P8Pu8MWWmskHMdxe8CAn0ckbqrM,638976
11
+ dicube/core/__init__.py,sha256=TJBS1xA2qVS7xxH6vZKPrd7YiQYtyLfNSs48tlmBCD8,676
12
+ dicube/core/image.py,sha256=WR9GKat0st0ior7zw1wi_5Bpd5cZoHWvsPzjYJAwpcg,14525
13
+ dicube/core/io.py,sha256=htVflYmSgB--2CSKftfEfNLEtIJ7Jrg02A1MveW3Xs8,16036
14
+ dicube/core/pixel_header.py,sha256=gikXfkjTGVQUqTWUk8Q2xAPsDfWm0iRPcwdIxIImr94,4124
15
+ dicube/dicom/__init__.py,sha256=OGnAHS3pCbb94Vpsc0x3d2gjv5UBAM9oUR3mxQVruYQ,383
16
+ dicube/dicom/dcb_streaming.py,sha256=6b50m5SEHFkdkWYKl6_ClzvPZZJKlJlVNR08sWF-Xdo,9335
17
+ dicube/dicom/dicom_io.py,sha256=EWR-VYXUYEF066dxucSb9E7xPpIReN-BbCeIZSsNskU,5069
18
+ dicube/dicom/dicom_meta.py,sha256=PgVeIWoYE7udQ3M26ytX1r3ic9leL35MMVfEJhTk-3Q,27552
19
+ dicube/dicom/dicom_status.py,sha256=VYp6q6UbxPkGwJ8_udEBRGSCFeSWedwRuEB4vfREOaE,10375
20
+ dicube/dicom/dicom_tags.py,sha256=BRw099dC-9FvAJ43QKESHBxtGtsGSSodcijJKXcCnsE,4065
21
+ dicube/dicom/merge_utils.py,sha256=ji0twDUBpR261l0pS2i8knetZI5Jg-OEaYsc2G_SBzk,9594
22
+ dicube/dicom/space_from_meta.py,sha256=i6zIXbFaNyz0g6z2oeoO5lzsfaeo4sgz_FI-PrEmqsg,2489
23
+ dicube/exceptions.py,sha256=VNnW7Dd9MwKzSK_y9GQITrsaHYiL-Crwmntzf-YS1sY,6877
24
+ dicube/storage/__init__.py,sha256=6WG1hdok-TkKShXCKFvH9ShoNILq3qvyZ6sNEO_urW4,481
25
+ dicube/storage/dcb_file.py,sha256=tt7aYi4x_gxf4s6De6tSkpiwyoCp1ZcG-Piixai1Jw8,32055
26
+ dicube/storage/pixel_utils.py,sha256=_KrsMoKHsgGc9QFSkmpx5YlDNcTOYPkCSu-zGt3WS7k,10017
27
+ dicube/utils/__init__.py,sha256=r4wAqNWphzYWaeYN9fcoFCX7mTcOtNACzZ4O-dDCQ10,154
28
+ dicube/validation.py,sha256=aFu2lKW9NDl13eKedYhOjw1g50uOf_x0wx3y3g9cLsA,12857
29
+ dicube-0.2.2.dist-info/METADATA,sha256=nBhXpfkqIUhGO2LUB-HmTwmDIolxcvd-cr8tW4CJNzM,10627
30
+ dicube-0.2.2.dist-info/WHEEL,sha256=dD8EM_eb3pZmEbkvPEPxqrbrFtr3mwlecsbXbps1z7Y,106
31
+ dicube-0.2.2.dist-info/RECORD,,
@@ -1,37 +0,0 @@
1
- dicube/__init__.py,sha256=CwVdLfPEkB6t5szG1eZnpF0JIbuOR1dC6WZ4pwNd0lI,4328
2
- dicube/codecs/__init__.py,sha256=Tqw2vmQML-pcaNtnyTtj4W4kBqGhV2FTiVAT5bW5FgA,3905
3
- dicube/codecs/jph/__init__.py,sha256=pWYkTNWYeGZx3khsJBAKRdB2Vgyr0BX5aLDKaanfe_o,407
4
- dicube/codecs/jph/codec.py,sha256=BMZM3ReemQheraSIYn19aqH-1NrbjQTPAx3iCTms7KU,5620
5
- dicube/codecs/jph/ojph_complete.cp310-win32.pyd,sha256=i2p1vJSrWjq_txiD4EHY76C_nyitew3FagVwqZIrHm0,507904
6
- dicube/codecs/jph/ojph_complete.cp310-win_amd64.pyd,sha256=0ZCRjZM7ydUEaai5PQrodUP8mJwPlIplvCCDetYxOz8,601600
7
- dicube/codecs/jph/ojph_complete.cp38-win32.pyd,sha256=g3NgC2GHqAEi85rd5OII7y9ecntQIJs3b0eOz_kImOk,507904
8
- dicube/codecs/jph/ojph_complete.cp38-win_amd64.pyd,sha256=18JF5Nd1AECJFgkkAUsimUJCAoeaSMp3CIo57EV7fV8,601600
9
- dicube/codecs/jph/ojph_complete.cp39-win32.pyd,sha256=M8Xd5KcPJUsYANUzG913DSH-rSk3tYyWOaZry2LhxOQ,507904
10
- dicube/codecs/jph/ojph_complete.cp39-win_amd64.pyd,sha256=v1mnIZ8T4ovtIysP3BcU-IG2yPTbI3XSbgt8rcNkPik,601600
11
- dicube/codecs/jph/ojph_decode_complete.cp310-win32.pyd,sha256=COfALkBP0f7dZ_kly8o3lPT6ilmSlWqacfsV3og1Boo,543744
12
- dicube/codecs/jph/ojph_decode_complete.cp310-win_amd64.pyd,sha256=Km461A6biNIjaQ1tvGlg8_99y5aV3c0kDL_e2UYthKU,639488
13
- dicube/codecs/jph/ojph_decode_complete.cp38-win32.pyd,sha256=HoJrMZc_Na-I2oRc0NKyBKuBOzHd98-S0I4MHz_jvoY,543232
14
- dicube/codecs/jph/ojph_decode_complete.cp38-win_amd64.pyd,sha256=wt4cGlqv5DYqVJjGKQaxSJzg8gtqtuSqzciaa9tRK_Q,639488
15
- dicube/codecs/jph/ojph_decode_complete.cp39-win32.pyd,sha256=v1ChiPI65TLCDYTT_4X82xduM_-MN7s7L_ruYrnvkO8,543232
16
- dicube/codecs/jph/ojph_decode_complete.cp39-win_amd64.pyd,sha256=5ztslxsLawb-_g7Rn0azqQFg9zqIVoaaJgNWQECdukA,638976
17
- dicube/core/__init__.py,sha256=TJBS1xA2qVS7xxH6vZKPrd7YiQYtyLfNSs48tlmBCD8,676
18
- dicube/core/image.py,sha256=24oBY6sjW52yuJ2DS4Cq8cyvCY1vdT-WmkXsUehV4qI,14530
19
- dicube/core/io.py,sha256=yvN7eCmJr6OarJz1m-YTE6yw_xinqvGqNCLnPpnAcco,14116
20
- dicube/core/pixel_header.py,sha256=KM61i3ZPAQYcu-IKgou8DruDJIjcbq5qSsTL0sFMtro,4045
21
- dicube/dicom/__init__.py,sha256=OGnAHS3pCbb94Vpsc0x3d2gjv5UBAM9oUR3mxQVruYQ,383
22
- dicube/dicom/dcb_streaming.py,sha256=MSPLTfUYQF1Wb4ut5I3xuxPjum1xW5DgZRGhMPX9Lq4,9291
23
- dicube/dicom/dicom_io.py,sha256=7TPfDz3f4qlvYhmweacT5GURRj2aZu3Tc6F8N7Yn1Hc,5093
24
- dicube/dicom/dicom_meta.py,sha256=PgVeIWoYE7udQ3M26ytX1r3ic9leL35MMVfEJhTk-3Q,27552
25
- dicube/dicom/dicom_status.py,sha256=VYp6q6UbxPkGwJ8_udEBRGSCFeSWedwRuEB4vfREOaE,10375
26
- dicube/dicom/dicom_tags.py,sha256=BRw099dC-9FvAJ43QKESHBxtGtsGSSodcijJKXcCnsE,4065
27
- dicube/dicom/merge_utils.py,sha256=ji0twDUBpR261l0pS2i8knetZI5Jg-OEaYsc2G_SBzk,9594
28
- dicube/dicom/space_from_meta.py,sha256=i6zIXbFaNyz0g6z2oeoO5lzsfaeo4sgz_FI-PrEmqsg,2489
29
- dicube/exceptions.py,sha256=VNnW7Dd9MwKzSK_y9GQITrsaHYiL-Crwmntzf-YS1sY,6877
30
- dicube/storage/__init__.py,sha256=6WG1hdok-TkKShXCKFvH9ShoNILq3qvyZ6sNEO_urW4,481
31
- dicube/storage/dcb_file.py,sha256=yl1fIgkP_qbCR_jj-rTt9FmHuVI9S2NSvJ3M1xunWeQ,31183
32
- dicube/storage/pixel_utils.py,sha256=Wspas9QyH5GUoGpVzs9DAB1PtO1jo0sDUvYjwQ8RVpM,5276
33
- dicube/utils/__init__.py,sha256=r4wAqNWphzYWaeYN9fcoFCX7mTcOtNACzZ4O-dDCQ10,154
34
- dicube/validation.py,sha256=aFu2lKW9NDl13eKedYhOjw1g50uOf_x0wx3y3g9cLsA,12857
35
- dicube-0.1.4.dist-info/METADATA,sha256=lLPPqXaJcIOIHhGuU3buJPhmi3GCMeKi5KW0FhlY0nk,10581
36
- dicube-0.1.4.dist-info/WHEEL,sha256=dD8EM_eb3pZmEbkvPEPxqrbrFtr3mwlecsbXbps1z7Y,106
37
- dicube-0.1.4.dist-info/RECORD,,
File without changes