Skip to content

Commit

Permalink
Spatial simulation bugfix (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
KCGallagher committed Aug 5, 2024
1 parent eedb61a commit 8b33fa1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pyEpiabm/pyEpiabm/output/_csv_dict_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, folder: str, filename: str, fieldnames: typing.List,
folder, os.path.splitext(filename)[0])
self.fieldnames = fieldnames

self.f = open(os.path.join(folder, filename), 'w')
self.f = open(os.path.join(folder, filename), 'w', newline='')
self.writer = csv.DictWriter(
self.f, fieldnames=fieldnames, delimiter=',')
self.writer.writeheader()
Expand Down
8 changes: 4 additions & 4 deletions python_examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ These .csv files are then used in the repository https://github.com/SABS-R3-Epid

## Spatial Simulations

Contained within the `spatial_example/` directory, this script runs a basic simulation with spatial dependance. It considers a population of 1000 individuals, spread across 40 households in 20 cells, each with a single microcell.
Contained within the `spatial_example/` directory, this script runs a basic simulation with spatial dependance. It considers a population of 10000 individuals, spread across 225 cells, each with a two microcells.

It subsequently saves the results to a .csv file, and plots the infection curve for each region. There is currently no differentiation between cells, and so any variation is due to random fluctuations. Any null curves are because there were no infectious individuals seeded in that cell, and no inter-cellular infection mechanisms are currently implemented.
It subsequently saves the results to a .csv file, and plots the infection curve for each region. There is currently no differentiation between cells, and so any variation is due to random fluctuations.

![Infection curves for multiple cells.pt](./spatial_example/spatial_outputs/spatial_flow_Icurve_plot.png)

We also provide a script (`voronoi_plotting_example.py`) for visualising the spatial distribution of the epidemic, generating cells based on a single location coordinate using [Voronoi Tesselation](https://en.wikipedia.org/wiki/Voronoi_diagram).
We also provide a script (`voronoi_plotting_example.py`) for visualising the spatial distribution of the epidemic, generating cells based on a single location coordinate using [Voronoi Tesselation](https://en.wikipedia.org/wiki/Voronoi_diagram). An [grid version](./spatial_example/spatial_outputs/voronoi_grid_img.png) of this plot is also generated.

![Voronoi_grid.pt](./spatial_example/spatial_outputs/voronoi_grid_img.png)
![Voronoi_grid.gif](./spatial_example/spatial_outputs/voronoi_animation.gif)

## Age Stratified Simulation

Expand Down
29 changes: 11 additions & 18 deletions python_examples/spatial_example/spatial_simulation_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,31 @@
"spatial_parameters.json"))

# Method to set the seed at the start of the simulation, for reproducibility

pe.routine.Simulation.set_random_seed(seed=30)

# Pop_params are used to configure the population structure being used in this
# simulation.

pop_params = {
"population_size": 10000,
"cell_number": 200,
"cell_number": 225,
"microcell_number": 2,
"household_number": 5,
}

# Create a population framework based on the parameters given.
# population = pe.routine.ToyPopulationFactory.make_pop(pop_params)

# Alternatively, can generate population from input file
file_loc = os.path.join(os.path.dirname(__file__), "input.csv")
population = pe.routine.FilePopulationFactory.make_pop(file_loc,
random_seed=42)


# Configure population with input data
pe.routine.ToyPopulationFactory.add_places(population, 1)
# pe.routine.FilePopulationFactory.print_population(population, file_loc)
# Version I: Create a population framework and save to file.
population = pe.routine.ToyPopulationFactory.make_pop(pop_params)
pe.routine.ToyPopulationFactory.assign_cell_locations(population, method='grid')
pe.routine.FilePopulationFactory.print_population(population, file_loc)

# Version II: Generate population from input file.
# population = pe.routine.FilePopulationFactory.make_pop(file_loc,
# random_seed=42)

# sim_ and file_params give details for the running of the simulations and
# where output should be written to.
sim_params = {"simulation_start_time": 0, "simulation_end_time": 30,
"initial_infected_number": 1, "initial_infect_cell": True}
sim_params = {"simulation_start_time": 0, "simulation_end_time": 80,
"initial_infected_number": 2, "initial_infect_cell": True}

file_params = {"output_file": "output.csv",
"output_dir": os.path.join(os.path.dirname(__file__),
Expand Down Expand Up @@ -90,9 +84,8 @@

df = df.pivot(index="time", columns="cell",
values="InfectionStatus.InfectMild")
df.plot()
df.plot(); plt.legend().remove()

plt.legend(labels=(range(len(df.columns))), title="Cell")
plt.title("Infection curves for multiple cells")
plt.ylabel("Infected Population")
plt.savefig(
Expand Down

0 comments on commit 8b33fa1

Please sign in to comment.