test_custom_plots.py 15.6 KB
Newer Older
1
2
"""Testing custom plots provided by Flint."""

Valentin Valls's avatar
Valentin Valls committed
3
import pytest
Valentin Valls's avatar
Valentin Valls committed
4
import gevent
Valentin Valls's avatar
Valentin Valls committed
5
import numpy
6
from bliss.common import plot
Valentin Valls's avatar
Valentin Valls committed
7
from bliss.controllers.lima import roi as lima_roi
8
9


Valentin Valls's avatar
Valentin Valls committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def test_plot__create_2_plot(flint_session):
    """Create 2 plots

    Make sure it exists 2 plots
    """
    p = plot.plot(name="Foo")
    pid = plot.get_flint()._pid
    assert "flint_pid={}".format(pid) in repr(p)
    assert p.name == "Foo"

    p = plot.plot(name="Some name")
    assert "flint_pid={}".format(pid) in repr(p)
    assert p.name == "Some name"


def test_plot__reuse_plot__api_1_0(flint_session):
    """Test reuse of custom plot from an ID"""
    widget = plot.plot_curve(name="foo")
    cos_data = numpy.cos(numpy.linspace(0, 2 * numpy.pi, 10))
    widget.add_data({"cos": cos_data, "foo": cos_data})
    widget2 = plot.plot_curve(name="foo", existing_id=widget.plot_id)
    cos = widget2.get_data()["cos"]
    numpy.testing.assert_allclose(cos, cos_data)
Valentin Valls's avatar
Valentin Valls committed
33
34


Valentin Valls's avatar
Valentin Valls committed
35
36
37
38
39
40
41
42
43
44
45
def test_plot__reuse_plot__api_1_6(flint_session):
    """Test reuse of custom plot from a name"""
    widget = plot.plot_curve(name="foo", existing_id="myplot")
    cos_data = numpy.cos(numpy.linspace(0, 2 * numpy.pi, 10))
    widget.add_data({"cos": cos_data, "foo": cos_data})
    widget2 = plot.plot_curve(name="foo", existing_id="myplot")
    cos = widget2.get_data()["cos"]
    numpy.testing.assert_allclose(cos, cos_data)


def test_plot__remove_plot(flint_session):
Valentin Valls's avatar
Valentin Valls committed
46
    flint = plot.get_flint()
Valentin Valls's avatar
Valentin Valls committed
47
    p = flint.get_plot(plot_class="curve", name="foo-rm", unique_name="foo-rm")
Valentin Valls's avatar
Valentin Valls committed
48
49
50
51
    flint.remove_plot(p.plot_id)
    assert flint.is_plot_exists("foo-rm") is False


Valentin Valls's avatar
Valentin Valls committed
52
53
54
55
56
57
58
def test_curveplot__create_empty(flint_session):
    flint = plot.get_flint()
    p = flint.get_plot(plot_class="curve", name="foo-empty", unique_name="foo-empty")
    assert flint.is_plot_exists("foo-empty")
    assert p is not None


59
60
def test_curveplot__bliss_1_8(flint_session):
    """Check custom plot curve API from BLISS <= 1.8"""
Valentin Valls's avatar
Valentin Valls committed
61
    flint = plot.get_flint()
62
    p = flint.get_plot(plot_class="curve", name="bliss-1.8")
Valentin Valls's avatar
Valentin Valls committed
63

64
65
    data1 = numpy.array([4, 5, 6])
    data2 = numpy.array([2, 5, 2])
Valentin Valls's avatar
Valentin Valls committed
66

67
68
69
    p.add_data({"data1": data1, "data2": data2})
    vrange = p.get_data_range()
    assert vrange == [None, None, None]
Valentin Valls's avatar
Valentin Valls committed
70

71
72
73
74
    p.select_data("data1", "data2")
    p.select_data("data1", "data2", color="green", symbol="x")
    vrange = p.get_data_range()
    assert vrange == [[4, 6], [2, 5], None]
Valentin Valls's avatar
Valentin Valls committed
75

76
77
78
79
80
81
82
83
84
85
    p.deselect_data("data1", "data2")
    vrange = p.get_data_range()
    assert vrange == [None, None, None]

    data = p.get_data("data1")
    assert data == pytest.approx(data1)

    p.remove_data("data1")
    data = p.get_data("data1")
    assert data == []
