442007 Michael Droettboom
55"""
66
7- import numpy as N
7+ import numpy as npy
88from numpy .linalg import inv
99from sets import Set
1010
@@ -37,7 +37,7 @@ def add_children(self, children):
3737class Bbox (TransformNode ):
3838 def __init__ (self , points ):
3939 TransformNode .__init__ (self )
40- self ._points = N .asarray (points , N .float_ )
40+ self ._points = npy .asarray (points , npy .float_ )
4141 self .track = False
4242
4343 #@staticmethod
@@ -52,7 +52,7 @@ def from_lbwh(left, bottom, width, height):
5252
5353 #@staticmethod
5454 def from_lbrt (* args ):
55- points = N .array (args , dtype = N .float_ ).reshape (2 , 2 )
55+ points = npy .array (args , dtype = npy .float_ ).reshape (2 , 2 )
5656 return Bbox (points )
5757 from_lbrt = staticmethod (from_lbrt )
5858
@@ -72,12 +72,15 @@ def __repr__(self):
7272 return 'Bbox(%s)' % repr (self ._points )
7373 __str__ = __repr__
7474
75+ def __array__ (self ):
76+ return self ._points
77+
7578 # JDH: the update method will update the box limits from the
7679 # existing limits and the new data; it appears here you are just
7780 # using the new data. We use an "ignore" flag to specify whether
7881 # you want to include the existing data or not in the update
7982 def update_from_data (self , x , y , ignore = True ):
80- self ._points = N .array ([[x .min (), y .min ()], [x .max (), y .max ()]], N .float_ )
83+ self ._points = npy .array ([[x .min (), y .min ()], [x .max (), y .max ()]], npy .float_ )
8184 self .invalidate ()
8285
8386 # MGDTODO: Probably a more efficient ways to do this...
@@ -150,7 +153,7 @@ def _get_bounds(self):
150153 self .xmax - self .xmin , self .ymax - self .ymin )
151154 def _set_bounds (self , bounds ):
152155 l ,b ,w ,h = bounds
153- self ._points = N .array ([[l , b ], [l + w , b + h ]], N .float_ )
156+ self ._points = npy .array ([[l , b ], [l + w , b + h ]], npy .float_ )
154157 self .invalidate ()
155158 bounds = property (_get_bounds , _set_bounds )
156159
@@ -165,7 +168,7 @@ def expanded(self, sw, sh):
165168 height = self .height
166169 deltaw = (sw * width - width ) / 2.0
167170 deltah = (sh * height - height ) / 2.0
168- a = N .array ([[- deltaw , - deltah ], [deltaw , deltah ]])
171+ a = npy .array ([[- deltaw , - deltah ], [deltaw , deltah ]])
169172 return Bbox (self ._points + a )
170173
171174 def contains (self , x , y ):
@@ -215,7 +218,7 @@ def __radd__(self, other):
215218 raise TypeError ("Can not add Transform to object of type '%s'" % type (other ))
216219
217220 def transform_point (self , point ):
218- return self .__call__ ([point ])[0 ]
221+ return self .__call__ (npy . asarray ( [point ]) )[0 ]
219222
220223 def has_inverse (self ):
221224 raise NotImplementedError ()
@@ -229,8 +232,6 @@ def is_separable(self):
229232 def is_affine (self ):
230233 return False
231234
232- # MGDTODO: Separate out Affine2DBase / Affine2DConcrete so BlendedAffine and CompositeAffine don't have translate/scale/rotate members
233-
234235class Affine2DBase (Transform ):
235236 input_dims = 2
236237 output_dims = 2
@@ -246,7 +247,7 @@ def _do_invalidation(self):
246247
247248 #@staticmethod
248249 def _concat (a , b ):
249- return N .dot (b , a )
250+ return npy .dot (b , a )
250251 _concat = staticmethod (_concat )
251252
252253 def to_values (self ):
@@ -255,7 +256,7 @@ def to_values(self):
255256
256257 #@staticmethod
257258 def matrix_from_values (a , b , c , d , e , f ):
258- affine = N .zeros ((3 ,3 ), N .float_ )
259+ affine = npy .zeros ((3 ,3 ), npy .float_ )
259260 affine [0 ,] = a , c , e
260261 affine [1 ,] = b , d , f
261262 affine [2 ,2 ] = 1
@@ -267,7 +268,7 @@ def get_matrix(self):
267268
268269 def __call__ (self , points ):
269270 """
270- Applies the transformation to a set of 2D points and
271+ Applies the transformation to an array of 2D points and
271272 returns the result.
272273
273274 points must be a numpy array of shape (N, 2), where N is the
@@ -277,9 +278,9 @@ def __call__(self, points):
277278 # the points to an array in the first place. If we can use
278279 # more arrays upstream, that should help here.
279280 mtx = self .get_matrix ()
280- points = N .asarray (points , N .float_ )
281+ points = npy .asarray (points , npy .float_ )
281282 points = points .transpose ()
282- points = N .dot (mtx [0 :2 , 0 :2 ], points )
283+ points = npy .dot (mtx [0 :2 , 0 :2 ], points )
283284 points = points + mtx [0 :2 , 2 :]
284285 return points .transpose ()
285286
@@ -311,7 +312,7 @@ def __init__(self, matrix = None):
311312 """
312313 Affine2DBase .__init__ (self )
313314 if matrix is None :
314- matrix = N .identity (3 )
315+ matrix = npy .identity (3 )
315316 else :
316317 assert matrix .shape == (3 , 3 )
317318 self ._mtx = matrix
@@ -348,19 +349,19 @@ def concat(a, b):
348349
349350 #@staticmethod
350351 def identity ():
351- return Affine2D (N .identity (3 ))
352+ return Affine2D (npy .identity (3 ))
352353 identity = staticmethod (identity )
353354
354355 def rotate (self , theta ):
355- a = N .cos (theta )
356- b = N .sin (theta )
356+ a = npy .cos (theta )
357+ b = npy .sin (theta )
357358 rotate_mtx = self .matrix_from_values (a , b , - b , a , 0 , 0 )
358359 self ._mtx = self ._concat (self ._mtx , rotate_mtx )
359360 self .invalidate ()
360361 return self
361362
362363 def rotate_deg (self , degrees ):
363- return self .rotate (degrees * N .pi / 180. )
364+ return self .rotate (degrees * npy .pi / 180. )
364365
365366 def translate (self , tx , ty ):
366367 translate_mtx = self .matrix_from_values (1. , 0. , 0. , 1. , tx , ty )
@@ -420,7 +421,7 @@ def _make__mtx(self):
420421 # This works because we already know the transforms are
421422 # separable, though normally one would want to set b and
422423 # c to zero.
423- self ._mtx = N .vstack ((x_mtx [0 ], y_mtx [1 ], [0.0 , 0.0 , 1.0 ]))
424+ self ._mtx = npy .vstack ((x_mtx [0 ], y_mtx [1 ], [0.0 , 0.0 , 1.0 ]))
424425
425426 def is_separable (self ):
426427 return True
@@ -444,7 +445,7 @@ def __call__(self, points):
444445 y_points = self ._y (points )
445446 # This works because we already know the transforms are
446447 # separable
447- return N .hstack ((x_points [:, 0 :1 ], y_points [:, 1 :2 ]))
448+ return npy .hstack ((x_points [:, 0 :1 ], y_points [:, 1 :2 ]))
448449
449450class CompositeAffine2D (Affine2DBase ):
450451 def __init__ (self , a , b ):
@@ -579,6 +580,7 @@ def interval_contains_open(interval, val):
579580 return interval [0 ] < val and interval [1 ] > val
580581
581582if __name__ == '__main__' :
583+ import copy
582584 from random import random
583585 import timeit
584586
@@ -588,36 +590,38 @@ def interval_contains_open(interval, val):
588590 assert bbox .xmax == 20
589591 assert bbox .ymax == 25
590592
591- assert N .all (bbox .min == [10 , 15 ])
592- assert N .all (bbox .max == [20 , 25 ])
593- assert N .all (bbox .intervalx == (10 , 20 ))
594- assert N .all (bbox .intervaly == (15 , 25 ))
593+ assert npy .all (bbox .min == [10 , 15 ])
594+ assert npy .all (bbox .max == [20 , 25 ])
595+ assert npy .all (bbox .intervalx == (10 , 20 ))
596+ assert npy .all (bbox .intervaly == (15 , 25 ))
595597
596598 assert bbox .width == 10
597599 assert bbox .height == 10
598600
599- assert bbox .get_bounds () == (10 , 15 , 10 , 10 )
601+ assert bbox .bounds == (10 , 15 , 10 , 10 )
600602
603+ print npy .asarray (bbox )
604+
601605 bbox .intervalx = (11 , 21 )
602606 bbox .intervaly = (16 , 26 )
603607
604- assert bbox .get_bounds () == (11 , 16 , 10 , 10 )
608+ assert bbox .bounds == (11 , 16 , 10 , 10 )
605609
606610 bbox .xmin = 12
607611 bbox .ymin = 17
608612 bbox .xmax = 22
609613 bbox .ymax = 27
610614
611- assert bbox .get_bounds () == (12 , 17 , 10 , 10 )
615+ assert bbox .bounds == (12 , 17 , 10 , 10 )
612616
613617 bbox = Bbox .from_lbwh (10 , 11 , 12 , 13 )
614- assert bbox .get_bounds () == (10 , 11 , 12 , 13 )
618+ assert bbox .bounds == (10 , 11 , 12 , 13 )
615619
616- bbox_copy = bbox .copy ()
620+ bbox_copy = copy .copy (bbox )
617621 assert bbox == bbox_copy
618622 bbox_copy .max = (14 , 15 )
619- assert bbox .get_bounds () == (10 , 11 , 12 , 13 )
620- assert bbox_copy .get_bounds () == (10 , 11 , 4 , 4 )
623+ assert bbox .bounds == (10 , 11 , 12 , 13 )
624+ assert bbox_copy .bounds == (10 , 11 , 4 , 4 )
621625
622626 bbox1 = Bbox ([[10. , 15. ], [20. , 25. ]])
623627 bbox2 = Bbox ([[30. , 35. ], [40. , 45. ]])
@@ -634,7 +638,7 @@ def interval_contains_open(interval, val):
634638 - 0.49999999999999994 , 0.86602540378443871 ,
635639 0.0 , 0.0 )
636640
637- points = N .array ([[1 ,2 ],[3 ,4 ],[5 ,6 ],[7 ,8 ]], N .float_ )
641+ points = npy .array ([[1 ,2 ],[3 ,4 ],[5 ,6 ],[7 ,8 ]], npy .float_ )
638642 translated_points = translation (points )
639643 assert (translated_points == [[11. , 22. ], [13. , 24. ], [15. , 26. ], [17. , 28. ]]).all ()
640644 scaled_points = scale (points )
@@ -655,7 +659,7 @@ def interval_contains_open(interval, val):
655659 t = timeit .Timer ("trans_sum(points)" , "from __main__ import trans_sum, points" )
656660 print "Time to transform 10000 x 10 points as tuples:" , t .timeit (10 )
657661
658- points2 = N .asarray (points )
662+ points2 = npy .asarray (points )
659663 t = timeit .Timer ("trans_sum(points2)" , "from __main__ import trans_sum, points2" )
660664 print "Time to transform 10000 x 10 points as numpy array:" , t .timeit (10 )
661665
0 commit comments