How to use the pytesmo.time_series.filtering.moving_average function in pytesmo

To help you get started, we’ve selected a few pytesmo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TUW-GEO / pytesmo / tests / test_time_series / test_filtering.py View on Github external
def test_moving_average_min_observations():
    """
    Test moving average filter with datetimeindex.
    """
    test_jd = pd.date_range(start='2000-01-01', periods=12, freq='D')
    test_data = np.array(
        [1, 2, 3, 4, np.nan, np.nan, 8, 9, 10, np.nan, np.nan, np.nan], dtype=np.double)

    ser = pd.Series(test_data, index=test_jd)

    filtered = filtering.moving_average(ser, window_size=5, fillna=True, min_obs=3)

    np.testing.assert_allclose(filtered.values, [2., 2.5, 2.5, 3.0, 5.0, 7.0, 9.0, 9.0, 9.0, np.nan, np.nan, np.nan])

    filtered = filtering.moving_average(ser, window_size=5, fillna=True, min_obs=4)

    np.testing.assert_allclose(filtered.values, [np.nan, 2.5, 2.5, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
github TUW-GEO / pytesmo / tests / test_time_series / test_filtering.py View on Github external
def test_moving_average_size_1():
    """
    Test moving average filter with input size 1.
    """
    test_jd = np.arange(1, dtype=np.double)
    test_data = np.array(
        [1], dtype=np.double)

    ser = pd.Series(test_data, index=test_jd)

    filtered = filtering.moving_average(ser, window_size=5)

    np.testing.assert_allclose(filtered.values, [1.])
github TUW-GEO / pytesmo / tests / test_time_series / test_filtering.py View on Github external
def test_moving_average_dt_index():
    """
    Test moving average filter with datetimeindex.
    """
    test_jd = pd.date_range(start='2000-01-01', periods=10, freq='D')
    test_data = np.array(
        [1, 2, 3, 4, -999999.0, 6, 7, 8, 9, np.nan], dtype=np.double)

    ser = pd.Series(test_data, index=test_jd)

    filtered = filtering.moving_average(ser, window_size=5)

    np.testing.assert_allclose(filtered.values, [2., 2.5, 2.5, 3.75, np.nan, 6.25,
                                                 7.5, 7.5, 8., np.nan])
github TUW-GEO / pytesmo / tests / test_time_series / test_filtering.py View on Github external
def test_moving_average_fillna():
    """
    Test moving average filter with datetimeindex.
    """
    test_jd = pd.date_range(start='2000-01-01', periods=12, freq='D')
    test_data = np.array(
        [1, 2, 3, 4, np.nan, np.nan, 8, 9, 10, np.nan, np.nan, np.nan], dtype=np.double)

    ser = pd.Series(test_data, index=test_jd)

    filtered = filtering.moving_average(ser, window_size=5, fillna=True)

    np.testing.assert_allclose(filtered.values, [2., 2.5, 2.5, 3.0, 5.0, 7.0, 9.0, 9.0, 9.0, 9.5, 10, np.nan])
github TUW-GEO / pytesmo / tests / test_time_series / test_filtering.py View on Github external
def test_moving_average():
    """
    Test moving average filter.
    """
    test_jd = np.arange(10, dtype=np.double)
    test_data = np.array(
        [1, 2, 3, 4, -999999.0, 6, 7, 8, 9, np.nan], dtype=np.double)

    ser = pd.Series(test_data, index=test_jd)

    filtered = filtering.moving_average(ser, window_size=5)

    np.testing.assert_allclose(filtered.values, [2., 2.5, 2.5, 3.75, np.nan, 6.25,
                                                 7.5, 7.5, 8., np.nan])
github TUW-GEO / pytesmo / tests / test_time_series / test_filtering.py View on Github external
def test_moving_average_min_observations():
    """
    Test moving average filter with datetimeindex.
    """
    test_jd = pd.date_range(start='2000-01-01', periods=12, freq='D')
    test_data = np.array(
        [1, 2, 3, 4, np.nan, np.nan, 8, 9, 10, np.nan, np.nan, np.nan], dtype=np.double)

    ser = pd.Series(test_data, index=test_jd)

    filtered = filtering.moving_average(ser, window_size=5, fillna=True, min_obs=3)

    np.testing.assert_allclose(filtered.values, [2., 2.5, 2.5, 3.0, 5.0, 7.0, 9.0, 9.0, 9.0, np.nan, np.nan, np.nan])

    filtered = filtering.moving_average(ser, window_size=5, fillna=True, min_obs=4)

    np.testing.assert_allclose(filtered.values, [np.nan, 2.5, 2.5, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan])
github TUW-GEO / pytesmo / src / pytesmo / time_series / anomaly.py View on Github external
elif interpolate_leapday and respect_leap_years:
        clim_ser[366] = np.mean((clim_ser[365], clim_ser[1]))

    if wraparound:
        index_old = clim_ser.index.copy()
        left_mirror = clim_ser.iloc[-moving_avg_clim:]
        right_mirror = clim_ser.iloc[:moving_avg_clim]
        # Shift index to start at 366 - index at -moving_avg_clim
        # to run over a whole year while keeping gaps the same size
        right_mirror.index = right_mirror.index + 366 * 2
        clim_ser.index = clim_ser.index + 366
        clim_ser = pd.concat([left_mirror,
                              clim_ser,
                              right_mirror])

        clim_ser = moving_average(clim_ser, window_size=moving_avg_clim, fillna=fillna, min_obs=min_obs_clim)
        clim_ser = clim_ser.iloc[moving_avg_clim:-moving_avg_clim]
        clim_ser.index = index_old
    else:
        clim_ser = moving_average(clim_ser, window_size=moving_avg_clim, fillna=fillna, min_obs=min_obs_clim)

    clim_ser = clim_ser.reindex(np.arange(366) + 1)
    clim_ser = clim_ser.fillna(fill)
    return clim_ser
github TUW-GEO / pytesmo / src / pytesmo / time_series / anomaly.py View on Github external
min_obs_clim: int
        Minimum observations required to give a valid output in the second
        moving average applied on the calculated climatology

    Returns
    -------
    climatology : pandas.Series
        Series containing the calculated climatology
        Always has 366 values behaving like a leap year
    '''

    if timespan is not None:
        Ser = Ser.truncate(before=timespan[0], after=timespan[1])

    Ser = moving_average(Ser, window_size=moving_avg_orig, fillna=fillna, min_obs=min_obs_orig)

    Ser = pd.DataFrame(Ser)

    if type(Ser.index) == pd.DatetimeIndex:
        year, month, day = (np.asarray(Ser.index.year),
                            np.asarray(Ser.index.month),
                            np.asarray(Ser.index.day))
    else:
        year, month, day = julian2date(Ser.index.values)[0:3]




    if respect_leap_years:
        doys = doy(month, day, year)
    else:
github TUW-GEO / pytesmo / src / pytesmo / time_series / anomaly.py View on Github external
df['absolute'] = Ser
        df['doy'] = doys

        clim = pd.DataFrame({'climatology': climatology})

        df = df.join(clim, on='doy', how='left')

        anomaly = df['absolute'] - df['climatology']
        anomaly.index = df.index

        if return_clim:
            anomaly = pd.DataFrame({'anomaly': anomaly})
            anomaly['climatology'] = df['climatology']

    else:
        reference = moving_average(Ser, window_size=window_size)
        anomaly = Ser - reference

    return anomaly
github TUW-GEO / pytesmo / src / pytesmo / time_series / anomaly.py View on Github external
index_old = clim_ser.index.copy()
        left_mirror = clim_ser.iloc[-moving_avg_clim:]
        right_mirror = clim_ser.iloc[:moving_avg_clim]
        # Shift index to start at 366 - index at -moving_avg_clim
        # to run over a whole year while keeping gaps the same size
        right_mirror.index = right_mirror.index + 366 * 2
        clim_ser.index = clim_ser.index + 366
        clim_ser = pd.concat([left_mirror,
                              clim_ser,
                              right_mirror])

        clim_ser = moving_average(clim_ser, window_size=moving_avg_clim, fillna=fillna, min_obs=min_obs_clim)
        clim_ser = clim_ser.iloc[moving_avg_clim:-moving_avg_clim]
        clim_ser.index = index_old
    else:
        clim_ser = moving_average(clim_ser, window_size=moving_avg_clim, fillna=fillna, min_obs=min_obs_clim)

    clim_ser = clim_ser.reindex(np.arange(366) + 1)
    clim_ser = clim_ser.fillna(fill)
    return clim_ser