Skip to content
This repository has been archived by the owner on Mar 7, 2022. It is now read-only.

Commit

Permalink
Add tests for mvpresults
Browse files Browse the repository at this point in the history
  • Loading branch information
lukassnoek committed Dec 30, 2016
1 parent d920092 commit 70d2d26
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
7 changes: 0 additions & 7 deletions skbold/core/tests/test_mvp_between.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,3 @@ def test_mvp_between_regress_out_confounds(mvp1c, var):
mvp1c.add_y(fpath, col_name='var_categorical', index_col=0, remove=999)
mvp1c.regress_out_confounds(fpath, var)
"""

# def test_teardown():

# setup code
# spaths = glob(op.join(testdata_path, 'mock_subjects',
# 'sub*', 'run1.feat'))
# _ = [shutil.rmtree(s) for s in spaths]
8 changes: 6 additions & 2 deletions skbold/postproc/mvp_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,12 @@ def _extract_values_from_pipeline(self, pipe):

if pipe.__class__.__name__ == 'GridSearchCV':
pipe = pipe.best_estimator_

pipe_steps = copy(pipe.named_steps)
elif pipe.__class__.__name__ != 'Pipeline':
# hack to allow non-pipelines
pipe.idx_ = np.ones(pipe.coef_.size, dtype=bool)
pipe_steps = {pipe.__class__.__name__: pipe}
else:
pipe_steps = copy(pipe.named_steps)

for name, step in pipe_steps.items():

Expand Down
Empty file.
37 changes: 37 additions & 0 deletions skbold/postproc/tests/test_mvp_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,40 @@
import os
import pytest
import numpy as np
from skbold.postproc import MvpResultsClassification
from sklearn.cross_validation import StratifiedKFold
from sklearn.svm import SVC

dpath = op.join(testdata_path, 'mock_subjects', 'sub*', 'run1.feat', 'stats')
bmask = op.join(roidata_path, 'GrayMatter.nii.gz')
source = dict()
source['Contrast1'] = {'path': op.join(testdata_path, 'mock_subjects',
'sub*', 'run1.feat', 'stats',
'cope1.nii.gz')}

mvp = MvpBetween(source=source, subject_idf='sub???', mask=bmask)
mvp.create()
fpath = op.join(testdata_path, 'sample_behav.tsv')
mvp.add_y(fpath, col_name='var_categorical', index_col=0, remove=999)


def test_mvp_results_init():

clf = SVC(kernel='linear')
folds = StratifiedKFold(mvp.y, n_folds=2)
mvpr = MvpResultsClassification(mvp=mvp, n_iter=2, feature_scoring='fwm',
out_path=op.join(testdata_path))

for train_idx, test_idx in folds:
train_X, test_X = mvp.X[train_idx, :], mvp.X[test_idx, :]
train_y, test_y = mvp.y[train_idx], mvp.y[test_idx]
clf.fit(train_X, train_y)
pred = clf.predict(test_X)
mvpr.update(test_idx, pred, pipeline=clf)

mvpr.compute_scores()
mvpr.write(feature_viz=True)

for f in ['Contrast1.nii.gz', 'results.tsv', 'confmat.npy']:
assert(op.isfile(op.join(testdata_path, f)))
os.remove(op.join(testdata_path, f))

0 comments on commit 70d2d26

Please sign in to comment.