def logdotexp(logA, logB): """ Given the log of two matrices A and B as logA and logB, carries out A * B in log space """ maxA = np.max(logA) maxB = np.max(logB) if np.abs(maxA) == np.Inf: sortA = np.sort(logA.reshape(-1)) if np.sum(np.abs(sortA) != np.Inf) == 0: C = np.zeros([logA.shape[0], logB.shape[1]], dtype='complex128') - np.Inf return C else: maxA = sortA[np.sum(np.abs(sortA) != np.Inf) - 1] if np.abs(maxB) == np.Inf: sortB = np.sort(logB.reshape(-1)) if np.sum(np.abs(sortB) != np.Inf) == 0: C = np.zeros([logA.shape[0], logB.shape[1]], dtype='complex128') - np.Inf return C else: maxB = sortB[np.sum(np.abs(sortB) != np.Inf)] expA = np.exp(logA - maxA) expB = np.exp(logB - maxB) C = np.log(np.dot(expA, expB)) + maxA + maxB return C
def G4(positions, cell, numbers, elements, params): cutoff_radius = params['cutoff_radius'] eta = params['eta'] zeta = params['zeta'] lbda = params['lbda'] cos, Rij, Rik, Rjk, i, jk = atomsAngle(positions, cell, cutoff_radius) g4 = (1 + lbda * cos)**zeta * np.exp( -eta * (Rij**2 + Rik**2 + Rjk**2) / cutoff_radius**2) g4 = g4 * cutoff(cutoff_radius, Rij) * cutoff(cutoff_radius, Rik) * cutoff( cutoff_radius, Rjk) g4 *= 2**(1 - zeta) atoms_mask = np.arange(len(positions))[:, None] == i[None, :] # the shape of g4 will become (#atoms, len(g4)) and multiply atoms_mask to get the corresponding center atom's fingerprints g4 = np.repeat(g4[None, :], len(positions), axis=0) g4 *= atoms_mask index = np.indices((len(elements), len(elements))).reshape(2, -1) mask = index[1] >= index[0] index = index[:, mask].T pairs = np.repeat(np.sort(numbers[jk])[:, None], len(index), axis=1) elements = np.sort(elements)[index] pairs_mask = np.sum(pairs == elements, axis=2) pairs_mask = np.where(pairs_mask == 2, 1, 0) g4 = np.dot(g4, pairs_mask) return g4
def mle_batch_euclidean(self, data, k): """ Calculates LID values of data w.r.t batch Args: data: samples to calculate LIDs of batch: samples to calculate LIDs against k: the number of nearest neighbors to consider Returns: the calculated LID values """ batch = self.training_data_ndarray f = lambda v: -k / np.sum(np.log((v / v[-1]) + 1e-9)) gamma = self.classifier.kernel.gamma if gamma is None: gamma = 1.0 / self.training_data_ndarray.shape[1] K = rbf_kernel(data, Y=batch, gamma=gamma) K = np.reciprocal(K) # K = cdist(data, batch) # get the closest k neighbours if self.xc is not None and self.xc.shape[0] == 1: # only one attack sample sorted_distances = np.sort(K)[0, 1:1 + k] else: sorted_distances = np.sort(K)[0, 0:k] a = np.apply_along_axis(f, axis=0, arr=sorted_distances) return np.nan_to_num(a)
def reduce_grid(x): """ Undoes expand_grid to take (nx, 2) array to two vectors containing unique values of each col. :param x: (nx, 2) points :return: x1, x2 each vectors """ x1 = np.sort(np.unique(x[:, 0])) x2 = np.sort(np.unique(x[:, 1])) return x1, x2
def other_r_selection(rl1_select, z2_z1s): ''' Chose the meaningful dimensions from the second layer to the end of the network rl1_select (list): The dimension kept over the first layer z2_z1s (list of ndarrays): z^{(l + 1)}| z^{(l)}, s -------------------------------------------------------------------------- return (list of int): The dimensions to keep from the second layer of the network ''' S = [zz.shape[2] for zz in z2_z1s] + [1] CORR_THRESHOLD = 0.20 L = len(z2_z1s) M = np.array([zz.shape[0] for zz in z2_z1s] + [z2_z1s[-1].shape[1]]) prev_new_r = [len(rl1_select)] dims_to_keep = [] for l in range(L): # Will not keep the following layers if one of the previous layer is of dim 1 if prev_new_r[l] <= 1: dims_to_keep.append([]) prev_new_r.append(0) else: old_rl = z2_z1s[l].shape[-1] corr = np.zeros(old_rl) for s in range(S[l]): for m1 in range(M[l + 1]): pca = PCA(n_components=1) pca.fit_transform(z2_z1s[l][m1, :, s]) corr += np.abs(pca.components_[0]) average_corr = corr / (S[l] * M[l + 1]) new_rl = np.sum(average_corr > CORR_THRESHOLD) if prev_new_r[l] > new_rl: # Respect r1 > r2 > r3 .... wanted_dims = np.where( average_corr > CORR_THRESHOLD)[0].tolist() wanted_dims = np.sort(wanted_dims) dims_to_keep.append(wanted_dims) else: # Have to delete other dimensions to match r1 > r2 > r3 .... nb_dims_to_remove = old_rl - prev_new_r[l] + 1 unwanted_dims = np.argpartition( average_corr, nb_dims_to_remove)[:nb_dims_to_remove] wanted_dims = list(set(range(old_rl)) - set(unwanted_dims)) wanted_dims = np.sort(wanted_dims) dims_to_keep.append(wanted_dims) new_rl = len(wanted_dims) prev_new_r.append(new_rl) return dims_to_keep
def step_generator(t, **kwargs): ''' Makes a random piecewise constant step frequency input. Note that the steps always take on integer values. ''' # lets make random piecewise frequency num_steps = 2 if 'num_steps' in kwargs: num_steps = kwargs['num_steps'] - 1 # pick num_pieces random locations for step ledges r = np.random.permutation(len(t))[:num_steps] r = np.sort(r) # generate random level per step levels = np.round(5 * np.random.rand(num_steps + 1)) + 1 f = np.ones((t.size, 1)) # set each chunk to appropriate level f[:r[0]] *= levels[0] # set first chunk to appropriate level for n in range(1, len(r) - 1): f[r[n - 1]:r[n]] *= levels[n] f[r[-1]:] *= levels[-1] return f
def run(backend=SUPPORTED_BACKENDS[0], quiet=True): num_rows = 10 rank = 3 matrix = np.random.normal(size=(num_rows, num_rows)) matrix = 0.5 * (matrix + matrix.T) # Solve the problem with pymanopt. manifold = Oblique(rank, num_rows) cost, euclidean_gradient, euclidean_hessian = create_cost_and_derivates( manifold, matrix, backend ) problem = pymanopt.Problem( manifold, cost, euclidean_gradient=euclidean_gradient, euclidean_hessian=euclidean_hessian, ) optimizer = TrustRegions(verbosity=2 * int(not quiet)) X = optimizer.run(problem).point if quiet: return C = X.T @ X print("Diagonal elements:", np.diag(C)) print("Eigenvalues:", np.sort(np.linalg.eig(C)[0].real)[::-1])
def train(self, subsample_idcs=None, ridge=1e-9, max_storage=1e8): self.idcs = np.sort(subsample_idcs) self.X_ind = self.X[self.idcs, :] self.chunk_size = int(max_storage / float(self.idcs.shape[0])) if self.chunk_size == 0: self.chunk_size = 1 csz = self.chunk_size #can handle len(idcs)**2 memory, len(idcs)**3 time cost Kxx = self.k(self.X[self.idcs, :]) Ysum = np.zeros((self.idcs.shape[0], self.Y.shape[1])) Ksum = np.zeros((self.idcs.shape[0], self.idcs.shape[0])) pbar = ProgressBar('Chunk sum', 0, self.X.shape[0]) j = 0 csz = self.idcs.shape[0] while j * csz < self.X.shape[0]: pbar.update(j * csz) Kchunk = self.k(self.X[j * csz:(j + 1) * csz, :], self.X[self.idcs, :]) Ychunk = self.Y[j * csz:(j + 1) * csz, :] Ysum += Kchunk.T.dot(Ychunk) Ksum += Kchunk.T.dot(Kchunk) j += 1 pbar.finish() self.V = Ksum + self.lvar * Kxx self.V += ridge * np.fabs(self.V).max() * np.eye(self.V.shape[0]) self.alpha = np.linalg.solve(self.V, Ysum) self.V /= self.lvar
def generate_random_features_1d(data, num_feat): ''' This functions generates random features for a set of paired inputs, such as times and observations Inputs: - data: (n x d) array - num_feat: number of random features to be generated Output: - features in fixed dimensional space (n x num_feat) array ''' # find length-scale for random features using median heuristic if isinstance(data, list): mean_obs = int(np.mean([len(row) for row in data])) data_sig = np.array([ x[np.sort(np.random.choice(range(len(x)), mean_obs, replace=False))] for x in data if len(x) >= mean_obs ]) sig = meddistance(np.concatenate(data_sig)[:, np.newaxis], subsample=1000) else: sig = meddistance(data, subsample=1000) return np.array( [f1(row[:, np.newaxis], rp(num_feat, sig, 1)) for row in data])
def specified_task(problem): """Given a problem, return parameters for running a topology optimization.""" fixdofs = np.flatnonzero(problem.normals.ravel()) alldofs = np.arange(2 * (problem.width + 1) * (problem.height + 1)) freedofs = np.sort(list(set(alldofs) - set(fixdofs))) params = { # material properties 'young': 1, 'young_min': 1e-9, 'poisson': 0.3, 'g': 0, # constraints 'volfrac': problem.density, 'xmin': 0.001, 'xmax': 1.0, # input parameters 'nelx': problem.width, 'nely': problem.height, 'mask': problem.mask, 'freedofs': freedofs, 'fixdofs': fixdofs, 'forces': problem.forces.ravel(), 'penal': 3.0, 'filter_width': 2, } return params
def summarize_res(sname, datasize): print(sname) res = [] times = [] for i in range(100): PATH = ROOT_PATH + "/MMR_IVs/results/zoo/" + sname + "/" filename = os.path.join( PATH, str(date.today()), 'LMO_errs_{}_nystr_prodkern_{}.npy'.format(i, datasize)) if os.path.exists(filename): tmp_res = np.load(filename, allow_pickle=True) if tmp_res[-1] is not None: res += [tmp_res[-1]] time_path = os.path.join( PATH, str(date.today()), '/LMO_errs_{}_nystr_prodkern_{}_time.npy'.format(i, datasize)) if os.path.exists(time_path): t = np.load(time_path) times += [t] res = np.array(res) times = np.array(times) res = remove_outliers(res) times = np.sort(times)[:80] print(times) print('mean, std: ', np.mean(res), np.std(res)) print('time: ', np.mean(times), np.std(times))
def __init__(self, breakpoints, alpha=0.05, penalizer=0.0, fit_intercept=True, *args, **kwargs): super(PiecewiseExponentialRegressionFitter, self).__init__(alpha=alpha) breakpoints = np.sort(breakpoints) if len(breakpoints) and not (breakpoints[-1] < np.inf): raise ValueError("Do not add inf to the breakpoints.") if len(breakpoints) and breakpoints[0] < 0: raise ValueError("First breakpoint must be greater than 0.") self.breakpoints = np.append(breakpoints, [np.inf]) self.n_breakpoints = len(self.breakpoints) self._hazard = egrad(self._cumulative_hazard, argnum=1) # pylint: disable=unexpected-keyword-arg self.penalizer = penalizer self.fit_intercept = fit_intercept self._fitted_parameter_names = [ "lambda_%d_" % i for i in range(self.n_breakpoints) ]
def interpolate(t, y, num_obs=50): """ Interpolates each trajectory such that observation times coincide for each one. Note: initially cubic interpolation gave great power, but this happens as an artifact of the interpolation, as both trajectories have the same number of observations. Type I error was increased as a result. To avoid this we settled for a linear interpolation between observations. Splines were also tried but gave very bad interpolations. """ t = np.array([np.sort(row) for row in t]) t = np.insert(t, 0, 0, axis=1) t = np.insert(t, len(t[0]), 1, axis=1) y = np.insert(y, 0, y[:, 0], axis=1) y = np.insert(y, len(y[0]), y[:, -1], axis=1) new_t = np.zeros(num_obs) new_y = np.zeros(num_obs) for i in range(len(t)): f = interp1d(t[i], y[i], kind='linear') #f = splrep(t[i], y[i]) t_temp = np.random.uniform(low=0.0, high=1.0, size=num_obs) #np.linspace(0.1,0.9,num_obs) y_temp = f(t_temp) #y_temp = splev(t_temp, f, der=0) new_y = np.vstack((new_y, y_temp)) new_t = np.vstack((new_t, t_temp)) return new_t[1:], new_y[1:]
def train(self, subsample_idcs=None, ridge=1e-9): self.idcs = np.sort(subsample_idcs) self.X_ind = self.X[self.idcs, :] Kxx = self.k(self.X[self.idcs, :]) self.alpha = np.linalg.solve( Kxx + self.lvar * np.eye(self.idcs.shape[0]), self.Y[self.idcs, :]) self.V = Kxx + self.lvar * np.eye(self.idcs.shape[0])
def permute(self, perm): """ Permute the discrete latent states. """ assert np.all(np.sort(perm) == np.arange(self.K)) self.init_state_distn.permute(perm) self.transitions.permute(perm) self.observations.permute(perm)
def _simplex_projection(x): u = np.sort(x)[::-1] idcs = np.arange(1, u.shape[0] + 1) rho_nz = u + 1. / idcs * (1. - np.cumsum(u)) > 0 rho = idcs[rho_nz].max() lmb = 1. / rho * (1. - u[:rho].sum()) out = np.maximum(x + lmb, 0.) return out / out.sum()
def pretrain(self, subsample_idcs, ridge=1e-9): self.sridcs = np.sort(subsample_idcs) #get matrices necessary to compute khat(x,x') and muhat(x) #using subset of regressors srgp = SubsetRegressorsGP(self.X, self.Y, self.k, self.lvar) srgp.train(self.sridcs, ridge) self.pre_alpha = srgp.alpha self.V = srgp.V self.V += ridge * np.fabs(self.V).max() * np.eye(self.sridcs.shape[0])
def compute_khat_iterates(iterate_chains, warmup=0.85, param_idx=0, increasing=True): """ Compute the khat over iterates for a variational parameter after removing warmup. Parameters ---------- iterate_chains : multi-dimensional array, shape=(n_chains, n_iters, n_var_params) warmup : warmup iterates param_idx : index of the variational parameter increasing : boolean sort array in increasing order: TRUE or decreasing order:FALSE fraction: the fraction of iterates Returns ------- maximum of khat over all chains for the variational parameter param_idx """ chains = iterate_chains[:, :, param_idx] n_iters = chains.shape[1] n_chains = chains.shape[0] k_hat_values = np.zeros(n_chains) for i in range(n_chains): if increasing: sorted_chain = np.sort(chains[i, :]) else: sorted_chain = np.sort(-chains[i, :]) ind_last = int(n_iters * warmup) filtered_chain = sorted_chain[ind_last:] if increasing: filtered_chain = filtered_chain - np.min(filtered_chain) else: filtered_chain = filtered_chain - np.min(filtered_chain) k_post, _ = gpdfit(filtered_chain) k_hat_values[i] = k_post return np.nanmax(k_hat_values)
def sample_inputs(type, ndata, range): low, high = range if type == 'gridbox': x = np.linspace(low, high, ndata).reshape(ndata, 1) elif type == "uniform": x = np.random.uniform(low, high, size=(ndata, 1)) elif type == 'normal': x = np.random.randn(ndata, 1) return np.sort(x, axis=0)
def load_data(path='datasets/plays.tsv'): raw_data = pd.read_table('datasets/plays.tsv') raw_data = raw_data.drop(raw_data.columns[1], axis=1) raw_data.columns = ['user', 'artist', 'plays'] data = raw_data.dropna().sample(frac=1)[:70] data['user_id'] = data['user'].astype("category").cat.codes data['artist_id'] = data['artist'].astype("category").cat.codes data = data.drop(['user', 'artist'], axis=1) data = data.loc[data.plays != 0] users = list(np.sort(data.user_id.unique())) artists = list(np.sort(data.artist_id.unique())) plays = list(data.plays) rows = data.user_id.astype(int) cols = data.artist_id.astype(int) return sparse.csr_matrix((plays, (rows, cols)), shape=(len(users), len(artists)))
def generate_synthetic_data(self, theta, Ndata): """ Generate a synthetic dataset from parameters theta. :param theta: ChangepointParams instance :param Ndata: number of data points :return: (x, y) pairs corresponding to synthetic dataset """ L = self.xmax - self.xmin x = np.sort(self.xmin + L * np.random.uniform(size=(Ndata, ))) epsilon = np.random.normal(size=x.shape) y = self.predict(theta, x) + theta.sig * epsilon return x, y
def k_select(w_s, k, new_L, clustering_layer): ''' Automatic choice of the number of components by layer w_s (list): The path probabilities for all s in [1,S] k (dict): The number of component on each layer new_L (int): The selected total number of layers. clustering_layer (int): The index of the clustering layer -------------------------------------------------------------------------- returns (dict): The components kept for all the layers of the network ''' L = len(k) n_clusters = k[clustering_layer] # If the clustering layer (cl) is deleted, define the last existing layer as cl last_layer_idx = new_L - 1 if last_layer_idx < clustering_layer: clustering_layer = last_layer_idx components_to_keep = [] w = w_s.reshape(*k, order='C') for l in range(new_L): PROBA_THRESHOLD = 1 / (k[l] * 4) other_layers_indices = tuple(set(range(L)) - set([l])) components_proba = w.sum(other_layers_indices) #print(components_proba) if l == clustering_layer: biggest_lik_comp = np.sort( components_proba.argsort()[::-1][:n_clusters]) components_to_keep.append(biggest_lik_comp) else: comp_kept = np.where(components_proba > PROBA_THRESHOLD)[0] comp_kept = np.sort(comp_kept) components_to_keep.append(comp_kept) return components_to_keep
def _update_clipping_parameter(self, impt_smplg_weight): """ Updates the clipping parameter for Bottou's heuristic Args: impt_smplg_weight (np.array): importance sampling weights """ if self.hyperparams['M'] == 'auto': sorted_copy = np.sort(impt_smplg_weight.copy()) self.hyperparams['M'] = sorted_copy[-5] elif self.hyperparams['M'] == 'None': pass else: self.hyperparams['M'] = float(self.hyperparams['M'])
def init_multicomponent_source(sky_coord, frame, observation, bg_rms, flux_percentiles=None, thresh=1., symmetric=True, monotonic=True): """Initialize multiple components See `MultiComponentSource` for a description of the parameters """ if flux_percentiles is None: flux_percentiles = [25] # Initialize the first component as an extended source sed, morph = init_extended_source(sky_coord, frame, observation, bg_rms, thresh, symmetric, monotonic) # create a list of components from base morph by layering them on top of # each other so that they sum up to morph K = len(flux_percentiles) + 1 Ny, Nx = morph.shape morphs = np.zeros((K, Ny, Nx), dtype=morph.dtype) morphs[0, :, :] = morph[:, :] max_flux = morph.max() percentiles_ = np.sort(flux_percentiles) last_thresh = 0 for k in range(1, K): perc = percentiles_[k - 1] flux_thresh = perc * max_flux / 100 mask_ = morph > flux_thresh morphs[k - 1][mask_] = flux_thresh - last_thresh morphs[k][mask_] = morph[mask_] - flux_thresh last_thresh = flux_thresh # renormalize morphs: initially Smax for k in range(K): if np.all(morphs[k] <= 0): msg = "Zero or negative morphology for component {} at y={}, x={}" logger.warning(msg.format(k, *skycoords)) morphs[k] /= morphs[k].max() # optimal SEDs given the morphologies, assuming img only has that source seds = get_best_fit_seds(morphs, frame, observation) for k in range(K): if np.any(seds[k] <= 0): # If the flux in all channels is <=0, # the new sed will be filled with NaN values, # which will cause the code to crash later msg = "Zero or negative SED {} for component {} at y={}, x={}".format(seds[k], k, *sky_coord) if np.all(sed <= 0): logger.warning(msg) else: logger.info(msg) return seds, morphs
def __init__(self, breakpoints, *args, **kwargs): breakpoints = np.sort(breakpoints) if not (breakpoints[-1] < np.inf): raise ValueError("Do not add inf to the breakpoints.") if breakpoints[0] < 0: raise ValueError("First breakpoint must be greater than 0.") self.breakpoints = np.append(breakpoints, [np.inf]) n_breakpoints = len(self.breakpoints) self._fitted_parameter_names = ["lambda_%d_" % i for i in range(n_breakpoints)] super(PiecewiseExponentialFitter, self).__init__(*args, **kwargs)
def plot_train_fits(W, axarr): params = pred_fun(W, train_images) means = params[:, :8] variances = np.exp(params[:,-8:]) # axis aligned variances # plot 5 random data points and 4 random marginals idx = np.sort(np.random.permutation(train_images.shape[0])[:axarr.shape[0]]) dims = np.sort(np.random.permutation(samps.shape[-1])[:axarr.shape[1]]) for r, i in enumerate(idx): for c, d in enumerate(dims): axarr[r, c].cla() n, bins, patches = axarr[r, c].hist(samps[i, :, d], bins=20, normed=True) axarr[r, c].plot( [means[i, d], means[i, d]], [0, n.max()] ) thgrid = np.linspace(min(bins[0], means[i,d]), max(bins[-1], means[i,d]), 50) axarr[r, c].plot(thgrid, np.exp(gu.mog_logmarglike(thgrid, means = np.array([ means[i] ]), covs = np.array([ np.diag(variances[i]) ]), pis = np.array([1.]), ind = d))) axarr[r, c].set_title("Idx = %d, dim = %d"%(i, d)) plt.draw()
def list_simulate_spectral(cov, J, n_simulate=1000, seed=82): """ Simulate the null distribution using the spectrums of the covariance matrix. This is intended to be used to approximate the null distribution. Return (a numpy array of simulated n*FSSD values, eigenvalues of cov) """ # eigen decompose eigs, _ = np.linalg.eig(cov) eigs = np.real(eigs) # sort in decreasing order eigs = -np.sort(-eigs) sim_fssds = FSSD.simulate_null_dist(eigs, J, n_simulate=n_simulate, seed=seed) return sim_fssds, eigs
def __init__(self, breakpoints, alpha=0.05, penalizer=0.0): super(PiecewiseExponentialRegressionFitter, self).__init__(alpha=alpha) breakpoints = np.sort(breakpoints) if len(breakpoints) and not (breakpoints[-1] < np.inf): raise ValueError("Do not add inf to the breakpoints.") if len(breakpoints) and breakpoints[0] < 0: raise ValueError("First breakpoint must be greater than 0.") self.breakpoints = np.append(breakpoints, [np.inf]) self.n_breakpoints = len(self.breakpoints) self.penalizer = penalizer self._fitted_parameter_names = ["lambda_%d_" % i for i in range(self.n_breakpoints)]
def generate_random_features(t, y, num_feat): ''' This functions generates random features for a set of paired inputs, such as times and observations Inputs: - t: first dimension of data, (n x d) array - y: second dimension of data, (n x d) array - num_feat: number of random features to be generated Output: - features in fixed dimensional space (n x num_feat) array ''' # if different sizes do interpolation and coerce to equal number of dimensions if isinstance(t, list): # find length-scale for random features using median heuristic mean_obs = int(np.mean([len(row) for row in t])) np.random.seed(0) t_sig = np.array([ x[np.sort(np.random.choice(range(len(x)), mean_obs, replace=False))] for x in t if len(x) > mean_obs ]) y_sig = np.array([ x[np.sort(np.random.choice(range(len(x)), mean_obs, replace=False))] for x in y if len(x) > mean_obs ]) sig = meddistance(np.hstack((t_sig, y_sig)), subsample=1000) np.random.seed() else: sig = meddistance(np.hstack((t, y)), subsample=1000) return np.array([ f1(np.hstack((t[:, np.newaxis], y[:, np.newaxis])), rp(num_feat, sig, 2)) for (t, y) in zip(t, y) ])
def init_multicomponent_source(sky_coord, scene, observations, bg_rms, flux_percentiles=None, obs_idx=0, thresh=1., symmetric=True, monotonic=True): """Initialize multiple components See `MultiComponentSource` for a description of the parameters """ try: iter(observations) except TypeError: observations = [observations] if flux_percentiles is None: flux_percentiles = [25] # Initialize the first component as an extended source sed, morph = init_extended_source(sky_coord, scene, observations, bg_rms, obs_idx, thresh, symmetric, monotonic) # create a list of components from base morph by layering them on top of # each other so that they sum up to morph K = len(flux_percentiles) + 1 Ny, Nx = morph.shape morphs = np.zeros((K, Ny, Nx), dtype=morph.dtype) morphs[0, :, :] = morph[:, :] max_flux = morph.max() percentiles_ = np.sort(flux_percentiles) last_thresh = 0 for k in range(1, K): perc = percentiles_[k - 1] flux_thresh = perc * max_flux / 100 mask_ = morph > flux_thresh morphs[k - 1][mask_] = flux_thresh - last_thresh morphs[k][mask_] = morph[mask_] - flux_thresh last_thresh = flux_thresh # renormalize morphs: initially Smax for k in range(K): morphs[k] /= morphs[k].max() # optimal SEDs given the morphologies, assuming img only has that source seds = get_best_fit_seds(morphs, scene, observations) return seds, morphs
def projectSimplex(mat): """ project each row vector to the simplex """ nPoints, nVars = mat.shape mu = np.fliplr(np.sort(mat, axis=1)) sum_hist = np.cumsum(mu, axis=1) flag = (mu - 1./np.tile(np.arange(1,nVars+1),(nPoints,1))*(sum_hist-1) > 0) f_flag = lambda flagPoint: len(flagPoint) - 1 - \ flagPoint[::-1].argmax() lastTrue = map(f_flag, flag) sm_row = sum_hist[np.arange(nPoints), lastTrue] theta = (sm_row - 1)*1./(np.array(lastTrue)+1.) w = np.maximum(mat - np.tile(theta, (nVars,1)).T, 0.) return w
def projectSimplex_vec(v): """ project vector v onto the probability simplex Parameter --------- v: shape(nVars,) input vector Returns ------- w: shape(nVars,) projection of v onto the probability simplex """ nVars = v.shape[0] mu = np.sort(v,kind='quicksort')[::-1] sm_hist = np.cumsum(mu) flag = (mu - 1./np.arange(1,nVars+1)*(sm_hist-1) > 0) lastTrue = len(flag) - 1 - flag[::-1].argmax() sm_row = sm_hist[lastTrue] theta = 1./(lastTrue+1) * (sm_row - 1) w = np.maximum(v-theta, 0.) return w