Skip to content

Commit

Permalink
Import from typing
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian4cast committed Sep 26, 2024
1 parent f82ac2a commit 597ad9b
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 45 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ repos:
flake8-builtins,
flake8-bugbear,
flake8-comprehensions,
flake8-docstrings,
flake8-eradicate,
flake8-mutable,
flake8-pytest-style,
Expand Down
6 changes: 3 additions & 3 deletions climetlab_maelstrom_power_production/dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc
import typing as t
from typing import Optional

import climetlab as cml # type: ignore
import pandas as pd # type: ignore
Expand All @@ -24,7 +24,7 @@ class AbstractDataset(cml.Dataset):
)
dataset = None
_as_dataframe = None
_merger: t.Optional[merger.AbstractMerger] = None
_merger: Optional[merger.AbstractMerger] = None

@property
@abc.abstractmethod
Expand All @@ -46,7 +46,7 @@ def _get_data(self) -> url.Url:
"""Get the data of the dataset."""

@property
def merger(self) -> t.Optional[merger.AbstractMerger]:
def merger(self) -> Optional[merger.AbstractMerger]:
"""Get the merger for the source files."""
return self._merger

Expand Down
14 changes: 7 additions & 7 deletions climetlab_maelstrom_power_production/weather/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import abc
import datetime
import itertools
import typing as t
from typing import Optional, Union

import climetlab as cml # type: ignore
import pandas as pd # type: ignore
Expand Down Expand Up @@ -58,7 +58,7 @@ class Weather(dataset.AbstractDataset):
model_timestamp_2 = MODEL_TIMESTAMP_2
dates = AVAILABLE_DATA_DATES

def __init__(self, date: t.Union[str, t.List[str]] = None):
def __init__(self, date: Optional[Union[str, list[str]]] = None):
"""Initialize and load the dataset."""
self.date = _convert_dates(date) if date is not None else self.dates
self._merger = merger.WeatherMerger()
Expand All @@ -67,7 +67,7 @@ def __init__(self, date: t.Union[str, t.List[str]] = None):

