Skip to content

Veet import efficiency - #90

Open
ThomasKraft wants to merge 3 commits into
tscnlab:mainfrom
ThomasKraft:veet_import_efficiency
Open

Veet import efficiency#90
ThomasKraft wants to merge 3 commits into
tscnlab:mainfrom
ThomasKraft:veet_import_efficiency

Conversation

@ThomasKraft

Copy link
Copy Markdown

This PR stems from issue #85 and generally attempts to speed up and manage memory efficiency for VEET import, which can cause particular problems due to the large nature of data types such as TOF. Speed is improved here using the data.table package for import. There are also some relevant changes for auto.plot defaults when TOF data are imported to avoid unnecessary processing time. The basic testing code pasted below shows a general outcome I found of these changes cutting VEET import processing time by ~50%:

Screenshot 2026-07-20 at 14 06 11

Ultimately I discovered that VEET files are large enough that the main memory issue occurs simply due to using map:::purr() on files that are of sufficient size to overwhelm a normal laptop when they are all loaded into memory. Packages such as GGIR manage this same type of issue by batch loading files and processing them into epochs in chunks before discarding the raw files during the process. Although something similar could be done here, the LightLogR infrastructure is sufficient for doing this manually with a short custom scripts (which is currently working well for me). So, I favor not introducing any changes for now.

Basic benchmarking code:

setwd(".")

cran_lib <- tempfile("cran-lib-")
local_lib <- tempfile("local-lib-")

dir.create(cran_lib)
dir.create(local_lib)

pkg <- "LightLogR"

# Install CRAN version into its own library
install.packages(
  pkg,
  lib = cran_lib,
  repos = "https://cloud.r-project.org",
  dependencies = TRUE
)

# Install your current local branch into its own library
install.packages(
  c("remotes", "pak"),
  repos = "https://cloud.r-project.org"
)

remotes::install_local(
  path = ".",
  lib = local_lib,
  dependencies = TRUE,
  upgrade = "never",
  force = TRUE
)

unload_lightlogr <- function() {
  if (paste0("package:", pkg) %in% search()) {
    detach(paste0("package:", pkg), unload = TRUE, character.only = TRUE)
  }
  
  if (pkg %in% loadedNamespaces()) {
    unloadNamespace(pkg)
  }
}

run_with_version <- function(lib, expr) {
  unload_lightlogr()
  library(pkg, lib.loc = lib, character.only = TRUE)
  force(expr)
}

filenames <- list.files(path = "PATH_TO_VEET_DATA/",
                        recursive = T, pattern = "Sensor_Data|sensor_data", full.names=TRUE)

microbenchmark::microbenchmark(
cran_time = {
  cran_result <- run_with_version(
    cran_lib,
    {
      LightLogR::import$VEET(
        filename = filenames[1:15],
        modality="TOF",
        auto.plot=F,
        silent=T
      )
    }
  )
},

local_time = {
  local_result <- run_with_version(
    local_lib,
    {
      LightLogR::import$VEET(
        filename = filenames[c(1:15)],
        modality="TOF",
        auto.plot=F,
        silent=T
      )
    }
  )
},
times = 2)

…s to improve both memory efficiency and speed for the large files produced by VEETs. Versions of these and other strategies were tested and although data.table() approaches performed the best, it seemed prudent to introduce minimal changes within the tidyverse style in order to maintain consistency in the package.
Add data.table as an explicit package import and update the VEET import expression to parse modality-filtered rows with data.table::fread using explicit column names and column classes. Per-file VEET results are combined with data.table::rbindlist, and file.name plus Datetime are added/reordered by reference to reduce conversion and copy overhead versus the previous vroom/list_rbind/dplyr mutate path.

Reduce downstream memory pressure for large VEET TOF imports by narrowing duplicate detection to likely duplicate Id/Datetime rows before doing full-row duplicate counting, and by using a minimal Id/Datetime data frame for the VEET TOF auto.plot overview so gap detection does not scan all 256 TOF measurement columns.

Keep the public VEET API unchanged after removing the experimental low.memory/chunk.size approach, since the observed allocation failure is caused by final object size rather than chunked import intermediates. Add regression coverage for typed TOF imports and the lightweight TOF auto.plot path.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves performance and memory characteristics of VEET imports (especially TOF) by switching to data.table-based parsing and by reducing costly work during duplicate detection and auto-plotting.

Changes:

  • Refactor VEET import expression to use data.table::fread() and rbindlist() with explicit column typing.
  • Optimize duplicate detection to avoid full-width scans unless duplicates are plausible, and avoid expensive gap detection during TOF auto-plot.
  • Add VEET-focused tests, update NEWS.md, and add data.table to package imports.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
R/import_expressions.R Reworks VEET import parsing to use data.table and sets Datetime via data.table::set().
R/import_LL.R Optimizes duplicate detection prefiltering and special-cases VEET/TOF auto-plot to skip gap finding.
tests/testthat/test-import_LL.R Adds VEET/TOF import and auto-plot tests.
DESCRIPTION Adds data.table as an imported dependency.
NEWS.md Documents VEET import efficiency improvements and TOF-specific optimizations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/testthat/test-import_LL.R Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants