Commit 39f40c32 authored by mjboos's avatar mjboos

bla2

parent 0bde122a
This diff is collapsed.
import numpy as np
import matplotlib.pyplot as plt
from nilearn import image as img
import pandas as pd
import joblib
import dill
from sklearn.linear_model import LinearRegression
from copy import deepcopy
def test_latent_space_reconstruction(feature, latent_activity,
estimator=None, **kwargs):
'''Returns the cross-validated explained variance (averaged across 8 folds) for predicting feature from latent_activity'''
from sklearn.model_selection import cross_validate
from sklearn.linear_model import RidgeCV
if estimator is None:
estimator = RidgeCV(alphas=[1e-5,1e-3,1e-1,1,1e3,1e5])
cv_result = cross_validate(estimator, latent_activity, feature, scoring='explained_variance', cv=8, **kwargs)
mean_test_score = cv_result['test_score'].mean()
if 'estimator' in cv_result:
return mean_test_score, cv_result['estimator']
else:
return mean_test_score
def get_feature_scores(feature_dict, latent_activity, ratings_idx, estimator=None, **kwargs):
scores_dict = dict()
for label in ['Time-Frequency Separability', 'Sound level (db)', 'Speech duration (s)', 'Positive separability']:
scores_dict[label] = test_latent_space_reconstruction(feature_dict[label], latent_activity, estimator=estimator, **kwargs)
scores_dict['Noise rating'] = test_latent_space_reconstruction(feature_dict['Noise rating'], latent_activity[ratings_idx], estimator=estimator, **kwargs)
return scores_dict
def remove_BF_from_coefs(estimator, remove_bf):
'''Returns estimator with all coefficients set to zero that correspond to basis functions in remove bool'''
remove_coef = np.tile(remove_bf, 60)
estimator_new = deepcopy(estimator)
estimator_new.transformedcomp[remove_coef,:] = 0
return estimator_new
with open('average_estimator.pkl','r') as fn:
estimator = dill.load(fn)
bsc = joblib.load('../semisupervised/logBSC_H200_stimuli.pkl')
joint_pcs = estimator.predict(bsc)[:,:3]
separability = joblib.load('mean_sep.pkl')
separability_pos = joblib.load('sep_of_pos_Ws_only.pkl')
separability_pos[np.isnan(separability_pos)] = 0
db = joblib.load('db_dict.pkl')
db = np.array([db[str(i)][1] for i in range(3539)])
speech_overlap = joblib.load('speech_overlap.pkl')
#pcs = joblib.load('testtest.pkl')[..., :3]
#average_pcs = pcs.mean(axis=0)
ratings_dict = joblib.load('ratings_dict.pkl')
cluster_idx = joblib.load('cluster_identity.pkl')
feature_dict = {'Time-Frequency Separability' : separability, 'Sound level (db)' : db, 'Positive separability' : separability_pos,
'Speech duration (s)' : speech_overlap, 'Noise rating' : ratings_dict['ratings']}
joint_scores = get_feature_scores(feature_dict, joint_pcs, ratings_dict['ratings_idx'], estimator=LinearRegression())
scores_dict = dict()
for i in np.unique(cluster_idx):
pc_predictions_wo_cluster = remove_BF_from_coefs(estimator, cluster_idx==i).predict(bsc)[:,:3]
scores_dict[i] = get_feature_scores(feature_dict, pc_predictions_wo_cluster, ratings_dict['ratings_idx'], estimator=LinearRegression())
#individual_scores = [get_feature_scores(feature_dict, individual_pcs, ratings_dict['ratings_idx'], estimator=LinearRegression(), return_estimator=True) for individual_pcs in pcs]
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