Skip to content

Commit

Permalink
Updated testing for csv writer (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
KCGallagher committed Aug 6, 2024
1 parent 8b33fa1 commit 222cbfb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_init(self, mock_mkdir):
mock_categories)
del m
mo.assert_called_once_with(
os.path.join('mock_folder', 'mock_filename'), 'w')
os.path.join('mock_folder', 'mock_filename'), 'w', newline='')
mo().write.assert_called_once_with('Cat1,Cat2,Cat3\r\n')
mock_mkdir.assert_called_with('mock_folder')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_configure(self, mock_mkdir):
del test_sim.ih_status_writer
del test_sim.ih_infectiousness_writer
del test_sim.secondary_infections_writer
mo.assert_called_with(filename, 'w')
mo.assert_called_with(filename, 'w', newline='')

@patch('os.makedirs')
@patch('logging.warning')
Expand Down Expand Up @@ -132,7 +132,7 @@ def test_configure_ih_status(self, mock_mkdir):
del test_sim.ih_status_writer
del test_sim.ih_infectiousness_writer
del test_sim.secondary_infections_writer
mo.assert_called_with(filename, 'w')
mo.assert_called_with(filename, 'w', newline='')

@patch('os.makedirs')
def test_configure_ih_infectiousness(self, mock_mkdir):
Expand Down Expand Up @@ -161,7 +161,7 @@ def test_configure_ih_infectiousness(self, mock_mkdir):
del test_sim.ih_status_writer
del test_sim.ih_infectiousness_writer
del test_sim.secondary_infections_writer
mo.assert_called_with(filename, 'w')
mo.assert_called_with(filename, 'w', newline='')

@patch('os.makedirs')
def test_configure_secondary_infections(self, mock_mkdir):
Expand Down Expand Up @@ -191,7 +191,7 @@ def test_configure_secondary_infections(self, mock_mkdir):
del test_sim.ih_status_writer
del test_sim.ih_infectiousness_writer
del test_sim.secondary_infections_writer
mo.assert_called_with(filename, 'w')
mo.assert_called_with(filename, 'w', newline='')

@patch('logging.exception')
@patch('os.path.join')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import unittest
from unittest.mock import patch, mock_open, call

import pyEpiabm as pe
Expand Down Expand Up @@ -87,8 +88,8 @@ def test_construct(self, mock_mkdir):
"Age group 0"])
del dem_sweep.writer
del dem_sweep.counts_writer
mo.assert_has_calls([call(file_name, 'w'),
call(counts_file_name, 'w')],
mo.assert_has_calls([call(file_name, 'w', newline=''),
call(counts_file_name, 'w', newline='')],
any_order=True)

@patch('os.makedirs')
Expand All @@ -115,8 +116,8 @@ def test_construct_age_spatial(self, mock_mkdir):
[f"Age group {i}" for i in range(17)])
del dem_sweep.writer
del dem_sweep.counts_writer
mo.assert_has_calls([call(file_name, 'w'),
call(counts_file_name, 'w')],
mo.assert_has_calls([call(file_name, 'w', newline=''),
call(counts_file_name, 'w', newline='')],
any_order=True)

@patch('os.makedirs')
Expand Down Expand Up @@ -253,3 +254,7 @@ def test_write_to_file_age_spatial(self, mock_mkdir):
mock_mkdir.assert_called_with(os.path.join(os.getcwd(),
self.dem_file_params[
"output_dir"]))


if __name__ == '__main__':
unittest.main()
6 changes: 4 additions & 2 deletions python_examples/spatial_example/spatial_simulation_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@

# 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.ToyPopulationFactory.assign_cell_locations(population,
method='grid')
pe.routine.FilePopulationFactory.print_population(population, file_loc)

# Version II: Generate population from input file.
Expand Down Expand Up @@ -84,8 +85,9 @@

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

plt.legend().remove()
plt.title("Infection curves for multiple cells")
plt.ylabel("Infected Population")
plt.savefig(
Expand Down

0 comments on commit 222cbfb

Please sign in to comment.