From 93d3b1e05716585f3f939a1bf2c6129ba9e1c839 Mon Sep 17 00:00:00 2001 From: Alice Date: Wed, 14 Oct 2026 18:32:49 +0200 Subject: [PATCH] added color bar levels and spin boxes for each counter --- pyctbgui/pyctbgui/services/Plot.py | 121 ++++--- pyctbgui/pyctbgui/services/Transceiver.py | 20 +- pyctbgui/pyctbgui/ui/plot.ui | 420 ++++++++++++---------- pyctbgui/pyctbgui/utils/defines.py | 8 +- 4 files changed, 320 insertions(+), 249 deletions(-) diff --git a/pyctbgui/pyctbgui/services/Plot.py b/pyctbgui/pyctbgui/services/Plot.py index de455720a..6132e2896 100644 --- a/pyctbgui/pyctbgui/services/Plot.py +++ b/pyctbgui/pyctbgui/services/Plot.py @@ -21,8 +21,6 @@ class PlotTab(QtWidgets.QWidget): def __init__(self, parent): super().__init__(parent) pg.setConfigOptions(imageAxisOrder="row-major") - self.frame_min: float = 0.0 - self.frame_max: float = 0.0 uic.loadUi(Path(__file__).parent.parent / 'ui' / "plot.ui", parent) self.view = parent self.mainWindow = None @@ -31,11 +29,13 @@ class PlotTab(QtWidgets.QWidget): self.transceiverTab = None self.acquisitionTab = None self.adcTab = None - self.cmin: float = 0.0 - self.cmax: float = 0.0 + self.cmin: list[float] = Defines.max_counters * [0.0] + self.cmax: list[float] = Defines.max_counters * [0.0] self.colorRangeMode: Defines.colorRange = Defines.colorRange.all self.ignoreHistogramSignal: bool = False self.imagePlots: list[PlotWidget] = [] + self.cminSpinBoxes : list[QtWidgets.QSpinBox] = None + self.cmaxSpinBoxes : list[QtWidgets.QSpinBox] = None # list of callback functions to notify tabs when we should hide their legend # follows the observer design pattern self.hideLegendObservers = [] @@ -59,6 +59,9 @@ class PlotTab(QtWidgets.QWidget): checkBox.setChecked(False) checkBox.setEnabled(False) + self.cminSpinBoxes = [self.view.cminSpinBox0, self.view.cminSpinBox1, self.view.cminSpinBox2, self.view.cminSpinBox3] + self.cmaxSpinBoxes = [self.view.cmaxSpinBox0, self.view.cmaxSpinBox1, self.view.cmaxSpinBox2, self.view.cmaxSpinBox3] + self.imagePlots = ( self.mainWindow.plotAnalogImage, self.mainWindow.plotDigitalImage, @@ -95,12 +98,16 @@ class PlotTab(QtWidgets.QWidget): self.view.spinBoxPlot.editingFinished.connect(self.setPlotBit) self.view.pushButtonReferesh.clicked.connect(self.acquisitionTab.refresh) # color range widgets - self.view.cminSpinBox.editingFinished.connect(self.setCmin) - self.view.cmaxSpinBox.editingFinished.connect(self.setCmax) self.view.radioButtonAutomatic.clicked.connect(partial(self.setColorRangeMode, Defines.colorRange.all)) self.view.radioButtonFixed.clicked.connect(partial(self.setColorRangeMode, Defines.colorRange.fixed)) self.view.radioButtonCenter.clicked.connect(partial(self.setColorRangeMode, Defines.colorRange.center)) + for index, cminSpinBox in enumerate(self.cminSpinBoxes): + cminSpinBox.editingFinished.connect(partial(self.setCmin, index=index)) + + for index, cmaxSpinBox in enumerate(self.cmaxSpinBoxes): + cmaxSpinBox.editingFinished.connect(partial(self.setCmax, index=index)) + for index, checkBox in enumerate(self.checkBoxCounters): checkBox.stateChanged.connect(partial(self.displayCounter, index)) @@ -111,6 +118,7 @@ class PlotTab(QtWidgets.QWidget): plot = self.mainWindow.plotAnalogImage plot.scene.sigMouseMoved.connect(partial(self.showPlotValues, plot, nMaxX, nMaxY, frame)) plot.getHistogramWidget().item.sigLevelChangeFinished.connect(partial(self.handleHistogramChange, plot)) + plot.getImageItem().sigImageChanged.connect(partial(self.updateColorRange, plot=plot)) # show image Values for digital image nMaxY = lambda : self.mainWindow.nDigitalRows @@ -119,6 +127,7 @@ class PlotTab(QtWidgets.QWidget): plot = self.mainWindow.plotDigitalImage plot.scene.sigMouseMoved.connect(partial(self.showPlotValues, plot, nMaxX, nMaxY, frame)) plot.getHistogramWidget().item.sigLevelChangeFinished.connect(partial(self.handleHistogramChange, plot)) + plot.getImageItem().sigImageChanged.connect(partial(self.updateColorRange, plot=plot)) # show image Values for transceiver image nMaxY = lambda : self.transceiverTab.nTransceiverRows @@ -127,7 +136,8 @@ class PlotTab(QtWidgets.QWidget): for index, image_view in enumerate(self.transceiverTab.transceiverImageViews): frame = lambda image_view=image_view: image_view.getImageItem().image image_view.scene.sigMouseMoved.connect(partial(self.showPlotValues, image_view, nMaxX, nMaxY, frame)) - image_view.getHistogramWidget().item.sigLevelChangeFinished.connect(partial(self.handleHistogramChange, image_view)) + image_view.getHistogramWidget().item.sigLevelChangeFinished.connect(partial(self.handleHistogramChange, image_view, index)) + image_view.getImageItem().sigImageChanged.connect(partial(self.updateColorRange, plot=image_view)) self.view.checkBoxShowLegend.stateChanged.connect(self.toggleLegend) @@ -231,27 +241,33 @@ class PlotTab(QtWidgets.QWidget): for notify_function in self.hideLegendObservers: notify_function() - def setCmin(self, value=None): + def setCmin(self, value=None, index = 0, updateColorBar=True): """ slot for setting cmin from cminSpinBox also used as a normal function """ if value is None: - self.cmin = self.view.cminSpinBox.value() + self.cmin[index] = self.cminSpinBoxes[index].value() else: - self.cmin = value - self.updateColorRangeUI() + self.cmin[index] = value + self.cminSpinBoxes[index].setValue(self.cmin[index]) + + if updateColorBar: + self.updateColorBar(index=index) - def setCmax(self, value=None): + def setCmax(self, value=None, index = 0, updateColorBar=True): """ slot for setting cmax from cmaxSpinBox also used as a normal function """ if value is None: - self.cmax = self.view.cmaxSpinBox.value() + self.cmax[index] = self.cmaxSpinBoxes[index].value() else: - self.cmax = value - self.updateColorRangeUI() + self.cmax[index] = value + self.cmaxSpinBoxes[index].setValue(self.cmax[index]) + + if updateColorBar: + self.updateColorBar(index=index) def setColorRangeMode(self, mode): """ @@ -261,36 +277,42 @@ class PlotTab(QtWidgets.QWidget): # disable or enable cmin/cmax spinboxes enableSpinBoxes = mode == Defines.colorRange.fixed - self.view.cminSpinBox.setEnabled(enableSpinBoxes) - self.view.cmaxSpinBox.setEnabled(enableSpinBoxes) - self.updateColorRange() - self.updateColorRangeUI() + for cminSpinBox, cmaxSpinBox in zip(self.cminSpinBoxes, self.cmaxSpinBoxes): + cminSpinBox.setEnabled(enableSpinBoxes) + cmaxSpinBox.setEnabled(enableSpinBoxes) + + for index in range(Defines.max_counters): # TODO does it work with other detectors? + self.updateColorRange(index=index) - def handleHistogramChange(self, plot): + def handleHistogramChange(self, plot, index=0): """ slot called after changing the color bar This is called even when pyqtgraph sets the image without any user intervention the class attribute ignore_histogram_signal is set to False before setting the image so that we can distinguish between the signal originates from pyqt or from the user - """ - if not self.ignoreHistogramSignal: - self.cmin, self.cmax = plot.getHistogramWidget().item.getLevels() - self.setCmin(self.cmin) - self.setCmax(self.cmax) + Parameters + ---------- + plot : Plot + The plot object whose histogram has changed. + index : int, optional + The index of the counter (default is 0). + """ + + print("handleHistogramChange called with index: ", index, " ignoreHistogramSignal: ", self.ignoreHistogramSignal) + + print("color range mode is: ", self.colorRangeMode) + if not self.ignoreHistogramSignal: + cmin, cmax = plot.getHistogramWidget().item.getLevels() + print("cmin, cmax for index: ", index, cmin, cmax) + self.setCmin(cmin, index=index, updateColorBar=True) + self.setCmax(cmax, index=index, updateColorBar=True) + self.ignoreHistogramSignal = False + # self.setColorRangeMode(Defines.colorRange.fixed) - def setFrameLimits(self, frame): - """ - function called from AcquisitionTab::read_zmq to get the maximum and minimum - values of the decoded frame and save them in frame_min/frame_max - """ - self.frame_min = np.min(frame) - self.frame_max = np.max(frame) - self.updateColorRange() - - def updateColorRange(self): + def updateColorRange(self, plot = None, index=0): """ for mode: - all: sets cmin and cmax to the maximums/minimum values of the frame @@ -299,26 +321,25 @@ class PlotTab(QtWidgets.QWidget): """ if self.colorRangeMode == Defines.colorRange.all: - self.cmin = self.frame_min - self.cmax = self.frame_max + frame_min = self.cmin[index] if plot is None else np.min(plot.getImageItem().image) + frame_max = self.cmax[index] if plot is None else np.max(plot.getImageItem().image) + self.setCmin(frame_min, index=index) # TODO: need multiple frame min and frame max - from where? + self.setCmax(frame_max, index=index) elif self.colorRangeMode == Defines.colorRange.center: - self.cmin = self.frame_min + 0.03 * (self.frame_max - self.frame_min) - self.cmax = self.frame_max - 0.03 * (self.frame_max - self.frame_min) + frame_min = self.cmin[index] if plot is None else np.min(plot.getImageItem().image) + frame_max = self.cmax[index] if plot is None else np.max(plot.getImageItem().image) + self.setCmin(frame_min + 0.03 * (frame_max - frame_min), index=index) + self.setCmax(frame_max - 0.03 * (frame_max - frame_min), index=index) - self.updateColorRangeUI() - - def updateColorRangeUI(self): + def updateColorBar(self, index = 0): """ - updates UI views should be called after every change to cmin or cmax + updates the color bar of the plot at index """ for plot in self.imagePlots: if isinstance(plot, list): # hacky for now only transceiver image is a PlotSplitter - for p in plot: - p.getHistogramWidget().item.setLevels(min=self.cmin, max=self.cmax) + plot[index].getHistogramWidget().item.setLevels(min=self.cmin[index], max=self.cmax[index]) else: - plot.getHistogramWidget().item.setLevels(min=self.cmin, max=self.cmax) - self.view.cminSpinBox.setValue(self.cmin) - self.view.cmaxSpinBox.setValue(self.cmax) + plot.getHistogramWidget().item.setLevels(min=self.cmin[index], max=self.cmax[index]) def setColorMap(self): cm = pg.colormap.getFromMatplotlib(self.view.comboBoxColorMap.currentText()) @@ -399,11 +420,11 @@ class PlotTab(QtWidgets.QWidget): self.mainWindow.transceiverPlots[i].hide() def addAllSelectedTransceiverPlots(self): - for i in range(Defines.transceiver.maxcount): + for i in range(Defines.max_counters): self.addSelectedTransceiverPlots(i) def removeAllTransceiverPlots(self): - for i in range(Defines.transceiver.maxcount): + for i in range(Defines.max_counters): self.mainWindow.transceiverPlots[i].hide() def showPlot(self): diff --git a/pyctbgui/pyctbgui/services/Transceiver.py b/pyctbgui/pyctbgui/services/Transceiver.py index 828decd26..8cd817cba 100644 --- a/pyctbgui/pyctbgui/services/Transceiver.py +++ b/pyctbgui/pyctbgui/services/Transceiver.py @@ -24,18 +24,18 @@ class TransceiverTab(QtWidgets.QWidget): self.plotTab = None self.legend: LegendItem | None = None self.acquisitionTab = None - self.nCounters: int = Defines.transceiver.maxcount + self.nCounters: int = Defines.max_counters self.nTransceiverRows : int = 0 self.nTransceiverCols : int = 0 - self.shownCounters: list[bool] = [True] * Defines.transceiver.maxcount # per default show all 4 counters + self.shownCounters: list[bool] = [True] * Defines.max_counters # per default show all 4 counters self.transceiverImageSplitter = QtWidgets.QSplitter(QtCore.Qt.Vertical) self.transceiverImageViews : list = [] # pg image view for each counter - self.firstTransceiverImage : list[bool] = [True] * Defines.transceiver.maxcount # to keep track of first image for each counter to maintain zoom state + self.firstTransceiverImage : list[bool] = [True] * Defines.max_counters # to keep track of first image for each counter to maintain zoom state def setup_ui(self): self.plotTab = self.mainWindow.plotTab self.acquisitionTab = self.mainWindow.acquisitionTab - for i in range(Defines.transceiver.maxcount): + for i in range(Defines.max_counters): self.setTransceiverButtonColor(i, self.plotTab.getRandomColor()) self.initializeAllTransceiverPlots() @@ -46,7 +46,7 @@ class TransceiverTab(QtWidgets.QWidget): self.plotTab.subscribeToggleLegend(self.updateLegend) def connect_ui(self): - for i in range(Defines.transceiver.maxcount): + for i in range(Defines.max_counters): getattr(self.view, f"checkBoxTransceiver{i}").stateChanged.connect(partial(self.setTransceiverEnable, i)) getattr(self.view, f"checkBoxTransceiver{i}Plot").stateChanged.connect(partial(self.setTransceiverEnablePlot, i)) @@ -62,7 +62,7 @@ class TransceiverTab(QtWidgets.QWidget): """ enabledPlots = [] self.legend.clear() - for i in range(Defines.transceiver.maxcount): + for i in range(Defines.max_counters): if getattr(self.view, f'checkBoxTransceiver{i}Plot').isChecked(): plotName = getattr(self.view, f"labelTransceiver{i}").text() enabledPlots.append((self.mainWindow.transceiverPlots[i], plotName)) @@ -111,7 +111,7 @@ class TransceiverTab(QtWidgets.QWidget): trans_array = self._processWaveformData(data, dSamples, self.mainWindow.romode.value, self.mainWindow.nDBitEnabled, self.nTransceiverEnabled) idx = 0 - for i in range(Defines.transceiver.maxcount): + for i in range(Defines.max_counters): checkBoxPlot = getattr(self.view, f"checkBoxTransceiver{i}Plot") checkBoxEn = getattr(self.view, f"checkBoxTransceiver{i}") if checkBoxEn.isChecked() and checkBoxPlot.isChecked(): @@ -166,8 +166,6 @@ class TransceiverTab(QtWidgets.QWidget): self.mainWindow.statusbar.showMessage(str(e)) print("Error: ", str(e)) - self.plotTab.setFrameLimits(transceiver_frame) - # keep the zoomed in state (not 1st image) for idx, image_view in enumerate(self.transceiverImageViews): if(self.firstTransceiverImage[idx] and self.shownCounters[idx]): @@ -180,7 +178,7 @@ class TransceiverTab(QtWidgets.QWidget): def update_numCounters(self, num_counters): # update the number of counters and adjust the image splitter accordingly self.nCounters = num_counters - for i in range(Defines.transceiver.maxcount): + for i in range(Defines.max_counters): self.shownCounters[i] = i < self.nCounters self.plotTab.setCounterCheckBox(i, self.shownCounters[i]) # check the counter checkbox self.plotTab.enableCounterCheckBox(i, self.shownCounters[i]) # disable counter checkbox @@ -198,7 +196,7 @@ class TransceiverTab(QtWidgets.QWidget): self.mainWindow.verticalLayoutPlot.addWidget(self.mainWindow.plotTransceiverWaveform, 5) self.mainWindow.transceiverPlots = {} waveform = np.zeros(1000) - for i in range(Defines.transceiver.maxcount): + for i in range(Defines.max_counters): pen = pg.mkPen(color=self.getTransceiverButtonColor(i), width=1) legendName = getattr(self.view, f"labelTransceiver{i}").text() self.mainWindow.transceiverPlots[i] = self.mainWindow.plotTransceiverWaveform.plot(waveform, diff --git a/pyctbgui/pyctbgui/ui/plot.ui b/pyctbgui/pyctbgui/ui/plot.ui index 01d2b5ec1..d063ec636 100644 --- a/pyctbgui/pyctbgui/ui/plot.ui +++ b/pyctbgui/pyctbgui/ui/plot.ui @@ -505,7 +505,7 @@ 10 - 440 + 480 841 400 @@ -1150,7 +1150,7 @@ 10 221 841 - 91 + 131 @@ -1166,189 +1166,241 @@ QFrame::Raised - - 9 - - - 9 - - - - - QFrame::NoFrame - - - QFrame::Plain - - - - - - true - - - - 16777215 - 15 - - - - 3-97% - - - buttonGroup_4 - - - - - - - - 16777215 - 15 - - - - All - - - true - - - buttonGroup_4 - - - - - - - - 16777215 - 15 - - - - Fixed - - - buttonGroup_4 - - - - - - - - - - Qt::Horizontal - - - QSizePolicy::Fixed - - - - 40 - 20 - - - - - - - - 0 - 31 - - - - - 16777215 - 31 - - - - Color Range: - - + + + + + Color Range: + + + + + + + All + + + true + + + buttonGroup_4 + + + + + + + 3-97% + + + buttonGroup_4 + + + + + + + Fixed + + + buttonGroup_4 + + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 40 + 20 + + + + + - - - - false - - - - 0 - 31 - - - - -1000.000000000000000 - - - 100000.000000000000000 - - - - - - - - 50 - 31 - - - - max: - - - - - - - false - - - - 0 - 31 - - - - 100000.000000000000000 - - - - - - - - 50 - 31 - - - - min: - - - - - - - Qt::Horizontal - - - QSizePolicy::Fixed - - - - 40 - 20 - - - + + + + + + Counter 0: + + + + + + + false + + + min: + + + -1000.000000000000000 + + + 100000.000000000000000 + + + + + + + false + + + max: + + + -1000.000000000000000 + + + 100000.000000000000000 + + + + + + + Counter 1: + + + + + + + false + + + min: + + + -1000.000000000000000 + + + 100000.000000000000000 + + + + + + + false + + + max: + + + -1000.000000000000000 + + + 100000.000000000000000 + + + + + + + Counter 2: + + + + + + + false + + + min: + + + -1000.000000000000000 + + + 100000.000000000000000 + + + + + + + false + + + max: + + + -1000.000000000000000 + + + 100000.000000000000000 + + + + + + + Counter 3: + + + + + + + false + + + min: + + + -1000.000000000000000 + + + 100000.000000000000000 + + + + + + + false + + + max: + + + -1000.000000000000000 + + + 100000.000000000000000 + + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 40 + 20 + + + + + @@ -1356,7 +1408,7 @@ 10 - 322 + 362 841 108 diff --git a/pyctbgui/pyctbgui/utils/defines.py b/pyctbgui/pyctbgui/utils/defines.py index b4ecfc04e..7cf4c8420 100644 --- a/pyctbgui/pyctbgui/utils/defines.py +++ b/pyctbgui/pyctbgui/utils/defines.py @@ -12,6 +12,10 @@ class Defines: Acquisition_Tab_Index = 7 Max_Tabs = 9 + + max_counters = 4 # max number of counters + max_counters_tabIndex = 4 + class adc: tabIndex = 5 count = 32 @@ -34,10 +38,6 @@ class Defines: tabIndex = 6 loops_count = 6 - class transceiver: - maxcount = 4 - maxtabIndex = 4 - class slowAdc: tabIndex = 2 count = 8