Repository navigation
Conversation
1c61c27 to
4f37529
Compare
abe1c3a to
16819a6
Compare
Marenz
left a comment
There was a problem hiding this comment.
🤖 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.
The merge-base changed after approval.
e5db09f to
1001703
Compare
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>
1001703 to
04c04db
Compare
Marenz
left a comment
There was a problem hiding this comment.
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 setmax_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. Eitherkwargs.setdefault("max_data_age_in_periods", 3.0)or soften the docstring._config.py:376:ResamplerConfig2duplicates ~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_meanlives inmicrogridalthough 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>
04c04db to
c883e17
Compare
|
Thanks for the review!
|
ResamplerConfigandResamplerConfig2default 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 addsResamplerConfig3and 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
ResamplingContextwith the window being resampled. That makesmicrogrid.latest_window_mean()possible: it averages only the newest window that has samples, so short gaps don't produceNonevalues, which is what control needs.microgrid.EXAMPLE_RESAMPLER_CONFIGuses 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), becausetyping_extensions.deprecatedadds 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 infrequenz-coreortyping_extensionsthan here.ResamplerConfig2also shows the deprecation warning ofResamplerConfig, 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.