data-raw/naming_refactoring.md

Design Matrix Naming Refactoring Plan

This document outlines the plan to refactor the design matrix column naming scheme within the fmrireg package. The goal is to establish a consistent, deterministic, and unambiguous grammar for column names generated from event models.

1. Naming Grammar Specification

1.1 Building Blocks (Atomic Tokens)

1.2 Condition Tag (condition_tag)

1.3 Term Tag (term_tag)

1.4 Final Column Name Structure

  1. General Case: term_tag + _ + condition_tag + [_b## HRF suffix]

  2. Special Case (Ident-only, no id): (term_tag is NULL) condition_tag + [_b## HRF suffix] (Where condition_tag is the raw variable name, e.g., RT1)

  3. This single canonical structure (with the Ident exception) replaces previous compact/qualified styles.

  4. Construction happens in make_column_names, called by convolve.event_term.

1.4.1 Examples of Final Column Names

1.5 Guarantees

2. Migration Plan Summary

A step-by-step refactoring process targeting specific functions in order:

  1. Prep Work: Create R/naming-utils.R with core helper functions.
  2. hrf() / hrfspec(): Ensure hrfspec$id is only set if explicitly provided by user.
  3. realise_event_terms(): Generate and store term_tag (potentially NULL for Ident-only) via make_term_tag. Attach term_tag attribute to event_term objects.
  4. levels.ParametricBasis methods: Ensure levels.Poly, levels.BSpline, etc. (when called on a basis object, typically via columns(event_object) -> columns.event in the naming pipeline) return only zero-padded indices. Ensure levels.Ident returns raw variable names. (Dedicated columns.BasisType S3 methods should also align if used directly elsewhere).
  5. conditions.event_term(): Rewrite to assemble condition_tags using tokens from level_token and (indirectly, as described above) levels.ParametricBasis methods, handling interactions.
  6. convolve.event_term(): Simplify. Use make_column_names() helper, passing the term_tag attribute (which might be NULL) and base condition names. This is the sole place final proposed names for a term are constructed.
  7. Event term construction (event_term()): Internal legacy column naming logic for basis functions within event_term objects (if any existed beyond what design_matrix and convolve.event_term now handle) is removed/superseded. The primary naming construction occurs in later stages.
  8. build_event_model_design_matrix(): Apply make.names(..., unique=TRUE) to the combined column names before returning the final matrix.
  9. Tests & Docs: Update tests, documentation, and examples to reflect the final naming scheme.

3. expand_basis Flag Handling

4. naming-utils.R Specification

Defines helper functions. Key decisions: Exports: Only sanitize, zeropad, basis_suffix are exported. Internal Helpers: make_unique_tags, make_term_tag, level_token, continuous_token, make_cond_tag, add_basis, make_column_names marked internal (@keywords internal). Padding: zeropad used for both HRF basis suffix (_b##) and generative basis indices (01, 02). Sanitization: sanitize(allow_dot=FALSE) used for term tags; sanitize(allow_dot=TRUE) generally used elsewhere. Basis Condition Tags: levels.Poly, levels.BSpline (called via columns(event_object_with_basis) in the naming pipeline) return only the zero-padded index (e.g., "01") as the condition_tag component. Info about basis type/variable captured in term_tag. Ident Handling: make_term_tag returns NULL for hrf(Ident(V1,V2,...)) without explicit id=. make_column_names detects this and produces final column names directly from the Ident variable names (V1, V2), plus any HRF basis suffix. * Global Uniqueness: Guaranteed by build_event_model_design_matrix applying make.names(..., unique=TRUE).



bbuchsbaum/fmrireg documentation built on June 10, 2025, 8:18 p.m.