8000 use new traitlets api (has quiver memory leaks) · rmorshea/matplotlib@6354091 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6354091

Browse files
committed
use new traitlets api (has quiver memory leaks)
1 parent 26a2252 commit 6354091

File tree

19 files changed

+232
-194
lines changed

19 files changed

+232
-194
lines changed

lib/matplotlib/artist.py

Lines changed: 97 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from .traitlets import (Instance, Configurable, gTransformInstance, Bool, Undefined, Union,
1919
BaseDescriptor, getargspec, PrivateMethodMixin, Float, TraitError,
20-
Unicode, Stringlike, Callable, Tuple, List)
20+
Unicode, Stringlike, Callable, Tuple, List, observe, validate, default)
2121

2222
from urlparse import urlparse
2323

@@ -90,92 +90,109 @@ class Artist(PrivateMethodMixin, Configurable):
9090

9191
transform = gTransformInstance(IdentityTransform())
9292

93-
def _transform_changed(self):
93+
@observe('transform')
94+
def _transform_changed(self, change):
9495
self.pchanged()
9596
self.stale = True
9697

97-
def _transform_validate(self, value, trait):
98+
@validate('transform')
99+
def _transform_validate(self, commit):
98100
self.transform_set = True
99-
return value
101+
return commit['value']
100102

101-
def _transform_getter(self, value, trait):
102-
if trait._conversion_method:
103-
return value(self.axes)
104-
return value
103+
def _transform_getter(self, pull):
104+
if pull['trait']._conversion_method:
105+
return pull['value'](self.axes)
106+
return pull['value']
105107

106108
stale = Bool(True)
107109

108-
def _stale_validate(self, value, trait):
110+
@validate('stale')
111+
def _stale_validate(self, commit):
109112
if self.animated:
110113
return self.stale
111-
return value
114+
return commit['value']
112115

113-
def _stale_changed(self, name, new):
114-
if new and self.stale_callback is not None:
115-
self.stale_callback(self, new)
116+
@observe('stale')
117+
def _stale_changed(self, change):
118+
if change['new'] and self.stale_callback is not None:
119+
self.stale_callback(self, change['new'])
116120

117121
transform_set = Bool(False)
118122

119-
def _axes_changed(self, name, old, new):
123+
axes = Instance(str('matplotlib.axes.Axes'), allow_none=True)
124+
125+
@observe('axes')
126+
def _axes_changed(self, change):
127+
new, old = change['new'], change['old']
120128
if new and old not in (Undefined,None):
121129
raise ValueError("Can not reset the axes. You are "
122130
"probably trying to re-use an artist "
123131
"in more than one Axes which is not "
124132
"supported")
133+
125134
if new not in (Undefined,None) and new is not self:
126135
self.stale_callback = _stale_axes_callback
127136

128-
axes = Instance(str('matplotlib.axes.Axes'), allow_none=True)
129-
130137
figure = Instance(str('matplotlib.figure.FigureBase'), allow_none=True)
131138

132-
def _figure_changed(self, name, old, new):
133-
if old not in (None, Undefined):
139+
@observe('figure')
140+
def _figure_changed(self, change):
141+
if change['old'] not in (None, Undefined):
134142
raise RuntimeError("Can not put single artist in "
135143
"more than one figure")
144+
new = change['new']
136145
if new and new is not self:
137146
self.pchanged()
138147
self.stale = True
139148

140149
visible = Bool(True)
141150

142-
def _visible_changed(self, name, new):
151+
@observe('visible')
152+
def _visible_changed(self, change):
143153
self.pchanged()
144154
self.stale = True
145155

146156
animated = Bool(False)
147157

148-
def _animated_changed(self, name, new):
158+
@observe('animated')
159+
def _animated_changed(self, change):
149160
self.pchanged()
150161
self.stale = True
151162

152163
# Float defaults to 0.0, must have None arg
153164
alpha = Float(None, allow_none=True)
154165

155-
def _alpha_validate(self, value, trait):
156-
if 0>value>1:
166+
@validate('alpha')
167+
def _alpha_validate(self, commit):
168+
if 0>commit['value']>1:
157169
msg = ("The '%s' trait of %s instance can only be"
158-
"transparent (0.0) through opaque (1.0)")
159-
raise TraitError(msg % (trait.name, self.__class__))
160-
return value
170+
" transparent (0.0) through opaque (1.0)")
171+
repl = (commit['trait'].name, self.__class__.__name__)
172+
raise TraitError(msg % repl)
173+
return commit['value']
161174

162-
def _alpha_changed(self, name, new):
175+
@observe('alpha')
176+
def _alpha_changed(self, c):
163177
self.pchanged()
164178
self.stale = True
165179

166180
rasterized = Bool(None, allow_none=True)
167181

