Skip to content

Make subgroups

SubgroupParams

Class to read and collect Interaction matrix information. Allows for reading of subgroups from generic bins

Source code in june/groups/group/make_subgroups.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
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
193
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
227
228
229
230
231
232
233
234
235
236
class SubgroupParams:
    """Class to read and collect Interaction matrix information. Allows for reading of subgroups from generic bins"""

    # AgeYoungAdult = 18
    # AgeAdult = 18
    # AgeOldAdult = 65

    PossibleLocs = [
        "pub",
        "grocery",
        "cinema",
        "city_transport",
        "inter_city_transport",
        "gym",
        "care_home",
        "student_dorm",
        "boarding_school",
        "university",
        "school",
        "household",
        "company",
        "communal",
        "distribution_center",
        "e_voucher",
        "female_communal",
        "isolation_unit",
        "n_f_distribution_center",
        "pump_latrine",
        "religious",
        "play_group",
        "learning_center",
        "hospital",
        "shelter",
        "informal_work",
        "sexual_encounter",
    ]

    def __init__(self, params=None) -> None:

        if params is None:
            self.params = params
            self.specs = None
        else:
            self.params = params
            self.specs = params.keys()

    def subgroup_bins(self, spec):
        """

        Args:
            spec: 

        """
        return self.params[spec]["bins"]

    def subgroup_type(self, spec):
        """

        Args:
            spec: 

        """
        return self.params[spec]["type"]

    def subgroup_labels(self, spec):
        """

        Args:
            spec: 

        """
        if spec not in self.params.keys():

            if spec not in self.PossibleLocs:
                print(f"{spec} not defined in interaction yaml or defualt options")
                return list(["default"])
            else:
                Bins, Type = get_defaults(spec)
                logger.info(
                    f"{spec} interaction bins not specified. Using default values {Bins}"
                )
                self.params[spec] = {"bins": Bins, "type": Type}

        if (
            "bins" not in self.params[spec].keys()
            or "type" not in self.params[spec].keys()
        ):
            Bins, Type = get_defaults(spec)
            logger.info(
                f"{spec} interaction bins not specified. Using default values {Bins}"
            )
            self.params[spec]["bins"] = Bins
            self.params[spec]["type"] = Type
        elif spec in [
            "learning_center",
            "hospital",
            "shelter",
            "university",
            "school",
            "care_home",
            "student_dorm",
            "boarding_school",
            "household",
            "company",
        ]:
            Bins, Type = get_defaults(spec)
            if self.params[spec]["bins"] != Bins:
                logger.info(f"{spec} interaction bins need default values for methods.")
                self.params[spec]["bins"] = Bins
                self.params[spec]["type"] = Type

        if self.subgroup_type(spec) == "Age":  # Make dummy names for N age bins
            Nbins = len(self.params[spec]["bins"]) - 1
            return list(itertools.islice(self.excel_cols(), Nbins))
        elif self.subgroup_type(spec) == "Discrete":
            return list(self.params[spec]["bins"])  # Already have our names!

    # def kids_indexes(self, spec):
    #     if self.subgroup_type(spec) == "Age": #Make dummy names for N age bins
    #         index = sum(np.array(self.params[spec]["bins"]) < self.AgeAdult)
    #         return np.arange(0, index, 1)
    #     else:
    #         return np.array([]) #Empty list of bin indexes

    # def adults_indexes(self, spec):
    #     if self.subgroup_type(spec) == "Age": #Make dummy names for N age bins
    #         index = sum(np.array(self.params[spec]["bins"]) < self.AgeAdult)
    #         return np.arange(index, len(self.params[spec]["bins"])-1, 1)
    #     else:
    #         return np.array([]) #Empty list of bin indexes

    def excel_cols(self):
        """Generate generic string labels in form ["A", "B", "C", ... , "Z", "AA", "AB", .... ]"""
        n = 1
        while True:
            yield from (
                "".join(group)
                for group in itertools.product(string.ascii_uppercase, repeat=n)
            )
            n += 1

    @classmethod
    def from_disease_config(cls, disease_config: DiseaseConfig) -> "SubgroupParams":
        """Initialise SubgroupParams using data from an existing DiseaseConfig object.

        Args:
            disease_config (DiseaseConfig): The disease-specific configuration object, which already contains the interaction data.

        Returns:
            SubgroupParams: SubgroupParams class instance.

        """

        # Extract the contact matrices from DiseaseConfig
        contact_matrices = disease_config.interaction_manager.contact_matrices

        # Create and return SubgroupParams
        return cls(params=contact_matrices)

excel_cols()

Generate generic string labels in form ["A", "B", "C", ... , "Z", "AA", "AB", .... ]

Source code in june/groups/group/make_subgroups.py
210
211
212
213
214
215
216
217
218
def excel_cols(self):
    """Generate generic string labels in form ["A", "B", "C", ... , "Z", "AA", "AB", .... ]"""
    n = 1
    while True:
        yield from (
            "".join(group)
            for group in itertools.product(string.ascii_uppercase, repeat=n)
        )
        n += 1

from_disease_config(disease_config) classmethod

