Skip to content

Leisure policies

ChangeLeisureProbability

Bases: LeisurePolicy

Source code in june/policy/leisure_policies.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
class ChangeLeisureProbability(LeisurePolicy):
    """ """
    policy_subtype = "change_leisure_probability"

    def __init__(
        self,
        start_time: str,
        end_time: str,
        activity_reductions: Dict[str, Dict[str, Dict[str, float]]],
    ):
        """
        Changes the probability of the specified leisure activities.

        Parameters
        ----------
        - start_time : starting time of the policy.
        - end_time : end time of the policy.
        - leisure_activities_probabilities : dictionary where the first key is an age range, and the second  a
            number with the new probability for the activity in that age. Example:
            * leisure_activities_probabilities = {"pubs" : {"men" :{"0-50" : 0.5, "50-99" : 0.2}, "women" : {"0-70" : 0.2, "71-99" : 0.8}}}
        """
        super().__init__(start_time, end_time)
        self.activity_reductions = self._read_activity_reductions(activity_reductions)

    def _read_activity_reductions(self, activity_reductions):
        """

        Args:
            activity_reductions: 

        """
        ret = {}
        day_types = ["weekday", "weekend"]
        sexes = ["male", "female"]
        _sex_t = {"male": "m", "female": "f"}
        for activity, pp in activity_reductions.items():
            ret[activity] = {}
            ret[activity]["weekday"] = {}
            ret[activity]["weekend"] = {}
            for first_entry in pp:
                if first_entry in ["weekday", "weekend"]:
                    day_type = first_entry
                    if "both_sexes" in pp[day_type]:
                        for sex in sexes:
                            june_sex = _sex_t[sex]
                            probs = parse_age_probabilities(
                                activity_reductions[activity][day_type]["both_sexes"]
                            )
                            ret[activity][day_type][june_sex] = probs
                    else:
                        for sex in sexes:
                            june_sex = _sex_t[sex]
                            probs = parse_age_probabilities(
                                activity_reductions[activity][day_type][sex]
                            )
                            ret[activity][day_type][june_sex] = probs
                elif first_entry == "any" or first_entry in ["male", "female"]:
                    for sex in sexes:
                        june_sex = _sex_t[sex]
                        probs = parse_age_probabilities(
                            activity_reductions[activity][sex]
                        )
                        for day_type in day_types:
                            ret[activity][day_type][june_sex] = probs
                elif first_entry == "both_sexes":
                    for sex in sexes:
                        june_sex = _sex_t[sex]
                        probs = parse_age_probabilities(
                            activity_reductions[activity]["both_sexes"]
                        )
                        for day_type in day_types:
                            ret[activity][day_type][june_sex] = probs
                else:
                    for day_type in day_types:
                        for sex in sexes:
                            june_sex = _sex_t[sex]
                            ret[activity][day_type][june_sex] = parse_age_probabilities(
                                activity_reductions[activity][day_type][sex]
                            )
        return ret

    def apply(self, leisure: Leisure):
        """

        Args:
            leisure (Leisure): 

        """
        return self.activity_reductions

__init__(start_time, end_time, activity_reductions)

Changes the probability of the specified leisure activities.

Parameters
  • start_time : starting time of the policy.
  • end_time : end time of the policy.
  • leisure_activities_probabilities : dictionary where the first key is an age range, and the second a number with the new probability for the activity in that age. Example:
    • leisure_activities_probabilities = {"pubs" : {"men" :{"0-50" : 0.5, "50-99" : 0.2}, "women" : {"0-70" : 0.2, "71-99" : 0.8}}}
Source code in june/policy/leisure_policies.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def __init__(
    self,
    start_time: str,
    end_time: str,
    activity_reductions: Dict[str, Dict[str, Dict[str, float]]],
):
    """
    Changes the probability of the specified leisure activities.

    Parameters
    ----------
    - start_time : starting time of the policy.
    - end_time : end time of the policy.
    - leisure_activities_probabilities : dictionary where the first key is an age range, and the second  a
        number with the new probability for the activity in that age. Example:
        * leisure_activities_probabilities = {"pubs" : {"men" :{"0-50" : 0.5, "50-99" : 0.2}, "women" : {"0-70" : 0.2, "71-99" : 0.8}}}
    """
    super().__init__(start_time, end_time)
    self.activity_reductions = self._read_activity_reductions(activity_reductions)

