From 8b33fa1c8d6ed0d32607f47aa2d74509ce0d5f6d Mon Sep 17 00:00:00 2001 From: kcgallagher Date: Tue, 6 Aug 2024 01:53:59 +0200 Subject: [PATCH] Spatial simulation bugfix (#268) --- pyEpiabm/pyEpiabm/output/_csv_dict_writer.py | 2 +- python_examples/README.md | 8 ++--- .../spatial_simulation_flow.py | 29 +++++++------------ 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/pyEpiabm/pyEpiabm/output/_csv_dict_writer.py b/pyEpiabm/pyEpiabm/output/_csv_dict_writer.py index 0269991c9..4a0b3d7e3 100644 --- a/pyEpiabm/pyEpiabm/output/_csv_dict_writer.py +++ b/pyEpiabm/pyEpiabm/output/_csv_dict_writer.py @@ -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() diff --git a/python_examples/README.md b/python_examples/README.md index 780be7d7a..e07084bac 100644 --- a/python_examples/README.md +++ b/python_examples/README.md @@ -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 diff --git a/python_examples/spatial_example/spatial_simulation_flow.py b/python_examples/spatial_example/spatial_simulation_flow.py index ad02e7f79..2bed7624a 100644 --- a/python_examples/spatial_example/spatial_simulation_flow.py +++ b/python_examples/spatial_example/spatial_simulation_flow.py @@ -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__), @@ -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(