Dynamic Time Warping from Excel Workbooks

python

#1

For my Preparations for Industrial Careers Math class, my team are working on a project.

We want the code to take lines in excel data and dynamic time warp them so the peaks will match as they can be averaged.

Can anyone help?

Here is the code so far:

import numpy as np
import xlrd
import matplotlib.pyplot as plt

**def euclidian_distance(x, y): **
** d = x-y **
** return abs(x-y)**

**def fill_dist_array(x, y, d_f): **
** tmp_d_a = np.ndarray((x.shape[0], y.shape[0])) **
** for i in range(x.shape[0]): **
** for j in range(y.shape[0]): **
** tmp_d_a[i,j] = d_f(i,j) **
** return tmp_d_a[i,j] **


**def DTW(x, y, dist_function=None, dist_array=None): **
** d_f = None **
** d_a = dist_array **
** if dist_function == None and dist_array == None: **
** d_f = lambda i,j: euclidian_distance(x[i], y[j]) **
** if dist_function != None: **
** d_f = lambda i,j: dist_function(x[i], y[j]) **
** else:**
** if fill_dist_array != None: **
** d_f = lambda i,j:dist_array[i][j] **
** cost = np.empty((x.shape[0], y.shape[0]), dtype=np.float)**
** cost[0,0]=d_f(0,0)**
** N = x.shape[0] **
** for i in range(1, N): **
** cost[i,0] = d_f(i,0) **
** M = y.shape[0] **
** for j in range(1, M): **
** cost[0,j] = d_f(0,j) **
** for i in range(1, N): **
** for j in range(1, M): **
** cost[i,j] = d_f(i,j) + np.min((cost[i-1,j],cost[i-1,j-1],cost[i,j-1])) **
** print("This is the cost[N-1,M-1]: ",cost[N-1,M-1])**
** print()**
** print("This is the cost:",cost)**
** print()**
** print("This is the path x and y:",[i,j])**


**def test(): **
** a = str(input("Please enter the workbook name you would like as the reference sample followed by .xls: "))**
** c = str(input("Please enter the sheet name you would like to use: "))**
** v= int(input("Please enter the column you would like to print(ex: A=0, B=1): "))**
** wb = xlrd.open_workbook(a)**
** ws = wb.sheet_by_name(c)**
** num_rows = ws.nrows-1**
** curr_row=0**
** row_array = []**
** while curr_row < num_rows:**
** row = ws.row(curr_row)**
** row_array+= row**
** curr_row+= 1**
** col=wb.sheet_by_index(0)**
** aa= str(input("Please enter the workbook name followed by .xls: "))**
** cc = str(input("Please enter the sheet name: "))**
** vv = int(input("Please enter the column you would like to print(ex: A=0, B=1): "))**
** wbb = xlrd.open_workbook(aa)**
** wss = wbb.sheet_by_name(cc)**
** num_rowss = wss.nrows-1**
** curr_rows = 0**
** row_arrays = []**
** while curr_rows < num_rowss:**
** rows = wss.row(curr_rows)**
** row_arrays+= rows**
** curr_rows+= 1**
** cols=wbb.sheet_by_index(0)**
** x=np.array(col.col_values(v))**
** y=np.array(cols.col_values(vv))**
** DTW(x,y,euclidian_distance)**
** plt.plot(x)**
** plt.plot(y)**
** plt.show()**


if name == 'main':
** test()**


#2

@datasurfer20850,
At least re-edit your post so the used indentation's can be seen....

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#3

the way you posted that is very cofusing and hard to read. please format it.