def test_var(): "Test Var objects" base = Factor('aabbcde') y = Var.from_dict(base, {'a': 5, 'e': 8}, default=0) assert_array_equal(y.x, [5, 5, 0, 0, 0, 0, 8]) # basic operations info = {'a': 1} v = Var(np.arange(4.), info=info) eq_(v.info, info) w = v - 1 eq_(w.info, info) assert_array_equal(w.x, v.x - 1) w = v + 1 eq_(w.info, info) assert_array_equal(w.x, v.x + 1) w = v * 2 eq_(w.info, info) assert_array_equal(w.x, v.x * 2) w = v / 2 eq_(w.info, info) assert_array_equal(w.x, v.x / 2) # assignment tgt1 = np.arange(10) tgt2 = np.tile(np.arange(5), 2) v = Var(np.arange(10)) v[v > 4] = np.arange(5) assert_array_equal(v, tgt2) v[5:] = np.arange(5, 10) assert_array_equal(v, tgt1) v = Var(np.arange(10)) v[v > 4] = Var(np.arange(5)) assert_array_equal(v, tgt2) v[5:] = Var(np.arange(5, 10)) assert_array_equal(v, tgt1) # .split() y = Var(np.arange(16)) for i in xrange(1, 9): split = y.split(i) eq_(len(split.cells), i) # .as_factor() v = Var(np.arange(4)) assert_array_equal(v.as_factor(), Factor('0123')) assert_array_equal(v.as_factor({0: 'a'}), Factor('a123')) assert_array_equal(v.as_factor({(0, 1): 'a', (2, 3): 'b'}), Factor('aabb')) assert_array_equal(v.as_factor({ (0, 1): 'a', 2: 'b', 'default': 'c' }), Factor('aabc')) assert_array_equal(v.as_factor({ (0, 1): 'a', (2, 'default'): 'b' }), Factor('aabb'))
def test_var(): "Test Var objects" base = Factor('aabbcde') y = Var.from_dict(base, {'a': 5, 'e': 8}, default=0) assert_array_equal(y.x, [5, 5, 0, 0, 0, 0, 8]) # basic operations info = {'a': 1} v = Var(np.arange(4.), info=info) eq_(v.info, info) w = v - 1 eq_(w.info, info) assert_array_equal(w.x, v.x - 1) w = v + 1 eq_(w.info, info) assert_array_equal(w.x, v.x + 1) w = v * 2 eq_(w.info, info) assert_array_equal(w.x, v.x * 2) w = v / 2 eq_(w.info, info) assert_array_equal(w.x, v.x / 2) # assignment tgt1 = np.arange(10) tgt2 = np.tile(np.arange(5), 2) v = Var(np.arange(10)) v[v > 4] = np.arange(5) assert_array_equal(v, tgt2) v[5:] = np.arange(5, 10) assert_array_equal(v, tgt1) v = Var(np.arange(10)) v[v > 4] = Var(np.arange(5)) assert_array_equal(v, tgt2) v[5:] = Var(np.arange(5, 10)) assert_array_equal(v, tgt1) # .split() y = Var(np.arange(16)) for i in xrange(1, 9): split = y.split(i) eq_(len(split.cells), i) # .as_factor() v = Var(np.arange(4)) assert_array_equal(v.as_factor(), Factor('0123')) assert_array_equal(v.as_factor({0: 'a'}), Factor('a123')) assert_array_equal(v.as_factor({(0, 1): 'a', (2, 3): 'b'}), Factor('aabb')) assert_array_equal(v.as_factor({(0, 1): 'a', 2: 'b', 'default': 'c'}), Factor('aabc')) assert_array_equal(v.as_factor({(0, 1): 'a', (2, 'default'): 'b'}), Factor('aabb'))
def test_var(): "Test Var objects" base = Factor('aabbcde') # initialization x = np.arange(4) y = Var(x) assert_array_equal(y, x) y = Var(x, repeat=2) assert_array_equal(y, x.repeat(2)) y = Var(x, repeat=x) assert_array_equal(y, x.repeat(x)) y = Var.from_dict(base, {'a': 5, 'e': 8}, default=0) assert_array_equal(y.x, [5, 5, 0, 0, 0, 0, 8]) assert_raises(TypeError, Var, x, info=1) # basic operations info = {'a': 1} v = Var([1., 2., 3., -4.], 'v', info=info) c = 2 v2 = Var([2., 2., 3., 3.], 'w', info=info) eq_(v.info, info) for op, iop, desc in OPERATORS: target = op(v.x, c) vtarget = op(v.x, v2.x) # op if desc == '+': w = v.copy() w.x = iop(w.x, c) else: w = op(v, c) eq_(w.info, {'a': 1, 'longname': 'v %s %s' % (desc, c)}) assert_array_equal(w, target) # with Var w = op(v, v2) eq_(w.info, {'a': 1, 'longname': 'v %s w' % desc}) assert_array_equal(w, vtarget) # i-op w = v.copy() w = iop(w, c) assert_array_equal(w, target) # i-op with Var w = v.copy() w = iop(w, v2) assert_array_equal(w, vtarget) # methods w = v.abs() eq_(w.info, {'a': 1, 'longname': 'abs(v)'}) assert_array_equal(w, np.abs(v.x)) x = w.log() eq_(x.info, {'a': 1, 'longname': 'log(abs(v))'}) assert_array_equal(x, np.log(w.x)) # assignment tgt1 = np.arange(10) tgt2 = np.tile(np.arange(5), 2) v = Var(np.arange(10)) v[v > 4] = np.arange(5) assert_array_equal(v, tgt2) v[5:] = np.arange(5, 10) assert_array_equal(v, tgt1) v = Var(np.arange(10)) v[v > 4] = Var(np.arange(5)) assert_array_equal(v, tgt2) v[5:] = Var(np.arange(5, 10)) assert_array_equal(v, tgt1) # .count() v = Var([1., 2., 1.11, 2., 1.11, 4.]) assert_array_equal(v.count(), [0, 0, 0, 1, 1, 0]) # .split() y = Var(np.arange(16)) for i in range(1, 9): split = y.split(i) eq_(len(split.cells), i) # .as_factor() v = Var(np.arange(4)) assert_dataobj_equal(v.as_factor(), Factor('0123')) assert_dataobj_equal(v.as_factor({0: 'a'}), Factor(['a', '', '', ''])) assert_dataobj_equal(v.as_factor({(0, 1): 'a', (2, 3): 'b'}), Factor('aabb')) assert_dataobj_equal(v.as_factor({(0, 1): 'a', 2: 'b', 'default': 'c'}), Factor('aabc')) assert_dataobj_equal(v.as_factor({(0, 1): 'a', (2, 'default'): 'b'}), Factor('aabb'))
def test_var(): "Test Var objects" base = Factor('aabbcde') # initialization x = np.arange(4) y = Var(x) assert_array_equal(y, x) y = Var(x, repeat=2) assert_array_equal(y, x.repeat(2)) y = Var(x, repeat=x) assert_array_equal(y, x.repeat(x)) y = Var.from_dict(base, {'a': 5, 'e': 8}, default=0) assert_array_equal(y.x, [5, 5, 0, 0, 0, 0, 8]) assert_raises(TypeError, Var, x, info=1) # basic operations info = {'a': 1} v = Var([1., 2., 3., -4.], 'v', info=info) c = 2 v2 = Var([2., 2., 3., 3.], 'w', info=info) eq_(v.info, info) for op, iop, desc in OPERATORS: target = op(v.x, c) vtarget = op(v.x, v2.x) # op if desc == '+': w = v.copy() w.x = iop(w.x, c) else: w = op(v, c) eq_(w.info, {'a': 1, 'longname': 'v %s %s' % (desc, c)}) assert_array_equal(w, target) # with Var w = op(v, v2) eq_(w.info, {'a': 1, 'longname': 'v %s w' % desc}) assert_array_equal(w, vtarget) # i-op w = v.copy() w = iop(w, c) assert_array_equal(w, target) # i-op with Var w = v.copy() w = iop(w, v2) assert_array_equal(w, vtarget) # methods w = v.abs() eq_(w.info, {'a': 1, 'longname': 'abs(v)'}) assert_array_equal(w, np.abs(v.x)) x = w.log() eq_(x.info, {'a': 1, 'longname': 'log(abs(v))'}) assert_array_equal(x, np.log(w.x)) # assignment tgt1 = np.arange(10) tgt2 = np.tile(np.arange(5), 2) v = Var(np.arange(10)) v[v > 4] = np.arange(5) assert_array_equal(v, tgt2) v[5:] = np.arange(5, 10) assert_array_equal(v, tgt1) v = Var(np.arange(10)) v[v > 4] = Var(np.arange(5)) assert_array_equal(v, tgt2) v[5:] = Var(np.arange(5, 10)) assert_array_equal(v, tgt1) # .count() v = Var([1., 2., 1.11, 2., 1.11, 4.]) assert_array_equal(v.count(), [0, 0, 0, 1, 1, 0]) # .split() y = Var(np.arange(16)) for i in xrange(1, 9): split = y.split(i) eq_(len(split.cells), i) # .as_factor() v = Var(np.arange(4)) assert_dataobj_equal(v.as_factor(), Factor('0123')) assert_dataobj_equal(v.as_factor({0: 'a'}), Factor(['a', '', '', ''])) assert_dataobj_equal(v.as_factor({(0, 1): 'a', (2, 3): 'b'}), Factor('aabb')) assert_dataobj_equal(v.as_factor({(0, 1): 'a', 2: 'b', 'default': 'c'}), Factor('aabc')) assert_dataobj_equal(v.as_factor({(0, 1): 'a', (2, 'default'): 'b'}), Factor('aabb'))