Kaow
Kaow

Reputation: 563

Networkx - How can i get value from "all_pairs_shortest_path_length" function?

First i have a look at how to use all_pairs_dijkstra_path_length function in Networkx - How to get shortest path length between nodes showing node id instead of label at the first anwser

when i try to get distance value from these code

G = nx.read_gpickle("Database/Pickle/man.gpickle")
path_lengths = nx.all_pairs_dijkstra_path_length(G, weight='cost')
l = path_lengths.get("Man").get("Year")
print(l)

but it error like this

AttributeError: 'generator' object has no attribute 'get'

this is how in path_lenghts look like when i print with these code

for p in path_lengths:
   print(p)

output

('Man', {'Man': 0, 'Woman': 1.1532125205930808, 'Year': 1.9607843137254901, 'Baby': 2.4390243902439024})
('Woman', {'Woman': 0, 'Man': 1.1532125205930808, 'Baby': 1.477832512315271, 'Year': 2.4390243902439024})
('Year', {'Year': 0, 'Man': 1.9607843137254901, 'Woman': 2.4390243902439024, 'Baby': 3.9168569025591733})
('Baby', {'Baby': 0, 'Woman': 1.477832512315271, 'Man': 2.4390243902439024, 'Year': 3.9168569025591733})

Upvotes: 1

Views: 3298

Answers (2)

Joel
Joel

Reputation: 23827

You are probably using networkx v2.x. The code you have written assumes all_pairs_dijkstra_path_length is a dict. This was the case in networkx v1.11 (and probably earlier as well).

However it has now changed. It now returns an iterator. Here is information from the migration guide:

With the release of NetworkX 2.0 we are moving to a view/iterator reporting API. We have changed many methods from reporting lists or dicts to iterating over the information.

The simplest change to your code would be to use

path_lengths = dict(nx.all_pairs_dijkstra_path_length(G, weight='cost'))

instead of how you currently define path_lengths.

Upvotes: 2

Prune
Prune

Reputation: 77837

This is still quite strange. Your posting is still incomplete, but I think I can identify one of two problems. See this lovely debug blog for help.

To diagnose the problem more closely, do the basic debugging:

print (type(path_lengths), path_lengths)

Your print loop masks the problem by pushing the critical variable through an iterator. The error message claim that you're trying to call the get method of a generator. You have two get calls in that line, but haven't tried to figure out which is the problem.

Hypothesis 1

Although all_pairs_dijkstra_path_length is supposed to return a dict, you somehow have a generator. The print type above will check that problem. If this is the case, you need to convert for your desired usage, such as

path_list = list(path_lengths)

Hypothesis 2

You did get a dict returned, but the info therein are not what you think you have. What you've printed out indicates that you have tuples as your keys. Look at one line, one p in path_lengths:

('Man', {'Man': 0,
         'Woman': 1.1532125205930808, 
         'Year': 1.9607843137254901, 
         'Baby': 2.4390243902439024
        }
)

This also does not have a get method, although your error then would have been

AttributeError: 'tuple' object has no attribute 'get'

Either way, I'm highly suspicious of your network set-up; the output indicates that you have unusual values in the network.

Upvotes: 0

Related Questions