Example #1
0
def test_settingerr():
    ref1 = np.seterr()
    ref2 = {"divide": "ignore", "invalid": "ignore", "over": "ignore", "under": "ignore"}
    ref3 = {"divide": "raise", "invalid": "ignore", "over": "warn", "under": "ignore"}

    with settingerr(all="ignore"):
        assert_eq(np.seterr(), ref2)
        with settingerr(divide="raise", over="warn"):
            assert_eq(np.seterr(), ref3)
        assert_eq(np.seterr(), ref2)
    assert_eq(np.seterr(), ref1)
Example #2
0
def test_settingerr():
    ref1 = np.seterr()
    ref2 = {'divide': 'ignore', 'invalid': 'ignore', 'over': 'ignore',
            'under': 'ignore'}
    ref3 = {'divide': 'raise', 'invalid': 'ignore', 'over': 'warn',
            'under': 'ignore'}

    with settingerr(all='ignore'):
        assert_eq(np.seterr(), ref2)
        with settingerr(divide='raise', over='warn'):
            assert_eq(np.seterr(), ref3)
        assert_eq(np.seterr(), ref2)
    assert_eq(np.seterr(), ref1)
Example #3
0
 def primary_beam(theta, phi):
     import numpy as np
     with settingerr(invalid='ignore'):
         return theta <= np.radians(9)
Example #4
0
def semilogy_spectra(ell,
                     spectra=None,
                     lmax=None,
                     Dl=False,
                     yerr=None,
                     xerr=None,
                     loc='best',
                     **keywords):
    """
    Plot Cl power spectra as sqrt(l(l+1)Cl/2pi) Cl with log scaling
    on the y axis.

    semilogy_spectra([ell,] spectra)

    Parameters
    ----------
    ell : integer array, optional
        The l values of the spectra. If not provided, it is assumed that
        the Cls start from 0 and are not binned.
    spectra : array-like, 4-tuple or 6-tuple
        The Cl spectrum or the TT, EE, BB and TE or TT, EE, BB, TE, EB, TB
        Cl spectra.
    Dl : boolean, optional
        Use `D_l` as label instead of `l(l+1)Cl/2pi`.
    lmax : integer
        Plot upper x limit.
    loc : string
        keyword passed to `legend` if there is more than one input spectrum.

    """
    import matplotlib.pyplot as mp
    if spectra is None:
        spectra = ell
        ell = None
    if lmax is None:
        lmax = np.max(ell)
    if not isinstance(spectra, (list, np.ndarray, tuple)):
        raise TypeError('Invalid type for the power spectra.')
    if not isinstance(spectra[0], (list, np.ndarray, tuple)):
        nspectra = 1
        spectra = (spectra, )
    else:
        nspectra = len(spectra)
    if nspectra not in (1, 4, 6):
        raise ValueError('Invalid number of spectra.')
    if Dl:
        label = '$\sqrt{{\mathcal{{D}}_\ell^{{{}}}}}$'
    else:
        label = '$\sqrt{{\ell(\ell+1)C_\ell^{{{}}}/2\pi}}$'

    if ell is None:
        ell = np.arange(spectra[0].size)
    fact = ell * (ell + 1) / (2 * np.pi)

    with settingerr(invalid='ignore'):
        if nspectra == 1:
            c = fact * spectra[0]
            sc = np.sqrt(c)
            if xerr is None and yerr is None:
                p = mp.plot(ell, np.sqrt(c), **keywords)
            else:
                if yerr is not None:
                    yerr = [
                        sc - np.sqrt(c - fact * yerr),
                        np.sqrt(c + fact * yerr) - sc
                    ]
                p = mp.errorbar(ell, sc, yerr=yerr, xerr=xerr, **keywords)
            mp.plot(ell, np.sqrt(-c), linestyle='--', color=p[0].get_color())
        else:
            if nspectra == 6:
                ctt, cee, cbb, cte, ceb, ctb = spectra
                mp.plot(ell,
                        np.sqrt(fact * ceb),
                        color='brown',
                        label=label.format('EB'))
                mp.plot(ell,
                        np.sqrt(-fact * ceb),
                        color='brown',
                        linestyle='--')
                mp.plot(ell,
                        np.sqrt(fact * ctb),
                        color='cyan',
                        label=label.format('TB'))
                mp.plot(ell,
                        np.sqrt(-fact * ctb),
                        color='cyan',
                        linestyle='--')
            else:
                ctt, cee, cbb, cte = spectra
            mp.plot(ell, np.sqrt(fact * ctt), 'k', label=label.format('TT'))
            mp.plot(ell, np.sqrt(fact * cte), 'g', label=label.format('TE'))
            mp.plot(ell, np.sqrt(-fact * cte), 'g', linestyle='--')
            mp.plot(ell, np.sqrt(fact * cee), 'b', label=label.format('EE'))
            c = fact * cbb
            sc = np.sqrt(c)
            if 'color' not in keywords:
                keywords['color'] = 'r'
            if 'label' not in keywords:
                keywords['label'] = label.format('BB')
            if xerr is None and yerr is None:
                mp.plot(ell, sc, **keywords)
            else:
                if yerr is not None:
                    yerr = [
                        sc - np.sqrt(c - fact * yerr),
                        np.sqrt(c + fact * yerr) - sc
                    ]
                mp.errorbar(ell, sc, yerr=yerr, xerr=xerr, **keywords)

    mp.yscale('log')
    mp.xlim(0, lmax)
    mp.ylim(0.01, 100)
    mp.xlabel('$\ell$')
    mp.ylabel(label.format('') + ' [$\mu$ K]')
    if nspectra > 1:
        mp.legend(loc=loc, frameon=False)
