Ejemplo n.º 1
0
def _narrow_selection(index: Index, data: Data, region_name: str,
                      season: int) -> Tuple[Index, Data]:
    """
    Return a narrowed index and data using the region and season information on the index.
    Require index to have epiweek and region
    """

    assert index.shape[0] == data.shape[
        0], "Shape of index and data not matching in first dimension"

    # All true selection
    selection = index["epiweek"] > 0
    narrowing = False
    if region_name is not None:
        selection = selection & (index["region"] == region_name)
        narrowing = True

    if season is not None:
        first_ew = (season * 100) + 40

        if pymmwr.mmwr_weeks_in_year(season) == 53:
            # We skip the 20yy20 data and only provide upto 20yy19
            # This makes it easy for the visualizations
            last_ew = ((season + 1) * 100) + 19
        else:
            last_ew = ((season + 1) * 100) + 20

        selection = selection & (index["epiweek"] >=
                                 first_ew) & (index["epiweek"] <= last_ew)
        narrowing = True

    if narrowing:
        return index[selection].reset_index(drop=True), data[selection]
    else:
        return index, data
Ejemplo n.º 2
0
def _process_csv_point_row(season_start_year, target_name, value):
    # returns: point value for the args
    if target_name == 'Season onset':  # nominal target. value: None or an EW Monday date
        if value is None:
            return 'none'  # convert back from None to original 'none' input
        else:  # value is an EW week number (float)
            # note that value may be a fraction (e.g., 50.0012056690978, 4.96302456525203), so we round
            # the EW number to get an int, but this could cause boundary issues where the value is
            # invalid, either:
            #   1) < 1 (so use last EW in season_start_year), or:
            #   2) > the last EW in season_start_year (so use EW01 of season_start_year + 1)
            ew_week = round(value)
            if ew_week < 1:
                ew_week = pymmwr.mmwr_weeks_in_year(
                    season_start_year)  # wrap back to previous EW
            elif ew_week > pymmwr.mmwr_weeks_in_year(
                    season_start_year):  # wrap forward to next EW
                ew_week = 1
            monday_date = _monday_date_from_ew_and_season_start_year(
                ew_week, season_start_year)
            return monday_date.strftime(YYYY_MM_DD_DATE_FORMAT)
    elif target_name in [
            '1_biweek_ahead', '2_biweek_ahead', '3_biweek_ahead',
            '4_biweek_ahead', '5_biweek_ahead'
    ]:  # thai
        return round(value)  # some point predictions are floats
    elif value is None:
        raise RuntimeError(
            f"None point values are only valid for 'Season onset' targets. "
            f"target_name={target_name}")
    elif target_name == 'Season peak week':  # date target. value: an EW Monday date
        # same 'wrapping' logic as above to handle rounding boundaries
        ew_week = round(value)
        if ew_week < 1:
            ew_week = pymmwr.mmwr_weeks_in_year(
                season_start_year)  # wrap back to previous EW
        elif ew_week > pymmwr.mmwr_weeks_in_year(
                season_start_year):  # wrap forward to next EW
            ew_week = 1
        monday_date = _monday_date_from_ew_and_season_start_year(
            ew_week, season_start_year)
        return monday_date.strftime(YYYY_MM_DD_DATE_FORMAT)
    else:  # 'Season peak percentage', '1 wk ahead', '2 wk ahead', '3 wk ahead', '4 wk ahead', '1_biweek_ahead', '2_biweek_ahead', '3_biweek_ahead', '4_biweek_ahead',  # thai '5_biweek_ahead'
        return value
Ejemplo n.º 3
0
def get_epiweek(season: str, season_wk: int):
    """
    Get epiweek integer
    """

    season_first_year = int(season.split("/")[0])
    season_second_year = int(season.split("/")[1])

    # Week 31 is season_wk 01
    week = 30 + season_wk
    first_year_weeks = pymmwr.mmwr_weeks_in_year(season_first_year)

    epiweek_year = season_first_year
    if week > first_year_weeks:
        week -= first_year_weeks
        epiweek_year = season_second_year

    return epiweek_year * 100 + week
Ejemplo n.º 4
0
def epiweek_to_model_week(epiweek: int) -> int:
    """
    Convert epiweek to model week starting at 20xx40 and ending at
    20xx19/20xx20
    """

    if np.isnan(epiweek):
        # We consider this as the sign of no onset and assign the last bin
        return 34

    epiweek = int(epiweek)
    season = epiweek_to_season(epiweek)
    nweeks = pymmwr.mmwr_weeks_in_year(season)

    week = epiweek % 100
    if week >= 40 and week <= nweeks:
        return epiweek - ((season * 100) + 40)
    elif (nweeks == 52 and week <= 21) or (nweeks == 53 and week <= 20):
        return week + (nweeks - 40)
    elif week >= 20:
        warnings.warn(f"Epiweek {epiweek} is greater than the models' range")
        return week + (nweeks - 40)
    else:
        raise Exception(f"Epiweek {epiweek} outside the desired range")