Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions healpy/sphtfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def alm2map(
return np.array(output)


def synalm(cls, lmax=None, mmax=None, new=False, verbose=True):
def synalm(cls, lmax=None, mmax=None, new=False, rng=None, verbose=True):
"""Generate a set of alm given cl.
The cl are given as a float array. Corresponding alm are generated.
If lmax is None, it is assumed lmax=cl.size-1
Expand All @@ -369,6 +369,10 @@ def synalm(cls, lmax=None, mmax=None, new=False, verbose=True):
(e.g. TT, EE, BB, TE, EB, TB or TT, EE, BB, TE if 4 cl as input).
If False, use the old ordering, ie by row
(e.g. TT, TE, TB, EE, EB, BB or TT, TE, EE, BB if 4 cl as input).

rng: numpy random number generator state. Useful when generating multiple
maps in parallel. Typically, `rng=np.random.RandomState(seed)`


Returns
-------
Expand Down Expand Up @@ -408,8 +412,14 @@ def synalm(cls, lmax=None, mmax=None, new=False, verbose=True):
cls_list = [np.asarray(cls, dtype=np.float64)]
szalm = Alm.getsize(lmax, mmax)
alm = np.zeros(szalm, "D")
alm.real = np.random.standard_normal(szalm)
alm.imag = np.random.standard_normal(szalm)

if rng is None:
alm.real = np.random.standard_normal(szalm)
alm.imag = np.random.standard_normal(szalm)
else:
alm.real = rng.standard_normal(szalm)
alm.imag = rng.standard_normal(szalm)

alms_list = [alm]
sphtlib._synalm(cls_list, alms_list, lmax, mmax)
return alm
Expand Down Expand Up @@ -442,8 +452,14 @@ def synalm(cls, lmax=None, mmax=None, new=False, verbose=True):
alms_list = []
for i in six.moves.xrange(Nspec):
alm = np.zeros(szalm, "D")
alm.real = np.random.standard_normal(szalm)
alm.imag = np.random.standard_normal(szalm)

if rng is None:
alm.real = np.random.standard_normal(szalm)
alm.imag = np.random.standard_normal(szalm)
else:
alm.real = rng.standard_normal(szalm)
alm.imag = rng.standard_normal(szalm)

alms_list.append(alm)
if new: # new input order: input given by diagonal, should be given by row
cls_list = new_to_old_spectra_order(cls_list)
Expand All @@ -467,6 +483,7 @@ def synfast(
fwhm=0.0,
sigma=None,
new=False,
rng=None,
verbose=True,
):
"""Create a map(s) from cl(s).
Expand Down Expand Up @@ -497,6 +514,7 @@ def synfast(
sigma : float, scalar, optional
The sigma of the Gaussian used to smooth the map (applied on alm)
[in radians]
rng: Numpy random number generator state, passed to synalm.

Returns
-------
Expand Down Expand Up @@ -524,7 +542,7 @@ def synfast(
cls_lmax = cb.len_array_or_arrays(cls) - 1
if lmax is None or lmax < 0:
lmax = min(cls_lmax, 3 * nside - 1)
alms = synalm(cls, lmax=lmax, mmax=mmax, new=new, verbose=verbose)
alms = synalm(cls, lmax=lmax, mmax=mmax, new=new, rng=rng, verbose=verbose)
maps = alm2map(
alms,
nside,
Expand Down