Skip to content

Commit

Permalink
Fix display bug in Gotthard Window and add more unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujun98 committed Jun 26, 2020
1 parent 62394eb commit 06c6384
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 52 deletions.
2 changes: 1 addition & 1 deletion extra_foam/special_suite/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ def topics(self):
_MAX_N_GOTTHARD_PULSES = 120

GOTTHARD_DEVICE = {
"MID": "MID_EXP_DES/DET/GOTTHARD_RECEIVER:daqOutput",
"MID": "MID_EXP_DES/DET/GOTTHARD_RECEIVER:output",
"SCS": "SCS_PAM_XOX/DET/GOTTHARD_RECEIVER1:daqOutput",
}
24 changes: 17 additions & 7 deletions extra_foam/special_suite/gotthard_pump_probe_w.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Copyright (C) European X-Ray Free-Electron Laser Facility GmbH.
All rights reserved.
"""
import numpy as np

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIntValidator
from PyQt5.QtWidgets import QSplitter
Expand Down Expand Up @@ -94,8 +96,10 @@ def __init__(self, *, parent=None):

def updateF(self, data):
"""Override."""
self._mean.setData(data['vfom_mean'])
self._mean_ma.setData(data['vfom_ma_mean'])
vfom_mean = data['vfom_mean']
x = np.arange(len(vfom_mean))
self._mean.setData(x, vfom_mean)
self._mean_ma.setData(x, data['vfom_ma_mean'])


class GotthardPpFomPulsePlot(PlotWidgetF):
Expand Down Expand Up @@ -128,8 +132,10 @@ def updateF(self, data):
self._idx = idx
self._updateTitle()

self._poi.setData(data['vfom'][idx])
self._poi_ma.setData(data['vfom_ma'][idx])
vfom = data['vfom'][idx]
x = np.arange(len(vfom))
self._poi.setData(x, vfom)
self._poi_ma.setData(x, data['vfom_ma'][idx])


class GotthardPpRawPulsePlot(PlotWidgetF):
Expand Down Expand Up @@ -161,8 +167,10 @@ def updateF(self, data):
self._idx = idx
self._updateTitle()

self._on.setData(data['raw'][data['on_slicer']][idx])
self._off.setData(data['raw'][data['off_slicer']][idx])
on = data['raw'][data['on_slicer']][idx]
x = np.arange(len(on))
self._on.setData(x, on)
self._off.setData(x, data['raw'][data['off_slicer']][idx])


class GotthardPpDarkPulsePlot(PlotWidgetF):
Expand Down Expand Up @@ -192,7 +200,9 @@ def updateF(self, data):
self._idx = idx
self._updateTitle()

self._plot.setData(data['raw'][data['dark_slicer']][idx])
raw = data['raw'][data['dark_slicer']][idx]
x = np.arange(len(raw))
self._plot.setData(x, raw)


class GotthardPpImageView(ImageViewF):
Expand Down
24 changes: 16 additions & 8 deletions extra_foam/special_suite/gotthard_w.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"""
from string import Template

import numpy as np

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QDoubleValidator, QIntValidator
from PyQt5.QtWidgets import QCheckBox, QSplitter
Expand Down Expand Up @@ -112,16 +114,19 @@ def __init__(self, *, parent=None):

def updateF(self, data):
"""Override."""
spectrum = data['spectrum_mean']
spectrum_ma = data['spectrum_ma_mean']

x = data["x"]
if x is None:
self._mean.setData(data['spectrum_mean'])
self._mean_ma.setData(data['spectrum_ma_mean'])
self.setLabel('bottom', "Pixel")
x = np.arange(len(spectrum))
else:
self._mean.setData(x, data['spectrum_mean'])
self._mean_ma.setData(x, data['spectrum_ma_mean'])
self.setLabel('bottom', "eV")

self._mean.setData(x, spectrum)
self._mean_ma.setData(x, spectrum_ma)


class GotthardPulsePlot(PlotWidgetF):
"""GotthardPulsePlot class.
Expand Down Expand Up @@ -152,16 +157,19 @@ def updateF(self, data):
self._idx = idx
self._updateTitle()

spectrum = data['spectrum'][idx]
spectrum_ma = data['spectrum_ma'][idx]

x = data["x"]
if x is None:
self._poi.setData(data['spectrum'][idx])
self._poi_ma.setData(data['spectrum_ma'][idx])
self.setLabel('bottom', "Pixel")
x = np.arange(len(spectrum))
else:
self._poi.setData(x, data['spectrum'][idx])
self._poi_ma.setData(x, data['spectrum_ma'][idx])
self.setLabel('bottom', "eV")

self._poi.setData(x, spectrum)
self._poi_ma.setData(x, spectrum_ma)


class GotthardImageView(ImageViewF):
"""GotthardImageView class.
Expand Down
27 changes: 18 additions & 9 deletions extra_foam/special_suite/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from extra_foam.gui.plot_widgets import TimedPlotWidgetF, TimedImageViewF

