コード例 #1
0
def build_interval_list(features):
    inter = InterLap()
    feature_coords = [[
        feature.start - 1, feature.end - 1,
        [feature.attributes["copy_id"][0], feature]
    ] for feature in features]
    if len(feature_coords) > 0:
        inter.update(feature_coords)
    return inter
コード例 #2
0
    def _find_contacts(self, mob_traces):
        """Find contacts in a given list `mob_traces` of `Visit`s"""
        # Group mobility traces by site
        mob_traces_at_site = defaultdict(list)
        for v in mob_traces:
            mob_traces_at_site[v.site].append(v)

        # dict of dict of list of contacts:
        # i.e. contacts[i][j][k] = "k-th contact from i to j"
        contacts = {i: defaultdict(InterLap) for i in range(self.num_people)}

        # For each site s
        for s in range(self.num_sites):
            if self.verbose:
                print('Checking site ' + str(s + 1) + '/' +
                      str(self.num_sites),
                      end='\r')
            if len(mob_traces_at_site[s]) == 0:
                continue

            # Init the interval overlap matcher
            inter = InterLap()
            inter.update(mob_traces_at_site[s])
            # Match contacts
            for v in mob_traces_at_site[s]:
                v_time = (v.t_from, v.t_to)
                for vo in list(inter.find(other=v_time)):
                    # Ignore contacts with same individual
                    if v.indiv == vo.indiv:
                        continue
                    # Compute contact time
                    c_t_from = max(v.t_from, vo.t_from)
                    c_t_to = min(v.t_to, vo.t_to_shifted)
                    if c_t_to > c_t_from:
                        # Set contact tuple
                        c = Contact(t_from=c_t_from,
                                    t_to=c_t_to,
                                    indiv_i=v.indiv,
                                    indiv_j=vo.indiv,
                                    id_tup=(v.id, vo.id),
                                    site=s,
                                    duration=c_t_to - c_t_from)
                        # Add it to interlap
                        contacts[v.indiv][vo.indiv].update([c])

        return contacts
コード例 #3
0
    def find_contacts_of_indiv(self,
                               indiv,
                               tmin,
                               tmax,
                               tracing=False,
                               p_reveal_visit=1.0):
        """
        Finds all delta-contacts of person 'indiv' with any other individual after time 'tmin'
        and returns them as InterLap object.
        In the simulator, this function is called for `indiv` as infector.
        """

        if tracing is True and self.beacon_config is None:
            # If function is used for contact tracing and there are no beacons, can only trace direct contacts
            extended_time_window = 0
        else:
            # If used for infection simulation or used for tracing with beacons, capture also indirect contacts
            extended_time_window = self.delta

        contacts = InterLap()

        # iterate over all visits of `indiv` intersecting with the interval [tmin, tmax]
        infector_traces = self.mob_traces_by_indiv[indiv].find(
            (tmin, tmax if (tmax is not None) else np.inf))

        for inf_visit in infector_traces:

            # coin flip of whether infector `indiv` reveals their visit
            if tracing is True and np.random.uniform(
                    low=0.0, high=1.0) > p_reveal_visit:
                continue

            # find all contacts of `indiv` by querying visits of
            # other individuals during visit time of `indiv` at the same site
            # (including delta-contacts; if beacon_cache=0, delta-contacts get filtered out below)
            inf_visit_time = (inf_visit.t_from, inf_visit.t_to_shifted)
            concurrent_site_traces = self.mob_traces_by_site[
                inf_visit.site].find(inf_visit_time)

            for visit in concurrent_site_traces:
                # ignore visits of `indiv` since it is not a contact
                if visit.indiv == inf_visit.indiv:
                    continue

                # ignore if begin of visit is after tmax
                # this can happen if inf_visit starts just before tmax but continues way beyond tmax
                if visit.t_from > tmax:
                    continue

                # Compute contact time
                c_t_from = max(visit.t_from, inf_visit.t_from)
                c_t_to = min(visit.t_to, inf_visit.t_to + extended_time_window)
                c_t_to_direct = min(visit.t_to, inf_visit.t_to)  # only direct

                if c_t_to > c_t_from and c_t_to > tmin:
                    c = Contact(t_from=c_t_from,
                                t_to=c_t_to,
                                indiv_i=visit.indiv,
                                indiv_j=inf_visit.indiv,
                                id_tup=(visit.id, inf_visit.id),
                                site=inf_visit.site,
                                duration=c_t_to - c_t_from,
                                t_to_direct=c_t_to_direct)
                    contacts.update([c])

        return contacts
