Skip to content
This repository

Allow strict casting #38

Closed
wants to merge 2 commits into from

3 participants

Mark Charles Harris Ralf Gommers
Mark
mwiebe commented June 15, 2011

This fixes a bug in netcdf, and tweaks ndimage so that it will work with a tighter default casting policy in NumPy. The errors are discussed here:

http://mail.scipy.org/pipermail/numpy-discussion/2011-June/056632.html

added some commits June 15, 2011
BUG: netcdf: was treating the NumPy typecode as if it were a NumPy kind
Typecodes are always one character, and kinds are added with a size.
'd' is the same as 'f8', for example. The code was producing 'd8',
which is invalid.
04fb79d
ENH: ndimage: specify unsafe casting so the default in NumPy can be t…
…ightened
dffed56
Charles Harris
Collaborator

Looks OK to me.

Ralf Gommers
Owner

Tested with 1.6.1rc2 and 2.0.0-latest and committed. Thanks Mark.

Ralf Gommers rgommers closed this July 03, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Jun 15, 2011
BUG: netcdf: was treating the NumPy typecode as if it were a NumPy kind
Typecodes are always one character, and kinds are added with a size.
'd' is the same as 'f8', for example. The code was producing 'd8',
which is invalid.
04fb79d
ENH: ndimage: specify unsafe casting so the default in NumPy can be t…
…ightened
dffed56
This page is out of date. Refresh to see the latest.
14  scipy/io/netcdf.py
@@ -454,11 +454,7 @@ def _write_values(self, values):
454 454
                 if isinstance(sample, class_): break
455 455
 
456 456
         typecode, size = TYPEMAP[nc_type]
457  
-        if typecode is 'c':
458  
-            dtype_ = '>c'
459  
-        else:
460  
-            dtype_ = '>%s' % typecode
461  
-            if size > 1: dtype_ += str(size)
  457
+        dtype_ = '>%s' % typecode
462 458
 
463 459
         values = asarray(values, dtype=dtype_)
464 460
 
@@ -627,11 +623,7 @@ def _read_var(self):
627 623
         begin = [self._unpack_int, self._unpack_int64][self.version_byte-1]()
628 624
 
629 625
         typecode, size = TYPEMAP[nc_type]
630  
-        if typecode is 'c':
631  
-            dtype_ = '>c'
632  
-        else:
633  
-            dtype_ = '>%s' % typecode
634  
-            if size > 1: dtype_ += str(size)
  626
+        dtype_ = '>%s' % typecode
635 627
 
636 628
         return name, dimensions, shape, attributes, typecode, size, dtype_, begin, vsize
637 629
 
@@ -646,7 +638,7 @@ def _read_values(self):
646 638
         self.fp.read(-count % 4)  # read padding
647 639
 
648 640
         if typecode is not 'c':
649  
-            values = fromstring(values, dtype='>%s%d' % (typecode, size))
  641
+            values = fromstring(values, dtype='>%s' % typecode)
650 642
             if values.shape == (1,): values = values[0]
651 643
         else:
652 644
             values = values.rstrip(asbytes('\x00'))
6  scipy/ndimage/filters.py
@@ -447,7 +447,11 @@ def generic_gradient_magnitude(input, derivative, output = None,
447 447
                              *extra_arguments, **extra_keywords)
448 448
             numpy.multiply(tmp, tmp, tmp)
449 449
             output += tmp
450  
-        numpy.sqrt(output, output)
  450
+        # This allows the sqrt to work with a different default casting
  451
+        if numpy.version.short_version > '1.6.1':
  452
+            numpy.sqrt(output, output, casting='unsafe')
  453
+        else:
  454
+            numpy.sqrt(output, output)
451 455
     else:
452 456
         output[...] = input[...]
453 457
     return return_value
4  scipy/ndimage/tests/test_ndimage.py
@@ -669,7 +669,7 @@ def test_gaussian_gradient_magnitude01(self):
669 669
             output = ndimage.gaussian_gradient_magnitude(array,
670 670
                                                                        1.0)
671 671
             expected = tmp1 * tmp1 + tmp2 * tmp2
672  
-            numpy.sqrt(expected, expected)
  672
+            expected = numpy.sqrt(expected).astype(type)
673 673
             assert_array_almost_equal(expected, output)
674 674
 
675 675
     def test_gaussian_gradient_magnitude02(self):
@@ -684,7 +684,7 @@ def test_gaussian_gradient_magnitude02(self):
684 684
             ndimage.gaussian_gradient_magnitude(array, 1.0,
685 685
                                                            output)
686 686
             expected = tmp1 * tmp1 + tmp2 * tmp2
687  
-            numpy.sqrt(expected, expected)
  687
+            expected = numpy.sqrt(expected).astype(type)
688 688
             assert_array_almost_equal(expected, output)
689 689
 
690 690
     def test_generic_gradient_magnitude01(self):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.