Skip to content

Commit

Permalink
Update deprecated pandas offset alias
Browse files Browse the repository at this point in the history
  • Loading branch information
connortann committed Jul 30, 2024
1 parent ce530c4 commit c8ed1f0
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 46 deletions.
24 changes: 12 additions & 12 deletions docs/user-guide/4-peaks-over-threshold.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ parameters of the `get_extremes` function:
from pyextremes import get_extremes
from pyextremes.plotting import plot_extremes

extremes = get_extremes(data, "POT", threshold=0.5, r="12H")
extremes = get_extremes(data, "POT", threshold=0.5, r="12h")
plot_extremes(
ts=data,
extremes=extremes,
extremes_method="POT",
extremes_type="high",
threshold=0.5,
r="12H",
r="12h",
)
```

Expand All @@ -53,7 +53,7 @@ parameters of the `get_extremes` function:
from pyextremes import EVA

model = EVA(data=data)
model.get_extremes("POT", threshold=0.5, r="12H")
model.get_extremes("POT", threshold=0.5, r="12h")
model.plot_extremes(show_clusters=True)
```

Expand Down Expand Up @@ -104,7 +104,7 @@ The following paramters are used only when `#!python method="POT"`:
to decluster exceedances by locating clusters where all exceedances are separated
by distances no more than `r` and then locating maximum or minimum
(depends on `extremes_type`) values within each cluster.
By default `#!python r="24H"` (24 hours).
By default `#!python r="24h"` (24 hours).

If we specify all of these parameters then the function would look as:

Expand All @@ -114,7 +114,7 @@ get_extremes(
method="POT",
extremes_type="high",
threshold=0.5,
r="12H",
r="12h",
)
```

Expand All @@ -129,22 +129,22 @@ extracted from the same data and using the same threshold as above, but with a l
=== "Standalone"

```python
extremes = get_extremes(data, "POT", threshold=0.5, r="24H")
extremes = get_extremes(data, "POT", threshold=0.5, r="24h")
plot_extremes(
ts=data,
extremes=extremes,
extremes_method="POT",
extremes_type="high",
threshold=0.5,
r="24H",
r="24h",
)
```

=== "Using EVA"

```python
model = EVA(data=data)
model.get_extremes("POT", threshold=0.5, r="24H")
model.get_extremes("POT", threshold=0.5, r="24h")
model.plot_extremes(show_clusters=True)
```

Expand All @@ -153,7 +153,7 @@ extracted from the same data and using the same threshold as above, but with a l
</figure>

!!! tip
Declustering value of `#!python r="24H"` was selected as a default value because
Declustering value of `#!python r="24h"` was selected as a default value because
`pyextremes` was developed when working with meteorological phenomena - namely,
storms. Extreme storm surge and waves are generally caused by a storm event which
generally doesn't exceed 12-24 hours and, because of this, the assumption of 24-hour
Expand All @@ -178,7 +178,7 @@ by setting the `extremes_type` argument to `#!python "low"`:
data,
"POT",
threshold=0.5,
r="24H",
r="24h",
extremes_type="low",
)
plot_extremes(
Expand All @@ -187,15 +187,15 @@ by setting the `extremes_type` argument to `#!python "low"`:
extremes_method="POT",
extremes_type="low",
threshold=0.5,
r="24H",
r="24h",
)
```

=== "Using EVA"

```python
model = EVA(data=data)
model.get_extremes("POT", threshold=0.5, r="24H", extremes_type="low")
model.get_extremes("POT", threshold=0.5, r="24h", extremes_type="low")
model.plot_extremes(show_clusters=True)
```

Expand Down
6 changes: 3 additions & 3 deletions docs/user-guide/5-threshold-selection.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ The `plot_parameter_stability` function uses the following parameters:
to decluster exceedances by locating clusters where all exceedances are separated
by distances no more than `r` and then locating maximum or minimum
(depends on `extremes_type`) values within each cluster.
By default `#!python r="24H"` (24 hours).
By default `#!python r="24h"` (24 hours).
- **extremes_type** - extreme value type:
`#!python "high"` for above threshold (default)
and `#!python "low"` for below threshold.
Expand Down Expand Up @@ -172,7 +172,7 @@ The `plot_return_value_stability` function uses the following parameters:
to decluster exceedances by locating clusters where all exceedances are separated
by distances no more than `r` and then locating maximum or minimum
(depends on `extremes_type`) values within each cluster.
By default `#!python r="24H"` (24 hours).
By default `#!python r="24h"` (24 hours).
- **extremes_type** - extreme value type:
`#!python "high"` for above threshold (default)
and `#!python "low"` for below threshold.
Expand Down Expand Up @@ -239,7 +239,7 @@ The `plot_threshold_stability` function uses the following parameters:
to decluster exceedances by locating clusters where all exceedances are separated
by distances no more than `r` and then locating maximum or minimum
(depends on `extremes_type`) values within each cluster.
By default `#!python r="24H"` (24 hours).
By default `#!python r="24h"` (24 hours).
- **extremes_type** - extreme value type:
`#!python "high"` for above threshold (default)
and `#!python "low"` for below threshold.
Expand Down
10 changes: 5 additions & 5 deletions src/pyextremes/eva.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def get_extremes(
extremes_type: typing.Literal["high", "low"] = "high",
*,
threshold: float,
r: typing.Union[pd.Timedelta, typing.Any] = "24H",
r: typing.Union[pd.Timedelta, typing.Any] = "24h",
) -> None:
...

