def _GenerateFromRandomChi2(config, base, value_type): """@brief Return a random value drawn from a Chi^2 distribution """ rng = galsim.config.GetRNG(config, base) req = { 'n' : float } kwargs, safe = galsim.config.GetAllParams(config, base, req=req) n = kwargs['n'] dev = galsim.Chi2Deviate(rng,n=n) val = dev() #print(base['obj_num'],'RandomChi2: ',val) return val, False
def _GenerateFromRandomChi2(param, param_name, base, value_type): """@brief Return a random value drawn from a Chi^2 distribution """ if 'rng' not in base: raise ValueError("No base['rng'] available for %s.type = RandomChi2"%param_name) rng = base['rng'] req = { 'n' : float } kwargs, safe = GetAllParams(param, param_name, base, req=req) n = kwargs['n'] dev = galsim.Chi2Deviate(rng,n=n) val = dev() #print base['obj_num'],'RandomChi2: ',val return val, False
def test_float_value(): """Test various ways to generate a float value """ import time t1 = time.time() config = { 'input': { 'catalog': [{ 'dir': 'config_input', 'file_name': 'catalog.txt' }, { 'dir': 'config_input', 'file_name': 'catalog.fits' }], 'dict': [{ 'dir': 'config_input', 'file_name': 'dict.p' }, { 'dir': 'config_input', 'file_name': 'dict.json' }, { 'dir': 'config_input', 'file_name': 'dict.yaml' }] }, 'val1': 9.9, 'val2': int(400), 'str1': '8.73', 'str2': '2.33e-9', 'str3': '6.e-9', 'cat1': { 'type': 'Catalog', 'col': 0 }, 'cat2': { 'type': 'Catalog', 'col': 1 }, 'cat3': { 'type': 'Catalog', 'num': 1, 'col': 'float1' }, 'cat4': { 'type': 'Catalog', 'num': 1, 'col': 'float2' }, 'ran1': { 'type': 'Random', 'min': 0.5, 'max': 3 }, 'ran2': { 'type': 'Random', 'min': -5, 'max': 0 }, 'gauss1': { 'type': 'RandomGaussian', 'sigma': 1 }, 'gauss2': { 'type': 'RandomGaussian', 'sigma': 3, 'mean': 4 }, 'gauss3': { 'type': 'RandomGaussian', 'sigma': 1.5, 'min': -2, 'max': 2 }, 'gauss4': { 'type': 'RandomGaussian', 'sigma': 0.5, 'min': 0, 'max': 0.8 }, 'gauss5': { 'type': 'RandomGaussian', 'sigma': 0.3, 'mean': 0.5, 'min': 0, 'max': 0.5 }, 'dist1': { 'type': 'RandomDistribution', 'function': 'config_input/distribution.txt', 'interpolant': 'linear' }, 'dist2': { 'type': 'RandomDistribution', 'function': 'config_input/distribution2.txt', 'interpolant': 'linear' }, 'dist3': { 'type': 'RandomDistribution', 'function': 'x*x', 'x_min': 0., 'x_max': 2.0 }, 'dev1': { 'type': 'RandomPoisson', 'mean': 137 }, 'dev2': { 'type': 'RandomBinomial', 'N': 17 }, 'dev3': { 'type': 'RandomBinomial', 'N': 17, 'p': 0.2 }, 'dev4': { 'type': 'RandomWeibull', 'a': 1.7, 'b': 4.3 }, 'dev5': { 'type': 'RandomGamma', 'k': 1, 'theta': 4 }, 'dev6': { 'type': 'RandomGamma', 'k': 1.9, 'theta': 4.1 }, 'dev7': { 'type': 'RandomChi2', 'n': 17 }, 'seq1': { 'type': 'Sequence' }, 'seq2': { 'type': 'Sequence', 'step': 0.1 }, 'seq3': { 'type': 'Sequence', 'first': 1.5, 'step': 0.5 }, 'seq4': { 'type': 'Sequence', 'first': 10, 'step': -2 }, 'seq5': { 'type': 'Sequence', 'first': 1, 'last': 2.1, 'repeat': 2 }, 'list1': { 'type': 'List', 'items': [73, 8.9, 3.14] }, 'list2': { 'type': 'List', 'items': [0.6, 1.8, 2.1, 3.7, 4.3, 5.5, 6.1, 7.0, 8.6, 9.3, 10.8, 11.2], 'index': { 'type': 'Sequence', 'first': 10, 'step': -3 } }, 'dict1': { 'type': 'Dict', 'key': 'f' }, 'dict2': { 'type': 'Dict', 'num': 1, 'key': 'f' }, 'dict3': { 'type': 'Dict', 'num': 2, 'key': 'f' }, 'dict4': { 'type': 'Dict', 'num': 2, 'key': 'noise.models.1.gain' }, 'sum1': { 'type': 'Sum', 'items': [72, '2.33', { 'type': 'Dict', 'key': 'f' }] } } test_yaml = True try: galsim.config.ProcessInput(config) except: # We don't require PyYAML as a dependency, so if this fails, just remove the YAML dict. del config['input']['dict'][2] galsim.config.ProcessInput(config) test_yaml = False # Test direct values val1 = galsim.config.ParseValue(config, 'val1', config, float)[0] np.testing.assert_almost_equal(val1, 9.9) val2 = galsim.config.ParseValue(config, 'val2', config, float)[0] np.testing.assert_almost_equal(val2, 400) # Test conversions from strings str1 = galsim.config.ParseValue(config, 'str1', config, float)[0] np.testing.assert_almost_equal(str1, 8.73) str2 = galsim.config.ParseValue(config, 'str2', config, float)[0] np.testing.assert_almost_equal(str2, 2.33e-9) str3 = galsim.config.ParseValue(config, 'str3', config, float)[0] np.testing.assert_almost_equal(str3, 6.0e-9) # Test values read from a Catalog cat1 = [] cat2 = [] cat3 = [] cat4 = [] config['index_key'] = 'file_num' for k in range(5): config['file_num'] = k cat1.append(galsim.config.ParseValue(config, 'cat1', config, float)[0]) cat2.append(galsim.config.ParseValue(config, 'cat2', config, float)[0]) cat3.append(galsim.config.ParseValue(config, 'cat3', config, float)[0]) cat4.append(galsim.config.ParseValue(config, 'cat4', config, float)[0]) np.testing.assert_array_almost_equal(cat1, [1.234, 2.345, 3.456, 1.234, 2.345]) np.testing.assert_array_almost_equal(cat2, [4.131, -900, 8000, 4.131, -900]) np.testing.assert_array_almost_equal(cat3, [1.234, 2.345, 3.456, 1.234, 2.345]) np.testing.assert_array_almost_equal(cat4, [4.131, -900, 8000, 4.131, -900]) # Test values generated from a uniform deviate rng = galsim.UniformDeviate(1234) config['rng'] = galsim.UniformDeviate( 1234) # A second copy starting with the same seed. for k in range(6): config[ 'obj_num'] = k # The Random type doesn't use obj_num, but this keeps it # from thinking current_val is still current. ran1 = galsim.config.ParseValue(config, 'ran1', config, float)[0] np.testing.assert_almost_equal(ran1, rng() * 2.5 + 0.5) ran2 = galsim.config.ParseValue(config, 'ran2', config, float)[0] np.testing.assert_almost_equal(ran2, rng() * 5 - 5) # Test values generated from a Gaussian deviate for k in range(6): config['obj_num'] = k gauss1 = galsim.config.ParseValue(config, 'gauss1', config, float)[0] gd = galsim.GaussianDeviate(rng, mean=0, sigma=1) np.testing.assert_almost_equal(gauss1, gd()) gauss2 = galsim.config.ParseValue(config, 'gauss2', config, float)[0] gd = galsim.GaussianDeviate(rng, mean=4, sigma=3) np.testing.assert_almost_equal(gauss2, gd()) gauss3 = galsim.config.ParseValue(config, 'gauss3', config, float)[0] gd = galsim.GaussianDeviate(rng, mean=0, sigma=1.5) gd_val = gd() while math.fabs(gd_val) > 2: gd_val = gd() np.testing.assert_almost_equal(gauss3, gd_val) gauss4 = galsim.config.ParseValue(config, 'gauss4', config, float)[0] gd = galsim.GaussianDeviate(rng, mean=0, sigma=0.5) gd_val = math.fabs(gd()) while gd_val > 0.8: gd_val = math.fabs(gd()) np.testing.assert_almost_equal(gauss4, gd_val) gauss5 = galsim.config.ParseValue(config, 'gauss5', config, float)[0] gd = galsim.GaussianDeviate(rng, mean=0.5, sigma=0.3) gd_val = gd() if gd_val > 0.5: gd_val = 1 - gd_val while gd_val < 0: gd_val = gd() if gd_val > 0.5: gd_val = 1 - gd_val np.testing.assert_almost_equal(gauss5, gd_val) # Test values generated from a distribution in a file dd = galsim.DistDeviate(rng, function='config_input/distribution.txt', interpolant='linear') for k in range(6): config['obj_num'] = k dist1 = galsim.config.ParseValue(config, 'dist1', config, float)[0] np.testing.assert_almost_equal(dist1, dd()) dd = galsim.DistDeviate(rng, function='config_input/distribution2.txt', interpolant='linear') for k in range(6): config['obj_num'] = k dist2 = galsim.config.ParseValue(config, 'dist2', config, float)[0] np.testing.assert_almost_equal(dist2, dd()) dd = galsim.DistDeviate(rng, function=lambda x: x * x, x_min=0., x_max=2.) for k in range(6): config['obj_num'] = k dist3 = galsim.config.ParseValue(config, 'dist3', config, float)[0] np.testing.assert_almost_equal(dist3, dd()) # Test values generated from various other deviates for k in range(6): config['obj_num'] = k dev = galsim.PoissonDeviate(rng, mean=137) dev1 = galsim.config.ParseValue(config, 'dev1', config, float)[0] np.testing.assert_almost_equal(dev1, dev()) dev = galsim.BinomialDeviate(rng, N=17) dev2 = galsim.config.ParseValue(config, 'dev2', config, float)[0] np.testing.assert_almost_equal(dev2, dev()) dev = galsim.BinomialDeviate(rng, N=17, p=0.2) dev3 = galsim.config.ParseValue(config, 'dev3', config, float)[0] np.testing.assert_almost_equal(dev3, dev()) dev = galsim.WeibullDeviate(rng, a=1.7, b=4.3) dev4 = galsim.config.ParseValue(config, 'dev4', config, float)[0] np.testing.assert_almost_equal(dev4, dev()) dev = galsim.GammaDeviate(rng, k=1, theta=4) dev5 = galsim.config.ParseValue(config, 'dev5', config, float)[0] np.testing.assert_almost_equal(dev5, dev()) dev = galsim.GammaDeviate(rng, k=1.9, theta=4.1) dev6 = galsim.config.ParseValue(config, 'dev6', config, float)[0] np.testing.assert_almost_equal(dev6, dev()) dev = galsim.Chi2Deviate(rng, n=17) dev7 = galsim.config.ParseValue(config, 'dev7', config, float)[0] np.testing.assert_almost_equal(dev7, dev()) # Test values generated from a Sequence seq1 = [] seq2 = [] seq3 = [] seq4 = [] seq5 = [] config['index_key'] = 'file_num' for k in range(6): config['file_num'] = k seq1.append(galsim.config.ParseValue(config, 'seq1', config, float)[0]) config['index_key'] = 'image_num' for k in range(6): config['image_num'] = k seq2.append(galsim.config.ParseValue(config, 'seq2', config, float)[0]) config['index_key'] = 'obj_num' for k in range(6): config['obj_num'] = k seq3.append(galsim.config.ParseValue(config, 'seq3', config, float)[0]) config['index_key'] = 'obj_num_in_file' config['start_obj_num'] = 10 for k in range(6): config['obj_num'] = k + 10 seq4.append(galsim.config.ParseValue(config, 'seq4', config, float)[0]) seq5.append(galsim.config.ParseValue(config, 'seq5', config, float)[0]) np.testing.assert_array_almost_equal(seq1, [0, 1, 2, 3, 4, 5]) np.testing.assert_array_almost_equal(seq2, [0, 0.1, 0.2, 0.3, 0.4, 0.5]) np.testing.assert_array_almost_equal(seq3, [1.5, 2, 2.5, 3, 3.5, 4]) np.testing.assert_array_almost_equal(seq4, [10, 8, 6, 4, 2, 0]) np.testing.assert_array_almost_equal(seq5, [1, 1, 2, 2, 1, 1]) # Test values taken from a List list1 = [] list2 = [] config['index_key'] = 'obj_num' for k in range(5): config['obj_num'] = k list1.append( galsim.config.ParseValue(config, 'list1', config, float)[0]) list2.append( galsim.config.ParseValue(config, 'list2', config, float)[0]) np.testing.assert_array_almost_equal(list1, [73, 8.9, 3.14, 73, 8.9]) np.testing.assert_array_almost_equal(list2, [10.8, 7.0, 4.3, 1.8, 10.8]) # Test values read from a Dict dict = [] dict.append(galsim.config.ParseValue(config, 'dict1', config, float)[0]) dict.append(galsim.config.ParseValue(config, 'dict2', config, float)[0]) if test_yaml: dict.append( galsim.config.ParseValue(config, 'dict3', config, float)[0]) dict.append( galsim.config.ParseValue(config, 'dict4', config, float)[0]) else: dict.append(0.1) dict.append(1.9) np.testing.assert_array_almost_equal(dict, [23.17, -17.23, 0.1, 1.9]) sum1 = galsim.config.ParseValue(config, 'sum1', config, float)[0] np.testing.assert_almost_equal(sum1, 72 + 2.33 + 23.17) t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)
def test_dep_random(): """Test the deprecated methods in galsim/deprecated/random.py """ rng = galsim.BaseDeviate(123) gd = galsim.GaussianDeviate(rng, mean=0.5, sigma=1.7) np.testing.assert_almost_equal(gd.getMean(), 0.5) np.testing.assert_almost_equal(gd.getSigma(), 1.7) check_dep(gd.setMean, 0.9) np.testing.assert_almost_equal(gd.getMean(), 0.9) np.testing.assert_almost_equal(gd.getSigma(), 1.7) check_dep(gd.setSigma, 2.3) np.testing.assert_almost_equal(gd.getMean(), 0.9) np.testing.assert_almost_equal(gd.getSigma(), 2.3) bd = galsim.BinomialDeviate(rng, N=7, p=0.7) np.testing.assert_almost_equal(bd.getN(), 7) np.testing.assert_almost_equal(bd.getP(), 0.7) check_dep(bd.setN, 9) np.testing.assert_almost_equal(bd.getN(), 9) np.testing.assert_almost_equal(bd.getP(), 0.7) check_dep(bd.setP, 0.3) np.testing.assert_almost_equal(bd.getN(), 9) np.testing.assert_almost_equal(bd.getP(), 0.3) pd = galsim.PoissonDeviate(rng, mean=0.5) np.testing.assert_almost_equal(pd.getMean(), 0.5) check_dep(pd.setMean, 0.9) np.testing.assert_almost_equal(pd.getMean(), 0.9) wd = galsim.WeibullDeviate(rng, a=0.5, b=1.7) np.testing.assert_almost_equal(wd.getA(), 0.5) np.testing.assert_almost_equal(wd.getB(), 1.7) check_dep(wd.setA, 0.9) np.testing.assert_almost_equal(wd.getA(), 0.9) np.testing.assert_almost_equal(wd.getB(), 1.7) check_dep(wd.setB, 2.3) np.testing.assert_almost_equal(wd.getA(), 0.9) np.testing.assert_almost_equal(wd.getB(), 2.3) gd = galsim.GammaDeviate(rng, k=0.5, theta=1.7) np.testing.assert_almost_equal(gd.getK(), 0.5) np.testing.assert_almost_equal(gd.getTheta(), 1.7) check_dep(gd.setK, 0.9) np.testing.assert_almost_equal(gd.getK(), 0.9) np.testing.assert_almost_equal(gd.getTheta(), 1.7) check_dep(gd.setTheta, 2.3) np.testing.assert_almost_equal(gd.getK(), 0.9) np.testing.assert_almost_equal(gd.getTheta(), 2.3) cd = galsim.Chi2Deviate(rng, n=5) np.testing.assert_almost_equal(cd.getN(), 5) check_dep(cd.setN, 9) np.testing.assert_almost_equal(cd.getN(), 9)