Valentin Valls's avatar
Valentin Valls committed
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
def test_curveplot__bliss_1_9(flint_session):
    """Check custom plot curve API from BLISS >= 1.9"""
    flint = plot.get_flint()
    p = flint.get_plot(plot_class="curve", name="bliss-1.9")
    p.submit("setRaiseOnException", True)

    y1 = numpy.array([4, 5, 6])
    y2 = numpy.array([2, 5, 2])
    x = numpy.array([1, 2, 3])

    # Setup the items
    p.add_curve_item("x", "y1", legend="item1", color="red", yaxis="left")
    p.add_curve_item("x", "y2", legend="item2", color="blue", yaxis="right")
    vrange = p.get_data_range()
    assert vrange == [None, None, None]

    # Update the data
    p.set_data(x=x, y1=y1, y2=y2)
    vrange = p.get_data_range()
    assert vrange == [[1, 3], [4, 6], [2, 5]]

    # Clear the data
    p.clear_data()
    vrange = p.get_data_range()
    assert vrange == [None, None, None]

    # Append the data
    for i in range(len(x)):
        p.append_data(x=x[i : i + 1], y1=y1[i : i + 1], y2=y2[i : i + 1])
    vrange = p.get_data_range()
    assert vrange == [[1, 3], [4, 6], [2, 5]]

    # Or change the item layout
    p.remove_item("item2")
    vrange = p.get_data_range()
    assert vrange == [[1, 3], [4, 6], None]

    # Check transaction
    p.clear_data()
    with p.transaction(resetzoom=False):
        p.set_data(x=x, y=y1)
        vrange = p.get_data_range()
        assert vrange == [None, None, None]
        p.add_curve_item("x", "y", legend="item")
        vrange = p.get_data_range()
        assert vrange == [None, None, None]
    vrange = p.get_data_range()
    assert vrange == [[1, 3], [4, 6], None]


Valentin Valls's avatar
Valentin Valls committed
138
def test_curveplot__reuse_plot(flint_session):
Valentin Valls's avatar
Valentin Valls committed
139
140
141
142
143
144
145
146
147
    flint = plot.get_flint()
    p = flint.get_plot(plot_class="curve", unique_name="foo-reuse")
    cos_data = numpy.cos(numpy.linspace(0, 2 * numpy.pi, 10))
    p.add_data({"cos": cos_data})
    p2 = flint.get_plot(plot_class="curve", unique_name="foo-reuse")
    data = p2.get_data("cos")
    assert data == pytest.approx(cos_data)


148
149
150
def test_select_points(flint_session):
    flint = plot.get_flint()
    p = plot.plot()
Valentin Valls's avatar
Valentin Valls committed
151
    context = []
152

Valentin Valls's avatar
Valentin Valls committed
153
154
155
156
157
158
159
160
161
162
163
164
    def active_gui():
        result = p.select_points(1)
        context.append(result)

    def do_actions():
        gevent.sleep(1)
        flint.test_mouse(
            p.plot_id, mode="click", position=(0, 0), relative_to_center=True
        )

    gevent.joinall([gevent.spawn(f) for f in [active_gui, do_actions]])
    assert len(context) == 1
165

Valentin Valls's avatar
Valentin Valls committed
166
    result = context[0]
167
168
169
170
171
    assert len(result) == 1
    assert len(result[0]) == 2


def test_select_shape(flint_session):
Valentin Valls's avatar
Valentin Valls committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    flint = plot.get_flint()
    p = plot.plot()
    context = []

    def active_gui():
        result = p.select_shape(shape="line")
        context.append(result)

    def do_actions():
        gevent.sleep(1)
        flint.test_mouse(
            p.plot_id, mode="click", position=(0, 0), relative_to_center=True
        )
        flint.test_mouse(
            p.plot_id, mode="click", position=(5, 5), relative_to_center=True
        )

    gevent.joinall([gevent.spawn(f) for f in [active_gui, do_actions]])
    assert len(context) == 1
191

Valentin Valls's avatar
Valentin Valls committed
192
    result = context[0]
