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

WIP: towards faster maxCorr #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 20 additions & 7 deletions pylabianca/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,19 @@ def correlation(X1, X2):
return rval_sel


def corr_rows(A, B):
# Rowwise mean of input arrays & subtract from input arrays themeselves
A_mA = A - A.mean(axis=1)[:, None]
B_mB = B - B.mean(axis=1)[:, None]

# Sum of squares across rows
ssA = (A_mA ** 2).sum(axis=1)
ssB = (B_mB ** 2).sum(axis=1)

# Finally get corr coeff
return np.dot(A_mA, B_mB.T) / np.sqrt(np.dot(ssA[:, None], ssB[None]))


# TODO: profile and consider using a better correlation function
# (numba for example)
class maxCorrClassifier(BaseEstimator):
Expand Down Expand Up @@ -681,7 +694,7 @@ def fit(self, X, y, scoring=None):
avg = X[msk, :].mean(axis=0)
self.class_averages_.append(avg)

self.class_averages_ = np.stack(self.class_averages_, axis=1)
self.class_averages_ = np.stack(self.class_averages_, axis=0)
self.scoring = 'accuracy' if scoring is None else scoring

return self
Expand Down Expand Up @@ -719,16 +732,16 @@ def predict(self, X):
message='invalid value encountered in true_divide',
category=RuntimeWarning
)
r = correlation(self.class_averages_, X.T)
r = corr_rows(self.class_averages_, X)
else:
distance = self.class_averages_[..., None] - X.T[:, None, :]
r = np.linalg.norm(distance, axis=0) * -1
distance = self.class_averages_[:, None, :] - X[None, :, :]
r = np.linalg.norm(distance, axis=-1) * -1

bad_trials = np.isnan(r).all(axis=0)
if bad_trials.any():
distance = (self.class_averages_[..., None]
- X.T[:, None, bad_trials])
r[:, bad_trials] = np.linalg.norm(distance, axis=0) * -1
distance = (self.class_averages_[:, None, :]
- X[None, bad_trials, :])
r[:, bad_trials] = np.linalg.norm(distance, axis=-1) * -1

# pick class with best correlation:
r_best = r.argmax(axis=0)
Expand Down