1 import scipy
2 import SloppyCell
3
4
5
6 if SloppyCell.my_rank != 0:
7 raise ImportError
8
9 try:
10 from pylab import *
11 except RuntimeError:
12
13
14
15 raise ImportError
16
17 import Residuals
18
19 rc('lines', linewidth=2)
20
21 basic_colors = ('b', 'g', 'r', 'c', 'm', 'k')
22 basic_symbols = ('o', 's', '^', 'v', '<', ">", 'x', 'D', 'h', 'p')
23 basic_lines = ('-', '--', '-.', ':')
24
27 """
28 ColorWheel()
29
30 Returns a generator that cycles through a selection of colors, symbols, and
31 line styles for matlibplot.matlab.plot.
32 """
33 if not colors:
34 colors = ('',)
35 if not symbols:
36 symbols = ('',)
37 if not lines:
38 lines = ('',)
39
40 while 1:
41 for l in lines:
42 for s in symbols:
43 for c in colors:
44 yield (c, s, l)
45
46 vals_cW = 0
48 """
49 Reset the ColorWheel used for plotting eigenvalues.
50 """
51 global vals_cW
52 vals_cW = ColorWheel(colors = ('b', 'r', 'g', 'c', 'm', 'y', 'k'),
53 lines = None)
54 reset_vals_cw()
55
56 -def plot_eigvals(vals, label=None, offset=0, indicate_neg=True, join=False,
57 sym=None, ax=None):
58 """
59 Make a nice eigenvalue plot.
60
61 vals: The eigenvalues to plot.
62 label: Label to give the line, for use in a legend.
63 offset: Horizontal offset for the starting point of the eigenvalue plot.
64 indicate_neg: If True (default), negative eigenvalues are indicated by red
65 points.
66 join: If True, the eigenvalues are plotted with a line joining them.
67 sym: If not None, the supplied symbol/color combination is used for the
68 plot. Otherwise it is automatically rotated.
69 ax: If not None, this matplotlib Axes object is plotted to. Otherwise the
70 current interactive axes are used.
71 """
72 posVals = abs(scipy.compress(scipy.real(vals) > 0, vals))
73 posRange = scipy.compress(scipy.real(vals) > 0, range(len(vals)))
74 negVals = abs(scipy.compress(scipy.real(vals) < 0, vals))
75 negRange = scipy.compress(scipy.real(vals) < 0, range(len(vals)))
76
77 if ax is None:
78 ax = gca()
79
80 if sym is None:
81 sym = vals_cW.next()
82 if indicate_neg:
83 if sym[0] == 'r':
84 sym = vals_cW.next()
85 if len(negVals) > 0:
86 ax.semilogy(negRange+offset, negVals, color = 'r', marker=sym[1],
87 linestyle='', mfc = 'r', zorder=1)
88
89 line = ax.semilogy(posRange+offset, posVals, color=sym[0], marker=sym[1],
90 label = label, zorder=0, markerfacecolor=sym[0],
91 linestyle='')
92
93 if join:
94 ax.plot(scipy.arange(len(vals)) + offset, abs(vals), color = sym[0],
95 linestyle='-', zorder=-1)
96
97 a = axis()
98 axis([-0.05*len(vals) + offset, 1.05*(len(vals) - 1) + offset, a[2], a[3]])
99
100 return line
101
103 """
104 Plot eigenvalues as a set of horizontal lines.
105
106 vals Eigenvalues to plot
107 widths Horizontal width of the lines
108 offset Starting x position for the lines
109 ax Axis instance to plot to. If ax is None, current plot axis is used
110 lc Color of lines
111 lw Line thicknesses
112 """
113
114 if ax is None:
115 ax = gca()
116
117 vals = scipy.compress(vals > 0, vals)
118
119 segs = scipy.array([((offset, val), (offset+widths, val)) for val in vals])
120 for seg in segs:
121 ax.plot(seg[:,0], seg[:,1], lw=lw, color=lc)
122 ax.set_yscale('log')
123
124 return
125
127 return plot_eigvals(vals, label, offset, indicate_neg=False, join=join)
128
129 PlotEigenvalueSpectrum = plot_eigvals
130
131 -def plot_eigvect(vect, labels=None, bottom=0, num_label=5, label_offset=0.15):
132 """
133 Plot a given eigenvector.
134
135 If a list of labels is passed in, the largest (in magnitude) num_label bars
136 will be labeled on the plot. label_offset controls how much the labels
137 are shifted from the top of the bars for clarity.
138 bottom controls where the bar plot is centered along the y axis. This is
139 useful for plotting several e'vectors on the same axes.
140 """
141
142
143 vect = scipy.real(vect)
144 max_index = scipy.argmax(abs(vect))
145 if vect[max_index] < 0:
146 vect = -vect
147 bar(scipy.arange(len(vect)) - 0.4, vect/scipy.linalg.norm(vect),
148 bottom=bottom)
149 a = list(axis())
150 a[0:2] = [-.03*len(vect) - 0.4, (len(vect) - 1)*1.03 + 0.4]
151
152 if labels is not None:
153 mags = zip(abs(vect), range(len(vect)), vect)
154 mags.sort()
155 mags.reverse()
156 for mag, index, val in mags[:num_label]:
157 name = labels[index]
158 text(index, val + scipy.sign(val)*label_offset, name,
159 horizontalalignment='center', verticalalignment='center')
160
161 a[2] -= 0.1
162 a[3] += 0.1
163
164 axis(a)
165
166 -def plot_priors(model,priorIDs=None,params=None,sameScale=False):
167 """
168 Plots specified priors and parameter values.
169 If no priors are specified, plots them all.
170 If no params are provided, uses the model params.
171 If sameScale is true, recenters everything so all prior optima are at 0.
172 Labeling is awkward and hence avoided here. I suggest using the
173 pylab.text command with parameter names after the plot has been generated.
174 """
175 if params is None:
176 params=model.get_params()
177 residuals = model.GetResiduals()
178 if priorIDs is None:
179 priorIDs = residuals.keys()
180
181 priorVals=[]
182 priorErrs=[]
183 parVals=[]
184 for resID in priorIDs:
185 res = residuals.getByKey(resID)
186 if isinstance(res, Residuals.PriorInLog):
187 priorVals.append(res.logPVal)
188 priorErrs.append(res.sigmaLogPVal)
189 parVals.append(params.getByKey(res.pKey))
190
191 if sameScale is False:
192 errorbar(scipy.arange(len(priorVals)),priorVals,yerr=priorErrs,fmt='bo',ecolor='k',capsize=6)
193 errorbar(scipy.arange(len(priorVals)),scipy.log(parVals),fmt='go')
194 else:
195 errorbar(scipy.arange(len(priorVals)),scipy.zeros(len(priorVals)),yerr=priorErrs,fmt=None,ecolor='k',capsize=6)
196 errorbar(scipy.arange(len(priorVals)),scipy.log(parVals)-priorVals,fmt='go')
197