25 import matplotlib.colors
26 from mpl_toolkits.axes_grid1
import make_axes_locatable
28 from .densityPlot
import hide_xticklabels, hide_yticklabels
29 from ..
import modelfitLib
31 __all__ = (
"OptimizerDisplay", )
40 self.
grid = parent.unitGrid * sample.get(parent.recorder.trust)
42 self.
grid += sample.get(parent.recorder.parameters).reshape((1,)*parent.ndim + (parent.ndim,))
49 return self.
sample.get(getattr(self.
parent.recorder, name))
55 self.
parent.objective.fillObjectiveValueGrid(self.
grid.reshape(-1, self.
parent.ndim),
73 def __init__(self, history, model, objective, steps=11):
74 self.
recorder = modelfitLib.OptimizerHistoryRecorder(history.schema)
84 mgridArgs = (slice(-1.0, 1.0, steps*1j),) * self.
ndim
86 transposeArgs = tuple(
list(range(1, self.
ndim+1)) + [0])
87 self.
unitGrid = numpy.mgrid[mgridArgs].transpose(transposeArgs).copy()
89 for sample
in history:
90 if sample.get(self.
recorder.state) & modelfitLib.Optimizer.STATUS_STEP_REJECTED:
91 assert current
is not None
92 current.rejected.append(sample)
97 def plot(self, xDim, yDim, n=0):
118 self.
sliceX[self.
j] = slice(
None)
121 self.
sliceY[self.
i] = slice(
None)
124 x=numpy.array([iteration.sample.get(self.
xKey)
for iteration
in self.
parent.track]),
125 y=numpy.array([iteration.sample.get(self.
yKey)
for iteration
in self.
parent.track]),
126 z=numpy.array([iteration.sample.get(self.
zKey)
for iteration
in self.
parent.track]),
129 self.
figure = matplotlib.pyplot.figure(f
"{xDim} vs {yDim}", figsize=(16, 8))
130 self.
figure.subplots_adjust(left=0.025, right=0.975, bottom=0.08, top=0.95, wspace=0.12)
132 self.
axes3d.autoscale(
False)
138 self.
axes2d.autoscale(
False)
139 divider = make_axes_locatable(self.
axes2d)
140 self.
axesX = divider.append_axes(
"top", 1.5, pad=0.1, sharex=self.
axes2d)
141 self.
axesX.autoscale(
False, axis=
'x')
143 self.
axesY = divider.append_axes(
"right", 1.5, pad=0.1, sharey=self.
axes2d)
144 self.
axesY.autoscale(
False, axis=
'y')
165 current = self.
parent.track[self.
n]
166 x = current.sample.get(self.
xKey)
167 y = current.sample.get(self.
yKey)
168 zMin1 = current.objectiveValues[self.
slice2d].
min()
169 zMax1 = current.objectiveValues[self.
slice2d].
max()
170 zMin2 = current.objectiveModel[self.
slice2d].
min()
171 zMax2 = current.objectiveModel[self.
slice2d].
max()
172 self.
setExtent(x0=x - current.trust, x1=x + current.trust,
173 y0=y - current.trust, y1=y + current.trust,
174 z0=
min(zMin1, zMin2), z1=
max(zMax1, zMax2), lock=
False)
176 def setExtent(self, x0=None, x1=None, y0=None, y1=None, z0=None, z1=None, lock=True):
189 self.
_extent = (x0, x1, y0, y1, z0, z1)
199 def _clipZ(self, x, y, z):
202 mask = numpy.logical_or.reduce([x < self.
xlim[0], x > self.
xlim[1],
204 z < self.
zlim[0], z > self.
zlim[1]],
208 return numpy.logical_not(mask).astype(int).sum() > 4
211 def _contour(self, axes, *args, **kwds):
212 self.
artists.extend(axes.contour(*args, **kwds).collections)
215 kwds = dict(markeredgewidth=0, markerfacecolor=
'g', color=
'g', marker=
'o')
222 kwds = dict(markeredgewidth=0, markerfacecolor=
'r', color=
'r', marker=
'v')
223 current = self.
parent.track[self.
n]
224 cx = current.sample.get(self.
xKey)
225 cy = current.sample.get(self.
yKey)
226 cz = current.sample.get(self.
zKey)
227 for r
in current.rejected:
228 x = [cx, r.get(self.
xKey)]
229 y = [cy, r.get(self.
yKey)]
230 z = [cz, r.get(self.
zKey)]
237 current = self.
parent.track[self.
n]
240 x = current.grid[self.
slice2d + (self.
j,)]
241 y = current.grid[self.
slice2d + (self.
i,)]
242 z1 = current.objectiveValues[self.
slice2d].copy()
243 z2 = current.objectiveModel[self.
slice2d].copy()
244 norm = matplotlib.colors.Normalize(vmin=self.
zlim[0], vmax=self.
zlim[1])
246 self.
_contour(self.
axes2d, x, y, z1, cmap=matplotlib.cm.spring, norm=norm)
247 self.
_contour(self.
axes2d, x, y, z2, cmap=matplotlib.cm.winter, norm=norm)
251 self.
_contour(self.
axes3d, x, y, z1, cmap=matplotlib.cm.spring, norm=norm)
253 cmap=matplotlib.cm.spring, norm=norm,
254 linewidth=0, antialiased=1, alpha=0.5))
256 self.
_contour(self.
axes3d, x, y, z2, cmap=matplotlib.cm.winter, norm=norm)
258 cmap=matplotlib.cm.winter, norm=norm,
259 linewidth=0, antialiased=1, alpha=0.5))
263 current.objectiveValues[self.
sliceX],
'm-'))
265 current.objectiveModel[self.
sliceX],
'c-'))
267 current.grid[self.
sliceY + (self.
i,)],
'm-'))
269 current.grid[self.
sliceY + (self.
i,)],
'c-'))