1 from builtins
import range
2 from builtins
import object
27 import matplotlib.colors
28 from mpl_toolkits.axes_grid1
import make_axes_locatable
30 from .densityPlot
import hide_xticklabels, hide_yticklabels
31 from ..
import modelfitLib
33 __all__ = (
"OptimizerDisplay", )
42 self.
grid = parent.unitGrid * sample.get(parent.recorder.trust)
44 self.
grid += sample.get(parent.recorder.parameters).reshape((1,)*parent.ndim + (parent.ndim,))
51 return self.
sample.get(getattr(self.
parent.recorder, name))
57 self.
parent.objective.fillObjectiveValueGrid(self.
grid.reshape(-1, self.
parent.ndim),
75 def __init__(self, history, model, objective, steps=11):
76 self.
recorder = modelfitLib.OptimizerHistoryRecorder(history.schema)
78 self.
dimensions = list(model.getNonlinearNames()) + list(model.getAmplitudeNames())
86 mgridArgs = (slice(-1.0, 1.0, steps*1j),) * self.
ndim 88 transposeArgs = tuple(list(range(1, self.
ndim+1)) + [0])
89 self.
unitGrid = numpy.mgrid[mgridArgs].transpose(transposeArgs).copy()
91 for sample
in history:
92 if sample.get(self.
recorder.state) & modelfitLib.Optimizer.STATUS_STEP_REJECTED:
93 assert current
is not None 94 current.rejected.append(sample)
97 self.
track.append(current)
99 def plot(self, xDim, yDim, n=0):
120 self.
sliceX[self.
j] = slice(
None)
123 self.
sliceY[self.
i] = slice(
None)
126 x=numpy.array([iteration.sample.get(self.
xKey)
for iteration
in self.
parent.track]),
127 y=numpy.array([iteration.sample.get(self.
yKey)
for iteration
in self.
parent.track]),
128 z=numpy.array([iteration.sample.get(self.
zKey)
for iteration
in self.
parent.track]),
131 self.
figure = matplotlib.pyplot.figure(
"%s vs %s" % (xDim, yDim), figsize=(16, 8))
132 self.
figure.subplots_adjust(left=0.025, right=0.975, bottom=0.08, top=0.95, wspace=0.12)
134 self.
axes3d.autoscale(
False)
140 self.
axes2d.autoscale(
False)
141 divider = make_axes_locatable(self.
axes2d)
142 self.
axesX = divider.append_axes(
"top", 1.5, pad=0.1, sharex=self.
axes2d)
143 self.
axesX.autoscale(
False, axis=
'x')
145 self.
axesY = divider.append_axes(
"right", 1.5, pad=0.1, sharey=self.
axes2d)
146 self.
axesY.autoscale(
False, axis=
'y')
167 current = self.
parent.track[self.
n]
168 x = current.sample.get(self.
xKey)
169 y = current.sample.get(self.
yKey)
170 zMin1 = current.objectiveValues[self.
slice2d].min()
171 zMax1 = current.objectiveValues[self.
slice2d].max()
172 zMin2 = current.objectiveModel[self.
slice2d].min()
173 zMax2 = current.objectiveModel[self.
slice2d].max()
174 self.
setExtent(x0=x - current.trust, x1=x + current.trust,
175 y0=y - current.trust, y1=y + current.trust,
176 z0=min(zMin1, zMin2), z1=max(zMax1, zMax2), lock=
False)
178 def setExtent(self, x0=None, x1=None, y0=None, y1=None, z0=None, z1=None, lock=True):
191 self.
_extent = (x0, x1, y0, y1, z0, z1)
201 def _clipZ(self, x, y, z):
204 mask = numpy.logical_or.reduce([x < self.
xlim[0], x > self.
xlim[1],
206 z < self.
zlim[0], z > self.
zlim[1]],
210 return numpy.logical_not(mask).astype(int).sum() > 4
213 def _contour(self, axes, *args, **kwds):
214 self.
artists.extend(axes.contour(*args, **kwds).collections)
217 kwds = dict(markeredgewidth=0, markerfacecolor=
'g', color=
'g', marker=
'o')
224 kwds = dict(markeredgewidth=0, markerfacecolor=
'r', color='r', marker='v')
225 current = self.
parent.track[self.
n]
226 cx = current.sample.get(self.
xKey)
227 cy = current.sample.get(self.
yKey)
228 cz = current.sample.get(self.
zKey)
229 for r
in current.rejected:
230 x = [cx, r.get(self.
xKey)]
231 y = [cy, r.get(self.
yKey)]
232 z = [cz, r.get(self.
zKey)]
239 current = self.
parent.track[self.
n]
242 x = current.grid[self.
slice2d + (self.
j,)]
243 y = current.grid[self.
slice2d + (self.
i,)]
244 z1 = current.objectiveValues[self.
slice2d].copy()
245 z2 = current.objectiveModel[self.
slice2d].copy()
246 norm = matplotlib.colors.Normalize(vmin=self.
zlim[0], vmax=self.
zlim[1])
248 self.
_contour(self.
axes2d, x, y, z1, cmap=matplotlib.cm.spring, norm=norm)
249 self.
_contour(self.
axes2d, x, y, z2, cmap=matplotlib.cm.winter, norm=norm)
253 self.
_contour(self.
axes3d, x, y, z1, cmap=matplotlib.cm.spring, norm=norm)
254 self.
artists.append(self.
axes3d.plot_surface(x, y, z1, rstride=1, cstride=1,
255 cmap=matplotlib.cm.spring, norm=norm,
256 linewidth=0, antialiased=1, alpha=0.5))
258 self.
_contour(self.
axes3d, x, y, z2, cmap=matplotlib.cm.winter, norm=norm)
259 self.
artists.append(self.
axes3d.plot_surface(x, y, z2, rstride=1, cstride=1,
260 cmap=matplotlib.cm.winter, norm=norm,
261 linewidth=0, antialiased=1, alpha=0.5))
265 current.objectiveValues[self.
sliceX],
'm-'))
267 current.objectiveModel[self.
sliceX],
'c-'))
269 current.grid[self.
sliceY + (self.
i,)],
'm-'))
271 current.grid[self.
sliceY + (self.
i,)],
'c-'))
def __getattr__(self, name)
def __init__(self, history, model, objective, steps=11)
def plot(self, xDim, yDim, n=0)
def hide_xticklabels(axes)
def objectiveValues(self)
def hide_yticklabels(axes)
def __init__(self, parent, sample)