Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ribasim Python: Add-API #1110

Merged
merged 58 commits into from
Mar 12, 2024
Merged

Ribasim Python: Add-API #1110

merged 58 commits into from
Mar 12, 2024

Conversation

Hofer-Julian
Copy link
Contributor

@Hofer-Julian Hofer-Julian commented Feb 12, 2024

Fixes #760
Fixes #252

Follow ups:

  • More validation
  • Avoid blocking of database by qgis
  • Plotting of control edges
  • Reading of model

@Hofer-Julian Hofer-Julian added the python Relates to one of the Ribasim python packages label Feb 12, 2024
visr added a commit that referenced this pull request Feb 12, 2024
Needed for #1110.

Before node IDs were globally unique. If you had the ID, you could look
up the type in the Node table. When designing #1110 we came to the
conclusion that this was not the right choice. It requires you to be
aware of the IDs that are used throughout the model, whereas with this
you only need to make sure that `Pump #5` doesn't already exist.

Most of the updates in this PR were to correct the tests. Some of the
code and error messages became easier to read. If we talk about a node
then we always know what the type is, there is no need to look it up
first.

Most tables stay the same, e.g. if you have a `Terminal / static` table
with a `node_id` Int column, you know that this refers to a Terminal
NodeID. Only when connecting to other nodes do we need to specify the
type next to the ID. So the `Edge` table now gets `from_node_type` next
to `from_node_id`, the `PidControl / static` gets `listen_node_type`
next to `listen_node_id`, etc. These extra columns are currently
automatically filled in by Ribasim-Python on model save, hence they
don't require changing the test models.

In terms of implementation, this basically adds the `type` field to
`NodeID` and fixes the resulting errors.

```julia
struct NodeID
    type::NodeType.T
    value::Int
end
```

It does not yet test if models with the same node IDs (`Pump #1` and
`Basin #1`) work, but this is hard to do right now with Ribasim Python,
so best left for a later moment.

---------

Co-authored-by: Hofer-Julian <[email protected]>
@Hofer-Julian Hofer-Julian force-pushed the add-api branch 3 times, most recently from b33839b to 5bc0a91 Compare February 26, 2024 12:08
@Hofer-Julian Hofer-Julian self-assigned this Feb 26, 2024
@Hofer-Julian Hofer-Julian force-pushed the add-api branch 5 times, most recently from 05a171b to e4b2a09 Compare March 1, 2024 16:12
Validate during initialization

Improve Node representation

Adapt type hint

Minimal version of `Model`

Add existing methods to `Model`

Continue

Fix remaining problems

Adapt codegen

Add launch config for playground

Generate schemas

Remove `Network` class

Stop avoiding database locking

Allow incrementally adding nodes

Move to add api only

Add `EdgeData`

Use explicit `Node` and `Point` class as input

Adapt the Edges class

Fix `Edge.add`

Share code between Basin and Basins

Avoid pydantic warnings

Add plotting functionality

Add more node types

Migrate tabulatedratingcurve

Update remaining nodes

Simplify Edge API

Port first part of examples

Add ribasim_version

Let tables be appended and continue on example

Migrate first example

Rename

Update examples

Move to new API

Add remaining nodes

Adapt yet another example

Rename `add` to `model` and port another example

Pre-commit
@visr visr added the breaking A change that breaks existing models label Mar 2, 2024
@visr
Copy link
Member

visr commented Mar 4, 2024

For updating ribasim_testmodels, I'm not including the crs="EPSG:28992" anymore. It is not important and I don't think it is supported yet anyway. Also I'm leaving out the # Setup X comments, they are quite obvious from the code, especially with this PR. I decided to first create all nodes and then all edges rather than having that interwoven. Also I'm leaving out the 0 row 0 column terminal.Static tables for now, see #1209.

visr added a commit that referenced this pull request Mar 8, 2024
)

@JvanHouwelingen noticed that we don't create a `node_id` column instead
of `fid`. To be able to edit tables in QGIS we always need `fid`, but
its value no longer matters, so leaving it to autogenerate is normally
best.

This removes `explode_and_connect` to automatically fill in part of the
Edge attributes when creating a new geometry. It was no longer correct
due to the schema updates, so better to remove it for now, and accept
that users have to both draw the geometry and fill in the correct
attributes. An issue with the desired behavior is already written in
#352. In short, `derive_connectivity` would still be nice to have, but
not `explode`.

- add `node_id` to the Node schema
- use `node_id` in the Node label
- add `from_node_type` and `to_node_type` to the Edge schema
- keep showing fid in the Edge label to be able to associate edge_id in
timeseries widget
- use a dropdown selector for the Node and Edge editor widget, see
screenshot below
- update schemas with `listen_node_type` as in #1110


![image](https://github.com/Deltares/Ribasim/assets/4471859/aae55ec1-0612-408f-864b-e1158af8fe28)

Edit: with the updates from Huite this fixes #352

---------

Co-authored-by: Huite Bootsma <[email protected]>
Hofer-Julian pushed a commit that referenced this pull request Mar 11, 2024
)

@JvanHouwelingen noticed that we don't create a `node_id` column instead
of `fid`. To be able to edit tables in QGIS we always need `fid`, but
its value no longer matters, so leaving it to autogenerate is normally
best.

This removes `explode_and_connect` to automatically fill in part of the
Edge attributes when creating a new geometry. It was no longer correct
due to the schema updates, so better to remove it for now, and accept
that users have to both draw the geometry and fill in the correct
attributes. An issue with the desired behavior is already written in
not `explode`.

- add `node_id` to the Node schema
- use `node_id` in the Node label
- add `from_node_type` and `to_node_type` to the Edge schema
- keep showing fid in the Edge label to be able to associate edge_id in
timeseries widget
- use a dropdown selector for the Node and Edge editor widget, see
screenshot below
- update schemas with `listen_node_type` as in #1110

![image](https://github.com/Deltares/Ribasim/assets/4471859/aae55ec1-0612-408f-864b-e1158af8fe28)

Edit: with the updates from Huite this fixes #352

---------

Co-authored-by: Huite Bootsma <[email protected]>
@Hofer-Julian Hofer-Julian marked this pull request as ready for review March 12, 2024 12:34
@visr visr merged commit 006ff90 into main Mar 12, 2024
23 checks passed
@visr visr deleted the add-api branch March 12, 2024 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A change that breaks existing models python Relates to one of the Ribasim python packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement object based "add" API in Python Ribasim Python: stop exposing Ribasim.Node and node IDs to users
3 participants