|
| 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