Skip to content

Commit

Permalink
Finished all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mghosh00 committed Jun 20, 2024
1 parent b0d030f commit 2873068
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 19 deletions.
5 changes: 3 additions & 2 deletions pyEpiabm/pyEpiabm/core/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ def store_serial_interval(self, latent_period: float):
"""Adds this `latent_period` to the current `exposure_period` to give
a `serial_interval`, which will be stored in the
`serial_interval_dict`. The serial interval is the time between a
primary case infection and a secondary case infection.
primary case infection and a secondary case infection. This method
is called immediately after a person becomes exposed.
Parameters
----------
Expand All @@ -328,7 +329,7 @@ def store_serial_interval(self, latent_period: float):
serial_interval = self.exposure_period + latent_period
# The reference day is the day the primary case was first infected
# This is what we will store in the dictionary
reference_day = self.infection_start_times[-1] - serial_interval
reference_day = self.time_of_status_change - serial_interval
try:
(self.serial_interval_dict[reference_day]
.append(serial_interval))
Expand Down
41 changes: 33 additions & 8 deletions pyEpiabm/pyEpiabm/tests/test_unit/test_core/test_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,40 @@ def test_increment_secondary_infections(self):
self.assertListEqual(self.person.secondary_infections_counts,
[2, 5, 2])

def test_add_serial_interval(self):
self.person.infection_start_times.append(1.0)
self.person.add_serial_interval(2.0)
self.person.infection_start_times.append(4.0)
self.person.add_serial_interval(1.0)
self.person.add_serial_interval(1.0)
self.person.add_serial_interval(3.0)
def test_set_exposure_period(self):
self.person.set_exposure_period(exposure_period=5)
self.assertEqual(self.person.exposure_period, 5)

def test_store_serial_interval_erroneous(self):
with self.assertRaises(RuntimeError) as ve_1:
self.person.store_serial_interval(4.0)
self.assertEqual(str(ve_1.exception),
"Cannot call store_serial_interval while the"
" exposure_period is None")
# Infect once with an exposure period for successful method call
self.person.set_exposure_period(1.0)
self.person.time_of_status_change = 2.0
self.person.store_serial_interval(latent_period=1.0)
# Do not add the exposure period for failure
self.person.time_of_status_change = 19.0
with self.assertRaises(RuntimeError) as ve_2:
self.person.store_serial_interval(latent_period=4.0)
self.assertEqual(str(ve_2.exception),
"Cannot call store_serial_interval while the"
" exposure_period is None")

def test_store_serial_interval(self):
self.person.set_exposure_period(1.0)
self.person.time_of_status_change = 2.0
self.person.store_serial_interval(latent_period=1.0)
self.person.set_exposure_period(2.0)
self.person.time_of_status_change = 6.0
self.person.store_serial_interval(latent_period=4.0)
self.person.set_exposure_period(5.0)
self.person.time_of_status_change = 19.0
self.person.store_serial_interval(latent_period=4.0)
self.assertDictEqual(self.person.serial_interval_dict,
{1.0: [2.0], 4.0: [1.0, 1.0, 3.0]})
{0.0: [2.0, 6.0], 10.0: [9.0]})


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,19 +706,19 @@ def test_write_to_serial_interval_file(self, mock_mkdir, time=1):
self.inf_history_params)
person1 = self.rt_test_population.cells[0].persons[0]
person1.num_times_infected = 2
person1.infection_start_times = [0.0, 2.0]
person1.serial_interval_dict = {0.0: [6.0, 3.0], 2.0: [3.0]}
person1.infection_start_times = [2.0, 10.0]
person1.serial_interval_dict = {0.0: [2.0], 1.0: [9.0]}
person2 = self.rt_test_population.cells[0].persons[1]
person2.num_times_infected = 1
person2.infection_start_times = [1.0]
person2.serial_interval_dict = {1.0: [4.0]}
person2.infection_start_times = [5.0]
person2.serial_interval_dict = {2.0: [3.0]}
person3 = self.rt_test_population.cells[0].persons[2]
person3.num_times_infected = 1
person3.infection_start_times = [1.0]
person3.serial_interval_dict = {1.0: [7.0, 5.0]}
dict_0 = {0.0: 6.0, 1.0: 4.0, 2.0: 3.0}
dict_1 = {0.0: 3.0, 1.0: 7.0, 2.0: np.nan}
dict_2 = {0.0: np.nan, 1.0: 5.0, 2.0: np.nan}
person3.infection_start_times = [3.0, 11.0, 21.0]
person3.serial_interval_dict = {0.0: [3.0, 11.0], 2.0: [19.0]}
dict_0 = {0.0: 2.0, 1.0: 9.0, 2.0: 3.0}
dict_1 = {0.0: 3.0, 1.0: np.nan, 2.0: 19.0}
dict_2 = {0.0: 11.0, 1.0: np.nan, 2.0: np.nan}

with patch('pyEpiabm.output._csv_dict_writer'
'._CsvDictWriter.write') as mock_write:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,32 @@ def test_update_time_status_change_no_age(self, current_time=100.0):
else:
self.assertLessEqual(current_time, time_of_status_change)

def test_update_time_status_change_serial_interval(self):
"""Tests that an exposed person has their latency period stored and
added to their exposure_period to give a serial interval (to be stored)
This occurs at the end of the update_time_status_change method
"""
test_sweep = pe.sweep.HostProgressionSweep()
# 3 days between their infector becoming infected and now (the time
# of the infection event)
self.person1.set_exposure_period(3.0)
self.person1.update_status(InfectionStatus.Exposed)
self.person1.next_infection_status = InfectionStatus.InfectMild
current_time = 5.0
test_sweep.update_time_status_change(self.person1, current_time)
time_of_status_change = self.person1.time_of_status_change
# The time_of_status_change to InfectMild minus the current_time
# gives the length of time that the person is latent
latent_period = time_of_status_change - current_time
# The reference time is the time in which their infector became
# infected
reference_time = 5.0 - 3.0
# The serial_interval_dict records their serial_interval (defined as
# the time between the infector having Infect status and the current
# person having Infect status) at the reference time
self.assertDictEqual(self.person1.serial_interval_dict,
{reference_time: [3.0 + latent_period]})

def test_update_time_status_change_waning_immunity(self):
"""Tests that the time to status change of a Recovered person with
waning immunity is equal to the output of the InverseCDF method. This
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,21 @@ def test__call__(self, mock_force):
# Add one susceptible to the population, with the mocked infectiousness
# ensuring they are added to the infected queue.
self.person.infection_status = pe.property.InfectionStatus.InfectMild
self.person.infection_start_times = [0.0]
test_queue = Queue()
new_person = pe.Person(self.microcell)
new_person.household = self.house
self.house.persons.append(new_person)
self.house.susceptible_persons.append(new_person)
self.pop.cells[0].persons.append(new_person)
self.assertEqual(new_person.exposure_period, None)

