Repository navigation
feat: 999 stacked area plot for ensemble model learned weights #1124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fleur-petit
wants to merge
6
commits into
main
Choose a base branch
from
feature/999-stacked-area-plot-for-ensemble-model-contributions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3cd72a4
feat: stacked-area ensemble model weights
fleur-petit 0b6a542
chore: poe autofixes linting and notebook sync.
fleur-petit 64273f3
chore: clear ipynb output.
fleur-petit 73b6994
chore: docstring model contributions plotter specifies model weights …
fleur-petit bea468b
refactor: explainability plotter add common helpers vertical dual plo…
fleur-petit 5348f31
fix: merge conflict testing ruff annotation
fleur-petit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -673,5 +673,41 @@ def predict_contributions( | |
| original_data=data, | ||
| ) | ||
|
|
||
| def predict_with_contributions( | ||
| self, | ||
| data: TimeSeriesDataset, | ||
| forecast_start: datetime | None = None, | ||
| ) -> tuple[ForecastDataset, TimeSeriesDataset]: | ||
| """Generate forecasts and per-model contributions in a single pass. | ||
|
|
||
| Args: | ||
| data: Input time series dataset. | ||
| forecast_start: Optional start time for forecasts. | ||
|
|
||
| Returns: | ||
| A tuple of ``(forecast, contributions)`` where *forecast* is the | ||
| ensemble prediction (a :class:`ForecastDataset`) and *contributions* | ||
| is the :class:`TimeSeriesDataset` returned by the configured combiner. | ||
| Its meaning depends on the combiner; for learned weights, columns | ||
| contain per-model selection weights. | ||
|
|
||
| Raises: | ||
| NotFittedError: If the model has not been fitted yet. | ||
| """ | ||
| if not self.is_fitted: | ||
| raise NotFittedError(self.__class__.__name__) | ||
|
|
||
| ensemble_predictions = self._predict_forecasters(data=data, forecast_start=forecast_start) | ||
|
|
||
| forecast = self._predict_transform_combiner( | ||
| ensemble_dataset=ensemble_predictions, | ||
| original_data=data, | ||
| ) | ||
| contributions = self._predict_contributions_combiner( | ||
| ensemble_dataset=ensemble_predictions, | ||
| original_data=data, | ||
| ) | ||
| return forecast, contributions | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added this so that I would have both the forecast and the contributions for the two model contributions plotter panels. Does it make sense to add this here? Or would another approach be better suited? |
||
|
|
||
|
|
||
| __all__ = ["EnsembleForecastingModel", "EnsembleModelFitResult", "ModelFitResult"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/openstef-models/src/openstef_models/explainability/plotters/common.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # SPDX-FileCopyrightText: 2026 Contributors to the OpenSTEF project <openstef@lfenergy.org> | ||
| # | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| """Layout helpers for explainability plots.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import plotly.graph_objects as go | ||
| from plotly.subplots import make_subplots | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pandas as pd | ||
|
|
||
|
|
||
| class VerticalTwoPanelPlotLayout: | ||
| """Create vertically aligned subplots.""" | ||
|
|
||
| @staticmethod | ||
| def make_two_panel_time_figure() -> go.Figure: | ||
| """Create the vertically aligned subplot layout for two subplots.""" | ||
| return make_subplots(rows=2, cols=1, shared_xaxes=True, row_heights=[0.2, 0.8], vertical_spacing=0.03) | ||
|
|
||
| @staticmethod | ||
| def add_top_panel_line_plot( | ||
| fig: go.Figure, x: pd.Index, y: pd.Series, *, name: str = "Prediction", show_legend: bool = False | ||
| ) -> go.Figure: | ||
| """Add a lineplot. Can be used to add a top panel prediction line plot.""" | ||
| return fig.add_trace( | ||
| go.Scatter( | ||
| x=x, y=y, mode="lines", line={"color": "black", "width": 1.5}, name=name, showlegend=show_legend | ||
| ), | ||
| row=1, | ||
| col=1, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@egordm in the heatmap you showed me the contributions of models at different quantiles simulteneously. That is not what this figure does, it shows the learned model weights for one quantile. It cannot replace the heatmap, but is it a useful addition? Are there tweaks I can do to make it as useful to you as the heatmap?