gboeing / osmnx-examples

Gallery of OSMnx tutorials, usage examples, and feature demonstations.
https://osmnx.readthedocs.io
MIT License
1.52k stars 521 forks source link

Issue running 00-osmnx-features-demo #8

Closed amedury closed 6 years ago

amedury commented 6 years ago

I am new to this package, and network analysis on python generally. As I was replicating the commands in the first example notebook, I ran in to the following issue:

edge_centrality = nx.closeness_centrality(nx.line_graph(G))

NetworkXNotImplemented                    Traceback (most recent call last)
<ipython-input-14-9e05ced82762> in <module>()
      1 # edge closeness centrality: convert graph to line graph so edges become nodes and vice versa
----> 2 edge_centrality = nx.closeness_centrality(nx.line_graph(G))

<decorator-gen-501> in line_graph(G, create_using)

/usr/local/lib/python2.7/dist-packages/networkx/utils/decorators.pyc in _not_implemented_for(not_implement_for_func, *args, **kwargs)
     69         if match:
     70             msg = 'not implemented for %s type' % ' '.join(graph_types)
---> 71             raise nx.NetworkXNotImplemented(msg)
     72         else:
     73             return not_implement_for_func(*args, **kwargs)

NetworkXNotImplemented: not implemented for multigraph type

I imagine this is something to do with the networkx package, but I was wondering if anyone else has any suggestions to overcome this problem.

Thanks!

gboeing commented 6 years ago

@amedury there's a bug in networkx 2.1. To calculate centrality on a line graph, use either nx 2.0 or the unreleased release candidate of 2.2. Will be formally resolved whenever nx 2.2 is released. See also https://github.com/gboeing/osmnx-examples/issues/6

matthewstidham commented 5 years ago

I'm running 2.2 and I am getting the error code NetworkXNotImplemented: not implemented for undirected type when I attempt to run centrality on a directed graph I generated from the from_pandas_edgelist function.

gboeing commented 5 years ago

What is the type(G) of the graph G you're passing in to the line_graph function?

matthewstidham commented 5 years ago

networkx.classes.graph.Graph I generated it from

def picturefeeder(text,function):
    keywords = []
    sentences = [x + '.' for x in text.replace('?','.').replace('!','.').split('.')]
    for n in [*range(0,len(sentences)-1)]:
        # Build your graph
        G=nx.from_pandas_edgelist(spacyfunction(sentences[n]),source='textnum',target='parentnum')
        comm = pd.DataFrame(function(G))
        centraldata = pd.merge(pd.DataFrame(comm.mean()),spacyfunction(sentences[n]), left_index=True,right_on='textnum')
        bestnoun = centraldata[centraldata['pos'].isin(['NOUN','PRON'])].sort_values(0,ascending=False).head(1)
        #keywords.append([*bestnoun['text']])
        #keywordsa.append([*bestnoun['textnum']])
        childnodes = centraldata[centraldata['parentnum'] == [*bestnoun['textnum']][0]]
        childnodes = childnodes[childnodes['pos'] != 'PUNCT'][childnodes['pos'] != 'ADP']
        feeder = pd.concat([childnodes,bestnoun])
        #keywords.append([[*bestnoun['text']],[*centraldata[centraldata['parentnum'] == [*bestnoun['textnum']][0]]['text']]])
        keywords.append(' '.join([*feeder.sort_index()['text']]))
    keywords = [x.strip() for x in keywords]
    return keywords

hydrangea is a string made of multiple sentences. spacyfunction is a function which takes that sentence and uses spacy to find its parts of speech, saving as a Pandas Dataframe.

gboeing commented 5 years ago

@matthewstidham I'm not seeing what your code snippet has to do with OSMnx or the Jupyter notebook that this issue was opened for.