From 736cad84cfbc3993a3a38c640690442f4aaa4cf2 Mon Sep 17 00:00:00 2001 From: Juniper Tyree <50025784+juntyr@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:42:09 +0300 Subject: [PATCH 1/3] Improve levels auto_range with proper symmetry --- src/earthkit/plots/styles/levels.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/earthkit/plots/styles/levels.py b/src/earthkit/plots/styles/levels.py index 5527ddc..c1ee8e9 100644 --- a/src/earthkit/plots/styles/levels.py +++ b/src/earthkit/plots/styles/levels.py @@ -57,22 +57,18 @@ def auto_range(data, divergence_point=None, n_levels=schema.default_style_levels initial_bin = data_range / n_levels - magnitude = 10 ** (math.floor(math.log(initial_bin, 10))) + magnitude = 10 ** (np.floor(np.log10(initial_bin))) bin_width = initial_bin - (initial_bin % -magnitude) - start = min_value - (min_value % magnitude) + min_value -= min_value % bin_width + max_value -= max_value % -bin_width - levels = np.arange( - start, - start + (bin_width * n_levels) + bin_width, - bin_width, + return np.linspace( + min_value, + max_value, + n_levels + 1, ).tolist() - while levels[-2] >= max_value: - levels = levels[:-1] - - return levels - def step_range(data, step, reference=None): """ From af6d94441e1fe1ee89ee0f138bcafd9c4666a2e0 Mon Sep 17 00:00:00 2001 From: Juniper Tyree <50025784+juntyr@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:49:25 +0300 Subject: [PATCH 2/3] Ensure symmetry around arbitrary divergence point --- src/earthkit/plots/styles/levels.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/earthkit/plots/styles/levels.py b/src/earthkit/plots/styles/levels.py index c1ee8e9..169a23b 100644 --- a/src/earthkit/plots/styles/levels.py +++ b/src/earthkit/plots/styles/levels.py @@ -50,8 +50,8 @@ def auto_range(data, divergence_point=None, n_levels=schema.default_style_levels if divergence_point is not None: max_diff = max(max_value - divergence_point, divergence_point - min_value) - max_value = divergence_point + max_diff - min_value = divergence_point - max_diff + max_value = max_diff + min_value = -max_diff data_range = max_value - min_value @@ -63,6 +63,10 @@ def auto_range(data, divergence_point=None, n_levels=schema.default_style_levels min_value -= min_value % bin_width max_value -= max_value % -bin_width + if divergence_point is not None: + min_value += divergence_point + max_value += divergence_point + return np.linspace( min_value, max_value, From 06b6c96dd2d8c3743fbc66e23a254be801f80ff8 Mon Sep 17 00:00:00 2001 From: James Varndell Date: Fri, 18 Oct 2024 17:35:20 +0100 Subject: [PATCH 3/3] Removes obsolete math import --- src/earthkit/plots/styles/levels.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/earthkit/plots/styles/levels.py b/src/earthkit/plots/styles/levels.py index 169a23b..730dd2b 100644 --- a/src/earthkit/plots/styles/levels.py +++ b/src/earthkit/plots/styles/levels.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import math - import numpy as np from earthkit.plots.schemas import schema