Changeset 6456
- Timestamp:
- Mar 5, 2009, 8:29:28 AM (16 years ago)
- Location:
- branches/numpy/anuga/utilities
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/numpy/anuga/utilities/numerical_tools.py
r6441 r6456 249 249 typecode: numeric type. If specified, use this in the conversion. 250 250 If not, let numeric package decide. 251 numpy assumes float64 if no type in A.252 251 typecode will always be one of num.float, num.int, etc. 253 252 … … 258 257 """ 259 258 260 if isinstance(A, basestring): 261 msg = 'Sorry, cannot handle string in ensure_numeric()' 259 #if isinstance(A, basestring): 260 if isinstance(A, str): 261 msg = 'ASorry, cannot handle strings in ensure_numeric()' 262 262 raise Exception, msg 263 264 try: 265 elt = A[0] 266 except TypeError: 267 pass 268 else: 269 if isinstance(A, basestring): 270 msg = 'BSorry, cannot handle strings in ensure_numeric()' 271 raise Exception, msg 272 263 273 264 274 if typecode is None: … … 271 281 272 282 273 274 275 283 def histogram(a, bins, relative=False): 276 284 """Standard histogram straight from the numeric manual -
branches/numpy/anuga/utilities/test_numerical_tools.py
r6441 r6456 97 97 assert A is B #Same object 98 98 99 # THIS FAILS! ASSUMED TYPE IS num.int!? 100 # # check default num.array type, which is supposed to be num.float 101 # A = num.array((1,2,3,4)) 102 # assert isinstance(A, num.ndarray) 103 # assert A.dtype.char == 'd', \ 104 # "Expected dtype='d', got '%s'" % A.dtype.char 99 # check default num.array type, which is supposed to be num.int32 100 A = num.array((1,2,3,4)) 101 assert isinstance(A, num.ndarray) 102 msg = "Expected dtype.char='l', got '%s'" % A.dtype.char 103 assert A.dtype.char == 'l', msg 105 104 106 105 A = num.array([1,2,3,4]) … … 116 115 B = ensure_numeric(A, num.float) 117 116 assert num.alltrue(A == B) 118 # print 'A=%s' % str(A)119 # print 'B=%s, B.shape=%s' % (str(B), str(B.shape))120 117 121 118 B = ensure_numeric(A, num.int) 122 119 assert num.alltrue(A == B) 123 120 124 # try to simulate getting (x,0) shape 125 data_points = [[ 413634. ],] 126 array_data_points = ensure_numeric(data_points) 127 #if not (0,) == array_data_points.shape: 128 # assert len(array_data_points.shape) == 2 129 # assert array_data_points.shape[1] == 2 130 121 # # try to simulate getting (x,0) shape 122 # data_points = [[ 413634. ],] 123 # array_data_points = ensure_numeric(data_points) 124 # if not (0,) == array_data_points.shape: 125 # assert len(array_data_points.shape) == 2 126 # assert array_data_points.shape[1] == 2 127 128 # strings input should raise exception 129 self.failUnlessRaises(Exception, ensure_numeric(['abc',])) 130 self.failUnlessRaises(Exception, ensure_numeric(('abc',))) 131 self.failUnlessRaises(Exception, ensure_numeric(num.array(('abc',)))) 131 132 132 133 def NO_test_ensure_numeric_char(self): … … 468 469 ## 469 470 # @brief Test to see if ensure_numeric() behaves as we expect. 470 # @note Under Numeric ensure_numeric() *always* returned a copy .471 # @note Under Numeric ensure_numeric() *always* returned a copy (bug). 471 472 # Under numpy it copies only when it has to. 472 473 def test_ensure_numeric_copy(self): … … 524 525 ##### 525 526 # Make 'points' a numeric array of int coordinates. 526 # Should be changed by ensure_numeric( ).527 # Should be changed by ensure_numeric(, num.float). 527 528 ##### 528 529 points = num.array([[1,2], [3,4], [5,6]], num.int) … … 551 552 msg = 'ensure_numeric() should return the original input' 552 553 self.failUnless(points_new_id == points_id, msg) 554 555 # should never change it's input parameter 556 msg = "ensure_numeric() changed it's input parameter" 557 self.failUnless(points_id == id(points), msg) 558 559 ##### 560 # Make 'points' a numeric array of float32 coordinates. 561 # Should NOT be changed by ensure_numeric(, num.float32). 562 ##### 563 points = num.array([[1.,2.], [3.,4.], [5.,6.]], num.float32) 564 points_id = id(points) 565 566 points_new = ensure_numeric(points, num.float32) 567 points_new_id = id(points_new) 568 569 msg = 'ensure_numeric() should return the original input' 570 self.failUnless(points_new_id == points_id, msg) 571 572 # should never change it's input parameter 573 msg = "ensure_numeric() changed it's input parameter" 574 self.failUnless(points_id == id(points), msg) 575 576 ##### 577 # Make 'points' a numeric array of float32 coordinates. 578 # Should be changed by ensure_numeric(, num.float64). 579 ##### 580 points = num.array([[1.,2.], [3.,4.], [5.,6.]], num.float32) 581 points_id = id(points) 582 583 points_new = ensure_numeric(points, num.float64) 584 points_new_id = id(points_new) 585 586 msg = 'ensure_numeric() should return a copy of the input' 587 self.failUnless(points_new_id != points_id, msg) 588 589 # should never change it's input parameter 590 msg = "ensure_numeric() changed it's input parameter" 591 self.failUnless(points_id == id(points), msg) 592 593 ##### 594 # Make 'points' a numeric array of float coordinates. 595 # Should NOT be changed by ensure_numeric(, num.float64). 596 ##### 597 points = num.array([[1.,2.], [3.,4.], [5.,6.]], num.float) 598 points_id = id(points) 599 600 points_new = ensure_numeric(points, num.float64) 601 points_new_id = id(points_new) 602 603 msg = 'ensure_numeric() should return the original input' 604 self.failUnless(points_new_id == points_id, msg) 605 #msg = 'ensure_numeric() should return a copy of the input' 606 #self.failUnless(points_new_id != points_id, msg) 607 608 # should never change it's input parameter 609 msg = "ensure_numeric() changed it's input parameter" 610 self.failUnless(points_id == id(points), msg) 611 612 ##### 613 # Make 'points' a numeric array of float coordinates. 614 # Should be changed by ensure_numeric(, num.float96). 615 ##### 616 points = num.array([[1.,2.], [3.,4.], [5.,6.]], num.float) 617 points_id = id(points) 618 619 points_new = ensure_numeric(points, num.float96) 620 points_new_id = id(points_new) 621 622 msg = 'ensure_numeric() should return a copy of the input' 623 self.failUnless(points_new_id != points_id, msg) 553 624 554 625 # should never change it's input parameter
Note: See TracChangeset
for help on using the changeset viewer.