starsimhub / starsim

Starsim disease modeling framework
http://starsim.org
MIT License
14 stars 8 forks source link

Odd error when rerunning a simulation #378

Closed cliffckerr closed 7 months ago

cliffckerr commented 7 months ago
import starsim as ss

ppl = ss.People(10000)
ppl.networks = ss.ndict(ss.StaticNet())

gon = ss.Gonorrhea()
gon.pars['beta'] = {'static': [0.02, 0.01]}

sim1 = ss.Sim(people=ppl, diseases=gon)
sim1.run()

sim2 = ss.Sim(people=ppl, diseases=gon)
sim2.run()

  File ~/idm/starsim/starsim/modules.py:68 in initialize
    s, scale = ss.lognorm_params(mu, stdev)  # Assume stdev of 1

UnboundLocalError: cannot access local variable 'mu' where it is not associated with a value

due to par and par_dists having been transformed and this code not handling that case:

            # If it's a lognormal distribution, initialize assuming the par is the desired mean
            if par_dist.name == 'lognorm':
                if sc.isiterable(par):
                    if isinstance(par, dict):
                        mu = par['mu']
                        stdev = par['stdev']
                    elif isinstance(par, list):
                        mu = par[0]
                        stdev = par[1]
                elif sc.isnumber(par):
                    mu = par
                    stdev = 1

                s, scale = ss.lognorm_params(mu, stdev)  # Assume stdev of 1
                self.pars[key] = self.par_dists[key](s=s, scale=scale)
cliffckerr commented 7 months ago

Error message is meaningful now, still an error, but that depends on #103

import starsim as ss

ppl = ss.People(1000)
networks = ss.ndict(ss.StaticNet())

gon = ss.Gonorrhea()
gon.pars['beta'] = {'static': [0.02, 0.01]}

sim1 = ss.Sim(people=ppl, diseases=gon, networks=networks)
sim1.run()

sim2 = ss.Sim(people=ppl, diseases=gon, networks=networks)
sim2.run()