annotate novelty.py @ 1:c11ea9e0357f

adding funcs
author mitian
date Thu, 02 Apr 2015 22:16:38 +0100
parents 26838b1f560f
children 294f66d285af
rev   line source
mi@0 1 #!/usr/bin/env python
mi@0 2 # encoding: utf-8
mi@0 3 """
mi@0 4 novelty.py
mi@0 5
mi@0 6 Created by mi tian on 2015-04-02.
mi@0 7 Copyright (c) 2015 __MyCompanyName__. All rights reserved.
mi@0 8 """
mi@0 9
mi@0 10 import sys, os
mi@0 11 import numpy as np
mi@0 12 from scipy.signal import correlate2d, convolve2d
mitian@1 13 import matplotlib.pyplot as plt
mi@0 14
mi@0 15 # from utils.PeakPickerUtil import PeakPicker
mi@0 16
mi@0 17 def getNoveltyCurve(ssm, kernel_size, normalise=False):
mi@0 18 '''Return novelty score from ssm.'''
mi@0 19
mi@0 20 kernel_size = int(np.floor(kernel_size/2.0) + 1)
mi@0 21 stripe = getDiagonalSlice(ssm, kernel_size)
mi@0 22 kernel = gaussian_kernel(kernel_size)
mi@0 23 xc = convolve2d(stripe,kernel,mode='same')
mi@0 24 xc[abs(xc)>1e+10]=0.00001
mi@0 25
mi@0 26 novelty = xc[int(np.floor(xc.shape[0]/2.0)),:]
mi@0 27 novelty = [0.0 if (np.isnan(x) or np.isinf(x) or x > 1e+100) else x for x in novelty]
mi@0 28
mi@0 29 if normalise:
mi@0 30 novelty = (novelty - np.min(novelty)) / (np.max(novelty) - np.min(novelty))
mi@0 31 return novelty
mi@0 32
mi@0 33 def getDiagonalSlice(ssm, width):
mi@0 34 ''' Return a diagonal stripe of the ssm given its width, with 45 degrees rotation.
mi@0 35 Note: requres 45 degrees rotated kernel also.'''
mi@0 36 w = int(np.floor(width/2.0))
mi@0 37 length = len(np.diagonal(ssm))
mi@0 38 stripe = np.zeros((2*w+1,length))
mi@0 39 # print 'diagonal', length, w, stripe.shape
mi@0 40 for i in xrange(-w, w+1) :
mi@0 41 stripe[w+i,:] = np.hstack(( np.zeros(int(np.floor(abs(i)/2.0))), np.diagonal(ssm,i), np.zeros(int(np.ceil(abs(i)/2.0))) ))
mi@0 42 return stripe
mi@0 43
mi@0 44 def gaussian_kernel(size):
mi@0 45 '''Create a gaussian tapered 45 degrees rotated checkerboard kernel.
mi@0 46 TODO: Unit testing: Should produce this with kernel size 3:
mi@0 47 0.1353 -0.3679 0.1353
mi@0 48 0.3679 1.0000 0.3679
mi@0 49 0.1353 -0.3679 0.1353
mi@0 50 '''
mi@0 51 n = float(np.ceil(size / 2.0))
mi@0 52 kernel = np.zeros((size,size))
mi@0 53 for i in xrange(1,size+1) :
mi@0 54 for j in xrange(1,size+1) :
mi@0 55 gauss = np.exp( -4.0 * (np.square( (i-n)/n ) + np.square( (j-n)/n )) )
mi@0 56 # gauss = 1
mi@0 57 if np.logical_xor( j - n > np.floor((i-n) / 2.0), j - n > np.floor((n-i) / 2.0) ) :
mi@0 58 kernel[i-1,j-1] = -gauss
mi@0 59 else:
mi@0 60 kernel[i-1,j-1] = gauss
mi@0 61
mi@0 62 return kernel
mi@0 63
mitian@1 64 def process(ssm, kernel_size, peak_picker, normalise=False, plot=False):
mi@0 65 '''Detect segment boundaries in the ssm.'''
mi@0 66 novelty = getNoveltyCurve(ssm, kernel_size, normalise=False)
mi@0 67 smoothed_novelty, novelty_peaks = peak_picker.process(novelty)
mi@0 68
mitian@1 69 if plot:
mitian@1 70 plot_detection(smoothed_novelty, novelty_peaks)
mitian@1 71 return novelty, smoothed_novelty, novelty_peaks
mitian@1 72
mitian@1 73 def plot_detection(smoothed_novelty, novelty_peaks):
mitian@1 74 pass