Anybody knows a way to get a str object (bytes for Python >= 2.6) out of
a buffer object (i.e. the .data attribute of ndarrays) without copying
I need this for avoid creating a new function that deals with buffer
objects (instead of reusing the one for str/byte objects that I already
have). So, a matter of laziness :-)
I just ran across the problem of priorities with ndarrays again and it
keeps biting me. I did once ago a workaround to get my ``undarray``
class's methods be called when being the second operand of e.g.
<ndarray> + <undarray>. But since I wrote it, always Python crashes
on exit with the message:
Python-32(68665) malloc: *** error for object 0x239680: incorrect
checksum for freed object - object was probably modified after being
*** set a breakpoint in malloc_error_break to debug
(Of course only if I imported the module. Occasionally I also
observed Bus errors, and even segfaults.)
I overloaded the numpy ops via numpy.set_numeric_ops() with
self-written classes, which are *not* derived from numpy.ufunc, and do
not resemble numpy ufuncs completely.
So I want to do it properly this time.
I therefore started with writing a Letter of Intent, and put it online
on http://github.com/friedrichromstedt/priops .
P.S.: I will start coding anyway, but it would be nice.
P.P.S.: The package this originates from is also online, under
http://upy.sourceforge.net. I will probably create a small example
script demonstrating the crash.
I have a small patch to absoft.py:
Compiling my fortran extension using f2py and distutils from Numpy 1.5, with
Python 2.7 on Windows, using Absoft v11.0 and MSVC 2008 Pro, first, it
complained about not finding the library U77.lib. Removing this from the
library list (in absoft.py) gave me this error:
error LNK2001: unresolved external symbol __absoft_s_cat
This symbol is now exported from amisc.lib, and after adding this to the
library list everything works fine.
The end result is the following patch: (or just replacing U77 with amisc)
Fri Sep 24 15:10:57 2010
+++ C:/Python27/Lib/site-packages/numpy/distutils/fcompiler/absoft.py Fri
Sep 24 15:09:52 2010
@@ -91,7 +91,9 @@
opt = FCompiler.get_libraries(self)
- if self.get_version() >= '10.0':
+ if self.get_version() >= '11.0':
+ opt.extend(['af90math', 'afio', 'af77math', 'amisc'])
+ elif self.get_version() >= '10.0':
opt.extend(['af90math', 'afio', 'af77math', 'U77'])
elif self.get_version() >= '8.0':
BTW, the performance increase compared to gfortran v4.5.0 is really
noticeable, perhaps more than 50%.
I couldn't find an answer to my newbie question, so I'm posting it here.
Via broadcasting, I know that
Being a recent convert from MATLAB, I expected the same result from a*b[:,0],
assuming b[:,0] would be the column vector [,].
Unfortunately, I was wrong. b[:,0] is apparently a 1-rank array of shape (2,).
This causes a*b[:,0] to evaluate as
a*numpy.array([[5,7]])=numpy.array([[5,14],[15,28]]) instead of
To get the result I desire, the only way I've been able to come up with is
to "coerce" b[:,0] into a column vector. Is there an easier way to do this,
without having to do the reshape?
I find similar things happen when I use other operations (e.g. "sum") that also
seem to reduce the array rank.
For example, I would expect numpy.sum(b,1) to also be a "column vector," but it
also evaluates to a 1-rank array [11, 15] with shape (2,)
Any thoughts, suggestions?
It seems that lib2to3 does not process the main f2py bootstrap script
that gets autogenerated by f2py's setup.py. The trivial patch below
replaces the print statements with sys.stderr.write() calls. After
that change, f2py works just fine in Python 3.2
--- numpy/f2py/setup.py (revision 8716)
+++ numpy/f2py/setup.py (working copy)
@@ -62,7 +62,7 @@
except ValueError: pass
- print >> sys.stderr, "G3 f2py support is not implemented, yet."
+ sys.stderr.write("G3 f2py support is not implemented, yet.\n")
from f2py2e import main
@@ -72,7 +72,7 @@
from numpy.f2py import main
- print >> sys.stderr, "Unknown mode:",`mode`
+ sys.stderr.write("Unknown mode: '%s'\n" % mode)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
I'm trying to do something ... unusual.
gdb support scripting with Python. From within my python script, I can
get the address of a contiguous area of memory that stores a fortran
array. I want to creat a NumPy array using "frombuffer". I see that
the CPython API supports the creation of a buffer, but, is there an
easier, more direct, way?
See below code pls,
import numpy as np
x = np.zeros((2,),dtype=('i4,f4,a10'))
x[:] = [(1,2.,'Hello'),(2,3.,"World")]
y = x['f2']
x['f2'] = y
#array([(1, 2.0, 'Hello'), (2, 3.0, 'World')],
dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])
y = y.astype('object')
array([Hello, World], dtype=object)
x['f2'] = y
array([(1, 2.0, 'HellWorld'), (2, 3.0, '\x00\x00\x00\x00\x00\x00\x18')],
dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '|S10')])
##here comes the problem: the f2 col type has not been changed and the
data is not I wanted...
here is why I need using this:
suppose I have a datasource, csv, sql based db or what ever look like this:
1, 2.0, 'Hello'
2, 3.0, 'World'
3, 2.0, 'other string'
I want to read them to a numpy array and process it's columns, it has no
problem for processing the float or int type but string.
After reading the manual and found the object dtype may store variable
string then I want to exact the string col into an new array, try to
process it then store back to the numpy "matrix" then store it back to
the data source.
May I know how I can do that? I do not care performance now.
Thanks for any hints
I have an indexing problem which I suspect has a simple solution, but
I've not been able to piece together various threads I've read on this
list to solve.
I have an 80x1200x1200 nd.array of floats this_par. I have a
1200x1200 boolean array idx, and an 80-element float array pars. For
each element of idx that is True, I wish to replace the corresponding
80x1x1 slice of this_par with the elements of pars.
I've tried lots of variations on the theme of
>>>this_par[idx[np.newaxis, ...]] = pars[:, np.newaxis, np.newaxis]
but so far, no dice.
Any help greatly appreciated!
Timothy W. Hilton
PhD Candidate, Department of Meteorology
The Pennsylvania State University
503 Walker Building, University Park, PA 16802
Hi Numpy Folks,
A while back, I filed this ticket:
http://projects.scipy.org/numpy/ticket/1441 suggesting a change to
rollaxis() and some fixes to the doc and error reporting. Ralf Gommers
suggested I float the behavior change here, so that's what I'm doing.
The motivation for the change comes because it seems like there should
be a simpler way to get some axis into the last position than to do this:
>>> a = np.ones((3,4,5,6))
>>> b = np.rollaxis(a, axis=0, start=len(a.shape))
(4, 5, 6, 3)
But currently it seems there isn't because when you specify -1 as the
'start' argument, the axis is moved into the second-to-last position.
My proposed change, which you can see on the ticket, would change that
so that using -1 referred to the end position. Note that the use of
negative 'start' arguments isn't currently documented and, in its
current form, doesn't seem very useful. My proposal wouldn't change the
behavior for positive 'start' values at all, and the interpretation of
'axis' arguments is also unaffected.
If that's going to break too much code, here's a pathway that might be
acceptable: Add a new function moveaxis() which works the way
rollaxis() does for positive arguments but in the new way for negative
arguments. Eventually, rollaxis could be deprecated to keep things
tidy. This has the added advantage of using a name that seems to fit
what the function does better - 'rollaxis' suggests a behavior like the
roll() function which affects other axes, which isn't what happens.
Thanks for listening; I'm a big fan of Numpy.