Example #1
0
def test_element_type_serialize_deserialize(fcn):
    '''
    test serialization/deserialization of ElementType for various initiailzers
    '''
    element_type = ElementType(initializers=[fcn], substance=oil)

    json_ = element_type.serialize()
    element_type2 = ElementType.deserialize(json_)

    assert element_type == element_type2
def test_element_type_serialize_deserialize(fcn, init_key):
    """
    test serialization/deserialization of ElementType for various initiailzers
    """
    initializers = {init_key: fcn}
    element_type = ElementType(initializers)

    json_ = element_type.serialize("save")
    dict_ = element_type.deserialize(json_)
    element_type2 = ElementType.new_from_dict(dict_)

    assert element_type == element_type2
def test_serialize_deserialize():
    '''
    serialize/deserialize for 'save' option is tested in test_save_load
    This tests serialize/deserilize with 'webapi' option
    '''
    et = floating()
    dict_ = ElementType.deserialize(et.serialize('webapi'))

    # for webapi, make new objects from nested objects before creating
    # new element_type
    dict_['initializers'] = et.initializers
    n_et = ElementType.new_from_dict(dict_)
    # following is not a requirement for webapi, but it is infact the case
    assert n_et == et
Example #4
0
def test_serialize_deserialize():
    '''
    serialize/deserialize for 'save' option is tested in test_save_load
    This tests serialize/deserilize with 'webapi' option
    '''
    et = floating()
    dict_ = ElementType.deserialize(et.serialize('webapi'))

    # for webapi, make new objects from nested objects before creating
    # new element_type
    dict_['initializers'] = et.initializers
    n_et = ElementType.new_from_dict(dict_)
    # following is not a requirement for webapi, but it is infact the case
    assert n_et == et
Example #5
0
def test_element_type_init(substance):
    et = ElementType(substance=substance)
    if isinstance(substance, basestring):
        assert et.substance.get('name') == substance
    elif isinstance(substance, int):
        assert et.substance.get('id') == substance
    else:
        assert et.substance.get('name') == substance.get('name')
Example #6
0
def test_element_type_init(substance):
    et = ElementType(substance=substance)
    if isinstance(substance, basestring):
        try:
            assert et.substance.get('name') == substance
        except AssertionError:
            assert et.substance.get('name') == _sample_oils[substance].name
    elif isinstance(substance, int):
        assert et.substance.get('id') == substance
    else:
        assert et.substance.get('name') == substance.get('name')
Example #7
0
def test_ordered_collection_api():
    release_time = datetime(2012, 1, 1, 12)
    start_position = (23.0, -78.5, 0.0)
    num_elements = 100

    sc = SpillContainer()
    sc.spills += point_line_release_spill(num_elements, start_position,
                                          release_time)
    assert len(sc.spills) == 1


""" tests w/ element types set for two spills """
el0 = ElementType([
    InitWindages((0.02, 0.02), -1),
    InitRiseVelFromDist(distribution=UniformDistribution(low=1, high=10))
],
                  substance=test_oil)

el1 = ElementType([InitWindages(), InitRiseVelFromDist()], substance=test_oil)

arr_types = {'windages', 'windage_range', 'windage_persist', 'rise_vel'}


@pytest.mark.parametrize(("elem_type", "arr_types"), [((el0, el1), arr_types)])
def test_element_types(elem_type, arr_types, sample_sc_no_uncertainty):
    """
    Tests that the spill_container's data_arrays associated with initializers
    are correctly setup for each spill
    uses sample_sc_no_uncertainty fixture defined in conftest.py
    """
Example #8
0
                         SpatialRelease, InitElemsFromFile)
from gnome.spill.elements import (floating, ElementType)
from gnome.spill_container import SpillContainer

from ..conftest import mock_sc_array_types, mock_append_data_arrays, test_oil, testdata

# Used to mock SpillContainer functionality of creating/appending data_arrays
# Only care about 'positions' array type for all spills, no need to define
# and carry remaining numpy arrays
arr_types = mock_sc_array_types({'positions', 'mass'})


@pytest.mark.parametrize(("element_type", "amount"), [
    (None, None),
    (None, 10.0),
    (ElementType(), None),
    (ElementType(), 1.0),
])
def test_init(element_type, amount):
    '''
    Test various initializations
    '''
    spill = Spill(Release(release_time=datetime.now()),
                  element_type=element_type,
                  amount=amount,
                  units='kg')

    if element_type is None:
        assert np.all(spill.windage_range == (0.01, 0.04))
        assert spill.windage_persist == 900
        # no need to test this in spill -- it shouldn't know about initializers
Example #9
0
def test_exception():
    with pytest.raises(Exception):
        ElementType(substance='junk')
Example #10
0
    assert_dataarray_shape_size(rise_vel_array, data_arrays, num_elems)

    assert np.all(0 != data_arrays['rise_vel'])


""" Element Types"""
# additional array_types corresponding with ElementTypes for following test
arr_types = windages
rise_vel = mock_sc_array_types(['rise_vel'])
rise_vel.update(arr_types)

oil = test_oil

inp_params = [((floating(substance=oil),
                ElementType([InitWindages()], substance=oil)), arr_types),
              ((floating(substance=oil),
                ElementType([InitWindages(),
                             InitRiseVelFromDist(distribution=UniformDistribution())],
                            substance=oil)), rise_vel),
              ((floating(substance=oil),
                ElementType([InitRiseVelFromDist(distribution=UniformDistribution())],
                            substance=oil)), rise_vel),
              ]


@pytest.mark.parametrize(("elem_type", "arr_types"), inp_params)
def test_element_types(elem_type, arr_types, sample_sc_no_uncertainty):
    """
    Tests data_arrays associated with the spill_container's
    initializers get initialized to non-zero values.
Example #11
0
    r = RiseVelocityMover()
    #r.water_density = 1
    #r.water_viscosity = 1.1e-6

    #assert r.water_density == 1
    #assert r.water_viscosity == 1.1e-6


time_step = 15 * 60  # seconds
rel_time = datetime(2012, 8, 20, 13)  # yyyy/month/day/hr/min/sec
sc = sample_sc_release(
    5, (3., 6., 0.),
    rel_time,
    uncertain=False,
    arr_types={'rise_vel'},
    element_type=ElementType(
        [InitRiseVelFromDist(distribution=UniformDistribution())]))
u_sc = sample_sc_release(
    5, (3., 6., 0.),
    rel_time,
    uncertain=True,
    arr_types={'rise_vel'},
    element_type=ElementType(
        [InitRiseVelFromDist(distribution=UniformDistribution())]))
model_time = rel_time


@pytest.mark.parametrize("test_sc", [sc, u_sc])
def test_one_move(test_sc):
    """
    calls one step for movement - just checks that it doesn't fail for any step
    Placeholder - get_move does not currently work since all data_arrays are
    r = RiseVelocityMover()
    #r.water_density = 1
    #r.water_viscosity = 1.1e-6

    #assert r.water_density == 1
    #assert r.water_viscosity == 1.1e-6


time_step = 15 * 60  # seconds
rel_time = datetime(2012, 8, 20, 13)  # yyyy/month/day/hr/min/sec
sc = sample_sc_release(5, (3., 6., 0.),
                       rel_time,
                       uncertain=False,
                       arr_types={'rise_vel'},
                       element_type=ElementType([InitRiseVelFromDist()]))
u_sc = sample_sc_release(5, (3., 6., 0.),
                         rel_time,
                         uncertain=True,
                         arr_types={'rise_vel'},
                         element_type=ElementType([InitRiseVelFromDist()]))
model_time = rel_time


@pytest.mark.parametrize("test_sc", [sc, u_sc])
def test_one_move(test_sc):
    """
    calls one step for movement - just checks that it doesn't fail for any step
    Placeholder - get_move does not currently work since all data_arrays are
    not yet defined
    """
Example #13
0
    assert_dataarray_shape_size(rise_vel_array, data_arrays, num_elems)

    assert np.all(0 != data_arrays['rise_vel'])


""" Element Types"""
# additional array_types corresponding with ElementTypes for following test
arr_types = windages
rise_vel = mock_sc_array_types(['rise_vel'])
rise_vel.update(arr_types)

oil = test_oil

inp_params = [
    ((floating(substance=oil), ElementType([InitWindages()],
                                           substance=oil)), arr_types),
    ((floating(substance=oil),
      ElementType([
          InitWindages(),
          InitRiseVelFromDist(distribution=UniformDistribution())
      ],
                  substance=oil)), rise_vel),
    ((floating(substance=oil),
      ElementType([InitRiseVelFromDist(distribution=UniformDistribution())],
                  substance=oil)), rise_vel),
]


@pytest.mark.parametrize(("elem_type", "arr_types"), inp_params)
def test_element_types(elem_type, arr_types, sample_sc_no_uncertainty):
    """