Skip to content

Commit

Permalink
Don't guard against ImportError for yaml in tests that use it.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmjarvis committed May 24, 2018
1 parent 4cfe870 commit 51b772e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 94 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pybind11>=2.2
pip==9.0.3 # For now, pybind11 in conjunction with pip version 10.0 is broken. Use 9.0.3.

# Not technically required, but useful.
pyyaml>=3.12
pyyaml>=3.12 # This one is required to run tests.
pandas>=0.20

# This is not in conda. Let pip install these.
Expand Down
1 change: 1 addition & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pyyaml>=3.12
pytest>=3.4
pytest-xdist>=1.19
pytest-timeout>=1.2
Expand Down
72 changes: 54 additions & 18 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,54 @@ def test_basic_catalog():

do_pickle(cat)

cat2 = galsim.Catalog('catalog.fits', 'config_input', hdu=1, file_type='FITS')
assert cat2 == cat

cat3 = galsim.Catalog('catalog.fits', 'config_input', _nobjects_only=True)
assert cat3 == cat
assert cat3.nobjects == cat3.getNObjects() == cat.nobjects
with assert_raises(AttributeError):
assert cat3.ncols
with assert_raises(AttributeError):
cat3.get(1,'angle2')

with assert_raises(galsim.GalSimValueError):
galsim.Catalog('catalog.fita', 'config_input', file_type='invalid')

with assert_raises(IndexError):
cat.get(-1, 'angle2')
with assert_raises(IndexError):
cat.get(3, 'angle2')
with assert_raises(KeyError):
cat.get(1, 'invalid')
with assert_raises(TypeError):
cat.get('val', 'angle2')

cat2 = galsim.Catalog('catalog2.fits', 'config_input', hdu=2)
assert cat2.nobjects == cat.nobjects
np.testing.assert_array_equal(cat2.data, cat.data)
assert cat2 != cat
do_pickle(cat2)

cat3 = galsim.Catalog('catalog2.fits', 'config_input', hdu='data')
assert cat3.nobjects == cat.nobjects
np.testing.assert_array_equal(cat3.data, cat.data)
assert cat3 != cat
assert cat3 != cat2 # Even though these are the same, it doesn't no 'data' is hdu 2.
do_pickle(cat3)

cat2n = galsim.Catalog('catalog2.fits', 'config_input', hdu=2, _nobjects_only=True)
assert cat2n.nobjects == 3

with assert_raises((IOError, OSError)):
galsim.Catalog('invalid.fits', 'config_input')


@timer
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)
Expand All @@ -79,24 +123,16 @@ def test_basic_dict():
do_pickle(d)

# YAML
try:
import yaml
except ImportError as e:
# Raise a warning so this message shows up when doing pytest (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)
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)


@timer
Expand Down
59 changes: 9 additions & 50 deletions tests/test_config_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,7 @@ def test_float_value():
'image_center' : galsim.PositionD(0,0),
}

test_yaml = True
try:
galsim.config.ProcessInput(config)
except ImportError:
# 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
galsim.config.ProcessInput(config)

# Test direct values
val1 = galsim.config.ParseValue(config,'val1',config, float)[0]
Expand Down Expand Up @@ -315,12 +308,8 @@ def test_float_value():
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)
dict.append(galsim.config.ParseValue(config,'dict3',config, float)[0])
dict.append(galsim.config.ParseValue(config,'dict4',config, float)[0])
np.testing.assert_array_almost_equal(dict, [ 23.17, -17.23, 0.1, 1.9 ])

sum1 = galsim.config.ParseValue(config,'sum1',config, float)[0]
Expand Down Expand Up @@ -530,14 +519,7 @@ def test_int_value():
'bad5' : { 'type' : 'Catalog' , 'num' : -1, 'col' : 'int1' },
}

test_yaml = True
try:
galsim.config.ProcessInput(config)
except ImportError:
# 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
galsim.config.ProcessInput(config)

