Skip to content

Commit

Permalink
WIP: clean and standardize M for compcor
Browse files Browse the repository at this point in the history
* remove columns without variation
* remove NaN or inf
  • Loading branch information
stnava committed Jan 21, 2024
1 parent aa29ee8 commit 5e088db
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ants/utils/quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,32 @@ def butter_bandpass_filter(data, lowcut, highcut, fs, order ):
matrix[:,k], lowf, highf, fs, order=order )
return matrixOut

def clean_data(arr, standardize=False):
"""
Remove columns from a NumPy array that have no variation or contain NA/Inf values.
Optionally standardize the remaining data.
:param arr: NumPy array to be cleaned.
:param standardize: Boolean, if True standardize the data.
:return: Cleaned (and optionally standardized) NumPy array.
"""
valid_columns = []

for i in range(arr.shape[1]):
column = arr[:, i]
if np.any(column != column[0]) and not np.any(np.isnan(column)) and not np.any(np.isinf(column)):
valid_columns.append(i)

cleaned_data = arr[:, valid_columns]

if standardize:
mean = np.mean(cleaned_data, axis=0)
std_dev = np.std(cleaned_data, axis=0)
# Avoid division by zero in case of zero standard deviation
std_dev[std_dev == 0] = 1
cleaned_data = (cleaned_data - mean) / std_dev

return cleaned_data

def compcor( boldImage, ncompcor=4, quantile=0.975, mask=None, filter_type=False, degree=2 ):
"""
Expand Down Expand Up @@ -406,6 +432,7 @@ def compute_tSTD(M, quantile, x=0, axis=0):
# M = M / compute_tSTD(M, 1.)['tSTD']
# "The covariance matrix C = MMT was constructed and decomposed into its
# principal components using a singular value decomposition."
M = clean_data( M )
u, _, _ = linalg.svd(M, full_matrices=False)
if components is None:
components = u[:, :ncompcor]
Expand Down

0 comments on commit 5e088db

Please sign in to comment.