예제 #1
0
 def test_with_cov(self):
     simdict = Simulations()
     meadict = Measurements()
     covdict = Covariances()
     # mock measurements
     dtuple = DomainTuple.make((RGSpace(1), RGSpace(12)))
     arr_a = np.random.rand(1, 12)
     comm.Bcast(arr_a, root=0)
     mea = Observable(dtuple, arr_a)
     meadict.append(('test', 'nan', '12', 'nan'), mea, True)
     # mock sims
     dtuple = DomainTuple.make((RGSpace(5*mpisize), RGSpace(12)))
     arr_b = np.random.rand(5, 12)
     sim = Observable(dtuple, arr_b)
     simdict.append(('test', 'nan', '12', 'nan'), sim, True)
     # mock covariance
     arr_c = np.random.rand(12, 12)
     comm.Bcast(arr_c, root=0)
     dtuple = DomainTuple.make((RGSpace(shape=arr_c.shape)))
     cov = Field.from_global_data(dtuple, arr_c)
     covdict.append(('test', 'nan', '12', 'nan'), cov, True)
     # with covariance
     lh = SimpleLikelihood(meadict, covdict)
     # calc by likelihood
     rslt = lh(simdict)  # feed variable value, not parameter value
     # calc by hand
     arr_b = sim.to_global_data()  # get global arr_b
     diff = (np.mean(arr_b, axis=0) - arr_a)
     (sign, logdet) = np.linalg.slogdet(arr_c*2.*np.pi)
     baseline = -float(0.5)*float(np.vdot(diff, np.linalg.solve(arr_c, diff.T))+sign*logdet)
     self.assertAlmostEqual(rslt, baseline)
예제 #2
0
 def test_without_simcov(self):
     simdict = Simulations()
     meadict = Measurements()
     covdict = Covariances()
     # mock measurements
     dtuple = DomainTuple.make((RGSpace(1), HPSpace(nside=2)))
     arr_a = np.random.rand(1, 48)
     comm.Bcast(arr_a, root=0)
     mea = Observable(dtuple, arr_a)
     meadict.append(('test', 'nan', '2', 'nan'), mea)
     # mock covariance
     dtuple = DomainTuple.make((RGSpace(shape=(48, 48))))
     arr_c = np.random.rand(48, 48)
     comm.Bcast(arr_c, root=0)
     cov = Field.from_global_data(dtuple, arr_c)
     covdict.append(('test', 'nan', '2', 'nan'), cov)
     # mock observable with repeated single realisation
     dtuple = DomainTuple.make((RGSpace(5*mpisize), HPSpace(nside=2)))
     arr_b = np.random.rand(1, 48)
     comm.Bcast(arr_b, root=0)
     arr_ens = np.zeros((5, 48))
     for i in range(len(arr_ens)):
         arr_ens[i] = arr_b
     sim = Observable(dtuple, arr_ens)
     simdict.append(('test', 'nan', '2', 'nan'), sim)
     # simplelikelihood
     lh_simple = SimpleLikelihood(meadict, covdict)
     rslt_simple = lh_simple(simdict)
     # ensemblelikelihood
     lh_ensemble = EnsembleLikelihood(meadict, covdict)
     rslt_ensemble = lh_ensemble(simdict)
     self.assertEqual(rslt_ensemble, rslt_simple)
예제 #3
0
 def test_with_cov(self):
     simdict = Simulations()
     meadict = Measurements()
     covdict = Covariances()
     # mock measurements
     arr_a = np.random.rand(1, 4 * mpisize)
     comm.Bcast(arr_a, root=0)
     mea = Observable(arr_a, 'measured')
     meadict.append(('test', 'nan', str(4 * mpisize), 'nan'), mea, True)
     # mock sims
     arr_b = np.random.rand(5, 4 * mpisize)
     sim = Observable(arr_b, 'simulated')
     simdict.append(('test', 'nan', str(4 * mpisize), 'nan'), sim, True)
     # mock covariance
     arr_c = np.random.rand(4, 4 * mpisize)
     cov = Observable(arr_c, 'covariance')
     covdict.append(('test', 'nan', str(4 * mpisize), 'nan'), cov, True)
     # with covariance
     lh = SimpleLikelihood(meadict, covdict)
     # calc by likelihood
     rslt = lh(simdict)  # feed variable value, not parameter value
     # calc by hand
     full_b = np.vstack(comm.allgather(arr_b))  # global arr_b
     diff = (np.mean(full_b, axis=0) - arr_a)
     full_cov = np.vstack(comm.allgather(arr_c))  # global covariance
     (sign, logdet) = np.linalg.slogdet(full_cov * 2. * np.pi)
     baseline = -float(0.5) * float(
         np.vdot(diff, np.linalg.solve(full_cov, diff.T)) + sign * logdet)
     self.assertAlmostEqual(rslt, baseline)
