def generate_prediction(est, val, modelspecs): list_val = False if type(val) is list: # ie, if jackknifing list_val = True else: # Evaluate estimation and validation data # Since ms.evaluate only does a shallow copy of rec, successive # evaluations of one rec on many modelspecs just results in a list of # different pointers to the same recording. So need to force copies of # est/val before evaluating. if len(modelspecs) == 1: # no copies needed for 1 modelspec est = [est] val = [val] else: est = [est.copy() for i, _ in enumerate(modelspecs)] val = [val.copy() for i, _ in enumerate(modelspecs)] new_est = [ms.evaluate(d, m) for m, d in zip(modelspecs, est)] new_val = [ms.evaluate(d, m) for m, d in zip(modelspecs, val)] if list_val: new_val = [recording.jackknife_inverse_merge(new_val)] return new_est, new_val
def generate_prediction(est, val, modelspecs): if type(val) is list: # ie, if jackknifing new_est = [ms.evaluate(d, m) for m, d in zip(modelspecs, est)] new_val = [ms.evaluate(d, m) for m, d in zip(modelspecs, val)] new_val = [recording.jackknife_inverse_merge(new_val)] else: # Evaluate estimation and validation data new_est = [ms.evaluate(est, m) for m in modelspecs] new_val = [ms.evaluate(val, m) for m in modelspecs] return new_est, new_val
def generate_prediction(est, val, modelspecs): list_val = False if type(val) is list: # ie, if jackknifing list_val = True else: # Evaluate estimation and validation data # Since ms.evaluate only does a shallow copy of rec, successive # evaluations of one rec on many modelspecs just results in a list of # different pointers to the same recording. So need to force copies of # est/val before evaluating. if len(modelspecs) == 1: # no copies needed for 1 modelspec est = [est] val = [val] else: est = [est.copy() for i, _ in enumerate(modelspecs)] val = [val.copy() for i, _ in enumerate(modelspecs)] new_est = [] new_val = [] for m, e, v in zip(modelspecs, est, val): # nan-out periods outside of mask e = ms.evaluate(e, m) v = ms.evaluate(v, m) if 'mask' in v.signals.keys(): m = v['mask'].as_continuous() x = v['pred'].as_continuous().copy() x[..., m[0,:] == 0] = np.nan v['pred'] = v['pred']._modified_copy(x) new_est.append(e) new_val.append(v) #new_est = [ms.evaluate(d, m) for m, d in zip(modelspecs, est)] #new_val = [ms.evaluate(d, m) for m, d in zip(modelspecs, val)] if list_val: new_val = [recording.jackknife_inverse_merge(new_val)] return new_est, new_val
def stim_estimator_PCA(rec, npcs): x = len(rec['resp'].chans) ########## pca_out = PCA(rec['resp'], center=False) rec['resp'] = rec['resp'].rasterize()._modified_copy( np.matmul(pca_out['pcs'][:, 0:npcs], pca_out['loading'][0:npcs, :]).T) for i in range(10): rec['stim'] = rec['stim'].rasterize() i += 1 nfolds = 10 ests, vals, m = preproc.mask_est_val_for_jackknife(rec, modelspecs=None, njacks=nfolds) for i in range(10): vals[i]['stim'] = vals[i]['stim'].rasterize() i += 1 new_val = recording.jackknife_inverse_merge(vals) S_est_list = [] for i in range(10): est = ests[i].apply_mask() stim = est['stim'] resp_est = est['resp'].rasterize() X_est = stim.rasterize().as_continuous() Y_est = resp_est.as_continuous() mean_array = dc.mean_array_resp(x, Y_est) Y_est_subtracted = dc.Y_est_subtractedavg(mean_array, x, Y_est) S = X_est # stim est set R = Y_est_subtracted # response est set R1 = np.concatenate((R[:x, 2:], np.zeros((x, 2))), axis=1) R2 = np.concatenate((R, R1), axis=0) R3 = np.concatenate((R[:x, 4:], np.zeros((x, 4))), axis=1) R4 = np.concatenate((R2, R3), axis=0) R5 = np.concatenate((R[:x, 6:], np.zeros((x, 6))), axis=1) R6 = np.concatenate((R4, R5), axis=0) R7 = np.concatenate((R[:x, 8:], np.zeros((x, 8))), axis=1) R8 = np.concatenate((R6, R7), axis=0) R9 = np.concatenate((R[:x, 10:], np.zeros((x, 10))), axis=1) R10 = np.concatenate((R8, R9), axis=0) R11 = np.concatenate((R[:x, 12:], np.zeros((x, 12))), axis=1) R12 = np.concatenate((R10, R11), axis=0) R13 = np.concatenate((R[:x, 14:], np.zeros((x, 14))), axis=1) R14 = np.concatenate((R12, R13), axis=0) R15 = np.concatenate((R[:x, 16:], np.zeros((x, 16))), axis=1) R16 = np.concatenate((R14, R15), axis=0) R17 = np.concatenate((R[:x, 18:], np.zeros((x, 18))), axis=1) R18 = np.concatenate((R16, R17), axis=0) R19 = np.concatenate((R[:x, 20:], np.zeros((x, 20))), axis=1) R20 = np.concatenate((R18, R19), axis=0) R_trans = np.transpose(R20) Crr = np.matmul(R20, R_trans) Crs = np.matmul(S, R_trans) Crr_inv = np.linalg.inv(Crr) G = np.matmul(Crs, Crr_inv) resp_val = new_val['resp'] Y_val = resp_val.as_continuous() Y_val = np.nan_to_num(Y_val) shape = Y_val.shape y = shape[1] Y_val_segment = Y_val[:, int((y / 10) * i):int((y / 10) * (i + 1))] shape = Y_val_segment.shape y = shape[1] Y_val_subtractedavg = np.zeros(shape=(x, y)) for u in range(x): Y_est_cell_mean = mean_array[u] difference = Y_val_segment[u, :] - Y_est_cell_mean Y_val_subtractedavg[u] = difference u += 1 R_val = Y_val_subtractedavg R1_val = np.concatenate((R_val[:x, 2:], np.zeros((x, 2))), axis=1) R2_val = np.concatenate((R_val, R1_val), axis=0) R3_val = np.concatenate((R_val[:x, 4:], np.zeros((x, 4))), axis=1) R4_val = np.concatenate((R2_val, R3_val), axis=0) R5_val = np.concatenate((R_val[:x, 6:], np.zeros((x, 6))), axis=1) R6_val = np.concatenate((R4_val, R5_val), axis=0) R7_val = np.concatenate((R_val[:x, 8:], np.zeros((x, 8))), axis=1) R8_val = np.concatenate((R6_val, R7_val), axis=0) R9_val = np.concatenate((R_val[:x, 10:], np.zeros((x, 10))), axis=1) R10_val = np.concatenate((R8_val, R9_val), axis=0) R11_val = np.concatenate((R_val[:x, 12:], np.zeros((x, 12))), axis=1) R12_val = np.concatenate((R10_val, R11_val), axis=0) R13_val = np.concatenate((R_val[:x, 14:], np.zeros((x, 14))), axis=1) R14_val = np.concatenate((R12_val, R13_val), axis=0) R15_val = np.concatenate((R_val[:x, 16:], np.zeros((x, 16))), axis=1) R16_val = np.concatenate((R14_val, R15_val), axis=0) R17_val = np.concatenate((R_val[:x, 18:], np.zeros((x, 18))), axis=1) R18_val = np.concatenate((R16_val, R17_val), axis=0) R19_val = np.concatenate((R_val[:x, 20:], np.zeros((x, 20))), axis=1) R20_val = np.concatenate((R18_val, R19_val), axis=0) S_est = np.matmul(G, R20_val) S_est[S_est < 0] = 0 S_est_list.append(S_est) i += 1 S_est = np.concatenate((S_est_list[0], S_est_list[1], S_est_list[2], S_est_list[3], S_est_list[4], S_est_list[5], S_est_list[6], S_est_list[7], S_est_list[8], S_est_list[9]), axis=1) return S_est