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

[ENH] BIDS Derivatives-compatible outputs #574

Merged
merged 34 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
21e479c
BIDS Derivatives-compatible outputs.
tsalo May 22, 2020
9dd2728
Fix outputs.
tsalo May 22, 2020
41c9512
Merge branch 'master' into ref/filenames-and-variables
tsalo Jul 13, 2020
66b0a0c
Fix filenames.
tsalo Jul 14, 2020
000997e
Merge branch 'master' into ref/filenames-and-variables
tsalo Sep 22, 2020
eb23f74
Merge branch 'master' into ref/filenames-and-variables
tsalo Oct 22, 2020
737b19c
Reformat MIR function with black.
tsalo Oct 22, 2020
0b82189
Update fiu_four_echo_outputs.txt
tsalo Oct 22, 2020
d00b03a
Merge branch 'main' into ref/filenames-and-variables
tsalo Feb 6, 2021
51c7a7b
Reorganize metric outputs.
tsalo Feb 6, 2021
65784d8
Write out PCA outputs.
tsalo Feb 7, 2021
7f88281
Fix tests.
tsalo Feb 7, 2021
141c1b1
Fix up gscontrol.
tsalo Feb 7, 2021
b6a375c
Fix row indexing.
tsalo Feb 7, 2021
06bd83f
Fix suffix.
tsalo Feb 7, 2021
de0526f
Simplify handling of component indices.
tsalo Feb 7, 2021
b5b69a1
Fix.
tsalo Feb 7, 2021
840b1c0
Fix the reports.
tsalo Feb 7, 2021
7e70a7b
Fix the bugs.
tsalo Feb 7, 2021
860bff3
Fix more bugs.
tsalo Feb 9, 2021
e587849
Fix everything!
tsalo Feb 9, 2021
bb15c2e
Fix the loading?
tsalo Feb 9, 2021
4303b97
Update fiu_four_echo_outputs.txt
tsalo Feb 9, 2021
1aa52bc
Fix outputs!
tsalo Feb 9, 2021
2b1524f
Ugh...
tsalo Feb 10, 2021
6b4ab31
Add dataset_description.json.
tsalo Feb 10, 2021
ed45069
Update outputs and workflow description.
tsalo Feb 10, 2021
f7a4103
Fix output list.
tsalo Feb 10, 2021
fb7e8f8
Fix more outputs.
tsalo Feb 10, 2021
39a18f7
Remove unused functions.
tsalo Feb 10, 2021
245c7bd
Fix imports.
tsalo Feb 10, 2021
ed85699
Change desc-ICA to desc-tedana.
tsalo Feb 12, 2021
cd1ce51
Update docs.
tsalo Feb 12, 2021
cc9296b
Update docs/outputs.rst
tsalo Feb 23, 2021
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
210 changes: 111 additions & 99 deletions docs/outputs.rst

Large diffs are not rendered by default.