Initialise SubgroupParams using data from an existing DiseaseConfig object.

Parameters:

Name Type Description Default
disease_config DiseaseConfig

The disease-specific configuration object, which already contains the interaction data.

required

Returns:

Name Type Description
SubgroupParams SubgroupParams

SubgroupParams class instance.

Source code in june/groups/group/make_subgroups.py
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
@classmethod
def from_disease_config(cls, disease_config: DiseaseConfig) -> "SubgroupParams":
    """Initialise SubgroupParams using data from an existing DiseaseConfig object.

    Args:
        disease_config (DiseaseConfig): The disease-specific configuration object, which already contains the interaction data.

    Returns:
        SubgroupParams: SubgroupParams class instance.

    """

    # Extract the contact matrices from DiseaseConfig
    contact_matrices = disease_config.interaction_manager.contact_matrices

    # Create and return SubgroupParams
    return cls(params=contact_matrices)

subgroup_bins(spec)

Parameters:

Name Type Description Default
spec
required
Source code in june/groups/group/make_subgroups.py
125
126
127
128
129
130
131
132
def subgroup_bins(self, spec):
    """

    Args:
        spec: 

    """
    return self.params[spec]["bins"]

subgroup_labels(spec)

Parameters:

Name Type Description Default
spec
required
Source code in june/groups/group/make_subgroups.py
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
193
194
def subgroup_labels(self, spec):
    """

    Args:
        spec: 

    """
    if spec not in self.params.keys():

        if spec not in self.PossibleLocs:
            print(f"{spec} not defined in interaction yaml or defualt options")
            return list(["default"])
        else:
            Bins, Type = get_defaults(spec)
            logger.info(
                f"{spec} interaction bins not specified. Using default values {Bins}"
            )
            self.params[spec] = {"bins": Bins, "type": Type}

    if (
        "bins" not in self.params[spec].keys()
        or "type" not in self.params[spec].keys()
    ):
        Bins, Type = get_defaults(spec)
        logger.info(
            f"{spec} interaction bins not specified. Using default values {Bins}"
        )
        self.params[spec]["bins"] = Bins
        self.params[spec]["type"] = Type
    elif spec in [
        "learning_center",
        "hospital",
        "shelter",
        "university",
        "school",
        "care_home",
        "student_dorm",
        "boarding_school",
        "household",
        "company",
    ]:
        Bins, Type = get_defaults(spec)
        if self.params[spec]["bins"] != Bins:
            logger.info(f"{spec} interaction bins need default values for methods.")
            self.params[spec]["bins"] = Bins
            self.params[spec]["type"] = Type

    if self.subgroup_type(spec) == "Age":  # Make dummy names for N age bins
        Nbins = len(self.params[spec]["bins"]) - 1
        return list(itertools.islice(self.excel_cols(), Nbins))
    elif self.subgroup_type(spec) == "Discrete":
        return list(self.params[spec]["bins"])  # Already have our names!

subgroup_type(spec)

Parameters:

Name Type Description Default
spec
required
Source code in june/groups/group/make_subgroups.py
134
135
136
137
138
139
140
141
def subgroup_type(self, spec):
    """

    Args:
        spec: 

    """
    return self.params[spec]["type"]

get_defaults(spec)

Parameters:

Name Type Description Default
spec
required
Source code in june/groups/group/make_subgroups.py
15
16
17
18
19
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
65
66
67
68
69
70
71
72
73
74
75
76
def get_defaults(spec):
    """

    Args:
        spec: 

    """
    if spec in [
        "pub",
        "grocery",
        "cinema",
        "city_transport",
        "inter_city_transport",
        "gym",
    ]:
        return [0, 100], "Age"

    elif spec in ["care_home"]:
        return ["workers", "residents", "visitors"], "Discrete"

    elif spec in ["student_dorm"]:
        return ["residents"], "Discrete"

    elif spec in ["boarding_school"]:
        return ["residents"], "Discrete"

    elif spec in ["university"]:
        return ["1", "2", "3", "4", "5"], "Discrete"
    elif spec in ["school"]:
        return ["teachers", "students"], "Discrete"
    elif spec in ["household"]:
        return ["kids", "young_adults", "adults", "old_adults"], "Discrete"
    elif spec in ["company"]:
        return ["workers"], "Discrete"
    elif spec in ["sexual_encounter"]:
        return [0, 100], "Age"

    # Cox defaults
    elif spec in [
        "communal",
        "distribution_center",
        "e_voucher",
        "female_communal",
        "isolation_unit",
        "n_f_distribution_center",
        "pump_latrine",
        "religious",
    ]:
        return [0, 18, 60], "Age"
    elif spec in ["play_group"]:
        return [3, 7, 12, 18], "Age"
    elif spec in ["learning_center"]:
        return ["students", "teachers"], "Discrete"
    elif spec in ["hospital"]:
        return ["workers", "patients", "icu_patients"], "Discrete"
    elif spec in ["shelter"]:
        return ["inter", "intra"], "Discrete"
    elif spec in ["informal_work"]:
        return [0, 100], "Age"

    else:
        return ["default"], "Discrete"