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

Vertices plot #223

Open
apollner opened this issue Sep 9, 2021 · 6 comments
Open

Vertices plot #223

apollner opened this issue Sep 9, 2021 · 6 comments
Assignees

Comments

@apollner
Copy link

apollner commented Sep 9, 2021

The Vertices disappear when I make a plot, how can I plot with Vertices? Thanks

@kaklise
Copy link
Collaborator

kaklise commented Sep 13, 2021

Right now, vertices are stored as an attributes on links (pipes, pumps, valves) and can be read in and written to INP files, but they are not used for visualization in WNTR. We’ve talked about updating graphics options in WNTR, but I’m not sure how quickly we’ll get to this.

To use vertices in functions like plot_network, I think we would need to create a temporary NetworkX graph from the water network model that includes the additional vertices as “hidden nodes” and keep the ability to plot node attributes only on actual nodes (junctions, tanks, reservoirs). If this is something you’d like to work on, please let us know.

@apollner
Copy link
Author

apollner commented Sep 13, 2021

Right now, vertices are stored as an attributes on links (pipes, pumps, valves) and can be read in and written to INP files, but they are not used for visualization in WNTR. We’ve talked about updating graphics options in WNTR, but I’m not sure how quickly we’ll get to this.

To use vertices in functions like plot_network, I think we would need to create a temporary NetworkX graph from the water network model that includes the additional vertices as “hidden nodes” and keep the ability to plot node attributes only on actual nodes (junctions, tanks, reservoirs). If this is something you’d like to work on, please let us know.

@kaklise thanks for the response. It sounds like an interesting feature to work on, just out of my scope at the moment.
For now I managed using QGISRed.

@ucchejbb
Copy link

@kaklise Not the prettiest code, but something like the code below should work. I didn't implement the "zip" approach that WNTR uses, but should be easy enough to do. The key is to create a third list for "vertices" and add them to the 'pos' list, but add them with zero size. I didn't change the core WNTR code, just created a quick script to see if it works. But the vertex list should be able to be created within the current link loop within plot_network.

--- code snippet ---
G = wn.get_graph()
pos = wntr.graphics.network.nx.get_node_attributes(G, 'pos')

#restart graph

G = nx.MultiDiGraph()
nodes_list = wn.node_name_list
vertex_list = []

for node in pos.keys():
G.add_node(node, size=10)

for link in wn.link_name_list:
link_prop = wn.get_link(link)
len_vertices = len(link_prop.vertices)
st_node = link_prop.start_node.name
en_node = link_prop.end_node.name

if len_vertices>0: # do something if there are vertices
    base_name = link+"_v"
    for vertex in range(len_vertices):
        val = vertex + 1
        name = base_name+str(val)
        vertex_list += [name]
        
        G.add_node(name, size=0)
        G.add_edge(st_node, name)
        if val == len_vertices:
            G.add_edge(name, en_node)
        
        st_node = name
        pos[name] = link_prop.vertices[vertex]

else:
    G.add_edge(st_node, en_node)

nx.draw_networkx_nodes(G, pos, nodelist=nodes_list, node_size=20) # real nodes
nx.draw_networkx_nodes(G, pos, nodelist=vertex_list, node_size=0) ## vertices
nx.draw_networkx_edges(G, pos, arrowstyle='-', arrowsize=0, node_size=0) #all links

@pahbloo
Copy link
Contributor

pahbloo commented Jan 2, 2022

Wouldn't this approach break the link_labels option? Because the original edges are now gone, and we have new, smaller edges away from where the original was.

@pahbloo
Copy link
Contributor

pahbloo commented Jan 8, 2022

I'm interested in working on this issue. I have some questions, though:

  1. Should plot the links with vertices be optional? (Ex.: wntr.graphics.plot_network(wn, vertices=True))
  2. In that case, what should be the default?
  3. And the name of the parameter? vertices? link_vertices?

@kaklise
Copy link
Collaborator

kaklise commented Jan 11, 2022

Thanks!

  1. For backward graphics compatibility, the option to not use vertices is a good idea.
  2. I suggest setting the default value to True (use vertices).
  3. I think vertices is a good parameter name

There might be additional graphics functions that could make use of this update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants