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

Validate (circular) links in Ribasim python on write #2140

Open
evetion opened this issue Mar 11, 2025 · 2 comments
Open

Validate (circular) links in Ribasim python on write #2140

evetion opened this issue Mar 11, 2025 · 2 comments
Labels
ribasim-nl Related to Ribasim NL project

Comments

@evetion
Copy link
Member

evetion commented Mar 11, 2025

Now Julia gives an error, but ideally we know that beforehand.

As mentioned by @DanielTollenaar.

@evetion evetion added the ribasim-nl Related to Ribasim NL project label Mar 11, 2025
@github-project-automation github-project-automation bot moved this to To do in Ribasim Mar 11, 2025
@visr
Copy link
Member

visr commented Mar 11, 2025

Yeah since #2112 Python validates this on add. It doesn't seem optimal to validate on input and output side because the add API is regularly bypassed.

@DanielTollenaar
Copy link
Contributor

For the time being we've implemented this in Ribasim-NL models:

    def validate_link_source_destination(self):
        """Check if links exist with reversed source-destination
        """
        df = self.link.df

        # on tuples we can easily check duplicates irrespective of order
        duplicated_links = pd.Series(
            list(zip(np.minimum(df["from_node_id"], df["to_node_id"]),
                           np.maximum(df["from_node_id"], df["to_node_id"]))), index=df.index).duplicated(keep=False)

        # if links are duplicated in reversed source-destination we raise an Exception
        if duplicated_links.any():
            raise ValueError(f"Links found with reversed source-destination: {list(df[duplicated_links].reset_index()[["link_id", "from_node_id", "to_node_id"]].to_dict(orient="index").values())}")

I run this before writing models which gives me a clear Exception before writing to disk. Would be great if this was the default when model.use_validation = True:

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ribasim-nl Related to Ribasim NL project
Projects
Status: To do
Development

No branches or pull requests

3 participants