-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilter.py
184 lines (177 loc) · 6.9 KB
/
filter.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'''
This module to "pretty print" the results
-------------------------------------------------------------------------------
Coded by Md. Iftekhar Tanveer ([email protected])
Rochester Human-Computer Interaction (ROCHCI)
University of Rochester
-------------------------------------------------------------------------------
'''
import matplotlib
from argparse import ArgumentParser
import scipy.io as sio
import numpy as np
import matplotlib.pyplot as plt
import math
def plotLcurve(args):
LplotDat = []
for afile in args.Files:
if afile.lower().endswith('.mat'):
allDat = sio.loadmat(afile)
LplotDat.append(np.concatenate((allDat['L0'],\
allDat['reconError'],\
allDat['Beta'],allDat['cost']),axis=1))
LplotDat = np.concatenate(LplotDat,axis=0)
plt.scatter(LplotDat[:,0],LplotDat[:,1])
ax=plt.gca()
for idx in xrange(len(LplotDat)):
ax.annotate('Beta='+str(LplotDat[idx,2])+'\n'+\
'{:0.2e}'.format(LplotDat[idx,3])\
,(LplotDat[idx,0],LplotDat[idx,1]+\
np.mean(LplotDat[:,1])/2*np.random.rand()))
plt.xlabel('L0 norm of alpha')
plt.ylabel('Reconstruction Error')
plt.show()
def buildArg():
pars = ArgumentParser(description="Program to 'pretty print' the results.\
It can also plot L curve.")
pars.add_argument('Files',nargs='+',help='List of the (.mat) files from\
which the results are to read')
pars.add_argument('-pprint',nargs='+',\
choices=['D','M','K','Beta','reconError','cost','SNR','L0'],\
help='Print the specified parameters from the files in a pretty format')
pars.add_argument('-hi',nargs='?',\
choices=['D','M','K','Beta','reconError','cost','SNR','L0'],\
help='Specify a parameter name.\
The program returns names of all the files that contain the highest\
value of this parameter')
pars.add_argument('-lo',nargs='?',\
choices=['D','M','K','Beta','reconError','cost','SNR','L0'],\
help='Specify a parameter name.\
The program returns names of all the files that contain the lowest\
value of this parameter')
pars.add_argument('-nhi',nargs='?',\
choices=['D','M','K','Beta','reconError','cost','SNR','L0'],\
help='Specify a parameter name.\
The program returns names of all the files that does not \
contain the highest\
value of this parameter')
pars.add_argument('-nlo',nargs='?',\
choices=['D','M','K','Beta','reconError','cost','SNR','L0'],\
help='Specify a parameter name.\
The program returns names of all the files that does not\
contain the lowest\
values of this parameter')
pars.add_argument('--Lcurve',action='store_true',help='Command to\
draw an L curve')
pars.add_argument('--showresults',action='store_true',help='Command to\
plot the patterns (psi) and corresponding activation sequences (alpha).\
If multiple files are given, it chooses one with minimum L0*exp(cost).')
return pars
def showresults(args):
import skelplot_mayavi as splt
for idx,afile in enumerate(args.Files):
if afile.lower().endswith('.mat'):
allData = sio.loadmat(afile)
L0 = allData['L0']
cost = allData['cost']
mult = L0*math.exp(cost)
if idx==0:
lowmult = mult
bestFile = afile
elif mult<lowmult:
lowmult = mult
bestFile = afile
allData = sio.loadmat(bestFile)
# Print nonzero component indices
sumAlpha = np.sum(allData['alpha_recon'],axis=0)
validIdx = np.nonzero(sumAlpha)
print 'Available nonzero components are:'
for ind in validIdx:
print ind,
print
component = input('which component do you want to see?')
mult = input('please enter a multiplier:')
psi = mult*allData['psi_comp'][:,:,component].dot(allData['princmp'].T)\
+allData['xmean']
splt.animateSkeleton(psi)
plt.clf()
plt.plot(allData['alpha_recon'][:,component])
plt.xlabel('frame')
plt.ylabel('alpha')
plt.show()
def printparams(args):
filelen = max([len(afile) for afile in args.Files])
# Build Template
template = '{0:'+str(filelen)+'} | '
for cnt,par in enumerate(args.pprint):
template=template+'{'+str(cnt+1)+':'+str(max(len(par),5))+'} | '
template = template[:-3]
# Print Header
header=template.format(*(['FILENAME']+[item for item in args.pprint]))
print header
print '='*len(header)
# Print the data
for afile in args.Files:
if afile.lower().endswith('.mat'):
allData = sio.loadmat(afile)
paramdat = ['{:0.2f}'.format(float(allData[par][0][0]))\
for par in args.pprint]
print template.format(*([afile]+paramdat))
def filtfile(args):
if args.hi or args.nhi:
# Scan all the highest indices
for idx,afile in enumerate(args.Files):
if afile.lower().endswith('.mat'):
curval = sio.loadmat(afile)[args.hi if \
args.hi else args.nhi][0][0]
if idx == 0:
hiIdx = [idx]
curmax = curval
else:
if curval > curmax:
hiIdx = [idx]
curmax = curval
elif curval == curmax:
hiIdx = hiIdx + [idx]
for idx,afile in enumerate(args.Files):
if afile.lower().endswith('.mat'):
if args.hi and idx in hiIdx:
print afile
elif args.nhi and not idx in hiIdx:
print afile
elif args.lo or args.nlo:
# Scan all the lowest indices
for idx,afile in enumerate(args.Files):
if afile.lower().endswith('.mat'):
curval = sio.loadmat(afile)[args.lo \
if args.lo else args.nlo][0][0]
if idx == 0:
loIdx = [idx]
curlo = curval
else:
if curval < curlo:
loIdx = [idx]
curlo = curval
elif curval == curlo:
loIdx = loIdx + [idx]
for idx,afile in enumerate(args.Files):
if afile.lower().endswith('.mat'):
if args.lo and idx in loIdx:
print afile
elif args.nlo and not idx in loIdx:
print afile
def main():
parser = buildArg()
args = parser.parse_args()
if not args.Files:
return
if args.pprint:
printparams(args)
if args.hi or args.lo or args.nhi or args.nlo:
filtfile(args)
if args.Lcurve:
plotLcurve(args)
if args.showresults:
showresults(args)
if __name__ == '__main__':
main()