Skip to content

Commit 5df6f78

Browse files
David C. Loniedoutriaux1
authored andcommitted
Store backend as a weakref.
1 parent cc7130c commit 5df6f78

File tree

8 files changed

+80
-68
lines changed

8 files changed

+80
-68
lines changed

Packages/vcs/Lib/vcsvtk/boxfillpipeline.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def _updateScalarData(self):
3131
if self._gm.boxfill_type == "log10":
3232
data = numpy.ma.log10(data)
3333

34-
self._data1 = self._context.trimData2D(data)
35-
self._data2 = self._context.trimData2D(self._originalData2)
34+
self._data1 = self._context().trimData2D(data)
35+
self._data2 = self._context().trimData2D(self._originalData2)
3636

3737
def _updateContourLevelsAndColors(self):
3838
"""Overrides baseclass implementation."""
@@ -174,7 +174,7 @@ def _plotInternal(self):
174174

175175
# create a new renderer for this mapper
176176
# (we need one for each mapper because of camera flips)
177-
self._context.fitToViewport(
177+
self._context().fitToViewport(
178178
act, [self._template.data.x1, self._template.data.x2,
179179
self._template.data.y1, self._template.data.y2],
180180
wc=[x1, x2, y1, y2], geo=self._vtkGeoTransform,
@@ -188,9 +188,9 @@ def _plotInternal(self):
188188
z = self._originalData1.getAxis(-3)
189189
else:
190190
z = None
191-
self._resultDict.update(self._context.renderTemplate(self._template,
192-
self._data1,
193-
self._gm, t, z))
191+
self._resultDict.update(self._context().renderTemplate(self._template,
192+
self._data1,
193+
self._gm, t, z))
194194

195195
if getattr(self._gm, "legend", None) is not None:
196196
self._contourLabels = self._gm.legend
@@ -217,17 +217,18 @@ def _plotInternal(self):
217217
self._contourLevels.append(1.e20)
218218

219219
self._resultDict.update(
220-
self._context.renderColorBar(self._template, self._contourLevels,
221-
self._contourColors,
222-
self._contourLabels,
223-
self._colorMap))
220+
self._context().renderColorBar(self._template, self._contourLevels,
221+
self._contourColors,
222+
self._contourLabels,
223+
self._colorMap))
224224

225-
if self._context.canvas._continents is None:
225+
if self._context().canvas._continents is None:
226226
self._useContinents = False
227227
if self._useContinents:
228228
projection = vcs.elements["projection"][self._gm.projection]
229-
self._context.plotContinents(x1, x2, y1, y2, projection,
230-
self._dataWrapModulo, self._template)
229+
self._context().plotContinents(x1, x2, y1, y2, projection,
230+
self._dataWrapModulo,
231+
self._template)
231232

232233
def _plotInternalBoxfill(self):
233234
"""Implements the logic to render a non-custom boxfill."""

Packages/vcs/Lib/vcsvtk/isofillpipeline.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def _plotInternal(self):
218218

219219
# create a new renderer for this mapper
220220
# (we need one for each mapper because of cmaera flips)
221-
self._context.fitToViewport(
221+
self._context().fitToViewport(
222222
act, [self._template.data.x1, self._template.data.x2,
223223
self._template.data.y1, self._template.data.y2],
224224
wc=[x1, x2, y1, y2], geo=self._vtkGeoTransform,
@@ -233,9 +233,9 @@ def _plotInternal(self):
233233
else:
234234
z = None
235235

236-
self._resultDict.update(self._context.renderTemplate(self._template,
237-
self._data1,
238-
self._gm, t, z))
236+
self._resultDict.update(self._context().renderTemplate(self._template,
237+
self._data1,
238+
self._gm, t, z))
239239

240240
legend = getattr(self._gm, "legend", None)
241241

@@ -260,13 +260,14 @@ def _plotInternal(self):
260260
self._contourLevels.append(1.e20)
261261

262262
self._resultDict.update(
263-
self._context.renderColorBar(self._template, self._contourLevels,
264-
self._contourColors, legend,
265-
self._colorMap))
263+
self._context().renderColorBar(self._template, self._contourLevels,
264+
self._contourColors, legend,
265+
self._colorMap))
266266

267-
if self._context.canvas._continents is None:
267+
if self._context().canvas._continents is None:
268268
self._useContinents = False
269269
if self._useContinents:
270270
projection = vcs.elements["projection"][self._gm.projection]
271-
self._context.plotContinents(x1, x2, y1, y2, projection,
272-
self._dataWrapModulo, self._template)
271+
self._context().plotContinents(x1, x2, y1, y2, projection,
272+
self._dataWrapModulo,
273+
self._template)

Packages/vcs/Lib/vcsvtk/isolinepipeline.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def _plotInternal(self):
8585

8686
lut = vtk.vtkLookupTable()
8787
lut.SetNumberOfTableValues(len(self._contourColors))
88-
cmap = vcs.elements["colormap"][self._context.canvas.getcolormapname()]
88+
cmap = self._context().canvas.getcolormapname()
89+
cmap = vcs.elements["colormap"][cmap]
8990
for i, col in enumerate(self._contourColors):
9091
r, g, b = cmap.index[col]
9192
lut.SetTableValue(i, r / 100., g / 100., b / 100.)
@@ -136,14 +137,15 @@ def _plotInternal(self):
136137
tt = tt.name
137138
tprop = vtk.vtkTextProperty()
138139
vcs2vtk.prepTextProperty(tprop,
139-
self._context.renWin.GetSize(),
140+
self._context().renWin.GetSize(),
140141
to, tt)
141142
tprops.AddItem(tprop)
142143
if colorOverride is not None:
143144
del(vcs.elements["texttable"][tt])
144145
else: # No text properties specified. Use the default:
145146
tprop = vtk.vtkTextProperty()
146-
vcs2vtk.prepTextProperty(tprop, self._context.renWin.GetSize())
147+
vcs2vtk.prepTextProperty(tprop,
148+
self._context().renWin.GetSize())
147149
tprops.AddItem(tprop)
148150
self._resultDict["vtk_backend_contours_labels_text_properties"] = \
149151
tprops
@@ -204,7 +206,7 @@ def _plotInternal(self):
204206

205207
# create a new renderer for this mapper
206208
# (we need one for each mapper because of cmaera flips)
207-
self._context.fitToViewport(
209+
self._context().fitToViewport(
208210
act, [self._template.data.x1, self._template.data.x2,
209211
self._template.data.y1, self._template.data.y2],
210212
wc=[x1, x2, y1, y2], geo=self._vtkGeoTransform,
@@ -219,13 +221,14 @@ def _plotInternal(self):
219221
else:
220222
z = None
221223

222-
self._resultDict.update(self._context.renderTemplate(self._template,
223-
self._data1,
224-
self._gm, t, z))
224+
self._resultDict.update(self._context().renderTemplate(self._template,
225+
self._data1,
226+
self._gm, t, z))
225227

226-
if self._context.canvas._continents is None:
228+
if self._context().canvas._continents is None:
227229
self._useContinents = False
228230
if self._useContinents:
229231
projection = vcs.elements["projection"][self._gm.projection]
230-
self._context.plotContinents(x1, x2, y1, y2, projection,
231-
self._dataWrapModulo, self._template)
232+
self._context().plotContinents(x1, x2, y1, y2, projection,
233+
self._dataWrapModulo,
234+
self._template)

Packages/vcs/Lib/vcsvtk/meshfillpipeline.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, context_):
1717
def _updateScalarData(self):
1818
"""Overrides baseclass implementation."""
1919
# We don't trim _data2 for meshfill:
20-
self._data1 = self._context.trimData2D(self._originalData1)
20+
self._data1 = self._context().trimData2D(self._originalData1)
2121
self._data2 = self._originalData2
2222

2323
def _updateContourLevelsAndColors(self):
@@ -240,7 +240,7 @@ def _plotInternal(self):
240240

241241
# create a new renderer for this mapper
242242
# (we need one for each mapper because of cmaera flips)
243-
self._context.fitToViewport(
243+
self._context().fitToViewport(
244244
act, [self._template.data.x1,
245245
self._template.data.x2,
246246
self._template.data.y1,
@@ -251,8 +251,8 @@ def _plotInternal(self):
251251

252252
self._resultDict["vtk_backend_actors"] = actors
253253

254-
self._template.plot(self._context.canvas, self._data1, self._gm,
255-
bg=self._context.bg,
254+
self._template.plot(self._context().canvas, self._data1, self._gm,
255+
bg=self._context().bg,
256256
X=numpy.arange(self._vtkDataSetBounds[0],
257257
self._vtkDataSetBounds[1] * 1.1,
258258
(self._vtkDataSetBounds[1] -
@@ -286,13 +286,14 @@ def _plotInternal(self):
286286
self._contourLevels.append(1.e20)
287287

288288
self._resultDict.update(
289-
self._context.renderColorBar(self._template, self._contourLevels,
290-
self._contourColors, legend,
291-
self._colorMap))
289+
self._context().renderColorBar(self._template, self._contourLevels,
290+
self._contourColors, legend,
291+
self._colorMap))
292292

293-
if self._context.canvas._continents is None:
293+
if self._context().canvas._continents is None:
294294
self._useContinents = False
295295
if self._useContinents:
296296
projection = vcs.elements["projection"][self._gm.projection]
297-
self._context.plotContinents(x1, x2, y1, y2, projection,
298-
self._dataWrapModulo, self._template)
297+
self._context().plotContinents(x1, x2, y1, y2, projection,
298+
self._dataWrapModulo,
299+
self._template)

Packages/vcs/Lib/vcsvtk/pipeline.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import weakref
2+
3+
14
class Pipeline(object):
25

36
"""Base class for a VTK pipeline implementation of a VCS plot command.
@@ -9,9 +12,10 @@ class Pipeline(object):
912
def __init__(self, context_):
1013
"""Initialize the pipeline object.
1114
12-
context is the VTKVCSBackend object that created this Pipeline.
15+
_context is a weakref of the VTKVCSBackend object that created this
16+
Pipeline.
1317
"""
14-
self._context = context_
18+
self._context = weakref.ref(context_)
1519

1620
# For now, we'll just throw everything at plot. This might need to be
1721
# broken up into set_data, set_template, etc methods...

Packages/vcs/Lib/vcsvtk/pipeline1d.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def __init__(self, context_):
2323

2424
def plot(self, data1, data2, tmpl, gm, grid, transform):
2525
"""Overrides baseclass implementation."""
26-
Y = self._context.trimData1D(data1)
26+
Y = self._context().trimData1D(data1)
2727
if data2 is None:
2828
X = Y.getAxis(0)
2929
else:
3030
X = Y
3131
data1._yname = data2.id
32-
Y = self._context.trimData1D(data2)
32+
Y = self._context().trimData1D(data2)
3333

3434
if gm.flip:
3535
tmp = Y
@@ -39,7 +39,7 @@ def plot(self, data1, data2, tmpl, gm, grid, transform):
3939
if gm.smooth is not None:
4040
Y = smooth(Y, gm.smooth)
4141

42-
l = self._context.canvas.createline()
42+
l = self._context().canvas.createline()
4343
Xs = X[:].tolist()
4444
Ys = Y[:].tolist()
4545
xs = []
@@ -83,7 +83,7 @@ def plot(self, data1, data2, tmpl, gm, grid, transform):
8383
x2 += .0001
8484
l._worldcoordinate = [x1, x2, y1, y2]
8585
if gm.marker is not None:
86-
m = self._context.canvas.createmarker()
86+
m = self._context().canvas.createmarker()
8787
m.type = gm.marker
8888
m.color = gm.markercolor
8989
if gm.markersize > 0:
@@ -98,13 +98,13 @@ def plot(self, data1, data2, tmpl, gm, grid, transform):
9898
if not (Y[:].min() > max(y1, y2) or Y[:].max() < min(y1, y2) or
9999
X[:].min() > max(x1, x2) or X[:].max() < min(x1, x2)):
100100
if l.priority > 0:
101-
self._context.canvas.plot(l, donotstoredisplay=True)
101+
self._context().canvas.plot(l, donotstoredisplay=True)
102102
if gm.marker is not None and m.priority > 0:
103-
self._context.canvas.plot(m, donotstoredisplay=True)
103+
self._context().canvas.plot(m, donotstoredisplay=True)
104104

105-
ren2 = self._context.createRenderer()
106-
self._context.renWin.AddRenderer(ren2)
107-
tmpl.plot(self._context.canvas, data1, gm, bg=self._context.bg,
105+
ren2 = self._context().createRenderer()
106+
self._context().renWin.AddRenderer(ren2)
107+
tmpl.plot(self._context().canvas, data1, gm, bg=self._context().bg,
108108
renderer=ren2, X=X, Y=Y)
109109
if hasattr(data1, "_yname"):
110110
del(data1._yname)
@@ -113,23 +113,23 @@ def plot(self, data1, data2, tmpl, gm, grid, transform):
113113
del(vcs.elements["marker"][m.name])
114114

115115
if tmpl.legend.priority > 0:
116-
legd = self._context.canvas.createline()
116+
legd = self._context().canvas.createline()
117117
legd.x = [tmpl.legend.x1, tmpl.legend.x2]
118118
legd.y = [tmpl.legend.y1, tmpl.legend.y1] # [y1, y1] intentional.
119119
legd.color = l.color
120120
legd.width = l.width
121121
legd.type = l.type
122-
t = self._context.canvas.createtext(
122+
t = self._context().canvas.createtext(
123123
To_source=tmpl.legend.textorientation,
124124
Tt_source=tmpl.legend.texttable)
125125
t.x = tmpl.legend.x2
126126
t.y = tmpl.legend.y2
127127
t.string = data1.id
128-
self._context.canvas.plot(t, donotstoredisplay=True)
128+
self._context().canvas.plot(t, donotstoredisplay=True)
129129
sp = t.name.split(":::")
130130
del(vcs.elements["texttable"][sp[0]])
131131
del(vcs.elements["textorientation"][sp[1]])
132132
del(vcs.elements["textcombined"][t.name])
133-
self._context.canvas.plot(legd, donotstoredisplay=True)
133+
self._context().canvas.plot(legd, donotstoredisplay=True)
134134
del(vcs.elements["line"][legd.name])
135135
return {}

Packages/vcs/Lib/vcsvtk/pipeline2d.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def plot(self, data1, data2, tmpl, gm, grid, transform):
130130
self._vtkDataSet = grid
131131
self._vtkGeoTransform = transform
132132
self._colorMap = \
133-
vcs.elements["colormap"][self._context.canvas.getcolormapname()]
133+
vcs.elements["colormap"][self._context().canvas.getcolormapname()]
134134

135135
# Preprocess the input scalar data:
136136
self._updateScalarData()
@@ -160,8 +160,8 @@ def plot(self, data1, data2, tmpl, gm, grid, transform):
160160

161161
def _updateScalarData(self):
162162
"""Overrides baseclass implementation."""
163-
self._data1 = self._context.trimData2D(self._originalData1)
164-
self._data2 = self._context.trimData2D(self._originalData2)
163+
self._data1 = self._context().trimData2D(self._originalData1)
164+
self._data2 = self._context().trimData2D(self._originalData2)
165165
self._min = self._data1.min()
166166
self._max = self._data1.max()
167167

Packages/vcs/Lib/vcsvtk/vectorpipeline.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ def plot(self, data1, data2, tmpl, gm, grid, transform):
2323
else:
2424
zaxis = None
2525

26-
data1 = self._context.trimData2D(data1) # Ok get3 only the last 2 dims
27-
data2 = self._context.trimData2D(data2)
26+
# Ok get3 only the last 2 dims
27+
data1 = self._context().trimData2D(data1)
28+
data2 = self._context().trimData2D(data2)
2829

2930
gridGenDict = vcs2vtk.genGridOnPoints(data1, gm, deep=False, grid=grid,
3031
geo=transform)
@@ -93,7 +94,8 @@ def plot(self, data1, data2, tmpl, gm, grid, transform):
9394
act = vtk.vtkActor()
9495
act.SetMapper(mapper)
9596

96-
cmap = vcs.elements["colormap"][self._context.canvas.getcolormapname()]
97+
cmap = self._context().canvas.getcolormapname()
98+
cmap = vcs.elements["colormap"][cmap]
9799
r, g, b = cmap.index[lcolor]
98100
act.GetProperty().SetColor(r / 100., g / 100., b / 100.)
99101

@@ -108,14 +110,14 @@ def plot(self, data1, data2, tmpl, gm, grid, transform):
108110
create_renderer=True)
109111

110112
returned.update(
111-
self._context.renderTemplate(tmpl, data1, gm, taxis, zaxis))
113+
self._context().renderTemplate(tmpl, data1, gm, taxis, zaxis))
112114

113-
if self._context.canvas._continents is None:
115+
if self._context().canvas._continents is None:
114116
continents = False
115117
if continents:
116118
projection = vcs.elements["projection"][gm.projection]
117-
self._context.plotContinents(x1, x2, y1, y2, projection, self._dataWrapModulo,
118-
tmpl)
119+
self._context().plotContinents(x1, x2, y1, y2, projection,
120+
self._dataWrapModulo, tmpl)
119121

120122
returned["vtk_backend_actors"] = [[act, [x1, x2, y1, y2]]]
121123
returned["vtk_backend_glyphfilters"] = [glyphFilter]

0 commit comments

Comments
 (0)