To quickly make this code, you need to "vectorize" it: replace all explicit Python loops with implicit loops using the NumPy boradcasting rules. I can try and give a vector version of your loop:
if self.color_array is None: self.color_array = numpy.empty((len(activity), 4)) diff_activity = (activity - self.min) / abs_diff self.color_array[:, :3] = (start_colors + diff_activity[:, numpy.newaxis] + end_colors) self.color_array[:, 3] = 1
Please note that I had to guess a lot, since I'm not sure what all your variables are and what the code should do, so I cannot guarantee this code. I have turned color_array into a two-dimensional array as this seems more appropriate. This probably needs changes in other parts of the code (or you need to flatten the array again).
I assume that self.min and abs_diff are scalars, and all other names refer to NumPy arrays of the following forms:
activity.shape == (len(vertices) // 3,) start_colors.shape == (3,) end_colors.shape == (3,)
It also looks like vertices is a one-dimensional array and should be a two-dimensional array.
source share