resize in old Numeric
Hi,
For compatibility reasons (work with Biopython) I would like to use to
Numeric in some code of mine. (Of course, I could make a little detour
converting into numpy.array, but first I wonder whether somebody might
know a solution for the problem with Numeric.)
I need to flatten a simple 3x3 array and try to do that with resize.
First time works, next time it doesn't. Here is an example:
In [1]: from Numeric import *
In [2]: a = array([[ 0.50622966, -0.54764389, -0.66619644],
[ 0.44338279, -0.4973092, 0.7457228 ],[-0.73969131, -0.67288704,
-0.00893311]])
In [3]: a.resize((9,))
Out[3]:
array([ 0.50622966, -0.54764389, -0.66619644, 0.44338279, -0.4973092 ,
0.7457228 ,
-0.73969131, -0.67288704, -0.00893311])
In [4]: a.resize((9,))
---------------------------------------------------------------------------
On Dec 12, 2007 11:39 AM, Christian Meesters
Hi,
For compatibility reasons (work with Biopython) I would like to use to Numeric in some code of mine. (Of course, I could make a little detour converting into numpy.array, but first I wonder whether somebody might know a solution for the problem with Numeric.)
I need to flatten a simple 3x3 array and try to do that with resize. First time works, next time it doesn't. Here is an example:
The first thing that I would try is switching from resize to reshape. In general, you shouldn't be using resize except for certain odd applications as it can potentially result in reallocating the data. I'm pretty sure what you want is reshape.
In [1]: from Numeric import *
In [2]: a = array([[ 0.50622966, -0.54764389, -0.66619644], [ 0.44338279, -0.4973092, 0.7457228 ],[-0.73969131, -0.67288704, -0.00893311]])
In [3]: a.resize((9,)) Out[3]: array([ 0.50622966, -0.54764389, -0.66619644, 0.44338279, -0.4973092 , 0.7457228 , -0.73969131, -0.67288704, -0.00893311])
In [4]: a.resize((9,))
---------------------------------------------------------------------------
Traceback (most recent call last) /home/cm/Documents/projects/SAXS/Eury/MC_rigid/<ipython console> in <module>()
: cannot resize an array that has been referenced or is referencing another array in this way. Use the resize function. Unfortunately, when applying resize on the actual array I have, I get a similar, yet slightly different error:
Traceback (most recent call last): File "sym_get.py", line 37, in <module> s.inquire_relation() File "/usr/lib/python2.5/site-packages/mc_saxs/mc_rigid.py", line 915, in inquire_relation print rotation, rotation.resize((9,)) ValueError: resize only works on contiguous arrays
The arrays 'a' and 'rotation' are identical.
Any ideas, what's going on or how to solve this? Numeric version is: 24.2
TIA Christian
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- . __ . |-\ . . tim.hochberg@ieee.org
Hi Christiaan On Wed, Dec 12, 2007 at 07:39:49PM +0100, Christian Meesters wrote:
I need to flatten a simple 3x3 array and try to do that with resize. First time works, next time it doesn't. Here is an example:
In [1]: from Numeric import *
In [2]: a = array([[ 0.50622966, -0.54764389, -0.66619644], [ 0.44338279, -0.4973092, 0.7457228 ],[-0.73969131, -0.67288704, -0.00893311]])
In [3]: a.resize((9,)) Out[3]: array([ 0.50622966, -0.54764389, -0.66619644, 0.44338279, -0.4973092 , 0.7457228 , -0.73969131, -0.67288704, -0.00893311])
In [4]: a.resize((9,)) ---------------------------------------------------------------------------
Traceback (most recent call last)
You'll notice that this doesn't happen when you run those commands as a script. IPython remembers the answer to the last command (as '_' and in the output history), hence the reference that Numeric complains about.
Unfortunately, when applying resize on the actual array I have, I get a similar, yet slightly different error:
Traceback (most recent call last): File "sym_get.py", line 37, in <module> s.inquire_relation() File "/usr/lib/python2.5/site-packages/mc_saxs/mc_rigid.py", line 915, in inquire_relation print rotation, rotation.resize((9,)) ValueError: resize only works on contiguous arrays
The arrays 'a' and 'rotation' are identical.
They may have identical values without having identical layout in memory. Try a.ascontiguousarray(). Cheers Stéfan
Thank you all for your valuable input. Learned something 'bout Numeric again. And my problem is solved ;-). Thanks Christian
Christian Meesters wrote:
Hi,
For compatibility reasons (work with Biopython) I would like to use to Numeric in some code of mine. (Of course, I could make a little detour converting into numpy.array, but first I wonder whether somebody might know a solution for the problem with Numeric.)
I need to flatten a simple 3x3 array and try to do that with resize. First time works, next time it doesn't. Here is an example:
In [1]: from Numeric import *
In [2]: a = array([[ 0.50622966, -0.54764389, -0.66619644], [ 0.44338279, -0.4973092, 0.7457228 ],[-0.73969131, -0.67288704, -0.00893311]])
In [3]: a.resize((9,)) Out[3]: array([ 0.50622966, -0.54764389, -0.66619644, 0.44338279, -0.4973092 , 0.7457228 , -0.73969131, -0.67288704, -0.00893311])
In [4]: a.resize((9,)) ---------------------------------------------------------------------------
Traceback (most recent call last) /home/cm/Documents/projects/SAXS/Eury/MC_rigid/<ipython console> in <module>()
: cannot resize an array that has been referenced or is referencing another array in this way. Use the resize function. Unfortunately, when applying resize on the actual array I have, I get a similar, yet slightly different error:
The problem is that Numeric has no way of knowing whether a reference held is safe or not. In other-words, is the reference because of a view into a part of the array or a simple name binding (like having the '_' variable in the interpreter hold on to the old reference). A reallocation of the memory while other views are expecting to be able to walk over the old pointer is a recipe for problems and is the reason that Numeric has this restriction. NumPy has the same restriction but allows you to 'over-ride' it if you know what you are doing. To fix the problem, you need to get rid of all other references to the array that you want to resize. -Travis O.
participants (4)
-
Christian Meesters
-
Stefan van der Walt
-
Timothy Hochberg
-
Travis E. Oliphant