Skip to content

Commit c99232a

Browse files
thewtexdzenanz
authored andcommitted
ENH: Use literate identifiers in Dask serialization.
A string pixel identifier and size-identifying component type is used during serialization, consistent with itk-wasm.
1 parent ecd025e commit c99232a

File tree

1 file changed

+49
-56
lines changed

1 file changed

+49
-56
lines changed

Wrapping/Generators/Python/itk/support/helpers.py

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -184,73 +184,66 @@ def wasm_type_from_image_type(itkimage): # noqa: C901
184184
component = itk.template(itkimage)[1][0]
185185
if component == itk.UL:
186186
if os.name == 'nt':
187-
return 'uint32_t', 1
187+
return 'uint32', 'Scalar'
188188
else:
189-
return 'uint64_t', 1
189+
return 'uint64', 'Scalar'
190190
mangle = None
191-
pixelType = 1
191+
pixelType = 'Scalar'
192192
if component == itk.SL:
193193
if os.name == 'nt':
194-
return 'int32_t', 1,
194+
return 'int32', 'Scalar',
195195
else:
196-
return 'int64_t', 1,
196+
return 'int64', 'Scalar',
197197
if component in (itk.SC, itk.UC, itk.SS, itk.US, itk.SI, itk.UI, itk.F,
198198
itk.D, itk.B, itk.SL, itk.SLL, itk.UL, itk.ULL):
199199
mangle = component
200200
elif component in [i[1] for i in itk.Vector.items()]:
201201
mangle = itk.template(component)[1][0]
202-
pixelType = 5
202+
pixelType = 'Vector'
203203
elif component == itk.complex[itk.F]:
204-
# complex float
205-
return 'float', 10
204+
return 'float32', 'Complex'
206205
elif component == itk.complex[itk.D]:
207-
# complex float
208-
return 'double', 10
206+
return 'float64', 'Complex'
209207
elif component in [i[1] for i in itk.CovariantVector.items()]:
210-
# CovariantVector
211208
mangle = itk.template(component)[1][0]
212-
pixelType = 7
209+
pixelType = 'CovariantVector',
213210
elif component in [i[1] for i in itk.Offset.items()]:
214-
# Offset
215-
return 'int64_t', 4
211+
return 'int64', 'Offset'
216212
elif component in [i[1] for i in itk.FixedArray.items()]:
217-
# FixedArray
218213
mangle = itk.template(component)[1][0]
219-
pixelType = 11
214+
pixelType = 'FixedArray'
220215
elif component in [i[1] for i in itk.RGBAPixel.items()]:
221-
# RGBA
222216
mangle = itk.template(component)[1][0]
223-
pixelType = 3
217+
pixelType = 'RGBA'
224218
elif component in [i[1] for i in itk.RGBPixel.items()]:
225-
# RGB
226219
mangle = itk.template(component)[1][0]
227-
pixelType = 2
220+
pixelType = 'RGB'
228221
elif component in [i[1] for i in itk.SymmetricSecondRankTensor.items()]:
229222
# SymmetricSecondRankTensor
230223
mangle = itk.template(component)[1][0]
231-
pixelType = 8
224+
pixelType = 'SymmetrySecondRankTensor'
232225
else:
233226
raise RuntimeError('Unrecognized component type: {0}'.format(str(component)))
234227

235228
def _long_type():
236229
if os.name == 'nt':
237-
return 'int32_t'
230+
return 'int32'
238231
else:
239-
return 'int64_t'
232+
return 'int64'
240233
_python_to_js = {
241-
itk.SC: 'int8_t',
242-
itk.UC: 'uint8_t',
243-
itk.SS: 'int16_t',
244-
itk.US: 'uint16_t',
245-
itk.SI: 'int32_t',
246-
itk.UI: 'uint32_t',
247-
itk.F: 'float',
248-
itk.D: 'double',
249-
itk.B: 'uint8_t',
234+
itk.SC: 'int8',
235+
itk.UC: 'uint8',
236+
itk.SS: 'int16',
237+
itk.US: 'uint16',
238+
itk.SI: 'int32',
239+
itk.UI: 'uint32',
240+
itk.F: 'float32',
241+
itk.D: 'float64',
242+
itk.B: 'uint8',
250243
itk.SL: _long_type(),
251244
itk.UL: 'u' + _long_type(),
252-
itk.SLL: 'int64_t',
253-
itk.ULL: 'uint64_t',
245+
itk.SLL: 'int64',
246+
itk.ULL: 'uint64',
254247
}
255248
imageType = dict(
256249
dimension=itkimage.GetImageDimension(),
@@ -265,19 +258,19 @@ def image_type_from_wasm_type(jstype):
265258
import itk
266259

267260
_pixelType_to_prefix = {
268-
1: '',
269-
2: 'RGB',
270-
3: 'RGBA',
271-
4: 'O',
272-
5: 'V',
273-
7: 'CV',
274-
8: 'SSRT',
275-
11: 'FA'
261+
'Scalar': '',
262+
'RGB': 'RGB',
263+
'RGBA': 'RGBA',
264+
'Offset': 'O',
265+
'Vector': 'V',
266+
'CovariantVector': 'CV',
267+
'SymmetricSecondRankTensor': 'SSRT',
268+
'FixedArray': 'FA'
276269
}
277270
pixelType = jstype['pixelType']
278271
dimension = jstype['dimension']
279-
if pixelType == 10:
280-
if jstype['componentType'] == 'float':
272+
if pixelType == 'Complex':
273+
if jstype['componentType'] == 'float32':
281274
return itk.Image[itk.complex, itk.F], np.float32
282275
else:
283276
return itk.Image[itk.complex, itk.D], np.float64
@@ -289,20 +282,20 @@ def _long_type():
289282
return 'L'
290283
prefix = _pixelType_to_prefix[pixelType]
291284
_js_to_python = {
292-
'int8_t': 'SC',
293-
'uint8_t': 'UC',
294-
'int16_t': 'SS',
295-
'uint16_t': 'US',
296-
'int32_t': 'SI',
297-
'uint32_t': 'UI',
298-
'int64_t': 'S' + _long_type(),
299-
'uint64_t': 'U' + _long_type(),
300-
'float': 'F',
301-
'double': 'D'
285+
'int8': 'SC',
286+
'uint8': 'UC',
287+
'int16': 'SS',
288+
'uint16': 'US',
289+
'int32': 'SI',
290+
'uint32': 'UI',
291+
'int64': 'S' + _long_type(),
292+
'uint64': 'U' + _long_type(),
293+
'float32': 'F',
294+
'float64': 'D'
302295
}
303-
if pixelType != 4:
296+
if pixelType != 'Offset':
304297
prefix += _js_to_python[jstype['componentType']]
305-
if pixelType not in (1, 2, 3, 10):
298+
if pixelType not in ('Scalar', 'RGB', 'RGBA', 'Complex'):
306299
prefix += str(dimension)
307300
prefix += str(dimension)
308301
return getattr(itk.Image, prefix)

0 commit comments

Comments
 (0)