Skip to content
Permalink
Browse files

Merge pull request #18 from dpeerlab/apprxomiate_neigbhors

Apprxomiate neigbhors
  • Loading branch information...
ManuSetty committed Aug 22, 2019
2 parents c7df09d + 73aa8fd commit 57069e26c1e6115b8319cee11cba0553fd3b1ef8
Showing with 30 additions and 13 deletions.
  1. +3 −2 setup.py
  2. +2 −2 src/palantir/plot.py
  3. +25 −9 src/palantir/utils.py
@@ -20,7 +20,7 @@


setup(name='palantir',
version=__version__, # read in from the exec of version.py; ignore error
version=__version__,# read in from the exec of version.py; ignore error
description='Palantir for modeling continuous cell state and cell fate choices in single cell data',
url='https://github.com/dpeerlab/palantir',
author='Manu Setty',
@@ -43,6 +43,7 @@
'matplotlib>=2.2.2',
'seaborn>=0.8.1',
'tzlocal',
'rpy2>=3.0.2'
'rpy2>=3.0.2',
'scanpy'
],
)
@@ -258,8 +258,8 @@ def plot_gene_expression(data, tsne, genes, plot_scale=False,
for g, ax in zip(genes, fig):
# Data
c = data.loc[cells, g]
vmin = np.percentile(c, percentile)
vmax = np.percentile(c, 100 - percentile)
vmin = np.percentile(c[~np.isnan(c)], percentile)
vmax = np.percentile(c[~np.isnan(c)], 100 - percentile)

ax.scatter(tsne['x'], tsne['y'], s=3, color='lightgrey')
ax.scatter(tsne.loc[cells, 'x'], tsne.loc[cells, 'y'], s=3,
@@ -7,6 +7,7 @@
from sklearn.neighbors import NearestNeighbors
from scipy.sparse import csr_matrix, find, issparse
from scipy.sparse.linalg import eigs
import scanpy as sc


def run_pca(data, n_components=300):
@@ -22,7 +23,7 @@ def run_pca(data, n_components=300):
return pca_projections, pca.explained_variance_ratio_


def run_diffusion_maps(data_df, n_components=10, knn=30, n_jobs=-1):
def run_diffusion_maps(data_df, n_components=10, knn=30, n_jobs=-1, alpha=0):
"""Run Diffusion maps using the adaptive anisotropic kernel
:param data_df: PCA projections of the data or adjancency matrix
@@ -34,17 +35,24 @@ def run_diffusion_maps(data_df, n_components=10, knn=30, n_jobs=-1):
N = data_df.shape[0]
if not issparse(data_df):
print('Determing nearest neighbor graph...')
nbrs = NearestNeighbors(n_neighbors=int(knn), metric='euclidean',
n_jobs=n_jobs).fit(data_df.values)
kNN = nbrs.kneighbors_graph(data_df.values, mode='distance')
# nbrs = NearestNeighbors(n_neighbors=int(knn), metric='euclidean',
# n_jobs=n_jobs).fit(data_df.values)
# kNN = nbrs.kneighbors_graph(data_df.values, mode='distance')
temp = sc.AnnData(data_df.values)
sc.pp.neighbors(temp, n_pcs=0, n_neighbors=knn)
kNN = temp.uns['neighbors']['distances']

# Adaptive k
adaptive_k = int(np.floor(knn / 3))
nbrs = NearestNeighbors(n_neighbors=int(adaptive_k),
metric='euclidean', n_jobs=n_jobs).fit(data_df.values)
adaptive_std = nbrs.kneighbors_graph(
data_df.values, mode='distance').max(axis=1)
adaptive_std = np.ravel(adaptive_std.todense())
# nbrs = NearestNeighbors(n_neighbors=int(adaptive_k),
# metric='euclidean', n_jobs=n_jobs).fit(data_df.values)
# adaptive_std = nbrs.kneighbors_graph(
# data_df.values, mode='distance').max(axis=1)
# adaptive_std = np.ravel(adaptive_std.todense())
adaptive_std = np.zeros(N)

for i in np.arange(len(adaptive_std)):
adaptive_std[i] = np.sort(kNN.data[kNN.indptr[i]:kNN.indptr[i + 1]])[adaptive_k - 1]

# Kernel
x, y, dists = find(kNN)
@@ -60,6 +68,14 @@ def run_diffusion_maps(data_df, n_components=10, knn=30, n_jobs=-1):

# Markov
D = np.ravel(kernel.sum(axis=1))

if alpha > 0:
# L_alpha
D[D != 0] = D[D != 0] ** (-alpha)
mat = csr_matrix((D, (range(N), range(N))), shape=[N, N])
kernel = mat.dot(kernel).dot(mat)
D = np.ravel(kernel.sum(axis=1))

D[D != 0] = 1 / D[D != 0]
T = csr_matrix((D, (range(N), range(N))), shape=[N, N]).dot(kernel)
# Eigen value dcomposition

0 comments on commit 57069e2

Please sign in to comment.
You can’t perform that action at this time.