193
194
195
196
    assert len(result) == 2
    assert len(result[0]) == 2
    assert len(result[1]) == 2

Valentin Valls's avatar
Valentin Valls committed
197

Valentin Valls's avatar
Valentin Valls committed
198
def test_select_shapes__rect(flint_session):
199
200
    flint = plot.get_flint()
    p = plot.plot()
201
    context = []
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
    def active_gui():
        result = p.select_shapes()
        context.append(result)

    def do_actions():
        gevent.sleep(1)
        flint.test_mouse(
            p.plot_id, mode="press", position=(-5, -5), relative_to_center=True
        )
        flint.test_mouse(
            p.plot_id, mode="release", position=(5, 5), relative_to_center=True
        )
        flint.test_active(p.plot_id, qaction="roi-apply-selection")

    gevent.joinall([gevent.spawn(f) for f in [active_gui, do_actions]])
    assert len(context) == 1

    result = context[0]
    assert len(result) == 1
    roi = result[0]
    assert isinstance(roi, dict)
    expected_keys = set(["origin", "size", "label", "kind"])
    assert len(expected_keys - roi.keys()) == 0
    assert roi["kind"] == "Rectangle"
Valentin Valls's avatar
Valentin Valls committed
227
228


Valentin Valls's avatar
Valentin Valls committed
229
230
231
232
233
234
def test_select_shapes__arc(flint_session):
    flint = plot.get_flint()
    p = plot.plot()
    context = []

    def active_gui():
235
        result = p.select_shapes(kinds=["lima-arc"])
Valentin Valls's avatar
Valentin Valls committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
        context.append(result)

    def do_actions():
        gevent.sleep(1)
        flint.test_mouse(
            p.plot_id, mode="press", position=(-5, -5), relative_to_center=True
        )
        flint.test_mouse(
            p.plot_id, mode="release", position=(5, 5), relative_to_center=True
        )
        flint.test_active(p.plot_id, qaction="roi-apply-selection")

    gevent.joinall([gevent.spawn(f) for f in [active_gui, do_actions]])
    assert len(context) == 1

    result = context[0]
    assert len(result) == 1
    roi = result[0]
Valentin Valls's avatar
Valentin Valls committed
254
    assert isinstance(roi, lima_roi.ArcRoi)
Valentin Valls's avatar
Valentin Valls committed
255
256


Valentin Valls's avatar
Valentin Valls committed
257
def test_select_shapes__rect_profile(flint_session):
Valentin Valls's avatar
Valentin Valls committed
258
259
260
261
262
    flint = plot.get_flint()
    p = plot.plot()
    context = []

    def active_gui():
263
        result = p.select_shapes(kinds=["lima-vertical-profile"])
Valentin Valls's avatar
Valentin Valls committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
        context.append(result)

    def do_actions():
        gevent.sleep(1)
        flint.test_mouse(
            p.plot_id, mode="press", position=(-5, -5), relative_to_center=True
        )
        flint.test_mouse(
            p.plot_id, mode="release", position=(5, 5), relative_to_center=True
        )
        flint.test_active(p.plot_id, qaction="roi-apply-selection")

    gevent.joinall([gevent.spawn(f) for f in [active_gui, do_actions]])
    assert len(context) == 1

    result = context[0]
    assert len(result) == 1
    roi = result[0]
Valentin Valls's avatar
Valentin Valls committed
282
283
    assert isinstance(roi, lima_roi.RoiProfile)
    assert roi.mode == "vertical"
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


def test_select_shapes__initial_selection(flint_session):
    flint = plot.get_flint()
    p = plot.plot()
    context = []
    roi_dict = dict(origin=(1, 2), size=(3, 4), kind="Rectangle", label="roi_dict")
    roi_rect = lima_roi.Roi(0, 1, 2, 3, name="roi_rect")
    roi_arc = lima_roi.ArcRoi(0, 1, 2, 3, 4, 5, name="roi_arc")
    roi_profile = lima_roi.RoiProfile(0, 1, 2, 3, mode="vertical", name="roi_profile")

    def active_gui():
        result = p.select_shapes(
            initial_selection=[roi_dict, roi_rect, roi_arc, roi_profile]
        )
        context.append(result)

    def do_actions():
        gevent.sleep(1)
        flint.test_active(p.plot_id, qaction="roi-apply-selection")

    gevent.joinall([gevent.spawn(f) for f in [active_gui, do_actions]])
    assert len(context) == 1

    rois = context[0]
    assert len(rois) == 4
    assert rois[0]["label"] == roi_dict["label"]
    assert rois[1].name == roi_rect.name
    assert rois[2].name == roi_arc.name
    assert rois[3].name == roi_profile.name