# Test direct values
val1 = galsim.config.ParseValue(config,'val1',config, int)[0]
Expand Down Expand Up @@ -684,10 +666,7 @@ def test_int_value():
dict = []
dict.append(galsim.config.ParseValue(config,'dict1',config, int)[0])
dict.append(galsim.config.ParseValue(config,'dict2',config, int)[0])
if test_yaml:
dict.append(galsim.config.ParseValue(config,'dict3',config, int)[0])
else:
dict.append(1)
dict.append(galsim.config.ParseValue(config,'dict3',config, int)[0])
np.testing.assert_array_equal(dict, [ 17, -23, 1 ])

sum1 = galsim.config.ParseValue(config,'sum1', config, int)[0]
Expand Down Expand Up @@ -760,14 +739,7 @@ def test_bool_value():
'bad3' : { 'type' : 'RandomBinomial', 'N' : 2 },
}

test_yaml = True
try:
galsim.config.ProcessInput(config)
except ImportError:
# 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
galsim.config.ProcessInput(config)

# Test direct values
val1 = galsim.config.ParseValue(config,'val1',config, bool)[0]
Expand Down Expand Up @@ -866,10 +838,7 @@ def test_bool_value():
dict = []
dict.append(galsim.config.ParseValue(config,'dict1',config, bool)[0])
dict.append(galsim.config.ParseValue(config,'dict2',config, bool)[0])
if test_yaml:
dict.append(galsim.config.ParseValue(config,'dict3',config, bool)[0])
else:
dict.append(False)
dict.append(galsim.config.ParseValue(config,'dict3',config, bool)[0])
np.testing.assert_array_equal(dict, [ True, False, False ])

# Test bad values
Expand Down Expand Up @@ -926,14 +895,7 @@ def test_str_value():
'bad5' : { 'type' : 'List', 'items' : [ 'Beautiful', 'plumage!', 'Ay?' ], 'index' : 5 },
}

test_yaml = True
try:
galsim.config.ProcessInput(config)
except ImportError:
# 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
galsim.config.ProcessInput(config)

# Test direct values
val1 = galsim.config.ParseValue(config,'val1',config, str)[0]
Expand Down Expand Up @@ -1003,10 +965,7 @@ def test_str_value():
dict = []
dict.append(galsim.config.ParseValue(config,'dict1',config, str)[0])
dict.append(galsim.config.ParseValue(config,'dict2',config, str)[0])
if test_yaml:
dict.append(galsim.config.ParseValue(config,'dict3',config, str)[0])
else:
dict.append('Brian')
dict.append(galsim.config.ParseValue(config,'dict3',config, str)[0])
np.testing.assert_array_equal(dict, [ 'Life', 'of', 'Brian' ])

with assert_raises(galsim.GalSimConfigError):
Expand Down
7 changes: 1 addition & 6 deletions tests/test_vonkarman.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,9 @@
import os
import sys

import galsim
from galsim_test_helpers import *

try:
import galsim
except ImportError:
sys.path.append(os.path.abspath(os.path.join(path, "..")))
import galsim


@timer
def test_vk(slow=False):
Expand Down
8 changes: 1 addition & 7 deletions tests/test_zernike.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@
import os
import sys

import galsim
from galsim_test_helpers import *

try:
import galsim
except ImportError:
path, filename = os.path.split(__file__)
sys.path.append(os.path.abspath(os.path.join(path, "..")))
import galsim


@timer
def test_Zernike_orthonormality():
Expand Down
7 changes: 1 addition & 6 deletions tests/time_noise_pad.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@

n_iter = 50

try:
import galsim
except ImportError:
path, filename = os.path.split(__file__)
sys.path.append(os.path.abspath(os.path.join(path, "..")))
import galsim
import galsim

def funcname():
import inspect
Expand Down
7 changes: 1 addition & 6 deletions tests/time_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@

n_iter = 20

try:
import galsim
except ImportError:
path, filename = os.path.split(__file__)
sys.path.append(os.path.abspath(os.path.join(path, "..")))
import galsim
import galsim

big_im = galsim.Image(5000, 5000)
big_im_file = 'big_im_file.fits'
Expand Down

0 comments on commit 51b772e

Please sign in to comment.