예제 #1
0
def main():
    #log.basicConfig(filename='imagine.log', level=log.DEBUG)

    nside = 2
    freq = 23

    mock_data, mock_cov = mock_errfix(nside, freq)
    mask_map = mask_map_prod(nside, 0, 90, 50)  # not parameterizing this
    mock_mask = Masks()
    mock_mask.append(('sync', str(freq), str(nside), 'Q'),
                     np.vstack([mask_map]))
    mock_mask.append(('sync', str(freq), str(nside), 'U'),
                     np.vstack([mask_map]))

    # using masked mock data/covariance
    # apply_mock will ignore masked input since mismatch in keys
    likelihood = EnsembleLikelihood(mock_data, mock_cov, mock_mask)

    breg_factory = BregLSAFactory(
        active_parameters=('b0', ))  # set active parameters
    breg_factory.parameter_ranges = {'b0': (0., 10.)}

    cre_factory = CREAnaFactory(
        active_parameters=('alpha', ))  # set active parameters
    cre_factory.parameter_ranges = {'alpha': (1., 5.)}

    fereg_factory = TEregYMW16Factory()
    factory_list = [breg_factory, cre_factory, fereg_factory]

    prior = FlatPrior()

    xmlpath = './params.xml'
    # only for triggering simulator
    # since we use masked mock_data/covariance
    # if use masked input, outputs from simulator will not be masked due to mismatch in keys
    x = np.zeros((1, 12 * nside**2))
    trigger = Measurements()
    trigger.append(('sync', str(freq), str(nside), 'Q'), x)
    trigger.append(('sync', str(freq), str(nside), 'U'), x)
    simer = Hammurabi(measurements=trigger, xml_path=xmlpath)

    ensemble_size = 5
    pipe = DynestyPipeline(simer, factory_list, likelihood, prior,
                           ensemble_size)
    pipe.random_type = 'free'
    pipe.sampling_controllers = {'nlive': 400}

    tmr = Timer()
    tmr.tick('test')
    results = pipe()
    tmr.tock('test')
    if not mpirank:
        print('\n elapse time ' + str(tmr.record['test']) + '\n')

    # saving results
    if mpirank == 0:
        samples = results['samples']
        np.savetxt('posterior_masked_regular.txt', samples)
