示例#1
0
def test_sub(session):
    sess = session

    # session - session
    other = Session({'e': e, 'f': f})
    other['e'] = e - 1
    other['f'] = ones_like(f)
    diff = sess - other
    assert_array_nan_equal(diff['e'], np.full((2, 3), 1, dtype=np.int32))
    assert_array_nan_equal(diff['f'], f - ones_like(f))
    assert isnan(diff['g']).all()
    assert diff.a is a
    assert diff.a01 is a01
    assert diff.c is c

    # session - scalar
    diff = sess - 2
    assert_array_nan_equal(diff['e'], e - 2)
    assert_array_nan_equal(diff['f'], f - 2)
    assert_array_nan_equal(diff['g'], g - 2)
    assert diff.a is a
    assert diff.a01 is a01
    assert diff.c is c

    # session - dict(Array and scalar)
    other = {'e': ones_like(e), 'f': 1}
    diff = sess - other
    assert_array_nan_equal(diff['e'], e - ones_like(e))
    assert_array_nan_equal(diff['f'], f - 1)
    assert isnan(diff['g']).all()
    assert diff.a is a
    assert diff.a01 is a01
    assert diff.c is c

    # session - array
    axes = [a, b]
    other = Session([('a', a), ('a01', a01), ('c', c), ('e', ndtest((a, b))),
                     ('f', full((a, b), fill_value=3)),
                     ('g', ndtest('c=c0..c2'))])
    diff = other - ones(axes)
    assert_array_nan_equal(diff['e'], other['e'] - ones(axes))
    assert_array_nan_equal(diff['f'], other['f'] - ones(axes))
    assert_array_nan_equal(diff['g'], other['g'] - ones(axes))
    assert diff.a is a
    assert diff.a01 is a01
    assert diff.c is c
示例#2
0
def test_element_equals(session):
    session_cls = session.__class__
    other_session = session_cls([(key, value)
                                 for key, value in session.items()])

    keys = [
        key for key, value in session.items()
        if isinstance(value, (Axis, Group, Array))
    ]
    expected_res = full(Axis(keys, 'name'), fill_value=True, dtype=bool)

    # ====== same sessions ======
    res = session.element_equals(other_session)
    assert res.axes == expected_res.axes
    assert res.equals(expected_res)

    # ====== session with missing/extra items ======
    # delete some items
    for deleted_key in ['b', 'b024', 'g']:
        del other_session[deleted_key]
        expected_res[deleted_key] = False
    # add one item
    other_session['k'] = k
    expected_res = expected_res.append('name', False, label='k')

    res = session.element_equals(other_session)
    assert res.axes == expected_res.axes
    assert res.equals(expected_res)

    # ====== session with a modified array ======
    h2 = h.copy()
    h2['a1', 'b1'] = 42
    other_session['h'] = h2
    expected_res['h'] = False

    res = session.element_equals(other_session)
    assert res.axes == expected_res.axes
    assert res.equals(expected_res)
示例#3
0
def test_ne(session):
    session_cls = session.__class__
    other_session = session_cls([(key, value)
                                 for key, value in session.items()])
    expected_res = full(Axis(list(session.keys()), 'name'),
                        fill_value=False,
                        dtype=bool)

    # ====== same sessions ======
    res = session != other_session
    res = to_boolean_array_ne(res)
    assert res.axes == expected_res.axes
    assert res.equals(expected_res)

    # ====== session with missing/extra items ======
    del other_session['g']
    expected_res['g'] = True
    other_session['k'] = k
    expected_res = expected_res.append('name', True, label='k')

    res = session != other_session
    res = to_boolean_array_ne(res)
    assert res.axes == expected_res.axes
    assert res.equals(expected_res)

    # ====== session with a modified array ======
    h2 = h.copy()
    h2['a1', 'b1'] = 42
    other_session['h'] = h2
    expected_res['h'] = True

    res = session != other_session
    assert res['h'].equals(session['h'] != other_session['h'])
    res = to_boolean_array_ne(res)
    assert res.axes == expected_res.axes
    assert res.equals(expected_res)
