Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Download data from OSF for testing #45

Merged
merged 18 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
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
26 changes: 8 additions & 18 deletions aroma/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from nilearn import image, masking
from nilearn._utils import load_niimg

from . import utils
from aroma import utils

LGR = logging.getLogger(__name__)

Expand Down Expand Up @@ -79,23 +79,14 @@ def feature_time_series(mel_mix, mc, metric_metadata=None):

# Determine the derivatives of the RPs (add zeros at time-point zero)
_, nparams = rp6.shape
rp6_der = np.vstack((
np.zeros(nparams),
np.diff(rp6, axis=0)
))
rp6_der = np.vstack((np.zeros(nparams), np.diff(rp6, axis=0)))

# Create an RP-model including the RPs and its derivatives
rp12 = np.hstack((rp6, rp6_der))

# add the fw and bw shifted versions
rp12_1fw = np.vstack((
np.zeros(2 * nparams),
rp12[:-1]
))
rp12_1bw = np.vstack((
rp12[1:],
np.zeros(2 * nparams)
))
rp12_1fw = np.vstack((np.zeros(2 * nparams), rp12[:-1]))
rp12_1bw = np.vstack((rp12[1:], np.zeros(2 * nparams)))
rp_model = np.hstack((rp12, rp12_1fw, rp12_1bw))

# Determine the maximum correlation between RPs and IC time-series
Expand All @@ -109,9 +100,7 @@ def feature_time_series(mel_mix, mc, metric_metadata=None):
for i in range(nsplits):
# Select a random subset of 90% of the dataset rows
# (*without* replacement)
chosen_rows = np.random.choice(a=range(nmixrows),
size=nrows_to_choose,
replace=False)
chosen_rows = np.random.choice(a=range(nmixrows), size=nrows_to_choose, replace=False)

# Combined correlations between RP and IC time-series, squared and
# non squared
Expand Down Expand Up @@ -293,8 +282,9 @@ def feature_spatial(mel_IC, metric_metadata=None):
tot_sum = np.sum(temp_IC_data)

if tot_sum == 0:
LGR.info("\t- The spatial map of component {} is empty. "
"Please check!".format(i + 1))
LGR.info(
"\t- The spatial map of component {} is empty. " "Please check!".format(i + 1)
)

# Get sum of Z-values of the voxels located within the CSF
# (calculate via the mean and number of non-zero voxels)
Expand Down
114 changes: 79 additions & 35 deletions aroma/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os.path as op
import os
import ssl
from urllib.request import urlretrieve

import aroma
import numpy as np
import pytest
from nilearn.datasets import fetch_development_fmri

Expand All @@ -20,83 +21,126 @@ def skip_integration(request):
return request.config.getoption("--skipintegration")


def fetch_file(osf_id, path, filename):
"""
Fetches file located on OSF and downloads to `path`/`filename`1

Parameters
----------
osf_id : str
Unique OSF ID for file to be downloaded. Will be inserted into relevant
location in URL: https://osf.io/{osf_id}/download
path : str
Path to which `filename` should be downloaded. Ideally a temporary
directory
filename : str
Name of file to be downloaded (does not necessarily have to match name
of file on OSF)

Returns
-------
full_path : str
Full path to downloaded `filename`
"""
# This restores the same behavior as before.
# this three lines make tests dowloads work in windows
if os.name == "nt":
orig_sslsocket_init = ssl.SSLSocket.__init__
ssl.SSLSocket.__init__ = (
lambda *args, cert_reqs=ssl.CERT_NONE, **kwargs: orig_sslsocket_init(
*args, cert_reqs=ssl.CERT_NONE, **kwargs
)
)
ssl._create_default_https_context = ssl._create_unverified_context
url = "https://osf.io/{}/download".format(osf_id)
full_path = os.path.join(path, filename)
if not os.path.isfile(full_path):
urlretrieve(url, full_path)
return full_path


@pytest.fixture(scope="session")
def testpath(tmp_path_factory):
"""Test path that will be used to download all files."""
return tmp_path_factory.mktemp(basename='data', numbered=False)
return tmp_path_factory.mktemp(basename="data", numbered=False)


@pytest.fixture
def base_test_path():
return op.join(op.dirname(aroma.__file__), 'tests', 'data')
def nilearn_data(testpath):
return fetch_development_fmri(n_subjects=1, age_group="adult", data_dir=str(testpath))


