Mercurial > hg > segmentation
diff utils/SegUtil.py @ 13:cc8ceb270e79
add some gmm ipynbs
author | mitian |
---|---|
date | Fri, 05 Jun 2015 18:02:05 +0100 |
parents | c23658e8ae38 |
children | 6dae41887406 |
line wrap: on
line diff
--- a/utils/SegUtil.py Mon May 25 17:27:48 2015 +0100 +++ b/utils/SegUtil.py Fri Jun 05 18:02:05 2015 +0100 @@ -278,7 +278,22 @@ 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 getMean(feature, winlen, stepsize): means = [] steps = int((feature.shape[0] - winlen + stepsize) / stepsize) @@ -336,10 +351,10 @@ def normaliseFeature(feature_array): - '''Normalise features column wisely.''' + '''Normalise features column wisely. Ensure numerical stability by adding a small constant.''' feature_array[np.isnan(feature_array)] = 0.0 feature_array[np.isinf(feature_array)] = 0.0 - feature_array = (feature_array - np.min(feature_array, axis=-1)[:,np.newaxis]) / (np.max(feature_array, axis=-1) - np.min(feature_array, axis=-1))[:,np.newaxis] + feature_array = (feature_array - np.min(feature_array, axis=-1)[:,np.newaxis]) / (np.max(feature_array, axis=-1) - np.min(feature_array, axis=-1) + 0.005)[:,np.newaxis] feature_array[np.isnan(feature_array)] = 0.0 feature_array[np.isinf(feature_array)] = 0.0 @@ -410,4 +425,28 @@ for i in xrange(size, len(envelope)-size-1): if envelope[i] > np.max(envelope[i-size:i]) and envelope[i] > np.max(envelope[i+1:i+size+1]): peaks.append(sig_env[i]) - return peaks \ No newline at end of file + return peaks + + +def deltaFeature(self, feature_array, step=1, axis=-1): + '''Return delta of a feature array''' + delta = np.zeros_like(feature_array) + delta[:, step:] = np.diff(feature_array, axis=axis) + return delta + + +def plotCurve(self, yp, yr, yf, x, labels): + '''Plot performance curve. + x axis: distance threshold for feature selection; y axis: f measure''' + + f = plt.figure() + ax = f.add_axes([0.1, 0.1, 0.7, 0.7]) + l1, l2, l3 = ax.plot(x, yp, 'rs-', x, yr, 'go-', x, yf, 'k^-') + f.legend((l1, l2, l3), ('Precision', 'Recall', 'F-measure'), 'upper left') + for i, label in enumerate(labels): + ax.annotate(label, (x[i], yf[i])) + plt.show() + plt.savefig('performance.pdf', format='pdf') + + return None +