2022-07-30 21:50:53 +02:00
|
|
|
import pyqtgraph as pg
|
|
|
|
from pyqtgraph.Qt import QtCore, QtGui
|
|
|
|
import numpy as np
|
2022-08-10 16:40:15 +02:00
|
|
|
import os
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
from pyqtgraph.dockarea import *
|
|
|
|
from PyQt5.QtWidgets import *
|
|
|
|
from PyQt5.QtCore import *
|
2022-08-09 15:37:34 +02:00
|
|
|
from .WidgetDataSelector import WidgetDataSelector
|
|
|
|
from .WidgetAnalysePanel import WidgetAnalysePanel
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
|
2022-08-10 16:40:15 +02:00
|
|
|
def write_pid():
|
|
|
|
with open("living_plot.ini", 'w') as pid_file:
|
|
|
|
pid = os.getpid()
|
|
|
|
pid_file.write(str(pid))
|
|
|
|
|
|
|
|
return pid
|
|
|
|
|
|
|
|
|
|
|
|
def clean_pid():
|
|
|
|
with open("living_plot.ini", 'w') as pid_file:
|
|
|
|
pid_file.truncate(0)
|
|
|
|
|
|
|
|
|
2022-07-30 21:50:53 +02:00
|
|
|
class MainPlotPanel(QtGui.QMainWindow):
|
|
|
|
|
|
|
|
def __init__(self, h5_paths, n_rows=3, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
2022-08-10 16:40:15 +02:00
|
|
|
write_pid()
|
|
|
|
|
|
|
|
self.h5_paths = np.array(h5_paths)
|
|
|
|
|
2022-07-30 21:50:53 +02:00
|
|
|
pg.mkQApp()
|
|
|
|
|
|
|
|
self.n_rows = n_rows
|
|
|
|
|
|
|
|
self.setWindowFlag(QtCore.Qt.WindowCloseButtonHint, False)
|
|
|
|
self.area = DockArea()
|
|
|
|
|
|
|
|
self.setCentralWidget(self.area)
|
2022-08-09 15:37:34 +02:00
|
|
|
self.resize(900, 500)
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
self.dshotselector = Dock("Shot selector")
|
2022-08-09 15:37:34 +02:00
|
|
|
self.shotselector = WidgetDataSelector()
|
|
|
|
self.dshotselector.setFixedSize(400, 150)
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
self.dshotselector.addWidget(self.shotselector)
|
2022-08-09 15:37:34 +02:00
|
|
|
self.area.addDock(self.dshotselector, 'left')
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
# self.qpg_dock = Dock("Quick Plot Generator")
|
|
|
|
# self.qpg_dock.addWidget(QuickPlotGenerator(self))
|
|
|
|
# self.qpg_dock.setMinimumSize(self.qpg_dock.minimumSizeHint())
|
|
|
|
# self.area.addDock(self.qpg_dock)
|
|
|
|
|
2022-08-09 15:37:34 +02:00
|
|
|
self.danalyse_panel = Dock("Analyse and Fitting")
|
|
|
|
self.analyse_panel = WidgetAnalysePanel()
|
|
|
|
self.danalyse_panel.addWidget(self.analyse_panel)
|
|
|
|
self.area.addDock(self.danalyse_panel, 'bottom')
|
|
|
|
|
2022-08-09 17:48:22 +02:00
|
|
|
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
|
|
|
|
self.showMaximized()
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
self.plots = {}
|
|
|
|
|
|
|
|
self.shotselector.valueChanged.connect(self.refresh)
|
|
|
|
self.shotselector.selectionChanged.connect(self.refresh)
|
|
|
|
|
2022-08-10 16:40:15 +02:00
|
|
|
def closeEvent(self, event):
|
|
|
|
clean_pid()
|
|
|
|
event.accept()
|
2022-07-30 21:50:53 +02:00
|
|
|
|
2022-08-09 15:37:34 +02:00
|
|
|
def add_plot_dock(self, plot_name, plot_widget, overlap=True, **kwargs):
|
2022-07-30 21:50:53 +02:00
|
|
|
if plot_name not in self.plots:
|
|
|
|
plot_widget.plot_name = plot_name
|
|
|
|
|
|
|
|
dock = Dock(plot_name, **kwargs)
|
|
|
|
|
|
|
|
dock.sigClosed.connect(self.remove_plot)
|
|
|
|
dock.addWidget(plot_widget)
|
2022-08-09 15:37:34 +02:00
|
|
|
if overlap and len(self.plots):
|
|
|
|
self.area.addDock(dock, 'above', relativeTo=self.area.docks[list(self.plots.keys())[-1]])
|
|
|
|
elif len(self.plots):
|
2022-07-30 21:50:53 +02:00
|
|
|
if len(self.plots) % self.n_rows == 2:
|
|
|
|
position = 'right'
|
|
|
|
self.area.addDock(dock, position)
|
|
|
|
if len(self.plots) % (self.n_rows * 2) in [0, 1]:
|
|
|
|
position = 'bottom'
|
|
|
|
self.area.addDock(dock, position, relativeTo=self.area.docks[list(self.plots.keys())[-1]])
|
|
|
|
if len(self.plots) % (self.n_rows * 2) in [3, 4]:
|
|
|
|
position = 'top'
|
|
|
|
self.area.addDock(dock, position, relativeTo=self.area.docks[list(self.plots.keys())[-1]])
|
|
|
|
print(len(self.plots) % (self.n_rows * 2), len(self.plots) % self.n_rows)
|
|
|
|
else:
|
|
|
|
self.area.addDock(dock, 'right')
|
|
|
|
|
|
|
|
self.plots[plot_name] = plot_widget
|
|
|
|
else:
|
|
|
|
print(f'Plot {plot_name} already exists. Please choose different name.')
|
|
|
|
|
|
|
|
def remove_plot(self, dock):
|
|
|
|
del self.plots[dock.title()]
|
|
|
|
|
|
|
|
def update_h5_paths(self, h5_paths):
|
2022-08-10 16:40:15 +02:00
|
|
|
self.h5_paths = np.array(h5_paths)
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
self.shotselector.update_nshots(len(h5_paths))
|
|
|
|
|
|
|
|
for plot_name in self.plots:
|
|
|
|
self.plots[plot_name].data_extractor_manager.clean_memory(h5_paths)
|
|
|
|
|
2022-08-10 16:40:15 +02:00
|
|
|
def add_h5_path(self, h5_path):
|
|
|
|
self.h5_paths = np.append(self.h5_paths, h5_path)
|
|
|
|
|
|
|
|
self.shotselector.update_nshots(len(self.h5_paths))
|
|
|
|
|
2022-07-30 21:50:53 +02:00
|
|
|
def refresh(self, h5_path=None):
|
|
|
|
if len(self.h5_paths):
|
|
|
|
|
2022-08-10 16:40:15 +02:00
|
|
|
self.h5_paths_selected = self.h5_paths[np.array(self.shotselector.get_selected_indices())]
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
if h5_path == None:
|
|
|
|
i = self.shotselector.get_current_index()
|
2022-08-10 16:40:15 +02:00
|
|
|
h5_path = self.h5_paths[np.array(i)]
|
2022-07-30 21:50:53 +02:00
|
|
|
|
|
|
|
for plot_name in self.plots:
|
|
|
|
self.plots[plot_name].data_extractor_manager.update_local_data(h5_path)
|
|
|
|
|
|
|
|
for plot_name, plot in self.plots.items():
|
|
|
|
plot.update_from_h5(h5_path)
|
|
|
|
else:
|
|
|
|
pass
|