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

Process edge attributes from networks graph based on user kwargs #1331

Open
sriharisundar opened this issue Apr 28, 2024 · 2 comments
Open

Comments

@sriharisundar
Copy link

Is your feature request related to a problem? Please describe.

This is my use case - I have a geodataframe with a bunch of linestring geometry. While I can visualize that with geodataframe.hvplot, I do want to show the network topology (arrows etc), so felt ploting a networkx directed graph might be better. So I used momepy.gdf_to_nx. This either returns a DiGraph or a MultiDiGraph, and converts all columns of the gdf to attributes of the graph. For my purposes a DiGraph seems sufficient. I also want to color the edges and show some attributes on hover (thanks to all the posts on ispection policy and hovertools I am able to achieve this). Since there is a geometry attribute in my initial gdf, I get an error when I visualize this graph - TypeError: '<' not supported between instances of 'LineString' and 'LineString'. Entire stack trace is here.

Describe the solution you'd like

I think there are a few ways to get around this, all with some changes in the from_networkx function:

  1. Ensure we only parse through types which can be compared (and not avoid just list/dict).
  2. Let the user specify edge attributes (as a list and passed in as a kwarg) from the graph to parse into the holoview Graph object.

Additional context

Initially I was using the defaults in momepy.gdf_to_nx and it returns a MultiDiGraph, which stores attributes one level deeper, so supporting that would also involve the keyword approach along with checking the type of graph.

There is a from_networkx function in hvplot.networkx as well as in holoviews graph.py which both seem replicas of each other, so we may have to make changes in the best possible location.

@hoxbro
Copy link
Member

hoxbro commented Apr 29, 2024

Can you share a minimal, reproducible example (MRE)?

In general, a complete script only with the essential code, which can be copied/pasted and immediately run as-is with no modifications.

@sriharisundar
Copy link
Author

sriharisundar commented Apr 29, 2024

Here is an MRE. Should I also share my package versions? Thanks!

from shapely.geometry import Point, LineString
import hvplot.networkx as hvnx
import geopandas as gpd
import momepy

d = {'line': ['p1 => p2', 'p2 => p3'], 
     'flow':[10,15], 
     'geometry': [LineString([Point(1,2),Point(2,4)]), LineString([Point(2,4),Point(3,1)])]}
gdf = gpd.GeoDataFrame(d)

graph = momepy.gdf_to_nx(gdf,approach="primal",directed=True,multigraph=False)

edges = hvnx.draw_networkx_edges(
    graph, {n: [n[0], n[1]] for n in list(graph.nodes)}, arrowstyle="->", 
    edge_cmap='viridis', edge_color='flow',colorbar=True
)

edges

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

No branches or pull requests

2 participants