示例#4
0
def test_create_checkedsession_instance(meta):
    # As of v1.0 of pydantic all fields with annotations (whether annotation-only or with a default value)
    # will precede all fields without an annotation. Within their respective groups, fields remain in the
    # order they were defined.
    # See https://pydantic-docs.helpmanual.io/usage/models/#field-ordering
    declared_variable_keys = ['a', 'a2', 'a01', 'c', 'e', 'g', 'f', 'h', 'b', 'b024', 'anonymous', 'ano01', 'd']

    # setting variables without default values
    cs = TestCheckedSession(a, a01, a2=a2, e=e, f=f, g=g, h=h)
    assert list(cs.keys()) == declared_variable_keys
    assert cs.b.equals(b)
    assert cs.b024.equals(b024)
    assert cs.a.equals(a)
    assert cs.a2.equals(a2)
    assert cs.anonymous.equals(anonymous)
    assert cs.a01.equals(a01)
    assert cs.ano01.equals(ano01)
    assert cs.c == c
    assert cs.d == d
    assert cs.e.equals(e)
    assert cs.g.equals(g)
    assert cs.f.equals(f)
    assert cs.h.equals(h)

    # metadata
    cs = TestCheckedSession(a, a01, a2=a2, e=e, f=f, g=g, h=h, meta=meta)
    assert cs.meta == meta

    # override default value
    b_alt = Axis('b=b0..b4')
    cs = TestCheckedSession(a, a01, b=b_alt, a2=a2, e=e, f=f, g=g, h=h)
    assert cs.b is b_alt

    # test for "NOT_LOADED" variables
    with must_warn(UserWarning, msg="No value passed for the declared variable 'a'", check_file=False):
        TestCheckedSession(a01=a01, a2=a2, e=e, f=f, g=g, h=h)
    cs = TestCheckedSession()
    assert list(cs.keys()) == declared_variable_keys
    # --- variables with default values ---
    assert cs.b.equals(b)
    assert cs.b024.equals(b024)
    assert cs.anonymous.equals(anonymous)
    assert cs.ano01.equals(ano01)
    assert cs.c == c
    assert cs.d == d
    # --- variables without default values ---
    assert isinstance(cs.a, NotLoaded)
    assert isinstance(cs.a2, NotLoaded)
    assert isinstance(cs.a01, NotLoaded)
    assert isinstance(cs.e, NotLoaded)
    assert isinstance(cs.g, NotLoaded)
    assert isinstance(cs.f, NotLoaded)
    assert isinstance(cs.h, NotLoaded)

    # passing a scalar to set all elements a CheckedArray
    cs = TestCheckedSession(a, a01, a2=a2, e=e, f=f, g=g, h=5)
    assert cs.h.axes == AxisCollection((a3, b2))
    assert cs.h.equals(full(axes=(a3, b2), fill_value=5))

    # add the undeclared variable 'i'
    with must_warn(UserWarning, f"'i' is not declared in '{cs.__class__.__name__}'", check_file=False):
        cs = TestCheckedSession(a, a01, a2=a2, i=5, e=e, f=f, g=g, h=h)
    assert list(cs.keys()) == declared_variable_keys + ['i']

    # test inheritance between checked sessions
    class TestInheritance(TestCheckedSession):
        # override variables
        b = b2
        c: int = 5
        f: CheckedArray((a3, b2), dtype=int)
        h: CheckedArray((Axis(3), Axis(2)))
        # new variables
        n0 = 'first new var'
        n1: str

    declared_variable_keys += ['n1', 'n0']
    cs = TestInheritance(a, a01, a2=a2, e=e, f=h, g=g, h=f, n1='second new var')
    assert list(cs.keys()) == declared_variable_keys
    # --- overriden variables ---
    assert cs.b.equals(b2)
    assert cs.c == 5
    assert cs.f.equals(h)
    assert cs.h.equals(f)
    # --- new variables ---
    assert cs.n0 == 'first new var'
    assert cs.n1 == 'second new var'
    # --- variables declared in the base class ---
    assert cs.b024.equals(b024)
    assert cs.a.equals(a)
    assert cs.a2.equals(a2)
    assert cs.anonymous.equals(anonymous)
    assert cs.a01.equals(a01)
    assert cs.ano01.equals(ano01)
    assert cs.d == d
    assert cs.e.equals(e)
    assert cs.g.equals(g)