-
Notifications
You must be signed in to change notification settings - Fork 65
implement power spectra #2398
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
iluise
wants to merge
12
commits into
develop
Choose a base branch
from
iluise/develop/power-spectra
base: develop
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.
+1,377
−7
Open
implement power spectra #2398
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c3f945
first working implementation of spectra
iluise effd35c
fix zonal (ukmet) psd
iluise e208eec
remove old psd implementation
iluise 64ed582
remove dead code
iluise efe2d5f
shorter functions
iluise fbca45a
implement inverse transform and tests
iluise 613406f
Merge branch 'develop' into iluise/develop/power-spectra
iluise d9f079d
lint
iluise 7bdaf33
Merge branch 'iluise/develop/power-spectra' of https://github.com/ecm…
iluise 10115e1
add var name to psd plot title
iluise caf169f
Merge branch 'develop' into iluise/develop/power-spectra
iluise 59adf91
Merge branch 'develop' into iluise/develop/power-spectra
iluise 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
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 |
|---|---|---|
|
|
@@ -768,6 +768,76 @@ def quantile_plot_metric_region( | |
| ) | ||
|
|
||
|
|
||
| def _extract_psd_attrs(data_ch: xr.DataArray, fstep: int, ch: str) -> list[dict] | None: | ||
| """Extract PSD curve data from DataArray attrs for a given fstep/channel. | ||
|
|
||
| Returns a single-element list of dicts ready for the plotter, or None if keys are missing. | ||
| """ | ||
| attrs = data_ch.attrs | ||
| fp = f"fstep_{fstep}/" | ||
|
|
||
| for prefix in (f"{fp}{ch}/", fp): | ||
| if f"{prefix}frequencies" in attrs and f"{prefix}psd_target" in attrs: | ||
| return [ | ||
| { | ||
| "frequencies": np.array(attrs[f"{prefix}frequencies"]), | ||
| "psd_target": np.array(attrs[f"{prefix}psd_target"]), | ||
| "psd_prediction": np.array(attrs[f"{prefix}psd_prediction"]), | ||
| "psd_method": attrs.get(f"{fp}psd_method", attrs.get("psd_method", "sht")), | ||
| } | ||
| ] | ||
| return None | ||
|
|
||
|
|
||
| def psd_plot_metric_region( | ||
| metric: str, | ||
| region: str, | ||
| runs: dict, | ||
| scores_dict: dict, | ||
| plotter: object, | ||
| ) -> None: | ||
| """Create PSD plots for all streams and channels for a given metric and region. | ||
|
|
||
| PSD curves (frequencies, target PSD, prediction PSD) are stored in | ||
| ``score.attrs`` by ``Scores.calc_psd`` and read back here. | ||
| """ | ||
| streams_set = collect_streams(runs) | ||
| channels_set = collect_channels(scores_dict, metric, region, runs) | ||
|
|
||
| for stream in streams_set: | ||
| for ch in channels_set: | ||
| for run_id, data in scores_dict[metric][region].get(stream, {}).items(): | ||
| if ch not in np.atleast_1d(data.channel.values): | ||
| continue | ||
|
|
||
| data_ch = data.sel(channel=ch) if "channel" in data.dims else data | ||
| if data_ch.isnull().all(): | ||
| continue | ||
|
|
||
| attr_fsteps = data_ch.attrs.get("attr_fsteps", []) | ||
| if not attr_fsteps: | ||
| _logger.warning(f"PSD attrs missing for {run_id}/{stream}/{ch}. Skipping.") | ||
| continue | ||
|
|
||
| label = runs[run_id].get("label", run_id) | ||
|
|
||
| for fstep in attr_fsteps: | ||
| psd_datasets = _extract_psd_attrs(data_ch, fstep, ch) | ||
| if psd_datasets is None: | ||
| continue | ||
|
|
||
| method_tag = psd_datasets[0].get("psd_method", "sht") | ||
|
Collaborator
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.
|
||
| name = create_filename( | ||
| prefix=[metric, method_tag, region], | ||
| middle=[run_id], | ||
| suffix=[stream, ch, f"fstep{fstep}"], | ||
| ) | ||
| plotter.psd_plot( | ||
| psd_datasets, [label], tag=name, | ||
| variable=ch, forecast_step=str(fstep), | ||
| ) | ||
|
|
||
|
|
||
| def create_filename( | ||
| *, | ||
| prefix: Sequence[str] = (), | ||
|
|
||
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.
Should this be determined from the dataset?