Mercurial Hosting > traffic-intelligence
diff python/utils.py @ 366:90bdabc06e9f
updated LCSS to be more generic with a single similarity function
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 15 Jul 2013 12:12:47 -0400 |
parents | a50a69e04c2a |
children | 2db4e76599a1 |
line wrap: on
line diff
--- a/python/utils.py Fri Jul 12 11:28:47 2013 -0400 +++ b/python/utils.py Mon Jul 15 12:12:47 2013 -0400 @@ -138,12 +138,15 @@ ######################### -# maths section +# sequence section ######################### -def LCSS(l1, l2, threshold, distance, delta = float('inf')): +def LCSS(l1, l2, similarityFunc, delta = float('inf')): '''returns the longest common subsequence similarity based on the threshold on distance between two elements of lists l1, l2 + similarityFunc returns True or False whether the two points are considered similar + + eg distance(p1, p2) < epsilon ''' from numpy import zeros, int as npint n1 = len(l1) @@ -151,23 +154,23 @@ similarity = zeros((n1+1,n2+1), dtype = npint) for i in xrange(1,n1+1): for j in xrange(max(1,i-delta),min(n2+1,i+delta+1)): - if distance(l1[i-1], l2[j-1])<=threshold: + if similarityFunc(l1[i-1], l2[j-1]): similarity[i][j] = similarity[i-1][j-1]+1 else: similarity[i][j] = max(similarity[i-1][j], similarity[i][j-1]) return max(max(similarity[:,-1]), max(similarity[-1,:])) -def normalizedLCSS(l1, l2, threshold, distance, delta = float('inf'), lengthMethod = min): +def normalizedLCSS(l1, l2, similarityFunc, delta = float('inf'), lengthMethod = min): ''' compute the normalized LCSS ie, the LCSS divided by the min or mean of the indicator lengths (using lengthMethod) lengthMethod = lambda x,y:float(x,y)/2''' - return float(LCSS(l1, l2, threshold, distance, delta))/lengthMethod(len(l1), len(l2)) + return float(LCSS(l1, l2, similarityFunc, delta))/lengthMethod(len(l1), len(l2)) -def DLCSS(l1, l2, threshold, distance, delta = float('inf'), lengthMethod = min): +def DLCSS(l1, l2, similarityFunc, delta = float('inf'), lengthMethod = min): ''' compute the LCSS distance''' - return 1-normalizedLCSS(l1, l2, threshold, distance, delta, lengthMethod) + return 1-normalizedLCSS(l1, l2, similarityFunc, delta, lengthMethod) -def alignedLCSS(_l1, _l2, threshold, distance, delta): +def alignedLCSS(_l1, _l2, similarityFunc, delta): '''returns the best matching if using a finite delta by shiftinig the series alignments''' if len(_l2) < len(_l1): # l1 is the shortest l1 = _l2 @@ -180,15 +183,19 @@ # for i in xrange(min(delta,n1), max(n1+n2-delta, n2+1)): # i is the alignment of the end of l1 in l2 # print l1[min(-i-1,n1):] # min(n1+n2-i,n1) # print l2[max(0,i-n1):] - # print LCSS(l1[min(-i-1,n1):], l2[max(0,i-n1):], threshold, distance, delta) - lcss = [LCSS(l1[min(-i-1,n1):], l2[max(0,i-n1):], threshold, distance, delta) for i in xrange(min(delta,n1), max(n1+n2-delta, n2+1))] + # print LCSS(l1[min(-i-1,n1):], l2[max(0,i-n1):], similarityFunc, delta) + lcss = [LCSS(l1[min(-i-1,n1):], l2[max(0,i-n1):], similarityFunc, delta) for i in xrange(min(delta,n1), max(n1+n2-delta, n2+1))] return max(lcss) -def normalizedAlignedLCSS(l1, l2, threshold, distance, delta, lengthMethod = min): - return float(alignedLCSS(l1, l2, threshold, distance, delta))/lengthMethod(len(l1), len(l2)) +def normalizedAlignedLCSS(l1, l2, similarityFunc, delta, lengthMethod = min): + return float(alignedLCSS(l1, l2, similarityFunc, delta))/lengthMethod(len(l1), len(l2)) -def alignedDLCSS(l1, l2, threshold, distance, delta, lengthMethod = min): - return 1-normalizedAlignedLCSS(l1, l2, threshold, distance, delta, lengthMethod) +def alignedDLCSS(l1, l2, similarityFunc, delta, lengthMethod = min): + return 1-normalizedAlignedLCSS(l1, l2, similarityFunc, delta, lengthMethod) + +######################### +# maths section +######################### def framesToTime(nFrames, frameRate, initialTime = (0.,0.,0.)): 'returns hour, minutes and seconds'