Python dask.array.array() Examples
The following are 30
code examples of dask.array.array().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
dask.array
, or try the search function
.
Example #1
Source File: k_means.py From dask-ml with BSD 3-Clause "New" or "Revised" License | 6 votes |
def predict(self, X): """Predict the closest cluster each sample in X belongs to. In the vector quantization literature, `cluster_centers_` is called the code book and each value returned by `predict` is the index of the closest code in the code book. Parameters ---------- X : array-like, shape = [n_samples, n_features] New data to predict. Returns ------- labels : array, shape [n_samples,] Index of the cluster each sample belongs to. """ check_is_fitted(self, "cluster_centers_") X = self._check_array(X) labels = pairwise_distances_argmin_min(X, self.cluster_centers_)[0].astype( np.int32 ) return labels
Example #2
Source File: geometry.py From pyresample with GNU Lesser General Public License v3.0 | 6 votes |
def _get_slice(segments, shape): """Segment a 1D or 2D array.""" if not (1 <= len(shape) <= 2): raise ValueError('Cannot segment array of shape: %s' % str(shape)) else: size = shape[0] slice_length = int(np.ceil(float(size) / segments)) start_idx = 0 end_idx = slice_length while start_idx < size: if len(shape) == 1: yield slice(start_idx, end_idx) else: yield (slice(start_idx, end_idx), slice(None)) start_idx = end_idx end_idx = min(start_idx + slice_length, size)
Example #3
Source File: test_bucket.py From pyresample with GNU Lesser General Public License v3.0 | 6 votes |
def test_round_to_resolution(self): """Test rounding to given resolution""" # Scalar, integer resolution self.assertEqual(bucket.round_to_resolution(5.5, 2.), 6) # Scalar, non-integer resolution self.assertEqual(bucket.round_to_resolution(5.5, 1.7), 5.1) # List self.assertTrue(np.all(bucket.round_to_resolution([4.2, 5.6], 2) == np.array([4., 6.]))) # Numpy array self.assertTrue(np.all(bucket.round_to_resolution(np.array([4.2, 5.6]), 2) == np.array([4., 6.]))) # Dask array self.assertTrue( np.all(bucket.round_to_resolution(da.array([4.2, 5.6]), 2) == np.array([4., 6.])))
Example #4
Source File: kernels.py From alibi-detect with Apache License 2.0 | 6 votes |
def infer_sigma(x: Union[np.ndarray, da.array], y: Union[np.ndarray, da.array], p: int = 2 ) -> float: """ Infer sigma used in the kernel by setting it to the median distance between each of the pairwise instances in x and y. Parameters ---------- x Batch of instances of shape [Nx, features]. y Batch of instances of shape [Ny, features]. p Power used in the distance calculation, default equals 2 (Euclidean distance). chunks Chunk sizes for x and y when using dask to compute the pairwise distances. Returns ------- Sigma used in the kernel. """ dist = distance.pairwise_distance(x, y, p=p) return np.median(dist) if isinstance(dist, np.ndarray) else da.median(dist.reshape(-1,), axis=0).compute()
Example #5
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 6 votes |
def test_call(self): """Test palette compositing.""" from satpy.composites import PaletteCompositor cmap_comp = PaletteCompositor('test_cmap_compositor') palette = xr.DataArray(np.array([[0, 0, 0], [127, 127, 127], [255, 255, 255]]), dims=['value', 'band']) palette.attrs['palette_meanings'] = [2, 3, 4] data = xr.DataArray(np.array([[4, 3, 2], [2, 3, 4]], dtype=np.uint8), dims=['y', 'x']) res = cmap_comp([data, palette]) exp = np.array([[[1., 0.498039, 0.], [0., 0.498039, 1.]], [[1., 0.498039, 0.], [0., 0.498039, 1.]], [[1., 0.498039, 0.], [0., 0.498039, 1.]]]) self.assertTrue(np.allclose(res, exp))
Example #6
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 6 votes |
def test_call(self): """Test the CloudTopHeight composite generation.""" from satpy.composites.cloud_products import CloudTopHeightCompositor cmap_comp = CloudTopHeightCompositor('test_cmap_compositor') palette = xr.DataArray(np.array([[0, 0, 0], [127, 127, 127], [255, 255, 255]]), dims=['value', 'band']) palette.attrs['palette_meanings'] = [2, 3, 4] status = xr.DataArray(np.array([[1, 0, 1], [1, 0, 65535]]), dims=['y', 'x'], attrs={'_FillValue': 65535}) data = xr.DataArray(np.array([[4, 3, 2], [2, 3, 4]], dtype=np.uint8), dims=['y', 'x']) res = cmap_comp([data, palette, status]) exp = np.array([[[0., 0.49803922, 0.], [0., 0.49803922, np.nan]], [[0., 0.49803922, 0.], [0., 0.49803922, np.nan]], [[0., 0.49803922, 0.], [0., 0.49803922, np.nan]]]) np.testing.assert_allclose(res, exp)
Example #7
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 6 votes |
def test_call(self): """Test the precip composite generation.""" from satpy.composites.cloud_products import PrecipCloudsRGB cmap_comp = PrecipCloudsRGB('test_precip_compositor') data_light = xr.DataArray(np.array([[80, 70, 60, 0], [20, 30, 40, 255]], dtype=np.uint8), dims=['y', 'x'], attrs={'_FillValue': 255}) data_moderate = xr.DataArray(np.array([[60, 50, 40, 0], [20, 30, 40, 255]], dtype=np.uint8), dims=['y', 'x'], attrs={'_FillValue': 255}) data_intense = xr.DataArray(np.array([[40, 30, 20, 0], [20, 30, 40, 255]], dtype=np.uint8), dims=['y', 'x'], attrs={'_FillValue': 255}) data_flags = xr.DataArray(np.array([[0, 0, 4, 0], [0, 0, 0, 0]], dtype=np.uint8), dims=['y', 'x']) res = cmap_comp([data_light, data_moderate, data_intense, data_flags]) exp = np.array([[[0.24313725, 0.18235294, 0.12156863, np.nan], [0.12156863, 0.18235294, 0.24313725, np.nan]], [[0.62184874, 0.51820728, 0.41456583, np.nan], [0.20728291, 0.31092437, 0.41456583, np.nan]], [[0.82913165, 0.7254902, 0.62184874, np.nan], [0.20728291, 0.31092437, 0.41456583, np.nan]]]) np.testing.assert_allclose(res, exp)
Example #8
Source File: distance.py From alibi-detect with Apache License 2.0 | 6 votes |
def norm(x: Union[np.ndarray, da.array], p: int) -> Union[np.ndarray, da.array]: """ Compute p-norm across the features of a batch of instances. Parameters ---------- x Batch of instances of shape [N, features]. p Power of the norm. Returns ------- Array where p-norm is applied to the features. """ return (x ** p).sum(axis=1) ** (1 / p)
Example #9
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 6 votes |
def setUp(self): """Create test data.""" from satpy.composites import GenericCompositor self.comp = GenericCompositor(name='test') self.comp2 = GenericCompositor(name='test2', common_channel_mask=False) all_valid = np.ones((1, 2, 2)) self.all_valid = xr.DataArray(all_valid, dims=['bands', 'y', 'x']) first_invalid = np.reshape(np.array([np.nan, 1., 1., 1.]), (1, 2, 2)) self.first_invalid = xr.DataArray(first_invalid, dims=['bands', 'y', 'x']) second_invalid = np.reshape(np.array([1., np.nan, 1., 1.]), (1, 2, 2)) self.second_invalid = xr.DataArray(second_invalid, dims=['bands', 'y', 'x']) wrong_shape = np.reshape(np.array([1., 1., 1.]), (1, 3, 1)) self.wrong_shape = xr.DataArray(wrong_shape, dims=['bands', 'y', 'x'])
Example #10
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 6 votes |
def test_multiple_sensors(self): """Test the background compositing from multiple sensor data.""" from satpy.composites import BackgroundCompositor import numpy as np comp = BackgroundCompositor("name") # L mode images attrs = {'mode': 'L', 'area': 'foo'} foreground = xr.DataArray(np.array([[[1., 0.5], [0., np.nan]]]), dims=('bands', 'y', 'x'), coords={'bands': [c for c in attrs['mode']]}, attrs=attrs.copy()) foreground.attrs['sensor'] = 'abi' background = xr.DataArray(np.ones((1, 2, 2)), dims=('bands', 'y', 'x'), coords={'bands': [c for c in attrs['mode']]}, attrs=attrs.copy()) background.attrs['sensor'] = 'glm' res = comp([foreground, background]) self.assertEqual(res.attrs['area'], 'foo') self.assertTrue(np.all(res == np.array([[1., 0.5], [0., 1.]]))) self.assertEqual(res.attrs['mode'], 'L') self.assertEqual(res.attrs['sensor'], {'abi', 'glm'})
Example #11
Source File: distance.py From alibi-detect with Apache License 2.0 | 6 votes |
def pairwise_distance(x: Union[np.ndarray, da.array], y: Union[np.ndarray, da.array], p: int = 2 ) -> Union[np.ndarray, da.array]: """ Compute pairwise distance between 2 samples. Parameters ---------- x Batch of instances of shape [Nx, features]. y Batch of instances of shape [Ny, features]. p Power of the norm used to compute the distance. Returns ------- [Nx, Ny] matrix with pairwise distances. """ assert len(x.shape) == len(y.shape) and len(x.shape) == 2 and x.shape[-1] == y.shape[-1] diff = x.reshape(x.shape + (1,)) - y.T.reshape((1,) + y.T.shape) # [Nx,F,1]-[1,F,Ny]=[Nx,F,Ny] dist = norm(diff, p) # [Nx,Ny] return dist
Example #12
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 6 votes |
def test_get_flag_value(self): """Test reading flag value from attributes based on a name.""" from satpy.composites import _get_flag_value flag_values = da.array([1, 2]) mask = da.array([[1, 2, 2], [2, 1, 2], [2, 2, 1]]) mask = xr.DataArray(mask, dims=['y', 'x']) flag_meanings = ['Cloud-free_land', 'Cloud-free_sea'] mask.attrs['flag_meanings'] = flag_meanings mask.attrs['flag_values'] = flag_values assert _get_flag_value(mask, 'Cloud-free_land') == 1 assert _get_flag_value(mask, 'Cloud-free_sea') == 2 flag_meanings_str = 'Cloud-free_land Cloud-free_sea' mask.attrs['flag_meanings'] = flag_meanings_str assert _get_flag_value(mask, 'Cloud-free_land') == 1 assert _get_flag_value(mask, 'Cloud-free_sea') == 2
Example #13
Source File: k_means.py From dask-ml with BSD 3-Clause "New" or "Revised" License | 5 votes |
def evaluate_cost(X, centers): # type: (da.Array, np.array) -> float # parallel for dask arrays return (pairwise_distances(X, centers).min(1) ** 2).sum()
Example #14
Source File: geometry.py From pyresample with GNU Lesser General Public License v3.0 | 5 votes |
def _flatten_cartesian_coords(cartesian_coords): """Flatten array to (n, 3) shape.""" shape = cartesian_coords.shape if len(shape) > 2: cartesian_coords = cartesian_coords.reshape(shape[0] * shape[1], 3) return cartesian_coords
Example #15
Source File: test_bucket.py From pyresample with GNU Lesser General Public License v3.0 | 5 votes |
def test_get_bucket_indices(self): """Test calculation of array indices.""" # Ensure nothing is calculated with dask.config.set(scheduler=CustomScheduler(max_computes=0)): self.resampler._get_indices() x_idxs, y_idxs = da.compute(self.resampler.x_idxs, self.resampler.y_idxs) np.testing.assert_equal(x_idxs, np.array([1710, 1710, 1707, 1705])) np.testing.assert_equal(y_idxs, np.array([465, 465, 459, 455])) # Additional small test case adef = create_area_def( area_id='test', projection={'proj': 'latlong'}, width=2, height=2, center=(0, 0), resolution=10) lons = da.from_array( np.array([-10.0, -9.9, -0.1, 0, 0.1, 9.9, 10.0, -10.1, 0]), chunks=2) lats = da.from_array( np.array([-10.0, -9.9, -0.1, 0, 0.1, 9.9, 10.0, 0, 10.1]), chunks=2) resampler = bucket.BucketResampler(source_lats=lats, source_lons=lons, target_area=adef) resampler._get_indices() np.testing.assert_equal(resampler.x_idxs, np.array([-1, 0, 0, 1, 1, 1, -1, -1, -1])) np.testing.assert_equal(resampler.y_idxs, np.array([-1, 1, 1, 1, 0, 0, -1, -1, -1]))
Example #16
Source File: test_bucket.py From pyresample with GNU Lesser General Public License v3.0 | 5 votes |
def test_get_sum(self): """Test drop-in-a-bucket sum.""" data = da.from_array(np.array([[2., 2.], [2., 2.]]), chunks=self.chunks) with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_sum(data) result = result.compute() # One bin with two hits, so max value is 2.0 self.assertTrue(np.max(result) == 4.) # Two bins with the same value self.assertEqual(np.sum(result == 2.), 2) # One bin with double the value self.assertEqual(np.sum(result == 4.), 1) self.assertEqual(result.shape, self.adef.shape) # Test that also Xarray.DataArrays work data = xr.DataArray(data) with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_sum(data) # One bin with two hits, so max value is 2.0 self.assertTrue(np.max(result) == 4.) # Two bins with the same value self.assertEqual(np.sum(result == 2.), 2) # One bin with double the value self.assertEqual(np.sum(result == 4.), 1) self.assertEqual(result.shape, self.adef.shape) # Test masking all-NaN bins data = da.from_array(np.array([[np.nan, np.nan], [np.nan, np.nan]]), chunks=self.chunks) with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_sum(data, mask_all_nan=True) self.assertTrue(np.all(np.isnan(result))) # By default all-NaN bins have a value of 0.0 with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_sum(data) self.assertEqual(np.nanmax(result), 0.0)
Example #17
Source File: test_bucket.py From pyresample with GNU Lesser General Public License v3.0 | 5 votes |
def test_get_average(self): """Test averaging bucket resampling.""" data = da.from_array(np.array([[2., 4.], [3., np.nan]]), chunks=self.chunks) # Without pre-calculated indices with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_average(data) result = result.compute() self.assertEqual(np.nanmax(result), 3.) self.assertTrue(np.any(np.isnan(result))) # Use a fill value other than np.nan with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_average(data, fill_value=-1) result = result.compute() self.assertEqual(np.max(result), 3.) self.assertEqual(np.min(result), -1) self.assertFalse(np.any(np.isnan(result))) # Test masking all-NaN bins data = da.from_array(np.array([[np.nan, np.nan], [np.nan, np.nan]]), chunks=self.chunks) with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_average(data, mask_all_nan=True) self.assertTrue(np.all(np.isnan(result))) # By default all-NaN bins have a value of NaN with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_average(data) self.assertTrue(np.all(np.isnan(result)))
Example #18
Source File: test_bucket.py From pyresample with GNU Lesser General Public License v3.0 | 5 votes |
def test_resample_bucket_fractions(self): """Test fraction calculations for categorical data.""" data = da.from_array(np.array([[2, 4], [2, 2]]), chunks=self.chunks) categories = [1, 2, 3, 4] with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_fractions(data, categories=categories) self.assertEqual(set(categories), set(result.keys())) res = result[1].compute() self.assertTrue(np.nanmax(res) == 0.) res = result[2].compute() self.assertTrue(np.nanmax(res) == 1.) self.assertTrue(np.nanmin(res) == 0.5) res = result[3].compute() self.assertTrue(np.nanmax(res) == 0.) res = result[4].compute() self.assertTrue(np.nanmax(res) == 0.5) self.assertTrue(np.nanmin(res) == 0.) # There should be NaN values self.assertTrue(np.any(np.isnan(res))) # Use a fill value with dask.config.set(scheduler=CustomScheduler(max_computes=0)): result = self.resampler.get_fractions(data, categories=categories, fill_value=-1) # There should not be any NaN values for i in categories: res = result[i].compute() self.assertFalse(np.any(np.isnan(res))) self.assertTrue(np.min(res) == -1) # No categories given, need to compute the data once to get # the categories with dask.config.set(scheduler=CustomScheduler(max_computes=1)): result = self.resampler.get_fractions(data, categories=None)
Example #19
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_basic_red(self): """Test that basic high resolution red can be passed.""" from satpy.composites import RatioSharpenedRGB comp = RatioSharpenedRGB(name='true_color') res = comp((self.ds1, self.ds2, self.ds3), optional_datasets=(self.ds4,)) res = res.values self.assertEqual(res.shape, (3, 2, 2)) np.testing.assert_allclose(res[0], self.ds4.values) np.testing.assert_allclose(res[1], np.array([[4.5, 4.5], [4.5, 4.5]], dtype=np.float64)) np.testing.assert_allclose(res[2], np.array([[6, 6], [6, 6]], dtype=np.float64))
Example #20
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_self_sharpened_basic(self): """Test that three datasets can be passed without optional high res.""" from satpy.composites import SelfSharpenedRGB comp = SelfSharpenedRGB(name='true_color') res = comp((self.ds1, self.ds2, self.ds3)) res = res.values self.assertEqual(res.shape, (3, 2, 2)) np.testing.assert_allclose(res[0], self.ds1.values) np.testing.assert_allclose(res[1], np.array([[3, 3], [3, 3]], dtype=np.float64)) np.testing.assert_allclose(res[2], np.array([[4, 4], [4, 4]], dtype=np.float64))
Example #21
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_basic_default_not_provided(self): """Test default limits when SZA isn't provided.""" from satpy.composites import SunZenithCorrector comp = SunZenithCorrector(name='sza_test', modifiers=tuple()) res = comp((self.ds1,), test_attr='test') np.testing.assert_allclose(res.values, np.array([[22.401667, 22.31777], [22.437503, 22.353533]])) self.assertIn('y', res.coords) self.assertIn('x', res.coords) ds1 = self.ds1.copy().drop_vars(('y', 'x')) res = comp((ds1,), test_attr='test') np.testing.assert_allclose(res.values, np.array([[22.401667, 22.31777], [22.437503, 22.353533]])) self.assertNotIn('y', res.coords) self.assertNotIn('x', res.coords)
Example #22
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_basic_lims_not_provided(self): """Test custom limits when SZA isn't provided.""" from satpy.composites import SunZenithCorrector comp = SunZenithCorrector(name='sza_test', modifiers=tuple(), correction_limit=90) res = comp((self.ds1,), test_attr='test') np.testing.assert_allclose(res.values, np.array([[66.853262, 68.168939], [66.30742, 67.601493]]))
Example #23
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_basic_default_provided(self): """Test default limits when SZA is provided.""" from satpy.composites import SunZenithCorrector comp = SunZenithCorrector(name='sza_test', modifiers=tuple()) res = comp((self.ds1, self.sza), test_attr='test') np.testing.assert_allclose(res.values, np.array([[22.401667, 22.31777], [22.437503, 22.353533]]))
Example #24
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def setUp(self): """Create test data.""" bands = ['R', 'G', 'B'] start_time = datetime(2018, 1, 1, 18, 0, 0) # RGB a = np.zeros((3, 2, 2), dtype=np.float) a[:, 0, 0] = 0.1 a[:, 0, 1] = 0.2 a[:, 1, 0] = 0.3 a[:, 1, 1] = 0.4 a = da.from_array(a, a.shape) self.data_a = xr.DataArray(a, attrs={'test': 'a', 'start_time': start_time}, coords={'bands': bands}, dims=('bands', 'y', 'x')) b = np.zeros((3, 2, 2), dtype=np.float) b[:, 0, 0] = np.nan b[:, 0, 1] = 0.25 b[:, 1, 0] = 0.50 b[:, 1, 1] = 0.75 b = da.from_array(b, b.shape) self.data_b = xr.DataArray(b, attrs={'test': 'b', 'start_time': start_time}, coords={'bands': bands}, dims=('bands', 'y', 'x')) sza = np.array([[80., 86.], [94., 100.]]) sza = da.from_array(sza, sza.shape) self.sza = xr.DataArray(sza, dims=('y', 'x')) # fake area my_area = mock.MagicMock() lons = np.array([[-95., -94.], [-93., -92.]]) lons = da.from_array(lons, lons.shape) lats = np.array([[40., 41.], [42., 43.]]) lats = da.from_array(lats, lats.shape) my_area.get_lonlats.return_value = (lons, lats) self.data_a.attrs['area'] = my_area self.data_b.attrs['area'] = my_area # not used except to check that it matches the data arrays self.sza.attrs['area'] = my_area
Example #25
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_basic_sza(self): """Test compositor when SZA data is included.""" from satpy.composites import DayNightCompositor comp = DayNightCompositor(name='dn_test') res = comp((self.data_a, self.data_b, self.sza)) res = res.compute() expected = np.array([[0., 0.22122352], [0.5, 1.]]) np.testing.assert_allclose(res.values[0], expected)
Example #26
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_basic_area(self): """Test compositor when SZA data is not provided.""" from satpy.composites import DayNightCompositor comp = DayNightCompositor(name='dn_test') res = comp((self.data_a, self.data_b)) res = res.compute() expected = np.array([[0., 0.33164983], [0.66835017, 1.]]) np.testing.assert_allclose(res.values[0], expected)
Example #27
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_fill(self): """Test filling.""" from satpy.composites import FillingCompositor comp = FillingCompositor(name='fill_test') filler = xr.DataArray(np.array([1, 2, 3, 4, 3, 2, 1])) red = xr.DataArray(np.array([1, 2, 3, np.nan, 3, 2, 1])) green = xr.DataArray(np.array([np.nan, 2, 3, 4, 3, 2, np.nan])) blue = xr.DataArray(np.array([4, 3, 2, 1, 2, 3, 4])) res = comp([filler, red, green, blue]) np.testing.assert_allclose(res.sel(bands='R').data, filler.data) np.testing.assert_allclose(res.sel(bands='G').data, filler.data) np.testing.assert_allclose(res.sel(bands='B').data, blue.data)
Example #28
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_compositor(self): """Test luminance sharpening compositor.""" from satpy.composites import LuminanceSharpeningCompositor comp = LuminanceSharpeningCompositor(name='test') # Three shades of grey rgb_arr = np.array([1, 50, 100, 200, 1, 50, 100, 200, 1, 50, 100, 200]) rgb = xr.DataArray(rgb_arr.reshape((3, 2, 2)), dims=['bands', 'y', 'x']) # 100 % luminance -> all result values ~1.0 lum = xr.DataArray(np.array([[100., 100.], [100., 100.]]), dims=['y', 'x']) res = comp([lum, rgb]) np.testing.assert_allclose(res.data, 1., atol=1e-9) # 50 % luminance, all result values ~0.5 lum = xr.DataArray(np.array([[50., 50.], [50., 50.]]), dims=['y', 'x']) res = comp([lum, rgb]) np.testing.assert_allclose(res.data, 0.5, atol=1e-9) # 30 % luminance, all result values ~0.3 lum = xr.DataArray(np.array([[30., 30.], [30., 30.]]), dims=['y', 'x']) res = comp([lum, rgb]) np.testing.assert_allclose(res.data, 0.3, atol=1e-9) # 0 % luminance, all values ~0.0 lum = xr.DataArray(np.array([[0., 0.], [0., 0.]]), dims=['y', 'x']) res = comp([lum, rgb]) np.testing.assert_allclose(res.data, 0.0, atol=1e-9)
Example #29
Source File: test_composites.py From satpy with GNU General Public License v3.0 | 5 votes |
def test_build_colormap(self): """Test colormap building.""" from satpy.composites import ColormapCompositor cmap_comp = ColormapCompositor('test_cmap_compositor') palette = np.array([[0, 0, 0], [127, 127, 127], [255, 255, 255]]) cmap, sqpal = cmap_comp.build_colormap(palette, np.uint8, {}) self.assertTrue(np.allclose(cmap.values, [0, 1])) self.assertTrue(np.allclose(sqpal, palette / 255.0)) palette = xr.DataArray(np.array([[0, 0, 0], [127, 127, 127], [255, 255, 255]]), dims=['value', 'band']) palette.attrs['palette_meanings'] = [2, 3, 4] cmap, sqpal = cmap_comp.build_colormap(palette, np.uint8, {}) self.assertTrue(np.allclose(cmap.values, [2, 3, 4])) self.assertTrue(np.allclose(sqpal, palette / 255.0))
Example #30
Source File: geometry.py From pyresample with GNU Lesser General Public License v3.0 | 5 votes |
def _compute_uniform_shape(self): """Compute the height and width of a domain to have uniform resolution across dimensions.""" g = Geod(ellps='WGS84') def notnull(arr): try: return arr.where(arr.notnull(), drop=True) except AttributeError: return arr[np.isfinite(arr)] leftlons = self.lons[:, 0] rightlons = self.lons[:, -1] middlelons = self.lons[:, int(self.lons.shape[1] / 2)] leftlats = self.lats[:, 0] rightlats = self.lats[:, -1] middlelats = self.lats[:, int(self.lats.shape[1] / 2)] try: import dask.array as da except ImportError: pass else: leftlons, rightlons, middlelons, leftlats, rightlats, middlelats = da.compute(leftlons, rightlons, middlelons, leftlats, rightlats, middlelats) leftlons = notnull(leftlons) rightlons = notnull(rightlons) middlelons = notnull(middlelons) leftlats = notnull(leftlats) rightlats = notnull(rightlats) middlelats = notnull(middlelats) az1, az2, width1 = g.inv(leftlons[0], leftlats[0], rightlons[0], rightlats[0]) az1, az2, width2 = g.inv(leftlons[-1], leftlats[-1], rightlons[-1], rightlats[-1]) az1, az2, height = g.inv(middlelons[0], middlelats[0], middlelons[-1], middlelats[-1]) width = min(width1, width2) vresolution = height * 1.0 / self.lons.shape[0] hresolution = width * 1.0 / self.lons.shape[1] resolution = min(vresolution, hresolution) width = int(width * 1.1 / resolution) height = int(height * 1.1 / resolution) return height, width