예제 #4
0
 def test_without_simcov(self):
     simdict = Simulations()
     meadict = Measurements()
     covdict = Covariances()
     # mock measurements
     arr_a = np.random.rand(1, 4 * mpisize)
     comm.Bcast(arr_a, root=0)
     mea = Observable(arr_a, 'measured')
     meadict.append(('test', 'nan', str(4 * mpisize), 'nan'), mea, True)
     # mock covariance
     arr_c = np.random.rand(4, 4 * mpisize)
     cov = Observable(arr_c, 'covariance')
     covdict.append(('test', 'nan', str(4 * mpisize), 'nan'), cov, True)
     # mock observable with repeated single realisation
     arr_b = np.random.rand(1, 4 * mpisize)
     comm.Bcast(arr_b, root=0)
     arr_ens = np.zeros((2, 4 * mpisize))
     for i in range(len(arr_ens)):
         arr_ens[i] = arr_b
     sim = Observable(arr_ens, 'simulated')
     simdict.append(('test', 'nan', str(4 * mpisize), 'nan'), sim, True)
     # simplelikelihood
     lh_simple = SimpleLikelihood(meadict, covdict)
     rslt_simple = lh_simple(simdict)
     # ensemblelikelihood
     lh_ensemble = EnsembleLikelihood(meadict, covdict)
     rslt_ensemble = lh_ensemble(simdict)
     self.assertEqual(rslt_ensemble, rslt_simple)
예제 #5
0
 def test_covdict_apply_mask(self):
     msk = np.random.randint(0, 2, 2 * mpisize).reshape(1, -1)
     mskdict = Masks()
     comm.Bcast(msk, root=0)
     mskdict.append(('test', 'nan', str(2 * mpisize), 'nan'), msk, True)
     cov = np.random.rand(2, 2 * mpisize)
     covdict = Covariances()
     covdict.append(('test', 'nan', str(2 * mpisize), 'nan'), cov, True)
     covdict.apply_mask(mskdict)
     pix_num = msk.sum()
     self.assertTrue(('test', 'nan', str(pix_num), 'nan') in covdict.keys())
 def test_covdict_apply_mask(self):
     msk = np.array([0, 1, 0, 1, 1]).reshape(1, 5)
     mskdict = Masks()
     mskdict.append(('test', 'nan', '5', 'nan'), msk, True)
     cov_field = Field.from_global_data(RGSpace(shape=(5, 5)),
                                        np.random.rand(5, 5))
     arr = cov_field.local_data
     covdict = Covariances()
     covdict.append(('test', 'nan', '5', 'nan'), arr, True)
     covdict.apply_mask(mskdict)
     arr = np.delete(cov_field.to_global_data(), [0, 2], 0)
     arr = np.delete(arr, [0, 2], 1)
     for i in range(arr.shape[0]):
         self.assertListEqual(
             list((covdict[('test', 'nan', '3',
                            'nan')].to_global_data())[i]), list(arr[i]))
