Skip to content
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 multiple lint rules #1245

Merged
merged 10 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions benchmarks/interpolation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
import datetime as dt
Expand Down Expand Up @@ -48,7 +47,12 @@ class Data:


def request_weather_data(
parameter: str, lat: float, lon: float, distance: float, start_date: dt.datetime, end_date: dt.datetime
parameter: str,
lat: float,
lon: float,
distance: float,
start_date: dt.datetime,
end_date: dt.datetime,
):
stations = DwdObservationRequest(
parameter=parameter,
Expand Down
12 changes: 8 additions & 4 deletions benchmarks/interpolation_over_time.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
import os
Expand Down Expand Up @@ -45,13 +44,15 @@ def get_regular_df(parameter: str, start_date: datetime, end_date: datetime, exc

def get_rmse(regular_values: pl.Series, interpolated_values: pl.Series) -> float:
return root_mean_squared_error(
regular_values.reshape((-1, 1)).to_list(), interpolated_values.reshape((-1, 1)).to_list()
regular_values.reshape((-1, 1)).to_list(),
interpolated_values.reshape((-1, 1)).to_list(),
)


def get_corr(regular_values: pl.Series, interpolated_values: pl.Series) -> float:
return r_regression(
regular_values.reshape((-1, 1)).to_list(), interpolated_values.reshape((-1, 1)).to_list()
regular_values.reshape((-1, 1)).to_list(),
interpolated_values.reshape((-1, 1)).to_list(),
).item()


Expand All @@ -62,7 +63,10 @@ def visualize(parameter: str, unit: str, regular_df: pl.DataFrame, interpolated_
plt.figure(figsize=(factor * 19.2, factor * 10.8))
plt.plot(regular_df.get_column("date"), regular_df.get_column("value"), color="red", label="regular")
plt.plot(
interpolated_df.get_column("date"), interpolated_df.get_column("value"), color="black", label="interpolated"
interpolated_df.get_column("date"),
interpolated_df.get_column("value"),
color="black",
label="interpolated",
)
ylabel = f"{parameter.lower()} [{unit}]"
plt.ylabel(ylabel)
Expand Down
1 change: 0 additions & 1 deletion benchmarks/interpolation_precipitation_difference.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
from datetime import datetime
Expand Down
3 changes: 1 addition & 2 deletions benchmarks/summary_over_time.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
import os
Expand Down Expand Up @@ -48,7 +47,7 @@ def main():
summarized_df = summarized_df.with_columns(
pl.col("taken_station_id")
.replace({"01050": "yellow", "01048": "green", "01051": "blue", "05282": "violet"})
.alias("color")
.alias("color"),
)

regular_df_01050 = get_regular_df(start_date, end_date, "01050")
Expand Down
1 change: 0 additions & 1 deletion examples/dwd_climate_summary_xarray_dump.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
import os
Expand Down
5 changes: 2 additions & 3 deletions examples/dwd_describe_fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down Expand Up @@ -37,7 +36,7 @@ def fields_example():
resolution=DwdObservationResolution.DAILY,
period=DwdObservationPeriod.RECENT,
language="en",
)
),
)

pprint(
Expand All @@ -46,7 +45,7 @@ def fields_example():
resolution=DwdObservationResolution.DAILY,
period=DwdObservationPeriod.RECENT,
language="de",
)
),
)


Expand Down
10 changes: 7 additions & 3 deletions examples/dwd_road_weather.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down Expand Up @@ -29,14 +28,19 @@ def dwd_road_weather_example():
end_date = dt.datetime.now(ZoneInfo("UTC")).replace(tzinfo=None)
start_date = end_date - dt.timedelta(days=1)
drw_request = DwdRoadRequest(
parameter="airTemperature", start_date=start_date, end_date=end_date
parameter="airTemperature",
start_date=start_date,
end_date=end_date,
).filter_by_station_id("A006")
print(drw_request)
df_drw = drw_request.values.all().df.drop_nulls(subset="value")
print(df_drw)

