def test_value(): """Test getting the value of the data at the current coordinates.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) data[-1] = [0, 0] layer = Points(data) value = layer.get_value() assert layer.coordinates == (0, 0) assert value == 9 layer.data = layer.data + 20 value = layer.get_value() assert value is None
def test_add_points_with_properties_as_list(): # test adding points initialized with properties as list shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) properties = { 'point_type': list(_make_cycled_properties(['A', 'B'], shape[0])) } layer = Points(data, properties=copy(properties)) coord = [18, 18] layer.add(coord) new_prop = {'point_type': np.append(properties['point_type'], 'B')} np.testing.assert_equal(layer.properties, new_prop)
def test_change_text_updates_node_string(): points = np.random.rand(3, 2) properties = { 'class': np.array(['A', 'B', 'C']), 'name': np.array(['D', 'E', 'F']), } layer = Points(points, text='class', properties=properties) vispy_layer = VispyPointsLayer(layer) text_node = vispy_layer._get_text_node() np.testing.assert_array_equal(text_node.text, properties['class']) layer.text = 'name' np.testing.assert_array_equal(text_node.text, properties['name'])
def test_add_colormap(attribute): """Test directly adding a vispy Colormap object""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) annotations = {'point_type': _make_cycled_properties([0, 1.5], shape[0])} color_kwarg = f'{attribute}_color' colormap_kwarg = f'{attribute}_colormap' args = {color_kwarg: 'point_type', colormap_kwarg: 'viridis'} layer = Points(data, properties=annotations, **args) setattr(layer, f'{attribute}_colormap', get_colormap('gray')) layer_colormap = getattr(layer, f'{attribute}_colormap') assert layer_colormap[0] == 'unknown_colormap'
def test_view_data(): coords = np.array([[0, 1, 1], [0, 2, 2], [1, 3, 3], [3, 3, 3]]) layer = Points(coords) layer.dims.set_point(0, 0) assert np.all( layer._view_data == coords[np.ix_([0, 1], layer.dims.displayed)]) layer.dims.set_point(0, 1) assert np.all( layer._view_data == coords[np.ix_([2], layer.dims.displayed)]) layer.dims.ndisplay = 3 assert np.all(layer._view_data == coords)
def update_points(self): if self.settings.points is not None: self.points_view_button.setVisible(True) if self.points_layer is None or self.points_layer not in self.viewer.layers: self.points_layer = Points( self.settings.points, scale=self.settings.image.normalized_scaling()) self.viewer.add_layer(self.points_layer) else: self.points_layer.data = self.settings.points self.points_layer.scale = self.settings.image.normalized_scaling( ) elif self.points_layer is not None and self.points_layer in self.viewer.layers: self.points_view_button.setVisible(False) self.points_layer.data = np.empty((0, 4))
def test_view_size(): coords = np.array([[0, 1, 1], [0, 2, 2], [1, 3, 3], [3, 3, 3]]) sizes = np.array([[3, 5, 5], [3, 5, 5], [3, 3, 3], [2, 2, 3]]) layer = Points(coords, size=sizes, n_dimensional=False) layer.dims.set_point(0, 0) assert np.all( layer._view_size == sizes[np.ix_([0, 1], layer.dims.displayed)] ) layer.dims.set_point(0, 1) assert np.all(layer._view_size == sizes[np.ix_([2], layer.dims.displayed)]) layer.n_dimensional = True assert len(layer._view_size) == 3
def test_message(): """Test converting value and coords to message.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) data[-1] = [0, 0] layer = Points(data) value = layer.get_value() msg = layer.get_message(layer.coordinates, value) assert type(msg) == str layer.data = layer.data + 5 value = layer.get_value() msg = layer.get_message(layer.coordinates, value) assert type(msg) == str
def test_layerlist_context(): assert 'num_selected_layers' in LayerListContextKeys.__members__ ctx = {} LLCK = LayerListContextKeys(ctx) assert LLCK.num_selected_layers == 0 assert ctx['num_selected_layers'] == 0 layer_list = LayerList() points_layer = Points() layer_list.selection.events.changed.connect(LLCK.update) layer_list.append(points_layer) assert LLCK.num_selected_layers == 1 assert ctx['num_selected_layers'] == 1
def test_n_dimensional(): """Test setting n_dimensional flag for 2D and 4D data.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) assert layer.n_dimensional is False layer.n_dimensional = True assert layer.n_dimensional is True layer = Points(data, n_dimensional=True) assert layer.n_dimensional is True shape = (10, 4) data = 20 * np.random.random(shape) layer = Points(data) assert layer.n_dimensional is False layer.n_dimensional = True assert layer.n_dimensional is True layer = Points(data, n_dimensional=True) assert layer.n_dimensional is True
def test_empty_layer_with_text_properties(): """Test initializing an empty layer with text defined""" default_properties = {'point_type': np.array([1.5], dtype=float)} text_kwargs = {'text': 'point_type', 'color': 'red'} layer = Points( properties=default_properties, text=text_kwargs, ) np.testing.assert_equal(layer.text.values, np.empty(0)) np.testing.assert_allclose(layer.text.color, [1, 0, 0, 1]) # add a point and check that the appropriate text value was added layer.add([1, 1]) np.testing.assert_equal(layer.text.values, ['1.5']) np.testing.assert_allclose(layer.text.color, [1, 0, 0, 1])
def test_color_cycle_dict(attribute): """Test setting edge/face color with a color cycle dict""" data = np.array([[0, 0], [100, 0], [0, 100]]) properties = {'my_colors': [2, 6, 3]} points_kwargs = { 'properties': properties, f'{attribute}_color': 'my_colors', f'{attribute}_color_cycle': {1: 'green', 2: 'red', 3: 'blue'}, } layer = Points(data, **points_kwargs) color_cycle_map = getattr(layer, f'{attribute}_color_cycle_map') np.testing.assert_allclose(color_cycle_map[2], [1, 0, 0, 1]) # 2 is red np.testing.assert_allclose(color_cycle_map[3], [0, 0, 1, 1]) # 3 is blue np.testing.assert_allclose(color_cycle_map[6], [1, 1, 1, 1]) # 6 is white
def test_updating_points_properties(): # test adding points initialized with properties shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) properties = {'point_type': _make_cycled_properties(['A', 'B'], shape[0])} layer = Points(data, properties=copy(properties)) layer.mode = 'select' layer.selected_data = [len(data) - 1] layer.current_properties = {'point_type': np.array(['A'])} updated_properties = properties updated_properties['point_type'][-1] = 'A' np.testing.assert_equal(layer.properties, updated_properties)
def test_empty_layer_with_face_colormap(): """Test creating an empty layer where the face color is a colormap See: https://github.com/napari/napari/pull/1069 """ default_properties = {'point_type': np.array([1.5], dtype=float)} layer = Points( properties=default_properties, face_color='point_type', face_colormap='gray', ) assert layer.face_color_mode == 'colormap' # verify the current_face_color is correct face_color = np.array([1, 1, 1, 1]) np.testing.assert_allclose(layer._face.current_color, face_color)
def test_interaction_box(): """Test the creation of the interaction box.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) assert layer._selected_box is None layer.selected_data = [0] assert len(layer._selected_box) == 4 layer.selected_data = [0, 1] assert len(layer._selected_box) == 4 layer.selected_data = [] assert layer._selected_box is None
def test_empty_layer_with_edge_colormap(): """ Test creating an empty layer where the face color is a colormap See: https://github.com/napari/napari/pull/1069 """ default_properties = {'point_type': np.array([1.5], dtype=np.float)} layer = Points( properties=default_properties, edge_color='point_type', edge_colormap='grays', ) assert layer.edge_color_mode == 'colormap' # verify the current_face_color is correct edge_color = np.array([1, 1, 1, 1]) assert np.all(layer._current_edge_color == edge_color)
def test_view_data(): coords = np.array([[0, 1, 1], [0, 2, 2], [1, 3, 3], [3, 3, 3]]) layer = Points(coords) layer._slice_dims([0, slice(None), slice(None)]) assert np.all( layer._view_data == coords[np.ix_([0, 1], layer._dims_displayed)] ) layer._slice_dims([1, slice(None), slice(None)]) assert np.all( layer._view_data == coords[np.ix_([2], layer._dims_displayed)] ) layer._slice_dims([1, slice(None), slice(None)], ndisplay=3) assert np.all(layer._view_data == coords)
def test_slice_data(): data = [ (10, 2, 4), (10 + 2 * 1e-7, 4, 6), (8, 1, 7), (10.1, 7, 2), (10 - 2 * 1e-7, 1, 6), ] layer = Points(data) assert len(layer._slice_data((8, slice(None), slice(None)))[0]) == 1 assert len(layer._slice_data((10, slice(None), slice(None)))[0]) == 3 assert ( len(layer._slice_data((10 + 2 * 1e-12, slice(None), slice(None)))[0]) == 3 ) assert len(layer._slice_data((10.1, slice(None), slice(None)))[0]) == 1
def test_interaction_box(): """Test the boxes calculated for selected points""" data = [[3, 3]] size = 2 layer = Points(data, size=size) # get a box with no points selected index = [] box = layer.interaction_box(index) assert box is None # get a box with a point selected index = [0] expected_box = points_to_squares(data, size) box = layer.interaction_box(index) np.all([np.isin(p, expected_box) for p in box])
def test_push_button(qtbot): """Make sure the QtModePushButton works with callbacks""" layer = Points() def set_test_prop(): layer.test_prop = True btn = QtModePushButton(layer, 'test_button', slot=set_test_prop, tooltip='tooltip') assert btn.property('mode') == 'test_button' assert btn.toolTip() == 'tooltip' btn.click() qtbot.wait(50) assert layer.test_prop
def layers(): """Fixture that supplies a layers list for testing. Returns ------- napari.components.LayerList The desired napari LayerList. """ np.random.seed(0) list_of_layers = [ Image(np.random.rand(20, 20)), Labels(np.random.randint(10, size=(20, 2))), Points(np.random.rand(20, 2)), Shapes(np.random.rand(10, 2, 2)), Vectors(np.random.rand(10, 2, 2)), ] return LayerList(list_of_layers)
def test_color_cycle(attribute, color_cycle): """Test setting edge/face color with a color cycle list""" # create Points using list color cycle shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) properties = {'point_type': _make_cycled_properties(['A', 'B'], shape[0])} points_kwargs = { 'properties': properties, f'{attribute}_color': 'point_type', f'{attribute}_color_cycle': color_cycle, } layer = Points(data, **points_kwargs) assert layer.properties == properties color_array = transform_color(list(islice(cycle(color_cycle), 0, shape[0]))) layer_color = getattr(layer, f'{attribute}_color') np.testing.assert_allclose(layer_color, color_array) # Add new point and test its color coord = [18, 18] layer.selected_data = {0} layer.add(coord) layer_color = getattr(layer, f'{attribute}_color') assert len(layer_color) == shape[0] + 1 np.testing.assert_allclose( layer_color, np.vstack((color_array, transform_color('red'))), ) # Check removing data adjusts colors correctly layer.selected_data = {0, 2} layer.remove_selected() assert len(layer.data) == shape[0] - 1 layer_color = getattr(layer, f'{attribute}_color') assert len(layer_color) == shape[0] - 1 np.testing.assert_allclose( layer_color, np.vstack((color_array[1], color_array[3:], transform_color('red'))), ) # refresh colors layer.refresh_colors(update_color_mapping=True)
def test_adding_annotations(): shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) properties = {'point_type': _make_cycled_properties(['A', 'B'], shape[0])} layer = Points(data) assert layer.properties == {} # add properties layer.properties = copy(properties) assert layer.properties == properties # change properties new_annotations = { 'other_type': _make_cycled_properties(['C', 'D'], shape[0]) } layer.properties = copy(new_annotations) assert layer.properties == new_annotations
def test_adding_annotations(): shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) annotations = {'point_type': np.array(['A', 'B'] * int((shape[0] / 2)))} layer = Points(data) assert layer.properties == {} # add properties layer.properties = copy(annotations) assert layer.properties == annotations # change properties new_annotations = { 'other_type': np.array(['C', 'D'] * int((shape[0] / 2))) } layer.properties = copy(new_annotations) assert layer.properties == new_annotations
def test_view_size(): coords = np.array([[0, 1, 1], [0, 2, 2], [1, 3, 3], [3, 3, 3]]) sizes = np.array([[3, 5, 5], [3, 5, 5], [3, 3, 3], [2, 2, 3]]) layer = Points(coords, size=sizes, n_dimensional=False) layer._slice_dims([0, slice(None), slice(None)]) assert np.all( layer._view_size == sizes[np.ix_([0, 1], layer.dims.displayed)]) layer._slice_dims([1, slice(None), slice(None)]) assert np.all(layer._view_size == sizes[np.ix_([2], layer.dims.displayed)]) layer.n_dimensional = True assert len(layer._view_size) == 3 # test a slice with no points layer.n_dimensional = False layer._slice_dims([2, slice(None), slice(None)]) assert np.all(layer._view_size == [])
def test_is_color_mapped(): shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) annotations = {'point_type': np.array(['A', 'B'] * int((shape[0] / 2)))} layer = Points(data, properties=annotations) # giving the name of an annotation should return True assert layer._is_color_mapped('point_type') # giving a list should return false (i.e., could be an RGBA color) assert not layer._is_color_mapped([1, 1, 1, 1]) # giving an ndarray should return false (i.e., could be an RGBA color) assert not layer._is_color_mapped(np.array([1, 1, 1, 1])) # give an invalid color argument with pytest.raises(ValueError): layer._is_color_mapped((123, 323))
def test_move(): """Test moving points.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) unmoved = copy(data) layer = Points(data) # Move one point relative to an initial drag start location layer._move([0], [0, 0]) layer._move([0], [10, 10]) layer._drag_start = None assert np.all(layer.data[0] == unmoved[0] + [10, 10]) assert np.all(layer.data[1:] == unmoved[1:]) # Move two points relative to an initial drag start location layer._move([1, 2], [2, 2]) layer._move([1, 2], np.add([2, 2], [-3, 4])) assert np.all(layer.data[1:2] == unmoved[1:2] + [-3, 4])
def test_empty_points_with_properties_list(): """Test instantiating an empty Points layer with properties stored in a list See: https://github.com/napari/napari/pull/1069 """ properties = {'label': ['label1', 'label2'], 'cont_prop': [0]} pts = Points(properties=properties) current_props = {k: np.asarray(v[0]) for k, v in properties.items()} np.testing.assert_equal(pts.current_properties, current_props) # add two points and verify the default property was applied pts.add([10, 10]) pts.add([20, 20]) props = { 'label': np.array(['label1', 'label1']), 'cont_prop': np.array([0, 0], dtype=float), } np.testing.assert_equal(pts.properties, props)
def test_changing_modes(): """Test changing modes.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) assert layer.mode == 'pan_zoom' assert layer.interactive is True layer.mode = 'add' assert layer.mode == 'add' assert layer.interactive is False layer.mode = 'select' assert layer.mode == 'select' assert layer.interactive is False layer.mode = 'pan_zoom' assert layer.mode == 'pan_zoom' assert layer.interactive is True
def test_view_colors(): coords = [[0, 1, 1], [0, 2, 2], [1, 3, 3], [3, 3, 3]] face_color = np.array([[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 1, 1]]) edge_color = np.array([[0, 0, 1, 1], [1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]]) layer = Points(coords, face_color=face_color, edge_color=edge_color) layer._slice_dims([0, slice(None), slice(None)]) assert np.all(layer._view_face_color == face_color[[0, 1]]) assert np.all(layer._view_edge_color == edge_color[[0, 1]]) layer._slice_dims([1, slice(None), slice(None)]) assert np.all(layer._view_face_color == face_color[[2]]) assert np.all(layer._view_edge_color == edge_color[[2]]) # view colors should return empty array if there are no points layer._slice_dims([2, slice(None), slice(None)]) assert len(layer._view_face_color) == 0 assert len(layer._view_edge_color) == 0