Skip to content

Commit 25409ff

Browse files
authored
Merge pull request jupyter-widgets#170 from vidartf/coerce_int64
Coerce 64-bit ints as well as floats
2 parents bfa16dc + 40f4dd4 commit 25409ff

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

pythreejs/traits.py

+15-3
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,24 @@ 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)
157161
value = super(WebGLDataUnion, self).validate(obj, value)
158162
array = value.array if isinstance(value, NDArrayWidget) else value
159163

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

168180

0 commit comments

Comments
 (0)