1
-
1
+ """"
2
+ This is a test file for Lifestyle Module. It contains a number of checks to ensure everything is running as expected
3
+ """
2
4
import os
3
5
from pathlib import Path
4
6
7
+ import numpy as np
8
+ import pandas as pd
5
9
import pytest
6
10
7
- from tlo import Date , Simulation
11
+ from tlo import Date , DateOffset , Module , Simulation
12
+ from tlo .events import PopulationScopeEventMixin , RegularEvent
8
13
from tlo .methods import demography , enhanced_lifestyle
9
14
15
+ resourcefilepath = Path (os .path .dirname (__file__ )) / '../resources'
10
16
start_date = Date (2010 , 1 , 1 )
11
17
end_date = Date (2012 , 4 , 1 )
12
18
popsize = 10000
@@ -22,14 +28,15 @@ def simulation(seed):
22
28
return sim
23
29
24
30
25
- def __check_properties (df ):
31
+ def check_properties (df ):
26
32
# no one under 15 can be overweight, low exercise, tobacco, excessive alcohol, married
27
33
under15 = df .age_years < 15
28
- assert not (under15 & df .li_bmi > 0 ).any ()
34
+ assert not (under15 & pd . notna ( df .li_bmi ) ).any ()
29
35
assert not (under15 & df .li_low_ex ).any ()
30
36
assert not (under15 & df .li_tob ).any ()
31
37
assert not (under15 & df .li_ex_alc ).any ()
32
38
assert not (under15 & (df .li_mar_stat != 1 )).any ()
39
+
33
40
# education: no one 0-5 should be in education
34
41
assert not ((df .age_years < 5 ) & (df .li_in_ed | (df .li_ed_lev != 1 ))).any ()
35
42
@@ -50,10 +57,80 @@ def __check_properties(df):
50
57
51
58
def test_properties_and_dtypes (simulation ):
52
59
simulation .make_initial_population (n = popsize )
53
- __check_properties (simulation .population .props )
60
+ check_properties (simulation .population .props )
54
61
simulation .simulate (end_date = end_date )
55
- __check_properties (simulation .population .props )
62
+ check_properties (simulation .population .props )
56
63
# check types of columns
57
64
df = simulation .population .props
58
65
orig = simulation .population .new_row
59
66
assert (df .dtypes == orig .dtypes ).all ()
67
+
68
+
69
+ def test_assign_rural_urban_by_district (simulation ):
70
+ """ test linear model integrity in assigning individual rural urban status based on their districts """
71
+ # make an initial population
72
+ simulation .make_initial_population (n = 1 )
73
+
74
+ # Make this individual rural
75
+ df = simulation .population .props
76
+ df .loc [0 , 'is_alive' ] = True
77
+ df .loc [0 , 'is_urban' ] = False
78
+ df .loc [0 , 'district_of_residence' ] = 'Lilongwe'
79
+
80
+ # confirm an individual is rural
81
+ assert not df .loc [0 , 'li_urban' ]
82
+
83
+ # reset district of residence to an urban district(Here we choose a district of residence with 1.0 urban
84
+ # probability i.e Lilongwe City), run the rural urban linear model and check the individual is now urban.
85
+ df .loc [0 , 'district_of_residence' ] = 'Lilongwe City'
86
+ rural_urban_lm = enhanced_lifestyle .LifestyleModels (simulation .modules ['Lifestyle' ]).rural_urban_linear_model ()
87
+ df .loc [df .is_alive , 'li_urban' ] = rural_urban_lm .predict (df .loc [df .is_alive ], rng = np .random )
88
+
89
+ # check an individual is now urban
90
+ assert df .loc [0 , 'li_urban' ]
91
+
92
+
93
+ def test_check_properties_daily_event ():
94
+ """ A test that seeks to test the integrity of lifestyle properties. It contains a dummy module with an event that
95
+ runs daily to ensure properties what they are expected """
96
+ class DummyModule (Module ):
97
+ """ a dummy module for testing lifestyle properties """
98
+ def read_parameters (self , data_folder ):
99
+ pass
100
+
101
+ def initialise_population (self , population ):
102
+ pass
103
+
104
+ def initialise_simulation (self , sim ):
105
+ event = DummyLifestyleEvent (self )
106
+ sim .schedule_event (event , sim .date )
107
+
108
+ def on_birth (self , mother , child ):
109
+ pass
110
+
111
+ class DummyLifestyleEvent (RegularEvent , PopulationScopeEventMixin ):
112
+ """ An event that runs daily to check the integrity of lifestyle properties """
113
+
114
+ def __init__ (self , module ):
115
+ """schedule to run everyday
116
+ """
117
+ self .repeat_months = 1
118
+ self .module = module
119
+ super ().__init__ (module , frequency = DateOffset (days = 1 ))
120
+
121
+ def apply (self , population ):
122
+ """ Apply this event to the population.
123
+ :param population: the current population
124
+ """
125
+ # check lifestyle properties
126
+ check_properties (population .props )
127
+
128
+ # Create simulation:
129
+ sim = Simulation (start_date = start_date )
130
+ sim .register (
131
+ demography .Demography (resourcefilepath = resourcefilepath ),
132
+ enhanced_lifestyle .Lifestyle (resourcefilepath = resourcefilepath ),
133
+ DummyModule ()
134
+ )
135
+ sim .make_initial_population (n = 2000 )
136
+ sim .simulate (end_date = end_date )
0 commit comments