|
| 1 | +# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file. |
| 2 | + |
| 3 | +""" |
| 4 | +X axis scale draw unit tests |
| 5 | +---------------------------- |
| 6 | +
|
| 7 | +The X axis of the signal view keeps its scale draw when the view is refreshed, so |
| 8 | +that its ticks stay aligned with the grid and the curves (the scale draw is only |
| 9 | +replaced when leaving the date/time mode). |
| 10 | +""" |
| 11 | + |
| 12 | +# guitest: show |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from datetime import datetime, timedelta |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +import pytest |
| 20 | +from plotpy.plot import BasePlot |
| 21 | +from qwt import QwtDateTimeScaleDraw |
| 22 | +from sigima.objects import SignalObj, create_signal |
| 23 | + |
| 24 | +from datalab.tests import datalab_test_app_context |
| 25 | + |
| 26 | +# X bounds which are not graduations of the axis once autoscaled (as for the |
| 27 | +# 'step.npy' test file): end labels do not overflow the axis ends |
| 28 | +X_MIN, X_MAX = 3.1e-7, 4.08e-7 |
| 29 | + |
| 30 | + |
| 31 | +def _create_step_signal() -> SignalObj: |
| 32 | + """Create a signal whose X axis bounds are not graduations""" |
| 33 | + x = np.linspace(X_MIN, X_MAX, 500) |
| 34 | + return create_signal("Step", x, np.tanh((x - 3.15e-7) * 1e8)) |
| 35 | + |
| 36 | + |
| 37 | +def _create_datetime_signal() -> SignalObj: |
| 38 | + """Create a signal with a date/time X axis""" |
| 39 | + start = datetime(2025, 1, 1) |
| 40 | + timestamps = [start + timedelta(seconds=i) for i in range(100)] |
| 41 | + obj = create_signal("Date/time", np.arange(100.0), np.sin(np.arange(100.0) / 10)) |
| 42 | + obj.set_x_from_datetime(timestamps, unit="s") |
| 43 | + return obj |
| 44 | + |
| 45 | + |
| 46 | +def _x_axis_ends(plot: BasePlot) -> tuple[list[float], list[float]]: |
| 47 | + """Return the ends of the X axis scale and of the X canvas map, in canvas |
| 48 | + coordinates""" |
| 49 | + widget = plot.axisWidget(plot.xBottom) |
| 50 | + offset = widget.x() - plot.canvas().x() |
| 51 | + smap = widget.scaleDraw().scaleMap() |
| 52 | + cmap = plot.canvasMap(plot.xBottom) |
| 53 | + return ( |
| 54 | + sorted((smap.p1() + offset, smap.p2() + offset)), |
| 55 | + sorted((cmap.p1(), cmap.p2())), |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def test_refresh_keeps_x_scale_draw() -> None: |
| 60 | + """Refreshing the signal view keeps the scale draw of the X axis""" |
| 61 | + with datalab_test_app_context(console=False, exec_loop=False) as win: |
| 62 | + panel = win.signalpanel |
| 63 | + plot = panel.plothandler.plot |
| 64 | + panel.add_object(_create_step_signal()) |
| 65 | + scale_draw = plot.axisScaleDraw(plot.xBottom) |
| 66 | + panel.manual_refresh() |
| 67 | + assert plot.axisScaleDraw(plot.xBottom) is scale_draw |
| 68 | + panel.add_object(_create_step_signal()) |
| 69 | + assert plot.axisScaleDraw(plot.xBottom) is scale_draw |
| 70 | + |
| 71 | + |
| 72 | +def test_x_axis_aligned_with_canvas() -> None: |
| 73 | + """The X axis ticks are aligned with the grid and the curves after loading""" |
| 74 | + with datalab_test_app_context(console=False, exec_loop=False) as win: |
| 75 | + panel = win.signalpanel |
| 76 | + plot = panel.plothandler.plot |
| 77 | + panel.add_object(_create_step_signal()) |
| 78 | + scale_ends, canvas_ends = _x_axis_ends(plot) |
| 79 | + assert scale_ends == pytest.approx(canvas_ends, abs=0.5) |
| 80 | + panel.manual_refresh() |
| 81 | + scale_ends, canvas_ends = _x_axis_ends(plot) |
| 82 | + assert scale_ends == pytest.approx(canvas_ends, abs=0.5) |
| 83 | + |
| 84 | + |
| 85 | +def test_datetime_x_axis_restored() -> None: |
| 86 | + """The date/time X axis formatting is removed when showing regular signals""" |
| 87 | + with datalab_test_app_context(console=False, exec_loop=False) as win: |
| 88 | + panel = win.signalpanel |
| 89 | + plot = panel.plothandler.plot |
| 90 | + panel.add_object(_create_datetime_signal()) |
| 91 | + assert isinstance(plot.axisScaleDraw(plot.xBottom), QwtDateTimeScaleDraw) |
| 92 | + panel.add_object(_create_step_signal()) |
| 93 | + scale_draw = plot.axisScaleDraw(plot.xBottom) |
| 94 | + assert not isinstance(scale_draw, QwtDateTimeScaleDraw) |
| 95 | + panel.manual_refresh() |
| 96 | + assert plot.axisScaleDraw(plot.xBottom) is scale_draw |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + test_refresh_keeps_x_scale_draw() |
| 101 | + test_x_axis_aligned_with_canvas() |
| 102 | + test_datetime_x_axis_restored() |
0 commit comments