data-raw/Manifold_Guided-HRF_proposal.md

This is a superb set of final clarifications and an excellent "API polish" for the LSS component. The decisions made are pragmatic, robust, and maintain the core elegance and efficiency of the manifold HRF approach.

Here's a full proposal incorporating these latest refinements, structured for an engineering team.

Proposal: Manifold-Guided HRF Estimation and Trial-wise Deconvolution (M-HRF-LSS)

1. Executive Summary:

This proposal outlines an advanced fMRI analysis pipeline, "Manifold-Guided HRF Estimation and Trial-wise Deconvolution (M-HRF-LSS)." It combines a novel HRF estimation technique based on manifold learning with a highly optimized Least Squares Separate (LSS) kernel for single-trial beta estimation. The M-HRF-LSS pipeline aims to provide: 1. Spatially smooth and physiologically plausible voxel-wise HRF estimates. 2. Associated per-condition activation amplitudes. 3. Accurate per-trial activation amplitudes. All components are designed for computational efficiency, primarily relying on closed-form solutions, BLAS operations, and sparse matrix algebra, making it suitable for large-scale whole-brain analyses on standard CPU hardware.

2. Core Components & Algorithmic Workflow:

Component 0: HRF Manifold Construction (Once per Study/HRF Library)

Component 1: Voxel-wise HRF Manifold Coordinate & Condition Amplitude Estimation

Component 2: Spatial Smoothing of Manifold Coordinates

Component 3: Trial-wise Amplitude Estimation (LSS using Smoothed Manifold HRFs)

6. Outputs of M-HRF-LSS Pipeline:

7. Engineering Considerations:

This detailed proposal provides a comprehensive engineering blueprint for the M-HRF-LSS pipeline, incorporating all the excellent insights and refinements discussed.

Okay, let's break down the implementation of the "Manifold-Guided HRF Estimation and Trial-wise Deconvolution (M-HRF-LSS)" pipeline into a granular sprint plan with actionable tickets.

Sprint Plan: M-HRF-LSS Pipeline Implementation (MVP)

Sprint Goal: Deliver a functional R implementation of the M-HRF-LSS pipeline (Components 0-3), capable of processing moderately sized fMRI datasets, with a focus on algorithmic correctness and modularity. CPU-based, BLAS-optimized R code is the target.

User Stories (Guiding the Sprint): As a methods developer, I need a robust implementation of each M-HRF-LSS component to enable research and validation. As an fMRI researcher (early adopter), I want to run the M-HRF-LSS pipeline on a sample dataset to obtain spatially smoothed HRFs, condition betas, and trial betas.

Tickets:

Epic 0: HRF Manifold Construction (Component 0 - Once per Study/Library)

  1. MHRF-MANIFOLD-01: Implement HRF Library Affinity & Markov Matrix Construction

    • Task: Create a function calculate_manifold_affinity(L_library, k_local_nn_for_sigma = 7, sparse_if_N_gt = 5000, k_nn_for_W_sparse = 20)
      • Input: L_library (p x N HRF shapes).
      • Compute pairwise Euclidean distances.
      • Implement self-tuning bandwidth σ_i, σ_j (using k_local_nn_for_sigma).
      • Compute affinity matrix W_ij = exp(-dists_ij² / (σ_i * σ_j)).
      • (Optional) If N > sparse_if_N_gt, implement k-NN sparsification for W.
      • Compute Markov Matrix S = D_inv %*% W.
    • DoD: Function returns S (and optionally W, D_inv). Tested with small L_library.
  2. MHRF-MANIFOLD-02: Implement Diffusion Map Eigendecomposition & Reconstructor B

    • Task: Create a function get_manifold_basis_and_reconstructor(S_markov, L_library, m_manifold_dim)
      • Input: S_markov (N x N), L_library (p x N), m_manifold_dim.
      • Use RSpectra::eigs_sym(S_markov, k = m_manifold_dim + 1, which = "LM") to get eigenvectors.
      • Extract manifold coordinates Φ = eig_S$vectors[, 2:(m_manifold_dim + 1)] (N x m).
      • Compute HRF reconstructor B_reconstructor = L_library %*% Φ %*% solve(crossprod(Φ) + 1e-8 * diag(m_manifold_dim)) (p x m).
    • DoD: Function returns Φ and B_reconstructor. Tested.

Epic 1: Voxel-wise Manifold Fit (Component 1 - Per Subject/Run)

  1. MHRF-VOXFIT-01: Confound Projection Module

    • Task: Create helper project_out_confounds(Y_data, X_list_data, Z_confounds_matrix)
      • Uses qr.Q(qr(Z_confounds_matrix, LAPACK=TRUE)) for Q_Z.
      • Returns projected Y_proj and X_list_proj.
    • DoD: Module tested for correct projection.
  2. MHRF-VOXFIT-02: Per-Condition Design in Manifold Basis (Z_list)

    • Task: Create helper transform_designs_to_manifold_basis(X_condition_list_proj, B_reconstructor)
      • Returns Z_list where Z_list[[c]] = X_condition_list_proj[[c]] %*% B_reconstructor.
    • DoD: Module tested.
  3. MHRF-VOXFIT-03: Main GLM Solve for Gamma_coeffs

    • Task: Implement the GLM solve part of Component 1.
      • Input: Z_list, Y_proj, lambda_gamma, orthogonal_approx_flag.
      • Form X_tilde = do.call(cbind, Z_list).
      • Form XtX_tilde_reg = crossprod(X_tilde) + lambda_gamma * diag(k*m).
        • (Later) Implement orthogonal_approx_flag modification to XtX_tilde_reg.
      • Solve for Gamma_coeffs.
    • DoD: Gamma_coeffs ((km) x V) computed correctly.
  4. MHRF-VOXFIT-04: SVD-based Extraction of ξ_raw and β_raw

    • Task: Implement the per-voxel SVD loop from Component 1, Step 4.
      • Input: Gamma_coeffs, m_manifold_dim, k_conditions.
      • Output: Xi_raw_allvox (m x V), Beta_raw_allvox (k x V).
      • Include robustness for near-zero singular values.
    • DoD: Raw Xi and Beta extracted.
  5. MHRF-VOXFIT-05: Intrinsic Identifiability for ξ and β

    • Task: Implement Component 1, Step 5.
      • Input: Xi_raw_allvox, Beta_raw_allvox, B_reconstructor, h_ref_shape_canonical.
      • Compute xi_ref_coord = MASS::ginv(B_reconstructor) %*% h_ref_shape_canonical.
      • Apply sign and scale adjustments to Xi_raw_allvox and Beta_raw_allvox per voxel.
    • DoD: Identifiability constraints correctly applied. Output Xi_ident_allvox, Beta_ident_allvox.

