Skip to content

Commit

Permalink
Add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
SouthEndMusic committed Dec 22, 2023
1 parent ff040aa commit 6049ae3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/ribasim/ribasim/geometry/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Edge(SpatialTableModel[EdgeSchema]):
def translate_spacially(
self, offset_spacial: tuple[float, float], inplace: bool = True
) -> "Edge":
"""Add the same spacial offset to all edges."""
if inplace:
edge = self
else:
Expand All @@ -61,6 +62,7 @@ def translate_spacially(
def offset_allocation_network_ids(
self, offset_allocation_network_id: int, inplace: bool = True
) -> "Edge":
"""Add the same offset to all node IDs."""
if inplace:
edge = self
else:
Expand Down
2 changes: 2 additions & 0 deletions python/ribasim/ribasim/geometry/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def node_ids_and_types(*nodes):
def translate_spacially(
self, offset_spacial: tuple[float, float], inplace: bool = True
) -> "Node":
"""Add the same spacial offset to all nodes."""
if inplace:
node = self
else:
Expand All @@ -81,6 +82,7 @@ def translate_spacially(
def offset_allocation_network_ids(
self, offset_allocation_network_id: int, inplace: bool = True
) -> "Node":
"""Add the same offset to all node IDs."""
if inplace:
node = self
else:
Expand Down
5 changes: 5 additions & 0 deletions python/ribasim/ribasim/input_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def node_ids(self) -> set[int]:
return node_ids

def offset_node_ids(self, offset_node_id: int) -> "TableModel[TableT]":
"""Add the same offset to all node IDs."""
copy = deepcopy(self)
df = copy.df
if isinstance(df, (pd.DataFrame, gpd.GeoDataFrame)):
Expand All @@ -234,6 +235,7 @@ def offset_node_ids(self, offset_node_id: int) -> "TableModel[TableT]":
def merge_table(
self, table_added: "TableModel[TableT]", inplace: bool = True
) -> "TableModel[TableT]":
"""Merge an added table of the same type into this table."""
assert type(self) == type(
table_added
), "Can only merge tables of the same type."
Expand Down Expand Up @@ -472,6 +474,7 @@ def node_ids_and_types(self) -> tuple[list[int], list[str]]:
return list(ids), len(ids) * [self.get_input_type()]

def offset_node_ids(self, offset_node_id: int) -> "NodeModel":
"""Add the same offset to all node IDs in all underlying tables."""
node_copy = deepcopy(self)
for field in node_copy.fields():
attr = getattr(node_copy, field)
Expand All @@ -485,6 +488,7 @@ def offset_node_ids(self, offset_node_id: int) -> "NodeModel":
return node_copy

def merge_node(self, node_added: "NodeModel", inplace: bool = True) -> "NodeModel":
"""Merge an added node of the same type into this node."""
assert type(self) == type(node_added), "Can only merge nodes of the same type."

if inplace:
Expand All @@ -505,6 +509,7 @@ def merge_node(self, node_added: "NodeModel", inplace: bool = True) -> "NodeMode
return node

def delete_by_ids(self, node_ids: list[int], inplace: bool = True) -> "NodeModel":
"""Delete all rows of the underlying tables whose node ID is in the given list."""
if inplace:
node = self
else:
Expand Down
8 changes: 8 additions & 0 deletions python/ribasim/ribasim/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def n_nodes(self):
def translate_spacially(
self, offset_spacial: tuple[float, float], inplace: bool = True
) -> "Network":
"""Add the same spacial offset to all nodes and edges."""
if inplace:
network = self

Check warning on line 69 in python/ribasim/ribasim/model.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/model.py#L69

Added line #L69 was not covered by tests
else:
Expand Down Expand Up @@ -92,6 +93,7 @@ def add_edges(
edge_type: list[str],
inplace: bool = True,
) -> "Network":
"""Add new edges to the network of the given type. Assumes no source edges are added."""
if inplace:
network = self
else:
Expand Down Expand Up @@ -463,6 +465,12 @@ def merge_model(
offset_spacial: tuple[float, float] = (0.0, 0.0),
inplace: bool = True,
):
"""
Merge copies of the nodes and edges of an added model into this model.
The added model is not modified, but the following modificadions are made to the added data:
- Node IDs are shifted by at least the maximum node ID of this model
- Allocation network IDs are shifted by at least the maximum allocation network ID of this model.
"""
if inplace:
model = self
else:
Expand Down
1 change: 1 addition & 0 deletions python/ribasim/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def test_model_merging(basic, subnetwork, tmp_path):
model.merge_model(model_added)
assert (model.network.node.df.index == range(1, 44)).all()
assert model.max_allocation_network_id() == 2
# Added model should not change
for node_type, node_added in model_added.nodes().items():
node_subnetwork = getattr(subnetwork, node_type)
for table_added, table_subnetwork in zip(
Expand Down

0 comments on commit 6049ae3

Please sign in to comment.