def test_calculate_signal_spectrum(self): signal_1 = [1, 1, 1, 1, 1, 1] signal_2 = [1, 3, 1, 3, 3, 1] spectrums = DWS()._calculate_signal_spectrum([signal_1, signal_2]) np.testing.assert_approx_equal(spectrums[0], 0.0) np.testing.assert_approx_equal(spectrums[1], 1.0)
def test_get_approximations(self): y = np.array([1, 2, 3, 4, 5, 6, 7, 8]) approximations = DWS(wavelet='db1')._get_approximations(y, 2) first_level = [2.12132034, 4.94974747, 7.77817459, 10.60660172] second_level = [5., 13.] np.testing.assert_almost_equal(approximations[0], first_level) np.testing.assert_almost_equal(approximations[1], second_level)
def test_detect_base_case(self): data_points = 1000 x = np.arange(0, data_points) noise = np.random.normal(size=data_points) dws = DWS() trend = dws.estimate_trend(x, noise) assert trend is None
def test_detect_trend(self): data_points = 1000 x = np.arange(0, data_points) y = 1 / 1000 * x**2 noise = np.random.normal(loc=0, scale=200, size=data_points) signal = y + noise dws = DWS() trend = dws.estimate_trend(x, signal) assert trend is not None assert trend[0] < trend[-1]
def test_detect_no_trend(self): data_points = 100 x = np.arange(0, data_points) y = x / 10 season = np.sin(x + np.pi / 2) noise = np.random.normal(size=data_points) signal = y + season + noise dws = DWS() trend = dws.estimate_trend(x, signal) assert trend is not None assert trend[0] < trend[-1]
# %% import matplotlib.pyplot as plt from src.compare_functions import trend_estimation_comparison from src.data_handler import data_handler from src.methods.dws import DWS from src.methods.emd import EmpiricalModeDecomposition from src.methods.lowess import Lowess from src.methods.hp_filter import HPfilter from src.methods.ita import ITA from src.methods.mk import MannKendall from src.methods.regression import Regression from src.methods.splines import Splines from src.methods.theil import Theil from src.methods.regression import Regression if __name__ == '__main__': methods_detection = [ITA(), MannKendall(), Regression(), Theil()] methods_estimation = [DWS(), HPfilter(), Splines(), Theil(), Lowess(), EmpiricalModeDecomposition(), Regression()] x, y, trend, seasonality, noise = data_handler.generate_synthetic_data('data', 'monthly.ini') plt.plot(x, y) plt.plot(x, trend) plt.show() trend_estimation_comparison(methods_estimation, 'monthly')
file_results.append(distance) # plt.plot(x, trend, label='True trend', linewidth=3.0, color='k', linestyle=':') # plt.legend() # plt.savefig(f'{PLOTS_DIR}/{name}_{timestamp}.png') # plt.close() results[name] = file_results table = pd.DataFrame(results, columns) table.to_csv( f'{RESULTS_DIR}/trend_estimation_{file_prefix}_{timestamp}.csv') return table if __name__ == '__main__': # methods_detection = [MannKendall(), ITA(), Theil(), DWS(), EmpiricalModeDecomposition(), # HPfilter(), Splines()] # # trend_detection_comparison(methods_detection, 'func') methods_estimation = [ Theil(), DWS(), EmpiricalModeDecomposition(), HPfilter(), Splines() ] trend_estimation_comparison(methods_estimation, 'func')
from src.utils import generate_timestamp def show_estimation(methods: List[Method], time_series_x: np.ndarray, time_series_y: np.ndarray) -> None: for method in methods: trend_estimation = method.estimate_trend(time_series_x, time_series_y) plt.figure(figsize=(12, 8)) plt.plot(time_series_x, time_series_y) plt.plot(time_series_x, trend_estimation, label='Estimated trend') plt.title(f'Trend estimation of {method.name}') plt.xlabel('Days from January 1962 to July 2012') plt.ylabel('Difference between LOD and 86400 seconds (ms)') plt.savefig(f'{PLOTS_DIR}/case_study_{generate_timestamp()}.png') plt.show() plt.close() if __name__ == '__main__': methods_detection = [MannKendall(), ITA(plot=True)] methods_estimation = [Regression(), Lowess(), Splines(), HPfilter(), DWS()] data = loadmat(f'{DATA_DIR}/case_study.mat') y = data['LOD'].squeeze() x = np.arange(0, y.shape[0]) #print(method_detection.detect_trend(x, y)) show_estimation(methods_estimation, x, y)
f'snr: {noise_title}') for method in methods_list: estimation = method.estimate_trend(x, y) plt.plot(x, estimation, label=method.name) distance = np.linalg.norm((estimation - trend)) file_results.append(distance) plt.plot(x, trend, label='True trend', linewidth=3.0, color='k', linestyle=':') plt.legend() plt.savefig(f'{PLOTS_DIR}/{name}_{timestamp}.png') plt.close() results[name] = file_results table = pd.DataFrame(results, columns) table.to_csv(f'{RESULTS_DIR}/trend_estimation_{file_prefix}_{timestamp}.csv') return table if __name__ == '__main__': # methods_detection = [MannKendall(), ITA(), Theil(), DWS(), EmpiricalModeDecomposition(), # HPfilter(), Splines()] # # trend_detection_comparison(methods_detection, 'func') methods_estimation = [Theil(), DWS(), EmpiricalModeDecomposition(), HPfilter(), Splines()] trend_estimation_comparison(methods_estimation, 'func')