Exemplo n.º 1
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(name=('test', None, 4*mpisize, None),
                    data=mea, otype='plain')
     # mock covariance
     arr_c = np.random.rand(4, 4*mpisize)
     cov = Observable(arr_c, 'covariance')
     covdict.append(name=('test', None, 4*mpisize, None),
                    cov_data=cov)
     # 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(name=('test', None, 4*mpisize, None),
                    data=sim, otype='plain')
     # simplelikelihood
     lh_simple = SimpleLikelihood(meadict, covdict)
     rslt_simple = lh_simple(simdict)
     # ensemblelikelihood
     lh_ensemble = EnsembleLikelihood(meadict, covdict)
     rslt_ensemble = lh_ensemble(simdict)
     assert rslt_ensemble == rslt_simple
Exemplo n.º 2
0
    def test_diag(self):
        simdict = Simulations()
        meadict = Measurements()
        covdict = Covariances()
        # mock measurements
        arr_a = np.random.rand(1, 40)
        mea = Observable(arr_a, 'measured')
        meadict.append(name=('test', None, 40, None),
                       data=mea, otype='plain')
        # mock (diagonal) covariance
        arr_var = np.random.rand(40)
        cov = Observable(np.diag(arr_var), 'covariance')
        covdict.append(name=('test', None, 40, None),
                       cov_data=cov)
        # mock observable
        arr_ens = np.random.rand(10, 40)

        sim = Observable(arr_ens, 'simulated')
        simdict.append(name=('test', None, 40, None),
                       data=sim, otype='plain')
        # ensemblelikelihood + diagonal_covcov
        lh_ens = EnsembleLikelihood(meadict, covdict, cov_func=diagonal_mcov)
        result_ens = lh_ens(simdict)
        # EnsembleLikelihoodDiagonal
        lh_diag = EnsembleLikelihoodDiagonal(meadict, covdict)
        result_diag = lh_diag(simdict)

        assert np.allclose(result_diag, result_ens)
Exemplo n.º 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(name=('test', None, 4*mpisize, None),
                    data=mea, otype='plain')
     # mock sims
     arr_b = np.random.rand(5, 4*mpisize)
     sim = Observable(arr_b, 'simulated')
     simdict.append(name=('test', None, 4*mpisize, None),
                    data=sim, otype='plain')
     # mock covariance
     arr_c = np.random.rand(4, 4*mpisize)
     cov = Observable(arr_c, 'covariance')
     covdict.append(name=('test', None, 4*mpisize, None),
                    cov_data=cov)
     # 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 = -0.5*(np.vdot(diff, np.linalg.solve(full_cov, diff.T))+sign*logdet)
     assert np.allclose(rslt, baseline)
Exemplo n.º 4
0
 def test_without_cov(self):
     simdict = Simulations()
     meadict = Measurements()
     # mock measurements
     arr_a = np.random.rand(1, 12*mpisize**2)
     comm.Bcast(arr_a, root=0)
     mea = Observable(arr_a, 'measured')
     meadict.append(name=('test', None, mpisize, None),
                    data=mea, otype='HEALPix')
     # mock observable with repeated single realisation
     arr_b = np.random.rand(1, 12*mpisize**2)
     comm.Bcast(arr_b, root=0)
     if not mpirank:
         arr_ens = np.zeros((3, 12*mpisize**2))
     else:
         arr_ens = np.zeros((2, 12*mpisize**2))
     for i in range(len(arr_ens)):
         arr_ens[i] = arr_b
     sim = Observable(arr_ens, 'simulated')
     simdict.append(name=('test', None, mpisize, None),
                    data=sim, otype='HEALPix')
     # simplelikelihood
     lh_simple = SimpleLikelihood(meadict)
     rslt_simple = lh_simple(simdict)
     # ensemblelikelihood
     lh_ensemble = EnsembleLikelihood(meadict)
     rslt_ensemble = lh_ensemble(simdict)
     assert rslt_ensemble == rslt_simple
