def test_edge_color_map_non_numeric_property(): """Test setting edge_color as a color map of a non-numeric property raises an error """ np.random.seed(0) shape = (10, 2, 2) data = np.random.random(shape) data[:, 0, :] = 20 * data[:, 0, :] properties = {'vector_type': np.array(['A', 'B'] * int((shape[0] / 2)))} color_cycle = ['red', 'blue'] initial_color = [0, 1, 0, 1] layer = Vectors( data, properties=properties, edge_color=initial_color, edge_color_cycle=color_cycle, edge_colormap='gray', ) # layer should start out in direct edge color mode with all green vectors assert layer.edge_color_mode == 'direct' np.testing.assert_allclose(layer.edge_color, np.repeat([initial_color], shape[0], axis=0)) # switching to colormap mode should raise an error because the 'vector_type' is non-numeric layer.edge_color = 'vector_type' with pytest.raises(TypeError): layer.edge_color_mode = 'colormap'
def test_edge_color_direct(): """Test setting edge color.""" np.random.seed(0) data = np.random.random((10, 2, 2)) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) np.testing.assert_allclose( layer.edge_color, np.repeat([[1, 0, 0, 1]], data.shape[0], axis=0) ) # set edge color as an RGB array layer.edge_color = [0, 0, 1] np.testing.assert_allclose( layer.edge_color, np.repeat([[0, 0, 1, 1]], data.shape[0], axis=0) ) # set edge color as an RGBA array layer.edge_color = [0, 1, 0, 0.5] np.testing.assert_allclose( layer.edge_color, np.repeat([[0, 1, 0, 0.5]], data.shape[0], axis=0) ) # set all edge colors directly edge_colors = np.random.random((data.shape[0], 4)) layer.edge_color = edge_colors np.testing.assert_allclose(layer.edge_color, edge_colors)
def test_message(): """Test converting value and coords to message.""" np.random.seed(0) data = np.random.random((10, 2, 2)) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) msg = layer.get_message() assert type(msg) == str
def test_value(): """Test getting the value of the data at the current coordinates.""" np.random.seed(0) data = np.random.random((10, 2, 2)) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) value = layer.get_value((0, ) * 2) assert value is None
def test_xml_list(): """Test the xml generation.""" np.random.seed(0) data = np.random.random((10, 2, 2)) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) xml = layer.to_xml_list() assert type(xml) == list assert len(xml) == 10 assert np.all([type(x) == Element for x in xml])
def test_invalid_edge_color(): """Test providing an invalid edge color raises an exception""" np.random.seed(0) shape = (10, 2, 2) data = np.random.random(shape) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) with pytest.raises(ValueError): layer.edge_color = 5
def test_thumbnail(): """Test the image thumbnail for square data.""" np.random.seed(0) data = np.random.random((10, 2, 2)) data[:, 0, :] = 18 * data[:, 0, :] + 1 data[0, :, :] = [0, 0] data[-1, 0, :] = [20, 20] data[-1, 1, :] = [0, 0] layer = Vectors(data) layer._update_thumbnail() assert layer.thumbnail.shape == layer._thumbnail_shape
def test_big_thumbail(): """Test the image thumbnail with n_vectors > _max_vectors_thumbnail""" np.random.seed(0) n_vectors = int(1.5 * Vectors._max_vectors_thumbnail) data = np.random.random((n_vectors, 2, 2)) data[:, 0, :] = 18 * data[:, 0, :] + 1 data[0, :, :] = [0, 0] data[-1, 0, :] = [20, 20] data[-1, 1, :] = [0, 0] layer = Vectors(data) layer._update_thumbnail() assert layer.thumbnail.shape == layer._thumbnail_shape
def test_length(): """Test setting length.""" np.random.seed(0) data = np.random.random((10, 2, 2)) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) assert layer.length == 1 layer.length = 2 assert layer.length == 2 layer = Vectors(data, length=3) assert layer.length == 3
def test_name(): """Test setting layer name.""" np.random.seed(0) data = np.random.random((10, 2, 2)) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) assert layer.name == 'Vectors' layer = Vectors(data, name='random') assert layer.name == 'random' layer.name = 'vcts' assert layer.name == 'vcts'
def test_edge_color(): """Test setting edge color.""" np.random.seed(0) data = np.random.random((10, 2, 2)) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) assert layer.edge_color == 'red' layer.edge_color = 'blue' assert layer.edge_color == 'blue' layer = Vectors(data, edge_color='green') assert layer.edge_color == 'green'
def test_edge_width(): """Test setting edge width.""" np.random.seed(0) data = np.random.random((10, 2, 2)) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) assert layer.edge_width == 1 layer.edge_width = 2 assert layer.edge_width == 2 layer = Vectors(data, edge_width=3) assert layer.edge_width == 3
def test_value_3d(position, view_direction, dims_displayed, world): """Currently get_value should return None in 3D""" np.random.seed(0) data = np.random.random((10, 2, 3)) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) layer._slice_dims([0, 0, 0], ndisplay=3) value = layer.get_value( position, view_direction=view_direction, dims_displayed=dims_displayed, world=world, ) assert value is None
def test_edge_color_colormap(): """Test creating Vectors where edge color is set by a colormap""" shape = (10, 2) shape = (10, 2, 2) data = np.random.random(shape) data[:, 0, :] = 20 * data[:, 0, :] properties = {'angle': np.array([0, 1.5] * int(shape[0] / 2))} layer = Vectors( data, properties=properties, edge_color='angle', edge_colormap='gray', ) np.testing.assert_equal(layer.properties, properties) 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 - edge_color should not change layer.edge_color_cycle = ['red', 'blue'] assert np.all(layer.edge_color == edge_color_array) # adjust the clims layer.edge_contrast_limits = (0, 3) layer.refresh_colors(update_color_mapping=False) np.testing.assert_allclose(layer.edge_color[-1], [0.5, 0.5, 0.5, 1]) # change the colormap new_colormap = 'viridis' layer.edge_colormap = new_colormap assert layer.edge_colormap.name == new_colormap # test adding a colormap with a vispy Colormap object layer.edge_colormap = get_colormap('gray') assert 'unnamed colormap' in layer.edge_colormap.name
def test_changing_data(): """Test changing Vectors data.""" shape_a = (10, 2, 2) np.random.seed(0) data_a = np.random.random(shape_a) data_a[:, 0, :] = 20 * data_a[:, 0, :] shape_b = (16, 2, 2) data_b = np.random.random(shape_b) data_b[:, 0, :] = 20 * data_b[:, 0, :] layer = Vectors(data_b) layer.data = data_b assert np.all(layer.data == data_b) assert layer.data.shape == shape_b assert layer.ndim == shape_b[2] assert layer._view_data.shape[2] == 2
def test_empty_layer_with_edge_color_cycle(): """ Test creating an empty layer where the edge color is a color cycle """ shape = (0, 2, 2) data = np.empty(shape) default_properties = {'vector_type': np.array(['A'])} layer = Vectors( data=data, properties=default_properties, edge_color='vector_type', ) assert layer.edge_color_mode == 'cycle' # edge_color should remain empty when refreshing colors layer.refresh_colors(update_color_mapping=True) np.testing.assert_equal(layer.edge_color, np.empty((0, 4)))
def test_properties_color_mode_without_properties(): """Test that switching to a colormode requiring properties without properties defined raises an exceptions """ np.random.seed(0) shape = (10, 2, 2) data = np.random.random(shape) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) assert layer.properties == {} with pytest.raises(ValueError): layer.edge_color_mode = 'colormap' with pytest.raises(ValueError): layer.edge_color_mode = 'cycle'
def test_empty_layer_with_edge_colormap(): """Test creating an empty layer where the edge color is a colormap""" shape = (0, 2, 2) data = np.empty(shape) default_properties = {'angle': np.array([1.5], dtype=float)} layer = Vectors( data=data, property_choices=default_properties, edge_color='angle', edge_colormap='grays', ) assert layer.edge_color_mode == 'colormap' # edge_color should remain empty when refreshing colors layer.refresh_colors(update_color_mapping=True) np.testing.assert_equal(layer.edge_color, np.empty((0, 4)))
def test_empty_vectors(): """Test instantiating Vectors layer with empty coordinate-like 2D data.""" shape = (0, 2, 2) data = np.empty(shape) layer = Vectors(data) assert np.all(layer.data == data) assert layer.data.shape == shape assert layer.ndim == shape[2] assert layer._view_data.shape[2] == 2
def test_properties_dataframe(): """test if properties can be provided as a DataFrame""" shape = (10, 2) np.random.seed(0) shape = (10, 2, 2) data = np.random.random(shape) data[:, 0, :] = 20 * data[:, 0, :] properties = {'vector_type': np.array(['A', 'B'] * int(shape[0] / 2))} properties_df = pd.DataFrame(properties) properties_df = properties_df.astype(properties['vector_type'].dtype) layer = Vectors(data, properties=properties_df) np.testing.assert_equal(layer.properties, properties) # test adding a dataframe via the properties setter properties_2 = {'vector_type2': np.array(['A', 'B'] * int(shape[0] / 2))} properties_df2 = pd.DataFrame(properties_2) layer.properties = properties_df2 np.testing.assert_equal(layer.properties, properties_2)
def test_random_3D_vectors_image(): """Test instantiating Vectors layer with random image-like 3D data.""" shape = (12, 20, 10, 3) np.random.seed(0) data = np.random.random(shape) layer = Vectors(data) assert layer.data.shape == (12 * 20 * 10, 2, 3) assert layer.ndim == 3 assert layer._data_view.shape[2] == 2
def test_random_vectors_image(): """Test instantiating Vectors layer with random image-like 2D data.""" shape = (20, 10, 2) np.random.seed(0) data = np.random.random(shape) layer = Vectors(data) assert layer.data.shape == (20 * 10, 2, 2) assert layer.ndim == 2 assert layer._view_data.shape[2] == 2
def test_world_data_extent(): """Test extent after applying transforms.""" # data input format is start position, then length. data = [[(7, -5, -3), (1, -1, 2)], [(0, 0, 0), (4, 30, 12)]] min_val = (0, -6, -3) max_val = (8, 30, 12) layer = Vectors(np.array(data)) extent = np.array((min_val, max_val)) check_layer_world_data_extent(layer, extent, (3, 1, 1), (10, 20, 5))
def test_empty_vectors_with_properties(): """Test instantiating Vectors layer with empty coordinate-like 2D data.""" shape = (0, 2, 2) data = np.empty(shape) properties = {'angle': np.array([0.5], dtype=np.float)} layer = Vectors(data, properties=properties) assert np.all(layer.data == data) assert layer.data.shape == shape assert layer.ndim == shape[2] assert layer._view_data.shape[2] == 2 np.testing.assert_equal(layer._property_choices, properties)
def test_random_vectors(): """Test instantiating Vectors layer with random coordinate-like 2D data.""" shape = (10, 2, 2) np.random.seed(0) data = np.random.random(shape) data[:, 0, :] = 20 * data[:, 0, :] layer = Vectors(data) assert np.all(layer.data == data) assert layer.data.shape == shape assert layer.ndim == shape[2] assert layer._view_data.shape[2] == 2
def layer_data_and_types(): np.random.seed(0) 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)), ] layer_data = [l.as_layer_data_tuple() for l in layers] layer_types = [ld[2] for ld in layer_data] return layer_data, layer_types
def test_adding_properties(): """test adding properties to a Vectors layer""" shape = (10, 2) np.random.seed(0) shape = (10, 2, 2) data = np.random.random(shape) data[:, 0, :] = 20 * data[:, 0, :] properties = {'vector_type': np.array(['A', 'B'] * int(shape[0] / 2))} layer = Vectors(data) # properties should start empty assert layer.properties == {} # add properties layer.properties = properties np.testing.assert_equal(layer.properties, properties) # removing a property that was the _edge_color_property should give a warning layer._edge_color_property = 'vector_type' properties_2 = { 'not_vector_type': np.array(['A', 'B'] * int(shape[0] / 2)) } with pytest.warns(UserWarning): layer.properties = properties_2 # adding properties with the wrong length should raise an exception bad_properties = {'vector_type': np.array(['A'])} with pytest.raises(ValueError): layer.properties = bad_properties
def test_data_setter(): n_vectors_0 = 10 shape = (n_vectors_0, 2, 3) np.random.seed(0) data = np.random.random(shape) data[:, 0, :] = 20 * data[:, 0, :] properties = { 'prop_0': np.random.random((n_vectors_0, )), 'prop_1': np.random.random((n_vectors_0, )), } layer = Vectors(data, properties=properties) assert len(layer.data) == n_vectors_0 assert len(layer.edge_color) == n_vectors_0 assert len(layer.properties['prop_0']) == n_vectors_0 assert len(layer.properties['prop_1']) == n_vectors_0 # set the data with more vectors n_vectors_1 = 20 data_1 = np.random.random((n_vectors_1, 2, 3)) data_1[:, 0, :] = 20 * data_1[:, 0, :] layer.data = data_1 assert len(layer.data) == n_vectors_1 assert len(layer.edge_color) == n_vectors_1 assert len(layer.properties['prop_0']) == n_vectors_1 assert len(layer.properties['prop_1']) == n_vectors_1 # set the data with fewer vectors n_vectors_2 = 5 data_2 = np.random.random((n_vectors_2, 2, 3)) data_2[:, 0, :] = 20 * data_2[:, 0, :] layer.data = data_2 assert len(layer.data) == n_vectors_2 assert len(layer.edge_color) == n_vectors_2 assert len(layer.properties['prop_0']) == n_vectors_2 assert len(layer.properties['prop_1']) == n_vectors_2
def test_out_of_slice_display(): """Test setting out_of_slice_display flag for 2D and 4D data.""" shape = (10, 2, 2) np.random.seed(0) data = 20 * np.random.random(shape) layer = Vectors(data) assert layer.out_of_slice_display is False layer.out_of_slice_display = True assert layer.out_of_slice_display is True layer = Vectors(data, out_of_slice_display=True) assert layer.out_of_slice_display is True shape = (10, 2, 4) data = 20 * np.random.random(shape) layer = Vectors(data) assert layer.out_of_slice_display is False layer.out_of_slice_display = True assert layer.out_of_slice_display is True layer = Vectors(data, out_of_slice_display=True) assert layer.out_of_slice_display is True
class Vectors2DSuite: """Benchmarks for the Vectors 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, 2)) self.layer = Vectors(self.data) def time_create_layer(self, n): """Time to create an image layer.""" Vectors(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_width(self, n): """Time to update width.""" self.layer.width = 2 def time_length(self, n): """Time to update length.""" self.layer.length = 2 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