Example #1
0
def synalm(cls, lmax=-1, mmax=-1):
    """Generate a set of alm given cl.
    The cl are given as a float array. Corresponding alm are generated.
    If lmax is not given or negative, it is assumed lmax=cl.size-1
    If mmax is not given or negative, it is assumed mmax=lmax.

    Call: alms = synalm(cls, lmax=-1, mmax=-1)

    Input:
      - cls: either one cl (1D array) or a tuple of either 4 cl (TT,TE,EE,BB)
             or of n(n+1)/2 cl. Some of the cl may be None, implying no
             cross-correlation. For example, (TT,TE,TB,EE,EB,BB).
             NOTE: this order differs from the alm2cl function !
    Parameters:
      - lmax: the lmax (if <0, the largest size-1 of cls)
      - mmax: the mmax (if <0, =lmax)

    Return: a list of n alms (with n(n+1)/2 the number of input cl,
            or n=3 if there are 4 input cl).
    """
    if not isinstance(cls[0], npy.ndarray):
        if lmax < 0: lmax = cls.size - 1
        if mmax < 0: mmax = lmax
        cls_list = [cls]
        szalm = Alm.getsize(lmax, mmax)
        alm = npy.zeros(szalm, 'D')
        alm.real = npy.random.standard_normal(szalm)
        alm.imag = npy.random.standard_normal(szalm)
        alms_list = [alm]
        sphtlib._synalm(cls_list, alms_list, lmax, mmax)
        return alm
    # otherwise, cls must be a sequence of arrays
    try:
        cls_list = list(cls)
        maxsize = 0
        for c in cls_list:
            if c is not None:
                if c.size > maxsize: maxsize = c.size
    except:
        raise TypeError(
            "First argument must be an array or a sequence of arrays.")
    if lmax < 0: lmax = maxsize - 1
    if mmax < 0: mmax = lmax
    if sphtlib._getn(len(cls_list)) <= 0:
        if len(cls_list) == 4:  # if 4 cls are given, assume TT, TE, EE, BB
            cls_list = [cls[0], cls[1], None, cls[2], None, cls[3]]
        else:
            raise TypeError(
                "The sequence of arrays must have either 4 elements "
                "(TT,TE,EE,BB)\n"
                "or n(n+1)/2 elements (some may be None)")
    szalm = Alm.getsize(lmax, mmax)
    alms_list = []
    for i in xrange(sphtlib._getn(len(cls_list))):
        alm = npy.zeros(szalm, 'D')
        alm.real = npy.random.standard_normal(szalm)
        alm.imag = npy.random.standard_normal(szalm)
        alms_list.append(alm)
    sphtlib._synalm(cls_list, alms_list, lmax, mmax)
    if len(alms_list) > 1:
        return alms_list
    else:
        return alms_list[0]
