Example for finding best patter similarities by using Distance Time Warping


In [ ]:
import os.path
import blaze
from blaze.ts.ucr_dtw import ucr

Some conversion code from text files into native Blaze format


In [ ]:
# Convert txt file into Blaze native format (if it is not yet)
def convert(filetxt, storage):
    if not os.path.exists(storage):
        blaze.Array(np.loadtxt(filetxt),
                    params=blaze.params(storage=storage))

In [ ]:
# Make sure that data is converted into a persistent Blaze array
convert("Data.txt", "Data")
convert("Query.txt", "Query")
convert("Query2.txt", "Query2")

Open Blaze array on-disk


In [ ]:
# Open Blaze arrays on-disk (will not be loaded in memory)
data = blaze.open("Data")
query = blaze.open("Query")
query2 = blaze.open("Query2")

Find the best similarities using DTW or ED (Euclidena Distance) algorithms


In [ ]:
# Play with different methods & parameters here...
#%time loc, dist = ucr.ed(data, query, 128)
%time loc, dist = ucr.dtw(data, query, 0.1, 128, verbose=False)
#%time loc, dist = ucr.dtw(data, query2, 0.1, 128)

Notice that times here can be up to 4x than the original code based on text files. Blaze format is fast to read!


In [ ]:
print "Location : ", loc
print "Distance : ", dist
print "Data Scanned : ", data.size

Check that patterns are really similar


In [ ]:
plot(data[loc:loc+128])

In [ ]:
plot(query[:])

In [ ]: