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)
def test_constructor(nside, expected): if 'error' in expected: with assert_raises(expected['error']): HPSpace(nside) else: h = HPSpace(nside) for key, value in expected.items(): assert_equal(getattr(h, key), value)
def test_init(self): # initialize observable dtuple = DomainTuple.make((RGSpace(3 * mpisize), HPSpace(nside=2))) val = np.random.rand(3, 48) obs = Observable(dtuple, val) # collect local val at master val_master = None if mpirank == 0: val_master = np.zeros((3 * mpisize, 48)) comm.Gather(val, val_master, root=0) # test domain/field shape self.assertEqual(obs.domain, dtuple) if mpirank == 0: self.assertEqual(obs.shape, val_master.shape) # test function to_global_data() raw = obs.to_global_data() if mpirank == 0: for i in range(len(val_master)): self.assertListEqual(list(raw[i]), list(val_master[i])) # test function ensemble_mean mean = obs.ensemble_mean if mpirank == 0: val_mean = np.mean(val_master, axis=0) for i in range(val_mean.size): self.assertAlmostEqual(mean[0][i], val_mean[i])
def append(self, name, data, plain=False): """ :param name: :param data: distributed :param plain: :return: """ assert (len(name) == 4) if name in self._archive.keys(): # app self._archive[name].append(data) else: # new if isinstance(data, Observable): self._archive.update({name: data}) elif isinstance(data, Field): self._archive.update( {name: Observable(data.domain, data.local_data)}) elif isinstance(data, np.ndarray): # distributed data if plain: assert (data.shape[1] == int(name[2])) domain = DomainTuple.make( (RGSpace(data.shape[0] * mpisize), RGSpace(data.shape[1]))) else: assert (data.shape[1] == 12 * int(name[2]) * int(name[2])) domain = DomainTuple.make( (RGSpace(data.shape[0] * mpisize), HPSpace(nside=int(name[2])))) self._archive.update({name: Observable(domain, data)}) else: raise TypeError('unsupported data type') log.debug('observable-dict appends data %s' % str(name))
def append(self, name, data, plain=False): """ :param name: :param data: distributed :param plain: :return: """ assert (len(name) == 4) if isinstance(data, Observable): assert (data.shape[0] == 1) self._archive.update({name: data}) # rw elif isinstance(data, Field): assert (data.shape[0] == 1) self._archive.update( {name: Observable(data.domain, data.local_data)}) # rw # reading from numpy array takes information only on master node elif isinstance(data, np.ndarray): assert (data.shape[0] == 1) if plain: assert (data.shape[1] == int(name[2])) domain = DomainTuple.make( (RGSpace(int(1)), RGSpace(data.shape[1]))) else: assert (data.shape[1] == 12 * int(name[2]) * int(name[2])) domain = DomainTuple.make( (RGSpace(int(1)), HPSpace(nside=int(name[2])))) self._archive.update({name: Observable(domain, data)}) # rw else: raise TypeError('unsupported data type') log.debug('measurements-dict appends data %s' % str(name))
def test_1dinit(self): # initialize observable dtuple = DomainTuple.make((RGSpace(1), HPSpace(nside=2))) val = np.random.rand(1, 48) comm.Bcast(val, root=0) obs = Observable(dtuple, val) # matches val at master raw = obs.to_global_data() self.assertListEqual(list(raw[0]), list(val[0]))
def test_append_twice(self): dtuple = DomainTuple.make((RGSpace(1 * mpisize), HPSpace(nside=2))) val = np.random.rand(1, 48) obs = Observable(dtuple, val) # test function append with 1d array new_data = np.random.rand(1, 48) obs.append(new_data) self.assertEqual(obs.shape, (2 * mpisize, 48)) obs.append(new_data) self.assertEqual(obs.shape, (3 * mpisize, 48))
def test_append_after_replace(self): dtuple = DomainTuple.make((RGSpace(1 * mpisize), HPSpace(nside=2))) obs = Observable(dtuple) self.assertTrue(obs.rw_flag) # test function append with 1d array new_data = np.random.rand(1, 48) obs.append(new_data) self.assertEqual(obs.shape, (1 * mpisize, 48)) obs.append(new_data) self.assertEqual(obs.shape, (2 * mpisize, 48))
def test_append_with_replace(self): dtuple = DomainTuple.make((RGSpace(1 * mpisize), HPSpace(nside=2))) obs = Observable(dtuple) self.assertTrue(obs.rw_flag) # test with empty observable dtuple = DomainTuple.make((RGSpace(8 * mpisize), HPSpace(nside=2))) new_data = np.random.rand(8, 48) new_obs = Observable(dtuple, new_data) obs.append(new_obs) raw_obs = obs.to_global_data() # collect new val at master new_val_master = None if mpirank == 0: new_val_master = np.zeros((8 * mpisize, 48)) comm.Gather(new_data, new_val_master, root=0) # do comparison at master if mpirank == 0: for i in range(new_val_master.shape[0]): self.assertListEqual(list(raw_obs[i]), list(new_val_master[i]))
def test_append_observable(self): dtuple = DomainTuple.make((RGSpace(3 * mpisize), HPSpace(nside=2))) val = np.random.rand(3, 48) obs = Observable(dtuple, val) # test function append with Observable dtuple = DomainTuple.make((RGSpace(5 * mpisize), HPSpace(nside=2))) new_data = np.random.rand(5, 48) new_obs = Observable(dtuple, new_data) obs.append(new_obs) raw_obs = obs.to_global_data() new_val = np.vstack([val, new_data]) # collect new val at master new_val_master = None if mpirank == 0: new_val_master = np.zeros((8 * mpisize, 48)) comm.Gather(new_val, new_val_master, root=0) # do comparison at master if mpirank == 0: for i in range(new_val_master.shape[0]): self.assertListEqual(list(raw_obs[i]), list(new_val_master[i]))
def test_without_cov(self): simdict = Simulations() meadict = Measurements() # 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 sims dtuple = DomainTuple.make((RGSpace(3*mpisize), HPSpace(nside=2))) arr_b = np.random.rand(3, 48) sim = Observable(dtuple, arr_b) simdict.append(('test', 'nan', '2', 'nan'), sim) # no covariance lh = SimpleLikelihood(meadict) # calc by likelihood rslt = lh(simdict) # feed variable value, not parameter value # calc by hand arr_b = sim.to_global_data() # global arr_b diff = (np.mean(arr_b, axis=0) - arr_a) baseline = -float(0.5)*float(np.vdot(diff, diff)) # comapre self.assertAlmostEqual(rslt, baseline)
def test_measuredict_append_observable(self): dtuple = DomainTuple.make((RGSpace(1), HPSpace(nside=2))) hrr = np.random.rand(1, 48) obs1 = Observable(dtuple, hrr) measuredict = Measurements() measuredict.append(('test', 'nan', '2', 'nan'), obs1) # healpix Observable if mpirank == 0: self.assertListEqual( list(measuredict[('test', 'nan', '2', 'nan')].to_global_data()[0]), list(hrr[0])) dtuple = DomainTuple.make((RGSpace(1), RGSpace(3))) arr = np.random.rand(1, 3) obs2 = Observable(dtuple, arr) measuredict.append(('test', 'nan', '3', 'nan'), obs2) # plain Observable if mpirank == 0: self.assertListEqual( list(measuredict[('test', 'nan', '3', 'nan')].to_global_data()[0]), list(arr[0]))
def append(self, name, data, plain=False): """ :param name: :param data: distributed :param plain: :return: """ assert (len(name) == 4) if isinstance(data, Observable): assert (data.shape[0] == mpisize) raw_data = data.local_data for i in raw_data[0]: assert (i == float(0) or i == float(1)) self._archive.update({name: data}) elif isinstance(data, Field): assert (data.shape[0] == mpisize) raw_data = data.local_data for i in raw_data[0]: assert (i == float(0) or i == float(1)) self._archive.update( {name: Observable(data.domain, data.local_data)}) elif isinstance(data, np.ndarray): assert (data.shape[0] == 1) for i in data[0]: assert (i == float(0) or i == float(1)) if plain: assert (data.shape[1] == int(name[2])) domain = DomainTuple.make( (RGSpace(mpisize), RGSpace(data.shape[1]))) else: assert (data.shape[1] == 12 * int(name[2]) * int(name[2])) domain = DomainTuple.make( (RGSpace(mpisize), HPSpace(nside=int(name[2])))) self._archive.update({name: Observable(domain, data)}) else: raise TypeError('unsupported data type') log.debug('mask-dict appends data %s' % str(name))
def test_simdict_append_observable(self): dtuple = DomainTuple.make((RGSpace(2 * mpisize), HPSpace(nside=2))) hrr = np.random.rand(2, 48) obs1 = Observable(dtuple, hrr) simdict = Simulations() simdict.append(('test', 'nan', '2', 'nan'), obs1) # healpix Observable self.assertEqual(simdict[('test', 'nan', '2', 'nan')].shape, (2 * mpisize, 48)) for i in range(len(hrr)): self.assertListEqual( list((simdict[('test', 'nan', '2', 'nan')].local_data)[i]), list(hrr[i])) dtuple = DomainTuple.make((RGSpace(5 * mpisize), RGSpace(3))) arr = np.random.rand(5, 3) obs2 = Observable(dtuple, arr) simdict.append(('test', 'nan', '3', 'nan'), obs2, True) # plain Observable self.assertEqual(simdict[('test', 'nan', '3', 'nan')].shape, (5 * mpisize, 3)) for i in range(len(arr)): self.assertListEqual( list((simdict[('test', 'nan', '3', 'nan')].local_data)[i]), list(arr[i]))
def test_dvol(): assert_almost_equal(HPSpace(2).dvol, np.pi / 12)
def test_property_ret_type(): x = HPSpace(2) assert_(isinstance(getattr(x, 'nside'), int))