Skip to content

Commit a70b4a1

Browse files
committed
tests: add test for bulk emodulus computation (#60)
1 parent bf28672 commit a70b4a1

File tree

3 files changed

+206
-11
lines changed

3 files changed

+206
-11
lines changed

shapeout2/gui/bulk/bulk_emodulus.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ def set_emodulus_properties(self):
8585
"""Set the given emodulus properties for all datasets"""
8686
medium = self.comboBox_medium.currentData()
8787
if self.comboBox_temp.isEnabled():
88-
temp = self.comboBox_temp.currentData()
88+
scen = self.comboBox_temp.currentData()
8989
else:
90-
temp = None
90+
scen = None
9191
if self.doubleSpinBox_temp.isEnabled():
9292
tempval = self.doubleSpinBox_temp.value()
9393
else:
@@ -105,27 +105,24 @@ def set_emodulus_properties(self):
105105

106106
# Use the internal sanity checks to determine whether
107107
# or not we can set the medium or temperature scenarios.
108-
choices_medium = SlotPanel.get_dataset_choices_medium(ds)
109-
choices_temp = SlotPanel.get_dataset_choices_temperature(ds)
110-
if medium in [m[1] for m in choices_medium]:
108+
valid_media = SlotPanel.get_dataset_choices_medium(ds)
109+
valid_scenarios = SlotPanel.get_dataset_choices_temperature(ds)
110+
if medium in [m[1] for m in valid_media]:
111111
state = slot.__getstate__()
112112
state["emodulus"]["emodulus medium"] = medium
113-
slot.__setstate__(state)
114-
115113
# Set the viscosity here, because unknown media are
116114
# available.
117115
if viscval is not None:
118116
state["emodulus"]["emodulus viscosity"] = viscval
117+
slot.__setstate__(state)
119118

120-
if temp in [t[1] for t in choices_temp]: # temp is not None
119+
if scen in [s[1] for s in valid_scenarios]: # scen is not None
121120
state = slot.__getstate__()
122-
state["emodulus"]["emodulus scenario"] = temp
121+
state["emodulus"]["emodulus scenario"] = scen
123122
if tempval is not None:
124123
state["emodulus"]["emodulus temperature"] = tempval
125124
slot.__setstate__(state)
126125

127-
print(state["emodulus"])
128-
129126
def update_ui(self):
130127
"""Update all relevant parts of the main user interface"""
131128
state = self.pipeline.__getstate__()

tests/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
import shutil
2+
import tempfile
3+
import time
4+
15
from PyQt5 import QtCore
26

37

8+
TMPDIR = tempfile.mkdtemp(prefix=time.strftime(
9+
"shapeout2_test_%H.%M_"))
10+
11+
412
def pytest_configure(config):
513
"""This is ran before all tests"""
614
# disable update checking
@@ -13,3 +21,13 @@ def pytest_configure(config):
1321
settings.setValue("check for updates", 0)
1422
settings.setValue("advanced/check pyqtgraph version", 0)
1523
settings.sync()
24+
# set global temp directory
25+
tempfile.tempdir = TMPDIR
26+
27+
28+
def pytest_unconfigure(config):
29+
"""
30+
called before test process is exited.
31+
"""
32+
# clear global temp directory
33+
shutil.rmtree(TMPDIR, ignore_errors=True)

tests/test_gui_bulk_emodulus.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
"""Test bulk action for emodulus computation"""
2+
import pathlib
3+
import tempfile
4+
5+
import dclab
6+
import h5py
7+
import numpy as np
8+
import pytest
9+
10+
from shapeout2.gui.main import ShapeOut2
11+
from shapeout2.gui import bulk
12+
from shapeout2 import session
13+
14+
15+
datapath = pathlib.Path(__file__).parent / "data"
16+
17+
18+
def make_dataset(medium="CellCarrier", temp=22.5, temp_range=[22, 23],
19+
chip_region="channel"):
20+
# create a fake dataset
21+
path = datapath / "calibration_beads_47.rtdc"
22+
ds = dclab.new_dataset(path)
23+
tmp = tempfile.mktemp(".rtdc", prefix="example_")
24+
ds.export.hdf5(tmp, features=["deform", "area_um", "bright_avg"])
25+
with h5py.File(tmp, mode="a") as h5:
26+
h5["events/temp"] = np.linspace(temp_range[0], temp_range[1], len(ds))
27+
if medium is None:
28+
h5.attrs.pop("setup:medium")
29+
else:
30+
h5.attrs["setup:medium"] = medium
31+
h5.attrs["setup:temperature"] = temp
32+
h5.attrs["setup:chip region"] = chip_region
33+
return pathlib.Path(tmp)
34+
35+
36+
@pytest.fixture(autouse=True)
37+
def run_around_tests():
38+
# Code that will run before your test, for example:
39+
session.clear_session()
40+
# A test function will be run at this point
41+
yield
42+
# Code that will run after your test, for example:
43+
session.clear_session()
44+
45+
46+
def test_manual_basic(qtbot):
47+
"""Most simple test"""
48+
mw = ShapeOut2()
49+
qtbot.addWidget(mw)
50+
51+
# add a dataslot
52+
path = datapath / "calibration_beads_47.rtdc"
53+
mw.add_dataslot(paths=[path])
54+
mw.add_dataslot(paths=[path])
55+
56+
# sanity check (no emodulus should be available)
57+
for slot in mw.pipeline.slots:
58+
ds = slot.get_dataset()
59+
assert "emodulus" not in ds
60+
assert ds.config["setup"]["medium"] == "CellCarrierB"
61+
62+
# create bulk action dialog manually
63+
dlg = bulk.BulkActionEmodulus(mw, pipeline=mw.pipeline)
64+
dlg.comboBox_temp.setCurrentIndex(dlg.comboBox_temp.findData("manual"))
65+
dlg.doubleSpinBox_temp.setValue(29.5)
66+
dlg.on_ok()
67+
68+
for slot in mw.pipeline.slots:
69+
ds = slot.get_dataset()
70+
assert "emodulus" in ds
71+
assert ds.config["setup"]["medium"] == "CellCarrierB"
72+
assert ds.config["calculation"]["emodulus model"] == "elastic sphere"
73+
assert ds.config["calculation"]["emodulus medium"] == "CellCarrierB"
74+
assert ds.config["calculation"]["emodulus temperature"] == 29.5
75+
assert "emodulus viscosity" not in ds.config["calculation"]
76+
77+
78+
def test_manual_wrong_medium(qtbot):
79+
"""Deliberately set wrong medium"""
80+
mw = ShapeOut2()
81+
qtbot.addWidget(mw)
82+
83+
# add a dataslot
84+
path = datapath / "calibration_beads_47.rtdc"
85+
mw.add_dataslot(paths=[path])
86+
mw.add_dataslot(paths=[path])
87+
88+
# create bulk action dialog manually
89+
dlg = bulk.BulkActionEmodulus(mw, pipeline=mw.pipeline)
90+
dlg.comboBox_temp.setCurrentIndex(dlg.comboBox_temp.findData("manual"))
91+
dlg.doubleSpinBox_temp.setValue(29.5)
92+
# Set medium to "water". This should not change the emodulus medium.
93+
dlg.comboBox_medium.setCurrentIndex(dlg.comboBox_medium.findData("water"))
94+
95+
dlg.on_ok()
96+
97+
for slot in mw.pipeline.slots:
98+
ds = slot.get_dataset()
99+
assert "emodulus" in ds
100+
assert ds.config["setup"]["medium"] == "CellCarrierB"
101+
assert ds.config["calculation"]["emodulus model"] == "elastic sphere"
102+
assert ds.config["calculation"]["emodulus medium"] == "CellCarrierB"
103+
assert ds.config["calculation"]["emodulus temperature"] == 29.5
104+
assert "emodulus viscosity" not in ds.config["calculation"]
105+
106+
107+
def test_temperature_feature(qtbot):
108+
mw = ShapeOut2()
109+
qtbot.addWidget(mw)
110+
111+
# add custom dataslot
112+
path = make_dataset(medium="CellCarrier", temp=22.5, temp_range=[22, 23])
113+
mw.add_dataslot(paths=[path])
114+
mw.add_dataslot(paths=[path])
115+
116+
# create bulk action dialog manually
117+
dlg = bulk.BulkActionEmodulus(mw, pipeline=mw.pipeline)
118+
dlg.comboBox_temp.setCurrentIndex(dlg.comboBox_temp.findData("feature"))
119+
dlg.on_ok()
120+
121+
for slot in mw.pipeline.slots:
122+
ds = slot.get_dataset()
123+
assert "emodulus" in ds
124+
assert ds.config["setup"]["medium"] == "CellCarrier"
125+
assert ds.config["calculation"]["emodulus model"] == "elastic sphere"
126+
assert ds.config["calculation"]["emodulus medium"] == "CellCarrier"
127+
assert "emodulus temperature" not in ds.config["calculation"]
128+
assert "emodulus viscosity" not in ds.config["calculation"]
129+
130+
131+
def test_viscosity(qtbot):
132+
mw = ShapeOut2()
133+
qtbot.addWidget(mw)
134+
135+
# add custom dataslot
136+
path = make_dataset(medium="other")
137+
mw.add_dataslot(paths=[path])
138+
mw.add_dataslot(paths=[path])
139+
140+
# create bulk action dialog manually
141+
dlg = bulk.BulkActionEmodulus(mw, pipeline=mw.pipeline)
142+
dlg.comboBox_medium.setCurrentIndex(dlg.comboBox_medium.findData("other"))
143+
dlg.doubleSpinBox_visc.setValue(1.0)
144+
dlg.on_ok()
145+
146+
for slot in mw.pipeline.slots:
147+
ds = slot.get_dataset()
148+
assert "emodulus" in ds
149+
assert ds.config["setup"]["medium"] == "other"
150+
assert ds.config["calculation"]["emodulus model"] == "elastic sphere"
151+
assert "emodulus medium" not in ds.config["calculation"]
152+
assert "emodulus temperature" not in ds.config["calculation"]
153+
assert ds.config["calculation"]["emodulus viscosity"] == 1.0
154+
155+
156+
def test_wrong_medium_viscosity(qtbot):
157+
"""Deliberately set wrong visosity"""
158+
mw = ShapeOut2()
159+
qtbot.addWidget(mw)
160+
161+
# add a dataslot
162+
path = datapath / "calibration_beads_47.rtdc"
163+
mw.add_dataslot(paths=[path])
164+
mw.add_dataslot(paths=[path])
165+
166+
# create bulk action dialog manually
167+
dlg = bulk.BulkActionEmodulus(mw, pipeline=mw.pipeline)
168+
dlg.comboBox_medium.setCurrentIndex(dlg.comboBox_medium.findData("other"))
169+
dlg.doubleSpinBox_visc.setValue(1.0) # random number
170+
171+
dlg.on_ok()
172+
173+
for slot in mw.pipeline.slots:
174+
ds = slot.get_dataset()
175+
assert "emodulus" not in ds, "because medium is fixed"
176+
assert ds.config["setup"]["medium"] == "CellCarrierB"
177+
assert ds.config["calculation"]["emodulus model"] == "elastic sphere"
178+
assert ds.config["calculation"]["emodulus medium"] == "CellCarrierB"
179+
assert "emodulus temperature" not in ds.config["calculation"]
180+
assert "emodulus viscosity" not in ds.config["calculation"]

0 commit comments

Comments
 (0)