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
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)
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)
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
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
def test_covdict_append_observable(self): cov = Observable(np.random.rand(2, 2 * mpisize), 'covariance') covdict = Covariances() covdict.append(name=('test', None, 2 * mpisize, None), cov_data=cov) # plain covariance assert np.allclose(covdict[('test', None, 2 * mpisize, None)].data, cov.data) cov = Observable(np.random.rand(12 * mpisize, 12 * mpisize * mpisize), 'covariance') covdict.append(name=('test', None, mpisize, None), cov_data=cov) # healpix covariance assert np.allclose(covdict[('test', None, mpisize, None)].data, cov.data)
def test_append_with_rewrite(self): if not mpirank: arr = np.random.rand(2, 128) else: arr = np.random.rand(1, 128) test_obs = Observable(arr, 'simulated') test_obs.rw_flag = True brr = np.random.rand(1, 128) test_obs.append(brr) global_shape = test_obs.shape globalrr = test_obs.global_data if not mpirank: assert global_shape == globalrr.shape assert np.allclose(test_obs.data[0], brr[0])
def test_append_ndarray(self): if not mpirank: arr = np.random.rand(2, 128) else: arr = np.random.rand(1, 128) test_obs = Observable(arr, 'simulated') brr = np.random.rand(1, 128) test_obs.append(brr) global_shape = test_obs.shape globalrr = test_obs.global_data if not mpirank: assert global_shape == globalrr.shape fullrr = np.vstack([arr, brr]) assert np.allclose(fullrr, test_obs.data)
def test_simdict_append_observable(self): hrr = np.random.rand(2, 48) obs1 = Observable(hrr, 'simulated') simdict = Simulations() simdict.append(name=('test', None, 2, None), data=obs1, otype='HEALPix') # healpix Observable assert simdict[('test', None, 2, None)].shape == (2 * mpisize, 48) assert np.allclose(simdict[('test', None, 2, None)].data, hrr) arr = np.random.rand(5, 3) obs2 = Observable(arr, 'simulated') simdict.append(name=('test', None, 3, None), data=obs2, otype='plain') # plain Observable assert simdict[('test', None, 3, None)].shape == (5 * mpisize, 3) assert np.allclose(simdict[('test', None, 3, None)].data, arr)
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])
def test_init_covariance(self): arr = np.random.rand(1, mpisize) test_obs = Observable(arr, 'covariance') assert test_obs.dtype == 'covariance' assert test_obs.shape == (mpisize, mpisize) assert np.allclose(arr[0], test_obs.data[0]) assert test_obs.size == mpisize
def test_init_measure(self): arr = np.random.rand(1, 128) test_obs = Observable(arr, 'measured') assert test_obs.dtype == 'measured' assert test_obs.shape == (mpisize, 128) assert np.allclose(arr[0], test_obs.data[0]) assert np.allclose(arr[0], test_obs.ensemble_mean[0])
def test_append_obs(self): if not mpirank: arr = np.random.rand(2, 128) else: arr = np.random.rand(1, 128) test_obs = Observable(arr, 'simulated') if not mpirank: brr = np.random.rand(1, 128) else: brr = np.random.rand(1, 128) test_obs2 = Observable(brr, 'simulated') test_obs2.append(test_obs) global_shape = test_obs2.shape globalrr = test_obs2.global_data if not mpirank: assert global_shape == globalrr.shape fullrr = np.vstack([arr, brr]) assert np.alltrue(np.isin(test_obs2.data, fullrr))
def _initialize_observable_dict_helper(self, observable_dict, magnetic_field, observable): ensemble_space = magnetic_field.domain[0] for component in observable.component_names: # It is important to initialize the Observables with an explicit # value. Otherwise the d2o will not instantaneuosly be created # (c.f. lazy object creation). observable_dict[component] = Observable( val=0, domain=(ensemble_space, self._hpSpace), distribution_strategy='equal') return observable_dict
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)
def test_append_after_rewrite(self): arr = np.random.rand(1, 128) test_obs = Observable(arr, 'simulated') if not mpirank: brr = np.random.rand(2, 128) else: brr = np.random.rand(1, 128) test_obs.rw_flag = True test_obs.append(brr) crr = np.random.rand(1, 128) # rw_flag must have be switched off test_obs.append(crr) global_shape = test_obs.shape globalrr = test_obs.global_data if not mpirank: assert global_shape == globalrr.shape fullrr = np.vstack([brr, crr]) assert np.alltrue(np.isin(test_obs.data, fullrr))