generated from openclimatefix/ocf-template
-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
add datetime features #89
Open
peterdudfield
wants to merge
5
commits into
main
Choose a base branch
from
datetime-features
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.
+70
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
abc3485
add datetime features
peterdudfield a3d8046
only make in site dataset
peterdudfield bd75790
Merge commit 'aae2af3818f2f1371097be834554981e2c2f032d' into datetime…
peterdudfield f327ab9
PR comments
peterdudfield e8cc5e2
remove from proces and combine, as it wont be used there
peterdudfield 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 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 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,42 @@ | ||
"""Datapipes to trigonometric date and time to NumpyBatch""" | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from numpy.typing import NDArray | ||
|
||
|
||
def _get_date_time_in_pi( | ||
dt: pd.DatetimeIndex, | ||
) -> tuple[NDArray[np.float64], NDArray[np.float64]]: | ||
""" | ||
Change the datetimes, into time and date scaled in radians | ||
""" | ||
|
||
day_of_year = dt.dayofyear | ||
minute_of_day = dt.minute + dt.hour * 60 | ||
|
||
# converting into positions on sin-cos circle | ||
time_in_pi = (2 * np.pi) * (minute_of_day / (24 * 60)) | ||
date_in_pi = (2 * np.pi) * (day_of_year / 365) | ||
|
||
return date_in_pi, time_in_pi | ||
|
||
|
||
def make_datetime_numpy_batch(datetimes: pd.DatetimeIndex, key_prefix: str = "wind") -> dict: | ||
""" Make dictionary of datetime features""" | ||
time_numpy_batch = {} | ||
|
||
date_in_pi, time_in_pi = _get_date_time_in_pi(datetimes) | ||
|
||
# Store | ||
date_sin_batch_key = key_prefix + "_date_sin" | ||
date_cos_batch_key = key_prefix + "_date_cos" | ||
time_sin_batch_key = key_prefix + "_time_sin" | ||
time_cos_batch_key = key_prefix + "_time_cos" | ||
|
||
time_numpy_batch[date_sin_batch_key] = np.sin(date_in_pi) | ||
time_numpy_batch[date_cos_batch_key] = np.cos(date_in_pi) | ||
time_numpy_batch[time_sin_batch_key] = np.sin(time_in_pi) | ||
time_numpy_batch[time_cos_batch_key] = np.cos(time_in_pi) | ||
|
||
return time_numpy_batch |
This file contains 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 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,26 @@ | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from ocf_data_sampler.numpy_batch.datetime_features import make_datetime_numpy_batch | ||
|
||
from ocf_data_sampler.numpy_batch import GSPBatchKey | ||
|
||
|
||
def test_calculate_azimuth_and_elevation(): | ||
|
||
# Pick the day of the summer solstice | ||
datetimes = pd.to_datetime(["2024-06-20 12:00", "2024-06-20 12:30", "2024-06-20 13:00"]) | ||
|
||
# Calculate sun angles | ||
datetime_features = make_datetime_numpy_batch(datetimes) | ||
|
||
assert len(datetime_features) == 4 | ||
|
||
assert len(datetime_features["wind_date_sin"]) == len(datetimes) | ||
assert (datetime_features["wind_date_cos"] != datetime_features["wind_date_sin"]).all() | ||
|
||
# assert all values are between -1 and 1 | ||
assert all(np.abs(datetime_features["wind_date_sin"]) <= 1) | ||
assert all(np.abs(datetime_features["wind_date_cos"]) <= 1) | ||
assert all(np.abs(datetime_features["wind_time_sin"]) <= 1) | ||
assert all(np.abs(datetime_features["wind_time_cos"]) <= 1) | ||
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. Testing wise - perhaps update to additionaly verify if a custom prefix is sufficiently applied to all dict keys that the function generates and a further basic check if an empty input corresponds to error raising as implemented in main. As example: def test_make_datetime_numpy_batch_custom_key_prefix():
# Test function correctly applies custom prefix to dict keys
datetimes = pd.to_datetime(["2024-06-20 12:00", "2024-06-20 12:30", "2024-06-20 13:00"])
key_prefix = "solar"
datetime_features = make_datetime_numpy_batch(datetimes, key_prefix=key_prefix)
# Assert dict contains expected quantity of keys and verify starting with custom prefix
assert len(datetime_features) == 4
assert all(key.startswith(key_prefix) for key in datetime_features.keys())
def test_make_datetime_numpy_batch_empty_input():
# Verification that function raises error for empty input
datetimes = pd.DatetimeIndex([])
with pytest.raises(ValueError, match="Input datetimes is empty"):
make_datetime_numpy_batch(datetimes) |
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.
Potentially update main function to include input validation - also to simplify the dict construction via literal. Feel this improves maintenance ease a little overall. As example: