Skip to content

Commit

Permalink
Improve Node representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian committed Feb 13, 2024
1 parent 57f3ca0 commit d4bcad6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
18 changes: 1 addition & 17 deletions python/ribasim/playground.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
# %%
from pydantic import BaseModel


class A(BaseModel):
x: float
y: float


class B(BaseModel):
a: A


b = B(a={"x": 1.2, "y": 1.4})

print(b.model_dump())

# %%
import geopandas
import pandas as pd
from pandas import DataFrame
from ribasim.add import Basins
Expand Down
22 changes: 8 additions & 14 deletions python/ribasim/ribasim/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pandas as pd
from pandas import DataFrame, Timestamp
from pydantic import ConfigDict, Field
from shapely.geometry import Point

Check warning on line 8 in python/ribasim/ribasim/add.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/add.py#L5-L8

Added lines #L5 - L8 were not covered by tests

from ribasim.config import Basin
from ribasim.geometry.node import NodeSchema
Expand Down Expand Up @@ -37,7 +38,7 @@


class _BaseModel(BaseModel):
model_config = ConfigDict(extra="forbid")
model_config = ConfigDict(extra="forbid", validate_assignment=True)

Check warning on line 41 in python/ribasim/ribasim/add.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/add.py#L40-L41

Added lines #L40 - L41 were not covered by tests


class Node(_BaseModel):
Expand Down Expand Up @@ -68,27 +69,20 @@ class Basins(_BaseModel):
default_factory=TableModel[BasinSubgridSchema],
json_schema_extra={"sort_keys": ["subgrid_id", "basin_level"]},
)
node: SpatialTableModel[NodeSchema] = Field(
default_factory=SpatialTableModel[NodeSchema],
)
node: list[Node] = []

Check warning on line 72 in python/ribasim/ribasim/add.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/add.py#L72

Added line #L72 was not covered by tests

def __init__(self, basins: list[Basin]):
super().__init__()
for basin in basins:
id = basin.node["id"]

Check warning on line 77 in python/ribasim/ribasim/add.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/add.py#L74-L77

Added lines #L74 - L77 were not covered by tests

basin_dict = basin.model_dump()
node = basin_dict.pop("node")
self.node.append(Node(**node))
for basin_key, basin_value in basin_dict.items():
if basin_key == "node":
node_dict = {
node_key: [value] for node_key, value in basin_value.items()
}
table_to_append = pd.DataFrame(node_dict)
else:
table_to_append = basin_value.assign(node_id=id)

original_table = getattr(self, basin_key)
original_table.df = pd.concat([original_table.df, table_to_append])
member = getattr(self, basin_key)
table_to_append = basin_value.assign(node_id=id)
member.df = pd.concat([member.df, table_to_append])

Check warning on line 85 in python/ribasim/ribasim/add.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/add.py#L79-L85

Added lines #L79 - L85 were not covered by tests


class Model(_BaseModel):
Expand Down

0 comments on commit d4bcad6

Please sign in to comment.