forked from DavidSabbagh/meeg_power_regression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_snr.py
110 lines (81 loc) · 3.51 KB
/
sim_snr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# %%
from os import makedirs
import os.path as op
import numpy as np
import pandas as pd
from sklearn.linear_model import RidgeCV
from sklearn.dummy import DummyRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline, Pipeline
from sklearn.model_selection import cross_val_score, cross_validate
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
# project imports
from library.simuls import generate_covariances, generate_covariances_and_a
from library.spfiltering import ProjIdentitySpace, ProjSPoCSpace
from library.featuring import Diag, LogDiag, Riemann
from library.pattern import PatternScorer
import config as cfg
# %% parameters
sampling_freq = 1 # Hz
n_matrices = 100 # Number of matrices
f_powers = 'log' # link function between the y and the source powers
rng = 4
noise_A = 0.
n_channels = 5
n_sources = 1
n_compo = n_channels
distance_A_id = 1
scoring = 'neg_mean_absolute_error'
out_path = op.join(cfg.path_outputs, 'simuls', 'sigma')
if not op.exists(out_path):
makedirs(out_path)
# %% pipelines
# define spatial filters
identity = ProjIdentitySpace()
spoc = ProjSPoCSpace(n_compo=n_channels, scale='auto', reg=0, shrink=0)
# define featuring
diag = Diag()
logdiag = LogDiag()
riemann = Riemann(n_fb=1, metric='riemann')
sc = StandardScaler()
# define algo
dummy = DummyRegressor()
ridge = RidgeCV(alphas=np.logspace(-5, 3, 25), scoring=scoring)
# define models
pipelines = {
'dummy': make_pipeline(identity, logdiag, sc, dummy),
'diag': make_pipeline(identity, logdiag, sc, ridge),
'spoc': make_pipeline(spoc, logdiag, sc, ridge),
'riemann': make_pipeline(identity, riemann, sc, ridge)
}
# %% cross-validation simulations for sigma parameter
sigmas = np.logspace(-2, 1, 10) # noise level in y
n_cv = 10
n_methods = len(pipelines)
# Run experiments
resdf = pd.DataFrame(index=range(n_methods * len(sigmas)), \
columns = ['method', 'nonlinearity', 'sigma', 'noise_A', 'n_sources', 'n_compo', \
'target_score_mu', 'target_score_sd', 'pattern_score_mu', 'pattern_score_sd'])
resix = 0
for j, sigma in enumerate(sigmas):
X, y, A = generate_covariances_and_a(n_matrices, n_channels, n_sources,
sigma=sigma, distance_A_id=distance_A_id,
f_p=f_powers, direction_A=None,
noise_A=noise_A, rng=rng)
X = X[:, None, :, :]
a = A[:,0:n_sources,None] / np.linalg.norm(A[:,0:n_sources,None], axis=0)
for i, (name, pipeline) in enumerate(pipelines.items()):
print('sigma = {}, {} method'.format(sigma, name))
pttrn_scorer = PatternScorer(a=a, name=name)
scoring = {'mae': 'neg_mean_absolute_error',
'pattrn': pttrn_scorer }
sc = cross_validate(pipeline, X, y, scoring=scoring,
cv=n_cv, n_jobs=-1, error_score=np.nan, return_train_score=True)
resdf.loc[resix,('method', 'nonlinearity', 'sigma', 'noise_A', 'n_sources', 'n_compo')] = \
(name, f_powers, sigma, noise_A, n_sources, n_compo)
resdf.loc[resix,('target_score_mu', 'target_score_sd')] = (- np.mean(sc['test_mae']), np.std(sc['test_mae']))
resdf.loc[resix,('pattern_score_mu', 'pattern_score_sd')] = (np.mean(1. - sc['train_pattrn']), np.std(1. - sc['train_pattrn']))
resix += 1
# %% save results
resdf.to_csv(op.join(out_path, 'scores.csv'), index=False)