def test_pairwise(): # Test the pairwise option. ngal = 1000 s = 10. rng = np.random.RandomState(8675309) x1 = rng.normal(0, s, (ngal, )) y1 = rng.normal(0, s, (ngal, )) w1 = rng.random_sample(ngal) x2 = rng.normal(0, s, (ngal, )) y2 = rng.normal(0, s, (ngal, )) w2 = rng.random_sample(ngal) k2 = rng.normal(0, 3, (ngal, )) w1 = np.ones_like(w1) w2 = np.ones_like(w2) cat1 = treecorr.Catalog(x=x1, y=y1, w=w1) cat2 = treecorr.Catalog(x=x2, y=y2, w=w2, k=k2) min_sep = 5. max_sep = 50. nbins = 10 bin_size = np.log(max_sep / min_sep) / nbins nk = treecorr.NKCorrelation(min_sep=min_sep, max_sep=max_sep, nbins=nbins) with assert_warns(FutureWarning): nk.process_pairwise(cat1, cat2) nk.finalize(cat2.vark) true_npairs = np.zeros(nbins, dtype=int) true_weight = np.zeros(nbins, dtype=float) true_xi = np.zeros(nbins, dtype=float) rsq = (x1 - x2)**2 + (y1 - y2)**2 r = np.sqrt(rsq) ww = w1 * w2 xi = ww * k2 index = np.floor(np.log(r / min_sep) / bin_size).astype(int) mask = (index >= 0) & (index < nbins) np.add.at(true_npairs, index[mask], 1) np.add.at(true_weight, index[mask], ww[mask]) np.add.at(true_xi, index[mask], xi[mask]) true_xi /= true_weight np.testing.assert_array_equal(nk.npairs, true_npairs) np.testing.assert_allclose(nk.weight, true_weight, rtol=1.e-5, atol=1.e-8) np.testing.assert_allclose(nk.xi, true_xi, rtol=1.e-4, atol=1.e-8) # If cats have names, then the logger will mention them. # Also, test running with optional args. cat1.name = "first" cat2.name = "second" with CaptureLog() as cl: nk.logger = cl.logger with assert_warns(FutureWarning): nk.process_pairwise(cat1, cat2, metric='Euclidean', num_threads=2) assert "for cats first, second" in cl.output
def test_check(): """Test checking the validity of config values. """ # First a simple case with no conflicts config1 = treecorr.read_config('configs/kg.yaml') valid_params = treecorr.corr2_valid_params config2 = treecorr.config.check_config(config1.copy(), valid_params) # Just check a few values assert config2['x_col'] == '1' assert config2['k_col'] == ['3', '0'] assert config2['verbose'] == 1 assert config2['kg_file_name'] == 'output/kg.out' config3 = treecorr.config.check_config({'g1_ext': [3, 'g1']}, valid_params) assert config3['g1_ext'] == ['3', 'g1'] # Will also have other parameters filled from the valid_params dict for key in config2: assert key in valid_params if key in config1: if isinstance(config1[key], list): assert [str(v) for v in config2[key] ] == [str(v) for v in config1[key]] else: assert config2[key] == config1[key] or str( config2[key]) == str(config1[key]) else: assert config2[key] == valid_params[key][2] # Check list of bool config1['flip_g1'] = [True, 0] config2 = treecorr.config.check_config(config1.copy(), valid_params) assert config2['flip_g1'] == [True, False] # Longer names are allowed config1['x_units'] = 'arcminutes' config1['y_units'] = 'arcminute' config2 = treecorr.config.check_config(config1.copy(), valid_params) assert config2['x_units'] == 'arcmin' assert config2['y_units'] == 'arcmin' # Also other aliases, but you need to list them explicitly. config1['reverse_g1'] = True with assert_raises(TypeError): treecorr.config.check_config(config1.copy(), valid_params) with assert_warns(FutureWarning): config2 = treecorr.config.check_config( config1.copy(), valid_params, aliases={'reverse_g1': 'flip_g1'}) assert config2['flip_g1'] is True assert 'reverse_g1' not in config2 del config1['reverse_g1'] # Invalid values raise errors config1['verbose'] = -1 with assert_raises(ValueError): treecorr.config.check_config(config1.copy(), valid_params) config1['verbose'] = 1 config1['metric'] = 'hyperbolic' with assert_raises(ValueError): treecorr.config.check_config(config1.copy(), valid_params) del config1['metric'] # With a logger, aliases write the warning to the logger. config1['n2_file_name'] = 'output/n2.out' with CaptureLog() as cl: config2 = treecorr.config.check_config( config1.copy(), valid_params, logger=cl.logger, aliases={'n2_file_name': 'nn_file_name'}) assert "The parameter n2_file_name is deprecated." in cl.output assert "You should use nn_file_name instead." in cl.output # corr2 has a list of standard aliases # It is currently empty, but let's mock it up to test the functionality. if sys.version_info < (3, ): return # mock only available on python 3 from unittest import mock with mock.patch('treecorr.corr2_aliases', {'n2_file_name': 'nn_file_name'}): with assert_warns(FutureWarning): config2 = treecorr.config.check_config( config1.copy(), valid_params, aliases=treecorr.corr2_aliases) assert 'n2_file_name' not in config2 assert config2['nn_file_name'] == 'output/n2.out' del config1['n2_file_name']
def test_pairwise2(): # Test the same profile, but with the pairwise calcualtion: nsource = 100000 gamma0 = 0.05 kappa = 0.23 r0 = 10. L = 5. * r0 rng = np.random.RandomState(8675309) x = (rng.random_sample(nsource) - 0.5) * L y = (rng.random_sample(nsource) - 0.5) * L r2 = (x**2 + y**2) gammat = gamma0 * np.exp(-0.5 * r2 / r0**2) g1 = -gammat * (x**2 - y**2) / r2 g2 = -gammat * (2. * x * y) / r2 dx = (rng.random_sample(nsource) - 0.5) * L dx = (rng.random_sample(nsource) - 0.5) * L k = kappa * np.ones(nsource) lens_cat = treecorr.Catalog(x=dx, y=dx, k=k, x_units='arcmin', y_units='arcmin') source_cat = treecorr.Catalog(x=x + dx, y=y + dx, g1=g1, g2=g2, x_units='arcmin', y_units='arcmin') kg = treecorr.KGCorrelation(bin_size=0.1, min_sep=1., max_sep=20., sep_units='arcmin', verbose=1, pairwise=True) with assert_warns(FutureWarning): kg.process(lens_cat, source_cat) r = kg.meanr true_kgt = kappa * gamma0 * np.exp(-0.5 * r**2 / r0**2) print('kg.xi = ', kg.xi) print('kg.xi_im = ', kg.xi_im) print('true_gammat = ', true_kgt) print('ratio = ', kg.xi / true_kgt) print('diff = ', kg.xi - true_kgt) print('max diff = ', max(abs(kg.xi - true_kgt))) np.testing.assert_allclose(kg.xi, true_kgt, rtol=1.e-2) np.testing.assert_allclose(kg.xi_im, 0, atol=1.e-4) # Check that we get the same result using the corr2 function lens_cat.write(os.path.join('data', 'kg_pairwise_lens.dat')) source_cat.write(os.path.join('data', 'kg_pairwise_source.dat')) config = treecorr.read_config('configs/kg_pairwise.yaml') config['verbose'] = 0 with assert_warns(FutureWarning): treecorr.corr2(config) corr2_output = np.genfromtxt(os.path.join('output', 'kg_pairwise.out'), names=True, skip_header=1) print('kg.xi = ', kg.xi) print('from corr2 output = ', corr2_output['kgamT']) print('ratio = ', corr2_output['kgamT'] / kg.xi) print('diff = ', corr2_output['kgamT'] - kg.xi) np.testing.assert_allclose(corr2_output['kgamT'], kg.xi, rtol=1.e-3) print('xi_im from corr2 output = ', corr2_output['kgamX']) np.testing.assert_allclose(corr2_output['kgamX'], 0., atol=1.e-4)