Skip to content

Add ResamplerConfig3 and deprecate the old configs - #1499

Open
llucax wants to merge 11 commits into
frequenz-floss:v1.x.xfrom
llucax:resampler-config-3
Open

llucax wants to merge 11 commits into
frequenz-floss:v1.x.xfrom
llucax:resampler-config-3

Conversation

@llucax

@llucax llucax commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

ResamplerConfig and ResamplerConfig2 default to the mean of the last three periods, labelled with the end of the window, which surprises anyone using the data for something other than control (#1473). Changing those defaults would silently change everyone's results, so this adds ResamplerConfig3 and deprecates the old classes and their resampling function protocols. With the new defaults, each value is the mean of its own window when downsampling, with windows closed and labelled on the left, like pandas.

Resampling functions for the new class receive a ResamplingContext with the window being resampled. That makes microgrid.latest_window_mean() possible: it averages only the newest window that has samples, so short gaps don't produce None values, which is what control needs. microgrid.EXAMPLE_RESAMPLER_CONFIG uses it, for examples and as a baseline for applications. The release notes explain the subtle effects of switching the window conventions.

Two things are left out on purpose:

  • inspect.signature() of the deprecated classes returns (*args, **kwargs), because typing_extensions.deprecated adds a variadic __new__ to the class. Calls and type checking aren't affected, and it happens to any class deprecated this way, so it's better fixed in frequenz-core or typing_extensions than here.
  • Subclassing ResamplerConfig2 also shows the deprecation warning of ResamplerConfig, its base class. The release notes mention it. Avoiding it would need extra code just for users that subclass a deprecated class.

Fixes #1473. Part of #1496.

@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) part:data-pipeline Affects the data pipeline part:microgrid Affects the interactions with the microgrid labels Nov 27, 2026
@llucax
llucax force-pushed the resampler-config-3 branch from 1c61c27 to 4f37529 Compare November 27, 2026 14:16
@llucax
llucax force-pushed the resampler-config-3 branch 5 times, most recently from abe1c3a to 16819a6 Compare November 27, 2026 14:48
Marenz
Marenz previously approved these changes Nov 30, 2026

@Marenz Marenz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Looks good. The old configs keep their defaults (closed/label right, max_data_age_in_periods=3.0), and ResamplerConfig3 switches to left-closed, left-labelled windows with max_data_age_in_periods=1.0, as the notes say. The deprecation warnings are tested, and the old configs stay covered by the parametrized resampling tests.

@github-project-automation github-project-automation Bot moved this from To do to Review approved in Python SDK Roadmap Nov 30, 2026
@llucax
llucax dismissed Marenz’s stale review November 30, 2026 11:40

The merge-base changed after approval.

@llucax
llucax force-pushed the resampler-config-3 branch 2 times, most recently from e5db09f to 1001703 Compare December 1, 2026 12:12
The SDK already imports frequenz.core, but only gets it through
frequenz-channels, and it will now use frequenz.core.warnings too. The
minimum version is the one frequenz-channels already requires, so the
installed versions don't change.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The resampler defaults (max_data_age_in_periods=3.0, closed and label
on the right) are surprising outside control use cases: every value is
the mean of the last three periods, and values are labelled with the
end of their window, unlike reporting data and pandas. Changing the
defaults of the existing classes would silently change the results of
every user, so this adds a new configuration class with new defaults:
each value is the mean of its own window, [start, end), labelled with
the window start.

ResamplerConfig3 doesn't inherit from ResamplerConfig. The old classes
will be deprecated, and a subclass of a deprecated class warns when it
is defined, so it would warn every time the SDK is imported. It always
uses the wall clock timer, so it has no align_to, and only the
resampling period can be passed by position, so fields can be added or
reordered later without breaking anyone.

