Skip to content

Commit 852f5b3

Browse files
committed
Function for proxy connections + PEtab constants usage
1 parent 57af072 commit 852f5b3

2 files changed

Lines changed: 41 additions & 68 deletions

File tree

src/petab_gui/views/simple_plot_view.py

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import defaultdict
22

3+
import petab.v1.C as PETAB_C
34
import qtawesome as qta
45
from matplotlib import pyplot as plt
56
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
@@ -102,6 +103,18 @@ def _get_cached_df(self, table_name, proxy_model):
102103
self._cache_valid[table_name] = True
103104
return self._df_cache[table_name]
104105

106+
def _connect_proxy_signals(self, proxy, cache_key):
107+
"""Connect proxy signals for cache invalidation and plotting."""
108+
for signal in [
109+
proxy.dataChanged,
110+
proxy.rowsInserted,
111+
proxy.rowsRemoved,
112+
]:
113+
signal.connect(
114+
lambda *, key=cache_key: self._invalidate_cache(key)
115+
)
116+
signal.connect(self._debounced_plot)
117+
105118
def initialize(
106119
self, meas_proxy, sim_proxy, cond_proxy, vis_proxy, petab_model
107120
):
@@ -114,61 +127,11 @@ def initialize(
114127
# Connect cache invalidation and data changes
115128
self.options_manager.option_changed.connect(self._debounced_plot)
116129

117-
# Measurements cache invalidation
118-
self.meas_proxy.dataChanged.connect(
119-
lambda: self._invalidate_cache("measurements")
120-
)
121-
self.meas_proxy.rowsInserted.connect(
122-
lambda: self._invalidate_cache("measurements")
123-
)
124-
self.meas_proxy.rowsRemoved.connect(
125-
lambda: self._invalidate_cache("measurements")
126-
)
127-
self.meas_proxy.dataChanged.connect(self._debounced_plot)
128-
self.meas_proxy.rowsInserted.connect(self._debounced_plot)
129-
self.meas_proxy.rowsRemoved.connect(self._debounced_plot)
130-
131-
# Conditions cache invalidation
132-
self.cond_proxy.dataChanged.connect(
133-
lambda: self._invalidate_cache("conditions")
134-
)
135-
self.cond_proxy.rowsInserted.connect(
136-
lambda: self._invalidate_cache("conditions")
137-
)
138-
self.cond_proxy.rowsRemoved.connect(
139-
lambda: self._invalidate_cache("conditions")
140-
)
141-
self.cond_proxy.dataChanged.connect(self._debounced_plot)
142-
self.cond_proxy.rowsInserted.connect(self._debounced_plot)
143-
self.cond_proxy.rowsRemoved.connect(self._debounced_plot)
144-
145-
# Simulations cache invalidation
146-
self.sim_proxy.dataChanged.connect(
147-
lambda: self._invalidate_cache("simulations")
148-
)
149-
self.sim_proxy.rowsInserted.connect(
150-
lambda: self._invalidate_cache("simulations")
151-
)
152-
self.sim_proxy.rowsRemoved.connect(
153-
lambda: self._invalidate_cache("simulations")
154-
)
155-
self.sim_proxy.dataChanged.connect(self._debounced_plot)
156-
self.sim_proxy.rowsInserted.connect(self._debounced_plot)
157-
self.sim_proxy.rowsRemoved.connect(self._debounced_plot)
158-
159-
# Visualization cache invalidation
160-
self.vis_proxy.dataChanged.connect(
161-
lambda: self._invalidate_cache("visualization")
162-
)
163-
self.vis_proxy.rowsInserted.connect(
164-
lambda: self._invalidate_cache("visualization")
165-
)
166-
self.vis_proxy.rowsRemoved.connect(
167-
lambda: self._invalidate_cache("visualization")
168-
)
169-
self.vis_proxy.dataChanged.connect(self._debounced_plot)
170-
self.vis_proxy.rowsInserted.connect(self._debounced_plot)
171-
self.vis_proxy.rowsRemoved.connect(self._debounced_plot)
130+
# Connect proxy signals for all tables
131+
self._connect_proxy_signals(self.meas_proxy, "measurements")
132+
self._connect_proxy_signals(self.cond_proxy, "conditions")
133+
self._connect_proxy_signals(self.sim_proxy, "simulations")
134+
self._connect_proxy_signals(self.vis_proxy, "visualization")
172135

173136
self.visibilityChanged.connect(self._debounced_plot)
174137

@@ -336,8 +299,8 @@ def highlight_from_selection(
336299
if not proxy:
337300
return
338301

339-
x_axis_col = "time"
340-
observable_col = "observableId"
302+
x_axis_col = PETAB_C.TIME
303+
observable_col = PETAB_C.OBSERVABLE_ID
341304

342305
def column_index(name):
343306
for col in range(proxy.columnCount()):

src/petab_gui/views/utils.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
import pandas as pd
2+
from petab.v1.C import (
3+
CONDITION_ID,
4+
MEASUREMENT,
5+
OBSERVABLE_ID,
6+
PARAMETER_ID,
7+
SIMULATION,
8+
TIME,
9+
X_OFFSET,
10+
Y_OFFSET,
11+
)
212
from PySide6.QtCore import Qt
313

414

@@ -34,22 +44,22 @@ def proxy_to_dataframe(proxy_model):
3444
table_type = proxy_model.source_model.table_type
3545

3646
if table_type == "condition":
37-
df = df.set_index("conditionId")
47+
df = df.set_index(CONDITION_ID)
3848
elif table_type == "observable":
39-
df = df.set_index("observableId")
49+
df = df.set_index(OBSERVABLE_ID)
4050
elif table_type == "parameter":
41-
df = df.set_index("parameterId")
51+
df = df.set_index(PARAMETER_ID)
4252
elif table_type == "measurement":
4353
# Use pd.to_numeric with errors='coerce' for robust conversion
44-
df["measurement"] = pd.to_numeric(df["measurement"], errors="coerce")
45-
df["time"] = pd.to_numeric(df["time"], errors="coerce")
54+
df[MEASUREMENT] = pd.to_numeric(df[MEASUREMENT], errors="coerce")
55+
df[TIME] = pd.to_numeric(df[TIME], errors="coerce")
4656
elif table_type == "simulation":
47-
df["simulation"] = pd.to_numeric(df["simulation"], errors="coerce")
48-
df["time"] = pd.to_numeric(df["time"], errors="coerce")
57+
df[SIMULATION] = pd.to_numeric(df[SIMULATION], errors="coerce")
58+
df[TIME] = pd.to_numeric(df[TIME], errors="coerce")
4959
elif table_type == "visualization":
50-
if "xOffset" in df.columns:
51-
df["xOffset"] = pd.to_numeric(df["xOffset"], errors="coerce")
52-
if "yOffset" in df.columns:
53-
df["yOffset"] = pd.to_numeric(df["yOffset"], errors="coerce")
60+
if X_OFFSET in df.columns:
61+
df[X_OFFSET] = pd.to_numeric(df[X_OFFSET], errors="coerce")
62+
if Y_OFFSET in df.columns:
63+
df[Y_OFFSET] = pd.to_numeric(df[Y_OFFSET], errors="coerce")
5464

5565
return df

0 commit comments

Comments
 (0)