Skip to content

Commit

Permalink
[FIX] Eliminate duplicate lines in logs (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo committed Jan 15, 2021
1 parent 4ecdf4b commit 63070e2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 34 deletions.
52 changes: 30 additions & 22 deletions tedana/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@due.dcite(Doi('10.1002/(SICI)1522-2594(199907)42:1<87::AID-MRM13>3.0.CO;2-O'),
description='T2* method of combining data across echoes using '
'monoexponential equation.')
def _combine_t2s(data, tes, ft2s):
def _combine_t2s(data, tes, ft2s, report=True):
"""
Combine data across echoes using weighted averaging according to voxel-
(and sometimes volume-) wise estimates of T2*.
Expand All @@ -27,6 +27,8 @@ def _combine_t2s(data, tes, ft2s):
Echo times in milliseconds.
ft2s : (M [x T] X 1) array_like
Either voxel-wise or voxel- and volume-wise estimates of T2*.
report : bool, optional
Whether to log a description of this step or not. Default is True.
Returns
-------
Expand All @@ -42,14 +44,15 @@ def _combine_t2s(data, tes, ft2s):
Medicine: An Official Journal of the International Society
for Magnetic Resonance in Medicine, 42(1), 87-97.
"""
RepLGR.info("Multi-echo data were then optimally combined using the "
"T2* combination method (Posse et al., 1999).")
RefLGR.info("Posse, S., Wiese, S., Gembris, D., Mathiak, K., Kessler, "
"C., Grosse‐Ruyken, M. L., ... & Kiselev, V. G. (1999). "
"Enhancement of BOLD‐contrast sensitivity by single‐shot "
"multi‐echo functional MR imaging. Magnetic Resonance in "
"Medicine: An Official Journal of the International Society "
"for Magnetic Resonance in Medicine, 42(1), 87-97.")
if report:
RepLGR.info("Multi-echo data were then optimally combined using the "
"T2* combination method (Posse et al., 1999).")
RefLGR.info("Posse, S., Wiese, S., Gembris, D., Mathiak, K., Kessler, "
"C., Grosse‐Ruyken, M. L., ... & Kiselev, V. G. (1999). "
"Enhancement of BOLD‐contrast sensitivity by single‐shot "
"multi‐echo functional MR imaging. Magnetic Resonance in "
"Medicine: An Official Journal of the International Society "
"for Magnetic Resonance in Medicine, 42(1), 87-97.")
n_vols = data.shape[-1]
alpha = tes * np.exp(-tes / ft2s)
if alpha.ndim == 2:
Expand All @@ -71,7 +74,7 @@ def _combine_t2s(data, tes, ft2s):
@due.dcite(Doi('10.1002/mrm.20900'),
description='PAID method of combining data across echoes using just '
'SNR/signal and TE.')
def _combine_paid(data, tes):
def _combine_paid(data, tes, report=True):
"""
Combine data across echoes using SNR/signal and TE via the
parallel-acquired inhomogeneity desensitized (PAID) ME-fMRI combination
Expand All @@ -83,6 +86,8 @@ def _combine_paid(data, tes):
Masked data.
tes : (1 x E) array_like
Echo times in milliseconds.
report : bool, optional
Whether to log a description of this step or not. Default is True.
Returns
-------
Expand All @@ -99,16 +104,17 @@ def _combine_paid(data, tes):
International Society for Magnetic Resonance in Medicine,
55(6), 1227-1235.
"""
RepLGR.info("Multi-echo data were then optimally combined using the "
"parallel-acquired inhomogeneity desensitized (PAID) "
"combination method.")
RefLGR.info("Poser, B. A., Versluis, M. J., Hoogduin, J. M., & Norris, "
"D. G. (2006). BOLD contrast sensitivity enhancement and "
"artifact reduction with multiecho EPI: parallel‐acquired "
"inhomogeneity‐desensitized fMRI. "
"Magnetic Resonance in Medicine: An Official Journal of the "
"International Society for Magnetic Resonance in Medicine, "
"55(6), 1227-1235.")
if report:
RepLGR.info("Multi-echo data were then optimally combined using the "
"parallel-acquired inhomogeneity desensitized (PAID) "
"combination method.")
RefLGR.info("Poser, B. A., Versluis, M. J., Hoogduin, J. M., & Norris, "
"D. G. (2006). BOLD contrast sensitivity enhancement and "
"artifact reduction with multiecho EPI: parallel‐acquired "
"inhomogeneity‐desensitized fMRI. "
"Magnetic Resonance in Medicine: An Official Journal of the "
"International Society for Magnetic Resonance in Medicine, "
"55(6), 1227-1235.")
n_vols = data.shape[-1]
snr = data.mean(axis=-1) / data.std(axis=-1)
alpha = snr * tes
Expand Down Expand Up @@ -215,17 +221,19 @@ def make_optcom(data, tes, adaptive_mask, t2s=None, combmode='t2s', verbose=True
data = data[mask, :, :] # mask out unstable voxels/samples
tes = np.array(tes)[np.newaxis, ...] # (1 x E) array_like
combined = np.zeros((data.shape[0], data.shape[2]))
report = True
for echo in np.unique(adaptive_mask[mask]):
echo_idx = adaptive_mask[mask] == echo

if combmode == 'paid':
combined[echo_idx, :] = _combine_paid(data[echo_idx, :echo, :],
tes[:echo])
tes[:echo], report=report)
else:
t2s_ = t2s[mask, ..., np.newaxis] # mask out empty voxels/samples

combined[echo_idx, :] = _combine_t2s(
data[echo_idx, :echo, :], tes[:, :echo], t2s_[echo_idx, ...])
data[echo_idx, :echo, :], tes[:, :echo], t2s_[echo_idx, ...], report=report)
report = False

