I'm trying to write a median function (surely one is there already, but if so I've not found it). The fastest technique seems to involve rearranging the data in place. So I'd like to offer two calls: - A version that rearranges the input array if no local copy needed to be made (e.g. the input data was already contiguous and of the expected type). I know how to write that one. - A safer, slower version that always makes a copy of the input data. I'm stuck on that. The high-level interface has flags for when to make a local copy, but none of them seems to mean "always make a local copy". Surely I am missing something there? I guess I could get the data, see if a local copy was made and if not, make a copy of my own, but that seems messy. Also, is there any simple sample code running around for handling arrays of any type (e.g. with C++ templates)? Right now I'm writing to a specific type (since it's easy and I know what type to expect for my application), but I'd rather make my code more general and numarray-like. I figured the numarray code itself would be a good example, but, well, I didn't understand the parts I looked at (maybe I'm clueless, maybe I didn't find the right bits to examine). -- Russell
On Fri, 2004-04-09 at 13:25, Russell E Owen wrote:
I'm trying to write a median function (surely one is there already, but if so I've not found it). The fastest technique seems to involve rearranging the data in place.
You might want to try out numarray.image.median (or numarray.image.combine.median for versions < 0.9).
So I'd like to offer two calls: - A version that rearranges the input array if no local copy needed to be made (e.g. the input data was already contiguous and of the expected type). I know how to write that one. - A safer, slower version that always makes a copy of the input data. I'm stuck on that. The high-level interface has flags for when to make a local copy, but none of them seems to mean "always make a local copy". Surely I am missing something there? I guess I could get the data, see if a local copy was made and if not, make a copy of my own, but that seems messy.
The high level API is set up to do the least amount of work to guarantee a set of constraints. You can independently constrain numarray properties (contiguousness, byteswap, alignment) and the array type to be what you know your code can process. The high level API will create or convert arrays as necessary, and then release them when the function is done processing (provided you manage your ref counts correctly as shown in the manual). So, the gist of the high level API is that it takes some mysterious object (a poorly behaved array, an array of the wrong type, nested lists, etc.) and converts it into a nicely formed proxy array on which you do your computing. In the case where the mysterious object actually was a well behaved properly typed array, almost no work has to be done and the proxy is the original array. If you need a copy because your code is going to destroy the input array, use NA_copy() or just do a .copy() method call yourself. Making this kind of copy could indeed be folded into the existing API and it would result in simpler application code, but it isn't there now.
Also, is there any simple sample code running around for handling arrays of any type (e.g. with C++ templates)?
I think Peter Verveer did some code like this in the nd_image package. The general idea is to constrain the arrays with type tAny in the high level API and then switch off of a->descr->type_num to select type specific code. Regards, Todd
-- Todd Miller
At 2:32 PM -0400 2004-04-09, Todd Miller wrote:
On Fri, 2004-04-09 at 13:25, Russell E Owen wrote:
I'm trying to write a median function (surely one is there already, but if so I've not found it). The fastest technique seems to involve rearranging the data in place.
You might want to try out numarray.image.median (or numarray.image.combine.median for versions < 0.9).
Thanks. Based on the source, it looks like that prefers a stack of identical images (instead of a single array, though perhaps one could make that case work). John Hunter also kindly pointed out MLab (which it turns out is buried under linear_algebra in numarray: numarray.linear_algebra.mlab). I had been working on a fast median instead of one that sorted the whole array, but after timing mlab's median I can probably get away with the simple implementation (and skip the C extension).
Also, is there any simple sample code running around for handling arrays of any type (e.g. with C++ templates)?
I think Peter Verveer did some code like this in the nd_image package. The general idea is to constrain the arrays with type tAny in the high level API and then switch off of a->descr->type_num to select type specific code.
Thank you very much for the explanation of the high level interface and the pointer to this sample code. -- Russell
participants (3)
-
John Hunter
-
Russell E Owen
-
Todd Miller