thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

slicing and write (Trac #2134) #5930

Open numpy-gitbot opened 12 years ago

numpy-gitbot commented 12 years ago

Original ticket http://projects.scipy.org/numpy/ticket/2134 on 2012-05-16 by trac user vascot, assigned to unknown.

Writing sliced arrays using Pythons buildin open function does not work as expected. The wrong slice is written.

Example code:

import numpy as np
foo = np.fromfunction(lambda x,y,z: x+10*y+100*z, (3,3,3))
print foo[:,:,2]
f = open('tmp','wb')
f.write(foo[:,:,2])
f.close()
print np.fromfile('tmp')

Gives output:

[[ 200.  210.  220.]
 [ 201.  211.  221.]
 [ 202.  212.  222.]]

[ 200.   10.  110.  210.   20.  120.  220.    1.  101.]

The second array should contain the same items as the first.

A working example is first copying the slice:

import numpy as np
foo = np.fromfunction(lambda x,y,z: x+10*y+100*z, (3,3,3))
print foo[:,:,2]
f = open('tmp','wb')
f.write(foo[:,:,2].copy())
f.close()
print np.fromfile('tmp')

Output:

[[ 200.  210.  220.]
 [ 201.  211.  221.]
 [ 202.  212.  222.]]

[ 200.  210.  220.  201.  211.  221.  202.  212.  222.]

As expected.