Example #2
0
def synalm(cls, lmax=None, mmax=None, new=False):
    """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
    If mmax is None, it is assumed mmax=lmax.

    Parameters
    ----------
    cls : float, array or tuple of arrays
      Either one cl (1D array) or a tuple of either 4 cl
      or of n*(n+1)/2 cl.
      Some of the cl may be None, implying no
      cross-correlation. See *new* parameter.
    lmax : int, scalar, optional
      The lmax (if None or <0, the largest size-1 of cls)
    mmax : int, scalar, optional
      The mmax (if None or <0, =lmax)
    new : bool, optional
      If True, use the new ordering of cl's, ie by diagonal
      (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).      

    Returns
    -------
    alms : array or list of arrays
      the generated alm if one spectrum is given, or a list of n alms 
      (with n(n+1)/2 the number of input cl, or n=3 if there are 4 input cl).

    Notes
    -----
    The order of the spectra will change in a future release. The new= parameter
    help to make the transition smoother. You can start using the new order
    by setting new=True.
    In the next version of healpy, the default will be new=True.
    This change is done for consistency between the different tools
    (alm2cl, synfast, anafast).
    In the new order, the spectra are ordered by diagonal of the correlation
    matrix. Eg, if fields are T, E, B, the spectra are TT, EE, BB, TE, EB, TB
    with new=True, and TT, TE, TB, EE, EB, BB if new=False.
    """
    if not new:
        warnings.warn(
            "The order of the input cl's will change in a future "
            "release.\n"
            "Use new=True keyword to start using the new order.\n"
            "See documentation of healpy.synalm.",
            category=FutureChangeWarning,
        )
    if not cb.is_seq(cls):
        raise TypeError("cls must be an array or a sequence of arrays")

    if not cb.is_seq_of_seq(cls):
        # Only one spectrum
        if lmax is None or lmax < 0:
            lmax = cls.size - 1
        if mmax is None or mmax < 0:
            mmax = lmax
        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)
        alms_list = [alm]
        sphtlib._synalm(cls_list, alms_list, lmax, mmax)
        return alm

    # From here, we interpret cls as a list of spectra
    cls_list = list(cls)
    maxsize = max([len(c) for c in cls])

    if lmax is None or lmax < 0:
        lmax = maxsize - 1
    if mmax is None or mmax < 0:
        mmax = lmax

    Nspec = sphtlib._getn(len(cls_list))

    if Nspec <= 0:
        if len(cls_list) == 4:
            if new:  ## new input order: TT EE BB TE -> TT EE BB TE 0 0
                cls_list = [cls[0], cls[1], cls[2], cls[3], None, None]
            else:  ## old input order: TT TE EE BB -> TT TE 0 EE 0 BB
                cls_list = [cls[0], cls[1], None, cls[2], None, cls[3]]
            Nspec = 3
        else:
            raise TypeError(
                "The sequence of arrays must have either 4 elements " "or n(n+1)/2 elements (some may be None)"
            )

    szalm = Alm.getsize(lmax, mmax)
    alms_list = []
    for i in xrange(Nspec):
        alm = np.zeros(szalm, "D")
        alm.real = np.random.standard_normal(szalm)
        alm.imag = np.random.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)
    # ensure cls are float64
    cls_list = [(np.asarray(cl, dtype=np.float64) if cl is not None else None) for cl in cls_list]
    sphtlib._synalm(cls_list, alms_list, lmax, mmax)
    return alms_list
Example #3
0
def synalm(cls, lmax=-1, mmax=-1):
    """Generate a set of alm given cl.
    The cl are given as a float array. Corresponding alm are generated.
    If lmax is not given or negative, it is assumed lmax=cl.size-1
    If mmax is not given or negative, it is assumed mmax=lmax.

    Call: alms = synalm(cls, lmax=-1, mmax=-1)

    Input:
      - cls: either one cl (1D array) or a tuple of either 4 cl (TT,TE,EE,BB)
             or of n(n+1)/2 cl. Some of the cl may be None, implying no
             cross-correlation. For example, (TT,TE,TB,EE,EB,BB).
             NOTE: this order differs from the alm2cl function !
    Parameters:
      - lmax: the lmax (if <0, the largest size-1 of cls)
      - mmax: the mmax (if <0, =lmax)

    Return: a list of n alms (with n(n+1)/2 the number of input cl,
            or n=3 if there are 4 input cl).
    """
    if not isinstance(cls[0], npy.ndarray):
        if lmax < 0: lmax = cls.size-1
        if mmax < 0: mmax = lmax
        cls_list = [cls]
        szalm = Alm.getsize(lmax,mmax)
        alm = npy.zeros(szalm,'D')
        alm.real = npy.random.standard_normal(szalm)
        alm.imag = npy.random.standard_normal(szalm)
        alms_list=[alm]
        sphtlib._synalm(cls_list,alms_list,lmax,mmax)
        return alm
    # otherwise, cls must be a sequence of arrays
    try:
        cls_list = list(cls)
        maxsize = 0
        for c in cls_list:
            if c is not None:
                if c.size > maxsize: maxsize=c.size
    except:
        raise TypeError("First argument must be an array or a sequence of arrays.")
    if lmax < 0: lmax = maxsize-1
    if mmax < 0: mmax = lmax
    if sphtlib._getn(len(cls_list)) <= 0:
        if len(cls_list) == 4:  # if 4 cls are given, assume TT, TE, EE, BB
            cls_list = [cls[0], cls[1], None, cls[2], None, cls[3]]
        else:
            raise TypeError("The sequence of arrays must have either 4 elements "
                            "(TT,TE,EE,BB)\n"
                            "or n(n+1)/2 elements (some may be None)")
    szalm = Alm.getsize(lmax,mmax)
    alms_list = []
    for i in xrange(sphtlib._getn(len(cls_list))):
        alm = npy.zeros(szalm,'D')
        alm.real = npy.random.standard_normal(szalm)
        alm.imag = npy.random.standard_normal(szalm)
        alms_list.append(alm)
    sphtlib._synalm(cls_list, alms_list, lmax, mmax)
    if len(alms_list) > 1:
        return alms_list
    else:
        return alms_list[0]
