Skip to content

Commit

Permalink
WIP: trying to allow scaling of diameter by xform
Browse files Browse the repository at this point in the history
* this works ok when reg is a function, but fails when reg is actually a reglist object (one of whose elements is a function)
  • Loading branch information
jefferis committed Feb 19, 2019
1 parent f4af0b1 commit 2993a4b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 63 deletions.
98 changes: 64 additions & 34 deletions R/xform.R
Original file line number Diff line number Diff line change
@@ -1,63 +1,79 @@
#' Transform the 3D location of objects such as neurons
#'
#' \code{xform} is designed to operate on a variety of data types, especially
#' objects encapsulating neurons. \code{xform} depends on two specialised
#' downstream functions \code{\link{xformpoints}} and \code{\link{xformimage}}.
#'
#' \code{xform} is designed to operate on a variety of data types, especially
#' objects encapsulating neurons. \code{xform} depends on two specialised
#' downstream functions \code{\link{xformpoints}} and \code{\link{xformimage}}.
#' These are user visible any contain some useful documentation, but should only
#' be required for expert use; in almost all circumstances, you should use only
#' be required for expert use; in almost all circumstances, you should use only
#' \code{xform}.
#'
#'
#' @section Registrations:
#'
#' When \code{reg} is a character vector, xform's specialised downstream
#' functions will check to see if it defines a path to one (or more)
#'
#' When \code{reg} is a character vector, xform's specialised downstream
#' functions will check to see if it defines a path to one (or more)
#' registrations on disk. These can be of two classes
#'
#'
#' \itemize{
#'
#'
#' \item CMTK registrations
#'
#' \item \code{\link{reglist}} objects saved in R's \code{RDS} format (see
#' \code{\link{readRDS}}) which can contain any sequence of registrations
#'
#' \item \code{\link{reglist}} objects saved in R's \code{RDS} format (see
#' \code{\link{readRDS}}) which can contain any sequence of registrations
#' supported by nat.
#'
#'
#' }
#'
#'
#' If the path does indeed point to a CMTK registration, this method will hand
#' off to \code{xformpoints.cmtkreg} or \code{xformimages.cmtkreg}. In this
#' case, the character vector may optionally have an attribute, 'swap', a
#' logical vector of the same length indicating whether the transformation
#' direction should be swapped. At the moment only CMTK registration files are
#' supported.
#'
#' If \code{reg} is a character vector of length >=1 defining a sequence of
#'
#' If \code{reg} is a character vector of length >=1 defining a sequence of
#' registration files on disk they should proceed from sample to reference.
#'
#' Where \code{reg} is a function, it should have a signature like
#' \code{myfun(x,), ...} where the \code{...} \strong{must} be provided in
#' order to swallow any arguments passed from higher level functions that are
#' not relevant to this particular transformation function.

#' @details Methods are provided for some specialised S3 classes. Further
#' methods can of course be constructed for user-defined S3 classes. However
#' this will probably not be necessary if the \code{xyzmatrix} and
#' \code{`xyzmatrix<-`} generics are suitably overloaded \emph{and} the S3
#'
#' Where \code{reg} is a function, it should have a signature like
#' \code{myfun(x,), ...} where the \code{...} \strong{must} be provided in
#' order to swallow any arguments passed from higher level functions that are
#' not relevant to this particular transformation function. Note that
#' functions can be supplied that expect an Nx3 matrix of points OR a
#' \code{neuron} as input. In the latter case, the function can be used to
#' scale the neuron's diameter as well as the point locations. See examples.
#'
#' @details Methods are provided for some specialised S3 classes. Further
#' methods can of course be constructed for user-defined S3 classes. However
#' this will probably not be necessary if the \code{xyzmatrix} and
#' \code{`xyzmatrix<-`} generics are suitably overloaded \emph{and} the S3
#' object inherits from \code{list}.
#'
#'
#' Note that given the behaviour of the \code{xyzmatrix} functions, the
#' \code{xform.data.frame} method will transform the x,y,z or X,Y,Z columns of
#' a data.frame if the data.frame has more than 3 columns, erroring out if no
#' such unique columns exist.
#'
#'
#' @param x an object to transform
#' @param reg A registration defined by a matrix, a function, a \code{cmtkreg}
#' object, or a character vector specifying a path to one or more
#' @param reg A registration defined by a matrix, a function, a \code{cmtkreg}
#' object, or a character vector specifying a path to one or more
#' registrations on disk (see Registrations section).
#' @param ... additional arguments passed to methods and eventually to
#' @param ... additional arguments passed to methods and eventually to
#' \code{\link{xformpoints}}
#' @export
#' @rdname xform
#' @seealso \code{\link{xformpoints}}
#'
#' @examples
#' ## using an affine matrix
#' scalemat=t(rgl::scaleMatrix(1,2,3))
#' scaled=xform(Cell07PNs[1:3], scalemat)
#'
#' ## using a function
#' # doesn't scale radius
#' tnm=xform(Cell07PNs[1:3], function(x, ...) x=x*1e3)
#' head(tnm[[1]][['d']])
#' # does scale radius
#' tnm2=xform(Cell07PNs[1:3], function(x, ...) x=x*rep(1e3, 4))
#' head(tnm2[[1]][['d']])
xform<-function(x, reg, ...) UseMethod('xform')

#' @details TODO get this to work for matrices with more than 3 columns by
Expand Down Expand Up @@ -116,7 +132,21 @@ xform.shape3d<-xform.list

#' @export
#' @rdname xform
xform.neuron<-xform.list
xform.neuron<-function(x, reg, FallBackToAffine=TRUE, na.action='error', ...){
if(is.function(reg)) {
# as a special case if we are passed a function, see if we can
# apply it directly to neuron. This is a bit hacky but lets us
# more easily scale
tx <- try(reg(x, FallBackToAffine=TRUE, na.action='error', ...))
if(!inherits(tx, 'try-error'))
return(tx)
}
points=xyzmatrix(x)
pointst=xformpoints(reg, points, FallBackToAffine=FallBackToAffine,
na.action=na.action, ...)
xyzmatrix(x)<-pointst
x
}

#' @export
#' @rdname xform
Expand Down
72 changes: 43 additions & 29 deletions man/xform.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2993a4b

Please sign in to comment.