Inconsistency between numpy and flex (sclice notation)
Hi, I've found a minor inconveniance using the flex type: test case: import numpy as np np_grid=np.double(np.zeros((3,7))) np_grid[1,3]=3.0 print(np_grid) # works print(np_grid[1,:]) # works from cctbx.array_family import flex grid=flex.double(flex.grid(3,7)) grid[1,3]=3.0 flex.show(grid) #works flex.show(grid[1,:]) #gives error (see below) "TypeError: All items must be of same type." Is there another simple way to do sclices with flex arrays or could this be implemented the same way as in numpy by someone who is more into c++ than myself? Thanks, Jan
On 2 Jan 2012, at 18:40, Jan Marten Simons wrote:
Is there another simple way to do sclices with flex arrays or could this be implemented the same way as in numpy by someone who is more into c++ than myself?
This has been on my todo list with a low priority for a long while! At the moment, I can only think of the following to get one row of the matrix grid.matrix_copy_block(i_row=1, i_column=0, n_rows=1, n_columns=grid.all()[1]) To get one column, you have the less verbose grid.matrix_copy_column Adding a grid.matrix_copy_row is a 2 min exercise. Adding proper support of 2d slicing is a few hours work. Note that contrary to numpy, those methods copy the row/column into a new array. And that when 2d slicing will get implemented into the cctbx, grid[1,:] will also return a new array instead of a mere proxy to the elements of grid, as it is the philosophy of the flex arrays. HtH, Luc Bourhis
I think that the following does more or less what you want:
flex.show(grid[1:2,:])
Currently the multidimensional slicing only accepts slices as all the
arguments, not a mixture of slices and integers.
Richard
2012/1/2 Jan Marten Simons
Hi,
I've found a minor inconveniance using the flex type:
test case:
import numpy as np np_grid=np.double(np.zeros((3,7))) np_grid[1,3]=3.0 print(np_grid) # works print(np_grid[1,:]) # works
from cctbx.array_family import flex grid=flex.double(flex.grid(3,7)) grid[1,3]=3.0 flex.show(grid) #works flex.show(grid[1,:]) #gives error (see below)
"TypeError: All items must be of same type."
Is there another simple way to do sclices with flex arrays or could this be implemented the same way as in numpy by someone who is more into c++ than myself?
Thanks, Jan _______________________________________________ cctbxbb mailing list [email protected] http://phenix-online.org/mailman/listinfo/cctbxbb
On 2 Jan 2012, at 19:47, Richard Gildea wrote:
I think that the following does more or less what you want:
flex.show(grid[1:2,:])
Currently the multidimensional slicing only accepts slices as all the arguments, not a mixture of slices and integers.
aha! I see that in June, you have implemented multidimensional slicing indeed. I hadn't noticed. Disregard my suggestion then, Jan, but my remark about copying versus proxying is still relevant. Luc
Am Montag 02 Januar 2012 21:05:58 schrieb Luc Bourhis:
On 2 Jan 2012, at 19:47, Richard Gildea wrote:
I think that the following does more or less what you want:
flex.show(grid[1:2,:])
Currently the multidimensional slicing only accepts slices as all the arguments, not a mixture of slices and integers.
aha! I see that in June, you have implemented multidimensional slicing indeed. I hadn't noticed. Disregard my suggestion then, Jan, but my remark about copying versus proxying is still relevant.
Ok, so if I want to do many (writing) operations on sclices of the miller array it would be best to copy/convert it into a numpy array first, so I don't have the overhead of the individual copy operations then. Thanks, Dipl. Phys. Jan M. Simons Institute of Crystallography RWTH Aachen University
On 3 Jan 2012, at 16:29, Jan Marten Simons wrote:
Ok, so if I want to do many (writing) operations on sclices of the miller array it would be best to copy/convert it into a numpy array first, so I don't have the overhead of the individual copy operations then.
No, write operations are done directly on the array on which you use the slicing notation: grild[1:2, :] = flex.double(...) will incur the memory allocation of the right-hand side but then there is no extra temporary array involved. My point was just that in: for x in grid[1:2, :]: .... there is the allocation and initialisation of a temporary array that holds the slice. In numpy, grid[1:2,:] would behave like an iterator in this context and there would be no temporary array. Luc
participants (3)
-
Jan Marten Simons
-
Luc Bourhis
-
Richard Gildea