コード例 #4
0
ファイル: measures.py プロジェクト: covid19-model/simulator
class UpperBoundCasesBetaMultiplier(BetaMultiplierMeasure):

    def __init__(self, t_window, beta_multiplier, max_pos_tests_per_week_per_100k, intervention_times=None, init_active=False):
        """
        Additional parameters:
        ----------------------
        max_pos_test_per_week : int
            If the number of positive tests per week exceeds this number the measure becomes active
        intervention_times : list of floats
            List of points in time at which interventions can be changed. If 'None' interventions can be changed at any time
        init_active : bool
            If true measure is active in the first week of the simulation when there are no test counts yet
        """

        super().__init__(t_window, beta_multiplier)
        self.max_pos_tests_per_week_per_100k = max_pos_tests_per_week_per_100k
        self.intervention_times = intervention_times
        self.intervention_history = InterLap()
        if init_active:
            self.intervention_history.update([(t_window.left, t_window.left + 7 * 24 - EPS, True)])

    def init_run(self, n_people, n_visits):
        self.scaled_test_threshold = self.max_pos_tests_per_week_per_100k / 100000 * n_people
        self._is_init = True

    @enforce_init_run
    def _is_measure_active(self, t, t_pos_tests):
        # If measures can only become active at 'intervention_times'
        if self.intervention_times is not None:
            # Find largest 'time' in intervention_times s.t. t > time
            intervention_times = np.asarray(self.intervention_times)
            idx = np.where(t - intervention_times > 0, t - intervention_times, np.inf).argmin()
            t = intervention_times[idx]

        t_in_history = list(self.intervention_history.find((t, t)))
        if t_in_history:
            is_active = t_in_history[0][2]
        else:
            is_active = self._are_cases_above_threshold(t, t_pos_tests)
            if is_active:
                self.intervention_history.update([(t, t+7*24 - EPS, True)])
        return is_active

    @enforce_init_run
    def _are_cases_above_threshold(self, t, t_pos_tests):
        # Count positive tests in last 7 days from last intervention time
        tmin = t - 7 * 24
        num_pos_tests = np.sum(np.greater(t_pos_tests, tmin) * np.less(t_pos_tests, t))
        is_above_threshold = num_pos_tests > self.scaled_test_threshold
        return is_above_threshold

    @enforce_init_run
    def beta_factor(self, *, typ, t, t_pos_tests):
        """Returns the multiplicative factor for site type `typ` at time `t`. The
        factor is one if `t` is not in the active time window of the measure.
        """
        if not self._in_window(t):
            return 1.0

        is_measure_active = self._is_measure_active(t, t_pos_tests)
        return self.beta_multiplier[typ] if is_measure_active else 1.0
