Skip to content

Commit

Permalink
add slides
Browse files Browse the repository at this point in the history
  • Loading branch information
LouiseDck committed Sep 12, 2024
1 parent 3e8429b commit ba1c9cc
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 183 deletions.
148 changes: 72 additions & 76 deletions assets/slides.html

Large diffs are not rendered by default.

148 changes: 72 additions & 76 deletions slides/slides.html

Large diffs are not rendered by default.

97 changes: 66 additions & 31 deletions slides/slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ While interoperability is currently possible developers continue to improve the
1. Package-based interoperability
2. Best practices

# Package-based interoperability
## Package-based interoperability
or: the question of reimplementation.

- Consider the pros:
Expand All @@ -68,47 +68,55 @@ or: the question of reimplementation.
3. How will you keep it up to date?
4. How will you ensure parity?

# Package-based interoperability
## Package-based interoperability

Please learn both R & Python

# Best practices
## Best practices
1. Work with the standards
2. Work with matrices, arrays and dataframes
3. Provide vignettes on interoperability

# In-memory interoperability
![](../book/in_memory/images/imm_overview.png)

# Overview
## Overview

1. Advantages & disadvantages
2. Pitfalls when using Python & R
2. Rpy2
3. Reticulate

# in-memory interoperability advantages
- no need to write & read results
- useful when you need a limited amount of functions in another language
## in-memory interoperability advantages and disadvantages
- advantages
- no need to write & read results
- useful when you need a limited amount of functions in another language

# in-memory interoperability drawbacks
- not always access to all classes
- data duplication
- you need to manage the environments
- disadvantages
- not always access to all classes
- data duplication
- you need to manage the environments

# Pitfalls when using Python and R
**Column major vs row major matrices**
---

## Pitfalls when using Python and R

---

### Column major vs row major matrices
In R: every dense matrix is stored as column major

![](../book/in_memory/images/inmemorymatrix.png)

# Pitfalls when using Python and R
**Indexing**
---

### Indexing

![](../book/in_memory/images/indexing.png)

# Pitfalls when using Python and R
**dots and underscores**
---

### Dots and underscores

- mapping in rpy2

Expand All @@ -120,8 +128,9 @@ d = {'package.dependencies': 'package_dot_dependencies',
tools = importr('tools', robject_translations = d)
```

# Pitfalls when using Python and R
**Integers**
---

### Integers

```{r}
library(reticulate)
Expand All @@ -139,7 +148,9 @@ bi$list(bi$range(0L, 5L))
# [1] 0 1 2 3 4
```

# Rpy2: basics
---

## Rpy2
- Accessing R from Python
- `rpy2.rinterface`, the low-level interface
- `rpy2.robjects`, the high-level interface
Expand All @@ -155,7 +166,9 @@ rsum = robjects.r['sum']
rsum(vector)
```

# Rpy2: basics
---

### Rpy2: basics

```{python}
#| echo: true
Expand All @@ -166,7 +179,9 @@ mtx = robjects.r.matrix(robjects.IntVector(range(10)), nrow=5)
print(mtx)
```

# Rpy2: numpy
---

### Rpy2: numpy

```{python}
#| echo: true
Expand All @@ -182,7 +197,9 @@ with (default_converter + numpy2ri.converter).context():
print(mtx)
```

# Rpy2: pandas
---

### Rpy2: pandas
```{python}
#| echo: true
import pandas as pd
Expand All @@ -197,7 +214,9 @@ with (default_converter + pandas2ri.converter).context():
print(pd_df_r)
```

# Rpy2: sparse matrices
---

### Rpy2: sparse matrices

```{python}
#| echo: true
Expand All @@ -212,7 +231,9 @@ with (default_converter + scipy2ri.converter).context():
print(sp_r)
```

# Rpy2: anndata
---

### Rpy2: anndata

```python
import anndata as ad
Expand All @@ -227,7 +248,9 @@ with anndata2ri.converter.context():
ad2 = anndata2ri.rpy2py(sce)
```

# Rpy2: interactivity
---

### Rpy2: interactivity

```python
%load_ext rpy2.ipython # line magic that loads the rpy2 ipython extension.
Expand All @@ -240,11 +263,15 @@ with anndata2ri.converter.context():
# the rest of the cell will be run as R code
```

# Reticulate
---

## Reticulate

![](images/reticulate_table.png)

# Reticulate
---

### Reticulate: basics

```r
library(reticulate)
Expand All @@ -261,7 +288,9 @@ cat(bi$list(bi$reversed(example)))
# [1] 3 2 1
```

# Reticulate numpy
---

### Reticulate numpy

```r
np <- reticulate::import("numpy")
Expand All @@ -277,7 +306,9 @@ np$concatenate(tuple(a, b), axis=0L)
# [3,] 5 6
```

# Reticulate conversion
---

### Reticulate conversion

```r
np <- reticulate::import("numpy", convert = FALSE)
Expand Down Expand Up @@ -309,7 +340,9 @@ r_to_py(result_r)
# [5., 6.]])
```

# Reticulate scanpy
---

### Reticulate scanpy

```r
library(anndata)
Expand Down Expand Up @@ -338,6 +371,8 @@ adata
# obsp: 'connectivities', 'distances'
```

---

# Disk-based interoperability

Disk-based interoperability is a strategy for achieving interoperability between tools written in different programming languages by **storing intermediate results in standardized, language-agnostic file formats**.
Expand Down

0 comments on commit ba1c9cc

Please sign in to comment.