Title: | NeuroAnatomy Toolbox ('nat') Extension for Handling Template Brains |
---|---|
Description: | Extends package 'nat' (NeuroAnatomy Toolbox) by providing objects and functions for handling template brains. |
Authors: | Gregory Jefferis [aut, cre] , James Manton [aut] |
Maintainer: | Gregory Jefferis <[email protected]> |
License: | GPL-3 |
Version: | 1.2 |
Built: | 2024-11-09 03:38:04 UTC |
Source: | https://github.com/natverse/nat.templatebrains |
add_reg_folders
sets
options('nat.templatebrains.regdirs') appropriately so that registrations
can be found by e.g. xform_brain
.
extra_reg_folders
lists extra registration folders
present in standard location
add_reg_folders(dir = extra_reg_folders(), first = TRUE) extra_reg_folders(full.names = TRUE)
add_reg_folders(dir = extra_reg_folders(), first = TRUE) extra_reg_folders(full.names = TRUE)
dir |
Path to one or more folders containing registrations. Default value will scan for registration folders in a standard location. (Please see Details and File layout sections) |
first |
Whether the new folder should be added to the start (default) or end of the search list. |
full.names |
Whether to list full path to registration folders |
When dir
is unset then it will default to the value of
extra_reg_folders()
i.e. any folders / cloned repositories in the
standard location
This function is principally called for its side effect. A path to the location on disk containing added registrations is returned invisibly.
You must pass a folder containing one or more registrations, not the registration folder itself. So if you have this situation on disk
myregistrations/
myregistrations/reg1.list
myregistrations/reg2.list
you should write add_reg_folders("/path/to/myregistrations")
## Not run: add_reg_folders("myextraregistrations") ## End(Not run) # adding a non-existent folder will generate an error tools::assertError(add_reg_folders(tempfile()))
## Not run: add_reg_folders("myextraregistrations") ## End(Not run) # adding a non-existent folder will generate an error tools::assertError(add_reg_folders(tempfile()))
By specifying either reference, sample
or
mirror
arguments, you can add a bridging or mirroring registration,
respectively, to the list of those in use for xform_brain
and
mirror_brain
.
add_reglist( x, reference = NULL, sample = NULL, mirror = NULL, temp = TRUE, ... )
add_reglist( x, reference = NULL, sample = NULL, mirror = NULL, temp = TRUE, ... )
x |
A single |
reference , sample
|
The reference and sample brains (in |
mirror |
The reference brain (in |
temp |
Whether to store the on disk representation in a session-specific
temporary folder (that will be removed when R closes). Defaults to
|
... |
Additional arguments passed to |
This function is called for its side effect and has no return value.
add_reg_folders
## Not run: library(nat.flybrains) # mirroring registration for a specific template brain object add_reglist(mirroring, mirror=JFRC2013) # equivalent but withhout needing to construct the template add_reglist(mirroring, mirror="JFRC2013") # add a bridging registration between two brains add_reglist(bridging, reference=JFRC2, sample=JFRC2013) ## End(Not run)
## Not run: library(nat.flybrains) # mirroring registration for a specific template brain object add_reglist(mirroring, mirror=JFRC2013) # equivalent but withhout needing to construct the template add_reglist(mirroring, mirror="JFRC2013") # add a bridging registration between two brains add_reglist(bridging, reference=JFRC2, sample=JFRC2013) ## End(Not run)
all_templatebrains
returns a data.frame detailing all
templatebrain
objects on the search path (including those inside
packages).
all_templatebrains(cached = TRUE, remove.duplicates = FALSE) guess_templatebrain( x, rval = c("templatebrain", "name"), cached = TRUE, mustWork = FALSE )
all_templatebrains(cached = TRUE, remove.duplicates = FALSE) guess_templatebrain( x, rval = c("templatebrain", "name"), cached = TRUE, mustWork = FALSE )
cached |
When |
remove.duplicates |
Whether to remove duplicate template brains (as determined by md5 hash) from the result list |
x |
A |
rval |
Whether to return the |
mustWork |
Whether to insist that exactly one template brain is found |
For all_templatebrains
, a data.frame
containing the
following columns:
object The name of the templatebrain
object
pos An integer specifying the environment
package Character vector naming the environment
md5 md5 hash of the templatebrain
object
name
W,H,D Width, height and depth of image stack (pixels)
guess_templatebrain
returns a templatebrain
object when rval='templatebrain'
or a character vector when
rval='name'
.
## Not run: all_templatebrains() guess_templatebrain(im3d(dims=c(30,40,50))) # or guess_templatebrain(c(30,40,50)) guess_templatebrain('path/to/my/image.nrrd') if(require('nat.flybrains')){ guess_templatebrain(im3d(dims=c(1024,512,218)), rval = 'name') # get the matching template brain tb=guess_templatebrain(im3d(dims=c(1024,512,218))) # get its voxel dimensions voxdims(tb) tb=guess_templatebrain(c(1024,512)) tb } ## End(Not run)
## Not run: all_templatebrains() guess_templatebrain(im3d(dims=c(30,40,50))) # or guess_templatebrain(c(30,40,50)) guess_templatebrain('path/to/my/image.nrrd') if(require('nat.flybrains')){ guess_templatebrain(im3d(dims=c(1024,512,218)), rval = 'name') # get the matching template brain tb=guess_templatebrain(im3d(dims=c(1024,512,218))) # get its voxel dimensions voxdims(tb) tb=guess_templatebrain(c(1024,512)) tb } ## End(Not run)
Make data.frame with details of all registrations
allreg_dataframe(regdirs = getOption("nat.templatebrains.regdirs"))
allreg_dataframe(regdirs = getOption("nat.templatebrains.regdirs"))
regdirs |
Character vector of directories to search for registrations (see details) |
by default regdirs
is set to
getOption('nat.templatebrains.regdirs')
data.frame with one row for each observed registration and columns
path
name
dup
bridge
reference
sample
If there are no registrations, there will be a data.frame with 0 rows and these columns.
## Not run: allreg_dataframe() ## End(Not run)
## Not run: allreg_dataframe() ## End(Not run)
Use image file or other object to initialise template brain
as.templatebrain(x, ...) ## S3 method for class 'character' as.templatebrain(x, ...) ## S3 method for class 'im3d' as.templatebrain(x, regName = NULL, name = regName, ...) ## S3 method for class 'templatebrain' as.templatebrain(x, ...)
as.templatebrain(x, ...) ## S3 method for class 'character' as.templatebrain(x, ...) ## S3 method for class 'im3d' as.templatebrain(x, regName = NULL, name = regName, ...) ## S3 method for class 'templatebrain' as.templatebrain(x, ...)
x |
object used to construct the templatebrain, either a character
vector with the path to a file or an |
... |
additional named arguments passed to methods and then on to
|
name , regName
|
name and short name of the template brain. Will use the filename (minus final extension) by default for both fields. |
as.templatebrain
can extract the key fields defining an
template space from an image file. This is generally a much more convenient
approach to defining a templatebrain
object than specifying all
fields by hand.
A list with class templatebrain
# Make templatebrain object using image info from the template brain NRRD file nhdr=system.file('images','FCWB.nhdr', package='nat.templatebrains') as.templatebrain(nhdr, name = "FlyCircuit Whole Brain")
# Make templatebrain object using image info from the template brain NRRD file nhdr=system.file('images','FCWB.nhdr', package='nat.templatebrains') as.templatebrain(nhdr, name = "FlyCircuit Whole Brain")
These functions are designed for expert use. In general it is
recommended to use xform_brain
.
bridging_graph
creates an igraph::graph representing all known
template brains (vertices) and the bridging registrations connecting them
(edges).
shortest_bridging_seq
finds the shortest bridging
sequence on a graph of all available bridging registrations, subject to
constraints defined by graph connectivity and the reciprocal
parameter
.
bridging_graph( regdirs = getOption("nat.templatebrains.regdirs"), reciprocal = NA ) shortest_bridging_seq( sample, reference, via = NULL, checkboth = TRUE, imagedata = FALSE, reciprocal = NA, ... )
bridging_graph( regdirs = getOption("nat.templatebrains.regdirs"), reciprocal = NA ) shortest_bridging_seq( sample, reference, via = NULL, checkboth = TRUE, imagedata = FALSE, reciprocal = NA, ... )
regdirs |
Character vector of directories to search for registrations (see details) |
reciprocal |
Sets the weight of reciprocal edges in the graph (and thereby whether inverse registrations will be considered). |
sample |
Source template brain (e.g. IS2) that data is currently in.
Specified either as character vector or a |
reference |
Target template brain (e.g. IS2) that data should be transformed into. |
via |
(optional) intermediate template brain that the registration sequence must pass through. |
checkboth |
When |
imagedata |
Whether |
... |
additional arguments passed on to |
When reciprocal != NA
we create a graph where each forward
transformation is matched by a corresponding inverse transformation with
the specified edge weight. The edge weight for forward transforms will
always be 1.0.
By default regdirs
is set to getOption('nat.templatebrains.regdirs')
## Not run: plot(bridging_graph(), vertex.size=25, edge.arrow.size=0.5) # with reciprocal edges plot(bridging_graph(reciprocal=3), vertex.size=25) ## End(Not run) ## Not run: shortest_bridging_seq(FCWB, IS2) # or shortest_bridging_seq('FCWB', 'IS2') shortest_bridging_seq(sample='FCWB', reference='IS2', via="JFRC2") ## End(Not run)
## Not run: plot(bridging_graph(), vertex.size=25, edge.arrow.size=0.5) # with reciprocal edges plot(bridging_graph(reciprocal=3), vertex.size=25) ## End(Not run) ## Not run: shortest_bridging_seq(FCWB, IS2) # or shortest_bridging_seq('FCWB', 'IS2') shortest_bridging_seq(sample='FCWB', reference='IS2', via="JFRC2") ## End(Not run)
This function is primarily intended for developer use (it is
used inside xform_brain
) but may be useful for end users.
bridging_sequence( sample, reference, via = NULL, imagedata = FALSE, checkboth = !imagedata, mustWork = FALSE )
bridging_sequence( sample, reference, via = NULL, imagedata = FALSE, checkboth = !imagedata, mustWork = FALSE )
sample |
Source template brain (e.g. IS2) that data is currently in.
Specified either as character vector or a |
reference |
Target template brain (e.g. IS2) that data should be transformed into. |
via |
(optional) intermediate template brain that the registration sequence must pass through. |
imagedata |
Whether |
checkboth |
whether to look for registrations in both
directions. The default ( |
mustWork |
whether to error out if appropriate registrations are not found. |
When checkboth=FALSE
, only registrations that can be directly
used to map image data from sample to reference are returned. When working
with 3D points, use checkboth=TRUE
. Note that all possible
directories will first be scanned for registrations in the preferred
direction and then rescanned for the opposite direction if nothing is
found.
When mapping points from JFRC2 -> IS2 -> FCWB
(i.e. sample=JFRC2, via=IS2, ref=FCWB) the command line passed to CMTK's streamxform
should look like:
streamxform -- JFRC2_IS2.list --inverse FCWB_IS2.list
However when mapping image data
the command line for CMTK's reformatx should look like:
reformatx -o out.nrrd --floating JFRC2.nrrd FCWB.nrrd FCWB_IS2.list --inverse JFRC2_IS2.list
bridging_sequence
produces output like
list(JFRC2 = structure(
"/GD/dev/R/nat.flybrains/inst/extdata/bridgingregistrations/JFRC2_IS2.list",
swap = TRUE),
IS2 = "/GD/dev/R/nat.flybrains/inst/extdata/bridgingregistrations/FCWB_IS2.list")
in these circumstances, which xformpoints.cmtkreg turns into "– JFRC2_IS2.list –inverse FCWB_IS2.list".
## Not run: bridging_sequence(sample=JFRC2, ref=FCWB, checkboth = TRUE) bridging_sequence(sample=JFRC2, via=IS2, ref=FCWB, checkboth = TRUE) ## End(Not run)
## Not run: bridging_sequence(sample=JFRC2, ref=FCWB, checkboth = TRUE) bridging_sequence(sample=JFRC2, via=IS2, ref=FCWB, checkboth = TRUE) ## End(Not run)
Display an image slice in 3D
display_slice(brain, slice, ...)
display_slice(brain, slice, ...)
brain |
template brain (e.g. IS2) of the slice. |
slice |
Path to PNG image containing slice to display. |
... |
extra arguments to pass to |
This function is principally called for its side effect of adding to the plot. A vector of rgl shape IDs is returned invisibly.
Note that these extra registrations will be downloaded to a standard location
on your hard drive that will be used for one session to the next. See
examples and local_reg_dir_for_url
.
download_reg_repo(url, localdir = NULL, ...)
download_reg_repo(url, localdir = NULL, ...)
url |
Location of one or more remote git repositories. Can accept partial github specifications of the form "<user>/<repo>". |
localdir |
Full path to local checkout location of git repository. When
|
... |
additional arguments passed to |
This function is principally called for its side effect. A path to the location on disk containing added registrations is returned invisibly.
add_reg_folders
, local_reg_dir_for_url
,
git2r::clone
## find the root location of all registration directories local_reg_dir_for_url() ## Not run: ## Add the two main jefferislab bridging and mirroring registration # collections for Drosophila brains from github.com. download_reg_repo("jefferislab/BridgingRegistrations") download_reg_repo("jefferislab/MirrorRegistrations") ## update all current registration repositories update_reg_repos() ## End(Not run)
## find the root location of all registration directories local_reg_dir_for_url() ## Not run: ## Add the two main jefferislab bridging and mirroring registration # collections for Drosophila brains from github.com. download_reg_repo("jefferislab/BridgingRegistrations") download_reg_repo("jefferislab/MirrorRegistrations") ## update all current registration repositories update_reg_repos() ## End(Not run)
This is a sample template brain for testing purposes which is equivalent to
the FCWB template brain defined by the nat.flybrains
, which should be
considered the canonical version.
Fit affine or thin plate spline transform to arbitrary transformation
fit_xform( reg, samplepts, refpts = NULL, type = c("affine", "rigid", "similarity", "tps"), subsample = FALSE, scale = c(1, 1), ... )
fit_xform( reg, samplepts, refpts = NULL, type = c("affine", "rigid", "similarity", "tps"), subsample = FALSE, scale = c(1, 1), ... )
reg |
Any registration compatible with |
samplepts |
A set of points in the sample (floating) space. Can be any
object compatible with |
refpts |
An optional set of points in the target (fixed) space matching
|
type |
A character string specifying the type of registration. See
|
subsample |
A number of points to subsample from
|
scale |
a 2-vector indicating the amount to scale the sample and
reference points. You can supply one vector if this is the same. If the
transform expects points in microns and returns points in microns then you
would need |
... |
Additional arguments passed to |
NB this function relies on installation of the suggested package Morpho.
A homogeneous affine matrix or a nat::tpsreg
object n.b. only
in development nat (>= 1.10.1)
## Not run: library(nat.flybrains) reg=shortest_bridging_seq(sample='FCWB', reference="JFRC2") fit_xform(reg, nat::kcs20, subsample=200, type='affine') # compute transform with translations in nm not microns fit_xform(reg, nat::kcs20, subsample=200, type='affine', scale=1000) ## End(Not run)
## Not run: library(nat.flybrains) reg=shortest_bridging_seq(sample='FCWB', reference="JFRC2") fit_xform(reg, nat::kcs20, subsample=200, type='affine') # compute transform with translations in nm not microns fit_xform(reg, nat::kcs20, subsample=200, type='affine', scale=1000) ## End(Not run)
Fit a single transform to a bridging registration between brains
fit_xform_brain( sample, reference, via = NULL, type = c("affine", "rigid", "similarity", "tps"), pts = NULL, npts = if (is.null(pts)) 1000 else NULL, scale = c(1, 1), ... )
fit_xform_brain( sample, reference, via = NULL, type = c("affine", "rigid", "similarity", "tps"), pts = NULL, npts = if (is.null(pts)) 1000 else NULL, scale = c(1, 1), ... )
sample |
Source template brain (e.g. IS2) that data is currently in.
Specified either as character vector or a |
reference |
Target template brain (e.g. IS2) that data should be transformed into. |
via |
(optional) intermediate template brain that the registration sequence must pass through. |
type |
A character string specifying the type of registration. See
|
pts |
Optional set of points to use for the fit. If they are not specified they will be randomly sampled (see details). |
npts |
Number of points to use for fit (defaults to 1000 when no
|
scale |
a 2-vector indicating the amount to scale the sample and
reference points. You can supply one vector if this is the same. If the
transform expects points in microns and returns points in microns then you
would need |
... |
Additional arguments passed to |
If pts
are supplied these will be used to construct the fit.
This might be useful if you are focussing on a particular brain region and
supply relevant neurons. But if you want to work with one registration to
use for the whole brain then it's better to use points across the brain.
When points are not provided then fit_xform_brain
will first check
to see if it can find a neuropil surface model for the sample
. If it
can, it will sample a number of points that lie inside the surface model.
If no surface model can be found, then it will look for a
templatebrain
object specifying a bounding box around the
sample brain. If successful, it will sample npts
within that
bounding box. If not there will be an error and you will have to supply the
points.
A homogeneous affine matrix or a nat::tpsreg
object
n.b. only in development nat (>= 1.10.1)
## Not run: library(nat.flybrains) t1=fit_xform_brain(sample='FCWB', reference="JFRC2", type='affine') t1 # the same but for points in nm fit_xform_brain(sample='FCWB', reference="JFRC2", type='affine', scale=1000) # run the fit based on a particular group of Kenyon cells t2=fit_xform_brain(sample='FCWB', reference="JFRC2", pts=nat::kcs20, npts=300, type='affine') nclear3d() mfrow3d(1, 2, sharedMouse = TRUE) plot3d(JFRC2) plot3d(xform(FCWB.surf, t1)) next3d() plot3d(JFRC2) plot3d(xform(FCWB.surf, t2)) # the whole brain surfaces clearly match better when you fit on the whole brain ## End(Not run)
## Not run: library(nat.flybrains) t1=fit_xform_brain(sample='FCWB', reference="JFRC2", type='affine') t1 # the same but for points in nm fit_xform_brain(sample='FCWB', reference="JFRC2", type='affine', scale=1000) # run the fit based on a particular group of Kenyon cells t2=fit_xform_brain(sample='FCWB', reference="JFRC2", pts=nat::kcs20, npts=300, type='affine') nclear3d() mfrow3d(1, 2, sharedMouse = TRUE) plot3d(JFRC2) plot3d(xform(FCWB.surf, t1)) next3d() plot3d(JFRC2) plot3d(xform(FCWB.surf, t2)) # the whole brain surfaces clearly match better when you fit on the whole brain ## End(Not run)
Standard local checkout location for extra registration directories
local_reg_dir_for_url(url = NULL)
local_reg_dir_for_url(url = NULL)
url |
Character vector containing a url. When |
When called without any argument returns the root directory that
will be inspected for extra registrations. You can put a sub-folder
yourself there manually and then call add_reg_folders, but you are much
better off in general using download_reg_repo
to install from
a github repository such as this one of ours:.
jefferislab/BridgingRegistrations
Note that this folder will always be the same place on a machine i.e. this
defines a consistent, persistent location on disk to store data across
sessions. You can modify the location of this folder by editing the
R_USER_DATA_DIR
environment variable. While this is not recommended
on a personal machine, it may be necessary on a server. See
rappdirs::user_data_dir
for details.
When called with a url, a SHA1 hash will be calculated for the URL and appended to the basepath. This should ensure that locations derived from different URLs do not clash.
path(s) containing registrations on disk.
download_reg_repo
,
rappdirs::user_data_dir
Mirror 3D object around a given axis, optionally using a warping registration
mirror_brain( x, brain = regtemplate(x), mirrorAxis = c("X", "Y", "Z"), transform = c("warp", "affine", "flip"), ... )
mirror_brain( x, brain = regtemplate(x), mirrorAxis = c("X", "Y", "Z"), transform = c("warp", "affine", "flip"), ... )
x |
the 3D object to be mirrored. |
brain |
source template brain (e.g. IS2) that data is in. |
mirrorAxis |
the axis to mirror (default |
transform |
whether to use warp (default) or affine component of registration, or simply flip about midplane of axis. |
... |
extra arguments to pass to |
A transformed version of x
data(FCWB.demo) # Simple mirror along the x i.e. medio-lateral axis kcs20.flip=mirror_brain(kcs20, FCWB.demo, transform='flip') ## Full non-rigid mirroring to account for differences in shape/centering of ## template brain. ## Depends on nat.flybrains package and system CMTK installation ## Not run: library(nat.flybrains) kcs20.right=mirror_brain(kcs20, FCWB, .progress='text') plot3d(kcs20, col='red') plot3d(kcs20.right, col='green') # include surface plot of brain plot3d(FCWB) # Compare simple flip with full mirror # This template brain is highly symmetric so these are almost identical clear3d() plot3d(kcs20.flip, col='blue') plot3d(kcs20.right, col='green') # Convert to JFRC2 and do the same kcs20.jfrc2=xform_brain(kcs20, sample = FCWB, reference=JFRC2, .progress='text') kcs20.jfrc2.right=mirror_brain(kcs20.jfrc2, JFRC2, .progress='text') kcs20.jfrc2.flip=mirror_brain(kcs20.jfrc2, JFRC2, transform='flip') clear3d() # This time there is a bigger difference between the two transformations plot3d(kcs20.jfrc2.flip, col='blue') plot3d(kcs20.jfrc2.right, col='green') # plot mushroom body neuropils as well plot3d(JFRC2NP.surf, "MB.*_R", alpha=0.3, col='grey') # Compare Euclidean distance between corresponding points in all neurons diffs=xyzmatrix(kcs20.jfrc2.flip)-xyzmatrix(kcs20.jfrc2.right) hist(sqrt(rowSums(diffs^2)), xlab='Distance /microns') ## End(Not run)
data(FCWB.demo) # Simple mirror along the x i.e. medio-lateral axis kcs20.flip=mirror_brain(kcs20, FCWB.demo, transform='flip') ## Full non-rigid mirroring to account for differences in shape/centering of ## template brain. ## Depends on nat.flybrains package and system CMTK installation ## Not run: library(nat.flybrains) kcs20.right=mirror_brain(kcs20, FCWB, .progress='text') plot3d(kcs20, col='red') plot3d(kcs20.right, col='green') # include surface plot of brain plot3d(FCWB) # Compare simple flip with full mirror # This template brain is highly symmetric so these are almost identical clear3d() plot3d(kcs20.flip, col='blue') plot3d(kcs20.right, col='green') # Convert to JFRC2 and do the same kcs20.jfrc2=xform_brain(kcs20, sample = FCWB, reference=JFRC2, .progress='text') kcs20.jfrc2.right=mirror_brain(kcs20.jfrc2, JFRC2, .progress='text') kcs20.jfrc2.flip=mirror_brain(kcs20.jfrc2, JFRC2, transform='flip') clear3d() # This time there is a bigger difference between the two transformations plot3d(kcs20.jfrc2.flip, col='blue') plot3d(kcs20.jfrc2.right, col='green') # plot mushroom body neuropils as well plot3d(JFRC2NP.surf, "MB.*_R", alpha=0.3, col='grey') # Compare Euclidean distance between corresponding points in all neurons diffs=xyzmatrix(kcs20.jfrc2.flip)-xyzmatrix(kcs20.jfrc2.right) hist(sqrt(rowSums(diffs^2)), xlab='Distance /microns') ## End(Not run)
Plot 3D surface of a template brain
## S3 method for class 'templatebrain' plot3d(x, col = "grey", alpha = 0.3, ...)
## S3 method for class 'templatebrain' plot3d(x, col = "grey", alpha = 0.3, ...)
x |
the template brain to plot. |
col |
the color of the surface. |
alpha |
the alpha value of the surface. |
... |
extra arguments to pass to |
This function will work immediately for the standard templatebrain defined in the package documentation. If passed an object called e.g. FCWB it expects to find another object named FCWB.surf containing the surface information. If you follow this naming convention for user-defined refbrains it will work for them as well.
This function is principally called for its side effect of adding to the plot. A vector of rgl shape IDs is returned invisibly.?
Get or set the registration template space in which an object lives
regtemplate(x) regtemplate(x) <- value
regtemplate(x) regtemplate(x) <- value
x |
The 3D object whose registration space will be set/returned |
value |
The registration template brain (either a character vector
naming the space or a |
In order to facilitate transformations between objects in defined anatomical spaces these functions allow the registration template for an object to be specified. Most of the time you will not need to use these functions manually since the appropriate space will be set by the function xform_brain and friends.
Either a templatebrain
object or the newly tagged object
## Not run: library(nat.flybrains) kcs3=kcs20[1:3] regtemplate(kcs3)=FCWB regtemplate(kcs3) kcs3m=mirror_brain(kcs3, brain=regtemplate(kcs20)) plot3d(kcs3, col='red') plot3d(kcs3m, col='green') ## End(Not run)
## Not run: library(nat.flybrains) kcs3=kcs20[1:3] regtemplate(kcs3)=FCWB regtemplate(kcs3) kcs3m=mirror_brain(kcs3, brain=regtemplate(kcs20)) plot3d(kcs3, col='red') plot3d(kcs3m, col='green') ## End(Not run)
templatebrain
objects encapsulate key information for the reference
brain in an image registration. Usually this will be a standard template
brain used for many registrations. It will normally be much more
convenient to use as.templatebrain
methods to convert an image
file or an im3d object into a templatebrain
.
templatebrain( name, regName = name, type = NULL, sex = NULL, dims = NULL, BoundingBox = NULL, voxdims = NULL, origin = NULL, units = NULL, description = NULL, doi = NULL, ... )
templatebrain( name, regName = name, type = NULL, sex = NULL, dims = NULL, BoundingBox = NULL, voxdims = NULL, origin = NULL, units = NULL, description = NULL, doi = NULL, ... )
name |
the full name of the template. |
regName |
the short name. This will be the stem used to prefix registrations (e.g. JFRC2_someimage.list) for this template brain and likely also the stem of the template brain image (e.g. JFRC2.nrrd). |
type |
one of |
sex |
the sex of the template brain. For templates with
|
dims |
dimensions of the image (number of voxels). |
BoundingBox |
physical dimensions of the image (see
|
voxdims |
physical spacing between voxels. |
origin |
the physical location of the first voxel |
units |
units of physical measurements (e.g. microns). |
description |
details of the template. |
doi |
a DOI for the original template brain image. |
... |
additional named arguments that will be added as fields to the
|
A variety of methods are available to work on templatebrain
objects. See
templatebrain-meths
for basic methods. The two main functions
that are available for using template brains are xform_brain
and
mirror_brain
.
templatebrain
objects are only useful for transformation processes
when the BoundingBox
is specified to define the physical extent of the
volume. We use the definition of the Amira 3D visualisation and analysis
software. This corresponds to the node centers option in the
NRRD format. The
bounding box can be obtained from NRRD or AmiraMesh format files. See
boundingbox
for details.
A list with class templatebrain
.
as.templatebrain
, templatebrain-meths
,
xform_brain
, mirror_brain
.
is.templatebrain
tests if object is of class
templatebrain
as.character.templatebrain
converts template brain to
character vector representation (normally used to extract the short name
i.e. regName
).
print.templatebrain
prints templatebrain information in
human-readable form
as.im3d
converts a template brain to a
nat::im3d
object; this is probably useful for
developers.
origin
extracts the space origin of a templatebrain
object.
dim
extracts the dimensions (in number of pixels) of the
image associated with a templatebrain
object.
voxdims
extracts the dimensions (in calibrated spatial
units, e.g. microns) of voxels in the image associated with a
templatebrain
object.
boundingbox
extracts the boundingbox (in calibrated
spatial units, e.g. microns) of the image associated with a templatebrain
object. See boundingbox
for details.
is.templatebrain(x) ## S3 method for class 'templatebrain' as.character(x, field = c("regName", "name"), ...) ## S3 method for class 'templatebrain' print(x, ...) ## S3 method for class 'templatebrain' as.im3d(x, ...) ## S3 method for class 'templatebrain' origin(x, ...) ## S3 method for class 'templatebrain' dim(x, ...) ## S3 method for class 'templatebrain' voxdims(x, ...) ## S3 method for class 'templatebrain' boundingbox(x, ...)
is.templatebrain(x) ## S3 method for class 'templatebrain' as.character(x, field = c("regName", "name"), ...) ## S3 method for class 'templatebrain' print(x, ...) ## S3 method for class 'templatebrain' as.im3d(x, ...) ## S3 method for class 'templatebrain' origin(x, ...) ## S3 method for class 'templatebrain' dim(x, ...) ## S3 method for class 'templatebrain' voxdims(x, ...) ## S3 method for class 'templatebrain' boundingbox(x, ...)
x |
an object (usually a |
field |
which field to use (defaults to |
... |
additional arguments for methods. |
Return values are documented in the generic methods.
is.templatebrain
returns a logical indicating whether or not the
object is a templatebrain
.
as.character.templatebrain
returns a character vector with the
name of the template brain.
dim.templatebrain
returns a 3-value integer vector.
data(FCWB.demo) is.templatebrain(FCWB.demo) origin(FCWB.demo) dim(FCWB.demo) voxdims(FCWB.demo) boundingbox(FCWB.demo) # print method FCWB.demo
data(FCWB.demo) is.templatebrain(FCWB.demo) origin(FCWB.demo) dim(FCWB.demo) voxdims(FCWB.demo) boundingbox(FCWB.demo) # print method FCWB.demo
When x=NULL
all repositories listed in
options(nat.templatebrains.regdirs) are checked to see if they are git
repositories and, if yes, they are pulled to update.
update_reg_repos(x = NULL)
update_reg_repos(x = NULL)
x |
Path to local checkout of a registration git repository. See details for meaning of default. |
This function is principally called for its side effect, but does
return a git2r::repository
object containing the path on disk to the
location of the git repository with registration.
Transform 3D object between template brains
xform_brain( x, sample = regtemplate(x), reference, via = NULL, imagedata = is.character(x), checkboth = NULL, target = NULL, Verbose = interactive(), ... )
xform_brain( x, sample = regtemplate(x), reference, via = NULL, imagedata = is.character(x), checkboth = NULL, target = NULL, Verbose = interactive(), ... )
x |
the 3D object to be transformed |
sample |
Source template brain (e.g. IS2) that data is currently in.
Specified either as character vector or a |
reference |
Target template brain (e.g. IS2) that data should be transformed into. |
via |
(optional) intermediate template brain that the registration sequence must pass through. |
imagedata |
Whether |
checkboth |
When |
target |
When transforming image data, this specifies the target space
(defaults to |
Verbose |
Whether to show a message with the sequence of template brains |
... |
extra arguments to pass to |
NB the sample
, reference
and via
brains can
either be templatebrain
objects or a character string containing the
short name of the template e.g. "IS2"
.
xform_brain
uses the helper function
shortest_bridging_seq
to find the shortest path between
different template brains based on the set of bridging registrations that
the natverse has been informed about (see bridging_graph
).
You can specify a via
argument to ensure that the registrations
passes through one or more intermediate templates. Note that when multiple
brains are passed to via
they should be in order from sample to
reference. If you are passing multiple templatebrain
objects,
they must be wrapped in a list.
When transforming image data (imagedata=TRUE
), the target
argument should normally be specified. This defines the absolute/voxel
dimensions of the target space. This can be calculated from a
templatebrain
object, so by default it will be set to the value of
the reference
argument. Alternatively an image file on disk can be
specified; this is essential if the reference
argument does not
specify a templatebrain
object but instead just names a
template space (i.e. is a string).
The significance of the imagedata
and checkboth
arguments is
that CMTK registrations are not directly invertible although they can be
numerically inverted in most cases (unless there are regions where folding
occurred). For image data, numerical inversion is much slower.
You can control whether you want to allow inverse registrations manually by
setting checkboth
explicitly. Otherwise when checkboth=NULL
the default is to act as if checkboth=TRUE
but issue a warning if an
inversion must be used.
A transformed version of x
mirror_brain
, shortest_bridging_seq
bridging_graph
, regtemplate
,
xform
. xformpoints
, xformimage
,
cmtk.reformatx
(for transforming image data with CMTK).
## depends on nat.flybrains package and system CMTK installation ## Not run: ## reformat neurons ## library(nat.flybrains) # Plot Kenyon cells in their original FCWB template brain nopen3d() plot3d(kcs20) plot3d(FCWB) # Convert to JFCR2 template brain kcs20.jfrc2=xform_brain(kcs20, sample = FCWB, reference=JFRC2) # now plot in the new JFRC2 space nopen3d() plot3d(kcs20.jfrc2) plot3d(JFRC2) # compare with the untransformed neurons plot3d(kcs20) # plot with neuropil sub regions for the left mushroom body clear3d() plot3d(kcs20.jfrc2) # nb "MB.*_L" is a regular expression plot3d(JFRC2NP.surf, "MB.*_L", alpha=0.3) # compare with originals - bridging registration is no perfect in peduncle nopen3d() plot3d(kcs20) plot3d(FCWBNP.surf, "MB.*_L", alpha=0.3) # insist on using a specific intermediate template brain # this would nor be an improvement in this case kcs20.jfrc2viais2=xform_brain(kcs20, sample = FCWB, via=IS2, reference=JFRC2) ## reformat image examples # see ?cmtk.reformatx for details of any additional arguments # note that for image data a target space defining the dimensions of the # output image must be specified - this happens by default using the # reference templatebrain object xform_brain('in.nrrd', sample=FCWB, ref=JFRC2, output='out.nrrd') # or you can specify an image file explicitly as target xform_brain('in.nrrd', sample=FCWB, ref=JFRC2, output='out.nrrd', target='JFRC2.nrrd') # use partial volume interpolation for label field xform_brain('labels.nrrd', sample=FCWB, ref=JFRC2, output='out.nrrd', interpolation='pv') # use binary mask to restrict (and speed up) reformatting xform_brain('in.nrrd', sample=FCWB, ref=JFRC2, output='out.nrrd', mask='neuropil.nrrd') ## End(Not run)
## depends on nat.flybrains package and system CMTK installation ## Not run: ## reformat neurons ## library(nat.flybrains) # Plot Kenyon cells in their original FCWB template brain nopen3d() plot3d(kcs20) plot3d(FCWB) # Convert to JFCR2 template brain kcs20.jfrc2=xform_brain(kcs20, sample = FCWB, reference=JFRC2) # now plot in the new JFRC2 space nopen3d() plot3d(kcs20.jfrc2) plot3d(JFRC2) # compare with the untransformed neurons plot3d(kcs20) # plot with neuropil sub regions for the left mushroom body clear3d() plot3d(kcs20.jfrc2) # nb "MB.*_L" is a regular expression plot3d(JFRC2NP.surf, "MB.*_L", alpha=0.3) # compare with originals - bridging registration is no perfect in peduncle nopen3d() plot3d(kcs20) plot3d(FCWBNP.surf, "MB.*_L", alpha=0.3) # insist on using a specific intermediate template brain # this would nor be an improvement in this case kcs20.jfrc2viais2=xform_brain(kcs20, sample = FCWB, via=IS2, reference=JFRC2) ## reformat image examples # see ?cmtk.reformatx for details of any additional arguments # note that for image data a target space defining the dimensions of the # output image must be specified - this happens by default using the # reference templatebrain object xform_brain('in.nrrd', sample=FCWB, ref=JFRC2, output='out.nrrd') # or you can specify an image file explicitly as target xform_brain('in.nrrd', sample=FCWB, ref=JFRC2, output='out.nrrd', target='JFRC2.nrrd') # use partial volume interpolation for label field xform_brain('labels.nrrd', sample=FCWB, ref=JFRC2, output='out.nrrd', interpolation='pv') # use binary mask to restrict (and speed up) reformatting xform_brain('in.nrrd', sample=FCWB, ref=JFRC2, output='out.nrrd', mask='neuropil.nrrd') ## End(Not run)