Ejemplo n.º 1
0
 def _get_features(self,
                   ew,
                   signal_to_truth_shift=0,
                   valid=False,
                   mask=np.ones((10), dtype=bool)):
     X = np.zeros((1, 10))
     i = self.ew2i[ew]
     X[0, 0] = 1
     for lag in range(3):
         if valid and not self.valid[i - lag][lag]:
             w = self.i2ew[i - lag]
             raise Exception('missing unstable wILI (ew=%d|lag=%d)' %
                             (w, lag))
         try:
             X[0, 1 + lag] = np.log(
                 np.maximum(
                     0.01,
                     self.data[i - lag - signal_to_truth_shift]['stable']))
         except Exception:
             X[0, 1 + lag] = np.nan
     for holiday in range(4):
         if EW.split_epiweek(EW.add_epiweeks(ew, holiday))[1] == 1:
             X[0, 4 + holiday] = 1
     y, w = EW.split_epiweek(ew)
     N = EW.get_num_weeks(y)
     offset = np.pi * 2 * w / N
     X[0, 8] = np.sin(offset)
     X[0, 9] = np.cos(offset)
     # todo linear time trend covariate?
     return X[:, mask]
Ejemplo n.º 2
0
 def from_epiweek(year, week):
     if year < 1 or week < 1 or week > get_num_weeks(year):
         raise Exception('invalid year or week')
     date = EpiDate(year, 7, 1)
     while date.get_ew_week() < week:
         date = date.add_weeks(+1)
     while date.get_ew_week() > week:
         date = date.add_weeks(-1)
     while date.get_day_of_week() < 3:
         date = date.add_days(+1)
     while date.get_day_of_week() > 3:
         date = date.add_days(-1)
     return date
Ejemplo n.º 3
0
 def _get_features(self, ew, valid=True):
   X = np.zeros((1, 7))
   i = self.ew2i[ew]
   X[0, 0] = 1
   for holiday in range(4):
     if EW.split_epiweek(EW.add_epiweeks(ew, holiday))[1] == 1:
       X[0, 1 + holiday] = 1
   y, w = EW.split_epiweek(ew)
   N = EW.get_num_weeks(y)
   offset = np.pi * 2 * w / N
   X[0, 5] = np.sin(offset)
   X[0, 6] = np.cos(offset)
   # todo linear time trend covariate?
   return X
Ejemplo n.º 4
0
 def _get_features(self, ew, valid=True):
   X = np.zeros((1, 10))
   i = self.ew2i[ew]
   X[0, 0] = 1
   for lag in range(3):
     if valid and not self.valid[i - lag][lag]:
       w = self.i2ew[i - lag]
       raise Exception('missing unstable %s (ew=%d|lag=%d)' % (self.target, w, lag))
     X[0, 1 + lag] = self.data[i - lag][lag]
   for holiday in range(4):
     if EW.split_epiweek(EW.add_epiweeks(ew, holiday))[1] == 1:
       X[0, 4 + holiday] = 1
   y, w = EW.split_epiweek(ew)
   N = EW.get_num_weeks(y)
   offset = np.pi * 2 * w / N
   X[0, 8] = np.sin(offset)
   X[0, 9] = np.cos(offset)
   return X
Ejemplo n.º 5
0
    def test_from_epiweek(self):
        for y, m, d, ey, ew in FunctionTests.sample_epiweeks:
            epwk = utils_epiweek.join_epiweek(ey, ew)
            with self.subTest(y=y, m=m, d=d, epwk=epwk):
                date1 = EpiDate(y, m, d)
                date2 = EpiDate.from_epiweek(ey, ew)
                self.assertEqual(date1.get_ew(), epwk)
                self.assertEqual(date2.get_ew(), epwk)
                self.assertEqual(date2.get_day_of_week(), 3)

        for year in range(2000, 2020):
            for week in range(1, utils_epiweek.get_num_weeks(year) + 1):
                epwk = utils_epiweek.join_epiweek(year, week)
                date = EpiDate.from_epiweek(year, week)
                self.assertEqual(date.get_ew(), epwk)
                self.assertEqual(date.get_day_of_week(), 3)

        with self.assertRaises(Exception):
            EpiDate.from_epiweek(2017, 0)
        with self.assertRaises(Exception):
            EpiDate.from_epiweek(2017, 53)
        with self.assertRaises(Exception):
            EpiDate.from_epiweek(0, 30)
def cum_to_week(data):
    epiweeks = list(data.keys())
    all_epiweeks = list(
        EW.range_epiweeks(min(epiweeks), max(epiweeks), inclusive=True))

    result = np.zeros((len(all_epiweeks)))
    last_valid = (-1, 0)  # (idx, value)
    for i in range(len(result)):
        ew = all_epiweeks[i]
        if ew in data:
            if data[all_epiweeks[i]] is not None:
                result[last_valid[0] + 1:i +
                       1] = (data[ew] - last_valid[1]) / float(
                           i -
                           last_valid[0])  # Evenly distribute missing counts
                last_valid = (i, data[ew])
        yr, wk = EW.split_epiweek(all_epiweeks[i])
        if EW.get_num_weeks(yr) == wk:
            result[
                last_valid[0] + 1:i +
                1] = 0  # Fill rest of year with 0s, not getting this information
            last_valid = (i, 0)  # Start new year at 0
    return {all_epiweeks[i]: result[i] for i in range(len(all_epiweeks))}