Exemple #1
0
def test_for_older_version():
    # Fit an ARIMA
    arima = ARIMA(order=(0, 0, 0), trend='c', suppress_warnings=True)

    # There are three possibilities here:
    # 1. The model is serialized/deserialized BEFORE it has been fit.
    #    This means we should not get a warning.
    #
    # 2. The model is saved after being fit, but it does not have a
    #    pkg_version_ attribute due to it being an old (very old) version.
    #    We still warn for this
    #
    # 3. The model is saved after the fit, and it's version does not match.
    #    We warn for this.
    for case, do_fit, expect_warning in [(1, False, False), (2, True, True),
                                         (3, True, True)]:

        # Only fit it if we should
        if do_fit:
            arima.fit(y)

        # If it's case 2, we remove the pkg_version_. If 3, we set it low
        if case == 2:
            delattr(arima, 'pkg_version_')
        elif case == 3:
            arima.pkg_version_ = '0.0.1'  # will always be < than current

        # Pickle it
        pickle_file = 'model.pkl'
        try:
            joblib.dump(arima, pickle_file)

            # Now unpickle it and show that we get a warning (if expected)
            with warnings.catch_warnings(record=True) as w:
                arm = joblib.load(pickle_file)  # type: ARIMA

                if expect_warning:
                    assert len(w) > 0
                else:
                    assert not len(w)

                # we can still produce predictions (only if we fit)
                if do_fit:
                    arm.predict(n_periods=4)

        finally:
            arima._clear_cached_state()
            os.unlink(pickle_file)
Exemple #2
0
def test_more_elaborate():
    # show we can fit this with a non-zero order
    arima = ARIMA(order=(2, 1, 2), suppress_warnings=True).fit(y=hr)
    _try_get_attrs(arima)

    # can we fit this same arima with a made-up exogenous array?
    xreg = rs.rand(hr.shape[0], 4)
    arima = ARIMA(order=(2, 1, 2), suppress_warnings=True).fit(y=hr,
                                                               exogenous=xreg)
    _try_get_attrs(arima)

    # pickle this for the __get/setattr__ coverage.
    # since the only time this is tested is in parallel in auto.py,
    # this doesn't actually get any coverage proof...
    fl = 'some_temp_file.pkl'
    with open(fl, 'wb') as p:
        pickle.dump(arima, p)

    # show we can predict with this even though it's been pickled
    new_xreg = rs.rand(5, 4)
    _preds = arima.predict(n_periods=5, exogenous=new_xreg)

    # now unpickle
    with open(fl, 'rb') as p:
        other = pickle.load(p)

    # show we can still predict, compare
    _other_preds = other.predict(n_periods=5, exogenous=new_xreg)
    assert_array_almost_equal(_preds, _other_preds)

    # now clear the cache and remove the pickle file
    arima._clear_cached_state()
    os.unlink(fl)

    # now show that since we fit the ARIMA with an exogenous array,
    # we need to provide one for predictions otherwise it breaks.
    assert_raises(ValueError, arima.predict, n_periods=5, exogenous=None)

    # show that if we DO provide an exogenous and it's the wrong dims, we
    # also break things down.
    assert_raises(ValueError,
                  arima.predict,
                  n_periods=5,
                  exogenous=rs.rand(4, 4))
Exemple #3
0
def test_new_serialization():
    arima = ARIMA(order=(0, 0, 0), suppress_warnings=True).fit(y)

    # Serialize it, show there is no tmp_loc_
    pkl_file = "file.pkl"
    new_loc = "ts_wrapper.pkl"
    try:
        joblib.dump(arima, pkl_file)

        # Assert it does NOT use the old-style pickling
        assert not _uses_legacy_pickling(arima)
        loaded = joblib.load(pkl_file)
        assert not _uses_legacy_pickling(loaded)
        preds = loaded.predict()
        os.unlink(pkl_file)

        # Now save out the arima_res_ piece separately, and show we can load
        # it from the legacy method
        arima.summary()
        arima.arima_res_.save(fname=new_loc)
        arima.tmp_pkl_ = new_loc

        assert _uses_legacy_pickling(arima)

        # Save/load it and show it works
        joblib.dump(arima, pkl_file)
        loaded2 = joblib.load(pkl_file)
        assert_array_almost_equal(loaded2.predict(), preds)

        # De-cache
        arima._clear_cached_state()
        assert not os.path.exists(new_loc)

        # Show we get an OSError now
        with pytest.raises(OSError) as ose:
            joblib.load(pkl_file)
        assert "Does it still" in str(ose), ose

    finally:
        _unlink_if_exists(pkl_file)
        _unlink_if_exists(new_loc)