forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
322 lines (248 loc) · 11.2 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
Module to test plotly.utils with optional dependencies.
"""
from __future__ import absolute_import
import datetime
import math
import decimal
from datetime import datetime as dt
from unittest import TestCase
import numpy as np
import pandas as pd
import pytz
from nose.plugins.attrib import attr
from pandas.util.testing import assert_series_equal
import json as _json
from plotly import optional_imports, utils
from plotly.graph_objs import Scatter, Scatter3d, Figure, Data
from plotly.grid_objs import Column
matplotlylib = optional_imports.get_module('plotly.matplotlylib')
if matplotlylib:
import matplotlib.pyplot as plt
from plotly.matplotlylib import Exporter, PlotlyRenderer
class TestJSONEncoder(TestCase):
def test_encode_as_plotly(self):
# should *fail* when object doesn't have `to_plotly_json` attribute
objs_without_attr = [
1, 'one', set(['a', 'set']), {'a': 'dict'}, ['a', 'list']
]
for obj in objs_without_attr:
self.assertRaises(utils.NotEncodable,
utils.PlotlyJSONEncoder.encode_as_plotly, obj)
# should return without exception when obj has `to_plotly_josn` attr
expected_res = 'wedidit'
class ObjWithAttr(object):
def to_plotly_json(self):
return expected_res
res = utils.PlotlyJSONEncoder.encode_as_plotly(ObjWithAttr())
self.assertEqual(res, expected_res)
def test_encode_as_list(self):
# should *fail* when object doesn't have `tolist` method
objs_without_attr = [
1, 'one', set(['a', 'set']), {'a': 'dict'}, ['a', 'list']
]
for obj in objs_without_attr:
self.assertRaises(utils.NotEncodable,
utils.PlotlyJSONEncoder.encode_as_list, obj)
# should return without exception when obj has `tolist` attr
expected_res = ['some', 'list']
class ObjWithAttr(object):
def tolist(self):
return expected_res
res = utils.PlotlyJSONEncoder.encode_as_list(ObjWithAttr())
self.assertEqual(res, expected_res)
def test_encode_as_pandas(self):
# should *fail* on things that are not specific pandas objects
not_pandas = ['giraffe', 6, float('nan'), ['a', 'list']]
for obj in not_pandas:
self.assertRaises(utils.NotEncodable,
utils.PlotlyJSONEncoder.encode_as_pandas, obj)
# should succeed when we've got specific pandas thingies
res = utils.PlotlyJSONEncoder.encode_as_pandas(pd.NaT)
self.assertTrue(res is None)
def test_encode_as_numpy(self):
# should *fail* on non-numpy-y things
not_numpy = ['hippo', 8, float('nan'), {'a': 'dict'}]
for obj in not_numpy:
self.assertRaises(utils.NotEncodable,
utils.PlotlyJSONEncoder.encode_as_numpy, obj)
# should succeed with numpy-y-thingies
res = utils.PlotlyJSONEncoder.encode_as_numpy(np.ma.core.masked)
self.assertTrue(math.isnan(res))
def test_encode_valid_datetime(self):
# should *fail* without 'utcoffset' and 'isoformat' and '__sub__' attrs
#non_datetimes = [datetime.date(2013, 10, 1), 'noon', 56, '00:00:00']
non_datetimes = [datetime.date(2013, 10, 1)]
for obj in non_datetimes:
self.assertRaises(utils.NotEncodable,
utils.PlotlyJSONEncoder.encode_as_datetime, obj)
def test_encode_as_datetime(self):
# should succeed with 'utcoffset', 'isoformat' and '__sub__' attrs
res = utils.PlotlyJSONEncoder.encode_as_datetime(
datetime.datetime(2013, 10, 1)
)
self.assertEqual(res, '2013-10-01')
def test_encode_as_datetime_with_microsecond(self):
# should not include extraneous microsecond info if DNE
res = utils.PlotlyJSONEncoder.encode_as_datetime(
datetime.datetime(2013, 10, 1, microsecond=0)
)
self.assertEqual(res, '2013-10-01')
# should include microsecond info if present
res = utils.PlotlyJSONEncoder.encode_as_datetime(
datetime.datetime(2013, 10, 1, microsecond=10)
)
self.assertEqual(res, '2013-10-01 00:00:00.000010')
def test_encode_as_datetime_with_localized_tz(self):
# should convert tzinfo to utc. Note that in october, we're in EDT!
# therefore the 4 hour difference is correct.
naive_datetime = datetime.datetime(2013, 10, 1)
aware_datetime = pytz.timezone('US/Eastern').localize(naive_datetime)
res = utils.PlotlyJSONEncoder.encode_as_datetime(aware_datetime)
self.assertEqual(res, '2013-10-01 04:00:00')
def test_encode_as_date(self):
# should *fail* without 'utcoffset' and 'isoformat' and '__sub__' attrs
non_datetimes = ['noon', 56, '00:00:00']
for obj in non_datetimes:
self.assertRaises(utils.NotEncodable,
utils.PlotlyJSONEncoder.encode_as_date, obj)
# should work with a date
a_date = datetime.date(2013, 10, 1)
res = utils.PlotlyJSONEncoder.encode_as_date(a_date)
self.assertEqual(res, '2013-10-01')
# should also work with a date time without a utc offset!
res = utils.PlotlyJSONEncoder.encode_as_date(
datetime.datetime(2013, 10, 1, microsecond=10)
)
self.assertEqual(res, '2013-10-01 00:00:00.000010')
def test_encode_as_decimal(self):
# should work with decimal values
res = utils.PlotlyJSONEncoder.encode_as_decimal(decimal.Decimal(1.023452))
self.assertAlmostEqual(res, 1.023452) # Checks upto 7 decimal places
self.assertIsInstance(res, float)
## JSON encoding
numeric_list = [1, 2, 3]
np_list = np.array([1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)])
mixed_list = [1, 'A', dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1),
dt(2014, 1, 5, 1, 1, 1, 1)]
dt_list = [dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1),
dt(2014, 1, 5, 1, 1, 1, 1)]
df = pd.DataFrame(columns=['col 1'],
data=[1, 2, 3, dt(2014, 1, 5), pd.NaT, np.NaN, np.Inf])
rng = pd.date_range('1/1/2011', periods=2, freq='H')
ts = pd.Series([1.5, 2.5], index=rng)
def test_column_json_encoding():
columns = [
Column(numeric_list, 'col 1'),
Column(mixed_list, 'col 2'),
Column(np_list, 'col 3')
]
json_columns = _json.dumps(
columns, cls=utils.PlotlyJSONEncoder, sort_keys=True
)
assert('[{"data": [1, 2, 3], "name": "col 1"}, '
'{"data": [1, "A", "2014-01-05", '
'"2014-01-05 01:01:01", '
'"2014-01-05 01:01:01.000001"], '
'"name": "col 2"}, '
'{"data": [1, 2, 3, null, null, null, '
'"2014-01-05"], "name": "col 3"}]' == json_columns)
def test_figure_json_encoding():
df = pd.DataFrame(columns=['col 1'], data=[1, 2, 3])
s1 = Scatter3d(x=numeric_list, y=np_list, z=mixed_list)
s2 = Scatter(x=df['col 1'])
data = Data([s1, s2])
figure = Figure(data=data)
js1 = _json.dumps(s1, cls=utils.PlotlyJSONEncoder, sort_keys=True)
js2 = _json.dumps(s2, cls=utils.PlotlyJSONEncoder, sort_keys=True)
assert(js1 == '{"type": "scatter3d", "x": [1, 2, 3], '
'"y": [1, 2, 3, null, null, null, "2014-01-05"], '
'"z": [1, "A", "2014-01-05", '
'"2014-01-05 01:01:01", "2014-01-05 01:01:01.000001"]}')
assert(js2 == '{"type": "scatter", "x": [1, 2, 3]}')
# Test JSON encoding works
_json.dumps(data, cls=utils.PlotlyJSONEncoder, sort_keys=True)
_json.dumps(figure, cls=utils.PlotlyJSONEncoder, sort_keys=True)
# Test data wasn't mutated
np_array = np.array(
[1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)]
)
for k in range(len(np_array)):
if k in [3, 4]:
# check NaN
assert np.isnan(np_list[k]) and np.isnan(np_array[k])
else:
# non-NaN
assert np_list[k] == np_array[k]
assert(set(data[0]['z']) ==
set([1, 'A', dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1),
dt(2014, 1, 5, 1, 1, 1, 1)]))
def test_datetime_json_encoding():
j1 = _json.dumps(dt_list, cls=utils.PlotlyJSONEncoder)
assert(j1 == '["2014-01-05", '
'"2014-01-05 01:01:01", '
'"2014-01-05 01:01:01.000001"]')
j2 = _json.dumps({"x": dt_list}, cls=utils.PlotlyJSONEncoder)
assert(j2 == '{"x": ["2014-01-05", '
'"2014-01-05 01:01:01", '
'"2014-01-05 01:01:01.000001"]}')
def test_pandas_json_encoding():
j1 = _json.dumps(df['col 1'], cls=utils.PlotlyJSONEncoder)
print(j1)
print('\n')
assert(j1 == '[1, 2, 3, "2014-01-05", null, null, null]')
# Test that data wasn't mutated
assert_series_equal(df['col 1'],
pd.Series([1, 2, 3, dt(2014, 1, 5),
pd.NaT, np.NaN, np.Inf], name='col 1'))
j2 = _json.dumps(df.index, cls=utils.PlotlyJSONEncoder)
assert(j2 == '[0, 1, 2, 3, 4, 5, 6]')
nat = [pd.NaT]
j3 = _json.dumps(nat, cls=utils.PlotlyJSONEncoder)
assert(j3 == '[null]')
assert(nat[0] is pd.NaT)
j4 = _json.dumps(rng, cls=utils.PlotlyJSONEncoder)
assert(j4 == '["2011-01-01", "2011-01-01 01:00:00"]')
j5 = _json.dumps(ts, cls=utils.PlotlyJSONEncoder)
assert(j5 == '[1.5, 2.5]')
assert_series_equal(ts, pd.Series([1.5, 2.5], index=rng))
j6 = _json.dumps(ts.index, cls=utils.PlotlyJSONEncoder)
assert(j6 == '["2011-01-01", "2011-01-01 01:00:00"]')
def test_numpy_masked_json_encoding():
l = [1, 2, np.ma.core.masked]
j1 = _json.dumps(l, cls=utils.PlotlyJSONEncoder)
print(j1)
assert(j1 == '[1, 2, null]')
if matplotlylib:
@attr('matplotlib')
def test_masked_constants_example():
# example from: https://gist.github.com/tschaume/d123d56bf586276adb98
data = {
'esN': [0, 1, 2, 3],
'ewe_is0': [-398.11901997, -398.11902774,
-398.11897111, -398.11882215],
'ewe_is1': [-398.11793027, -398.11792966, -398.11786308, None],
'ewe_is2': [-398.11397008, -398.11396421, None, None]
}
df = pd.DataFrame.from_dict(data)
plotopts = {'x': 'esN'}
fig, ax = plt.subplots(1, 1)
df.plot(ax=ax, **plotopts)
renderer = PlotlyRenderer()
Exporter(renderer).run(fig)
_json.dumps(renderer.plotly_fig, cls=utils.PlotlyJSONEncoder)
jy = _json.dumps(renderer.plotly_fig['data'][1]['y'],
cls=utils.PlotlyJSONEncoder)
print(jy)
array = _json.loads(jy)
assert(array == [-398.11793027, -398.11792966, -398.11786308, None])
def test_numpy_dates():
a = np.arange(np.datetime64('2011-07-11'), np.datetime64('2011-07-18'))
j1 = _json.dumps(a, cls=utils.PlotlyJSONEncoder)
assert(j1 == '["2011-07-11", "2011-07-12", "2011-07-13", '
'"2011-07-14", "2011-07-15", "2011-07-16", '
'"2011-07-17"]')
def test_datetime_dot_date():
a = [datetime.date(2014, 1, 1), datetime.date(2014, 1, 2)]
j1 = _json.dumps(a, cls=utils.PlotlyJSONEncoder)
assert(j1 == '["2014-01-01", "2014-01-02"]')