def test_compare_ndarrays(IR_dataset_1D):

    nda1 = NDArray(IR_dataset_1D)
    nda2 = nda1.copy()

    assert not nda1.implements("NDDataset")
    testing.assert_ndarray_equal(nda1, nda2)

    # equality does not depend on title
    nda3 = nda1.copy()
    nda3.title = "xxx"
    testing.assert_ndarray_equal(nda1, nda3)

    # should have same units
    nda3.ito("km", force=True)
    with testing.raises(AssertionError):
        testing.assert_ndarray_equal(nda1, nda3)

    # almost equal ndarrays
    nda4 = nda1.copy()
    nda4.data += 1.0e-6
    with testing.raises(AssertionError):
        testing.assert_ndarray_equal(nda1, nda4)
    testing.assert_ndarray_almost_equal(nda1, nda4)
    with testing.raises(AssertionError):
        testing.assert_ndarray_equal(nda1, nda4, decimal=7)

    # data only
    nda5 = nda1.copy()
    nda5.ito("km", force=True)
    with testing.raises(AssertionError):
        testing.assert_ndarray_equal(nda1, nda5)
    testing.assert_ndarray_equal(nda1, nda5, data_only=True)
示例#2
0
def test_invalid_key():
    meta = Meta()
    meta.readonly = False  # this is accepted`
    with raises(KeyError):
        meta['readonly'] = True  # this not because readonly is reserved
    with raises(KeyError):
        meta[
            '_data'] = True  # this not because _xxx type attributes are private
示例#3
0
def test_readonly():
    meta = Meta()
    meta.chaine = "a string"
    assert meta.chaine == 'a string'
    meta.readonly = True
    with raises(ValueError):
        meta.chaine = "a modified string"
    assert meta.chaine != "a modified string"
示例#4
0
def test_range():
    class MyClass(HasTraits):
        r = Range()  # Initialized with some default values

    c = MyClass()
    c.r = [10, 5]
    assert c.r == [5, 10]
    with raises(TraitError):
        c.r = [10, 5, 1]
示例#5
0
def test_compare(IR_dataset_1D, simple_project):
    # dataset comparison

    nd1 = IR_dataset_1D.copy()
    nd2 = nd1.copy()

    testing.assert_dataset_equal(nd1, nd2)

    nd3 = nd1.copy()
    nd3.title = 'ddd'

    with testing.raises(AssertionError):
        testing.assert_dataset_equal(nd1, nd3)

    nd4 = nd1.copy()
    nd4.data += 0.001

    with testing.raises(AssertionError):
        testing.assert_dataset_equal(nd1, nd4)

    testing.assert_dataset_almost_equal(nd1, nd4, decimal=3)

    with testing.raises(AssertionError):
        testing.assert_dataset_almost_equal(nd1, nd4, decimal=4)

    # project comparison

    proj1 = simple_project.copy()
    proj1.name = 'PROJ1'
    proj2 = proj1.copy()
    proj2.name = 'PROJ2'

    testing.assert_project_equal(proj1, proj2)

    proj3 = proj2.copy()
    proj3.add_script(Script(content='print()', name='just_a_try'))

    with testing.raises(AssertionError):
        testing.assert_project_equal(proj1, proj3)
def test_compare_coords(IR_dataset_2D):

    x1 = IR_dataset_2D.x
    x2 = x1.copy()

    assert x1.implements("LinearCoord")
    testing.assert_coord_equal(x1, x2)

    y1 = IR_dataset_2D.y
    y2 = y1.copy()

    assert y2.implements("Coord")
    testing.assert_coord_equal(y1, y2)

    # equality do depend on title for coordinates
    y3 = y1.copy()
    y3.title = "xxx"
    with testing.raises(AssertionError):
        testing.assert_coord_equal(y1, y3)

    # should have same units
    y2.ito("km", force=True)
    with testing.raises(AssertionError):
        testing.assert_coord_equal(y1, y2)

    x2.ito("km", force=True)
    with testing.raises(AssertionError):
        testing.assert_coord_equal(x1, x2)

    # almost equal coords
    x4 = x1.copy()
    x4.data += 1.0e-6
    with testing.raises(AssertionError):
        testing.assert_coord_equal(x1, x4)
    testing.assert_coord_almost_equal(x1, x4)
    with testing.raises(AssertionError):
        testing.assert_coord_equal(x1, x4, decimal=7)
def test_compare_dataset(IR_dataset_1D):
    # dataset comparison

    nd1 = IR_dataset_1D.copy()
    nd2 = nd1.copy()

    testing.assert_dataset_equal(nd1, nd2)

    nd3 = nd1.copy()
    nd3.title = "ddd"

    with testing.raises(AssertionError):
        testing.assert_dataset_equal(nd1, nd3)

    nd4 = nd1.copy()
    nd4.data += 0.001

    with testing.raises(AssertionError):
        testing.assert_dataset_equal(nd1, nd4)

    testing.assert_dataset_almost_equal(nd1, nd4, decimal=3)

    with testing.raises(AssertionError):
        testing.assert_dataset_almost_equal(nd1, nd4, decimal=4)
