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_copy_and_paste(): """Test copying and pasting selected points.""" shape = (10, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Points(data) # Clipboard starts empty assert layer._clipboard == {} # Pasting empty clipboard doesn't change data layer._paste_data() assert len(layer.data) == 10 # Copying with nothing selected leave clipboard empty layer._copy_data() assert layer._clipboard == {} # Copying and pasting with two points selected adds to clipboard and data layer.selected_data = [0, 1] layer._copy_data() layer._paste_data() assert len(layer._clipboard.keys()) > 0 assert len(layer.data) == shape[0] + 2 assert np.all(layer.data[:2] == layer.data[-2:]) # Pasting again adds two more points to data layer._paste_data() assert len(layer.data) == shape[0] + 4 assert np.all(layer.data[:2] == layer.data[-2:]) # Unselecting everything and copying and pasting will empty the clipboard # and add no new data layer.selected_data = [] layer._copy_data() layer._paste_data() assert layer._clipboard == {} assert len(layer.data) == shape[0] + 4