dobs_request = DwdObservationRequest(
parameter="temperature_air_mean_200", resolution="10_minutes", start_date=start_date, end_date=end_date
parameter="temperature_air_mean_200",
resolution="10_minutes",
start_date=start_date,
end_date=end_date,
).summarize(latlon=(54.8892, 8.9087))
print(dobs_request.stations)
df_dobs = dobs_request.df.drop_nulls(subset="value")
Expand Down
1 change: 0 additions & 1 deletion examples/mosmix_forecasts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
1 change: 0 additions & 1 deletion examples/observations_sql.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
24 changes: 16 additions & 8 deletions examples/observations_station_gaussian_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand All @@ -14,24 +13,29 @@

""" # Noqa:D205,D400

from __future__ import annotations

import logging
import os
from pathlib import Path
from typing import Tuple
from typing import TYPE_CHECKING

import matplotlib.pyplot as plt
import polars as pl
from lmfit import Parameters
from lmfit.model import ModelResult

from wetterdienst import Settings
from wetterdienst.core.timeseries.result import StationsResult
from wetterdienst.provider.dwd.observation import (
DwdObservationParameter,
DwdObservationRequest,
DwdObservationResolution,
)

if TYPE_CHECKING:
from lmfit import Parameters
from lmfit.model import ModelResult

from wetterdienst.core.timeseries.result import StationsResult

HERE = Path(__file__).parent

log = logging.getLogger()
Expand Down Expand Up @@ -103,7 +107,7 @@ def validate_yearly_data(df: pl.DataFrame) -> bool:
return False
return True

def make_composite_yearly_model(self, valid_data: pl.DataFrame) -> Tuple[GaussianModel, Parameters]:
def make_composite_yearly_model(self, valid_data: pl.DataFrame) -> tuple[GaussianModel, Parameters]:
"""makes a composite model
https://lmfit.github.io/lmfit-py/model.html#composite-models-adding-or-multiplying-models"""
number_of_years = valid_data.get_column("date").dt.year().n_unique()
Expand All @@ -129,7 +133,11 @@ def make_composite_yearly_model(self, valid_data: pl.DataFrame) -> Tuple[Gaussia

@staticmethod
def model_pars_update(
year: int, group: pl.DataFrame, pars: Parameters, index_per_year: float, y_max: float
year: int,
group: pl.DataFrame,
pars: Parameters,
index_per_year: float,
y_max: float,
) -> Parameters:
"""updates the initial values of the model parameters"""
idx = group.get_column("rc").to_numpy()
Expand All @@ -150,7 +158,7 @@ def plot_data_and_model(self, valid_data: pl.DataFrame, out: ModelResult, savefi
"year": valid_data.get_column("date"),
"value": valid_data.get_column("value").to_numpy(),
"model": out.best_fit,
}
},
)
title = valid_data.get_column("parameter").unique()[0]
df.to_pandas().plot(x="year", y=["value", "model"], title=title)
Expand Down
1 change: 0 additions & 1 deletion examples/observations_stations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
1 change: 0 additions & 1 deletion examples/plot_german_weather_stations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2023 earthobservations
import os
from pathlib import Path
Expand Down
5 changes: 2 additions & 3 deletions examples/plot_hohenpeissenberg_warming_stripes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2023 earthobservations
import os
from pathlib import Path
Expand Down Expand Up @@ -30,7 +29,7 @@ def plot_hohenpeissenberg_warming_stripes():
Source: https://matplotlib.org/matplotblog/posts/warming-stripes/
"""
request = DwdObservationRequest("temperature_air_mean_200", "annual", "historical").filter_by_name(
"Hohenpeissenberg"
"Hohenpeissenberg",
)
df_values = request.values.all().df
# definition of years
Expand Down Expand Up @@ -65,7 +64,7 @@ def plot_hohenpeissenberg_warming_stripes():
"#cb181d",
"#a50f15",
"#67000d",
]
],
)
ax.set_axis_off()
col = PatchCollection([Rectangle((y, 0), 1, 1) for y in range(first_year, last_year + 1)])
Expand Down
8 changes: 5 additions & 3 deletions examples/plot_temperature_timeseries.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2023 earthobservations
import os
from pathlib import Path
Expand All @@ -21,12 +20,15 @@
def plot_temperature_timeseries():
"""Create plot for README sketch"""
stations = DwdObservationRequest(
parameter="temperature_air_mean_200", resolution="daily", period="historical"
parameter="temperature_air_mean_200",
resolution="daily",
period="historical",
).filter_by_name("Hohenpeissenberg")
df = stations.values.all().df
df_annual = df.group_by([pl.col("date").dt.year()], maintain_order=True).agg(pl.col("value").mean().alias("value"))
df_annual = df_annual.with_columns(
pl.col("date").cast(str).str.to_datetime("%Y"), pl.col("value").mean().alias("mean")
pl.col("date").cast(str).str.to_datetime("%Y"),
pl.col("value").mean().alias("mean"),
)
fig, ax = plt.subplots(tight_layout=True)
df.to_pandas().plot("date", "value", ax=ax, color="blue", label="Tmean,daily", legend=False)
Expand Down
1 change: 0 additions & 1 deletion examples/radar/radar_composite_rw.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
1 change: 0 additions & 1 deletion examples/radar/radar_radolan_cdc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
1 change: 0 additions & 1 deletion examples/radar/radar_radolan_rw.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
1 change: 0 additions & 1 deletion examples/radar/radar_scan_precip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
1 change: 0 additions & 1 deletion examples/radar/radar_scan_volume.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
1 change: 0 additions & 1 deletion examples/radar/radar_site_dx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
1 change: 0 additions & 1 deletion examples/radar/radar_sweep_hdf5.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2023, earthobservations developers.
# Distributed under the MIT License. See LICENSE for more info.
"""
Expand Down
15 changes: 12 additions & 3 deletions examples/wetterdienst_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,9 @@
"]\n",
"values = (\n",
" DwdObservationRequest(\n",
" parameter=parameters, resolution=DwdObservationResolution.DAILY, period=DwdObservationPeriod.HISTORICAL\n",
" parameter=parameters,\n",
" resolution=DwdObservationResolution.DAILY,\n",
" period=DwdObservationPeriod.HISTORICAL,\n",
" )\n",
" .filter_by_station_id(station_id=(1048,))\n",
" .values.all()\n",
Expand Down Expand Up @@ -762,7 +764,7 @@
"for (parameter,), group in values.df.groupby([pl.col(\"parameter\")], maintain_order=True):\n",
" if parameter == \"precipitation_height\":\n",
" agg_df = group.groupby(pl.col(\"date\").dt.year(), maintain_order=True).agg(\n",
" pl.when(pl.col(\"value\").is_not_null().sum() > 330).then(pl.col(\"value\").sum())\n",
" pl.when(pl.col(\"value\").is_not_null().sum() > 330).then(pl.col(\"value\").sum()),\n",
" )\n",
" else:\n",
" agg_df = group.groupby(pl.col(\"date\").dt.year(), maintain_order=True).agg([pl.col(\"value\").mean()])\n",
Expand Down Expand Up @@ -835,7 +837,14 @@
" ax2 = ax\n",
" daily.to_pandas().plot(x=\"date\", y=\"value\", label=parameter, alpha=0.75, ax=ax, c=color, legend=False)\n",
" annual.to_pandas().plot(\n",
" x=\"date\", y=\"value\", kind=\"line\", label=f\"annual({parameter})\", alpha=0.75, ax=ax2, c=\"black\", legend=False\n",
" x=\"date\",\n",
" y=\"value\",\n",
" kind=\"line\",\n",
" label=f\"annual({parameter})\",\n",
" alpha=0.75,\n",
" ax=ax2,\n",
" c=\"black\",\n",
" legend=False,\n",
" )\n",
" ax.legend(loc=0)\n",
" if ax != ax2:\n",
Expand Down
Loading
Loading