Exemple #1
0
def test_erase(Event, brush_shape, expected_sum):
    """Test erasing labels with different brush shapes."""
    data = np.ones((20, 20))
    layer = Labels(data)
    layer.brush_size = 10
    layer.mode = 'erase'
    layer.brush_shape = brush_shape
    layer.selected_label = 3
    layer.position = (0, 0)

    # Simulate click
    event = ReadOnlyWrapper(Event(type='mouse_press', is_dragging=False))
    mouse_press_callbacks(layer, event)

    layer.position = (19, 19)

    # Simulate drag
    event = ReadOnlyWrapper(Event(type='mouse_move', is_dragging=True))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(Event(type='mouse_release', is_dragging=False))
    mouse_release_callbacks(layer, event)

    # Painting goes from (0, 0) to (19, 19) with a brush size of 10, changing
    # all pixels along that path, but non outside it.
    assert np.unique(layer.data[:8, :8]) == 0
    assert np.unique(layer.data[-8:, -8:]) == 0
    assert np.unique(layer.data[:5, -5:]) == 1
    assert np.unique(layer.data[-5:, :5]) == 1
    assert np.sum(layer.data == 1) == expected_sum
def test_selecting_shapes_with_drag(mode, create_known_shapes_layer, Event):
    """Select all shapes when drag box includes all of them."""
    layer, n_shapes, known_non_shape = create_known_shapes_layer

    layer.mode = mode
    layer.position = known_non_shape

    # Simulate click
    event = ReadOnlyWrapper(
        Event(type='mouse_press', is_dragging=False, modifiers=[]))
    mouse_press_callbacks(layer, event)

    # Simulate drag start
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, modifiers=[]))
    mouse_move_callbacks(layer, event)

    layer.position = [0, 0]
    # Simulate drag end
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, modifiers=[]))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', is_dragging=True, modifiers=[]))
    mouse_release_callbacks(layer, event)

    # Check all shapes selected as drag box contains them
    assert len(layer.selected_data) == n_shapes
def test_selecting_no_points_with_drag_2d(create_known_points_layer_2d):
    """Select no points when drag box outside of all of them."""
    layer, n_points, known_non_point = create_known_points_layer_2d

    layer.mode = 'select'

    # Simulate click
    event = ReadOnlyWrapper(Event(type='mouse_press',
                                  position=known_non_point))
    mouse_press_callbacks(layer, event)

    # Simulate drag start
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, position=known_non_point))
    mouse_move_callbacks(layer, event)

    # Simulate drag end
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, position=(50, 60)))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', is_dragging=True, position=(50, 60)))
    mouse_release_callbacks(layer, event)

    # Check no points selected as drag box doesn't contain them
    assert len(layer.selected_data) == 0
    def setup(self, n):
        np.random.seed(0)
        self.data = [50 * np.random.random((6, 2)) for i in range(n)]
        self.layer = Shapes(self.data, shape_type='polygon')
        self.layer.mode = 'select'

        # initialize the position and select a shape
        position = tuple(np.mean(self.layer.data[0], axis=0))

        # create events
        click_event = ReadOnlyWrapper(
            Event(
                type='mouse_press',
                is_dragging=False,
                modifiers=[],
                position=position,
            ))
        # Simulate click
        mouse_press_callbacks(self.layer, click_event)

        release_event = ReadOnlyWrapper(
            Event(
                type='mouse_release',
                is_dragging=False,
                modifiers=[],
                position=position,
            ))

        # Simulate release
        mouse_release_callbacks(self.layer, release_event)
Exemple #5
0
def test_add_point_3d(create_known_points_layer_3d):
    """Add a point by clicking in 3D mode."""
    layer, n_points, known_not_point = create_known_points_layer_3d

    layer.mode = 'add'

    # Simulate click
    event = ReadOnlyWrapper(
        Event(
            type='mouse_press',
            position=known_not_point,
            view_direction=[1, 0, 0],
            dims_displayed=[0, 1, 2],
        )
    )
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', position=known_not_point)
    )
    mouse_release_callbacks(layer, event)

    # Check clicked point selected
    assert len(layer.data) == (n_points + 1)
    np.testing.assert_array_equal(layer.data[-1], known_not_point)
