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

[WIP, ENH] Add GODEC #120

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4eda775
Add godec.
tsalo Aug 26, 2018
0f6eb2f
Fix integration test workflow call.
tsalo Aug 26, 2018
6c4abc3
Fix potential bug in T1c and GODEC.
tsalo Aug 28, 2018
60ce2f3
Remove mean from High-Kappa time series.
tsalo Aug 28, 2018
b7119ea
Fix mmix shape bug.
tsalo Aug 28, 2018
dba5c24
Improve gscontrol_mmix docstring.
tsalo Aug 28, 2018
b1851a0
Incorporate "out_dir" variable into workflow and functions.
tsalo Aug 31, 2018
e72af8f
Fix test (ish)
tsalo Aug 31, 2018
a63a00e
Now *really* fix tests. Well, one test.
tsalo Aug 31, 2018
b27dff3
Fix t2smap and associated tests. Update CLI.
tsalo Aug 31, 2018
21b5d98
Refactor tedana workflow. Make more readable.
tsalo Aug 31, 2018
60da7f3
Fix import issue.
tsalo Sep 1, 2018
6910f5c
Merge remote-tracking branch 'ME-ICA/master' into godec
tsalo Sep 3, 2018
c6cfc88
Revert changes to gscontrol_mmix.
tsalo Sep 9, 2018
9fe4123
And... fix output directory after reverting changes..
tsalo Sep 9, 2018
a540da1
Merge branch 'master' into godec
tsalo Sep 18, 2018
cf44965
Fix style issue.
tsalo Sep 18, 2018
5bdfe9b
Merge remote-tracking branch 'ME-ICA/master' into godec
tsalo Nov 28, 2018
68d59ff
Merge remote-tracking branch 'ME-ICA/master' into godec
tsalo Nov 28, 2018
b69ff09
Undo changes to gitignore.
tsalo Nov 28, 2018
d838a4a
Fix.
tsalo Nov 28, 2018
92c2c8c
Revert out_dir changes.
tsalo Nov 28, 2018
08d4fb9
Revert more.
tsalo Nov 28, 2018
bb04f4c
Fix style problem.
tsalo Nov 28, 2018
9a0ff51
Merge remote-tracking branch 'ME-ICA/master' into godec
tsalo Feb 15, 2019
8b03fbb
Fix style issues.
tsalo Feb 15, 2019
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ ENV/
.mypy_cache/

# vscode
.vscode
.vscode
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ API

tedana.decomposition.tedpca
tedana.decomposition.tedica
tedana.decomposition.tedgodec

:template: module.rst

Expand Down
6 changes: 5 additions & 1 deletion tedana/decomposition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from .eigendecomp import (
tedpca, tedica,
)
from .godecomp import (
tedgodec
)


__all__ = [
'tedpca', 'tedica']
'tedpca', 'tedica', 'tedgodec',
]
184 changes: 184 additions & 0 deletions tedana/decomposition/godecomp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
"""
Go Decomposition
"""
import logging
import os.path as op

import numpy as np
from numpy.linalg import qr, lstsq

from tedana import utils
from tedana.decomposition._utils import dwtmat, idwtmat

LGR = logging.getLogger(__name__)


def _wthresh(a, thresh):
"""
Soft wavelet threshold
"""
res = np.abs(a) - thresh
return np.sign(a) * ((res > 0) * res)


def godec(data, thresh=.03, rank=2, power=1, tol=1e-3, max_iter=100,
random_seed=0, verbose=True):
"""
Perform Go Decomposition

Default threshold of .03 is assumed to be for input in the range 0-1...
original matlab had 8 out of 255, which is about .03 scaled to 0-1 range

Parameters
----------
data : (M x T) array_like

Returns
-------
L : array_like
Low-rank components. Similar to global signals. Should be discarded,
according to Power et al. (2018).
S : array_like
Sparse components. Should be retained, according to Power et al.
(2018).
G : array_like
Residuals (i.e., data minus sparse and low-rank components)
"""
LGR.info('Starting Go Decomposition')
_, n_vols = data.shape
L = data
S = np.zeros(L.shape)
itr = 0
random_state = np.random.RandomState(random_seed)
while True:
Y2 = random_state.randn(n_vols, rank)
for i in range(power + 1):
Y1 = np.dot(L, Y2)
Y2 = np.dot(L.T, Y1)
Q, R = qr(Y2)
L_new = np.dot(np.dot(L, Q), Q.T)
T = L - L_new + S
L = L_new
S = _wthresh(T, thresh)
T -= S
err = np.linalg.norm(T.ravel(), 2)
if err < tol:
if verbose:
LGR.info('Successful convergence after %i iterations', itr + 1)
break
elif itr >= max_iter:
if verbose:
LGR.warning('Model failed to converge after %i iterations',
itr + 1)
break
L += T
itr += 1

