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_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_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_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_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_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
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_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_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()
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])
def tutorial_one(): # Package imports from imagine.fields import ( CosThermalElectronDensityFactory, NaiveGaussianMagneticFieldFactory, UniformGrid) from imagine.likelihoods import EnsembleLikelihood from imagine.observables import Covariances, Measurements, TabularDataset from imagine.pipelines import UltranestPipeline from imagine.priors import FlatPrior from imagine.simulators import TestSimulator # IMAGINE-PRISM imports from imagine_prism import PRISMPipeline # Obtain data data = get_mock_data() # Create dataset mock_dataset = TabularDataset(data, name='test', data_col='meas', err_col='err') # Create measurements and covariances objects mock_data = Measurements(mock_dataset) mock_cov = Covariances(mock_dataset) # Create grid grid = UniformGrid(box=[[0, 2*np.pi]*apu.kpc, [0, 0]*apu.kpc, [0, 0]*apu.kpc], resolution=[30, 1, 1]) # Create factory for electron density field ne_factory = CosThermalElectronDensityFactory(grid=grid) ne_factory.default_parameters = {'a': 1*apu.rad/apu.kpc, 'beta': np.pi/2*apu.rad, 'gamma': np.pi/2*apu.rad} # Create factory for magnetic field B_factory = NaiveGaussianMagneticFieldFactory(grid=grid) B_factory.active_parameters = ('a0', 'b0') B_factory.priors = {'a0': FlatPrior(*[-5, 5]*apu.microgauss), 'b0': FlatPrior(*[0, 10]*apu.microgauss)} # Combine factories together factories = [ne_factory, B_factory] # Create simulator simulator = TestSimulator(mock_data) # Create likelihood likelihood = EnsembleLikelihood(mock_data, mock_cov) # Create pipeline img_pipe = UltranestPipeline(simulator=simulator, factory_list=factories, likelihood=likelihood, ensemble_size=150) # Create PRISMPipeline object pipe = PRISMPipeline(img_pipe, root_dir='tests', working_dir='imagine_1') # Return pipe return(pipe)