def test_timeseries_constructor_error_log(self): # test assert error log when trying to construct a TimeSeries that is too short empty_series = pd.DataFrame() with LogCapture() as lc: get_logger("darts.timeseries").handlers = [] try: TimeSeries(xr.DataArray(empty_series)) except Exception: pass lc.check(( "darts.timeseries", "ERROR", "ValueError: The time series array must not be empty.", ))
def test_raise_log(self): exception_was_raised = False with LogCapture() as lc: logger = get_logger(__name__) logger.handlers = [] try: raise_log(Exception("test"), logger) except Exception: exception_was_raised = True # testing correct log message lc.check((__name__, "ERROR", "Exception: test")) # checking whether exception was properly raised self.assertTrue(exception_was_raised)
def test_time_log(self): logger = get_logger(__name__) logger.handlers = [] @time_log(logger) def _my_timed_fn(): # do something for some time for _ in range(2): pass with LogCapture() as lc: _my_timed_fn() logged_message = lc.records[-1].getMessage() self.assertTrue( re.match("_my_timed_fn function ran for [0-9]+ milliseconds", logged_message))
def test_timeseries_split_error_log(self): # test raised error log that occurs when trying to split TimeSeries at a point outside of the time index range times = pd.date_range(start="2000-01-01", periods=3, freq="D") values = np.array(range(3)) ts = TimeSeries.from_times_and_values(times, values) with LogCapture() as lc: get_logger("darts.timeseries").handlers = [] try: ts.split_after(pd.Timestamp("2020-02-01")) except Exception: pass lc.check(( "darts.timeseries", "ERROR", "ValueError: Timestamp must be between 2000-01-01 00:00:00 and 2000-01-03 00:00:00", ))
from typing import List, Optional import numpy as np import statsmodels.tsa.holtwinters as hw from darts.logging import get_logger, raise_if_not, raise_log from darts.models.forecasting.forecasting_model import ForecastingModel from darts.timeseries import TimeSeries from darts.utils.statistics import ( check_seasonality, extract_trend_and_seasonality, remove_from_series, ) from darts.utils.utils import ModelMode, SeasonalityMode, TrendMode logger = get_logger(__name__) ALPHA_START = 0.2 class Theta(ForecastingModel): # .. todo: Implement OTM: Optimized Theta Method (https://arxiv.org/pdf/1503.03529.pdf) # .. todo: Try with something different than SES? They do that in the paper. def __init__( self, theta: int = 2, seasonality_period: Optional[int] = None, season_mode: SeasonalityMode = SeasonalityMode.MULTIPLICATIVE, ): """ An implementation of the Theta method with configurable `theta` parameter. See [1]_.