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

Refactor Python Model #512

Closed
evetion opened this issue Aug 17, 2023 · 2 comments · Fixed by #580 or #731
Closed

Refactor Python Model #512

evetion opened this issue Aug 17, 2023 · 2 comments · Fixed by #580 or #731
Labels
python Relates to one of the Ribasim python packages tech-debt Improvements related to technical debt

Comments

@evetion
Copy link
Member

evetion commented Aug 17, 2023

There's some technical debt in there, as it mixes all nodetypes and the required config options.

@visr visr added python Relates to one of the Ribasim python packages tech-debt Improvements related to technical debt labels Aug 17, 2023
@evetion evetion moved this from To do to Sprint backlog in Ribasim Aug 17, 2023
@evetion
Copy link
Member Author

evetion commented Aug 17, 2023

This was already started once by @SouthEndMusic. What also came up is to generate a JSONSchema for our TOML config file and use it like we use our nodes.

@gijsber gijsber moved this from Sprint backlog to To do in Ribasim Aug 29, 2023
@evetion evetion moved this from To do to 🏗 In progress in Ribasim Sep 4, 2023
@evetion evetion mentioned this issue Sep 7, 2023
4 tasks
@visr visr closed this as completed in #580 Sep 14, 2023
@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in Ribasim Sep 14, 2023
visr pushed a commit that referenced this issue Sep 14, 2023
Fixes #512 

- [x] Add JSON Schema of Config and generate Python code
- [x] Autogenerate Pandera Schemas
- [x] Update docs to include the above
- [x] ~Refactor Python model into split Config & Root submodels~
evetion added a commit that referenced this issue Nov 14, 2023
Fixes #512
Groundwork for #630 & #252

The code has become much simpler in some places (e.g. write_toml).

```python
from ribasim import Model

m = Model(filepath="generated_testmodels/basic/ribasim.toml")
m.database.node.df # Node table
m.basin.static.df # BasinStatc table
m.write("test")
```

### Some notes:
- The config.py file cannot be autogenerated anymore. The schemas still
can, but I disabled it for now to be sure (some imports error).

### Changes:
I created new (parent) classes:
- BaseModel, from Pydantic, with our own config
- FileModel, like hydrolib-core (but now Pydantic v2), which can take a
single filepath for initilization. Models who inherit require defining
_load and _save, dealing with that filepath.
- NodeModel, a class for nodes (where `add` will be).
- TableModel, a class to read/write individual tables from/to
gpkg/arrow.
- SpatialTableModel, inherits TableModel, but reads/writes spatial
stuff.

I changed:
- the Model class to be a carbon copy of Config (which has been
deleted), so it mirrors the toml.
- in turn this created a `database` NodeModel class (reflecting the
field in the toml), with only Node and Edge underneath.
- the NodeModel classes Basin from their node_type version to the one in
Config, and set the type of the underlying table with a TypeVar like so:
```python
class Terminal(NodeModel):
    static: TableModel[TerminalStaticSchema]
```

### Yet to do:
- [x] Update tests
- [x] Fix sort! rules
- [x] Delete node_types folder
- [x] Link schemas to their Pydantic class
(TableModel[TerminalStaticSchema] => TerminalStatic)

---------

Co-authored-by: Martijn Visser <[email protected]>
Co-authored-by: Hofer-Julian <[email protected]>
Co-authored-by: Hofer-Julian <[email protected]>
visr added a commit that referenced this issue Nov 14, 2023
Fixes #512
Groundwork for #630 & #252

The code has become much simpler in some places (e.g. write_toml).

```python
from ribasim import Model

m = Model(filepath="generated_testmodels/basic/ribasim.toml")
m.database.node.df # Node table
m.basin.static.df # BasinStatc table
m.write("test")
```

### Some notes:
- The config.py file cannot be autogenerated anymore. The schemas still
can, but I disabled it for now to be sure (some imports error).

### Changes:
I created new (parent) classes:
- BaseModel, from Pydantic, with our own config
- FileModel, like hydrolib-core (but now Pydantic v2), which can take a
single filepath for initilization. Models who inherit require defining
_load and _save, dealing with that filepath.
- NodeModel, a class for nodes (where `add` will be).
- TableModel, a class to read/write individual tables from/to
gpkg/arrow.
- SpatialTableModel, inherits TableModel, but reads/writes spatial
stuff.

I changed:
- the Model class to be a carbon copy of Config (which has been
deleted), so it mirrors the toml.
- in turn this created a `database` NodeModel class (reflecting the
field in the toml), with only Node and Edge underneath.
- the NodeModel classes Basin from their node_type version to the one in
Config, and set the type of the underlying table with a TypeVar like so:
```python
class Terminal(NodeModel):
    static: TableModel[TerminalStaticSchema]
```

### Yet to do:
- [x] Update tests
- [x] Fix sort! rules
- [x] Delete node_types folder
- [x] Link schemas to their Pydantic class
(TableModel[TerminalStaticSchema] => TerminalStatic)

---------

Co-authored-by: Martijn Visser <[email protected]>
Co-authored-by: Hofer-Julian <[email protected]>
Co-authored-by: Hofer-Julian <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Relates to one of the Ribasim python packages tech-debt Improvements related to technical debt
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants