-
Notifications
You must be signed in to change notification settings - Fork 0
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 climatology features #38
Open
louisPoulain
wants to merge
19
commits into
main
Choose a base branch
from
add-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.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
182bb2f
Create climatology.py
louisPoulain 3f3582b
Add clim for cloud cover
louisPoulain eea04a4
Update discover_input with climatology
louisPoulain 3a5c7cf
Merge branch 'main' into add-features
202e666
Remove unused imports
louisPoulain 2b8d953
Update tests to account for new module
louisPoulain eef3b11
Create test dataset for climatology
louisPoulain a49f5dc
Fix missing registering as fixture
louisPoulain fc717f7
Remove input
louisPoulain a343e3d
Add missing input
louisPoulain 4de66fd
Missing import
82a4a72
Rename to avoid clash with obs
6de62eb
Add interpolation and rename
5bd442b
Update conftest.py
louisPoulain b926cf0
revert update
louisPoulain 302aa90
Remove non needed interp
5f17105
add a new method to compute rolling means over hours and days
f61c213
Move the calculation of rolling mean into mlpp-features
ded706d
Small fix to prevent error if the dataset does not contain any data f…
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
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,60 @@ | ||
import logging | ||
from typing import Dict | ||
|
||
import pandas as pd | ||
import xarray as xr | ||
|
||
from mlpp_features.decorators import out_format | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
# Set global options | ||
xr.set_options(keep_attrs=True) | ||
|
||
|
||
# @out_format() | ||
# def cloud_area_fraction_rollingmean_1h_10d( | ||
# data: Dict[str, xr.Dataset], stations, reftimes, leadtimes, **kwargs | ||
# ) -> xr.DataArray: | ||
# """ | ||
# Climatology of cloud cover: the values were averaged at +-1h and +-10 days to account for seasonal variability | ||
# """ | ||
# return ( | ||
# data["climatology"] | ||
# .mlpp.get("cloud_area_fraction") | ||
# .mlpp.unstack_time(reftimes, leadtimes) | ||
# .astype("float32") | ||
# ) | ||
|
||
|
||
@out_format() | ||
def cloud_area_fraction_rollingmean_1h_10d( | ||
data: Dict[str, xr.Dataset], stations, reftimes, leadtimes, **kwargs | ||
) -> xr.DataArray: | ||
""" | ||
Climatology of cloud cover: the values were averaged at +-1h and +-10 days to account for seasonal variability | ||
""" | ||
rolling_mean_ds = ( | ||
data["obs"] | ||
.mlpp.get("cloud_area_fraction") | ||
.mlpp.rolling_mean_days_and_hours(hours=1, days=10) | ||
) | ||
clim_ds = xr.Dataset( | ||
None, | ||
coords={ | ||
"forecast_reference_time": pd.to_datetime(reftimes).astype("datetime64[ns]"), | ||
"lead_time": leadtimes.astype("timedelta64[ns]"), | ||
}, | ||
) | ||
|
||
clim_ds = clim_ds.assign_coords(time=clim_ds.forecast_reference_time + clim_ds.lead_time) | ||
days = clim_ds.time.dt.dayofyear | ||
hours = clim_ds.time.dt.hour | ||
|
||
clim_ds = ( | ||
rolling_mean_ds.sel(dayofyear=days, hourofday=hours) | ||
.mlpp.unstack_time(reftimes, leadtimes) | ||
.astype("float32") | ||
) | ||
|
||
return clim_ds |
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 |
---|---|---|
|
@@ -272,6 +272,47 @@ def _data(): | |
return _data | ||
|
||
|
||
@pytest.fixture | ||
def clim_dataset(): | ||
"""Create climatology dataset as if loaded from zarr files, still unprocessed.""" | ||
|
||
def _data(): | ||
|
||
variables = [ | ||
"cloud_area_fraction", | ||
] | ||
|
||
stations = _stations_dataframe() | ||
times = pd.date_range("2000-01-01T00", "2000-01-02T00", freq="1h") | ||
|
||
n_times = len(times) | ||
n_stations = len(stations) | ||
|
||
var_shape = (n_times, n_stations) | ||
ds = xr.Dataset( | ||
None, | ||
coords={ | ||
"time": times, | ||
"station": stations.index, | ||
"longitude": ("station", stations.longitude), | ||
"latitude": ("station", stations.latitude), | ||
"height_masl": ("station", stations.height_masl), | ||
"owner_id": ("station", np.random.randint(1, 5, stations.shape[0])), | ||
"pole_height": ("station", np.random.randint(5, 15, stations.shape[0])), | ||
"roof_height": ("station", np.zeros(stations.shape[0])), | ||
}, | ||
) | ||
for var in variables: | ||
measurements = np.random.randn(*var_shape) | ||
nan_idx = [np.random.randint(0, d, 60) for d in var_shape] | ||
measurements[nan_idx[0], nan_idx[1]] = np.nan | ||
ds[var] = (("time", "station"), measurements) | ||
return ds | ||
|
||
return _data | ||
Comment on lines
+275
to
+312
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. If we move to using the "obs" part of the variable dictionnary, we likely don't need this anymore |
||
|
||
|
||
|
||
@pytest.fixture | ||
def preproc_dataset(): | ||
def _data(): | ||
|
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
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.
Sort of fix to pass the pytest.
It fails (without that fix) if one the days window is empty.