Re: [Numpy-discussion] Change default order to Fortran order
Thank you all for replying!
I did a quick test, using python 2.6.6, and the original numpy package on my Linux computer without any change.==
x = np.zeros((2,3),dtype=np.int32,order='F')
print "x.strides ="
print x.strides
y = x + 1
print "y.strides ="
print y.strides
==
Output:
--------
x.strides =
(4, 8)
y.strides =
(12, 4)
--------
So, basically, "x" is Fortran-style column-major (because I explicitly write order='F'), but "y" is C-style row-major. This is going to be very annoying. What I really want is:
- I do not have to write order='F' explicitly when declaring "x"
- both "x" and "y" are Fortran-style column-major
Which file should I modify to achieve this goal?
Right now, I am just trying to get some basic stuff working with all arrays default to Fortran-style, and I can worry about interfacing with other code/libraries later.
Thanks,
Kang
On 08/02/15, Sebastian Berg
Well, numpy has a tendency to prefer C order. There is nothing you can do about that really. But you just cannot be sure what you get in some cases. Often you need something specific for interfaceing other code. But in that case quite often you also do not need to fear the copy.
- Sebastian
On Sun Aug 2 16:27:08 2015 GMT+0200, Sturla Molden wrote:
On 02/08/15 15:55, Kang Wang wrote:
Can anyone provide any insight/help?
There is no "default order". There was before, but now all operators control the order of their return arrays from the order of their input array. The only thing that makes C order "default" is the keyword argument to np.empty, np.ones and np.zeros. Just monkey patch those functions and it should be fine.
Sturla
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Kang Wang, Ph.D. 1111 Highland Ave., Room 1113 Madison, WI 53705-2275 TEL 608-263-0066 http://www.medphysics.wisc.edu/~kang/ ----------------------------------------
On 02/08/15 22:14, Kang Wang wrote:
Thank you all for replying!
I did a quick test, using python 2.6.6, and the original numpy package on my Linux computer without any change. == x = np.zeros((2,3),dtype=np.int32,order='F') print "x.strides =" print x.strides
y = x + 1 print "y.strides =" print y.strides ==
Output: -------- x.strides = (4, 8) y.strides = (12, 4) --------
Update NumPy. This is the behavior I talked about that has changed. Now NumPy does this: In [21]: x = np.zeros((2,3),dtype=np.int32,order='F') In [22]: y = x + 1 In [24]: x.strides Out[24]: (4, 8) In [25]: y.strides Out[25]: (4, 8) Sturla
And to eliminate the order kwarg, use functools.partial to patch the zeros function (or any others, as needed): In [26]: import numpy as np In [27]: from functools import partial In [28]: np.zeros = partial(np.zeros, order="F") In [29]: x = np.zeros((2,3), dtype=np.int32) In [30]: y = x + 1 In [31]: x.strides Out[31]: (4, 8) In [32]: y.strides Out[32]: (4, 8) In [33]: np.__version__ Out[33]: '1.9.2' Bryan
On Aug 2, 2015, at 3:22 PM, Sturla Molden
wrote: On 02/08/15 22:14, Kang Wang wrote:
Thank you all for replying!
I did a quick test, using python 2.6.6, and the original numpy package on my Linux computer without any change. == x = np.zeros((2,3),dtype=np.int32,order='F') print "x.strides =" print x.strides
y = x + 1 print "y.strides =" print y.strides ==
Output: -------- x.strides = (4, 8) y.strides = (12, 4) --------
Update NumPy. This is the behavior I talked about that has changed.
Now NumPy does this:
In [21]: x = np.zeros((2,3),dtype=np.int32,order='F')
In [22]: y = x + 1
In [24]: x.strides Out[24]: (4, 8)
In [25]: y.strides Out[25]: (4, 8)
Sturla
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On 02/08/15 22:28, Bryan Van de Ven wrote:
And to eliminate the order kwarg, use functools.partial to patch the zeros function (or any others, as needed):
This will probably break code that depends on NumPy, like SciPy and scikit-image. But if NumPy is all that matters, sure go ahead and monkey patch. Otherwise keep the patched functions in another namespace. :-) Sturla
On Sun, Aug 2, 2015 at 1:46 PM, Sturla Molden
On 02/08/15 22:28, Bryan Van de Ven wrote:
And to eliminate the order kwarg, use functools.partial to patch the zeros function (or any others, as needed):
This will probably break code that depends on NumPy, like SciPy and scikit-image. But if NumPy is all that matters, sure go ahead and monkey patch. Otherwise keep the patched functions in another namespace.
I"d be really careful about this -- sure it's annoying, but a kind of global change of behavior could wreak havok. I'd create a set of Fortran-order constructors -- if it were me, I do: fzeros fones, etc..... but you could, I suppose, create a namespace and ut hem all there, then create a fnumpy that would write those over: import numpy as np and away you go -- but that wouldn't change any code that imports numpy in the usual way. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
On Aug 2, 2015 1:17 PM, "Kang Wang"
Thank you all for replying!
I did a quick test, using python 2.6.6,
There's pretty much no good reason these days to be using python 2.6 (which was released in *2008*). I assume you're using it because you're using redhat or some redhat derivative, and that's what they ship by default? Even redhat engineers officially recommend that users *not* use the default python -- it's basically only intended for use by their own built-in system management scripts. If you're just getting started with python, then at this point I'd recommend starting with python 3.4. Some easy ways to get this installed: - Anaconda: the most popular scientific python distribution -- you pretty much just download one file and get a full, up to date setup of python and all the main scientific packages, in your home directory. Supported on all popular platforms. Trivial to use and requires no special permissions. http://continuum.io/downloads#py34 - One of Anaconda's competitors: http://www.scipy.org/install.html - Software collections: redhat's official way to do things like this: https://www.softwarecollections.org/en/ -n
participants (5)
-
Bryan Van de Ven
-
Chris Barker
-
Kang Wang
-
Nathaniel Smith
-
Sturla Molden