-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_3.py
executable file
·75 lines (57 loc) · 2.14 KB
/
reader_3.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
#!/usr/bin/python2.6
#import pylab as pl
import numpy as np
from scipy import linalg
from scipy import stats
from covariance.generate_data import generate_standard_sparse_mvn
from covariance.cov_estimator_l1 import CovEstimatorL1CV
from covariance.cov_estimator_l1 import CovEstimatorL21CV
import glob
import sys
inputSubjects=[]
reducedSubjects=[]
for file in sys.argv[1:]:
print "reading "+str(file)
inputSubject=np.loadtxt(file)
print "demeaning columns: "
means = inputSubject.mean(0)
inputSubject = inputSubject - means
inputSubjects.append(inputSubject)
for subject in range(len(inputSubjects)):
print "reducing subject "+str(subject)
reducedSubjects.append(inputSubjects[subject])
print "Found "+str(inputSubjects[subject].shape[0])+" timepoints and "+str(inputSubjects[subject].shape[1])+" components."
print "Reduced to "+str(reducedSubjects[subject].shape[1])+" components."
if subject == 0:
concatSubjects=reducedSubjects[subject]
else:
concatSubjects=np.append(concatSubjects,reducedSubjects[subject],axis=0)
print "concatenated: "+str(concatSubjects.shape)
print "Number of subjects: " + str(len(inputSubjects)) + " with " + str(concatSubjects.shape[1]) + " components."
print "Samples per subject: " + str(inputSubjects[0].shape[0])
labels = np.repeat(np.arange(len(inputSubjects)), inputSubjects[0].shape[0])
print labels.shape
print labels
model = CovEstimatorL21CV()
model.fit(concatSubjects,labels)
l1=model.l21
precs_ = model.precisions
covs_ = np.array([linalg.inv(prec) for prec in precs_])
for subject in range(len(inputSubjects)):
fileName="prec_subject"+str(subject)+"_l1_"+str(l1)
np.savetxt(str(fileName),precs_[subject])
fileName="cov_subject"+str(subject)+"_l1_"+str(l1)
np.savetxt(str(fileName),covs_[subject])
#model = CovEstimatorL1CV()
#model.fit(reducedSubjects[0])
#l1 = model.best_model.l1
#
#if 1:
# prec_ = model.precision
#
#pl.figure()
#vmin = stats.scoreatpercentile(prec_.ravel(),2)
#vmax = stats.scoreatpercentile(prec_.ravel(),98)
#pl.imshow(prec_, interpolation='nearest', vmin=vmin, vmax=vmax, cmap=pl.cm.YlOrRd)
#cbar2=pl.colorbar()
#pl.show()