combined = unmask(combined, mask)
return combined
31 changes: 19 additions & 12 deletions tedana/decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def monoexponential(tes, s0, t2star):
return s0 * np.exp(-tes / t2star)


def fit_monoexponential(data_cat, echo_times, adaptive_mask):
def fit_monoexponential(data_cat, echo_times, adaptive_mask, report=True):
"""
Fit monoexponential decay model with nonlinear curve-fitting.
Expand All @@ -74,6 +74,8 @@ def fit_monoexponential(data_cat, echo_times, adaptive_mask):
adaptive_mask : (S,) :obj:`numpy.ndarray`
Array where each value indicates the number of echoes with good signal
for that voxel.
report : bool, optional
Whether to log a description of this step or not. Default is True.
Returns
-------
Expand All @@ -84,13 +86,14 @@ def fit_monoexponential(data_cat, echo_times, adaptive_mask):
-----
This method is slower, but more accurate, than the log-linear approach.
"""
RepLGR.info("A monoexponential model was fit to the data at each voxel "
"using nonlinear model fitting in order to estimate T2* and S0 "
"maps, using T2*/S0 estimates from a log-linear fit as "
"initial values. For each voxel, the value from the adaptive "
"mask was used to determine which echoes would be used to "
"estimate T2* and S0. In cases of model fit failure, T2*/S0 "
"estimates from the log-linear fit were retained instead.")
if report:
RepLGR.info("A monoexponential model was fit to the data at each voxel "
"using nonlinear model fitting in order to estimate T2* and S0 "
"maps, using T2*/S0 estimates from a log-linear fit as "
"initial values. For each voxel, the value from the adaptive "
"mask was used to determine which echoes would be used to "
"estimate T2* and S0. In cases of model fit failure, T2*/S0 "
"estimates from the log-linear fit were retained instead.")
n_samp, n_echos, n_vols = data_cat.shape

# Currently unused
Expand Down Expand Up @@ -255,7 +258,7 @@ def fit_loglinear(data_cat, echo_times, adaptive_mask, report=True):
return t2s_limited, s0_limited, t2s_full, s0_full


def fit_decay(data, tes, mask, adaptive_mask, fittype):
def fit_decay(data, tes, mask, adaptive_mask, fittype, report=True):
"""
Fit voxel-wise monoexponential decay models to `data`
Expand All @@ -274,6 +277,8 @@ def fit_decay(data, tes, mask, adaptive_mask, fittype):
given sample
fittype : {loglin, curvefit}
The type of model fit to use
report : bool, optional
Whether to log a description of this step or not. Default is True.
Returns
-------
Expand Down Expand Up @@ -318,10 +323,10 @@ def fit_decay(data, tes, mask, adaptive_mask, fittype):

if fittype == 'loglin':
t2s_limited, s0_limited, t2s_full, s0_full = fit_loglinear(
data_masked, tes, adaptive_mask_masked)
data_masked, tes, adaptive_mask_masked, report=report)
elif fittype == 'curvefit':
t2s_limited, s0_limited, t2s_full, s0_full = fit_monoexponential(
data_masked, tes, adaptive_mask_masked)
data_masked, tes, adaptive_mask_masked, report=report)
else:
raise ValueError('Unknown fittype option: {}'.format(fittype))

Expand Down Expand Up @@ -388,12 +393,14 @@ def fit_decay_ts(data, tes, mask, adaptive_mask, fittype):
t2s_full_ts = np.copy(t2s_limited_ts)
s0_full_ts = np.copy(t2s_limited_ts)

report = True
for vol in range(n_vols):
t2s_limited, s0_limited, t2s_full, s0_full = fit_decay(
data[:, :, vol][:, :, None], tes, mask, adaptive_mask, fittype)
data[:, :, vol][:, :, None], tes, mask, adaptive_mask, fittype, report=report)
t2s_limited_ts[:, vol] = t2s_limited
s0_limited_ts[:, vol] = s0_limited
t2s_full_ts[:, vol] = t2s_full
s0_full_ts[:, vol] = s0_full
report = False

return t2s_limited_ts, s0_limited_ts, t2s_full_ts, s0_full_ts

0 comments on commit 63070e2

Please sign in to comment.