Example #4
0
def synalm(cls, lmax = None, mmax = None, new = False):
    """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
    If mmax is None, it is assumed mmax=lmax.

    Parameters
    ----------
    cls : float, array or tuple of arrays
      Either one cl (1D array) or a tuple of either 4 cl
      or of n*(n+1)/2 cl.
      Some of the cl may be None, implying no
      cross-correlation. See *new* parameter.
    lmax : int, scalar, optional
      The lmax (if None or <0, the largest size-1 of cls)
    mmax : int, scalar, optional
      The mmax (if None or <0, =lmax)
    new : bool, optional
      If True, use the new ordering of cl's, ie by diagonal
      (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).      

    Returns
    -------
    alms : array or list of arrays
      the generated alm if one spectrum is given, or a list of n alms 
      (with n(n+1)/2 the number of input cl, or n=3 if there are 4 input cl).

    Notes
    -----
    The order of the spectra will change in a future release. The new= parameter
    help to make the transition smoother. You can start using the new order
    by setting new=True.
    In the next version of healpy, the default will be new=True.
    This change is done for consistency between the different tools
    (alm2cl, synfast, anafast).
    In the new order, the spectra are ordered by diagonal of the correlation
    matrix. Eg, if fields are T, E, B, the spectra are TT, EE, BB, TE, EB, TB
    with new=True, and TT, TE, TB, EE, EB, BB if new=False.
    """
    if not new:
        warnings.warn("The order of the input cl's will change in a future "
                      "release.\n"
                      "Use new=True keyword to start using the new order.\n"
                      "See documentation of healpy.synalm.",
                      category=FutureChangeWarning)
    if not cb.is_seq(cls):
        raise TypeError('cls must be an array or a sequence of arrays')

    if not cb.is_seq_of_seq(cls):
        # Only one spectrum
        if lmax is None or lmax < 0:
            lmax = cls.size-1
        if mmax is None or mmax < 0:
            mmax = lmax
        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)
        alms_list=[alm]
        sphtlib._synalm(cls_list,alms_list,lmax,mmax)
        return alm

    # From here, we interpret cls as a list of spectra
    cls_list = list(cls)
    maxsize = max([len(c) for c in cls])
    
    if lmax is None or lmax < 0:
        lmax = maxsize-1
    if mmax is None or mmax < 0:
        mmax = lmax
    
    Nspec = sphtlib._getn(len(cls_list))
    
    if Nspec <= 0:
        if len(cls_list) == 4:
            if new: ## new input order: TT EE BB TE -> TT EE BB TE 0 0
                cls_list = [cls[0], cls[1], cls[2], cls[3], None, None]
            else: ## old input order: TT TE EE BB -> TT TE 0 EE 0 BB
                cls_list = [cls[0], cls[1], None, cls[2], None, cls[3]]
            Nspec = 3
        else:
            raise TypeError("The sequence of arrays must have either 4 elements "
                            "or n(n+1)/2 elements (some may be None)")
    
    szalm = Alm.getsize(lmax,mmax)
    alms_list = []
    for i in xrange(Nspec):
        alm = np.zeros(szalm,'D')
        alm.real = np.random.standard_normal(szalm)
        alm.imag = np.random.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)
    # ensure cls are float64
    cls_list = [(np.asarray(cl, dtype = np.float64) if cl is not None else None)
                for cl in cls_list]
    sphtlib._synalm(cls_list, alms_list, lmax, mmax)
    return alms_list