-
Notifications
You must be signed in to change notification settings - Fork 129
Enforce strict mypy type checking and additional ruff rules #492
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
base: master
Are you sure you want to change the base?
Changes from all commits
bf12877
5543e1a
e0c511c
9228ca7
820e7e2
b1edf3f
c5a7a8d
8153e27
16cd01c
6591a14
80d6cdb
5a2fba9
7fe656b
0f1d464
dfdcc97
fd68cd3
2d0497c
2486e59
67f7366
aef7b64
182b93e
0c807ce
80f744a
55d5345
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # SPDX-FileCopyrightText: Contributors to atlite <https://github.com/pypsa/atlite> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import Any, Literal, TypeAlias, TypedDict | ||
|
|
||
| import geopandas as gpd | ||
| import numpy as np | ||
| import scipy.sparse as sp | ||
| import xarray as xr | ||
| from pyproj import CRS | ||
| from shapely.geometry.base import BaseGeometry | ||
|
|
||
| NDArray: TypeAlias = np.ndarray[Any, np.dtype[np.floating[Any]]] | ||
| NDArrayInt: TypeAlias = np.ndarray[Any, np.dtype[np.signedinteger[Any]]] | ||
| NDArrayBool: TypeAlias = np.ndarray[Any, np.dtype[np.bool_]] | ||
| DataArray: TypeAlias = xr.DataArray | ||
| Dataset: TypeAlias = xr.Dataset | ||
| PathLike: TypeAlias = str | Path | ||
| NumericArray: TypeAlias = NDArray | DataArray | ||
| Number: TypeAlias = int | float | np.number[Any] | ||
| GeoDataFrame: TypeAlias = gpd.GeoDataFrame | ||
| GeoSeries: TypeAlias = gpd.GeoSeries | ||
| Geometry: TypeAlias = BaseGeometry | ||
| CrsLike: TypeAlias = str | int | CRS | dict[str, Any] | None | ||
| SparseMatrix: TypeAlias = sp.lil_matrix | sp.csr_matrix | ||
|
|
||
| TrackingType: TypeAlias = ( | ||
| Literal["horizontal", "tilted_horizontal", "vertical", "dual"] | None | ||
|
Member
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. I like to define those literals! But I would not put |
||
| ) | ||
| ClearskyModel: TypeAlias = Literal["simple", "enhanced"] | ||
| TrigonModel: TypeAlias = Literal["simple", "perez"] | ||
| IrradiationType: TypeAlias = Literal["total", "direct", "diffuse", "ground"] | ||
| HeatPumpSource: TypeAlias = Literal["air", "soil"] | ||
| OrientationName: TypeAlias = Literal["latitude_optimal", "constant", "latitude"] | ||
| DataFormat: TypeAlias = Literal["grib", "netcdf"] | ||
|
|
||
|
|
||
| class ERA5RetrievalParams(TypedDict, total=False): | ||
|
Member
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. Is it wise to hardcode that here? Since it's a dict it doesn't give you any benefits, is it? |
||
| product: str | ||
| area: list[float] | ||
| grid: str | ||
| chunks: dict[str, int] | None | ||
| tmpdir: str | Path | None | ||
| lock: Any | None | ||
| data_format: Literal["grib", "netcdf"] | ||
| year: list[str] | ||
| month: list[str] | str | ||
| day: list[str] | str | ||
| time: str | list[str] | ||
| variable: str | list[str] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,58 @@ | ||
| # SPDX-FileCopyrightText: Contributors to atlite <https://github.com/pypsa/atlite> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
| """ | ||
| Functions for aggregating results. | ||
| """ | ||
| """Functions for aggregating results.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, cast | ||
|
|
||
| import dask | ||
| import xarray as xr | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pandas as pd | ||
| from scipy.sparse import spmatrix | ||
|
|
||
| from atlite._types import DataArray | ||
|
|
||
|
|
||
| def aggregate_matrix( | ||
| da: DataArray, | ||
| matrix: spmatrix, | ||
| index: pd.Index, | ||
| ) -> DataArray: | ||
| """ | ||
| Aggregate spatial data with a sparse matrix. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| da : xarray.DataArray | ||
| DataArray with spatial dimensions ``y`` and ``x``. | ||
| matrix : scipy.sparse.spmatrix | ||
| Aggregation matrix mapping flattened spatial cells to ``index``. | ||
| index : pandas.Index | ||
| Index defining the aggregated dimension. | ||
|
|
||
| def aggregate_matrix(da, matrix, index): | ||
| Returns | ||
| ------- | ||
| xarray.DataArray | ||
| Aggregated data indexed by ``index`` and, if present, time. | ||
| """ | ||
| if index.name is None: | ||
| index = index.rename("dim_0") | ||
| if isinstance(da.data, dask.array.core.Array): | ||
| da = da.stack(spatial=("y", "x")) | ||
| da = da.chunk(dict(spatial=-1)) | ||
| return xr.apply_ufunc( | ||
| da = da.chunk({"spatial": -1}) | ||
| result = xr.apply_ufunc( | ||
| lambda da: da * matrix.T, | ||
| da, | ||
| input_core_dims=[["spatial"]], | ||
| output_core_dims=[[index.name]], | ||
| dask="parallelized", | ||
| output_dtypes=[da.dtype], | ||
| dask_gufunc_kwargs=dict(output_sizes={index.name: index.size}), | ||
| dask_gufunc_kwargs={"output_sizes": {index.name: index.size}}, | ||
| ).assign_coords(**{index.name: index}) | ||
| else: | ||
| da = da.stack(spatial=("y", "x")).transpose("spatial", "time") | ||
| return xr.DataArray(matrix * da, [index, da.coords["time"]]) | ||
| return cast("DataArray", result) | ||
| da = da.stack(spatial=("y", "x")).transpose("spatial", "time") | ||
| return xr.DataArray(matrix * da, [index, da.coords["time"]]) |
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.
Do we need all those? Some are just aliases. I think it's much cleaner to just keep for example use
xr.Datasetthan redefining it?