WNTR icon indicating copy to clipboard operation
WNTR copied to clipboard

Vertices plot

Open apollner opened this issue 3 years ago • 6 comments

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

apollner avatar Sep 09 '21 13:09 apollner

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 avatar Sep 13 '21 14:09 kaklise

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.

apollner avatar Sep 13 '21 16:09 apollner

@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

ucchejbb avatar Sep 13 '21 17:09 ucchejbb

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 avatar Jan 02 '22 04:01 pahbloo

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?

pahbloo avatar Jan 08 '22 05:01 pahbloo

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.

kaklise avatar Jan 11 '22 18:01 kaklise