dynamicslab / pysindy

A package for the sparse identification of nonlinear dynamical systems from data
https://pysindy.readthedocs.io/en/latest/
Other
1.46k stars 324 forks source link

[BUG] Short-circuit `WrappedOptimizer` when passed an existing SINDy optimizer #572

Open Jacob-Stevens-Haas opened 1 month ago

Jacob-Stevens-Haas commented 1 month ago

WrappedOptimizer allows users to make SINDy-compatible optimizers (which accept unbiasing and normalizing) from generic regression optimizers. However, its called on a range of SINDy optimizers that natively prevent unbiasing.

Reproducing code example:

import pysindy as ps

ps.TrappingSR3(unbias=True)  # ValueError: Trapping does not allow unbiasing
ps.WrappedOptimizer(ps.TrappingSR3(), unbias=True)  # Should raise a ValueError but does not

Solution

Pretty straightforwardly, we can prevent this by implementing WrappedOptimizer.__new__(cls, optimizer, ...) and checking the type of optimizer. If it's already a ps.BaseOptimzer, then just return it (potentially, verifying that a conflicting unbias is not present)