def test_compare_project(simple_project):
    # project comparison

    proj1 = simple_project.copy()
    proj1.name = "PROJ1"
    proj2 = proj1.copy()
    proj2.name = "PROJ2"

    testing.assert_project_equal(proj1, proj2)

    proj3 = proj2.copy()
    proj3.add_script(Script(content="print()", name="just_a_try"))

    with testing.raises(AssertionError):
        testing.assert_project_equal(proj1, proj3)
示例#9
0
def test_ndarray_methods(refarray, ndarray, ndarrayunit):
    ref = refarray
    nd = ndarray.copy()
    assert nd.data.size == ref.size
    assert nd.shape == ref.shape
    assert nd.size == ref.size
    assert nd.ndim == 2
    assert nd.data[1, 1] == ref[1, 1]
    assert nd.dims == ['y', 'x']
    assert nd.unitless  # no units
    assert not nd.dimensionless  # no unit so dimensionless has no sense

    with catch_warnings() as w:
        # try to change to an array with units
        nd.to('m')  # should not change anything (but raise a warning)
        assert w[-1].category == SpectroChemPyWarning

    assert nd.unitless

    nd.units = 'm'
    assert nd.units == ur.meter

    nd1 = nd.to('km')
    assert nd.units != ur.kilometer  # not inplace
    assert nd1.units == ur.kilometer
    nd.ito('m')
    assert nd.units == ur.meter

    # change of units - ok if it can be casted to the current one

    nd.units = 'cm'

    # cannot change to incompatible units

    with pytest.raises(TypeError):
        nd.units = 'radian'

    # we can force them

    nd.ito('radian', force=True)

    # check dimensionless and scaling

    assert 1 * nd.units == 1. * ur.dimensionless
    assert nd.units.dimensionless
    assert nd.dimensionless
    with raises(DimensionalityError):
        nd1 = nd1.ito('km/s')  # should raise an error
    nd.units = 'm/km'
    assert nd.units.dimensionless
    assert nd.units.scaling == 0.001
    nd.to(1 * ur.m, force=True)
    assert nd.dims == ['y', 'x']

    # check units compatibility

    nd.ito('m', force=True)
    nd2 = ndarray.copy()
    assert nd2.dims == ['y', 'x']
    nd2.units = 'km'
    assert nd.is_units_compatible(nd2)
    nd2.ito('radian', force=True)
    assert not nd.is_units_compatible(nd2)

    # check masking

    assert not nd.is_masked
    repr(nd)
    assert repr(nd).startswith('NDArray: ')
    nd[0] = MASKED
    assert nd.is_masked
    assert nd.dims == ['y', 'x']

    # check len and size

    assert len(nd) == ref.shape[0]
    assert nd.shape == ref.shape
    assert nd.size == ref.size
    assert nd.ndim == 2
    assert nd.dims == ['y', 'x']

    # a vector is a 1st rank tensor. Internally (it will always be represented
    # as a 1D matrix.

    v = NDArray([[1., 2., 3.]])
    assert v.ndim == 2
    assert v.shape == (1, 3)
    assert v.dims == ['y', 'x']
    assert_array_equal(v.data, np.array([[1., 2., 3.]]))

    vt = v.transpose()
    assert vt.shape == (3, 1)
    assert vt.dims == ['x', 'y']
    assert_array_equal(vt.data, np.array([[1.], [2.], [3.]]))

    # test repr

    nd = ndarrayunit.copy()
    h, w = ref.shape
    assert nd.__repr__(
    ) == f"NDArray: [float64] m.s^-1 (shape: (y:{h}, x:{w}))"
    nd[1] = MASKED
    assert nd.is_masked

    # test repr_html
    assert '<table style=\'background:transparent\'>' in nd._repr_html_()

    # test iterations

    nd = ndarrayunit.copy()
    nd[1] = MASKED

    # force units to change

    np.random.seed(12345)
    ndd = NDArray(data=np.random.random((3, 3)),
                  mask=[[True, False, False], [False, True, False],
                        [False, False, True]],
                  units='meters')

    with raises(Exception):
        ndd.to('second')
    ndd.to('second', force=True)

    # swapdims

    np.random.seed(12345)
    d = np.random.random((4, 3))
    d3 = NDArray(d,
                 units=ur.Hz,
                 mask=[[False, True, False], [False, True, False],
                       [False, True, False], [True, False,
                                              False]])  # with units & mask
    assert d3.shape == (4, 3)
    assert d3._data.shape == (4, 3)
    assert d3.dims == ['y', 'x']
    d4 = d3.swapdims(0, 1)
    assert d4.dims == ['x', 'y']
    assert d4.shape == (3, 4)
    assert d4._data.shape == (3, 4)

    # test iter
    for i, item in enumerate(ndd):
        assert item == ndd[i]

    ndz = NDArray()
    assert not list(item for item in ndz)

    assert str(ndz) == repr(ndz) == 'NDArray: empty (size: 0)'