Exemple #6
0
def test_paint(Event, brush_shape, expected_sum):
    """Test painting labels with circle/square brush."""
    data = np.ones((20, 20), dtype=np.int32)
    layer = Labels(data)
    layer.brush_size = 10
    assert layer.cursor_size == 10
    with pytest.warns(FutureWarning):
        layer.brush_shape = brush_shape

    layer.mode = 'paint'
    layer.selected_label = 3

    # Simulate click
    event = ReadOnlyWrapper(
        Event(type='mouse_press', is_dragging=False, position=(0, 0)))
    mouse_press_callbacks(layer, event)

    # Simulate drag
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, position=(19, 19)))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', is_dragging=False, position=(19, 19)))
    mouse_release_callbacks(layer, event)

    # Painting goes from (0, 0) to (19, 19) with a brush size of 10, changing
    # all pixels along that path, but none outside it.
    assert np.unique(layer.data[:8, :8]) == 3
    assert np.unique(layer.data[-8:, -8:]) == 3
    assert np.unique(layer.data[:5, -5:]) == 1
    assert np.unique(layer.data[-5:, :5]) == 1
    assert np.sum(layer.data == 3) == expected_sum
def test_unselect_select_shape(mode, create_known_shapes_layer, Event):
    """Select a shape by clicking on one in select mode."""
    layer, n_shapes, _ = create_known_shapes_layer

    layer.mode = mode
    position = tuple(layer.data[0][0])
    layer.selected_data = {1}

    # Simulate click
    event = ReadOnlyWrapper(
        Event(
            type='mouse_press',
            is_dragging=False,
            modifiers=[],
            position=position,
        ))
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(
            type='mouse_release',
            is_dragging=False,
            modifiers=[],
            position=position,
        ))
    mouse_release_callbacks(layer, event)

    # Check clicked shape selected
    assert len(layer.selected_data) == 1
    assert layer.selected_data == {0}
def test_not_adding_or_selecting_shape(create_known_shapes_layer, Event):
    """Don't add or select a shape by clicking on one in pan_zoom mode."""
    layer, n_shapes, _ = create_known_shapes_layer
    layer.mode = 'pan_zoom'

    # Simulate click
    event = ReadOnlyWrapper(
        Event(
            type='mouse_press',
            is_dragging=False,
            modifiers=[],
            position=(0, 0),
        ))
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(
            type='mouse_release',
            is_dragging=False,
            modifiers=[],
            position=(0, 0),
        ))
    mouse_release_callbacks(layer, event)

    # Check no new shape added and non selected
    assert len(layer.data) == n_shapes
    assert len(layer.selected_data) == 0
Exemple #9
0
def test_drag_in_add_mode(create_known_points_layer, Event):
    """Drag in add mode and make sure no point is added."""
    layer, n_points, known_non_point = create_known_points_layer

    # Add point at location where non exists
    layer.mode = 'add'
    layer.interactive = True
    layer.position = known_non_point

    # Simulate click
    event = ReadOnlyWrapper(
        Event(type='mouse_press', is_dragging=False, modifiers=[]))
    mouse_press_callbacks(layer, event)

    known_non_point_end = [40, 60]
    layer.position = known_non_point_end

    # Simulate drag end
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, modifiers=[]))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', is_dragging=False, modifiers=[]))
    mouse_release_callbacks(layer, event)

    # Check that no new point has been added
    assert len(layer.data) == n_points
Exemple #10
0
def test_unselect_by_click_empty_3d(create_known_points_layer_3d):
    """Select unselecting point by clicking in empty space"""
    layer, n_points, known_not_point = create_known_points_layer_3d

    layer.mode = 'select'

    layer.selected_data = {0, 1}

    # Simulate click on point
    event = ReadOnlyWrapper(
        Event(
            type='mouse_press',
            position=known_not_point,
            view_direction=[1, 0, 0],
            dims_displayed=[0, 1, 2],
        )
    )
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', position=known_not_point)
    )
    mouse_release_callbacks(layer, event)

    # Check clicked point selected
    assert len(layer.selected_data) == 0
Exemple #11
0
def test_selecting_all_points_with_drag(create_known_points_layer_2d):
    """Select all points when drag box includes all of them."""
    layer, n_points, known_non_point = create_known_points_layer_2d

    layer.mode = 'select'

    # Simulate click
    event = ReadOnlyWrapper(
        Event(type='mouse_press', position=known_non_point)
    )
    mouse_press_callbacks(layer, event)

    # Simulate drag start
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, position=known_non_point)
    )
    mouse_move_callbacks(layer, event)

    # Simulate drag end
    event = ReadOnlyWrapper(Event(type='mouse_move', is_dragging=True))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(Event(type='mouse_release', is_dragging=True))
    mouse_release_callbacks(layer, event)

    # Check all points selected as drag box contains them
    assert len(layer.selected_data) == n_points
