From 5c2c7fb0aabd2b6836974aec4d6b58a4957c5e67 Mon Sep 17 00:00:00 2001 From: David Hoese Date: Sat, 25 Nov 2023 14:33:16 -0600 Subject: [PATCH 1/2] Fix stretch when min/max value it outside input data type --- trollimage/tests/test_image.py | 233 +++++++++++++++++---------------- trollimage/xrimage.py | 2 +- 2 files changed, 122 insertions(+), 113 deletions(-) diff --git a/trollimage/tests/test_image.py b/trollimage/tests/test_image.py index 1f8820cf..3db5a3d7 100644 --- a/trollimage/tests/test_image.py +++ b/trollimage/tests/test_image.py @@ -37,7 +37,6 @@ from trollimage._xrimage_rasterio import RIODataset from .utils import assert_maximum_dask_computes - EPSILON = 0.0001 @@ -1102,13 +1101,17 @@ def test_save_geotiff_int_gcps(self): lons = xr.DataArray(da.from_array(np.arange(25).reshape(5, 5), chunks=5), dims=['y', 'x'], - attrs={'gcps': gcps, - 'crs': crs}) + attrs={ + 'gcps': gcps, + 'crs': crs + }) lats = xr.DataArray(da.from_array(np.arange(25).reshape(5, 5), chunks=5), dims=['y', 'x'], - attrs={'gcps': gcps, - 'crs': crs}) + attrs={ + 'gcps': gcps, + 'crs': crs + }) swath_def = SwathDefinition(lons, lats) data = xr.DataArray(da.from_array(np.arange(75).reshape(5, 5, 3), chunks=5), @@ -1377,7 +1380,7 @@ def test_save_cloud_optimized_geotiff(self): # trigger COG driver to create 2 overview levels # COG driver is only available in GDAL 3.1 or later if rio.__gdal_version__ >= '3.1': - data = xr.DataArray(np.arange(1200*1200*3).reshape(1200, 1200, 3), dims=[ + data = xr.DataArray(np.arange(1200 * 1200 * 3).reshape(1200, 1200, 3), dims=[ 'y', 'x', 'bands'], coords={'bands': ['R', 'G', 'B']}) img = xrimage.XRImage(data) assert np.issubdtype(img.data.dtype, np.integer) @@ -1402,7 +1405,7 @@ def test_save_overviews(self): assert len(f.overviews(1)) == 2 # auto-levels - data = np.zeros(25*25*3, dtype=np.uint8).reshape(25, 25, 3) + data = np.zeros(25 * 25 * 3, dtype=np.uint8).reshape(25, 25, 3) data = xr.DataArray(data, dims=[ 'y', 'x', 'bands'], coords={'bands': ['R', 'G', 'B']}) img = xrimage.XRImage(data) @@ -1413,7 +1416,7 @@ def test_save_overviews(self): assert len(f.overviews(1)) == 4 # auto-levels and resampling - data = np.zeros(25*25*3, dtype=np.uint8).reshape(25, 25, 3) + data = np.zeros(25 * 25 * 3, dtype=np.uint8).reshape(25, 25, 3) data = xr.DataArray(data, dims=[ 'y', 'x', 'bands'], coords={'bands': ['R', 'G', 'B']}) img = xrimage.XRImage(data) @@ -1511,6 +1514,7 @@ def test_crude_stretch(self, dtype): @pytest.mark.parametrize("dtype", (np.float32, np.float64, float)) def test_crude_stretch_with_limits(self, dtype): + """Test crude stretch with different input dtypes.""" arr = np.arange(75, dtype=dtype).reshape(5, 5, 3) data = xr.DataArray(arr.copy(), dims=['y', 'x', 'bands'], coords={'bands': ['R', 'G', 'B']}) @@ -1519,14 +1523,18 @@ def test_crude_stretch_with_limits(self, dtype): assert img.data.dtype == dtype np.testing.assert_allclose(img.data.values, arr / 74., rtol=1e-6) - def test_crude_stretch_integer_data(self): - arr = np.arange(75, dtype=int).reshape(5, 5, 3) + @pytest.mark.parametrize("dtype", [np.uint8, int]) + # include a stretch within 8-bit uint and outside + @pytest.mark.parametrize("max_stretch", [74, 74 * 4]) + def test_crude_stretch_integer_data(self, dtype, max_stretch): + """Test crude stretch with different input integer dtypes.""" + arr = np.arange(75, dtype=dtype).reshape(5, 5, 3) data = xr.DataArray(arr.copy(), dims=['y', 'x', 'bands'], coords={'bands': ['R', 'G', 'B']}) img = xrimage.XRImage(data) - img.crude_stretch(0, 74) + img.crude_stretch(0, max_stretch) assert img.data.dtype == np.float32 - np.testing.assert_allclose(img.data.values, arr.astype(np.float32) / 74., rtol=1e-6) + np.testing.assert_allclose(img.data.values, arr.astype(np.float32) / max_stretch, rtol=1e-6) @pytest.mark.parametrize("dtype", (np.float32, np.float64, float)) def test_invert(self, dtype): @@ -2024,24 +2032,24 @@ def test_blend(self, dtype): assert img3.data.dtype == dtype np.testing.assert_allclose( - (alpha1 + alpha2 * (1 - alpha1)).squeeze(), - img3.data.sel(bands="A")) + (alpha1 + alpha2 * (1 - alpha1)).squeeze(), + img3.data.sel(bands="A")) np.testing.assert_allclose( img3.data.sel(bands="R").values, np.array( - [[1., 0.95833635, 0.9136842, 0.8666667, 0.8180645], - [0.768815, 0.72, 0.6728228, 0.62857145, 0.5885714], - [0.55412847, 0.5264665, 0.50666666, 0.495612, 0.49394494], - [0.5020408, 0.52, 0.5476586, 0.5846154, 0.63027024], - [0.683871, 0.7445614, 0.81142855, 0.8835443, 0.96]], dtype=dtype), - rtol=2e-6) + [[1., 0.95833635, 0.9136842, 0.8666667, 0.8180645], + [0.768815, 0.72, 0.6728228, 0.62857145, 0.5885714], + [0.55412847, 0.5264665, 0.50666666, 0.495612, 0.49394494], + [0.5020408, 0.52, 0.5476586, 0.5846154, 0.63027024], + [0.683871, 0.7445614, 0.81142855, 0.8835443, 0.96]], dtype=dtype), + rtol=2e-6) with pytest.raises(TypeError): img1.blend("Salekhard") wrongimg = xrimage.XRImage( - xr.DataArray(np.zeros((0, 0)), dims=("y", "x"))) + xr.DataArray(np.zeros((0, 0)), dims=("y", "x"))) with pytest.raises(ValueError): img1.blend(wrongimg) @@ -2072,7 +2080,7 @@ def test_apply_pil(self): data = xr.DataArray(np_data, dims=[ 'y', 'x', 'bands'], coords={'bands': ['R', 'G', 'B']}) - dummy_args = [(OrderedDict(), ), {}] + dummy_args = [(OrderedDict(),), {}] def dummy_fun(pil_obj, *args, **kwargs): dummy_args[0] = args @@ -2097,12 +2105,12 @@ def dummy_fun(pil_obj, *args, **kwargs): res = img.apply_pil(dummy_fun, 'RGB', fun_args=('Hey', 'Jude'), fun_kwargs={'chorus': "La lala lalalala"}) - assert dummy_args == [({}, ), {}] + assert dummy_args == [({},), {}] res.data.data.compute() assert dummy_args == [(OrderedDict(), 'Hey', 'Jude'), {'chorus': "La lala lalalala"}] # Test HACK for _burn_overlay - dummy_args = [(OrderedDict(), ), {}] + dummy_args = [(OrderedDict(),), {}] def _burn_overlay(pil_obj, *args, **kwargs): dummy_args[0] = args @@ -2163,92 +2171,93 @@ class TestXRImageColorize: 9.24115112e-02, 6.24348956e-02, 2.53761173e-02, 4.08181032e-03, 4.27986478e-03, 4.17929690e-03, 3.78662146e-03, 3.12692318e-03, 2.24023940e-03, 1.17807264e-03, 3.21825881e-08]], - [[1.88235327e-01, 2.05148705e-01, 2.22246533e-01, 2.39526068e-01, - 2.56989487e-01, 2.74629823e-01, 2.92439994e-01, 3.10413422e-01, - 3.32343814e-01, 3.57065419e-01, 3.82068278e-01, 4.07348961e-01, - 4.32903760e-01, 4.58728817e-01, 4.84820203e-01], - [5.12920806e-01, 5.47946930e-01, 5.82732545e-01, 6.17314757e-01, - 6.51719365e-01, 6.85963748e-01, 7.20058900e-01, 7.54010901e-01, - 7.76938576e-01, 7.97119666e-01, 8.17286145e-01, 8.37436050e-01, - 8.57567245e-01, 8.77677444e-01, 8.97764221e-01], - [9.14974807e-01, 9.26634684e-01, 9.36490370e-01, 9.44572058e-01, - 9.50936890e-01, 9.55671974e-01, 9.58899694e-01, 9.60784377e-01, - 9.54533524e-01, 9.48563492e-01, 9.42789228e-01, 9.37126769e-01, - 9.31494725e-01, 9.25815456e-01, 9.20015949e-01], - [9.08501736e-01, 8.93232137e-01, 8.77927046e-01, 8.62584949e-01, - 8.47204357e-01, 8.31783811e-01, 8.16321878e-01, 7.98071154e-01, - 7.68921238e-01, 7.39943765e-01, 7.11141597e-01, 6.82517728e-01, - 6.54075286e-01, 6.25817541e-01, 5.97747906e-01], - [5.70776450e-01, 5.44247780e-01, 5.17943011e-01, 4.91867999e-01, - 4.66028940e-01, 4.40432405e-01, 4.15085375e-01, 3.90762603e-01, - 3.67819656e-01, 3.45100714e-01, 3.22617398e-01, 3.00381887e-01, - 2.78406993e-01, 2.56706267e-01, 2.35294109e-01]], - [[1.96078164e-02, 2.42548791e-02, 2.74972980e-02, 2.96227786e-02, - 3.17156285e-02, 3.38568546e-02, 3.60498743e-02, 3.82990372e-02, - 5.17340107e-02, 7.13424499e-02, 9.00791380e-02, 1.08349520e-01, - 1.26372958e-01, 1.44280386e-01, 1.62155431e-01], - [1.84723724e-01, 2.25766583e-01, 2.66872651e-01, 3.08395883e-01, - 3.50522786e-01, 3.93349758e-01, 4.36919863e-01, 4.81242202e-01, - 5.19495725e-01, 5.56210021e-01, 5.93054317e-01, 6.30051817e-01, - 6.67218407e-01, 7.04564489e-01, 7.42096284e-01], - [7.75703258e-01, 8.04590533e-01, 8.34519408e-01, 8.64314044e-01, - 8.92841230e-01, 9.19042335e-01, 9.41963593e-01, 9.60784303e-01, - 9.45735072e-01, 9.32649658e-01, 9.21608884e-01, 9.12665692e-01, - 9.05844317e-01, 9.01139812e-01, 8.98517976e-01], - [8.86846758e-01, 8.68087757e-01, 8.49200397e-01, 8.30188000e-01, - 8.11054008e-01, 7.91801982e-01, 7.72435606e-01, 7.51368872e-01, - 7.24059052e-01, 6.97016433e-01, 6.70243011e-01, 6.43740898e-01, - 6.17512331e-01, 5.91559687e-01, 5.65885492e-01], - [5.39262087e-01, 5.12603461e-01, 4.86221750e-01, 4.60123396e-01, - 4.34315297e-01, 4.08804859e-01, 3.83600057e-01, 3.58016749e-01, - 3.31909003e-01, 3.06406088e-01, 2.81515756e-01, 2.57245695e-01, - 2.33603632e-01, 2.10597439e-01, 1.88235281e-01]]], dtype=np.float64), + [[1.88235327e-01, 2.05148705e-01, 2.22246533e-01, 2.39526068e-01, + 2.56989487e-01, 2.74629823e-01, 2.92439994e-01, 3.10413422e-01, + 3.32343814e-01, 3.57065419e-01, 3.82068278e-01, 4.07348961e-01, + 4.32903760e-01, 4.58728817e-01, 4.84820203e-01], + [5.12920806e-01, 5.47946930e-01, 5.82732545e-01, 6.17314757e-01, + 6.51719365e-01, 6.85963748e-01, 7.20058900e-01, 7.54010901e-01, + 7.76938576e-01, 7.97119666e-01, 8.17286145e-01, 8.37436050e-01, + 8.57567245e-01, 8.77677444e-01, 8.97764221e-01], + [9.14974807e-01, 9.26634684e-01, 9.36490370e-01, 9.44572058e-01, + 9.50936890e-01, 9.55671974e-01, 9.58899694e-01, 9.60784377e-01, + 9.54533524e-01, 9.48563492e-01, 9.42789228e-01, 9.37126769e-01, + 9.31494725e-01, 9.25815456e-01, 9.20015949e-01], + [9.08501736e-01, 8.93232137e-01, 8.77927046e-01, 8.62584949e-01, + 8.47204357e-01, 8.31783811e-01, 8.16321878e-01, 7.98071154e-01, + 7.68921238e-01, 7.39943765e-01, 7.11141597e-01, 6.82517728e-01, + 6.54075286e-01, 6.25817541e-01, 5.97747906e-01], + [5.70776450e-01, 5.44247780e-01, 5.17943011e-01, 4.91867999e-01, + 4.66028940e-01, 4.40432405e-01, 4.15085375e-01, 3.90762603e-01, + 3.67819656e-01, 3.45100714e-01, 3.22617398e-01, 3.00381887e-01, + 2.78406993e-01, 2.56706267e-01, 2.35294109e-01]], + [[1.96078164e-02, 2.42548791e-02, 2.74972980e-02, 2.96227786e-02, + 3.17156285e-02, 3.38568546e-02, 3.60498743e-02, 3.82990372e-02, + 5.17340107e-02, 7.13424499e-02, 9.00791380e-02, 1.08349520e-01, + 1.26372958e-01, 1.44280386e-01, 1.62155431e-01], + [1.84723724e-01, 2.25766583e-01, 2.66872651e-01, 3.08395883e-01, + 3.50522786e-01, 3.93349758e-01, 4.36919863e-01, 4.81242202e-01, + 5.19495725e-01, 5.56210021e-01, 5.93054317e-01, 6.30051817e-01, + 6.67218407e-01, 7.04564489e-01, 7.42096284e-01], + [7.75703258e-01, 8.04590533e-01, 8.34519408e-01, 8.64314044e-01, + 8.92841230e-01, 9.19042335e-01, 9.41963593e-01, 9.60784303e-01, + 9.45735072e-01, 9.32649658e-01, 9.21608884e-01, 9.12665692e-01, + 9.05844317e-01, 9.01139812e-01, 8.98517976e-01], + [8.86846758e-01, 8.68087757e-01, 8.49200397e-01, 8.30188000e-01, + 8.11054008e-01, 7.91801982e-01, 7.72435606e-01, 7.51368872e-01, + 7.24059052e-01, 6.97016433e-01, 6.70243011e-01, 6.43740898e-01, + 6.17512331e-01, 5.91559687e-01, 5.65885492e-01], + [5.39262087e-01, 5.12603461e-01, 4.86221750e-01, 4.60123396e-01, + 4.34315297e-01, 4.08804859e-01, 3.83600057e-01, 3.58016749e-01, + 3.31909003e-01, 3.06406088e-01, 2.81515756e-01, 2.57245695e-01, + 2.33603632e-01, 2.10597439e-01, 1.88235281e-01]]], dtype=np.float64), np.float32: np.array([[ - [0.32941175, 0.35765505, 0.38643414, 0.4156936 , 0.44535455, - 0.47540084, 0.5058213 , 0.53660583, 0.56515497, 0.5920884 , - 0.61906797, 0.64608735, 0.67314035, 0.7002214 , 0.72732455], - [0.7523298 , 0.76888514, 0.7854807 , 0.8021649 , 0.8189918 , - 0.8360192 , 0.8533116 , 0.8709379 , 0.88421535, 0.8963408 , - 0.90846986, 0.9206159 , 0.9327928 , 0.9450152 , 0.95729893], - [0.9561593 , 0.9379867 , 0.925193 , 0.9186452 , 0.9189398 , - 0.9262958 , 0.9404795 , 0.96078414, 0.9400202 , 0.9179358 , - 0.89463943, 0.8702369 , 0.84483355, 0.8185319 , 0.79143286], - [0.75844824, 0.7217417 , 0.6848227 , 0.64762676, 0.61007077, - 0.57204896, 0.533422 , 0.49457097, 0.4574643 , 0.42000294, + [0.32941175, 0.35765505, 0.38643414, 0.4156936, 0.44535455, + 0.47540084, 0.5058213, 0.53660583, 0.56515497, 0.5920884, + 0.61906797, 0.64608735, 0.67314035, 0.7002214, 0.72732455], + [0.7523298, 0.76888514, 0.7854807, 0.8021649, 0.8189918, + 0.8360192, 0.8533116, 0.8709379, 0.88421535, 0.8963408, + 0.90846986, 0.9206159, 0.9327928, 0.9450152, 0.95729893], + [0.9561593, 0.9379867, 0.925193, 0.9186452, 0.9189398, + 0.9262958, 0.9404795, 0.96078414, 0.9400202, 0.9179358, + 0.89463943, 0.8702369, 0.84483355, 0.8185319, 0.79143286], + [0.75844824, 0.7217417, 0.6848227, 0.64762676, 0.61007077, + 0.57204896, 0.533422, 0.49457097, 0.4574643, 0.42000294, 0.38201877, 0.34326664, 0.30337277, 0.26172766, 0.21724297], - [0.18990554, 0.1670632 , 0.14352395, 0.11888929, 0.09241185, - 0.06243531, 0.02537645, 0.00408208, 0.0042801 , 0.00417955, - 0.00378686, 0.00312716, 0.00224016, 0.00117794, 0. ]], - [[0.18823533, 0.20514871, 0.22224654, 0.23952611, 0.25698954, - 0.27462983, 0.2924401 , 0.31041354, 0.33234388, 0.35706556, - 0.38206834, 0.40734893, 0.4329038 , 0.45872888, 0.4848204 ], - [0.51292086, 0.54794705, 0.5827325 , 0.6173149 , 0.65171933, - 0.6859637 , 0.720059 , 0.754011 , 0.7769386 , 0.7971198 , - 0.81728625, 0.8374362 , 0.8575672 , 0.87767744, 0.8977643 ], - [0.9152645 , 0.92748123, 0.93765223, 0.9458176 , 0.9520587 , - 0.95650315, 0.9593312 , 0.96078444, 0.9547297 , 0.9488435 , - 0.9430687 , 0.9373506 , 0.93163645, 0.9258765 , 0.92002386], - [0.9085018 , 0.89323217, 0.8779272 , 0.86258495, 0.84720427, - 0.83178383, 0.81632185, 0.79807115, 0.7689212 , 0.73994386, - 0.71114165, 0.68251765, 0.65407526, 0.62581754, 0.597748 ], - [0.5707766 , 0.5442478 , 0.51794314, 0.49186793, 0.46602884, - 0.4404323 , 0.4150853 , 0.39076254, 0.3678196 , 0.34510067, - 0.32261738, 0.3003818 , 0.27840695, 0.25670624, 0.23529409]], - [[0.01960781, 0.02425484, 0.02749731, 0.02962274, 0.03171561, - 0.03385685, 0.03604981, 0.03829896, 0.05173399, 0.07134236, - 0.0900792 , 0.10834952, 0.12637301, 0.14428039, 0.16215537], - [0.18472369, 0.22576652, 0.26687256, 0.30839586, 0.35052276, - 0.39334968, 0.43691984, 0.48124215, 0.51949567, 0.55621 , - 0.59305423, 0.6300518 , 0.6672183 , 0.7045645 , 0.7420964 ], - [0.7757837 , 0.80522877, 0.83597785, 0.8665506 , 0.8955321 , - 0.9216227 , 0.9436848 , 0.96078426, 0.94695187, 0.93475634, - 0.9242341 , 0.9154071 , 0.9082816 , 0.90284896, 0.8990856 ], - [0.8868469 , 0.8680878 , 0.8492005 , 0.83018804, 0.8110541 , - 0.791802 , 0.7724357 , 0.7513689 , 0.7240591 , 0.69701654, - 0.67024314, 0.64374095, 0.6175124 , 0.59155977, 0.5658856 ], - [0.53926224, 0.5126035 , 0.48622182, 0.4601233 , 0.43431523, - 0.40880483, 0.3836 , 0.35801673, 0.331909 , 0.30640608, - 0.28151578, 0.25724563, 0.23360366, 0.21059746, 0.18823537]]], dtype=np.float32)} + [0.18990554, 0.1670632, 0.14352395, 0.11888929, 0.09241185, + 0.06243531, 0.02537645, 0.00408208, 0.0042801, 0.00417955, + 0.00378686, 0.00312716, 0.00224016, 0.00117794, 0.]], + [[0.18823533, 0.20514871, 0.22224654, 0.23952611, 0.25698954, + 0.27462983, 0.2924401, 0.31041354, 0.33234388, 0.35706556, + 0.38206834, 0.40734893, 0.4329038, 0.45872888, 0.4848204], + [0.51292086, 0.54794705, 0.5827325, 0.6173149, 0.65171933, + 0.6859637, 0.720059, 0.754011, 0.7769386, 0.7971198, + 0.81728625, 0.8374362, 0.8575672, 0.87767744, 0.8977643], + [0.9152645, 0.92748123, 0.93765223, 0.9458176, 0.9520587, + 0.95650315, 0.9593312, 0.96078444, 0.9547297, 0.9488435, + 0.9430687, 0.9373506, 0.93163645, 0.9258765, 0.92002386], + [0.9085018, 0.89323217, 0.8779272, 0.86258495, 0.84720427, + 0.83178383, 0.81632185, 0.79807115, 0.7689212, 0.73994386, + 0.71114165, 0.68251765, 0.65407526, 0.62581754, 0.597748], + [0.5707766, 0.5442478, 0.51794314, 0.49186793, 0.46602884, + 0.4404323, 0.4150853, 0.39076254, 0.3678196, 0.34510067, + 0.32261738, 0.3003818, 0.27840695, 0.25670624, 0.23529409]], + [[0.01960781, 0.02425484, 0.02749731, 0.02962274, 0.03171561, + 0.03385685, 0.03604981, 0.03829896, 0.05173399, 0.07134236, + 0.0900792, 0.10834952, 0.12637301, 0.14428039, 0.16215537], + [0.18472369, 0.22576652, 0.26687256, 0.30839586, 0.35052276, + 0.39334968, 0.43691984, 0.48124215, 0.51949567, 0.55621, + 0.59305423, 0.6300518, 0.6672183, 0.7045645, 0.7420964], + [0.7757837, 0.80522877, 0.83597785, 0.8665506, 0.8955321, + 0.9216227, 0.9436848, 0.96078426, 0.94695187, 0.93475634, + 0.9242341, 0.9154071, 0.9082816, 0.90284896, 0.8990856], + [0.8868469, 0.8680878, 0.8492005, 0.83018804, 0.8110541, + 0.791802, 0.7724357, 0.7513689, 0.7240591, 0.69701654, + 0.67024314, 0.64374095, 0.6175124, 0.59155977, 0.5658856], + [0.53926224, 0.5126035, 0.48622182, 0.4601233, 0.43431523, + 0.40880483, 0.3836, 0.35801673, 0.331909, 0.30640608, + 0.28151578, 0.25724563, 0.23360366, 0.21059746, 0.18823537]]], dtype=np.float32) + } @pytest.mark.parametrize("colormap_tag", [None, "colormap"]) def test_colorize_geotiff_tag(self, tmp_path, colormap_tag): @@ -2525,8 +2534,8 @@ def test_save_scale_offset_from_lists(self): self.img.crude_stretch([1], [24]) self._save_and_check_tags( - expected_tags, - scale_offset_tags=("scale", "offset")) + expected_tags, + scale_offset_tags=("scale", "offset")) @pytest.mark.skipif(sys.platform.startswith('win'), reason="'NamedTemporaryFile' not supported on Windows") def test_save_scale_offset_custom_labels(self): @@ -2534,8 +2543,8 @@ def test_save_scale_offset_custom_labels(self): expected_tags = {"gradient": 24.0 / 255, "axis_intercept": 0} self.img.stretch() self._save_and_check_tags( - expected_tags, - scale_offset_tags=("gradient", "axis_intercept")) + expected_tags, + scale_offset_tags=("gradient", "axis_intercept")) def _get_tags_after_writing_to_geotiff(data): diff --git a/trollimage/xrimage.py b/trollimage/xrimage.py index be218c03..bf7c9d5f 100644 --- a/trollimage/xrimage.py +++ b/trollimage/xrimage.py @@ -1104,7 +1104,7 @@ def _check_stretch_value(self, val, kind='min'): try: val = val.astype(dtype) except AttributeError: - val = self.data.dtype.type(val) + val = dtype.type(val) return val From ba55be6f88a54122e30a939c53c71b0ed7a24043 Mon Sep 17 00:00:00 2001 From: David Hoese Date: Sat, 25 Nov 2023 14:53:34 -0600 Subject: [PATCH 2/2] Fix flake8 issues --- doc/examples/utilities-examples.py | 16 +++--- trollimage/utilities.py | 83 ++++++++++++++++-------------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/doc/examples/utilities-examples.py b/doc/examples/utilities-examples.py index 11ebbf71..5b2ab3ae 100644 --- a/doc/examples/utilities-examples.py +++ b/doc/examples/utilities-examples.py @@ -1,22 +1,18 @@ +"""Example showing importing colormaps from on-disk files.""" from trollimage import utilities as tu - -# -# Examples: importing colormaps -# -filename='setvak.rgb' +# Examples: importing colormaps +filename = 'setvak.rgb' my_cmap = tu.cmap_from_text(filename) print(my_cmap.colors) my_cmap_norm = tu.cmap_from_text(filename, norm=True) print(my_cmap_norm.colors) -my_cmap_transp = tu.cmap_from_text(filename,norm=True, transparency=True) +my_cmap_transp = tu.cmap_from_text(filename, norm=True, transparency=True) print(my_cmap_transp.colors) -filename='hrv.rgb' -my_cmap_hex = tu.cmap_from_text(filename,hex=True) +filename = 'hrv.rgb' +my_cmap_hex = tu.cmap_from_text(filename, hex=True) print(my_cmap_hex.colors) -# # Example: converting PIL to trollimage.Image -# image = "pifn.png" timage = tu.pilimage2trollimage(image) diff --git a/trollimage/utilities.py b/trollimage/utilities.py index 56d7609f..85b798dc 100644 --- a/trollimage/utilities.py +++ b/trollimage/utilities.py @@ -7,65 +7,65 @@ from trollimage.image import Image from PIL import Image as Pimage + def _hex_to_rgb(value): - ''' - _hex_to_rgb converts a string of 3 hex color values - into a tuple of decimal values - ''' + """Convert a string of 3 hex color values into a tuple of decimal values.""" value = value.lstrip('#') dec = int(value, 16) - return dec + return dec + + +def _text_to_rgb(value, norm=False, cat=1, tot=1, offset=0.5, hex=False): + """Take text line and convert to RGB color. -def _text_to_rgb(value,norm=False,cat=1, tot=1,offset=0.5,hex=False): - ''' - _text_to_rgb takes as input a string composed by 3 values in the range [0,255] + This takes as input a string composed by 3 values in the range [0,255] and returns a tuple of integers. If the parameters cat and tot are given, the function generates a transparency value for this color and returns a tuple of length 4. tot is the total number of colors in the colormap cat is the index of the current colour in the colormap if norm is set to True, the input values are normalized between 0 and 1. - ''' + """ tokens = value.split() if hex: - for i in range(len(tokens)): - tokens[i] = _hex_to_rgb(tokens[i]) - transparency = float(cat)/float(tot)+offset + for i in range(len(tokens)): + tokens[i] = _hex_to_rgb(tokens[i]) + transparency = float(cat) / float(tot) + offset if transparency > 1.0: transparency = 1.0 if norm: - return (float(tokens[0])/255.0, float(tokens[1])/255.0, float(tokens[2])/255.0, transparency) - else: + return (float(tokens[0]) / 255.0, float(tokens[1]) / 255.0, float(tokens[2]) / 255.0, transparency) + else: return (int(tokens[0]), int(tokens[1]), int(tokens[2]), int(round(transparency * 255.0))) def _make_cmap(colors, position=None, bit=False): - ''' - _make_cmap takes a list of tuples which contain RGB values. The RGB + """Convert list of tuples into Colormap object. + + This takes a list of tuples which contain RGB values. The RGB values may either be in 8-bit [0 to 255] (in which bit must be set to True when called) or arithmetic [0 to 1] (default). _make_cmap returns a cmap with equally spaced colors. Arrange your tuples so that the first color is the lowest value for the colorbar and the last is the highest. position contains values from 0 to 1 to dictate the location of each color. - ''' - bit_rgb = np.linspace(0,1,256) - if position == None: - position = np.linspace(0,1,len(colors)) - else: - if len(position) != len(colors): - sys.exit("position length must be the same as colors") - elif position[0] != 0 or position[-1] != 1: - sys.exit("position must start with 0 and end with 1") + """ + if position is None: + position = np.linspace(0, 1, len(colors)) + if len(position) != len(colors): + sys.exit("position length must be the same as colors") + elif position[0] != 0 or position[-1] != 1: + sys.exit("position must start with 0 and end with 1") palette = [(i, (float(r), float(g), float(b), float(a))) for - i, (r, g, b, a) in enumerate(colors)] + i, (r, g, b, a) in enumerate(colors)] cmap = Colormap(*palette) return cmap def cmap_from_text(filename, norm=False, transparency=False, hex=False): - ''' - cmap_from_text takes as input a file that contains a colormap in text format + """Convert text file colormap to Colormap object. + + This takes as input a file that contains a colormap in text format composed by lines with 3 values in the range [0,255] or [00,FF] and returns a tuple of integers. If the parameters cat and tot are given, the function generates a transparency value for this color and returns a tuple @@ -73,36 +73,39 @@ def cmap_from_text(filename, norm=False, transparency=False, hex=False): tot is the total number of colors in the colormap cat is the index of the current colour in the colormap if norm is set to True, the input values are normalized between 0 and 1. - ''' - lines = [line.rstrip('\n') for line in open(filename)] - _colors=[] + """ + lines = [line.rstrip('\n') for line in open(filename)] + _colors = [] _tot = len(lines) _index = 1 for i in lines: if transparency: - _colors.append(_text_to_rgb(i,norm=norm,cat=_index,tot=_tot,hex=hex)) + _colors.append(_text_to_rgb(i, norm=norm, cat=_index, tot=_tot, hex=hex)) else: - _colors.append(_text_to_rgb(i,norm=norm,hex=hex)) + _colors.append(_text_to_rgb(i, norm=norm, hex=hex)) _index = _index + 1 return _make_cmap(_colors) def _image2array(filepath): - ''' + """Extract individual R, G, and B channels from on-disk image file. + Utility function that converts an image file in 3 np arrays that can be fed into geo_image.GeoImage in order to generate a PyTROLL GeoImage object. - ''' + """ im = Pimage.open(filepath).convert('RGB') (width, height) = im.size - _r = np.array(list(im.getdata(0)))/255.0 - _g = np.array(list(im.getdata(1)))/255.0 - _b = np.array(list(im.getdata(2)))/255.0 + _r = np.array(list(im.getdata(0))) / 255.0 + _g = np.array(list(im.getdata(1))) / 255.0 + _b = np.array(list(im.getdata(2))) / 255.0 _r = _r.reshape((height, width)) _g = _g.reshape((height, width)) _b = _b.reshape((height, width)) return _r, _g, _b + def pilimage2trollimage(pimage): - (r,g,b) = _image2array(pimage) - return Image((r,g,b), mode="RGB") + """Convert PIL Image to trollimage Image.""" + (r, g, b) = _image2array(pimage) + return Image((r, g, b), mode="RGB")