def test_palettize(self): """Test palettize """ cm_ = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0)), (3, (1, 1, 1)), (4, (0, 0, 0))) data = np.array([1, 2, 3, 4]) channels, colors = cm_.palettize(data) self.assertTrue(np.allclose(colors, cm_.colors)) self.assertTrue(all(channels == [0, 1, 2, 3])) cm_ = colormap.Colormap((0, (0.0, 0.0, 0.0)), (1, (1.0, 1.0, 1.0)), (2, (2, 2, 2)), (3, (3, 3, 3))) data = np.arange(-1, 5) channels, colors = cm_.palettize(data) self.assertTrue(np.allclose(colors, cm_.colors)) self.assertTrue(all(channels == [0, 0, 1, 2, 3, 3])) data = np.arange(-1.0, 5.0) data[-1] = np.nan channels, colors = cm_.palettize(data) self.assertTrue(np.allclose(colors, cm_.colors)) self.assertTrue(all(channels == [0, 0, 1, 2, 3, 3]))
def test_only_colors_only_values(self): """Test passing only colors or only values keyword arguments.""" with pytest.raises(ValueError, match=r"Both 'colors' and 'values'.*"): colormap.Colormap(colors=np.arange(5 * 3, dtype=np.float64).reshape( (5, 3)), ) with pytest.raises(ValueError, match=r"Both 'colors' and 'values'.*"): colormap.Colormap(values=np.linspace(0, 1, 5 * 3), )
def test_add(self): """Test adding colormaps.""" cm_ = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0)), (3, (1, 1, 1)), (4, (0, 0, 0))) cm1 = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0))) cm2 = colormap.Colormap((3, (1.0, 1.0, 1.0)), (4, (0.0, 0.0, 0.0))) cm3 = cm1 + cm2 self.assertTrue(np.allclose(cm3.colors, cm_.colors)) self.assertTrue(np.allclose(cm3.values, cm_.values))
def test_merge_rgb_rgba(self, colors1, colors2): """Test that two colormaps with RGB or RGBA colors can be merged.""" cmap1 = colormap.Colormap( values=np.linspace(0.2, 0.5, colors1.shape[0]), colors=colors1, ) cmap2 = colormap.Colormap( values=np.linspace(0.51, 0.8, colors2.shape[0]), colors=colors2, ) new_cmap = cmap1 + cmap2 assert new_cmap.values.shape[0] == colors1.shape[0] + colors2.shape[0]
def test_merge_equal_values(self): """Test that merged colormaps can have equal values at the merge point.""" cmap1 = colormap.Colormap( colors=np.arange(5 * 3).reshape((5, 3)), values=np.linspace(0, 1, 5), ) cmap2 = colormap.Colormap( colors=np.arange(5 * 3).reshape((5, 3)), values=np.linspace(1, 2, 5), ) assert cmap1.values[-1] == cmap2.values[0] # this should succeed _ = cmap1 + cmap2
def test_merge_nonmonotonic(self): """Test that merged colormaps must have monotonic values.""" cmap1 = colormap.Colormap( colors=np.arange(5 * 3).reshape((5, 3)), values=np.linspace(2, 3, 5), ) cmap2 = colormap.Colormap( colors=np.arange(5 * 3).reshape((5, 3)), values=np.linspace(0, 1, 5), ) with pytest.raises(ValueError, match=r".*monotonic.*"): cmap1 + cmap2 # this should succeed cmap2 + cmap1
def test_bad_color_dims(self): """Test passing colors that aren't RGB or RGBA.""" # Nonsense with pytest.raises(ValueError, match=r".*colors.*shape.*"): colormap.Colormap( colors=np.arange(5 * 5, dtype=np.float64).reshape((5, 5)), values=np.linspace(0, 1, 5 * 5), ) # LA with pytest.raises(ValueError, match=r".*colors.*shape.*"): colormap.Colormap( colors=np.arange(5 * 2, dtype=np.float64).reshape((5, 2)), values=np.linspace(0, 1, 5 * 2), )
def test_reverse(self): """Test reverse.""" cm_ = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0)), (3, (1, 1, 1)), (4, (0, 0, 0))) colors = cm_.colors cm_.reverse() self.assertTrue(np.allclose(np.flipud(colors), cm_.colors))
def test_diff_colors_values(self): """Test failure when colors and values have different number of elements.""" with pytest.raises(ValueError, match=r".*same number.*"): colormap.Colormap( colors=np.arange(5 * 3, dtype=np.float64).reshape((5, 3)), values=np.linspace(0, 1, 6), )
def test_invert_set_range(self): """Test inverted set_range.""" cm_ = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0)), (3, (1, 1, 1)), (4, (0, 0, 0))) cm_.set_range(8, 0) self.assertTrue(cm_.values[0] == 0) self.assertTrue(cm_.values[-1] == 8)
def test_palettebar(self): """Test colorbar.""" cm_ = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0)), (3, (1.0, 1.0, 1.0)), (4, (0.0, 0.0, 0.0))) channel, palette = colormap.palettebar(1, 4, cm_) self.assertTrue(np.allclose(channel, np.arange(4))) self.assertTrue(np.allclose(palette, cm_.colors))
def test_colorbar(self): """Test colorbar.""" cm_ = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0)), (3, (1.0, 1.0, 1.0)), (4, (0.0, 0.0, 0.0))) channels = colormap.colorbar(1, 4, cm_) for i in range(3): self.assertTrue( np.allclose(channels[i], cm_.colors[:, i], atol=0.001))
def test_to_rgba(self, colors): """Test 'to_rgba' method.""" cmap = colormap.Colormap( values=np.linspace(0.2, 0.5, colors.shape[0]), colors=colors.copy(), ) rgb_cmap = cmap.to_rgba() assert rgb_cmap.colors.shape[-1] == 4 if colors.shape[-1] == 4: assert rgb_cmap is cmap else: assert rgb_cmap is not cmap
def test_colorize(self): """Test colorize """ cm_ = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0)), (3, (1, 1, 1)), (4, (0, 0, 0))) data = np.array([1, 2, 3, 4]) channels = cm_.colorize(data) for i in range(3): self.assertTrue( np.allclose(channels[i], cm_.colors[:, i], atol=0.001))
def test_reverse(self, inplace): """Test colormap reverse.""" values = np.linspace(0.2, 0.5, 10) colors = np.repeat(np.linspace(0.2, 0.8, 10)[:, np.newaxis], 3, 1) orig_values = values.copy() orig_colors = colors.copy() cmap = colormap.Colormap(values=values, colors=colors) new_cmap = cmap.reverse(inplace) self._assert_inplace_worked(cmap, new_cmap, inplace) self._compare_reversed_colors(cmap, new_cmap, inplace, orig_colors) self._assert_unchanged_values(cmap, new_cmap, inplace, orig_values)
def test_to_rio(self): """Test conversion to rasterio colormap """ cm_ = colormap.Colormap((1, (1, 1, 0)), (2, (0, 1, 1)), (3, (1, 1, 1)), (4, (0, 0, 0))) d = cm_.to_rio() exp = {1: (255, 255, 0), 2: (0, 255, 255), 3: (255, 255, 255), 4: (0, 0, 0)} self.assertEqual(d, exp)
def test_set_range(self, new_range, inplace): """Test 'set_range' method.""" values = np.linspace(0.2, 0.5, 10) colors = np.repeat(np.linspace(0.2, 0.8, 10)[:, np.newaxis], 3, 1) orig_values = values.copy() orig_colors = colors.copy() cmap = colormap.Colormap(values=values, colors=colors) new_cmap = cmap.set_range(*new_range, inplace) self._assert_inplace_worked(cmap, new_cmap, inplace) self._assert_monotonic_values(cmap) self._assert_monotonic_values(new_cmap) self._assert_values_changed(cmap, new_cmap, inplace, orig_values) if new_range[0] > new_range[1]: self._compare_reversed_colors(cmap, new_cmap, inplace, orig_colors)
def palettize(img, min_out, max_out, min_in=0, max_in=1.0, colormap=None, alpha=True, **kwargs): """Apply a colormap to data and return the indices in to that colormap.""" import xarray as xr import dask.array as da from trollimage.xrimage import XRImage import trollimage.colormap as ticolormap from satpy import CHUNK_SIZE good_data_mask = kwargs['good_data_mask'] if img.ndim > 2: raise ValueError("Not sure how to palettize more than 2 dimensions") if colormap is None: raise ValueError("'colormap' is required for 'palettize' rescaling") elif not isinstance(colormap, ticolormap.Colormap): raise ValueError("Unknown 'colormap' type: %s", str(type(colormap))) dims = ('y', 'x') if img.ndim == 2 else ('y',) attrs = kwargs.get('attrs', {}) xrimg = XRImage( xr.DataArray(da.from_array(img, chunks=CHUNK_SIZE), dims=dims, attrs=attrs)) if alpha: # use colormap as is tmp_cmap = colormap # produce LA image xrimg = xrimg.convert(xrimg.mode + 'A') else: # the colormap has a value at 0 (first position) that represents # invalid data. We should palettize based on the colormap without # the 0 and then increment tmp_cmap = ticolormap.Colormap(*zip(colormap.values[1:], colormap.colors[1:])) tmp_cmap.set_range(min_in, max_in) xrimg.palettize(tmp_cmap) img_data = xrimg.data.values if alpha: # multiply alpha by the output size img_data[1, :, :] *= max_out else: # get the single band (L) img_data = img_data[0] # increment the indexes by 1 because the colormap has a 0 fill value img_data += 1 img_data[~good_data_mask] = 0 # our data values are now integers that can't be scaled to the output type # because they need to match the colormap return img_data
def test_to_rio(self): """Test conversion to rasterio colormap.""" cm_ = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0)), (3, (1.0, 1.0, 1.0)), (4, (0.0, 0.0, 0.0))) orig_colors = cm_.colors.copy() d = cm_.to_rio() exp = { 1: (255, 255, 0), 2: (0, 255, 255), 3: (255, 255, 255), 4: (0, 0, 0) } self.assertEqual(d, exp) # assert original colormap information hasn't changed np.testing.assert_allclose(orig_colors, cm_.colors)
def __init__(self, message): super().__init__(message, BTD.Product, "TIR BTD", "brightness temperature difference") self.color_bar_font = aggdraw.Font((0, 0, 0), TYPEFACE, size=14) self.colors = colormap.Colormap( (0.0, (0.5, 0.0, 0.0)), (0.071428, (1.0, 0.0, 0.0)), (0.142856, (1.0, 0.5, 0.0)), (0.214284, (1.0, 1.0, 0.0)), (0.285712, (0.5, 1.0, 0.5)), (0.357140, (0.0, 1.0, 1.0)), (0.428568, (0.0, 0.5, 1.0)), (0.499999, (0.0, 0.0, 1.0)), (0.5000, (0.5, 0.5, 0.5)), (1.0, (1.0, 1.0, 1.0)), ) self.colors.set_range(-6, 5)
def __init__(self, message): super().__init__(message, MIR.Product, "Mid-IR", "mid-infrared brightness temperature (c)") self.colors = colormap.Colormap((0.0, (0.0, 0.0, 0.0)), (1.0, (1.0, 1.0, 1.0))) self.colors.set_range(-50, 50)
def test_nonfloat_colors(self): """Pass integer colors to colormap.""" colormap.Colormap( colors=np.arange(5 * 3, dtype=np.uint8).reshape((5, 3)), values=np.linspace(0, 1, 5), )
def setUp(self): """Set up the test case.""" self.colormap = colormap.Colormap((1, (1.0, 1.0, 0.0)), (2, (0.0, 1.0, 1.0)), (3, (1, 1, 1)), (4, (0, 0, 0)))