Arrays of Python Values

Hi, So working on the radiosity renderer: http://a.imageshack.us/img186/2479/image2f.png. The code now runs fast enough to generate the data required to draw that. Now, I need to optimize the radiosity calculation, so that it will converge in a reasonable amount of time. Right now, the individual blocks ("patches") are stored as instances of Python classes in a list. I'd heard that NumPy supports arrays of Python objects, so, I simply made an array out of the list, which seemed ok. Unfortunately, there is a custom sorting operation that sorted the list of by an attribute of each class: self.patches.sort( lambda x,y:cmp(x.residual_radiance,y.residual_radiance), reverse=True ) Because I've never used arrays of Python objects (and Googling didn't turn up any examples), I'm stuck on how to sort the corresponding array in NumPy in the same way. Of course, perhaps I'm just trying something that's absolutely impossible, or there's an obviously better way. I get the feeling that having no Python objects in the NumPy array would speed things up even more, but I couldn't figure out how I'd handle the different attributes (or specifically, how to keep them together during a sort). What're my options? Thanks again for the extremely valuable help, Ian

Ian Mallett wrote:
self.patches.sort( lambda x,y:cmp(x.residual_radiance,y.residual_radiance), reverse=True )
...
more, but I couldn't figure out how I'd handle the different attributes (or specifically, how to keep them together during a sort).
I'm not sure you gain much using numpy to sort python objects. In any case, how about: r = [ x.residual_radiance for x in self.patches ] ordered_indices = numpy.argsort(r) ordered_patches = numpy.take( self.patches, indices ) ordered_other_thing = numpy.take( other_thing, indices ) etc... HTH, Jon

Fri, 23 Jul 2010 01:16:41 -0700, Ian Mallett wrote: [clip]
Because I've never used arrays of Python objects (and Googling didn't turn up any examples), I'm stuck on how to sort the corresponding array in NumPy in the same way.
I doubt you will gain any speed by switching from Python lists to Numpy arrays containing Python objects.
Of course, perhaps I'm just trying something that's absolutely impossible, or there's an obviously better way. I get the feeling that having no Python objects in the NumPy array would speed things up even more, but I couldn't figure out how I'd handle the different attributes (or specifically, how to keep them together during a sort).
What're my options?
One option could be to use structured arrays to store the data, instead of Python objects. -- Pauli Virtanen

On Friday 23 July 2010 10:16:41 Ian Mallett wrote:
self.patches.sort( lambda x,y:cmp(x.residual_radiance,y.residual_radiance), reverse=True )
Using sort(key = lambda x: x.residual_radiance) should be faster.
Because I've never used arrays of Python objects (and Googling didn't turn up any examples), I'm stuck on how to sort the corresponding array in NumPy in the same way.
Of course, perhaps I'm just trying something that's absolutely impossible, or there's an obviously better way. I get the feeling that having no Python objects in the NumPy array would speed things up even more, but [...]
Exactly. Maybe you can use record arrays? (Have a look at "complex dtypes", i.e. struct-like dtypes for arrays.) Never tried sort on these, but I'd hope that's possible. HTH, Hans

Hi, I've converted all of the code to use record arrays, for a 10-fold speed boost. Thanks, Ian
participants (4)
-
Hans Meine
-
Ian Mallett
-
Jon Wright
-
Pauli Virtanen