def test_ccorr(): from makelcs import tsmaker from crosscorr import kernel_corr, kernel_dist, standardize t1 = standardize(tsmaker(0.5, 0.1, random.uniform(0, 10))) t2 = standardize(tsmaker(0.5, 0.1, random.uniform(0, 10))) assert (kernel_corr(t1, t1) == 1) assert (kernel_dist(t1, t1) == 0)
def calc_distances(vp_k, timeseries_dict): """Calculates kernel distance between vantage point and all loaded light curves""" distances = [] vp = standardize(timeseries_dict[vp_k]) for k in timeseries_dict: if k != vp_k: k_dist = kernel_dist(vp, standardize(timeseries_dict[k])) distances.append((k_dist, k)) return distances
def plot_two_ts(ts1, ts1_name, ts2, ts2_name, stand=True): """Plots two time series with matplotlib""" import matplotlib.pyplot as plt if (stand): ts1 = standardize(ts1) ts2 = standardize(ts2) plt.plot(ts1, label=ts1_name) plt.plot(ts2, label=ts2_name) plt.legend() plt.show()
def find_closest_vp(vps_dict, ts): """ Calculates distances from ts to all vantage points. Returns tuple with filename of closest vantage point and distance to that vantage point. """ s_ts = standardize(ts) vp_distances = sorted([(kernel_dist(s_ts, standardize(vps_dict[vp])), vp) for vp in vps_dict]) dist_to_vp, vp_fn = vp_distances[0] return (vp_fn, dist_to_vp)
def search_vpdb(vp_t, ts): """ Searches for most similar light curve based on pre-computed distances in vpdb Args: vp_t: tuple containing vantage point filename and distance of time series to vantage point ts: time series to search on. Returns: Tuple: Distance to closest light curve, filename of closest light curve, ats object for closest light curve """ vp_fn, dist_to_vp = vp_t db_path = DB_DIR + vp_fn[:-4] + ".dbdb" db = connect(DB_DIR + vp_fn[:-4] + ".dbdb") s_ts = standardize(ts) # Identify light curves in selected vantage db that are up to 2x the distance # that the time series is from the vantage point lc_candidates = db.chop(2 * dist_to_vp) db.close() # Vantage point is ts to beat as we search through candidate light curves min_dist = dist_to_vp closest_ts_fn = vp_fn closest_ts = load_ts(vp_fn) for d_to_vp, ts_fn in lc_candidates: candidate_ts = load_ts(ts_fn) dist_to_ts = kernel_dist(standardize(candidate_ts), s_ts) if (dist_to_ts < min_dist): min_dist = dist_to_ts closest_ts_fn = ts_fn closest_ts = candidate_ts return (min_dist, closest_ts_fn, closest_ts)