예제 #1
0
 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]
예제 #2
0
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 == []
예제 #3
0
 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_add_subview():
    view = PDSImageViewCanvas()
    subview1 = PDSImageViewCanvas()
    view.add_subview(subview1)
    assert subview1 in view._subviews
    subview2 = ImageViewCanvas()
    view.add_subview(subview2)
    assert subview2 in view._subviews

    with pytest.raises(TypeError):
        view.add_subview('foo')
예제 #5
0
class TestBasicHistogramModel(object):
    image_view = PDSImageViewCanvas()

    @pytest.fixture
    def model(self):
        return BasicHistogramModel(self.image_view)

    def test_connect_model(self, model):
        connect_model = BasicHistogramModel(self.image_view)
        model.cuts = (24, 42)
        assert model.connected_models == []
        model.connect_model(connect_model)
        assert model.connected_models == [connect_model]
        assert connect_model.cuts == (24, 42)

    def test_disconnect_model(self, model):
        connect_model = BasicHistogramModel(self.image_view)
        model.connected_models == [connect_model]
        model.disconnect_model(connect_model)
        assert model.connected_models == []

    def test_disconnect_from_all_models(self, model):
        connect_model1 = BasicHistogramModel(self.image_view)
        connect_model2 = BasicHistogramModel(self.image_view)
        model.connected_models == [connect_model1, connect_model2]
        model.disconnect_from_all_models()
        assert model.connected_models == []
예제 #6
0
class TestBasicHistogramController(object):
    image_view = PDSImageViewCanvas()
    model = BasicHistogramModel(image_view)
    connect_model = BasicHistogramModel(image_view)
    model.connected_models = [connect_model]

    @pytest.fixture
    def controller(self):
        self.image_view = PDSImageViewCanvas()
        self.model = BasicHistogramModel(self.image_view)
        self.connect_model = BasicHistogramModel(self.image_view)
        self.model.connected_models = [self.connect_model]
        return BasicHistogramController(self.model, None)

    def test_set_cut_low(self, controller):
        assert self.model._cut_low is None
        assert self.connect_model._cut_low is None
        controller.set_cut_low(24)
        assert self.model._cut_low == 24
        assert self.connect_model._cut_low == 24

    def test_set_cut_high(self, controller):
        assert self.model._cut_high is None
        assert self.connect_model._cut_high is None
        controller.set_cut_high(42)
        assert self.model._cut_high == 42
        assert self.connect_model._cut_high == 42

    def test_set_cuts(self, controller):
        assert self.model.cuts == (0, 0)
        assert self.connect_model.cuts == (0, 0)
        controller.set_cuts(24, 42)
        assert self.model.cuts == (24, 42)
        assert self.connect_model.cuts == (24, 42)
예제 #7
0
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)
예제 #8
0
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]
예제 #9
0
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
예제 #10
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]]))
예제 #11
0
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)]
예제 #12
0
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)

    @pytest.mark.parametrize('x, y, expected_x, expected_y',
                             [(0, 0, -.5, -.5), (1023, 1023, 1022.5, 1022.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)
예제 #13
0
 def poly(self):
     self.view_canvas = PDSImageViewCanvas()
     return Polygon(self.image_set, self.view_canvas)
예제 #14
0
 def controller(self):
     self.image_view = PDSImageViewCanvas()
     self.model = BasicHistogramModel(self.image_view)
     self.connect_model = BasicHistogramModel(self.image_view)
     self.model.connected_models = [self.connect_model]
     return BasicHistogramController(self.model, None)
예제 #15
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)]
예제 #16
0
 def rect(self):
     reset_image_set(self.image_set)
     self.view_canvas = PDSImageViewCanvas()
     return Rectangle(self.image_set, self.view_canvas)
예제 #17
0
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]]))
예제 #18
0
 def pencil(self):
     self.image_set.zoom = 1.0
     self.view_canvas = PDSImageViewCanvas()
     return Pencil(self.image_set, self.view_canvas)
예제 #19
0
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)
예제 #20
0
def test_abstract_base_class():
    image_set = PDSSpectImageSet(TEST_FILES)
    view_canvas = PDSImageViewCanvas()
    with pytest.raises(TypeError):
        ROIBase(image_set, view_canvas)