コード例 #1
0
def _check_cutoffs_against_train_windows(cutoffs, windows, y):
    # Cutoffs should always be the last values of the train windows.
    if array_is_int(cutoffs):
        actual = np.array([window[-1] for window in windows[1:]])
    elif array_is_datetime64(cutoffs):
        actual = np.array(
            [y.index[window[-1]].to_datetime64() for window in windows[1:]],
            dtype="datetime64",
        )
    else:
        raise ValueError(
            f"Provided `cutoffs` type is not supported: {type(cutoffs[0])}")
    np.testing.assert_array_equal(actual, cutoffs[1:])

    # We treat the first window separately, since it may be empty when setting
    # `start_with_window=False`.
    if len(windows[0]) > 0:
        if array_is_int(cutoffs):
            np.testing.assert_array_equal(windows[0][-1], cutoffs[0])
        elif array_is_datetime64(cutoffs):
            np.testing.assert_array_equal(
                y.index[windows[0][-1]].to_datetime64(), cutoffs[0])
        else:
            raise ValueError(
                f"Provided `cutoffs` type is not supported: {type(cutoffs[0])}"
            )
コード例 #2
0
def _check_cutoffs_and_y(cutoffs: VALID_CUTOFF_TYPES,
                         y: ACCEPTED_Y_TYPES) -> None:
    """Check that combination of inputs is compatible.

    Parameters
    ----------
    cutoffs : np.array or pd.Index
        cutoff points, positive and integer- or datetime-index like
    y : pd.Series, pd.DataFrame, np.ndarray, or pd.Index
        coerced and checked version of input y

    Raises
    ------
    ValueError
        if max cutoff is above the last observation in `y`
    TypeError
        if `cutoffs` type is not supported
    """
    max_cutoff = np.max(cutoffs)
    msg = ("`cutoffs` are incompatible with given `y`. "
           "Maximum cutoff is not smaller than the ")
    if array_is_int(cutoffs):
        if max_cutoff >= y.shape[0]:
            raise ValueError(msg + "number of observations.")
    elif array_is_datetime64(cutoffs):
        if max_cutoff >= np.max(y):
            raise ValueError(msg + "maximum index value of `y`.")
    else:
        raise TypeError("Unsupported type of `cutoffs`")
コード例 #3
0
def _cutoffs_fh_window_length_types_are_supported(
    cutoffs: VALID_CUTOFF_TYPES,
    fh: FORECASTING_HORIZON_TYPES,
    window_length: ACCEPTED_WINDOW_LENGTH_TYPES,
) -> bool:
    """Check that combination of inputs is supported.

    Currently, only two cases are allowed:
    either all inputs are integers, or they are all datetime or timedelta

    Parameters
    ----------
    cutoffs : np.array or pd.Index
        cutoff points, positive and integer- or datetime-index like
    fh : int, timedelta, list or np.array of ints or timedeltas
    window_length : int or timedelta or pd.DateOffset

    Returns
    -------
    True if all inputs are compatible, False otherwise
    """
    all_int = array_is_int(cutoffs) and array_is_int(fh) and is_int(
        window_length)
    all_dates = (array_is_datetime64(cutoffs)
                 and array_is_timedelta_or_date_offset(fh)
                 and is_timedelta_or_date_offset(window_length))
    if all_int or all_dates:
        return True
    else:
        return False
コード例 #4
0
def check_cutoffs(cutoffs: VALID_CUTOFF_TYPES) -> np.ndarray:
    """Validate the cutoff.

    Parameters
    ----------
    cutoffs : np.ndarray or pd.Index

    Returns
    -------
    cutoffs (Sorted array)

    Raises
    ------
    ValueError
        If cutoffs is not a instance of np.array or pd.Index
        If cutoffs array is empty.

    """
    if not isinstance(cutoffs, ACCEPTED_CUTOFF_TYPES):
        raise ValueError(
            f"`cutoffs` must be a np.array or pd.Index, but found: {type(cutoffs)}"
        )
    assert array_is_int(cutoffs) or array_is_datetime64(cutoffs)

    if len(cutoffs) == 0:
        raise ValueError("Found empty `cutoff` array")

    return np.sort(cutoffs)
コード例 #5
0
def _check_cutoffs(cutoffs):
    assert isinstance(cutoffs, np.ndarray)
    assert array_is_int(cutoffs) or array_is_datetime64(cutoffs)
    assert cutoffs.ndim == 1
    assert len(cutoffs) > 0