apply(leisure)

Parameters:

Name Type Description Default
leisure Leisure
required
Source code in june/policy/leisure_policies.py
185
186
187
188
189
190
191
192
def apply(self, leisure: Leisure):
    """

    Args:
        leisure (Leisure): 

    """
    return self.activity_reductions

ChangeVisitsProbability

Bases: LeisurePolicy

Source code in june/policy/leisure_policies.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
class ChangeVisitsProbability(LeisurePolicy):
    """ """
    policy_subtype = "change_visits_probability"

    def __init__(
        self,
        start_time: str,
        end_time: str,
        new_residence_type_probabilities: Dict[str, float],
    ):
        """
        Changes the probability of the specified leisure activities.

        Parameters
        ----------
        - start_time : starting time of the policy.
        - end_time : end time of the policy.
        - new_residence_type_probabilities
            new probabilities for residence visits splits, eg, {"household" : 0.8, "care_home" : 0.2}
        """
        super().__init__(start_time, end_time)
        self.policy_reductions = new_residence_type_probabilities

    def apply(self, leisure: Leisure):
        """

        Args:
            leisure (Leisure): 

        """
        leisure.leisure_distributors[
            "residence_visits"
        ].policy_reductions = self.policy_reductions

__init__(start_time, end_time, new_residence_type_probabilities)

Changes the probability of the specified leisure activities.

Parameters
  • start_time : starting time of the policy.
  • end_time : end time of the policy.
  • new_residence_type_probabilities new probabilities for residence visits splits, eg, {"household" : 0.8, "care_home" : 0.2}
Source code in june/policy/leisure_policies.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
def __init__(
    self,
    start_time: str,
    end_time: str,
    new_residence_type_probabilities: Dict[str, float],
):
    """
    Changes the probability of the specified leisure activities.

    Parameters
    ----------
    - start_time : starting time of the policy.
    - end_time : end time of the policy.
    - new_residence_type_probabilities
        new probabilities for residence visits splits, eg, {"household" : 0.8, "care_home" : 0.2}
    """
    super().__init__(start_time, end_time)
    self.policy_reductions = new_residence_type_probabilities

apply(leisure)

Parameters:

Name Type Description Default
leisure Leisure
required
Source code in june/policy/leisure_policies.py
217
218
219
220
221
222
223
224
225
226
def apply(self, leisure: Leisure):
    """

    Args:
        leisure (Leisure): 

    """
    leisure.leisure_distributors[
        "residence_visits"
    ].policy_reductions = self.policy_reductions

CloseLeisureVenue

Bases: LeisurePolicy

Source code in june/policy/leisure_policies.py
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
class CloseLeisureVenue(LeisurePolicy):
    """ """

    policy_subtype = "close_venues"

    def __init__(
        self,
        start_time: Union[str, datetime.datetime],
        end_time: Union[str, datetime.datetime],
        venues_to_close=("cinemas", "groceries"),
    ):
        """
        Template for policies that will close types of leisure venues

        Parameters
        ----------
        start_time:
            date at which to start applying the policy
        end_time:
            date from which the policy won't apply
        venues_to_close:
            list of leisure venues that will close
        """

        super().__init__(start_time, end_time)
        self.venues_to_close = venues_to_close

    def apply(self, leisure: Leisure):
        """

        Args:
            leisure (Leisure): 

        """
        for region in leisure.regions:
            for venue in self.venues_to_close:
                region.policy["global_closed_venues"].add(venue)

__init__(start_time, end_time, venues_to_close=('cinemas', 'groceries'))

Template for policies that will close types of leisure venues

Parameters

start_time: date at which to start applying the policy end_time: date from which the policy won't apply venues_to_close: list of leisure venues that will close

Source code in june/policy/leisure_policies.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def __init__(
    self,
    start_time: Union[str, datetime.datetime],
    end_time: Union[str, datetime.datetime],
    venues_to_close=("cinemas", "groceries"),
):
    """
    Template for policies that will close types of leisure venues

    Parameters
    ----------
    start_time:
        date at which to start applying the policy
    end_time:
        date from which the policy won't apply
    venues_to_close:
        list of leisure venues that will close
    """

    super().__init__(start_time, end_time)
    self.venues_to_close = venues_to_close