from extra_foam.special_suite import logger, mkQApp


class _SpecialSuiteWindowTestBase(unittest.TestCase):
@staticmethod
Expand All @@ -12,15 +14,22 @@ def data4visualization():
def _check_update_plots(self):
win = self._win
worker = win._worker_st
worker._output_st.put_pop(self.data4visualization())

win.updateWidgetsST()
for widget in win._plot_widgets_st:
if isinstance(widget, TimedPlotWidgetF):
widget.refresh()
for widget in win._image_views_st:
if isinstance(widget, TimedImageViewF):
widget.refresh()

with self.assertLogs(logger, level="ERROR") as cm:
logger.error("dummy") # workaround

win.updateWidgetsST() # with empty data

worker._output_st.put_pop(self.data4visualization())
win.updateWidgetsST()
for widget in win._plot_widgets_st:
if isinstance(widget, TimedPlotWidgetF):
widget.refresh()
for widget in win._image_views_st:
if isinstance(widget, TimedImageViewF):
widget.refresh()

self.assertEqual(1, len(cm.output))


class _SpecialSuiteProcessorTestBase:
Expand Down
25 changes: 21 additions & 4 deletions extra_foam/special_suite/tests/test_camview.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
CamViewWindow, CameraView, CameraViewRoiHist
)

from . import _SpecialSuiteWindowTestBase, _SpecialSuiteProcessorTestBase


app = mkQApp()

logger.setLevel('CRITICAL')
logger.setLevel('INFO')


class TestCamView(unittest.TestCase):
class TestCamViewWindow(_SpecialSuiteWindowTestBase):
@classmethod
def setUpClass(cls):
cls._win = CamViewWindow('SCS')
Expand All @@ -32,6 +35,14 @@ def tearDownClass(cls):
# explicitly close the MainGUI to avoid error in GuiLogger
cls._win.close()

@staticmethod
def data4visualization():
"""Override."""
return {
"displayed": np.arange(20).reshape(4, 5),
"roi_hist": (np.arange(4), np.arange(4), 1, 2, 3)
}

def testWindow(self):
win = self._win

Expand All @@ -43,7 +54,7 @@ def testWindow(self):
self.assertEqual(1, counter[CameraView])
self.assertEqual(1, counter[CameraViewRoiHist])

win.updateWidgetsST()
self._check_update_plots()

def testCtrl(self):
from extra_foam.special_suite.cam_view_w import (
Expand Down Expand Up @@ -99,7 +110,7 @@ def testCtrl(self):
self.assertEqual(999, proc._n_bins)


class TestCamViewProcessor(_RawDataMixin):
class TestCamViewProcessor(_RawDataMixin, _SpecialSuiteProcessorTestBase):
@pytest.fixture(autouse=True)
def setUp(self):
self._proc = CamViewProcessor(object(), object())
Expand Down Expand Up @@ -208,6 +219,7 @@ def testProcessing(self, subtract_dark):

# 1st train
processed = proc.process(self._get_data(12345))
self._check_processed_data_structure(processed)
np.testing.assert_array_almost_equal(imgdata_gt, processed["displayed"])

# 2nd train
Expand All @@ -222,3 +234,8 @@ def testProcessing(self, subtract_dark):
# reset
proc.reset()
assert proc._raw_ma is None

def _check_processed_data_structure(self, ret):
"""Override."""
data_gt = TestCamViewWindow.data4visualization().keys()
assert set(ret.keys()) == set(data_gt)
29 changes: 25 additions & 4 deletions extra_foam/special_suite/tests/test_gotthard.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
ProcessingError
)

from . import _SpecialSuiteWindowTestBase, _SpecialSuiteProcessorTestBase

app = mkQApp()

logger.setLevel('CRITICAL')
logger.setLevel('INFO')


class TestGotthard(unittest.TestCase):
class TestGotthardWindow(_SpecialSuiteWindowTestBase):
@classmethod
def setUpClass(cls):
cls._win = GotthardWindow('MID')
Expand All @@ -36,6 +38,19 @@ def tearDownClass(cls):
# explicitly close the MainGUI to avoid error in GuiLogger
cls._win.close()

@staticmethod
def data4visualization(n_pulses=4):
"""Override."""
return {
"x": None,
"spectrum": np.arange(10 * n_pulses).reshape(n_pulses, 10),
"spectrum_ma": np.arange(10 * n_pulses).reshape(n_pulses, 10),
"spectrum_mean": np.arange(10),
"spectrum_ma_mean": np.arange(10),
"poi_index": 0,
"hist": (np.arange(5), np.arange(5), 1, 1, 1),
}

def testWindow(self):
win = self._win

Expand All @@ -49,7 +64,7 @@ def testWindow(self):
self.assertEqual(1, counter[GotthardPulsePlot])
self.assertEqual(1, counter[GotthardHist])

win.updateWidgetsST()
self._check_update_plots()