314
315


316
def test_plot1d(flint_session):
317
    f = plot.get_flint()
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
    p = f.get_plot(plot_class="plot1d", name="plot1d")

    # Check the default data setter
    x = numpy.arange(11) * 2
    y = numpy.arange(11)
    y2 = numpy.arange(11) / 2
    p.add_curve(x=x, y=y, legend="c1", yaxis="left")
    p.add_curve(x=x, y=y2, legend="c2", yaxis="right")
    vrange = p.get_data_range()
    assert vrange == [[0, 20], [0, 10], [0, 5]]

    # Clear the data
    p.clear_data()
    vrange = p.get_data_range()
    assert vrange == [None, None, None]

    # Check deprecated API
    x = numpy.arange(11) * 2
    y = numpy.arange(11)
    y2 = numpy.arange(11) / 2
    p.add_data(x, field="x")
    p.add_data(y, field="y")
    p.add_data(y2, field="y2")
    p.select_data("x", "y", yaxis="left")
    p.select_data("x", "y2", yaxis="right")
    vrange = p.get_data_range()
    assert vrange == [[0, 20], [0, 10], [0, 5]]

    # Check the default way to clear data
    p.clear_data()
    vrange = p.get_data_range()
    assert vrange[0:2] == [None, None]
350

351
352
353
354
355
356
357
358
    # Check the scales
    assert p.xscale == "linear"
    assert p.yscale == "linear"
    p.xscale = "log"
    p.yscale = "log"
    assert p.xscale == "log"
    assert p.yscale == "log"

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461

def test_plot2d(flint_session):
    f = plot.get_flint()
    p = f.get_plot(plot_class="plot2d", name="plot2d")

    # Check the default data setter
    image = numpy.arange(10 * 10)
    image.shape = 10, 10
    p.add_image(image)
    vrange = p.get_data_range()
    assert vrange[0:2] == [[0, 10], [0, 10]]

    # FIXME: addImage have to support this API
    # p.set_colormap(lut="viridis", vmin=0, vmax=10)

    # Check the default way to clear data
    p.clear_data()
    vrange = p.get_data_range()
    assert vrange[0:2] == [None, None]

    # Check deprecated API
    image = numpy.arange(9 * 9)
    image.shape = 9, 9
    p.add_data(image, field="image")
    p.select_data("image")
    vrange = p.get_data_range()
    assert vrange[0:2] == [[0, 9], [0, 9]]

    # Check the default way to clear data
    p.clear_data()
    vrange = p.get_data_range()
    assert vrange[0:2] == [None, None]


def test_scatter_view(flint_session):
    f = plot.get_flint()
    p = f.get_plot(plot_class="scatter", name="scatterview")

    # Check the default data setter
    x = numpy.arange(11)
    y = numpy.arange(11)
    value = numpy.arange(11)
    p.set_data(x=x, y=y, value=value)
    vrange = p.get_data_range()
    assert vrange[0:2] == [[0, 10], [0, 10]]

    # Allow to setup the colormap
    p.set_colormap(lut="viridis", vmin=0, vmax=10)

    # Set none can be use to clear the data
    p.set_data(None, None, None)
    vrange = p.get_data_range()
    assert vrange[0:2] == [None, None]

    # Check deprecated API
    x = numpy.arange(9)
    y = numpy.arange(9)
    value = numpy.arange(9)
    p.add_data(x, field="x")
    p.add_data(y, field="y")
    p.add_data(value, field="value")
    p.select_data("x", "y", "value")
    vrange = p.get_data_range()
    assert vrange[0:2] == [[0, 8], [0, 8]]

    # Check the default way to clear data
    p.clear_data()
    vrange = p.get_data_range()
    assert vrange[0:2] == [None, None]


