nikcleju@0
|
1 # -*- coding: utf-8 -*-
|
nikcleju@0
|
2 """
|
nikcleju@0
|
3 Created on Wed Nov 09 12:28:55 2011
|
nikcleju@0
|
4
|
nikcleju@0
|
5 @author: ncleju
|
nikcleju@0
|
6 """
|
nikcleju@0
|
7
|
nikcleju@0
|
8 import numpy
|
nikcleju@0
|
9 import scipy.io
|
nikcleju@0
|
10 import matplotlib.pyplot as plt
|
nikcleju@0
|
11 import matplotlib.cm as cm
|
nikcleju@0
|
12 import matplotlib.colors as mcolors
|
nikcleju@0
|
13
|
nikcleju@0
|
14 # Sample call
|
nikcleju@0
|
15 #utils.loadshowmatrices_multipixels('H:\\CS\\Python\\Results\\pt_std1\\approx_pt_std1.mat', dosave=True, saveplotbase='approx_pt_std1_',saveplotexts=('png','eps','pdf'))
|
nikcleju@0
|
16
|
nikcleju@0
|
17 #def loadshowmatrices(filename, algonames = None):
|
nikcleju@0
|
18 # mdict = scipy.io.loadmat(filename)
|
nikcleju@0
|
19 # if algonames == None:
|
nikcleju@0
|
20 # algonames = mdict['algonames']
|
nikcleju@0
|
21 #
|
nikcleju@0
|
22 # for algonameobj in algonames:
|
nikcleju@0
|
23 # algoname = algonameobj[0][0]
|
nikcleju@0
|
24 # print algoname
|
nikcleju@0
|
25 # if mdict['meanmatrix'][algoname][0,0].ndim == 2:
|
nikcleju@0
|
26 # plt.figure()
|
nikcleju@0
|
27 # plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@0
|
28 # elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
|
nikcleju@0
|
29 # for matrix in mdict['meanmatrix'][algoname][0,0]:
|
nikcleju@0
|
30 # plt.figure()
|
nikcleju@0
|
31 # plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@0
|
32 # plt.show()
|
nikcleju@0
|
33 # print "Finished."
|
nikcleju@0
|
34 #
|
nikcleju@0
|
35 #
|
nikcleju@0
|
36 #def loadsavematrices(filename, saveplotbase, saveplotexts, algonames = None):
|
nikcleju@0
|
37 #
|
nikcleju@0
|
38 # mdict = scipy.io.loadmat(filename)
|
nikcleju@0
|
39 # lambdas = mdict['lambdas']
|
nikcleju@0
|
40 #
|
nikcleju@0
|
41 # if algonames is None:
|
nikcleju@0
|
42 # algonames = mdict['algonames']
|
nikcleju@0
|
43 #
|
nikcleju@0
|
44 # for algonameobj in algonames:
|
nikcleju@0
|
45 # algoname = algonameobj[0][0]
|
nikcleju@0
|
46 # print algoname
|
nikcleju@0
|
47 # if mdict['meanmatrix'][algoname][0,0].ndim == 2:
|
nikcleju@0
|
48 # plt.figure()
|
nikcleju@0
|
49 # plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@0
|
50 # for ext in saveplotexts:
|
nikcleju@0
|
51 # plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
|
nikcleju@0
|
52 # elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
|
nikcleju@0
|
53 # ilbd = 0
|
nikcleju@0
|
54 # for matrix in mdict['meanmatrix'][algoname][0,0]:
|
nikcleju@0
|
55 # plt.figure()
|
nikcleju@0
|
56 # plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@0
|
57 # for ext in saveplotexts:
|
nikcleju@0
|
58 # plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
|
nikcleju@0
|
59 # ilbd = ilbd + 1
|
nikcleju@0
|
60 # print "Finished."
|
nikcleju@0
|
61
|
nikcleju@0
|
62 def loadmatrices(filename, algonames=None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
|
nikcleju@0
|
63
|
nikcleju@0
|
64 if dosave and (saveplotbase is None or saveplotexts is None):
|
nikcleju@0
|
65 print('Error: please specify name and extensions for saving')
|
nikcleju@0
|
66 raise Exception('Name or extensions for saving not specified')
|
nikcleju@0
|
67
|
nikcleju@0
|
68 mdict = scipy.io.loadmat(filename)
|
nikcleju@0
|
69
|
nikcleju@0
|
70 if dosave:
|
nikcleju@0
|
71 lambdas = mdict['lambdas']
|
nikcleju@0
|
72
|
nikcleju@0
|
73 if algonames is None:
|
nikcleju@0
|
74 algonames = mdict['algonames']
|
nikcleju@0
|
75
|
nikcleju@0
|
76 for algonameobj in algonames:
|
nikcleju@0
|
77 algoname = algonameobj[0][0]
|
nikcleju@0
|
78 print algoname
|
nikcleju@0
|
79 if mdict['meanmatrix'][algoname][0,0].ndim == 2:
|
nikcleju@0
|
80 plt.figure()
|
nikcleju@0
|
81 plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
|
nikcleju@0
|
82 if dosave:
|
nikcleju@0
|
83 for ext in saveplotexts:
|
nikcleju@0
|
84 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
|
nikcleju@0
|
85 elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
|
nikcleju@0
|
86 if dosave:
|
nikcleju@0
|
87 ilbd = 0
|
nikcleju@0
|
88 for matrix in mdict['meanmatrix'][algoname][0,0]:
|
nikcleju@0
|
89 plt.figure()
|
nikcleju@0
|
90 plt.imshow(matrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
|
nikcleju@0
|
91 if dosave:
|
nikcleju@0
|
92 for ext in saveplotexts:
|
nikcleju@0
|
93 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
|
nikcleju@0
|
94 ilbd = ilbd + 1
|
nikcleju@0
|
95
|
nikcleju@0
|
96 if doshow:
|
nikcleju@0
|
97 plt.show()
|
nikcleju@0
|
98 print "Finished."
|
nikcleju@0
|
99
|
nikcleju@0
|
100
|
nikcleju@0
|
101 def loadshowmatrices_multipixels(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
|
nikcleju@0
|
102
|
nikcleju@0
|
103 if dosave and (saveplotbase is None or saveplotexts is None):
|
nikcleju@0
|
104 print('Error: please specify name and extensions for saving')
|
nikcleju@0
|
105 raise Exception('Name or extensions for saving not specified')
|
nikcleju@0
|
106
|
nikcleju@0
|
107 mdict = scipy.io.loadmat(filename)
|
nikcleju@0
|
108
|
nikcleju@0
|
109 if dosave:
|
nikcleju@0
|
110 lambdas = mdict['lambdas']
|
nikcleju@0
|
111
|
nikcleju@0
|
112 if algonames == None:
|
nikcleju@0
|
113 algonames = mdict['algonames']
|
nikcleju@0
|
114
|
nikcleju@0
|
115 # thresh = 0.90
|
nikcleju@0
|
116 N = 10
|
nikcleju@0
|
117 # border = 2
|
nikcleju@0
|
118 # bordercolor = 0 # black
|
nikcleju@0
|
119
|
nikcleju@0
|
120 for algonameobj in algonames:
|
nikcleju@0
|
121 algoname = algonameobj[0][0]
|
nikcleju@0
|
122 print algoname
|
nikcleju@0
|
123 if mdict['meanmatrix'][algoname][0,0].ndim == 2:
|
nikcleju@0
|
124
|
nikcleju@0
|
125 # Prepare bigger matrix
|
nikcleju@0
|
126 rows,cols = mdict['meanmatrix'][algoname][0,0].shape
|
nikcleju@0
|
127 bigmatrix = numpy.zeros((N*rows,N*cols))
|
nikcleju@0
|
128 for i in numpy.arange(rows):
|
nikcleju@0
|
129 for j in numpy.arange(cols):
|
nikcleju@0
|
130 bigmatrix[i*N:i*N+N,j*N:j*N+N] = mdict['meanmatrix'][algoname][0,0][i,j]
|
nikcleju@5
|
131 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.95,2,0)
|
nikcleju@6
|
132 #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.9, 2,0.2)
|
nikcleju@5
|
133 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.8, 2,0.4)
|
nikcleju@5
|
134 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.5, 2,1)
|
nikcleju@0
|
135 # # Mark 95% border
|
nikcleju@0
|
136 # if mdict['meanmatrix'][algoname][0,0][i,j] > thresh:
|
nikcleju@0
|
137 # # Top border
|
nikcleju@0
|
138 # if mdict['meanmatrix'][algoname][0,0][i-1,j] < thresh and i>0:
|
nikcleju@0
|
139 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
140 # # Bottom border
|
nikcleju@0
|
141 # if mdict['meanmatrix'][algoname][0,0][i+1,j] < thresh and i<rows-1:
|
nikcleju@0
|
142 # bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
143 # # Left border
|
nikcleju@0
|
144 # if mdict['meanmatrix'][algoname][0,0][i,j-1] < thresh and j>0:
|
nikcleju@0
|
145 # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
|
nikcleju@0
|
146 # # Right border (not very probable)
|
nikcleju@0
|
147 # if j<cols-1 and mdict['meanmatrix'][algoname][0,0][i,j+1] < thresh:
|
nikcleju@0
|
148 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
|
nikcleju@0
|
149
|
nikcleju@0
|
150 plt.figure()
|
nikcleju@0
|
151 #plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@5
|
152 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
|
nikcleju@0
|
153 if dosave:
|
nikcleju@0
|
154 for ext in saveplotexts:
|
nikcleju@0
|
155 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
|
nikcleju@0
|
156 elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
|
nikcleju@0
|
157 if dosave:
|
nikcleju@0
|
158 ilbd = 0
|
nikcleju@0
|
159
|
nikcleju@0
|
160 for matrix in mdict['meanmatrix'][algoname][0,0]:
|
nikcleju@0
|
161
|
nikcleju@0
|
162 # Prepare bigger matrix
|
nikcleju@0
|
163 rows,cols = matrix.shape
|
nikcleju@0
|
164 bigmatrix = numpy.zeros((N*rows,N*cols))
|
nikcleju@0
|
165 for i in numpy.arange(rows):
|
nikcleju@0
|
166 for j in numpy.arange(cols):
|
nikcleju@0
|
167 bigmatrix[i*N:i*N+N,j*N:j*N+N] = matrix[i,j]
|
nikcleju@5
|
168 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.95,2,0)
|
nikcleju@6
|
169 #bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.9, 2,0.2)
|
nikcleju@5
|
170 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.8, 2,0.4)
|
nikcleju@5
|
171 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.5, 2,1)
|
nikcleju@0
|
172 # # Mark 95% border
|
nikcleju@0
|
173 # if matrix[i,j] > thresh:
|
nikcleju@0
|
174 # # Top border
|
nikcleju@0
|
175 # if matrix[i-1,j] < thresh and i>0:
|
nikcleju@0
|
176 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
177 # # Bottom border
|
nikcleju@0
|
178 # if matrix[i+1,j] < thresh and i<rows-1:
|
nikcleju@0
|
179 # bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
180 # # Left border
|
nikcleju@0
|
181 # if matrix[i,j-1] < thresh and j>0:
|
nikcleju@0
|
182 # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
|
nikcleju@0
|
183 # # Right border (not very probable)
|
nikcleju@0
|
184 # if j<cols-1 and matrix[i,j+1] < thresh:
|
nikcleju@0
|
185 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
|
nikcleju@0
|
186
|
nikcleju@0
|
187 plt.figure()
|
nikcleju@0
|
188 #plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@5
|
189 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
|
nikcleju@0
|
190 if dosave:
|
nikcleju@0
|
191 for ext in saveplotexts:
|
nikcleju@0
|
192 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
|
nikcleju@0
|
193 ilbd = ilbd + 1
|
nikcleju@0
|
194 if doshow:
|
nikcleju@0
|
195 plt.show()
|
nikcleju@0
|
196 print "Finished."
|
nikcleju@0
|
197
|
nikcleju@0
|
198 def appendtomatfile(filename, toappend, toappendname):
|
nikcleju@0
|
199 mdict = scipy.io.loadmat(filename)
|
nikcleju@0
|
200 mdict[toappendname] = toappend
|
nikcleju@0
|
201 try:
|
nikcleju@0
|
202 scipy.io.savemat(filename, mdict)
|
nikcleju@0
|
203 except:
|
nikcleju@0
|
204 print "Save error"
|
nikcleju@0
|
205
|
nikcleju@0
|
206 # To save to a cell array, create an object array:
|
nikcleju@0
|
207 # >>> obj_arr = np.zeros((2,), dtype=np.object)
|
nikcleju@0
|
208 # >>> obj_arr[0] = 1
|
nikcleju@0
|
209 # >>> obj_arr[1] = 'a string'
|
nikcleju@0
|
210
|
nikcleju@0
|
211 def int_drawseparation(matrix,bigmatrix,N,thresh,border,bordercolor):
|
nikcleju@0
|
212 rows,cols = matrix.shape
|
nikcleju@0
|
213 for i in numpy.arange(rows):
|
nikcleju@0
|
214 for j in numpy.arange(cols):
|
nikcleju@0
|
215 # Mark border
|
nikcleju@0
|
216 # Use top-left corner of current square for reference
|
nikcleju@0
|
217 if matrix[i,j] > thresh:
|
nikcleju@0
|
218 # Top border
|
nikcleju@0
|
219 if matrix[i-1,j] < thresh and i>0:
|
nikcleju@0
|
220 bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
221 # Bottom border
|
nikcleju@0
|
222 if i<rows-1 and matrix[i+1,j] < thresh:
|
nikcleju@0
|
223 bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
224 # Left border
|
nikcleju@0
|
225 if matrix[i,j-1] < thresh and j>0:
|
nikcleju@0
|
226 bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
|
nikcleju@0
|
227 # Right border (not very probable)
|
nikcleju@0
|
228 if j<cols-1 and matrix[i,j+1] < thresh:
|
nikcleju@0
|
229 bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
|
nikcleju@0
|
230
|
nikcleju@0
|
231 return bigmatrix |