def test_to_globals(session): with pytest.warns(RuntimeWarning) as caught_warnings: session.to_globals() assert len(caught_warnings) == 1 assert caught_warnings[0].message.args[0] == "Session.to_globals should usually only be used in interactive " \ "consoles and not in scripts. Use warn=False to deactivate this " \ "warning." assert caught_warnings[0].filename == __file__ assert a is session.a assert b is session.b assert c is session.c assert d is session.d assert e is session.e assert f is session.f assert g is session.g # test inplace backup_dest = e backup_value = session.e.copy() session.e = zeros_like(e) session.to_globals(inplace=True, warn=False) # check the variable is correct (the same as before) assert e is backup_dest assert e is not session.e # check the content has changed assert_array_nan_equal(e, session.e) assert not e.equals(backup_value) # reset e to its original value e[:] = backup_value
def test_to_globals(self): with pytest.warns(RuntimeWarning) as caught_warnings: self.session.to_globals() assert len(caught_warnings) == 1 assert caught_warnings[0].message.args[0] == "Session.to_globals should usually only be used in interactive " \ "consoles and not in scripts. Use warn=False to deactivate this " \ "warning." assert caught_warnings[0].filename == __file__ self.assertIs(a, self.a) self.assertIs(b, self.b) self.assertIs(c, self.c) self.assertIs(d, self.d) self.assertIs(e, self.e) self.assertIs(f, self.f) self.assertIs(g, self.g) # test inplace backup_dest = e backup_value = self.session.e.copy() self.session.e = zeros_like(e) self.session.to_globals(inplace=True, warn=False) # check the variable is correct (the same as before) self.assertIs(e, backup_dest) self.assertIsNot(e, self.session.e) # check the content has changed assert_array_nan_equal(e, self.session.e) self.assertFalse(larray_equal(e, backup_value))
def test_to_globals(session): msg = "Session.to_globals should usually only be used in interactive consoles and not in scripts. " \ "Use warn=False to deactivate this warning." with must_warn(RuntimeWarning, msg=msg): session.to_globals() assert a is session.a assert b is session.b assert c is session.c assert d is session.d assert e is session.e assert f is session.f assert g is session.g # test inplace backup_dest = e backup_value = session.e.copy() session.e = zeros_like(e) session.to_globals(inplace=True, warn=False) # check the variable is correct (the same as before) assert e is backup_dest assert e is not session.e # check the content has changed assert_array_nan_equal(e, session.e) assert not e.equals(backup_value) # reset e to its original value e[:] = backup_value
def test_setattr_cs(checkedsession): cs = checkedsession # only change values of an array -> OK cs.h = zeros_like(h) # trying to add an undeclared variable -> prints a warning message with must_warn(UserWarning, msg=f"'i' is not declared in '{cs.__class__.__name__}'"): cs.i = ndtest((3, 3)) # trying to set a variable with an object of different type -> should fail # a) type given explicitly # -> Axis with must_raise(TypeError, msg="instance of Axis expected"): cs.a = 0 # -> CheckedArray with must_raise(TypeError, msg="Expected object of type 'Array' or a scalar for the variable 'h' but got " "object of type 'ndarray'"): cs.h = h.data # b) type deduced from the given default value with must_raise(TypeError, msg="instance of Axis expected"): cs.b = ndtest((3, 3)) # trying to set a CheckedArray variable using a scalar -> OK cs.h = 5 # trying to set a CheckedArray variable using an array with axes in different order -> OK cs.h = h.transpose() assert cs.h.axes.names == h.axes.names # broadcasting (missing axis) is allowed cs.h = ndtest(a3) assert_array_nan_equal(cs.h['b0'], cs.h['b1']) # trying to set a CheckedArray variable using an array with wrong axes -> should fail # a) extra axis with must_raise(ValueError, msg="Array 'h' was declared with axes {a, b} but got array with axes {a, b, c} " "(unexpected {c} axis)"): cs.h = ndtest((a3, b2, 'c=c0..c2')) # b) incompatible axis msg = """\ Incompatible axis for array 'h': Axis(['a0', 'a1', 'a2', 'a3', 'a4'], 'a') vs Axis(['a0', 'a1', 'a2', 'a3'], 'a')""" with must_raise(ValueError, msg=msg): cs.h = h.append('a', 0, 'a4')
def test_sub_cs(checkedsession): cs = checkedsession session_cls = cs.__class__ # session - session other = session_cls(a=a, a2=a2, a01=a01, e=e - 1, g=zeros_like(g), f=zeros_like(f), h=ones_like(h)) diff = cs - other assert isinstance(diff, session_cls) # --- non-array variables --- assert diff.b is b assert diff.b024 is b024 assert diff.a is a assert diff.a2 is a2 assert diff.anonymous is anonymous assert diff.a01 is a01 assert diff.ano01 is ano01 assert diff.c is c assert diff.d is d # --- array variables --- assert_array_nan_equal(diff.e, np.full((2, 3), 1, dtype=np.int32)) assert_array_nan_equal(diff.g, g) assert_array_nan_equal(diff.f, f) assert_array_nan_equal(diff.h, h - ones_like(h)) # session - scalar diff = cs - 2 assert isinstance(diff, session_cls) # --- non-array variables --- assert diff.b is b assert diff.b024 is b024 assert diff.a is a assert diff.a2 is a2 assert diff.anonymous is anonymous assert diff.a01 is a01 assert diff.ano01 is ano01 assert diff.c is c assert diff.d is d # --- non constant arrays --- assert_array_nan_equal(diff.e, e - 2) assert_array_nan_equal(diff.g, g - 2) assert_array_nan_equal(diff.f, f - 2) assert_array_nan_equal(diff.h, h - 2) # session - dict(Array and scalar) other = {'e': ones_like(e), 'h': 1} diff = cs - other assert isinstance(diff, session_cls) # --- non-array variables --- assert diff.b is b assert diff.b024 is b024 assert diff.a is a assert diff.a2 is a2 assert diff.anonymous is anonymous assert diff.a01 is a01 assert diff.ano01 is ano01 assert diff.c is c assert diff.d is d # --- non constant arrays --- assert_array_nan_equal(diff.e, e - ones_like(e)) assert isnan(diff.g).all() assert isnan(diff.f).all() assert_array_nan_equal(diff.h, h - 1) # session - array axes = cs.h.axes cs.e = ndtest(axes) cs.g = ones_like(cs.h) diff = cs - ones(axes) assert isinstance(diff, session_cls) # --- non-array variables --- assert diff.b is b assert diff.b024 is b024 assert diff.a is a assert diff.a2 is a2 assert diff.anonymous is anonymous assert diff.a01 is a01 assert diff.ano01 is ano01 assert diff.c is c assert diff.d is d # --- non constant arrays --- assert_array_nan_equal(diff.e, cs.e - ones(axes)) assert_array_nan_equal(diff.g, cs.g - ones(axes)) assert isnan(diff.f).all() assert_array_nan_equal(diff.h, cs.h - ones(axes))