apply(leisure)

Parameters:

Name Type Description Default
leisure Leisure
required
Source code in june/policy/leisure_policies.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
def apply(self, leisure: Leisure):
    """

    Args:
        leisure (Leisure): 

    """
    for region in leisure.regions:
        for venue in self.venues_to_close:
            region.policy["global_closed_venues"].add(venue)

LeisurePolicies

Bases: PolicyCollection

Source code in june/policy/leisure_policies.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class LeisurePolicies(PolicyCollection):
    """ """
    policy_type = "leisure"

    def apply(self, date: datetime, leisure: Leisure):
        """Applies all the leisure policies. Each Leisure policy will change the probability of
        doing a certain leisure activity. For instance, closing Pubs sets the probability of
        going to the Pub to zero. We store a dictionary with the relative reductions in leisure
        probabilities per activity, and this dictionary is then looked at by the leisure module.

        This is very similar to how we deal with social distancing / mask wearing policies.

        Args:
            date (datetime): 
            leisure (Leisure): 

        """

        # Initialise regions
        for region in leisure.regions:
            region.policy["global_closed_venues"] = set()

        # Initialise policy reductions
        leisure.policy_reductions = {}

        # Initialise residence visits if applicable
        if "residence_visits" in leisure.leisure_distributors:
            leisure.leisure_distributors["residence_visits"].policy_reductions = {}

        change_leisure_probability_policies_counter = 0

        # Apply active policies
        for policy in self.get_active(date):

            if policy.policy_subtype == "change_leisure_probability":
                change_leisure_probability_policies_counter += 1
                if change_leisure_probability_policies_counter > 1:
                    raise ValueError(
                        "Having more than one change leisure probability policy"
                        "active is not supported."
                    )
                # Apply the policy and update leisure policy reductions
                leisure.policy_reductions = policy.apply(leisure=leisure)
            else:
                policy.apply(leisure=leisure)

apply(date, leisure)

Applies all the leisure policies. Each Leisure policy will change the probability of doing a certain leisure activity. For instance, closing Pubs sets the probability of going to the Pub to zero. We store a dictionary with the relative reductions in leisure probabilities per activity, and this dictionary is then looked at by the leisure module.

This is very similar to how we deal with social distancing / mask wearing policies.

Parameters:

Name Type Description Default
date datetime
required
leisure Leisure
required
Source code in june/policy/leisure_policies.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def apply(self, date: datetime, leisure: Leisure):
    """Applies all the leisure policies. Each Leisure policy will change the probability of
    doing a certain leisure activity. For instance, closing Pubs sets the probability of
    going to the Pub to zero. We store a dictionary with the relative reductions in leisure
    probabilities per activity, and this dictionary is then looked at by the leisure module.

    This is very similar to how we deal with social distancing / mask wearing policies.

    Args:
        date (datetime): 
        leisure (Leisure): 

    """

    # Initialise regions
    for region in leisure.regions:
        region.policy["global_closed_venues"] = set()

    # Initialise policy reductions
    leisure.policy_reductions = {}

    # Initialise residence visits if applicable
    if "residence_visits" in leisure.leisure_distributors:
        leisure.leisure_distributors["residence_visits"].policy_reductions = {}

    change_leisure_probability_policies_counter = 0

    # Apply active policies
    for policy in self.get_active(date):

        if policy.policy_subtype == "change_leisure_probability":
            change_leisure_probability_policies_counter += 1
            if change_leisure_probability_policies_counter > 1:
                raise ValueError(
                    "Having more than one change leisure probability policy"
                    "active is not supported."
                )
            # Apply the policy and update leisure policy reductions
            leisure.policy_reductions = policy.apply(leisure=leisure)
        else:
            policy.apply(leisure=leisure)

LeisurePolicy

Bases: Policy

Source code in june/policy/leisure_policies.py
 8
 9
10
11
12
13
14
15
16
17
18
class LeisurePolicy(Policy):
    """ """
    policy_type = "leisure"

    def __init__(
        self,
        start_time: Union[str, datetime.datetime],
        end_time: Union[str, datetime.datetime],
    ):
        super().__init__(start_time, end_time)
        self.policy_type = "leisure"