Resampling functions for it receive a ResamplingContext instead of the
configuration and the source properties. It holds both, plus the window
being calculated, with helpers to find previous windows and the window
of a sample. A function that only uses the newest window with data, for
example, can't be written without knowing the window boundaries. New
information can be added to the context later without changing the
signature of resampling functions again.

The new class also rejects a max_data_age_in_periods that is not
finite, or too big for that many resampling periods to fit in a
timedelta, which the old classes accepted and then failed while
resampling. It also rejects resampling periods too small for the
default timer configuration, which ResamplerConfig2 only found when
the resampler created the timer. It checks them by creating that
configuration, so the check always matches the timer.

Its initial_buffer_len is called input_period_estimation_samples, as
it is the number of samples to receive before estimating the input
sampling period, and the buffer no longer starts with that length.

The resampler doesn't accept the new class yet.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax
llucax force-pushed the resampler-config-3 branch from 1001703 to 04c04db Compare December 1, 2026 13:31
@llucax
llucax marked this pull request as ready for review December 1, 2026 13:40
@llucax
llucax requested a review from a team as a code owner December 1, 2026 13:40
@llucax
llucax requested review from cwasicki, daniel-zullo-frequenz, florian-wagner-frequenz, malteschaaf and shsms and removed request for a team December 1, 2026 13:40
@llucax llucax self-assigned this Dec 1, 2026
@llucax
llucax requested a review from Marenz December 1, 2026 13:42
Marenz
Marenz previously approved these changes Dec 1, 2026

@Marenz Marenz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I checked the window math (ResamplingContext.window_offset/window_bounds) against the buffer's boundary handling for both WindowSides, the latest_window_mean bisect logic, and that ResamplerConfig2 is a faithful copy of the old class (defaults and validation), so old results are preserved. The deprecation plumbing (typing_extensions.deprecated, the ignoring_deprecations() wrapper needed because subclassing warns at class-definition time, and the import-doesn't-warn test) is sound.

Only minor, non-blocking notes:

  • tests/timeseries/test_resampling.py:74: _make_config's docstring says it "behaves like the old classes", but it does not set max_data_age_in_periods=3.0 (the new default is 1.0). It works today because every value test passes it explicitly; a future caller could be misled. Either kwargs.setdefault("max_data_age_in_periods", 3.0) or soften the docstring.
  • _config.py:376: ResamplerConfig2 duplicates ~140 lines of the deprecated base to freeze the old defaults and __post_init__. Fine; a one-line comment on why the duplication is deliberate (or a shared private base) would stop it from drifting later.
  • Minor: latest_window_mean lives in microgrid although it is a generic timeseries function; acceptable given it targets the microgrid-control baseline.

With a ResamplerConfig3 the resampler uses the wall clock timer, like
with a ResamplerConfig2, and passes a ResamplingContext to the
resampling function.

ResamplerConfig2 is a subclass of ResamplerConfig, so the configuration
is matched in that order. _calculate_window_end() now gets the narrowed
ResamplerConfig, since the other configurations have no align_to.

The parametrized resampler tests now run with all three classes. They
use the old window conventions for ResamplerConfig3, and call its
resampling function like the old classes do, so the same expectations
hold. New tests cover the new defaults, the context and the default
mean including older samples when upsampling.

