Strategy: Refactor event_model
construction into a three-stage pipeline (parse
-> realise terms
-> build design matrix
) to unify entry points (.formula
, .list
, create_event_model
), eliminate redundant computations, improve performance, and simplify the codebase. Leverage the refactored event
and event_term
structures. Update contrast handling to use rlang
and align with the new pipeline.
Phase 1: Implement Core Pipeline Components
[X] Ticket EM-1: Implement parse_event_model
Helper
parse_event_model(formula_or_list, data, block, durations)
in R/event_model_helpers.R
(marked @noRd
).hrfspec
objects.parse_event_formula
(Ticket EM-2).hrfspec
objects, converts to spec table, checks for onset
column in data
(with clear error message if missing).onsets
(checking NAs).blockids
(handling formula/vector input, checking NAs, using base stopifnot()
for non-decreasing check, warning if non-strictly increasing).blockids
to consecutive integers 1..R using match(..., unique(...))
. durations
(recycling using recycle_or_error
, checking NAs).spec_tbl
, onsets
, durations
, canonicalized blockids
, data
, formula_env
.parse_event_model
correctly normalizes inputs and extracts/validates/canonicalizes shared event timing information.[X] Ticket EM-2: Implement parse_event_formula
Helper
parse_event_model
.parse_event_formula(formula, data)
in R/event_model_helpers.R
(marked @noRd
).rlang
to identify terms on RHS.find_and_eval_hrf_calls
(marked @noRd
) that recursively finds hrf()
etc. calls and gracefully ignores non-target calls/symbols (returns list()
).hrf()
/trialwise()
calls once using strict evaluation masking (e.g., rlang::eval_tidy(..., data=mask, env=f_env)
where mask uses env_bury
) in the formula's environment.hrf()
calls.spec_tbl
, onsets
vector, and formula_env
.hrfspec
objects, extracts onsets, and returns environment.[X] Ticket EM-3: Implement realise_event_terms
Helper
event_term
objects.realise_event_terms(parsed_spec, sampling_frame, drop_empty, progress = FALSE)
in R/event_model_helpers.R
(marked @noRd
).parse_event_model
.spec_tbl
(e.g., lapply
or purrr::map
).cli::cli_progress_bar
.construct_event_term
(Ticket EM-4) for each spec.event_term
objects.event_term
objects, with optional progress display.[X] Ticket EM-4: Implement/Refactor construct_event_term
Helper
event_term
from a single hrfspec
and associated model info.construct_event_term(hrfspec, model_spec)
in R/event_model_helpers.R
(marked @noRd
).hrfspec
and the output list from parse_event_model
(model_spec
).hrfspec
using strict masking (using model_spec$data
and model_spec$formula_env
).hrfspec
-specific onsets if the spec allows overriding global onsets (check hrfspec
definition).event_term()
with evaluated variables and shared timing info from model_spec
.event_term
object.event_term
with robust evaluation.[X] Ticket EM-5: Implement build_design_matrix.event_model
Helper
build_event_model_design_matrix(terms, sampling_frame, precision, parallel = FALSE)
in R/event_model_helpers.R
(marked @noRd
).event_term
objects.convolve.event_term
for each term. Consider future.apply::future_lapply
if parallel=TRUE
.cbind
s resulting matrices.term_spans
attribute. Also attach col_indices
(named list: term name -> numeric index vector).tibble
.[ ] Ticket EM-18: Implement HRF Basis Cache/Memoization (Optional)
Phase 2: Refactor Public API and Main S3 Methods
[ ] Ticket EM-6: Refactor event_model()
Public Constructor
event_model()
S3 generic/methods.parse_event_model
, realise_event_terms
, build_event_model_design_matrix
.event_model
object (with $terms
, $design_matrix
containing term_spans
& col_indices
attrs, etc.).c("event_model", "list")
.event_model.list
, event_model.formula
, create_event_model
using .Deprecated()
wrappers.event_model()
uses new pipeline, returns expected structure, old entries deprecated.[X] Ticket EM-7: Implement Vectorized .sanitizeName
Helper
.sanitizeName
to R/utils-internal.R
(e.g., make.names(..., unique=TRUE)
). Store original names as attribute for lossless round-tripping.[X] Ticket EM-8: Replace design_matrix.event_term
design_matrix.event_term
with the more direct 25-line version proposed in the feedback, removing the locenv
detour.design_matrix.event_term
function in R/event_vector.R
(or R/event-term.R
if created).[X] Ticket EM-9: Update hrf()
for Contrasts
hrf()
.hrf()
in R/hrf.R
with updated validation. Emit clear error with hint if numeric contrast is passed.hrf()
only accepts symbolic contrasts with clear error message.Phase 3: Update Contrast System
[X] Ticket EM-10: Update Contrast Weights Functions to use rlang
contrast_weights.*
in R/contrast.R
:lazyeval
calls with rlang
equivalents (is_formula
, f_rhs
, eval_tidy
).where
clause evaluation.lazyeval
dependency removed.[X] Ticket EM-11: Update makeWeights
Helper
makeWeights
in R/contrast.R
with .make_weights
using logical masks and returning named numeric vector. Update callsites..make_weights
implemented and used.[X] Ticket EM-12: Clean up Fcontrasts.event_term
design_matrix.event_term
.Fcontrasts.event_term
to remove SVD/QR fallback logic.Phase 4: Cleanup and Verification
[ ] Ticket EM-13: Update Tests for event_model
event_model
pipeline and verify backward compatibility.tests/testthat/test_event_model.R
:event_model()
implicitly covering pipeline stages (formula/list inputs, multiple runs, HRFs, interactions, modulators, expressions, contrasts).event_model
object structure (term_spans
, col_indices
attrs).contrast_weights.event_model
works with new attributes.design_matrix(event_model)
comparison.[ ] Ticket EM-14: Remove Old Code / Deprecate
event_model.list
, .formula
, create_event_model
(handled in EM-6).R/event_model.R
: construct_model
(old), .pre_eval_hrf_calls
, extract_terms
, extract_variables
, parse_term
, .extract_expr_variables_into_events
.[ ] Ticket EM-15: Update Documentation
event_model()
.a_04_event_models.Rmd
, Overview.Rmd
, a_05_contrasts.Rmd
) re: API changes (symbolic contrasts only) and add note about cross-term contrasts being future work.[ ] Ticket EM-16: Final Namespace/Import Check
rlang
, tibble
, purrr
, cli
, future.apply
(optional)) present. Remove unused (lazyeval
). Check exports/internal markers. Check R CMD check
.[ ] Ticket EM-17: Add Performance Benchmark Script
inst/benchmarks/
(or similar) comparing old vs. new event_model
design matrix build time on a representative synthetic dataset. Potentially add assertion for CI.[ ] Ticket EM-19: Add Error Check for Empty Factors
construct_event_term
(or earlier) raises informative error if a factor has 0 events after subsetting.[ ] Ticket EM-20: Implement Cross-Term Contrasts (Post-Refactor)
event_model
.contrast_weights.contrast_formula_spec_event_model
(or potentially adapt contrast_weights.contrast_formula_spec
to detect context).TermName$ConditionName
syntax (e.g., ~ Term1$Face - Term2$Scene
).term_spans
and/or col_indices
attributes stored on the event model's design matrix to identify the correct columns corresponding to TermName$ConditionName
.TermName$ConditionName
is mapped to a logical mask or index vector for the relevant columns in the full design matrix..make_weights
helper.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.