예제 #2
0
def mock_errprop(_nside, _freq):
    """
    return masked mock synchrotron Q, U
    error propagated from theoretical uncertainties
    """
    # hammurabi parameter base file
    xmlpath = './params.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*true_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
        # BregLSA field
        paramlist = {'b0': b0_var[i], 'psi0': psi0_var[i], 'psi1': psi1_var[i], 'chi0': chi0_var[i]}
        breg_lsa = BregLSA(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)
        # TEregYMW16 field
        paramlist = dict()
        fereg_ymw16 = TEregYMW16(paramlist, 1)
        # BrndES field
        paramlist = {'rms': rms_var[i], 'k0': 10.0, 'k1': 0.1, 'a1': 0.0, '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_lsa, cre_ana, fereg_ymw16, brnd_es])
        mock_raw_q[i, :] = outputs[('sync', str(_freq), str(_nside), 'Q')].data
        mock_raw_u[i, :] = outputs[('sync', str(_freq), str(_nside), 'U')].data
    # collect mean and cov from simulated results
    sim_data = Simulations()
    mock_data = Measurements()
    mock_cov = Covariances()
    mock_mask = Masks()
    
    sim_data.append(('sync', str(_freq), str(_nside), 'Q'), mock_raw_q)
    sim_data.append(('sync', str(_freq), str(_nside), 'U'), mock_raw_u)
    
    mask_map = mask_map_prod(_nside, 0, 90, 50)  # not parameterizing this
    mock_mask.append(('sync', str(_freq), str(_nside), 'Q'), np.vstack([mask_map]))
    mock_mask.append(('sync', str(_freq), str(_nside), 'U'), np.vstack([mask_map]))
    sim_data.apply_mask(mock_mask)
    for key in sim_data.keys():
        global_mock = np.vstack([(sim_data[key].data)[0]])
        comm.Bcast(global_mock, root=0)
        mock_data.append(key, global_mock, True)
        mock_cov.append(key, oas_cov(sim_data[key].data), True)
    return mock_data, mock_cov
예제 #3
0
def mock_errfix(_nside, _freq):
    """
    return masked mock synchrotron Q, U
    error fixed
    """
    # hammurabi parameter base file
    xmlpath = './params.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
    # BregLSA field
    paramlist = {'b0': true_b0, 'psi0': true_psi0, 'psi1': true_psi1, 'chi0': true_chi0}
    breg_lsa = BregLSA(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)
    # TEregYMW16 field
    paramlist = dict()
    fereg_ymw16 = TEregYMW16(paramlist, 1)
    # BrndES field
    paramlist = {'rms': true_rms, 'k0': 0.1, 'k1': 0.1, 'a1': 0.0, '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_lsa, cre_ana, fereg_ymw16, brnd_es])
    mock_raw_q = outputs[('sync', str(_freq), str(_nside), 'Q')].data
    mock_raw_u = outputs[('sync', str(_freq), str(_nside), 'U')].data
    # collect mean and cov from simulated results
    mock_data = Measurements()
    mock_cov = Covariances()
    mock_mask = Masks()
    
    mock_data.append(('sync', str(_freq), str(_nside), 'Q'), mock_raw_q)
    mock_data.append(('sync', str(_freq), str(_nside), 'U'), mock_raw_u)
    
    mask_map = mask_map_prod(_nside, 0, 90, 50)  # not parameterizing this
    mock_mask.append(('sync', str(_freq), str(_nside), 'Q'), np.vstack([mask_map]))
    mock_mask.append(('sync', str(_freq), str(_nside), 'U'), np.vstack([mask_map]))
    mock_data.apply_mask(mock_mask)
    for key in mock_data.keys():
        mock_cov.append(key, (error**2*(np.std(mock_raw_q))**2)*mpi_eye(int(key[2])), True)
    return mock_data, mock_cov
예제 #4
0
def lsa_errprop():
    #log.basicConfig(filename='imagine.log', level=log.DEBUG)
    """
    only LSA regular magnetic field model in test, @ 23GHz
    Faraday rotation provided by YMW16 thermal electron model
    full LSA parameter set {b0, psi0, psi1, chi0}
    """
    # hammurabi parameter base file
    xmlpath = './params.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
        # BregLSA field
        paramlist = {
            'b0': b0_var[i],
            'psi0': psi0_var[i],
            'psi1': psi1_var[i],
            'chi0': chi0_var[i]
        }  # inactive parameters at default
        breg_lsa = BregLSA(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)
        # TEregYMW16 field
        tereg_ymw16 = TEregYMW16(dict(), 1)
        # collect mock data and covariance
        outputs = mocker([breg_lsa, cre_ana, tereg_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')].data)
    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)

    breg_factory = BregLSAFactory(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.)
    }
    tereg_factory = TEregYMW16Factory()
    factory_list = [breg_factory, cre_factory, tereg_factory]

    prior = FlatPrior()

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

    ensemble_size = 10
    pipe = DynestyPipeline(simer, factory_list, likelihood, prior,
                           ensemble_size)
    pipe.random_type = 'free'
    pipe.sampling_controllers = {'nlive': 4000}

    tmr = Timer()
    tmr.tick('test')
    results = pipe()
    tmr.tock('test')
    if not mpirank:
        print('\n elapse time ' + str(tmr.record['test']) + '\n')
    """
    # step 3, visualize (with corner package)
    """
    if mpirank == 0:
        samples = results['samples']
        np.savetxt('posterior_fullsky_regular_errprop.txt', samples)
    """
예제 #5
0
def lsa_errfix():
    #log.basicConfig(filename='imagine.log', level=log.DEBUG)
    """
    only LSA regular magnetic field model in test, @ 23GHz
    Faraday rotation provided by YMW16 thermal electron model
    full LSA parameter set {b0, psi0, psi1, chi0}
    """
    # hammurabi parameter base file
    xmlpath = './params.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
    error = 0.1  # theoretical raltive uncertainty for each (active) parameter
    mocker = Hammurabi(measurements=trigger, xml_path=xmlpath)
    # start simulation
    # BregLSA field
    paramlist = {
        'b0': true_b0,
        'psi0': true_psi0,
        'psi1': true_psi1,
        'chi0': true_chi0
    }  # inactive parameters at default
    breg_lsa = BregLSA(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
    }  # inactive parameters at default
    cre_ana = CREAna(paramlist, 1)
    # TEregYMW16 field
    tereg_ymw16 = TEregYMW16(dict(), 1)
    # collect mock data and covariance
    outputs = mocker([breg_lsa, cre_ana, tereg_ymw16])
    Imap = outputs[('sync', '23', str(mea_nside), 'I')].local_data
    # collect mean and cov from simulated results
    mock_data = Measurements()
    mock_cov = Covariances()
    mock_data.append(('sync', '23', str(mea_nside), 'I'), Imap)
    mock_cov.append(('sync', '23', str(mea_nside), 'I'),
                    (error**2 * (mpi_mean(Imap))**2) * mpi_eye(mea_pix))
    """
    # step 2, prepare pipeline and execute analysis
    """
    likelihood = EnsembleLikelihood(mock_data, mock_cov)

    breg_factory = BregLSAFactory(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.)
    }
    tereg_factory = TEregYMW16Factory()
    factory_list = [breg_factory, cre_factory, tereg_factory]

    prior = FlatPrior()

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

    ensemble_size = 10
    pipe = DynestyPipeline(simer, factory_list, likelihood, prior,
                           ensemble_size)
    pipe.random_type = 'free'
    pipe.sampling_controllers = {'nlive': 4000}
    results = pipe()
    """
    # step 3, visualize (with corner package)
    """
    if mpirank == 0:
        samples = results['samples']
        np.savetxt('posterior_fullsky_regular_errfix.txt', samples)
    """