コード例 #5
0
ファイル: measures.py プロジェクト: covid19-model/simulator
class UpperBoundCasesSocialDistancing(SocialDistancingForAllMeasure):

    def __init__(self, t_window, p_stay_home, max_pos_tests_per_week_per_100k, intervention_times=None, init_active=False):
        """
        Additional parameters:
        ----------------------
        max_pos_test_per_week : int
            If the number of positive tests per week exceeds this number the measure becomes active
        intervention_times : list of floats
            List of points in time at which measures can become active. If 'None' measures can be changed at any time
        """

        super().__init__(t_window, p_stay_home)
        self.max_pos_tests_per_week_per_100k = max_pos_tests_per_week_per_100k
        self.intervention_times = intervention_times
        self.intervention_history = InterLap()
        if init_active:
            self.intervention_history.update([(t_window.left, t_window.left + 7 * 24 - EPS, True)])

    def init_run(self, n_people, n_visits):
        super().init_run(n_people, n_visits)
        self.scaled_test_threshold = self.max_pos_tests_per_week_per_100k / 100000 * n_people

    def _is_measure_active(self, t, t_pos_tests):
        # If measures can only become active at 'intervention_times'
        if self.intervention_times is not None:
            # Find largest 'time' in intervention_times s.t. t > time
            intervention_times = np.asarray(self.intervention_times)
            idx = np.where(t - intervention_times > 0, t - intervention_times, np.inf).argmin()
            t = intervention_times[idx]

        t_in_history = list(self.intervention_history.find((t, t)))
        if t_in_history:
            is_active = t_in_history[0][2]
        else:
            is_active = self._are_cases_above_threshold(t, t_pos_tests)
            if is_active:
                self.intervention_history.update([(t, t + 7 * 24 - EPS, True)])
        return is_active

    def _are_cases_above_threshold(self, t, t_pos_tests):
        # Count positive tests in last 7 days from last intervention time
        tmin = t - 7 * 24
        num_pos_tests = np.sum(np.greater(t_pos_tests, tmin) * np.less(t_pos_tests, t))
        is_above_threshold = num_pos_tests > self.scaled_test_threshold
        return is_above_threshold

    @enforce_init_run
    def is_contained(self, *, j, j_visit_id, t, t_pos_tests):
        """Indicate if individual `j` respects measure for visit `j_visit_id`
        """
        if not self._in_window(t):
            return False

        is_home_now = self.bernoulli_stay_home[j, j_visit_id]
        return is_home_now and self._is_measure_active(t, t_pos_tests)

    @enforce_init_run
    def is_contained_prob(self, *, j, t, t_pos_tests):
        """Returns probability of containment for individual `j` at time `t`
        """
        if not self._in_window(t):
            return 0.0

        if self._is_measure_active(t, t_pos_tests):
            return self.p_stay_home
        return 0.0
コード例 #6
0
    def from_json(fp, compute_contacts=True):
        """
        Reach the from `fp` (.read()-supporting file-like object) that is
        expected to be JSON-formated from the `to_json` file.

        Parameters
        ----------
        fp : object
            The input .read()-supporting file-like object
        compute_contacts : bool (optional, default: True)
            Indicate if contacts should be computed from the mobility traces.
            If True, then any `contact` key in `fp` will be ignored.
            If False, `fp` must have a contact` key.

        Return
        ------
        sim : MobilitySimulator
            The loaded object
        """
        # Read file into json dict
        data = json.loads(fp.read())

        # Init object
        init_attrs = [
            'num_people', 'num_sites', 'delta', 'mob_mean', 'dur_mean',
            'verbose'
        ]
        obj = MobilitySimulator(**{attr: data[attr] for attr in init_attrs})

        # Set np.ndarray attributes
        for attr in ['home_loc', 'site_loc']:
            setattr(obj, attr, np.array(data[attr]))

        # Set list attributes
        for attr in ['visit_counts']:
            setattr(obj, attr, list(data[attr]))

        # Set `mob_traces` attribute into dict:defaultdict:InterLap
        setattr(obj, 'mob_traces',
                {i: defaultdict(InterLap)
                 for i in range(obj.num_people)})
        for indiv, traces_i in data['mob_traces'].items():
            indiv = int(indiv)  # JSON does not support int keys
            for site, visit_list in traces_i.items():
                site = int(site)  # JSON does not support int keys
                if len(visit_list) > 0:
                    inter = InterLap()
                    inter.update(list(map(lambda t: Visit(*t), visit_list)))
                    obj.mob_traces[indiv][site] = inter

        # Set `contacts` attribute into dict:defaultdict:InterLap
        if compute_contacts:  # Compute from `mob_traces`
            all_mob_traces = []
            for i, traces_i in obj.mob_traces.items():
                for j, inter in traces_i.items():
                    all_mob_traces.extend(inter._iset)
            # Compute contacts from mobility traces
            obj.contacts = obj._find_contacts(all_mob_traces)
        else:  # Load from file
            setattr(obj, 'contacts',
                    {i: defaultdict(InterLap)
                     for i in range(obj.num_people)})
            for indiv_i, contacts_i in data['contacts'].items():
                indiv_i = int(indiv_i)  # JSON does not support int keys
                for indiv_j, contact_list in contacts_i.items():
                    indiv_j = int(indiv_j)  # JSON does not support int keys
                    if len(contact_list) > 0:
                        inter = InterLap()
                        inter.update(
                            list(map(lambda t: Contact(*t), contact_list)))
                        obj.contacts[indiv_i][indiv_j] = inter

        return obj