Exemple #12
0
def test_selct_by_shift_click_3d(create_known_points_layer_3d):
    """Select selecting point by shift clicking on an additional point in 3D"""
    layer, n_points, _ = create_known_points_layer_3d

    layer.mode = 'select'
    position = tuple(layer.data[1])

    layer.selected_data = {0}

    # Simulate shift+click on point 1
    event = ReadOnlyWrapper(
        Event(
            type='mouse_press',
            position=position,
            modifiers=['Shift'],
            view_direction=[1, 0, 0],
            dims_displayed=[0, 1, 2],
        )
    )
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', modifiers=['Shift'], position=position)
    )
    mouse_release_callbacks(layer, event)

    # Check clicked point selected
    assert layer.selected_data == {0, 1}
Exemple #13
0
def test_select_point_3d(create_known_points_layer_3d):
    """Select a point by clicking on one in select mode in 3D mode."""
    layer, n_points, _ = create_known_points_layer_3d

    layer.mode = 'select'
    position = tuple(layer.data[1])

    # Simulate click
    event = ReadOnlyWrapper(
        Event(
            type='mouse_press',
            position=position,
            view_direction=[1, 0, 0],
            dims_displayed=[0, 1, 2],
        )
    )
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(Event(type='mouse_release', position=position))
    mouse_release_callbacks(layer, event)

    # Check clicked point selected
    assert len(layer.selected_data) == 1
    assert 1 in layer.selected_data
    def time_select_shape(self, n):
        """Time to process shape selection events"""
        position = tuple(np.mean(self.layer.data[1], axis=0))

        # create events
        click_event = ReadOnlyWrapper(
            Event(
                type='mouse_press',
                is_dragging=False,
                modifiers=[],
                position=position,
            ))
        # Simulate click
        mouse_press_callbacks(self.layer, click_event)

        release_event = ReadOnlyWrapper(
            Event(
                type='mouse_release',
                is_dragging=False,
                modifiers=[],
                position=position,
            ))

        # Simulate release
        mouse_release_callbacks(self.layer, release_event)
def test_add_simple_shape(shape_type, create_known_shapes_layer, Event):
    """Add simple shape by clicking in add mode."""
    layer, n_shapes, known_non_shape = create_known_shapes_layer

    # Add shape at location where non exists
    layer.mode = 'add_' + shape_type
    layer.position = known_non_shape

    # Simulate click
    event = ReadOnlyWrapper(
        Event(type='mouse_press', is_dragging=False, modifiers=[]))
    mouse_press_callbacks(layer, event)

    known_non_shape_end = [40, 60]
    layer.position = known_non_shape_end
    # Simulate drag end
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, modifiers=[]))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', is_dragging=False, modifiers=[]))
    mouse_release_callbacks(layer, event)

    # Check new shape added at coordinates
    assert len(layer.data) == n_shapes + 1
    np.testing.assert_allclose(layer.data[-1][0], known_non_shape)
    new_shape_max = np.max(layer.data[-1], axis=0)
    np.testing.assert_allclose(new_shape_max, known_non_shape_end)
    assert layer.shape_type[-1] == shape_type
def test_add_complex_shape(shape_type, create_known_shapes_layer, Event):
    """Add simple shape by clicking in add mode."""
    layer, n_shapes, known_non_shape = create_known_shapes_layer

    desired_shape = [[20, 30], [10, 50], [60, 40], [80, 20]]
    # Add shape at location where non exists
    layer.mode = 'add_' + shape_type
    layer.position = desired_shape[0]

    for coord in desired_shape:
        layer.position = coord
        # Simulate move, click, and release
        event = ReadOnlyWrapper(
            Event(type='mouse_move', is_dragging=False, modifiers=[]))
        mouse_move_callbacks(layer, event)
        event = ReadOnlyWrapper(
            Event(type='mouse_press', is_dragging=False, modifiers=[]))
        mouse_press_callbacks(layer, event)
        event = ReadOnlyWrapper(
            Event(type='mouse_release', is_dragging=False, modifiers=[]))
        mouse_release_callbacks(layer, event)

    # finish drawing
    layer._finish_drawing()

    # Check new shape added at coordinates
    assert len(layer.data) == n_shapes + 1
    np.testing.assert_allclose(layer.data[-1], desired_shape)
    assert layer.shape_type[-1] == shape_type
