1
1
from __future__ import print_function
2
+ import itertools
3
+ import unittest
4
+
2
5
from nose .tools import assert_equal
3
- from numpy .testing import assert_almost_equal
6
+ import numpy .testing as np_test , assert_almost_equal
4
7
from matplotlib .transforms import Affine2D , BlendedGenericTransform
5
8
from matplotlib .path import Path
6
9
from matplotlib .scale import LogScale
11
14
import matplotlib .pyplot as plt
12
15
13
16
14
-
15
17
@cleanup
16
18
def test_non_affine_caching ():
17
19
class AssertingNonAffineTransform (mtrans .Transform ):
@@ -106,37 +108,37 @@ def test_pre_transform_plotting():
106
108
107
109
108
110
def test_Affine2D_from_values ():
109
- points = [ [0 ,0 ],
111
+ points = np . array ( [ [0 ,0 ],
110
112
[10 ,20 ],
111
113
[- 1 ,0 ],
112
- ]
114
+ ])
113
115
114
- t = Affine2D .from_values (1 ,0 ,0 ,0 ,0 ,0 )
116
+ t = mtrans . Affine2D .from_values (1 ,0 ,0 ,0 ,0 ,0 )
115
117
actual = t .transform (points )
116
118
expected = np .array ( [[0 ,0 ],[10 ,0 ],[- 1 ,0 ]] )
117
119
assert_almost_equal (actual ,expected )
118
120
119
- t = Affine2D .from_values (0 ,2 ,0 ,0 ,0 ,0 )
121
+ t = mtrans . Affine2D .from_values (0 ,2 ,0 ,0 ,0 ,0 )
120
122
actual = t .transform (points )
121
123
expected = np .array ( [[0 ,0 ],[0 ,20 ],[0 ,- 2 ]] )
122
124
assert_almost_equal (actual ,expected )
123
125
124
- t = Affine2D .from_values (0 ,0 ,3 ,0 ,0 ,0 )
126
+ t = mtrans . Affine2D .from_values (0 ,0 ,3 ,0 ,0 ,0 )
125
127
actual = t .transform (points )
126
128
expected = np .array ( [[0 ,0 ],[60 ,0 ],[0 ,0 ]] )
127
129
assert_almost_equal (actual ,expected )
128
130
129
- t = Affine2D .from_values (0 ,0 ,0 ,4 ,0 ,0 )
131
+ t = mtrans . Affine2D .from_values (0 ,0 ,0 ,4 ,0 ,0 )
130
132
actual = t .transform (points )
131
133
expected = np .array ( [[0 ,0 ],[0 ,80 ],[0 ,0 ]] )
132
134
assert_almost_equal (actual ,expected )
133
135
134
- t = Affine2D .from_values (0 ,0 ,0 ,0 ,5 ,0 )
136
+ t = mtrans . Affine2D .from_values (0 ,0 ,0 ,0 ,5 ,0 )
135
137
actual = t .transform (points )
136
138
expected = np .array ( [[5 ,0 ],[5 ,0 ],[5 ,0 ]] )
137
139
assert_almost_equal (actual ,expected )
138
140
139
- t = Affine2D .from_values (0 ,0 ,0 ,0 ,0 ,6 )
141
+ t = mtrans . Affine2D .from_values (0 ,0 ,0 ,0 ,0 ,6 )
140
142
actual = t .transform (points )
141
143
expected = np .array ( [[0 ,6 ],[0 ,6 ],[0 ,6 ]] )
142
144
assert_almost_equal (actual ,expected )
@@ -165,6 +167,115 @@ def test_clipping_of_log():
165
167
assert np .allclose (tpoints [- 1 ], tpoints [0 ])
166
168
167
169
170
+ class BasicTransformTests (unittest .TestCase ):
171
+ def setUp (self ):
172
+ class NonAffineForTest (mtrans .Transform ):
173
+ is_affine = False
174
+ output_dims = 2
175
+ input_dims = 2
176
+
177
+ def __init__ (self , real_trans , * args , ** kwargs ):
178
+ self .real_trans = real_trans
179
+ r = mtrans .Transform .__init__ (self , * args , ** kwargs )
180
+
181
+ def transform_non_affine (self , values ):
182
+ return self .real_trans .transform (values )
183
+
184
+ def transform_path_non_affine (self , path ):
185
+ return self .real_trans .transform_path (path )
186
+
187
+ self .ta1 = mtrans .Affine2D (shorthand_name = 'ta1' ).rotate (np .pi / 2 )
188
+ self .ta2 = mtrans .Affine2D (shorthand_name = 'ta2' ).translate (10 , 0 )
189
+ self .ta3 = mtrans .Affine2D (shorthand_name = 'ta3' ).scale (1 , 2 )
190
+
191
+ self .tn1 = NonAffineForTest (mtrans .Affine2D ().translate (1 , 2 ), shorthand_name = 'tn1' )
192
+ self .tn2 = NonAffineForTest (mtrans .Affine2D ().translate (1 , 2 ), shorthand_name = 'tn2' )
193
+ self .tn3 = NonAffineForTest (mtrans .Affine2D ().translate (1 , 2 ), shorthand_name = 'tn3' )
194
+
195
+ # creates a transform stack which looks like ((A, (N, A)), A)
196
+ self .stack1 = (self .ta1 + (self .tn1 + self .ta2 )) + self .ta3
197
+ # creates a transform stack which looks like (((A, N), A), A)
198
+ self .stack2 = self .ta1 + self .tn1 + self .ta2 + self .ta3
199
+ # creates a transform stack which is a subset of stack2
200
+ self .stack2_subset = self .tn1 + self .ta2 + self .ta3
201
+
202
+ # when in debug, the transform stacks can produce dot images:
203
+ # self.stack1.write_graphviz(file('stack1.dot', 'w'))
204
+ # self.stack2.write_graphviz(file('stack2.dot', 'w'))
205
+ # self.stack2_subset.write_graphviz(file('stack2_subset.dot', 'w'))
206
+
207
+ def test_left_to_right_iteration (self ):
208
+ stack3 = (self .ta1 + (self .tn1 + (self .ta2 + self .tn2 ))) + self .ta3
209
+ # stack3.write_graphviz(file('stack3.dot', 'w'))
210
+
211
+ target_transforms = [stack3 ,
212
+ (self .tn1 + (self .ta2 + self .tn2 )) + self .ta3 ,
213
+ (self .ta2 + self .tn2 ) + self .ta3 ,
214
+ self .tn2 + self .ta3 ,
215
+ self .ta3 ,
216
+ ]
217
+ r = list (self .stack3 ._iter_break_from_left_to_right ())
218
+ self .assertEqual (len (r ), len (target_transforms ))
219
+
220
+ for target_stack , stack in itertools .izip (target_transforms , r ):
221
+ self .assertEqual (target_stack , stack )
222
+
223
+ def test_contains_branch (self ):
224
+ r1 = (self .ta2 + self .ta1 )
225
+ r2 = (self .ta2 + self .ta1 )
226
+ self .assertEqual (r1 , r2 )
227
+ self .assertNotEqual (r1 , self .ta1 )
228
+ self .assertTrue (r1 .contains_branch (r2 ))
229
+ self .assertTrue (r1 .contains_branch (self .ta1 ))
230
+ self .assertFalse (r1 .contains_branch (self .ta2 ))
231
+ self .assertFalse (r1 .contains_branch ((self .ta2 + self .ta2 )))
232
+
233
+ self .assertEqual (r1 , r2 )
234
+
235
+ self .assertTrue (self .stack1 .contains_branch (self .ta3 ))
236
+ self .assertTrue (self .stack2 .contains_branch (self .ta3 ))
237
+
238
+ self .assertTrue (self .stack1 .contains_branch (self .stack2_subset ))
239
+ self .assertTrue (self .stack2 .contains_branch (self .stack2_subset ))
240
+
241
+ self .assertFalse (self .stack2_subset .contains_branch (self .stack1 ))
242
+ self .assertFalse (self .stack2_subset .contains_branch (self .stack2 ))
243
+
244
+ self .assertTrue (self .stack1 .contains_branch ((self .ta2 + self .ta3 )))
245
+ self .assertTrue (self .stack2 .contains_branch ((self .ta2 + self .ta3 )))
246
+
247
+ self .assertFalse (self .stack1 .contains_branch ((self .tn1 + self .ta2 )))
248
+
249
+ def test_affine_simplification (self ):
250
+ points = np .array ([[0 , 0 ], [10 , 20 ], [np .nan , 1 ], [- 1 , 0 ]], dtype = np .float64 )
251
+ na_pts = self .stack1 .transform_non_affine (points )
252
+ all_pts = self .stack1 .transform (points )
253
+
254
+ na_expected = np .array ([[1. , 2. ], [- 19. , 12. ],
255
+ [np .nan , np .nan ], [1. , 1. ]], dtype = np .float64 )
256
+ all_expected = np .array ([[11. , 4. ], [- 9. , 24. ],
257
+ [np .nan , np .nan ], [11. , 2. ]], dtype = np .float64 )
258
+
259
+ # check we have the expected results from doing the affine part only
260
+ np_test .assert_array_almost_equal (na_pts , na_expected )
261
+ # check we have the expected results from a full transformation
262
+ np_test .assert_array_almost_equal (all_pts , all_expected )
263
+ # check we have the expected results from doing the transformation in two steps
264
+ np_test .assert_array_almost_equal (self .stack1 .transform_affine (na_pts ), all_expected )
265
+ # check that getting the affine transformation first, then fully transforming using that
266
+ # yields the same result as before.
267
+ np_test .assert_array_almost_equal (self .stack1 .get_affine ().transform (na_pts ), all_expected )
268
+
269
+ # check that the affine part of stack1 & stack2 are equivalent (i.e. the optimization
270
+ # is working)
271
+ expected_result = (self .ta2 + self .ta3 ).get_matrix ()
272
+ result = self .stack1 .get_affine ().get_matrix ()
273
+ np_test .assert_array_equal (expected_result , result )
274
+
275
+ result = self .stack2 .get_affine ().get_matrix ()
276
+ np_test .assert_array_equal (expected_result , result )
277
+
278
+
168
279
if __name__ == '__main__' :
169
280
import nose
170
281
nose .runmodule (argv = ['-s' ,'--with-doctest' ], exit = False )
0 commit comments