def testCtrl(self):
from extra_foam.special_suite.gotthard_w import _DEFAULT_N_BINS, _DEFAULT_BIN_RANGE
Expand Down Expand Up @@ -135,7 +150,7 @@ def testCtrl(self):
self.assertTrue(proc._hist_over_ma)


class TestGotthardProcessor(_RawDataMixin):
class TestGotthardProcessor(_RawDataMixin, _SpecialSuiteProcessorTestBase):
@pytest.fixture(autouse=True)
def setUp(self):
self._proc = GotthardProcessor(object(), object())
Expand Down Expand Up @@ -269,6 +284,7 @@ def testProcessing(self, subtract_dark):

# 1st train
processed = proc.process(self._get_data(12345))
self._check_processed_data_structure(processed)
assert 1 == processed["poi_index"]
np.testing.assert_array_almost_equal(adc_gt, processed["spectrum"])
np.testing.assert_array_almost_equal(adc_gt, processed["spectrum_ma"])
Expand Down Expand Up @@ -329,3 +345,8 @@ def testRemoveDark(self):
proc.onRemoveDark()
assert proc._dark_ma is None
assert proc._dark_mean_ma is None

def _check_processed_data_structure(self, ret):
"""Override."""
data_gt = TestGotthardWindow.data4visualization().keys()
assert set(ret.keys()) == set(data_gt)
33 changes: 29 additions & 4 deletions extra_foam/special_suite/tests/test_gotthard_pump_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
)
from extra_foam.special_suite.special_analysis_base import ProcessingError

from . import _SpecialSuiteWindowTestBase, _SpecialSuiteProcessorTestBase

app = mkQApp()

logger.setLevel('CRITICAL')
logger.setLevel('INFO')


class TestGotthardPumpProbe(unittest.TestCase):
class TestGotthardPpWindow(_SpecialSuiteWindowTestBase):
@classmethod
def setUpClass(cls):
cls._win = GotthardPumpProbeWindow('SCS')
Expand All @@ -33,6 +35,23 @@ def tearDownClass(cls):
# explicitly close the MainGUI to avoid error in GuiLogger
cls._win.close()

@staticmethod
def data4visualization(n_pulses=5, n_on=2, n_off=2):
"""Override."""
return {
"raw": np.arange(8*n_pulses).reshape(n_pulses, 8),
"corrected": np.arange(8 * n_pulses).reshape(n_pulses, 8),
"on_slicer": slice(0, n_on),
"off_slicer": slice(n_on, n_on + n_off),
"dark_slicer": slice(n_on + n_off, None),
"poi_index": n_on - 1,
"dark_poi_index": 0,
"vfom": np.arange(40).reshape(5, 8),
"vfom_ma": np.arange(40).reshape(5, 8),
"vfom_mean": np.arange(8),
"vfom_ma_mean": np.arange(8),
}

def testWindow(self):
win = self._win

Expand All @@ -47,7 +66,7 @@ def testWindow(self):
self.assertEqual(1, counter[GotthardPpRawPulsePlot])
self.assertEqual(1, counter[GotthardPpDarkPulsePlot])

win.updateWidgetsST()
self._check_update_plots()

def testCtrl(self):
win = self._win
Expand Down Expand Up @@ -103,7 +122,7 @@ def testCtrl(self):
self.assertEqual(9, proc.__class__._vfom_ma.window)


class TestGotthardPpProcessor(_RawDataMixin):
class TestGotthardPpProcessor(_RawDataMixin, _SpecialSuiteProcessorTestBase):
@pytest.fixture(autouse=True)
def setUp(self):
self._proc = GotthardPpProcessor(object(), object())
Expand Down Expand Up @@ -153,9 +172,15 @@ def testProcessing(self):
adc_gt = self._adc.astype(_PIXEL_DTYPE)

processed = proc.process(self._get_data(12345))
self._check_processed_data_structure(processed)

np.testing.assert_array_almost_equal(adc_gt, processed["raw"])
corrected_gt = adc_gt - np.mean(adc_gt[proc._dark_slicer], axis=0)
np.testing.assert_array_almost_equal(corrected_gt, processed["corrected"])
vfom_gt = corrected_gt[proc._on_slicer] - corrected_gt[proc._off_slicer]
np.testing.assert_array_almost_equal(vfom_gt, processed["vfom"])

def _check_processed_data_structure(self, ret):
"""Override."""
data_gt = TestGotthardPpWindow.data4visualization().keys()
assert set(ret.keys()) == set(data_gt)
5 changes: 3 additions & 2 deletions extra_foam/special_suite/tests/test_module_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
ModuleScanWindow
)

from . import _SpecialSuiteWindowTestBase, _SpecialSuiteProcessorTestBase

app = mkQApp()

logger.setLevel('CRITICAL')
logger.setLevel('INFO')


class TestModuleScan(unittest.TestCase):
class TestModuleScan(_SpecialSuiteWindowTestBase):
@classmethod
def setUpClass(cls):
cls._win = ModuleScanWindow('DET')
Expand Down

0 comments on commit 06c6384

Please sign in to comment.