test_queue.put(new_person)
self.test_sweep.bind_population(self.pop)
self.test_sweep(self.time)
self.assertEqual(self.cell.person_queue.qsize(), 1)
self.assertListEqual(self.person.secondary_infections_counts, [1])
self.assertEqual(new_person.exposure_period, 1.0)

# Change the additional person to recovered, and assert the queue
# is empty.
Expand All @@ -87,6 +90,7 @@ def test__call__(self, mock_force):
self.test_sweep(self.time)
self.assertTrue(self.cell.person_queue.empty())
self.assertListEqual(self.person.secondary_infections_counts, [1])
self.assertEqual(new_person.exposure_period, 1.0)

def test_no_households(self):
pop_nh = pe.Population() # Population without households
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ def test__call__(self, mock_inf, mock_force):
# ensuring they are added to the infected queue and the infector's
# secondary_infections_counts are incremented.
self.person1.update_status(pe.property.InfectionStatus.InfectMild)
self.person1.infection_start_times = [0.0]
self.person1.secondary_infections_counts = [0]
self.assertEqual(self.new_person.exposure_period, None)
self.place.add_person(self.new_person)
self.cell.person_queue = Queue()
self.test_sweep.bind_population(self.pop)
self.test_sweep(time)
self.assertEqual(self.cell.person_queue.qsize(), 1)
self.assertListEqual(self.person1.secondary_infections_counts, [1])
self.assertEqual(self.new_person.exposure_period, 1.0)

# Change the additional person to recovered, and assert the queue
# is empty.
Expand All @@ -76,6 +79,7 @@ def test__call__(self, mock_inf, mock_force):
self.test_sweep(time)
self.assertTrue(self.cell.person_queue.empty())
self.assertListEqual(self.person1.secondary_infections_counts, [1])
self.assertEqual(self.new_person.exposure_period, 1.0)

