def test_procs(): # check that standardization is successful _, t1 = tsmaker(0.5, 0.1, 0.01) # ignore meta-data returned t2 = random_ts(2) standts1 = stand(t1, t1.mean(), t1.std()) standts2 = stand(t2, t2.mean(), t2.std()) assert np.round(standts1.mean(), 10) == 0.0 assert np.round(standts1.std(), 10) == 1.0 assert np.round(standts2.mean(), 10) == 0.0 assert np.round(standts2.std(), 10) == 1.0 # once more, with hard-coded data so we know what answers to expect v1 = [2.00984793, 3.94985729, 0.51427819, 4.16184495, 2.73640138, 0.07386398, 1.32847121, 0.3811719, 4.34006452, 1.86213488] v2 = [6.43496991, 10.34439479, 11.71829468, 3.92319708, 7.07694841, 6.7165553, 5.72293448, 4.79283759, 11.74512723, 11.74048488] t1 = TimeSeries(np.arange(0.0, 1.0, 0.1), v1) t2 = TimeSeries(np.arange(0.0, 1.0, 0.1), v2) standts1 = stand(t1, t1.mean(), t1.std()) standts2 = stand(t2, t2.mean(), t2.std()) assert np.round(standts1.mean(), 10) == 0.0 assert np.round(standts1.std(), 10) == 1.0 assert np.round(standts2.mean(), 10) == 0.0 assert np.round(standts2.std(), 10) == 1.0 idx, mcorr = max_corr_at_phase(standts1, standts2) assert idx == 2 assert np.round(mcorr, 4) == 0.5207 sumcorr = kernel_corr(standts1, standts2, mult=10) assert np.round(sumcorr, 4) == 0.0125
def test_kernelcorr(): t1 = TimeSeries([1, 2, 3, 4], [40, 50, 60, 70]) t2 = TimeSeries([1, 2, 3, 4], [40, 50, 60, 70]) standts1 = _corr.stand(t1, t1.mean(), t1.std()) standts2 = _corr.stand(t2, t2.mean(), t2.std()) #Kernel_corr should return a correlation of 1.0 since we use similar timeseries assert (_corr.kernel_corr(standts1, standts2, mult=1) == 1.0)
def test_kernelcorr(): t1 = TimeSeries([1, 2, 3, 4], [40, 50, 60, 70]) t2 = TimeSeries([1, 2, 3, 4], [40, 50, 60, 70]) standts1 = _corr.stand(t1, t1.mean(), t1.std()) standts2 = _corr.stand(t2, t2.mean(), t2.std()) #Kernel_corr should return a correlation of 1.0 since we use similar timeseries assert(_corr.kernel_corr(standts1, standts2, mult=1) == 1.0)
def test_maxcorr(): t1 = TimeSeries([1, 2, 3, 4], [40, 50, 60, 70]) t2 = TimeSeries([1, 2, 3, 4], [50, 60, 70, 40]) standts1 = _corr.stand(t1, t1.mean(), t1.std()) standts2 = _corr.stand(t2, t2.mean(), t2.std()) idx, mcorr = _corr.max_corr_at_phase(standts1, standts2) #idx should be equal to one since the second ts is shifted by 1 assert (idx == 1) assert (np.real(mcorr) == 4)
def test_maxcorr(): t1 = TimeSeries([1, 2, 3, 4], [40, 50, 60, 70]) t2 = TimeSeries([1, 2, 3, 4], [50, 60, 70, 40]) standts1 = _corr.stand(t1, t1.mean(), t1.std()) standts2 = _corr.stand(t2, t2.mean(), t2.std()) idx, mcorr = _corr.max_corr_at_phase(standts1, standts2) #idx should be equal to one since the second ts is shifted by 1 assert(idx == 1) assert(np.real(mcorr) == 4)
def proc_main(pk, row, arg): ''' Calculates the distance between two time series, using the normalized kernelized cross-correlation. Note: used directly for augmented selects. Parameters ---------- pk : any hashable type The primary key of the database entry row : dictionary The database entry Returns ------- [damean, dastd] : list of floats Mean and standard deviation of the time series data ''' # recast the argument as a time series (type is lost due to serialization) if isinstance(arg, TimeSeries): argts = arg # for server-side testing else: argts = TimeSeries(*arg) # for live client-side operations # standardize the time series stand_argts = stand(argts, argts.mean(), argts.std()) # standardize each row of the data that has tripped the trigger stand_rowts = stand(row['ts'], row['ts'].mean(), row['ts'].std()) # compute the normalized kernelized cross-correlation between the # time series being upserted/selected and the time series argument kerncorr = kernel_corr(stand_rowts, stand_argts, 5) # use the normalized kernelized cross-correlation to compute the distance # between the time series and return return [np.sqrt(2*(1-kerncorr))]
def test_std2(self): t1 = TimeSeries([1, 2, 3], [10, 100, 1000]) t2 = TimeSeries([4, 6, 8], [100, 1000, 10]) self.assertEqual(t1.std(), t2.std())
def test_std1(self): t1 = TimeSeries([1], [10]) with self.assertRaises(ValueError): t1.std()