def test_index_level_a(self) -> None: groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) targets = np.array((IndexLevelGO(index=observations), IndexLevelGO(observations, offset=2))) level0 = IndexLevelGO(index=groups, targets=ArrayGO(targets)) level1 = level0.to_index_level() groups = IndexGO(('C', 'D')) observations = IndexGO(('x', 'y', 'z')) targets = np.array((IndexLevelGO(index=observations), IndexLevelGO(observations, offset=3))) level2 = IndexLevelGO(index=groups, targets=ArrayGO(targets)) level0.extend(level2) self.assertEqual(len(level0.get_labels()), 10) self.assertEqual(len(level1.get_labels()), 4) self.assertEqual(len(level2.get_labels()), 6) assert level0.targets is not None # import ipdb; ipdb.set_trace() self.assertEqual([lvl.offset for lvl in level0.targets], [0, 2, 4, 7])
def test_index_level_append_a(self): category = IndexGO(('I', 'II')) groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) lvl2a = IndexLevelGO(index=observations) lvl2b = IndexLevelGO(index=observations, offset=2) lvl2_targets = ArrayGO((lvl2a, lvl2b)) # must defensively copy index assert id(lvl2a.index) != id(lvl2b.index) lvl1a = IndexLevelGO(index=groups, targets=lvl2_targets, offset=0) # must create new index levels for each lower node lvl2c = IndexLevelGO(index=observations) lvl2d = IndexLevelGO(index=observations, offset=2) lvl2_targets = ArrayGO((lvl2c, lvl2d)) # must defensively copy index assert id(lvl2c.index) != id(lvl2d.index) lvl1b = IndexLevelGO(index=groups, targets=lvl2_targets, offset=len(lvl1a)) # we need as many targets as len(index) lvl0 = IndexLevelGO(index=category, targets=ArrayGO((lvl1a, lvl1b))) lvl0.append(('II', 'B', 'z')) # depth not found is 2 self.assertEqual( [lvl0.loc_to_iloc(tuple(x)) for x in lvl0.get_labels()], list(range(9))) lvl0.append(('II', 'C', 'a')) # depth not found is 1 self.assertEqual( [lvl0.loc_to_iloc(tuple(x)) for x in lvl0.get_labels()], list(range(10))) lvl0.append(('III', 'A', 'a')) # 0 self.assertEqual( [lvl0.loc_to_iloc(tuple(x)) for x in lvl0.get_labels()], list(range(11))) self.assertEqual( lvl0.get_labels().tolist(), [['I', 'A', 'x'], ['I', 'A', 'y'], ['I', 'B', 'x'], ['I', 'B', 'y'], ['II', 'A', 'x'], ['II', 'A', 'y'], ['II', 'B', 'x'], ['II', 'B', 'y'], ['II', 'B', 'z'], ['II', 'C', 'a'], ['III', 'A', 'a']])
def test_index_level_get_labels_a(self): groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) targets = ArrayGO( (IndexLevelGO(index=observations), IndexLevelGO(observations, offset=2))) level0 = IndexLevelGO(index=groups, targets=targets) self.assertEqual(level0.get_labels().tolist(), [['A', 'x'], ['A', 'y'], ['B', 'x'], ['B', 'y']]) self.assertEqual(level0.get_labels().dtype.kind, 'U') groups = IndexGO((1, 2)) observations = IndexGO((10, 20)) targets = ArrayGO( (IndexLevelGO(index=observations), IndexLevelGO(observations, offset=2))) level1 = IndexLevelGO(index=groups, targets=targets) self.assertEqual(level1.get_labels().dtype.kind, 'i')
def test_index_level_append_b(self) -> None: groups = IndexGO(('A', 'B')) observations = IndexGO(('x', 'y')) lvl2a = IndexLevelGO(index=observations) lvl2b = IndexLevelGO(index=observations, offset=2) lvl2_targets = ArrayGO((lvl2a, lvl2b)) lvl1a = IndexLevelGO(index=groups, targets=lvl2_targets, offset=0) with self.assertRaises(RuntimeError): lvl1a.append((1, 2, 3)) lvl1a.append((1, 2)) self.assertEqual(lvl1a.get_labels().tolist(), [['A', 'x'], ['A', 'y'], ['B', 'x'], ['B', 'y'], [1, 2]])