Skip to content

Commit b4ce9d9

Browse files
committed
refactor: clean comparison logic
1 parent 7174769 commit b4ce9d9

4 files changed

Lines changed: 21 additions & 27 deletions

File tree

src/e3sm_compareview/app.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
comparison_signature_for,
1919
label_signature_for,
2020
normalize_comparison_mode,
21+
normalize_comparison_type,
2122
normalize_two_sim_target,
2223
)
2324
from e3sm_compareview.components import doc, drawers, file_browser, toolbars
@@ -459,21 +460,11 @@ async def _import_state(self, state_content):
459460
self.state.two_sim_test_simulation_file = comparisons.get(
460461
"target", self.state.two_sim_test_simulation_file
461462
)
462-
raw_mode = comparisons.get("mode")
463-
if raw_mode in ("two-sim", "multi-sim"):
464-
self.state.comparison_mode = raw_mode
465-
else:
466-
self.state.comparison_mode = comparisons.get(
467-
"strategy", self.state.comparison_mode
468-
)
469-
470-
raw_type = comparisons.get("type")
471-
if raw_type in ("diff", "comp1", "comp2"):
472-
self.state.comparison_type = raw_type
473-
else:
474-
legacy_type = comparisons.get("mode")
475-
if legacy_type in ("diff", "comp1", "comp2"):
476-
self.state.comparison_type = legacy_type
463+
self.state.comparison_mode = normalize_comparison_mode(
464+
comparisons.get("mode", comparisons.get("strategy", self.state.comparison_mode))
465+
)
466+
raw_type = comparisons.get("type", comparisons.get("mode"))
467+
self.state.comparison_type = normalize_comparison_type(raw_type)
477468
self.state.selected_columns = comparisons.get(
478469
"columns", self.state.selected_columns
479470
)

src/e3sm_compareview/comparison.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ def normalize_comparison_mode(mode):
1111
return "multi-sim"
1212

1313

14+
def normalize_comparison_type(comparison_type):
15+
if comparison_type in COMPARISON_TYPES:
16+
return comparison_type
17+
return "diff"
18+
19+
1420
def default_simulation_label(file_path):
1521
return Path(file_path).stem
1622

src/e3sm_compareview/components/simulation_selection.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from trame.widgets import vuetify3 as v3
33

44
from e3sm_compareview.components.drawer_utils import drawer_style
5+
from e3sm_compareview.comparison import normalize_two_sim_target
56
from e3sm_quickview.utils import js
67

78
SIMULATION_DRAG_HANDLE_EVENTS = """
@@ -196,13 +197,9 @@ def _on_control_selected(self, control_path, **_):
196197
self.state.control_simulation_file = control_path
197198
if self.state.comparison_mode != "two-sim":
198199
return
199-
if self.state.two_sim_test_simulation_file != control_path:
200-
return
201200

202-
fallback = control_path
203-
for sim in self.state.simulation_configs or []:
204-
sim_path = sim.get("path")
205-
if sim_path and sim_path != control_path:
206-
fallback = sim_path
207-
break
208-
self.state.two_sim_test_simulation_file = fallback
201+
self.state.two_sim_test_simulation_file = normalize_two_sim_target(
202+
self.state.simulation_configs,
203+
control_path,
204+
self.state.two_sim_test_simulation_file,
205+
)

src/e3sm_compareview/pipeline.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def _build_view_specs(self, variables):
271271

272272
control = self.simulation_configs[0]
273273
for var_name in variables:
274-
per_mode_specs = {}
274+
per_type_specs = {}
275275
control_array_name = self.control_array_name(var_name)
276276
control_metadata = {
277277
"array_name": control_array_name,
@@ -319,7 +319,7 @@ def _build_view_specs(self, variables):
319319
specs.append(comparison_spec)
320320
self.array_metadata[comparison_spec["array_name"]] = comparison_spec
321321

322-
per_mode_specs[comparison_type] = sorted(
322+
per_type_specs[comparison_type] = sorted(
323323
specs,
324324
key=lambda spec: spec.get("source_index", 0),
325325
)
@@ -360,7 +360,7 @@ def _build_view_specs(self, variables):
360360
)
361361

362362
self.variable_view_specs[var_name] = {
363-
"multi-sim": per_mode_specs,
363+
"multi-sim": per_type_specs,
364364
"two-sim": two_sim_specs,
365365
}
366366

0 commit comments

Comments
 (0)