Skip to content

Commit f02da19

Browse files
committed
Coerce 64-bit ints as well as floats
The base class warns about 64-bit ints in the serializer, which is mostly a source of noice. Here, we try to be smarter about it by casting on validation, and only giving a warning if the original source was another ndarray.
1 parent 7fba16b commit f02da19

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pythreejs/traits.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
from collections import namedtuple, Sequence
5+
import warnings
56

67
from traitlets import (
78
Unicode, Int, CInt, Instance, Enum, List, Dict, Float, CFloat,
@@ -12,6 +13,8 @@
1213

1314
from ipydatawidgets import DataUnion, NDArrayWidget
1415

16+
import numpy as np
17+
1518

1619
def _castable_namedtuple(typename, field_names):
1720
base = namedtuple('%s_base' % typename, field_names)
@@ -154,15 +157,25 @@ class WebGLDataUnion(DataUnion):
154157
Also constrains the use of 64-bit arrays, as this is not supported by WebGL.
155158
"""
156159
def validate(self, obj, value):
160+
was_original_array = isinstance(value, np.ndarray)
161+
print(value, was_original_array)
157162
value = super(WebGLDataUnion, self).validate(obj, value)
158163
array = value.array if isinstance(value, NDArrayWidget) else value
159164

160-
if array is not Undefined and str(array.dtype) == 'float64':
165+
dtype_str = str(array.dtype) if array is not Undefined else ''
166+
if dtype_str == 'float64' or dtype_str.endswith('int64'):
161167
if isinstance(value, NDArrayWidget):
162-
raise TraitError('Cannot use a float64 data widget as a BufferAttribute source.')
168+
raise TraitError(
169+
'Cannot use a %s data widget as a WebGL source.' %
170+
(dtype_str,))
163171
else:
164172
# 64-bit not supported, coerce to 32-bit
165-
value = value.astype('float32')
173+
# If original was another array, warn about casting,
174+
# as it might otherwise silently increase memory usage:
175+
if was_original_array:
176+
warnings.warn('64-bit data types not supported for WebGL '
177+
'data, casting to 32-bit.')
178+
value = value.astype(dtype_str.replace('64', '32'))
166179
return value
167180

168181

0 commit comments

Comments
 (0)