--- a +++ b/docs/utils.html @@ -0,0 +1,359 @@ +<!doctype html> +<html lang="en"> +<head> +<meta charset="utf-8"> +<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1"> +<meta name="generator" content="pdoc3 0.11.1"> +<title>VITAE.utils API documentation</title> +<meta name="description" content=""> +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/sanitize.min.css" integrity="sha512-y1dtMcuvtTMJc1yPgEqF0ZjQbhnc/bFhyvIyVNb9Zk5mIGtqVaAB1Ttl28su8AvFMOY0EwRbAe+HCLqj6W7/KA==" crossorigin> +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/typography.min.css" integrity="sha512-Y1DYSb995BAfxobCkKepB1BqJJTPrOp3zPL74AWFugHHmmdcvO+C48WLrUOlhGMc0QG7AE3f7gmvvcrmX2fDoA==" crossorigin> +<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css" crossorigin> +<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:1.5em;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:2em 0 .50em 0}h3{font-size:1.4em;margin:1.6em 0 .7em 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .2s ease-in-out}a:visited{color:#503}a:hover{color:#b62}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900;font-weight:bold}pre code{font-size:.8em;line-height:1.4em;padding:1em;display:block}code{background:#f3f3f3;font-family:"DejaVu Sans Mono",monospace;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em 1em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style> +<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul ul{padding-left:1em}.toc > ul > li{margin-top:.5em}}</style> +<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style> +<script type="text/x-mathjax-config">MathJax.Hub.Config({ tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ], processEscapes: true } });</script> +<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS_CHTML" integrity="sha256-kZafAc6mZvK3W3v1pHOcUix30OHQN6pU/NO2oFkqZVw=" crossorigin></script> +<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js" integrity="sha512-D9gUyxqja7hBtkWpPWGt9wfbfaMGVt9gnyCvYa+jojwwPHLCzUm5i8rpk7vD7wNee9bA35eYIjobYPaQuKS1MQ==" crossorigin></script> +<script>window.addEventListener('DOMContentLoaded', () => { +hljs.configure({languages: ['bash', 'css', 'diff', 'graphql', 'ini', 'javascript', 'json', 'plaintext', 'python', 'python-repl', 'rust', 'shell', 'sql', 'typescript', 'xml', 'yaml']}); +hljs.highlightAll(); +})</script> +</head> +<body> +<main> +<article id="content"> +<header> +<h1 class="title">Module <code>VITAE.utils</code></h1> +</header> +<section id="section-intro"> +</section> +<section> +</section> +<section> +</section> +<section> +<h2 class="section-title" id="header-functions">Functions</h2> +<dl> +<dt id="VITAE.utils.reset_random_seeds"><code class="name flex"> +<span>def <span class="ident">reset_random_seeds</span></span>(<span>seed)</span> +</code></dt> +<dd> +<div class="desc"></div> +</dd> +<dt id="VITAE.utils.get_embedding"><code class="name flex"> +<span>def <span class="ident">get_embedding</span></span>(<span>z, dimred='umap', **kwargs)</span> +</code></dt> +<dd> +<div class="desc"><p>Get low-dimensional embeddings for visualizations.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>z</code></strong> : <code>np.array</code></dt> +<dd><span><span class="MathJax_Preview">[N, d]</span><script type="math/tex">[N, d]</script></span> The latent variables.</dd> +<dt><strong><code>dimred</code></strong> : <code>str</code>, optional</dt> +<dd>'pca', 'tsne', or umap'.</dd> +<dt><strong><code>**kwargs</code></strong> : <code> </code></dt> +<dd>Extra key-value arguments for dimension reduction algorithms.</dd> +</dl> +<h2 id="returns">Returns:</h2> +<p>embed : np.array +<span><span class="MathJax_Preview">[N, 2]</span><script type="math/tex">[N, 2]</script></span> The latent variables after dimension reduction.</p></div> +</dd> +<dt id="VITAE.utils.get_igraph"><code class="name flex"> +<span>def <span class="ident">get_igraph</span></span>(<span>z, random_state=0)</span> +</code></dt> +<dd> +<div class="desc"><p>Get igraph for running Leidenalg clustering.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>z</code></strong> : <code>np.array</code></dt> +<dd><span><span class="MathJax_Preview">[N, d]</span><script type="math/tex">[N, d]</script></span> The latent variables.</dd> +<dt><strong><code>random_state</code></strong> : <code>int</code>, optional</dt> +<dd>The random state.</dd> +</dl> +<h2 id="returns">Returns:</h2> +<p>g : igraph +The igraph object of connectivities.</p></div> +</dd> +<dt id="VITAE.utils.leidenalg_igraph"><code class="name flex"> +<span>def <span class="ident">leidenalg_igraph</span></span>(<span>g, res, random_state=0)</span> +</code></dt> +<dd> +<div class="desc"><p>Leidenalg clustering on an igraph object.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>g</code></strong> : <code>igraph</code></dt> +<dd>The igraph object of connectivities.</dd> +<dt><strong><code>res</code></strong> : <code>float</code></dt> +<dd>The resolution parameter for Leidenalg clustering.</dd> +<dt><strong><code>random_state</code></strong> : <code>int</code>, optional</dt> +<dd>The random state.</dd> +</dl> +<h2 id="returns">Returns</h2> +<dl> +<dt><strong><code>labels</code></strong> : <code>np.array +</code></dt> +<dd><span><span class="MathJax_Preview">[N, ]</span><script type="math/tex">[N, ]</script></span> The clustered labels.</dd> +</dl></div> +</dd> +<dt id="VITAE.utils.plot_clusters"><code class="name flex"> +<span>def <span class="ident">plot_clusters</span></span>(<span>embed_z, labels, plot_labels=False, path=None)</span> +</code></dt> +<dd> +<div class="desc"><p>Plot the clustering results.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>embed_z</code></strong> : <code>np.array</code></dt> +<dd><span><span class="MathJax_Preview">[N, 2]</span><script type="math/tex">[N, 2]</script></span> The latent variables after dimension reduction.</dd> +<dt><strong><code>labels</code></strong> : <code>np.array +</code></dt> +<dd><span><span class="MathJax_Preview">[N, ]</span><script type="math/tex">[N, ]</script></span> The clustered labels.</dd> +<dt><strong><code>plot_labels</code></strong> : <code>boolean</code>, optional</dt> +<dd>Whether to plot text of labels or not.</dd> +<dt><strong><code>path</code></strong> : <code>str</code>, optional</dt> +<dd>The path to save the figure.</dd> +</dl></div> +</dd> +<dt id="VITAE.utils.plot_marker_gene"><code class="name flex"> +<span>def <span class="ident">plot_marker_gene</span></span>(<span>expression, gene_name: str, embed_z, path=None)</span> +</code></dt> +<dd> +<div class="desc"><p>Plot the marker gene.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>expression</code></strong> : <code>np.array</code></dt> +<dd><span><span class="MathJax_Preview">[N, ]</span><script type="math/tex">[N, ]</script></span> The expression of the marker gene.</dd> +<dt><strong><code>gene_name</code></strong> : <code>str</code></dt> +<dd>The name of the marker gene.</dd> +<dt><strong><code>embed_z</code></strong> : <code>np.array</code></dt> +<dd><span><span class="MathJax_Preview">[N, 2]</span><script type="math/tex">[N, 2]</script></span> The latent variables after dimension reduction.</dd> +<dt><strong><code>path</code></strong> : <code>str</code>, optional</dt> +<dd>The path to save the figure.</dd> +</dl></div> +</dd> +<dt id="VITAE.utils.plot_uncertainty"><code class="name flex"> +<span>def <span class="ident">plot_uncertainty</span></span>(<span>uncertainty, embed_z, path=None)</span> +</code></dt> +<dd> +<div class="desc"><p>Plot the uncertainty for all selected cells.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>uncertainty</code></strong> : <code>np.array</code></dt> +<dd><span><span class="MathJax_Preview">[N, ]</span><script type="math/tex">[N, ]</script></span> The uncertainty of the all cells.</dd> +<dt><strong><code>embed_z</code></strong> : <code>np.array</code></dt> +<dd><span><span class="MathJax_Preview">[N, 2]</span><script type="math/tex">[N, 2]</script></span> The latent variables after dimension reduction.</dd> +<dt><strong><code>path</code></strong> : <code>str</code>, optional</dt> +<dd>The path to save the figure.</dd> +</dl></div> +</dd> +<dt id="VITAE.utils.DE_test"><code class="name flex"> +<span>def <span class="ident">DE_test</span></span>(<span>Y, X, gene_names, i_test, alpha: float = 0.05)</span> +</code></dt> +<dd> +<div class="desc"><p>Differential gene expression test.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>Y</code></strong> : <code>numpy.array</code></dt> +<dd><span><span class="MathJax_Preview">n,</span><script type="math/tex">n,</script></span> the expression matrix.</dd> +<dt><strong><code>X</code></strong> : <code>numpy.array</code></dt> +<dd><span><span class="MathJax_Preview">n,1+1+s</span><script type="math/tex">n,1+1+s</script></span> the constant term, the pseudotime and the covariates.</dd> +<dt><strong><code>gene_names</code></strong> : <code>numpy.array</code></dt> +<dd><span><span class="MathJax_Preview">n,</span><script type="math/tex">n,</script></span> the names of all genes.</dd> +<dt><strong><code>i_test</code></strong> : <code>numpy.array</code></dt> +<dd>The indices of covariates to be tested.</dd> +<dt><strong><code>alpha</code></strong> : <code>float</code>, optional</dt> +<dd>The cutoff of p-values.</dd> +</dl> +<h2 id="returns">Returns</h2> +<dl> +<dt><strong><code>res_df</code></strong> : <code>pandas.DataFrame</code></dt> +<dd>The test results of expressed genes with two columns, +the estimated coefficients and the adjusted p-values.</dd> +</dl></div> +</dd> +<dt id="VITAE.utils.load_data"><code class="name flex"> +<span>def <span class="ident">load_data</span></span>(<span>path, file_name, return_dict=False)</span> +</code></dt> +<dd> +<div class="desc"><p>Load h5df data.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>path</code></strong> : <code>str</code></dt> +<dd>The path of the h5 files.</dd> +<dt><strong><code>file_name</code></strong> : <code>str</code></dt> +<dd>The dataset name.</dd> +<dt><strong><code>return_dict</code></strong> : <code>boolean</code>, optional</dt> +<dd>Whether to return the dict of the dataset or not.</dd> +</dl> +<h2 id="returns">Returns:</h2> +<p>data : dict +The dict containing count, grouping, etc. of the dataset. +dd : anndata.AnnData +The AnnData object of the dataset.</p></div> +</dd> +<dt id="VITAE.utils.compute_kernel"><code class="name flex"> +<span>def <span class="ident">compute_kernel</span></span>(<span>x, y, kernel='rbf', **kwargs)</span> +</code></dt> +<dd> +<div class="desc"><p>Computes RBF kernel between x and y.</p> +<h2 id="parameters">Parameters</h2> +<pre><code>x: Tensor + Tensor with shape [batch_size, z_dim] +y: Tensor + Tensor with shape [batch_size, z_dim] +</code></pre> +<h2 id="returns">Returns</h2> +<pre><code>The computed RBF kernel between x and y +</code></pre></div> +</dd> +<dt id="VITAE.utils.squared_distance"><code class="name flex"> +<span>def <span class="ident">squared_distance</span></span>(<span>x, y)</span> +</code></dt> +<dd> +<div class="desc"><p>Compute the pairwise euclidean distance.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>x</code></strong> : <code>Tensor</code></dt> +<dd>Tensor with shape [batch_size, z_dim]</dd> +<dt><strong><code>y</code></strong> : <code>Tensor</code></dt> +<dd>Tensor with shape [batch_size, z_dim]</dd> +</dl> +<h2 id="returns">Returns</h2> +<p>The pairwise euclidean distance between x and y.</p></div> +</dd> +<dt id="VITAE.utils.compute_mmd"><code class="name flex"> +<span>def <span class="ident">compute_mmd</span></span>(<span>x, y, kernel, **kwargs)</span> +</code></dt> +<dd> +<div class="desc"><p>Computes Maximum Mean Discrepancy(MMD) between x and y.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>x</code></strong> : <code>Tensor</code></dt> +<dd>Tensor with shape [batch_size, z_dim]</dd> +<dt><strong><code>y</code></strong> : <code>Tensor</code></dt> +<dd>Tensor with shape [batch_size, z_dim]</dd> +<dt><strong><code>kernel</code></strong> : <code>str</code></dt> +<dd>The kernel type used in MMD. It can be 'rbf', 'multi-scale-rbf' or 'raphy'.</dd> +<dt><strong><code>**kwargs</code></strong> : <code>dict</code></dt> +<dd>The parameters used in kernel function.</dd> +</dl> +<h2 id="returns">Returns</h2> +<dl> +<dt><code>The computed MMD between x and y</code></dt> +<dd> </dd> +</dl></div> +</dd> +<dt id="VITAE.utils.sample_z"><code class="name flex"> +<span>def <span class="ident">sample_z</span></span>(<span>args)</span> +</code></dt> +<dd> +<div class="desc"><p>Samples from standard Normal distribution with shape [size, z_dim] and +applies re-parametrization trick. It is actually sampling from latent +space distributions with N(mu, var) computed in <code>_encoder</code> function.</p> +<h2 id="parameters">Parameters</h2> +<dl> +<dt><strong><code>args</code></strong> : <code>list</code></dt> +<dd>List of [mu, log_var] computed in <code>_encoder</code> function.</dd> +</dl> +<h2 id="returns">Returns</h2> +<p>The computed Tensor of samples with shape [size, z_dim].</p></div> +</dd> +</dl> +</section> +<section> +<h2 class="section-title" id="header-classes">Classes</h2> +<dl> +<dt id="VITAE.utils.Early_Stopping"><code class="flex name class"> +<span>class <span class="ident">Early_Stopping</span></span> +<span>(</span><span>warmup=0, patience=10, tolerance=0.001, relative=False, is_minimize=True)</span> +</code></dt> +<dd> +<div class="desc"><p>The early-stopping monitor.</p></div> +<details class="source"> +<summary> +<span>Expand source code</span> +</summary> +<pre><code class="python">class Early_Stopping(): + ''' + The early-stopping monitor. + ''' + def __init__(self, warmup=0, patience=10, tolerance=1e-3, + relative=False, is_minimize=True): + self.warmup = warmup + self.patience = patience + self.tolerance = tolerance + self.is_minimize = is_minimize + self.relative = relative + + self.step = -1 + self.best_step = -1 + self.best_metric = np.inf + + if not self.is_minimize: + self.factor = -1.0 + else: + self.factor = 1.0 + + def __call__(self, metric): + self.step += 1 + + if self.step < self.warmup: + return False + elif (self.best_metric==np.inf) or \ + (self.relative and (self.best_metric-metric)/self.best_metric > self.tolerance) or \ + ((not self.relative) and self.factor*metric<self.factor*self.best_metric-self.tolerance): + self.best_metric = metric + self.best_step = self.step + return False + elif self.step - self.best_step>self.patience: + print('Best Epoch: %d. Best Metric: %f.'%(self.best_step, self.best_metric)) + return True + else: + return False</code></pre> +</details> +</dd> +</dl> +</section> +</article> +<nav id="sidebar"> +<div class="toc"> +<ul></ul> +</div> +<ul id="index"> +<li><h3>Super-module</h3> +<ul> +<li><code><a title="VITAE" href="index.html">VITAE</a></code></li> +</ul> +</li> +<li><h3><a href="#header-functions">Functions</a></h3> +<ul class="two-column"> +<li><code><a title="VITAE.utils.reset_random_seeds" href="#VITAE.utils.reset_random_seeds">reset_random_seeds</a></code></li> +<li><code><a title="VITAE.utils.get_embedding" href="#VITAE.utils.get_embedding">get_embedding</a></code></li> +<li><code><a title="VITAE.utils.get_igraph" href="#VITAE.utils.get_igraph">get_igraph</a></code></li> +<li><code><a title="VITAE.utils.leidenalg_igraph" href="#VITAE.utils.leidenalg_igraph">leidenalg_igraph</a></code></li> +<li><code><a title="VITAE.utils.plot_clusters" href="#VITAE.utils.plot_clusters">plot_clusters</a></code></li> +<li><code><a title="VITAE.utils.plot_marker_gene" href="#VITAE.utils.plot_marker_gene">plot_marker_gene</a></code></li> +<li><code><a title="VITAE.utils.plot_uncertainty" href="#VITAE.utils.plot_uncertainty">plot_uncertainty</a></code></li> +<li><code><a title="VITAE.utils.DE_test" href="#VITAE.utils.DE_test">DE_test</a></code></li> +<li><code><a title="VITAE.utils.load_data" href="#VITAE.utils.load_data">load_data</a></code></li> +<li><code><a title="VITAE.utils.compute_kernel" href="#VITAE.utils.compute_kernel">compute_kernel</a></code></li> +<li><code><a title="VITAE.utils.squared_distance" href="#VITAE.utils.squared_distance">squared_distance</a></code></li> +<li><code><a title="VITAE.utils.compute_mmd" href="#VITAE.utils.compute_mmd">compute_mmd</a></code></li> +<li><code><a title="VITAE.utils.sample_z" href="#VITAE.utils.sample_z">sample_z</a></code></li> +</ul> +</li> +<li><h3><a href="#header-classes">Classes</a></h3> +<ul> +<li> +<h4><code><a title="VITAE.utils.Early_Stopping" href="#VITAE.utils.Early_Stopping">Early_Stopping</a></code></h4> +</li> +</ul> +</li> +</ul> +</nav> +</main> +<footer id="footer"> +<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.11.1</a>.</p> +</footer> +</body> +</html>