本文整理汇总了Python中anndata.AnnData方法的典型用法代码示例。如果您正苦于以下问题:Python anndata.AnnData方法的具体用法?Python anndata.AnnData怎么用?Python anndata.AnnData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类anndata
的用法示例。
在下文中一共展示了anndata.AnnData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit_transform
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def fit_transform(self, X, **kwargs):
"""Computes the diffusion operator and the position of the cells in the
embedding space
Parameters
----------
X : array, shape=[n_samples, n_features]
input data with `n_samples` samples and `n_dimensions`
dimensions. Accepted data types: `numpy.ndarray`,
`scipy.sparse.spmatrix`, `pd.DataFrame`, `anndata.AnnData` If
`knn_dist` is 'precomputed', `data` should be a n_samples x
n_samples distance or affinity matrix
kwargs : further arguments for `PHATE.transform()`
Keyword arguments as specified in :func:`~phate.PHATE.transform`
Returns
-------
embedding : array, shape=[n_samples, n_dimensions]
The cells embedded in a lower dimensional space using PHATE
"""
with _logger.task("PHATE"):
self.fit(X)
embedding = self.transform(**kwargs)
return embedding
示例2: test_log1p
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def test_log1p(tmp_path):
A = np.random.rand(200, 10)
A_l = np.log1p(A)
ad = AnnData(A)
ad2 = AnnData(A)
ad3 = AnnData(A)
ad3.filename = tmp_path / 'test.h5ad'
sc.pp.log1p(ad)
assert np.allclose(ad.X, A_l)
sc.pp.log1p(ad2, chunked=True)
assert np.allclose(ad2.X, ad.X)
sc.pp.log1p(ad3, chunked=True)
assert np.allclose(ad3.X, ad.X)
# Test base
ad4 = AnnData(A)
sc.pp.log1p(ad4, base=2)
assert np.allclose(ad4.X, A_l/np.log(2))
示例3: test_normalize_per_cell
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def test_normalize_per_cell():
adata = AnnData(
np.array([[1, 0], [3, 0], [5, 6]]))
sc.pp.normalize_per_cell(adata, counts_per_cell_after=1,
key_n_counts='n_counts2')
assert adata.X.sum(axis=1).tolist() == [1., 1., 1.]
# now with copy option
adata = AnnData(
np.array([[1, 0], [3, 0], [5, 6]]))
# note that sc.pp.normalize_per_cell is also used in
# pl.highest_expr_genes with parameter counts_per_cell_after=100
adata_copy = sc.pp.normalize_per_cell(
adata, counts_per_cell_after=1, copy=True)
assert adata_copy.X.sum(axis=1).tolist() == [1., 1., 1.]
# now sparse
adata = AnnData(
np.array([[1, 0], [3, 0], [5, 6]]))
adata_sparse = AnnData(
sp.csr_matrix([[1, 0], [3, 0], [5, 6]]))
sc.pp.normalize_per_cell(adata)
sc.pp.normalize_per_cell(adata_sparse)
assert adata.X.sum(axis=1).tolist() == adata_sparse.X.sum(
axis=1).A1.tolist()
示例4: test_regress_out_ordinal
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def test_regress_out_ordinal():
from scipy.sparse import random
adata = AnnData(random(1000, 100, density=0.6, format='csr'))
adata.obs['percent_mito'] = np.random.rand(adata.X.shape[0])
adata.obs['n_counts'] = adata.X.sum(axis=1)
# results using only one processor
single = sc.pp.regress_out(
adata, keys=['n_counts', 'percent_mito'], n_jobs=1, copy=True)
assert adata.X.shape == single.X.shape
# results using 8 processors
multi = sc.pp.regress_out(
adata, keys=['n_counts', 'percent_mito'], n_jobs=8, copy=True)
np.testing.assert_array_equal(single.X, multi.X)
示例5: test_scvi
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def test_scvi():
n_samples = 4
n_genes = 7
batch1 = np.random.randint(1, 5, size=(n_samples, n_genes))
batch2 = np.random.randint(1, 5, size=(n_samples, n_genes))
ad1 = AnnData(batch1)
ad2 = AnnData(batch2)
adata = ad1.concatenate(ad2, batch_categories=['test1', 'test2'])
n_latent = 30
sce.pp.scvi(
adata,
use_cuda=False,
n_epochs=1,
n_latent=n_latent,
return_posterior=True,
batch_key='batch',
model_kwargs={'reconstruction_loss': 'nb'},
)
assert adata.obsm['X_scvi'].shape == (n_samples * 2, n_latent)
assert adata.obsm['X_scvi_denoised'].shape == adata.shape
assert adata.obsm['X_scvi_sample_rate'].shape == adata.shape
示例6: _check_array_function_arguments
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def _check_array_function_arguments(**kwargs):
"""Checks for invalid arguments when an array is passed.
Helper for functions that work on either AnnData objects or array-likes.
"""
# TODO: Figure out a better solution for documenting dispatched functions
invalid_args = [k for k, v in kwargs.items() if v is not None]
if len(invalid_args) > 0:
raise TypeError(
f"Arguments {invalid_args} are only valid if an AnnData object is passed."
)
# --------------------------------------------------------------------------------
# Graph stuff
# --------------------------------------------------------------------------------
示例7: paga_degrees
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def paga_degrees(adata: AnnData) -> List[int]:
"""Compute the degree of each node in the abstracted graph.
Parameters
----------
adata
Annotated data matrix.
Returns
-------
List of degrees for each node.
"""
import networkx as nx
g = nx.Graph(adata.uns['paga']['connectivities'])
degrees = [d for _, d in g.degree(weight='weight')]
return degrees
示例8: paga_expression_entropies
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def paga_expression_entropies(adata) -> List[float]:
"""Compute the median expression entropy for each node-group.
Parameters
----------
adata : AnnData
Annotated data matrix.
Returns
-------
Entropies of median expressions for each node.
"""
from scipy.stats import entropy
groups_order, groups_masks = _utils.select_groups(
adata, key=adata.uns['paga']['groups']
)
entropies = []
for mask in groups_masks:
X_mask = adata.X[mask].todense()
x_median = np.nanmedian(X_mask, axis=1,overwrite_input=True)
x_probs = (x_median - np.nanmin(x_median)) / (np.nanmax(x_median) - np.nanmin(x_median))
entropies.append(entropy(x_probs))
return entropies
示例9: burczynski06
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def burczynski06() -> AnnData:
"""\
Bulk data with conditions ulcerative colitis (UC) and Crohn's disease (CD).
The study assesses transcriptional profiles in peripheral blood mononuclear
cells from 42 healthy individuals, 59 CD patients, and 26 UC patients by
hybridization to microarrays interrogating more than 22,000 sequences.
Reference
---------
Burczynski et al., "Molecular classification of Crohn's disease and
ulcerative colitis patients using transcriptional profiles in peripheral
blood mononuclear cells"
J Mol Diagn 8, 51 (2006). PMID:16436634.
"""
filename = settings.datasetdir / 'burczynski06/GDS1615_full.soft.gz'
url = 'ftp://ftp.ncbi.nlm.nih.gov/geo/datasets/GDS1nnn/GDS1615/soft/GDS1615_full.soft.gz'
adata = read(filename, backup_url=url)
return adata
示例10: toggleswitch
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def toggleswitch() -> AnnData:
"""\
Simulated toggleswitch.
Data obtained simulating a simple toggleswitch [Gardner00]_
Simulate via :func:`~scanpy.tl.sim`.
Returns
-------
Annotated data matrix.
"""
filename = HERE / 'toggleswitch.txt'
adata = read(filename, first_column_names=True)
adata.uns['iroot'] = 0
return adata
示例11: pbmc68k_reduced
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def pbmc68k_reduced() -> AnnData:
"""\
Subsampled and processed 68k PBMCs.
10x PBMC 68k dataset from
https://support.10xgenomics.com/single-cell-gene-expression/datasets
The original PBMC 68k dataset was preprocessed using scanpy and was saved
keeping only 724 cells and 221 highly variable genes.
The saved file contains the annotation of cell types (key: `'bulk_labels'`),
UMAP coordinates, louvain clustering and gene rankings based on the
`bulk_labels`.
Returns
-------
Annotated data matrix.
"""
filename = HERE / '10x_pbmc68k_reduced.h5ad'
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning, module="anndata")
return read(filename)
示例12: _get_plot_data
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def _get_plot_data(data, ndim=None):
"""Get plot data out of an input object
Parameters
----------
data : array-like, `phate.PHATE` or `scanpy.AnnData`
ndim : int, optional (default: None)
Minimum number of dimensions
"""
out = data
if isinstance(data, PHATE):
out = data.transform()
else:
try:
if isinstance(data, anndata.AnnData):
try:
out = data.obsm["X_phate"]
except KeyError:
raise RuntimeError(
"data.obsm['X_phate'] not found. "
"Please run `sc.tl.phate(adata)` before plotting."
)
except NameError:
# anndata not installed
pass
if ndim is not None and out[0].shape[0] < ndim:
if isinstance(data, PHATE):
data.set_params(n_components=ndim)
out = data.transform()
else:
raise ValueError(
"Expected at least {}-dimensional data, got {}".format(
ndim, out[0].shape[0]
)
)
return out
示例13: test_simple
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def test_simple():
tree_data, tree_clusters = phate.tree.gen_dla(n_branch=3)
phate_operator = phate.PHATE(knn=15, t=100, verbose=False)
tree_phate = phate_operator.fit_transform(tree_data)
assert tree_phate.shape == (tree_data.shape[0], 2)
clusters = phate.cluster.kmeans(phate_operator, n_clusters='auto')
assert np.issubdtype(clusters.dtype, np.signedinteger)
assert len(np.unique(clusters)) >= 2
assert len(clusters.shape) == 1
assert len(clusters) == tree_data.shape[0]
clusters = phate.cluster.kmeans(phate_operator, n_clusters=3)
assert np.issubdtype(clusters.dtype, np.signedinteger)
assert len(np.unique(clusters)) == 3
assert len(clusters.shape) == 1
assert len(clusters) == tree_data.shape[0]
phate_operator.fit(phate_operator.graph)
G = graphtools.Graph(
phate_operator.graph.kernel,
precomputed="affinity",
use_pygsp=True,
verbose=False,
)
phate_operator.fit(G)
G = pygsp.graphs.Graph(G.W)
phate_operator.fit(G)
phate_operator.fit(anndata.AnnData(tree_data))
with assert_raises_message(TypeError, "Expected phate_op to be of type PHATE. Got 1"):
phate.cluster.kmeans(1)
示例14: integrate_scanpy
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def integrate_scanpy(adatas, **kwargs):
"""Integrate a list of `scanpy.api.AnnData`.
Parameters
----------
adatas : `list` of `scanpy.api.AnnData`
Data sets to integrate.
kwargs : `dict`
See documentation for the `integrate()` method for a full list of
parameters to use for batch correction.
Returns
-------
integrated
Returns a list of `np.ndarray` with integrated low-dimensional
embeddings.
"""
datasets_dimred, genes = integrate(
[adata.X for adata in adatas],
[adata.var_names.values for adata in adatas],
**kwargs
)
return datasets_dimred
# Visualize a scatter plot with cluster labels in the
# `cluster' variable.
示例15: test_init
# 需要导入模块: import anndata [as 别名]
# 或者: from anndata import AnnData [as 别名]
def test_init(self):
data = np.random.randint(1, 5, size=(3, 7))
ad = anndata.AnnData(data)
dataset = AnnDatasetFromAnnData(ad)
self.assertEqual(3, dataset.nb_cells)
self.assertEqual(7, dataset.nb_genes)