168-
def _rasterized_changed(self, name, new):
169-
if new and not hasattr(self.draw, "_supports_rasterization"):
182+
@observe('rasterized')
183+
def _rasterized_changed(self, change):
184+
if change['new'] and not hasattr(self.draw, "_supports_rasterization"):
170185
warnings.warn("Rasterization of '%s' will be ignored" % self)
171186

172187
pickable = Bool()
173188

174-
def _pickabe_validate(self, value, trait):
189+
@validate('pickable')
190+
def _pickabe_validate(self, commit):
175191
msg = "the '%s' trait of a %s instance is not assignable"
176-
raise TraitError(msg % (trait.name, self.__class__.__name__))
192+
repl = (commit['trait'].name, self.__class__.__name__)
193+
raise TraitError(msg % repl)
177194

178-
def _pickable_getter(self):
195+
def _pickable_getter(self, pull):
179196
return (self.figure is not None and
180197
self.figure.canvas is not None and
181198
self.picker is not None)
@@ -184,87 +201,112 @@ def _pickable_getter(self):
184201

185202
clipbox = Instance(str('matplotlib.transforms.BboxBase'), allow_none=True)
186203

187-
def _clipbox_changed(self, name, old, new):
204+
@observe('clipbox')
205+
def _clipbox_changed(self, change):
188206
self.pchanged()
189207
self.stale = True
190208

191209
clippath = Union((Instance(str('matplotlib.patches.Patch')),
192210
Instance(str('matplotlib.transforms.TransformedPath'))),
193211
allow_none=True)
194212

213+
@default('clippath')
195214
def _clippath_default(self): pass
196215

197-
def _clippath_validate(self, value, trait):
216+
@validate('clippath')
217+
def _clippath_validate(self, commit):
218+
value, trait = commit['value'], commit['trait']
198219
if isinstance(value, trait.trait_types[0].klass):
199220
value = TransformedPath(value.get_path(), value.transform)
200221
return value
201222

202-
def _clippath_changed(self, name, new, old):
223+
@observe('clippath')
224+
def _clippath_changed(self, change):
203225
self.pchanged()
204226
self.s 17AE tale = True
205227

206228
clipon = Bool(True)
207229

208-
def _clipon_changed(self, name, old, new):
230+
@observe('clipon')
231+
def _clipon_changed(self, change):
209232
self.pchanged()
210233
self.stale = True
211234

212235
label = Stringlike('', allow_none=True)
213236

214-
def _label_changed(self, name, old, new):
237+
@observe('label')
238+
def _label_changed(self, change):
215239
self.pchanged()
216240

217241
picker = Union((Bool(),Float(),Callable()), allow_none=True)
218242

243+
@default('picker')
219244
def _picker_default(self): pass
220245

221246
_contains = Callable(None, allow_none=True)
222247

223248
mouseover = Bool(False)
224249

225-
def _mouseover_validate(self, value, trait):
250+
@validate('mouseover')
251+
def _mouseover_validate(self, commit):
226252
ax = self.axes
227253
if ax:
228-
if value:
254+
if commit['value']:
229255
ax.mouseover_set.add(self)
230256
else:
231257
ax.mouseover_set.discard(self)
232-
return value
258+
return commit['value']
233259

234260
agg_filter = Callable(None, allow_none=True)
235261

236-
def _agg_filter_changed(self):
262+
@observe('agg_filter')
263+
def _agg_filter_changed(self, change):
237264
self.stale = True
238265

239266
snap = Bool(None, allow_none=True)
240267

241-
def _snap_getter(self, value, trait):
268+
def _snap_getter(self, pull):
242269
if rcParams['path.snap']:
243-
return value
270+
return pull['value']
244271
else:
245272
return False
246273

247-
def _snap_changed(self):
274+
@observe('snap')
275+
def _snap_changed(self, change):
248276
self.stale = True
249277

250278
sketch_scale = Float(None, allow_none=True)
251279

252-
def _sketch_scale_changed(self, name, new):
253-
self.sketch_params = (new, self.sketch_length, self.sketch_randomness)
280+
@observe('sketch_scale')
281+
def _sketch_scale_changed(self, change):
282+
new = change['new']
283+
length = self.sketch_length
284+
randomness = self.sketch_randomness
285+
self.sketch_params = (new, length, randomness)
254286

255287
sketch_length = Float(None, allow_none=True)
256288

257-
def _sketch_length_changed(self, name, new):
258-
self.sketch_params = (self.sketch_scale, new, self.sketch_randomness)
289+
@observe('sketch_length')
290+
def _sketch_length_changed(self, change):
291+
new = change['new']
292+
scale = self.sketch_scale
293+
randomness = self.sketch_randomness
294+
self.sketch_params = (scale, new, randomness)
259295

260296
sketch_randomness = Float(None, allow_none=True)
261297

262-
def _sketch_randomness_changed(self, name, new):
263-
self.sketch_params = (self.sketch_scale, self.sketch_length, new)
298+
@observe('sketch_randomness')
299+
def _sketch_randomness_changed(self, change):
300+
new = change['new']
301+
scale = self.sketch_scale
302+
length = self.sketch_length
303+
self.sketch_params = (scale, length, new)
264304

