R/read_physio.R

Defines functions read_acq_txt read_acq read_physio read_physio_neighborhood

Documented in read_acq read_acq_txt read_physio read_physio_neighborhood

#' @title Read physiology from neighborhood task
#' @param physio_data This can be an \code{ep.physio} object, in which case we just window it to the relevant
#'   time periods. Or, this
read_physio_neighborhood <- function(physio_data) {

  if (!inherits(physio_data, "ep.physio")) {
    #An acq filename was passed in. Import it
    assert_file_exists(physio_data)

  }

  #Now nar

}

#' general wrapper for reading physio data into the package
#' @export
read_physio <- function(file, parser=NULL, ...) {
  if (is.null(parser)) { stop("Need to pass parser function to read_physio") }
  stopifnot(file.exists(file))
  physio <- parser(file, ...)
  class(physio) <- c(class(physio), "ep.physio") #tag with ep.physio class

  #other general post-processing for eye data
  return(physio)
}


### Acqknowledge read functions
#' @title Import Acqknowledge ACQ files into data.frames
#' @details
#'   This function calls the acq2hdf5 wrapper R function. The converted HDF5 files are then
#'   parsed by biopac_hdf5_to_dataframe, which relies heavily on the data.table package to build the
#'   data.frame containing physiological data.
#'
#' @return a list of
#' @param acq_files character vector of acq files to be converted to HDF5, then imported as data.frame
#' @param hdf5_output_dir location for converted hdf5 files produced by acq2hdf5. Passed to acq2hdf5.
#' @param keep_hdf5 logical indicating whether to retain hdf5 file after import completes. Default: TRUE
#' @param acq2hdf5_location path to acq2hdf5 binary, installed by the bioread python package.
#'
#' @importFrom checkmate assert_file_exists
#' @export
#' @examples
#' \dontrun{
#'    acq_data <- read_acq("~/Data_Analysis/neuromap/s4_behav_data/physio/data/nmap016/nmap016.acq",
#'                  hdf5_output_dir = "~/temp_acq",
#'                  acq2hdf5_location = "~/Library/Python/3.7/bin/acq2hdf5")
#' }
read_acq <- function(acq_files, hdf5_output_dir=NULL, keep_hdf5=TRUE, acq2hdf5_location=NULL, ...) {
  sapply(acq_files, assert_file_exists)
  if (!keep_hdf5) {
    hdf5_output_dir <- tempdir()  #output txts to temporary directory
  } else {
    if (!is.null(hdf5_output_dir) && !dir.exists(hdf5_output_dir)) { dir.create(hdf5_output_dir) } #create output directory for TXT files if requested
  }

  hdf5_files <- acq2hdf5(acq_files, acq2hdf5_location=acq2hdf5_location)

  #pass additional arguments such as parse_all to read.asc
  res <- lapply(1:length(hdf5_files), function(ff) {
    physio_data <- list(raw=biopac_hdf5_to_dataframe(hdf5file=hdf5_files[ff], ...), hdf5_file=hdf5_files[ff], acq_file=acq_files[ff])
    physio_data$sampling_rate <- attr(physio_data$raw, "sampling_rate") #promote to metadata
    physio_data$max_channel_rate <- attr(physio_data$raw, "max_channel_rate") #promote to metadata
    class(physio_data) <- c("list", "ep.physio") #add ep.physio class
    return(physio_data)
  })

  if (!keep_hdf5) { file.remove(hdf5_files) } #cleanup hdf5 files if requested

  names(res) <- basename(acq_files)
  return(res)

}


#' @title Read a text file generated by acq2txt
#' @description At present, this is just a thin wrapper around fread, which reads
#'   in the Acqknowledge txt file of the physio acquisition. Given that the acq2hdf5
#'   approach seems both faster and more robust, I've developed that in greater depth
#'   and this remains as more of a useful stub
#' @param acq_txt_file A .txt or .txt.gz file created by acq2txt
#' @importFrom data.table fread
#' @importFrom checkmate assert_file_exists
#' @export
#' @examples
#' \dontrun{
#'   acq_data <- read_acq_txt("~/temp_acq/nmap016.txt.gz", na.strings=".")
#' }
read_acq_txt <- function(acq_txt_file, ...) {
  assert_file_exists(acq_txt_file)
  dat <- fread(acq_txt_file, ...)
  return(dat)
}
PennStateDEPENdLab/experiment.pipeline documentation built on April 14, 2024, 9:37 p.m.