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

Always write fid index of Node and Edge tables #995

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python/ribasim/ribasim/input_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,9 @@ def _write_table(self, path: FilePath) -> None:

gdf = gpd.GeoDataFrame(data=self.df)
gdf = gdf.set_geometry("geometry")
gdf.index.name = "fid"

gdf.to_file(path, layer=self.tablename(), driver="GPKG")
gdf.to_file(path, layer=self.tablename(), driver="GPKG", index=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly out of curiosty: What happens if index=True but no name is provided, does it error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oef, then it writes the index to an index column, and still adds a 1-based autoincrement fid.
This is the trivial model, which has edge_id 9 and 11 only.
image


def sort(self):
self.df.sort_index(inplace=True)
Expand Down
23 changes: 21 additions & 2 deletions python/ribasim/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,29 @@ def test_plot(discrete_control_of_pid_control):

def test_write_adds_fid_in_tables(basic, tmp_path):
model_orig = basic
# for node an explicit index was provided
assert model_orig.network.node.df.index.name == "fid"
assert model_orig.network.node.df.index.equals(pd.RangeIndex(start=1, stop=18))
# for edge no index was provided, but it still needs to write it to file
assert model_orig.network.edge.df.index.name is None
assert model_orig.network.edge.df.index.equals(pd.RangeIndex(start=0, stop=17))

model_orig.write(tmp_path / "basic/ribasim.toml")
with connect(tmp_path / "basic/database.gpkg") as connection:
query = f"select * from {esc_id('Basin / profile')}"
df = pd.read_sql_query(query, connection, parse_dates=["time"])
df = pd.read_sql_query(query, connection)
assert "fid" in df.columns
fids = df["fid"]
assert fids.equals(pd.Series(range(1, len(fids) + 1)))

query = "select fid from Node"
df = pd.read_sql_query(query, connection)
assert "fid" in df.columns
fids = df.get("fid")
fids = df["fid"]
assert fids.equals(pd.Series(range(1, len(fids) + 1)))

query = "select fid from Edge"
df = pd.read_sql_query(query, connection)
assert "fid" in df.columns
fids = df["fid"]
assert fids.equals(pd.Series(range(0, len(fids))))
Loading