# Is this even useful in soft GoDec? May be a display issue...
G = data - L - S
return L, S, G


def _tedgodec(data, wavelet=False, rank=2, power=2, tol=1e-3,
thresh=10, max_iter=500, norm_mode='vn', random_seed=0,
verbose=True):
"""
Perform TE-dependent Go Decomposition

Parameters
----------
data : (M x T) array_like
wavelet : :obj:`bool`, optional

"""
if norm_mode == 'dm':
# Demean
data_mean = data.mean(-1)
data_norm = data - data_mean[:, np.newaxis]
elif norm_mode == 'vn':
# Variance normalize
data_mean = data.mean(-1)[:, np.newaxis]
data_std = data.std(-1)[:, np.newaxis]
data_norm = (data - data_mean) / data_std
else:
data_norm = data

# GoDec
if wavelet:
data_wt, cal = dwtmat(data_norm)
L, S, G = godec(data_wt, thresh=(data_wt.std() * thresh), rank=rank,
power=power, tol=tol, max_iter=max_iter,
random_seed=random_seed, verbose=verbose)
L = idwtmat(L, cal)
S = idwtmat(S, cal)
G = idwtmat(G, cal)
else:
L, S, G = godec(data_norm, thresh=thresh, rank=rank,
power=power, tol=tol, max_iter=max_iter,
random_seed=random_seed, verbose=verbose)

if norm_mode == 'dm':
# Remean
L += data_mean
elif norm_mode == 'vn':
L = (L * data_std) + data_mean
S *= data_std
G *= data_std

return L, S, G


def tedgodec(optcom_ts, mmix, mask, acc, ign, ref_img, ranks=[2],
wavelet=False, thresh=10, norm_mode='vn', power=2, out_dir='.'):
"""
optcom_ts : (S x T) array_like
Optimally combined time series data
mmix : (C x T) array_like
Mixing matrix for converting input data to component space, where `C`
is components and `T` is the same as in `optcom_ts`
mask : (S,) array_like
Boolean mask array
acc : :obj:`list`
Indices of accepted (BOLD) components in `mmix`
ign : :obj:`list`
Indices of all ignored components in `mmix`
ref_img : :obj:`str` or img_like
Reference image to dictate how outputs are saved to disk
ranks : list of int
Ranks of low-rank components to run
norm_mode : {'vn', 'dm', None}
"""
# Construct denoised data from optcom, mmix, acc, and all_ref
optcom_masked = optcom_ts[mask, :]
optcom_mu = optcom_masked.mean(axis=-1)[:, np.newaxis]
optcom_std = optcom_masked.std(axis=-1)[:, np.newaxis]
data_norm = (optcom_masked - optcom_mu) / optcom_std
cbetas = lstsq(mmix, data_norm.T, rcond=None)[0].T
all_comps = np.arange(mmix.shape[0])
not_ign = sorted(np.setdiff1d(all_comps, ign))
resid = data_norm - np.dot(cbetas[:, not_ign], mmix[:, not_ign].T)
bold_ts = np.dot(cbetas[:, acc], mmix[:, acc].T)
medn_ts = optcom_mu + ((bold_ts + resid) * optcom_std)

for rank in ranks:
L, S, G = _tedgodec(medn_ts, rank=rank, power=power, thresh=thresh,
max_iter=500, norm_mode=norm_mode)

if norm_mode is None:
name_norm_mode = ''
else:
name_norm_mode = 'n{0}'.format(norm_mode)

if wavelet:
name_norm_mode = 'w{0}'.format(name_norm_mode)

suffix = '{0}r{1}p{2}t{3}'.format(name_norm_mode, rank, power, thresh)
utils.filewrite(utils.unmask(L, mask),
op.join(out_dir, 'lowrank_{0}.nii'.format(suffix)),
ref_img)
utils.filewrite(utils.unmask(S, mask),
op.join(out_dir, 'sparse_{0}.nii'.format(suffix)),
ref_img)
utils.filewrite(utils.unmask(G, mask),
op.join(out_dir, 'noise_{0}.nii'.format(suffix)),
ref_img)
14 changes: 9 additions & 5 deletions tedana/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

