wannesm / dtaidistance

Time series distances: Dynamic Time Warping (fast DTW implementation in C)
Other
1.09k stars 184 forks source link

using dtw_ndim with one column #158

Closed miladad8 closed 2 years ago

miladad8 commented 2 years ago

how can i use dtw_ndim.distance for one column that is list of lists? the shape of the column is looks like below: first_row= [[t11, lon11, lat11] , [t12, lon12, lat12],[t13, lon13, lat13],........] second_row= [[t21, lon21, lat21] , [t22, lon22, lat22],[t23, lon23, lat23],........] . .

I want to calculate path and distance between corresponding elements between rows(ex: [t11, lon11, lat11] and [t21, lon21, lat21] ). should i even use dtw_ndim.distance or other functions? i tried this:

for i in range(len(column)):
    ds = dtw_ndim.distance(column[i],column[i+1])
    if (i + 1) == df.shape[0]:  
        break          
    ds

but i get this error: TypeError: unsupported operand type(s) for -: 'list' and 'list'

wannesm commented 2 years ago

The example is not complete enough to answer and uses a strange data structure (i.e. how column is created and what the for loop intends to do). The easiest approach is to use numpy and follow the convention from the documentation (not tested):

First, use pyproj Transformer to project lat-lon on a cartesian coordinate system (to support Euclidean distance):

xy_path1 = np.empty((len(lonlat_path1),2), np.double)
transform = pyproj.Transformer.from_crs('EPSG:4326', 'EPSG:3395').transform
for idx, (lon, lat) in enumerate(lonlat_path1):
    xy_path1[idx,:] = transform(lon, lat)

Second, compare the paths you want:

dist_between_path1_path2 = dtw_ndim.distance(xy_path1, xy_path2)