From afcfd367310eb1c0ce16bc00b1b868457643c1f7 Mon Sep 17 00:00:00 2001 From: Raoul Collenteur Date: Mon, 16 Jan 2023 11:09:29 +0100 Subject: [PATCH] Fix runtime error in ChangeModel (#464) --- doc/examples/16_changing_responses.ipynb | 44 ++++++++++++------------ pastas/stressmodels.py | 24 +++++-------- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/doc/examples/16_changing_responses.ipynb b/doc/examples/16_changing_responses.ipynb index f8382153..5a1e42ff 100644 --- a/doc/examples/16_changing_responses.ipynb +++ b/doc/examples/16_changing_responses.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "98258ce4", + "id": "50090af9", "metadata": {}, "source": [ "## Changing response functions\n", @@ -15,7 +15,7 @@ { "cell_type": "code", "execution_count": null, - "id": "626e5215", + "id": "4fd7c43e", "metadata": {}, "outputs": [], "source": [ @@ -30,7 +30,7 @@ }, { "cell_type": "markdown", - "id": "532700b3", + "id": "f64dd3fa", "metadata": {}, "source": [ "## 1. Load the data" @@ -39,7 +39,7 @@ { "cell_type": "code", "execution_count": null, - "id": "2ea82904", + "id": "fa1a8b85", "metadata": {}, "outputs": [], "source": [ @@ -59,7 +59,7 @@ }, { "cell_type": "markdown", - "id": "cbd0cac5", + "id": "167417e7", "metadata": {}, "source": [ "## 2. The weighting factor\n", @@ -70,7 +70,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4ec1409f", + "id": "54e244ad", "metadata": {}, "outputs": [], "source": [ @@ -92,7 +92,7 @@ }, { "cell_type": "markdown", - "id": "835063ec", + "id": "b773618e", "metadata": {}, "source": [ "## 3. Make a model\n", @@ -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", @@ -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", @@ -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. " @@ -142,7 +142,7 @@ { "cell_type": "code", "execution_count": null, - "id": "593a9e93", + "id": "80d08e59", "metadata": {}, "outputs": [], "source": [ @@ -153,7 +153,7 @@ { "cell_type": "code", "execution_count": null, - "id": "2c7d555a", + "id": "bbade6d3", "metadata": {}, "outputs": [], "source": [ @@ -162,7 +162,7 @@ }, { "cell_type": "markdown", - "id": "c0ba0158", + "id": "89145651", "metadata": {}, "source": [ "## 4. Compare the response functions\n", @@ -172,7 +172,7 @@ { "cell_type": "code", "execution_count": null, - "id": "7e6e212e", + "id": "171d6a10", "metadata": {}, "outputs": [], "source": [ @@ -188,7 +188,7 @@ }, { "cell_type": "markdown", - "id": "1dbce7c7", + "id": "67041afc", "metadata": {}, "source": [ "## 5. Another way\n", @@ -198,7 +198,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3587cda5", + "id": "7cb3e1f4", "metadata": {}, "outputs": [], "source": [ @@ -221,7 +221,7 @@ }, { "cell_type": "markdown", - "id": "9296a68c", + "id": "35ed498d", "metadata": {}, "source": [ "## How do the results compare?" @@ -230,7 +230,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3eef4b58", + "id": "e3d0e83b", "metadata": {}, "outputs": [], "source": [ @@ -249,7 +249,7 @@ }, { "cell_type": "markdown", - "id": "8ab2efc2", + "id": "cbb77044", "metadata": {}, "source": [ "## References\n", @@ -274,7 +274,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.10.5" } }, "nbformat": 4, diff --git a/pastas/stressmodels.py b/pastas/stressmodels.py index bba0a011..392151ee 100644 --- a/pastas/stressmodels.py +++ b/pastas/stressmodels.py @@ -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) @@ -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,