def test_calculate_fromts(self): matrix = DTW(self.xts, self.yts).calculate_cost()[0] acc_cost = [[1., 1., 1., 2., 6., 7., 7.], [2., 1., 1., 2., 6., 7., 7.], [6., 2., 2., 1., 2., 2., 3.], [15., 6., 6., 2., 1., 2., 6.], [19., 7., 7., 2., 2., 1., 2.], [19., 8., 8., 6., 11., 5., 2.]] acc_cost = numpy.matrix(acc_cost) self.assertEqual(matrix.all(), acc_cost.all())
def test(self): CTGF = TimeSeries(self.CTGF) smad7 = TimeSeries(self.smad7) CTGF.interpolate(inplace=True) smad7.interpolate(inplace=True) CTGF.norm(inplace=True) smad7.norm(inplace=True) dtw = DTW(CTGF, smad7) fig = dtw.cost_plot() fig = dtw.plot() plt.show() self.assertTrue(isinstance(fig, Figure))
def dist_matrix(self): dct = {} matrix = numpy.zeros((len(self.tsg), len(self.tsg))) matrix[:] = numpy.nan df = pandas.DataFrame(matrix, columns=self.tsg.features, index=self.tsg.features) comb = [i for i in combinations(self.tsg.features, 2)] for i, j in comb: cost = DTW(self.tsg[i], self.tsg[j]).cost df.loc[i, j] = cost df.loc[j, i] = cost return df
def test2(self): dir = r'/home/b3053674/Documents/Microarray/GSS2265/python' tsg1 = TimeSeriesGroup(self.data.iloc[:10]) tsg1 = tsg1.norm() tsg1.interpolate('linear', 30) ts_l = tsg1.to_ts() ts = ts_l[0] ts2 = ts_l[2] fname_plot = os.path.join(dir, 'CTGFVsCTGF.png') fname_map = os.path.join(dir, 'CTGFVsCTGFMap.png') fname_normed_plot = os.path.join(dir, 'CTGFVsCTGFnormed.png') fname_normed_map = os.path.join(dir, 'CTGFVsCTGFnormedMap.png') fname_normed_interp_plot = os.path.join(dir, 'CTGFVsCTGFnormedInterp.png') fname_normed_interp_map = os.path.join(dir, 'CTGFVsCTGFnormedInterpMap.png') d = DTW(ts, ts2) fig = d.plot() fig.savefig(fname_normed_interp_plot, dpi=300, bbox_inches='tight') fig2 = d.cost_plot() fig2.savefig(fname_normed_interp_map, dpi=300, bbox_inches='tight')
def compute_dtw(self, y): return DTW(self.x, y)
def compute_dtw(self, vec): x = self.clusters[vec[0]].centroid_by_eucl y = self.clusters[vec[1]].centroid_by_eucl dtw = DTW(x, y, dist=self.dtw_dist) return vec[0], vec[1], dtw.cost
def dtw_wrapper(x, y, axis=1): return DTW(x, y).cost
df = pandas.read_excel(os.path.join(dire, 'MicroarrayDEGAgeravedData.xlsx'), index_col=[0, 1]) df = df.transpose() df = df['TGFb'] / df['Control'] # put all other information in db as well so that plots can be produced from reading it perm = [i for i in itertools.combinations(list(df.index), 2)] DOALL = True if DOALL: for x_gene, y_gene in perm: dwt = DTW(df.loc[x_gene], df.loc[y_gene]) path = pickle.dumps(dwt.path) db.execute("""INSERT INTO dwt_data ( x, y, cost, path) VALUES ('{}', '{}', {}, '{}')""".format( x_gene, y_gene, dwt.cost, path ) ) db.commit() # dtw1pair()
def test_from_df_list(self): l = [DTW(self.CTGF, self.smad7), DTW(self.smad7, self.CTGF)] df = pandas.DataFrame(l) self.assertTrue(isinstance(df.iloc[0, 0].cost, float))
def test_real_data(self): dtw = DTW(self.CTGF, self.smad7) self.assertTrue(isinstance(dtw, DTW))
def test_distance_cost_plot(self): dtw = DTW(self.xts, self.yts) fig = dtw.cost_plot() from matplotlib.figure import Figure self.assertTrue(isinstance(fig, Figure))
def test_find_best_path_fromts(self): path, cost = DTW(self.xts, self.yts).find_best_path() path_answer = [[0, 0], [0, 1], [1, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]] cost_ans = 2.0 self.assertListEqual(path_answer, path) self.assertEqual(cost_ans, cost)
def test_same_example_from_DTW(self): x = numpy.array([1, 1, 2, 3, 2, 0]) y = numpy.array([0, 1, 1, 2, 3, 2, 1]) dtw1 = DTW(x, y) dtw2 = FastDTW(x, y, radius=1) self.assertAlmostEqual(dtw1.cost, dtw2.cost)