def test_image_view(flint_session):
    f = plot.get_flint()
    p = f.get_plot(plot_class="imageview", name="imageview")

    # Check the default data setter
    image = numpy.arange(10 * 10)
    image.shape = 10, 10
    p.set_data(image)
    vrange = p.get_data_range()
    assert vrange[0:2] == [[0, 10], [0, 10]]

    # Allow to setup the colormap
    p.set_colormap(lut="viridis", vmin=0, vmax=10)

    # Set none can be use to clear the data
    p.set_data(None)
    vrange = p.get_data_range()
    assert vrange[0:2] == [None, None]

    # Check deprecated API
    image = numpy.arange(9 * 9)
    image.shape = 9, 9
    p.add_data(image, field="image")
    p.select_data("image")
    vrange = p.get_data_range()
    assert vrange[0:2] == [[0, 9], [0, 9]]

    # Check the default way to clear data
    p.clear_data()
    vrange = p.get_data_range()
    assert vrange[0:2] == [None, None]

462
463
464
465
466
467
    assert p.yaxis_direction == "up"
    p.yaxis_direction = "down"
    assert p.yaxis_direction == "down"
    p.yaxis_direction = "up"
    assert p.yaxis_direction == "up"

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502

def test_image_stack(flint_session):
    f = plot.get_flint()
    p = f.get_plot(plot_class="imagestack", name="imagestack")

    cube = numpy.arange(10 * 10 * 10)
    cube.shape = 10, 10, 10

    # Check the default data setter
    p.set_data(cube)
    vrange = p.get_data_range()
    assert vrange[0:2] == [[0, 10], [0, 10]]

    # Allow to setup the colormap
    p.set_colormap(lut="viridis", vmin=0, vmax=10)

    # Set none can be use to clear the data
    p.set_data(None)
    vrange = p.get_data_range()
    assert vrange[0:2] == [None, None]

    # Check deprecated API
    p.add_data(cube, field="cube")
    p.select_data("cube")
    vrange = p.get_data_range()
    assert vrange[0:2] == [[0, 10], [0, 10]]

    # Check the default way to clear data
    p.clear_data()
    vrange = p.get_data_range()
    assert vrange[0:2] == [None, None]


def test_curve_stack(flint_session):
    f = plot.get_flint()
503
504
505
506
507
508
509
510
511
512
513
514
515
516
    p = f.get_plot(plot_class="curvestack", name="curve-stack")

    curves = numpy.empty((10, 100))
    for i in range(10):
        curves[i] = numpy.sin(numpy.arange(100) / 30 + i * 6)
    x = numpy.arange(100) * 10

    p.set_data(curves=curves, x=x)
    vrange = p.get_data_range()
    assert vrange[0] == [0, 990]

    p.clear_data()
    vrange = p.get_data_range()
    assert vrange == [None, None]
517
518
519
520
521
522
523
524


def test_time_curve_plot(flint_session):
    """Coverage of the main time curve plot API"""
    f = plot.get_flint()

    p = f.get_plot(plot_class="timecurveplot", name="timecurveplot")

525
526
    p.add_time_curve_item("diode1")
    p.add_time_curve_item("diode2")
527
528
529
530
531
532
533
534
535
536
537
538
539

    # set_data update the curves
    p.set_data(time=[0, 1, 2], diode1=[0, 1, 1], diode2=[1, 5, 1])
    vrange = p.get_data_range()
    assert vrange[0] == [0, 2]
    assert vrange[1] == [0, 5]

    # append data update the data on the right side
    p.append_data(time=[3], diode1=[2], diode2=[6])
    vrange = p.get_data_range()
    assert vrange[0] == [0, 3]
    assert vrange[1] == [0, 6]

540
541
    # when a fixed duration is used, the oldest data disappear
    p.ttl = 5
542
543
544
545
546
547
548
549
550
    p.append_data(time=[10], diode1=[2], diode2=[6])
    vrange = p.get_data_range()
    assert vrange[0][0] > 1
    assert vrange[0][1] == 10

    # clean data clean up the plot
    p.clear_data()
    vrange = p.get_data_range()
    assert vrange == [None, None]