예제 #7
0
 def test_covdict_append_observable(self):
     cov = Observable(np.random.rand(2, 2 * mpisize), 'covariance')
     covdict = Covariances()
     covdict.append(('test', 'nan', str(2 * mpisize), 'nan'), cov,
                    True)  # plain covariance
     for i in range(len(cov.data)):
         self.assertListEqual(
             list((covdict[('test', 'nan', str(2 * mpisize),
                            'nan')].data)[i]), list((cov.data)[i]))
     cov = Observable(np.random.rand(12 * mpisize, 12 * mpisize * mpisize),
                      'covariance')
     covdict.append(('test', 'nan', str(mpisize), 'nan'),
                    cov)  # healpix covariance
     for i in range(len(cov.data)):
         self.assertListEqual(
             list((covdict[('test', 'nan', str(mpisize), 'nan')].data)[i]),
             list((cov.data)[i]))
 def test_covdict_append_field(self):
     cov_field = Field.from_global_data(RGSpace(shape=(3, 3)),
                                        np.random.rand(3, 3))
     covdict = Covariances()
     covdict.append(('test', 'nan', '3', 'nan'), cov_field,
                    True)  # plain covariance
     for i in range(len(cov_field.local_data)):
         self.assertListEqual(
             list((covdict[('test', 'nan', '3', 'nan')].local_data)[i]),
             list((cov_field.local_data)[i]))
     cov_field = Field.from_global_data(RGSpace(shape=(48, 48)),
                                        np.random.rand(48, 48))
     covdict.append(('test', 'nan', '2', 'nan'),
                    cov_field)  # healpix covariance
     for i in range(len(cov_field.local_data)):
         self.assertListEqual(
             list((covdict[('test', 'nan', '2', 'nan')].local_data)[i]),
             list((cov_field.local_data)[i]))
 def test_covdict_append_array(self):
     cov = (Field.from_global_data(RGSpace(shape=(5, 5)),
                                   np.random.rand(5, 5))).local_data
     covdict = Covariances()
     covdict.append(('test', 'nan', '5', 'nan'), cov,
                    True)  # plain covariance
     self.assertEqual(covdict[('test', 'nan', '5', 'nan')].shape, (5, 5))
     for i in range(len(cov)):
         self.assertListEqual(
             list((covdict[('test', 'nan', '5', 'nan')].local_data)[i]),
             list(cov[i]))
     cov = np.random.rand(48, 48)
     comm.Bcast(cov, root=0)
     covdict.append(('test', 'nan', '2', 'nan'), cov)  # healpix covariance
     self.assertEqual(covdict[('test', 'nan', '2', 'nan')].shape, (48, 48))
     for i in range(len(cov)):
         self.assertListEqual(
             list((covdict[('test', 'nan', '2',
                            'nan')].to_global_data())[i]), list(cov[i]))
예제 #10
0
 def test_covdict_append_array(self):
     cov = np.random.rand(2, 2 * mpisize)
     covdict = Covariances()
     covdict.append(('test', 'nan', str(2 * mpisize), 'nan'), cov,
                    True)  # plain covariance
     self.assertEqual(
         covdict[('test', 'nan', str(2 * mpisize), 'nan')].shape,
         (2 * mpisize, 2 * mpisize))
     for i in range(len(cov)):
         self.assertListEqual(
             list((covdict[('test', 'nan', str(2 * mpisize),
                            'nan')].data)[i]), list(cov[i]))
     cov = np.random.rand(12 * mpisize, 12 * mpisize * mpisize)
     covdict.append(('test', 'nan', str(mpisize), 'nan'),
                    cov)  # healpix covariance
     self.assertEqual(covdict[('test', 'nan', str(mpisize), 'nan')].shape,
                      (12 * mpisize * mpisize, 12 * mpisize * mpisize))
     for i in range(len(cov)):
         self.assertListEqual(
             list((covdict[('test', 'nan', str(mpisize), 'nan')].data)[i]),
             list(cov[i]))
