Skip to content

Clustered infection seed

ClusteredInfectionSeed

Bases: InfectionSeed

Source code in june/epidemiology/infection_seed/clustered_infection_seed.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
 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
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
class ClusteredInfectionSeed(InfectionSeed):
    """ """
    def __init__(
        self,
        world: "World",
        infection_selector: InfectionSelector,
        daily_cases_per_capita_per_age_per_region: pd.DataFrame,
        seed_past_infections: bool = True,
        seed_strength=1.0,
        account_secondary_infections=False,
    ):
        super().__init__(
            world=world,
            infection_selector=infection_selector,
            daily_cases_per_capita_per_age_per_region=daily_cases_per_capita_per_age_per_region,
            seed_past_infections=seed_past_infections,
            seed_strength=seed_strength,
            account_secondary_infections=account_secondary_infections,
        )

    def get_total_people_to_infect(self, people, cases_per_capita_per_age):
        """

        Args:
            people: 
            cases_per_capita_per_age: 

        """
        people_by_age = defaultdict(int)
        for person in people:
            people_by_age[person.age] += 1
        total = sum(
            [
                people_by_age[age] * cases_per_capita_per_age.loc[age]
                for age in people_by_age
            ]
        )
        ret = int(total)
        ret += int(random() < (total - ret))
        return ret

    def get_household_score(self, household, age_distribution):
        """

        Args:
            household: 
            age_distribution: 

        """
        if len(household.residents) == 0:
            return 0
        ret = 0
        for resident in household.residents:
            ret += age_distribution.loc[resident.age]
        return ret / np.sqrt(len(household.residents))

    def infect_super_area(
        self, super_area, cases_per_capita_per_age, time, record=None
    ):
        """

        Args:
            super_area: 
            cases_per_capita_per_age: 
            time: 
            record: (Default value = None)

        """

        infection_id = self.infection_selector.infection_class.infection_id()
        people = super_area.people
        total_to_infect = self.get_total_people_to_infect(
            people=people, cases_per_capita_per_age=cases_per_capita_per_age
        )

        # Early exit if nothing to infect
        if total_to_infect <= 0:
            return

        # Early exit if no households or people
        if len(super_area.households) == 0 or len(people) == 0:
            return

        # Handle division by zero
        cases_sum = cases_per_capita_per_age.sum()
        if cases_sum == 0:
            return

        age_distribution = cases_per_capita_per_age / cases_sum
        households = np.array(super_area.households)
        scores = [self.get_household_score(h, age_distribution) for h in households]

        # Early exit if all scores are zero
        if sum(scores) == 0:
            return

        cum_scores = np.cumsum(scores)
        seeded_households = set()
        attempts = 0
        max_attempts = len(households) * 10  # Prevent infinite loops

        while total_to_infect > 0 and attempts < max_attempts:
            attempts += 1
            num = random() * cum_scores[-1]
            idx = np.searchsorted(cum_scores, num)
            household = households[idx]

            if household.id in seeded_households:
                continue

            # Mark household as attempted regardless of outcome
            seeded_households.add(household.id)

            # Try to infect household members
            infected_in_household = False
            for person in household.residents:
                if person.immunity.get_susceptibility(infection_id) > 0:
                    self.infect_person(person=person, time=time, record=record)
                    if time < 0:
                        self.bring_infection_up_to_date(
                            person=person, time_from_infection=-time, record=record
                        )
                    total_to_infect -= 1
                    infected_in_household = True
                    if total_to_infect <= 0:
                        return

            # If we've tried all households and can't infect anyone, exit
            if len(seeded_households) >= len(households):
                return

get_household_score(household, age_distribution)

Parameters:

Name Type Description Default
household
required
age_distribution
required
Source code in june/epidemiology/infection_seed/clustered_infection_seed.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def get_household_score(self, household, age_distribution):
    """

    Args:
        household: 
        age_distribution: 

    """
    if len(household.residents) == 0:
        return 0
    ret = 0
    for resident in household.residents:
        ret += age_distribution.loc[resident.age]
    return ret / np.sqrt(len(household.residents))

get_total_people_to_infect(people, cases_per_capita_per_age)

Parameters:

Name Type Description Default
people
required
cases_per_capita_per_age
required
Source code in june/epidemiology/infection_seed/clustered_infection_seed.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def get_total_people_to_infect(self, people, cases_per_capita_per_age):
    """

    Args:
        people: 
        cases_per_capita_per_age: 

    """
    people_by_age = defaultdict(int)
    for person in people:
        people_by_age[person.age] += 1
    total = sum(
        [
            people_by_age[age] * cases_per_capita_per_age.loc[age]
            for age in people_by_age
        ]
    )
    ret = int(total)
    ret += int(random() < (total - ret))
    return ret

infect_super_area(super_area, cases_per_capita_per_age, time, record=None)

Parameters:

Name Type Description Default
super_area
required
cases_per_capita_per_age
required
time
required
record

(Default value = None)

None
Source code in june/epidemiology/infection_seed/clustered_infection_seed.py
 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
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
def infect_super_area(
    self, super_area, cases_per_capita_per_age, time, record=None
):
    """

    Args:
        super_area: 
        cases_per_capita_per_age: 
        time: 
        record: (Default value = None)

    """

    infection_id = self.infection_selector.infection_class.infection_id()
    people = super_area.people
    total_to_infect = self.get_total_people_to_infect(
        people=people, cases_per_capita_per_age=cases_per_capita_per_age
    )

    # Early exit if nothing to infect
    if total_to_infect <= 0:
        return

    # Early exit if no households or people
    if len(super_area.households) == 0 or len(people) == 0:
        return

    # Handle division by zero
    cases_sum = cases_per_capita_per_age.sum()
    if cases_sum == 0:
        return

    age_distribution = cases_per_capita_per_age / cases_sum
    households = np.array(super_area.households)
    scores = [self.get_household_score(h, age_distribution) for h in households]

    # Early exit if all scores are zero
    if sum(scores) == 0:
        return

    cum_scores = np.cumsum(scores)
    seeded_households = set()
    attempts = 0
    max_attempts = len(households) * 10  # Prevent infinite loops

    while total_to_infect > 0 and attempts < max_attempts:
        attempts += 1
        num = random() * cum_scores[-1]
        idx = np.searchsorted(cum_scores, num)
        household = households[idx]

        if household.id in seeded_households:
            continue

        # Mark household as attempted regardless of outcome
        seeded_households.add(household.id)

        # Try to infect household members
        infected_in_household = False
        for person in household.residents:
            if person.immunity.get_susceptibility(infection_id) > 0:
                self.infect_person(person=person, time=time, record=record)
                if time < 0:
                    self.bring_infection_up_to_date(
                        person=person, time_from_infection=-time, record=record
                    )
                total_to_infect -= 1
                infected_in_household = True
                if total_to_infect <= 0:
                    return

        # If we've tried all households and can't infect anyone, exit
        if len(seeded_households) >= len(households):
            return