Skip to content

Commit

Permalink
Merge pull request #167 from ricardogsilva/166-replace-loess-with-pyl…
Browse files Browse the repository at this point in the history
…oess

Replaced loess.loess_1d() calls with pyloess.loess()
  • Loading branch information
francbartoli authored Jul 15, 2024
2 parents 0b7d575 + 2cb7cdd commit 6143427
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 232 deletions.
15 changes: 10 additions & 5 deletions arpav_ppcv/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import netCDF4
import numpy as np
import pandas as pd
import pyloess
import pymannkendall as mk
import pyproj
import shapely
Expand All @@ -20,7 +21,6 @@
from anyio.from_thread import start_blocking_portal
from dateutil.parser import isoparse
from geoalchemy2.shape import to_shape
from loess.loess_1d import loess_1d
from pyproj.enums import TransformDirection
from shapely.ops import transform

Expand Down Expand Up @@ -392,11 +392,11 @@ async def async_retrieve_data_via_ncss(

async def retrieve_multiple_ncss_datasets(
settings: ArpavPpcvSettings,
client: httpx.AsyncClient,
datasets_to_retrieve: list[coverages.CoverageInternal],
point_geom: shapely.Point,
temporal_range: tuple[dt.datetime | None, dt.datetime | None],
):
client = httpx.AsyncClient()
raw_data = {}
async with anyio.create_task_group() as tg:
for to_retrieve in datasets_to_retrieve:
Expand Down Expand Up @@ -529,6 +529,7 @@ def get_related_coverages(
def get_coverage_time_series(
settings: ArpavPpcvSettings,
session: sqlmodel.Session,
http_client: httpx.AsyncClient,
coverage: coverages.CoverageInternal,
point_geom: shapely.Point,
temporal_range: str,
Expand Down Expand Up @@ -564,6 +565,7 @@ def get_coverage_time_series(
raw_data = portal.call(
retrieve_multiple_ncss_datasets,
settings,
http_client,
to_retrieve_from_ncss,
point_geom,
(start, end),
Expand Down Expand Up @@ -797,10 +799,13 @@ def _apply_loess_smoothing(
with warnings.catch_warnings():
if ignore_warnings:
warnings.simplefilter("ignore")
_, loess_smoothed, _ = loess_1d(
df.index.year.astype("int"), df[source_column_name], degree=0.2, frac=0.75
loess_smoothed = pyloess.loess(
df.index.year.astype("int").values,
df[source_column_name],
span=0.75,
degree=2,
)
return loess_smoothed
return loess_smoothed[:, 1]


def _parse_temporal_range(
Expand Down
2 changes: 2 additions & 0 deletions arpav_ppcv/webapp/api_v2/routers/coverages.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ def get_climate_barometer_time_series(
def get_time_series(
db_session: Annotated[Session, Depends(dependencies.get_db_session)],
settings: Annotated[ArpavPpcvSettings, Depends(dependencies.get_settings)],
http_client: Annotated[httpx.AsyncClient, Depends(dependencies.get_http_client)],
coverage_identifier: str,
coords: str,
datetime: Optional[str] = "../..",
Expand Down Expand Up @@ -460,6 +461,7 @@ def get_time_series(
) = operations.get_coverage_time_series(
settings,
db_session,
http_client,
coverage,
point_geom,
datetime,
Expand Down
62 changes: 20 additions & 42 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ itsdangerous = "^2.2.0"
jinja2 = "^3.1.4"
pyyaml = "^6.0.1"
alembic-postgresql-enum = "^1.2.0"
loess = "^2.1.2"
pymannkendall = "^1.4.3"
typing-extensions = "^4.12.1"
netcdf4 = "^1.7.1"
cftime = "^1.6.4"
babel = "^2.15.0"
pyloess = "^0.1.0"


[tool.poetry.group.dev]
Expand Down
Loading

0 comments on commit 6143427

Please sign in to comment.