Skip to content

Commit

Permalink
Add vignette giving examples of flywire-fafb mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferis committed Aug 8, 2020
1 parent b0c1ebc commit e14e3b4
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
141 changes: 141 additions & 0 deletions vignettes/articles/FAFB-FlyWire.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
title: "FAFB-FlyWire"
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
rgl::setupKnitr()
havelmr=require('elmr', quietly = TRUE)
options(nat.plotengine = 'plotly')
```
## Introduction

This articles gives a quick overview of mapping points between FAFB14 (as used
by the "walled garden" and [Virtual Fly Brain](https://fafb.catmaid.virtualflybrain.org/) and [FlyWire](https://flywire.ai).

```{r setup}
library(fafbseg)
```

### Basic point mapping
Some known points
```{r}
# identified location in FAFB14
p.fafb.nm <- cbind(477042, 284535, 90680)
p.fafb.raw <- p.fafb.nm/c(4,4,40)
# corresponding location in FlyWire
p.flywire.raw <- cbind(118865, 71338, 2267)
p.flywire.nm <- p.flywire.raw * c(4,4,40)
```

Compare displacements (in nm) for forward or inverse mapping
```{r}
# check displacement
flywire2fafb(p.flywire.nm)-p.fafb.nm
# check what happens when you apply the inverse
fafb2flywire(p.fafb.nm)-p.flywire.nm
```


A sample neuron. First map the points
```{r}
data("AV4b1", package='catmaid')
before=xyzmatrix(AV4b1)
after=fafb2flywire(before)
```

Then some stats and a quick histogram
```{r}
d=sqrt(rowSums((before-after)^2))
summary(d)
```

```{r}
hist(d, br=20, main='Displacement /µm')
```

```{r}
sample_points_in_surf <- function(x, n){
x=as.mesh3d(x)
bb=boundingbox(x)
mm=mapply(runif, min=bb[1,], max=bb[2,], n = n)
colnames(mm)=c("X","Y","Z")
data.frame(mm, inside=pointsinside(mm,x))
}
```

```{r, eval=havelmr, cache=T}
set.seed(42)
sxyz=sample_points_in_surf(FAFB14.surf,25000)
sxyz.in=subset(sxyz, inside)
sxyz.fw=fafb2flywire(xyzmatrix(sxyz.in))
deltas=sxyz.fw-sxyz.in
delta=rowSums(deltas[,c("X","Y")])
```

```{r, webgl=T, eval=havelmr}
jet.colors<-colorRampPalette(c('navy','cyan','yellow','red'))
nclear3d();
spheres3d(xyzmatrix(sxyz.in), col=jet.colors(10)[cut(delta, breaks = 10)], rad=2000)
```

### Round trip error
Let's send those points back again
```{r, cache=T}
sxyz.fafb2=flywire2fafb(sxyz.fw)
```

```{r}
deltas=sxyz.fafb2-xyzmatrix(sxyz.in)
delta=rowSums(deltas[,c("X","Y")])
hist(delta, main = "Round Trip Error", xlab='delta /nm')
```

## Mapping anything

To map complex objects, use `xform_brain()`

```{r, cache=T}
AV4b1.fw=xform_brain(AV4b1, sample='FAFB14', reference = 'FlyWire')
```

```{r}
# find the main branch point of a neuron, a good place to point to
mainbranch <- function(x, ...) {
if(is.neuronlist(x))
return(nlapply(x, mainbranch, ...))
sx=nat::simplify_neuron(x, ...)
xyzmatrix(sx)[sx$BranchPoints,]
}
```

```{r}
choose_segmentation("flywire")
open_fafb_ngl(mainbranch(AV4b1.fw), coords.only = TRUE)
```

![catmaid](catmaid-av4.png){width=400px}
![catmaid](flywire-av4.png){width=800px}

Load the flywire mesh

```{r, cache=TRUE, message=FALSE}
av4.fwm=read_cloudvolume_meshes('720575940618054533')
```


And plot with the CATMAID skeleton

```{r}
nclear3d()
plot3d(AV4b1.fw, col='red', lwd=2)
shade3d(av4.fwm[[1]], col='grey', alpha=.2)
```

It's obvious that both are only partially traced
Binary file added vignettes/articles/catmaid-av4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added vignettes/articles/flywire-av4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e14e3b4

Please sign in to comment.