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

[REF] Modularize metric calculation #591

Merged
merged 27 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ three-echo:
four-echo:
@py.test --log-cli-level=INFO --cov-append --cov-report term-missing --cov=tedana -k test_integration_four_echo tedana/tests/test_integration.py

four-echo:
@py.test --cov-append --cov-report term-missing --cov=tedana -k test_integration_four_echo tedana/tests/test_integration.py

five-echo:
@py.test --log-cli-level=INFO --cov-append --cov-report term-missing --cov=tedana -k test_integration_five_echo tedana/tests/test_integration.py

Expand Down
7 changes: 3 additions & 4 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,10 @@ API

.. autosummary::
:toctree: generated/
:template: function.rst

tedana.metrics.dependence_metrics
tedana.metrics.kundu_metrics
:template: module.rst

tedana.metrics.collect
tedana.metrics.dependence

.. _api_selection_ref:

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@

# https://github.com/rtfd/sphinx_rtd_theme/issues/117
def setup(app):
app.add_stylesheet('theme_overrides.css')
app.add_javascript("https://cdn.rawgit.com/chrisfilo/zenodo.js/v0.1/zenodo.js")
app.add_css_file('theme_overrides.css')
app.add_js_file("https://cdn.rawgit.com/chrisfilo/zenodo.js/v0.1/zenodo.js")


html_favicon = '_static/tedana_favicon.png'
Expand Down
90 changes: 90 additions & 0 deletions docs/zenodo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copied from
// https://cdn.rawgit.com/chrisfilo/zenodo.js/v0.1/zenodo.js
var getContent = function(url, acceptType, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
if (acceptType == 'application/json') {
xhr.responseType = 'json';
}
xhr.setRequestHeader('Accept', acceptType);
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};

String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

function getZenodoIDFromTag(conceptRecID, tagName, callback) {
getContent('https://zenodo.org/api/records/?q=conceptrecid:' + conceptRecID + '%20AND%20related.identifier:*github*' + tagName + '&all_versions&sort=-version',
'application/json',
function(err, data) {
if (err !== null) {
callback(err, null);
} else {
if (data.length == 0) {
callback('No records found for this tag and Zenodo ID', null);
} else if (data.length > 1) {
callback('Ambiguous numnber of records (more than one) found for this tag and Zenodo ID', null);
} else {
targetID = data[0].id
callback(null, targetID);
}
}
});
}

function getLatestIDFromconceptID(conceptRecID, callback) {
getContent('https://zenodo.org/api/records/' + conceptRecID,
'application/json',
function(err, data) {
if (err !== null) {
callback(err, null);
} else {
targetID = data.id
callback(null, targetID);
}
});
}

function getCitation(recordID, style, callback) {
style = typeof style !== 'undefined' ? style : 'vancouver-brackets-no-et-al';
getContent('https://www.zenodo.org/api/records/' + recordID + '?style=' + style, 'text/x-bibliography',
function(err, data) {
if (err !== null) {
callback(err, null);
} else {
callback(null, data);
}
});
}

function getDOI(recordID, callback) {
getContent('https://www.zenodo.org/api/records/' + recordID, 'application/json',
function(err, data) {
if (err !== null) {
callback(err, null);
} else {
var DOI = data.doi;
callback(null, DOI);
}
});
}

function getBIBTEX(recordID, callback) {
getContent('https://www.zenodo.org/api/records/' + recordID, 'text/x-bibtex',
function(err, data) {
if (err !== null) {
callback(err, null);
} else {
callback(null, data);
}
});
}
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ parentdir_prefix =
[flake8]
max-line-length = 99
exclude=*build/
ignore = E126,E402,W504
ignore = E126,E402,W503,W504
per-file-ignores =
*/__init__.py:F401

