2 Commits

Author SHA1 Message Date
72c51c5744 dont try to stop the RST 2023-03-18 16:55:25 +01:00
1e203873c9 disable RPC thread where not needed 2023-03-18 16:44:01 +01:00
16 changed files with 48 additions and 227 deletions

View File

@@ -6,7 +6,7 @@ stages:
- pip install pytest pytest-random-order pytest-cov
- pip install -e ./
- apt-get update
- apt-get install -y ffmpeg libnss3 libxcomposite1 libxtst6 libxdamage1
- apt-get install -y ffmpeg libnss3 libxcomposite1 libxtst6
tests:
stage: Tests
@@ -24,10 +24,8 @@ tests:
artifacts:
when: always
reports:
junit: report.xml
coverage_report:
coverage_format: cobertura
path: coverage.xml
cobertura: coverage.xml
junit: report-junit.xml
tests-3.6:
stage: OptionalTests

View File

@@ -1,6 +1,6 @@
# grum
<img src="https://gitea.psi.ch/SwissFEL/grum/wiki/raw/uploads%2Fe4cd2be847d26bb7ac7100080edbccce%2Fscreenshot.png" width="50%" />
<img src="https://gitlab.psi.ch/augustin_s/grum/-/wikis/uploads/1a259a1d74e7b79e0230e7bbad3b1284/screenshot2.png" width="50%" />
## Overview
@@ -22,11 +22,11 @@ Via the RPC server, new plots can be created and new data appended to existing p
- `new_plot(name, cfg)`
Creates a new plot named `name` in the grum list. The configuration dict `cfg` is used as arguments for the constructor of [`PlotDescription`](https://gitea.psi.ch/SwissFEL/grum/src/grum/descs/plotdesc.py#L4).
Creates a new plot named `name` in the grum list. The configuration dict `cfg` is used as arguments for the constructor of [`PlotDescription`](https://gitlab.psi.ch/augustin_s/grum/-/blob/master/grum/plotdesc.py#L4).
- `append_data(name, point)`
Append data point to the plot named `name`. The new `point` is forwarded to [`PlotDescription.append()`](https://gitea.psi.ch/SwissFEL/grum/src/grum/descs/plotdesc.py#L24).
Append data point to the plot named `name`. The new `point` is forwarded to [`PlotDescription.append()`](https://gitlab.psi.ch/augustin_s/grum/-/blob/master/grum/plotdesc.py#L18).
### Utility functions

View File

@@ -1,13 +0,0 @@
from .desc import Description
from .imgdesc import ImageDescription
from .plotdesc import PlotDescription
DESC_TYPES = {
ImageDescription.get_type(): ImageDescription,
PlotDescription.get_type(): PlotDescription
}

View File

@@ -1,23 +0,0 @@
class Description:
def to_dict(self):
res = {k: v for k, v in self.__dict__.items() if not k.startswith("_") and k != "name" and v is not None}
tn = self.get_type()
res.setdefault("type", tn)
return res
@classmethod
def get_type(cls):
tn = cls.__name__
suffix = "Description"
if not tn.endswith(suffix):
raise ValueError(f'"{tn}" does not end with "{suffix}"')
tn = tn[:-len(suffix)]
tn = tn.casefold()
tn = tn or None
return tn

View File

@@ -1,52 +0,0 @@
import numpy as np
import pyqtgraph as pg
from .desc import Description
class ImageDescription(Description):
def __init__(self, name, title=None, xlabel=None, ylabel=None, image=None, levels=None, cmap="viridis"):
self.name = name
self.title = title
self.xlabel = xlabel
self.ylabel = ylabel
self.image = image
self.levels = levels #TODO: might be better to use vmin and vmax
self.cmap = cmap
@property
def data(self):
return np.asarray(self.image)
@data.setter
def data(self, value):
self.image = value
def append(self, xy):
print("ignored image append")
def extend(self, data):
print("ignored image extend")
def make_plot(self, plotwidget, style):
res = plotwidget.setImage(self.data, levels=self.levels)
if self.title:
plotwidget.setTitle(self.title)
if self.xlabel:
plotwidget.getView().setLabel("bottom", self.xlabel)
if self.ylabel:
plotwidget.getView().setLabel("left", self.ylabel)
if self.cmap:
cm = pg.colormap.get(self.cmap)
plotwidget.setColorMap(cm)
return res

View File

@@ -1,6 +1,6 @@
import numpy as np
from .descs import PlotDescription, ImageDescription
from .plotdesc import PlotDescription
X = np.arange(100) / 10
@@ -31,20 +31,4 @@ for name, (xs, ys) in exampledata_raw.items():
)
name = "image"
xdim = ydim = 100
size = xdim * ydim
shape = (xdim, ydim)
img = np.arange(size).reshape(shape) / size
img += np.random.random(shape) / 10
exampledata[name] = ImageDescription(
name,
image=img,
xlabel="x",
ylabel="y"
)

View File

@@ -2,27 +2,21 @@ from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtWidgets import QMainWindow, QSplitter
from . import assets
from .descs import DESC_TYPES, Description, ImageDescription, PlotDescription
from .dictlist import DictList
from .exampledata import exampledata
from .h5filedlg import open_h5_files_dialog, save_h5_file_dialog
from .io import write_dict, read_dict
from .mdi import MDIArea, MDISubMultiPlot, MDISubPlot, MDISubImage, MDIWindowMode
from .mdi import MDIArea, MDISubMultiPlot, MDISubPlot, MDIWindowMode
from .menus import BarMenu
from .plotdesc import PlotDescription
from .rpc import RPCServerThread
from .shortcut import shortcut
from .webview import WebView
DESC_TYPE_TO_MDI_SUB_TYPE = {
ImageDescription: MDISubImage,
PlotDescription: MDISubPlot
}
class MainWindow(QMainWindow):
sig_make_new_subwin = pyqtSignal(str, Description)
sig_make_new_plot = pyqtSignal(str, PlotDescription)
def __init__(self, *args, title="grum", host="localhost", port=8000, offline=False, add_examples=False, window_mode=MDIWindowMode.MULTI, **kwargs):
super().__init__(*args, **kwargs)
@@ -90,13 +84,12 @@ class MainWindow(QMainWindow):
if not offline:
self.rst = rst = RPCServerThread(host, port, doc_title_suffix=title)
rst.start()
rst.server.register_function(self.new_image)
rst.server.register_function(self.new_plot)
rst.server.register_function(self.append_data)
rst.server.register_function(self.extend_data)
rst.server.register_function(self.set_data)
self.sig_make_new_subwin.connect(self.on_make_new_subwin)
self.sig_make_new_plot.connect(self.on_make_new_plot)
def keyPressEvent(self, event):
@@ -106,30 +99,16 @@ class MainWindow(QMainWindow):
# Remote API calls
def new_image(self, name, cfg):
"""
Create a new image <name> using the configuration dict <cfg>.
The configuration is forwarded to the constructor of ImageDescription.
Allowed keys are: title, xlabel, ylabel, image, levels, cmap.
"""
desc = self.add_new_desc_to_list(ImageDescription, name, cfg)
if self.menu_settings.checkboxes["Open new plots"].isChecked():
sub = self.mdi.findSubWindow(name)
if sub:
sub.pw.setImage(desc.data) #TODO lacks the list sync
else:
self.sig_make_new_subwin.emit(name, desc)
def new_plot(self, name, cfg):
"""
Create a new plot <name> using the configuration dict <cfg>.
The configuration is forwarded to the constructor of PlotDescription.
Allowed keys are: title, xlabel, ylabel, xs, ys.
"""
desc = self.add_new_desc_to_list(PlotDescription, name, cfg)
desc = self.add_new_desc_to_list(name, cfg)
if self.menu_settings.checkboxes["Open new plots"].isChecked():
if not self.mdi.findSubWindow(name):
self.sig_make_new_subwin.emit(name, desc)
self.sig_make_new_plot.emit(name, desc)
def append_data(self, name, point):
"""
@@ -164,10 +143,8 @@ class MainWindow(QMainWindow):
# Signal callbacks
def on_make_new_subwin(self, name, desc):
DescType = type(desc)
MDISubType = DESC_TYPE_TO_MDI_SUB_TYPE[DescType]
self.make_subwin(MDISubType, name, desc)
def on_make_new_plot(self, *args, **kwargs):
self.make_subwin(MDISubPlot, *args, **kwargs)
def on_dclick_list_item(self, item):
self.plot_single_item(item)
@@ -210,10 +187,8 @@ class MainWindow(QMainWindow):
for fn in fns:
data = read_dict(fn)
for name, cfg in data.items():
tn = cfg.pop("type")
DescType = DESC_TYPES[tn]
self.add_new_desc_to_list(DescType, name, cfg)
for k, v in data.items():
self.add_new_desc_to_list(k, v)
def on_file_save(self):
@@ -231,8 +206,8 @@ class MainWindow(QMainWindow):
# Plumbing
def add_new_desc_to_list(self, DescType, name, cfg):
desc = DescType(name, **cfg)
def add_new_desc_to_list(self, name, cfg):
desc = PlotDescription(name, **cfg)
self.lst.set(name, desc)
return desc
@@ -251,22 +226,19 @@ class MainWindow(QMainWindow):
item.timestamps.access.update()
item.set_alarm(False)
name, desc = item.key, item.value
DescType = type(desc)
MDISubType = DESC_TYPE_TO_MDI_SUB_TYPE[DescType]
self.activate_or_make_subwin(MDISubType, name, desc)
self.activate_or_make_subwin(MDISubPlot, name, desc)
def plot_multiple_items(self, items):
for i in items:
i.timestamps.access.update()
i.set_alarm(False)
items = (i for i in items if isinstance(i.value, PlotDescription)) #TODO: for now, only overlay plots
descs = {i.key: i.value for i in items}
names = descs.keys()
name = " | ".join(names)
self.activate_or_make_subwin(MDISubMultiPlot, name, descs)
#TODO: the following two could be methods of MDIArea?
#TODO: the following two could be methods to MDIArea?
def activate_or_make_subwin(self, MDISubType, name, *args, **kwargs):
sub = self.mdi.findSubWindow(name)

View File

@@ -1,6 +1,5 @@
from .mdiarea import MDIArea, MDIWindowMode
from .mdisubplot import MDISubPlot, MDISubMultiPlot
from .mdisubimg import MDISubImage

View File

@@ -1,38 +0,0 @@
import pyqtgraph as pg
from .mdisubwin import MDISubWindow
from ..theme import pg_plot_style
class MDISubImage(MDISubWindow):
def __init__(self, name, desc, *args, **kwargs):
super().__init__(name, *args, **kwargs)
self.pw = pw = pg.ImageView(view=pg.PlotItem()) # for axis ticks and labels, view needs to be a PlotItem
self.setWidget(pw)
# connect to plot mouse-over event
pw.scene.sigMouseMoved.connect(self.on_hover)
style = pg_plot_style()
plot = desc.make_plot(self.pw, style)
self.plots = {name: plot}
self.image = desc.data
def on_hover(self, event):
coord = self.pw.imageItem.mapFromScene(event)
x = coord.x()
y = coord.y()
x = int(x)
y = int(y)
try:
z = self.image[x, y]
except IndexError:
return
z = round(z, 3)
self.setToolTip(f"x = {x}\ny = {y}\nz = {z}")

View File

@@ -4,17 +4,12 @@ from PyQt5.QtWidgets import QMdiSubWindow
from .. import assets
SUB_WIN_WIDTH = 640
SUB_WIN_HEIGHT = 480
class MDISubWindow(QMdiSubWindow):
def __init__(self, title, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle(title)
self.setWindowIcon(assets.char())
self.resize(SUB_WIN_WIDTH, SUB_WIN_HEIGHT)
# without this, the SubWindow is not removed from the subWindowList
self.setAttribute(Qt.WA_DeleteOnClose)

View File

@@ -1,7 +1,5 @@
from .desc import Description
class PlotDescription(Description):
class PlotDescription:
def __init__(self, name, title=None, xlabel=None, ylabel=None, xs=None, ys=None):
self.name = name
@@ -47,4 +45,8 @@ class PlotDescription(Description):
return res
def to_dict(self):
return {k: v for k, v in self.__dict__.items() if not k.startswith("_") and k != "name" and v is not None}

View File

@@ -2,7 +2,7 @@ from PyQt5.QtCore import QUrl
try:
from PyQt5.QtWebKitWidgets import QWebView
except (ImportError, ModuleNotFoundError):
except ImportError:
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView

View File

@@ -4,9 +4,9 @@ version = 0.0.1
description = GUI for Remote Unified Monitoring
long_description = file: README.md
long_description_content_type = text/markdown
url = https://gitea.psi.ch/SwissFEL/grum
url = https://gitlab.psi.ch/augustin_s/grum
project_urls =
Bug Tracker = https://gitea.psi.ch/SwissFEL/grum/issues
Bug Tracker = https://gitlab.psi.ch/augustin_s/grum/issues
classifiers =
Programming Language :: Python :: 3
License :: OSI Approved :: MIT License

View File

@@ -14,7 +14,7 @@ from grum.mainwin import MainWindow
from grum.mdi import MDIArea, MDISubMultiPlot, MDISubPlot
from grum.menus import BarMenu
from grum.menus.rclickmenu import RClickMenu
from grum.descs import Description, PlotDescription
from grum.plotdesc import PlotDescription
from grum.rpc import RPCServerThread
@@ -47,7 +47,7 @@ class TestMainWin:
for key in mw.lst.lst.items:
assert isinstance(mw.lst.lst.get(key), DictListItem)
assert isinstance(mw.lst.lst.get(key).value, Description)
assert isinstance(mw.lst.lst.get(key).value, PlotDescription)
assert isinstance(mw.lst.menu, RClickMenu)
assert isinstance(mw.menu_settings, BarMenu)
@@ -65,7 +65,7 @@ class TestMainWin:
xlabel = "xlabel"
ylabel = "ylabel"
cfg = {"title": title, "xlabel": xlabel, "ylabel": ylabel}
spy_sig_make_new_subwin = QSignalSpy(mw.sig_make_new_subwin)
spy_sig_make_new_plot = QSignalSpy(mw.sig_make_new_plot)
mw.new_plot(name, cfg=cfg)
@@ -76,28 +76,28 @@ class TestMainWin:
assert mw.lst.lst.get(name).value.ylabel == ylabel
assert mw.menu_settings.checkboxes["Open new plots"].isChecked()
assert len(spy_sig_make_new_subwin) == 1 # assert called once
assert spy_sig_make_new_subwin[0][0] == name # assert called with name
assert isinstance(spy_sig_make_new_subwin[0][1], PlotDescription)
assert len(spy_sig_make_new_plot) == 1 # assert called once
assert spy_sig_make_new_plot[0][0] == name # assert called with name
assert isinstance(spy_sig_make_new_plot[0][1], PlotDescription)
mw.menu_settings.checkboxes["Open new plots"].setChecked(False)
assert mw.menu_settings.checkboxes["Open new plots"].isChecked() == False
spy_sig_make_new_subwin = QSignalSpy(mw.sig_make_new_subwin)
spy_sig_make_new_plot = QSignalSpy(mw.sig_make_new_plot)
mw.new_plot("new_name", cfg)
assert len(spy_sig_make_new_subwin) == 0 # assert not called
assert len(spy_sig_make_new_plot) == 0 # assert not called
mw.menu_settings.checkboxes["Open new plots"].setChecked(True)
assert mw.menu_settings.checkboxes["Open new plots"].isChecked() == True
spy_sig_make_new_subwin = QSignalSpy(mw.sig_make_new_subwin)
spy_sig_make_new_plot = QSignalSpy(mw.sig_make_new_plot)
new_name_item = mw.lst.lst.get("new_name")
sub = MDISubPlot("new_name", new_name_item.value)
mw.mdi.add(sub)
mw.new_plot("new_name", cfg)
assert len(spy_sig_make_new_subwin) == 0 # assert not called
assert len(spy_sig_make_new_plot) == 0 # assert not called
def test_append_data(self):
@@ -122,19 +122,17 @@ class TestMainWin:
assert sine_item.set_alarm.call_args[0][0] == False
def test_on_make_new_subwin(self):
def test_on_make_new_plot(self):
mw = self.mw
mw.make_subwin = mock.MagicMock()
name = "test"
cfg = {"title": "title"}
args = (1, 2, "name")
kwargs = {"title": "plot_title"}
desc = PlotDescription(name, *cfg)
mw.on_make_new_plot(args, kwargs)
mw.on_make_new_subwin(name, desc)
mw.make_subwin.assert_called_once_with(MDISubPlot, name, desc)
mw.make_subwin.assert_called_once_with(MDISubPlot, args, kwargs)
def test_on_dclick_list_item(self):

View File

@@ -15,13 +15,13 @@ class TestMDIArea:
def setup_method(self):
print("setup")
self.app = QApplication(sys.argv)
self.mw = MainWindow(add_examples=True)
self.mw = MainWindow(add_examples=True, offline=True)
self.mw.show()
def teardown_method(self):
print("teardown")
self.mw.rst.wait_for_stop()
# self.mw.rst.wait_for_stop()
self.app.quit()
print("app quit called")
del self.mw

View File

@@ -6,7 +6,7 @@ import pyqtgraph as pg
from grum import theme
from grum.mainwin import MainWindow
from grum.mdi.mdisubplot import MDISubPlot
from grum.descs import PlotDescription
from grum.plotdesc import PlotDescription
from grum.theme import pg_plot_style
@@ -53,7 +53,7 @@ def test_make_plot():
)
app = QApplication(sys.argv)
theme.apply(app)
mw = MainWindow(add_examples=True)
mw = MainWindow(add_examples=True, offline=True)
mdi_sub = MDISubPlot("mdi", pd)
pw = pg.PlotWidget()
@@ -78,7 +78,6 @@ def test_to_dict():
"xs": [1, 2],
"ylabel": "plot_ylabel",
"ys": [3, 4],
"type": "plot"
}