-
Notifications
You must be signed in to change notification settings - Fork 45
Unify hierarchical mesh edge level metadata #66
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
98ef11b
c8eb1b9
f387b31
fe7ef4d
42ad2dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,9 +139,16 @@ def create_all_graph_components( | |
| **m2m_connectivity_kwargs, | ||
| ) | ||
| # Only connect grid to bottom level of hierarchy | ||
| grid_connect_graph = split_graph_by_edge_attribute( | ||
| graph_components["m2m"], "level" | ||
| )[0] | ||
| bottom_same_level_edges = [ | ||
| (u, v) | ||
| for u, v, edge_data in graph_components["m2m"].edges(data=True) | ||
| if edge_data.get("direction") == "same" | ||
| and edge_data.get("from_level") == 0 | ||
| and edge_data.get("to_level") == 0 | ||
| ] | ||
| grid_connect_graph = graph_components["m2m"].edge_subgraph( | ||
| bottom_same_level_edges | ||
| ) | ||
| elif m2m_connectivity == "flat_multiscale": | ||
| graph_components["m2m"] = create_flat_multiscale_mesh_graph( | ||
| xy=xy, | ||
|
|
@@ -365,20 +372,39 @@ def _find_neighbour_node_idxs_in_source_mesh(xy_target): | |
| for edge_check_graph in (G_source, G_target): | ||
| # Check if graph has edges | ||
| if len(edge_check_graph.edges) > 0: | ||
| ( | ||
| level_subgraph, | ||
| no_level_subgraph, | ||
| ) = split_on_edge_attribute_existance(edge_check_graph, "level") | ||
|
|
||
| # Check if graph has levels (hierarchical or multi-scale edges) | ||
| if nx.is_empty(level_subgraph): | ||
| # Consider edges in whole graph (whole graph is level 1) | ||
| first_level_graph = edge_check_graph # == no_level_subgraph | ||
| # Hierarchical mesh graphs expose a direction + level-range | ||
| # edge schema. In that case use only same-level edges on | ||
| # bottom level to estimate local characteristic edge length. | ||
| if any( | ||
| "from_level" in edge_data and "to_level" in edge_data | ||
| for _, _, edge_data in edge_check_graph.edges(data=True) | ||
| ): | ||
| first_level_graph = edge_check_graph.edge_subgraph( | ||
| [ | ||
| (u, v) | ||
| for u, v, edge_data in edge_check_graph.edges(data=True) | ||
| if edge_data.get("direction") == "same" | ||
| and edge_data.get("from_level") == 0 | ||
| and edge_data.get("to_level") == 0 | ||
| ] | ||
| ) | ||
| else: | ||
| # Has levels, only consider edges in level 1 graph | ||
| first_level_graph = split_graph_by_edge_attribute( | ||
| level_subgraph, "level" | ||
| )[0] | ||
| ( | ||
| level_subgraph, | ||
| _no_level_subgraph, | ||
| ) = split_on_edge_attribute_existance(edge_check_graph, "level") | ||
|
|
||
| # Check if graph has levels (hierarchical or | ||
| # multi-scale edges) | ||
| if nx.is_empty(level_subgraph): | ||
| # Consider edges in whole graph (whole graph is | ||
| # level 1) | ||
| first_level_graph = edge_check_graph # == no_level_subgraph | ||
| else: | ||
| # Has levels, only consider edges in level 1 graph | ||
| first_level_graph = split_graph_by_edge_attribute( | ||
| level_subgraph, "level" | ||
| )[0] | ||
|
Comment on lines
+405
to
+407
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we are splitting on "level" again. Will this work?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this works. That split on "level" is only in the non-hierarchical fallback branch (when edges do not have from_level/to_level). Hierarchical graphs take the branch above and use direction + from_level/to_level, so they don’t rely on "level" there.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also @joeloskarsson I forgot to run precommit in my last commit that caused the CI to fail. I've resolved it in my latest commit |
||
| longest_graph_edge = max( | ||
| first_level_graph.edges(data=True), | ||
| key=lambda x: x[2].get("len", 0), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,11 +61,16 @@ def create_hierarchical_multiscale_mesh_graph( | |
| for level_i, graph in enumerate(Gs_all_levels) | ||
| ] | ||
|
|
||
| # add `direction` attribute to all edges with value `same`` | ||
| # Add edge metadata using a consistent schema across all directions. | ||
| for i, G in enumerate(Gs_all_levels): | ||
| for u, v in G.edges: | ||
| # Remove legacy keys to keep one consistent edge schema. | ||
| G.edges[u, v].pop("level", None) | ||
| G.edges[u, v].pop("levels", None) | ||
leifdenby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| G.edges[u, v]["direction"] = "same" | ||
| G.edges[u, v]["level"] = i | ||
| G.edges[u, v]["from_level"] = i | ||
| G.edges[u, v]["to_level"] = i | ||
| G.edges[u, v]["connection"] = f"{i}>{i}" | ||
|
|
||
| # Create inter-level mesh edges | ||
| up_graphs = [] | ||
|
|
@@ -105,15 +110,22 @@ def create_hierarchical_multiscale_mesh_graph( | |
| G_down.edges[u, v]["vdiff"] = ( | ||
| G_down.nodes[u]["pos"] - G_down.nodes[v]["pos"] | ||
| ) | ||
| G_down.edges[u, v]["levels"] = f"{from_level}>{to_level}" | ||
| G_down.edges[u, v]["direction"] = "down" | ||
| G_down.edges[u, v]["from_level"] = from_level | ||
| G_down.edges[u, v]["to_level"] = to_level | ||
| G_down.edges[u, v]["connection"] = f"{from_level}>{to_level}" | ||
|
|
||
| G_up = networkx.DiGraph() | ||
| G_up.add_nodes_from(G_down.nodes(data=True)) | ||
| for u, v, data in G_down.edges(data=True): | ||
| data = data.copy() | ||
| data["levels"] = f"{to_level}>{from_level}" | ||
| # Remove legacy keys to keep one consistent edge schema. | ||
| data.pop("level", None) | ||
| data.pop("levels", None) | ||
|
||
| data["direction"] = "up" | ||
| data["from_level"] = to_level | ||
| data["to_level"] = from_level | ||
| data["connection"] = f"{to_level}>{from_level}" | ||
| G_up.add_edge(v, u, **data) | ||
|
|
||
| up_graphs.append(G_up) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be helpful if you summarised what the attributes that have been removed are and what they have been replaced with