Exemple #17
0
def test_selecting_no_points_with_drag(create_known_points_layer, Event):
    """Select all points when drag box includes all of them."""
    layer, n_points, known_non_point = create_known_points_layer

    layer.mode = 'select'
    layer.position = known_non_point

    # Simulate click
    event = ReadOnlyWrapper(
        Event(type='mouse_press', is_dragging=False, modifiers=[]))
    mouse_press_callbacks(layer, event)

    # Simulate drag start
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, modifiers=[]))
    mouse_move_callbacks(layer, event)

    layer.position = [50, 60]
    # Simulate drag end
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, modifiers=[]))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', is_dragging=True, modifiers=[]))
    mouse_release_callbacks(layer, event)

    # Check no points selected as drag box doesn't contain them
    assert len(layer.selected_data) == 0
def test_drag_vertex(create_known_shapes_layer, Event):
    """Select and drag vertex."""
    layer, n_shapes, _ = create_known_shapes_layer

    layer.mode = 'direct'
    layer.selected_data = {0}
    layer.position = tuple(layer.data[0][0])

    # Simulate click
    event = ReadOnlyWrapper(
        Event(type='mouse_press', is_dragging=False, modifiers=[]))
    mouse_press_callbacks(layer, event)

    layer.position = [0, 0]
    # Simulate move, click, and release
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, modifiers=[]))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', is_dragging=True, modifiers=[]))
    mouse_release_callbacks(layer, event)

    # Check clicked shape selected
    assert len(layer.selected_data) == 1
    assert layer.selected_data == {0}
    np.testing.assert_allclose(layer.data[0][-1], [0, 0])
Exemple #19
0
def test_paint_scale(Event, brush_shape, expected_sum):
    """Test painting labels with circle/square brush when scaled."""
    data = np.ones((20, 20))
    layer = Labels(data, scale=(2, 2))
    layer.brush_size = 20
    layer.brush_shape = brush_shape
    layer.mode = 'paint'
    layer.selected_label = 3
    layer.position = (0, 0)

    # Simulate click
    event = ReadOnlyWrapper(Event(type='mouse_press', is_dragging=False))
    mouse_press_callbacks(layer, event)

    layer.position = (39, 39)

    # Simulate drag
    event = ReadOnlyWrapper(Event(type='mouse_move', is_dragging=True))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(Event(type='mouse_release', is_dragging=False))
    mouse_release_callbacks(layer, event)

    # Painting goes from (0, 0) to (19, 19) with a brush size of 10, changing
    # all pixels along that path, but none outside it.
    assert np.unique(layer.data[:8, :8]) == 3
    assert np.unique(layer.data[-8:, -8:]) == 3
    assert np.unique(layer.data[:5, -5:]) == 1
    assert np.unique(layer.data[-5:, :5]) == 1
    assert np.sum(layer.data == 3) == expected_sum
def test_unselecting_shapes(mode, create_known_shapes_layer, Event):
    """Unselect shapes by not clicking on one in select mode."""
    layer, n_shapes, known_non_shape = create_known_shapes_layer

    layer.mode = mode
    layer.selected_data = {0, 1}
    assert len(layer.selected_data) == 2

    # Simulate click
    event = ReadOnlyWrapper(
        Event(
            type='mouse_press',
            is_dragging=False,
            modifiers=[],
            position=known_non_shape,
        ))
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(
            type='mouse_release',
            is_dragging=False,
            modifiers=[],
            position=known_non_shape,
        ))
    mouse_release_callbacks(layer, event)

    # Check clicked shape selected
    assert len(layer.selected_data) == 0
    def time_select_shape(self, n):
        """Time to process shape selection events"""
        self.layer.position = tuple(np.mean(self.layer.data[1], axis=0))

        # Simulate click
        mouse_press_callbacks(self.layer, self.click_event)

        # Simulate release
        mouse_release_callbacks(self.layer, self.release_event)
