-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vignette giving examples of flywire-fafb mapping
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.