# Now test when binomial dist is activated.
mock_inf.return_value = 1
Expand All @@ -86,6 +90,7 @@ def test__call__(self, mock_inf, mock_force):
self.test_sweep(time)
self.assertTrue(self.cell.person_queue.empty())
self.assertListEqual(self.person1.secondary_infections_counts, [1])
self.assertEqual(self.new_person.exposure_period, 1.0)

# Change the additional person to susceptible.
self.new_person.update_status(pe.property.InfectionStatus.Susceptible)
Expand All @@ -95,6 +100,7 @@ def test__call__(self, mock_inf, mock_force):
self.test_sweep(time)
self.assertEqual(self.cell.person_queue.qsize(), 1)
self.assertListEqual(self.person1.secondary_infections_counts, [2])
self.assertEqual(self.new_person.exposure_period, 1.0)


if __name__ == '__main__':
Expand Down
13 changes: 13 additions & 0 deletions pyEpiabm/pyEpiabm/tests/test_unit/test_sweep/test_spatial_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,12 @@ def test__call__(self, mock_inf, mock_foi, mock_poisson, mock_inf_list,
# Assert a basic population
test_pop = self.pop
test_sweep.bind_population(test_pop)
self.infector.infection_start_times = [0.0]
self.assertEqual(self.infectee.exposure_period, None)
test_sweep(time)
self.assertTrue(self.cell_inf.person_queue.empty())
self.assertListEqual(self.infector.secondary_infections_counts, [1])
self.assertEqual(self.infectee.exposure_period, 1.0)

mock_inf.assert_called_once_with(self.cell_inf, time)
mock_foi.assert_called_once_with(self.cell_inf, self.cell_susc,
Expand All @@ -266,26 +269,31 @@ def test__call__(self, mock_inf, mock_foi, mock_poisson, mock_inf_list,

# Change infector's status to infected
self.infector.update_status(InfectionStatus.InfectMild)
self.infector.infection_start_times = [0.0]
test_sweep(time)
self.assertEqual(self.cell_inf.person_queue.qsize(), 0)
self.assertListEqual(self.infector.secondary_infections_counts, [2])
self.assertEqual(self.infectee.exposure_period, 1.0)
Parameters.instance().do_CovidSim = True
self.cell_susc.person_queue = Queue()
test_sweep(time)
self.assertEqual(self.cell_susc.person_queue.qsize(), 1)
self.assertListEqual(self.infector.secondary_infections_counts, [3])
self.assertEqual(self.infectee.exposure_period, 1.0)
# Check when we have an infector but no infectees
self.infectee.update_status(InfectionStatus.Recovered)
self.cell_susc.person_queue = Queue()
test_sweep(time)
self.assertEqual(self.cell_susc.person_queue.qsize(), 0)
self.assertListEqual(self.infector.secondary_infections_counts, [3])
self.assertEqual(self.infectee.exposure_period, 1.0)

# Test parameters break-out clause
Parameters.instance().infection_radius = 0
test_sweep(time)
self.assertEqual(self.cell_susc.person_queue.qsize(), 0)
self.assertListEqual(self.infector.secondary_infections_counts, [3])
self.assertEqual(self.infectee.exposure_period, 1.0)

mock_inf_list.assert_called_with(self.cell_inf, [self.cell_susc], time)
mock_list_covid.assert_called_with(self.infector, [self.cell_susc],
Expand Down Expand Up @@ -323,17 +331,22 @@ def test_do_infection_event(self, mock_random):
fake_infectee = microcell_susc.persons[1]
fake_infectee.update_status(InfectionStatus.Recovered)
actual_infectee = microcell_susc.persons[0]
self.infector.infection_start_times = [0.0]

self.assertTrue(cell_susc.person_queue.empty())
test_sweep.do_infection_event(self.infector, fake_infectee, 1)
self.assertFalse(mock_random.called) # Should have already returned
self.assertTrue(cell_susc.person_queue.empty())
self.assertListEqual(self.infector.secondary_infections_counts, [0])
self.assertEqual(fake_infectee.exposure_period, None)
self.assertEqual(actual_infectee.exposure_period, None)

test_sweep.do_infection_event(self.infector, actual_infectee, 1)
mock_random.assert_called_once()
self.assertEqual(cell_susc.person_queue.qsize(), 1)
self.assertListEqual(self.infector.secondary_infections_counts, [1])
self.assertEqual(fake_infectee.exposure_period, None)
self.assertEqual(actual_infectee.exposure_period, 1.0)


if __name__ == '__main__':
Expand Down

0 comments on commit 2873068

Please sign in to comment.