Exemple #22
0
def test_paint(MouseEvent):
    """Test painting labels with circle brush."""
    data = np.ones((20, 20), dtype=np.int32)
    layer = Labels(data)
    layer.brush_size = 10
    assert layer.cursor_size == 10

    layer.mode = 'paint'
    layer.selected_label = 3

    # Simulate click
    event = ReadOnlyWrapper(
        MouseEvent(
            type='mouse_press',
            is_dragging=False,
            position=(0, 0),
            view_direction=None,
            dims_displayed=(0, 1),
            dims_point=(0, 0),
        )
    )
    mouse_press_callbacks(layer, event)

    # Simulate drag
    event = ReadOnlyWrapper(
        MouseEvent(
            type='mouse_move',
            is_dragging=True,
            position=(19, 19),
            view_direction=None,
            dims_displayed=(0, 1),
            dims_point=(0, 0),
        )
    )
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        MouseEvent(
            type='mouse_release',
            is_dragging=False,
            position=(19, 19),
            view_direction=None,
            dims_displayed=(0, 1),
            dims_point=(0, 0),
        )
    )
    mouse_release_callbacks(layer, event)

    # Painting goes from (0, 0) to (19, 19) with a brush size of 10, changing
    # all pixels along that path, but none outside it.
    assert np.unique(layer.data[:8, :8]) == 3
    assert np.unique(layer.data[-8:, -8:]) == 3
    assert np.unique(layer.data[:5, -5:]) == 1
    assert np.unique(layer.data[-5:, :5]) == 1
    assert np.sum(layer.data == 3) == 244
def test_selecting_no_points_with_drag_3d(create_known_points_layer_3d):
    """Select no points when drag box outside of all of them."""
    layer, n_points, known_non_point = create_known_points_layer_3d

    layer.mode = 'select'

    # Simulate click
    event = ReadOnlyWrapper(
        Event(
            type='mouse_press',
            position=(5, 15, 15),
            view_direction=[1, 0, 0],
            up_direction=[0, 1, 0],
            dims_displayed=[0, 1, 2],
        ))
    mouse_press_callbacks(layer, event)

    # Simulate drag start
    event = ReadOnlyWrapper(
        Event(
            type='mouse_move',
            is_dragging=True,
            position=(5, 15, 15),
            view_direction=[1, 0, 0],
            up_direction=[0, 1, 0],
            dims_displayed=[0, 1, 2],
        ))
    mouse_move_callbacks(layer, event)

    # Simulate drag end
    event = ReadOnlyWrapper(
        Event(
            type='mouse_move',
            is_dragging=True,
            position=(5, 20, 20),
            view_direction=[1, 0, 0],
            up_direction=[0, 1, 0],
            dims_displayed=[0, 1, 2],
        ))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(
            type='mouse_release',
            is_dragging=True,
            position=(5, 20, 20),
            view_direction=[1, 0, 0],
            up_direction=[0, 1, 0],
            dims_displayed=[0, 1, 2],
        ))
    mouse_release_callbacks(layer, event)

    # Check all points selected as drag box contains them
    assert len(layer.selected_data) == 0
    def time_drag_shape(self, n):
        """Time to process 5 shape drag events"""
        # initialize the position and select a shape
        position = tuple(np.mean(self.layer.data[0], axis=0))

        # create events
        click_event = ReadOnlyWrapper(
            Event(
                type='mouse_press',
                is_dragging=False,
                modifiers=[],
                position=position,
            ))

        # Simulate click
        mouse_press_callbacks(self.layer, click_event)

        # create events
        drag_event = ReadOnlyWrapper(
            Event(
                type='mouse_press',
                is_dragging=True,
                modifiers=[],
                position=position,
            ))

        # start drag event
        mouse_move_callbacks(self.layer, drag_event)

        # simulate 5 drag events
        for _ in range(5):
            position = tuple(np.add(position, [10, 5]))
            drag_event = ReadOnlyWrapper(
                Event(
                    type='mouse_press',
                    is_dragging=True,
                    modifiers=[],
                    position=position,
                ))

            # Simulate move, click, and release
            mouse_move_callbacks(self.layer, drag_event)

        release_event = ReadOnlyWrapper(
            Event(
                type='mouse_release',
                is_dragging=False,
                modifiers=[],
                position=position,
            ))

        # Simulate release
        mouse_release_callbacks(self.layer, release_event)
Exemple #25
0
def test_not_adding_or_selecting_point(create_known_points_layer_2d):
    """Don't add or select a point by clicking on one in pan_zoom mode."""
    layer, n_points, _ = create_known_points_layer_2d
    layer.mode = 'pan_zoom'

    # Simulate click
    event = ReadOnlyWrapper(Event(type='mouse_press'))
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(Event(type='mouse_release'))
    mouse_release_callbacks(layer, event)

    # Check no new point added and non selected
    assert len(layer.data) == n_points
    assert len(layer.selected_data) == 0
    def time_drag_shape(self, n):
        """Time to process 5 shape drag events"""
        # Simulate click
        mouse_press_callbacks(self.layer, self.click_event)

        # start drag event
        mouse_move_callbacks(self.layer, self.drag_event)

        # simulate 5 drag events
        for _ in range(5):
            self.layer.position = tuple(np.add(self.layer.position, [10, 5]))
            # Simulate move, click, and release
            mouse_move_callbacks(self.layer, self.drag_event)

        # Simulate release
        mouse_release_callbacks(self.layer, self.release_event)
