Skip to content

Commit faebf51

Browse files
committed
fix(view): fix colorbar range display bug
1 parent 5df6af8 commit faebf51

5 files changed

Lines changed: 65 additions & 2 deletions

File tree

src/e3sm_compareview/components/view.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def create_bottom_bar(config, update_color_preset):
154154
classes="rounded",
155155
)
156156
html.Div(
157-
"{{ utils.quickview.formatRange(config.color_range?.[0], config.use_log_scale, config.color_range?.[0], config.color_range?.[1]) }}",
157+
"{{ config.color_range_min_label }}",
158158
classes="text-caption px-2 text-no-wrap",
159159
)
160160
with html.Div(
@@ -191,6 +191,6 @@ def create_bottom_bar(config, update_color_preset):
191191
style=("`width:1.5px;flex:1;background:${tick.color};`",),
192192
)
193193
html.Div(
194-
"{{ utils.quickview.formatRange(config.color_range?.[1], config.use_log_scale, config.color_range?.[0], config.color_range?.[1]) }}",
194+
"{{ config.color_range_max_label }}",
195195
classes="text-caption px-2 text-no-wrap",
196196
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .format import format_color_range_endpoint as format_color_range_endpoint
2+
from .format import format_color_range_endpoints as format_color_range_endpoints
3+
4+
__all__ = ["format_color_range_endpoint", "format_color_range_endpoints"]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import math
2+
3+
4+
def format_color_range_endpoint(value, scale="linear", range_min=None, range_max=None):
5+
if value is None:
6+
return "Auto"
7+
8+
try:
9+
value = float(value)
10+
except (TypeError, ValueError):
11+
return "Auto"
12+
13+
if math.isnan(value):
14+
return "Auto"
15+
16+
if scale == "log" and value > 0:
17+
return f"10^({math.log10(value):.1f})"
18+
19+
if scale == "symlog":
20+
if value == 0:
21+
return "0"
22+
range_min = 0 if range_min is None else float(range_min)
23+
range_max = 0 if range_max is None else float(range_max)
24+
linthresh = max(abs(range_min), abs(range_max)) * 1e-2 or 1.0
25+
abs_value = abs(value)
26+
if abs_value <= linthresh:
27+
return f"{value:.1e}"
28+
sign = "-" if value < 0 else ""
29+
return f"{sign}10^({math.log10(abs_value):.1f})"
30+
31+
if value == 0:
32+
return "0"
33+
return f"{value:.4g}"
34+
35+
36+
def format_color_range_endpoints(color_range, scale="linear"):
37+
range_min, range_max = color_range
38+
return [
39+
format_color_range_endpoint(range_min, scale, range_min, range_max),
40+
format_color_range_endpoint(range_max, scale, range_min, range_max),
41+
]

src/e3sm_compareview/view_manager.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from vtkmodules.vtkRenderingCore import vtkActor, vtkPolyDataMapper
1212

1313
from e3sm_compareview.components import view as tview
14+
from e3sm_compareview.utils import format_color_range_endpoints
1415
from e3sm_quickview.presets import COLOR_BLIND_SAFE
1516
from e3sm_quickview.utils.color import COLORBAR_CACHE, lut_to_img
1617
from e3sm_quickview.utils.math import compute_color_ticks, tick_contrast_color
@@ -73,6 +74,8 @@ class ViewConfiguration(dataclass.StateDataModel):
7374
lut_img: str = dataclass.Sync(str)
7475
color_ticks: list = dataclass.Sync(list, list)
7576
effective_color_range: list[float] = dataclass.Sync(tuple[float, float], (0, 1))
77+
color_range_min_label: str = dataclass.Sync(str, "0")
78+
color_range_max_label: str = dataclass.Sync(str, "1")
7679

7780

7881
class VariableView(TrameComponent):
@@ -434,6 +437,12 @@ def update_color_range(self, *_):
434437

435438
def _compute_ticks(self):
436439
vmin, vmax = self.config.color_range
440+
(
441+
self.config.color_range_min_label,
442+
self.config.color_range_max_label,
443+
) = format_color_range_endpoints(
444+
self.config.color_range, self.config.use_log_scale
445+
)
437446
ticks = compute_color_ticks(vmin, vmax, scale=self.config.use_log_scale, n=5)
438447
if not ticks:
439448
self.config.color_ticks = []

src/e3sm_compareview/view_manager2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626

2727
from e3sm_compareview.components import view as tview
28+
from e3sm_compareview.utils import format_color_range_endpoints
2829
from e3sm_quickview.presets import COLOR_BLIND_SAFE
2930
from e3sm_quickview.utils.color import COLORBAR_CACHE, lut_to_img
3031
from e3sm_quickview.utils.math import compute_color_ticks, tick_contrast_color
@@ -87,6 +88,8 @@ class ViewConfiguration(dataclass.StateDataModel):
8788
lut_img: str = dataclass.Sync(str)
8889
color_ticks: list = dataclass.Sync(list, list)
8990
effective_color_range: list[float] = dataclass.Sync(tuple[float, float], (0, 1))
91+
color_range_min_label: str = dataclass.Sync(str, "0")
92+
color_range_max_label: str = dataclass.Sync(str, "1")
9093

9194

9295
class VariableView(TrameComponent):
@@ -477,6 +480,12 @@ def update_color_range(self, *_):
477480

478481
def _compute_ticks(self):
479482
vmin, vmax = self.config.color_range
483+
(
484+
self.config.color_range_min_label,
485+
self.config.color_range_max_label,
486+
) = format_color_range_endpoints(
487+
self.config.color_range, self.config.use_log_scale
488+
)
480489
ticks = compute_color_ticks(vmin, vmax, scale=self.config.use_log_scale, n=5)
481490
if not ticks:
482491
self.config.color_ticks = []

0 commit comments

Comments
 (0)