In this example, we're going to illustrate how discrete data can be used as a 2D distribution. To illustrate this, we're going to take samples from this 2D distribution to use as the geographic location of the people in the simulation. We'll then make a histogram of these locations to show that we've indeed been sampling from the specified discrete distribution.
In [1]:
%matplotlib inline
import pysimpactcyan
import pandas as pd
import matplotlib.pyplot as plt
In [2]:
simpact = pysimpactcyan.PySimpactCyan()
In [3]:
cfg = { }
cfg["population.nummen"] = 100000
cfg["population.numwomen"] = 100000
cfg["population.maxevents"] = 1 # We don't need an actual simulation, just the 2D locations
cfg["population.eyecap.fraction"] = 0 # Don't initialize formation events (would not even be possible for this many people)
# We're going to sample the geographic location (which is not currently used for anything)
# from a discrete distribution, based on data in a tiff file
cfg["person.geo.dist2d.type"] = "discrete"
cfg["person.geo.dist2d.discrete.densfile"] = "/tmp/simptest/up32.tiff"
cfg["person.geo.dist2d.discrete.maskfile"] = "" # This option must be set, but we leave it blank to disable the mask
cfg["person.geo.dist2d.discrete.width"] = 320
cfg["person.geo.dist2d.discrete.height"] = 240
In [4]:
# The data in 'up32.tiff' corresponds to the following image
plt.imshow(plt.imread("up.png"));
In [5]:
res = simpact.run(cfg, "/tmp/simptest")
In [6]:
# We're going to get the information from the persons log, in which the X and Y
# coordinate for each person will be saved
persons = pd.read_csv(res["logpersons"])
In [7]:
# Let's make a histogram of these coordinates, which should show that we're
# sampling from the discrete distribution that corresponds to the image shown
# above
plt.hist2d(persons["XCoord"], persons["YCoord"],bins=100,range=[[0,320],[0,240]]);
In [8]:
# As explained in the Simpact Cyan documentation, the default value of the following
# parameter is 'yes', to account for a difference in Y-axis orientation between images
# and regular plots. To illustrate what would happen otherwise, we'll set it to 'no'
# in the next test
cfg["person.geo.dist2d.discrete.flipy"] = "no"
# Again run the simulation and plot the histogram
res = simpact.run(cfg, "/tmp/simptest")
persons = pd.read_csv(res["logpersons"])
plt.hist2d(persons["XCoord"], persons["YCoord"],bins=100,range=[[0,320],[0,240]]);
In [ ]: