Esempio n. 1
0
def test_mpi_read():

    comm = MPI.COMM_WORLD

    assert is_power2(
        comm.size), "Run with a number of MPI processes which is power of 2"

    nside = 64
    npix = hp.nside2npix(nside)

    num_local_pixels = npix // comm.size

    if comm.size == 1:
        pixel_indices = None
        comm = None
    else:
        pixel_indices = np.arange(comm.rank * num_local_pixels,
                                  (comm.rank + 1) * num_local_pixels,
                                  dtype=np.int)

    pysm_model = "s3,d7,f1,c1,a2"

    sky_config = build_sky_config(pysm_model, nside, pixel_indices, comm)

    sky = pysm.Sky(sky_config, mpi_comm=comm)

    instrument_bpass = {
        'use_smoothing': False,
        'nside': nside,
        'add_noise': False,
        'use_bandpass': True,
        'channels': [(np.linspace(20, 25, 10), np.ones(10))],
        'channel_names': ['channel_1'],
        'output_units': 'uK_RJ',
        'output_directory': './',
        'output_prefix': 'test',
        'noise_seed': 1234,
        'pixel_indices': pixel_indices
    }

    instrument = pysm.Instrument(instrument_bpass)
    local_map = instrument.observe(sky, write_outputs=False)

    # Run PySM again locally on each process on the full map

    sky_config = build_sky_config(pysm_model, nside)

    sky = pysm.Sky(sky_config, mpi_comm=comm)

    instrument_bpass["pixel_indices"] = None

    instrument = pysm.Instrument(instrument_bpass)
    complete_map = instrument.observe(sky, write_outputs=False)

    if pixel_indices is None:
        pixel_indices = np.arange(npix)

    np.testing.assert_array_almost_equal(local_map[0],
                                         complete_map[0][:, :, pixel_indices])
Esempio n. 2
0
    def test_Sigma_dust_one_parameter(self):
        NSIDE = 8
        MODEL = 'd0'
        INSTRUMENT = 'litebird'
        SIGNAL_TO_NOISE = 10
        sky = pysm.Sky(get_sky(NSIDE, MODEL))
        instrument = pysm.Instrument(get_instrument(INSTRUMENT, NSIDE))
        components = [cm.Dust(100., temp=20.)]
        ref = []
        for component in components:
            ref += component.defaults

        with suppress_stdout():
            freq_maps, noise_maps = instrument.observe(sky,
                                                       write_outputs=False)

        signal = freq_maps[:, 0, 0]
        noise = noise_maps[:, 0]
        signal_ver = signal / np.dot(signal, signal)**0.5
        noise_std = np.std([np.dot(n, signal_ver) for n in noise.T])
        maps = signal_ver * noise_std * SIGNAL_TO_NOISE
        maps = maps[:, np.newaxis] + noise

        res = basic_comp_sep(components,
                             instrument,
                             maps,
                             nside=hp.get_nside(maps))
        white = (res.x[0] - ref[0]) / res.Sigma[0, 0]**0.5
        _, p = kstest(white, 'norm')
        assert p > 0.01
Esempio n. 3
0
    def test_Sigma_dust_sync_betas_temp(self):
        NSIDE = 8
        MODEL = 'd0s0'
        INSTRUMENT = 'litebird'
        SIGNAL_TO_NOISE = 10000
        UNITS = 'uK_CMB'
        sky = pysm.Sky(get_sky(NSIDE, MODEL))
        instrument = pysm.Instrument(
            get_instrument(INSTRUMENT, NSIDE, units=UNITS))
        components = [
            cm.Dust(150., temp=20., units=UNITS),
            cm.Synchrotron(150., units=UNITS)
        ]
        ref = []
        for component in components:
            ref += component.defaults
        ref = np.array(ref)

        with suppress_stdout():
            freq_maps, noise_maps = instrument.observe(sky,
                                                       write_outputs=False)

        signal = freq_maps[:, 0, 0]  # Same signal for all the pixels
        noise = noise_maps[:, 0]
        signal_ver = signal / np.dot(signal, signal)**0.5
        noise_std = np.std([np.dot(n, signal_ver) for n in noise.T])
        maps = signal_ver * noise_std * SIGNAL_TO_NOISE
        maps = maps[:, np.newaxis] + noise

        res = basic_comp_sep(components, instrument, maps, nside=NSIDE)
        diff = (res.x.T - ref)
        postS = np.mean(diff[..., None] * diff[..., None, :], axis=0)
        S = res.Sigma.T[0]
        aac(postS, S, rtol=1. / NSIDE)
Esempio n. 4
0
 def test_smoothing(self):
     instrument_config = self.instrument_config
     instrument = pysm.Instrument(instrument_config)
     smoothed = instrument.smoother(self.synch_1_30GHz)
     np.testing.assert_almost_equal(smoothed[0][0],
                                    self.synch_1_30GHz_smoothed,
                                    decimal=3)
Esempio n. 5
0
    def exec(self, local_map, out, bandpasses=None):

        if pysm is None:
            raise RuntimeError('pysm not available')

        autotimer = timing.auto_timer(type(self).__name__)
        if self.sky is None:
            self.sky = self.init_sky(
                           self.pysm_sky_config, self.pysm_precomputed_cmb_K_CMB
                       ) if init_sky else None

        pysm_instrument_config = {
            'beams': None,
            'nside': self._nside,
            'use_bandpass': True,
            'channels': None,
            'channel_names': list(bandpasses.keys()),
            'add_noise': False,
            'output_units': self._units,
            'use_smoothing': False,
            'pixel_indices': self._local_pixels
        }

        for ch_name, bandpass in bandpasses.items():
            pysm_instrument_config["channels"] = [bandpass]
            instrument = pysm.Instrument(pysm_instrument_config)
            out_name = (out + "_" + ch_name) if ch_name else out
            # output of observe is a tuple, first item is the map
            # however the map has 1 extra dimension of size 1,
            # so we need to index [0] twice to get a map of (3, npix)
            local_map[out_name] = \
                instrument.observe(self.sky, write_outputs=False)[0][0]
            assert local_map[out_name].shape[0] == 3
            assert local_map[out_name].ndim == 2
Esempio n. 6
0
    def test_noise_write_partialsky(self):
        local_instrument_config = self.instrument_config.copy()
        npix = 20000
        local_instrument_config["pixel_indices"] = np.arange(npix,
                                                             dtype=np.int)
        instrument = pysm.Instrument(local_instrument_config)
        s1 = models("s1",
                    self.nside,
                    pixel_indices=local_instrument_config["pixel_indices"])
        s1[0]['A_I'] = np.zeros(npix)
        s1[0]['A_Q'] = np.zeros(npix)
        s1[0]['A_U'] = np.zeros(npix)
        sky_config = {'synchrotron': s1}
        partial_sky = pysm.Sky(sky_config)
        instrument.observe(partial_sky)
        # use masked array to handle partial sky
        T, Q, U = hp.ma(
            pysm.read_map(self.test_file, self.nside, field=(0, 1, 2)))
        T_std = np.ma.std(T)
        Q_std = np.ma.std(Q)
        U_std = np.ma.std(U)

        np.testing.assert_almost_equal(T_std, self.expected_T_std, decimal=2)
        np.testing.assert_almost_equal(Q_std, self.expected_P_std, decimal=2)
        np.testing.assert_almost_equal(U_std, self.expected_P_std, decimal=2)
Esempio n. 7
0
    def __init__(self, NSIDE, As):
        self.NSIDE = NSIDE
        self.Npix = 12 * NSIDE**2
        self.As = As
        print("Initialising sampler")
        self.cosmo = Class()
        #print("Maps")
        #A recommenter
        #self.Qs, self.Us, self.sigma_Qs, self.sigma_Us = aggregate_by_pixels_params(get_pixels_params(self.NSIDE))
        #print("betas")
        self.matrix_mean, self.matrix_var = aggregate_mixing_params(
            get_mixing_matrix_params(self.NSIDE))
        print("Cosmo params")
        self.cosmo_means = np.array(COSMO_PARAMS_MEANS)
        self.cosmo_stdd = np.diag(COSMO_PARAMS_SIGMA)

        self.instrument = pysm.Instrument(
            get_instrument('litebird', self.NSIDE))
        self.components = [CMB(), Dust(150.), Synchrotron(150.)]
        self.mixing_matrix = MixingMatrix(*self.components)
        self.mixing_matrix_evaluator = self.mixing_matrix.evaluator(
            self.instrument.Frequencies)

        self.noise_covar_one_pix = self.noise_covariance_in_freq(self.NSIDE)
        #A recommenter
        #self.noise_stdd_all = np.concatenate([np.sqrt(self.noise_covar_one_pix) for _ in range(2*self.Npix)])
        print("End of initialisation")
Esempio n. 8
0
    def __init__(self, NSIDE):
        self.NSIDE = NSIDE
        self.Npix = 12 * NSIDE**2
        print("Initialising sampler")
        self.cosmo = Class()
        print("Maps")
        self.templates_map, self.templates_var = aggregate_pixels_params(
            get_pixels_params(self.NSIDE))
        print("betas")
        self.matrix_mean, self.matrix_var = aggregate_mixing_params(
            get_mixing_matrix_params(self.NSIDE))
        print("Cosmo params")
        self.cosmo_means = np.array(COSMO_PARAMS_MEANS)
        self.cosmo_var = (np.diag(COSMO_PARAMS_SIGMA) / 2)**2

        plt.hist(self.templates_map)
        plt.savefig("mean_values.png")
        plt.close()
        plt.hist(self.templates_var)
        plt.savefig("std_values.png")
        plt.close()
        self.instrument = pysm.Instrument(
            get_instrument('litebird', self.NSIDE))
        self.components = [CMB(), Dust(150.), Synchrotron(150.)]
        self.mixing_matrix = MixingMatrix(*self.components)
        self.mixing_matrix_evaluator = self.mixing_matrix.evaluator(
            self.instrument.Frequencies)
        print("End of initialisation")
Esempio n. 9
0
    def test_dependence_on_nu0_RJ(self):
        NSIDE = 8
        MODEL = 'c1s0'
        INSTRUMENT = 'litebird'
        sky = pysm.Sky(get_sky(NSIDE, MODEL))
        instrument = pysm.Instrument(
            get_instrument(INSTRUMENT, NSIDE, units='uK_RJ'))
        components100 = [
            cm.CMB(units='K_RJ'),
            cm.Synchrotron(100., units='K_RJ')
        ]
        components10 = [
            cm.CMB(units='K_RJ'),
            cm.Synchrotron(10., units='K_RJ')
        ]

        with suppress_stdout():
            freq_maps, _ = instrument.observe(sky, write_outputs=False)

        res100 = basic_comp_sep(components100, instrument, freq_maps)
        res10 = basic_comp_sep(components10, instrument, freq_maps)
        aac(res100.Sigma, res10.Sigma)
        aac(res100.x, res10.x)
        aac(res100.s[0], res10.s[0])
        aac(res100.s[1], res10.s[1] * 10**res10.x[0])
Esempio n. 10
0
    def __init__(self, skyconfig, d):

        Nf = d['nf_sub']
        band = d['filter_nu'] / 1e9
        filter_relative_bandwidth = d['filter_relative_bandwidth']
        _, _, central_nus, _, _, _ = qubic.compute_freq(
            band, Nf, filter_relative_bandwidth)
        names = [np.str(np.round(cn, 2)) for cn in central_nus]
        names = [n.replace('.', 'p') for n in names]
        instrument = pysm.Instrument({
            'nside': d['nside'],
            'frequencies': central_nus,  # GHz
            'use_smoothing': False,
            'beams': np.ones_like(central_nus),  # arcmin 
            'add_noise': False,
            'noise_seed': 0,
            'sens_I': np.ones_like(central_nus),
            'sens_P': np.ones_like(central_nus),
            'use_bandpass': False,
            'channel_names': names,
            'channels': np.ones_like(central_nus),
            'output_units': 'uK_RJ',
            'output_directory': "./",
            'output_prefix': "qubic",
            'pixel_indices': None
        })

        sky.__init__(self, skyconfig, d, instrument)
Esempio n. 11
0
    def test_Sigma_synchrotron(self):
        NSIDE = 8
        MODEL = 's0'
        INSTRUMENT = 'litebird'
        SIGNAL_TO_NOISE = 20
        sky = pysm.Sky(get_sky(NSIDE, MODEL))
        instrument = pysm.Instrument(get_instrument(INSTRUMENT, NSIDE))
        components = [cm.Synchrotron(100.)]
        ref = []
        for component in components:
            ref += component.defaults

        with suppress_stdout():
            freq_maps, noise_maps = instrument.observe(sky,
                                                       write_outputs=False)

        signal = freq_maps[:, 0, 0]
        noise = np.std(noise_maps[:, 0], axis=-1)
        maps = signal / np.dot(signal, noise) * SIGNAL_TO_NOISE
        maps = maps[:, np.newaxis] + noise_maps[:, 0]
        res = basic_comp_sep(components,
                             instrument,
                             maps,
                             nside=hp.get_nside(maps))
        white = (res.x[0] - ref[0]) / res.Sigma[0, 0]**0.5
        _, p = kstest(white, 'norm')
        assert p > 0.01
Esempio n. 12
0
    def test_noise(self):
        instrument = pysm.Instrument(self.instrument_config)
        instrument.observe(self.sky)
        T, Q, U = pysm.read_map(self.test_file, self.nside, field=(0, 1, 2))
        T_std = np.std(T)
        Q_std = np.std(Q)
        U_std = np.std(U)

        np.testing.assert_almost_equal(T_std, self.expected_T_std, decimal=2)
        np.testing.assert_almost_equal(Q_std, self.expected_P_std, decimal=2)
        np.testing.assert_almost_equal(U_std, self.expected_P_std, decimal=2)
Esempio n. 13
0
 def test_smoothing_partial_sky(self):
     """Smoothing on a partial sky sets the UNSEEN pixels to zero, so take a large fraction of the sky and check
     only close to the galactic plane"""
     pixel_indices = np.arange(10000, 30000, dtype=np.int)
     instrument_config = self.instrument_config
     instrument_config["pixel_indices"] = pixel_indices
     instrument = pysm.Instrument(instrument_config)
     smoothed = instrument.smoother(self.synch_1_30GHz[..., pixel_indices])
     np.testing.assert_almost_equal(
         smoothed[0, 0, 10000:10100],
         self.synch_1_30GHz_smoothed[20000:20100],
         decimal=1)
Esempio n. 14
0
    def __init__(self, skyconfig, d, output_directory="./", output_prefix="planck_sky", band=143):
        self.band = band
        self.planck_central_nus = np.array([30, 44, 70, 100, 143, 217, 353, 545, 857])
        self.planck_relative_bandwidths = np.array([0.2, 0.2, 0.2, 0.33, 0.33, 0.33, 0.33, 0.33, 0.33])
        self.planck_beams = np.array([33, 24, 14, 9.2, 7.1, 5.5, 5, 5, 5])
        self.planck_Isensitivities_pixel = np.array([2, 2.7, 4.7, 2, 2.2, 4.8, 14.7, 147, 6700])
        self.planck_Psensitivities_pixel = np.array([2.8, 3.9, 6.7, np.NaN, 4.2, 9.8, 29.8, np.NaN, np.NaN])
        self.planck_channels = self.create_planck_bandwidth()
        self.planck_channels_names = ['33_GHz', '44_GHz', '70_GHz', '100_GHz', '143_GHz', '217_GHz', '353_GHz',
                                      '545_GHz', '857_GHz']

        if band is not None:
            idx = np.argwhere(self.planck_central_nus == band)[0][0]
            instrument = pysm.Instrument(
                {'nside': d['nside'], 'frequencies': self.planck_central_nus[idx:idx + 1],  # GHz
                 'use_smoothing': True, 'beams': self.planck_beams[idx:idx + 1],  # arcmin
                 'add_noise': True,  # If True `sens_I` and `sens_Q` are required
                 'noise_seed': 0,  # Not used if `add_noise` is False
                 'sens_I': self.get_planck_sensitivity("I")[idx:idx + 1],  # Not used if `add_noise` is False
                 'sens_P': self.get_planck_sensitivity("P")[idx:idx + 1],  # Not used if `add_noise` is False
                 'use_bandpass': True,  # If True pass banpasses  with the key `channels`
                 'channel_names': self.planck_channels_names[idx:idx + 1],
                 'channels': self.planck_channels[idx:idx + 1], 'output_units': 'uK_RJ',
                 'output_directory': output_directory, 'output_prefix': output_prefix, 'pixel_indices': None})
        else:
            instrument = pysm.Instrument({'nside': d['nside'], 'frequencies': self.planck_central_nus,  # GHz
                                          'use_smoothing': True, 'beams': self.planck_beams,  # arcmin
                                          'add_noise': True,  # If True `sens_I` and `sens_Q` are required
                                          'noise_seed': 0,  # Not used if `add_noise` is False
                                          'sens_I': self.get_planck_sensitivity("I"),
                                          # Not used if `add_noise` is False
                                          'sens_P': self.get_planck_sensitivity("P"),
                                          # Not used if `add_noise` is False
                                          'use_bandpass': True,  # If True pass banpasses  with the key `channels`
                                          'channel_names': self.planck_channels_names, 'channels': self.planck_channels,
                                          'output_units': 'uK_RJ', 'output_directory': output_directory,
                                          'output_prefix': output_prefix, 'pixel_indices': None})

        sky.__init__(self, skyconfig, d, instrument, output_directory, output_prefix)
Esempio n. 15
0
    def test_noise_partialsky(self):
        local_instrument_config = self.instrument_config.copy()
        local_instrument_config["pixel_indices"] = np.arange(20000,
                                                             dtype=np.int)
        instrument = pysm.Instrument(local_instrument_config)
        noise = instrument.noiser()

        assert noise[0].shape == (
            3, len(local_instrument_config["pixel_indices"]))
        np.testing.assert_almost_equal(np.std(noise[0][0]),
                                       self.expected_T_std,
                                       decimal=2)
        np.testing.assert_almost_equal(np.std(noise[0][1]),
                                       self.expected_P_std,
                                       decimal=2)
        np.testing.assert_almost_equal(np.std(noise[0][2]),
                                       self.expected_P_std,
                                       decimal=2)
Esempio n. 16
0
    def setUp(self):
        NSIDE = 16
        MODEL = 'c1d0s0f1'
        INSTRUMENT = 'litebird'
        X0_FACTOR = 0.99
        sky = pysm.Sky(get_sky(NSIDE, MODEL))
        self.instrument = pysm.Instrument(get_instrument(INSTRUMENT, NSIDE))
        with suppress_stdout():
            self.freq_maps, self.noise = self.instrument.observe(
                sky, write_outputs=False)

        self.components = [cm.CMB(), cm.Dust(200.), cm.Synchrotron(100.)]
        freefree = cm.PowerLaw(100.)
        freefree.defaults = [-2.14]  # Otherwise it is the same as Synchrotron
        self.components.append(freefree)
        self.input = []
        for component in self.components:
            self.input += component.defaults
            component.defaults = [d * X0_FACTOR for d in component.defaults]
Esempio n. 17
0
    def __init__(self, skyconfig, d, output_directory="./", output_prefix="qubic_sky"):
        _, nus_edge_in, central_nus, deltas, _, _ = qubic.compute_freq(d['filter_nu'] / 1e9, d['nf_sub'],
                                                                       # Multiband instrument model
                                                                       d['filter_relative_bandwidth'])
        self.qubic_central_nus = central_nus
        self.qubic_resolution_nus = 61.347409 / self.qubic_central_nus
        self.qubic_channels_names = ["{:.3s}".format(str(i)) + "_GHz" for i in self.qubic_central_nus]

        instrument = pysm.Instrument({'nside': d['nside'], 'frequencies': central_nus,  # GHz
                                      'use_smoothing': False, 'beams': np.ones_like(central_nus),  # arcmin
                                      'add_noise': False,  # If True `sens_I` and `sens_Q` are required
                                      'noise_seed': 0.,  # Not used if `add_noise` is False
                                      'sens_I': np.ones_like(central_nus),  # Not used if `add_noise` is False
                                      'sens_P': np.ones_like(central_nus),  # Not used if `add_noise` is False
                                      'use_bandpass': False,  # If True pass banpasses  with the key `channels`
                                      'channel_names': self.qubic_channels_names,  # np.ones_like(central_nus),
                                      'channels': np.ones_like(central_nus), 'output_units': 'uK_RJ',
                                      'output_directory': output_directory, 'output_prefix': output_prefix,
                                      'pixel_indices': None})

        sky.__init__(self, skyconfig, d, instrument, output_directory, output_prefix)
def _get_instrument(tag, nside=None):
    if 'dict' in tag:
        instrument = {}
        instrument['Frequencies'] = np.arange(10., 300, 30.)
        if 'h**o' in tag:
            instrument['Sens_I'] = (np.linspace(20., 40., 10) - 30)**2
            instrument['Sens_P'] = instrument['Sens_I']
        elif 'vary' in tag:
            np.random.seed(0)
            instrument['Cov_N'] = (np.linspace(20., 40., 10) - 30)**4
            instrument['Cov_N'] /= hp.nside2resol(nside, arcmin=True)**2
            shape = (instrument['Frequencies'].size, hp.nside2npix(nside))
            factor = 10**np.random.uniform(-1, 1, size=np.prod(shape))
            factor = factor.reshape(shape)
            instrument['Cov_N'] = instrument['Cov_N'][:, np.newaxis] * factor

            instrument['Cov_I'] = instrument['Cov_N'][:, np.newaxis]

            instrument['Cov_P'] = np.stack([instrument['Cov_N']] * 2, axis=1)
    elif 'pysm' in tag:
        instrument = pysm.Instrument(get_instrument('test', nside))
    else:
        raise ValueError('Unsupported tag: %s' % tag)
    return instrument
Esempio n. 19
0
    def __init__(self, skyconfig, d, band=143, channel_length=100):
        self.band = band
        self.planck_central_nus = np.array(
            [30, 44, 70, 100, 143, 217, 353, 545, 857])
        self.planck_relative_bandwidths = np.array(
            [0.2, 0.2, 0.2, 0.33, 0.33, 0.33, 0.33, 0.33, 0.33])
        self.planck_beams = np.array([33, 24, 14, 10, 7.1, 5, 5, 5, 5])
        self.planck_Isensitivities_pixel = np.array(
            [2, 2.7, 4.7, 2.5, 2.2, 4.8, 14.7, 147, 6700])
        self.planck_Psensitivities_pixel = np.array(
            [2.8, 3.9, 6.7, 4.0, 4.2, 9.8, 29.8, np.NaN, np.NaN])
        self.planck_channels = self.create_planck_bandwidth(channel_length)
        self.planck_channels_names = [
            '33_GHz', '44_GHz', '70_GHz', '100_GHz', '143_GHz', '217_GHz',
            '353_GHz', '545_GHz', '857_GHz'
        ]

        if band is not None:
            idx = np.argwhere(self.planck_central_nus == band)[0][0]
            instrument = pysm.Instrument({
                'nside':
                d['nside'],
                'frequencies':
                self.planck_central_nus[idx:idx + 1],  # GHz
                'use_smoothing':
                True,
                'beams':
                self.planck_beams[idx:idx + 1],  # arcmin 
                'add_noise':
                True,
                'noise_seed':
                0,
                'sens_I':
                self.get_planck_sensitivity("I")[idx:idx + 1],
                'sens_P':
                self.get_planck_sensitivity("P")[idx:idx + 1],
                'use_bandpass':
                True,
                'channel_names':
                self.planck_channels_names[idx:idx + 1],
                'channels':
                self.planck_channels[idx:idx + 1],
                'output_units':
                'uK_RJ',
                'output_directory':
                "./",
                'output_prefix':
                "planck_one",
                'pixel_indices':
                None
            })
        else:
            instrument = pysm.Instrument({
                'nside':
                d['nside'],
                'frequencies':
                self.planck_central_nus,  # GHz
                'use_smoothing':
                True,
                'beams':
                self.planck_beams,  # arcmin 
                'add_noise':
                True,
                'noise_seed':
                0,
                'sens_I':
                self.get_planck_sensitivity("I"),
                'sens_P':
                self.get_planck_sensitivity("P"),
                'use_bandpass':
                True,
                'channel_names':
                self.planck_channels_names,
                'channels':
                self.planck_channels,
                'output_units':
                'uK_RJ',
                'output_directory':
                "./",
                'output_prefix':
                "planck_all",
                'pixel_indices':
                None
            })

        sky.__init__(self, skyconfig, d, instrument)
Esempio n. 20
0
    def test_planck_sky(self):

        maps_p = sky_p.get_sky_map()
        maps_p_all = sky_p_all.get_sky_map()
        for i in [0, 1, 2, 3, 5, 6, 7, 8]:
            self.assertFalse(
                np.allclose(np.std(maps_p_all[i], axis=0),
                            np.std(maps_p[0], axis=0),
                            atol=1e-2))
            self.assertFalse(
                np.allclose(np.mean(maps_p_all[i], axis=0),
                            np.mean(maps_p[0], axis=0),
                            atol=1e-2))
        self.assertTrue(
            np.allclose(np.std(maps_p_all[4], axis=0),
                        np.std(maps_p[0], axis=0),
                        atol=1e-2))
        self.assertTrue(
            np.allclose(np.mean(maps_p_all[4], axis=0),
                        np.mean(maps_p[0], axis=0),
                        atol=1e-2))

        instrument_nless_pless = pysm.Instrument({
            'nside':
            d['nside'],
            'frequencies':
            sky_p.planck_central_nus[4:5],  # GHz
            'use_smoothing':
            False,
            'beams':
            None,  # arcmin 
            'add_noise':
            False,
            'noise_seed':
            None,
            'sens_I':
            None,
            'sens_P':
            None,
            'use_bandpass':
            True,
            'channel_names':
            sky_p.planck_channels_names[4:5],
            'channels':
            sky_p.planck_channels[4:5],
            'output_units':
            'Jysr',
            'output_directory':
            "./",
            'output_prefix':
            "planck_sky_pless_nless",
            'pixel_indices':
            None
        })
        sky_p_nless_pless = si.sky(sky_config, d, instrument_nless_pless)
        maps_p_nless_pless = sky_p_nless_pless.get_sky_map()
        disintegrated_maps = np.rollaxis(
            sky_p_nless_pless.sky.signal()(nu=sky_p.planck_channels[4:5][0][0])
            * pysm.pysm.convert_units(
                "uK_RJ", "Jysr", sky_p.planck_channels[4:5][0][0])[..., None,
                                                                   None], 2, 1)
        integrated_maps = np.mean(disintegrated_maps, axis=0)
        self.assertTrue(np.allclose(maps_p_nless_pless, integrated_maps))

        instrument_noisy = pysm.Instrument({
            'nside':
            d['nside'],
            'frequencies':
            sky_p.planck_central_nus[4:5],  # GHz
            'use_smoothing':
            False,
            'beams':
            None,  # arcmin 
            'add_noise':
            True,
            'noise_seed':
            0,
            'sens_I':
            sky_p.get_planck_sensitivity("I")[4:5],
            'sens_P':
            sky_p.get_planck_sensitivity("P")[4:5],
            'use_bandpass':
            False,
            'channel_names':
            sky_p.planck_channels_names[4:5],
            'channels':
            None,
            'output_units':
            'uK_RJ',
            'output_directory':
            "./",
            'output_prefix':
            "planck_sky_noisy",
            'pixel_indices':
            None
        })
        sky_p_noisy = si.sky(sky_config, d, instrument_noisy)
        maps_p_noisy, noise_map_produced = sky_p_noisy.get_sky_map(
            noise_map=True)
        noise_map = maps_p_noisy[0] - sky_p.sky.signal()(
            nu=sky_p.planck_central_nus[4:5]).T
        self.assertTrue(np.allclose(noise_map, noise_map_produced))
        c = np.sqrt(hp.nside2pixarea(d['nside'], degrees=True)) * 60  # arcmin
        C = pysm.pysm.convert_units("uK_RJ", "uK_CMB",
                                    sky_p.planck_central_nus[4:5][0])
        self.assertTrue(
            np.allclose(np.std(noise_map, axis=0)[0] * c * C,
                        sky_p.get_planck_sensitivity("I")[4:5],
                        atol=1e-1))
        self.assertTrue(
            np.allclose(np.std(noise_map, axis=0)[1] * c * C,
                        sky_p.get_planck_sensitivity("P")[4:5],
                        atol=1e-1))
        self.assertTrue(
            np.allclose(np.std(noise_map, axis=0)[2] * c * C,
                        sky_p.get_planck_sensitivity("P")[4:5],
                        atol=1e-1))

        sm_pns = sky_p.planck_beams[4:5][0] * np.pi / (60 * 180)
        jysky = sky_p.instrument.apply_bandpass(sky_p.sky.signal(), sky_p.sky)
        maps_a = sky_p.instrument.smoother(jysky)[0].T
        maps_b = hp.smoothing(jysky[0], fwhm=sm_pns).T
        self.assertTrue(np.allclose(maps_a, maps_b))

        # instrument_mono = pysm.Instrument({
        #     'nside': d['nside'],
        #     'frequencies' : sky_p.planck_central_nus[4:5], # GHz
        #     'use_smoothing' : True,
        #     'beams' : sky_p.planck_beams[4:5], # arcmin
        #     'add_noise' : True,
        #     'noise_seed' : 0,
        #     'sens_I': sky_p.get_planck_sensitivity("I")[4:5],
        #     'sens_P': sky_p.get_planck_sensitivity("P")[4:5],
        #     'use_bandpass' : False,
        #     'channel_names' : sky_p.planck_channels_names[4:5],
        #     'channels' : None,
        #     'output_units' : 'uK_RJ',
        #     'output_directory' : "./",
        #     'output_prefix' : "planck_sky_mono",
        #     'pixel_indices' : None})
        # sky_p_mono = si.sky(sky_config, d, instrument_mono)
        # maps_p_mono = sky_p_mono.get_sky_map()
        # self.assertTrue(maps_p_mono.shape == (1, 196608, 3))

        # instrument_mono_nless = pysm.Instrument({
        #     'nside': d['nside'],
        #     'frequencies' : sky_p.planck_central_nus[4:5], # GHz
        #     'use_smoothing' : True,
        #     'beams' : sky_p.planck_beams[4:5], # arcmin
        #     'add_noise' : False,
        #     'noise_seed' : None,
        #     'sens_I': None,
        #     'sens_P': None,
        #     'use_bandpass' : False,
        #     'channel_names' : sky_p.planck_channels_names[4:5],
        #     'channels' : None,
        #     'output_units' : 'uK_RJ',
        #     'output_directory' : "./",
        #     'output_prefix' : "planck_sky_nless_mono",
        #     'pixel_indices' : None})
        # sky_p_mono_nless = si.sky(sky_config, d, instrument_mono_nless)
        # maps_p_mono_nless = sky_p_mono_nless.get_sky_map()
        # self.assertTrue(maps_p_mono_nless.shape == (1, 196608, 3))

        instrument_mono_nless_pless = pysm.Instrument({
            'nside':
            d['nside'],
            'frequencies':
            sky_p.planck_central_nus[4:5],  # GHz
            'use_smoothing':
            False,
            'beams':
            None,  # arcmin 
            'add_noise':
            False,
            'noise_seed':
            None,
            'sens_I':
            None,
            'sens_P':
            None,
            'use_bandpass':
            False,
            'channel_names':
            sky_p.planck_channels_names[4:5],
            'channels':
            None,
            'output_units':
            'uK_RJ',
            'output_directory':
            "./",
            'output_prefix':
            "planck_sky_pless_nless_mono",
            'pixel_indices':
            None
        })
        sky_p_mono_nless_pless = si.sky(sky_config, d,
                                        instrument_mono_nless_pless)
        maps_p_mono_nless_pless = sky_p_mono_nless_pless.get_sky_map()
        self.assertTrue(
            np.allclose(maps_p_mono_nless_pless[0],
                        pysm.Sky(sky_config).signal()(nu=143).T))
Esempio n. 21
0
    def test_d0s0(self):

        # EXTERNAL_xFORECAST_RUN RESULTS
        EXT_BETA = [1.54000015, 19.99999402, -3.00000035]
        EXT_SIGMA_BETA = np.array(
            [[1.07107731e+09, 1.02802459e+07, 3.05900728e+07],
             [1.02802459e+07, 8.80751745e+05, 2.64258857e+05],
             [3.05900728e+07, 2.64258857e+05, 8.80649611e+05]])
        EXT_NOISE_POST_COMP_SEP = [
            6.985360419978725e-07, 6.954968812329504e-07,
            6.952971018678473e-07, 6.97621370087622e-07, 6.97915989771863e-07,
            6.981532542428136e-07, 6.984690244398313e-07,
            6.963706038663776e-07, 6.962958090983174e-07,
            6.999793141962897e-07, 6.966029199088166e-07,
            6.998332244730213e-07, 6.97245540450936e-07, 7.013469190449905e-07,
            6.98145319051069e-07, 6.997552902541847e-07, 7.006828378883164e-07,
            6.993357502111902e-07, 7.016843277673384e-07, 7.02276431905913e-07,
            7.009651598790946e-07, 7.024327502574484e-07,
            7.058590396724249e-07, 7.035637090541009e-07,
            7.034402740635456e-07, 7.05326337473677e-07, 7.086905417607417e-07,
            7.067287662339356e-07, 7.06396320822362e-07, 7.075857215168964e-07,
            7.102089978543108e-07, 7.118461226661247e-07
        ]
        EXT_CL_STAT_RES = [
            3.43065437e-08, 2.13752688e-07, 2.50160994e-08, 4.39734801e-08,
            1.75192647e-08, 2.10382699e-08, 9.55361360e-09, 8.80726572e-09,
            7.34671936e-09, 4.24354505e-09, 3.50430309e-09, 3.21803173e-09,
            3.62342203e-09, 1.83222822e-09, 2.40687985e-09, 1.76806752e-09,
            2.57252032e-09, 1.19987889e-09, 1.71606507e-09, 1.01867261e-09,
            1.11709059e-09, 1.05584166e-09, 8.37499498e-10, 1.04610499e-09,
            7.27953346e-10, 7.55604710e-10, 5.50190292e-10, 6.38657310e-10,
            4.82912230e-10, 5.21029442e-10, 4.77954181e-10
        ]
        EXT_BIAS_R = 0.00100556
        EXT_SIGMA_R = 0.0003163

        nside = 16
        # define sky and foregrounds simulations
        sky = pysm.Sky(get_sky(nside, 'd0s0'))
        # define instrument
        instrument = pysm.Instrument(get_instrument('litebird', nside))
        # get noiseless frequency maps
        with suppress_stdout():
            freq_maps = instrument.observe(sky, write_outputs=False)[0]
        # take only the Q and U maps
        freq_maps = freq_maps[:, 1:]
        # define components used in the modeling
        components = [CMB(), Dust(150.), Synchrotron(150.)]
        # call for xForecast
        with suppress_stdout():
            res = xForecast(components,
                            instrument,
                            freq_maps,
                            2,
                            2 * nside - 1,
                            1.0,
                            make_figure=False)
        # list of checks
        aac(EXT_BETA, res.x, rtol=1e-03)
        aac(np.diag(EXT_SIGMA_BETA), np.diag(res.Sigma_inv), rtol=5e-02)
        aac(EXT_NOISE_POST_COMP_SEP[0], res.noise[0], rtol=1e-02)
        aac(EXT_BIAS_R, res.cosmo_params['r'][0][0], rtol=1e-02)
        aac(EXT_SIGMA_R, res.cosmo_params['r'][1][0], rtol=1e-02)
Esempio n. 22
0
	hp.mollview(sigma_U_dust, sub=(248), title='sigma U dust')
	pl.show()
	# exit()
def p_template_computation(temp_):
	logprob = -0.5*np.sum(templates_map**2/np.diag(covariance_templates)**2)
	return logprob
# build a random vector of template of size 4 x Npix 
# for example : 
template_test = np.random.normal(0,1,(4,Npix))
print p_template_computation(template_test)
'''
############################################
####  CONSTRUCTING THE MIXING MATRIX A
############################################
# define the instrumental specifications
instrument = pysm.Instrument(get_instrument(NSIDE, 'litebird'))
# define the components in the sky and their scaling laws
components=[CMB(), Dust(150.), Synchrotron(150.)]
# initiate function to estimate scaling laws for LiteBIRD frequencies
A = MixingMatrix(*components)
A_ev = A.evaluator(instrument.Frequencies)
print(A_ev([1.56,20,-3.1]).shape)
print(instrument.Frequencies)

'''
####################################################
####  CONSTRUCTING TEMPLATES OF BETA AND SIGMA BETA
#### -> p(\beta} \propto exp[-1/2 (\beta-\bar{\beta})^T \sigma_\beta^{-2} (\beta-\bar{\beta})]
####################################################
dust_spectral_indices_ = hp.read_map('COM_CompMap_dust-commander_0256_R2.00.fits', field=(3,5,6,8))
dust_spectral_indices_ = hp.ud_grade(dust_spectral_indices_, nside_out=NSIDE)
Esempio n. 23
0
 def test_no_smoothing(self):
     instrument_config = self.instrument_config
     instrument_config['use_smoothing'] = False
     instrument = pysm.Instrument(instrument_config)
     smoothed = instrument.smoother(self.synch_1_30GHz)
     np.testing.assert_almost_equal(smoothed, self.synch_1_30GHz, decimal=6)
Esempio n. 24
0
    #Instrument config
    instr_config = {
        'nside': nside,
        'frequencies': nu,
        'use_smoothing': True,
        'beams': np.ones(len(nu)) * 30.,  # arcmin
        'add_noise': True,
        'sens_I': np.ones(len(nu)) * sense_P / np.sqrt(2),
        'sens_P': np.ones(len(nu)) * sense_P,
        'noise_seed': seed + i,
        'use_bandpass': False,
        'output_units': 'uK_RJ',
        'output_directory': outdir,
        'output_prefix': 'test',
    }
    instr = pysm.Instrument(instr_config)

    x = instr.observe(sky, write_outputs=False)

    tot_map_flat = x[0][:, :, hp_pixels]
    noise = x[1][:, :, hp_pixels]
    #tot_map_flat = instr.observe(sky, write_outputs=False)[0][:,:,hp_pixels]
    tot_map_flat = np.array(tot_map_flat, dtype=np.float32)

    np.save(
        os.path.join(
            outdir, 'total_signal_SV_' + str(int(npix)) + 'x' +
            str(int(npix)) + '_senseP' + str(sense_P) + '_seed' +
            str(int(seed)) + '_nu' + str(len(nu)),
            'tot_' + str(i).zfill(6) + '.npy'), tot_map_flat)
Esempio n. 25
0
def get_sky_realization(nside, seed=None, mean_pars=None, moment_pars=None,
                        compute_cls=False, delta_ell=10):
    """ Generate a sky realization for a set of input sky parameters.

    Args:
        nside: HEALPix resolution parameter.
        seed: seed to be used (if `None`, then a random seed will
            be used).
        mean_pars: mean parameters (see `get_default_params`).
            If `None`, then a default set will be used.
        moment_pars: mean parameters (see `get_default_params`).
            If `None`, then a default set will be used.
        compute_cls: return also the power spectra? Default: False.
        delta_ell: bandpower size to use if compute_cls is True.

    Returns:
        A dictionary containing the different component maps,
        spectral index maps and frequency maps.
        If `compute_cls=True`, then the dictionary will also
        contain power spectrum information.
    """
    nu = get_freqs()
    npix = hp.nside2npix(nside)
    if seed is not None:
        np.random.seed(seed)
    if mean_pars is None:
        mean_pars, _ = get_default_params()
    if moment_pars is None:
        _, moment_pars = get_default_params()
    lmax = 3*nside-1
    ells, dl2cl, cl2dl, cl_dust_bb, cl_dust_ee, cl_sync_bb, cl_sync_ee, cl_cmb_bb, cl_cmb_ee = get_mean_spectra(lmax, mean_pars)
    cl0 = 0 * cl_dust_bb
    # Dust amplitudes
    Q_dust, U_dust = hp.synfast([cl0, cl_dust_ee, cl_dust_bb, cl0, cl0, cl0],
                                nside, new=True, verbose=False)[1:]
    # Sync amplitudes
    Q_sync, U_sync = hp.synfast([cl0, cl_sync_ee, cl_sync_bb, cl0, cl0, cl0],
                                nside, new=True, verbose=False)[1:]

    # CMB amplitude
    Q_cmb, U_cmb = hp.synfast([cl0, cl_cmb_ee, cl_cmb_bb, cl0, cl0, cl0],
                              nside, new=True, verbose=False)[1:]

    # Dust spectral index
    beta_dust = get_beta_map(nside,
                             mean_pars['beta_dust'],
                             moment_pars['amp_beta_dust'],
                             moment_pars['gamma_beta_dust'],
                             moment_pars['l0_beta_dust'],
                             moment_pars['l_cutoff_beta_dust'])
    # Dust temperature
    temp_dust = np.ones(npix) * mean_pars['temp_dust']
    # Synchrotron spectral index
    beta_sync = get_beta_map(nside, 
                             mean_pars['beta_sync'],
                             moment_pars['amp_beta_sync'],
                             moment_pars['gamma_beta_sync'],
                             moment_pars['l0_beta_sync'],
                             moment_pars['l_cutoff_beta_sync'])

    # Create PySM simulation
    zeromap = np.zeros(npix)
    # Dust
    d2 = models("d2", nside)
    d2[0]['nu_0_I'] = mean_pars['nu0_dust']
    d2[0]['nu_0_P'] = mean_pars['nu0_dust']
    d2[0]['A_I'] = zeromap
    d2[0]['A_Q'] = Q_dust
    d2[0]['A_U'] = U_dust
    d2[0]['spectral_index'] = beta_dust
    d2[0]['temp'] = temp_dust
    # Sync
    s1 = models("s1", nside)
    s1[0]['nu_0_I'] = mean_pars['nu0_sync']
    s1[0]['nu_0_P'] = mean_pars['nu0_sync']
    s1[0]['A_I'] = zeromap
    s1[0]['A_Q'] = Q_sync
    s1[0]['A_U'] = U_sync
    s1[0]['spectral_index'] = beta_sync
    # CMB
    c1 = models("c1", nside)
    c1[0]['model'] = 'pre_computed' #different output maps at different seeds 
    c1[0]['A_I'] = zeromap
    c1[0]['A_Q'] = Q_cmb
    c1[0]['A_U'] = U_cmb

    sky_config = {'dust' : d2, 'synchrotron' : s1, 'cmb' : c1}
    sky = pysm.Sky(sky_config)
    instrument_config = {
        'nside' : nside,
        'frequencies' : nu, #Expected in GHz 
        'use_smoothing' : False,
        'beams' : np.ones_like(nu), #Expected in arcmin 
        'add_noise' : False,
        'use_bandpass' : False,
        'channel_names' : ['LF1', 'LF2', 'MF1', 'MF2', 'UHF1', 'UHF2'],
        'output_units' : 'uK_RJ',
        'output_directory' : 'none',
        'output_prefix' : 'none',
    }
    sky = pysm.Sky(sky_config)
    instrument = pysm.Instrument(instrument_config)
    maps_signal, _ = instrument.observe(sky, write_outputs=False)
    maps_signal = maps_signal[:,1:,:]
    # Change to CMB units
    maps_signal = maps_signal/fcmb(nu)[:,None,None]

    dict_out = {'maps_dust': np.array([Q_dust, U_dust]),
                'maps_sync': np.array([Q_sync, U_sync]),
                'maps_cmb': np.array([Q_cmb, U_cmb]),
                'beta_dust': beta_dust,
                'beta_sync': beta_sync,
                'freq_maps': maps_signal}

    if compute_cls:
        cls_unbinned = map2cl(maps_signal)
        l_binned, windows, cls_binned = bin_cls(cls_unbinned,
                                                delta_ell=delta_ell)
        indices, cls_binned, cov_binned = get_vector_and_covar(l_binned,
                                                               cls_binned)
        dict_out['ls_unbinned'] = ells
        dict_out['cls_unbinned'] = cls_unbinned
        dict_out['ls_binned'] = l_binned
        dict_out['cls_binned'] = cls_binned
        dict_out['cov_binned'] = cov_binned
        dict_out['ind_cl'] = indices
        dict_out['windows'] = windows

    return dict_out
Esempio n. 26
0
bandpass_low = det["Frequency"] - det["Bandwidth"]/2
bandpass_high = det["Frequency"] + det["Bandwidth"]/2
unit = 'uK_CMB'
use_bandpass = False
output_directory = 'pysm_components/nside{}_{}bandpass_{}'.format(nside, "tophat" if use_bandpass else "delta", unit)
os.makedirs(output_directory, exist_ok=True)
instrument_bpass = {
    'use_smoothing' : True,
    'beams' : np.array([det["Beam_FWHM"]]),
    'nside' : nside,
    'add_noise' : False,
    'use_bandpass' : use_bandpass,
    'channels' : [(np.linspace(bandpass_low, bandpass_high, 10), np.ones(10))],
    'frequencies' : [det["Frequency"]],
    'output_units' : unit,
    'output_directory' : output_directory,
    'output_prefix' : 'pico',
    'noise_seed' : 1234,
}

for c in components:

    full_name = components_name[c[0]]
    instrument_bpass['channel_names'] = [f'band_{band}_{full_name}_{unit}']
    sky_config = { full_name : models(c, nside=nside) }
    sky = pysm.Sky(sky_config)

    instrument = pysm.Instrument(instrument_bpass)
    instrument.observe(sky)
Esempio n. 27
0
Npix = 12 * NSIDE**2

COSMO_PARAMS_NAMES = [
    "n_s", "omega_b", "omega_cdm", "100*theta_s", "ln10^{10}A_s", "tau_reio"
]
COSMO_PARAMS_MEANS = [0.9665, 0.02242, 0.11933, 1.04101, 3.047, 0.0561]
COSMO_PARAMS_SIGMA = [0.0038, 0.00014, 0.00091, 0.00029, 0.014, 0.0071]
LiteBIRD_sensitivities = np.array([
    36.1, 19.6, 20.2, 11.3, 10.3, 8.4, 7.0, 5.8, 4.7, 7.0, 5.8, 8.0, 9.1, 11.4,
    19.6
])

Qs, Us, sigma_Qs, sigma_Us = aggregate_by_pixels_params(
    get_pixels_params(NSIDE))

instrument = pysm.Instrument(get_instrument('litebird', NSIDE))
components = [CMB(), Dust(150.), Synchrotron(150.)]
mixing_matrix = MixingMatrix(*components)
mixing_matrix_evaluator = mixing_matrix.evaluator(instrument.Frequencies)


def noise_covariance_in_freq(nside):
    cov = LiteBIRD_sensitivities**2 / hp.nside2resol(nside, arcmin=True)**2
    return cov


noise_covar_one_pix = noise_covariance_in_freq(NSIDE)


def sample_mixing_matrix_parallel(betas):
    return mixing_matrix_evaluator(betas)[:, 1:]