def gscontrol_mmix(optcom_ts, mmix, mask, comptable, ref_img):
"""
Perform global signal regression.
Perform minimum image regression to identify and remove global artifacts.
This is an alternative to methods like GoDec with fewer parameters to tune.

Parameters
----------
Expand Down Expand Up @@ -186,10 +187,13 @@ def write_split_ts(data, mmix, mask, acc, rej, midk, ref_img, suffix=''):
====================== =================================================
Filename Content
====================== =================================================
hik_ts_[suffix].nii High-Kappa time series.
hik_ts_[suffix].nii High-Kappa time series. Combination of accepted
components.
midk_ts_[suffix].nii Mid-Kappa time series.
low_ts_[suffix].nii Low-Kappa time series.
dn_ts_[suffix].nii Denoised time series.
low_ts_[suffix].nii Low-Kappa time series. Combination of rejected
components.
dn_ts_[suffix].nii Denoised time series. Optimally combined data
minus mid-Kappa and rejected components.
====================== =================================================
"""

Expand Down Expand Up @@ -332,7 +336,7 @@ def writeresults(ts, mask, comptable, mmix, n_vols, fixed_seed,
"""

fout = filewrite(ts, 'ts_OC', ref_img)
LGR.info('Writing optimally-combined time series: {}'.format(op.abspath(fout)))
LGR.info('Writing optimally combined time series: {}'.format(op.abspath(fout)))

write_split_ts(ts, mmix, mask, acc, rej, midk, ref_img, suffix='OC')

Expand Down
15 changes: 14 additions & 1 deletion tedana/model/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def gscontrol_raw(catd, optcom, n_echos, ref_img, dtrank=4):
Removes global signal from individual echo `catd` and `optcom` time series

This function uses the spatial global signal estimation approach to
to removal global signal out of individual echo time series datasets. The
to remove global signal out of individual echo time series datasets. The
spatial global signal is estimated from the optimally combined data after
detrending with a Legendre polynomial basis of `order = 0` and
`degree = dtrank`.
Expand All @@ -438,6 +438,19 @@ def gscontrol_raw(catd, optcom, n_echos, ref_img, dtrank=4):
Input `catd` with global signal removed from time series
dm_optcom : (S x T) array_like
Input `optcom` with global signal removed from time series

Notes
-----
This function writes out several files. Files are listed below:

====================== =================================================
Filename Content
====================== =================================================
T1gs.nii Spatial global signal
tsoc_orig.nii Optimally combined data
tsoc_nogs.nii Optimally combined data with global signal
regressed out
====================== =================================================
"""
LGR.info('Applying amplitude-based T1 equilibration correction')
if catd.shape[0] != optcom.shape[0]:
Expand Down
8 changes: 7 additions & 1 deletion tedana/workflows/tedana.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def tedana_workflow(data, tes, mask=None, mixm=None, ctab=None, manacc=None,
tedort : :obj:`bool`, optional
Orthogonalize rejected components w.r.t. accepted ones prior to
denoising. Default is False.
gscontrol : {None, 't1c', 'gsr'} or :obj:`list`, optional
gscontrol : {None, 't1c', 'gsr', 'godec'} or :obj:`list`, optional
Perform additional denoising to remove spatially diffuse noise. Default
is None.
tedpca : {'mle', 'kundu', 'kundu-stabilize'}, optional
Expand Down Expand Up @@ -382,10 +382,16 @@ def tedana_workflow(data, tes, mask=None, mixm=None, ctab=None, manacc=None,
acc=acc, rej=rej, midk=midk, empty=ign,
ref_img=ref_img)

# Widespread noise control
if 't1c' in gscontrol:
LGR.info('Performing T1c global signal regression to remove spatially '
'diffuse noise')
io.gscontrol_mmix(data_oc, mmix, mask, comptable, ref_img)
elif 'godec' in gscontrol:
decomposition.tedgodec(data_oc, mmix, mask, acc, ign, ref_img,
ranks=[2], wavelet=wvpca,
thresh=10, norm_mode='vn', power=2,
out_dir=out_dir)

if verbose:
io.writeresults_echoes(catd, mmix, mask, acc, rej, midk, ref_img)
Expand Down