|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
4 | 4 | from collections import namedtuple, Sequence
|
| 5 | +import warnings |
5 | 6 |
|
6 | 7 | from traitlets import (
|
7 | 8 | Unicode, Int, CInt, Instance, Enum, List, Dict, Float, CFloat,
|
|
12 | 13 |
|
13 | 14 | from ipydatawidgets import DataUnion, NDArrayWidget
|
14 | 15 |
|
| 16 | +import numpy as np |
| 17 | + |
15 | 18 |
|
16 | 19 | def _castable_namedtuple(typename, field_names):
|
17 | 20 | base = namedtuple('%s_base' % typename, field_names)
|
@@ -154,15 +157,24 @@ class WebGLDataUnion(DataUnion):
|
154 | 157 | Also constrains the use of 64-bit arrays, as this is not supported by WebGL.
|
155 | 158 | """
|
156 | 159 | def validate(self, obj, value):
|
| 160 | + was_original_array = isinstance(value, np.ndarray) |
157 | 161 | value = super(WebGLDataUnion, self).validate(obj, value)
|
158 | 162 | array = value.array if isinstance(value, NDArrayWidget) else value
|
159 | 163 |
|
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'): |
161 | 166 | 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,)) |
163 | 170 | else:
|
164 | 171 | # 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')) |
166 | 178 | return value
|
167 | 179 |
|
168 | 180 |
|
|
0 commit comments