seirsplus
seirsplus copied to clipboard
KeyError triggered by model.run()
This has happened after migrating from 0.14 to 1.0.x on code that ran without issues on the older version. This is a basic SEIR model on a network (no quarantine/tracing/social distancing), after instantiating the network. I'll do more investigating to see what the exact context is, but in the meantime I thought I would flag it. If it matters, the network I am using has roughly 4,000 nodes and 100,000 edges.
The error message is as follows:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-6-4d6a510161ce> in <module>
104 gamma = 1/15,
105 initI = 5)
--> 106 model.run(T=100, print_interval=10)
seirsplus\models.py in run(self, T, checkpoints, print_interval, verbose)
1336 while running:
1337
-> 1338 running = self.run_iteration()
1339
1340 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
seirsplus\models.py in run_iteration(self)
1224 # Save information about infection events when they occur:
1225 if(transitionType == 'StoE'):
-> 1226 transitionNode_GNbrs = list(self.G[transitionNode].keys())
1227 transitionNode_GQNbrs = list(self.G_Q[transitionNode].keys())
1228 self.infectionsLog.append({ 't': self.t,
networkx\classes\graph.py in __getitem__(self, n)
473 AtlasView({1: {}})
474 """
--> 475 return self.adj[n]
476
477 def add_node(self, node_for_adding, **attr):
networkx\classes\coreviews.py in __getitem__(self, name)
79
80 def __getitem__(self, name):
---> 81 return AtlasView(self._atlas[name])
82
83 def copy(self):
KeyError: 3047
@ryansmcgee I believe the problem is that the routine run_iteration
in module.py expects both G
and G_Q
to be indexed sequentially, e.g. G[0]
is the first node and so on. However, if the graph built in networkx has labels attached to the nodes, e.g. G['Hello']
is the first node, then the KeyError mentioned above occurs.
Edit: Here's a minimal working example that triggers the error:
from seirsplus.models import *
from seirsplus.networks import *
import networkx as nx
import random
graph = nx.erdos_renyi_graph(100, 0.1)
nlist = ["{:02}".format(n) for n in range(100)] # generate node ids as strings '00' through '99'
random.shuffle(nlist) # assign the node ids randomly
mapping = dict(zip(list(graph.nodes), nlist))
graph = nx.relabel_nodes(graph, mapping)
model = SEIRSNetworkModel(G = graph,
beta = 0.25
sigma = 1 / 5,
gamma = 1 / 15,
initI = 5)
model.run(T=100, print_interval=10)
model.figure_infections(ylim=0.5)
Commenting out lines 8-11 (node relabeling) removes the error.
In the meantime, I suggest trying out nx.relabel.convert_node_labels_to_integers