def test_crosscorr(): t1 = standardize(tsmaker(0.5, 0.1, random.uniform(0, 10))) # First confirm that the kernel correlation and distance methods # return 1 and 0 when comparing a ts with itself assert (kernel_corr(t1, t1) == 1) assert (kernel_dist(t1, t1) == 0) t2 = standardize(tsmaker(0.5, 0.1, random.uniform(0, 10))) t3 = standardize(random_ts(0.5)) # Now let's do the opposite -- ensure that we see some distance for different curves assert (kernel_dist(t1, t2) > 0) assert (kernel_dist(t1, t3) > 0) assert (kernel_corr(t1, t2) < 1) assert (kernel_corr(t1, t3) < 1)
def test_get_n_nearest_ts_for_ts(): """ Make new ts. Send over socket, and request nearest ts. Compare results to (non socket) simsearch_by_id method """ new_ts = tsmaker(0.5, 0.1, random.uniform(0, 10)) d = s_client.get_n_nearest_ts_for_ts(new_ts, 5) s_n_closest_dict = d['n_closest_dict'] n_closest_dict = simsearch_by_id(d['tsid'], 5) for tsid in s_n_closest_dict.values(): assert (tsid in n_closest_dict.values())
def test_add_ts(): """ Create a ts, add to db, retrieve it, assert that it's the same ts""" new_ts = standardize(tsmaker(0.5, 0.1, random.uniform(0, 10))) new_tsid = add_ts(new_ts) ts_as_saved = get_by_id(new_tsid) assert (kernel_dist(standardize(ts_as_saved), standardize(new_ts)) < .00001) # Confirm that we get the same id back when we attempt to add it a second time assert (add_ts(new_ts) == new_tsid)
def test_simsearch_by_ts(): ats_75 = get_by_id(75) n_closest_dict, tsid, is_new = simsearch_by_ts(ats_75, 5) assert (tsid == 75) assert (is_new == False) assert (n_closest_dict == simsearch_by_id(75, 5)) new_ts = standardize(tsmaker(0.5, 0.1, random.uniform(0, 10))) n_closest_dict, tsid, is_new = simsearch_by_ts(new_ts, 5) assert (is_new == True) assert (tsid > 250) assert (len(n_closest_dict) == 5)
def test_crosscorr_errors(): """Test that we have checks for varies error conditions""" t1 = standardize(tsmaker(0.5, 0.1, random.uniform(0, 10))) t4 = standardize(random_ts(0.5, 200)) t5 = tsmaker(0.5, 0.1, random.uniform(0, 10)) #Confirm that we raise value error if we attempt to compare time series # that are not the same length with raises(ValueError): ccor(t1, t4) with raises(ValueError): kernel_dist(t1, t4) with raises(ValueError): kernel_corr(t1, t4) #Confirm that we raise value error if we attempt to compare time series # that have not been standardized first t5 = tsmaker(0.5, 0.1, random.uniform(0, 10)) with raises(ValueError): kernel_dist(t4, t5)
def test_save_ts_to_db(): # Save a ts, request it by id, compare to original new_ts = (tsmaker(0.5, 0.1, random.uniform(0, 10))) new_tsid = s_client.save_ts_to_db(new_ts) echo_ts = s_client.get_ts_with_id(new_tsid) assert (kernel_dist(standardize(echo_ts), standardize(new_ts)) < .00001)