Example #5
0
 def primary_beam(theta, phi):
     import numpy as np
     with settingerr(invalid='ignore'):
         return theta <= np.radians(9)
Example #6
0
def semilogy_spectra(ell, spectra=None, lmax=None, Dl=False, yerr=None,
                     xerr=None, loc='best', **keywords):
    """
    Plot Cl power spectra as sqrt(l(l+1)Cl/2pi) Cl with log scaling
    on the y axis.

    semilogy_spectra([ell,] spectra)

    Parameters
    ----------
    ell : integer array, optional
        The l values of the spectra. If not provided, it is assumed that
        the Cls start from 0 and are not binned.
    spectra : array-like, 4-tuple or 6-tuple
        The Cl spectrum or the TT, EE, BB and TE or TT, EE, BB, TE, EB, TB
        Cl spectra.
    Dl : boolean, optional
        Use `D_l` as label instead of `l(l+1)Cl/2pi`.
    lmax : integer
        Plot upper x limit.
    loc : string
        keyword passed to `legend` if there is more than one input spectrum.

    """
    import matplotlib.pyplot as mp
    if spectra is None:
        spectra = ell
        ell = None
    if lmax is None:
        lmax = np.max(ell)
    if not isinstance(spectra, (list, np.ndarray, tuple)):
        raise TypeError('Invalid type for the power spectra.')
    if not isinstance(spectra[0], (list, np.ndarray, tuple)):
        nspectra = 1
        spectra = (spectra,)
    else:
        nspectra = len(spectra)
    if nspectra not in (1, 4, 6):
        raise ValueError('Invalid number of spectra.')
    if Dl:
        label = '$\sqrt{{\mathcal{{D}}_\ell^{{{}}}}}$'
    else:
        label = '$\sqrt{{\ell(\ell+1)C_\ell^{{{}}}/2\pi}}$'

    if ell is None:
        ell = np.arange(spectra[0].size)
    fact = ell * (ell + 1) / (2 * np.pi)

    with settingerr(invalid='ignore'):
        if nspectra == 1:
            c = fact * spectra[0]
            sc = np.sqrt(c)
            if xerr is None and yerr is None:
                p = mp.plot(ell, np.sqrt(c), **keywords)
            else:
                if yerr is not None:
                    yerr = [sc - np.sqrt(c - fact * yerr),
                            np.sqrt(c + fact * yerr) - sc]
                p = mp.errorbar(ell, sc, yerr=yerr, xerr=xerr, **keywords)
            mp.plot(ell, np.sqrt(-c), linestyle='--', color=p[0].get_color())
        else:
            if nspectra == 6:
                ctt, cee, cbb, cte, ceb, ctb = spectra
                mp.plot(ell, np.sqrt(fact * ceb), color='brown',
                        label=label.format('EB'))
                mp.plot(ell, np.sqrt(-fact * ceb), color='brown',
                        linestyle='--')
                mp.plot(ell, np.sqrt(fact * ctb), color='cyan',
                        label=label.format('TB'))
                mp.plot(ell, np.sqrt(-fact * ctb), color='cyan',
                        linestyle='--')
            else:
                ctt, cee, cbb, cte = spectra
            mp.plot(ell, np.sqrt(fact * ctt), 'k', label=label.format('TT'))
            mp.plot(ell, np.sqrt(fact * cte), 'g', label=label.format('TE'))
            mp.plot(ell, np.sqrt(-fact * cte), 'g', linestyle='--')
            mp.plot(ell, np.sqrt(fact * cee), 'b', label=label.format('EE'))
            c = fact * cbb
            sc = np.sqrt(c)
            if 'color' not in keywords:
                keywords['color'] = 'r'
            if 'label' not in keywords:
                keywords['label'] = label.format('BB')
            if xerr is None and yerr is None:
                mp.plot(ell, sc, **keywords)
            else:
                if yerr is not None:
                    yerr = [sc - np.sqrt(c - fact * yerr),
                            np.sqrt(c + fact * yerr) - sc]
                mp.errorbar(ell, sc, yerr=yerr, xerr=xerr, **keywords)

    mp.yscale('log')
    mp.xlim(0, lmax)
    mp.ylim(0.01, 100)
    mp.xlabel('$\ell$')
    mp.ylabel(label.format('') + ' [$\mu$ K]')
    if nspectra > 1:
        mp.legend(loc=loc, frameon=False)