Feat: forecast input bounds - #2555
Conversation
The three regressor options accept a source-filtered sensor reference, so a regressor can say which of the sources recording on a sensor it reads. The target sensor took a bare ID only, so a sensor that several sources report on was trained on whichever of them won each event, with no way to say which one holds the truth. The target now accepts the same reference. A bare ID keeps behaving as before: every source is trained on except forecasters, which are left out so that a forecaster does not learn from its own forecasts. A reference replaces that default rather than adding to it, so pass exclude-source-types yourself to keep forecasters out alongside another filter. Forecasts are still recorded on the sensor itself, never on a source-filtered view of it, so the output sensor unwraps a referenced target. The reference survives serialization into a queued job and storage on an automation. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Main gained the scheduling side of automations (#2293, #2294) while this branch was open. Both sides rewrote the sensor-ID collection in get_automation_job_stats: main split it into a scheduling and a forecasting branch, this branch taught it to resolve a target stored as a source-filtered reference. Kept main's split, resolving the reference inside its forecasting branch. Also unwrapped a referenced target in create_automation, whose new check that the sensor to forecast belongs to the automation's asset only recognised a plain Sensor, and so would have passed a filtered target over in silence. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
#2297 moved the code this branch edited into helpers, so this branch's handling of a source-filtered target sensor moves with it: - data_add.py: parse a JSON target sensor reference, then drop unset values the way main now does, empty tuples included. - Job stats: main's shared _relevant_sensor_ids reads each stored parameter through _stored_sensor_id, so a reference counts, where its int() would have skipped it silently. - A forecast automation's preparation, now _prepare_forecast_automation, unwraps a SensorReference before checking which asset the sensor is on. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC Signed-off-by: F.N. Claessen <claessen@seita.nl>
Context: - Clipping and snapping are about to be applied to forecaster inputs as well as to forecast output, so the logic can no longer live inside the output path. Change: - Split parse_bounds and apply_bounds_to_values out of apply_forecast_post_processing, keeping snap-then-clip order and the [first, second) interval semantics. - Let error messages carry a label, so input bounds can name the sensor at fault. - Move _is_parseable_quantity here from the schema, where the input reference schema can reach it too without importing the pipeline schema. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
… per regressor Context: - detect_and_fill_missing_values copied the whole frame on every pass of its per-sensor loop, so each pass built a Darts series holding every sensor's column. Stacking those passes gave N copies of all N columns: two past regressors reached LightGBM as components a, b, a_1, b_1. - The test covering this frame shape monkeypatches the method away, so the real one never ran against more than one regressor. Change: - Narrow the frame to the sensor's own column before converting to a Darts series, keeping the missing-column case on its existing all-NaN path. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…sor bounds Context: - lower, upper and snap only shaped the forecast on its way out. A sensor with implausible readings could not be cleaned up on its way in, so training on it meant fixing the data upstream or copying it to another sensor. Change: - Let a regressor or target reference carry lower, upper and snap, alongside the source filters it already takes. A bare sensor ID keeps working unchanged. - Hold the bounds on a forecaster-specific reference schema rather than the shared one, which flex-model and flex-context also use, where they mean nothing. - Bound each input series after its gaps are filled, so an interpolated value is bounded too, reusing the snap-then-clip order and interval semantics of the output path. - Carry the bounds through the queued-job payload, omitting them when unset so a reference without cleaning serialises exactly as before. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context: - Forecaster inputs can now carry cleaning bounds, and filling no longer duplicates regressor columns. Neither was covered. Change: - Test that bounds clip, snap and reach values interpolated across a gap, that they read the regressor's own unit rather than the target's, and that an incompatible unit is refused by name. - Test that two regressors reach the model as two components. - Name the bounded input in quantity-conversion errors too, so a bad bound on a regressor no longer reports itself as forecast post-processing. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context: - Regressor and target references now take lower, upper and snap, and the shared sensor reference deliberately does not. Change: - Test that a regressor and a target load their bounds, that an unparseable bound is refused at load time, and that a reference asking for neither bounds nor filters still collapses to the plain sensor. - Test that the shared sensor reference, which flex-model and flex-context use, refuses the bound keys that the forecaster's own reference accepts. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context: - lower, upper and snap can now clean a regressor or the target before training, not only shape the forecast on its way out. Change: - Add a section covering where the bounds go, that each sensor's bounds are read in its own unit, and that input and output bounds are configured separately. - Say plainly that bounding runs after gaps are filled, including what that costs when a bad reading sits next to a gap. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…or fix Context: - Both are user-visible: a new way to configure a forecaster, and a correction to what a forecaster with several regressors trained on. Change: - Add a New features entry for the cleaning bounds and a Bugfixes entry for the duplicated regressor data, both pointing at PR #2555. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Documentation build overview
20 files changed ·
|
Extending the shared
No rush on formally deprecating this, but worth a comment in the code that it is meant to be deprecated. If in the user documentation we advise using it (perhaps in an example) then let's replace that example with the new way.
That's completely fine. I actually think all sensor references could use this. |
There was a problem hiding this comment.
🟡 Changes recommended
Schema/OpenAPI metadata and error-handling/docstring convention issues need to be addressed before this can be safely approved.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR extends the forecasting pipeline to support input-side cleaning bounds (lower, upper, snap) on both target and regressor sensor references, so training data can be snapped/clipped before model fitting. It also fixes a regressor gap-filling bug where each per-sensor pass accidentally duplicated all columns into the Darts series, leading to duplicated model components.
Changes:
- Add a forecaster-specific sensor reference schema (
ForecastInputReference*) that supports optional input bounds while keeping the sharedSensorReferenceSchemastrict. - Apply input bounds after missing-value filling in
BasePipeline.detect_and_fill_missing_values, and refactor output post-processing to reuse shared bound parsing/application helpers. - Add/adjust tests and documentation, plus changelog entries for both the new feature and the regressor-component duplication bugfix.
File summaries
| File | Description |
|---|---|
| flexmeasures/data/tests/test_forecasting_pipeline.py | Adds pipeline-level tests for input bounds and for the “one component per regressor” regression. |
| flexmeasures/data/schemas/tests/test_forecasting.py | Adds schema tests ensuring bounds load correctly for forecasters and remain rejected on shared sensor references. |
| flexmeasures/data/schemas/forecasting/references.py | Introduces ForecastInputReference, ForecastInputReferenceSchema, and ForecastInputField to support bounds on forecaster inputs. |
| flexmeasures/data/schemas/forecasting/pipeline.py | Switches forecaster regressor/target fields to ForecastInputField and updates schema behavior accordingly. |
| flexmeasures/data/models/forecasting/utils.py | Extracts reusable bound parsing/application (parse_bounds, apply_bounds_to_values) and reuses it for output post-processing. |
| flexmeasures/data/models/forecasting/pipelines/train_predict.py | Ensures queued-job payload round-trips forecasting-specific references (including bounds). |
| flexmeasures/data/models/forecasting/pipelines/base.py | Fixes regressor duplication during filling, and applies input bounds after filling via shared helpers. |
| documentation/features/forecasting.rst | Documents how to configure input-side bounds for regressors and the target. |
| documentation/changelog.rst | Adds changelog entries for the new feature and the regressor duplication bugfix. |
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 7
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…tion Context: - Schemas need to tell a usable bound from an unusable one while loading, and the forecaster's copy of that check caught a bare Exception, against the repo's error-handling guideline. - The check has to live somewhere both schemas/sensors.py and the forecasting models can reach, and the latter already imports the former. Change: - Add is_parseable_quantity here, catching QUANTITY_PARSE_ERRORS: pint's own errors plus the tokenizer and arithmetic errors its expression parser surfaces for input like '', '[[[' or '1/0', which PintError alone misses. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context: - Adding a typing import to this module brought it into mypy's file list, which is built by grepping for "from typing import", and that surfaced a latent error in split_into_magnitude_and_unit. Change: - Return the formatted magnitude through its own local instead of writing it back over the str parameter. Behaviour is unchanged, as its doctests show. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context: - Review asked for the bounds to sit on the shared sensor reference rather than a forecaster-specific one, since they are at least as useful for the data the scheduler reads. `default` already sits there without applying everywhere. Change: - Move lower, upper and snap onto SensorReferenceSchema and SensorReference, and return a reference whenever either bound keys or source filters are given. - Drop the forecaster-specific reference schema, field and dataclass, which the forecasting pipeline no longer needs. - Say on each field where the bounds are honoured so far, the way `default` documents its own gap: forecaster inputs act on them, flex-model and flex-context accept and ignore them until the scheduler follow-up. - This also corrects the published OpenAPI, which advertised a plain SensorReference for a field that accepted more. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context: - The bounds moved off the forecaster-specific reference, so the test that pinned them off the shared schema now asserted the opposite of the design. Change: - Point the pipeline and schema tests at SensorReference. - Replace the isolation test with its inverse: the shared reference takes the bounds, and still refuses one that cannot be read as a quantity. - Add a test that a reference setting no bounds serialises as it did before, while a meaningful zero bound survives. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
…nsure-positive Context: - Review found the regressor and target descriptions still describing the dict form as source filtering only, which misleads anyone writing config, and asked for ensure-positive to be marked as intended for deprecation. Change: - Say on the regressor and target fields that a reference can also carry bounds, and spell out on the target that input bounds clean what the model learns from while the forecaster's own bounds shape what it writes back out. - Mark ensure-positive as meant for deprecation, in its description and beside the line that applies it, pointing at the explicit lower bound instead. - Catch pint's parse errors rather than every exception when reading a bound. - Re-wrap the snap docstring to break only after punctuation. - Say in the payload docstring that bounds are serialised alongside filters. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
Context: - The bounds moved onto the shared sensor reference, so flex-model and flex-context references accept them, while only forecaster inputs act on them. Change: - Say so, rather than leaving a reader to find out that a bound set on a scheduling reference is quietly ignored. Signed-off-by: Mohamed Belhsan Hmida <mohamedbelhsanhmida@gmail.com>
All addressed, plus the seven Copilot comments Shared schema: you're right, moved. Scheduling: follow-up PR, landing before v1.1. Small change, but it sits behind ~50 scheduling call sites, and a wrong bound there produces a plausible schedule rather than an error — not what I want in an rc. Changelog stays one entry, both PR links.
|
… and generators Bound parsing and application move out of the forecasting utils into their own module, so that sensor references and schedulers can use them without importing forecasting code. The parseability check that the forecaster config and the sensor reference schema both repeated is now one helper. The quantity-parsing helpers in unit_utils now sit below the registry setup instead of splitting it in two. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…hen it is loaded The referenced sensor is already loaded when its bounds are validated, so an incompatible unit, a snap target outside its interval and a lower bound above the upper bound are now refused up front, instead of surfacing inside the queued job once its data has been read. The three schema tests that bounded an MWh sensor in kW relied on that gap, and now use the MW dummy sensor. A reference also parses its bounds once, and applies them itself, rather than having each read parse them again. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ferences until scheduling applies them The bounds live on the shared sensor reference, but only forecaster inputs act on them for now. A flex-model or flex-context reference used to accept them and silently drop them, without even checking them, since those references are deserialized by their own field rather than by the shared schema. Refusing them keeps a release from shipping bounds that look configured but do nothing. Scheduling will apply them in a follow-up, which lifts this refusal. The shared bound validation also checks the shape of each snap entry itself now, so it reports a malformed interval instead of failing on it, whichever field feeds it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…rries it on its own Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
There was a problem hiding this comment.
🟡 Changes recommended
There are a few correctness/documentation inconsistencies in the changed code paths (notably bounds refusal logic for scheduling references and ensure_positive default documentation) that should be addressed before approval.
Get a fresh assessment by requesting another Copilot review.
Review details
- Files reviewed: 14/14 changed files
- Comments generated: 4
- Review effort level: Lite
…lly sets, not an explicit null or empty snap Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…tity parse errors Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
… as it does Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ounds Takes #2555's refusal of unset bounds (an explicit null or empty snap) as the rule for when a scheduling reference counts as bounded. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
There was a problem hiding this comment.
🟡 Changes recommended
The current SensorIdOrReferenceField treats no-op bound keys as “cleaning requested” (changing backward-compatible deserialization/serialization), and boolean values are unintentionally accepted as numeric bounds.
Get a fresh assessment by requesting another Copilot review.
Review details
Suppressed comments (3)
Previously missed (3) — in code that hasn't changed since the last review.
flexmeasures/utils/bound_utils.py:35
_quantity_to_sensor_valueacceptsboolas a numeric bound becauseboolis a subclass ofint(numbers.Real). That can silently turntrue/falseinto1.0/0.0bounds.
It’s safer to explicitly exclude booleans from the numeric fast-path.
flexmeasures/utils/unit_utils.py:63
is_parseable_quantitycurrently treatsboolvalues as parseable quantities becauseboolis a subclass ofint(numbers.Real). That means schema validation would accepttrue/falseas bounds and later interpret them as1.0/0.0, which is very likely unintended input.
flexmeasures/utils/bound_utils.py:218apply_bounds_to_valuessays “Values that are not a number are left alone”, but the implementation coercesvaluestodtype=float, which will raise on non-numeric inputs (it only preserves NaNs). To avoid misleading readers, consider tightening the wording to describe NaN handling explicitly.
- Files reviewed: 14/14 changed files
- Comments generated: 1
- Review effort level: Lite
…nds a plain sensor An explicit null bound or empty snap no longer turns a regressor or target into a reference, so its queued-job payload stays a bare sensor ID. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…values alone A JSON true or false is a numbers.Real to Python, so it passed as a bound of 1 or 0. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
…wer and upper bounds already are The shared reference schema refused it, while the flex-config field accepted it, so the same reference loaded in one place and failed in another. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: F.N. Claessen <claessen@seita.nl>
There was a problem hiding this comment.
🔵 Needs a closer look
It changes core forecasting schema and pipeline behavior (input data cleaning and series construction), which merits final human review despite strong test coverage.
Review details
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
flexmeasures/data/schemas/sensors.py:386
- The
_sets_boundsdocstring summary does not end with a period, which is inconsistent with the repo’s docstring convention and can make the docs/readability uneven.
- Files reviewed: 14/14 changed files
- Comments generated: 0 new
- Review effort level: Lite
…/forecast-target-source-filter Signed-off-by: F.N. Claessen <claessen@seita.nl> # Conflicts: # flexmeasures/cli/data_add.py
…st-input-bounds Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ounds Signed-off-by: F.N. Claessen <claessen@seita.nl>
Description
Stacked on #2542 — base this PR on
feat/forecast-target-source-filter, notmain. It depends on that PR's change making the forecast target accept a sensor reference.lower,upperandsnapcurrently shape a forecast on its way out, right before it is stored. This PR lets the same three fields be set on an individual regressor or on the target, where they clean that sensor's readings on the way in, before the model trains on them. It is for a sensor whose recorded data is not trustworthy as it stands — an occasional implausible spike, or an error sentinel such as-9999— that you would otherwise have to correct upstream or copy to a second sensor.Regressor and target references accept
lower,upperandsnapalongside the source filters they already take. A bare sensor ID keeps working unchanged.The bounds live on the shared
SensorReferenceSchema, so any sensor reference can carry them. Only forecasters apply them in this PR: flex-model and flex-context references refuse them for now, rather than accepting bounds they would ignore. Scheduling applies them in the follow-up, Let the scheduler clean its inputs with bounds on a sensor reference #2561.Bounds that cannot be applied to the referenced sensor (an incompatible unit, a snap target outside its interval, a lower bound above the upper bound) are refused when the reference is loaded, not inside the queued job.
Each sensor's bounds are read in that sensor's own unit, not the unit of the sensor being forecast.
Bounding runs after gap filling, so a value interpolated across a gap is bounded too.
Snapping and clipping reuse the output path's helpers, now in
flexmeasures/utils/bound_utils.py, so the snap-then-clip order and the[first, second)interval rule are identical on both sides by construction. A reference parses its bounds once, not once per read.Bounds survive the queued-job payload, and are omitted when unset so a reference without cleaning serialises exactly as before.
Also fixes a pre-existing bug found on the way:
detect_and_fill_missing_valueshanded the model N copies of all N regressor columns. That fix is split out into Hand the forecasting model each regressor once, not once per regressor #2560 (with its changelog entry), so it can land and be backported on its own; the same commit stays here, since this PR builds on it.Changelog: one entry under New features.
Added changelog item in
documentation/changelog.rstLook & Feel
Regressor bounds go in the forecaster config, next to the regressors:
{ "past-regressors": [ 2094, {"sensor": 2095, "lower": "0 kW", "snap": {"0 kW": ["0 kW", "0.5 kW"]}} ] }The target is named in the forecast parameters rather than the config, so its bounds go there:
{ "sensor": {"sensor": 2092, "upper": "20 kW"} }Both are passed with
flexmeasures add forecasts --config/--parameters, or as the APIconfig/ parameters payload. As with the existing output bounds, there are no dedicated CLI flags.Given a regressor reading
[-5, 0.3, 99, <gap>, 4]withlower: 0 kW,upper: 20 kWandsnap: {"0 kW": ["0 kW", "0.5 kW"]}:[-5.0, 0.3, 99.0, 51.5, 4.0][0.0, 0.0, 20.0, 20.0, 4.0]-5clips to 0,0.3snaps to 0,99clips to 20, and the gap — which interpolates to51.5between 99 and 4 — is clipped to 20 as well, because bounding runs after filling.How to test
flexmeasures/data/tests/test_forecasting_pipeline.py:test_input_bounds_clean_a_regressor_after_its_gaps_are_filledtest_input_bounds_leave_an_unbounded_regressor_alonetest_input_bounds_are_read_in_the_regressors_own_unittest_input_bounds_reject_a_unit_the_regressor_cannot_taketest_filling_gives_each_regressor_exactly_one_componentflexmeasures/data/schemas/tests/test_forecasting.py:test_forecaster_config_schema_loads_regressor_cleaning_boundstest_forecaster_config_schema_keeps_an_unbounded_regressor_a_plain_sensortest_forecaster_parameters_schema_loads_target_cleaning_boundstest_forecaster_config_schema_rejects_an_unparseable_regressor_boundtest_cleaning_bounds_live_on_the_shared_sensor_referencetest_a_reference_without_bounds_serialises_as_it_did_beforetest_sensor_reference_refuses_bounds_it_cannot_apply_when_loadedtest_forecaster_config_schema_refuses_a_regressor_bound_in_an_incompatible_unitflexmeasures/data/schemas/tests/test_sensor.py:test_scheduling_references_refuse_bounds_they_would_ignoreWhat was broken to prove these are not vacuous:
test_filling_gives_each_regressor_exactly_one_component(four components instead of two).test_sensor_reference_refuses_bounds_it_cannot_apply_when_loadedandtest_forecaster_config_schema_refuses_a_regressor_bound_in_an_incompatible_unit.test_scheduling_references_refuse_bounds_they_would_ignore.test_input_bounds_leave_an_unbounded_regressor_alonesurvives all of those breaks. It is a control, not a binding test — it pins the unbounded baseline (51.5) that gives the "after filling" claim in the test above it its meaning, and it would catch a regression that made bounding unconditional. Read it as documentation of the baseline rather than as coverage of the feature.The existing output post-processing tests were left untouched and still pass, which is what establishes that pulling
parse_bounds/apply_bounds_to_valuesout ofapply_forecast_post_processingdid not change output behaviour.Further Improvements
10, -9999, <gap>, 14withlower: 0, the gap interpolates from-9999and is then clipped to0, where filling between10and14would have given roughly12. Where readings are wrong rather than merely out of range, the more honest operation may be to treat out-of-bounds values as missing and let interpolation fill across them, instead of clipping them to the bound and injecting a fabricated0into the training data. That interacts with themissing_thresholdaccounting, so it is left as a separate decision.defaultor bounds from stored flex configs: Deleting a sensor leaves flex-config references to it behind when they carry a default (or bounds) #2558.test_forecasting_pipeline.pymixes thedbandfresh_dbfixtures (2 uses against 49), which.github/instructions/testing.instructions.mdexplicitly warns will hang CI. Pre-existing and untouched here, but worth its own issue.Related Items
lower,upperandsnapfor forecast output.Sign-off