Skip to content

Commit 48b2cf9

Browse files
committed
Fix duplicate summary table callbacks
1 parent 9a83c02 commit 48b2cf9

File tree

2 files changed

+54
-48
lines changed

2 files changed

+54
-48
lines changed

mlip_testing/app/utils/build_components.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from dash.html import H1, H2, Button, Div, Label
1010

1111
from mlip_testing.app.utils.register_callbacks import (
12-
register_table_callbacks,
12+
register_summary_table_callbacks,
13+
register_tab_table_callbacks,
1314
register_weight_callbacks,
1415
)
1516

@@ -120,10 +121,11 @@ def build_weight_components(
120121
]
121122
)
122123

123-
# Callback to update table scores when table weight dict changes
124-
124+
# Callbacks to update table scores when table weight dicts change
125125
if table_id != "summary-table":
126-
register_table_callbacks(table_id=table_id)
126+
register_tab_table_callbacks(table_id=table_id)
127+
else:
128+
register_summary_table_callbacks()
127129

128130
# Callbacks to sync sliders, text boxes, and stored table weights
129131
for column, input_id in zip(columns, input_ids, strict=True):

mlip_testing/app/utils/register_callbacks.py

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,54 @@
88
from mlip_testing.analysis.utils.utils import calc_ranks, calc_scores
99

1010

11-
def register_table_callbacks(table_id) -> None:
11+
def register_summary_table_callbacks() -> None:
12+
"""Register callbacks to update summary table."""
13+
14+
@callback(
15+
Output("summary-table", "data"),
16+
Input("all-tabs", "value"),
17+
Input("summary-table-weight-store", "data"),
18+
State("summary-table-scores-store", "data"),
19+
State("summary-table", "data"),
20+
prevent_initial_call=False,
21+
)
22+
def update_summary_table(
23+
tabs_value: str,
24+
stored_weights: dict[str, float],
25+
stored_scores: dict[str, list[float]],
26+
summary_data: list[dict],
27+
) -> list[dict]:
28+
"""
29+
Update summary table when scores change.
30+
31+
Parameters
32+
----------
33+
tabs_value
34+
Value of selected tab. Parameter unused, but required to register Input.
35+
stored_weights
36+
Stored summary weights dictionary.
37+
stored_scores
38+
Stored scores for table scores.
39+
summary_data
40+
Data from summary table to be updated.
41+
42+
Returns
43+
-------
44+
list[dict]
45+
Updated summary table data.
46+
"""
47+
# Update table from stored scores
48+
if stored_scores:
49+
for key, values in stored_scores.items():
50+
for row, value in zip(summary_data, values, strict=True):
51+
row[key] = value
52+
53+
# Update table contents
54+
summary_data = calc_scores(summary_data, stored_weights)
55+
return calc_ranks(summary_data)
56+
57+
58+
def register_tab_table_callbacks(table_id) -> None:
1259
"""
1360
Register callback to update table scores/rankings when stored values change.
1461
@@ -77,49 +124,6 @@ def update_scores_store(
77124
# Update scores store
78125
return {table_id.removesuffix("-table"): [row["Score"] for row in table_data]}
79126

80-
@callback(
81-
Output("summary-table", "data"),
82-
Input("all-tabs", "value"),
83-
Input("summary-table-weight-store", "data"),
84-
State("summary-table-scores-store", "data"),
85-
State("summary-table", "data"),
86-
prevent_initial_call=False,
87-
)
88-
def update_summary_table(
89-
tabs_value: str,
90-
stored_weights: dict[str, float],
91-
stored_scores: dict[str, list[float]],
92-
summary_data: list[dict],
93-
) -> list[dict]:
94-
"""
95-
Update summary table when scores change.
96-
97-
Parameters
98-
----------
99-
tabs_value
100-
Value of selected tab. Parameter unused, but required to register Input.
101-
stored_weights
102-
Stored summary weights dictionary.
103-
stored_scores
104-
Stored scores for table scores.
105-
summary_data
106-
Data from summary table to be updated.
107-
108-
Returns
109-
-------
110-
list[dict]
111-
Updated summary table data.
112-
"""
113-
# Update table from stored scores
114-
if stored_scores:
115-
for key, values in stored_scores.items():
116-
for row, value in zip(summary_data, values, strict=True):
117-
row[key] = value
118-
119-
# Update table contents
120-
summary_data = calc_scores(summary_data, stored_weights)
121-
return calc_ranks(summary_data)
122-
123127

124128
def register_weight_callbacks(input_id: str, table_id: str, column: str) -> None:
125129
"""

0 commit comments

Comments
 (0)