Expand Down Expand Up @@ -522,7 +522,7 @@ def get_extremes(
)
else:
self.__extremes_kwargs["threshold"] = kwargs.get("threshold")
self.__extremes_kwargs["r"] = pd.to_timedelta(kwargs.get("r", "24H"))
self.__extremes_kwargs["r"] = pd.to_timedelta(kwargs.get("r", "24h"))
logger.info("successfully collected extreme value properties")

logger.debug("creating extremes transformer")
Expand Down Expand Up @@ -556,7 +556,7 @@ def set_extremes(
extremes_type: typing.Literal["high", "low"] = "high",
*,
threshold: float,
r: typing.Union[pd.Timedelta, typing.Any] = "24H",
r: typing.Union[pd.Timedelta, typing.Any] = "24h",
) -> None:
...

Expand Down Expand Up @@ -699,7 +699,7 @@ def set_extremes(
raise ValueError("invalid `threshold` value")

# Get `r`
extremes_kwargs["r"] = pd.to_timedelta(kwargs.pop("r", "24H"))
extremes_kwargs["r"] = pd.to_timedelta(kwargs.pop("r", "24h"))
if extremes_kwargs["r"] <= pd.to_timedelta("0D"):
raise ValueError(
"`r` must be a positive timedelta, not %s" % extremes_kwargs["r"]
Expand Down Expand Up @@ -746,7 +746,7 @@ def from_extremes(
extremes_type: typing.Literal["high", "low"] = "high",
*,
threshold: float,
r: typing.Union[pd.Timedelta, typing.Any] = "24H",
r: typing.Union[pd.Timedelta, typing.Any] = "24h",
) -> None:
...

Expand Down
2 changes: 1 addition & 1 deletion src/pyextremes/extremes/extremes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_extremes(
extremes_type: Literal["high", "low"] = "high",
*,
threshold: float,
r: Union[pd.Timedelta, Any] = "24H",
r: Union[pd.Timedelta, Any] = "24h",
) -> pd.Series:
...

Expand Down
2 changes: 1 addition & 1 deletion src/pyextremes/extremes/peaks_over_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_extremes_peaks_over_threshold(
ts: pd.Series,
extremes_type: Literal["high", "low"],
threshold: float,
r: Union[pd.Timedelta, Any] = "24H",
r: Union[pd.Timedelta, Any] = "24h",
) -> pd.Series:
"""
Get extreme events from time series using the Peaks Over Threshold method.
Expand Down
8 changes: 4 additions & 4 deletions src/pyextremes/tuning/threshold_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _calculate_modified_parameters(
def plot_parameter_stability(
ts: pd.Series,
thresholds=None,
r: typing.Union[str, pd.Timedelta] = "24H",
r: typing.Union[str, pd.Timedelta] = "24h",
extremes_type: str = "high",
alpha: typing.Optional[float] = None,
n_samples: int = 100,
Expand Down Expand Up @@ -518,7 +518,7 @@ def plot_return_value_stability(
return_period: float,
return_period_size: typing.Union[str, pd.Timedelta] = "365.2425D",
thresholds=None,
r: typing.Union[str, pd.Timedelta] = "24H",
r: typing.Union[str, pd.Timedelta] = "24h",
extremes_type: str = "high",
distributions: typing.Optional[
typing.List[typing.Union[str, scipy.stats.rv_continuous]]
Expand Down Expand Up @@ -728,7 +728,7 @@ def _input_generator() -> (
def plot_aic_scores(
ts: pd.Series,
thresholds=None,
r: typing.Union[str, pd.Timedelta] = "24H",
r: typing.Union[str, pd.Timedelta] = "24h",
extremes_type: str = "high",
distributions: typing.Optional[
typing.List[typing.Union[str, scipy.stats.rv_continuous]]
Expand Down Expand Up @@ -853,7 +853,7 @@ def plot_threshold_stability(
return_period: float,
return_period_size: typing.Union[str, pd.Timedelta] = "365.2425D",
thresholds=None,
r: typing.Union[str, pd.Timedelta] = "24H",
r: typing.Union[str, pd.Timedelta] = "24h",
extremes_type: str = "high",
distributions: typing.Optional[
typing.List[typing.Union[str, scipy.stats.rv_continuous]]
Expand Down
4 changes: 2 additions & 2 deletions tests/_shared_data/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def main() -> None:
kwargs = {"block_size": "365.2425D", "errors": "ignore"}
else:
if extremes_type == "high":
kwargs = {"threshold": 1.35, "r": "24H"}
kwargs = {"threshold": 1.35, "r": "24h"}
else:
kwargs = {"threshold": -1.55, "r": "24H"}
kwargs = {"threshold": -1.55, "r": "24h"}
extremes = get_extremes(
ts=data, method=method, extremes_type=extremes_type, **kwargs
)
Expand Down
14 changes: 7 additions & 7 deletions tests/extremes/test_peaks_over_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_invalid_arguments(battery_wl):
method="POT",
extremes_type="BAD EXTREMES_TYPE VALUE",
threshold=2,
r="24H",
r="24h",
)

# Test wrong r type
Expand All @@ -30,19 +30,19 @@ def test_extreme_value_extraction(battery_wl, extremes_type, threshold):
method="POT",
extremes_type=extremes_type,
threshold=threshold,
r="24H",
r="24h",
)
if extremes_type == "high":
assert np.isclose(extremes.max(), battery_wl.max())
assert len(extremes) == 117
elif extremes_type == "low":
assert np.isclose(extremes.min(), battery_wl.min())
assert len(extremes) == 104
assert np.all(np.diff(extremes.index) > pd.to_timedelta("24H").to_numpy())
assert np.all(np.diff(extremes.index) > pd.to_timedelta("24h").to_numpy())


def test_single_cluster():
index = pd.date_range(start="2021/01/01", end="2021/01/02", freq="1H")
index = pd.date_range(start="2021/01/01", end="2021/01/02", freq="1h")
data = pd.Series(data=np.random.random(size=len(index)), index=index)

# Tesh high extremes
Expand All @@ -51,7 +51,7 @@ def test_single_cluster():
method="POT",
extremes_type="high",
threshold=data.min() - 1,
r="24H",
r="24h",
)
assert len(extremes) == 1
assert np.isclose(extremes.values[0], data.max())
Expand All @@ -62,7 +62,7 @@ def test_single_cluster():
method="POT",
extremes_type="low",
threshold=data.max() + 1,
r="24H",
r="24h",
)
assert len(extremes) == 1
assert np.isclose(extremes.values[0], data.min())
Expand All @@ -86,7 +86,7 @@ def test_threshold_producing_empty_series(battery_wl: pd.Series, extremes_type:
threshold=battery_wl.max() + 1
if extremes_type == "high"
else battery_wl.min() - 1,
r="24H",
r="24h",
)
assert isinstance(extremes, pd.Series)
assert len(extremes) == 0
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/test_eva.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def eva_model_pot(battery_wl_preprocessed) -> EVA:
method="POT",
extremes_type="high",
threshold=1.35,
r="24H",
r="24h",
)
return eva_model

Expand Down Expand Up @@ -69,7 +69,7 @@ def eva_model_pot_mle(battery_wl_preprocessed) -> EVA:
method="POT",
extremes_type="high",
threshold=1.35,
r="24H",
r="24h",
)
eva_model.fit_model("MLE")
return eva_model
Expand Down Expand Up @@ -163,7 +163,7 @@ def test_init_attributes(self, eva_model):
"method": "POT",
"extremes_type": "high",
"threshold": 1.35,
"r": "24H",
"r": "24h",
},
{
"method": "POT",
Expand All @@ -188,7 +188,7 @@ def test_get_extremes(self, eva_model, input_params):
else:
assert len(eva_model.extremes_kwargs) == 2
assert eva_model.extremes_kwargs["threshold"] == 1.35
assert eva_model.extremes_kwargs["r"] == pd.to_timedelta("24H")
assert eva_model.extremes_kwargs["r"] == pd.to_timedelta("24h")
with pytest.raises(AttributeError, match=r"model must first"):
eva_model.model

Expand Down Expand Up @@ -384,7 +384,7 @@ def test_from_extremes(self):
extremes_type="high",
)
assert np.isclose(eva_model.extremes_kwargs["threshold"], 0, rtol=0, atol=1e-6)
assert eva_model.extremes_kwargs["r"] == pd.to_timedelta("24H")
assert eva_model.extremes_kwargs["r"] == pd.to_timedelta("24h")
eva_model = EVA.from_extremes(
extremes=pd.Series(
data=np.arange(100),
Expand All @@ -395,7 +395,7 @@ def test_from_extremes(self):
extremes_type="low",
)
assert np.isclose(eva_model.extremes_kwargs["threshold"], 99, rtol=0, atol=1e-6)
assert eva_model.extremes_kwargs["r"] == pd.to_timedelta("24H")
assert eva_model.extremes_kwargs["r"] == pd.to_timedelta("24h")

@pytest.mark.parametrize(
"extremes_params",
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_distribution(self, distribution_input, theta, kwargs, scipy_parameters)
scipy_distribution = getattr(scipy.stats, distribution_name)
distribution = Distribution(
extremes=pd.Series(
index=pd.date_range(start="2000-01-01", periods=100, freq="1H"),
index=pd.date_range(start="2000-01-01", periods=100, freq="1h"),
data=scipy_distribution.rvs(*scipy_parameters, size=100),
),
distribution=distribution_input,
Expand Down
4 changes: 2 additions & 2 deletions tests/models/test_model_emcee.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def extremes() -> pd.Series:
np.random.seed(0)
return pd.Series(
index=pd.date_range(start="2000-01-01", periods=1000, freq="1H"),
index=pd.date_range(start="2000-01-01", periods=1000, freq="1h"),
data=scipy.stats.genextreme.rvs(c=0, loc=10, scale=2, size=1000),
)

Expand Down Expand Up @@ -163,7 +163,7 @@ def test_emcee_distributions(

np.random.seed(0)
extremes = pd.Series(
index=pd.date_range(start="2000-01-01", periods=100, freq="1H"),
index=pd.date_range(start="2000-01-01", periods=100, freq="1h"),
data=scipy_distribution.rvs(*scipy_parameters, size=100),
)
model = get_model(
Expand Down
Loading

0 comments on commit c8ed1f0

Please sign in to comment.