25 changes: 12 additions & 13 deletions tedana/decomposition/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ def tedpca(data_cat, data_oc, combmode, mask, adaptive_mask, t2sG,

This function writes out several files:

====================== =================================================
Filename Content
====================== =================================================
pca_decomposition.json PCA component table.
pca_mixing.tsv PCA mixing matrix.
pca_components.nii.gz Component weight maps.
====================== =================================================
=========================== =============================================
Filename Content
=========================== =============================================
desc-PCA_decomposition.json PCA component table
desc-PCA_mixing.tsv PCA mixing matrix
desc-PCA_components.nii.gz Component weight maps
=========================== =============================================
"""
if algorithm == 'kundu':
alg_str = ("followed by the Kundu component selection decision "
Expand Down Expand Up @@ -209,7 +209,7 @@ def tedpca(data_cat, data_oc, combmode, mask, adaptive_mask, t2sG,
comptable, _, _, _ = metrics.dependence_metrics(
data_cat, data_oc, comp_ts, adaptive_mask, tes, ref_img,
reindex=False, mmixN=vTmixN, algorithm=None,
label='mepca_', out_dir=out_dir, verbose=verbose)
label='PCA', out_dir=out_dir, verbose=verbose)

# varex_norm from PCA overrides varex_norm from dependence_metrics,
# but we retain the original
Expand All @@ -218,9 +218,8 @@ def tedpca(data_cat, data_oc, combmode, mask, adaptive_mask, t2sG,
comptable['normalized variance explained'] = varex_norm

# write component maps to 4D image
comp_ts_z = stats.zscore(comp_ts, axis=0)
jbteves marked this conversation as resolved.
Show resolved Hide resolved
comp_maps = utils.unmask(computefeats2(data_oc, comp_ts_z, mask), mask)
io.filewrite(comp_maps, op.join(out_dir, 'pca_components.nii.gz'), ref_img)
comp_maps = utils.unmask(computefeats2(data_oc, comp_ts, mask), mask)
io.filewrite(comp_maps, op.join(out_dir, 'desc-PCA_components.nii.gz'), ref_img)
tsalo marked this conversation as resolved.
Show resolved Hide resolved

# Select components using decision tree
if algorithm == 'kundu':
Expand All @@ -238,7 +237,7 @@ def tedpca(data_cat, data_oc, combmode, mask, adaptive_mask, t2sG,
for comp in comptable.index.values]

mixing_df = pd.DataFrame(data=comp_ts, columns=comp_names)
mixing_df.to_csv(op.join(out_dir, 'pca_mixing.tsv'), sep='\t', index=False)
mixing_df.to_csv(op.join(out_dir, 'desc-PCA_mixing.tsv'), sep='\t', index=False)

comptable['Description'] = 'PCA fit to optimally combined data.'
mmix_dict = {}
Expand All @@ -247,7 +246,7 @@ def tedpca(data_cat, data_oc, combmode, mask, adaptive_mask, t2sG,
'explained in descending order. '
'Component signs are flipped to best match the '
'data.')
io.save_comptable(comptable, op.join(out_dir, 'pca_decomposition.json'),
io.save_comptable(comptable, op.join(out_dir, 'desc-PCA_decomposition.json'),
label='pca', metadata=mmix_dict)

acc = comptable[comptable.classification == 'accepted'].index.values
Expand Down
57 changes: 46 additions & 11 deletions tedana/gscontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
from numpy.linalg import lstsq
import pandas as pd
from scipy import stats
from scipy.special import lpmv

Expand Down Expand Up @@ -78,23 +79,38 @@ def gscontrol_raw(catd, optcom, n_echos, ref_img, out_dir='.', dtrank=4):
detr = dat - np.dot(sol.T, Lmix.T)[0]
sphis = (detr).min(axis=1)
sphis -= sphis.mean()
io.filewrite(utils.unmask(sphis, Gmask), op.join(out_dir, 'T1gs'), ref_img)
io.filewrite(
utils.unmask(sphis, Gmask),
op.join(out_dir, 'T1gs.nii.gz'),
tsalo marked this conversation as resolved.
Show resolved Hide resolved
ref_img
)

# find time course ofc the spatial global signal
# make basis with the Legendre basis
glsig = np.linalg.lstsq(np.atleast_2d(sphis).T, dat, rcond=None)[0]
glsig = stats.zscore(glsig, axis=None)
np.savetxt(op.join(out_dir, 'glsig.1D'), glsig)

glsig_df = pd.DataFrame(data=glsig.T, columns=['global_signal'])
glsig_df.to_csv(op.join(out_dir, 'desc-globalSignal_regressors.tsv'),
sep='\t', index=False)
tsalo marked this conversation as resolved.
Show resolved Hide resolved
glbase = np.hstack([Lmix, glsig.T])

# Project global signal out of optimally combined data
sol = np.linalg.lstsq(np.atleast_2d(glbase), dat.T, rcond=None)[0]
tsoc_nogs = dat - np.dot(np.atleast_2d(sol[dtrank]).T,
np.atleast_2d(glbase.T[dtrank])) + Gmu[Gmask][:, np.newaxis]

io.filewrite(optcom, op.join(out_dir, 'tsoc_orig'), ref_img)
io.filewrite(
optcom,
op.join(out_dir, 'desc-optcomWithGlobalSignal_bold.nii.gz'),
ref_img
)
dm_optcom = utils.unmask(tsoc_nogs, Gmask)
io.filewrite(dm_optcom, op.join(out_dir, 'tsoc_nogs'), ref_img)
io.filewrite(
dm_optcom,
op.join(out_dir, 'desc-optcomNoGlobalSignal_bold.nii.gz'),
ref_img
)

# Project glbase out of each echo
dm_catd = catd.copy() # don't overwrite catd
Expand Down Expand Up @@ -170,7 +186,11 @@ def gscontrol_mmix(optcom_ts, mmix, mask, comptable, ref_img, out_dir='.'):
bold_ts = np.dot(cbetas[:, acc], mmix[:, acc].T)
t1_map = bold_ts.min(axis=-1)
t1_map -= t1_map.mean()
io.filewrite(utils.unmask(t1_map, mask), op.join(out_dir, 'sphis_hik'), ref_img)
io.filewrite(
utils.unmask(t1_map, mask),
op.join(out_dir, 'desc-optcomAccepted_min.nii.gz'),
ref_img
)
t1_map = t1_map[:, np.newaxis]

"""
Expand All @@ -184,14 +204,21 @@ def gscontrol_mmix(optcom_ts, mmix, mask, comptable, ref_img, out_dir='.'):
bold_noT1gs = bold_ts - np.dot(lstsq(glob_sig.T, bold_ts.T,
rcond=None)[0].T, glob_sig)
hik_ts = bold_noT1gs * optcom_std
io.filewrite(utils.unmask(hik_ts, mask), op.join(out_dir, 'hik_ts_OC_T1c'),
ref_img)
io.filewrite(
utils.unmask(hik_ts, mask),
op.join(out_dir, 'desc-optcomAcceptedT1cDenoised_bold.nii.gz'),
ref_img
)

"""
Make denoised version of T1-corrected time series
"""
medn_ts = optcom_mu + ((bold_noT1gs + resid) * optcom_std)
io.filewrite(utils.unmask(medn_ts, mask), op.join(out_dir, 'dn_ts_OC_T1c'), ref_img)
io.filewrite(
utils.unmask(medn_ts, mask),
op.join(out_dir, 'desc-optcomT1cDenoised_bold.nii.gz'),
ref_img
)

"""
Orthogonalize mixing matrix w.r.t. T1-GS
Expand All @@ -208,6 +235,14 @@ def gscontrol_mmix(optcom_ts, mmix, mask, comptable, ref_img, out_dir='.'):
Write T1-GS corrected components and mixing matrix
"""
cbetas_norm = lstsq(mmixnogs_norm.T, data_norm.T, rcond=None)[0].T
io.filewrite(utils.unmask(cbetas_norm[:, 2:], mask),
op.join(out_dir, 'betas_hik_OC_T1c'), ref_img)
np.savetxt(op.join(out_dir, 'meica_mix_T1c.1D'), mmixnogs)
io.filewrite(
utils.unmask(cbetas_norm[:, 2:], mask),
op.join(out_dir, 'desc-TEDICAAcceptedT1cDenoised_components.nii.gz'),
ref_img
)
comp_names = [io.add_decomp_prefix(comp, prefix='ica',
max_value=comptable.index.max())
for comp in comptable.index.values]
mixing_df = pd.DataFrame(data=mmixnogs.T, columns=comp_names)
mixing_df.to_csv(op.join(out_dir, 'desc-TEDICAT1cDenoised_mixing.tsv'),
sep='\t', index=False)
Loading