def sparse_sift(image, fraction=1.0): """ sparse oriented SIFT at Harris-LaPlace and Difference of Gaussians keypoints use VLFEAT vl_covdet through octave; expects a grayscale image """ octave.eval("addpath ~/CC/vlfeat/vlfeat-0.9.20/toolbox") octave.eval("vl_setup") octave.push("im", image) octave.eval("im = single(im);") octave.eval( "[kp,sift_hl] = vl_covdet(im, 'method', 'HarrisLaplace', 'EstimateOrientation', true); " ) octave.eval( "[kp,sift_dog] = vl_covdet(im, 'method', 'DoG', 'EstimateOrientation', true); " ) octave.eval("descrs = [sift_hl, sift_dog];") descriptors = octave.pull("descrs") kp = octave.pull("kp") # flip from column-major to row-major descriptors = descriptors.T if fraction < 1.0: descriptors = random_sample(descriptors, fraction) return kp, descriptors
def set(self, expid, variables, values): ''' Writes one or more variables to the workspace of the current Octave session ''' n = len(variables) for i in range(n): try: octave.push(variables[i], values[i]) except: pass
def fit(self, K, y): """Learn a low-rank kernel approximation. :param K: (``numpy.ndarray``) or of (``Kinterface``). The kernel to be approximated with G. :param y: (``numpy.ndarray``) Class labels :math:`y_i \in {-1, 1}` or regression targets. """ # Convert to explicit form K = K[:, :] y = y.reshape((len(y), 1)) # Call original implementation octave.push(["K", "y", "rank", "centering", "kappa", "delta", "tol"], [ K, y, self.rank, self.centering, self.kappa, self.delta, self.tol ]) octave.eval( "[G, P, Q, R, error1, error2, error, predicted_gain, true_gain] = csi(K, y, rank, centering, kappa, delta, tol)", verbose=False) G, P, Q, R, error1, error2, error, predicted_gain, true_gain = \ octave.pull(["G", "P", "Q", "R", "error1", "error2", "error", "predicted_gain", "true_gain"]) R = np.atleast_2d(np.array(R)) # Octave indexes from 1 P = P.ravel().astype(int) - 1 # Resort rows to respect the order n, k = K.shape[0], self.rank self.I = self.active_set_ = list(P[:k]) Go = np.zeros((n, k)) Qo = np.zeros((n, k)) Ro = np.zeros((k, k)) km = min(k, G.shape[1]) Go[P, :km] = G[:, :km] Qo[P, :km] = Q[:, :km] Ro[:km, :km] = R[:km, :km] self.G = Go[:, :self.rank] self.P = P[:self.rank] self.Q = Qo[:, :] self.R = Ro[:, :self.rank] self.error1 = error1 self.error1 = error2 self.error = error self.predicted_gain = predicted_gain self.true_gain = true_gain self.trained = True self.active_set_ = self.I[:self.rank]
def plot_calibration_mapping(calibration_model, min_score, max_score, resolution=1000, file_name='calibration_mapping.png'): # Function for plotting what probabilities different scores get mapped to. # "General purpose prediction function" # PERHAPS ADD PROBABILITY DISTRIBUTION OF TRAINING DATA (OR TESTING?)? # WOULD INDICATE HOW MANY SAMPLES FALL INTO ONE BIN. diff = max_score - min_score scores = [ min_score + i * diff / float(resolution) for i in range(resolution + 1) ] try: # IR model probabilities = calibration_model.predict(scores) except: try: # ENIR import rpy2.robjects as robjects from rpy2.robjects.packages import importr enir = importr('enir') r = robjects.r # Automatic conversion or numpy arrays to R-vectors import rpy2.robjects.numpy2ri rpy2.robjects.numpy2ri.activate() # ENIR-MODEL MIGHT NEED TO BE PUSHED TO R-ENVIRONMENT? probabilities = enir.enir_predict(calibration_model, robjects.FloatVector(scores)) probabilities = np.array(probabilities) except: try: # BBQ from oct2py import octave octave.eval("addpath('./calibration/BBQ/')", verbose=False) octave.push('scores', scores, verbose=False) octave.push('calibration_model', calibration_model, verbose=False) octave.eval( 'probabilities = predict(calibration_model, scores, 1)', verbose=False) probabilities = octave.pull('probabilities', verbose=False) probabilities = np.array([item[0] for item in probabilities]) except: pass # Continue with BIR and WABIR? RCIR? # Plot score vs. probability: plt.plot(scores, probabilities) plt.title("Calibration mapping") plt.savefig(file_name) plt.gcf().clear()
def dense_sift(image, fraction=1.0): """ dense SIFT use VLFEAT vl_phow through octave; expects a grayscale image """ octave.push("im", image) octave.eval("im = single(im);") octave.eval("[kp,siftd] = vl_phow(im); ") descriptors = octave.pull("siftd") # flip from column-major to row-major descriptors = descriptors.T if fraction < 1.0: descriptors = random_sample(descriptors, fraction) return descriptors
# Get good trials indices goodtrials = np.where(df['badtrial'] == 0)[0] df = df.iloc[goodtrials] mod = sm.OLS(df["amp_['POz']_0.4-0.8"], sm.add_constant(df[variable]), hasconst=True).fit() bic_erps.loc[p, variable] = mod.bic bic_beta.loc[p, variable] = mod.params[variable] bic_erps.to_csv(opj(outpath, 'bic_erps.csv')) # Use octave to run the VBA-toolbox octave.push('L', np.asarray(bic_erps.transpose()) * -1) octave.addpath('/matlab/vbatoolbox') octave.addpath('/matlab/vbatoolbox/core') octave.addpath('/matlab/vbatoolbox/core/display') octave.addpath('/matlab/vbatoolbox/utils') octave.eval("options.DisplayWin = 0") p, out = octave.eval("VBA_groupBMC(L, options)", nout=2) # Save to plot file = open(opj(outpath, 'erps_olsmean_VBAmodelcomp.pkl'), "wb") pickle.dump(out, file) # ######################################################################## # Mass univariate regression ########################################################################### # Load model data
label="Credible intervals") # Create reliability diagram: isotonic.plot_reliability_diagram(isotonic_regression_model, test_scores, test_class, file_name="./" + test_description + "/reliability_diagram" + ".png", label="Reliability diagram") # Naeini's model # Comparison to Naeini's model (it's a matlab model, using Octave). print( "Training Naeini-model. This might take a while (~20 minutes on a laptop)." ) octave.push('training_scores', naeini_training_scores, verbose=False) octave.push('training_class', naeini_training_class, verbose=False) octave.eval("options.N0 = 2", verbose=False) octave.eval( "BBQ_model = build(training_scores', training_class', options)", verbose=False) # In the following, '1' indicates model averaging, as done in the paper by Naeini & al. octave.eval("training_bbq_prob = predict(BBQ_model, training_scores, 1)", verbose=False) training_bbq_prob = octave.pull("training_bbq_prob", verbose=False) training_bbq_prob = np.array([item[0] for item in training_bbq_prob]) octave.push('test_scores', test_scores, verbose=False) octave.eval("test_bbq_prob = predict(BBQ_model, test_scores, 0)", verbose=False) test_bbq_prob = octave.pull("test_bbq_prob", verbose=False) test_bbq_prob = np.array([item[0] for item in test_bbq_prob])
if args.n_features_keep: weights = weights[..., sorted_inds_keep] # get the shape of the weights and make sure they satisfy certain conditions w_x, w_y, w_in, w_out = weights.shape # compute the new number of features you will have and make placeholder to store them nx = args.input_w - (w_x - 1) ny = args.input_h - (w_y - 1) w_out_new = (nx // args.stride_x) * (ny // args.stride_y) * w_out nonshared = np.zeros([args.input_w, args.input_h, w_in, w_out_new], dtype=np.float64) # fill in the original features in the simple cell tensor count = 0 for k in range(w_out): for i in range(0, nx, args.stride_x): for j in range(0, ny, args.stride_y): nonshared[i:i + w_x, j:j + w_y, :, count] = weights[:, :, :, k] count += 1 # write the new features write_fpath = os.path.join(args.save_dir, os.path.split(fpath)[1]) feat_data[0]['values'][0] = nonshared octave.push(['write_fpath', 'feat_data'], [write_fpath, feat_data]) octave.eval('writepvpsharedweightfile(write_fpath, feat_data)') logging.info('NONSHARED GRID SIZE IS {}x{}x{}.'.format(ny // args.stride_y, nx // args.stride_x, w_out))
# Create ENIR model using R: enir_model = enir.enir_build( robjects.FloatVector(training_scores.tolist()), robjects.BoolVector(training_class.tolist())) enir_prob = enir.enir_predict(enir_model, robjects.FloatVector(test_scores.tolist())) # Convert to numpy.array: enir_prob = np.array(enir_prob) # Kill session? # Create isotonic regression model ir_model = IsotonicRegression(y_min=0, y_max=1, out_of_bounds='clip') ir_model.fit(X=training_scores, y=training_class) # Create BBQ-model octave.push('training_scores', training_scores, verbose=False) octave.push('training_class', training_class, verbose=False) octave.eval('options.N0 = 2', verbose=False) octave.eval( "bbq_model = build(training_scores', training_class', options)", verbose=False) octave.push('test_scores', test_scores) octave.eval("test_prob = predict(bbq_model, test_scores, 1)", verbose=False) bbq_test_prob = octave.pull('test_prob', verbose=False) bbq_test_prob = [item[0] for item in bbq_test_prob] # Create RCIR model with d = .20 (?) # QUICK HACK TO TEST RCIR-CV INSTEAD OF RCIR WITH SOME d: # NOTE THAT RCIR-CV GETS _MORE_ _DATA_ THAN # rcir_model = isotonic.train_rcir_cv(training_scores, training_class , d=.1)
def snmf_fhals(A, k, init='normal'): """ Nonnegative Matrix Factorization. Hierarchical alternating least squares algorithm for computing the approximate low-rank nonnegative matrix factorization of a square `(m, m)` matrix `A`. Given the target rank `k << m`, the input matrix `A` is factored as `A = W Wt`. The nonnegative factor matrices `W` and `Wt` are of dimension `(m, k)` and `(k, m)`, respectively. Parameters ---------- A : array_like, shape `(m, m)`. Real nonnegative input matrix. k : integer, `k << m`. Target rank. init : str `{'normal'}`. 'normal' : Factor matrices are initialized with nonnegative Gaussian random numbers. tol : float, default: `tol=1e-4`. Tolerance of the stopping condition. maxiter : integer, default: `maxiter=100`. Number of iterations. verbose : boolean, default: `verbose=False`. The verbosity level. Returns ------- W: array_like, `(m, k)`. Solution to the non-negative least squares problem. """ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Error catching #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ m, n = A.shape assert m == n if (A < 0).any(): raise ValueError("Input matrix with nonnegative elements is required.") if A.dtype == sci.float32: data_type = sci.float32 elif A.dtype == sci.float64: data_type = sci.float64 else: raise ValueError("A.dtype is not supported.") #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Initialization methods for factor matrices W and H # 'normal': nonnegative standard normal random init #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ if init == 'normal': n, _ = A.shape H = 2 * np.sqrt(np.mean(np.mean(A)) / k) * np.random.rand(n, k) maxiter = 10000 tol = 1e-3 alpha = np.max(H)**2 W = H.copy() I_k = alpha * np.identity(k) else: raise ValueError('Initialization method is not supported.') #End if #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Iterate the HALS algorithm until convergence or maxiter is reached # i) Update factor matrix H and normalize columns # ii) Update low-dimensional factor matrix W # iii) Compute fit log( ||A-WH|| ) # -> break if fit <-5 or fit_change < tol #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ projnorm = float('inf') left = H.T.dot(H) right = A.dot(H) octave.push('A', A) octave.push('k', k) for niter in range(maxiter): octave.push('W', W) octave.push('H', H) octave.push('left', left) octave.push('right', right) octave.eval("[W, H, left, right, violation] = symnmf_anls_iter(A, W, H, left, right, k)", verbose=False) W = octave.pull("W") H = octave.pull("H") left = octave.pull("left") right = octave.pull("right") violation = octave.pull("violation") if niter == 0: initgrad = violation else: projnorm = violation if projnorm < tol * initgrad: break return H
for frame_num, lca_fpath in enumerate(lca_fpaths): # read in the pvp files and get the lca features pvp_data = octave.readpvpfile(lca_fpath) weights = pvp_data[0]['values'][0] w_out_original = weights.shape[-1] # take out the current frame from the strf and add to the weights mouse_strfs_frame = mouse_strfs[:, :, frame_num, :] mouse_strfs_frame = mouse_strfs_frame[:, :, None, :] try: # if shapes don't line up weights = np.concatenate((weights, mouse_strfs_frame), 3) except ValueError: raise # add the new weights back to the pvp_data structure and push to octave session pvp_data[0]['values'][0] = weights write_fpath = os.path.join(args.save_dir, os.path.split(lca_fpath)[1]) octave.push(['pvp_data', 'write_fpath'], [pvp_data, write_fpath]) # write the new pvp weight file octave.eval('writepvpsharedweightfile(write_fpath, pvp_data)') print('[INFO] THE NEW NUMBER OF FEATURES IS {}.'.format(weights.shape[-1])) # write out the indices for each cell in the dictionary cell_ids = [cell_id.split('_')[1] for cell_id in cell_ids] cell_inds = [cell_ind + w_out_original for cell_ind in cell_inds] df = pd.DataFrame(zip(cell_ids, cell_inds), columns=['CellID', 'FeatureIndex']) df.to_csv(os.path.join(args.save_dir, 'cell_inds.txt'), index=False)