generated from openclimatefix/ocf-template
-
-
Notifications
You must be signed in to change notification settings - Fork 47
Numpy conversion consolidation function #396
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
felix-e-h-p
wants to merge
4
commits into
main
Choose a base branch
from
numpy-conversion-consolidation
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
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| """Conversion from Xarray to NumpySample""" | ||
|
|
||
| from .convert import convert_to_numpy_sample | ||
| from .datetime_features import encode_datetimes, get_t0_embedding | ||
| from .generation import convert_generation_to_numpy_sample, GenerationSampleKey | ||
| from .nwp import convert_nwp_to_numpy_sample, NWPSampleKey | ||
| from .satellite import convert_satellite_to_numpy_sample, SatelliteSampleKey | ||
| from .collate import stack_np_samples_into_batch | ||
| from .common_types import NumpySample, NumpyBatch, TensorBatch | ||
| from .sun_position import make_sun_position_numpy_sample |
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,60 @@ | ||
| """Convert a dictionary of xarray objects to a NumpySample.""" | ||
|
|
||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
| from ocf_data_sampler.numpy_sample.common_types import NumpySample | ||
|
|
||
|
|
||
| def convert_to_numpy_sample( | ||
| sample: dict[str, xr.DataArray | dict[str, xr.DataArray]], | ||
| t0_idx: int, | ||
| ) -> NumpySample: | ||
| """Convert a dictionary of xarray objects to a NumpySample. | ||
|
|
||
| Args: | ||
| sample: Dictionary of xarray DataArrays, with same structure as used inside | ||
| PVNet Dataset classes. Expected keys are any of following: | ||
| - "generation": DataArray of generation data | ||
| - "sat": DataArray of satellite data | ||
| - "nwp": dict of DataArrays by provider name (e.g. {"ukv": da, "ecmwf": da}) | ||
| t0_idx: Index of t0 within generation | ||
|
|
||
| Returns: | ||
| NumpySample dictionary with all modalities merged | ||
| """ | ||
| numpy_sample: NumpySample = {} | ||
|
|
||
| if "generation" in sample: | ||
| da = sample["generation"] | ||
| numpy_sample.update({ | ||
| "generation": da.values, | ||
| "capacity_mwp": da.capacity_mwp.values[0], | ||
| "time_utc": da["time_utc"].values.astype(float), | ||
| "t0_idx": int(t0_idx), | ||
| "longitude": float(da.longitude.values), | ||
| "latitude": float(da.latitude.values), | ||
| }) | ||
|
|
||
| if "sat" in sample: | ||
| da = sample["sat"] | ||
| numpy_sample.update({ | ||
| "satellite_actual": da.values, | ||
| "satellite_time_utc": da.time_utc.values.astype(float), | ||
| "satellite_x_geostationary": da.x_geostationary.values, | ||
| "satellite_y_geostationary": da.y_geostationary.values, | ||
| }) | ||
|
|
||
| if "nwp" in sample: | ||
| numpy_sample["nwp"] = {} | ||
| for provider, da in sample["nwp"].items(): | ||
| target_time_utc = da.init_time_utc.values + da.step.values | ||
| numpy_sample["nwp"][provider] = { | ||
| "nwp": da.values, | ||
| "nwp_channel_names": da.channel.values, | ||
| "nwp_init_time_utc": da.init_time_utc.values.astype(float), | ||
| "nwp_step": (da.step.values / np.timedelta64(1, "h")).astype(int), | ||
| "nwp_target_time_utc": target_time_utc.astype(float), | ||
| } | ||
|
|
||
| return numpy_sample | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.