def test_init4(self): c0 = Categorizer('C0', { 'C0-1': _c01, }) exp_categories = ['C0-1'] self.assertEqual(exp_categories, c0.categories()) self.assertEqual({'C0-1': _c01}, c0.categorizeC2F) self.assertEqual({_c01: 'C0-1'}, c0.categorizeF2C)
def setUp(self): self.symbols = range(10) self.c0 = Categorizer('C0', { 'C0-1': _c01, }) self.c01 = 'C0-1' self.c1 = Categorizer('C1', { 'C1-1': _c11, 'C1-2': _c12, }) self.c11 = 'C1-1' self.c12 = 'C1-2' self.c2 = Categorizer('C2', { 'C2-1': _c21, 'C2-2': _c22, }) self.c21 = 'C2-1' self.c22 = 'C2-2' # self.c3=Categorizer('C3', { # 'C3-1' : _c31, # 'C3-2' : _c32, # }) self.cx = Categorizer('CX', { str(NOTCATEGORIZED): _cx, })
def test_build1(self): symbols = range(10) c0 = Categorizer('C0', { 'C0-1': _c01, }) c1 = Categorizer('C1', { 'C1-1': _c11, 'C1-2': _c12, }) cinst = { 'C0': c0, 'C1': c1, } cmap = ['C0', 'C1'] sh = SubsetHierarchy() sh.build(cmap, cinst, symbols) self.assertIsNone(sh.symboltree)
def test_deuniquifyCategory1(self): ucategories = [ self.c1.uniquifyCategory(c) for c in self.c1.categories() ] dcategories = [ Categorizer.deuniquifyCategory(uc) for uc in ucategories ] exp_categories = [(self.c1.id, c) for c in self.c1.categories()] self.assertSequenceEqual(exp_categories, dcategories)
def setUp(self): self.symbols = range(10) self.c0 = Categorizer('C0', { 'C0-1': _c01, }) self.uniq_c01 = self.c0.uniquifyCategory('C0-1') self.c1 = Categorizer('C1', { 'C1-1': _c11, 'C1-2': _c12, }) self.uniq_c11 = self.c1.uniquifyCategory('C1-1') self.uniq_c12 = self.c1.uniquifyCategory('C1-2') self.c2 = Categorizer('C2', { 'C2-1': _c21, 'C2-2': _c22, }) self.uniq_c21 = self.c2.uniquifyCategory('C2-1') self.uniq_c22 = self.c2.uniquifyCategory('C2-2') self.c3 = Categorizer('C3', { 'C3-1': _c31, 'C3-2': _c32, }) self.uniq_c31 = self.c3.uniquifyCategory('C3-1') self.uniq_c32 = self.c3.uniquifyCategory('C3-2') self.cinst = { 'C0': self.c0, 'C1': self.c1, 'C2': self.c2, 'C3': self.c3, } self.cinstx = {} self.cmap0 = [] self.cmap1 = ['C0'] self.cmap2 = ['C0', 'C1'] self.cmap3 = ['C0', 'C2'] self.cmap4 = ['C0', 'C3'] self.cmap5 = ['C0', 'C1', 'C2'] self.cmapX = ['C0', 'XXXXX']
class TestSubsetHierarchy2(unittest.TestCase): def setUp(self): self.symbols = range(10) self.c0 = Categorizer('C0', { 'C0-1': _c01, }) self.uniq_c01 = self.c0.uniquifyCategory('C0-1') self.c1 = Categorizer('C1', { 'C1-1': _c11, 'C1-2': _c12, }) self.uniq_c11 = self.c1.uniquifyCategory('C1-1') self.uniq_c12 = self.c1.uniquifyCategory('C1-2') self.c2 = Categorizer('C2', { 'C2-1': _c21, 'C2-2': _c22, }) self.uniq_c21 = self.c2.uniquifyCategory('C2-1') self.uniq_c22 = self.c2.uniquifyCategory('C2-2') self.c3 = Categorizer('C3', { 'C3-1': _c31, 'C3-2': _c32, }) self.uniq_c31 = self.c3.uniquifyCategory('C3-1') self.uniq_c32 = self.c3.uniquifyCategory('C3-2') self.cinst = { 'C0': self.c0, 'C1': self.c1, 'C2': self.c2, 'C3': self.c3, } self.cinstx = {} self.cmap0 = [] self.cmap1 = ['C0'] self.cmap2 = ['C0', 'C1'] self.cmap3 = ['C0', 'C2'] self.cmap4 = ['C0', 'C3'] self.cmap5 = ['C0', 'C1', 'C2'] self.cmapX = ['C0', 'XXXXX'] def test_init0(self): sh = MockSubsetHierarchy1() self.assertEqual({}, sh.hierarchy) self.assertEqual({}, sh.symboltree) def test_init1(self): sh = MockSubsetHierarchy1() sh.build(self.cmap0, self.cinst, self.symbols) self.assertEqual({}, sh.hierarchy) self.assertEqual({}, sh.symboltree) def test_init2(self): sh = MockSubsetHierarchy1() sh.build(self.cmap1, self.cinst, self.symbols) expected_hierarchy = {None: self.c0.id, self.uniq_c01: None} expected_symboltree = {None: {self.uniq_c01: self.symbols}} self.assertEqual(expected_hierarchy, sh.hierarchy) self.assertEqual(expected_symboltree, sh.symboltree) def test_init3(self): sh = MockSubsetHierarchy1() # wrong instance dictionary with self.assertRaises(Error): sh.build(self.cmap1, self.cinstx, self.symbols) def test_init4(self): sh = MockSubsetHierarchy1() # wrong categorizers list with self.assertRaises(Error): sh.build(self.cmapX, self.cinstx, self.symbols) def test_init5(self): sh = MockSubsetHierarchy1() sh.build(self.cmap2, self.cinst, self.symbols) expected_hierarchy = {} expected_hierarchy.update({None: self.c0.id}) expected_hierarchy.update({self.uniq_c01: self.c1.id}) expected_hierarchy.update({self.uniq_c11: None}) expected_hierarchy.update({self.uniq_c12: None}) expected_symboltree = {} expected_symboltree.update({None: {self.uniq_c01: self.symbols}}) expected_symboltree.update({ self.uniq_c01: { self.uniq_c11: self.symbols[:5], self.uniq_c12: self.symbols[5:] } }) self.assertEqual(expected_hierarchy, sh.hierarchy) self.assertEqual(expected_symboltree, sh.symboltree) def test_init6(self): sh = MockSubsetHierarchy1() sh.build(self.cmap3, self.cinst, self.symbols) expected_hierarchy = {} expected_hierarchy.update({None: self.c0.id}) expected_hierarchy.update({self.uniq_c01: self.c2.id}) expected_hierarchy.update({self.uniq_c21: None}) expected_hierarchy.update({self.uniq_c22: None}) expected_symboltree = {} expected_symboltree.update({None: {self.uniq_c01: self.symbols}}) expected_symboltree.update({ self.uniq_c01: { self.uniq_c21: self.symbols[:2], self.uniq_c22: self.symbols[2:] } }) self.assertEqual(expected_hierarchy, sh.hierarchy) self.assertEqual(expected_symboltree, sh.symboltree) def test_init7(self): sh = MockSubsetHierarchy1() sh.build(self.cmap4, self.cinst, self.symbols) expected_hierarchy = {} expected_hierarchy.update({None: self.c0.id}) expected_hierarchy.update({self.uniq_c01: self.c3.id}) expected_hierarchy.update({self.uniq_c31: None}) expected_hierarchy.update({self.uniq_c32: None}) expected_symboltree = {} expected_symboltree.update({None: {self.uniq_c01: self.symbols}}) expected_symboltree.update({ self.uniq_c01: { self.uniq_c31: self.symbols[:8], self.uniq_c32: self.symbols[8:] } }) self.assertEqual(expected_hierarchy, sh.hierarchy) self.assertEqual(expected_symboltree, sh.symboltree) def test_init8(self): sh = MockSubsetHierarchy1() sh.build(self.cmap5, self.cinst, self.symbols) expected_hierarchy = {} expected_hierarchy.update({None: self.c0.id}) expected_hierarchy.update({self.uniq_c01: self.c1.id}) expected_hierarchy.update({self.uniq_c11: self.c2.id}) expected_hierarchy.update({self.uniq_c12: self.c2.id}) expected_hierarchy.update({self.uniq_c21: None}) expected_hierarchy.update({self.uniq_c22: None}) expected_symboltree = {} expected_symboltree.update({None: {self.uniq_c01: self.symbols}}) expected_symboltree.update({ self.uniq_c01: { self.uniq_c11: self.symbols[:5], self.uniq_c12: self.symbols[5:] } }) expected_symboltree.update({ self.uniq_c11: { self.uniq_c21: self.symbols[:2], self.uniq_c22: self.symbols[2:5] } }) # nothing is added for C2-1 below expected_symboltree.update( {self.uniq_c12: { self.uniq_c22: self.symbols[5:] }}) self.assertEqual(expected_hierarchy, sh.hierarchy) self.assertEqual(expected_symboltree, sh.symboltree)
def test_init5(self): Categorizer('CX', { str(NOTCATEGORIZED): _cx, })
def test_init3(self): with self.assertRaises(Error): Categorizer('IDXXX', {})
def test_init2(self): with self.assertRaises(Error): Categorizer(None, {})
class TestCategorizer2(unittest.TestCase): def setUp(self): self.symbols = range(10) self.c0 = Categorizer('C0', { 'C0-1': _c01, }) self.c01 = 'C0-1' self.c1 = Categorizer('C1', { 'C1-1': _c11, 'C1-2': _c12, }) self.c11 = 'C1-1' self.c12 = 'C1-2' self.c2 = Categorizer('C2', { 'C2-1': _c21, 'C2-2': _c22, }) self.c21 = 'C2-1' self.c22 = 'C2-2' # self.c3=Categorizer('C3', { # 'C3-1' : _c31, # 'C3-2' : _c32, # }) self.cx = Categorizer('CX', { str(NOTCATEGORIZED): _cx, }) def test_categorize1(self): categories = [self.c0.categorize(s) for s in self.symbols] expected_categories = [self.c01] * len(self.symbols) self.assertEqual(expected_categories, categories) def test_categorize2(self): categories = [self.cx.categorize(s) for s in self.symbols] expected_categories = [NOTCATEGORIZED] * len(self.symbols) self.assertEqual(expected_categories, categories) def test_categorize3(self): # C0, [0 1 2 3 4 5 6 7 8 9] produced_syms = list() ddcats0 = collections.defaultdict(list) for s in self.symbols: ddcats0[self.c0.categorize(s)].append(s) for sc in sorted(ddcats0.keys()): produced_syms.append(tuple([sc, ddcats0[sc]])) expected_syms = list() expected_syms.append(tuple([self.c01, self.symbols])) self.assertEqual(expected_syms, produced_syms) def test_categorize4(self): # C0->C1, [0 1 2 3 4 5 6 7 8 9] -> <5: [0 1 2 3 4] >=5: [5 6 7 8 9] produced_syms = list() # CO ddcats0 = collections.defaultdict(list) for s in self.symbols: ddcats0[self.c0.categorize(s)].append(s) for sc in sorted(ddcats0.keys()): produced_syms.append(tuple([sc, ddcats0[sc]])) # C1 for scat0 in sorted(ddcats0.keys()): ddcats1 = collections.defaultdict(list) for s in ddcats0[scat0]: ddcats1[self.c1.categorize(s)].append(s) for sc in sorted(ddcats1.keys()): produced_syms.append(tuple([sc, ddcats1[sc]])) expected_syms = list() expected_syms.append(tuple([self.c01, self.symbols])) expected_syms.append(tuple([self.c11, self.symbols[:5]])) expected_syms.append(tuple([self.c12, self.symbols[5:]])) self.assertEqual(expected_syms, produced_syms) def test_categorize5(self): # C0->C1->C2 # [0 1 2 3 4 5 6 7 8 9] -> <5: [0 1 2 3 4] >=5: [5 6 7 8 9] # [0 1 2 3 4] -> <2: [0 1] >=2: [2 3 4] # [5 6 7 8 9] -> <2: <None>, >=2: [5 6 7 8 9] produced_syms = list() # CO ddcats0 = collections.defaultdict(list) for s in self.symbols: ddcats0[self.c0.categorize(s)].append(s) for sc in sorted(ddcats0.keys()): produced_syms.append(tuple([sc, ddcats0[sc]])) # C1 for scat0 in sorted(ddcats0.keys()): ddcats1 = collections.defaultdict(list) for s in ddcats0[scat0]: ddcats1[self.c1.categorize(s)].append(s) for sc in sorted(ddcats1.keys()): produced_syms.append(tuple([sc, ddcats1[sc]])) # C2 for scat1 in sorted(ddcats1.keys()): ddcats2 = collections.defaultdict(list) for s in ddcats1[scat1]: ddcats2[self.c2.categorize(s)].append(s) for sc in sorted(ddcats2.keys()): produced_syms.append(tuple([sc, ddcats2[sc]])) expected_syms = list() # C0 expected_syms.append(tuple([self.c01, self.symbols])) # C1 expected_syms.append(tuple([self.c11, self.symbols[:5]])) expected_syms.append(tuple([self.c12, self.symbols[5:]])) # C2 expected_syms.append(tuple([self.c21, self.symbols[:2]])) expected_syms.append(tuple([self.c22, self.symbols[2:5]])) # Nothing added for C2-1 expected_syms.append(tuple([self.c22, self.symbols[5:]])) self.assertEqual(expected_syms, produced_syms) def test_uniquifyCategory1(self): uc = 'C[%s]->c[%s]' exp_categories = [uc % (self.c1.id, c) for c in self.c1.categories()] categories = [ self.c1.uniquifyCategory(c) for c in self.c1.categories() ] self.assertSequenceEqual(exp_categories, categories) def test_deuniquifyCategory1(self): ucategories = [ self.c1.uniquifyCategory(c) for c in self.c1.categories() ] dcategories = [ Categorizer.deuniquifyCategory(uc) for uc in ucategories ] exp_categories = [(self.c1.id, c) for c in self.c1.categories()] self.assertSequenceEqual(exp_categories, dcategories)
def test_categories1(self): c0 = Categorizer('C0', { 'C0-1': _c01, }) self.assertSequenceEqual(['C0-1'], c0.categories())