コード例 #7
0
    def _find_mob_trace_overlaps(self, sites, mob_traces_at_site,
                                 infector_mob_traces_at_site, tmin,
                                 for_all_individuals):

        # decide way of storing depending on way the function is used (all or individual)
        # FIXME: this could be done in a cleaner way by calling this function several times in `_find_contacts`
        if for_all_individuals:
            # dict of dict of list of contacts:
            # i.e. contacts[i][j][k] = "k-th contact from i to j"
            contacts = {
                i: defaultdict(InterLap)
                for i in range(self.num_people)
            }
        else:
            contacts = InterLap()

        if self.verbose and for_all_individuals:
            print()  # otherwise jupyter notebook looks ugly

        for s in sites:
            if self.verbose and for_all_individuals:
                print('Checking site ' + str(s + 1) + '/' + str(len(sites)),
                      end='\r')
            if len(mob_traces_at_site[s]) == 0:
                continue

            # Init the interval overlap matcher
            inter = InterLap()
            inter.update(mob_traces_at_site[s])

            # Match contacts
            # Iterate over each visit of the infector at site s
            for v_inf in infector_mob_traces_at_site[s]:

                # Skip if delta-contact ends before `tmin`
                if v_inf.t_to_shifted > tmin:

                    v_time = (v_inf.t_from, v_inf.t_to_shifted)

                    # Find any othe person that had overlap with this visit
                    for v in list(inter.find(other=v_time)):

                        # Ignore contacts with same individual
                        if v.indiv == v_inf.indiv:
                            continue

                        # Compute contact time
                        c_t_from = max(v.t_from, v_inf.t_from)
                        c_t_to = min(v.t_to, v_inf.t_to_shifted)
                        if c_t_to > c_t_from and c_t_to > tmin:

                            # Init contact tuple
                            # Note 1: Contact always considers delta overlap for `indiv_j`
                            # (i.e. for `indiv_j` being the infector)
                            # Note 2: Contact contains the delta-extended visit of `indiv_j`
                            # (i.e. there is a `Contact` even when `indiv_j` never overlapped physically with `indiv_i`)
                            # (i.e. need to adjust for that in dY_i integral)
                            c = Contact(t_from=c_t_from,
                                        t_to=c_t_to,
                                        indiv_i=v.indiv,
                                        indiv_j=v_inf.indiv,
                                        id_tup=(v.id, v_inf.id),
                                        site=s,
                                        duration=c_t_to - c_t_from)

                            # Add it to interlap
                            if for_all_individuals:
                                # Dictionary of all contacts
                                contacts[v.indiv][v_inf.indiv].update([c])
                            else:
                                # All contacts of (infector) 'indiv' only
                                contacts.update([c])
        return contacts
コード例 #8
0
    #irange = interval()
    with open(args['-r'], 'r') as inf:
        for line in inf:
            line = line.strip()
            if line:
                ss = line.split()
                if not (ss[0] in itvMap):
                    itvMap[ss[0]] = Interval()

                itvMap[ss[0]].add([(float(ss[1]), float(ss[2]))
                                   ])  # auto merge region.

    checkMap = {}
    for k, v in itvMap.items():
        inter = InterLap()
        inter.update(v._as_tuples(v))
        checkMap[k] = inter  # convert inverval to trees.
        # for i in inter:
        #     print(i)
#-------------------------------------------------
# print(checkMap)
    for line in sys.stdin:
        line = line.strip()
        if line:
            if title:
                sys.stdout.write('%s\n' % (line))
                title = False
                continue

            ss = line.split()
            try:
コード例 #9
0
    # from interval import interval
    from interlap import InterLap
    from interlap import Interval  # This class can auto merge overlapped regions.
    # interlap really significantly increased the search speed.

    irange = Interval()
    with open(args['-r'], 'r') as inf:
        for line in inf:
            line = line.strip()
            if line:
                ss = line.split()
                irange.add([(float(ss[0]), float(ss[1]))])

    inter = InterLap()
    inter.update(irange._as_tuples(irange))
    #-------------------------------------------------
    for line in sys.stdin:
        line = line.strip()
        if line:
            ss = line.split()
            try:
                v = int(ss[colValue])
                if keep:
                    if inter.__contains__((v, v)):
                        sys.stdout.write('%s\n' % (line))
                else:
                    if not (inter.__contains__((v, v))):
                        sys.stdout.write('%s\n' % (line))

            except ValueError: