R/mapped_neurovec.R

Defines functions MappedNeuroVec MappedNeuroVecSource

Documented in MappedNeuroVec MappedNeuroVecSource

#' MappedNeuroVecSource
#'
#' constructs a MappedNeuroVecSource object
#'
#' @param file_name the name of the image file to be memory mapped
#' @export
#' @rdname MappedNeuroVecSource-class
MappedNeuroVecSource <- function(file_name) {
  meta <- read_header(file_name)
  stopifnot(length(dim(meta)) >= 3)
  new("MappedNeuroVecSource", meta_info=meta)
}


#' MappedNeuroVec
#'
#' constructs a MappedNeuroVec object
#'
#' @param file_name the name of the 4D image file that containing the memory-mapped data source.
#' @export
#' @rdname MappedNeuroVec-class
MappedNeuroVec <- function(file_name) {
  src <- MappedNeuroVecSource(file_name)
  load_data(src)
}


#' @export
#' @rdname load_data-methods
setMethod(f="load_data", signature=c(x="MappedNeuroVecSource"),
          def=function(x) {
            meta <- x@meta_info
            fmap <- mmap::mmap(meta@data_file, mode=.getMMapMode(meta@data_type), prot=mmap::mmapFlags("PROT_READ"))
            offset <- meta@data_offset/.getDataSize(meta@data_type)

            bspace <- NeuroSpace(dim(meta), meta@spacing,
                                 meta@origin, meta@spatial_axes, trans=trans(meta))

            new("MappedNeuroVec", space=bspace, filemap=fmap, offset=as.integer(offset))

          })


#' @export
#' @rdname linear_access-methods
setMethod(f="linear_access", signature=signature(x = "MappedNeuroVec", i = "numeric"),
          def=function (x, i) {
            idx <- i + x@offset
            x@filemap[idx]
          })
bbuchsbaum/neuroim2 documentation built on Oct. 12, 2019, 6:54 a.m.