Commit 01d775e5 authored by Valentin Valls's avatar Valentin Valls
Browse files

Exhaustive coverage of the custom plot API + fixes

parent c3523622
......@@ -100,7 +100,11 @@ class BasePlot(object):
def get_data_range(self):
widget = self.widget()
return widget.getDataRange()
if hasattr(widget, "getDataRange"):
return widget.getDataRange()
plot = widget.getPlotWidget()
if hasattr(plot, "getDataRange"):
return plot.getDataRange()
def _register(self, flint, plot_id, register):
"""Register everything needed remotely"""
......@@ -494,6 +498,9 @@ class Plot1D(BasePlot):
ydata = numpy.asarray(ydata)
self._flint.update_user_data(self._plot_id, unique_name, channel_name, ydata)
def add_curve(self, x, y, **kwargs):
self.submit("addCurve", x, y, **kwargs)
def set_xaxis_scale(self, value):
"""
Set the X-axis scale of this plot.
......@@ -526,7 +533,7 @@ class ScatterView(BasePlot):
ALIASES = ["scatter"]
# Name of the method to add data to the plot
METHOD = "addScatter"
METHOD = "setData"
# The dimension of the data to plot
DATA_DIMENSIONS = (1,)
......@@ -541,6 +548,15 @@ class ScatterView(BasePlot):
# Make it public
self.set_colormap = self._set_colormap
def clear_data(self):
self.submit("setData", None, None, None)
def set_data(self, x, y, value, **kwargs):
if x is None or y is None or value is None:
self.clear_data()
else:
self.submit("setData", x, y, value, **kwargs)
class Plot2D(BasePlot):
......@@ -571,6 +587,9 @@ class Plot2D(BasePlot):
self.submit("setKeepDataAspectRatio", True)
self._remote_plot().show_intensity_histogram(True)
def add_image(self, data, **kwargs):
self.submit("addImage", data, **kwargs)
def select_mask(self, initial_mask: numpy.ndarray = None, directory: str = None):
"""Request a mask image from user selection.
......@@ -691,7 +710,7 @@ class ImageView(BasePlot):
WIDGET = "silx.gui.plot.ImageView"
# Available name to identify this plot
ALIASES = ["imageview", "histogramimage"]
ALIASES = ["image", "imageview", "histogramimage"]
# Name of the method to add data to the plot
METHOD = "setImage"
......@@ -705,6 +724,13 @@ class ImageView(BasePlot):
# Data input number for a single representation
DATA_INPUT_NUMBER = 1
def _init(self):
# Make it public
self.set_colormap = self._set_colormap
def set_data(self, data, **kwargs):
self.submit("setImage", data, **kwargs)
class StackView(BasePlot):
......@@ -726,6 +752,13 @@ class StackView(BasePlot):
# Data input number for a single representation
DATA_INPUT_NUMBER = 1
def _init(self):
# Make it public
self.set_colormap = self._set_colormap
def set_data(self, data, **kwargs):
self.submit("setStack", data, **kwargs)
class LiveCurvePlot(Plot1D):
......
......@@ -955,10 +955,17 @@ class FlintApi:
Allows to setup the default colormap of a widget.
"""
widget = self._get_plot_widget(plot_id)
if not hasattr(widget, "defaultColormap"):
raise TypeError("Widget %s does not expose a colormap" % plot_id)
if hasattr(widget, "defaultColormap"):
colormap = widget.defaultColormap()
elif hasattr(widget, "getColormap"):
colormap = widget.getColormap()
else:
colormap - None
if colormap is None:
raise TypeError(
f"Widget {plot_id} ({type(widget)}) does not expose a colormap"
)
colormap = widget.defaultColormap()
if lut is not None:
colormap.setName(lut)
if vmin is not None:
......
......@@ -61,6 +61,14 @@ class CustomPlot(qt.QWidget):
layout.addWidget(plot)
self.__plot = plot
def defaultColormap(self):
plot = self.__plot
if plot is None:
return None
if hasattr(plot, "getColormap"):
return plot.getColormap()
return None
def _silxPlot(self):
return self.__plot
......
......@@ -218,9 +218,179 @@ def test_select_shapes__initial_selection(flint_session):
assert rois[3].name == roi_profile.name
def test_curve_stack(flint_session):
def test_plot1d(flint_session):
f = plot.get_flint()
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]
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]
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()
p = f.get_plot(plot_class="curvestack", name="curve-stack")
curves = numpy.empty((10, 100))
......
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