The docs now say the resampler expects samples in timestamp order,
which it always did (frequenz-floss#47).

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The resampler calculates the oldest relevant timestamp by subtracting
max_data_age_in_periods periods from the end of the window. With a big
enough maximum data age, the period times the age doesn't fit in a
timedelta, or the result is before the oldest datetime, and every
resampling raised OverflowError.

The old configuration classes accept any maximum data age, so this
can happen with them. ResamplerConfig3 rejects ages that don't fit in
a timedelta with the resampling period, but the result can still be
before the oldest datetime, and when upsampling, the age is relative
to the input period, which can be longer.

Now, when the oldest relevant timestamp can't be represented, all
samples are relevant, which is what such a maximum data age means.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
microgrid.initialize(), the data pipeline, the resampling actor and the
MovingWindow now accept a ResamplerConfig3 too. They only use fields
that all configurations have, or pass the configuration to the
resampler.

The configurations are listed in a union instead of a private alias, so
the documentation doesn't show a private name, and removing the old
classes later only needs a search.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
To control a microgrid, a short gap in a data stream, or a device that
sends data less often than the resampling period, should not make
resampled values None. The old defaults covered this by averaging the
last three periods, which also smoothed and delayed every value.

latest_window_mean() averages only the samples in the newest window that
has any, so each value covers a single window when there is data, and
falls back to previous windows only when the newer ones are empty. With
max_data_age_in_periods=3.0, two missing windows are covered.

It lives in the microgrid package, and the setup for control is
documented in initialize(), because the timeseries package should not
know about microgrids or control.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Examples need a resampler configuration to call microgrid.initialize(),
and repeating the values recommended for control in each of them, with
the explanation of why they are used, is noisy and easy to get out of
sync.

EXAMPLE_RESAMPLER_CONFIG has those values, and its documentation
explains why each one is used. That explanation moves there from the
documentation of initialize(), which now links to it, so readers can
find it from the constant, from initialize() and from
latest_window_mean().

It is meant for examples and quick experiments, not as a default:
applications should build their own configuration, using this one as
a baseline. This is also why initialize() doesn't use it as the
default for its resampler_config argument.

Code examples in attribute docstrings are not linted by the docs
examples checks, so a test checks the dataclasses.replace() example.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Docstring examples, the getting started tutorial, the examples and the
benchmarks that call microgrid.initialize() now use
microgrid.EXAMPLE_RESAMPLER_CONFIG. Microgrid tests use it too, with
dataclasses.replace() when they need another resampling period.

Tests that aren't about the old classes use ResamplerConfig3 too. Where
their expectations depend on values labelled with the end of their
window, they set the old window conventions explicitly. The tests that
are about the monotonic timer, or that run with every configuration
class, keep using the old classes until they are removed.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
ResamplerConfig makes the resampler use a monotonic timer, which is not
suitable for resampling, as it drifts away from the wall clock. The
wall clock timer has been available for a long time through
ResamplerConfig2, and ResamplerConfig3 uses it too.

The warning says the replacement uses a wall clock timer and has
different defaults, so users don't replace one class with the other
without checking how their results change.

ResamplerConfig2 still inherits from ResamplerConfig, so isinstance()
checks and code typed as ResamplerConfig keep working. Subclassing a
deprecated class warns when the subclass is defined, which would
happen every time the SDK is imported, so ResamplerConfig2 is defined
while ignoring deprecations.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
ResamplerConfig3 replaces ResamplerConfig2 with defaults that don't
average several periods and that label values like reporting data, and
with resampling functions that know the window they resample.

The warning says the replacement has different defaults and a
different resampling function signature, so users don't replace one
class with the other without checking how their results change.
Creating a ResamplerConfig2 only warns about ResamplerConfig2, not
about its deprecated base class.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Add the summary, the new defaults with the configuration that keeps the
old results, the ways changing closed and label can break code without
any error, and the new features.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax

llucax commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

  1. I clarified _make_config()’s docstring in 2974116. The helper still leaves the look-back default alone, and every test that checks resampled values sets it explicitly.
  2. ResamplerConfig2 already duplicated those fields and checks on v1.x.x. I’d leave that alone since both old classes are going away. I did fix the indentation of the initial_buffer_len docstring in 56cd79a so the prose renders correctly.
  3. I’m keeping latest_window_mean() in microgrid. The calculation is generic, but its main use here is the control configuration passed to microgrid.initialize().

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:data-pipeline Affects the data pipeline part:docs Affects the documentation part:microgrid Affects the interactions with the microgrid part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.)

Projects

Status: Review approved

Development

Successfully merging this pull request may close these issues.

Use more reasonable defaults in the resampler config

2 participants