#!/usr/bin/python
# -*- coding: utf-8 -*-
# --------------------------------------------------
# File Name: dtw_mesh.py
# Purpose:
# Creation Date: 24-03-2017
# Last Modified: Fri, Mar 24, 2017 3:30:06 PM
# Author(s): Mike Stout
# Copyright 2017 The Author(s) All Rights Reserved
# Credits:
# --------------------------------------------------
from visual import *
import numpy as np
# Map values tp rgb ...
def rgbs(x):
def f(y): return min(max(0, 1-abs(1-3*(x-y))), 1)
r,g,b = map(f, [.5,.25,0])
return r,g,b
w=256
# Read text from file ...
def read(a):
with open(a) as f:
#s= f.readlines()
s= f.read()
return s
x = read("Ham_F1.html.txt")
y = read("Ham_Q1.html.txt")
# Codecs char <--> ascii values
def enc(xs): return map(ord, xs[:w])
def dec(xs): return ''.join(map(chr, xs[:w]))
x_ = enc(x)
y_ = enc(y)
print dec(x_)
print dec(y_)
from mlpy import dtw_subsequence
dist, cost, path = dtw_subsequence(x_ ,y_)
h = w/2.
# Calc elevation from cost matrix ....
def toZ((x,y)):
return (x-h, y-h, -h + 2*np.sqrt(cost[x][y]))
# Calc color value from cost matrix ....
def toCol((x,y)):
return rgbs(cost[x][y]/10000.)
# Setup mesh ...
grid = [ [ (x,y) for x in xrange(w) ] for y in xrange(w) ]
grid_ = [ [ (x,y) for y in xrange(w) ] for x in xrange(w) ]
# Render the mesh ...
for row in grid:
#points(pos=map(toZ, row), color=map(toCol, row), size=5 )
curve(pos=map(toZ, row), color=map(toCol, row)) # , radius=1 )
for row in grid_:
curve(pos=map(toZ, row), color=map(toCol, row)) # , radius=1 )
# Render the DTW Path ...
points( pos=[ toZ((x,y)) for x,y in zip(path[0],path[1])] , color=(1,1,1), size=20 )
# Add text ....
def txt(s,ps):
label(text=s
, height=24
, pos=ps
, align='center'
, depth=-.003
#, axis=ax
, box=0
# , linecolor=(0,0,0)
, opacity=0
, color=color.white)
# Add Axis Labels and Title ...
k = -h/2
txt("Hamlet",(0,h,k))
txt("F1",(0,-h,k))
txt("Q1",(-h,0,k))