@property
@abc.abstractmethod
def type(self) -> str:
def type(self) -> str: # noqa: A003
"""Return the type of the weather data.
Either ml (model level) or pl (pressure level).
Expand All @@ -77,7 +77,7 @@ def type(self) -> str:
@property
def merger(
self,
) -> t.Optional[climetlab_maelstrom_power_production.merger.AbstractMerger]:
) -> Optional[climetlab_maelstrom_power_production.merger.AbstractMerger]:
"""Get the merger for the weather data."""
return self._merger

Expand All @@ -88,7 +88,7 @@ def _get_data(self) -> cml.Source:
date_with_model_timestamp=dates_with_model_timestamps,
)

def _add_timestamps_to_each_date(self) -> t.List[str]:
def _add_timestamps_to_each_date(self) -> list[str]:
dates = (date.strftime(DATE_FORMAT_REMOTE) for date in self.date)
timestamps = (self.model_timestamp_1, self.model_timestamp_2)
dates_with_model_timestamps = (
Expand All @@ -98,7 +98,7 @@ def _add_timestamps_to_each_date(self) -> t.List[str]:
return list(dates_with_model_timestamps)


def _convert_dates(dates: t.Union[str, t.List[str]]) -> t.List[datetime.datetime]:
def _convert_dates(dates: Union[str, list[str]]) -> list[datetime.datetime]:
if isinstance(dates, str):
dates_as_datetime = [_convert_to_datetime(dates)] # type: ignore
else:
Expand All @@ -116,7 +116,7 @@ def _convert_to_datetime(date: str) -> datetime.datetime:
)


def _check_dates_availability(dates: t.List[datetime.datetime]) -> None:
def _check_dates_availability(dates: list[datetime.datetime]) -> None:
for date in dates:
if not _date_is_available(date):
start = AVAILABLE_DATA_DATES[0].strftime(DATE_FORMAT)
Expand Down
4 changes: 2 additions & 2 deletions climetlab_maelstrom_power_production/weather/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#
import typing as t
from typing import Optional

import pandas as pd # type: ignore
import xarray as xr
Expand All @@ -20,7 +20,7 @@ class WeatherMerger(merger.AbstractMerger):
# Coordinate to use for merging multiple datasets.
concat_dim = "time"

def __init__(self, options: t.Optional[dict] = None):
def __init__(self, options: Optional[dict] = None):
"""Initialize the merger."""
self.options = options or {}

Expand Down
21 changes: 10 additions & 11 deletions notebooks/utils/data.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import re
import typing as t
from datetime import datetime
from typing import Dict, List, Set, Tuple, Union

import numpy as np
import pandas as pd
import xarray as xr

Number = t.Union[int, float]
Number = Union[int, float]


def get_closest_grid_point_to_wind_turbine(
production_data: xr.Dataset,
data: xr.Dataset,
) -> t.Dict[str, Number]:
) -> dict[str, Number]:
"""Get the grid point in the data closest to the wind turbine.
Parameters
Expand Down Expand Up @@ -41,7 +40,7 @@ def get_closest_grid_point_to_wind_turbine(
return {"longitude": longitude, "latitude": latitude}


def _get_wind_turbine_coordinates(ds: xr.Dataset) -> t.Tuple[Number, Number]:
def _get_wind_turbine_coordinates(ds: xr.Dataset) -> tuple[Number, Number]:
"""Get the coordinates (longitude, latitude) of a wind turbine from the production data.
Parameters
Expand Down Expand Up @@ -81,8 +80,8 @@ def _get_grid_coordinates(ds: xr.Dataset, coordinate: str) -> np.ndarray:


def _get_closest_grid_point(
grid: t.List[np.ndarray], coordinates: t.Tuple[Number, ...]
) -> t.Tuple[t.Union[int, float], ...]:
grid: list[np.ndarray], coordinates: tuple[Number, ...]
) -> tuple[Union[int, float], ...]:
"""Get the grid point that is closest to the given coordinates.
Parameters
Expand Down Expand Up @@ -122,15 +121,15 @@ def _get_closest_coordinate(coordinates: np.ndarray, value: Number) -> Number:

def resample_and_clear_production_data_to_hourly_timeseries(
production_data: xr.Dataset,
dates: t.List[datetime],
dates: list[datetime],
) -> xr.DataArray:
"""Resample the production data to an hourly timeseries and clear negative values.
Parameters
----------
production_data : pandas.DataFrame
Production data from the `maelstrom-power-production` dataset.
dates : t.List[datetime]
dates : List[datetime]
Dates from the weather data to get all matching timestamps of the resample.
Returns
Expand Down Expand Up @@ -170,8 +169,8 @@ def _resample_production_data_to_hourly_timeseries(


def _get_intersection_of_dates(
left: pd.DataFrame, right: t.List[datetime]
) -> t.Set[pd.Timestamp]:
left: pd.DataFrame, right: list[datetime]
) -> set[pd.Timestamp]:
return sorted(set(left.index) & set(right))


Expand Down
8 changes: 4 additions & 4 deletions notebooks/utils/density.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Functionality for calculating the density of air."""
import typing as t
from typing import Dict, Optional

import numpy as np
import xarray as xr
Expand All @@ -22,7 +22,7 @@


def calculate_air_density(
grid_point: t.Dict[str, float],
grid_point: dict[str, float],
model_level: int,
model_level_data: xr.Dataset,
surface_data: xr.Dataset,
Expand All @@ -32,7 +32,7 @@ def calculate_air_density(
Parameters
----------
grid_point : t.Tuple[float, float]
grid_point : Dict[str, float]
Grid point for which to calculate the air density.
model_level : int
Model level for which to calculate the air density.
Expand Down Expand Up @@ -70,7 +70,7 @@ def _calculate_density(
temperature: xr.DataArray,
pressure: xr.DataArray,
specific_humidity: xr.DataArray,
relative_humidity: t.Optional[xr.DataArray] = None,
relative_humidity: Optional[xr.DataArray] = None,
) -> xr.DataArray:
"""Calculate air density.
Expand Down
7 changes: 4 additions & 3 deletions notebooks/utils/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
from typing import Iterator, List
from collections.abc import Iterator
from typing import List

import numpy as np
import xarray as xr
Expand Down Expand Up @@ -58,7 +59,7 @@ def predict_power_production(
return np.array(production)


def _prepare_x(time_coordinate_name: str, *args: xr.DataArray) -> List:
def _prepare_x(time_coordinate_name: str, *args: xr.DataArray) -> list:
intersecting_dates = _get_intersecting_dates(time_coordinate_name, *args)
intersecting_data = _select_subset(intersecting_dates, time_coordinate_name, *args)
return list(zip(*intersecting_data))
Expand All @@ -77,7 +78,7 @@ def _get_intersecting_dates(


def _select_subset(
dates: List[datetime.datetime], time_coordinate_name: str, *args
dates: list[datetime.datetime], time_coordinate_name: str, *args
) -> Iterator[np.ndarray]:
for arg in args:
yield arg.sel({time_coordinate_name: dates}).values
18 changes: 9 additions & 9 deletions notebooks/utils/time.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import datetime
import typing as t
from typing import Callable, List, Tuple

import numpy as np
import xarray as xr


def get_dates_from_time_coordinate(data: xr.Dataset) -> t.List[datetime.datetime]:
def get_dates_from_time_coordinate(data: xr.Dataset) -> list[datetime.datetime]:
"""Get the time index/coordinate as a list of datetimes."""
return data.coords["time"].to_index().to_list()


def get_time_of_day_and_year(
dates: t.List[datetime.datetime], time_coord_name: str = "time"
) -> t.Tuple[xr.DataArray, xr.DataArray]:
dates: list[datetime.datetime], time_coord_name: str = "time"
) -> tuple[xr.DataArray, xr.DataArray]:
"""Calculate the time of day and year for given datetimes."""
time_of_day = get_time_of_day(dates, time_coord_name=time_coord_name)
time_of_year = get_time_of_year(dates, time_coord_name=time_coord_name)
return time_of_day, time_of_year


def get_time_of_day(
dates: t.List[datetime.datetime], time_coord_name: str = "time"
dates: list[datetime.datetime], time_coord_name: str = "time"
) -> xr.DataArray:
"""Calculate relative time of day for datetimes."""
converted = _apply_to_dates(_time_of_day, dates=dates)
Expand All @@ -31,7 +31,7 @@ def get_time_of_day(


def get_time_of_year(
dates: t.List[datetime.datetime], time_coord_name: str = "time"
dates: list[datetime.datetime], time_coord_name: str = "time"
) -> xr.DataArray:
"""Calculate relative time of year for datetimes."""
converted = _apply_to_dates(_time_of_year, dates=dates)
Expand All @@ -41,11 +41,11 @@ def get_time_of_year(
)


def _apply_to_dates(func: t.Callable, dates: t.List[datetime.datetime]) -> np.ndarray:
def _apply_to_dates(func: Callable, dates: list[datetime.datetime]) -> np.ndarray:
return np.array(list(map(func, dates)))


def _time_of_day(dt: datetime) -> float:
def _time_of_day(dt: datetime.datetime) -> float:
"""Convert a given datetime `dt` to a point on the unit circle, where the 'period' corresponds to one day.
:param datetime.datetime dt: input datetime UTC
Expand All @@ -56,7 +56,7 @@ def _time_of_day(dt: datetime) -> float:
return seconds_since_midnight / 86400


def _time_of_year(dt: datetime) -> float:
def _time_of_year(dt: datetime.datetime) -> float:
"""Convert a given datetime `dt` to a point on the unit circle, where the 'period' corresponds to one year.
:param datetime.datetime dt: input datetime (preferably UTC)
Expand Down
10 changes: 5 additions & 5 deletions notebooks/utils/wind.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import typing as t
from typing import Dict, Tuple

import numpy as np
import xarray as xr


def calculate_absolute_wind_speed_and_wind_direction(
grid_point: t.Dict[str, float],
grid_point: dict[str, float],
model_level: int,
model_level_data: xr.Dataset,
) -> t.Tuple[xr.DataArray, xr.DataArray]:
) -> tuple[xr.DataArray, xr.DataArray]:
"""Calculate the absolute wind speed and wind direction.
Parameters
----------
grid_point : t.Tuple[float, float]
grid_point : Dict[str, float]
Grid point for which to calculate the wind properties.
model_level : int
Model level for which to calculate the wind properties.
Expand All @@ -23,7 +23,7 @@ def calculate_absolute_wind_speed_and_wind_direction(
Returns
-------
t.Tuple[pandas.Series, pandas.Series]
Tuple[xr.DataArray, xr.DataArray]
Absolute wind speed and wind direction (angle relative to longitude).
"""
Expand Down

0 comments on commit 597ad9b

Please sign in to comment.