# Feature outputs generated with the following command (adding breakpoints to save results)
# python2 ICA_AROMA.py -o out -i
# sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz
# -mc mc.tsv -tr 2 -np


@pytest.fixture
def featurespath(base_test_path):
return op.join(base_test_path, 'features_test')
def mel_FT_mix(testpath):
return fetch_file("ezhfg", testpath, "melodic_FTmix")


@pytest.fixture
def nilearn_data(testpath):
return fetch_development_fmri(n_subjects=1, age_group="adult",
data_dir=str(testpath))
def mel_mix(testpath):
return fetch_file("69j2h", testpath, "melodic_mix")

# Feature outputs generated with the following command (adding breakpoints to save results)
# python2 ICA_AROMA.py -o out -i
# sub-pixar123_task-pixar_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz
# -mc mc.tsv -tr 2 -np

@pytest.fixture
def mc(testpath):
return fetch_file("af275", testpath, "mc_fmriprep.tsv")


@pytest.fixture
def mel_FT_mix(featurespath):
return np.loadtxt(op.join(featurespath, 'melodic_FTmix'))
def mel_IC(testpath):
return fetch_file("bw3mq", testpath, "melodic_IC_thr_MNI2mm.nii.gz")


@pytest.fixture
def mel_mix(featurespath):
return np.loadtxt(op.join(featurespath, 'melodic_mix'))
def csfFract(testpath):
return fetch_file("d298c", testpath, "csfFract.npy")


@pytest.fixture
def mc(featurespath):
return op.join(featurespath, 'mc.txt')
def edgeFract(testpath):
return fetch_file("vxdu6", testpath, "edgeFract.npy")


@pytest.fixture
def mel_IC(featurespath):
return op.join(featurespath, 'melodic_IC_thr_MNI2mm.nii.gz')
def max_correls(testpath):
return fetch_file("hpd2m", testpath, "max_correls.npy")


@pytest.fixture
def csfFract(featurespath):
return op.join(featurespath, 'csfFract.npy')
def HFC(testpath):
return fetch_file("rymwg", testpath, "HFC.npy")


@pytest.fixture
def edgeFract(featurespath):
return op.join(featurespath, 'edgeFract.npy')
def classification_overview(testpath):
return fetch_file("2hv3a", testpath, "classification_overview.txt")


@pytest.fixture
def max_correls(featurespath):
return op.join(featurespath, 'max_correls.npy')
def classified_motion_ICs(testpath):
return fetch_file("jnhgc", testpath, "AROMAnoiseICs.csv")


@pytest.fixture
def HFC(featurespath):
return op.join(featurespath, 'HFC.npy')
def feature_scores(testpath):
return fetch_file("cxwfk", testpath, "feature_scores.txt")


@pytest.fixture
def motion_parameters(featurespath):
def motion_parameters(testpath):
"""Motion parameter outputs in different formats.

All outputs manually converted from FSL version.
"""
files = {
"FSL": op.join(featurespath, "mc_fsl.txt"),
"AfNI": op.join(featurespath, "mc_afni.1D"),
"SPM": op.join(featurespath, "rp_mc_spm.txt"),
"fMRIPrep": op.join(featurespath, "mc_fmriprep.tsv"),
"FSL": fetch_file("ahtrv", testpath, "mc_fsl.txt"),
"AfNI": fetch_file("p6ybt", testpath, "mc_afni.1D"),
"SPM": fetch_file("ct6q4", testpath, "rp_mc_spm.txt"),
"fMRIPrep": fetch_file("af275", testpath, "mc_fmriprep.tsv"),
}
return files
1 change: 0 additions & 1 deletion aroma/tests/data/AROMAnoiseICs.csv

This file was deleted.

45 changes: 0 additions & 45 deletions aroma/tests/data/classification_overview.txt

This file was deleted.

1 change: 0 additions & 1 deletion aroma/tests/data/eigenvalues_percent

This file was deleted.

45 changes: 0 additions & 45 deletions aroma/tests/data/feature_scores.txt

This file was deleted.

Binary file removed aroma/tests/data/features_test/HFC.npy
Binary file not shown.
Binary file removed aroma/tests/data/features_test/csfFract.npy
Binary file not shown.
Binary file removed aroma/tests/data/features_test/edgeFract.npy
Binary file not shown.
Binary file removed aroma/tests/data/features_test/max_correls.npy
Binary file not shown.
Loading