Skip to content

Conversation

@synchon
Copy link
Member

@synchon synchon commented Mar 11, 2025

Which marker do you want to include?

  1. Compute Cross-Correlation:

For two time series X(t) and Y(t) from different brain regions:
CCF(τ)=∑tX(t)Y(t+τ)
CCF(τ)=t∑​X(t)Y(t+τ)

where τ represents a time lag (positive or negative shifts).

Compute cross-correlation for each ROI pair across a range of time lags τ (e.g., -10s to +10s).
The peak of the cross-correlation function indicates the optimal time lag (e.g., if peak at τ = 2s, Y lags behind X by 2s).

  1. Construct a Lag-Based Functional Connectivity Matrix:

Identify the lag at which cross-correlation is maximal.
Store the peak correlation value and associated lag for each ROI pair.
Construct:

  • A functional connectivity matrix (correlation values).
  • A lag matrix (time lags at which correlation is maximal).

Is there any publication or available code?

https://pmc.ncbi.nlm.nih.gov/articles/PMC4097876

Do you have a sample code that implements this outside of junifer?

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import correlate

# Simulated fMRI data (example: 10 ROIs, 300 time points)
np.random.seed(42)
n_rois = 10
n_timepoints = 300
fmri_data = np.random.rand(n_rois, n_timepoints)  # Replace with real fMRI signals

# Define time lag range (e.g., -10 to +10 time points)
max_lag = 10
lags = np.arange(-max_lag, max_lag + 1)

# Initialize matrices
fc_matrix = np.zeros((n_rois, n_rois))  # Functional connectivity matrix
lag_matrix = np.zeros((n_rois, n_rois))  # Lag matrix

# Compute cross-correlation for each ROI pair
for i in range(n_rois):
    for j in range(n_rois):
        if i != j:
            # Compute cross-correlation
            ccf = correlate(fmri_data[i], fmri_data[j], mode='full', method='auto')
            ccf = ccf[len(ccf)//2 - max_lag : len(ccf)//2 + max_lag + 1]  # Limit lag range
            
            # Find peak correlation & corresponding lag
            peak_idx = np.argmax(np.abs(ccf))  # Peak correlation index
            peak_corr = ccf[peak_idx]  # Peak correlation value
            peak_lag = lags[peak_idx]  # Corresponding lag
            
            # Store in matrices
            fc_matrix[i, j] = peak_corr
            lag_matrix[i, j] = peak_lag

# Plot Functional Connectivity Matrix
plt.figure(figsize=(8,6))
plt.imshow(fc_matrix, cmap='coolwarm', aspect='auto')
plt.colorbar(label="Peak Cross-Correlation")
plt.title("Functional Connectivity Matrix")
plt.xlabel("ROI")
plt.ylabel("ROI")
plt.show()

# Plot Lag Matrix
plt.figure(figsize=(8,6))
plt.imshow(lag_matrix, cmap='jet', aspect='auto')
plt.colorbar(label="Lag (Time Points)")
plt.title("Lag Matrix")
plt.xlabel("ROI")
plt.ylabel("ROI")
plt.show()

Anything else to say?

No response

@synchon synchon added the marker Issues or pull requests related to markers label Mar 10, 2025
@synchon synchon added this to the 0.0.6 (alpha 5) milestone Mar 10, 2025
@synchon synchon self-assigned this Mar 10, 2025
@synchon synchon changed the title [MARKER]: Support FunctionalConnectivityLagged [MARKER]: Support FunctionalConnectivityLagged Mar 10, 2025
@codecov
Copy link

codecov bot commented Mar 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.09%. Comparing base (e8a99eb) to head (6aed916).
⚠️ Report is 231 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #435   +/-   ##
=======================================
  Coverage   91.09%   91.09%           
=======================================
  Files         134      134           
  Lines        5422     5422           
  Branches      903      903           
=======================================
  Hits         4939     4939           
  Misses        309      309           
  Partials      174      174           
Flag Coverage Δ
docs 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
junifer/pipeline/pipeline_component_registry.py 94.64% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link

github-actions bot commented Mar 11, 2025

PR Preview Action v1.6.1

🚀 View preview at
https://juaml.github.io/junifer/pr-preview/pr-435/

Built to branch gh-pages at 2025-05-09 12:45 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@synchon synchon requested review from fraimondo and kaurao March 11, 2025 14:06
@synchon synchon force-pushed the feat/functional-connectivity-lagged branch from eb418ae to 69c8204 Compare March 14, 2025 09:53
@synchon synchon removed request for fraimondo and kaurao March 14, 2025 15:29
@synchon synchon marked this pull request as draft March 14, 2025 15:32
@synchon synchon force-pushed the feat/functional-connectivity-lagged branch 2 times, most recently from ca90c06 to 0ee82bf Compare March 18, 2025 09:34
@synchon synchon force-pushed the feat/functional-connectivity-lagged branch from 0ee82bf to b1a9645 Compare May 8, 2025 08:08
@synchon synchon force-pushed the feat/functional-connectivity-lagged branch from b1a9645 to 6aed916 Compare May 9, 2025 12:06
@github-actions github-actions bot added the Stale label Sep 17, 2025
@github-actions github-actions bot removed the Stale label Sep 19, 2025
@github-actions github-actions bot added the Stale label Oct 19, 2025
@synchon synchon removed the Stale label Oct 20, 2025
@github-actions github-actions bot added the Stale label Nov 20, 2025
@synchon synchon removed the Stale label Nov 24, 2025
@github-actions github-actions bot added Stale and removed Stale labels Dec 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

marker Issues or pull requests related to markers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants