def test_or_lset(): # without axis assert LSet(['a', 'b']) | LSet(['c', 'd']) == LSet(['a', 'b', 'c', 'd']) assert LSet(['a', 'b', 'c']) | LSet(['c', 'd']) == LSet( ['a', 'b', 'c', 'd']) alpha = Axis('alpha=a,b,c,d') res = alpha['a', 'b'].set() | alpha['c', 'd'].set() assert res.axis is alpha assert res == alpha['a', 'b', 'c', 'd'].set() assert alpha['a', 'b', 'c'].set() | alpha['c', 'd'].set() == alpha['a', 'b', 'c', 'd'].set() alpha = Axis('alpha=a,b,c,d') res = alpha['a', 'b'].set() | alpha['c', 'd'] assert res.axis is alpha assert res == alpha['a', 'b', 'c', 'd'].set() assert alpha['a', 'b', 'c'].set() | alpha['c', 'd'] == alpha['a', 'b', 'c', 'd'].set() alpha = Axis('alpha=a,b,c,d') res = alpha['a', 'b'].set().named('ab') | alpha['c', 'd'].set().named('cd') assert res.axis is alpha assert res.name == 'ab | cd' assert res == alpha['a', 'b', 'c', 'd'].set() assert alpha['a', 'b', 'c'].set() | alpha['c', 'd'] == alpha['a', 'b', 'c', 'd'].set() num = Axis(range(10), 'num') assert num[1, 5, 3].set() | 4 == num[1, 5, 3, 4].set() assert num[1, 5, 3].set() | num[4] == num[1, 5, 3, 4].set() assert num[4].set() | num[1, 5, 3] == num[4, 1, 5, 3].set() assert num[:2].set() | num[8:] == num[0, 1, 2, 8, 9].set() assert num[:2].set() | num[5] == num[0, 1, 2, 5].set()
def test_init_lgroup(lgroups): assert lgroups.slice_both_named_wh_named_axis.name == "full" assert lgroups.slice_both_named_wh_named_axis.key == slice(1, 5, None) assert lgroups.slice_both_named_wh_named_axis.axis is age assert lgroups.slice_both_named.name == "named" assert lgroups.slice_both_named.key == slice(1, 5, None) assert lgroups.slice_both.key == slice(1, 5, None) assert lgroups.slice_start.key == slice(1, None, None) assert lgroups.slice_stop.key == slice(None, 5, None) assert lgroups.slice_none_no_axis.key == slice(None, None, None) assert lgroups.slice_none_wh_named_axis.axis is lipro assert lgroups.slice_none_wh_anonymous_axis.axis is anonymous assert lgroups.single_value.key == 'P03' assert lgroups.list.key == ['P01', 'P03', 'P04'] group = LGroup('1:5', age, age) assert group.name == age.name group = age['1:5'] >> age assert group.name == age.name group2 = LGroup('1', axis=age) group = LGroup('1', group2, axis=age) assert group.name == '1' group = age['1'] >> group2 assert group.name == '1' group2 = LGroup('1:5', 'age', age) group = LGroup('1:5', group2, axis=age) assert group.name == group2.name group = age['1:5'] >> group2 assert group.name == group2.name axis = Axis('axis=a,a0..a3,b,b0..b3,c,c0..c3') for code in axis.matching(regex='^.$'): group = axis.startingwith(code) >> code assert group.equals(axis.startingwith(code) >> str(code))
def test_contains(self): col = self.collection self.assertTrue('lipro' in col) self.assertFalse('nonexisting' in col) self.assertTrue(0 in col) self.assertTrue(1 in col) self.assertTrue(2 in col) self.assertTrue(-1 in col) self.assertTrue(-2 in col) self.assertTrue(-3 in col) self.assertFalse(3 in col) # objects actually in col self.assertTrue(self.lipro in col) self.assertTrue(self.sex in col) self.assertTrue(self.age in col) # other axis with the same name self.assertTrue(self.sex2 in col) self.assertFalse(self.geo in col) self.assertFalse(self.value in col) # test anonymous axes anon = Axis([0, 1]) col.append(anon) self.assertTrue(anon in col) # different object, same values anon2 = anon.copy() self.assertTrue(anon2 in col) # different values anon3 = Axis([0, 2]) self.assertFalse(anon3 in col)
def test_index(col): assert col.index('lipro') == 0 with pytest.raises(ValueError): col.index('nonexisting') assert col.index(0) == 0 assert col.index(1) == 1 assert col.index(2) == 2 assert col.index(-1) == -1 assert col.index(-2) == -2 assert col.index(-3) == -3 with pytest.raises(ValueError): col.index(3) # objects actually in col assert col.index(lipro) == 0 assert col.index(sex) == 1 assert col.index(age) == 2 assert col.index(sex2) == 1 with pytest.raises(ValueError): col.index(geo) with pytest.raises(ValueError): col.index(value) # test anonymous axes anon = Axis([0, 1]) col.append(anon) assert col.index(anon) == 3 anon2 = anon.copy() assert col.index(anon2) == 3 anon3 = Axis([0, 2]) with pytest.raises(ValueError): col.index(anon3)
def test_add(self): col = self.collection.copy() lipro, sex, age = self.lipro, self.sex, self.age geo, value = self.geo, self.value # 1) list # a) no dupe new = col + [self.geo, value] self.assertEqual(new, [lipro, sex, age, geo, value]) # check the original has not been modified self.assertEqual(col, self.collection) # b) with compatible dupe # the "new" age axis is ignored (because it is compatible) new = col + [Axis('geo=A11,A12,A13'), Axis('age=0..7')] self.assertEqual(new, [lipro, sex, age, geo]) # c) with incompatible dupe # XXX: the "new" age axis is ignored. We might want to ignore it if it # is the same but raise an exception if it is different with self.assertRaises(ValueError): col + [Axis('geo=A11,A12,A13'), Axis('age=0..6')] # 2) other AxisCollection new = col + AxisCollection([geo, value]) self.assertEqual(new, [lipro, sex, age, geo, value])
def test_add(session): i = Axis('i=i0..i2') i01 = i['i0,i1'] >> 'i01' session.add(i, i01, j='j') assert i.equals(session.i) assert i01 == session.i01 assert session.j == 'j'
def test_add(session): h = Axis('h=h0..h2') h01 = h['h0,h1'] >> 'h01' session.add(h, h01, i='i') assert h.equals(session.h) assert h01 == session.h01 assert session.i == 'i'
def test_index(col): assert col.index('lipro') == 0 with pytest.raises(ValueError): col.index('nonexisting') assert col.index(0) == 0 assert col.index(1) == 1 assert col.index(2) == 2 assert col.index(-1) == -1 assert col.index(-2) == -2 assert col.index(-3) == -3 with pytest.raises(ValueError): col.index(3) # objects actually in col assert col.index(lipro) == 0 assert col.index(sex) == 1 assert col.index(age) == 2 assert col.index(sex2) == 1 with pytest.raises(ValueError): col.index(geo) with pytest.raises(ValueError): col.index(value) # test anonymous axes anon = Axis([0, 1]) col.append(anon) assert col.index(anon) == 3 anon2 = anon.copy() assert col.index(anon2) == 3 anon3 = Axis([0, 2]) with pytest.raises(ValueError): col.index(anon3)
def test_init_lgroup(lgroups): assert lgroups.slice_both_named_wh_named_axis.name == "full" assert lgroups.slice_both_named_wh_named_axis.key == slice(1, 5, None) assert lgroups.slice_both_named_wh_named_axis.axis is age assert lgroups.slice_both_named.name == "named" assert lgroups.slice_both_named.key == slice(1, 5, None) assert lgroups.slice_both.key == slice(1, 5, None) assert lgroups.slice_start.key == slice(1, None, None) assert lgroups.slice_stop.key == slice(None, 5, None) assert lgroups.slice_none_no_axis.key == slice(None, None, None) assert lgroups.slice_none_wh_named_axis.axis is lipro assert lgroups.slice_none_wh_anonymous_axis.axis is anonymous assert lgroups.single_value.key == 'P03' assert lgroups.list.key == ['P01', 'P03', 'P04'] group = LGroup('1:5', age, age) assert group.name == age.name group = age['1:5'] >> age assert group.name == age.name group2 = LGroup('1', axis=age) group = LGroup('1', group2, axis=age) assert group.name == '1' group = age['1'] >> group2 assert group.name == '1' group2 = LGroup('1:5', 'age', age) group = LGroup('1:5', group2, axis=age) assert group.name == group2.name group = age['1:5'] >> group2 assert group.name == group2.name axis = Axis('axis=a,a0..a3,b,b0..b3,c,c0..c3') for code in axis.matching('^.$'): group = axis.startingwith(code) >> code assert group.equals(axis.startingwith(code) >> str(code))
def test_ipfp(self): a = Axis(2, 'a') b = Axis(2, 'b') initial = LArray([[2, 1], [1, 2]], [a, b]) # array sums already match target sums # [3, 3], [3, 3] r = ipfp([initial.sum(a), initial.sum(b)], initial) assert_array_equal(r, [[2, 1], [1, 2]]) # array sums do not match target sums (ie the usual case) along_a = LArray([2, 1], b) along_b = LArray([1, 2], a) r = ipfp([along_a, along_b], initial) assert_array_equal(r, [[0.8, 0.2], [1.0, 1.0]]) # same as above but using a more precise threshold r = ipfp([along_a, along_b], initial, threshold=0.01) assert_array_equal(r, [[0.8450704225352113, 0.15492957746478875], [1.1538461538461537, 0.8461538461538463]]) # inverted target sums with self.assertRaisesRegexp( ValueError, "axes of target sum along axis 0 \(a\) do not match corresponding " "array axes: got {a\*} but expected {b\*}. Are the target sums in the " "correct order\?"): ipfp([along_b, along_a], initial, threshold=0.01)
def test_init(self): sex_tuple = ('M', 'F') sex_list = ['M', 'F'] sex_array = np.array(sex_list) # wildcard axis axis = Axis(10, 'axis') assert len(axis) == 10 assert list(axis.labels) == list(range(10)) # tuple of strings assert_array_equal(Axis(sex_tuple, 'sex').labels, sex_array) # list of strings assert_array_equal(Axis(sex_list, 'sex').labels, sex_array) # array of strings assert_array_equal(Axis(sex_array, 'sex').labels, sex_array) # single string assert_array_equal(Axis('sex=M,F').labels, sex_array) # list of ints assert_array_equal(Axis(range(116), 'age').labels, np.arange(116)) # range-string axis = Axis('0..115', 'age') assert_array_equal(axis.labels, np.arange(116)) # another axis group group = axis[:10] group_axis = Axis(group) assert_array_equal(group_axis.labels, np.arange(11)) assert_array_equal(group_axis.name, 'age') # another axis as labels argument other = Axis('other=0..10') axis = Axis(other, 'age') assert_array_equal(axis.labels, other.labels) assert_array_equal(axis.name, 'age')
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
def test_aslarray(self): with open_excel(visible=False) as wb: sheet = wb[0] arr1 = ndtest([Axis(2), Axis(3)]) # no header so that we have an uniform dtype for the whole sheet sheet['A1'] = arr1 res1 = aslarray(sheet['A1:C2']) assert res1.equals(arr1) assert res1.dtype == arr1.dtype
def test_ipfp_no_values(): # 6, 12, 18 along_a = ndtest([(3, 'b')], start=1) * 6 # 6, 12, 18 along_b = ndtest([(3, 'a')], start=1) * 6 r = ipfp([along_a, along_b]) assert_array_equal(r, [[1.0, 2.0, 3.0], [2.0, 4.0, 6.0], [3.0, 6.0, 9.0]]) along_a = Array([2, 1], Axis(2, 'b')) along_b = Array([1, 2], Axis(2, 'a')) r = ipfp([along_a, along_b]) assert_array_equal(r, [[2 / 3, 1 / 3], [4 / 3, 2 / 3]])
def test_add(col): col2 = col.copy() new = col2 + [geo, value] assert new == [lipro, sex, age, geo, value] assert col2 == col new = col2 + [Axis('geo=A11,A12,A13'), Axis('age=0..7')] assert new == [lipro, sex, age, geo] with pytest.raises(ValueError): col2 + [Axis('geo=A11,A12,A13'), Axis('age=0..6')] # 2) other AxisCollection new = col2 + AxisCollection([geo, value]) assert new == [lipro, sex, age, geo, value]
class TestCheckedSession(CheckedSession): b = b b024 = b024 a: Axis a2: Axis anonymous = anonymous a01: Group ano01 = ano01 c: str = c d = d e: Array g: Array f: CheckedArray((Axis(3), Axis(2))) h: CheckedArray((a3, b2), dtype=int)
def test_sub(): col = AxisCollection('a0,a1;b0,b1,b2') res = col - Axis(2) assert res == AxisCollection('b0,b1,b2') res = col - Axis(3) assert res == AxisCollection('a0,a1') col = AxisCollection('a0,a1;b0,b1') # when several axes are compatible, remove first res = col - Axis(2) assert res == AxisCollection('b0,b1') # when no axis is compatible, do not remove any res = col - Axis(3) assert res == col
def test_getitem_lgroup_keys(self): def group_equal(g1, g2): return (g1.key == g2.key and g1.name == g2.name and g1.axis is g2.axis) age = Axis(range(100), 'age') ages = [1, 5, 9] val_only = LGroup(ages) self.assertTrue(group_equal(age[val_only], LGroup(ages, axis=age))) self.assertTrue( group_equal(age[val_only] >> 'a_name', LGroup(ages, 'a_name', axis=age))) val_name = LGroup(ages, 'val_name') self.assertTrue( group_equal(age[val_name], LGroup(ages, 'val_name', age))) self.assertTrue( group_equal(age[val_name] >> 'a_name', LGroup(ages, 'a_name', age))) val_axis = LGroup(ages, axis=age) self.assertTrue(group_equal(age[val_axis], LGroup(ages, axis=age))) self.assertTrue( group_equal(age[val_axis] >> 'a_name', LGroup(ages, 'a_name', axis=age))) val_axis_name = LGroup(ages, 'val_axis_name', age) self.assertTrue( group_equal(age[val_axis_name], LGroup(ages, 'val_axis_name', age))) self.assertTrue( group_equal(age[val_axis_name] >> 'a_name', LGroup(ages, 'a_name', age)))
def test_add_cs(checkedsession): cs = checkedsession test_add(cs) u = Axis('u=u0..u2') with must_warn(UserWarning, msg=f"'u' is not declared in '{cs.__class__.__name__}'", check_file=False): cs.add(u)
def _test_io(tmpdir, session, meta, engine, ext): filename = f"test_{engine}.{ext}" if 'csv' not in engine else f"test_{engine}{ext}" fpath = tmp_path(tmpdir, filename) is_excel_or_csv = 'excel' in engine or 'csv' in engine kind = Array if is_excel_or_csv else (Axis, Group, Array) + _supported_scalars_types session = session.filter(kind=kind) session.meta = meta # save and load session.save(fpath, engine=engine) s = Session() s.load(fpath, engine=engine) # use Session.names instead of Session.keys because CSV, Excel and HDF do *not* keep ordering assert s.names == session.names assert s.equals(session) if not is_excel_or_csv: for key in s.filter(kind=Axis).keys(): assert s[key].dtype == session[key].dtype if engine != 'pandas_excel': assert s.meta == meta # update a Group + an Axis + an array (overwrite=False) a4 = Axis('a=0..3') a4_01 = a3['0,1'] >> 'a01' e2 = ndtest((a4, 'b=b0..b2')) h2 = full_like(h, fill_value=10) Session(a=a4, a01=a4_01, e=e2, h=h2).save(fpath, overwrite=False, engine=engine) s = Session() s.load(fpath, engine=engine) if engine == 'pandas_excel': # Session.save() via engine='pandas_excel' always overwrite the output Excel files assert s.names == ['e', 'h'] elif is_excel_or_csv: assert s.names == ['e', 'f', 'g', 'h'] else: assert s.names == session.names assert s['a'].equals(a4) assert s['a01'].equals(a4_01) assert_array_nan_equal(s['e'], e2) if engine != 'pandas_excel': assert s.meta == meta # load only some objects session.save(fpath, engine=engine) s = Session() names_to_load = ['e', 'f'] if is_excel_or_csv else [ 'a', 'a01', 'a2', 'anonymous', 'e', 'f', 's_bool', 's_int' ] s.load(fpath, names=names_to_load, engine=engine) assert s.names == names_to_load if engine != 'pandas_excel': assert s.meta == meta return fpath
def setUp(self): self.age = Axis('age=0..10') self.lipro = Axis('lipro=P01..P05') self.anonymous = Axis(range(3)) self.slice_both_named_wh_named_axis = LGroup('1:5', "full", self.age) self.slice_both_named = LGroup('1:5', "named") self.slice_both = LGroup('1:5') self.slice_start = LGroup('1:') self.slice_stop = LGroup(':5') self.slice_none_no_axis = LGroup(':') self.slice_none_wh_named_axis = LGroup(':', axis=self.lipro) self.slice_none_wh_anonymous_axis = LGroup(':', axis=self.anonymous) self.single_value = LGroup('P03') self.list = LGroup('P01,P03,P04') self.list_named = LGroup('P01,P03,P04', "P134")
def test_and_lset(): # without axis assert LSet(['a', 'b', 'c']) & LSet(['c', 'd']) == LSet(['c']) alpha = Axis('alpha=a,b,c,d') res = alpha['a', 'b', 'c'].named('abc').set() & alpha['c', 'd'].named('cd') assert res.axis is alpha assert res.name == 'abc & cd' assert res == alpha[['c']].set()
def test_positional(self): age = Axis('age=0..115') # these are NOT equivalent (not translated until used in an LArray # self.assertEqual(age.i[:17], age[':17']) key = age.i[:-1] self.assertEqual(key.key, slice(None, -1)) self.assertIs(key.axis, age)
def test_getitem(self): age = Axis('age=0..10') # a tuple a159 = age[1, 5, 9] self.assertEqual(a159.key, [1, 5, 9]) self.assertIs(a159.name, None) self.assertIs(a159.axis, age) # a normal list a159 = age[[1, 5, 9]] self.assertEqual(a159.key, [1, 5, 9]) self.assertIs(a159.name, None) self.assertIs(a159.axis, age) # a string list a159 = age['1,5,9'] self.assertEqual(a159.key, [1, 5, 9]) self.assertIs(a159.name, None) self.assertIs(a159.axis, age) # a normal slice a10to20 = age[5:9] self.assertEqual(a10to20.key, slice(5, 9)) self.assertIs(a10to20.axis, age) # a string slice a10to20 = age['5:9'] self.assertEqual(a10to20.key, slice(5, 9)) self.assertIs(a10to20.axis, age) # with name group = age[[1, 5, 9]] >> 'test' self.assertEqual(group.key, [1, 5, 9]) self.assertEqual(group.name, 'test') self.assertIs(group.axis, age) # all group = age[:] >> 'all' self.assertEqual(group.key, slice(None)) self.assertIs(group.axis, age) # an axis age2 = Axis('age=0..5') group = age[age2] assert list(group.key) == list(age2.labels)
def test_and(self): # without axis self.assertEqual(LSet(['a', 'b', 'c']) & LSet(['c', 'd']), LSet(['c'])) # with axis & name alpha = Axis('alpha=a,b,c,d') res = alpha['a', 'b', 'c'].named('abc').set() & alpha['c', 'd'].named('cd') self.assertIs(res.axis, alpha) self.assertEqual(res.name, 'abc & cd') self.assertEqual(res, alpha[['c']].set())
def setUp(self): self.a = Axis([], 'a') self.b = Axis([], 'b') self.c = 'c' self.d = {} self.e = ndrange([(2, 'a0'), (3, 'a1')]) self.e2 = ndrange(('a=a0..a2', 'b=b0..b2')) self.f = ndrange([(3, 'a0'), (2, 'a1')]) self.g = ndrange([(2, 'a0'), (4, 'a1')]) self.session = Session([ ('b', self.b), ('a', self.a), ('c', self.c), ('d', self.d), ('e', self.e), ('g', self.g), ('f', self.f), ])
def test_getitem(self): axis = Axis("a=a0,a1") assert axis.i[0][0] == 'a' assert axis.i[0][1] == '0' assert axis.i[0:1][1] == 'a1' assert axis.i[:][1] == 'a1' assert list(axis.i[:][0:2]) == ['a0', 'a1'] assert list((axis.i[:][[1, 0]])) == ['a1', 'a0'] assert axis.i[[0, 1, 0]][2] == 'a0' assert axis.i[(0, 1, 0)][2] == 'a0'
def test_or(self): # without axis self.assertEqual( LSet(['a', 'b']) | LSet(['c', 'd']), LSet(['a', 'b', 'c', 'd'])) self.assertEqual( LSet(['a', 'b', 'c']) | LSet(['c', 'd']), LSet(['a', 'b', 'c', 'd'])) # with axis (pure) alpha = Axis('alpha=a,b,c,d') res = alpha['a', 'b'].set() | alpha['c', 'd'].set() self.assertIs(res.axis, alpha) self.assertEqual(res, alpha['a', 'b', 'c', 'd'].set()) self.assertEqual(alpha['a', 'b', 'c'].set() | alpha['c', 'd'].set(), alpha['a', 'b', 'c', 'd'].set()) # with axis (mixed) alpha = Axis('alpha=a,b,c,d') res = alpha['a', 'b'].set() | alpha['c', 'd'] self.assertIs(res.axis, alpha) self.assertEqual(res, alpha['a', 'b', 'c', 'd'].set()) self.assertEqual(alpha['a', 'b', 'c'].set() | alpha['c', 'd'], alpha['a', 'b', 'c', 'd'].set()) # with axis & name alpha = Axis('alpha=a,b,c,d') res = alpha['a', 'b'].set().named('ab') | alpha['c', 'd'].set().named('cd') self.assertIs(res.axis, alpha) self.assertEqual(res.name, 'ab | cd') self.assertEqual(res, alpha['a', 'b', 'c', 'd'].set()) self.assertEqual(alpha['a', 'b', 'c'].set() | alpha['c', 'd'], alpha['a', 'b', 'c', 'd'].set()) # numeric axis num = Axis(range(10), 'num') # single int self.assertEqual(num[1, 5, 3].set() | 4, num[1, 5, 3, 4].set()) self.assertEqual(num[1, 5, 3].set() | num[4], num[1, 5, 3, 4].set()) self.assertEqual(num[4].set() | num[1, 5, 3], num[4, 1, 5, 3].set()) # slices self.assertEqual(num[:2].set() | num[8:], num[0, 1, 2, 8, 9].set()) self.assertEqual(num[:2].set() | num[5], num[0, 1, 2, 5].set())
def test_equals(): a = Axis('a=a0..a2') # keys are single value assert a['a0'].equals(a['a0']) # keys are arrays assert a[np.array(['a0', 'a2'])].equals(a[np.array(['a0', 'a2'])]) # axis names anonymous_axis = Axis('a0..a2') assert anonymous_axis['a0:a2'].equals(anonymous_axis['a0:a2']) assert not a['a0:a2'].equals(anonymous_axis['a0:a2']) assert not a['a0:a2'].equals(a.rename('other_name')['a0:a2']) # list of labels a02 = a['a0,a1,a2'] >> 'group' assert a02.equals(a02) a02_unamed = a['a0,a1,a2'] assert not a02.equals(a02_unamed) a13 = a['a1,a2,a3'] >> 'group' assert not a02.equals(a13) a02_other_axis_name = a.rename('other_name')['a0,a1,a2'] >> 'group' assert not a02.equals(a02_other_axis_name)
def test_equals(): a = Axis('a=a0..a2') # keys are single value assert a['a0'].equals(a['a0']) # keys are arrays assert a[np.array(['a0', 'a2'])].equals(a[np.array(['a0', 'a2'])]) # axis names anonymous_axis = Axis('a0..a2') assert anonymous_axis['a0:a2'].equals(anonymous_axis['a0:a2']) assert not a['a0:a2'].equals(anonymous_axis['a0:a2']) assert not a['a0:a2'].equals(a.rename('other_name')['a0:a2']) # list of labels a02 = a['a0,a1,a2'] >> 'group' assert a02.equals(a02) a02_unamed = a['a0,a1,a2'] assert not a02.equals(a02_unamed) a13 = a['a1,a2,a3'] >> 'group' assert not a02.equals(a13) a02_other_axis_name = a.rename('other_name')['a0,a1,a2'] >> 'group' assert not a02.equals(a02_other_axis_name)
def test_getitem(self): axis = Axis("a=a0,a1") assert axis['a0'][0] == 'a' assert axis['a0'][1] == '0' assert axis['a0':'a1'][1] == 'a1' assert axis[:][1] == 'a1' assert list(axis[:][0:2]) == ['a0', 'a1'] assert list((axis[:][[1, 0]])) == ['a1', 'a0'] assert axis[['a0', 'a1', 'a0']][2] == 'a0' assert axis[('a0', 'a1', 'a0')][2] == 'a0' assert axis[ndtest("a=a0,a1,a0")][2] == 2
def __init__(self): self.code_axis = Axis('code=a0..a4') self.slice_both_named = self.code_axis.i[1:4] >> 'a123' self.slice_both = self.code_axis.i[1:4] self.slice_start = self.code_axis.i[1:] self.slice_stop = self.code_axis.i[:4] self.slice_none = self.code_axis.i[:] self.first_value = self.code_axis.i[0] self.last_value = self.code_axis.i[-1] self.list = self.code_axis.i[[0, 1, -2, -1]] self.tuple = self.code_axis.i[0, 1, -2, -1]
def test_init(self): self.assertEqual(self.slice_both_named_wh_named_axis.name, "full") self.assertEqual(self.slice_both_named_wh_named_axis.key, slice(1, 5, None)) self.assertIs(self.slice_both_named_wh_named_axis.axis, self.age) self.assertEqual(self.slice_both_named.name, "named") self.assertEqual(self.slice_both_named.key, slice(1, 5, None)) self.assertEqual(self.slice_both.key, slice(1, 5, None)) self.assertEqual(self.slice_start.key, slice(1, None, None)) self.assertEqual(self.slice_stop.key, slice(None, 5, None)) self.assertEqual(self.slice_none_no_axis.key, slice(None, None, None)) self.assertIs(self.slice_none_wh_named_axis.axis, self.lipro) self.assertIs(self.slice_none_wh_anonymous_axis.axis, self.anonymous) self.assertEqual(self.single_value.key, 'P03') self.assertEqual(self.list.key, ['P01', 'P03', 'P04']) # passing an axis as name group = LGroup('1:5', self.age, self.age) assert group.name == self.age.name group = self.age['1:5'] >> self.age assert group.name == self.age.name # passing an unnamed group as name group2 = LGroup('1', axis=self.age) group = LGroup('1', group2, axis=self.age) assert group.name == '1' group = self.age['1'] >> group2 assert group.name == '1' # passing a named group as name group2 = LGroup('1:5', 'age', self.age) group = LGroup('1:5', group2, axis=self.age) assert group.name == group2.name group = self.age['1:5'] >> group2 assert group.name == group2.name # additional test axis = Axis('axis=a,a0..a3,b,b0..b3,c,c0..c3') for code in axis.matching('^.$'): group = axis.startingwith(code) >> code assert group == axis.startingwith(code) >> str(code)
def setUp(self): self.lipro = Axis('lipro=P01..P04') self.sex = Axis('sex=M,F') self.sex2 = Axis('sex=F,M') self.age = Axis('age=0..7') self.geo = Axis('geo=A11,A12,A13') self.value = Axis('value=0..10') self.collection = AxisCollection((self.lipro, self.sex, self.age))
def test_contains(col): assert 'lipro' in col assert not ('nonexisting' in col) assert 0 in col assert 1 in col assert 2 in col assert -1 in col assert -2 in col assert -3 in col assert not (3 in col) assert lipro in col assert sex in col assert age in col assert sex2 in col assert not (geo in col) assert not (value in col) anon = Axis([0, 1]) col.append(anon) assert anon in col anon2 = anon.copy() assert anon2 in col anon3 = Axis([0, 2]) assert not (anon3 in col)
def test_matching(): sutcode = Axis(['A23', 'A2301', 'A25', 'A2501'], 'sutcode') assert sutcode.matching('^...$') == LGroup(['A23', 'A25']) assert sutcode.startingwith('A23') == LGroup(['A23', 'A2301']) assert sutcode.endingwith('01') == LGroup(['A2301', 'A2501'])
def test_h5_io(tmpdir): age = Axis('age=0..10') lipro = Axis('lipro=P01..P05') anonymous = Axis(range(3)) wildcard = Axis(3, 'wildcard') fpath = os.path.join(str(tmpdir), 'axes.h5') # ---- default behavior ---- # int axis age.to_hdf(fpath) age2 = read_hdf(fpath, key=age.name) assert age.equals(age2) # string axis lipro.to_hdf(fpath) lipro2 = read_hdf(fpath, key=lipro.name) assert lipro.equals(lipro2) # anonymous axis with pytest.raises(ValueError, message="Argument key must be provided explicitly in case of anonymous axis"): anonymous.to_hdf(fpath) # wildcard axis wildcard.to_hdf(fpath) wildcard2 = read_hdf(fpath, key=wildcard.name) assert wildcard2.iswildcard assert wildcard.equals(wildcard2) # ---- specific key ---- # int axis key = 'axis_age' age.to_hdf(fpath, key) age2 = read_hdf(fpath, key=key) assert age.equals(age2) # string axis key = 'axis_lipro' lipro.to_hdf(fpath, key) lipro2 = read_hdf(fpath, key=key) assert lipro.equals(lipro2) # anonymous axis key = 'axis_anonymous' anonymous.to_hdf(fpath, key) anonymous2 = read_hdf(fpath, key=key) assert anonymous2.name is None assert_array_equal(anonymous.labels, anonymous2.labels) # wildcard axis key = 'axis_wildcard' wildcard.to_hdf(fpath, key) wildcard2 = read_hdf(fpath, key=key) assert wildcard2.iswildcard assert wildcard.equals(wildcard2) # ---- specific hdf group + key ---- hdf_group = 'my_axes' # int axis key = hdf_group + '/axis_age' age.to_hdf(fpath, key) age2 = read_hdf(fpath, key=key) assert age.equals(age2) # string axis key = hdf_group + '/axis_lipro' lipro.to_hdf(fpath, key) lipro2 = read_hdf(fpath, key=key) assert lipro.equals(lipro2) # anonymous axis key = hdf_group + '/axis_anonymous' anonymous.to_hdf(fpath, key) anonymous2 = read_hdf(fpath, key=key) assert anonymous2.name is None assert_array_equal(anonymous.labels, anonymous2.labels) # wildcard axis key = hdf_group + '/axis_wildcard' wildcard.to_hdf(fpath, key) wildcard2 = read_hdf(fpath, key=key) assert wildcard2.iswildcard assert wildcard.equals(wildcard2)
def test_split(): # test splitting an anonymous axis a_b = Axis('a0_b0,a0_b1,a0_b2,a1_b0,a1_b1,a1_b2') a, b = a_b.split() assert a.equals(Axis(['a0', 'a1'])) assert b.equals(Axis(['b0', 'b1', 'b2']))
def test_translate(): # an axis with labels having the object dtype a = Axis(np.array(["a0", "a1"], dtype=object), 'a') assert a.index('a1') == 1 assert a.index('a1 >> A1') == 1