143
143
from .viewers import OrthoSlicer3D
144
144
from .volumeutils import shape_zoom_affine
145
145
146
+ try :
147
+ from functools import cache
148
+ except ImportError : # PY38
149
+ from functools import lru_cache as cache
150
+
146
151
147
152
class HeaderDataError (Exception ):
148
153
"""Class to indicate error in getting or setting header data"""
@@ -268,22 +273,29 @@ def data_from_fileobj(self, fileobj):
268
273
return np .ndarray (shape , dtype , data_bytes , order = self .data_layout )
269
274
270
275
271
- def supported_np_types (obj ):
272
- """Numpy data types that instance `obj` supports
276
+ @cache
277
+ def _supported_np_types (klass ):
278
+ """Numpy data types that instances of ``klass`` support
273
279
274
280
Parameters
275
281
----------
276
- obj : object
277
- Object implementing `get_data_dtype` and `set_data_dtype`. The object
282
+ klass : class
283
+ Class implementing `get_data_dtype` and `set_data_dtype` methods . The object
278
284
should raise ``HeaderDataError`` for setting unsupported dtypes. The
279
285
object will likely be a header or a :class:`SpatialImage`
280
286
281
287
Returns
282
288
-------
283
289
np_types : set
284
- set of numpy types that `obj` supports
290
+ set of numpy types that ``klass`` instances support
285
291
"""
286
- dt = obj .get_data_dtype ()
292
+ try :
293
+ obj = klass ()
294
+ except TypeError as e :
295
+ if hasattr (klass , 'header_class' ):
296
+ obj = klass .header_class ()
297
+ else :
298
+ raise e
287
299
supported = set ()
288
300
for np_type in set (np .sctypeDict .values ()):
289
301
try :
@@ -293,10 +305,27 @@ def supported_np_types(obj):
293
305
# Did set work?
294
306
if np .dtype (obj .get_data_dtype ()) == np .dtype (np_type ):
295
307
supported .add (np_type )
296
- obj .set_data_dtype (dt )
297
308
return supported
298
309
299
310
311
+ def supported_np_types (obj ):
312
+ """Numpy data types that instance `obj` supports
313
+
314
+ Parameters
315
+ ----------
316
+ obj : object
317
+ Object implementing `get_data_dtype` and `set_data_dtype`. The object
318
+ should raise ``HeaderDataError`` for setting unsupported dtypes. The
319
+ object will likely be a header or a :class:`SpatialImage`
320
+
321
+ Returns
322
+ -------
323
+ np_types : set
324
+ set of numpy types that `obj` supports
325
+ """
326
+ return _supported_np_types (obj .__class__ )
327
+
328
+
300
329
class ImageDataError (Exception ):
301
330
pass
302
331
0 commit comments