1414from io import BytesIO
1515
1616
17- def depth_getter (obj ,
18- current_depth = 0 ,
19- depth_stack = None ,
17+ def depth_getter (obj ,
18+ current_depth = 0 ,
19+ depth_stack = None ,
2020 nest_info = 'top level object' ):
2121 """
2222 Returns a dictionary mapping:
23-
23+
2424 id(obj): (shallowest_depth, obj, nest_info)
25-
26- for the given object (and its subordinates).
27-
28- This, in conjunction with recursive_pickle, can be used to debug
25+
26+ for the given object (and its subordinates).
27+
28+ This, in conjunction with recursive_pickle, can be used to debug
2929 pickling issues, although finding others is sometimes a case of
3030 trial and error.
31-
31+
3232 """
3333 if depth_stack is None :
3434 depth_stack = {}
35-
35+
3636 if id (obj ) in depth_stack :
3737 stack = depth_stack [id (obj )]
3838 if stack [0 ] > current_depth :
3939 del depth_stack [id (obj )]
4040 else :
4141 return depth_stack
42-
42+
4343 depth_stack [id (obj )] = (current_depth , obj , nest_info )
44-
44+
4545 if isinstance (obj , (list , tuple )):
4646 for i , item in enumerate (obj ):
47- depth_getter (item , current_depth = current_depth + 1 ,
48- depth_stack = depth_stack ,
47+ depth_getter (item , current_depth = current_depth + 1 ,
48+ depth_stack = depth_stack ,
4949 nest_info = 'list/tuple item #%s in (%s)' % (i , nest_info ))
5050 else :
5151 if isinstance (obj , dict ):
@@ -58,16 +58,16 @@ def depth_getter(obj,
5858 state = obj .__dict__
5959 else :
6060 state = {}
61-
61+
6262 for key , value in state .iteritems ():
63- depth_getter (value , current_depth = current_depth + 1 ,
64- depth_stack = depth_stack ,
63+ depth_getter (value , current_depth = current_depth + 1 ,
64+ depth_stack = depth_stack ,
6565 nest_info = 'attribute "%s" in (%s)' % (key , nest_info ))
66-
66+
6767 # for instancemethod picklability (and some other issues), uncommenting
6868 # the following may be helpful
6969# print([(name, dobj.__class__) for name, dobj in state.iteritems()], ': ', nest_info, ';', type(obj))
70-
70+
7171 return depth_stack
7272
7373
@@ -76,14 +76,14 @@ def recursive_pickle(top_obj):
7676 Recursively pickle all of the given objects subordinates, starting with
7777 the deepest first. **Very** handy for debugging pickling issues, but
7878 also very slow (as it literally pickles each object in turn).
79-
79+
8080 Handles circular object references gracefully.
81-
81+
8282 """
8383 objs = depth_getter (top_obj )
8484 # sort by depth then by nest_info
8585 objs = sorted (objs .itervalues (), key = lambda val : (- val [0 ], val [2 ]))
86-
86+
8787 for _ , obj , location in objs :
8888# print('trying %s' % location)
8989 try :
@@ -110,90 +110,90 @@ def test_simple():
110110
111111# recursive_pickle(fig)
112112 pickle .dump (ax , BytesIO (), pickle .HIGHEST_PROTOCOL )
113-
113+
114114# ax = plt.subplot(121, projection='hammer')
115115# recursive_pickle(ax, 'figure')
116116# pickle.dump(ax, BytesIO(), pickle.HIGHEST_PROTOCOL)
117117
118118
119- @image_comparison (baseline_images = ['multi_pickle' ],
120- extensions = ['png' ])
119+ @image_comparison (baseline_images = ['multi_pickle' ],
120+ extensions = ['png' ], remove_text = True )
121121def test_complete ():
122122 fig = plt .figure ('Figure with a label?' , figsize = (10 , 6 ))
123-
123+
124124 plt .suptitle ('Can you fit any more in a figure?' )
125-
125+
126126 # make some arbitrary data
127127 x , y = np .arange (8 ), np .arange (10 )
128128 data = u = v = np .linspace (0 , 10 , 80 ).reshape (10 , 8 )
129129 v = np .sin (v * - 0.6 )
130-
130+
131131 plt .subplot (3 ,3 ,1 )
132132 plt .plot (range (10 ))
133-
133+
134134 plt .subplot (3 , 3 , 2 )
135135 plt .contourf (data , hatches = ['//' , 'ooo' ])
136136 plt .colorbar ()
137-
137+
138138 plt .subplot (3 , 3 , 3 )
139139 plt .pcolormesh (data )
140-
141-
140+
141+
142142 plt .subplot (3 , 3 , 4 )
143143 plt .imshow (data )
144-
144+
145145 plt .subplot (3 , 3 , 5 )
146146 plt .pcolor (data )
147-
147+
148148 plt .subplot (3 , 3 , 6 )
149149 plt .streamplot (x , y , u , v )
150-
150+
151151 plt .subplot (3 , 3 , 7 )
152152 plt .quiver (x , y , u , v )
153-
153+
154154 plt .subplot (3 , 3 , 8 )
155155 plt .scatter (x , x ** 2 , label = '$x^2$' )
156156 plt .legend (loc = 'upper left' )
157-
157+
158158 plt .subplot (3 , 3 , 9 )
159159 plt .errorbar (x , x * - 0.5 , xerr = 0.2 , yerr = 0.4 )
160-
160+
161161 ###### plotting is done, now test its pickle-ability #########
162-
162+
163163 # Uncomment to debug any unpicklable objects. This is slow (~200 seconds).
164164# recursive_pickle(fig)
165-
165+
166166 result_fh = BytesIO ()
167167 pickle .dump (fig , result_fh , pickle .HIGHEST_PROTOCOL )
168-
168+
169169 plt .close ('all' )
170-
170+
171171 # make doubly sure that there are no figures left
172172 assert_equal (plt ._pylab_helpers .Gcf .figs , {})
173-
173+
174174 # wind back the fh and load in the figure
175175 result_fh .seek (0 )
176176 fig = pickle .load (result_fh )
177-
177+
178178 # make sure there is now a figure manager
179179 assert_not_equal (plt ._pylab_helpers .Gcf .figs , {})
180-
180+
181181 assert_equal (fig .get_label (), 'Figure with a label?' )
182-
183-
182+
183+
184184def test_no_pyplot ():
185185 # tests pickle-ability of a figure not created with pyplot
186-
186+
187187 import pickle as p
188188 from matplotlib .backends .backend_pdf import FigureCanvasPdf as fc
189189 from matplotlib .figure import Figure
190-
190+
191191 fig = Figure ()
192192 can = fc (fig )
193193 ax = fig .add_subplot (1 , 1 , 1 )
194194 ax .plot ([1 , 2 , 3 ], [1 , 2 , 3 ])
195-
196- # Uncomment to debug any unpicklable objects. This is slow so is not
195+
196+ # Uncomment to debug any unpicklable objects. This is slow so is not
197197 # uncommented by default.
198198# recursive_pickle(fig)
199199 pickle .dump (fig , BytesIO (), pickle .HIGHEST_PROTOCOL )
0 commit comments