Commit ad751970 authored by Valentin Valls's avatar Valentin Valls
Browse files

Added scan_info helper to create curve plot

parent 0f9b42d4
......@@ -286,8 +286,8 @@ class ScanInfo(dict):
This can be used as default plot for the scan.
Arguments:
name: Unique name for the plot. If not defined a default plot name
is used.
name: Unique name for the plot. If not defined, it is considered as a
"default" plot
x: Channel name for the x-axis
y: Channel name for the y-axis
value: Channel name for the data value
......@@ -314,6 +314,70 @@ class ScanInfo(dict):
plots.append(plot)
def has_default_curve_plot(self) -> bool:
"""Returns true if a curve plot is already defined"""
plots = self._scan_info.get("plots", [])
for plot in plots:
if plot["kind"] == "curve-plot":
if plot.get("name") is None:
return True
return False
@typeguard.typechecked
def add_curve_plot(
self,
name: typing.Optional[str] = None,
x: typing.Optional[str] = None,
yleft: typing.Union[None, typing.List[str], str] = None,
yright: typing.Union[None, typing.List[str], str] = None,
):
"""
Add a curve plot definition to this `scan_info`.
It is an helper to simplify the creation of many curves.
This can replace the default plot for the scan.
Arguments:
name: Unique name for the plot. If not defined, it is considered as a
"default" plot
x: Channel name for the x-axis
yleft: Channel names of the curves which have to be displayed in the left y-axis
yright: Channel names of the curves which have to be displayed in the right y-axis
"""
plots = self._scan_info.setdefault("plots", [])
if not isinstance(plots, list):
raise TypeError("The 'plots' metadata is corrupted. A list is expected.")
items = []
if x is not None and yleft is None and yright is None:
item = {"kind": "curve", "x": x}
items.append(item)
if yleft is not None:
if isinstance(yleft, str):
yleft = [yleft]
for y in yleft:
item = {"kind": "curve", "y_axis": "left"}
if x is not None:
item["x"] = x
item["y"] = y
items.append(item)
if yright is not None:
if isinstance(yright, str):
yleft = [yright]
for y in yright:
item = {"kind": "curve", "y_axis": "right"}
if x is not None:
item["x"] = x
item["y"] = y
items.append(item)
plot = {"kind": "curve-plot", "items": items}
if name is not None:
plot["name"] = name
plots.append(plot)
def set_sequence_info(self, scan_count: typing.Optional[int] = None):
"""
Set extra-info for a sequence.
......
......@@ -79,3 +79,39 @@ def test_add_scatter_axis():
factory.add_scatter_plot(x="a", y="b")
expected = {"kind": "scatter", "x": "a", "y": "b"}
assert scan_info["plots"][0]["items"] == [expected]
def test_add_curve_plot():
scan_info = {}
factory = ScanInfoFactory(scan_info)
factory.add_curve_plot(x="a", yleft=["b"], yright=["c"], name="foo")
assert "plots" in scan_info
expected = {
"kind": "curve-plot",
"name": "foo",
"items": [
{"kind": "curve", "x": "a", "y": "b", "y_axis": "left"},
{"kind": "curve", "x": "a", "y": "c", "y_axis": "right"},
],
}
assert scan_info["plots"] == [expected]
def test_add_curve_axis():
"""It is valid to specify only part of the scatter item"""
scan_info = {}
factory = ScanInfoFactory(scan_info)
factory.add_curve_plot(x="a")
expected = {"kind": "curve", "x": "a"}
assert scan_info["plots"][0]["items"] == [expected]
def test_add_default_curve_plot():
scan_info = {}
factory = ScanInfoFactory(scan_info)
assert factory.has_default_curve_plot() is False
factory.add_scatter_plot(x="a", y="b")
factory.add_curve_plot(x="a", name="foo2")
assert factory.has_default_curve_plot() is False
factory.add_curve_plot(x="a", yleft=["b"], yright=["c"])
assert factory.has_default_curve_plot() is True
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment