Skip to content

Commit

Permalink
Bring back io tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Apr 16, 2024
1 parent 9c174eb commit 2dcefd3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
27 changes: 5 additions & 22 deletions docs/python/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@
" discrete_control.Variable(\n",
" compound_variable_id=1,\n",
" listen_node_id=1,\n",
" listen_node_type=[\"Basin\", \"Basin\"],\n",
" variable=[\"level\", \"level\"],\n",
" listen_node_type=[\"Basin\"],\n",
" variable=[\"level\"],\n",
" ),\n",
" discrete_control.Condition(\n",
" compound_variable_id=1,\n",
Expand Down Expand Up @@ -707,8 +707,6 @@
"metadata": {},
"outputs": [],
"source": [
"from matplotlib.dates import date2num\n",
"\n",
"df_basin = pd.read_feather(datadir / \"level_range/results/basin.arrow\")\n",
"df_basin_wide = df_basin.pivot_table(\n",
" index=\"time\", columns=\"node_id\", values=[\"storage\", \"level\"]\n",
Expand All @@ -727,23 +725,7 @@
" color=\"k\",\n",
")\n",
"\n",
"df_control = pd.read_feather(datadir / \"level_range/control.arrow\")\n",
"\n",
"y_min, y_max = ax.get_ybound()\n",
"ax.fill_between(\n",
" df_control.time[:2].to_numpy(), 2 * [y_min], 2 * [y_max], alpha=0.2, color=\"C0\"\n",
")\n",
"ax.fill_between(\n",
" df_control.time[2:4].to_numpy(), 2 * [y_min], 2 * [y_max], alpha=0.2, color=\"C0\"\n",
")\n",
"\n",
"ax.set_xticks(\n",
" date2num(df_control.time).tolist(),\n",
" df_control.control_state.tolist(),\n",
" rotation=50,\n",
")\n",
"\n",
"ax.set_yticks(greater_than, [\"min\", \"setpoint\", \"max\"])\n",
"ax.set_yticks(greater_than, [\"min\", \"max\"])\n",
"ax.set_ylabel(\"level\")\n",
"plt.show()"
]
Expand All @@ -752,7 +734,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The highlighted regions show where a pump is active.\n"
"We see that in Januari the level of the basin is too high and thus water is pumped out until the maximum level of the desried range is reached. Then until May water flows out of the basin freely trough the tabulated rating curve until the minimum leavel is reached. From \n",
"this point until the start of July water is pumped into the basin in short bursts to stay within the desired range. at the Start of July rain starts falling on the basin, which causes the basin level to rise until the maximum level. From this point onward water is pumped out of the basin in short bursts to stay within the desired range."
]
},
{
Expand Down
5 changes: 5 additions & 0 deletions python/ribasim/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def discrete_control_of_pid_control() -> ribasim.Model:
return ribasim_testmodels.discrete_control_of_pid_control_model()


@pytest.fixture()
def level_range() -> ribasim.Model:
return ribasim_testmodels.level_range_model()


@pytest.fixture()
def trivial() -> ribasim.Model:
return ribasim_testmodels.trivial_model()
35 changes: 35 additions & 0 deletions python/ribasim/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,41 @@ def test_extra_columns():
terminal.Static(meta_id=[-1, -2, -3], extra=[-1, -2, -3])


def test_sort(level_range, tmp_path):
model = level_range
table = model.discrete_control.condition
edge = model.edge

# apply a wrong sort, then call the sort method to restore order
table.df.sort_values("greater_than", ascending=False, inplace=True)
assert table.df.iloc[0]["greater_than"] == 15.0
assert table._sort_keys == [
"node_id",
"compound_variable_id",
"greater_than",
]
table.sort()
assert table.df.iloc[0]["greater_than"] == 5.0

# The edge table is not sorted
assert edge.df.iloc[1]["from_node_type"] == "Pump"
assert edge.df.iloc[1]["from_node_id"] == 3

# re-apply wrong sort, then check if it gets sorted on write
table.df.sort_values("greater_than", ascending=False, inplace=True)
model.write(tmp_path / "basic/ribasim.toml")
# write sorts the model in place
assert table.df.iloc[0]["greater_than"] == 5.0
model_loaded = ribasim.Model.read(filepath=tmp_path / "basic/ribasim.toml")
table_loaded = model_loaded.discrete_control.condition
edge_loaded = model_loaded.edge
assert table_loaded.df.iloc[0]["greater_than"] == 5.0
assert edge.df.iloc[1]["from_node_type"] == "Pump"
assert edge.df.iloc[1]["from_node_id"] == 3
__assert_equal(table.df, table_loaded.df)
__assert_equal(edge.df, edge_loaded.df)


def test_roundtrip(trivial, tmp_path):
model1 = trivial
# set custom Edge index
Expand Down

0 comments on commit 2dcefd3

Please sign in to comment.