legend-exp / dspeed

Fast Digital Signal Processing for particle detector signals in Python
https://dspeed.readthedocs.io
GNU General Public License v3.0
2 stars 13 forks source link

Using RNG in DSP #5

Open iguinn opened 2 years ago

iguinn commented 2 years ago

It could be useful to generate random numbers in a ProcessingChain. One use for this would be in pulse generation. An example of what such a processing chain config might look like is:

{
    "processors": {
    "amp": {
        "processor": "random_gaus",
        "module": "???",
        "args": [10, 1, "amp"]
    },
    "risetime": {
        "processor": "random_uniform",
        "module": "???",
        "args": ["0.5*us", "1*us", "risetime(unit=us)"]
    },
    "offset": {
        "processor": "random_gaus",
        "module": "???",
        "args": ["10*us", "1*us", "offset(unit=us)"],
    },
    "waveform": {
        "processor": "pulse_injector",
        "module": "pygama.dsp.processors",
        "args": ["zeros_in", "amp", "risetime", "offset", "waveform"]
    }
    }
}

However, right now it's not clear what RNG processors we could use. The numpy.random generators to do not work with in place array filling. The same is true of the scipy.stats generators. Numba seems to have support for random number generation, but I'm not sure how it works yet. We also want to make sure that we can control the seeding of the RNGs, and make sure that this would work for multi-threading.

Here are the features we want for our RNG processors: 1) Fill output array in place (i.e. have an out argument) 2) Have a way to set initial seeds 3) Robust for multithreaded and MPI

jasondet commented 1 year ago

@iguinn numpy.random() does have an out argument that does in-place array filling, can't that do the job?

gipert commented 1 year ago

I guess we will then wrap NumPy's random functions to include an out argument? Maybe we can also do it generically with meta-programming?

From the Numba docs, looks like their implementation is thread-safe (except when using Generators).

To control random number sequences, as far as I understand, we will have to make sure to seed both the Numba and the NumPy random generator (see note here)

@jasondet can you elaborate?