示例#1
0
def mmwrid_to_epiweek(mmwrid):
  """Convert a CDC week index into an epiweek."""

  # Add the difference in IDs, which are sequential, to a reference epiweek,
  # which is 2003w40 in this case.
  epiweek_200340 = EpiDate(2003, 9, 28)
  mmwrid_200340 = 2179
  return epiweek_200340.add_weeks(mmwrid - mmwrid_200340).get_ew()
示例#2
0
def get_current_issue():
  """Scrape the current issue from the FluSurv main page."""

  # fetch
  data = fetch_json('GetPhase03InitApp?appVersion=Public', None)

  # extract
  date = datetime.strptime(data['loaddatetime'], '%b %d, %Y')

  # convert and return
  return EpiDate(date.year, date.month, date.day).get_ew()
示例#3
0
def season_db_to_epiweek(season_str,
                         db_date_str,
                         first_db_date_of_season_str="1-Aug"):
    year_strs = season_str.split("-")
    first_year = int(year_strs[0])
    second_year = first_year + 1
    # FIXME check/enforce locale
    first_date_of_season = datetime.datetime.strptime(
        first_db_date_of_season_str + "-" + str(first_year),
        "%d-%b-%Y").date()
    date_using_first_year = datetime.datetime.strptime(
        db_date_str + "-" + str(first_year), "%d-%b-%Y").date()
    date_using_second_year = datetime.datetime.strptime(
        db_date_str + "-" + str(second_year), "%d-%b-%Y").date()
    date = date_using_first_year if date_using_first_year >= first_date_of_season else date_using_second_year
    epiweek = EpiDate(date.year, date.month, date.day).get_ew()
    return epiweek
示例#4
0
  def extract_epiweek_and_team(filename):
    """
    Extract the submission epiweek (epiweek of most recently published report)
    and the team name from the file name of a flu contest submission.

    The return value is a tuple of:
      1. the submission epiweek (e.g. 201751)
      2. the team name (e.g. "delphi-epicast")
    """

    # this is the naming convention for 2017 flu contest submissions
    pattern = re.compile('^EW(\\d{2})-(.*)-(\\d{4})-(\\d{2})-(\\d{2}).csv$')
    match = pattern.match(os.path.basename(filename))
    if match is None:
      # only able to parse this specific naming convention
      raise Exception()

    week = int(match.group(1))
    team = match.group(2)
    year = int(match.group(3))
    month = int(match.group(4))
    day = int(match.group(5))
    epiweek = EpiDate(year, month, day).get_ew()

    # We know the week number, but the year has to be inferred from the
    # submission date. Since the week of submission is never less than the week
    # of the most recent report, we can step backwards from the week of
    # submission until we find the expected week number. Ordinarily, this will
    # take exactly two steps. For example, data collected on 2017w51 is
    # reported on 2017w52, and our forecast is submitted on 2018w01; so we
    # start with 2018w01 and step backwards until find the first week 51, which
    # is 2017w51.
    if not 1 <= week <= 53:
      # prevent an infinite loop
      raise Exception('invalid week number: %d' % week)
    while Epiweek.split_epiweek(epiweek)[1] != week:
      epiweek = Epiweek.add_epiweeks(epiweek, -1)

    return epiweek, team