265305
sketch_params = Tuple(allow_none=True)
266306

267-
def _sketch_validate(self, value, trait):
307+
@validate('sketch')
308+
def _sketch_validate(self, commit):
309+
value = commit['value']
268310
names = ('sketch_scale',
269311
'sketch_length',
270312
'sketch_randomness')
@@ -278,13 +320,15 @@ def _sketch_validate(self, value, trait):
278320
for n,v in zip(names, params):
279321
self.private(n, v)
280322

281-
def _sketch_params_changed(self, name, new):
323+
@observe('sketch_params')
324+
def _sketch_params_changed(self, change):
282325
self.stale = True
283326

284327
path_effects = List(Instance('matplotlib.patheffects.AbstractPathEffect'),
285328
allow_none=True)
286329

287-
def _path_effects_changed(self):
330+
@observe('path_effects')
331+
def _path_effects_changed(self, change):
288332
self.stale = True
289333

290334
url = Unicode(allow_none=True)
@@ -328,13 +372,11 @@ def __init__(self):
328372
self.path_effects = rcParams['path.effects']
329373

330374
def __getstate__(self):
331-
d = self.__dict__.copy()
375+
d = super(Artist, self).__getstate__()
332376
# remove the unpicklable remove method, this will get re-added on load
333377
# (by the axes) if the artist lives on an axes.
334378
d['_remove_method'] = None
335379
d['stale_callback'] = None
336-
# .private(name, value) forces _notify_trait into __dict__
337-
d.pop('_notify_trait', None)
338380
return d
339381

340382
def remove(self):

lib/matplotlib/axes/_base.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@
3434
from matplotlib.artist import allow_rasterization
3535
from matplotlib.cbook import iterable, index_of
3636
from matplotlib.rcsetup import cycler
37+
from matplotlib.traitlets import observe
3738

3839
rcParams = matplotlib.rcParams
3940

4041
is_string_like = cbook.is_string_like
4142
is_sequence_of_strings = cbook.is_sequence_of_strings
4243

4344

45+
46+
4447
def _process_plot_format(fmt):
4548
"""
4649
Process a MATLAB style color/line style format string. Return a
@@ -562,7 +565,7 @@ def __init__(self, fig, rect,
562565
self.relim)
563566

564567
def __setstate__(self, state):
565-
self.__dict__ = state
568+
martist.Artist.__setstate__(self, state)
566569
# put the _remove_method back on all artists contained within the axes
567570
for container_name in ['lines', 'collections', 'tables', 'patches',
568571
'texts', 'images']:
@@ -588,11 +591,13 @@ def _init_axis(self):
588591
self.spines['right'].register_axis(self.yaxis)
589592
self._update_transScale()
590593

591-
def _figure_changed(self, name, old, new):
592-
martist.Artist._figure_changed(self, name, old, new)
594+
@observe('figure')
595+
def _figure_changed(self, change):
596+
martist.Artist._figure_changed(self, change)
593597

594-
self.bbox = mtransforms.TransformedBbox(self._position,
595-
new.transFigure)
598+
tbox = mtransforms.TransformedBbox
599+
self.bbox = tbox(self._position,change['new'].transFigure)
600+
596601
# these will be updated later as data is added
597602
self.dataLim = mtransforms.Bbox.null()
598603
self.viewLim = mtransforms.Bbox.unit()

lib/matplotlib/axis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ def set_label_coords(self, x, y, transform=None):
688688
self.label.set_position((x, y))
689689
self.stale = True
690690

691-
def _transform_getter(self):
691+
def _transform_getter(self, pull):
692692
return self._scale.get_transform()
693693

694694
# !DEPRECATED

lib/matplotlib/collections.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import matplotlib.path as mpath
2929
from matplotlib import _path
3030
import matplotlib.mlab as mlab
31+
from matplotlib.traitlets import validate
3132

3233

3334
CIRCLE_AREA_FACTOR = 1.0 / np.sqrt(np.pi)
@@ -665,8 +666,9 @@ def set_edgecolors(self, c):
665666
"""alias for set_edgecolor"""
666667
return self.set_edgecolor(c)
667668

668-
def _alpha_validate(self, value, trait):
669-
value = artist.Artist._alpha_validate(self, value, trait)
669+
@validate('alpha')
670+
def _alpha_validate(self, commit):
671+
value = artist.Artist._alpha_validate(self, commit)
670672

671673
try:
672674
self._facecolors = mcolors.colorConverter.to_rgba_array(

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ def __setstate__(self, state):
14001400
"and is unlikely to function correctly." %
14011401
(version, ))
14021402

1403-
self.__dict__ = state
1403+
Artist.__setstate__(self, state)
14041404

14051405
# re-initialise some of the unstored state information
14061406
self._axobservers = []

0 commit comments

Comments
 (0)
0