Epic 2: Spatial Smoothing (Component 2 - Per Subject/Run)

  1. MHRF-SP SMOOTH-01: Graph Laplacian Construction

    • Task: Create helper make_voxel_graph_laplacian(voxel_coordinates_matrix, num_neighbors = 6)
      • Input: V x 3 matrix of voxel coordinates.
      • Constructs a sparse V x V graph Laplacian (e.g., based on k-NN or distance threshold).
    • DoD: Sparse L_sp matrix generated.
  2. MHRF-SP SMOOTH-02: Apply Spatial Smoothing to ξ Coordinates

    • Task: Implement Component 2, Step 2.
      • Input: Xi_ident_allvox (from MHRF-VOXFIT-05), voxel_graph_laplacian_Lsp, lambda_spatial_smooth.
      • Loop m times, solving the sparse system (I_V + λ_sp L_sp) ξ_j_smooth = ξ_j_ident.
    • DoD: Xi_smoothed_allvox (m x V) computed.

Epic 3: Trial-wise LSS (Component 3 - Per Subject/Run)

  1. MHRF-LSS-01: LSS Fixed Regressor Precomputation (P_lss, p_lss_vec)

    • Task: Create helper prepare_lss_fixed_components(A_lss_fixed, intercept_column_in_Alss = NULL, lambda_ridge_Alss = 0.1)
      • Computes P_lss = (A_lss_fixedᵀA_lss_fixed + lambda_ridge_Alss * I)⁻¹A_lss_fixedᵀ.
      • Computes p_lss_vec based on A_lss_fixed and intercept_column_in_Alss.
    • DoD: P_lss and p_lss_vec returned.
  2. MHRF-LSS-02: Voxel-Specific HRF Shape Reconstruction

    • Task: Helper reconstruct_hrf_shapes(B_reconstructor, Xi_smoothed_allvox)
      • Returns H_shapes_allvox = B_reconstructor %*% Xi_smoothed_allvox (p x V).
    • DoD: Voxel-specific HRF shapes computed.
  3. MHRF-LSS-03: Implement Core Woodbury LSS Kernel (Per Voxel)

    • Task: Create internal function run_lss_for_voxel(Y_proj_vx, X_trial_onset_list, H_shape_vx, A_lss_fixed, P_lss, p_lss_vec)
      • Implements Component 3, Steps 5a-5c (forming C_vx, then Woodbury steps for S_vx, then beta_trial_vx).
    • DoD: Function returns T x 1 trial betas for one voxel. Tested.
  4. MHRF-LSS-04: Main LSS Loop and (Optional) R_t Precomputation

    • Task: Implement the main loop of Component 3.
      • Optionally precompute all R_t_allvox if memory strategy allows.
      • Loop over voxels, calling run_lss_for_voxel.
    • DoD: Beta_trial_allvox (T x V) computed.

Epic 4: Pipeline Orchestration, Output, and Documentation (MVP)

  1. MHRF-PIPELINE-01: Top-Level Orchestration Function run_mhrf_lss_pipeline()

    • Task: Create the main function that calls Components 0 (if needed, or loads precomputed B), 1, 2, and 3 in sequence.
    • Manages inputs and outputs between components.
    • DoD: Full pipeline runnable end-to-end on sample data.
  2. MHRF-OUTPUT-02: Define and Implement M-HRF-LSS Output Object

    • Task: Create an S3/S4 class to store all key outputs (Xi_raw, Beta_condition, Xi_smoothed, H_reconstructed, Beta_trial, parameters, B_reconstructor).
    • Implement basic print(), summary(), and plot() (e.g., plot mean reconstructed HRF, map of one ξ coordinate).
    • DoD: Structured output available with basic utility methods.
  3. MHRF-DOC-03: Initial User Documentation and Example Script

    • Task: Draft R documentation for the main pipeline function and output object.
    • Create a simple example script using synthetic or public data to demonstrate usage.
    • DoD: Basic documentation and a runnable example are available.

Sprint Review Focus: Successful end-to-end execution of the M-HRF-LSS pipeline on a test dataset. Plausibility of estimated HRF shapes, condition betas, and trial betas. Modularity and correctness of individual components. Initial performance benchmarks (CPU-based R).

Post-MVP Considerations (Future Sprints): Parallelization of voxel loops (parallel, future.apply, or RcppParallel). RcppArmadillo optimization for computationally intensive loops (SVD, LSS kernel). Advanced output visualization. Robust parameter selection helpers (e.g., for m_manifold_dim, lambda_spatial_smooth). * Implementation of the "Bonus Refinement" (joint spatial and manifold smoothness prior).

This granular sprint plan breaks the complex M-HRF-LSS pipeline into manageable, testable tickets, focusing on delivering a functional MVP first.



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