def testfield():
    """

    :return:

    log.basicConfig(filename='imagine.log', level=log.INFO)
    """
    """
    # step 0, set 'a' and 'b', 'mea_std'

    TestField in LiSimulator is modeled as
        field = gaussian_random(mean=a,std=b)_x * cos(x)
        where x in (0,2pi)

    for generating mock data we need
    true values of a and b: true_a, true_b, mea_seed
    measurement uncertainty: mea_std
    measurement points, positioned in (0,2pi) evenly, due to TestField modelling
    """
    true_a = 3.
    true_b = 6.
    mea_std = 0.1  # std of gaussian measurement error
    mea_seed = 233
    mea_points = 10  # data points in measurements
    truths = [true_a, true_b]  # will be used in visualizing posterior
    """
    # step 1, prepare mock data
    """
    """
    # 1.1, generate measurements
    mea_field = signal_field + noise_field
    """
    x = np.linspace(0, 2. * np.pi, mea_points)
    np.random.seed(mea_seed)  # seed for signal field
    signal_field = np.multiply(
        np.cos(x), np.random.normal(loc=true_a, scale=true_b, size=mea_points))
    mea_field = np.vstack([
        signal_field + np.random.normal(loc=0., scale=mea_std, size=mea_points)
    ])
    """
    # 1.2, generate covariances
    """
    # pre-defined according to measurement error
    mea_cov = (mea_std**2) * np.eye(mea_points)
    """
    # 1.3 assemble in imagine convention
    """

    mock_data = Measurements()  # create empty Measrurements object
    mock_cov = Covariances()  # create empty Covariance object
    # pick up a measurement
    mock_data.append(('test', 'nan', str(mea_points), 'nan'), mea_field, True)
    mock_cov.append(('test', 'nan', str(mea_points), 'nan'), mea_cov, True)
    """
    # 1.4, visualize mock data
    """
    #if mpirank == 0:
    #matplotlib.pyplot.plot(x, mock_data[('test', 'nan', str(mea_points), 'nan')].to_global_data()[0])
    #matplotlib.pyplot.savefig('testfield_mock.pdf')
    """
    # step 2, prepare pipeline and execute analysis
    """
    """
    # 2.1, ensemble likelihood
    """
    likelihood = EnsembleLikelihood(
        mock_data, mock_cov)  # initialize likelihood with measured info
    """
    # 2.2, field factory list
    """
    factory = TestFieldFactory(
        active_parameters=('a', 'b'))  # factory with single active parameter
    factory.parameter_ranges = {
        'a': (0, 10),
        'b': (0, 10)
    }  # adjust parameter range for Bayesian analysis
    factory_list = [factory]  # likelihood requires a list/tuple of factories
    """
    # 2.3, flat prior
    """
    prior = FlatPrior()
    """
    # 2.4, simulator 
    """
    simer = LiSimulator(mock_data)
    """
    # 2.5, pipeline
    """
    ensemble_size = 10
    pipe = MultinestPipeline(simer, factory_list, likelihood, prior,
                             ensemble_size)
    pipe.random_type = 'free'
    pipe.sampling_controllers = {
        'n_iter_before_update': 1,
        'n_live_points': 400,
        'verbose': True,
        'resume': False
    }
    results = pipe()  # run with pymultinest
    """
    # step 3, visualize (with corner package)
    """
    if mpirank == 0:
        samples = results['samples']
        for i in range(len(
                pipe.active_parameters)):  # convert variables into parameters
            low, high = pipe.active_ranges[pipe.active_parameters[i]]
            for j in range(samples.shape[0]):
                samples[j, i] = unity_mapper(samples[j, i], low, high)
        # corner plot
        corner.corner(samples[:, :len(pipe.active_parameters)],
                      range=[0.99] * len(pipe.active_parameters),
                      quantiles=[0.02, 0.5, 0.98],
                      labels=pipe.active_parameters,
                      show_titles=True,
                      title_kwargs={"fontsize": 15},
                      color='steelblue',
                      truths=truths,
                      truth_color='firebrick',
                      plot_contours=True,
                      hist_kwargs={'linewidth': 2},
                      label_kwargs={'fontsize': 15})
        matplotlib.pyplot.savefig('testfield_posterior.pdf')
