-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_analysis.py
More file actions
133 lines (116 loc) · 5.7 KB
/
test_analysis.py
File metadata and controls
133 lines (116 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# pylint: disable=no-self-use
from unittest.mock import patch
import numpy as np
import pytest
from qtpy.QtCore import QEvent
from qtpy.QtWidgets import QApplication, QCheckBox
from PartSeg._roi_analysis.measurement_widget import MeasurementsStorage, MeasurementWidget
from PartSeg._roi_mask.simple_measurements import SimpleMeasurements
from PartSegCore.analysis.measurement_base import AreaType, PerComponent
from PartSegCore.analysis.measurement_calculation import ComponentsInfo, MeasurementResult
class TestMeasurementWidget:
@patch("PartSeg._roi_analysis.measurement_widget.QMessageBox")
def test_missed_mask(self, qmessagebox_path, qtbot, analysis_segmentation, part_settings, monkeypatch):
widget = MeasurementWidget(part_settings)
qtbot.addWidget(widget)
assert widget.measurement_type.count() == 4
part_settings.set_project_info(analysis_segmentation)
with qtbot.waitSignal(widget.measurement_type.currentIndexChanged):
widget.measurement_type.setCurrentIndex(2)
assert widget.measurement_type.currentIndex() == 0
assert not widget.recalculate_button.isEnabled()
qmessagebox_path.information.assert_called_once()
assert qmessagebox_path.information.call_args[0][1] == "Need mask"
@pytest.mark.enablethread()
@pytest.mark.enabledialog()
def test_base(self, qtbot, analysis_segmentation, part_settings):
widget = MeasurementWidget(part_settings)
qtbot.addWidget(widget)
assert widget.measurement_type.count() == 4
part_settings.set_project_info(analysis_segmentation)
widget.measurement_type.setCurrentIndex(1)
assert widget.recalculate_button.isEnabled()
widget.recalculate_button.click()
assert widget.info_field.columnCount() == 2
assert widget.info_field.rowCount() == 2
assert widget.info_field.item(1, 1).text() == "4"
widget.horizontal_measurement_present.setChecked(True)
assert widget.info_field.columnCount() == 2
assert widget.info_field.rowCount() == 2
@pytest.mark.enablethread()
@pytest.mark.enabledialog()
def test_base2(self, qtbot, analysis_segmentation2, part_settings):
widget = MeasurementWidget(part_settings)
qtbot.addWidget(widget)
assert widget.measurement_type.count() == 4
part_settings.set_project_info(analysis_segmentation2)
widget.measurement_type.setCurrentIndex(2)
assert widget.recalculate_button.isEnabled()
widget.recalculate_button.click()
assert widget.info_field.columnCount() == 2
assert widget.info_field.rowCount() == 3
assert widget.info_field.item(1, 1).text() == "4"
widget.horizontal_measurement_present.setChecked(True)
assert widget.info_field.columnCount() == 3
assert widget.info_field.rowCount() == 2
@pytest.mark.enablethread()
@pytest.mark.enabledialog()
def test_base_channels(self, qtbot, analysis_segmentation2, part_settings):
widget = MeasurementWidget(part_settings)
qtbot.addWidget(widget)
part_settings.set_project_info(analysis_segmentation2)
widget.measurement_type.setCurrentIndex(3)
assert widget.recalculate_button.isEnabled()
widget.recalculate_button.click()
assert widget.info_field.columnCount() == 2
assert widget.info_field.rowCount() == 4
assert widget.info_field.item(1, 1).text() == "4"
widget.horizontal_measurement_present.setChecked(True)
assert widget.info_field.columnCount() == 4
assert widget.info_field.rowCount() == 2
class TestSimpleMeasurementsWidget:
@pytest.mark.enablethread()
@pytest.mark.enabledialog()
def test_base(self, stack_settings, stack_segmentation1, qtbot):
widget = SimpleMeasurements(stack_settings)
qtbot.addWidget(widget)
stack_settings.set_project_info(stack_segmentation1)
widget.show()
event = QEvent(QEvent.WindowActivate)
QApplication.sendEvent(widget, event)
assert widget.measurement_layout.count() > 2
for i in range(2, widget.measurement_layout.count()):
chk = widget.measurement_layout.itemAt(i).widget()
assert isinstance(chk, QCheckBox)
chk.setChecked(True)
widget.calculate()
assert widget.result_view.rowCount() == widget.measurement_layout.count() - 1
assert widget.result_view.columnCount() == len(stack_settings.roi_info.bound_info) + 1
class TestMeasurementsStorage:
def test_empty(self):
obj = MeasurementsStorage()
assert obj.get_size(True) == (0, 0)
assert obj.get_size(False) == (0, 0)
def test_base(self):
info = ComponentsInfo(np.arange(1, 4), np.array([]), {})
storage = MeasurementResult(info)
storage["aa"] = 1, "", (PerComponent.No, AreaType.ROI)
storage["bb"] = [4, 5, 6], "np", (PerComponent.Yes, AreaType.ROI)
obj = MeasurementsStorage()
obj.add_measurements(storage)
obj.set_expand(False)
assert obj.get_size(True) == (2, 2)
obj.set_expand(True)
assert obj.get_size(True) == (3, 4)
assert obj.get_size(False) == (4, 3)
obj.set_expand(False)
assert obj.get_val_as_str(0, 0, True) == "aa"
assert obj.get_val_as_str(0, 0, False) == "aa"
assert obj.get_val_as_str(0, 1, True) == "bb"
assert obj.get_val_as_str(1, 0, False) == "bb"
assert obj.get_header(True) == ["0", "1"]
assert obj.get_header(False) == ["Name", "Value"]
assert obj.get_rows(False) == ["0", "1"]
assert obj.get_rows(True) == ["Name", "Value"]
obj.set_expand(True)
assert obj.get_header(False) == ["Name", "Value", "Value", "Value"]