Expand Down
11 changes: 10 additions & 1 deletion tedana/decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
Functions to estimate S0 and T2* from multi-echo data.
"""
import logging
import scipy
import numpy as np
import scipy
from scipy import stats

from tedana import utils

LGR = logging.getLogger(__name__)
Expand Down Expand Up @@ -376,6 +378,13 @@ def fit_decay(data, tes, mask, adaptive_mask, fittype, report=True):
t2s_full = utils.unmask(t2s_full, mask)
s0_full = utils.unmask(s0_full, mask)

# set a hard cap for the T2* map
# anything that is 10x higher than the 99.5 %ile will be reset to 99.5 %ile
cap_t2s = stats.scoreatpercentile(t2s_limited.flatten(), 99.5,
interpolation_method='lower')
LGR.debug('Setting cap on T2* map at {:.5f}'.format(cap_t2s * 10))
t2s_limited[t2s_limited > cap_t2s * 10] = cap_t2s

return t2s_limited, s0_limited, t2s_full, s0_full


Expand Down
26 changes: 11 additions & 15 deletions tedana/decomposition/pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,16 @@ def tedpca(data_cat, data_oc, combmode, mask, adaptive_mask, t2sG,
varex_norm = varex / varex.sum()

# Compute Kappa and Rho for PCA comps
# Normalize each component's time series
vTmixN = stats.zscore(comp_ts, axis=0)
comptable, _, metric_metadata, _, _ = metrics.dependence_metrics(
data_cat, data_oc, comp_ts, adaptive_mask, tes, io_generator,
reindex=False, mmixN=vTmixN, algorithm=None,
label='PCA', verbose=verbose
required_metrics = [
'kappa', 'rho', 'countnoise', 'countsigFT2', 'countsigFS0',
'dice_FT2', 'dice_FS0', 'signal-noise_t',
'variance explained', 'normalized variance explained',
'd_table_score'
]
comptable, _ = metrics.collect.generate_metrics(
data_cat, data_oc, comp_ts, adaptive_mask,
tes, io_generator, 'PCA',
metrics=required_metrics, sort_by=None
)

# varex_norm from PCA overrides varex_norm from dependence_metrics,
Expand All @@ -249,7 +253,6 @@ def tedpca(data_cat, data_oc, combmode, mask, adaptive_mask, t2sG,
if algorithm == 'kundu':
comptable, metric_metadata = kundu_tedpca(
comptable,
metric_metadata,
n_echos,
kdaw,
rdaw,
Expand All @@ -258,7 +261,6 @@ def tedpca(data_cat, data_oc, combmode, mask, adaptive_mask, t2sG,
elif algorithm == 'kundu-stabilize':
comptable, metric_metadata = kundu_tedpca(
comptable,
metric_metadata,
n_echos,
kdaw,
rdaw,
Expand All @@ -282,13 +284,7 @@ def tedpca(data_cat, data_oc, combmode, mask, adaptive_mask, t2sG,
temp_comptable = comptable.set_index("Component", inplace=False)
io_generator.save_file(temp_comptable, "PCA metrics tsv")

metric_metadata["Component"] = {
"LongName": "Component identifier",
"Description": (
"The unique identifier of each component. "
"This identifier matches column names in the mixing matrix TSV file."
),
}
metric_metadata = metrics.collect.get_metadata(temp_comptable)
io_generator.save_file(metric_metadata, "PCA metrics json")

decomp_metadata = {
Expand Down
6 changes: 5 additions & 1 deletion tedana/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class OutputGenerator():
This will correspond to a "figures" subfolder of ``out_dir``.
prefix : str
Prefix to prepend to output filenames.
verbose : bool
Whether or not to generate verbose output
"""

def __init__(
Expand All @@ -68,7 +70,8 @@ def __init__(
out_dir=".",
prefix="",
config="auto",
make_figures=True
make_figures=True,
verbose=False,
):

if config == "auto":
Expand All @@ -94,6 +97,7 @@ def __init__(
self.out_dir = op.abspath(out_dir)
self.figures_dir = op.join(out_dir, "figures")
self.prefix = prefix + "_" if prefix != "" else ""
self.verbose = verbose

if not op.isdir(self.out_dir):
LGR.info(f"Generating output directory: {self.out_dir}")
Expand Down
9 changes: 3 additions & 6 deletions tedana/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# emacs: -*- mode: python-mode; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
# ex: set sts=4 ts=4 sw=4 et:
"""TE-dependence and TE-independence metrics."""

from .kundu_fit import (
dependence_metrics, kundu_metrics
)
from .collect import generate_metrics

__all__ = [
'dependence_metrics', 'kundu_metrics'
]
__all__ = ["generate_metrics"]
Loading