예제 #12
0
def testfield(measure_size, simulation_size, make_plots=True, debug=False):
    if debug:
        log.basicConfig(filename='imagine_li_dynesty.log', level=log.DEBUG)
    else:
        log.basicConfig(filename='imagine_li_dynesty.log')
    """

    :return:

    log.basicConfig(filename='imagine.log', level=log.INFO)
    """
    """
    # step 0, set 'a' and 'b', 'mea_std'

    TestField in LiSimulator is modeled as
        field = gaussian_random(mean=a,std=b)_x * cos(x)
        where x in (0,2pi)

    for generating mock data we need
    true values of a and b: true_a, true_b, mea_seed
    measurement uncertainty: mea_std
    measurement points, positioned in (0,2pi) evenly, due to TestField modelling
    """
    true_a = 3.
    true_b = 6.
    mea_std = 0.1  # std of gaussian measurement error
    mea_seed = 233
    truths = [true_a, true_b]  # will be used in visualizing posterior
    """
    # step 1, prepare mock data
    """
    """
    # 1.1, generate measurements
    mea_field = signal_field + noise_field
    """
    x = np.linspace(0, 2. * np.pi, measure_size)  # data points in measurements
    np.random.seed(mea_seed)  # seed for signal field
    signal_field = np.multiply(
        np.cos(x), np.random.normal(loc=true_a,
                                    scale=true_b,
                                    size=measure_size))
    mea_field = np.vstack([
        signal_field +
        np.random.normal(loc=0., scale=mea_std, size=measure_size)
    ])
    """
    # 1.2, generate covariances
    what's the difference between pre-define dan re-estimated?
    """
    # re-estimate according to measurement error
    mea_repeat = np.zeros((simulation_size, measure_size))
    for i in range(simulation_size):  # times of repeated measurements
        mea_repeat[i, :] = signal_field + np.random.normal(
            loc=0., scale=mea_std, size=measure_size)
    mea_cov = oas_mcov(mea_repeat)[1]

    print(mpirank, 're-estimated: \n', mea_cov, 'slogdet',
          mpi_slogdet(mea_cov))

    # pre-defined according to measurement error
    mea_cov = (mea_std**2) * mpi_eye(measure_size)

    print(mpirank, 'pre-defined: \n', mea_cov, 'slogdet', mpi_slogdet(mea_cov))
    """
    # 1.3 assemble in imagine convention
    """

    mock_data = Measurements()  # create empty Measrurements object
    mock_cov = Covariances()  # create empty Covariance object
    # pick up a measurement
    mock_data.append(('test', 'nan', str(measure_size), 'nan'), mea_field,
                     True)
    mock_cov.append(('test', 'nan', str(measure_size), 'nan'), mea_cov, True)
    """
    # 1.4, visualize mock data
    """
    if mpirank == 0 and make_plots:
        plt.plot(x,
                 mock_data[('test', 'nan', str(measure_size), 'nan')].data[0])
        plt.savefig('testfield_mock_li.pdf')
    """
    # step 2, prepare pipeline and execute analysis
    """
    """
    # 2.1, ensemble likelihood
    """
    likelihood = EnsembleLikelihood(
        mock_data, mock_cov)  # initialize likelihood with measured info
    """
    # 2.2, field factory list
    """
    factory = TestFieldFactory(
        active_parameters=('a', 'b'))  # factory with single active parameter
    factory.parameter_ranges = {
        'a': (0, 10),
        'b': (0, 10)
    }  # adjust parameter range for Bayesian analysis
    factory_list = [factory]  # likelihood requires a list/tuple of factories
    """
    # 2.3, flat prior
    """
    prior = FlatPrior()
    """
    # 2.4, simulator
    """
    simer = LiSimulator(mock_data)
    """
    # 2.5, pipeline
    """
    pipe = DynestyPipeline(simer, factory_list, likelihood, prior,
                           simulation_size)
    pipe.random_type = 'controllable'  # 'fixed' random_type doesnt work for Dynesty pipeline, yet
    pipe.seed_tracer = int(23)
    pipe.sampling_controllers = {'nlive': 400}

    tmr = Timer()
    tmr.tick('test')
    results = pipe()
    tmr.tock('test')
    if mpirank == 0:
        print('\n elapse time ' + str(tmr.record['test']) + '\n')
    """
    # step 3, visualize (with corner package)
    """
    if mpirank == 0 and make_plots:
        samples = results['samples']
        for i in range(len(
                pipe.active_parameters)):  # convert variables into parameters
            low, high = pipe.active_ranges[pipe.active_parameters[i]]
            for j in range(samples.shape[0]):
                samples[j, i] = unity_mapper(samples[j, i], low, high)
        # corner plot
        corner.corner(samples[:, :len(pipe.active_parameters)],
                      range=[0.99] * len(pipe.active_parameters),
                      quantiles=[0.02, 0.5, 0.98],
                      labels=pipe.active_parameters,
                      show_titles=True,
                      title_kwargs={"fontsize": 15},
                      color='steelblue',
                      truths=truths,
                      truth_color='firebrick',
                      plot_contours=True,
                      hist_kwargs={'linewidth': 2},
                      label_kwargs={'fontsize': 20})
        plt.savefig('testfield_posterior_li_dynesty.pdf')
