def test_basic_dict(): """Test basic operations on Dict.""" # Pickle d = galsim.Dict(dir='config_input', file_name='dict.p') np.testing.assert_equal(len(d), 4) np.testing.assert_equal(d.file_type, 'PICKLE') np.testing.assert_equal(d['i'], 17) np.testing.assert_equal(d.get('s'), 'Life') np.testing.assert_equal(d.get('s2', 'Grail'), 'Grail') # Not in dict. Use default. np.testing.assert_almost_equal(d.get('f', 999.), 23.17) # In dict. Ignore default. d2 = galsim.Dict(dir='config_input', file_name='dict.p', file_type='pickle') assert d == d2 do_pickle(d) # JSON d = galsim.Dict(dir='config_input', file_name='dict.json') np.testing.assert_equal(len(d), 4) np.testing.assert_equal(d.file_type, 'JSON') np.testing.assert_equal(d['i'], -23) np.testing.assert_equal(d.get('s'), 'of') np.testing.assert_equal(d.get('s2', 'Grail'), 'Grail') # Not in dict. Use default. np.testing.assert_almost_equal(d.get('f', 999.), -17.23) # In dict. Ignore default. d2 = galsim.Dict(dir='config_input', file_name='dict.json', file_type='json') assert d == d2 do_pickle(d) # YAML try: import yaml except ImportError as e: # Raise a warning so this message shows up when doing nosetests (or scons tests). import warnings warnings.warn("Unable to import yaml. Skipping yaml tests") print("Caught ", e) else: d = galsim.Dict(dir='config_input', file_name='dict.yaml') np.testing.assert_equal(len(d), 5) np.testing.assert_equal(d.file_type, 'YAML') np.testing.assert_equal(d['i'], 1) np.testing.assert_equal(d.get('s'), 'Brian') np.testing.assert_equal(d.get('s2', 'Grail'), 'Grail') # Not in dict. Use default. np.testing.assert_almost_equal(d.get('f', 999.), 0.1) # In dict. Ignore default. d2 = galsim.Dict(dir='config_input', file_name='dict.yaml', file_type='yaml') assert d == d2 do_pickle(d)
def test_basic_dict(): """Test basic operations on Dict.""" import time t1 = time.time() # Pickle d = galsim.Dict(dir='config_input', file_name='dict.p') np.testing.assert_equal(len(d), 4) np.testing.assert_equal(d.file_type, 'PICKLE') np.testing.assert_equal(d['i'], 17) np.testing.assert_equal(d.get('s'), 'Life') np.testing.assert_equal(d.get('s2', 'Grail'), 'Grail') # Not in dict. Use default. np.testing.assert_almost_equal(d.get('f', 999.), 23.17) # In dict. Ignore default. d2 = galsim.Dict(dir='config_input', file_name='dict.p', file_type='pickle') assert d == d2 do_pickle(d) # JSON d = galsim.Dict(dir='config_input', file_name='dict.json') np.testing.assert_equal(len(d), 4) np.testing.assert_equal(d.file_type, 'JSON') np.testing.assert_equal(d['i'], -23) np.testing.assert_equal(d.get('s'), 'of') np.testing.assert_equal(d.get('s2', 'Grail'), 'Grail') # Not in dict. Use default. np.testing.assert_almost_equal(d.get('f', 999.), -17.23) # In dict. Ignore default. d2 = galsim.Dict(dir='config_input', file_name='dict.json', file_type='json') assert d == d2 do_pickle(d) # YAML d = galsim.Dict(dir='config_input', file_name='dict.yaml') np.testing.assert_equal(len(d), 5) np.testing.assert_equal(d.file_type, 'YAML') np.testing.assert_equal(d['i'], 1) np.testing.assert_equal(d.get('s'), 'Brian') np.testing.assert_equal(d.get('s2', 'Grail'), 'Grail') # Not in dict. Use default. np.testing.assert_almost_equal(d.get('f', 999.), 0.1) # In dict. Ignore default. d2 = galsim.Dict(dir='config_input', file_name='dict.yaml', file_type='yaml') assert d == d2 do_pickle(d) t2 = time.time() print 'time for %s = %.2f' % (funcname(), t2 - t1)
def test_basic_dict(): """Test basic operations on Dict.""" import yaml # Pickle d = galsim.Dict(dir='config_input', file_name='dict.p') np.testing.assert_equal(len(d), 4) np.testing.assert_equal(d.file_type, 'PICKLE') np.testing.assert_equal(d['i'], 17) np.testing.assert_equal(d.get('s'), 'Life') np.testing.assert_equal(d.get('s2', 'Grail'), 'Grail') # Not in dict. Use default. np.testing.assert_almost_equal(d.get('f', 999.), 23.17) # In dict. Ignore default. d2 = galsim.Dict(dir='config_input', file_name='dict.p', file_type='pickle') assert d == d2 do_pickle(d) # JSON d = galsim.Dict(dir='config_input', file_name='dict.json') np.testing.assert_equal(len(d), 4) np.testing.assert_equal(d.file_type, 'JSON') np.testing.assert_equal(d['i'], -23) np.testing.assert_equal(d.get('s'), 'of') np.testing.assert_equal(d.get('s2', 'Grail'), 'Grail') # Not in dict. Use default. np.testing.assert_almost_equal(d.get('f', 999.), -17.23) # In dict. Ignore default. d2 = galsim.Dict(dir='config_input', file_name='dict.json', file_type='json') assert d == d2 do_pickle(d) # YAML d = galsim.Dict(dir='config_input', file_name='dict.yaml') np.testing.assert_equal(len(d), 5) np.testing.assert_equal(d.file_type, 'YAML') np.testing.assert_equal(d['i'], 1) np.testing.assert_equal(d.get('s'), 'Brian') np.testing.assert_equal(d.get('s2', 'Grail'), 'Grail') # Not in dict. Use default. np.testing.assert_almost_equal(d.get('f', 999.), 0.1) # In dict. Ignore default. d2 = galsim.Dict(dir='config_input', file_name='dict.yaml', file_type='yaml') assert d == d2 do_pickle(d) # We also have longer chained keys in dict.yaml np.testing.assert_equal(d.get('noise.models.0.variance'), 0.12) np.testing.assert_equal(d.get('noise.models.1.gain'), 1.9) with assert_raises(KeyError): d.get('invalid') with assert_raises(KeyError): d.get('noise.models.invalid') with assert_raises(KeyError): d.get('noise.models.1.invalid') with assert_raises(IndexError): d.get('noise.models.2.invalid') with assert_raises(TypeError): d.get('noise.models.1.gain.invalid') # It's really hard to get to this error. I think this is the only (contrived) way. d3 = galsim.Dict('dict.yaml', 'config_input', key_split=None) with assert_raises(KeyError): d3.get('') do_pickle(d3) with assert_raises(galsim.GalSimValueError): galsim.Dict(dir='config_input', file_name='dict.yaml', file_type='invalid') with assert_raises(galsim.GalSimValueError): galsim.Dict(dir='config_input', file_name='dict.txt') with assert_raises((IOError, OSError)): galsim.Catalog('invalid.yaml', 'config_input') # Check some dict equivalences. assert 'noise' in d assert len(d) == 5 assert sorted(d.keys()) == ['b', 'f', 'i', 'noise', 's'] assert all(d[k] == v for k, v in d.items()) assert all(d[k] == v for k, v in zip(d.keys(), d.values())) assert all(d[k] == v for k, v in d.iteritems()) assert all(d[k] == v for k, v in zip(d.iterkeys(), d.itervalues())) assert all(k in d for k in d)
def test_str_value(): """Test various ways to generate a str value """ import time t1 = time.time() config = { 'input' : { 'catalog' : { 'dir' : 'config_input', 'file_name' : 'catalog.txt' }, 'dict' : [ { 'dir' : 'config_input', 'file_name' : 'dict.p' }, { 'dir' : 'config_input', 'file_name' : 'dict.yaml' }, { 'dir' : 'config_input', 'file_name' : 'dict.json' } ] }, 'val1' : -93, 'val2' : True, 'val3' : 123.8, 'str1' : "Norwegian", 'str2' : u"Blue", 'cat1' : { 'type' : 'Catalog' , 'col' : 6 }, 'cat2' : { 'type' : 'Catalog' , 'col' : 7 }, 'list1' : { 'type' : 'List', 'items' : [ 'Beautiful', 'plumage!', 'Ay?' ] }, 'file1' : { 'type' : 'NumberedFile', 'root' : 'file', 'num' : 5, 'ext' : '.fits.fz', 'digits' : 3 }, 'file2' : { 'type' : 'NumberedFile', 'root' : 'file', 'num' : 5 }, 'fs1' : { 'type' : 'FormattedStr', 'format' : 'realgal_type%02d_dilation%d.fits', 'items' : [ { 'type' : 'Sequence' , 'repeat' : 3 }, { 'type' : 'Sequence' , 'nitems' : 3 } ] }, 'fs2' : { 'type' : 'FormattedStr', 'format' : '%%%d %i %x %o%i %lf=%g=%e %hi%u %r%s %%', 'items' : [4, 5, 12, 9, 9, math.pi, math.pi, math.pi, 11, -11, 'Goodbye cruel world.', ', said Pink.'] }, 'dict1' : { 'type' : 'Dict', 'key' : 's' }, 'dict2' : { 'type' : 'Dict', 'num' : 1, 'key' : 's' }, 'dict3' : { 'type' : 'Dict', 'num' : 2, 'key' : 's' } } galsim.config.ProcessInput(config) # Test direct values val1 = galsim.config.ParseValue(config,'val1',config, str)[0] np.testing.assert_equal(val1, '-93') val2 = galsim.config.ParseValue(config,'val2',config, str)[0] np.testing.assert_equal(val2, 'True') val3 = galsim.config.ParseValue(config,'val3',config, str)[0] np.testing.assert_equal(val3, '123.8') # Test conversions from strings str1 = galsim.config.ParseValue(config,'str1',config, str)[0] np.testing.assert_equal(str1, 'Norwegian') str2 = galsim.config.ParseValue(config,'str2',config, str)[0] np.testing.assert_equal(str2, 'Blue') # Test values read from a Catalog input_cat = galsim.Catalog(dir='config_input', file_name='catalog.txt') cat1 = [] cat2 = [] for k in range(3): config['seq_index'] = k cat1.append(galsim.config.ParseValue(config,'cat1',config, str)[0]) cat2.append(galsim.config.ParseValue(config,'cat2',config, str)[0]) np.testing.assert_array_equal(cat1, ["He's", "bleedin'", "demised!"]) # Note: white space in the input catalog always separates columns. ' and " don't work. np.testing.assert_array_equal(cat2, ['"ceased', '"bereft', '"kicked']) # Test values taken from a List list1 = [] for k in range(5): config['seq_index'] = k list1.append(galsim.config.ParseValue(config,'list1',config, str)[0]) np.testing.assert_array_equal(list1, ['Beautiful', 'plumage!', 'Ay?', 'Beautiful', 'plumage!']) # Test values built using NumberedFile file1 = galsim.config.ParseValue(config,'file1',config, str)[0] np.testing.assert_equal(file1, 'file005.fits.fz') file2 = galsim.config.ParseValue(config,'file2',config, str)[0] np.testing.assert_equal(file2, 'file5') # Test value built from FormattedStr for k in range(9): config['seq_index'] = k type = k / 3 dil = k % 3 fs1 = galsim.config.ParseValue(config,'fs1',config, str)[0] np.testing.assert_equal(fs1, 'realgal_type%02d_dilation%d.fits'%(type,dil)) fs2 = galsim.config.ParseValue(config,'fs2',config, str)[0] np.testing.assert_equal(fs2, "%4 5 c 119 3.141593=3.14159=3.141593e+00 11-11 'Goodbye cruel world.', said Pink. %") # Test values read from a Dict pickle_dict = galsim.Dict(dir='config_input', file_name='dict.p') yaml_dict = galsim.Dict(dir='config_input', file_name='dict.yaml') json_dict = galsim.Dict(dir='config_input', file_name='dict.json') dict = [] dict.append(galsim.config.ParseValue(config,'dict1',config, str)[0]) dict.append(galsim.config.ParseValue(config,'dict2',config, str)[0]) dict.append(galsim.config.ParseValue(config,'dict3',config, str)[0]) np.testing.assert_array_equal(dict, [ 'Life', 'of', 'Brian' ]) t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)
def test_bool_value(): """Test various ways to generate a bool value """ import time t1 = time.time() config = { 'input' : { 'catalog' : { 'dir' : 'config_input', 'file_name' : 'catalog.txt' }, 'dict' : [ { 'dir' : 'config_input', 'file_name' : 'dict.p' }, { 'dir' : 'config_input', 'file_name' : 'dict.yaml' }, { 'dir' : 'config_input', 'file_name' : 'dict.json' } ] }, 'val1' : True, 'val2' : 1, 'val3' : 0.0, 'str1' : 'true', 'str2' : '0', 'str3' : 'yes', 'str4' : 'No', 'cat1' : { 'type' : 'Catalog' , 'col' : 4 }, 'cat2' : { 'type' : 'Catalog' , 'col' : 5 }, 'ran1' : { 'type' : 'Random' }, 'seq1' : { 'type' : 'Sequence' }, 'seq2' : { 'type' : 'Sequence', 'first' : True, 'repeat' : 2 }, 'list1' : { 'type' : 'List', 'items' : [ 'yes', 'no', 'no' ] }, 'list2' : { 'type' : 'List', 'items' : [ 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0 ], 'index' : { 'type' : 'Sequence', 'first' : 10, 'step' : -3 } }, 'dict1' : { 'type' : 'Dict', 'key' : 'b' }, 'dict2' : { 'type' : 'Dict', 'num' : 1, 'key' : 'b' }, 'dict3' : { 'type' : 'Dict', 'num' : 2, 'key' : 'b' } } galsim.config.ProcessInput(config) # Test direct values val1 = galsim.config.ParseValue(config,'val1',config, bool)[0] np.testing.assert_equal(val1, True) val2 = galsim.config.ParseValue(config,'val2',config, bool)[0] np.testing.assert_equal(val2, True) val3 = galsim.config.ParseValue(config,'val3',config, bool)[0] np.testing.assert_equal(val3, False) # Test conversions from strings str1 = galsim.config.ParseValue(config,'str1',config, bool)[0] np.testing.assert_equal(str1, True) str2 = galsim.config.ParseValue(config,'str2',config, bool)[0] np.testing.assert_equal(str2, False) str3 = galsim.config.ParseValue(config,'str3',config, bool)[0] np.testing.assert_equal(str3, True) str4 = galsim.config.ParseValue(config,'str4',config, bool)[0] np.testing.assert_equal(str4, False) # Test values read from a Catalog input_cat = galsim.Catalog(dir='config_input', file_name='catalog.txt') cat1 = [] cat2 = [] for k in range(5): config['seq_index'] = k cat1.append(galsim.config.ParseValue(config,'cat1',config, bool)[0]) cat2.append(galsim.config.ParseValue(config,'cat2',config, bool)[0]) np.testing.assert_array_equal(cat1, [ 1, 0, 1, 1, 0 ]) np.testing.assert_array_equal(cat2, [ 1, 0, 0, 1, 0 ]) # 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): ran1 = galsim.config.ParseValue(config,'ran1',config, bool)[0] np.testing.assert_equal(ran1, rng() < 0.5) # Test values generated from a Sequence seq1 = [] seq2 = [] for k in range(6): config['seq_index'] = k seq1.append(galsim.config.ParseValue(config,'seq1',config, bool)[0]) seq2.append(galsim.config.ParseValue(config,'seq2',config, bool)[0]) np.testing.assert_array_equal(seq1, [ 0, 1, 0, 1, 0, 1 ]) np.testing.assert_array_equal(seq2, [ 1, 1, 0, 0, 1, 1 ]) # Test values taken from a List list1 = [] list2 = [] for k in range(5): config['seq_index'] = k list1.append(galsim.config.ParseValue(config,'list1',config, bool)[0]) list2.append(galsim.config.ParseValue(config,'list2',config, bool)[0]) np.testing.assert_array_equal(list1, [ 1, 0, 0, 1, 0 ]) np.testing.assert_array_equal(list2, [ 0, 1, 1, 1, 0 ]) # Test values read from a Dict pickle_dict = galsim.Dict(dir='config_input', file_name='dict.p') yaml_dict = galsim.Dict(dir='config_input', file_name='dict.yaml') json_dict = galsim.Dict(dir='config_input', file_name='dict.json') dict = [] dict.append(galsim.config.ParseValue(config,'dict1',config, bool)[0]) dict.append(galsim.config.ParseValue(config,'dict2',config, bool)[0]) dict.append(galsim.config.ParseValue(config,'dict3',config, bool)[0]) np.testing.assert_array_equal(dict, [ True, False, False ]) t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)
def test_int_value(): """Test various ways to generate an int value """ import time t1 = time.time() config = { 'input' : { 'catalog' : { 'dir' : 'config_input', 'file_name' : 'catalog.txt' }, 'dict' : [ { 'dir' : 'config_input', 'file_name' : 'dict.p' }, { 'dir' : 'config_input', 'file_name' : 'dict.yaml' }, { 'dir' : 'config_input', 'file_name' : 'dict.json' } ] }, 'val1' : 9, 'val2' : float(8.7), # Reading as int will drop the fraction. 'val3' : -400.8, # Not floor - negatives will round up. 'str1' : '8', 'str2' : '-2', 'cat1' : { 'type' : 'Catalog' , 'col' : 2 }, 'cat2' : { 'type' : 'Catalog' , 'col' : 3 }, 'ran1' : { 'type' : 'Random', 'min' : 0, 'max' : 3 }, 'ran2' : { 'type' : 'Random', 'min' : -5, 'max' : 10 }, 'seq1' : { 'type' : 'Sequence' }, 'seq2' : { 'type' : 'Sequence', 'step' : 3 }, 'seq3' : { 'type' : 'Sequence', 'first' : 1, 'step' : 5 }, 'seq4' : { 'type' : 'Sequence', 'first' : 10, 'step' : -2 }, 'seq5' : { 'type' : 'Sequence', 'first' : 1, 'last' : 2, 'repeat' : 2 }, 'list1' : { 'type' : 'List', 'items' : [ 73, 8, 3 ] }, 'list2' : { 'type' : 'List', 'items' : [ 6, 8, 1, 7, 3, 5, 1, 0, 6, 3, 8, 2 ], 'index' : { 'type' : 'Sequence', 'first' : 10, 'step' : -3 } }, 'dict1' : { 'type' : 'Dict', 'key' : 'i' }, 'dict2' : { 'type' : 'Dict', 'num' : 1, 'key' : 'i' }, 'dict3' : { 'type' : 'Dict', 'num' : 2, 'key' : 'i' } } galsim.config.ProcessInput(config) # Test direct values val1 = galsim.config.ParseValue(config,'val1',config, int)[0] np.testing.assert_equal(val1, 9) val2 = galsim.config.ParseValue(config,'val2',config, int)[0] np.testing.assert_equal(val2, 8) val3 = galsim.config.ParseValue(config,'val3',config, int)[0] np.testing.assert_equal(val3, -400) # Test conversions from strings str1 = galsim.config.ParseValue(config,'str1',config, int)[0] np.testing.assert_equal(str1, 8) str2 = galsim.config.ParseValue(config,'str2',config, int)[0] np.testing.assert_equal(str2, -2) # Test values read from a Catalog input_cat = galsim.Catalog(dir='config_input', file_name='catalog.txt') cat1 = [] cat2 = [] for k in range(5): config['seq_index'] = k cat1.append(galsim.config.ParseValue(config,'cat1',config, int)[0]) cat2.append(galsim.config.ParseValue(config,'cat2',config, int)[0]) np.testing.assert_array_equal(cat1, [ 9, 0, -4, 9, 0 ]) np.testing.assert_array_equal(cat2, [ -3, 8, 17, -3, 8 ]) # 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): ran1 = galsim.config.ParseValue(config,'ran1',config, int)[0] np.testing.assert_equal(ran1, int(math.floor(rng() * 4))) ran2 = galsim.config.ParseValue(config,'ran2',config, int)[0] np.testing.assert_equal(ran2, int(math.floor(rng() * 16))-5) # Test values generated from a Sequence seq1 = [] seq2 = [] seq3 = [] seq4 = [] seq5 = [] for k in range(6): config['seq_index'] = k seq1.append(galsim.config.ParseValue(config,'seq1',config, int)[0]) seq2.append(galsim.config.ParseValue(config,'seq2',config, int)[0]) seq3.append(galsim.config.ParseValue(config,'seq3',config, int)[0]) seq4.append(galsim.config.ParseValue(config,'seq4',config, int)[0]) seq5.append(galsim.config.ParseValue(config,'seq5',config, int)[0]) np.testing.assert_array_equal(seq1, [ 0, 1, 2, 3, 4, 5 ]) np.testing.assert_array_equal(seq2, [ 0, 3, 6, 9, 12, 15 ]) np.testing.assert_array_equal(seq3, [ 1, 6, 11, 16, 21, 26 ]) np.testing.assert_array_equal(seq4, [ 10, 8, 6, 4, 2, 0 ]) np.testing.assert_array_equal(seq5, [ 1, 1, 2, 2, 1, 1 ]) # Test values taken from a List list1 = [] list2 = [] for k in range(5): config['seq_index'] = k list1.append(galsim.config.ParseValue(config,'list1',config, int)[0]) list2.append(galsim.config.ParseValue(config,'list2',config, int)[0]) np.testing.assert_array_equal(list1, [ 73, 8, 3, 73, 8 ]) np.testing.assert_array_equal(list2, [ 8, 0, 3, 8, 8 ]) # Test values read from a Dict pickle_dict = galsim.Dict(dir='config_input', file_name='dict.p') yaml_dict = galsim.Dict(dir='config_input', file_name='dict.yaml') json_dict = galsim.Dict(dir='config_input', file_name='dict.json') dict = [] dict.append(galsim.config.ParseValue(config,'dict1',config, int)[0]) dict.append(galsim.config.ParseValue(config,'dict2',config, int)[0]) dict.append(galsim.config.ParseValue(config,'dict3',config, int)[0]) np.testing.assert_array_equal(dict, [ 17, 1, -23 ]) t2 = time.time() print 'time for %s = %.2f'%(funcname(),t2-t1)