Exemple #27
0
def test_labels_painting(make_test_viewer):
    """Test painting labels updates image."""
    data = np.zeros((100, 100))

    viewer = make_test_viewer(show=True)
    viewer.add_labels(data)
    layer = viewer.layers[0]

    screenshot = viewer.screenshot(canvas_only=True)

    # Check that no painting has occurred
    assert layer.data.max() == 0
    assert screenshot[:, :, :2].max() == 0

    # Enter paint mode
    layer.position = (0, 0)
    layer.mode = 'paint'
    layer.selected_label = 3

    # Simulate click
    Event = collections.namedtuple(
        'Event', field_names=['type', 'is_dragging']
    )

    # Simulate click
    event = ReadOnlyWrapper(Event(type='mouse_press', is_dragging=False))
    mouse_press_callbacks(layer, event)

    layer.position = (100, 100)

    # Simulate drag
    event = ReadOnlyWrapper(Event(type='mouse_move', is_dragging=True))
    mouse_move_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(Event(type='mouse_release', is_dragging=False))
    mouse_release_callbacks(layer, event)

    event = ReadOnlyWrapper(Event(type='mouse_press', is_dragging=False))
    mouse_press_callbacks(layer, event)

    screenshot = viewer.screenshot(canvas_only=True)
    # Check that painting has now occurred
    assert layer.data.max() > 0
    assert screenshot[:, :, :2].max() > 0
Exemple #28
0
def test_select_point(create_known_points_layer_2d):
    """Select a point by clicking on one in select mode."""
    layer, n_points, _ = create_known_points_layer_2d

    layer.mode = 'select'
    position = tuple(layer.data[0])

    # Simulate click
    event = ReadOnlyWrapper(Event(type='mouse_press', position=position))
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(Event(type='mouse_release', position=position))
    mouse_release_callbacks(layer, event)

    # Check clicked point selected
    assert len(layer.selected_data) == 1
    assert 0 in layer.selected_data
def test_not_selecting_point(create_known_points_layer_2d):
    """Don't select a point by not clicking on one in select mode."""
    layer, n_points, known_non_point = create_known_points_layer_2d

    layer.mode = 'select'

    # Simulate click
    event = ReadOnlyWrapper(Event(type='mouse_press',
                                  position=known_non_point))
    mouse_press_callbacks(layer, event)

    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', position=known_non_point))
    mouse_release_callbacks(layer, event)

    # Check clicked point selected
    assert len(layer.selected_data) == 0
def test_drag_shape(create_known_shapes_layer, Event):
    """Select and drag vertex."""
    layer, n_shapes, _ = create_known_shapes_layer

    layer.mode = 'select'
    orig_data = layer.data[0].copy()
    assert len(layer.selected_data) == 0

    layer.position = tuple(np.mean(layer.data[0], axis=0))

    # Simulate click
    event = ReadOnlyWrapper(
        Event(type='mouse_press', is_dragging=False, modifiers=[]))
    mouse_press_callbacks(layer, event)
    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', is_dragging=False, modifiers=[]))
    mouse_release_callbacks(layer, event)

    assert len(layer.selected_data) == 1
    assert layer.selected_data == {0}

    # Simulate click
    event = ReadOnlyWrapper(
        Event(type='mouse_press', is_dragging=True, modifiers=[]))
    mouse_press_callbacks(layer, event)
    # start drag event
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, modifiers=[]))
    mouse_move_callbacks(layer, event)
    layer.position = tuple(np.add(layer.position, [10, 5]))
    # Simulate move, click, and release
    event = ReadOnlyWrapper(
        Event(type='mouse_move', is_dragging=True, modifiers=[]))
    mouse_move_callbacks(layer, event)
    # Simulate release
    event = ReadOnlyWrapper(
        Event(type='mouse_release', is_dragging=True, modifiers=[]))
    mouse_release_callbacks(layer, event)

    # Check clicked shape selected
    assert len(layer.selected_data) == 1
    assert layer.selected_data == {0}
    np.testing.assert_allclose(layer.data[0], orig_data + [10, 5])