Commit 45030e6c authored by Moritz Boos's avatar Moritz Boos
Browse files

updates before migration

parent 9afa76e1
......@@ -201,10 +201,26 @@ def pearson_r(x,y):
r = (1/(n-1))*(x*y).sum(axis=0)
return r
def r2_score_predictions(predictions, subj, folder='/data/mboos/encoding', n_splits=10):
'''Helper functions to score a matrix of obs X voxels of predictions without loading all fmri data into memory'''
from sklearn.metrics import r2_score
from sklearn.feature_selection import VarianceThreshold
file_name = join(folder, 'fmri', 'fmri_subj_{}.pkl'.format(subj))
fmri = joblib.load(file_name, mmap_mode='c')
fmri = fmri[:predictions.shape[0]]
split_indices = np.array_split(np.arange(predictions.shape[1]), n_splits)
scores = []
for indices in split_indices:
r2_scores = r2_score(fmri[:, indices], predictions[:, indices], multioutput='raw_values')
r2_scores[np.var(fmri[:, indices], axis=0)==0.0] = 0
scores.append(r2_scores)
return np.concatenate(scores)
def score_predictions(predictions, subj, folder='/data/mboos/encoding', n_splits=10):
'''Helper functions to score a matrix of obs X voxels of predictions without loading all fmri data into memory'''
file_name = join(folder, 'fmri', 'fmri_subj_{}.pkl'.format(subj))
fmri = joblib.load(file_name, mmap_mode='c')
fmri = fmri[:predictions.shape[0]]
split_indices = np.array_split(np.arange(predictions.shape[1]), n_splits)
scores = []
for indices in split_indices:
......
......@@ -15,15 +15,17 @@ def save_map(scores, threshold=None, model='logBSC_H200', name='mean', mask='gro
fname = os.path.join(folder, 'maps', '{}_{}_map.nii.gz'.format(name, model))
unmasked.to_filename(fname)
def glassbrain_contours(map_dict, colors=['r', 'g', 'b', 'cyan', 'magenta', 'k'], cutoff=[90], smooth=None, alpha=1.0, **kwargs):
def glassbrain_contours(map_dict, colors=['r', 'g', 'b', 'cyan', 'magenta', 'k'], cutoff=[0.1], smooth=None, alpha=1.0, **kwargs):
import nilearn.image as img
from nilearn import plotting
from itertools import cycle
img_dict = { label : img.load_img(map_dict[label]) for label in sorted(map_dict.keys())}
display = plotting.plot_glass_brain(None, **kwargs)
for color, (label, image) in zip(cycle(colors), img_dict.iteritems()):
data = image.get_data()
data = data[np.logical_not(np.isclose(data, 0.0))]
cutoff_perc = np.percentile(data.flatten(), cutoff)
image = img.smooth_img(image, fwhm=smooth)
cutoff_perc = np.percentile(image.get_data().flatten(), cutoff)
display.add_contours(image, levels=cutoff_perc, colors=color, alpha=alpha)
return display
......
......@@ -37,23 +37,25 @@ class LeaveOneGroupCombinationOut(BaseCrossValidator):
groups = check_array(groups, copy=True, ensure_2d=False, dtype=None)
return np.multiply.reduce([len(np.unique(group)) for group in groups.T])
def fit_cross_subject_model(targets, stimulus_name, estimator, n_targets=3, n_runs=8, cv_args=None):
def fit_cross_subject_model(targets, features, estimator, n_targets=3, n_runs=8, cv_args=None):
'''Fits an estimator to targets using the features specified in stimulus_name.
Returns a tuple of a fitted estimator and the cross-validated predictions'''
from sklearn.model_selection import cross_val_predict
if cv_args is None:
cv_args = {'cv' : LeaveOneGroupCombinationOut()}
# make runs equal in length
targets = targets[:3536,:,:n_targets]
n_samples, n_subj = targets.shape[:2]
targets = targets[:, :3536, :n_targets]
n_subj, n_samples = targets.shape[:2]
run_length = n_samples / n_runs
features = joblib.load('/data/mboos/encoding/stimulus/preprocessed/{}_stimuli.pkl'.format(stimulus_name)).astype('float32')[:n_samples]
# ordering is now [n_runs * samples_per_run * n_subj] with C-ordering (last index changes fastest
features = features.astype('float32')[:n_samples]
# ordering is now [n_subj * n_runs * samples_per_run, n_targets] with C-ordering (last index changes fastest
targets = np.reshape(targets, (-1, n_targets))
features = np.repeat(features, n_subj, axis=0)
groups_subj = np.tile(np.arange(n_subj), n_samples)
groups_runs = np.repeat(np.arange(n_runs), run_length*n_subj)
features = np.tile(features, (n_subj, 1))
groups_subj = np.repeat(np.arange(n_subj), n_samples)
one_run = np.repeat(np.arange(n_runs), run_length)
groups_runs = np.tile(one_run, n_subj)
groups = np.array([groups_subj, groups_runs]).T
predictions = cross_val_predict(estimator, features, targets, groups=groups, **cv_args)
return predictions, estimator.fit(features, targets)
......@@ -123,14 +125,12 @@ def pca_reduction(predictions, select_voxels=None, **pca_params):
pc_predictions = pca.fit_transform(predictions)
return pc_predictions, pca
def project_fmri_on_pcs(subj, pca, select_voxels=None, folder='/data/mboos/encoding'):
'''Projects fmri data from subj onto the pca and returns it'''
from os.path import join
fmri = joblib.load(join(folder, 'fmri', 'fmri_subj_{}.pkl'.format(subj)))
return pca.transform(fmri)
def scores_from_pc(pc, pc_predictions, pca, subj, n_splits=10, folder='/data/mboos/encoding'):
'''Computes scores for individual voxels for only one pc for subj.
PC STARTS FROM ZERO.'''
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment