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