Skip to content

Commit

Permalink
Fix runtime error in ChangeModel (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulcollenteur committed Jan 16, 2023
1 parent 8039221 commit afcfd36
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
44 changes: 22 additions & 22 deletions doc/examples/16_changing_responses.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "98258ce4",
"id": "50090af9",
"metadata": {},
"source": [
"## Changing response functions\n",
Expand All @@ -15,7 +15,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "626e5215",
"id": "4fd7c43e",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -30,7 +30,7 @@
},
{
"cell_type": "markdown",
"id": "532700b3",
"id": "f64dd3fa",
"metadata": {},
"source": [
"## 1. Load the data"
Expand All @@ -39,7 +39,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "2ea82904",
"id": "fa1a8b85",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -59,7 +59,7 @@
},
{
"cell_type": "markdown",
"id": "cbd0cac5",
"id": "167417e7",
"metadata": {},
"source": [
"## 2. The weighting factor\n",
Expand All @@ -70,7 +70,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "4ec1409f",
"id": "54e244ad",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -92,7 +92,7 @@
},
{
"cell_type": "markdown",
"id": "835063ec",
"id": "b773618e",
"metadata": {},
"source": [
"## 3. Make a model\n",
Expand All @@ -105,15 +105,15 @@
{
"cell_type": "code",
"execution_count": null,
"id": "ac597dfd",
"id": "92783392",
"metadata": {},
"outputs": [],
"source": [
"# Normal Model\n",
"ml = ps.Model(head, name=\"linear\")\n",
"\n",
"sm = ps.StressModel(river, ps.Exponential, name=\"test\")\n",
"step = ps.StepModel(\"2012-01-01\", rfunc=ps.One, name=\"step\")\n",
"sm = ps.StressModel(river, ps.Exponential(), name=\"test\")\n",
"step = ps.StepModel(\"2012-01-01\", rfunc=ps.One(), name=\"step\")\n",
"\n",
"ml.add_stressmodel([sm, step])\n",
"ml.solve(report=False, tmin=\"2004\", tmax=\"2018\", noise=True)\n",
Expand All @@ -123,7 +123,7 @@
"ml2 = ps.Model(head, name=\"linear\")\n",
"\n",
"cm = ps.ChangeModel(\n",
" river, ps.Exponential, ps.Exponential, name=\"test\", tchange=\"2012-01-01\"\n",
" river, ps.Exponential(), ps.Exponential(), name=\"test\", tchange=\"2012-01-01\"\n",
")\n",
"\n",
"ml2.add_stressmodel([cm, step])\n",
Expand All @@ -133,7 +133,7 @@
},
{
"cell_type": "markdown",
"id": "09ddea87",
"id": "d319b7ac",
"metadata": {},
"source": [
"The second model shows a better fit, but also the step trend changed. "
Expand All @@ -142,7 +142,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "593a9e93",
"id": "80d08e59",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -153,7 +153,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "2c7d555a",
"id": "bbade6d3",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -162,7 +162,7 @@
},
{
"cell_type": "markdown",
"id": "c0ba0158",
"id": "89145651",
"metadata": {},
"source": [
"## 4. Compare the response functions\n",
Expand All @@ -172,7 +172,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "7e6e212e",
"id": "171d6a10",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -188,7 +188,7 @@
},
{
"cell_type": "markdown",
"id": "1dbce7c7",
"id": "67041afc",
"metadata": {},
"source": [
"## 5. Another way\n",
Expand All @@ -198,7 +198,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3587cda5",
"id": "7cb3e1f4",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -221,7 +221,7 @@
},
{
"cell_type": "markdown",
"id": "9296a68c",
"id": "35ed498d",
"metadata": {},
"source": [
"## How do the results compare?"
Expand All @@ -230,7 +230,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "3eef4b58",
"id": "e3d0e83b",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -249,7 +249,7 @@
},
{
"cell_type": "markdown",
"id": "8ab2efc2",
"id": "cbb77044",
"metadata": {},
"source": [
"## References\n",
Expand All @@ -274,7 +274,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
24 changes: 8 additions & 16 deletions pastas/stressmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,24 +1582,15 @@ def __init__(
tmin=stress.series.index.min(),
tmax=stress.series.index.max(),
)
if inspect.isclass(rfunc1):
DeprecationWarning(
"Response functions should be added to a "
"stress-model as an instance, and not as a "
"class. This will raise an error from "
"Pastas version 0.23."
if inspect.isclass(rfunc1) or inspect.isclass(rfunc2):
raise DeprecationWarning(
"Response functions should be provided as an instance (e.g., ps.One()),"
" not as a class (e.g., ps.One). Please provide an instance."
)
rfunc1 = rfunc1()

rfunc1._set_init_parameter_settings(up=up, cutoff=cutoff)
self.rfunc1 = rfunc1
if inspect.isclass(rfunc2):
DeprecationWarning(
"Response functions should be added to a "
"stress-model as an instance, and not as a "
"class. This will raise an error from "
"Pastas version 0.23."
)
rfunc2 = rfunc2()

rfunc2._set_init_parameter_settings(up=up, cutoff=cutoff)
self.rfunc2 = rfunc2
self.tchange = Timestamp(tchange)
Expand Down Expand Up @@ -1654,10 +1645,11 @@ def simulate(
stress = self.stress[0].series
npoints = stress.index.size
t = np.linspace(0, 1, npoints)
beta = p[-2] * npoints
beta = p[-2]

sigma = stress.index.get_loc(Timestamp.fromordinal(int(p[-1]))) / npoints
omega = 1 / (np.exp(beta * (t - sigma)) + 1)

h1 = Series(
data=fftconvolve(stress, rfunc1, "full")[:npoints],
index=stress.index,
Expand Down

0 comments on commit afcfd36

Please sign in to comment.