Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update functions to work with current libraries. Cleanup white spaces. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions classif.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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]
Expand All @@ -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)):
Expand All @@ -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]

Expand All @@ -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)]
users = users.astype(int)
return [numpy.random.choice(users,size=bag_size,replace=False) for i in range(mdls)]
12 changes: 6 additions & 6 deletions prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
df.to_csv(submission,index=False)
15 changes: 6 additions & 9 deletions preproc/preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ 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/'
list_of_files = glob.glob(DataFolder + 'Data_*.csv')
list_of_files.sort()

reg = re.compile('\d+')



freq = 200.0

epoc_window = 1.3*freq
epoc_window = int(1.3*freq)

X = []
User = []
Expand All @@ -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))
Expand All @@ -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]

Expand Down