diff --git a/classif.py b/classif.py index d982806..612ce30 100644 --- a/classif.py +++ b/classif.py @@ -8,39 +8,39 @@ from sklearn.base import BaseEstimator, ClassifierMixin, TransformerMixin ############################################################################### class XdawnCovariances(BaseEstimator,TransformerMixin): - """ + """ Compute double xdawn, project the signal and compute the covariances - """ + """ def __init__(self,nfilter=4,subelec=-1): self.nfilter = nfilter self.subelec = subelec - + def fit(self,X,y): Nt,Ne,Ns = X.shape # Prototyped responce for each class P1 = numpy.mean(X[y==1,:,:],axis=0) P0 = numpy.mean(X[y==0,:,:],axis=0) - + # Covariance matrix of the prototyper response & signal C1 = numpy.matrix(numpy.cov(P1)) C0 = numpy.matrix(numpy.cov(P0)) - - #FIXME : too many reshape operation + + #FIXME : too many reshape operation tmp = X.transpose((1,2,0)) Cx = numpy.matrix(numpy.cov(tmp.reshape(Ne,Ns*Nt))) - + # Spatial filters - D,V1 = geig(C1,Cx) + D,V1 = geig(C1,Cx) D,V0 = geig(C0,Cx) - + # create the reduced prototyped response self.P = numpy.concatenate((numpy.dot(V1[:,0:self.nfilter].T,P1),numpy.dot(V0[:,0:self.nfilter].T,P0)),axis=0) - + def transform(self,X): covmats = riemann.covariances_EP(X[:,self.subelec,:],self.P) return covmats - + def fit_transform(self,X,y): self.fit(X,y) return self.transform(X) @@ -50,15 +50,15 @@ class TangentSpace(BaseEstimator, TransformerMixin): def __init__(self,metric='riemann',tsupdate = False): self.metric = metric - self.tsupdate = tsupdate + self.tsupdate = tsupdate + - def fit(self,X,y=None): # compute mean covariance self.Cr = riemann.mean_covariance(X,metric=self.metric) - + def transform(self,X): - + if self.tsupdate: Cr = riemann.mean_covariance(X,metric=self.metric) else: @@ -74,10 +74,10 @@ class AddMeta(BaseEstimator, TransformerMixin): def __init__(self,meta=None): self.meta = meta - + def fit(self,X,y=None): pass - + def transform(self,X): if self.meta is not None: return numpy.c_[X,self.meta] @@ -96,14 +96,14 @@ def __init__(self,nelec = 20,nfilters=5,metric='riemann'): self.nfilters = nfilters self.subelec = -1 self.dist = [] - + def fit(self,X,y=None): C1 = riemann.mean_covariance(X[y==1,...],self.metric) C0 = riemann.mean_covariance(X[y==0,...],self.metric) - + Ne,_ = C0.shape - - self.subelec = range(0,Ne,1) + + self.subelec = range(0,Ne,1) while (len(self.subelec)-2*self.nfilters)>self.nelec: di = numpy.zeros((len(self.subelec),1)) for idx in range(2*self.nfilters,len(self.subelec)): @@ -113,9 +113,9 @@ def fit(self,X,y=None): #print di torm = di.argmax() self.dist.append(di.max()) - self.subelec.pop(torm) + self.subelec.pop(torm) #print self.subelec - + def transform(self,X): return X[:,self.subelec,:][:,:,self.subelec] @@ -125,13 +125,14 @@ def fit_transform(self,X,y=None): ############################################################################### def updateMeta(clf,Meta): if clf.named_steps.has_key('addmeta'): - clf.set_params(addmeta__meta=Meta) + clf.set_params(addmeta__meta=Meta) def baggingIterator(opts,users): mdls = opts['bagging']['models'] bag_size = 1-opts['bagging']['bag_size'] - bag_size = numpy.floor(bag_size*len(users)) + bag_size = int(numpy.floor(bag_size*len(users))) if bag_size == 0: return [[u] for u in users] else: - return [numpy.random.choice(users,size=bag_size,replace=False) for i in range(mdls)] \ No newline at end of file + users = users.astype(int) + return [numpy.random.choice(users,size=bag_size,replace=False) for i in range(mdls)] diff --git a/prediction.py b/prediction.py index c0be13f..7b4ae48 100644 --- a/prediction.py +++ b/prediction.py @@ -25,23 +25,23 @@ def from_yaml_to_func(method,params): def BaggingFunc(bag,Labels,X,Meta,User,X_test,Meta_test,User_test): bagUsers = np.array([True if u in set(bag) else False for u in User]) - train_index = np.negative(bagUsers) + train_index = np.logical_not(bagUsers) updateMeta(clf,Meta[train_index]) clf.fit(X[train_index,:,:],Labels[train_index]) - + ### predicting prob = [] for ut in users_test: updateMeta(clf,Meta_test[User_test==ut,...]) prob.extend(clf.predict(X_test[User_test==ut,...])) prob = np.array(prob) - + return prob # load parameters file yml = yaml.load(open(sys.argv[1])) -# imports +# imports for pkg, functions in yml['imports'].iteritems(): stri = 'from ' + pkg + ' import ' + ','.join(functions) exec(stri) @@ -74,7 +74,7 @@ def BaggingFunc(bag,Labels,X,Meta,User,X_test,Meta_test,User_test): ### training np.random.seed(5) -allProb = 0 +allProb = 0 if opts.has_key('bagging'): bagging = baggingIterator(opts,users) @@ -95,4 +95,4 @@ def BaggingFunc(bag,Labels,X,Meta,User,X_test,Meta_test,User_test): submission = yml['Submission']['path'] df = pd.DataFrame({'IdFeedBack':feedbackid,'Prediction':allProb}) -df.to_csv(submission,index=False) \ No newline at end of file +df.to_csv(submission,index=False) diff --git a/preproc/preproc.py b/preproc/preproc.py index c92c6fa..67e446f 100644 --- a/preproc/preproc.py +++ b/preproc/preproc.py @@ -16,7 +16,7 @@ def bandpass(sig,band,fs): B,A = butter(5, array(band)/(fs/2), btype='bandpass') return lfilter(B, A, sig, axis=0) -for test in [False,True] : +for test in [False,True] : prefix = '' if test is False else 'test_' DataFolder = '../data/train/' if test is False else '../data/test/' @@ -24,12 +24,10 @@ def bandpass(sig,band,fs): list_of_files.sort() reg = re.compile('\d+') - - freq = 200.0 - epoc_window = 1.3*freq + epoc_window = int(1.3*freq) X = [] User = [] @@ -52,10 +50,9 @@ def bandpass(sig,band,fs): Trigger = sig[:,-1] sigF = bandpass(EEG,[1.0,40.0],freq) - - + idxFeedBack = np.where(Trigger==1)[0] - + for fbkNum,idx in enumerate(idxFeedBack): X.append(sigF[idx:idx+epoc_window,:]) User.append(int(user)) @@ -66,9 +63,9 @@ def bandpass(sig,band,fs): Word.append(floor(fbkNum/5)+1) FeedbackTot.append(fbkNum + (int(session)-1)*60) WordTot.append(floor(fbkNum/5)+1 + (int(session)-1)*12) - + Meta = array([Session,Feedback,Letter,Word,FeedbackTot,WordTot]).transpose() - + Meta2 = pd.read_csv('metadata.csv') currentUserSet = [True if val in set(User) else False for val in Meta2.subject]