예제 #13
0
def mock_errprop(_nside, _freq):
    """
    return masked mock synchrotron Q, U
    error propagated from theoretical uncertainties
    """
    # hammurabi parameter base file
    xmlpath = './params_masked_random.xml'
    # active parameters
    true_b0 = 3.0
    true_psi0 = 27.0
    true_psi1 = 0.9
    true_chi0 = 25.
    true_alpha = 3.0
    true_r0 = 5.0
    true_z0 = 1.0
    true_rms = 6.0
    true_rho = 0.8
    true_a0 = 1.7
    #
    _npix = 12 * _nside**2
    #
    x = np.zeros((1, _npix))  # only for triggering simulator
    trigger = Measurements()
    trigger.append(('sync', str(_freq), str(_nside), 'Q'), x)  # Q map
    trigger.append(('sync', str(_freq), str(_nside), 'U'), x)  # U map
    # initialize simulator
    mocksize = 20  # ensemble of mock data
    error = 0.1  # theoretical raltive uncertainty for each (active) parameter
    mocker = Hammurabi(measurements=trigger, xml_path=xmlpath)
    # prepare theoretical uncertainty
    b0_var = np.random.normal(true_b0, error * true_b0, mocksize)
    psi0_var = np.random.normal(true_psi0, error * true_psi0, mocksize)
    psi1_var = np.random.normal(true_psi1, error * true_psi1, mocksize)
    chi0_var = np.random.normal(true_chi0, error * true_chi0, mocksize)
    alpha_var = np.random.normal(true_alpha, error * true_alpha, mocksize)
    r0_var = np.random.normal(true_r0, error * true_r0, mocksize)
    z0_var = np.random.normal(true_z0, error * true_z0, mocksize)
    rms_var = np.random.normal(true_rms, error * ture_rms, mocksize)
    rho_var = np.random.normal(true_rho, error * true_rho, mocksize)
    a0_var = np.random.normal(true_a0, error * true_a0, mocksize)
    mock_raw_q = np.zeros((mocksize, _npix))
    mock_raw_u = np.zeros((mocksize, _npix))
    # start simulation
    for i in range(mocksize):  # get one realization each time
        # BregWMAP field
        paramlist = {
            'b0': b0_var[i],
            'psi0': psi0_var[i],
            'psi1': psi1_var[i],
            'chi0': chi0_var[i]
        }
        breg_wmap = BregWMAP(paramlist, 1)
        # CREAna field
        paramlist = {
            'alpha': alpha_var[i],
            'beta': 0.0,
            'theta': 0.0,
            'r0': r0_var[i],
            'z0': z0_var[i],
            'E0': 20.6,
            'j0': 0.0217
        }
        cre_ana = CREAna(paramlist, 1)
        # FEregYMW16 field
        paramlist = dict()
        fereg_ymw16 = FEregYMW16(paramlist, 1)
        # BrndES field
        paramlist = {
            'rms': rms_var[i],
            'k0': 0.1,
            'a0': a0_var[i],
            'rho': rho_var[i],
            'r0': 8.0,
            'z0': 1.0
        }
        brnd_es = BrndES(paramlist, 1)
        # collect mock data and covariance
        outputs = mocker([breg_wmap, cre_ana, fereg_ymw16, brnd_es])
        mock_raw_q[i, :] = outputs[('sync', str(_freq), str(_nside),
                                    'Q')].local_data
        mock_raw_u[i, :] = outputs[('sync', str(_freq), str(_nside),
                                    'U')].local_data
    # collect mean and cov from simulated results
    sim_data = Simulations()
    mock_data = Measurements()
    mock_cov = Covariances()

    sim_data.append(('sync', str(_freq), str(_nside), 'Q'), mock_raw_q)
    sim_data.append(('sync', str(_freq), str(_nside), 'U'), mock_raw_u)
    mock_mask = mask_map(_nside, _freq)
    sim_data.apply_mask(mock_mask)
    for key in sim_data.keys():
        mock_data.append(
            key,
            np.vstack([(sim_data[key].to_global_data())[np.random.randint(
                0, mocksize)]]), True)
        mock_cov.append(key, oas_cov(sim_data[key].to_global_data()), True)
    return mock_data, mock_cov
