Skip to content

Commit

Permalink
Deploying to gh-pages from @ 3ca98db 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
berombau committed Sep 11, 2024
1 parent 1b38617 commit f5369bc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion book/in_memory/reticulate.html
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ <h1 data-number="6"><span class="header-section-number">6</span> Reticulate: bas
</div>
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>rd<span class="sc">$</span><span class="fu">choice</span>(example)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
<pre><code>[1] 2</code></pre>
</div>
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>bi<span class="sc">$</span><span class="fu">list</span>(bi<span class="sc">$</span><span class="fu">reversed</span>(example))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
Expand Down
23 changes: 12 additions & 11 deletions book/in_memory/rpy2.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,19 @@ <h2 data-number="4.1" class="anchored" data-anchor-id="rpy2-basic-functionality"
<div class="cell-output cell-output-stdout">
<pre><code>
0%| | 0.00/9.82M [00:00&lt;?, ?B/s]
0%| | 8.00k/9.82M [00:00&lt;02:09, 79.2kB/s]
0%| | 8.00k/9.82M [00:00&lt;02:10, 78.9kB/s]
0%| | 32.0k/9.82M [00:00&lt;01:01, 167kB/s]
1%| | 96.0k/9.82M [00:00&lt;00:27, 367kB/s]
2%|1 | 200k/9.82M [00:00&lt;00:16, 609kB/s]
4%|4 | 416k/9.82M [00:00&lt;00:08, 1.12MB/s]
8%|8 | 848k/9.82M [00:00&lt;00:04, 2.12MB/s]
17%|#7 | 1.67M/9.82M [00:00&lt;00:02, 4.08MB/s]
34%|###4 | 3.37M/9.82M [00:00&lt;00:00, 7.97MB/s]
45%|####4 | 4.38M/9.82M [00:00&lt;00:00, 8.11MB/s]
74%|#######3 | 7.24M/9.82M [00:01&lt;00:00, 13.8MB/s]
92%|#########2| 9.05M/9.82M [00:01&lt;00:00, 15.3MB/s]
100%|##########| 9.82M/9.82M [00:01&lt;00:00, 8.43MB/s]</code></pre>
1%| | 96.0k/9.82M [00:00&lt;00:27, 366kB/s]
2%|1 | 200k/9.82M [00:00&lt;00:16, 608kB/s]
4%|3 | 392k/9.82M [00:00&lt;00:09, 1.04MB/s]
8%|7 | 792k/9.82M [00:00&lt;00:04, 1.96MB/s]
16%|#5 | 1.56M/9.82M [00:00&lt;00:02, 3.80MB/s]
27%|##7 | 2.67M/9.82M [00:00&lt;00:01, 5.61MB/s]
41%|####1 | 4.05M/9.82M [00:00&lt;00:00, 8.06MB/s]
58%|#####8 | 5.73M/9.82M [00:01&lt;00:00, 10.8MB/s]
70%|######9 | 6.84M/9.82M [00:01&lt;00:00, 10.1MB/s]
94%|#########4| 9.26M/9.82M [00:01&lt;00:00, 14.3MB/s]
100%|##########| 9.82M/9.82M [00:01&lt;00:00, 7.70MB/s]</code></pre>
</div>
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> anndata2ri.converter.context():</span>
Expand Down
4 changes: 2 additions & 2 deletions search.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
"href": "book/in_memory/rpy2.html",
"title": "4  In-memory interoperability",
"section": "",
"text": "4.1 Rpy2: basic functionality\nRpy2 is a foreign function interface to R. It can be used in the following way:\nimport rpy2\nimport rpy2.robjects as robjects\n\n/home/runner/work/polygloty/polygloty/renv/python/virtualenvs/renv-python-3.12/lib/python3.12/site-packages/rpy2/rinterface_lib/embedded.py:276: UserWarning: R was initialized outside of rpy2 (R_NilValue != NULL). Trying to use it nevertheless.\n warnings.warn(msg)\nR was initialized outside of rpy2 (R_NilValue != NULL). Trying to use it nevertheless.\n\nvector = robjects.IntVector([1,2,3])\nrsum = robjects.r['sum']\n\nrsum(vector)\n\n\n IntVector with 1 elements.\n \n\n\n\n6\nLuckily, we’re not restricted to just calling R functions and creating R objects. The real power of this in-memory interoperability lies in the conversion of Python objects to R objects to call R functions on, and then to the conversion of the results back to Python objects.\nRpy2 requires specific conversion rules for different Python objects. It is straightforward to create R vectors from corresponding Python lists:\nstr_vector = robjects.StrVector(['abc', 'def', 'ghi'])\nflt_vector = robjects.FloatVector([0.3, 0.8, 0.7])\nint_vector = robjects.IntVector([1, 2, 3])\nmtx = robjects.r.matrix(robjects.IntVector(range(10)), nrow=5)\nHowever, for single cell biology, the objects that are most interesting to convert are (count) matrices, arrays and dataframes. In order to do this, you need to import the corresponding rpy2 modules and specify the conversion context.\nimport numpy as np\n\nfrom rpy2.robjects import numpy2ri\nfrom rpy2.robjects import default_converter\n\nrd_m = np.random.random((10, 7))\n\nwith (default_converter + numpy2ri.converter).context():\n mtx2 = robjects.r.matrix(rd_m, nrow = 10)\nimport pandas as pd\n\nfrom rpy2.robjects import pandas2ri\n\npd_df = pd.DataFrame({'int_values': [1,2,3],\n 'str_values': ['abc', 'def', 'ghi']})\n\nwith (default_converter + pandas2ri.converter).context():\n pd_df_r = robjects.DataFrame(pd_df)\nOne big limitation of rpy2 is the inability to convert sparse matrices: there is no built-in conversion module for scipy. The anndata2ri package provides, apart from functionality to convert SingleCellExperiment objects to an anndata objects, functions to convert sparse matrices.\nimport scipy as sp\n\nfrom anndata2ri import scipy2ri\n\nsparse_matrix = sp.sparse.csc_matrix(rd_m)\n\nwith (default_converter + scipy2ri.converter).context():\n sp_r = scipy2ri.py2rpy(sparse_matrix)\nWe will showcase how to use anndata2ri to convert an anndata object to a SingleCellExperiment object and vice versa as well:\nimport anndata as ad\nimport scanpy.datasets as scd\n\nimport anndata2ri\n\nadata_paul = scd.paul15()\n\n\n 0%| | 0.00/9.82M [00:00&lt;?, ?B/s]\n 0%| | 8.00k/9.82M [00:00&lt;02:09, 79.2kB/s]\n 0%| | 32.0k/9.82M [00:00&lt;01:01, 167kB/s] \n 1%| | 96.0k/9.82M [00:00&lt;00:27, 367kB/s]\n 2%|1 | 200k/9.82M [00:00&lt;00:16, 609kB/s] \n 4%|4 | 416k/9.82M [00:00&lt;00:08, 1.12MB/s]\n 8%|8 | 848k/9.82M [00:00&lt;00:04, 2.12MB/s]\n 17%|#7 | 1.67M/9.82M [00:00&lt;00:02, 4.08MB/s]\n 34%|###4 | 3.37M/9.82M [00:00&lt;00:00, 7.97MB/s]\n 45%|####4 | 4.38M/9.82M [00:00&lt;00:00, 8.11MB/s]\n 74%|#######3 | 7.24M/9.82M [00:01&lt;00:00, 13.8MB/s]\n 92%|#########2| 9.05M/9.82M [00:01&lt;00:00, 15.3MB/s]\n100%|##########| 9.82M/9.82M [00:01&lt;00:00, 8.43MB/s]\n\n\nwith anndata2ri.converter.context():\n sce = anndata2ri.py2rpy(adata_paul)\n ad2 = anndata2ri.rpy2py(sce)",
"text": "4.1 Rpy2: basic functionality\nRpy2 is a foreign function interface to R. It can be used in the following way:\nimport rpy2\nimport rpy2.robjects as robjects\n\n/home/runner/work/polygloty/polygloty/renv/python/virtualenvs/renv-python-3.12/lib/python3.12/site-packages/rpy2/rinterface_lib/embedded.py:276: UserWarning: R was initialized outside of rpy2 (R_NilValue != NULL). Trying to use it nevertheless.\n warnings.warn(msg)\nR was initialized outside of rpy2 (R_NilValue != NULL). Trying to use it nevertheless.\n\nvector = robjects.IntVector([1,2,3])\nrsum = robjects.r['sum']\n\nrsum(vector)\n\n\n IntVector with 1 elements.\n \n\n\n\n6\nLuckily, we’re not restricted to just calling R functions and creating R objects. The real power of this in-memory interoperability lies in the conversion of Python objects to R objects to call R functions on, and then to the conversion of the results back to Python objects.\nRpy2 requires specific conversion rules for different Python objects. It is straightforward to create R vectors from corresponding Python lists:\nstr_vector = robjects.StrVector(['abc', 'def', 'ghi'])\nflt_vector = robjects.FloatVector([0.3, 0.8, 0.7])\nint_vector = robjects.IntVector([1, 2, 3])\nmtx = robjects.r.matrix(robjects.IntVector(range(10)), nrow=5)\nHowever, for single cell biology, the objects that are most interesting to convert are (count) matrices, arrays and dataframes. In order to do this, you need to import the corresponding rpy2 modules and specify the conversion context.\nimport numpy as np\n\nfrom rpy2.robjects import numpy2ri\nfrom rpy2.robjects import default_converter\n\nrd_m = np.random.random((10, 7))\n\nwith (default_converter + numpy2ri.converter).context():\n mtx2 = robjects.r.matrix(rd_m, nrow = 10)\nimport pandas as pd\n\nfrom rpy2.robjects import pandas2ri\n\npd_df = pd.DataFrame({'int_values': [1,2,3],\n 'str_values': ['abc', 'def', 'ghi']})\n\nwith (default_converter + pandas2ri.converter).context():\n pd_df_r = robjects.DataFrame(pd_df)\nOne big limitation of rpy2 is the inability to convert sparse matrices: there is no built-in conversion module for scipy. The anndata2ri package provides, apart from functionality to convert SingleCellExperiment objects to an anndata objects, functions to convert sparse matrices.\nimport scipy as sp\n\nfrom anndata2ri import scipy2ri\n\nsparse_matrix = sp.sparse.csc_matrix(rd_m)\n\nwith (default_converter + scipy2ri.converter).context():\n sp_r = scipy2ri.py2rpy(sparse_matrix)\nWe will showcase how to use anndata2ri to convert an anndata object to a SingleCellExperiment object and vice versa as well:\nimport anndata as ad\nimport scanpy.datasets as scd\n\nimport anndata2ri\n\nadata_paul = scd.paul15()\n\n\n 0%| | 0.00/9.82M [00:00&lt;?, ?B/s]\n 0%| | 8.00k/9.82M [00:00&lt;02:10, 78.9kB/s]\n 0%| | 32.0k/9.82M [00:00&lt;01:01, 167kB/s] \n 1%| | 96.0k/9.82M [00:00&lt;00:27, 366kB/s]\n 2%|1 | 200k/9.82M [00:00&lt;00:16, 608kB/s] \n 4%|3 | 392k/9.82M [00:00&lt;00:09, 1.04MB/s]\n 8%|7 | 792k/9.82M [00:00&lt;00:04, 1.96MB/s]\n 16%|#5 | 1.56M/9.82M [00:00&lt;00:02, 3.80MB/s]\n 27%|##7 | 2.67M/9.82M [00:00&lt;00:01, 5.61MB/s]\n 41%|####1 | 4.05M/9.82M [00:00&lt;00:00, 8.06MB/s]\n 58%|#####8 | 5.73M/9.82M [00:01&lt;00:00, 10.8MB/s]\n 70%|######9 | 6.84M/9.82M [00:01&lt;00:00, 10.1MB/s]\n 94%|#########4| 9.26M/9.82M [00:01&lt;00:00, 14.3MB/s]\n100%|##########| 9.82M/9.82M [00:01&lt;00:00, 7.70MB/s]\n\n\nwith anndata2ri.converter.context():\n sce = anndata2ri.py2rpy(adata_paul)\n ad2 = anndata2ri.rpy2py(sce)",
"crumbs": [
"In-memory interoperability",
"<span class='chapter-number'>4</span>  <span class='chapter-title'>rpy2</span>"
Expand All @@ -181,7 +181,7 @@
"href": "book/in_memory/reticulate.html",
"title": "5  Reticulate",
"section": "",
"text": "Reticulate is a foreign function interface in R to Python.\n\n6 Reticulate: basic functionality\nData types are automatically converted from Python to R and vice versa. A useful table of automatic conversions can be found here.\nYou can easily import python modules, and call the functions in the following way:\n\nlibrary(reticulate)\n\nbi &lt;- reticulate::import_builtins()\nrd &lt;- reticulate::import(\"random\")\n\nexample &lt;- c(1,2,3)\nbi$max(example)\n\n[1] 3\n\nrd$choice(example)\n\n[1] 1\n\nbi$list(bi$reversed(example))\n\n[1] 3 2 1\n\n\nNumpy is also easily used:\n\nnp &lt;- reticulate::import(\"numpy\")\n\na &lt;- np$asarray(tuple(list(1,2), list(3, 4)))\nb &lt;- np$asarray(list(5,6))\nb &lt;- np$reshape(b, newshape = tuple(1L,2L))\n\nnp$concatenate(tuple(a, b), axis=0L)\n\n [,1] [,2]\n[1,] 1 2\n[2,] 3 4\n[3,] 5 6\n\n\nIf you want more finegrained control over conversion, you can specify in the import statement that you do not want results of functions of that package to be converted to R data types.\n\nnp &lt;- reticulate::import(\"numpy\", convert = FALSE)\n\na &lt;- np$asarray(tuple(list(1,2), list(3, 4)))\nb &lt;- np$asarray(list(5,6))\nb &lt;- np$reshape(b, newshape = tuple(1L,2L))\n\nnp$concatenate(tuple(a, b), axis=0L)\n\narray([[1., 2.],\n [3., 4.],\n [5., 6.]])\n\n\nYou can explicitly convert data types:\n\nresult &lt;- np$concatenate(tuple(a, b), axis=0L)\n\npy_to_r(result)\n\n [,1] [,2]\n[1,] 1 2\n[2,] 3 4\n[3,] 5 6\n\nresult_r &lt;- py_to_r(result)\nr_to_py(result_r)\n\narray([[1., 2.],\n [3., 4.],\n [5., 6.]])\n\n\n\n\n7 Interactive sessions\nOne of the most useful ways to take advantage of in-memory interoperability is to use it in interactive sessions, where you’re exploring the data and want to try out some functions non-native to your language of choice.\nJupyter notebooks (and some other notebooks) make this possible from the Python side: using IPython line and cell magic and rpy2, you can easily run an R jupyter cell in your notebooks.\n\n%load_ext rpy2.ipython # line magic that loads the rpy2 ipython extension.\n # this extension allows the use of the following cell magic\n\n%%R -i input -o output # this line allows to specify inputs \n # (which will be converted to R objects) and outputs \n # (which will be converted back to Python objects) \n # this line is put at the start of a cell\n # the rest of the cell will be run as R code\n\n\n\n8 Usecase\nWe will not showcase the usefulness of reticulate by using the DE analysis: it would involve loading in pandas to create a Python dataframe, adding rownames and columnnames and then grouping them, but that is easier to do just in R.\nA more interesting thing you can do using reticulate is interacting with anndata-based Python packages, such as scanpy!\n\nlibrary(anndata)\nlibrary(reticulate)\nsc &lt;- import(\"scanpy\")\n\nadata_path &lt;- \"../usecase/data/sc_counts_subset.h5ad\"\nadata &lt;- anndata::read_h5ad(adata_path)\n\nWe can preprocess the data:\n\nsc$pp$filter_cells(adata, min_genes = 200)\nsc$pp$filter_genes(adata, min_cells = 3)\n\n\nsc$pp$pca(adata)\nsc$pp$neighbors(adata)\nsc$tl$umap(adata)\n\nadata\n\nAnnData object with n_obs × n_vars = 32727 × 20542\n obs: 'dose_uM', 'timepoint_hr', 'well', 'row', 'col', 'plate_name', 'cell_id', 'cell_type', 'split', 'donor_id', 'sm_name', 'control', 'SMILES', 'sm_lincs_id', 'library_id', 'leiden_res1', 'group', 'cell_type_orig', 'plate_well_celltype_reannotated', 'cell_count_by_well_celltype', 'cell_count_by_plate_well', 'n_genes'\n var: 'highly_variable', 'means', 'dispersions', 'dispersions_norm', 'n_cells'\n uns: 'cell_type_colors', 'celltypist_celltype_colors', 'donor_id_colors', 'hvg', 'leiden_res1_colors', 'log1p', 'neighbors', 'over_clustering', 'rank_genes_groups', 'pca', 'umap'\n obsm: 'HTO_clr', 'X_pca', 'X_umap', 'protein_counts'\n varm: 'PCs'\n obsp: 'connectivities', 'distances'\n\n\nWe can’t easily show the result of the plot in this Quarto notebook, but we can save it and show it:\n\npath &lt;- \"umap.png\"\nsc$pl$umap(adata, color=\"leiden_res1\", save=path)\n\n)",
"text": "Reticulate is a foreign function interface in R to Python.\n\n6 Reticulate: basic functionality\nData types are automatically converted from Python to R and vice versa. A useful table of automatic conversions can be found here.\nYou can easily import python modules, and call the functions in the following way:\n\nlibrary(reticulate)\n\nbi &lt;- reticulate::import_builtins()\nrd &lt;- reticulate::import(\"random\")\n\nexample &lt;- c(1,2,3)\nbi$max(example)\n\n[1] 3\n\nrd$choice(example)\n\n[1] 2\n\nbi$list(bi$reversed(example))\n\n[1] 3 2 1\n\n\nNumpy is also easily used:\n\nnp &lt;- reticulate::import(\"numpy\")\n\na &lt;- np$asarray(tuple(list(1,2), list(3, 4)))\nb &lt;- np$asarray(list(5,6))\nb &lt;- np$reshape(b, newshape = tuple(1L,2L))\n\nnp$concatenate(tuple(a, b), axis=0L)\n\n [,1] [,2]\n[1,] 1 2\n[2,] 3 4\n[3,] 5 6\n\n\nIf you want more finegrained control over conversion, you can specify in the import statement that you do not want results of functions of that package to be converted to R data types.\n\nnp &lt;- reticulate::import(\"numpy\", convert = FALSE)\n\na &lt;- np$asarray(tuple(list(1,2), list(3, 4)))\nb &lt;- np$asarray(list(5,6))\nb &lt;- np$reshape(b, newshape = tuple(1L,2L))\n\nnp$concatenate(tuple(a, b), axis=0L)\n\narray([[1., 2.],\n [3., 4.],\n [5., 6.]])\n\n\nYou can explicitly convert data types:\n\nresult &lt;- np$concatenate(tuple(a, b), axis=0L)\n\npy_to_r(result)\n\n [,1] [,2]\n[1,] 1 2\n[2,] 3 4\n[3,] 5 6\n\nresult_r &lt;- py_to_r(result)\nr_to_py(result_r)\n\narray([[1., 2.],\n [3., 4.],\n [5., 6.]])\n\n\n\n\n7 Interactive sessions\nOne of the most useful ways to take advantage of in-memory interoperability is to use it in interactive sessions, where you’re exploring the data and want to try out some functions non-native to your language of choice.\nJupyter notebooks (and some other notebooks) make this possible from the Python side: using IPython line and cell magic and rpy2, you can easily run an R jupyter cell in your notebooks.\n\n%load_ext rpy2.ipython # line magic that loads the rpy2 ipython extension.\n # this extension allows the use of the following cell magic\n\n%%R -i input -o output # this line allows to specify inputs \n # (which will be converted to R objects) and outputs \n # (which will be converted back to Python objects) \n # this line is put at the start of a cell\n # the rest of the cell will be run as R code\n\n\n\n8 Usecase\nWe will not showcase the usefulness of reticulate by using the DE analysis: it would involve loading in pandas to create a Python dataframe, adding rownames and columnnames and then grouping them, but that is easier to do just in R.\nA more interesting thing you can do using reticulate is interacting with anndata-based Python packages, such as scanpy!\n\nlibrary(anndata)\nlibrary(reticulate)\nsc &lt;- import(\"scanpy\")\n\nadata_path &lt;- \"../usecase/data/sc_counts_subset.h5ad\"\nadata &lt;- anndata::read_h5ad(adata_path)\n\nWe can preprocess the data:\n\nsc$pp$filter_cells(adata, min_genes = 200)\nsc$pp$filter_genes(adata, min_cells = 3)\n\n\nsc$pp$pca(adata)\nsc$pp$neighbors(adata)\nsc$tl$umap(adata)\n\nadata\n\nAnnData object with n_obs × n_vars = 32727 × 20542\n obs: 'dose_uM', 'timepoint_hr', 'well', 'row', 'col', 'plate_name', 'cell_id', 'cell_type', 'split', 'donor_id', 'sm_name', 'control', 'SMILES', 'sm_lincs_id', 'library_id', 'leiden_res1', 'group', 'cell_type_orig', 'plate_well_celltype_reannotated', 'cell_count_by_well_celltype', 'cell_count_by_plate_well', 'n_genes'\n var: 'highly_variable', 'means', 'dispersions', 'dispersions_norm', 'n_cells'\n uns: 'cell_type_colors', 'celltypist_celltype_colors', 'donor_id_colors', 'hvg', 'leiden_res1_colors', 'log1p', 'neighbors', 'over_clustering', 'rank_genes_groups', 'pca', 'umap'\n obsm: 'HTO_clr', 'X_pca', 'X_umap', 'protein_counts'\n varm: 'PCs'\n obsp: 'connectivities', 'distances'\n\n\nWe can’t easily show the result of the plot in this Quarto notebook, but we can save it and show it:\n\npath &lt;- \"umap.png\"\nsc$pl$umap(adata, color=\"leiden_res1\", save=path)\n\n)",
"crumbs": [
"In-memory interoperability",
"<span class='chapter-number'>5</span>  <span class='chapter-title'>reticulate</span>"
Expand Down
Loading

0 comments on commit f5369bc

Please sign in to comment.