def test_abstract_base_class(): image_set = PDSSpectImageSet(TEST_FILES) view_canvas = PDSImageViewCanvas() with pytest.raises(TypeError): ROIBase(image_set, view_canvas) class MockROIBase(ROIBase): def start_ROI(self, x, y): super(MockROIBase, self).start_ROI(x, y) return True def continue_ROI(self, x, y): super(MockROIBase, self).continue_ROI(x, y) return True def extend_ROI(self, x, y): super(MockROIBase, self).extend_ROI(x, y) return True def stop_ROI(self, x, y): super(MockROIBase, self).stop_ROI(x, y) return True # Test Proper subclass of Base Class mock_roi_base_class = MockROIBase(image_set, view_canvas) assert isinstance(mock_roi_base_class, ROIBase) assert mock_roi_base_class.start_ROI(0, 0) assert mock_roi_base_class.continue_ROI(1, 1) assert mock_roi_base_class.extend_ROI(2, 2) assert mock_roi_base_class.stop_ROI(2, 0)
class TestTransformsController(object): image_set = PDSSpectImageSet(TEST_FILES) subset = image_set.create_subset() controller = TransformsController(image_set, None) def test_set_flip_x(self): assert not self.image_set.flip_x assert not self.subset.flip_x self.controller.set_flip_x(True) assert self.image_set.flip_x assert self.subset.flip_x self.controller.set_flip_x(False) assert not self.image_set.flip_x assert not self.subset.flip_x def test_set_flip_y(self): assert not self.image_set.flip_y assert not self.subset.flip_y self.controller.set_flip_y(True) assert self.image_set.flip_y assert self.subset.flip_y self.controller.set_flip_y(False) assert not self.image_set.flip_y assert not self.subset.flip_y def test_set_swap_xy(self): assert not self.image_set.swap_xy assert not self.subset.swap_xy self.controller.set_swap_xy(True) assert self.image_set.swap_xy assert self.subset.swap_xy self.controller.set_swap_xy(False) assert not self.image_set.swap_xy assert not self.subset.swap_xy
class TestSetWavelengthController(object): image_set = PDSSpectImageSet([FILE_1, FILE_3]) model = SetWavelengthModel(image_set) @pytest.fixture def controller(self): reset_image_set(self.image_set) self.model = SetWavelengthModel(self.image_set) return SetWavelengthController(self.model, None) def test_set_current_image_index(self, controller): assert self.model.current_image_index == 0 controller.set_current_image_index(1) assert self.model.current_image_index == 1 def test_change_unit(self, controller): assert self.model.unit == 'nm' controller.change_unit(1) assert self.model.unit == 'um' controller.change_unit(2) assert self.model.unit == 'AA' controller.change_unit(0) assert self.model.unit == 'nm' def test_set_image_wavelength(self, controller): assert np.isnan(self.model.current_image.wavelength) controller.set_image_wavelength(100.0) assert self.model.current_image.wavelength == 100.0 controller.set_current_image_index(1) assert self.model.current_image.wavelength == 440.0 controller.set_image_wavelength(50.0) assert self.model.current_image.wavelength == 50.0 controller.set_current_image_index(0) assert self.model.current_image.wavelength == 100.0
class TestPDSSpectViewController(object): image_set = PDSSpectImageSet(TEST_FILES) controller = PDSSpectViewController(image_set, None) default_center = image_set.center @pytest.fixture def test_set(self): yield self.image_set self.image_set.zoom = 1 self.image_set.center = self.default_center def test_init(self, test_set): assert self.controller.image_set == test_set assert self.controller.view is None def test_change_pan_center(self, test_set): test_set.zoom = 2 assert test_set.center == (16, 32) self.controller.change_pan_center(24, 48) assert test_set.center == (24, 48) self.controller.change_pan_center(8, 16) assert test_set.center == (8, 16) self.controller.change_pan_center(16, 32) assert test_set.center == (16, 32) test_set.zoom = 1 assert test_set.center == (16, 32) def test_change_pan_size(self, test_set): assert test_set.zoom == 1 self.controller.change_pan_size(2) assert test_set.zoom == 2 self.controller.change_pan_size(1) assert test_set.zoom == 1
class TestBasicWidget(object): image_set = PDSSpectImageSet(TEST_FILES) image_view = PDSImageViewCanvas() image_view.set_image(image_set.images[0]) @pytest.fixture def basic_widget(self, qtbot): reset_image_set(self.image_set) self.image_view = PDSImageViewCanvas() self.image_view.set_image(self.image_set.images[0]) basic_widget = BasicWidget(self.image_set, self.image_view) basic_widget.show() qtbot.add_widget(basic_widget) return basic_widget def test_init(self, basic_widget): assert basic_widget.image_set == self.image_set assert len(basic_widget.basics) == 1 assert basic_widget.main_layout.count() == 1 basic = basic_widget.main_layout.itemAt(0).widget() assert basic == basic_widget.basics[0] def test_add_basic(self, basic_widget): basic_widget.add_basic(self.image_set, self.image_view) assert len(basic_widget.basics) == 2 assert basic_widget.main_layout.count() == 2 basic1 = basic_widget.main_layout.itemAt(0).widget() basic2 = basic_widget.main_layout.itemAt(1).widget() assert basic1 == basic_widget.basics[0] assert basic2 == basic_widget.basics[1] def test_connect_model(self, basic_widget): image_set = self.image_set basic = basic_widget.basics[0] subset1 = SubPDSSpectImageSet(self.image_set) basic1 = Basic(subset1, self.image_view, basic_widget) basic_widget.basics.append(basic1) assert image_set.current_image_index == subset1.current_image_index basic_widget.connect_model(basic1) assert basic1.histogram.connected_models == [basic.histogram] assert basic.histogram.connected_models == [basic1.histogram] subset2 = SubPDSSpectImageSet(self.image_set) basic2 = Basic(subset2, self.image_view, basic_widget) basic_widget.basics.append(basic2) subset2.current_image_index = 1 assert image_set.current_image_index != subset2.current_image_index assert subset1.current_image_index != subset2.current_image_index basic_widget.connect_model(basic2) assert basic1.histogram.connected_models == [basic.histogram] assert basic.histogram.connected_models == [basic1.histogram] assert basic2.histogram.connected_models == [] subset1.current_image_index = 2 assert image_set.current_image_index != subset1.current_image_index assert image_set.current_image_index != subset2.current_image_index assert subset1.current_image_index != subset2.current_image_index basic_widget.connect_model(basic1) assert basic1.histogram.connected_models == [] assert basic.histogram.connected_models == [] assert basic2.histogram.connected_models == []
class TestSetWavelengthWidget(object): image_set = PDSSpectImageSet([FILE_1, FILE_3]) model = SetWavelengthModel(image_set) @pytest.fixture def widget(self, qtbot): reset_image_set(self.image_set) self.model = SetWavelengthModel(self.image_set) widget = SetWavelengthWidget(self.model) qtbot.add_widget(widget) widget.show() return widget def test_select_image(self, widget): assert self.model.current_image_index == 0 widget.select_image(1) assert self.model.current_image_index == 1 def test_display_current_wavelength(self, widget): assert widget.wavelength_text.text() == '' self.model.current_image.wavelength = 100.0 widget.display_current_wavelength() assert widget.wavelength_text.text() == '100.0' self.model.current_image.wavelength = float('nan') widget.display_current_wavelength() assert widget.wavelength_text.text() == '' def test_set_wavelength(self, widget): assert widget.wavelength_text.text() == '' widget.wavelength_text.setText('100') assert np.isnan(self.model.current_image.wavelength) widget.set_wavelength() self.model.current_image.wavelength = 100.0 assert widget.wavelength_text.text() == '100.0' widget.wavelength_text.setText('foo') widget.set_wavelength() assert widget.wavelength_text.text() == '100.0' widget.wavelength_text.setText('') widget.set_wavelength() assert np.isnan(self.model.current_image.wavelength) assert widget.wavelength_text.text() == '' def test_change_unit(self, widget): assert self.model.unit == 'nm' widget.change_unit(1) assert self.model.unit == 'um' widget.change_unit(2) assert self.model.unit == 'AA' widget.change_unit(0) assert self.model.unit == 'nm' def test_show_status_bar_wavelength_set(self, widget, qtbot): status_bar = widget.statusBar() widget.show_status_bar_wavelength_set() assert status_bar.currentMessage() == 'Wavelength Set' widget.show_status_bar_wavelength_set() qtbot.wait(1000) # Test message disapears after 1 second assert status_bar.currentMessage() == ''
class TestBasicController(object): image_set = PDSSpectImageSet(TEST_FILES) controller = BasicController(image_set, None) def test_change_current_image_index(self): assert self.image_set.current_image_index == 0 self.controller.change_current_image_index(2) assert self.image_set.current_image_index == 2
class TestROIPlotModel(object): image_set = PDSSpectImageSet([FILE_1, FILE_3]) @pytest.fixture() def test_model(self): reset_image_set(self.image_set) return roi_plot.ROIPlotModel(self.image_set) def test_image_sets(self, test_model): assert test_model.image_sets == [self.image_set] subset = self.image_set.create_subset() assert test_model.image_sets == [self.image_set, subset] def test_image_set(self, test_model): assert test_model.image_set == self.image_set subset = self.image_set.create_subset() test_model._view_index = 1 assert test_model.image_set == subset def test_has_multiple_views(self, test_model): assert not test_model.has_multiple_views self.image_set.create_subset() assert test_model.has_multiple_views def test_view_index(self, test_model): assert not test_model.has_multiple_views assert test_model._view_index == 0 assert test_model.view_index == -1 self.image_set.create_subset() assert test_model.has_multiple_views assert test_model.view_index == 0 test_model.view_index = 1 assert test_model.view_index == 1 assert test_model._view_index == 1 test_model.image_index = 1 test_model.view_index = 0 assert test_model.view_index == 0 assert test_model._view_index == 0 assert test_model.image_index == -1 def test_add_selected_color(self, test_model): assert test_model.selected_colors == [] test_model.add_selected_color('red') assert test_model.selected_colors == ['red'] def test_remove_selected_color(self, test_model): test_model.selected_colors = ['red'] test_model.remove_selected_color('red') assert test_model.selected_colors == [] def test_unit(self, test_model): assert self.image_set.unit == 'nm' assert test_model.unit == 'nm' self.image_set.unit = 'um' assert test_model.unit == '\mu m' self.image_set.unit = 'AA' assert test_model.unit == '\AA'
class TestTransforms(object): image_set = PDSSpectImageSet(TEST_FILES) subset = image_set.create_subset() view_canvas = PDSImageViewCanvas() view_canvas.set_image(image_set.current_image) trans = Transforms(image_set, view_canvas) def test_flip_x_checked(self, qtbot): qtbot.add_widget(self.trans) self.trans.show() assert not self.trans.flip_x_box.isChecked() assert not self.image_set.flip_x assert not self.subset.flip_x qtbot.mouseClick(self.trans.flip_x_box, QtCore.Qt.LeftButton) assert self.trans.flip_x_box.isChecked() assert self.image_set.flip_x assert self.subset.flip_x qtbot.mouseClick(self.trans.flip_x_box, QtCore.Qt.LeftButton) assert not self.trans.flip_x_box.isChecked() assert not self.image_set.flip_x assert not self.subset.flip_x def test_flip_y_checked(self, qtbot): qtbot.add_widget(self.trans) self.trans.show() assert not self.trans.flip_y_box.isChecked() assert not self.image_set.flip_y assert not self.subset.flip_y qtbot.mouseClick(self.trans.flip_y_box, QtCore.Qt.LeftButton) assert self.trans.flip_y_box.isChecked() assert self.image_set.flip_y assert self.subset.flip_y qtbot.mouseClick(self.trans.flip_y_box, QtCore.Qt.LeftButton) assert not self.trans.flip_y_box.isChecked() assert not self.image_set.flip_y assert not self.subset.flip_y def test_swap_xy_checked(self, qtbot): qtbot.add_widget(self.trans) self.trans.show() assert not self.trans.swap_xy_box.isChecked() assert not self.image_set.swap_xy assert not self.subset.swap_xy qtbot.mouseClick(self.trans.swap_xy_box, QtCore.Qt.LeftButton) assert self.trans.swap_xy_box.isChecked() assert self.image_set.swap_xy assert self.subset.swap_xy qtbot.mouseClick(self.trans.swap_xy_box, QtCore.Qt.LeftButton) assert not self.trans.swap_xy_box.isChecked() assert not self.image_set.swap_xy assert not self.subset.swap_xy
class TestROIHistogramModel(object): image_set = PDSSpectImageSet([FILE_1, FILE_3]) @pytest.fixture() def test_model(self): reset_image_set(self.image_set) return roi_histogram.ROIHistogramModel(self.image_set) def test_image_index(self, test_model): assert test_model.image_index == -1 test_model.image_index = 0 assert test_model.image_index == -1 test_model.image_index = 1 assert test_model.image_index == 1 def test_compare_data(self, test_model): assert not test_model.compare_data test_model.image_index = 1 assert test_model.compare_data def test_xdata(self, test_model): coords = np.array([[42, 24]]) self.image_set.add_coords_to_roi_data_with_color(coords, 'red') test_data = test_model.xdata('red')[0] assert round(test_data, 4) == round(1163.19384766, 4) def test_ydata(self, test_model): coords = np.array([[42, 24]]) self.image_set.add_coords_to_roi_data_with_color(coords, 'red') assert not test_model.compare_data with pytest.raises(RuntimeError): test_model.ydata('red') test_model.image_index = 1 test_data = test_model.ydata('red')[0] assert round(test_data, 4) == round(24.0, 4) def test_xlim(self, test_model): assert test_model.xlim[0] == 0 assert round(test_model.xlim[1], 4) == 2959.6763 def test_ylim(self, test_model): assert not test_model.compare_data with pytest.raises(RuntimeError): test_model.ydata('red') test_model.image_index = 1 assert test_model.ylim[0] == 22.0 assert test_model.ylim[1] == 115.0
class TestPencil(object): image_set = PDSSpectImageSet([FILE_1]) view_canvas = PDSImageViewCanvas() @pytest.fixture def pencil(self): self.view_canvas = PDSImageViewCanvas() return Pencil(self.image_set, self.view_canvas) def test_start_ROI(self, pencil): assert not pencil._current_path pencil.start_ROI(3.5, 1.5) assert isinstance(pencil._current_path[0], basic.Point) assert pencil._current_path[0] in self.view_canvas.objects assert pencil._current_path[0].x == 4 assert pencil._current_path[0].y == 2 def test_add_point(self, pencil): pencil.start_ROI(3.5, 1.5) pencil._add_point(4.5, 6.5) assert len(pencil._current_path) == 2 assert pencil._current_path[1].x == 5 assert pencil._current_path[1].y == 7 assert pencil._current_path[1] in self.view_canvas.objects def test_move_delta(self, pencil): pencil.start_ROI(3.5, 1.5) pencil._add_point(4.5, 6.5) assert pencil._current_path[0].x == 4 assert pencil._current_path[0].y == 2 assert pencil._current_path[1].x == 5 assert pencil._current_path[1].y == 7 pencil.move_delta(-1, 3) assert pencil._current_path[0].x == 3 assert pencil._current_path[0].y == 5 assert pencil._current_path[1].x == 4 assert pencil._current_path[1].y == 10 def test_stop_ROI(self, pencil): pencil.start_ROI(3.5, 1.5) pencil._add_point(4.5, 6.5) assert pencil._current_path[0] in self.view_canvas.objects assert pencil._current_path[1] in self.view_canvas.objects test_coords = pencil.stop_ROI(0, 0) assert pencil._current_path[0] not in self.view_canvas.objects assert pencil._current_path[1] not in self.view_canvas.objects assert np.array_equal(test_coords, np.array([[2, 4], [7, 5]]))
class TestRectangle(object): image_set = PDSSpectImageSet([FILE_1]) view_canvas = PDSImageViewCanvas() @pytest.fixture def rect(self): self.view_canvas = PDSImageViewCanvas() return Rectangle(self.image_set, self.view_canvas) def test_start_ROI(self, rect): assert rect._current_path is None rect.start_ROI(2.5, 3.5) assert isinstance(rect._current_path, basic.Rectangle) assert rect._current_path.x1 == 2.5 assert rect._current_path.y1 == 3.5 assert rect._current_path.x2 == 3.5 assert rect._current_path.y2 == 4.5 assert rect._current_path is not None assert rect._current_path in self.view_canvas.objects def test_extend_ROI(self, rect): rect.start_ROI(2.5, 3.5) rect.extend_ROI(2.5, 3.5) assert rect._current_path.x2 == 3.5 assert rect._current_path.y2 == 4.5 rect.extend_ROI(3.5, 3.5) assert rect._current_path.x2 == 4.5 assert rect._current_path.y2 == 4.5 rect.extend_ROI(3.5, 4.5) assert rect._current_path.x2 == 4.5 assert rect._current_path.y2 == 5.5 rect.extend_ROI(1.5, 3.5) assert rect._current_path.x2 == 1.5 assert rect._current_path.y2 == 4.5 rect.extend_ROI(1.5, 2.5) assert rect._current_path.x2 == 1.5 assert rect._current_path.y2 == 2.5 def test_stop_ROI(self, rect): rect.start_ROI(2.5, 3.5) assert rect._current_path in self.view_canvas.objects rect.extend_ROI(3.5, 4.5) rect.stop_ROI(3.5, 4.5) assert rect._current_path not in self.view_canvas.objects assert rect.get_data_points() == [(2.5, 3.5), (4.5, 3.5), (4.5, 5.5), (2.5, 5.5)]
class TestROIHistogramController(object): image_set = PDSSpectImageSet([FILE_1, FILE_3]) model = roi_histogram.ROIHistogramModel(image_set) @pytest.fixture() def test_controller(self): reset_image_set(self.image_set) self.model = roi_histogram.ROIHistogramModel(self.image_set) return roi_histogram.ROIHistogramController(self.model, None) def test_set_image_index(self, test_controller): assert self.model.image_index == -1 test_controller.set_image_index(0) assert self.model.image_index == -1 test_controller.set_image_index(1) assert self.model.image_index == 1
class TestPDSSpectViewWidget(object): image_set = PDSSpectImageSet(TEST_FILES) @pytest.fixture def view_widget(self): self.image_set = PDSSpectImageSet(TEST_FILES) return PDSSpectViewWidget(self.image_set) def test_init(self, view_widget): assert view_widget.image_set == self.image_set spect_view = view_widget.spect_views[0] assert view_widget.main_layout.itemAt(0).widget() == spect_view def test_create_spect_view(self, view_widget): subset = SubPDSSpectImageSet(self.image_set) spect_view = view_widget.create_spect_view(subset) spect_view == view_widget.spect_views[1] assert view_widget.main_layout.itemAt(1).widget() == spect_view
class TestROIPlotController(object): image_set = PDSSpectImageSet([FILE_1, FILE_3]) model = roi_plot.ROIPlotModel(image_set) @pytest.fixture() def test_controller(self): reset_image_set(self.image_set) self.model = roi_plot.ROIPlotModel(self.image_set) return roi_plot.ROIPlotController(self.model, None) def test_color_state_changed(self, test_controller): assert self.model.selected_colors == [] test_controller.color_state_changed('red') assert self.model.selected_colors == ['red'] test_controller.color_state_changed('red') assert self.model.selected_colors == [] def test_select_color(self, test_controller): assert self.model.selected_colors == [] test_controller.select_color('red') assert self.model.selected_colors == ['red'] def test_remove_color(self, test_controller): assert self.model.selected_colors == [] self.model.selected_colors = ['red'] test_controller.remove_color('red') assert self.model.selected_colors == [] def test_set_view_index(self, test_controller): assert not self.model.has_multiple_views assert self.model._view_index == 0 assert self.model.view_index == -1 self.image_set.create_subset() assert self.model.has_multiple_views assert self.model.view_index == 0 test_controller.set_view_index(1) assert self.model.view_index == 1 assert self.model._view_index == 1 self.model.image_index = 1 test_controller.set_view_index(0) assert self.model.view_index == 0 assert self.model._view_index == 0 assert self.model.image_index == -1
class TestSetWavelengthModel(object): image_set = PDSSpectImageSet([FILE_1, FILE_3]) @pytest.fixture def model(self): self.image_set.unit = 'nm' return SetWavelengthModel(self.image_set) def test_accepted_units(self, model): assert model.accepted_units == ['nm', 'um', 'AA'] def test_current_image_index(self, model): assert model.current_image_index == self.image_set.current_image_index model.current_image_index = 1 assert model.current_image_index == 1 assert model._current_image_index == 1 assert model.current_image_index != self.image_set.current_image_index def test_current_image(self, model): assert model.current_image == self.image_set.current_image model.current_image_index = 1 assert model.current_image == self.image_set.images[1] def test_unit(self, model): assert model.unit == self.image_set.unit model.unit = 'um' assert model.unit == 'um' assert self.image_set.unit == 'um' def test_unit_index(self, model): assert model.unit == 'nm' assert model.unit_index == 0 model.unit = 'um' assert model.unit == 'um' assert model.unit_index == 1 model.unit = 'AA' assert model.unit == 'AA' assert model.unit_index == 2 model.unit = 'nm' assert model.unit == 'nm' assert model.unit_index == 0
class TestSubPDSSpectImageSet(object): image_set = PDSSpectImageSet(TEST_FILES) subset = SubPDSSpectImageSet(image_set) def test_init(self): subset = self.subset assert subset.parent_set == self.image_set assert not subset._views assert subset.current_image_index == self.image_set.current_image_index assert subset.current_color_index == self.image_set.current_color_index assert subset.zoom == self.image_set.zoom assert subset.center == self.image_set.center assert subset.alpha == self.image_set.alpha assert not subset._roi_data.any() assert subset.selection_index == self.image_set.selection_index assert subset.flip_x == self.image_set.flip_x assert subset.flip_y == self.image_set.flip_y assert subset.swap_xy == self.image_set.swap_xy assert subset.shape == self.image_set.shape assert subset.images == self.image_set.images assert not subset._simultaneous_roi
class TestPanViewWidget(object): image_set = PDSSpectImageSet([FILE_1]) pan = PanView(image_set) @pytest.fixture def pan_widget(self): reset_image_set(self.image_set) self.pan = PanView(self.image_set) return PanViewWidget(self.pan, None) def test_init(self, pan_widget): assert len(pan_widget.pans) == 1 assert pan_widget.pans[0] == self.pan assert pan_widget.main_layout.itemAt(0).widget() == self.pan def test_add_pan(self, pan_widget): subset = SubPDSSpectImageSet(self.image_set) pan2 = PanView(subset) pan_widget.add_pan(pan2) assert pan_widget.pans[1] == pan2 assert pan_widget.main_layout.itemAt(1).widget() == pan2
class TestBasic(object): image_set = PDSSpectImageSet(TEST_FILES) view_canvas = PDSImageViewCanvas() view_canvas.set_image(image_set.current_image) basic_widget = BasicWidget(image_set, view_canvas) @pytest.fixture def basic(self, qtbot): reset_image_set(self.image_set) self.view_canvas = PDSImageViewCanvas() self.view_canvas.set_image(self.image_set.current_image) self.basic_widget = BasicWidget(self.image_set, self.view_canvas) qtbot.add_widget(self.basic_widget) return self.basic_widget.basics[0] def test_change_image1(self, basic): original_cuts = basic.histogram.cuts new_cuts = original_cuts[0] + 10, original_cuts[1] + 20 first_image = self.image_set.current_image basic.histogram._cut_low = new_cuts[0] basic.histogram._cut_high = new_cuts[1] basic.change_image(1) assert self.image_set.current_image_index == 1 assert self.image_set.current_image is not first_image basic.change_image(0) assert self.image_set.current_image_index == 0 assert self.image_set.current_image is first_image assert self.image_set.current_image.cuts == new_cuts def test_change_image2(self, basic): subset = SubPDSSpectImageSet(self.image_set) basic.change_image(1) self.basic_widget.add_basic(subset, self.view_canvas) basic2 = self.basic_widget.basics[1] assert basic.histogram.connected_models == [] assert basic2.histogram.connected_models == [] basic.change_image(0) assert basic.histogram.connected_models == [basic2.histogram] assert basic2.histogram.connected_models == [basic.histogram]
def view_widget(self): self.image_set = PDSSpectImageSet(TEST_FILES) return PDSSpectViewWidget(self.image_set)
class TestPanView(object): image_set = PDSSpectImageSet([FILE_1]) @pytest.fixture def view(self, qtbot): reset_image_set(self.image_set) view = PanView(self.image_set) view.show() qtbot.add_widget(view) return view def test_is_erasing(self, view): assert not view.is_erasing self.image_set.current_color_index = 14 assert view.is_erasing self.image_set.current_color_index = 0 assert not view.is_erasing def test_set_data(self, view): assert np.array_equal(view.view_canvas.get_image().get_data(), self.image_set.pan_data) self.image_set._zoom = 2 assert not np.array_equal(view.view_canvas.get_image().get_data(), self.image_set.pan_data) view.set_data() assert np.array_equal(view.view_canvas.get_image().get_data(), self.image_set.pan_data) self.image_set._zoom = 1 view.set_data() assert np.array_equal(view.view_canvas.get_image().get_data(), self.image_set.pan_data) def test_set_roi_data(self, view): assert np.array_equal(self.image_set._maskrgb.get_data(), self.image_set.pan_roi_data) self.image_set._zoom = 2 assert not np.array_equal(self.image_set._maskrgb.get_data(), self.image_set.pan_roi_data) view.set_data() assert np.array_equal(self.image_set._maskrgb.get_data(), self.image_set.pan_roi_data) self.image_set._zoom = 1 view.set_data() assert np.array_equal(self.image_set._maskrgb.get_data(), self.image_set.pan_roi_data) @pytest.mark.parametrize('pre_x, pre_y, expected_x, expected_y', [ (512, 512, 512, 512), (-.5, -.5, -.5, -.5), (1023, -.5, 1023, -.5), (-.5, 1023, -.5, 1023), (1023, 1023, 1023, 1023), (-.6, -.6, -.5, -.5), (1024, -.6, 1023, -.5), (-.6, 1024, -.5, 1023), (1024, 1024, 1023, 1023), ]) def test_make_x_y_in_pan(self, pre_x, pre_y, expected_x, expected_y, view): post_x, post_y = view._make_x_y_in_pan(pre_x, pre_y) assert post_x == expected_x assert post_y == expected_y def test_start_ROI(self, view): assert not view._making_roi assert view._current_roi is None view.start_ROI(view.view_canvas, None, 512, 512) assert view._making_roi assert view._current_roi is not None assert self.image_set.selection_type == 'filled rectangle' assert isinstance(view._current_roi, Rectangle) view._making_roi = False view._current_roi = None self.image_set._selection_index = 1 assert self.image_set.selection_type == 'filled polygon' view.start_ROI(view.view_canvas, None, 512, 512) assert view._making_roi assert view._current_roi is not None assert self.image_set.selection_type == 'filled polygon' assert isinstance(view._current_roi, Polygon) view._making_roi = False view._current_roi = None self.image_set._selection_index = 2 assert self.image_set.selection_type == 'pencil' view.start_ROI(view.view_canvas, None, 512, 512) # Pencil ROIs stop directly after starting assert not view._making_roi assert view._current_roi is None assert self.image_set.selection_type == 'pencil' self.image_set._selection_index = 0 assert self.image_set.selection_type == 'filled rectangle' view.start_ROI(view.view_canvas, None, 512, 512) assert view._making_roi assert view._current_roi is not None assert self.image_set.selection_type == 'filled rectangle' assert isinstance(view._current_roi, Rectangle) view._making_roi = False view._current_roi = None def test_continue_ROI(self, view): assert not view._making_roi assert view._current_roi is None view.start_ROI(view.view_canvas, None, 512, 512) assert view._making_roi assert view._current_roi is not None view.continue_ROI(None, None, 514, 514) assert not view._making_roi assert view._current_roi is None self.image_set._selection_index = 1 assert self.image_set.selection_type == 'filled polygon' view.start_ROI(view.view_canvas, None, 512, 512) assert view._making_roi assert view._current_roi is not None # Make sure to continue ROI even when start_ROI is called view.start_ROI(None, None, 514, 514) assert view._making_roi assert view._current_roi is not None view._making_roi = False view._current_roi = None def test_extend_ROI(self, view): assert not view._making_roi assert view._current_roi is None view.start_ROI(view.view_canvas, None, 512, 512) assert view._making_roi assert view._current_roi is not None view.extend_ROI(None, None, 514, 514) assert view._making_roi assert view._current_roi is not None def test_stop_ROI(self, view): assert not view._making_roi assert view._current_roi is None view.start_ROI(view.view_canvas, None, 512, 512) assert view._making_roi assert view._current_roi is not None view.stop_ROI(view.view_canvas, None, 512, 512) assert not view._making_roi assert view._current_roi is None assert self.image_set.get_coordinates_of_color('red') == ([513], [513]) self.image_set.current_color_index = 14 assert self.image_set.color == 'eraser' view.start_ROI(view.view_canvas, None, 512, 512) assert view._making_roi assert view._current_roi is not None view.stop_ROI(view.view_canvas, None, 512, 512) assert not view._making_roi assert view._current_roi is None assert np.array_equal(self.image_set.get_coordinates_of_color('red'), (np.array([]), np.array([])))
class TestPanViewController(object): image_set = PDSSpectImageSet([FILE_1]) controller = PanViewController(image_set, None) default_roi_data = image_set._roi_data.copy() @pytest.fixture def test_set(self): yield self.image_set self.image_set._roi_data = self.default_roi_data self.image_set._alpha = 1 self.image_set._subsets = [] def test_get_parent_set(self, test_set): subset = test_set.create_subset() assert self.controller._get_parent_set() == test_set controller2 = PanViewController(subset, None) assert controller2._get_parent_set() == test_set def test_add_ROI(self, test_set): subset = test_set.create_subset() assert test_set.current_color_index == 0 assert test_set.color == 'red' coords = np.array([[42, 42]]) rows, cols = np.column_stack(coords) assert np.array_equal(test_set._roi_data[rows, cols], np.array([[0.0, 0.0, 0.0, 0.0]])) assert np.array_equal(subset._roi_data[rows, cols], np.array([[0.0, 0.0, 0.0, 0.0]])) self.image_set.alpha = 1 self.controller.add_ROI(coords) assert np.array_equal(self.image_set._roi_data[rows, cols], np.array([[255.0, 0.0, 0.0, 255.]])) assert np.array_equal(subset._roi_data[rows, cols], np.array([[0.0, 0.0, 0.0, 0.0]])) self.image_set.alpha = .75 self.image_set.current_color_index = 1 self.controller.add_ROI(coords) assert np.array_equal(self.image_set._roi_data[rows, cols], np.array([[165.0, 42.0, 42.0, 191.25]])) assert np.array_equal(subset._roi_data[rows, cols], np.array([[0.0, 0.0, 0.0, 0.0]])) self.image_set.alpha = .25 self.image_set.current_color_index = 13 self.controller.add_ROI(coords) assert np.array_equal(self.image_set._roi_data[rows, cols], np.array([[160.0, 32.0, 240.0, 63.75]])) assert np.array_equal(subset._roi_data[rows, cols], np.array([[0.0, 0.0, 0.0, 0.0]])) self.image_set.alpha = 1 self.image_set.current_color_index = 0 test_set.simultaneous_roi = True self.controller.add_ROI(coords) assert np.array_equal(self.image_set._roi_data[rows, cols], np.array([[255.0, 0.0, 0.0, 255.0]])) assert np.array_equal(subset._roi_data[rows, cols], np.array([[255.0, 0.0, 0.0, 255.0]])) self.image_set.current_color_index = 1 test_set.simultaneous_roi = False self.controller.add_ROI(coords) assert np.array_equal(self.image_set._roi_data[rows, cols], np.array([[165.0, 42.0, 42.0, 255.0]])) assert np.array_equal(subset._roi_data[rows, cols], np.array([[255.0, 0.0, 0.0, 255.0]])) def test_erase_ROI(self, test_set): subset = test_set.create_subset() coords = np.array([[42, 42]]) rows, cols = np.column_stack(coords) test_set.add_coords_to_roi_data_with_color(coords, 'red') subset.add_coords_to_roi_data_with_color(coords, 'red') assert np.array_equal(test_set._roi_data[rows, cols], np.array([[255.0, 0.0, 0.0, 255.0]])) assert np.array_equal(subset._roi_data[rows, cols], np.array([[255.0, 0.0, 0.0, 255.0]])) self.controller.erase_ROI(coords) assert np.array_equal(test_set._roi_data[rows, cols], np.array([[0.0, 0.0, 0.0, 0.0]])) assert np.array_equal(subset._roi_data[rows, cols], np.array([[255.0, 0.0, 0.0, 255.0]])) test_set.add_coords_to_roi_data_with_color(coords, 'brown') assert np.array_equal(test_set._roi_data[rows, cols], np.array([[165.0, 42.0, 42.0, 255.0]])) self.controller.erase_ROI(coords) assert np.array_equal(test_set._roi_data[rows, cols], np.array([[0.0, 0.0, 0.0, 0.0]])) test_set.simultaneous_roi = True test_set.add_coords_to_roi_data_with_color(coords, 'red') subset.add_coords_to_roi_data_with_color(coords, 'red') self.controller.erase_ROI(coords) assert np.array_equal(test_set._roi_data[rows, cols], np.array([[0.0, 0.0, 0.0, 0.0]])) assert np.array_equal(subset._roi_data[rows, cols], np.array([[0.0, 0.0, 0.0, 0.0]]))
class TestRectangle(object): image_set = PDSSpectImageSet([FILE_1]) view_canvas = PDSImageViewCanvas() @pytest.fixture def rect(self): reset_image_set(self.image_set) self.view_canvas = PDSImageViewCanvas() return Rectangle(self.image_set, self.view_canvas) def test_start_ROI(self, rect): assert rect._current_path is None rect.start_ROI(2.5, 3.5) assert isinstance(rect._current_path, basic.Rectangle) assert rect._current_path.x1 == 2.5 assert rect._current_path.y1 == 3.5 assert rect._current_path.x2 == 3.5 assert rect._current_path.y2 == 4.5 assert rect._current_path is not None assert rect._current_path in self.view_canvas.objects @pytest.mark.parametrize('point, expected', [(1, (1, 3)), (2, (2, 3)), (3, (2, 4)), (4, (2, 4))]) def test_extend_point(self, point, expected, rect): anchor_point = 2 edge = 4 extended_point = rect._extend_point(point, anchor_point, edge) assert expected == extended_point def test_extend_ROI(self, rect): rect.start_ROI(2.5, 3.5) rect.extend_ROI(2.5, 3.5) assert rect._current_path.x1 == 2.5 assert rect._current_path.x2 == 3.5 assert rect._current_path.y1 == 3.5 assert rect._current_path.y2 == 4.5 rect.extend_ROI(3.5, 3.5) assert rect._current_path.x1 == 2.5 assert rect._current_path.x2 == 4.5 assert rect._current_path.y1 == 3.5 assert rect._current_path.y2 == 4.5 rect.extend_ROI(3.5, 4.5) assert rect._current_path.x1 == 2.5 assert rect._current_path.x2 == 4.5 assert rect._current_path.y1 == 3.5 assert rect._current_path.y2 == 5.5 rect.extend_ROI(1.5, 3.5) assert rect._current_path.x1 == 1.5 assert rect._current_path.x2 == 3.5 assert rect._current_path.y1 == 3.5 assert rect._current_path.y2 == 4.5 rect.extend_ROI(1.5, 2.5) assert rect._current_path.x1 == 1.5 assert rect._current_path.x2 == 3.5 assert rect._current_path.y1 == 2.5 assert rect._current_path.y2 == 4.5 def test_stop_ROI(self, rect): rect.start_ROI(2.5, 3.5) assert rect._current_path in self.view_canvas.objects rect.extend_ROI(3.5, 4.5) rect.stop_ROI(3.5, 4.5) assert rect._current_path not in self.view_canvas.objects assert rect.get_data_points() == [(2.5, 3.5), (4.5, 3.5), (4.5, 5.5), (2.5, 5.5)]
class TestPencil(object): image_set = PDSSpectImageSet([FILE_1]) view_canvas = PDSImageViewCanvas() @pytest.fixture def pencil(self): self.image_set.zoom = 1.0 self.view_canvas = PDSImageViewCanvas() return Pencil(self.image_set, self.view_canvas) def test_start_ROI(self, pencil): assert not pencil._current_path pencil.start_ROI(3.5, 1.5) assert isinstance(pencil._current_path[0], basic.Point) assert pencil._current_path[0] in self.view_canvas.objects assert pencil._current_path[0].x == 4 assert pencil._current_path[0].y == 2 def test_add_point(self, pencil): pencil.start_ROI(3.5, 1.5) pencil._add_point(4.5, 6.5) assert len(pencil._current_path) == 2 assert pencil._current_path[1].x == 5 assert pencil._current_path[1].y == 7 assert pencil._current_path[1] in self.view_canvas.objects def test_move_delta(self, pencil): pencil.start_ROI(3.5, 1.5) pencil._add_point(4.5, 6.5) assert pencil._current_path[0].x == 4 assert pencil._current_path[0].y == 2 assert pencil._current_path[1].x == 5 assert pencil._current_path[1].y == 7 pencil.move_delta(-1, 3) assert pencil._current_path[0].x == 3 assert pencil._current_path[0].y == 5 assert pencil._current_path[1].x == 4 assert pencil._current_path[1].y == 10 def test_fix_coordinate(self, pencil): assert pencil._fix_coordinate(1.7) == 2 assert pencil._fix_coordinate(2.0) == 2 assert pencil._fix_coordinate(2.3) == 2 def test_stop_ROI(self, pencil): pencil.start_ROI(3.5, 1.5) pencil._add_point(4.5, 6.5) assert pencil._current_path[0] in self.view_canvas.objects assert pencil._current_path[1] in self.view_canvas.objects test_coords = pencil.stop_ROI(0, 0) assert pencil._current_path[0] not in self.view_canvas.objects assert pencil._current_path[1] not in self.view_canvas.objects # The order may be different due to using set try: assert np.array_equal(test_coords, np.array([[2, 4], [7, 5]])) except AssertionError: assert np.array_equal(test_coords, np.array([[7, 5], [2, 4]])) self.image_set.zoom = 2.0 x, y = self.image_set.center self.image_set.center = (x + 5.2, y + 5.7) pencil.start_ROI(3.5, 1.5) pencil._add_point(4.5, 6.5) test_coords = pencil.stop_ROI(0, 0) # The order may be different due to using set try: assert np.array_equal(test_coords, np.array([[269, 266], [264, 265]])) except AssertionError: assert np.array_equal(test_coords, np.array([[264, 265], [269, 266]]))
class TestROIPlotWidget(object): image_set = PDSSpectImageSet([FILE_1]) model = roi_plot.ROIPlotModel(image_set) @pytest.fixture def widget(self, qtbot): reset_image_set(self.image_set) self.model = roi_plot.ROIPlotModel(self.image_set) widget = roi_plot.ROIPlotWidget(self.model) widget.show() qtbot.add_widget(widget) return widget def test_init(self, widget): assert widget in self.model._views for color in self.model.image_set.colors[:-1]: assert hasattr(widget, color + '_checkbox') assert not hasattr(widget, 'eraser_checkbox') def test_create_color_checkbox(self, widget): assert not hasattr(widget, 'foo_checkbox') widget.create_color_checkbox('foo') assert hasattr(widget, 'foo_checkbox') def test_check_color(self, qtbot, widget): assert self.model.selected_colors == [] widget.check_color('red') assert self.model.selected_colors == ['red'] widget.check_color('red') assert self.model.selected_colors == [] assert not widget.red_checkbox.isChecked() widget.red_checkbox.nextCheckState() assert widget.red_checkbox.isChecked() assert self.model.selected_colors == ['red'] assert widget.red_checkbox.isChecked() widget.red_checkbox.nextCheckState() assert self.model.selected_colors == [] assert not widget.red_checkbox.isChecked() def test_add_view(self, widget): self.image_set.create_subset() assert widget.view_boxes_layout.count() == 0 widget.add_view() assert widget.view_boxes_layout.count() == 2 box1 = widget.view_boxes_layout.itemAt(0).widget() assert box1.isChecked() box2 = widget.view_boxes_layout.itemAt(1).widget() assert not box2.isChecked() self.image_set.create_subset() self.model.view_index = 1 widget.add_view(2) assert self.model.view_index == 1 assert widget.view_boxes_layout.count() == 3 assert not box1.isChecked() assert box2.isChecked() box3 = widget.view_boxes_layout.itemAt(2).widget() assert not box3.isChecked() def test_check_view_box(self, widget): self.image_set.create_subset() widget.add_view() self.image_set.create_subset() widget.add_view() box1 = widget.view_boxes_layout.itemAt(0).widget() box2 = widget.view_boxes_layout.itemAt(1).widget() box3 = widget.view_boxes_layout.itemAt(2).widget() assert box1.isChecked() assert not box2.isChecked() assert not box3.isChecked() box2.setChecked(True) widget.check_view_checkbox(box2) assert not box1.isChecked() assert box2.isChecked() assert not box3.isChecked() box3.setChecked(True) widget.check_view_checkbox(box3) assert not box1.isChecked() assert not box2.isChecked() assert box3.isChecked() box1.setChecked(True) widget.check_view_checkbox(box1) assert box1.isChecked() assert not box2.isChecked() assert not box3.isChecked()
class TestSelectionController(object): image_set = PDSSpectImageSet(TEST_FILES) subset = image_set.create_subset() @pytest.fixture def controller(self): reset_image_set(self.image_set) self.subset = self.image_set.create_subset() return SelectionController(self.image_set, None) def test_change_current_color_index(self, controller): assert self.image_set.current_color_index == 0 assert self.subset.current_color_index == 0 controller.change_current_color_index(1) assert self.image_set.current_color_index == 1 assert self.subset.current_color_index == 1 controller.change_current_color_index(0) assert self.image_set.current_color_index == 0 assert self.subset.current_color_index == 0 def test_selection_index(self, controller): assert self.image_set.selection_index == 0 assert self.subset.selection_index == 0 controller.change_selection_index(1) assert self.image_set.selection_index == 1 assert self.subset.selection_index == 1 controller.change_selection_index(0) assert self.image_set.selection_index == 0 assert self.subset.selection_index == 0 def test_change_alpha(self, controller): assert self.image_set.alpha == 1.0 assert self.subset.alpha == 1.0 controller.change_alpha(50) assert self.image_set.alpha == 0.5 assert self.subset.alpha == 0.5 controller.change_alpha(100) assert self.image_set.alpha == 1.0 assert self.subset.alpha == 1.0 def test_clear_current_color(self, controller): self.image_set._roi_data[4, 2] = [255.0, 0.0, 0.0, 255.] self.subset._roi_data[4, 2] = [255.0, 0.0, 0.0, 255.] controller.clear_current_color() assert np.array_equal(self.image_set._roi_data[4, 2], [0, 0, 0, 0]) assert np.array_equal(self.subset._roi_data[4, 2], [0, 0, 0, 0]) def test_clear_all(self, controller): self.image_set._roi_data[4, 2] = [255.0, 0.0, 0.0, 255.] self.image_set._roi_data[2, 4] = [165.0, 42.0, 42.0, 255.] self.subset._roi_data[4, 2] = [255.0, 0.0, 0.0, 255.] self.subset._roi_data[2, 4] = [165.0, 42.0, 42.0, 255.] controller.clear_all() assert np.array_equal(self.image_set._roi_data[4, 2], [0, 0, 0, 0]) assert np.array_equal(self.image_set._roi_data[2, 4], [0, 0, 0, 0]) assert np.array_equal(self.subset._roi_data[4, 2], [0, 0, 0, 0]) assert np.array_equal(self.subset._roi_data[2, 4], [0, 0, 0, 0]) def test_add_ROI(self, controller): coords = np.array([[4, 2]]) controller.add_ROI(coords, 'red') assert np.array_equal(self.image_set._roi_data[4, 2], [255.0, 0.0, 0.0, 255.]) assert not np.array_equal(self.subset._roi_data[4, 2], [255.0, 0.0, 0.0, 255.]) controller.add_ROI(coords, 'red', self.subset) assert np.array_equal(self.subset._roi_data[4, 2], [255.0, 0.0, 0.0, 255.]) def test_set_simultaneous_roi(self, controller): assert not self.image_set.simultaneous_roi controller.set_simultaneous_roi(True) assert self.image_set.simultaneous_roi controller.set_simultaneous_roi(False) assert not self.image_set.simultaneous_roi
class TestSelection(object): image_set = PDSSpectImageSet([FILE_1]) subset = image_set.create_subset() roi_coords = np.array([ [512, 509], [512, 510], [513, 509], [513, 510], ]) @pytest.fixture def selection(self, qtbot): reset_image_set(self.image_set) self.subset = self.image_set.create_subset() selection = Selection(self.image_set) selection.show() qtbot.add_widget(selection) return selection def test_change_color(self, qtbot, selection): assert self.image_set.current_color_index == 0 assert self.subset.current_color_index == 0 selection.change_color(1) assert self.image_set.current_color_index == 1 assert self.subset.current_color_index == 1 selection.change_color(0) assert self.image_set.current_color_index == 0 assert self.subset.current_color_index == 0 selection.color_menu.setCurrentIndex(1) assert self.image_set.current_color_index == 1 assert self.subset.current_color_index == 1 selection.color_menu.setCurrentIndex(0) assert self.image_set.current_color_index == 0 assert self.subset.current_color_index == 0 def test_change_selection_type(self, qtbot, selection): assert self.image_set.selection_index == 0 assert self.subset.selection_index == 0 selection.change_selection_type(1) assert self.image_set.selection_index == 1 assert self.subset.selection_index == 1 selection.change_selection_type(0) assert self.image_set.selection_index == 0 assert self.subset.selection_index == 0 selection.selection_menu.setCurrentIndex(1) assert self.image_set.selection_index == 1 assert self.subset.selection_index == 1 selection.selection_menu.setCurrentIndex(0) assert self.image_set.selection_index == 0 assert self.subset.selection_index == 0 def test_change_alpah(self, qtbot, selection): assert self.image_set.alpha == 1.0 assert self.subset.alpha == 1.0 selection.change_alpha(50) assert self.image_set.alpha == .5 assert self.subset.alpha == .5 selection.change_alpha(100) assert self.image_set.alpha == 1.0 assert self.subset.alpha == 1.0 selection.opacity_slider.setValue(50) assert self.image_set.alpha == .5 assert self.subset.alpha == .5 selection.opacity_slider.setValue(100) assert self.image_set.alpha == 1.0 assert self.subset.alpha == 1.0 def test_clear_current_color(self, qtbot, selection): self.image_set._roi_data[4, 2] = [255.0, 0.0, 0.0, 255.] selection.clear_current_color() assert np.array_equal(self.image_set._roi_data[4, 2], [0, 0, 0, 0]) self.image_set._roi_data[4, 2] = [255.0, 0.0, 0.0, 255.] qtbot.mouseClick(selection.clear_current_color_btn, QtCore.Qt.LeftButton) assert np.array_equal(self.image_set._roi_data[4, 2], [0, 0, 0, 0]) def test_clear_all(self, qtbot, selection): self.image_set._roi_data[4, 2] = [255.0, 0.0, 0.0, 255.] self.image_set._roi_data[2, 4] = [165.0, 42.0, 42.0, 255.] selection.clear_all() assert np.array_equal(self.image_set._roi_data[4, 2], [0, 0, 0, 0]) assert np.array_equal(self.image_set._roi_data[2, 4], [0, 0, 0, 0]) self.image_set._roi_data[4, 2] = [255.0, 0.0, 0.0, 255.] self.image_set._roi_data[2, 4] = [165.0, 42.0, 42.0, 255.] qtbot.mouseClick(selection.clear_all_btn, QtCore.Qt.LeftButton) assert np.array_equal(self.image_set._roi_data[4, 2], [0, 0, 0, 0]) assert np.array_equal(self.image_set._roi_data[2, 4], [0, 0, 0, 0]) def test_export(self, selection): selection.controller.add_ROI(self.roi_coords, 'red') selection.controller.add_ROI(self.roi_coords, 'darkgreen', self.subset) rows, cols = np.column_stack(self.roi_coords) with make_temp_directory() as tmpdirname: save_file = os.path.join(tmpdirname, 'temp.npz') selection.export(save_file) np_file = np.load(save_file) assert np.array_equal(np.where(np_file['red'])[0], rows) assert np.array_equal(np.where(np_file['red'])[1], cols) assert np.array_equal(np.where(np_file['darkgreen2'])[0], rows) assert np.array_equal(np.where(np_file['darkgreen2'])[1], cols) assert np_file['files'] == FILE_1_NAME assert np_file['views'] == 2 def test_check_pdsspect_selection_is_file(self, selection): selection._check_pdsspect_selection_is_file('foo.npz') with pytest.raises(RuntimeError): selection._check_pdsspect_selection_is_file('foo.txt') def test_check_files_in_selection_file_compatible(self, selection): selection._check_files_in_selection_file_compatible([FILE_1]) with pytest.raises(RuntimeError): selection._check_files_in_selection_file_compatible(TEST_FILES) def test_check_shape_is_the_same(self, selection): fail_shape = (self.image_set.shape[0] - 1, self.image_set.shape[1] + 5) with pytest.raises(RuntimeError): selection._check_shape_is_the_same(fail_shape) def test_load_selections(self, selection): """Test loading when there are equal loaded views to current""" selection.load_selections([SAMPLE_ROI]) rows, cols = np.column_stack(self.roi_coords) for pixel in self.image_set._roi_data[rows, cols]: assert np.array_equal(pixel, [255.0, 0.0, 0.0, 255.]) for pixel in self.subset._roi_data[rows, cols]: assert np.array_equal(pixel, [0.0, 100.0, 0.0, 255.]) def test_load_selections2(self, selection): """Test when there is no multiple views""" self.image_set._subsets = [] selection.load_selections([SAMPLE_ROI]) rows, cols = np.column_stack(self.roi_coords) for pixel in self.image_set._roi_data[rows, cols]: assert np.array_equal(pixel, [255.0, 0.0, 0.0, 255.]) def test_load_selections3(self, selection): """Test when there are more current views""" self.image_set.create_subset() selection.load_selections([SAMPLE_ROI]) rows, cols = np.column_stack(self.roi_coords) for pixel in self.image_set._roi_data[rows, cols]: assert np.array_equal(pixel, [255.0, 0.0, 0.0, 255.]) for pixel in self.subset._roi_data[rows, cols]: assert np.array_equal(pixel, [0.0, 100.0, 0.0, 255.]) def test_select_simultaneous_roi(self, qtbot, selection): assert not self.image_set.simultaneous_roi selection.simultaneous_roi_box.setChecked(True) assert self.image_set.simultaneous_roi selection.simultaneous_roi_box.setChecked(False) assert not self.image_set.simultaneous_roi
class TestPolygon(object): image_set = PDSSpectImageSet([FILE_1]) view_canvas = PDSImageViewCanvas() shape1 = [(2.5, 5.5), (4.5, 3.5), (6.5, 5.5), (6.5, 2.5), (2.5, 2.5)] @pytest.fixture def poly(self): self.view_canvas = PDSImageViewCanvas() return Polygon(self.image_set, self.view_canvas) def test_top(self, poly): assert poly.top == self.image_set.current_image.shape[0] + 1.5 def test_right(self, poly): assert poly.right == self.image_set.current_image.shape[1] - 0.5 @pytest.mark.parametrize('point, expected', [ (-1, -0.5), (-0.5, -0.5), (-0.4, -0.5), (0, -0.5), (0.4, None), (0.5, None), (0.9, None), (1, 0.5), (1.1, 0.5), ]) def test_get_default_point_value(self, point, expected, poly): high_edege = 0.5 default_point_value = poly._get_default_point_value(point, high_edege) if expected is None: assert default_point_value is None else: assert default_point_value == expected def test_get_default_data_values(self, poly): assert poly._get_default_data_values(-1, -1) == (-0.5, -0.5) assert poly._get_default_data_values(0, 0) == (-0.5, -0.5) x, y = poly.right - 1, poly.top - 1 assert poly._get_default_data_values(x, y) == (None, None) x, y = poly.right, poly.top assert poly._get_default_data_values(x, y) == (None, None) x, y = poly.right + 0.5, poly.top + 0.5 assert poly._get_default_data_values(x, y) == (poly.right, poly.top) x, y = poly.right + 1, poly.top + 1 assert poly._get_default_data_values(x, y) == (poly.right, poly.top) @pytest.mark.parametrize('coordinate, expected', [ (2.3, 1.5), (1.7, 1.5), (2.5, 2.5), ]) def test_lock_coordinate_to_pixel(self, coordinate, expected, poly): assert poly._lock_coordinate_to_pixel(coordinate) == expected @pytest.mark.parametrize('x, y, expected_x, expected_y', [(0, 0, -.5, -.5), (1023, 1023, 1023.5, 1023.5), (2.3, 2.3, 1.5, 1.5), (1.7, 2.3, 1.5, 1.5), (1.7, 1.7, 1.5, 1.5), (2.3, 1.7, 1.5, 1.5), (2.5, 3.5, 2.5, 3.5)]) def test_lock_coords_to_pixel(self, x, y, expected_x, expected_y, poly): assert poly.lock_coords_to_pixel(x, y) == (expected_x, expected_y) def test_contains_arr(self, poly): poly.create_ROI(self.shape1) x1, y1, x2, y2 = poly.get_llur() x1, y1 = np.floor([x1, y1]).astype(int) x2, y2 = np.ceil([x2, y2]).astype(int) X, Y = np.mgrid[x1:x2, y1:y2] test_mask = np.array([ [False, False, False, False], [False, True, True, True], [False, True, True, False], [False, True, True, False], [False, True, True, True], ]) mask = poly.contains_arr(X, Y) assert np.array_equal(mask, test_mask) def test_move_by_delta(self, poly): poly.create_ROI(self.shape1) rect_points = poly.get_points() for rect_point, point in zip(rect_points, self.shape1): assert rect_point == point with poly._temporary_move_by_delta((2, 3)) as moved_rect: moved_points = moved_rect.get_points() for moved_point, point in zip(moved_points, self.shape1): assert moved_point[0] == point[0] + 2 assert moved_point[1] == point[1] + 3 rect_points = poly.get_points() for rect_point, point in zip(rect_points, self.shape1): assert rect_point == point def test_start_ROI(self, poly): assert poly._current_path is None poly.start_ROI(2.5, 3.5) assert isinstance(poly._current_path, basic.Path) assert poly._current_path in self.view_canvas.objects assert poly._current_path.get_points() == [(2.5, 3.5)] def test_current_ROI(self): poly = Polygon(self.image_set, self.view_canvas) poly.start_ROI(2.5, 3.5) poly._has_temp_point = True poly.continue_ROI(5.5, 4.5) assert poly._current_path.get_points() == [(5.5, 4.5), (2.5, 3.5)] assert not poly._has_temp_point def test_extend_ROI(self, poly): poly.start_ROI(2.5, 3.5) poly._has_temp_point = False poly.extend_ROI(5.5, 4.5) assert poly._current_path.get_points() == [(5.5, 4.5), (2.5, 3.5)] assert poly._has_temp_point poly.extend_ROI(6.5, 5.5) assert poly._current_path.get_points() == [(6.5, 5.5), (2.5, 3.5)] def test_stop_ROI(self, poly): poly.start_ROI(2.5, 3.5) poly.continue_ROI(2.5, 4.5) poly.continue_ROI(5.5, 3.5) poly.extend_ROI(3.5, 3.5) poly.stop_ROI(2.5, 3.5) assert poly._current_path not in self.view_canvas.objects assert poly._current_path.get_points() == [(5.5, 3.5), (2.5, 4.5), (2.5, 3.5)] assert poly.get_data_points() == [(5.5, 3.5), (2.5, 4.5), (2.5, 3.5)] poly.start_ROI(2.5, 3.5) poly.continue_ROI(2.5, 4.5) with pytest.warns(UserWarning): poly.stop_ROI(2.5, 3.5)
class TestPDSSpect(object): image_set = PDSSpectImageSet(TEST_FILES) @pytest.fixture def window(self, qtbot): self.image_set._subsets = [] window = PDSSpect(self.image_set) window.show() qtbot.add_widget(window) qtbot.add_widget(window.basic_window) qtbot.add_widget(window.pan_view) return window def test_init(self, qtbot, window): assert window.selection_window is None assert window.basic_window is not None assert window.transforms_window is None def test_image_sets(self, qtbot, window): assert window.image_sets == [self.image_set] subset = self.image_set.create_subset() assert window.image_sets == [self.image_set, subset] self.image_set.remove_subset(subset) assert window.image_sets == [self.image_set] def test_open_selection(self, qtbot, window): qtbot.mouseClick(window.selection_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.selection_window) assert window.selection_window is not None assert isinstance(window.selection_window, Selection) assert window.selection_window.isVisible() def test_open_basic(self, qtbot, window): window.basic_window.close() qtbot.mouseClick(window.basic_btn, QtCore.Qt.LeftButton) assert window.basic_window.isVisible() assert isinstance(window.basic_window, BasicWidget) def test_open_transforms(self, qtbot, window): assert window.transforms_window is None qtbot.mouseClick(window.transforms_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.transforms_window) assert window.transforms_window is not None assert window.transforms_window.isVisible() assert isinstance(window.transforms_window, Transforms) def test_open_roi_histogram(self, qtbot, window): assert window.roi_histogram_window is None qtbot.mouseClick(window.roi_histogram_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.roi_histogram_window) assert window.roi_histogram_window is not None assert window.roi_histogram_window.isVisible() assert isinstance(window.roi_histogram_window, ROIHistogramWidget) def test_open_roi_line_plot(self, qtbot, window): assert window.roi_line_plot_window is None qtbot.mouseClick(window.roi_line_plot_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.roi_line_plot_window) assert window.roi_line_plot_window is not None assert window.roi_line_plot_window.isVisible() assert isinstance(window.roi_line_plot_window, ROILinePlotWidget) def test_add_window(self, qtbot, window): qtbot.mouseClick(window.selection_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.selection_window) qtbot.mouseClick(window.roi_histogram_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.roi_histogram_window) qtbot.mouseClick(window.roi_line_plot_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.roi_line_plot_window) assert len(window.image_sets) == 1 assert len(window.basic_window.basics) == 1 assert len(window.pan_view.pans) == 1 qtbot.mouseClick(window.add_window_btn, QtCore.Qt.LeftButton) assert len(window.image_sets) == 2 assert len(window.basic_window.basics) == 2 assert len(window.pan_view.pans) == 2 def test_open_set_wavelengths(self, qtbot, window): assert window.set_wavelength_window is None qtbot.mouseClick(window.set_wavelengths_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.set_wavelength_window) assert window.set_wavelength_window is not None assert window.set_wavelength_window.isVisible() assert isinstance(window.set_wavelength_window, SetWavelengthWidget) def test_quit(self, qtbot, window): qtbot.mouseClick(window.transforms_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.transforms_window) qtbot.mouseClick(window.basic_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.basic_window) qtbot.mouseClick(window.selection_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.selection_window) qtbot.mouseClick(window.roi_histogram_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.roi_histogram_window) qtbot.mouseClick(window.roi_line_plot_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.roi_line_plot_window) qtbot.mouseClick(window.set_wavelengths_btn, QtCore.Qt.LeftButton) qtbot.add_widget(window.set_wavelength_window) assert window.transforms_window.isVisible() assert window.basic_window.isVisible() assert window.selection_window.isVisible() assert window.roi_histogram_window.isVisible() assert window.roi_line_plot_window.isVisible() assert window.set_wavelength_window.isVisible() assert window.isVisible() qtbot.mouseClick(window.quit_btn, QtCore.Qt.LeftButton) assert not window.transforms_window.isVisible() assert not window.basic_window.isVisible() assert not window.selection_window.isVisible() assert not window.roi_histogram_window.isVisible() assert not window.roi_line_plot_window.isVisible() assert not window.set_wavelength_window.isVisible() assert not window.pan_view.isVisible() assert not window.isVisible()
class TestPDSSpectView(object): image_set = PDSSpectImageSet(TEST_FILES) view = PDSSpectView(image_set) @pytest.fixture def test_set(self, qtbot): self.view.show() qtbot.add_widget(self.view) qtbot.add_widget(self.view.pan_view) yield self.image_set self.image_set._current_image_index = 0 self.image_set.images[0].cuts = (None, None) self.image_set.images[1].cuts = (None, None) self.view.set_image() self.image_set._flip_x = False self.image_set._flip_y = False self.image_set._swap_xy = False self.image_set.zoom = 1 def test_set_image(self, qtbot, test_set): view_image = self.view.view_canvas.get_image() assert view_image == test_set.current_image test_set._current_image_index += 1 self.view.set_image() assert not view_image == test_set.current_image view_image = self.view.view_canvas.get_image() assert view_image == test_set.current_image test_set._current_image_index -= 1 test_set.current_image.cuts = (10, 100) self.view.set_image() assert not view_image == test_set.current_image view_image = self.view.view_canvas.get_image() assert view_image == test_set.current_image assert self.view.view_canvas.get_cut_levels() == (10, 100) def test_set_transforms(self, qtbot, test_set): assert self.view.view_canvas.get_transforms() == (False, False, False) assert test_set.transforms == (False, False, False) test_set._flip_x = True assert test_set.transforms == (True, False, False) assert self.view.view_canvas.get_transforms() == (False, False, False) self.view.set_transforms() assert self.view.view_canvas.get_transforms() == (True, False, False) test_set._swap_xy = True self.view.set_transforms() assert self.view.view_canvas.get_transforms() == (True, False, True) test_set._flip_x = False test_set._swap_xy = False self.view.set_transforms() assert self.view.view_canvas.get_transforms() == (False, False, False) def test_change_zoom(self, qtbot, test_set): assert float(self.view.zoom_text.text()) == test_set.zoom self.view.zoom_text.setText('2') self.view.change_zoom() assert test_set.zoom == 2.0 self.view.zoom_text.setText('1.00') qtbot.keyPress(self.view.zoom_text, QtCore.Qt.Key_Return) assert test_set.zoom == 1.0 self.view.zoom_text.setText('foo') qtbot.keyPress(self.view.zoom_text, QtCore.Qt.Key_Return) assert test_set.zoom == 1.0 def test_adjust_pan_size(self, qtbot, test_set): assert self.view.pan.xradius == 16.5 assert self.view.pan.yradius == 32.5 test_set._zoom = 2 self.view.pan.x = 20 self.view.pan.y = 30 self.view.adjust_pan_size() assert self.view.pan.xradius == 8.5 assert self.view.pan.yradius == 16.5 assert test_set.center == (20, 30) test_set._zoom = 1 self.view.adjust_pan_size() assert self.view.pan.xradius == 16.5 assert self.view.pan.yradius == 32.5 assert test_set.center == (16, 32) def test_zoom_with_scroll(self, test_set): foward = ScrollEvent(direction=0.0) backwards = ScrollEvent(direction=15.0) assert test_set.zoom == 1.0 self.view.zoom_with_scroll(self.view.view_canvas, foward) assert test_set.zoom == 2.0 self.view.zoom_with_scroll(self.view.view_canvas, backwards) assert test_set.zoom == 1.0 self.view.zoom_with_scroll(self.view.view_canvas, backwards) assert test_set.zoom == 1.0 def test_arrow_key_move_center(self, test_set): test_set.zoom = 2 default_x, default_y = test_set.center self.view.arrow_key_move_center(self.view.view_canvas, 'left') assert test_set.center == (default_x - 1, default_y) self.view.arrow_key_move_center(self.view.view_canvas, 'right') assert test_set.center == (default_x, default_y) self.view.arrow_key_move_center(self.view.view_canvas, 'right') assert test_set.center == (default_x + 1, default_y) self.view.arrow_key_move_center(self.view.view_canvas, 'left') assert test_set.center == (default_x, default_y) self.view.arrow_key_move_center(self.view.view_canvas, 'up') assert test_set.center == (default_x, default_y + 1) self.view.arrow_key_move_center(self.view.view_canvas, 'down') assert test_set.center == (default_x, default_y) self.view.arrow_key_move_center(self.view.view_canvas, 'down') assert test_set.center == (default_x, default_y - 1) self.view.arrow_key_move_center(self.view.view_canvas, 'up') assert test_set.center == (default_x, default_y) def test_change_center(self, qtbot, test_set): test_set.zoom = 2 assert test_set.center == (16, 32) self.view.change_center(None, None, 20, 30) assert test_set.center == (20, 30) self.view.change_center(None, None, 8, 16) assert test_set.center == (8, 16) self.view.change_center(None, None, 16, 32) assert test_set.center == (16, 32) test_set.zoom = 1 assert test_set.center == (16, 32) def test_move_pan(self, qtbot, test_set): test_set.zoom = 2 test_set._center = (20, 30) assert self.view.pan.x == 16 assert self.view.pan.y == 32 self.view.move_pan() assert self.view.pan.x == 20 assert self.view.pan.y == 30 test_set._center = (16, 32) self.view.move_pan() assert self.view.pan.x == 16 assert self.view.pan.y == 32 test_set.zoom = 1