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@0
|
131 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.9,2,0)
|
nikcleju@0
|
132 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.8,2,0.2)
|
nikcleju@0
|
133 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.5,2,1)
|
nikcleju@0
|
134 # # Mark 95% border
|
nikcleju@0
|
135 # if mdict['meanmatrix'][algoname][0,0][i,j] > thresh:
|
nikcleju@0
|
136 # # Top border
|
nikcleju@0
|
137 # if mdict['meanmatrix'][algoname][0,0][i-1,j] < thresh and i>0:
|
nikcleju@0
|
138 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
139 # # Bottom border
|
nikcleju@0
|
140 # if mdict['meanmatrix'][algoname][0,0][i+1,j] < thresh and i<rows-1:
|
nikcleju@0
|
141 # bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
142 # # Left border
|
nikcleju@0
|
143 # if mdict['meanmatrix'][algoname][0,0][i,j-1] < thresh and j>0:
|
nikcleju@0
|
144 # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
|
nikcleju@0
|
145 # # Right border (not very probable)
|
nikcleju@0
|
146 # if j<cols-1 and mdict['meanmatrix'][algoname][0,0][i,j+1] < thresh:
|
nikcleju@0
|
147 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
|
nikcleju@0
|
148
|
nikcleju@0
|
149 plt.figure()
|
nikcleju@0
|
150 #plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@0
|
151 plt.imshow(bigmatrix, cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@0
|
152 if dosave:
|
nikcleju@0
|
153 for ext in saveplotexts:
|
nikcleju@0
|
154 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
|
nikcleju@0
|
155 elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
|
nikcleju@0
|
156 if dosave:
|
nikcleju@0
|
157 ilbd = 0
|
nikcleju@0
|
158
|
nikcleju@0
|
159 for matrix in mdict['meanmatrix'][algoname][0,0]:
|
nikcleju@0
|
160
|
nikcleju@0
|
161 # Prepare bigger matrix
|
nikcleju@0
|
162 rows,cols = matrix.shape
|
nikcleju@0
|
163 bigmatrix = numpy.zeros((N*rows,N*cols))
|
nikcleju@0
|
164 for i in numpy.arange(rows):
|
nikcleju@0
|
165 for j in numpy.arange(cols):
|
nikcleju@0
|
166 bigmatrix[i*N:i*N+N,j*N:j*N+N] = matrix[i,j]
|
nikcleju@0
|
167 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.9,2,0)
|
nikcleju@0
|
168 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.8,2,0.2)
|
nikcleju@0
|
169 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.5,2,1)
|
nikcleju@0
|
170 # # Mark 95% border
|
nikcleju@0
|
171 # if matrix[i,j] > thresh:
|
nikcleju@0
|
172 # # Top border
|
nikcleju@0
|
173 # if matrix[i-1,j] < thresh and i>0:
|
nikcleju@0
|
174 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
175 # # Bottom border
|
nikcleju@0
|
176 # if matrix[i+1,j] < thresh and i<rows-1:
|
nikcleju@0
|
177 # bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
178 # # Left border
|
nikcleju@0
|
179 # if matrix[i,j-1] < thresh and j>0:
|
nikcleju@0
|
180 # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
|
nikcleju@0
|
181 # # Right border (not very probable)
|
nikcleju@0
|
182 # if j<cols-1 and matrix[i,j+1] < thresh:
|
nikcleju@0
|
183 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
|
nikcleju@0
|
184
|
nikcleju@0
|
185 plt.figure()
|
nikcleju@0
|
186 #plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@0
|
187 plt.imshow(bigmatrix, cmap=cm.gray, interpolation='nearest',origin='lower')
|
nikcleju@0
|
188 if dosave:
|
nikcleju@0
|
189 for ext in saveplotexts:
|
nikcleju@0
|
190 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
|
nikcleju@0
|
191 ilbd = ilbd + 1
|
nikcleju@0
|
192 if doshow:
|
nikcleju@0
|
193 plt.show()
|
nikcleju@0
|
194 print "Finished."
|
nikcleju@0
|
195
|
nikcleju@0
|
196 def appendtomatfile(filename, toappend, toappendname):
|
nikcleju@0
|
197 mdict = scipy.io.loadmat(filename)
|
nikcleju@0
|
198 mdict[toappendname] = toappend
|
nikcleju@0
|
199 try:
|
nikcleju@0
|
200 scipy.io.savemat(filename, mdict)
|
nikcleju@0
|
201 except:
|
nikcleju@0
|
202 print "Save error"
|
nikcleju@0
|
203
|
nikcleju@0
|
204 # To save to a cell array, create an object array:
|
nikcleju@0
|
205 # >>> obj_arr = np.zeros((2,), dtype=np.object)
|
nikcleju@0
|
206 # >>> obj_arr[0] = 1
|
nikcleju@0
|
207 # >>> obj_arr[1] = 'a string'
|
nikcleju@0
|
208
|
nikcleju@0
|
209 def int_drawseparation(matrix,bigmatrix,N,thresh,border,bordercolor):
|
nikcleju@0
|
210 rows,cols = matrix.shape
|
nikcleju@0
|
211 for i in numpy.arange(rows):
|
nikcleju@0
|
212 for j in numpy.arange(cols):
|
nikcleju@0
|
213 # Mark border
|
nikcleju@0
|
214 # Use top-left corner of current square for reference
|
nikcleju@0
|
215 if matrix[i,j] > thresh:
|
nikcleju@0
|
216 # Top border
|
nikcleju@0
|
217 if matrix[i-1,j] < thresh and i>0:
|
nikcleju@0
|
218 bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
219 # Bottom border
|
nikcleju@0
|
220 if i<rows-1 and matrix[i+1,j] < thresh:
|
nikcleju@0
|
221 bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
|
nikcleju@0
|
222 # Left border
|
nikcleju@0
|
223 if matrix[i,j-1] < thresh and j>0:
|
nikcleju@0
|
224 bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
|
nikcleju@0
|
225 # Right border (not very probable)
|
nikcleju@0
|
226 if j<cols-1 and matrix[i,j+1] < thresh:
|
nikcleju@0
|
227 bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
|
nikcleju@0
|
228
|
nikcleju@0
|
229 return bigmatrix |