def test_ccor(self): t0 = ts(times=[0,1,2,3],values=[1,2,3,4]) t0_stand = distances.stand(t0,t0.mean(),t0.std()) t1 = ts(times=[0,1,2,3],values=[-1,2,1,-1]) t1_stand = distances.stand(t1,t1.mean(), t1.std()) d = distances.ccor(t0_stand,t1_stand) assert (str(d) == str(np.array([0.25819889,-0.94672926,-0.0860663,0.77459667])))
def test_kernelcorr(self): """tests that the kernelized cross correlation is 1 when the two timeseries are identical""" t0 = ts(times=[0,1,2,4,5,6],values=[3,4,5,6,7,8]) t0_stand = distances.stand(t0,t0.mean(),t0.std()) t1 = ts(times=[0,1,2,4,5,6],values=[3,4,5,6,7,8]) t1_stand = distances.stand(t1,t1.mean(), t1.std()) assert distances.kernel_corr(t1_stand, t0_stand) == 1 t3 = ts(times=[0,1,2,4,5,6],values=[3,7,9,10,16,20]) t3_stand = distances.stand(t3,t3.mean(), t3.std()) assert (distances.kernel_corr(t1_stand, t3_stand) < 1)
def random_ts(a): """Creates a TimeSeries with random values a: scaling term to generate random values for time series """ t = np.arange(0.0, 1.0, 0.01) v = a * np.random.random(100) return ts(t, v)
def test_standardize(self): t0 = ts(times=[0,1,2,4,5,6],values=[3,4,5,6,7,8]) assert t0.mean() == 5.5 #check mean assert t0.std() == np.sqrt(17.5/6.0) #check sqrt standardized_values = distances.stand(t0,t0.mean(),t0.std()).values() assert (str(standardized_values) == str(np.array([-1.46385011,-0.87831007,-0.29277002,0.29277002,0.87831007,1.46385011]))) #check that standardized values are correct
def decode(json_object): with open(json_object, 'r') as infile: try: d = json.load(infile) return ts(d['times'], d['values']) except json.JSONDecodeError: raise TSDBInputError('Invalid JSON object received\n')
def tsmaker(m, s, j): """Makes a TimeSeries whose values are approximately normally distributed m: location parameter for normal pdf s: scale parameter for normal pdf j: coefficient for extra randomness added to normally distributed values """ t = np.arange(0.0, 1.0, 0.01) v = norm.pdf(t, m, s) + j * np.random.randn(100) return ts(t, v)
def interpolate_to_match_input(ts_database, ts_input): """ interpolate the generated timeseries in the database to match the timeseries input from the client Parameter ---------------- ts_input: timeseries input by the client ts_database: the timeseries generated in the database """ ts_input_times = ts_input.times() ts_database_times = ts_database.times() interpolated_values = ts_database.interpolate(ts_input_times) interpolated_ts = ts(ts_input_times, interpolated_values) return interpolated_ts
"compute a kernelized correlation so that we can get a real distance" num = np.sum(np.exp(mult * ccor(ts1, ts2))) denom1 = np.sqrt(np.sum(np.exp(mult * ccor(ts1, ts1)))) denom2 = np.sqrt(np.sum(np.exp(mult * ccor(ts2, ts2)))) return (num / denom1) / denom2 def distance(ts1, ts2, mult=1): """Calculates the distance metric using the kernal coefficient""" return 2 * (1 - kernel_corr(ts1, ts2, mult)) ##this is for a quick and dirty test of these functions if __name__ == "__main__": t0 = ts(times=[0, 1, 2, 4, 5, 6], values=[3, 4, 20, 6, 7, 8]) t0_stand = stand(t0, t0.mean(), t0.std()) t1 = ts(times=[0, 1, 2, 4, 5, 6], values=[3, 4, 5, 6, 7, 8]) t1_stand = stand(t1, t1.mean(), t1.std()) print(distance(t0_stand, t1_stand)) t1 = tsmaker(0.5, 0.1, 0.01) t2 = tsmaker(0.5, 0.1, 0.01) import matplotlib.pyplot as plt plt.plot(t1) plt.plot(t2) plt.show() standts1 = stand(t1, t1.mean(), t1.std()) standts2 = stand(t2, t2.mean(), t2.std())
def test_distance(self): t0 = ts(times=[0,1,2,4,5,6],values=[3,4,5,6,7,8]) t0_stand = distances.stand(t0,t0.mean(),t0.std()) t1 = ts(times=[0,1,2,4,5,6],values=[3,4,5,6,7,8]) t1_stand = distances.stand(t1,t1.mean(), t1.std()) assert distances.distance(t0_stand, t1_stand) == 0
def test_maxcorratphase(self): t0 = ts(times=[0,1,2,3],values=[1,2,3,4]) t0_stand = distances.stand(t0,t0.mean(),t0.std()) t1 = ts(times=[0,1,2,3],values=[-1,2,1,-1]) t1_stand = distances.stand(t1,t1.mean(), t1.std()) assert (distances.max_corr_at_phase(t0_stand,t1_stand)) == (3, 0.77459666924148329)
def test_standardizeConstant(self): t0 = ts(times=[0,1,2,4,5,6],values=[3,3, 3, 3, 3, 3]) standardized_values = distances.stand(t0,t0.mean(),t0.std()).values() assert (str(standardized_values) == str(np.array([0.,0.,,0.,0.,0.]))) #check that standardize a series of constant return a series of zeros
def sdecode(json_string): try: d = json.loads(json_string) return ts(d['times'], d['values']) except json.JSONDecodeError: raise TSDBInputError('Invalid JSON object received:\n'+str(json_str))