Skip to content

Add optional time field to Saver - #352

Open
physicalfleet-dev-team wants to merge 1 commit into
rlabbe:masterfrom
physicalfleet-dev-team:saver-time-field
Open

physicalfleet-dev-team wants to merge 1 commit into
rlabbe:masterfrom
physicalfleet-dev-team:saver-time-field

Conversation

@physicalfleet-dev-team

Copy link
Copy Markdown

Hi Roger! 👋

This adds the optional time field to Saver that you sketched in #231, so a saved history can be plotted or analyzed against time without keeping a parallel list by hand.

How to use it

# fixed time step: every save() advances the time by dt
saver = Saver(kf, dt=0.1)
for z in zs:
    kf.predict()
    kf.update(z)
    saver.save()            # t = 0.1, 0.2, 0.3, ...

saver.to_array()
plt.plot(saver.t, saver.x[:, 0])

# or the time stamp of each measurement (variable rate, gaps, ...)
saver = Saver(kf)
for t, z in zip(times, zs):
    kf.predict()
    kf.update(z)
    saver.save(t)

Details

  • save(t) stores t as given. Saver(kf, dt=..., t0=0.) advances the time by dt on each save(). t0 is the time of the state saved by save_current=True, so with save_current the times are t0, t0+dt, .... An explicit save(t) resets the clock, and later save() calls continue from it (handy after a gap in the measurements).
  • The times live in saver.t / saver['t'] like any other saved attribute, and to_array() converts them too.
  • With neither t nor dt, nothing changes: no t key is added. I deliberately didn't pick up the filter's own dt automatically. Otherwise every existing Saver of a g-h, fading-memory or ensemble filter would suddenly grow a t entry.
  • To keep t aligned with the other lists, mixing timed and untimed saves raises a ValueError. So does saving a time from an object that already has an attribute named t (the message suggests ignore=['t']). Both checks run before anything is appended, so the Saver stays consistent.
  • dt and t0 are keyword-only, so the existing positional signature is unchanged. The Saver's own bookkeeping attributes are named _time_step / _last_time rather than _dt, because save() copies the filter's attributes (e.g. the UKF's _dt) into the Saver's __dict__.

Tests

New tests in filterpy/common/tests/test_helpers.py: no time by default, explicit times, dt with and without save_current/t0, a UKF with dt (its _dt is still saved), an explicit time resetting the dt clock, the consistency errors, and the t name clash. Six of them fail on master, since save() takes no argument there.

On an M1 Mac, Python 3.12, numpy 2, uv pip install -e . pytest scipy matplotlib:

MPLBACKEND=Agg python -m pytest -q filterpy/common   -> 21 passed
MPLBACKEND=Agg python -m pytest -q filterpy          -> 130 passed, 1 failed

The one failure, kalman/tests/test_kf.py::test_steadystate (ValueError: math domain error), also fails on master here. pylint --rcfile=.pylintrc filterpy/common/helpers.py reports no new messages compared to master.

Heads-up: #346 also touches Saver.save() (the property loop). The two changes are in different parts of the method, so a rebase should be easy whichever lands first.

Thanks for filterpy and the book, they taught a lot of us Kalman filtering! 🚀

Saver can now record the time of every saved state in `t`:

* save(t) stores the given time.
* Saver(kf, dt=...) advances the time by dt on every save(), starting at
  t0 (the time of the state saved by save_current). An explicit save(t)
  resets the clock.

Without either, nothing changes. Mixing timed and untimed saves, or a
filter attribute named 't', raises a ValueError instead of producing a
`t` list that does not line up with the other attributes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant