def keypress(self, event): old_xlim = self.ax1.get_xlim() new_xlim = old_xlim xrange = old_xlim[1] - old_xlim[0] xcenter = np.mean(old_xlim) if event.key == 'alt+d': #small forward self.ax1.set_xlim([old_xlim[0]+(xrange/4.), old_xlim[1]+(xrange/4.)]) elif event.key == 'alt+s': #small back self.ax1.set_xlim([old_xlim[0]-(xrange/4.), old_xlim[1]-(xrange/4.)]) elif event.key == 'alt+f': #large forward self.ax1.set_xlim([old_xlim[1], old_xlim[1]+xrange]) elif event.key == 'alt+a': # large back self.ax1.set_xlim([old_xlim[0]-xrange, old_xlim[0]]) elif event.key == 'alt+e': #zoom in self.ax1.set_xlim([xcenter-0.25*xrange, xcenter+0.25*xrange]) elif event.key == 'alt+w': #zoom out self.ax1.set_xlim([xcenter-1.5*xrange, xcenter+1.5*xrange]) elif event.key == 'alt+g': xy = plt.ginput(n = 0, timeout = -1) self.hits = np.vstack((self.hits, xy)) for xy_ in xy: self.ax1.axvline(xy_[0], color = 'r', ls = '--') self.ax2.axvline(xy_[0], color = 'r', ls = '--') plt.show() np.savetxt(self.savepaths[self.counter], self.hits) elif event.key == 'alt+q': self.ax1.cla() self.ax2.cla() self.counter += 1 print self.fnames[self.counter] print self.savepaths[self.counter] self.hits = np.empty((0, 2), dtype = 'float32') np.savetxt(self.savepaths[self.counter], self.hits) P, F, T = load_spec(self.fnames[self.counter]) P, F = lowpass_spec(P, F) P = zscore_spec(P) P[P<0] = 0 P = P**0.5 wind = np.hamming(10) wind = wind / wind.sum() P_sum = (np.convolve(P.sum(0), wind, 'same')) nfreqs, ntimes = P.shape Tmax = T.max() print P.shape plot_spec(P[:, ::10], F, T[::10], ax = self.ax1) self.ax1.set_title(self.fnames[self.counter]) self.ax1.set_xlim([0, 1.5]) self.ax2.plot(T, P_sum) self.ax2.set_xlim([0, Tmax]) plt.show() plt.draw() plt.show()
from sklearn.svm import SVC from sklearn.decomposition import PCA from voc import load_spec, plot_spec, lowpass_spec from scipy.stats import zscore hits_fname = '/Volumes/BOB_SAGET/Vocalization/analysis/gui_count_output/KO4_P09_20.txt' spec_fname = '/Volumes/BOB_SAGET/Vocalization/Cages/KO4/P09/KO4_P09_20.h5' P, F, T = load_spec(spec_fname) P, F = lowpass_spec(P, F) nfreqs, ntimes = P.shape hits = np.loadtxt(hits_fname) times = hits[:, 0] ix = time2ix(times, T.max(), ntimes) X = np.zeros((500, 42300)) Y = np.zeros(500) for i in range(500): X[i, :] = P[:, i*150:i*150+300].ravel() Y[i] = np.vstack(((ix-(i*150))>0, (ix-(i*150))<150)).all(0).sum()>0 X_ = zscore(X, axis = 0) plot_spec(P[:, ix[0]-100 : ix[0]+200]) svc = SVC() svc.fit(X, Y)