Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNM: Detrend optimally combined data before running PCA #1090

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions tedana/decomposition/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import pandas as pd
from mapca import MovingAveragePCA
from nilearn.signal import standardize_signal
from scipy import stats
from sklearn.decomposition import PCA

Expand Down Expand Up @@ -143,9 +144,9 @@ def tedpca(

- Nonsignificant :math:`{\kappa}` and :math:`{\rho}`.
- Nonsignificant variance explained.

Generated Files
---------------

=========================== =============================================
Default Filename Content
=========================== =============================================
Expand Down Expand Up @@ -204,9 +205,13 @@ def tedpca(
f"Computing PCA of optimally combined multi-echo data with selection criteria: {algorithm}"
)
data = data_oc[mask, :]

data_z = ((data.T - data.T.mean(axis=0)) / data.T.std(axis=0)).T # var normalize ts
data_z = (data_z - data_z.mean()) / data_z.std() # var normalize everything
if algorithm in ["mdl", "aic", "kic"]:
# Detrend the data, but don't z-score, if using MAPCA
data = standardize_signal(data.T, detrend=True, standardize=False).T
else:
# Z-score the data otherwise
data = standardize_signal(data.T, detrend=True, standardize="zscore_sample").T
data = (data - data.mean()) / data.std() # var normalize everything

if algorithm in ["mdl", "aic", "kic"]:
data_img = io.new_nii_like(io_generator.reference_img, utils.unmask(data, mask))
Expand Down Expand Up @@ -315,23 +320,23 @@ def tedpca(

elif isinstance(algorithm, Number):
ppca = PCA(copy=False, n_components=algorithm, svd_solver="full")
ppca.fit(data_z)
ppca.fit(data)
comp_ts = ppca.components_.T
varex = ppca.explained_variance_
voxel_comp_weights = np.dot(np.dot(data_z, comp_ts), np.diag(1.0 / varex))
voxel_comp_weights = np.dot(np.dot(data, comp_ts), np.diag(1.0 / varex))
varex_norm = ppca.explained_variance_ratio_
elif low_mem:
voxel_comp_weights, varex, varex_norm, comp_ts = low_mem_pca(data_z)
voxel_comp_weights, varex, varex_norm, comp_ts = low_mem_pca(data)
else:
# If algorithm is kundu or kundu-stablize component metrics
# are calculated without dimensionality estimation and
# reduction and then kundu identifies components that are
# to be accepted or rejected
ppca = PCA(copy=False, n_components=(n_vols - 1))
ppca.fit(data_z)
ppca.fit(data)
comp_ts = ppca.components_.T
varex = ppca.explained_variance_
voxel_comp_weights = np.dot(np.dot(data_z, comp_ts), np.diag(1.0 / varex))
voxel_comp_weights = np.dot(np.dot(data, comp_ts), np.diag(1.0 / varex))
varex_norm = ppca.explained_variance_ratio_

# Compute Kappa and Rho for PCA comps
Expand Down