def test_add_edge_color_cycle_to_empty_layer(): """ Test adding a point to an empty layer when edge color is a color cycle See: https://github.com/napari/napari/pull/1069 """ default_properties = {'point_type': np.array(['A'])} color_cycle = ['red', 'blue'] layer = Points( properties=default_properties, edge_color='point_type', edge_color_cycle=color_cycle, ) # verify the current_edge_color is correct edge_color = transform_color(color_cycle[0]) assert np.all(layer._current_edge_color == edge_color) # add a point layer.add([10, 10]) props = {'point_type': np.array(['A'])} edge_color = np.array([[1, 0, 0, 1]]) assert layer.properties == props np.testing.assert_allclose(layer.edge_color, edge_color) # add a point with a new property layer.selected_data = [] layer.current_properties = {'point_type': np.array(['B'])} layer.add([12, 12]) new_color = np.array([0, 0, 1, 1]) edge_color = np.vstack((edge_color, new_color)) new_properties = {'point_type': np.array(['A', 'B'])} np.testing.assert_allclose(layer.edge_color, edge_color) np.testing.assert_equal(layer.properties, new_properties)
def test_properties(): shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) properties = {'point_type': np.array(['A', 'B'] * int(shape[0] / 2))} layer = Points(data, properties=copy(properties)) assert layer.properties == properties # test removing points layer.selected_data = [0, 1] layer.remove_selected() remove_properties = properties['point_type'][2::] assert len(layer.properties['point_type']) == (shape[0] - 2) assert np.all(layer.properties['point_type'] == remove_properties) # test selection of properties layer.selected_data = [0] selected_annotation = layer.current_properties['point_type'] assert len(selected_annotation) == 1 assert selected_annotation[0] == 'A' # test adding properties layer.add([10, 10]) add_annotations = np.concatenate((remove_properties, ['A']), axis=0) assert np.all(layer.properties['point_type'] == add_annotations) # test copy/paste layer.selected_data = [0, 1] layer._copy_data() assert np.all(layer._clipboard['properties']['point_type'] == ['A', 'B']) layer._paste_data() paste_annotations = np.concatenate((add_annotations, ['A', 'B']), axis=0) assert np.all(layer.properties['point_type'] == paste_annotations)
def test_text_from_property_fstring(properties): """Test setting text with an f-string from the property value""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data, properties=copy(properties), text='type: {point_type}') expected_text = ['type: ' + v for v in properties['point_type']] np.testing.assert_equal(layer.text.values, expected_text) # test updating the text layer.text = 'type-ish: {point_type}' expected_text_2 = ['type-ish: ' + v for v in properties['point_type']] np.testing.assert_equal(layer.text.values, expected_text_2) # copy/paste layer.selected_data = {0} layer._copy_data() layer._paste_data() expected_text_3 = expected_text_2 + ['type-ish: A'] np.testing.assert_equal(layer.text.values, expected_text_3) # add point layer.selected_data = {0} new_shape = np.random.random((1, 2)) layer.add(new_shape) expected_text_4 = expected_text_3 + ['type-ish: A'] np.testing.assert_equal(layer.text.values, expected_text_4)
def test_color_colormap(attribute): """Test setting edge/face color with a colormap""" # create Points using with a colormap shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) properties = {'point_type': _make_cycled_properties([0, 1.5], shape[0])} points_kwargs = { 'properties': properties, f'{attribute}_color': 'point_type', f'{attribute}_colormap': 'gray', } layer = Points(data, **points_kwargs) assert layer.properties == properties color_mode = getattr(layer, f'{attribute}_color_mode') assert color_mode == 'colormap' color_array = transform_color(['black', 'white'] * int((shape[0] / 2))) attribute_color = getattr(layer, f'{attribute}_color') assert np.all(attribute_color == color_array) # change the color cycle - face_color should not change setattr(layer, f'{attribute}_color_cycle', ['red', 'blue']) attribute_color = getattr(layer, f'{attribute}_color') assert np.all(attribute_color == color_array) # Add new point and test its color coord = [18, 18] layer.selected_data = {0} layer.add(coord) attribute_color = getattr(layer, f'{attribute}_color') assert len(attribute_color) == shape[0] + 1 np.testing.assert_allclose( attribute_color, np.vstack((color_array, transform_color('black'))), ) # Check removing data adjusts colors correctly layer.selected_data = {0, 2} layer.remove_selected() assert len(layer.data) == shape[0] - 1 attribute_color = getattr(layer, f'{attribute}_color') assert len(attribute_color) == shape[0] - 1 np.testing.assert_allclose( attribute_color, np.vstack( (color_array[1], color_array[3:], transform_color('black'),) ), ) # adjust the clims setattr(layer, f'{attribute}_contrast_limits', (0, 3)) layer.refresh_colors(update_color_mapping=False) attribute_color = getattr(layer, f'{attribute}_color') np.testing.assert_allclose(attribute_color[-2], [0.5, 0.5, 0.5, 1]) # change the colormap new_colormap = 'viridis' setattr(layer, f'{attribute}_colormap', new_colormap) attribute_colormap = getattr(layer, f'{attribute}_colormap') assert attribute_colormap.name == new_colormap
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) # test adding a point with a new property value layer.selected_data = {} current_properties = layer.current_properties current_properties['point_type'] = np.array(['new']) layer.current_properties = current_properties layer.add([10, 10]) color_cycle_map = getattr(layer, f'{attribute}_color_cycle_map') assert 'new' in color_cycle_map np.testing.assert_allclose( color_cycle_map['new'], np.squeeze(transform_color(color_cycle[0])) )
def test_add_point_direct(attribute: str): """Test adding points to layer directly""" layer = Points() assert len(getattr(layer, f'{attribute}_color')) == 0 setattr(layer, f'current_{attribute}_color', 'red') coord = [18, 18] layer.add(coord) np.testing.assert_allclose([[1, 0, 0, 1]], getattr(layer, f'{attribute}_color'))
def test_edge_color_colormap(): # create Points using with face_color colormap shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) annotations = {'point_type': _make_cycled_properties([0, 1.5], shape[0])} layer = Points( data, properties=annotations, edge_color='point_type', edge_colormap='gray', ) assert layer.properties == annotations assert layer.edge_color_mode == 'colormap' edge_color_array = transform_color(['black', 'white'] * int( (shape[0] / 2))) assert np.all(layer.edge_color == edge_color_array) # change the color cycle - face_color should not change layer.edge_color_cycle = ['red', 'blue'] assert np.all(layer.edge_color == edge_color_array) # Add new point and test its color coord = [18, 18] layer.selected_data = {0} layer.add(coord) assert len(layer.edge_color) == shape[0] + 1 np.testing.assert_allclose( layer.edge_color, np.vstack((edge_color_array, transform_color('black'))), ) # Check removing data adjusts colors correctly layer.selected_data = {0, 2} layer.remove_selected() assert len(layer.data) == shape[0] - 1 assert len(layer.edge_color) == shape[0] - 1 np.testing.assert_allclose( layer.edge_color, np.vstack(( edge_color_array[1], edge_color_array[3:], transform_color('black'), )), ) # adjust the clims layer.edge_contrast_limits = (0, 3) layer.refresh_colors(update_color_mapping=False) np.testing.assert_allclose(layer.edge_color[-2], [0.5, 0.5, 0.5, 1]) # change the colormap new_colormap = 'viridis' layer.edge_colormap = new_colormap assert layer.edge_colormap[1] == get_colormap(new_colormap)
def test_face_color(): """Test setting face color.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) assert layer.face_color == 'white' assert len(layer.face_colors) == shape[0] assert np.all([col == 'white' for col in layer.face_colors]) # With no data selected chaning face color has no effect layer.face_color = 'blue' assert layer.face_color == 'blue' assert np.all([col == 'white' for col in layer.face_colors]) # Select data and change edge color of selection layer.selected_data = [0, 1] assert layer.face_color == 'white' layer.face_color = 'green' assert np.all([col == 'green' for col in layer.face_colors[:2]]) assert np.all([col == 'white' for col in layer.face_colors[2:]]) # Add new point and test its color coord = [18, 18] layer.selected_data = [] layer.face_color = 'blue' layer.add(coord) assert len(layer.face_colors) == shape[0] + 1 assert np.all([col == 'green' for col in layer.face_colors[:2]]) assert np.all([col == 'white' for col in layer.face_colors[2:10]]) assert np.all(layer.face_colors[10] == 'blue') # Instantiate with custom face color layer = Points(data, face_color='red') assert layer.face_color == 'red' # Instantiate with custom face color list col_list = ['red', 'green'] * 5 layer = Points(data, face_color=col_list) assert layer.face_color == 'white' assert layer.face_colors == col_list # Add new point and test its color coord = [18, 18] layer.face_color = 'blue' layer.add(coord) assert len(layer.face_colors) == shape[0] + 1 assert layer.face_colors == col_list + ['blue'] # Check removing data adjusts colors correctly layer.selected_data = [0, 2] layer.remove_selected() assert len(layer.data) == shape[0] - 1 assert len(layer.face_colors) == shape[0] - 1 assert layer.face_colors == [col_list[1]] + col_list[3:] + ['blue']
def test_adding_points_to_empty(): """Test adding Points data to empty.""" shape = (0, 2) data = np.empty(shape) layer = Points(data) assert len(layer.data) == 0 coord = [20, 20] layer.add(coord) assert len(layer.data) == 1 assert np.all(layer.data[0] == coord)
def test_add_points_with_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)) 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_face_color_colormap(): # create Points using with face_color colormap shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) annotations = {'point_type': np.array([0, 1.5] * int((shape[0] / 2)))} layer = Points( data, properties=annotations, face_color='point_type', face_colormap='gray', ) assert layer.properties == annotations assert layer.face_color_mode == 'colormap' face_color_array = transform_color( ['black', 'white'] * int((shape[0] / 2)) ) assert np.all(layer.face_color == face_color_array) # change the color cycle - face_color should not change layer.face_color_cycle = ['red', 'blue'] assert np.all(layer.face_color == face_color_array) # Add new point and test its color coord = [18, 18] layer.selected_data = {0} layer.add(coord) assert len(layer.face_color) == shape[0] + 1 np.testing.assert_allclose( layer.face_color, np.vstack((face_color_array, transform_color('black'))), ) # change the colormap new_colormap = 'viridis' layer.face_colormap = new_colormap assert layer.face_colormap[1] == get_colormap(new_colormap) # Check removing data adjusts colors correctly layer.selected_data = {0, 2} layer.remove_selected() assert len(layer.data) == shape[0] - 1 assert len(layer.face_color) == shape[0] - 1 np.testing.assert_allclose( layer.face_color, np.vstack( ( face_color_array[1], face_color_array[3:], transform_color('black'), ) ), )
def test_empty_layer_with_text_formatted(): """Test initializing an empty layer with text defined""" default_properties = {'point_type': np.array([1.5], dtype=float)} layer = Points( properties=default_properties, text='point_type: {point_type:.2f}', ) np.testing.assert_equal(layer.text.values, np.empty(0)) # add a point and check that the appropriate text value was added layer.add([1, 1]) np.testing.assert_equal(layer.text.values, ['point_type: 1.50'])
def test_adding_points(): """Test adding Points data.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) assert len(layer.data) == 10 coord = [20, 20] layer.add(coord) assert len(layer.data) == 11 assert np.all(layer.data[10] == coord)
def test_color_direct(attribute: str): """Test setting colors directly""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer_kwargs = {f'{attribute}_color': 'black'} layer = Points(data, **layer_kwargs) color_array = transform_color(['black'] * shape[0]) current_color = getattr(layer, f'current_{attribute}_color') layer_color = getattr(layer, f'{attribute}_color') assert current_color == 'black' assert len(layer.edge_color) == shape[0] np.testing.assert_allclose(color_array, layer_color) # With no data selected changing color has no effect setattr(layer, f'current_{attribute}_color', 'blue') current_color = getattr(layer, f'current_{attribute}_color') assert current_color == 'blue' np.testing.assert_allclose(color_array, layer_color) # Select data and change edge color of selection selected_data = {0, 1} layer.selected_data = {0, 1} current_color = getattr(layer, f'current_{attribute}_color') assert current_color == 'black' setattr(layer, f'current_{attribute}_color', 'green') colorarray_green = transform_color(['green'] * len(layer.selected_data)) color_array[list(selected_data)] = colorarray_green layer_color = getattr(layer, f'{attribute}_color') np.testing.assert_allclose(color_array, layer_color) # Add new point and test its color coord = [18, 18] layer.selected_data = {} setattr(layer, f'current_{attribute}_color', 'blue') layer.add(coord) color_array = np.vstack([color_array, transform_color('blue')]) layer_color = getattr(layer, f'{attribute}_color') assert len(layer_color) == shape[0] + 1 np.testing.assert_allclose(color_array, layer_color) # 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:])), )
def test_size_with_arrays(): """Test setting size with arrays.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) sizes = 5 * np.random.random(shape) layer.size = sizes assert np.all(layer.size == sizes) # Test broadcasting of sizes sizes = [5, 5] layer.size = sizes assert np.all(layer.size[0] == sizes) # Create new layer with new size array data sizes = 5 * np.random.random(shape) layer = Points(data, size=sizes) assert layer.current_size == 10 assert layer.size.shape == shape assert np.all(layer.size == sizes) # Create new layer with new size array data sizes = [5, 5] layer = Points(data, size=sizes) assert layer.current_size == 10 assert layer.size.shape == shape assert np.all(layer.size[0] == sizes) # Add new point, should have new size coord = [18, 18] layer.current_size = 13 layer.add(coord) assert layer.size.shape == (11, 2) assert np.unique(layer.size[:10])[0] == 5 assert np.all(layer.size[10] == [13, 13]) # Select data and change size layer.selected_data = [0, 1] assert layer.current_size == 5 layer.current_size = 16 assert layer.size.shape == (11, 2) assert np.unique(layer.size[2:10])[0] == 5 assert np.unique(layer.size[:2])[0] == 16 # Check removing data adjusts colors correctly layer.selected_data = [0, 2] layer.remove_selected() assert len(layer.data) == 9 assert len(layer.size) == 9 assert np.all(layer.size[0] == [16, 16]) assert np.all(layer.size[1] == [5, 5])
def test_edge_color_cycle(): # create Points using list color cycle shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) annotations = {'point_type': _make_cycled_properties(['A', 'B'], shape[0])} color_cycle = ['red', 'blue'] layer = Points( data, properties=annotations, edge_color='point_type', edge_color_cycle=color_cycle, ) assert layer.properties == annotations edge_color_array = transform_color(color_cycle * int((shape[0] / 2))) assert np.all(layer.edge_color == edge_color_array) # create Points using color array color cycle color_cycle_array = transform_color(color_cycle) layer2 = Points( data, properties=annotations, edge_color='point_type', edge_color_cycle=color_cycle_array, ) assert np.all(layer2.edge_color == edge_color_array) # Add new point and test its color coord = [18, 18] layer2.selected_data = {0} layer2.add(coord) assert len(layer2.edge_color) == shape[0] + 1 np.testing.assert_allclose( layer2.edge_color, np.vstack((edge_color_array, transform_color('red'))), ) # Check removing data adjusts colors correctly layer2.selected_data = {0, 2} layer2.remove_selected() assert len(layer2.data) == shape[0] - 1 assert len(layer2.edge_color) == shape[0] - 1 np.testing.assert_allclose( layer2.edge_color, np.vstack((edge_color_array[1], edge_color_array[3:], transform_color('red'))), ) # refresh colors layer.refresh_colors(update_color_mapping=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_face_color_cycle(): # create Points using list color cycle shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) annotations = {'point_type': np.array(['A', 'B'] * int((shape[0] / 2)))} color_cycle = ['red', 'blue'] layer = Points( data, properties=annotations, face_color='point_type', face_color_cycle=color_cycle, ) assert layer.properties == annotations face_color_array = transform_color(color_cycle * int((shape[0] / 2))) assert np.all(layer.face_color == face_color_array) # create Points using color array color cycle color_cycle_array = transform_color(color_cycle) layer2 = Points( data, properties=annotations, face_color='point_type', face_color_cycle=color_cycle_array, ) assert np.all(layer2.face_color == face_color_array) # Add new point and test its color coord = [18, 18] layer2.selected_data = [0] layer2.add(coord) assert len(layer2.face_color) == shape[0] + 1 np.testing.assert_allclose( layer2.face_color, np.vstack((face_color_array, transform_color('red'))), ) # Check removing data adjusts colors correctly layer2.selected_data = [0, 2] layer2.remove_selected() assert len(layer2.data) == shape[0] - 1 assert len(layer2.face_color) == shape[0] - 1 np.testing.assert_allclose( layer2.face_color, np.vstack( (face_color_array[1], face_color_array[3:], transform_color('red')) ), )
def test_size(): """Test setting size with scalar.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) assert layer.current_size == 10 assert layer.size.shape == shape assert np.unique(layer.size)[0] == 10 # Add a new point, it should get current size coord = [17, 17] layer.add(coord) assert layer.size.shape == (11, 2) assert np.unique(layer.size)[0] == 10 # Setting size affects newly added points not current points layer.current_size = 20 assert layer.current_size == 20 assert layer.size.shape == (11, 2) assert np.unique(layer.size)[0] == 10 # Add new point, should have new size coord = [18, 18] layer.add(coord) assert layer.size.shape == (12, 2) assert np.unique(layer.size[:11])[0] == 10 assert np.all(layer.size[11] == [20, 20]) # Select data and change size layer.selected_data = [0, 1] assert layer.current_size == 10 layer.current_size = 16 assert layer.size.shape == (12, 2) assert np.unique(layer.size[2:11])[0] == 10 assert np.unique(layer.size[:2])[0] == 16 # Select data and size changes layer.selected_data = [11] assert layer.current_size == 20 # Create new layer with new size data layer = Points(data, size=15) assert layer.current_size == 15 assert layer.size.shape == shape assert np.unique(layer.size)[0] == 15
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_adding_points(): """Test adding Points data.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) assert len(layer.data) == 10 coord = [20, 20] layer.add(coord) assert len(layer.data) == 11 assert np.all(layer.data[10] == coord) # the added point should be selected assert layer.selected_data == {10} # test adding multiple points coords = [[10, 10], [15, 15]] layer.add(coords) assert len(layer.data) == 13 assert np.all(layer.data[11:, :] == coords)
class Points2DSuite: """Benchmarks for the Points layer with 2D data""" params = [2**i for i in range(4, 18, 2)] def setup(self, n): np.random.seed(0) self.data = np.random.random((n, 2)) self.layer = Points(self.data) def time_create_layer(self, n): """Time to create layer.""" Points(self.data) def time_refresh(self, n): """Time to refresh view.""" self.layer.refresh() def time_set_view_slice(self, n): """Time to set view slice.""" self.layer._set_view_slice() def time_update_thumbnail(self, n): """Time to update thumbnail.""" self.layer._update_thumbnail() def time_get_value(self, n): """Time to get current value.""" self.layer.get_value((0, ) * 2) def time_add(self, n): self.layer.add(self.data) def mem_layer(self, n): """Memory used by layer.""" return self.layer def mem_data(self, n): """Memory used by raw data.""" return self.data
def test_add_color_cycle_to_empty_layer(attribute): """Test adding a point to an empty layer when edge/face color is a color cycle See: https://github.com/napari/napari/pull/1069 """ default_properties = {'point_type': np.array(['A'])} color_cycle = ['red', 'blue'] points_kwargs = { 'properties': default_properties, f'{attribute}_color': 'point_type', f'{attribute}_color_cycle': color_cycle, } layer = Points(**points_kwargs) # verify the current_edge_color is correct expected_color = transform_color(color_cycle[0])[0] color_manager = getattr(layer, f'_{attribute}') current_color = color_manager.current_color np.testing.assert_allclose(current_color, expected_color) # add a point layer.add([10, 10]) props = {'point_type': np.array(['A'])} expected_color = np.array([[1, 0, 0, 1]]) np.testing.assert_equal(layer.properties, props) attribute_color = getattr(layer, f'{attribute}_color') np.testing.assert_allclose(attribute_color, expected_color) # add a point with a new property layer.selected_data = [] layer.current_properties = {'point_type': np.array(['B'])} layer.add([12, 12]) new_color = np.array([0, 0, 1, 1]) expected_color = np.vstack((expected_color, new_color)) new_properties = {'point_type': np.array(['A', 'B'])} attribute_color = getattr(layer, f'{attribute}_color') np.testing.assert_allclose(attribute_color, expected_color) np.testing.assert_equal(layer.properties, new_properties)
def test_empty_points_with_properties(): """ Test instantiating an empty Points layer with properties See: https://github.com/napari/napari/pull/1069 """ properties = { 'label': np.array(['label1', 'label2']), 'cont_prop': np.array([0], dtype=np.float), } pts = Points(properties=properties) current_props = {k: v[0] for k, v in properties.items()} np.testing.assert_equal(pts.current_properties, current_props) # verify the property datatype is correct assert pts.properties['cont_prop'].dtype == np.float # 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=np.float), } np.testing.assert_equal(pts.properties, props)
def test_adding_points(): """Test adding Points data.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) assert len(layer.data) == 10 coord = [20, 20] layer.add(coord) assert len(layer.data) == 11 assert np.all(layer.data[10] == coord) # the added point should be selected assert layer.selected_data == {10} # test adding multiple points coords = [[10, 10], [15, 15]] layer.add(coords) assert len(layer.data) == 13 assert np.all(layer.data[11:, :] == coords) # test that the last added points can be deleted layer.remove_selected() np.testing.assert_equal(layer.data, np.vstack((data, coord)))
def test_face_color_direct(): """Test setting face color.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) colorarray = transform_color(['white'] * shape[0]) assert layer.current_face_color == 'white' assert len(layer.face_color) == shape[0] np.testing.assert_allclose(colorarray, layer.face_color) # With no data selected chaning face color has no effect layer.current_face_color = 'blue' assert layer.current_face_color == 'blue' np.testing.assert_allclose(colorarray, layer.face_color) # Select data and change edge color of selection layer.selected_data = [0, 1] assert layer.current_face_color == 'white' layer.current_face_color = transform_color('green') colorarray_green = transform_color(['green'] * len(layer.selected_data)) np.testing.assert_allclose(colorarray_green, layer.face_color[:2]) np.testing.assert_allclose(colorarray[2:], layer.face_color[2:]) # Add new point and test its color coord = [18, 18] layer.selected_data = [] layer.current_face_color = 'blue' layer.add(coord) colorarray = np.vstack((colorarray, transform_color('blue'))) assert len(layer.face_color) == shape[0] + 1 np.testing.assert_allclose(colorarray_green, layer.face_color[:2]) np.testing.assert_allclose(colorarray[2:], layer.face_color[2:]) np.testing.assert_allclose(transform_color("blue"), np.atleast_2d(layer.face_color[10])) # Instantiate with custom face color layer = Points(data, face_color='red') assert layer.current_face_color == 'red' # Instantiate with custom face color list col_list = transform_color(['red', 'green'] * 5) layer = Points(data, face_color=col_list) assert layer.current_face_color == 'green' np.testing.assert_allclose(layer.face_color, col_list) # Add new point and test its color coord = [18, 18] layer.current_face_color = 'blue' layer.add(coord) assert len(layer.face_color) == shape[0] + 1 np.testing.assert_allclose(layer.face_color, np.vstack((col_list, transform_color('blue')))) # Check removing data adjusts colors correctly layer.selected_data = [0, 2] layer.remove_selected() assert len(layer.data) == shape[0] - 1 assert len(layer.face_color) == shape[0] - 1 np.testing.assert_allclose( layer.face_color, np.vstack((col_list[1], col_list[3:], transform_color('blue'))), )
def test_size_with_3D_arrays(): """Test setting size with 3D arrays.""" shape = (10, 3) np.random.seed(0) data = 20 * np.random.random(shape) data[:2, 0] = 0 layer = Points(data) assert layer.current_size == 10 assert layer.size.shape == shape assert np.unique(layer.size)[0] == 10 sizes = 5 * np.random.random(shape) layer.size = sizes assert np.all(layer.size == sizes) # Test broadcasting of sizes sizes = [1, 5, 5] layer.size = sizes assert np.all(layer.size[0] == sizes) # Create new layer with new size array data sizes = 5 * np.random.random(shape) layer = Points(data, size=sizes) assert layer.current_size == 10 assert layer.size.shape == shape assert np.all(layer.size == sizes) # Create new layer with new size array data sizes = [1, 5, 5] layer = Points(data, size=sizes) assert layer.current_size == 10 assert layer.size.shape == shape assert np.all(layer.size[0] == sizes) # Add new point, should have new size in last dim only coord = [4, 18, 18] layer.current_size = 13 layer.add(coord) assert layer.size.shape == (11, 3) assert np.unique(layer.size[:10, 1:])[0] == 5 assert np.all(layer.size[10] == [1, 13, 13]) # Select data and change size layer.selected_data = [0, 1] assert layer.current_size == 5 layer.current_size = 16 assert layer.size.shape == (11, 3) assert np.unique(layer.size[2:10, 1:])[0] == 5 assert np.all(layer.size[0] == [16, 16, 16]) # Create new 3D layer with new 2D points size data sizes = [0, 5, 5] layer = Points(data, size=sizes) assert layer.current_size == 10 assert layer.size.shape == shape assert np.all(layer.size[0] == sizes) # Add new point, should have new size only in last 2 dimensions coord = [4, 18, 18] layer.current_size = 13 layer.add(coord) assert layer.size.shape == (11, 3) assert np.all(layer.size[10] == [0, 13, 13]) # Select data and change size layer.selected_data = [0, 1] assert layer.current_size == 5 layer.current_size = 16 assert layer.size.shape == (11, 3) assert np.unique(layer.size[2:10, 1:])[0] == 5 assert np.all(layer.size[0] == [0, 16, 16])
def test_size_with_arrays(): """Test setting size with arrays.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) sizes = 5 * np.random.random(shape) layer.size = sizes assert np.all(layer.size == sizes) # Test broadcasting of sizes sizes = [5, 5] layer.size = sizes assert np.all(layer.size[0] == sizes) # Test broadcasting of transposed sizes sizes = np.random.randint(low=1, high=5, size=shape[::-1]) layer.size = sizes np.testing.assert_equal(layer.size, sizes.T) # Un-broadcastable array should raise an exception bad_sizes = np.random.randint(low=1, high=5, size=(3, 8)) with pytest.raises(ValueError): layer.size = bad_sizes # Create new layer with new size array data sizes = 5 * np.random.random(shape) layer = Points(data, size=sizes) assert layer.current_size == 10 assert layer.size.shape == shape assert np.all(layer.size == sizes) # Create new layer with new size array data sizes = [5, 5] layer = Points(data, size=sizes) assert layer.current_size == 10 assert layer.size.shape == shape assert np.all(layer.size[0] == sizes) # Add new point, should have new size coord = [18, 18] layer.current_size = 13 layer.add(coord) assert layer.size.shape == (11, 2) assert np.unique(layer.size[:10])[0] == 5 assert np.all(layer.size[10] == [13, 13]) # Select data and change size layer.selected_data = {0, 1} assert layer.current_size == 5 layer.current_size = 16 assert layer.size.shape == (11, 2) assert np.unique(layer.size[2:10])[0] == 5 assert np.unique(layer.size[:2])[0] == 16 # Check removing data adjusts colors correctly layer.selected_data = {0, 2} layer.remove_selected() assert len(layer.data) == 9 assert len(layer.size) == 9 assert np.all(layer.size[0] == [16, 16]) assert np.all(layer.size[1] == [5, 5])