def test_invalid_color_properties(c_props): colors = np.array([[1, 1, 1, 1], [1, 0, 0, 1], [0, 0, 0, 1]]) with pytest.raises(ValidationError): _ = ColorManager(colors=colors, color_properties=c_props, color_mode='direct')
def test_init_color_manager_cycle_with_colors_dict(): """Test initializing color cycle ColorManager from layer kwargs where the colors are given as a dictionary of ColorManager fields/values """ n_colors = 10 color_cycle = [[0, 0, 0, 1], [1, 1, 1, 1]] properties = {'point_type': _make_cycled_properties(['A', 'B'], n_colors)} colors_dict = { 'color_properties': 'point_type', 'color_mode': 'cycle', 'categorical_colormap': color_cycle, } color_manager = ColorManager._from_layer_kwargs( colors=colors_dict, properties=properties, n_colors=n_colors, continuous_colormap='viridis', ) assert len(color_manager.colors) == n_colors assert color_manager.color_mode == 'cycle' color_array = transform_color(list(islice(cycle(color_cycle), 0, n_colors))) np.testing.assert_allclose(color_manager.colors, color_array) assert color_manager.color_properties.current_value == 'B' assert color_manager.continuous_colormap.name == 'viridis'
def test_categorical_colormap_from_dict(cat_cmap, expected): colors = np.array([[1, 1, 1, 1], [1, 0, 0, 1], [0, 0, 0, 1]]) cm = ColorManager(colors=colors, categorical_colormap=cat_cmap, color_mode='direct') np.testing.assert_equal(cm.categorical_colormap.colormap, expected[0]) np.testing.assert_almost_equal( cm.categorical_colormap.fallback_color.values, expected[1])
def test_invalid_categorical_colormap(): colors = np.array([[1, 1, 1, 1], [1, 0, 0, 1], [0, 0, 0, 1]]) invalid_cmap = 42 with pytest.raises(ValidationError): _ = ColorManager( colors=colors, categorical_colormap=invalid_cmap, color_mode='direct', )
def test_color_cycle(color_cycle): """Test setting color with a color cycle list""" # create Points using list color cycle n_colors = 10 properties = { 'name': 'point_type', 'values': _make_cycled_properties(['A', 'B'], n_colors), } cm = ColorManager( color_mode='cycle', color_properties=properties, categorical_colormap=color_cycle, ) color_mode = cm.color_mode assert color_mode == 'cycle' color_array = transform_color(list(islice(cycle(color_cycle), 0, n_colors))) np.testing.assert_allclose(cm.colors, color_array) # Add 2 color elements and test their color cm._add('A', n_colors=2) cm_colors = cm.colors assert len(cm_colors) == n_colors + 2 np.testing.assert_allclose( cm_colors, np.vstack( (color_array, transform_color('red'), transform_color('red'))), ) # Check removing data adjusts colors correctly cm._remove({0, 2, 11}) cm_colors_2 = cm.colors assert len(cm_colors_2) == (n_colors - 1) np.testing.assert_allclose( cm_colors_2, np.vstack((color_array[1], color_array[3:], transform_color('red'))), ) # update the colormap cm.categorical_colormap = ['black', 'white'] # the first color should now be black np.testing.assert_allclose(cm.colors[0], [0, 0, 0, 1]) # test pasting values paste_props = {'point_type': np.array(['B', 'B'])} paste_colors = np.array([[0, 0, 0, 1], [0, 0, 0, 1]]) cm._paste(colors=paste_colors, properties=paste_props) np.testing.assert_allclose(cm.colors[-2:], paste_colors)
def test_color_manager_invalid_color_properties(): """Passing an invalid property name for color_properties should raise a KeyError """ n_colors = 10 color_cycle = [[0, 0, 0, 1], [1, 1, 1, 1]] properties = {'point_type': _make_cycled_properties([0, 1.5], n_colors)} colors_dict = { 'color_properties': 'not_point_type', 'color_mode': 'colormap', 'categorical_colormap': color_cycle, 'continuous_colormap': 'gray', } with pytest.raises(KeyError): _ = ColorManager._from_layer_kwargs(colors=colors_dict, properties=properties, n_colors=n_colors)
def test_refresh_colors(): # create ColorManager with a continuous colormap n_colors = 4 properties = { 'name': 'point_type', 'values': _make_cycled_properties([0, 1.5], n_colors), } cm = ColorManager( color_properties=properties, continuous_colormap='gray', color_mode='colormap', ) color_mode = cm.color_mode assert color_mode == 'colormap' color_array = transform_color(['black', 'white'] * int(n_colors / 2)) colors = cm.colors.copy() np.testing.assert_allclose(colors, color_array) np.testing.assert_allclose(cm.current_color, [1, 1, 1, 1]) # after refresh, the color should now be white. since we didn't # update the color mapping, the other values should remain # unchanged even though we added a value that extends the range # of values new_properties = {'point_type': properties['values']} new_properties['point_type'][0] = 3 cm._refresh_colors(new_properties, update_color_mapping=False) new_colors = color_array.copy() new_colors[0] = [1, 1, 1, 1] np.testing.assert_allclose(cm.colors, new_colors) # now, refresh the colors, but update the mapping cm._refresh_colors(new_properties, update_color_mapping=True) refreshed_colors = [ [1, 1, 1, 1], [0.5, 0.5, 0.5, 1], [0, 0, 0, 1], [0.5, 0.5, 0.5, 1], ] np.testing.assert_allclose(cm.colors, refreshed_colors)
def test_color_manager_empty(): cm = ColorManager() np.testing.assert_allclose(cm.colors, np.empty((0, 4))) assert cm.color_mode == 'direct'
def test_continuous_colormap(): # create ColorManager with a continuous colormap n_colors = 10 properties = { 'name': 'point_type', 'values': _make_cycled_properties([0, 1.5], n_colors), } cm = ColorManager( color_properties=properties, continuous_colormap='gray', color_mode='colormap', ) color_mode = cm.color_mode assert color_mode == 'colormap' color_array = transform_color(['black', 'white'] * int(n_colors / 2)) colors = cm.colors.copy() np.testing.assert_allclose(colors, color_array) np.testing.assert_allclose(cm.current_color, [1, 1, 1, 1]) # Add 2 color elements and test their color cm._add(0, n_colors=2) cm_colors = cm.colors assert len(cm_colors) == n_colors + 2 np.testing.assert_allclose( cm_colors, np.vstack( (color_array, transform_color('black'), transform_color('black'))), ) # Check removing data adjusts colors correctly cm._remove({0, 2, 11}) cm_colors_2 = cm.colors assert len(cm_colors_2) == (n_colors - 1) np.testing.assert_allclose( cm_colors_2, np.vstack((color_array[1], color_array[3:], transform_color('black'))), ) # adjust the clims cm.contrast_limits = (0, 3) updated_colors = cm.colors np.testing.assert_allclose(updated_colors[-2], [0.5, 0.5, 0.5, 1]) # first verify that prop value 0 is colored black current_colors = cm.colors np.testing.assert_allclose(current_colors[-1], [0, 0, 0, 1]) # change the colormap new_colormap = 'gray_r' cm.continuous_colormap = new_colormap assert cm.continuous_colormap.name == new_colormap # the props valued 0 should now be white updated_colors = cm.colors np.testing.assert_allclose(updated_colors[-1], [1, 1, 1, 1]) # test pasting values paste_props = {'point_type': np.array([0, 0])} paste_colors = np.array([[1, 1, 1, 1], [1, 1, 1, 1]]) cm._paste(colors=paste_colors, properties=paste_props) np.testing.assert_allclose(cm.colors[-2:], paste_colors)
def test_current_color_coercion(curr_color, expected): colors = np.array([[1, 1, 1, 1], [1, 0, 0, 1], [0, 0, 0, 1]]) cm = ColorManager(colors=colors, current_color=curr_color, color_mode='direct') np.testing.assert_allclose(cm.current_color, expected)
def test_color_properties_coercion(c_props, expected): colors = np.array([[1, 1, 1, 1], [1, 0, 0, 1], [0, 0, 0, 1]]) cm = ColorManager(colors=colors, color_properties=c_props, color_mode='direct') assert cm.color_properties == expected