def pseudotime_analysis(self, vlm, figsize=(9,9), dpi=100): nn = NearestNeighbors(50) nn.fit(ind4.pcs[:,:4]) knn_pca = nn.kneighbors_graph(mode='distance') knn_pca = knn_pca.tocoo() G = igraph.Graph(list(zip(knn_pca.row, knn_pca.col)), directed=False, edge_attrs={'weight': knn_pca.data}) VxCl = G.community_multilevel(return_levels=False, weights="weight") labels = np.array(VxCl.membership) pc_obj = principal_curve(ind4.pcs[:,:4], False) pc_obj.arclength = np.max(pc_obj.arclength) - pc_obj.arclength labels = np.argsort(np.argsort(aggregate_np(labels, pc_obj.arclength, func=np.median)))[labels] manual_annotation = {str(i):[i] for i in labels} annotation_dict = {v:k for k, values in manual_annotation.items() for v in values } clusters = np.array([annotation_dict[i] for i in labels]) colors20 = np.vstack((plt.cm.tab20b(np.linspace(0., 1, 20))[::2], plt.cm.tab20c(np.linspace(0, 1, 20))[1::2])) vlm.set_clusters(clusters, cluster_colors_dict={k:colors20[v[0] % 20,:] for k,v in manual_annotation.items()}) k = 550 vlm.knn_imputation(n_pca_dims=n_comps,k=k, balanced=True, b_sight=np.minimum(k*8, vlm.S.shape[1]-1), b_maxl=np.minimum(k*4, vlm.S.shape[1]-1)) vlm.normalize_median() vlm.fit_gammas(maxmin_perc=[2,95], limit_gamma=True) vlm.normalize(which="imputed", size=False, log=True) vlm.Pcs = np.array(vlm.pcs[:,:2], order="C") vlm.predict_U() vlm.calculate_velocity() vlm.calculate_shift() vlm.extrapolate_cell_at_t(delta_t=1) vlm.estimate_transition_prob(hidim="Sx_sz", embed="Pcs", transform="log", psc=1, n_neighbors=150, knn_random=True, sampled_fraction=1) vlm.calculate_grid_arrows(smooth=0.9, steps=(25, 25), n_neighbors=200) vlm.set_clusters(ind4.ca["ClusterName"]) plt.figure(None,figsize=figsize, dpi=dpi) vlm.plot_grid_arrows(scatter_kwargs_dict={"alpha":0.7, "lw":0.7, "edgecolor":"0.4", "s":70, "rasterized":True}, min_mass=2.9, angles='xy', scale_units='xy', headaxislength=2.75, headlength=5, headwidth=4.8, quiver_scale=0.35, scale_type="absolute") plt.plot(pc_obj.projections[pc_obj.ixsort,0], pc_obj.projections[pc_obj.ixsort,1], c="w", lw=6, zorder=1000000) plt.plot(pc_obj.projections[pc_obj.ixsort,0], pc_obj.projections[pc_obj.ixsort,1], c="k", lw=3, zorder=2000000) plt.gca().invert_xaxis() plt.axis("off") plt.axis("equal");
def build_histogram(x_hist, data_mat, N_x_bins, N_y_bins, weighting_vec=1, min_resp=None, max_resp=None, func=None, debug=False, *args, **kwargs): """ Creates histogram for a single block of pixels Parameters ---------- x_hist : 1D numpy array bins for x-axis of 2d histogram data_mat : numpy array data to be binned for y-axis of 2d histogram weighting_vec : 1D numpy array or float weights. If setting all to one value, can be a scalar N_x_bins : integer number of bins in the x-direction N_y_bins : integer number of bins in the y-direction min_resp : float minimum value for y binning max_resp : float maximum value for y binning func : function function to be used to bin data_vec. All functions should take as input data_vec. Arguments should be passed properly to func. This has not been heavily tested. debug : bool, optional If True, extra debugging statements are printed. Default False Returns ------- pixel_hist : 2D numpy array contains the histogram of the input data Apply func to input data, convert to 1D array, and normalize """ if func is not None: y_hist = func(data_mat, *args, **kwargs) else: y_hist = data_mat ''' Get the min_resp and max_resp from y_hist if they are none ''' if min_resp is None: min_resp = np.min(y_hist) if max_resp is None: max_resp = np.max(y_hist) if debug: print('min_resp', min_resp, 'max_resp', max_resp) y_hist = __scale_and_discretize(y_hist, N_y_bins, max_resp, min_resp, debug) ''' Combine x_hist and y_hist into one matrix ''' if debug: print(np.shape(x_hist)) print(np.shape(y_hist)) try: group_idx = np.zeros((2, x_hist.size), dtype=np.int32) group_idx[0, :] = x_hist group_idx[1, :] = y_hist except: raise ''' Aggregate matrix for histogram of current chunk ''' if debug: print(np.shape(group_idx)) print(np.shape(weighting_vec)) print(N_x_bins, N_y_bins) try: if not disable_histogram: pixel_hist = aggregate_np(group_idx, weighting_vec, func='sum', size=(N_x_bins, N_y_bins), dtype=np.int32) else: pixel_hist = None except: raise return pixel_hist
def buildHistogram(x_hist, data_mat, N_x_bins, N_y_bins, weighting_vec=1, min_resp=None, max_resp=None, func=None, debug=False, *args, **kwargs): ''' Creates histogram for a single block of pixels Parameters: ------------- x_hist : 1D numpy array bins for x-axis of 2d histogram data_mat : numpy array data to be binned for y-axis of 2d histogram weighting_vec : 1D numpy array or float weights. If setting all to one value, can be a scalar N_x_bins : integer number of bins in the x-direction N_y_bins : integer number of bins in the y-direction min_resp : float minimum value for y binning max_resp : float maximum value for y binning func : function function to be used to bin data_vec. All functions should take as input data_vec. Arguments should be passed properly to func. This has not been heavily tested. Output: -------------- pixel_hist : 2D numpy array contains the histogram of the input data Apply func to input data, convert to 1D array, and normalize ''' if debug: print 'min_resp', min_resp, 'max_resp', max_resp y_hist = data_mat if func is not None: y_hist = func(y_hist, *args, **kwargs) y_hist = np.squeeze(np.reshape(y_hist, (data_mat.size, 1))) y_hist = np.clip(y_hist, min_resp, max_resp, y_hist) y_hist = y_hist - min_resp y_hist = y_hist / (max_resp - min_resp) ''' Descritize y_hist ''' y_hist = np.rint(y_hist * (N_y_bins - 1)) if debug: print 'ymin', min(y_hist), 'ymax', max(y_hist) ''' Combine x_hist and y_hist into one matrix ''' if debug: print np.shape(x_hist) print np.shape(y_hist) try: group_idx = np.zeros((2, x_hist.size), dtype=np.int32) group_idx[0, :] = x_hist group_idx[1, :] = y_hist except: raise ''' Aggregate matrix for histogram of current chunk ''' if debug: print np.shape(group_idx) print np.shape(weighting_vec) print N_x_bins, N_y_bins try: pixel_hist = npg.aggregate_np(group_idx, weighting_vec, func='sum', size=(N_x_bins, N_y_bins), dtype=np.int32) except: raise return pixel_hist
def aggregate_group_loop(*args, **kwargs): """wraps func in lambda which prevents aggregate_numpy from recognising and optimising it. Instead it groups and loops.""" func = kwargs['func'] del kwargs['func'] return aggregate_np(*args, func=lambda x: func(x), **kwargs)
"""wraps func in lambda which prevents aggregate_numpy from recognising and optimising it. Instead it groups and loops.""" func = kwargs['func'] del kwargs['func'] return aggregate_np(*args, func=lambda x: func(x), **kwargs) print("TODO: use more extensive tests") print("") print("-----simple examples----------") test_a = np.array([12.0, 3.2, -15, 88, 12.9]) test_group_idx = np.array([1, 0, 1, 4, 1 ]) print("test_a: ", test_a) print("test_group_idx: ", test_group_idx) print("aggregate(test_group_idx, test_a):") print(aggregate_np(test_group_idx, test_a)) # group vals by idx and sum # array([3.2, 9.9, 0., 0., 88.]) print("aggregate(test_group_idx, test_a, sz=8, func='min', fill_value=np.nan):") print(aggregate_np(test_group_idx, test_a, size=8, func='min', fill_value=np.nan)) # array([3.2, -15., nan, 88., nan, nan, nan, nan]) print("aggregate(test_group_idx, test_a, sz=5, func=lambda x: ' + '.join(str(xx) for xx in x),fill_value='')") print(aggregate_np(test_group_idx, test_a, size=5, func=lambda x: ' + '.join(str(xx) for xx in x), fill_value='')) print("") print("---------testing--------------") print("compare against group-and-loop with numpy") testable_funcs = {aliasing[f]: f for f in (np.sum, np.prod, np.any, np.all, np.min, np.max, np.std, np.var, np.mean)} test_group_idx = np.random.randint(0, int(1e3), int(1e5)) test_a = np.random.rand(int(1e5)) * 100 - 50 test_a[test_a > 25] = 0 # for use with bool functions
if pca: results.PCs = np.array(xr) #only the used components return results nn = NearestNeighbors(50) nn.fit(ind_pseudotime.pcs[:,:4]) knn_pca = nn.kneighbors_graph(mode='distance') knn_pca = knn_pca.tocoo() G = igraph.Graph(list(zip(knn_pca.row, knn_pca.col)), directed=False, edge_attrs={'weight': knn_pca.data}) VxCl = G.community_multilevel(return_levels=False, weights="weight") labels = np.array(VxCl.membership) pc_obj = principal_curve(ind_pseudotime.pcs[:,:4], False) pc_obj.arclength = np.max(pc_obj.arclength) - pc_obj.arclength labels = np.argsort(np.argsort(aggregate_np(labels, pc_obj.arclength, func=np.median)))[labels] manual_annotation = {str(i):[i] for i in labels} annotation_dict = {v:k for k, values in manual_annotation.items() for v in values } clusters = np.array([annotation_dict[i] for i in labels]) colors20 = np.vstack((plt.cm.tab20b(np.linspace(0., 1, 20))[::2], plt.cm.tab20c(np.linspace(0, 1, 20))[1::2])) #ind_pseudotime.set_clusters(clusters, cluster_colors_dict={k:colors20[v[0] % 20,:] for k,v in manual_annotation.items()}) ind_pseudotime.set_clusters(ind_pseudotime.ca["ClusterName"]) k = 550 ind_pseudotime.knn_imputation(n_pca_dims=n_comps,k=k, balanced=True, b_sight=np.minimum(k*8, ind_pseudotime.S.shape[1]-1), b_maxl=np.minimum(k*4, ind_pseudotime.S.shape[1]-1)) ind_pseudotime.normalize_median() ind_pseudotime.fit_gammas(maxmin_perc=[2,95], limit_gamma=True)
def build_histogram(x_hist, data_mat, N_x_bins, N_y_bins, weighting_vec=1, min_resp=None, max_resp=None, func=None, debug=False, *args, **kwargs): """ Creates histogram for a single block of pixels Parameters ---------- x_hist : 1D numpy array bins for x-axis of 2d histogram data_mat : numpy array data to be binned for y-axis of 2d histogram weighting_vec : 1D numpy array or float weights. If setting all to one value, can be a scalar N_x_bins : integer number of bins in the x-direction N_y_bins : integer number of bins in the y-direction min_resp : float minimum value for y binning max_resp : float maximum value for y binning func : function function to be used to bin data_vec. All functions should take as input data_vec. Arguments should be passed properly to func. This has not been heavily tested. debug : bool, optional If True, extra debugging statements are printed. Default False Returns ------- pixel_hist : 2D numpy array contains the histogram of the input data Apply func to input data, convert to 1D array, and normalize """ if func is not None: y_hist = func(data_mat, *args, **kwargs) else: y_hist = data_mat ''' Get the min_resp and max_resp from y_hist if they are none ''' if min_resp is None: min_resp = np.min(y_hist) if max_resp is None: max_resp = np.max(y_hist) if debug: print('min_resp', min_resp, 'max_resp', max_resp) y_hist = __scale_and_discretize(y_hist, N_y_bins, max_resp, min_resp, debug) ''' Combine x_hist and y_hist into one matrix ''' if debug: print(np.shape(x_hist)) print(np.shape(y_hist)) try: group_idx = np.zeros((2, x_hist.size), dtype=np.int32) group_idx[0, :] = x_hist group_idx[1, :] = y_hist except: raise ''' Aggregate matrix for histogram of current chunk ''' if debug: print(np.shape(group_idx)) print(np.shape(weighting_vec)) print(N_x_bins, N_y_bins) try: if not disable_histogram: pixel_hist = aggregate_np(group_idx, weighting_vec, func='sum', size=(N_x_bins, N_y_bins), dtype=np.int32) else: pixel_hist = None except: raise return pixel_hist