def projected_warp_path(curve, refCurve): refCurve = scaler.fit_transform(refCurve.reshape(-1,1)).squeeze() curve = scaler.fit_transform(curve.reshape(-1,1)).squeeze() dist, cumcost = dtw.warping_paths_fast(refCurve, curve) steps = dtw.best_path(cumcost) # return steps of warp path # define Euclidean line l l_start = np.array([0,0]) l_end = np.array([len(refCurve)-1,len(curve)-1]) l = LineString([l_start, l_end]) # calculate orthogonal distance to l (sign corresponds to direction of projection) dist_orthogonal = np.cross(l_end-l_start,steps-l_start)/np.linalg.norm(l_end-l_start) # calculate coordinates z on l of orthogonal projections z = list() for step in steps: p = Point(step) z.append(l.project(p)) z = np.array(z)/l.length*100 # map projected warp path to vector of length 100 f = interpolate.interp1d(z, dist_orthogonal) mapped_projection = f(np.arange(0,100)) return dist, mapped_projection
def test_normalize2_prob(): psi = 0 if dtw.dtw_cc is not None: dtw.dtw_cc.srand(random.randint(1, 100000)) else: print("WARNING: dtw_cc not found") with util_numpy.test_uses_numpy() as np: s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0, 2, 1, 0, 0]) s2 = np.array([0., 1, 2, 3, 1, 0, 0, 0, 2, 1, 0, 0, 0]) d1, paths1 = dtw.warping_paths(s1, s2, psi=psi) d2, paths2 = dtw.warping_paths_fast(s1, s2, psi=psi) # print(np.power(paths1,2)) path1 = dtw.best_path(paths1) path2 = dtw.best_path(paths2) prob_paths = [] for i in range(30): prob_paths.append(dtw.warping_path_prob(s1, s2, d1/len(s1), psi=psi)) if not dtwvis.test_without_visualization(): if directory: fig, ax = dtwvis.plot_warpingpaths(s1, s2, paths1, path1) for p in prob_paths: py, px = zip(*p) py = [pyi + (random.random() - 0.5) / 5 for pyi in py] px = [pxi + (random.random() - 0.5) / 5 for pxi in px] ax[3].plot(px, py, ".-", color="yellow", alpha=0.25) fig.savefig(directory / "normalize2_prob.png") np.testing.assert_almost_equal(d1, d2, decimal=4) np.testing.assert_almost_equal(paths1, paths2, decimal=4) np.testing.assert_almost_equal(path1, path2, decimal=4)
def test_psi_dtw_1d(): with util_numpy.test_uses_numpy() as np: x = np.arange(0, 20, .5) s1 = np.sin(x) s2 = np.sin(x - 1) random.seed(1) for idx in range(len(s2)): if random.random() < 0.05: s2[idx] += (random.random() - 0.5) / 2 # print(f's1 = [' + ','.join(f'{vv:.2f}' for vv in s1) + ']') # print(f's2 = [' + ','.join(f'{vv:.2f}' for vv in s2) + ']') # print('distance_fast') d1 = dtw.distance_fast(s1, s2, psi=2) # print(f'{d1=}') # print('warping_paths') d2, paths = dtw.warping_paths(s1, s2, window=25, psi=2) # print(f'{d2=}') with np.printoptions(threshold=np.inf, linewidth=np.inf): print(paths) # print('warping_paths fast') d3, paths = dtw.warping_paths_fast(s1, s2, window=25, psi=2) # print(f'{d3=}') # print(paths) # print('best_path') best_path = dtw.best_path(paths) if not dtwvis.test_without_visualization(): if directory: dtwvis.plot_warpingpaths(s1, s2, paths, best_path, filename=directory / "test_psi_dtw_1d.png") np.testing.assert_almost_equal(d1, d2) np.testing.assert_almost_equal(d1, d3)
def test_bug_size(): """Two series of length 1500 should not trigger a size error. The warping paths matrix is of size 1501**2 = 2_253_001. If using 64bit values: 1501**2*64/(8*1024*1024) = 17.2MiB. """ with util_numpy.test_uses_numpy() as np: s1 = np.random.rand(1500) s2 = np.random.rand(1500) d1, _ = dtw.warping_paths_fast(s1, s2) d2, _ = dtw.warping_paths(s1, s2) assert d1 == pytest.approx(d2)
def test_normalize2(): with util_numpy.test_uses_numpy() as np: s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0, 2, 1, 0, 0]) s2 = np.array([0., 1, 2, 3, 1, 0, 0, 0, 2, 1, 0, 0, 0]) d1, paths1 = dtw.warping_paths(s1, s2, psi=2) d2, paths2 = dtw.warping_paths_fast(s1, s2, psi=2) path1 = dtw.best_path(paths1) path2 = dtw.best_path(paths2) if directory: dtwvis.plot_warpingpaths(s1, s2, paths1, path1, filename=directory / "normalize.png") np.testing.assert_almost_equal(d1, d2, decimal=4) np.testing.assert_almost_equal(paths1, paths2, decimal=4) np.testing.assert_almost_equal(path1, path2, decimal=4)
def test(amplitude, center, width, noise, target_norm,len_a,window): source_norm = amplitude*norm.pdf(range(0,400),center,width) source_norm = np.random.normal(source_norm, scale=noise) d, paths = dtw.warping_paths_fast(target_norm, source_norm, window, psi=0) best_path = dtw.best_path(paths) paths = np.array(best_path) total_intensity = source_norm.sum() euclidean = d euclidean = np.exp(euclidean/total_intensity) init_euclidean = abs(paths[:,0]-paths[:,1]) amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise) return euclidean, init_euclidean
def test_subsequence(): with util_numpy.test_uses_numpy() as np: s1 = np.array([1., 2, 0]) s2 = np.array([1., 0, 1, 2, 1, 0, 1, 0, 0, 0, 0]) penalty = 0.1 psi = [0, 0, len(s2), len(s2)] d1, paths1 = dtw.warping_paths(s1, s2, penalty=penalty, psi=psi) d2, paths2 = dtw.warping_paths_fast(s1, s2, penalty=penalty, psi=psi) path1 = dtw.best_path(paths1) print(paths1) path2 = dtw.best_path(paths2) print(paths2) if not dtwvis.test_without_visualization(): if directory: dtwvis.plot_warpingpaths(s1, s2, paths1, path1, filename=directory / "subseq.png") np.testing.assert_almost_equal(d1, d2, decimal=4) np.testing.assert_almost_equal(paths1, paths2, decimal=4) np.testing.assert_almost_equal(path1, path2, decimal=4) np.testing.assert_almost_equal(paths1[3:4, 0:12][0], [np.inf,1.421,1.005,1.421,2.002,1.000,-1,-1,-1,-1,-1,-1], decimal=3)
def test(amplitude, center, width, noise, background, target_norm,len_a,window): source_norm = amplitude*norm.pdf(range(0,400),center,width) source_norm = np.random.normal(source_norm, scale=noise) background = (max(abs(source_norm)))/2 ''' plt.figure(5) plt.plot(range(0,400),source_norm) plt.plot(range(0,400),target_norm,'k-') ''' source_norm = source_norm + background ''' plt.figure(6) plt.plot(range(0,400),source_norm) plt.plot(range(0,400),target_norm,'k-') ''' ''' Choose the normalisation method by commenting/uncommenting below ''' target_norm,source_norm = target_norm/max(abs(target_norm)),source_norm/max(abs(source_norm)) ''' target_norm = preprocessing.normalize([target_norm]) target_norm = target_norm.reshape((400,)) source_norm = preprocessing.normalize([source_norm]) source_norm = source_norm.reshape((400,)) ''' ''' ''' target_deriv,source_deriv = deriv(target_norm),deriv(source_norm) target_deriv.insert(0,0) target_deriv.insert(len(target_deriv),0) source_deriv.insert(0,0) source_deriv.insert(len(source_deriv),0) target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv) d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0) best_path = dtw.best_path(paths) paths = np.array(best_path) total_intensity = (abs(source_norm)).sum() euclidean = pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2) euclidean = np.sqrt(euclidean.sum()) euclidean = np.exp(-euclidean/total_intensity) init_euclidean = abs(paths[:,0]-paths[:,1]) amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise) plt.figure(1) plt.plot(range(0,400),source_norm) #label='Altered Gaussian: mean='+center+', sd='+width+', amplitude='+amplitude+', noise='+noise plt.plot(range(0,400),target_norm,'k-') #plt.legend() plt.figure(2) plt.plot(range(0,400),source_deriv) plt.plot(range(0,400),target_deriv,'k-') return euclidean, init_euclidean
def test(amplitude, center, width, noise, target_norm,len_a,window,global_max, ii): source_no_noise = amplitude*norm.pdf(range(0,400),center,width) source_no_noise = np.random.normal(source_no_noise, scale=0) source_max = max(abs(source_no_noise)) source_norm = amplitude*norm.pdf(range(0,400),center,width) source_norm = np.random.normal(source_norm, scale=noise) source_global_norm = source_norm/global_max total_intensity = (abs(source_norm)).sum() t,s = target_norm,source_norm ''' Choose the normalisation method by commenting/uncommenting below ''' target_norm,source_norm = target_norm/max(abs(target_norm)),source_norm/source_max ''' target_norm = preprocessing.normalize([target_norm]) target_norm = target_norm.reshape((400,)) source_norm = preprocessing.normalize([source_norm]) source_norm = source_norm.reshape((400,)) ''' ''' ''' target,source = deriv(target_norm),deriv(source_global_norm) target.insert(0,0) target.insert(len(target),0) source.insert(0,0) source.insert(len(source),0) target,source = np.asarray(target),np.asarray(source) D = dtw.distance_fast(target,source,window,psi=0) target_deriv,source_deriv = deriv(target_norm),deriv(source_norm) target_deriv.insert(0,0) target_deriv.insert(len(target_deriv),0) source_deriv.insert(0,0) source_deriv.insert(len(source_deriv),0) target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv) d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0) best_path = dtw.best_path(paths) paths = np.array(best_path) ''' euclidean = pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2) euclidean = np.sqrt(euclidean.sum()) ''' euclidean = abs(t[paths[:,0]] - s[paths[:,1]]) euclidean = euclidean.sum() plt.figure(3) plt.scatter(ii,euclidean) plt.title('warping cost') euclidean = np.exp(-euclidean/(total_intensity/D)) init_euclidean = abs(paths[:,0]-paths[:,1]) amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise) plt.figure(1) plt.plot(range(0,400),source_norm) plt.figure(2) plt.plot(range(0,400),source_deriv) plt.plot(range(0,400),target_deriv,'k-') plt.figure(4) plt.scatter(ii,total_intensity) plt.title('total intensity') plt.figure(5) plt.scatter(ii,D) plt.title('D') return euclidean, init_euclidean, target_norm
from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter #%% np.random.seed(0) target_norm = norm.pdf(range(0,400),200,40) source_norm = norm.pdf(range(0,400),200,40) noise_t = target_norm.max()/5.0 noise_s = source_norm.max()/40.0 target_norm = np.random.normal(target_norm, scale=noise_t) source_norm = np.random.normal(source_norm, scale=noise_s) #target_norm /= noise_t #source_norm /= noise_s d, paths = dtw.warping_paths_fast(target_norm, source_norm, window=1000, psi=0) best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(target_norm, source_norm, paths, best_path) paths = np.array(best_path) total_intensity = (abs(source_norm)).sum() euclidean = np.sqrt(pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2)) #euclidean = np.exp(-euclidean/total_intensity) correction = np.sqrt(euclidean.sum()) correction = np.exp(-correction/total_intensity) init_euclidean = abs(target_norm-source_norm) cov = np.correlate(source_norm, target_norm,mode='same')
template_deriv = deriv(template) query_deriv.insert(0, 0) query_deriv.insert(len(query_deriv), 0) template_deriv.insert(0, 0) template_deriv.insert(len(template_deriv), 0) query_deriv, template_deriv = np.asarray(query_deriv), np.asarray( template_deriv) plt.figure() plt.plot(query) plt.plot(template) d, paths = dtw.warping_paths_fast(query_deriv, template_deriv, window=1000, psi=0) best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(query, template, paths, best_path) best_path = np.asarray(best_path) L = np.arange(0, len(best_path), 20) x1, x2, y1, y2 = [], [], [], [] for i in L: x11 = best_path[i, 0] x22 = best_path[i, 1] y11 = d1[x11] y22 = D2[x22]
def test(amplitude, center, width, noise, background, target_norm,len_a,window,global_max, ii): source_no_noise = amplitude*norm.pdf(range(0,400),center,width) source_no_noise = np.random.normal(source_no_noise, scale=0) source_max = max(abs(source_no_noise)) background = -source_max/2 #background = 0.04 source_no_noise = source_no_noise + background source_max = max(abs(source_no_noise)) source_norm = amplitude*norm.pdf(range(0,400),center,width) source_norm = np.random.normal(source_norm, scale=noise) source_norm = source_norm + background source_global_norm = source_norm/global_max #total_intensity = (abs(source_norm)).sum() t,s = target_norm,source_norm ''' Choose the normalisation method by commenting/uncommenting below ''' target_norm,source_norm = target_norm/max(abs(target_norm)),source_norm/source_max ''' target_norm = preprocessing.normalize([target_norm]) target_norm = target_norm.reshape((400,)) source_norm = preprocessing.normalize([source_norm]) source_norm = source_norm.reshape((400,)) ''' ''' ''' total_intensity = (abs(source_norm)).sum() target,source = deriv(target_norm),deriv(source_global_norm) target.insert(0,target[0]) target.insert(len(target),target[len(target)-1]) source.insert(0,source[0]) source.insert(len(source),source[len(source)-1]) target,source = np.asarray(target),np.asarray(source) D = dtw.distance_fast(target,source,window,psi=0) target_deriv,source_deriv = deriv(target_norm),deriv(source_norm) target_deriv.insert(0,target_deriv[0]) target_deriv.insert(len(target_deriv),target_deriv[len(target_deriv)-1]) source_deriv.insert(0,source_deriv[0]) source_deriv.insert(len(source_deriv),source_deriv[len(source_deriv)-1]) target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv) d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0) best_path = dtw.best_path(paths) paths = np.array(best_path) ''' euclidean = pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2) euclidean = np.sqrt(euclidean.sum()) ''' euclidean = abs(t[paths[:,0]] - s[paths[:,1]]) euclidean = euclidean.sum() C = ['#000000','#FF0000','#FFFF00','#008000','#0000FF','#FF00FF','#C0C0C0','#800000','#808000','#00FFFF','#800080','#808080','#00FF00','#008080','#000080'] plt.figure(2) plt.scatter(ii,euclidean,c=C[ii%15]) plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key') plt.title('warping cost') euclidean = np.exp(-euclidean/(total_intensity/D)) init_euclidean = abs(paths[:,0]-paths[:,1]) amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise) plt.figure(5) plt.plot(range(0,400),s,c=C[ii%15]) plt.plot(range(0,400),t,'k-') plt.title('source and target plots before normalisation and with noise') plt.figure(6) plt.plot(range(0,400),source_no_noise,c=C[ii%15]) plt.plot(range(0,400),t,'k') plt.title('source and target plots before normalisation and without noise') plt.figure(7) plt.plot(range(0,400),source_norm,c=C[ii%15]) plt.plot(range(0,400),target_norm,'k-') plt.title('source and target plots after normalisation and with noise') plt.figure(8) plt.plot(range(0,400),source_deriv,c=C[ii%15]) plt.plot(range(0,400),target_deriv,'k-') plt.title('source and target derivative plots after normalisation') plt.figure(3) plt.scatter(ii,total_intensity,c=C[ii%15]) plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key') plt.title('total intensity') plt.figure(4) plt.scatter(ii,D,c=C[ii%15]) plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key') plt.title('D') return euclidean, init_euclidean
def test(amplitude, center, width, noise, sdhalf, target_norm,len_a,window,global_max, ii): b = 0 source_norm1 = skewnorm.pdf(np.arange(0,201),b,200,40) source_norm2 = skewnorm.pdf(np.arange(200,400),b,200,sdhalf) source_norm = np.concatenate((source_norm1[0:200],source_norm2)) t,s = target_norm,source_norm source_global_norm1,source_global_norm2 = source_norm1/global_max,source_norm2/global_max source_global_norm2 = source_global_norm2 - abs(min(target_norm[201:400])-min(source_global_norm2)) source_global_norm2 = source_global_norm2/max(source_global_norm2) source_global_norm = np.concatenate((source_global_norm1[0:200],source_global_norm2)) source_norm1,source_norm2 = source_norm1/max(source_norm1),source_norm2/max(source_norm2) source_norm2 = source_norm2 - abs(min(target_norm[201:400])-min(source_norm2)) source_norm2 = source_norm2/max(source_norm2) source_norm = np.concatenate((source_norm1[0:200],source_norm2)) #total_intensity = (abs(source_norm)).sum() ''' Choose the normalisation method by commenting/uncommenting below ''' target_norm = target_norm/max(abs(target_norm)) ''' target_norm = preprocessing.normalize([target_norm]) target_norm = target_norm.reshape((400,)) source_norm = preprocessing.normalize([source_norm]) source_norm = source_norm.reshape((400,)) ''' source_norm = np.random.normal(source_norm, scale=noise) ''' ''' total_intensity1 = (source_norm).sum() total_intensity2 = (target_norm).sum() total_intensity = abs(total_intensity1 - total_intensity2) total_intensity1 = (abs(source_norm)).sum() target,source = deriv(target_norm),deriv(source_global_norm) target.insert(0,target[0]) target.insert(len(target),target[len(target)-1]) source.insert(0,source[0]) source.insert(len(source),source[len(source)-1]) target,source = np.asarray(target),np.asarray(source) D = dtw.distance_fast(target,source,window,psi=0) target_deriv,source_deriv = deriv(target_norm),deriv(source_norm) target_deriv.insert(0,target_deriv[0]) target_deriv.insert(len(target_deriv),target_deriv[len(target_deriv)-1]) source_deriv.insert(0,source_deriv[0]) source_deriv.insert(len(source_deriv),source_deriv[len(source_deriv)-1]) target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv) d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0) best_path = dtw.best_path(paths) paths = np.array(best_path) ''' euclidean = pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2) euclidean = np.sqrt(euclidean.sum()) ''' euclidean = abs(t[paths[:,0]] - s[paths[:,1]]) euclidean = euclidean.sum() C = ['#000000','#FF0000','#FFFF00','#008000','#0000FF','#FF00FF','#C0C0C0','#800000','#808000','#00FFFF','#800080','#808080','#00FF00','#008080','#000080'] ''' plt.figure(2) plt.scatter(ii,euclidean,c=C[ii%15]) plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key') plt.title('warping cost') ''' #n = noise/0.01 #euclidean = np.exp(-euclidean*(total_intensity/D)) euclidean = np.exp(-(euclidean/(total_intensity1/D))-total_intensity/(total_intensity2)*D) #euc1 = np.exp(-euclidean/(total_intensity1/D)) #euc2 = np.exp(-total_intensity/(total_intensity1)*D) init_euclidean = abs(paths[:,0]-paths[:,1]) amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise) plt.figure(5) plt.plot(range(0,400),s,c=C[ii%15]) plt.plot(range(0,400),t,'k-') plt.title('source and target plots before normalisation and with noise') ''' plt.figure(6) plt.plot(range(0,400),source_no_noise,c=C[ii%15]) plt.plot(range(0,400),t,'k') plt.title('source and target plots before normalisation and without noise') ''' plt.figure(7) plt.plot(range(0,400),source_norm,c=C[ii%15]) plt.plot(range(0,400),target_norm,'k-') plt.title('source and target plots after normalisation and with noise') ''' plt.figure(8) plt.plot(range(0,400),source_deriv,c=C[ii%15]) plt.plot(range(0,400),target_deriv,'k-') plt.title('source and target derivative plots after normalisation') ''' plt.figure(3) plt.scatter(ii,total_intensity1,c=C[ii%15]) plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key') plt.title('total intensity') ''' plt.figure(4) plt.scatter(ii,D,c=C[ii%15]) plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key') plt.title('D') ''' return euclidean, init_euclidean
D = dtw.distance_fast(target,source,window,psi=0) target_deriv,source_deriv = deriv(target_norm),deriv(source_norm) target_deriv.insert(0,target_deriv[0]) target_deriv.insert(len(target_deriv),target_deriv[len(target_deriv)-1]) source_deriv.insert(0,source_deriv[0]) source_deriv.insert(len(source_deriv),source_deriv[len(source_deriv)-1]) target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv) d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0) best_path = dtw.best_path(paths) paths = np.array(best_path) euclidean = abs(t[paths[:,0]] - s[paths[:,1]]) euclidean = euclidean.sum() exp = np.exp(-(euclidean/(total_intensity1/D))-total_intensity/total_intensity1) plt.figure() plt.plot(t) plt.plot(s) plt.figure()
def d(): dd, _ = dtw.warping_paths_fast(s1, s2, window=window, compact=True) return dd
def d(): dd, _ = dtw.warping_paths_fast(s1, s2, window=window) print(dd) return dd