-
Notifications
You must be signed in to change notification settings - Fork 38
[929][evalution/io] Make lead time available to evaluation via OutputDataset #1087
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
Draft
grassesi
wants to merge
3
commits into
ecmwf:develop
Choose a base branch
from
grassesi:sgrasse/develop/issue_929_lead_time_in_output
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.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,6 +111,8 @@ class OutputDataset: | |
|
|
||
| channels: list[str] | ||
| geoinfo_channels: list[str] | ||
| # lead time in hours defined as forecast step * length of forecast step (len_hours) | ||
| lead_time_hrs: int | ||
|
|
||
| @functools.cached_property | ||
| def arrays(self) -> dict[str, zarr.Array]: | ||
|
|
@@ -149,6 +151,7 @@ def as_xarray(self, chunk_nsamples=CHUNK_N_SAMPLES) -> xr.Dataset: | |
| "sample": [self.item_key.sample], | ||
| "stream": [self.item_key.stream], | ||
| "forecast_step": [self.item_key.forecast_step], | ||
| "lead_time_hrs": ("forecast_step", [self.lead_time_hrs]), | ||
| "ipoint": self.datapoints, | ||
| "channel": self.channels, # TODO: make sure channel names align with data | ||
| "valid_time": ("ipoint", times.astype("datetime64[ns]")), | ||
|
|
@@ -247,6 +250,7 @@ def _write_dataset(self, item_group: zarr.Group, dataset: OutputDataset): | |
| def _write_metadata(self, dataset_group: zarr.Group, dataset: OutputDataset): | ||
| dataset_group.attrs["channels"] = dataset.channels | ||
| dataset_group.attrs["geoinfo_channels"] = dataset.geoinfo_channels | ||
| dataset_group.attrs["lead_time_hrs"] = dataset.lead_time_hrs | ||
|
|
||
| def _write_arrays(self, dataset_group: zarr.Group, dataset: OutputDataset): | ||
| for array_name, array in dataset.arrays.items(): # suffix is eg. data or coords | ||
|
|
@@ -263,6 +267,14 @@ def _create_dataset(self, group: zarr.Group, name: str, array: NDArray): | |
| ) | ||
| group.create_dataset(name, data=array, chunks=chunks) | ||
|
|
||
| @functools.cached_property | ||
| def example_key(self) -> ItemKey: | ||
| sample, example_sample = next(self.data_root.groups()) | ||
| stream, example_stream = next(example_sample.groups()) | ||
| fstep, example_item = next(example_stream.groups()) | ||
|
|
||
| return ItemKey(sample, fstep, stream) | ||
|
|
||
| @functools.cached_property | ||
| def samples(self) -> list[int]: | ||
| """Query available samples in this zarr store.""" | ||
|
|
@@ -281,8 +293,17 @@ def forecast_steps(self) -> list[int]: | |
| # assume stream/samples/forecast_steps are orthogonal | ||
| _, example_sample = next(self.data_root.groups()) | ||
| _, example_stream = next(example_sample.groups()) | ||
|
|
||
| return list(example_stream.group_keys()) | ||
|
|
||
| @functools.cached_property | ||
| def lead_times(self) -> list[int]: | ||
| """Calculate available lead times from available forecast steps and len_hrs.""" | ||
| example_prediction = self.load_zarr(self.example_key).prediction | ||
| len_hrs = example_prediction.lead_time_hrs // self.example_key.forecast_step | ||
|
|
||
| return [step * len_hrs for step in self.forecast_steps] | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class DataCoordinates: | ||
|
|
@@ -326,6 +347,7 @@ class OutputBatchData: | |
|
|
||
| sample_start: int | ||
| forecast_offset: int | ||
| t_window_len_hours: int | ||
|
|
||
| @functools.cached_property | ||
| def samples(self): | ||
|
|
@@ -386,17 +408,29 @@ def extract(self, key: ItemKey) -> OutputItem: | |
| "Number of channel names does not align with prediction data." | ||
| ) | ||
|
|
||
| lead_time = self.t_window_len_hours * key.forecast_step | ||
|
|
||
| if key.with_source: | ||
| source_dataset = self._extract_sources(offset_key.sample, stream_idx, key) | ||
| source_dataset = self._extract_sources(offset_key.sample, stream_idx, key, lead_time) | ||
| else: | ||
| source_dataset = None | ||
|
|
||
| return OutputItem( | ||
| key=key, | ||
| source=source_dataset, | ||
| target=OutputDataset("target", key, target_data, **dataclasses.asdict(data_coords)), | ||
| target=OutputDataset( | ||
| "target", | ||
| key, | ||
| target_data, | ||
| lead_time_hrs=lead_time, | ||
| **dataclasses.asdict(data_coords), | ||
| ), | ||
| prediction=OutputDataset( | ||
| "prediction", key, preds_data, **dataclasses.asdict(data_coords) | ||
| "prediction", | ||
| key, | ||
| preds_data, | ||
| lead_time_hrs=lead_time, | ||
| **dataclasses.asdict(data_coords), | ||
| ), | ||
| ) | ||
|
|
||
|
|
@@ -456,7 +490,7 @@ def _extract_coordinates(self, stream_idx, offset_key, datapoints) -> DataCoordi | |
|
|
||
| return DataCoordinates(times, coords, geoinfo, channels, geoinfo_channels) | ||
|
|
||
| def _extract_sources(self, sample, stream_idx, key): | ||
| def _extract_sources(self, sample, stream_idx, key, lead_time: int): | ||
|
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. Can we add types for all args. |
||
| channels = self.source_channels[stream_idx] | ||
| geoinfo_channels = self.geoinfo_channels[stream_idx] | ||
|
|
||
|
|
@@ -475,6 +509,7 @@ def _extract_sources(self, sample, stream_idx, key): | |
| source.geoinfos, | ||
| channels, | ||
| geoinfo_channels, | ||
| lead_time, | ||
| ) | ||
|
|
||
| _logger.debug(f"source shape: {source_dataset.data.shape}") | ||
|
|
||
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
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.
are you using it? I don't see a place where you would need it.
Also, let's not used cached_property unless it is vital to do so. Most people will be confused.