comparison experiment-reverb/code/supervised_training_pca.py @ 0:246d5546657c

initial commit, needs cleanup
author Emmanouil Theofanis Chourdakis <e.t.chourdakis@qmul.ac.uk>
date Wed, 14 Dec 2016 13:15:48 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:246d5546657c
1 #!/usr/bin/python2
2 # -*- coding: utf-8 -*-
3 """
4 Created on Thu Apr 23 11:53:17 2015
5
6 @author: mmxgn
7 """
8
9 # This file does the cluster estimation and the removal of outliers
10
11 from sys import argv, exit
12 from essentia.standard import YamlInput, YamlOutput
13 from essentia import Pool
14 from pca import *
15
16 from numpy import *
17 from sklearn import cluster
18 from sklearn.metrics import pairwise_distances
19 from sklearn.cluster import KMeans, MiniBatchKMeans
20 import matplotlib.pyplot as plt
21 #from sklearn.mixture import GMM
22 from sklearn.naive_bayes import GaussianNB, MultinomialNB
23 from scipy.signal import decimate
24 from sklearn import cross_validation
25
26 #from hmmlearn import hmm
27 from hmmlearn.hmm import GMM
28 from hmmlearn import hmm
29 #from adpcm import adm, adm_reconstruct
30
31
32 mse = lambda A,B: ((array(A)-array(B)) ** 2).mean()
33
34
35 def smooth_matrix_1D(X):
36 window = scipy.signal.gaussian(51,4)
37 window = window/sum(window)
38 intermx = zeros((X.shape[0],X.shape[1]+100))
39 intermx[:, 50:-50] = X
40
41 for m in range(0, X.shape[0]):
42 # print intermx.shape
43 intermx[m,:] = convolve(ravel(intermx[m,:]), window,'same')
44
45 return intermx[:,50:-50]
46
47 def adm_reconstruct(codeword, h, dmin=.01, dmax=.28):
48 x = zeros((1, codeword.shape[1]))
49
50 delta1 = dmin
51 delta2 = dmin
52 Sum = h
53
54 x[0] = h
55 for i in range(0, codeword.shape[1]):
56 if codeword[0,i] == 0:
57 delta1 = dmin
58 delta2 = dmin
59
60 elif codeword[0,i] == 1:
61 delta2 = dmin
62 Sum += delta1
63 delta1 *= 2
64 if delta1 > dmax:
65 delta1 = dmax
66
67 elif codeword[0,i] == -1:
68 delta1 = dmin
69 Sum -= delta2
70 delta2 *= 2
71 if delta2 > dmax:
72 delta2 = dmax
73 x[0,i] = Sum
74 return x
75
76 def adm(x, dmin=.01, dmax=.28, tol=0.0001):
77
78 # Adaptive delta modulation adapted by code:
79 # (adeltamod.m)
80 #
81 # Adaptive Delta Modulator
82 # by Gandhar Desai (gdesai)
83 # BITS Pilani Goa Campus
84 # Date: 28 Sept, 2013
85
86 xsig = x
87
88 Lx = len(x)
89
90 ADMout = zeros((1, Lx))
91 codevec = zeros((1, Lx))
92
93
94 Sum = x[0]
95 delta1 = dmin
96 delta2 = dmin
97 mult1 = 2
98 mult2 = 2
99 for i in range(0, Lx):
100 #print abs(xsig[i] - Sum)
101 if (abs(xsig[i] - Sum) < tol):
102 bit = 0
103 delta2 = dmin
104 delta1 = dmin
105
106
107 elif (xsig[i] >= Sum):
108 bit = 1
109 delta2 = dmin
110 Sum += delta1
111 delta1 *= mult1
112 if delta1 > dmax:
113 delta1 = dmax
114
115
116 else:
117 bit = -1
118 delta1 = dmin
119 Sum -= delta2
120 delta2 *= mult2
121 if delta2 > dmax:
122 delta2 = dmax
123
124
125
126 ADMout[0, i] = Sum
127 codevec[0, i]= bit
128
129 return ADMout,codevec, x[0]
130
131 if __name__=="__main__":
132 if len(argv) != 2:
133 print "[EE] Wrong number of arguments"
134 print "[II] Correct syntax is:"
135 print "[II] \t%s <training_file>"
136 print "[II] where <training_file> is a .yaml file containing the"
137 print "[II] features of the dataset (try output2_stage/fulltraining-last.yaml)"
138 exit(-1)
139
140
141 n_clusters = 2
142 UpsamplingFactor = 1
143 dmin = 0.001
144 dmax = 0.28
145 tol = 0.001
146
147 infile = argv[1]
148
149 features_pool = YamlInput(filename = infile)()
150
151
152
153 feature_captions = features_pool.descriptorNames()
154 parameter_captions = []
155
156
157 for c in features_pool.descriptorNames():
158 if c.split('.')[0] == 'parameter':
159 parameter_captions.append(c)
160 if c.split('.')[0] == 'metadata' or c.split('.')[0] == 'parameter':
161 feature_captions.remove(c)
162
163
164
165 close('all')
166
167 print "[II] Loaded training data from %s (%s) " % (infile, features_pool['metadata.date'][0])
168 print "[II] %d Features Available: " % len(feature_captions)
169
170
171
172 print str(feature_captions).replace("', ","\n").replace('[','').replace("'","[II]\t ")[:-7]
173
174 nfeatures_in = len(feature_captions)
175 nparameters_in = len(parameter_captions)
176 1 features_vector = zeros((nfeatures_in, len(features_pool[feature_captions[0]])))
177
178 parameters_vector = zeros((nparameters_in, len(features_pool[parameter_captions[0]])))
179
180
181 for i in range(0, nfeatures_in):
182 features_vector[i, :] = features_pool[feature_captions[i]].T
183 for i in range(0, nparameters_in):
184 parameters_vector[i, :] = features_pool[parameter_captions[0]].T
185
186 print "[II] %d parameters used:" % len(parameter_captions)
187 print str(parameter_captions).replace("', ","\n").replace('[','').replace("'","[II]\t ")[:-7].replace('parameter.','')
188
189 print "[II] Marking silent parts"
190
191 silent_parts = zeros((1, len(features_pool[feature_captions[i]].T)))
192
193 rms = features_vector[feature_captions.index('rms'), :]
194
195 # Implementing Hysteresis Gate -- High threshold is halfway between
196 # the mean and the max and Low is halfway between the mean dn the min
197
198 rms_threshold_mean = mean(rms)
199
200 rms_threshold_max = max(rms)
201 rms_threshold_min = min(rms)
202
203 rms_threshold_high = 0.1 * rms_threshold_mean
204 rms_threshold_low = 0.01 * rms_threshold_mean
205
206 for n in range(1, len(rms)):
207 prev = rms[n-1]
208 curr = rms[n]
209
210 if prev >= rms_threshold_high:
211 if curr < rms_threshold_low:
212 silent_parts[0,n] = 1
213 else:
214 silent_parts[0,n] = 0
215 elif prev <= rms_threshold_low:
216 if curr > rms_threshold_high:
217 silent_parts[0,n] = 0
218 else:
219 silent_parts[0,n] = 1
220 else:
221 silent_parts[0,n] = silent_parts[0,n-1]
222
223
224 if silent_parts[0,1] == 1:
225 silent_parts[0, 0] = 1
226
227
228 # plot(rms)
229 # plot(silent_parts.T)
230 # plot(ones((len(rms), 1))*rms_threshold_high)
231 # plot(ones((len(rms), 1))*rms_threshold_low)
232
233 active_index = invert(silent_parts.flatten().astype(bool))
234
235 # Keep only active parts
236
237 # Uncomment this
238 features_vector = features_vector[:, active_index]
239 # parameters_vector = parameters_vector[:, active_index]
240
241 moments_vector = zeros((features_vector.shape[0], 2))
242
243 print "[II] Storing moments vector"
244 for i in range(0, features_vector.shape[0]):
245 mean_ = mean(features_vector[i,:])
246 std_ = std(features_vector[i,:], ddof=1)
247 moments_vector[i,0] = mean_
248 moments_vector[i,1] = std_
249
250 features_vector[i,:] = (features_vector[i,:] - mean_)/std_
251
252 features_vector_original = features_vector
253
254
255 print "[II] Extracting PCA configuration "
256
257 kernel, q, featurelist = extract_pca_configuration_from_data(features_vector)
258
259 print "[II] Optimal number of PCs to keep: %d" % q
260
261 feature_captions_array = array(feature_captions)
262
263 # features_to_keep = features_vector
264
265 features_to_keep = list(feature_captions_array[featurelist])
266 print "[II] Decided to keep %d features:" % len(features_to_keep)
267 print str(features_to_keep).replace("', ","\n").replace('[','').replace("'","[II]\t ")[:-7]
268
269
270
271 # Keep the desired features
272 #Uncomment this
273 features_kept_data = features_vector[featurelist,:]
274 # features_kept_data = features_vector
275 # features_kept_data = features_vector
276
277 # Generate the parameter clusters using k-means
278
279 # Uncomment this
280 features_vector = (kernel.T*features_kept_data)[0:q,:]
281 #features_vector = log(features_vector+0.001)
282 # features_vector = features_vector_original
283
284
285 # parameters_k_means = KMeans(n_clusters = parameters_k, init='k-means++', max_iter=300, tol=0.0000001, verbose = 1)
286 parameters_k_means = KMeans(init='k-means++', n_init=10, max_iter=300, tol=0.0000001, verbose = 0)
287 # # parameters_k_means = MiniBatchKMeans(init='k-means++', max_iter=300, tol=0.00001, verbose = 1)
288 #
289 # # Quantize the differences of the parameters instead of the parameters themselves
290 # parameters_vector_diff = concatenate((zeros((shape(parameters_vector)[0],1)),diff(parameters_vector, axis=1)),axis=1)
291 # features_vector_diff = concatenate((zeros((shape(features_vector)[0],1)),diff(features_vector,axis=1)),axis=1)
292 #
293 # # Delete this afterwards
294 # # features_vector = features_vector_diff
295 # parameters_k_means.fit(parameters_vector_diff.T)
296
297 print "[II] Trying ADM-coded parameters"
298 print "[II] Upsampling features and parameters by a factor of %d" % UpsamplingFactor
299
300
301 # Upsampled features and parameters
302 features_vector_upsampled = smooth_matrix_1D(repeat(features_vector,UpsamplingFactor, axis=1))
303
304 # feature_labels_upsampled = repeat(features_clustering_labels,UpsamplingFactor, axis=0)
305 parameters_vector_upsampled = repeat(parameters_vector,UpsamplingFactor, axis=1)
306
307 # parameters_vector_upsampled = smooth_matrix_1D(parameters_vector_upsampled)
308
309 parameters_vector_upsampled_adm = matrix(zeros(shape(parameters_vector_upsampled)))
310 parameters_vector_upsampled_code = matrix(zeros(shape(parameters_vector_upsampled)))
311 parameters_vector_upsampled_firstval = matrix(zeros((parameters_vector_upsampled.shape[0],1)))
312
313 # Reconstructed parameters
314
315 parameters_vector_upsampled_reconstructed = matrix(zeros(shape(parameters_vector_upsampled)))
316
317
318
319
320 def adm_matrix(X, dmin=0.001,dmax=0.28,tol=0.001):
321
322 out = matrix(zeros(shape(X)))
323 code = matrix(zeros(shape(X)))
324 firstval = matrix(zeros((X.shape[0], 1)))
325
326 for i in range(0, X.shape[0]):
327 out[i,:], code[i,:], firstval[i,0] = adm(X[i,:],dmin=dmin,dmax=dmax,tol=tol)
328
329 return out,code,firstval
330
331 # parameters_vector_upsampled_reconstructed[i,:] = adm_reconstruct(parameters_vector_upsampled_code[i,:],parameters_vector_upsampled_firstval[i,0], dmin=dmin,dmax=dmax)
332
333 def adm_matrix_reconstruct(code, firstval, dmin=0.001, dmax=0.28):
334 X = matrix(zeros(shape(code)))
335 for i in range(0, code.shape[0]):
336 X[i,:] = adm_reconstruct(code[i,:], firstval[i,0], dmin=dmin, dmax=dmax)
337
338 return X
339
340
341 parameters_vector_upsampled_adm, parameters_vector_upsampled_code, parameters_vector_upsampled_firstval = adm_matrix(parameters_vector_upsampled, dmin, dmax, tol)
342
343
344 def diff_and_pad(X):
345 return concatenate((
346 zeros((
347 shape(X)[0],
348 1
349 )),
350 diff(X, axis=1)),
351 axis=1)
352
353
354 # features_vector_upsampled = features_vector_upsampled
355
356
357 # features_vector_upsampled = diff_and_pad(features_vector_upsampled)
358
359
360 # features_vector_diff = concatenate((zeros((shape(features_vector)[0],1)),diff(features_vector,axis=1)),axis=1)
361
362
363
364 # Segmentation stuff
365
366
367
368 # for i in range(0, parameters_vector_upsampled.shape[0]):
369 # out, code, h = adm(parameters_vector_upsampled[i,:],dmin=dmin,dmax=dmax,tol=tol)
370 # parameters_vector_upsampled_adm[i,:] = out
371 # parameters_vector_upsampled_code[i,:] = code
372 # parameters_vector_upsampled_firstval[i, 0] = h
373
374 # parameters_k_means.fit(parameters_vector.T)
375 ##
376 # parameters_k_means_centers = parameters_k_means.cluster_centers_
377 # parameters_k_means_labels = parameters_k_means.labels_
378 ##
379 # parameters_vector_estimated = zeros(shape(parameters_vector))
380 ##
381 # for n in range(0, len(parameters_vector_estimated[0])):
382 # parameters_vector_estimated[:,n] = parameters_k_means_centers[parameters_k_means_labels[n]]
383 ##
384 ### plot(parameters_vector[0])
385 ## # plot(parameters_vector_estimated[0])
386 ##
387 ## # PROvLIMA EDW
388 # print "[II] Parameters MSE for %d clusters: %.3f" % (len(parameters_k_means.cluster_centers_), mse(parameters_vector, parameters_vector_estimated))
389 #
390 ##
391 print "[II] Clustering features."
392 #
393 features_clustering = GMM(n_components = n_clusters, covariance_type='full')
394 #
395 features_clustering.fit( features_vector_upsampled.T, y=parameters_vector_upsampled_code)
396 #
397 features_clustering_means = features_clustering.means_
398 features_clustering_labels = features_clustering.predict(features_vector_upsampled.T)
399 features_clustering_sigmas = features_clustering.covars_
400 #
401 features_vector_upsampled_estimated = zeros(shape(features_vector_upsampled))
402 #
403 #
404 for n in range(0, len(features_vector_upsampled_estimated[0])):
405 features_vector_upsampled_estimated[:,n] = features_clustering_means[features_clustering_labels[n]]
406 #
407 # # for n in range(0,features_vector.shape[0]):
408 # # hist(features_vector[1]-features_vector_estimated[1], 256)
409 # std(features_vector[1]-features_vector_estimated[1], ddof=1)
410 # mean(features_vector[1]-features_vector_estimated[1])
411 #
412 print "[II] Features MSE for %d clusters: %.3f" % (n_clusters, mse(features_vector_upsampled, features_vector_upsampled_estimated))
413
414
415
416 def cross_validate_clustering(data, estimator):
417 print "[II] Crossvalidating... "
418 estimator_fulldata = estimator
419 estimator_fulldata.fit(data)
420
421 # labels = estimator_fulldata.predict(data)
422 means = estimator_fulldata.means_
423 # print means
424
425 percents = arange(0.1,0.6,0.1)
426 MSEs = []
427 reconstructed = zeros(shape(data))
428 labels = estimator.predict(data)
429 for n in range(0, len(reconstructed)):
430 reconstructed[n,:] = means[labels[n]]
431
432 MSEs.append(mse(data,reconstructed))
433 for p in percents:
434 train,test = cross_validation.train_test_split(data,test_size=p,random_state=0)
435 train = matrix(train)
436 test = matrix(test)
437 # print shape(train)
438 # print shape(test)
439 estimator.fit(train)
440 means = estimator.means_
441 labels = estimator.predict(test)
442 reconstructed = zeros(shape(test))
443 for n in range(0, len(reconstructed)):
444 reconstructed[n,:] = means[labels[n]]
445
446 m = mse(test,reconstructed)
447
448 print "[II] MSE for clustering crossvalidated data %.2f-%.2f: %.5f" % ((1-p), p, m)
449 MSEs.append(m)
450
451 print "[II] Crossvalidation complete"
452
453 return MSEs
454
455 # print "[II] Trying Cross Validation"
456
457 # cross_validate_clustering(features_vector_upsampled.T, features_clustering)
458
459
460 # Construct parameters alphabet, each symbol is going to be a different column vector
461 # in parameter code matrix
462
463
464 def vector_to_states(X):
465 """
466 Input: a vector MxN with N samples and M variables
467 Output: a codeword dictionary `parameters_alphabet',
468 state_seq, inverse `parameters_alphabet_inv' """
469
470
471 parameters_alphabet = {}
472 n = 0
473
474 for i in range(0, X.shape[1]):
475 vec = tuple(ravel(X[:,i]))
476 if vec not in parameters_alphabet:
477 parameters_alphabet[vec] = n
478 n += 1
479
480 parameters_alphabet_inv = dict([(parameters_alphabet[m],m) for m in parameters_alphabet])
481
482 state_seq = array([parameters_alphabet[tuple(ravel(X[:,m]))] for m in range(0, parameters_vector_upsampled_code.shape[1])] )
483
484 return state_seq, parameters_alphabet, parameters_alphabet_inv
485
486
487 def states_to_vector(predicted, parameters_alphabet_inv):
488 estimated = matrix(zeros((len(parameters_alphabet_inv[0]), len(predicted))))
489 for i in range(0, len(state_seq)):
490 estimated[:, i] = matrix(parameters_alphabet_inv[predicted[i]]).T
491
492 return estimated
493
494 state_seq, parameters_alphabet, parameters_alphabet_inv = vector_to_states(parameters_vector_upsampled_code)
495
496
497 parameters_change_variable = matrix(diff_and_pad(parameters_vector_upsampled)!=0).astype(int)
498
499 changes_state_seq, changes_parameters_alphabet, changes_parameters_alphabet_inv = vector_to_states(parameters_change_variable)
500
501
502 # This is an hmm that just codes the changes"
503 # We have only two states, change and stay the same.
504
505
506 print "[II] Creating emission probability mixtures for every state "
507 parameters_state, parameter_state_alphabet, parameter_state_alphabet_inv = vector_to_states(parameters_vector_upsampled)
508
509 gmm_list_changes = []
510 for n in range(0, len(parameter_state_alphabet)):
511 vectors = features_vector_upsampled[:,parameters_state == n]
512 gmm = GMM(n_components=n_clusters, covariance_type = 'diag')
513 gmm.fit(vectors.T)
514 gmm_list_changes.append(gmm)
515
516
517 hmm_changes = hmm.GMMHMM(n_components=len(parameter_state_alphabet), gmms=array(gmm_list_changes),n_mix=n_clusters)
518 hmm_changes.fit([array(features_vector_upsampled).T])
519
520 # subplot(211)
521 # plot(parameters_change_variable.T)
522
523 subplot(211), plot(parameters_vector_upsampled.T)
524 subplot(212)
525
526 predicted_states = hmm_changes.predict(array(features_vector_upsampled.T))
527 predicted_states_estimated = states_to_vector(predicted_states, parameter_state_alphabet_inv)
528
529 plot(predicted_states_estimated.T)
530
531
532 # print "[II] (changes hmm-chain) Creating emission probability mixtures for every state "
533 #
534 # gmm_list_changes = []
535 # for n in range(0, 2):
536 # vectors = features_vector_upsampled[:,changes_state_seq == n]
537 # gmm = GMM(n_components=n_clusters, covariance_type = 'diag')
538 # gmm.fit(vectors.T)
539 # gmm_list_changes.append(gmm)
540 #
541 #
542 # hmm_changes = hmm.GMMHMM(n_components=2, gmms=array(gmm_list_changes),n_mix=n_clusters)
543 # hmm_changes.fit([array(features_vector_upsampled).T])
544 #
545 # subplot(211)
546 # plot(parameters_change_variable.T)
547 # subplot(212)
548 #
549 # changes_predicted_states = hmm_changes.predict(array(features_vector_upsampled.T))
550 # predicted_changes_estimated = states_to_vector(changes_predicted_states, changes_parameters_alphabet_inv)
551 #
552 # plot(predicted_changes_estimated.T)
553
554
555
556
557 # End of changes HMM here
558
559
560 print "[II] Creating emission probability mixtures for every state"
561 gmm_list = []
562 for n in range(0, 3):
563 vectors = features_vector_upsampled[:,state_seq == n]
564 gmm = GMM(n_components=n_clusters,covariance_type = 'diag')
565 gmm.fit(vectors.T)
566 gmm_list.append(gmm)
567
568 hmm1 = hmm.GMMHMM(n_components=3, gmms=array(gmm_list),n_mix=n_clusters)
569 hmm1.fit([array(features_vector_upsampled).T])
570
571 figure()
572 subplot(221)
573 plot(parameters_vector_upsampled_code.T)
574
575 predicted = hmm1.predict(array(features_vector_upsampled.T))
576
577 code_estimated = matrix(zeros((len(parameters_vector_upsampled), len(state_seq))))
578
579 # for i in range(0, len(state_seq)):
580 # code_estimated[:, i] = matrix(parameters_alphabet_inv[predicted[i]]).T
581
582 code_estimated = states_to_vector(predicted,parameters_alphabet_inv)
583 subplot(222)
584 plot(code_estimated.T)
585
586 reconstructed_original = adm_matrix_reconstruct(parameters_vector_upsampled_code, parameters_vector_upsampled_firstval)
587 subplot(223)
588 plot(reconstructed_original.T)
589 subplot(224)
590 reconstructed_estimated = adm_matrix_reconstruct(code_estimated, parameters_vector_upsampled_firstval)
591 plot(reconstructed_estimated.T)
592
593 # scatter(features_vector_upsampled[0,:],features_vector_upsampled_estimated[0,:])
594 # scatter(features_vector_upsampled[1,:],features_vector_upsampled_estimated[1,:])
595 #
596 # xlabel('Original Features on Principal Components')
597 # ylabel('Estimated Features on Principal Components')
598 # title('Original vs Estimated Features')
599 # savefig('original_vs_estimated.png')
600
601
602
603 #
604 #
605 print "[II] Testing Gaussian Naive Bayes Classifier"
606 ##
607 gnb = GaussianNB()
608 gnb.fit(features_vector_upsampled.T, parameters_state)
609
610 parameters_state_estimated = gnb.predict(features_vector_upsampled.T)
611
612 output = states_to_vector(parameters_state_estimated, parameter_state_alphabet_inv)
613
614 figure()
615 subplot(211)
616 plot(parameters_vector_upsampled.T)
617 subplot(212)
618 plot(smooth_matrix_1D(output.T))
619 # parameters_vector_upsampled_code_estimated = gnb.predict(features_vector_upsampled.T)
620 #
621 #
622 ##
623 # print "[II] Raw Parameters - Gaussian Naive Bayes classifier testing ratio: %.4f" % (float(sum((parameters_vector_upsampled_code_estimated == parameters_vector_upsampled_code).astype(float)))/float(len(parameters_k_means_labels)))
624 ##
625 # plot(adm_matrix_reconstruct(parameters_vector_upsampled_code_estimated,parameters_vector_upsampled_firstval,dmin,dmax).T)
626 #
627 # print "[II] Trying ADM-coded parameters"
628 # UpsamplingFactor = 100
629 # print "[II] Upsampling features and parameters by a factor of %d" % UpsamplingFactor
630 #
631 #
632 # # Upsampled features and parameters
633 # features_vector_upsampled = repeat(features_vector,UpsamplingFactor, axis=1)
634 # feature_labels_upsampled = repeat(features_clustering_labels,UpsamplingFactor, axis=0)
635 # parameters_vector_upsampled = repeat(parameters_vector,UpsamplingFactor, axis=1)
636 #
637 # parameters_vector_upsampled_adm = matrix(zeros(shape(parameters_vector_upsampled)))
638 # parameters_vector_upsampled_code = matrix(zeros(shape(parameters_vector_upsampled)))
639 # parameters_vector_upsampled_firstval = matrix(zeros((parameters_vector_upsampled.shape[0],1)))
640 #
641 # # Reconstructed parameters
642 #
643 # parameters_vector_upsampled_reconstructed = matrix(zeros(shape(parameters_vector_upsampled)))
644 #
645 # dmin = 0.001
646 # dmax = 0.28
647 # tol = 0.001
648 # for i in range(0, parameters_vector_upsampled.shape[0]):
649 # out, code, h = adm(parameters_vector_upsampled[i,:],dmin=dmin,dmax=dmax,tol=tol)
650 # parameters_vector_upsampled_adm[i,:] = out
651 # parameters_vector_upsampled_code[i,:] = code
652 # parameters_vector_upsampled_firstval[i, 0] = h
653 #
654 #
655 # # Reconstruct-ADM
656 # parameters_vector_upsampled_reconstructed[i,:] = adm_reconstruct(parameters_vector_upsampled_code[i,:],parameters_vector_upsampled_firstval[i,0], dmin=dmin,dmax=dmax)
657 #
658 #
659 # # plot(parameters_vector_upsampled_adm.T, 'r--')
660 #
661 #
662 # # plot(parameters_vector_upsampled.T)
663 # # plot(parameters_vector_upsampled_reconstructed.T, 'g.')
664 #
665 #
666 #
667 # parameters_vector_reconstructed = zeros(shape(parameters_vector))
668 # for n in range(0, parameters_vector.shape[1]):
669 # parameters_vector_reconstructed[:,n] = parameters_vector_upsampled_reconstructed[:,n*UpsamplingFactor]
670 #
671 #
672 # mse_adm = mse(parameters_vector_reconstructed, parameters_vector)
673 #
674 # print "[II] Expected ADM reconstruction MSE: %.4f" % mse_adm
675 #
676 # # figure()
677 # #plot(parameters_vector.T)
678 # # plot(parameters_vector_reconstructed.T)
679 #
680 # print "[II] Building HMM transition, emission matrices and priors"
681 #
682 # transmat = zeros((3,3))
683 # startprob = zeros((3,))
684 # emissionmat = zeros((3, n_clusters))
685 #
686 #
687 # state_labels = parameters_vector_upsampled_code + 1
688 # stateseq = state_labels.T
689 #
690 # for n in range(0,shape(parameters_vector_upsampled_code)[1]):
691 # if n>0:
692 # transmat[state_labels[0,n-1],state_labels[0,n]] += 1
693 # startprob[state_labels[0,n]] +=1
694 # emissionmat[state_labels[0,n],feature_labels_upsampled[n]] += 1
695 #
696 #
697 # for n in range(0, transmat.shape[0]):
698 # transmat[n,:]/=sum(transmat[n,:])
699 # emissionmat[n,:]/=sum(emissionmat[n,:])
700 #
701 #
702 # transmat = matrix(transmat)
703 # emissionmat = matrix(emissionmat)
704 # # Prior
705 # startprob = startprob/sum(startprob)
706 # startprob = ravel(startprob)
707 #
708 # # Vocabulary
709 #
710 # model = hmm.GMMHMM(n_mix=n_clusters, n_components=3, covariance_type="diag")
711 # model.means_ = features_clustering.means_
712 # model.covars_ = features_clustering.covars_
713 #
714 # features_vector_array = array(features_vector)
715 # features_vector_upsampled_array=array(features_vector_upsampled)
716 #
717 # model.fit([features_vector_array.T])
718 # stateseq_estimated = model.predict(features_vector_upsampled_array.T)
719 #
720 # parameters_vector_upsampled_reconstructed_decoded = matrix(zeros(shape(parameters_vector_upsampled)))
721 #
722 #
723 # plot(stateseq_estimated)
724 # plot(stateseq)
725 #
726 # code_estimated = matrix(zeros(shape(parameters_vector_upsampled)))
727 # code_estimated[0,:] = stateseq_estimated - 1
728 #
729 #
730 #
731 # parameters_vector_upsampled_reconstructed_estimated = matrix(zeros(shape(parameters_vector_upsampled)))
732 #
733 # for i in range(0, parameters_vector_upsampled.shape[0]):
734 # parameters_vector_upsampled_reconstructed_estimated[i,:] = adm_reconstruct(code_estimated,parameters_vector_upsampled_firstval[i,0], dmin=dmin,dmax=dmax)
735 # figure()
736 # plot(parameters_vector_upsampled_reconstructed_estimated.T)
737 # plot(parameters_vector_upsampled.T)