GalSim-developers / GalSim

The modular galaxy image simulation toolkit. Documentation:
http://galsim-developers.github.io/GalSim/
Other
224 stars 106 forks source link

Add a GalSim deviates (partial) drop-in replacement for numpy.RandomState. #1067

Closed jmeyers314 closed 2 years ago

jmeyers314 commented 4 years ago

In numpy, if I want a random array uniformly distributed between -0.6 and +0.6 with 100 elements, I'd write:

rng = numpy.random.RandomState(seed)
array = rng.uniform(-0.6, 0.6, 100)

Using GalSim deviates, I'd do something like:

ud = galsim.UniformDeviate(seed)
array = np.empty(100)
ud.generate(array)
array = array*1.2 - 0.6

I find the former easier to read. I don't think it'd be too difficult to create an object based on GalSim deviates with methods like .uniform, .normal=.gaussian, .binomial, .poisson, etc that have the same syntax as numpy. Making a drop-in replacement also means that it's easier for external packages (e.g., imsim, batoid, ...) to switch from numpy random numbers to Galsim random numbers with their stronger guarantee of stability over time.

rmjarvis commented 4 years ago

Sounds good. I wouldn't make a new class for this though. These can be methods of BaseDeviate I think.

jmeyers314 commented 4 years ago

I thought about that. The only awkward part is that then UniformDeviate will have an inherited method .poisson and so on. Maybe that's fine though.

rmjarvis commented 4 years ago

Doesn't bother me. :) But either way. If you prefer to make a new subclass of BaseDeviate that would enable these various driver methods, that's also fine.

rmjarvis commented 2 years ago

Done via #1179