Exemplo n.º 5
0
    def test_with_trace_approximation(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(name=('test', None, 4*mpisize, None),
                       data=mea, otype='plain')
        # mock covariance (NB for the trace approximation to work, the data
        # covariance needs to be diagonal)
        arr_c = np.diag(np.random.rand(4))

        cov = Observable(arr_c, 'covariance')
        covdict.append(name=('test', None, 4*mpisize, None),
                       cov_data=cov)
        # 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(name=('test', None, 4*mpisize, None),
                       data=sim, otype='plain')

        # simplelikelihood
        lh_simple = SimpleLikelihood(meadict, covdict)
        result_simple = lh_simple(simdict)
        # ensemblelikelihood
        lh_ensemble = EnsembleLikelihood(meadict, covdict,
                                         use_trace_approximation=True)
        result_ensemble = lh_ensemble(simdict)
        assert result_ensemble == result_simple
Exemplo n.º 6
0
 def test_measuredict_append_observable(self):
     hrr = np.random.rand(1, 48)
     obs1 = Observable(hrr, 'measured')
     measuredict = Measurements()
     measuredict.append(name=('test', None, 2, None),
                        data=obs1)  # healpix Observable
     assert np.allclose(measuredict[('test', None, 2, None)].data[0],
                        hrr[0])
     arr = np.random.rand(1, 3)
     obs2 = Observable(arr, 'measured')
     measuredict.append(name=('test', None, 3, None),
                        data=obs2,
                        otype='plain')  # plain Observable
     assert np.allclose(measuredict[('test', None, 3, None)].data[0],
                        arr[0])
Exemplo n.º 7
0
 def test_measuredict_append_array(self):
     arr = np.random.rand(1, 3)
     measuredict = Measurements()
     measuredict.append(name=('test', None, 3, None),
                        data=arr,
                        otype='plain')  # plain array
     local_arr = measuredict[('test', None, 3, None)].data
     if mpirank == 0:
         assert np.allclose(local_arr[0], arr[0])
     hrr = np.random.rand(1, 48)
     measuredict.append(name=('test', None, 2, None),
                        data=hrr,
                        otype='HEALPix')  # healpix array
     local_arr = measuredict[('test', None, 2, None)].data
     if mpirank == 0:
         assert np.allclose(local_arr[0], hrr[0])
Exemplo n.º 8
0
 def test_meadict_apply_mask(self):
     msk = np.array([0, 1, 0, 1, 1]).reshape(1, 5)
     mskdict = Masks()
     comm.Bcast(msk, root=0)
     mskdict.append(name=('test', None, 5, None), data=msk)
     arr = np.array([0., 1., 2., 3., 4.]).reshape(1, 5)
     meadict = Measurements()
     meadict.append(name=('test', None, 5, None), data=arr, otype='plain')
     meadict = mskdict(meadict)
     assert np.allclose(meadict[('test', None, 3, None)].data[0], [1, 3, 4])
     # HEALPix map
     msk = np.random.randint(0, 2, 48).reshape(1, 48)
     comm.Bcast(msk, root=0)
     mskdict.append(name=('test', None, 2, None), data=msk)
     arr = np.random.rand(1, 48)
     meadict.append(name=('test', None, 2, None), data=arr, otype='HEALPix')
     pix_num = msk.sum()
     meadict = mskdict(meadict)
     assert ('test', None, pix_num, None) in meadict.keys()
Exemplo n.º 9
0
def test_Simulator_dependency_resolution():
    dat = TabularDataset({
        'data': [0],
        'lat': 0,
        'lon': 0,
        'err': 0.1
    },
                         name='nothing',
                         units=u.rad,
                         data_col='data',
                         err_col='err')
    mea = Measurements()
    mea.append(dat)

    sim = DummySimulator(mea)

    fields_list = list(map(lambda x: x(grid), (F, E, D, C, B, A)))
    obs = sim(fields_list)

    assert obs[('nothing', None, 'tab', None)].global_data[0][0] == 33.3
Exemplo n.º 10
0
 def test_without_cov(self):
     simdict = Simulations()
     meadict = Measurements()
     # mock measurements
     arr_a = np.random.rand(1, 48)
     comm.Bcast(arr_a, root=0)
     mea = Observable(arr_a, 'measured')
     meadict.append(name=('test', None, 2, None), data=mea, otype='HEALPix')
     # mock sims
     arr_b = np.random.rand(3, 48)
     sim = Observable(arr_b, 'simulated')
     simdict.append(name=('test', None, 2, None), data=sim, otype='HEALPix')
     # no covariance
     lh = SimpleLikelihood(meadict)
     # 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)
     baseline = -float(0.5)*float(np.vdot(diff, diff))
     # comapre
     assert np.allclose(rslt, baseline)