I am new at using Numba and just trying to learn about it. I had a code based on scipy and python dictionaries, which I have written as follows to speed it up using numba but it is way slower than the original code. I believe it is running into the nopython mode and I have tried some variations as well. I don’t know what am I doing wrong. Can someone help?
@export
class ModuleCrystalsPMTPulses:
# Define structured array for parameters
parameters_array = np.array(
[
(0.5, 0.2), (0.5, 0.2), (0.5, 0.2), (0.5, 0.2),
(0.5, 0.2), (0.5, 0.2), (0.5, 0.2), (0.5, 0.2),
(0.5, 0.2), (0.5, 0.2), (0.5, 0.2), (0.5, 0.2),
(0.5, 0.2), (0.5, 0.2)
], dtype=[('mu', 'f8'), ('sigma', 'f8')]
)
@staticmethod
@njit(parallel=True, nogil=True)
def compute_pulse_area(pulses, photoelectrons, params):
for i in prange(photoelectrons.shape[0]):
mu = params[i]['mu']
sigma = params[i]['sigma']
pulses[i] = np.random.normal(
loc=(photoelectrons[i] * mu),
scale=(np.sqrt(photoelectrons[i]) * sigma)
)
def action(self, photoelectrons, pmt_indices):
# Initialize array to store results
pulse_areas = np.zeros_like(photoelectrons, dtype=np.float64)
# Call the static method for computation
self.compute_pulse_area(pulse_areas, photoelectrons, self.parameters_array[pmt_indices])
return pulse_areas