def classify_trial(cv): train = cv[0] test = cv[1] regr = clf(**clf_args) XX = X[train].reshape(-1, X.shape[2]) yy = y[train].ravel() Xt = X[test].reshape(-1, X.shape[2]) regr.fit(XX, yy) coef = None if return_coefs: try: coef = regr.coef_ except: pass pred = np.nan_to_num(regr.predict(Xt)) return (pred, coef)
def classify_time(cv): train = cv[0] # dont use the edge bits for training train = train[train > (edges[0] - 1)] train = train[train < (len(train) - edges[1])] test = cv[1] regr = clf(**clf_args) XX = X[:, train].T yy = y[:, train].ravel() Xt = X[:, test].T regr.fit(XX, yy) coef = None if return_coefs: try: coef = regr.coef_ except: pass pred = np.nan_to_num(regr.predict(Xt)).reshape([y.shape[0], len(test)]) return (pred, coef)
def classify_time(cv): train = cv[0] # dont use the edge bits for training train = train[train > (edges[0] - 1)] train = train[train < (len(train) - edges[1])] test = cv[1] regr = clf(**clf_args) XX = X[train] yy = y[train].ravel() Xt = X[test] regr.fit(XX, yy) coef = None if return_coefs: try: coef = regr.coef_ except: pass pred = np.nan_to_num(regr.predict(Xt)) return (pred, coef)
def classify(cv): train = cv[0] # dont use the edge bits for training train = train[train > (edge - 1)] train = train[train < (len(train) - edge)] test = cv[1] regr = clf(**clf_args) XX = X[:, train].reshape(-1, X.shape[2]) yy = y[:, train].ravel().copy() Xt = X[0, test] regr.fit(XX, yy) coef = None if return_coefs: try: coef = regr.coef_ except: pass pred = np.nan_to_num(regr.predict(Xt)) return (pred, coef)
crr_w = [] for c in cell_idx: crr_w.append(corr_trial_to_trial(dt[c].T)) crr_w = np.array(crr_w) np.save('%s%s_%s' % (target, exp, src), crr_w) src_res = [] trials = np.arange(dt.shape[2]) ys = [] preds = [] for t in trials: X = dt[cell_idx != cell] X = X[:, :, trials != t].reshape(X.shape[0], -1).T y = dt[cell, :, trials != t].ravel() XX = dt[cell_idx != cell, :, t].reshape(X.shape[1], -1).T yy = dt[cell, :, t].ravel() c = clf(**clf_args) c.fit(X, y) ys.append(yy) preds.append(c.predict(XX)) crr = do_thresh_corr(np.array(ys).ravel(), np.array(preds).ravel()) cell_res.append(crr) res.append(cell_res) res = np.array(res) print res.shape plt.figure(figsize=(16, 8)) plt.subplot(131, aspect='equal') plt.scatter(res[:, 0], res[:, 1]) plt.plot([0, 1], [0, 1], '--') plt.xlim([-0.01, res[:, 0].max() * 1.1]) plt.ylim([-0.01, res[:, 1].max() * 1.1]) plt.xlabel('Pred Corr Centre')
def do_classification(exp_type='SOM', targets=['Center', 'Surround', 'Whole'], four_downsample=None, max_exp=None): # Sub directory of the figure path to put the plots in fig_path = startup.fig_path + 'Sparseness/%s/pred/' % (exp_type) mov_path = startup.data_path + 'Sparseness/%s/' % (exp_type) if not os.path.exists(fig_path): os.makedirs(fig_path) if not os.path.exists(fig_path + str(downsample)): os.makedirs(fig_path + str(downsample)) dat = load_EphysData(exp_type) all_corrs = [] surr_corrs = [] mask_corrs = [] whole_corrs = [] for i, e in enumerate(dat.values()): if max_exp is not None and i >= max_exp: break expdate = e['expdate'] cellid = e['cellid'] if not os.path.exists(mov_path + cellid + '_processed.npz'): print '\nNo movie found ', cellid continue else: print '\ndoing ', e['cellid'] clf_vals = [] sig_found = False for targ_type in targets: fname = '%s%s/%s_%s_pred' % (fig_path, str(four_downsample), cellid, targ_type) print fname X, y, plot_params = get_mov_data(targ_type, e, cellid, exp_type, four_downsample) for xi in range(X.shape[2]): cv = LeaveOneOut(X.shape[1], indices=True) cv = KFold(X.shape[1], 20, indices=True, shuffle=True) pred = np.zeros(y[0].shape) X_dims = X.shape[2] coefs = [] for train, test in cv: regr = clf(alpha=0.01) regr.fit(X[:, train, xi].reshape([-1, 1]), y[:, train].ravel()) coefs.append(regr.coef_) pred[test] = regr.predict(X[0, test, xi].reshape([-1, 1])) coefs = np.array(coefs).mean(0) clf_vals.append([targ_type, coefs, plot_params]) mn = y.mean(0) std = np.std(y, 0) [crr_pred, p_pred] = corr(mn, pred) if p_pred < 0.05: crr_pred = np.nan_to_num(crr_pred) else: crr_pred = 0. p_pred = 0. if crr_pred > 0: sig_found = True if crr_pred > 0.3: plt.figure() plt.hold(True) plt.plot(pred, 'r') plt.plot(mn, 'k') plt.title('Corr: %.2f' % crr_pred) plt.show() all_corrs += [crr_pred] if targ_type == 'Center': mask_corrs += [crr_pred] elif targ_type == 'Surround': surr_corrs += [crr_pred] elif targ_type == 'Whole': whole_corrs += [crr_pred] plt.subplot(211) plt.scatter(range(len(mask_corrs)), mask_corrs) plt.subplot(212) plt.scatter(range(len(whole_corrs)), whole_corrs) plt.show() # xcorr = [] # for i in range(y.shape[0]): # xcorr.append(corr(y[i], mn)) # xcorr = np.nan_to_num(np.array(xcorr)) # xcorr[xcorr[:, 1] > 0.05, 0] = 0 # [crr_y, p_y] = xcorr.mean(0) # print crr_pred, p_pred # only do plots for the fourier trained classifier comb_vals = [] comb_vals.append(['mask', mask_corrs]) comb_vals.append(['surround', surr_corrs]) comb_vals.append(['whole', whole_corrs]) comb_vals.append(['overall', all_corrs]) comb_corrs.append([comb, comb_vals]) # make this a boxplot plot_summary(comb_corrs, fig_path, str(downsample)) return comb_corrs
cell_vals = np.nan_to_num(cell_vals) vals.append(cell_vals) cells.append([e, exp_type, cell]) vals = np.array(vals) cells = np.array(cells) y = cells[:, 0] idx = np.arange(len(y)) np.random.shuffle(idx) y = y[idx] vals = vals[idx] from sklearn.cross_validation import cross_val_score from sklearn.linear_model import LogisticRegression as clf print cross_val_score(clf(), vals, y, verbose=True) ## # pca = PCA(n_components=2) pca.fit(vals) print(pca.explained_variance_ratio_) vals2 = pca.transform(vals) fig = plt.figure(figsize=(8, 8)) fig.set_facecolor('white') plt.hold(True) for c in range(len(vals2)): if cells[c][1] == 'PYR': col = 'r' elif cells[c][1] == 'FS':
cell_vals = np.nan_to_num(cell_vals) vals.append(cell_vals) cells.append([e, exp_type, cell]) vals = np.array(vals) cells = np.array(cells) y = cells[:, 0] idx = np.arange(len(y)) np.random.shuffle(idx) y = y[idx] vals = vals[idx] from sklearn.cross_validation import cross_val_score from sklearn.linear_model import LogisticRegression as clf print cross_val_score(clf(), vals, y, verbose=True) ## # pca = PCA(n_components=2) pca.fit(vals) print (pca.explained_variance_ratio_) vals2 = pca.transform(vals) fig = plt.figure(figsize=(8, 8)) fig.set_facecolor("white") plt.hold(True) for c in range(len(vals2)): if cells[c][1] == "PYR": col = "r" elif cells[c][1] == "FS":