diff utils/SegUtil.py @ 17:c01fcb752221

new annotations
author mitian
date Fri, 21 Aug 2015 10:15:29 +0100
parents 8b814fe5781d
children
line wrap: on
line diff
--- a/utils/SegUtil.py	Wed Jun 17 18:02:33 2015 +0100
+++ b/utils/SegUtil.py	Fri Aug 21 10:15:29 2015 +0100
@@ -20,12 +20,13 @@
 from scipy.ndimage import filters, zoom
 from scipy import signal
 from scipy.signal import correlate2d, convolve2d, filtfilt, resample, butter
-import pylab as plt
+# import pylab as plt
 from scipy.spatial.distance import squareform, pdist
 from scipy.ndimage.filters import maximum_filter, minimum_filter, percentile_filter, uniform_filter
-from scipy.ndimage.filters import median_filter as med_filter
+from scipy.ndimage.filters import median_filter as med_filter # median_filter is a user defined function in this script
 from sklearn.metrics.pairwise import pairwise_distances
 
+from GmmMetrics import GmmDistance
 
 def lognormalize_chroma(C):
 	"""Log-normalizes chroma such that each vector is between -80 to 0."""
@@ -47,6 +48,7 @@
 	if not os.path.exists(directory):
 		os.makedirs(directory)
 
+
 def median_filter(X, M=8):
 	"""Median filter along the first axis of the feature matrix X."""
 	for i in xrange(X.shape[1]):
@@ -279,22 +281,6 @@
 
 	return rec
 
-def finiteMax(X):
-	'''Return the smallest finite value in the array'''
-	if not (np.isnan(X).any() or np.isposinf(X).any()):
-		return np.max(X)
-	data = np.sort(np.ndarray.flatten(X))
-	pos = np.where(data == np.inf)[0]
-	fMax = 0
-	return fMax
-
-def finiteMin(feature):
-	'''Return the smallest finite value in the array'''
-	if not (np.isnan(X).any() or np.isinf(X).any()):
-		return np.min(X)
-	fMin = 0
-	return fMin
-
 def lp(signal, fc=0.34, axis=-1):
 	'''Low pass filter function
 	signal: Raw signal to be smoothed.
@@ -304,6 +290,7 @@
 	lp_smoothed_signal = filtfilt(bCoeffs, aCoeffs, signal, axis)
 	return lp_smoothed_signal
 
+
 def hp(signal, fc=0.34, axis=-1):
 	'''Low pass filter function
 	signal: Raw signal to be smoothed.
@@ -312,7 +299,8 @@
 	bCoeffs, aCoeffs = butter(2, fc, 'highpass')
 	hp_smoothed_signal = filtfilt(bCoeffs, aCoeffs, signal, axis)
 	return hp_smoothed_signal
-	
+
+
 def getMean(feature, winlen, stepsize):
 	means = []
 	steps = int((feature.shape[0] - winlen + stepsize) / stepsize)
@@ -343,17 +331,18 @@
 	if norm == 'simple':
 		ssm = 1 - (dm - np.min(dm)) / (np.max(dm) - np.min(dm))
 	if norm == 'exp': # Use with cosine metric only
-		ssm = np.exp(dm - 1)
+		ssm = 1 - np.exp(dm - 1)
 	if reduce:
 		ssm = reduceSSM(ssm)
 	return ssm
 
+
 def enhanceSSM(ssm, fc=0.34, med_size=(5,5), max_size=(5,5), min_size=(5,5), filter_type='min', axis=-1):
 	'''A series of filtering for SSM enhancement
 	fc: cutoff frequency for LP filtering.
 	med_size: Median filter window size.
 			  int or tuple. If using an integer for a 2d input, axis must be specified.
-	filter_type: Select either to use maximum filter or minimum filter.
+	filter_type: Select either to use maximum filter or minimum filter according to the distance metric with which the SSM was computed.
 			float ['min', 'max', None]
 	max_size: Maximum filter window size.
 			  int or tuple. If using an integer for a 2d input, axis must be specified.
@@ -375,16 +364,17 @@
 	else:
 		enhanced_ssm = ssm_med
 	return enhanced_ssm
-	
+
+
 def reduceSSM(ssm, maxfilter_size = 2, remove_size=50):
 	'''Adaptive thresholding using OTSU method
-	Required package: skimage (0.10+)'''
+	Required package: skimage (0.10+) -- NOT installed on ignis, do NOT call this function!'''
 	
 	from skimage.morphology import disk
 	# from skimage.filters import threshold_otsu, rank #skimage 0.12
 	from skimage.filter.rank import otsu #skimage 0.10
 	from skimage.filter import threshold_otsu
-	
+
 	reduced_ssm = copy(ssm)
 	reduced_ssm[reduced_ssm<0.75] = 0
 	# # reduced_ssm = maximum_filter(reduced_ssm,size=maxfilter_size)
@@ -416,6 +406,25 @@
 
 	return feature_array		
 
+def normaliseArray(X):
+	'''Normalise 1d array.'''
+	if np.max(X) == np.min(X):
+		return None
+	return (X - np.min(X)) / (np.max(X) - np.min(X))
+	
+def pairwiseSKL(self, gmm_list):
+	'''Compute pairwise symmetrised KL divergence of a list of GMMs.'''
+	n_GMMs = len(gmm_list)
+	distance_matrix = np.zeros((n_GMMs, n_GMMs))
+	for i in xrange(n_GMMs):
+		for j in xrange(i, n_GMMs):
+			distance_matrix[i][j] = gmm_list[i].skl_distance_full(gmm_list[j])
+			distance_matrix[j][i] = distance_matrix[i][j]
+
+	np.fill_diagonal(distance_matrix, 0.0)
+	distance_matrix[np.isinf(distance_matrix)] = np.finfo(np.float64).max
+	
+	return distance_matrix
 
 def getRolloff(data, tpower, filterbank, thresh=0.9):
 	nFrames = data.shape[0]