Firststep changes
In order to use the compatibility layer there are still a few changes that need to be made to your code. Many of these changes can be made by running the alter_codel module with your code as input.
1. Importing (the alter_codel module handles all these changes)
(a) import Numeric -> import numpy.oldnumeric as Numeric
(b) import Numeric as XX -> import numpy.oldnumeric as XX
(c) from Numeric import <name1>,...<nameN> -> from numpy.oldnumeric import <name1>,...,<nameN>
(d) from Numeric import * -> from numpy.oldnumeric import *
(e) Similar name changes need to be made for Matrix, MLab, UserAr-ray, LinearAlgebra, RandomArray RNG, RNG.Statistics, and FFT. The new names are numpy.oldnumeric.<pkg> where <pkg> is matrix, mlab, user_array, linear_algebra, random_array, rng, rngjstats, and fft.
(f) multiarray and umath (if you used them directly) are now numpy.core.multiarray and numpy.core.umath, but it is more future proof to replace usages of these internal modules with numpy.oldnumeric.
2. Method name changes and methods converted to attributes. The alter_codel module handles all these changes.
(b) arr.iscontiguous() -> arr.flags.contiguous
(f) arr.spacesaver() eliminated
(g) arr.savespace() eliminated
3. Some of the typecode characters have changed to be more consistent with other Python modules (array and struct). You should only notice this change if you used the actual typecode characters (instead of the named constants). The alter_codel module will change uses of 'b' to 'B' for internal Numeric functions that it knows about because NumPy will interpret 'b' to mean a signed byte type (instead of the old unsigned). It will also change the character codes when they are used explicitly in the .astype method. In the compatibility layer (and only in the compatibility layer), typecode-requiring function calls (e.g. zeros, array) understand the old typecode characters.
The changes are (Numeric -> NumPy):
4. arr.flat now returns an indexable 1-D iterator. This behaves correctly when passed to a function, but if you expected methods or attributes on arr.flat — besides .copy() — then you will need to replace arr.flat with arr.ravel() (copies only when necessary) or arr.flattenQ (always copies). The alter_codel module will change arr.flat to arr.ravel() unless you used the construct arr.flat = obj or arr.flat[ind].
5. If you used type-equality testing on the objects returned from arrays, then you need to change this to isinstance testing. Thus type(a[0]) is float or type(a[0]) == float should be changed to isinstance(a[0], float). This is because array scalar objects are now returned from arrays. These inherit from the Python scalars where they can, but define their own methods and attributes. This conversion is done by alter_codel for the types (float, int, complex, and Ar-rayType)
6. If your code should produce 0-d arrays. These no-longer have a length as they should be interpreted similarly to real scalars which don't have a length.
7. Arrays cannot be tested for truth value unless they are empty (returns False) or have only one element. This means that if Z: where Z is an array will fail (unless Z is empty or has only one element). Also the 'and' and 'or' operations (which test for object truth value) will also fail on arrays of more than one element. Use the .any() and .all() methods to test for truth value of an array.
8. Masked arrays return a special nomask object instead of None when there is no mask on the array for the functions getmask and attribute access arr.mask
9. Masked array functions have a default axis of None (meaning ravel), make sure to specify an axis if your masked arrays are larger than 1-d.
10. If you used the construct arr . shape=<tuple>, this will not work for array scalars (which can be returned from array operations). You cannot set the shape of an array-scalar (you can read it though). As a result, for more general code you should use arr=arr.reshape (<tuple>) which works for both array-scalars and arrays.
The alter_codel script should handle the changes outlined in steps 1-5 above. The final incompatibilities in 6-9 are less common and must be modified by hand if necessary.
Post a comment