예제 #14
0
def mock_errfix(_nside, _freq):
    """
    return masked mock synchrotron Q, U
    error fixed
    """
    # hammurabi parameter base file
    xmlpath = './params_masked_random.xml'
    # active parameters
    true_b0 = 3.0
    true_psi0 = 27.0
    true_psi1 = 0.9
    true_chi0 = 25.
    true_alpha = 3.0
    true_r0 = 5.0
    true_z0 = 1.0
    true_rms = 6.0
    true_rho = 0.8
    true_a0 = 1.7
    #
    _npix = 12 * _nside**2
    #
    x = np.zeros((1, _npix))  # only for triggering simulator
    trigger = Measurements()
    trigger.append(('sync', str(_freq), str(_nside), 'Q'), x)  # Q map
    trigger.append(('sync', str(_freq), str(_nside), 'U'), x)  # U map
    # initialize simulator
    error = 0.1
    mocker = Hammurabi(measurements=trigger, xml_path=xmlpath)
    # start simulation
    # BregWMAP field
    paramlist = {
        'b0': true_b0,
        'psi0': true_psi0,
        'psi1': true_psi1,
        'chi0': true_chi0
    }
    breg_wmap = BregWMAP(paramlist, 1)
    # CREAna field
    paramlist = {
        'alpha': true_alpha,
        'beta': 0.0,
        'theta': 0.0,
        'r0': true_r0,
        'z0': true_z0,
        'E0': 20.6,
        'j0': 0.0217
    }
    cre_ana = CREAna(paramlist, 1)
    # FEregYMW16 field
    paramlist = dict()
    fereg_ymw16 = FEregYMW16(paramlist, 1)
    # BrndES field
    paramlist = {
        'rms': true_rms,
        'k0': 0.1,
        'a0': true_a0,
        'rho': true_rho,
        'r0': 8.0,
        'z0': 1.0
    }
    brnd_es = BrndES(paramlist, 1)
    # collect mock data and covariance
    outputs = mocker([breg_wmap, cre_ana, fereg_ymw16, brnd_es])
    mock_raw_q = outputs[('sync', str(_freq), str(_nside), 'Q')].local_data
    mock_raw_u = outputs[('sync', str(_freq), str(_nside), 'U')].local_data
    # collect mean and cov from simulated results
    mock_data = Measurements()
    mock_cov = Covariances()

    mock_data.append(('sync', str(_freq), str(_nside), 'Q'), mock_raw_q)
    mock_data.append(('sync', str(_freq), str(_nside), 'U'), mock_raw_u)
    mock_mask = mask_map(_nside, _freq)
    mock_data.apply_mask(mock_mask)
    for key in mock_data.keys():
        mock_cov.append(key, (error**2 * (np.std(mock_raw_q))**2) *
                        np.eye(int(key[2])), True)
    return mock_data, mock_cov
