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_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_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(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]
def test_eq(col): assert col == col assert col == AxisCollection((lipro, sex, age)) assert col == (lipro, sex, age) assert col != (lipro, age, sex)
def test_init_from_string(): col = AxisCollection('age=10;sex=M,F;year=2000..2017') assert col.names == ['age', 'sex', 'year'] assert list(col.age.labels) == [10] assert list(col.sex.labels) == ['M', 'F'] assert list(col.year.labels) == [y for y in range(2000, 2018)]
def test_init_from_group(): lipro_subset = lipro[:'P03'] col2 = AxisCollection((lipro_subset, sex)) assert col2.names == ['lipro', 'sex'] assert_array_equal(col2.lipro.labels, ['P01', 'P02', 'P03']) assert_array_equal(col2.sex.labels, ['M', 'F'])
def col(): return AxisCollection((lipro, sex, age))
def test_eq(self): col = self.collection self.assertEqual(col, col) self.assertEqual(col, AxisCollection((self.lipro, self.sex, self.age))) self.assertEqual(col, (self.lipro, self.sex, self.age)) self.assertNotEqual(col, (self.lipro, self.age, self.sex))
class TestAxisCollection(TestCase): 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_init_from_group(self): lipro_subset = self.lipro[:'P03'] col2 = AxisCollection((lipro_subset, self.sex)) self.assertEqual(col2.names, ['lipro', 'sex']) assert_array_equal(col2.lipro.labels, ['P01', 'P02', 'P03']) assert_array_equal(col2.sex.labels, ['M', 'F']) def test_init_from_string(self): col = AxisCollection('age=10;sex=M,F;year=2000..2017') assert col.names == ['age', 'sex', 'year'] assert list(col.age.labels) == [10] assert list(col.sex.labels) == ['M', 'F'] assert list(col.year.labels) == [y for y in range(2000, 2018)] def test_eq(self): col = self.collection self.assertEqual(col, col) self.assertEqual(col, AxisCollection((self.lipro, self.sex, self.age))) self.assertEqual(col, (self.lipro, self.sex, self.age)) self.assertNotEqual(col, (self.lipro, self.age, self.sex)) def test_getitem_name(self): col = self.collection self.assert_axis_eq(col['lipro'], self.lipro) self.assert_axis_eq(col['sex'], self.sex) self.assert_axis_eq(col['age'], self.age) def test_getitem_int(self): col = self.collection self.assert_axis_eq(col[0], self.lipro) self.assert_axis_eq(col[-3], self.lipro) self.assert_axis_eq(col[1], self.sex) self.assert_axis_eq(col[-2], self.sex) self.assert_axis_eq(col[2], self.age) self.assert_axis_eq(col[-1], self.age) def test_getitem_slice(self): col = self.collection[:2] self.assertEqual(len(col), 2) self.assert_axis_eq(col[0], self.lipro) self.assert_axis_eq(col[1], self.sex) def test_setitem_name(self): col = self.collection[:] # replace an axis with one with another name col['lipro'] = self.geo self.assertEqual(len(col), 3) self.assertEqual(col, [self.geo, self.sex, self.age]) # replace an axis with one with the same name col['sex'] = self.sex2 self.assertEqual(col, [self.geo, self.sex2, self.age]) col['geo'] = self.lipro self.assertEqual(col, [self.lipro, self.sex2, self.age]) col['age'] = self.geo self.assertEqual(col, [self.lipro, self.sex2, self.geo]) col['sex'] = self.sex col['geo'] = self.age self.assertEqual(col, self.collection) def test_setitem_name_axis_def(self): col = self.collection[:] # replace an axis with one with another name col['lipro'] = 'geo=A11,A12,A13' self.assertEqual(len(col), 3) self.assertEqual(col, [self.geo, self.sex, self.age]) # replace an axis with one with the same name col['sex'] = 'sex=F,M' self.assertEqual(col, [self.geo, self.sex2, self.age]) col['geo'] = 'lipro=P01..P04' self.assertEqual(col, [self.lipro, self.sex2, self.age]) col['age'] = 'geo=A11,A12,A13' self.assertEqual(col, [self.lipro, self.sex2, self.geo]) col['sex'] = 'sex=M,F' col['geo'] = 'age=0..7' self.assertEqual(col, self.collection) def test_setitem_int(self): col = self.collection[:] col[1] = self.geo self.assertEqual(len(col), 3) self.assertEqual(col, [self.lipro, self.geo, self.age]) col[2] = self.sex self.assertEqual(col, [self.lipro, self.geo, self.sex]) col[-1] = self.age self.assertEqual(col, [self.lipro, self.geo, self.age]) def test_setitem_list_replace(self): col = self.collection[:] col[['lipro', 'age']] = [self.geo, self.lipro] self.assertEqual(col, [self.geo, self.sex, self.lipro]) def test_setitem_slice_replace(self): col = self.collection[:] # replace by list col[1:] = [self.geo, self.sex] self.assertEqual(col, [self.lipro, self.geo, self.sex]) # replace by collection col[1:] = self.collection[1:] self.assertEqual(col, self.collection) def test_setitem_slice_insert(self): col = self.collection[:] col[1:1] = [self.geo] self.assertEqual(col, [self.lipro, self.geo, self.sex, self.age]) def test_setitem_slice_delete(self): col = self.collection[:] col[1:2] = [] self.assertEqual(col, [self.lipro, self.age]) col[0:1] = [] self.assertEqual(col, [self.age]) def assert_axis_eq(self, axis1, axis2): self.assertTrue(axis1.equals(axis2)) def test_delitem(self): col = self.collection[:] self.assertEqual(len(col), 3) del col[0] self.assertEqual(len(col), 2) self.assert_axis_eq(col[0], self.sex) self.assert_axis_eq(col[1], self.age) del col['age'] self.assertEqual(len(col), 1) self.assert_axis_eq(col[0], self.sex) del col[self.sex] self.assertEqual(len(col), 0) def test_delitem_slice(self): col = self.collection[:] self.assertEqual(len(col), 3) del col[0:2] self.assertEqual(len(col), 1) self.assertEqual(col, [self.age]) del col[:] self.assertEqual(len(col), 0) def test_pop(self): col = self.collection[:] lipro, sex, age = col self.assertEqual(len(col), 3) self.assertIs(col.pop(), age) self.assertEqual(len(col), 2) self.assertIs(col[0], lipro) self.assertIs(col[1], sex) self.assertIs(col.pop(), sex) self.assertEqual(len(col), 1) self.assertIs(col[0], lipro) self.assertIs(col.pop(), lipro) self.assertEqual(len(col), 0) def test_replace(self): col = self.collection[:] newcol = col.replace('sex', self.geo) # original collection is not modified self.assertEqual(col, self.collection) self.assertEqual(len(newcol), 3) self.assertEqual(newcol.names, ['lipro', 'geo', 'age']) self.assertEqual(newcol.shape, (4, 3, 8)) newcol = newcol.replace(self.geo, self.sex) self.assertEqual(len(newcol), 3) self.assertEqual(newcol.names, ['lipro', 'sex', 'age']) self.assertEqual(newcol.shape, (4, 2, 8)) # from now on, reuse original collection newcol = col.replace(self.sex, 3) self.assertEqual(len(newcol), 3) self.assertEqual(newcol.names, ['lipro', None, 'age']) self.assertEqual(newcol.shape, (4, 3, 8)) newcol = col.replace(self.sex, ['a', 'b', 'c']) self.assertEqual(len(newcol), 3) self.assertEqual(newcol.names, ['lipro', None, 'age']) self.assertEqual(newcol.shape, (4, 3, 8)) newcol = col.replace(self.sex, "letters=a,b,c") self.assertEqual(len(newcol), 3) self.assertEqual(newcol.names, ['lipro', 'letters', 'age']) self.assertEqual(newcol.shape, (4, 3, 8)) 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(self): col = self.collection self.assertEqual(col.index('lipro'), 0) with self.assertRaises(ValueError): col.index('nonexisting') self.assertEqual(col.index(0), 0) self.assertEqual(col.index(1), 1) self.assertEqual(col.index(2), 2) self.assertEqual(col.index(-1), -1) self.assertEqual(col.index(-2), -2) self.assertEqual(col.index(-3), -3) with self.assertRaises(ValueError): col.index(3) # objects actually in col self.assertEqual(col.index(self.lipro), 0) self.assertEqual(col.index(self.sex), 1) self.assertEqual(col.index(self.age), 2) # other axis with the same name self.assertEqual(col.index(self.sex2), 1) # non existing with self.assertRaises(ValueError): col.index(self.geo) with self.assertRaises(ValueError): col.index(self.value) # test anonymous axes anon = Axis([0, 1]) col.append(anon) self.assertEqual(col.index(anon), 3) # different object, same values anon2 = anon.copy() self.assertEqual(col.index(anon2), 3) # different values anon3 = Axis([0, 2]) with self.assertRaises(ValueError): col.index(anon3) def test_get(self): col = self.collection self.assert_axis_eq(col.get('lipro'), self.lipro) self.assertIsNone(col.get('nonexisting')) self.assertIs(col.get('nonexisting', self.value), self.value) def test_keys(self): self.assertEqual(self.collection.keys(), ['lipro', 'sex', 'age']) def test_getattr(self): col = self.collection self.assert_axis_eq(col.lipro, self.lipro) self.assert_axis_eq(col.sex, self.sex) self.assert_axis_eq(col.age, self.age) def test_append(self): col = self.collection geo = Axis('geo=A11,A12,A13') col.append(geo) self.assertEqual(col, [self.lipro, self.sex, self.age, geo]) def test_extend(self): col = self.collection col.extend([self.geo, self.value]) self.assertEqual( col, [self.lipro, self.sex, self.age, self.geo, self.value]) def test_insert(self): col = self.collection col.insert(1, self.geo) self.assertEqual(col, [self.lipro, self.geo, self.sex, self.age]) 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_combine(self): col = self.collection.copy() lipro, sex, age = self.lipro, self.sex, self.age res = col.combine_axes((lipro, sex)) self.assertEqual(res.names, ['lipro_sex', 'age']) self.assertEqual(res.size, col.size) self.assertEqual(res.shape, (4 * 2, 8)) print(res.info) assert_array_equal(res.lipro_sex.labels[0], 'P01_M') res = col.combine_axes((lipro, age)) self.assertEqual(res.names, ['lipro_age', 'sex']) self.assertEqual(res.size, col.size) self.assertEqual(res.shape, (4 * 8, 2)) assert_array_equal(res.lipro_age.labels[0], 'P01_0') res = col.combine_axes((sex, age)) self.assertEqual(res.names, ['lipro', 'sex_age']) self.assertEqual(res.size, col.size) self.assertEqual(res.shape, (4, 2 * 8)) assert_array_equal(res.sex_age.labels[0], 'M_0') def test_info(self): expected = """\ 4 x 2 x 8 lipro [4]: 'P01' 'P02' 'P03' 'P04' sex [2]: 'M' 'F' age [8]: 0 1 2 ... 5 6 7""" self.assertEqual(self.collection.info, expected) def test_str(self): self.assertEqual(str(self.collection), "{lipro, sex, age}") def test_repr(self): self.assertEqual( repr(self.collection), """AxisCollection([ Axis(['P01', 'P02', 'P03', 'P04'], 'lipro'), Axis(['M', 'F'], 'sex'), Axis([0, 1, 2, 3, 4, 5, 6, 7], 'age') ])""")
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)