Skip to content

Fix rolling-median smoothing and improve part 1 performance - #1564

Merged
vincentvanhees merged 6 commits into
mainfrom
issue1562_rollingmedian
Nov 8, 2026
Merged

vincentvanhees merged 6 commits into
mainfrom
issue1562_rollingmedian

Conversation

@vincentvanhees

@vincentvanhees vincentvanhees commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

This PR improves two parts of the part 1 pipeline: rolling-median metric extraction in g.applymetrics() and calibration in g.calibrate().

1. g.applymetrics(): rolling-median correctness and performance

Fixes loss of smoothing when block length is not divisible by the downsampling step, including typical 75 Hz blocks.

  • Trim the repeated median to the input length instead of reverting to raw acceleration.
  • Correct trailing padding and preserve valid zero medians.
  • Call stats::runmed() directly for complete data, retaining the zoo fallback for missing or short inputs.
  • Add regression coverage and update downstream hip sleep-window expectations after corrected padding removes a spurious final-day sleep-period fragment.

Angle and rolling-median metrics intentionally change where smoothing or padding was previously incorrect.

Fixes #1562

2. g.calibrate(): computation and allocation overhead

  • Avoid full raw-block matrix copies and release large vectors earlier.
  • Reduce epoch-mean temporaries while preserving cumulative-sum arithmetic.
  • Use vapply() for epoch SDs with the same sd() calculation.
  • Size feature buffers by epoch count and prepare only populated rows.
  • Reuse matrix inputs and apply per-axis transforms during fitting.

The existing calibration arithmetic, thresholds, solver and stopping rules are retained.

Performance: On a cached block of 6.7 million real samples, the larger calibration refactor reduced median computation time from 1.08 to 0.66 seconds and cumulative R allocations from 2,266 to 1,095 MiB—approximately 39% less computation time and 52% fewer allocated bytes. This baseline already includes the SD-loop optimisation, and the benchmark excludes file reading.

Combined impact and validation

Both changes reduce part 1 processing overhead. Their percentages come from separate workloads and cannot be added. File reading remains a substantial cost, and variable full-file timings prevented a reliable combined end-to-end percentage.

  • Full local test suite: 1,657 passes and zero failures.
  • Expanded regression coverage for both processing paths.
  • Real-file calibration results match exactly, excluding resource logs.
  • A complete GGIR(mode = 1) run preserves inspection and epoch metrics relative to the preceding implementation.
  • Reset inherited ndayswindow in the study-dates test; that test now runs without unexpected warnings.
  • Added a reproducible calibration benchmark, performance notes and NEWS entries.

All runtime changes remain in R, with the public interface and dependencies unchanged.

Checklist before merging:

  • Existing tests still work (check by running the test suite, e.g. from RStudio).
  • Added tests (if you added functionality) or fixed existing test (if you fixed a bug).
  • Clean code has been attempted, e.g. intuitive object names and no code redundancy.
  • Documentation updated:
    • Function documentation
    • Chapter vignettes for GitHub IO
    • Vignettes for CRAN
  • Corresponding issue tagged in PR message. If no issue exist, please create an issue and tag it.
  • Updated release notes in inst/NEWS.Rd with a user-readable summary. Please, include references to relevant issues or PR discussions.
  • If you think you made a significant contribution, add your name to the contributors lists in the DESCRIPTION, zenodo.json, and inst/CITATION files.
  • GGIR parameters were added/removed. If yes, please also complete checklist below.

- Preserve smoothing when block length is not divisible by the sampling step
- Correct trailing median padding and preserve valid zero medians
- Use runmed directly for complete data, retaining the zoo fallback
- Reduce calibration epoch SD overhead with vapply
- Add regression tests
Correct median padding removes a spurious 15-second sleep-period
fragment on the final partial day. Update the hip test to expect
two complete days, while retaining the wrist partial-day checks.
Avoid full raw-block matrix copies, reduce epoch-mean temporaries,
size feature buffers by epoch count, and reuse matrix inputs during
fitting. Preserve calibration arithmetic and stopping rules.

Expand regression coverage and add a reproducible cached-block
benchmark. On the tested block, computation time falls by 39% and
R allocations by 52%, with identical calibration results apart
from resource logs.
Record profiling results, benchmark methods, numerical validation
and remaining optimisation opportunities. Link findings to the
relevant commits, mark completed work, and distinguish historical
measurements from current performance.
Explicitly restore ndayswindow = 7 when switching from masking
strategy 5 to strategy 1, preventing two unrelated parameter
warnings in the out-of-range study-dates test.
@vincentvanhees
vincentvanhees marked this pull request as ready for review October 28, 2026 16:18
@vincentvanhees
vincentvanhees merged commit 43fba0f into main Nov 8, 2026
7 checks passed
@vincentvanhees
vincentvanhees deleted the issue1562_rollingmedian branch November 9, 2026 10:35
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.

Rolling median for anglez is discarded when block length is not a multiple of stepsize

1 participant