예제 #15
0
def wmap_errprop():
    #log.basicConfig(filename='imagine.log', level=log.DEBUG)
    """
    only WMAP regular magnetic field model in test, @ 23GHz
    Faraday rotation provided by YMW16 free electron model
    full WMAP parameter set {b0, psi0, psi1, chi0}
    """
    # hammurabi parameter base file
    xmlpath = './params_fullsky_regular.xml'

    # we take three active parameters
    true_b0 = 6.0
    true_psi0 = 27.0
    true_psi1 = 0.9
    true_chi0 = 25.
    true_alpha = 3.0
    true_r0 = 5.0
    true_z0 = 1.0

    mea_nside = 2  # observable Nside
    mea_pix = 12 * mea_nside**2  # observable pixel number
    """
    # step 1, prepare mock data
    """
    x = np.zeros((1, mea_pix))  # only for triggering simulator
    trigger = Measurements()
    trigger.append(('sync', '23', str(mea_nside), 'I'), x)  # only I map
    # initialize simulator
    mocksize = 10  # ensemble of mock data (per node)
    error = 0.1  # theoretical raltive uncertainty for each (active) parameter
    mocker = Hammurabi(measurements=trigger, xml_path=xmlpath)
    # prepare theoretical uncertainty
    b0_var = np.random.normal(true_b0, error * true_b0, mocksize)
    psi0_var = np.random.normal(true_psi0, error * true_psi0, mocksize)
    psi1_var = np.random.normal(true_psi1, error * true_psi1, mocksize)
    chi0_var = np.random.normal(true_chi0, error * true_chi0, mocksize)
    alpha_var = np.random.normal(true_alpha, error * true_alpha, mocksize)
    r0_var = np.random.normal(true_r0, error * true_r0, mocksize)
    z0_var = np.random.normal(true_z0, error * true_z0, mocksize)
    mock_ensemble = Simulations()
    # start simulation
    for i in range(mocksize):  # get one realization each time
        # BregWMAP field
        paramlist = {
            'b0': b0_var[i],
            'psi0': psi0_var[i],
            'psi1': psi1_var[i],
            'chi0': chi0_var[i]
        }  # inactive parameters at default
        breg_wmap = BregWMAP(paramlist, 1)
        # CREAna field
        paramlist = {
            'alpha': alpha_var[i],
            'beta': 0.0,
            'theta': 0.0,
            'r0': r0_var[i],
            'z0': z0_var[i],
            'E0': 20.6,
            'j0': 0.0217
        }  # inactive parameters at default
        cre_ana = CREAna(paramlist, 1)
        # FEregYMW16 field
        fereg_ymw16 = FEregYMW16(dict(), 1)
        # collect mock data and covariance
        outputs = mocker([breg_wmap, cre_ana, fereg_ymw16])
        mock_ensemble.append(('sync', '23', str(mea_nside), 'I'),
                             outputs[('sync', '23', str(mea_nside), 'I')])
    # collect mean and cov from simulated results
    mock_data = Measurements()
    mock_cov = Covariances()
    mean, cov = oas_mcov(mock_ensemble[('sync', '23', str(mea_nside), 'I')])
    mock_data.append(('sync', '23', str(mea_nside), 'I'), mean)
    mock_cov.append(('sync', '23', str(mea_nside), 'I'), cov)
    """
    # step 2, prepare pipeline and execute analysis
    """
    #likelihood = EnsembleLikelihood(mock_data, mock_cov)
    likelihood = SimpleLikelihood(mock_data, mock_cov)

    breg_factory = BregWMAPFactory(active_parameters=('b0', 'psi0', 'psi1',
                                                      'chi0'))
    breg_factory.parameter_ranges = {
        'b0': (0., 10.),
        'psi0': (0., 50.),
        'psi1': (0., 2.),
        'chi0': (0., 50.)
    }
    cre_factory = CREAnaFactory(active_parameters=('alpha', 'r0', 'z0'))
    cre_factory.parameter_ranges = {
        'alpha': (1., 5.),
        'r0': (1., 10.),
        'z0': (0.1, 5.)
    }
    fereg_factory = FEregYMW16Factory()
    factory_list = [breg_factory, cre_factory, fereg_factory]

    prior = FlatPrior()

    simer = Hammurabi(measurements=mock_data, xml_path=xmlpath)

    ensemble_size = 1
    pipe = MultinestPipeline(simer, factory_list, likelihood, prior,
                             ensemble_size)
    pipe.random_type = 'free'
    pipe.sampling_controllers = {
        'n_live_points': 4000,
        'resume': False,
        'verbose': True
    }
    results = pipe()
    """
    # step 3, visualize (with corner package)
    """
    if mpirank == 0:
        samples = results['samples']
        np.savetxt('posterior_fullsky_regular_errprop.txt', samples)
    """