Skip to content

Commit

Permalink
streamlined and optimised tide functions + added CI
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsail committed Mar 10, 2024
1 parent c4bc72c commit 7455427
Show file tree
Hide file tree
Showing 27 changed files with 287,887 additions and 9,229 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: test

on:
push:
branches:
- "main"
- "master"
- "dev"
paths:
- "**.py"
- ".github/workflows/*test*.yml"
- "pyproject.toml"
- "poetry.lock"
- "requirements/requirements*.txt"
pull_request:
paths:
- "**.py"
- ".github/workflows/*test*.yml"
- "pyproject.toml"
- "poetry.lock"
- "requirements/requirements*.txt"

jobs:
test:
name: "test Python ${{ matrix.python }} on ${{ matrix.os }}"
runs-on: "${{ matrix.os }}"
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
python: ["3.9", "3.10", "3.11"]
# include:
# - os: "macos-latest"
# python-version: "3.10"
defaults:
run:
shell: "bash -eo pipefail {0}"

steps:
- uses: "actions/checkout@main"
- uses: "actions/setup-python@main"
with:
python-version: "${{ matrix.python }}"
- uses: "actions/cache@main"
id: "cache"
with:
path: "${{ env.pythonLocation }}"
key: "test-${{ runner.os }}-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml', 'requirements/*') }}"
- run: "python --version"
- run: "python -mpip install -U pip"
- run: "python -mpip --version"
- run: "python -mpip install -r requirements/requirements-dev.txt"
- run: "python -mpip install ./"
- run: "python -mpip cache info"
- run: "python -mpip freeze"
- run: "pytest tests/"
env:
PYTHONPATH: ${{ github.workspace }}/tests
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ repos:
"-o",
"requirements/requirements-dev.txt",
]

- repo: "local"
hooks:
- id: "mypy"
Expand Down
11 changes: 11 additions & 0 deletions analysea/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
import datetime
from typing import Any
from typing import Dict
from typing import TypedDict
from typing import TypeVar
from typing import Union

import numpy.typing as npt
import pandas as pd
from typing_extensions import NotRequired
from typing_extensions import TypeAlias # "from typing" in Python 3.9+


StrDict: TypeAlias = Dict[str, Any]
DateTimeLike: TypeAlias = Union[str, datetime.date, datetime.datetime, pd.Timestamp]

ScalarOrArray = TypeVar("ScalarOrArray", int, float, npt.NDArray[Any])


class UTideArgs(TypedDict):
constit: NotRequired[str]
method: NotRequired[str]
order_constit: NotRequired[str]
Rayleigh_min: NotRequired[float]
lat: str
verbose: NotRequired[str]
4 changes: 2 additions & 2 deletions analysea/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def interp(df: pd.Series[Any], new_index: pd.Index[Any]) -> pd.DataFrame:
return df_out


def filter_fft(df: pd.DataFrame) -> pd.DataFrame:
def filter_fft(df: pd.DataFrame, fcut: float = 20) -> pd.DataFrame:
# df is a single channel dataframe with :
# index as pandas.DatetimeIndex
data = df.dropna().values
Expand All @@ -31,7 +31,7 @@ def filter_fft(df: pd.DataFrame) -> pd.DataFrame:
fA = fs * 3600 * 24 # seconds in a day
fftfreq = sp.fftpack.fftfreq(len(temp_psd), 1 / fA)
temp_fft_bis = temp_fft.copy()
temp_fft_bis[np.abs(fftfreq) > 20] = 0
temp_fft_bis[np.abs(fftfreq) > fcut] = 0
# suppressing everything passed the 10th harmonic
# (first one being the semi-diurnal consituent of the tide)
temp_slow = np.real(sp.fftpack.ifft(temp_fft_bis))
Expand Down
15 changes: 8 additions & 7 deletions analysea/gesla.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

import warnings
from typing import Optional
from typing import Union

import geopandas as gp
Expand Down Expand Up @@ -132,11 +131,11 @@ def load_N_closest(self, lat, lon, N=1, force_xarray=False):

def load_lat_lon_range(
self,
region: Optional[Union[Polygon, MultiPolygon]] = None,
south_lat: Optional[float] = None,
north_lat: Optional[float] = None,
west_lon: Optional[float] = None,
east_lon: Optional[float] = None,
region: Union[Polygon, MultiPolygon] | None,
south_lat: float | None,
north_lat: float | None,
west_lon: float | None,
east_lon: float | None,
force_xarray=False,
):
"""Load GESLA records within a rectangular lat/lon range into a xarray.
Expand Down Expand Up @@ -171,7 +170,9 @@ def load_lat_lon_range(
)

locations = gp.GeoDataFrame(
self.meta, geometry=gp.points_from_xy(self.meta.longitude, self.meta.latitude), crs="EPSG:4326"
self.meta,
geometry=gp.points_from_xy(self.meta.longitude, self.meta.latitude),
crs="EPSG:4326",
)

meta = self.meta.loc[locations.within(region)]
Expand Down
204 changes: 0 additions & 204 deletions analysea/plot.py

This file was deleted.

17 changes: 0 additions & 17 deletions analysea/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import numpy.typing as npt
import pandas as pd
import ruptures as rpt
from scipy import interpolate
from skimage.restoration import denoise_tv_chambolle

from analysea.utils import detect_time_step

Expand Down Expand Up @@ -69,18 +67,3 @@ def remove_steps_simple(df: pd.DataFrame, threshold: float) -> Tuple[pd.DataFram
step = df.interpolate().iloc[stepx : steps_ix[i + 1]].mean()
step_function.iloc[stepx : steps_ix[i + 1]] = step
return step_function, steps_ix


def step_function_TV(df: pd.DataFrame, weight: float = 1) -> Tuple[pd.DataFrame, npt.NDArray[Any]]:
idx = range(0, len(df), 200)
signal = np.array(df.interpolate())[idx]
# adjust the parameters
signal_denoise = denoise_tv_chambolle(signal, weight=weight) # type: ignore[no-untyped-call]
# x_step = -2*np.cumsum(signal_denoise)
# step_indicator = x_step == x_step.max()
f = interpolate.interp1d(
idx, signal_denoise, fill_value="extrapolate"
) # for extrapolation in last scipy version
res = pd.Series(data=f(range(len(df))), index=df.index)
#
return remove_steps_simple(res, res.std())
Loading

0 comments on commit 7455427

Please sign in to comment.