Skip to content

Commit 6d274a8

Browse files
Clean up system_discovery.py
1 parent 67bad67 commit 6d274a8

2 files changed

Lines changed: 13 additions & 20 deletions

File tree

src/system_discovery.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,20 @@
2020
2121
Usage
2222
-----
23-
from chemical_network_sindy import NetworkRateDiscovery
23+
from src.system_discovery import SystemDiscovery, discoverNetwork
2424
25-
discovery = NetworkRateDiscovery(
25+
disc = SystemDiscovery(
2626
df,
2727
threshold=0.05, # STLSQ sparsity threshold
2828
alpha=0.05, # L2 regularisation
2929
differentiation="smooth" # "smooth" | "finite" | "spectral"
3030
)
31-
discovery.fit()
32-
discovery.print_equations()
33-
discovery.plot_results()
34-
summary = discovery.summary()
35-
36-
To Do:
37-
1. Integrate normalizer
31+
disc.fit()
32+
disc.print_equations()
33+
disc.plot_results()
34+
summary = disc.summary()
3835
"""
36+
3937
import constants as cn # type: ignore
4038
from src.model import Model # type: ignore
4139
from src.scaler import Scaler # type: ignore
@@ -191,21 +189,18 @@ def __init__(
191189
self.include_bias = True
192190
self.bias_species: list[str] | None = bias_species
193191

194-
self.model: ps.SINDy
192+
self._differentiator = self._build_differentiator()
195193

196194
library = PolynomialLibrary(
197195
degree=self.poly_degree,
198196
include_bias=self.include_bias,
199197
include_interaction=True,
200198
)
201-
202199
optimizer = ps.STLSQ(threshold=0, alpha=self.alpha)
203200

204-
self._differentiator = self._build_differentiator()
205-
206201
diff_method = self._differentiator
207202

208-
self.model = ps.SINDy(
203+
self.model: ps.SINDy = ps.SINDy(
209204
feature_library=library,
210205
optimizer=optimizer,
211206
differentiation_method=diff_method,
@@ -219,7 +214,7 @@ def __str__(self) -> str:
219214
result = "Model not fitted yet."
220215
return result
221216

222-
def _apply_threshold(self) -> None:
217+
def _applyThreshold(self) -> None:
223218
"""
224219
Zero out normalized coefficients whose physical value is below
225220
self.threshold.
@@ -546,8 +541,6 @@ def analyzePerturbations(
546541
num_skip = max(1, int(num_point * frac_scatter_skip))
547542
for pos_idx, sp_name in enumerate(plot_species_names):
548543
sp_idx = disc.species_names.index(sp_name)
549-
if not sp_name in plot_species_names:
550-
continue
551544
ax_row, ax_col = divmod(pos_idx, ncols)
552545
ax = axes[ax_row][ax_col]
553546
sp_col = disc.species_cols[sp_idx]
@@ -642,7 +635,7 @@ def fit(self) -> "SystemDiscovery":
642635
for i, name in enumerate(self.species_names):
643636
if name not in allowed:
644637
self.model.optimizer.coef_[i, 0] = 0.0
645-
self._apply_threshold()
638+
self._applyThreshold()
646639
# Check that the features align with the species names.
647640
if not all([n1 == n2 for n1, n2 in zip(self.species_names, self.model.feature_names)]): # type: ignore
648641
raise RuntimeError(
@@ -1242,7 +1235,7 @@ def discoverNetwork(
12421235
for name, val in accuracy_dct.items():
12431236
print(f" {name}: {val:.6f}")
12441237
print()
1245-
#
1238+
# Print accuracy for time derivatives
12461239
accuracy_dct = disc.calculateSpeciesScores(score_type="derivative", test_df=test_df)
12471240
print("Accuracy for species time derivatives:")
12481241
for name, val in accuracy_dct.items():

tests/test_system_discovery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def test_threshold_prunes_small_coefficients(self) -> None:
485485
coefs_before = disc.model.coefficients().copy()
486486
# Set an extremely high threshold so everything gets pruned
487487
disc.threshold = 1e6
488-
disc._apply_threshold()
488+
disc._applyThreshold()
489489
coefs_after = disc.model.coefficients()
490490
# All should be zero now (or very close)
491491
for i in range(coefs_after.shape[0]):

0 commit comments

Comments
 (0)