| 1 |
import bqplot |
| 2 |
import numpy as np |
| 3 |
import pandas as pd |
| 4 |
from bqplot.traits import array_to_json, array_from_json |
| 5 |
import pytest |
| 6 |
|
| 7 |
|
| 8 |
def test_binary_serialize_1d(figure): |
| 9 |
x = np.arange(10, dtype=np.float64) |
| 10 |
y = (x**2).astype(np.int32) |
| 11 |
scatter = bqplot.Scatter(x=x, y=y) |
| 12 |
|
| 13 |
state = scatter.get_state() |
| 14 |
assert state['x']['dtype'] == 'float64' |
| 15 |
assert state['y']['dtype'] == 'int32' |
| 16 |
|
| 17 |
assert state['x']['value'] == memoryview(x) |
| 18 |
assert state['y']['value'] == memoryview(y) |
| 19 |
|
| 20 |
assert state['x']['shape'] == (10,) |
| 21 |
assert state['y']['shape'] == (10,) |
| 22 |
|
| 23 |
scatter2 = bqplot.Scatter() |
| 24 |
scatter2.set_state(state) |
| 25 |
|
| 26 |
assert scatter.x.dtype == np.float64 |
| 27 |
assert scatter.y.dtype == np.int32 |
| 28 |
|
| 29 |
assert scatter.x.shape == (10,) |
| 30 |
assert scatter.y.shape == (10,) |
| 31 |
|
| 32 |
assert scatter2.x.tolist() == x.tolist() |
| 33 |
assert scatter2.y.tolist() == y.tolist() |
| 34 |
|
| 35 |
|
| 36 |
def test_binary_serialize_datetime(): |
| 37 |
x = np.arange('2005-02-25', '2005-03', dtype='datetime64[D]') |
| 38 |
x_ms = np.array([1109289600000, 1109376000000, 1109462400000, 1109548800000], dtype=np.int64) |
| 39 |
scatter = bqplot.Scatter(x=x) |
| 40 |
|
| 41 |
state = scatter.get_state() |
| 42 |
assert state['x']['dtype'] == 'float64' |
| 43 |
assert np.array(state['x']['value'], dtype=np.float64).astype(np.int64).tolist() == x_ms.tolist() |
| 44 |
|
| 45 |
x = np.array([pd.Timestamp('2005-02-25'), pd.Timestamp('2005-02-26'), pd.Timestamp('2005-02-27'), pd.Timestamp('2005-02-28')]) |
| 46 |
scatter = bqplot.Scatter(x=x) |
| 47 |
|
| 48 |
state = scatter.get_state() |
| 49 |
assert state['x']['dtype'] == 'float64' |
| 50 |
assert np.array(state['x']['value'], dtype=np.float64).astype(np.int64).tolist() == x_ms.tolist() |
| 51 |
|
| 52 |
|
| 53 |
# currently a roundtrip does not converse the datetime64 type |
| 54 |
scatter2 = bqplot.Scatter() |
| 55 |
scatter2.set_state(state) |
| 56 |
|
| 57 |
assert scatter2.x.dtype.kind == 'M' |
| 58 |
assert scatter2.x.astype(np.int64).tolist() == x_ms.tolist() |
| 59 |
|
| 60 |
|
| 61 |
def test_binary_serialize_text(): |
| 62 |
# string do not get serialized in binary (since numpy uses utf32, and js/browsers do not support that) |
| 63 |
text = np.array(['aap', 'noot', 'mies']) |
| 64 |
label = bqplot.Label(text=text) |
| 65 |
|
| 66 |
state = label.get_state() |
| 67 |
assert state['text'] == ['aap', 'noot', 'mies'] |
| 68 |
|
| 69 |
|
| 70 |
# currently a roundtrip does not converse the datetime64 type |
| 71 |
label2 = bqplot.Label() |
| 72 |
label2.set_state(state) |
| 73 |
|
| 74 |
assert label2.text.tolist() == label.text.tolist() |
| 75 |
|
| 76 |
|
| 77 |
def test_dtype_with_str(): |
| 78 |
# dtype object is not supported |
| 79 |
text = np.array(['foo', None, 'bar']) |
| 80 |
assert text.dtype == object |
| 81 |
with pytest.raises(ValueError, match='.*Unsupported dtype object*'), pytest.warns(UserWarning): |
| 82 |
array_to_json(text) |
| 83 |
# but if they contain all strings, it should convert them. |
| 84 |
# This is for backward compatibility of expecting pandas dataframe |
| 85 |
# string columns to work (which are of dtype==np.object) |
| 86 |
text[1] = 'foobar' |
| 87 |
assert array_to_json(text) == ['foo', 'foobar', 'bar'] |
| 88 |
|
| 89 |
|
| 90 |
def test_serialize_nested_list(): |
| 91 |
data = np.array([ |
| 92 |
[0, 1, 2, 3, 4, 5, 6], |
| 93 |
[0, 1, 2, 2, 3], |
| 94 |
[0, 1, 2, 3, 4, 5, 6, 7], |
| 95 |
], dtype=object) |
| 96 |
|
| 97 |
serialized_data = array_to_json(data) |
| 98 |
|
| 99 |
assert len(serialized_data) == 3 |
| 100 |
|
| 101 |
assert serialized_data[0]['dtype'] == 'int32' |
| 102 |
assert serialized_data[0]['value'] == memoryview(np.array(data[0])) |
| 103 |
|
| 104 |
assert serialized_data[1]['dtype'] == 'int32' |
| 105 |
assert serialized_data[1]['value'] == memoryview(np.array(data[1])) |
| 106 |
|
| 107 |
assert serialized_data[2]['dtype'] == 'int32' |
| 108 |
assert serialized_data[2]['value'] == memoryview(np.array(data[2])) |
| 109 |
|
| 110 |
deserialized_data = array_from_json(serialized_data) |
| 111 |
|
| 112 |
for el, deserialized_el in zip(data, deserialized_data): |
| 113 |
assert np.all(el == deserialized_el) |
| 114 |
|
| 115 |
data = np.array([ |
| 116 |
[0, 1, 2, 3, 4, 5, 6], |
| 117 |
np.array([0, 1, 2, 2, 3]), |
| 118 |
[0, 1, 2, 3] |
| 119 |
], dtype=object) |
| 120 |
|
| 121 |
serialized_data = array_to_json(data) |
| 122 |
|
| 123 |
assert len(serialized_data) == 3 |
| 124 |
|
| 125 |
assert serialized_data[0]['dtype'] == 'int32' |
| 126 |
assert serialized_data[0]['value'] == memoryview(np.array(data[0])) |
| 127 |
|
| 128 |
assert serialized_data[1]['dtype'] == 'int32' |
| 129 |
assert serialized_data[1]['value'] == memoryview(np.array(data[1])) |
| 130 |
|
| 131 |
assert serialized_data[2]['dtype'] == 'int32' |
| 132 |
assert serialized_data[2]['value'] == memoryview(np.array(data[2])) |
| 133 |
|
| 134 |
deserialized_data = array_from_json(serialized_data) |
| 135 |
|
| 136 |
for el, deserialized_el in zip(data, deserialized_data): |
| 137 |
assert np.all(el == deserialized_el) |
| 138 |
|
| 139 |
data = np.array([ |
| 140 |
['Hello', 'Hallo'], |
| 141 |
['Coucou', 'Hi', 'Ciao'] |
| 142 |
], dtype=object) |
| 143 |
|
| 144 |
serialized_data = array_to_json(data) |
| 145 |
|
| 146 |
assert len(serialized_data) == 2 |
| 147 |
|
| 148 |
assert serialized_data[0] == ['Hello', 'Hallo'] |
| 149 |
assert serialized_data[1] == ['Coucou', 'Hi', 'Ciao'] |
| 150 |
|
| 151 |
deserialized_data = array_from_json(serialized_data) |
| 152 |
|
| 153 |
assert np.all(data == deserialized_data) |