[SciPy-User] Populating a recarray from 0 size

David Baddeley david_baddeley at yahoo.com.au
Sun Aug 22 16:44:02 EDT 2010


Is there any way of knowing the final size in advance so you can pre-allocate 
the array? Or at least guessing it with reasonable accuracy so that you can 
pre-allocate a slightly larger array and then cut it down to size? Either of 
these is likely to be significantly faster than resizing an array at each 
iteration.

Something else to look at would be the hstack/vstack functions, although you pay 
a similar performance penalty as with resizing.

David


----- Original Message ----
From: Sergi Pons Freixes <spons at utm.csic.es>
To: SciPy Users List <scipy-user at scipy.org>
Sent: Sun, 22 August, 2010 1:18:04 PM
Subject: [SciPy-User] Populating a recarray from 0 size

Hi everybody,

I'm interested in populating a recarray a row at a time. I thought in using:

-------
# Creation of empty recarray
names = ["cruise", "SEQ", "param", "r2", "stderr", "slope", "intercept"
formats = ["i4", "i4", "S10", "f4", "f4", "f4", "f4"]
statsc = scipy.empty(0, dtype={"names":names, "formats":formats})

# Adding row and setting values
statsc = scipy.resize(statsc, statsc.size + 1)
statsc["cruise"][-1] = cr
statsc["SEQ"][-1] = ca
statsc["param"][-1] = yvar
...
-------

But scipy.resize complains with:
statsc = scipy.resize(statsc, statsc.size + 1)
  File "/usr/lib/python2.6/site-packages/numpy/core/fromnumeric.py",
line 833, in resize
    if not Na: return mu.zeros(new_shape, a.dtype.char)
ValueError: Empty data-type

So, is scipy not happy because statsc.size is 0 on the first resize?
In this case, how could I overcome this limitation?

Regards,
Sergi

-- 
Sergi Pons Freixes
Ph.D. Student
Marine Technology Unit
Centre Mediterrani d'Investigacions Marines i Ambientals (CMIMA-CSIC)
Pg. Marítim Barceloneta, 37-49
E-08003 Barcelona (Spain)
Ph.  +34 93 230 95 00 (ext. 1510)
Fax. +34 93 230 95 55
spons at utm.csic.es
http://www.utm.csic.es
_______________________________________________
SciPy-User mailing list
SciPy-User at scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user



      



More information about the SciPy-User mailing list