question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'KeysView' object is returned instead of a list

See original GitHub issue

When running this: G_normal = custom_exponential_graph(baseGraph, scale=100)

I get the following error:

==================================================

TypeError Traceback (most recent call last) mtrand.pyx in mtrand.RandomState.choice()

TypeError: ‘KeysView’ object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) <ipython-input-2-1a0d817170a8> in <module> 3 4 # Baseline normal interactions: ----> 5 G_normal = custom_exponential_graph(baseGraph, scale=100) 6 plot_degree_distn(G_normal, max_degree=40)

~\Anaconda3\envs\corona\lib\site-packages\seirsplus\models.py in custom_exponential_graph(base_graph, scale, min_num_edges, m, n) 1175 neighbors = graph[node].keys() 1176 quarantineEdgeNum = int( max(min(numpy.random.exponential(scale=scale, size=1), len(neighbors)), min_num_edges) ) -> 1177 quarantineKeepNeighbors = numpy.random.choice(neighbors, size=quarantineEdgeNum, replace=False) 1178 for neighbor in neighbors: 1179 if(neighbor not in quarantineKeepNeighbors):

mtrand.pyx in mtrand.RandomState.choice()

ValueError: ‘a’ must be 1-dimensional or an integer

solved it by wrapping graph[node].keys() with list(): neighbors = graph[node].keys() -> neighbors = list(graph[node].keys())

Is it right?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kpelechriniscommented, Mar 26, 2020

@tomermilo actually the following version does not give any problem (essentially it is what you also mentioned I think) and I was able to run the example provided:

============================================== def custom_exponential_graph(base_graph=None, scale=100, min_num_edges=0, m=9, n=None): # Generate a random preferential attachment power law graph as a starting point. # By the way this graph is constructed, it is expected to have 1 connected component. # Every node is added along with m=8 edges, so the min degree is m=8. if(base_graph): graph = base_graph.copy() else: assert(n is not None), “Argument n (number of nodes) must be provided when no base graph is given.” graph = networkx.barabasi_albert_graph(n=n, m=m)

# To get a graph with power-law-esque properties but without the fixed minimum degree,
# We modify the graph by probabilistically dropping some edges from each node. 
for node in graph:
    neighbors = list(graph[node].keys())
    quarantineEdgeNum = int( max(min(numpy.random.exponential(scale=scale, size=1), len(neighbors)), min_num_edges) )
    quarantineKeepNeighbors = numpy.random.choice(neighbors, size=quarantineEdgeNum, replace=False)
    for neighbor in neighbors:
        if(neighbor not in quarantineKeepNeighbors):
            graph.remove_edge(node, neighbor)

return graph
0reactions
ryansmcgeecommented, Mar 29, 2020

The change to list(dict.keys()) has been made throughout the package to make this keys lookup work in both Python 2 and 3. Thank you for flagging this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

KeysView is returned instead of the expected list (quickstart ...
I expected to get a list of keys, but instead I got this object: KeysView(<HDF5 file "myfile.nwb" (mode r+)>).
Read more >
TypeError: 'KeysView' object does not support indexing
In Python3, dictionary keys returns a 'view', not an indexable list. ... TypeError: 'KeysView' object does not support indexing In [89]: ...
Read more >
8.3. collections — High-performance container datatypes
Counter objects have a dictionary interface except that they return a zero count for missing items instead of ... For fast random access,...
Read more >
KeysView Object? Why? - Replit
I think it has to do with how much memory it uses. I think that Dictionary View items use less memory and processing...
Read more >
python/typing - Gitter
When mypy complained about the type List[int] being wrong, ... the values() method should return a KeysView rather than a ValuesView , because...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found