Skip to content

Commit

Permalink
Rename variables (ME-ICA#1145)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo authored Nov 20, 2024
1 parent 6438dfa commit 7041e95
Show file tree
Hide file tree
Showing 22 changed files with 429 additions and 404 deletions.
36 changes: 17 additions & 19 deletions tedana/decomposition/ica.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def tedica(
Returns
-------
mmix : (T x C) :obj:`numpy.ndarray`
mixing : (T x C) :obj:`numpy.ndarray`
Z-scored mixing matrix for converting input data to component space,
where `C` is components and `T` is the same as in `data`
fixed_seed : :obj:`int`
Expand All @@ -69,15 +69,15 @@ def tedica(
ica_method = ica_method.lower()

if ica_method == "robustica":
mmix, fixed_seed = r_ica(
mixing, fixed_seed = r_ica(
data,
n_components=n_components,
fixed_seed=fixed_seed,
n_robust_runs=n_robust_runs,
max_it=maxit,
)
elif ica_method == "fastica":
mmix, fixed_seed = f_ica(
mixing, fixed_seed = f_ica(
data,
n_components=n_components,
fixed_seed=fixed_seed,
Expand All @@ -87,7 +87,7 @@ def tedica(
else:
raise ValueError("The selected ICA method is invalid!")

return mmix, fixed_seed
return mixing, fixed_seed


def r_ica(data, n_components, fixed_seed, n_robust_runs, max_it):
Expand All @@ -109,7 +109,7 @@ def r_ica(data, n_components, fixed_seed, n_robust_runs, max_it):
Returns
-------
mmix : (T x C) :obj:`numpy.ndarray`
mixing : (T x C) :obj:`numpy.ndarray`
Z-scored mixing matrix for converting input data to component space,
where `C` is components and `T` is the same as in `data`
fixed_seed : :obj:`int`
Expand Down Expand Up @@ -140,7 +140,7 @@ def r_ica(data, n_components, fixed_seed, n_robust_runs, max_it):
robust_method=robust_method,
)

s, mmix = robust_ica.fit_transform(data)
s, mixing = robust_ica.fit_transform(data)
q = robust_ica.evaluate_clustering(
robust_ica.S_all,
robust_ica.clustering.labels_,
Expand All @@ -151,7 +151,7 @@ def r_ica(data, n_components, fixed_seed, n_robust_runs, max_it):

except Exception:
if robust_method == "DBSCAN":
# if RobustICA failed wtih DBSCAN, run again wtih AgglomerativeClustering
# if RobustICA failed wtih DBSCAN, run again with AgglomerativeClustering
robust_method = "AgglomerativeClustering"
LGR.warning(
"DBSCAN clustering method did not converge. "
Expand All @@ -165,20 +165,18 @@ def r_ica(data, n_components, fixed_seed, n_robust_runs, max_it):
f"components across different runs"
)

iq = np.array(
np.mean(q[q["cluster_id"] >= 0].iq)
) # Excluding outliers (cluster -1) from the index quality calculation
# Excluding outliers (cluster -1) from the index quality calculation
iq = np.array(np.mean(q[q["cluster_id"] >= 0].iq))

if iq < WARN_IQ:
LGR.warning(
f"The resultant mean Index Quality is low ({iq}). It is recommended to rerun the "
"process with a different seed."
)

mmix = mmix[
:, q["cluster_id"] >= 0
] # Excluding outliers (cluster -1) when calculating the mixing matrix
mmix = stats.zscore(mmix, axis=0)
# Excluding outliers (cluster -1) when calculating the mixing matrix
mixing = mixing[:, q["cluster_id"] >= 0]
mixing = stats.zscore(mixing, axis=0)

LGR.info(
f"RobustICA with {n_robust_runs} robust runs and seed {fixed_seed} was used. "
Expand All @@ -194,7 +192,7 @@ def r_ica(data, n_components, fixed_seed, n_robust_runs, max_it):
f"decomposition."
)

return mmix, fixed_seed
return mixing, fixed_seed


def f_ica(data, n_components, fixed_seed, maxit, maxrestart):
Expand All @@ -218,7 +216,7 @@ def f_ica(data, n_components, fixed_seed, maxit, maxrestart):
Returns
-------
mmix : (T x C) :obj:`numpy.ndarray`
mixing : (T x C) :obj:`numpy.ndarray`
Z-scored mixing matrix for converting input data to component space,
where `C` is components and `T` is the same as in `data`
fixed_seed : :obj:`int`
Expand Down Expand Up @@ -262,6 +260,6 @@ def f_ica(data, n_components, fixed_seed, maxit, maxrestart):
)
break

mmix = ica.mixing_
mmix = stats.zscore(mmix, axis=0)
return mmix, fixed_seed
mixing = ica.mixing_
mixing = stats.zscore(mixing, axis=0)
return mixing, fixed_seed
41 changes: 21 additions & 20 deletions tedana/decomposition/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def low_mem_pca(data):

def tedpca(
data_cat,
data_oc,
data_optcom,
mask,
adaptive_mask,
io_generator,
Expand All @@ -66,7 +66,7 @@ def tedpca(
----------
data_cat : (S x E x T) array_like
Input functional data
data_oc : (S x T) array_like
data_optcom : (S x T) array_like
Optimally combined time series data
mask : (S,) array_like
Boolean mask array
Expand Down Expand Up @@ -203,7 +203,7 @@ def tedpca(
LGR.info(
f"Computing PCA of optimally combined multi-echo data with selection criteria: {algorithm}"
)
data = data_oc[mask, :]
data = data_optcom[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
Expand Down Expand Up @@ -349,9 +349,9 @@ def tedpca(
"d_table_score",
]
# Even if user inputted, don't fit external_regressors to PCA components
comptable, _ = metrics.collect.generate_metrics(
component_table, _ = metrics.collect.generate_metrics(
data_cat=data_cat,
data_optcom=data_oc,
data_optcom=data_optcom,
mixing=comp_ts,
adaptive_mask=adaptive_mask,
tes=tes,
Expand All @@ -363,27 +363,27 @@ def tedpca(

# varex_norm from PCA overrides varex_norm from dependence_metrics,
# but we retain the original
comptable["estimated normalized variance explained"] = comptable[
component_table["estimated normalized variance explained"] = component_table[
"normalized variance explained"
]
comptable["normalized variance explained"] = varex_norm
component_table["normalized variance explained"] = varex_norm

# write component maps to 4D image
comp_maps = utils.unmask(computefeats2(data_oc, comp_ts, mask), mask)
comp_maps = utils.unmask(computefeats2(data_optcom, comp_ts, mask), mask)
io_generator.save_file(comp_maps, "z-scored PCA components img")

# Select components using decision tree
if algorithm == "kundu":
comptable, metric_metadata = kundu_tedpca(
comptable,
component_table, metric_metadata = kundu_tedpca(
component_table,
n_echos,
kdaw,
rdaw,
stabilize=False,
)
elif algorithm == "kundu-stabilize":
comptable, metric_metadata = kundu_tedpca(
comptable,
component_table, metric_metadata = kundu_tedpca(
component_table,
n_echos,
kdaw,
rdaw,
Expand All @@ -397,25 +397,26 @@ def tedpca(
else:
alg_str = algorithm
LGR.info(
f"Selected {comptable.shape[0]} components with {round(100 * varex_norm.sum(), 2)}% "
f"Selected {component_table.shape[0]} components with "
f"{round(100 * varex_norm.sum(), 2)}% "
f"normalized variance explained using {alg_str} dimensionality estimate"
)
comptable["classification"] = "accepted"
comptable["rationale"] = ""
component_table["classification"] = "accepted"
component_table["rationale"] = ""

# Save decomposition files
comp_names = [
io.add_decomp_prefix(comp, prefix="pca", max_value=comptable.index.max())
for comp in comptable.index.values
io.add_decomp_prefix(comp, prefix="pca", max_value=component_table.index.max())
for comp in component_table.index.values
]

mixing_df = pd.DataFrame(data=comp_ts, columns=comp_names)
io_generator.save_file(mixing_df, "PCA mixing tsv")

# Save component table and associated json
io_generator.save_file(comptable, "PCA metrics tsv")
io_generator.save_file(component_table, "PCA metrics tsv")

metric_metadata = metrics.collect.get_metadata(comptable)
metric_metadata = metrics.collect.get_metadata(component_table)
io_generator.save_file(metric_metadata, "PCA metrics json")

decomp_metadata = {
Expand All @@ -431,7 +432,7 @@ def tedpca(
}
io_generator.save_file(decomp_metadata, "PCA decomposition json")

acc = comptable[comptable.classification == "accepted"].index.values
acc = component_table[component_table.classification == "accepted"].index.values
n_components = acc.size
voxel_kept_comp_weighted = voxel_comp_weights[:, acc] * varex[None, acc]
kept_data = np.dot(voxel_kept_comp_weighted, comp_ts[:, acc].T)
Expand Down
14 changes: 7 additions & 7 deletions tedana/gscontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def minimum_image_regression(
data_optcom: np.ndarray,
mixing: np.ndarray,
mask: np.ndarray,
comptable: pd.DataFrame,
component_table: pd.DataFrame,
classification_tags: list,
io_generator: io.OutputGenerator,
):
Expand All @@ -163,7 +163,7 @@ def minimum_image_regression(
is components and ``T`` is the same as in ``data_optcom``
mask : (S,) array_like
Boolean mask array
comptable : (C x X) :obj:`pandas.DataFrame`
component_table : (C x X) :obj:`pandas.DataFrame`
Component metric table. One row for each component, with a column for
each metric. The index should be the component number.
classification_tags : :obj:`list` of :obj:`str`
Expand Down Expand Up @@ -210,7 +210,7 @@ def minimum_image_regression(
"diffuse noise \\citep{kundu2013integrated}."
)

all_comps = comptable.index.values
all_comps = component_table.index.values
# Get accepted and ignored components
# Tedana has removed the "ignored" classification,
# so we must separate "accepted" components based on the classification tag(s).
Expand All @@ -225,11 +225,11 @@ def minimum_image_regression(
pattern = "|".join(ignore_tags) # Create a pattern that matches any of the ignore tags

# Select rows where the 'classification_tags' column contains any of the ignore tags
ign = comptable[
comptable.classification_tags.str.contains(pattern, na=False, regex=True)
ign = component_table[
component_table.classification_tags.str.contains(pattern, na=False, regex=True)
].index.values

acc = comptable[comptable.classification == "accepted"].index.values
acc = component_table[component_table.classification == "accepted"].index.values
# Ignored components are classified as "accepted", so we need to remove them from the list
acc = sorted(np.setdiff1d(acc, ign))
not_ign = sorted(np.setdiff1d(all_comps, ign))
Expand Down Expand Up @@ -273,7 +273,7 @@ def minimum_image_regression(
mixing_not1gs_z = np.vstack((np.atleast_2d(np.ones(max(gs_ts.shape))), gs_ts, mixing_not1gs_z))

# Write T1-corrected components and mixing matrix
mixing_df = pd.DataFrame(data=mixing_not1gs.T, columns=comptable["Component"].values)
mixing_df = pd.DataFrame(data=mixing_not1gs.T, columns=component_table["Component"].values)
io_generator.save_file(mixing_df, "ICA MIR mixing tsv")

if io_generator.verbose:
Expand Down
Loading

0 comments on commit 7041e95

Please sign in to comment.