InstituteforDiseaseModeling / covasim

COVID-19 Agent-based Simulator (Covasim): a model for exploring coronavirus dynamics and interventions
https://covasim.org
MIT License
250 stars 223 forks source link

outputing transmission tree throws error. #348

Closed aezarebski closed 2 years ago

aezarebski commented 3 years ago

Describe the bug

Requesting the transmission tree from a simulation throws an error

To reproduce

  1. Clone covasim (I used commit 7da3bc46e2344fa8128dfa66c260cadf4213bea2)
  2. Create virtual env using the included requirements.txt
  3. pip install networkx (because this is needed)
  4. Start python REPL
import covasim as cv
sim = cv.Sim()
sim.run()
sim.make_transtree(to_networkx=True)

Expected behavior

I would have expected the graph object to be constructed.

Screenshots or outputs

Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import covasim as cv
Covasim 3.0.7 (2021-06-29) — © 2021 by IDM
>>> sim = cv.Sim()
>>> sim.run()
Initializing sim with 20000 people for 60 days
  Running 2020-03-01 ( 0/60) (0.22 s)  ———————————————————— 2%
  Running 2020-03-11 (10/60) (0.24 s)  •••————————————————— 18%
  Running 2020-03-21 (20/60) (0.27 s)  ••••••—————————————— 34%
  Running 2020-03-31 (30/60) (0.29 s)  ••••••••••—————————— 51%
  Running 2020-04-10 (40/60) (0.32 s)  •••••••••••••——————— 67%
  Running 2020-04-20 (50/60) (0.36 s)  ••••••••••••••••———— 84%
  Running 2020-04-30 (60/60) (0.41 s)  •••••••••••••••••••• 100%

Simulation summary:
   11828 cumulative infections
       0 cumulative reinfections
    9581 cumulative infectious
    6119 cumulative symptomatic cases
     370 cumulative severe cases
     121 cumulative critical cases
    4929 cumulative recoveries
      19 cumulative deaths
       0 cumulative tests
       0 cumulative diagnoses
       0 cumulative known deaths
       0 cumulative quarantined people
       0 cumulative vaccinations
       0 cumulative vaccinated people

Sim(<no label>; 2020-03-01 to 2020-04-30; pop: 20000 random; epi: 11828⚙, 19☠)
>>> sim.make_transtree(to_networkx=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/aez/sandbox/covasim/covasim/sim.py", line 1222, in make_transtree
    tt = cva.TransTree(self, *args, **kwargs)
  File "/home/aez/sandbox/covasim/covasim/analysis.py", line 1529, in __init__
    self.graph.add_edge(edge['source'],edge['target'],date=edge['date'],layer=edge['layer'])
  File "/home/aez/sandbox/covasim/venv/lib/python3.8/site-packages/networkx/classes/digraph.py", line 622, in add_edge
    raise ValueError("None cannot be a node")
ValueError: None cannot be a node
>>> 
cliffckerr commented 3 years ago

Thanks @aezarebski , this is a regression introduced in Networkx 2.6: https://networkx.org/documentation/stable/release/release_2.6.html It should work in Networkx 2.5 but we'll fix the implementation so it works for 2.6 as well.

cliffckerr commented 3 years ago

Hi @aezarebski , this will be fixed in the next release (3.1.0). If you want to fix it yourself, in analysis.py just change

            # Next, add edges from linelist
            for edge in people.infection_log:
                self.graph.add_edge(edge['source'],edge['target'],date=edge['date'],layer=edge['layer'])

to

            # Next, add edges from linelist
            for edge in people.infection_log:
                if edge['source'] is not None: # Skip seed infections
                    self.graph.add_edge(edge['source'],edge['target'],date=edge['date'],layer=edge['layer'])
aezarebski commented 3 years ago

Thanks @cliffckerr

cliffckerr commented 2 years ago

Fixed in 3.1