def ProcessIt(composites, nToConsider=3, verbose=0): composite = composites[0] nComposites = len(composites) ns = composite.GetDescriptorNames() # nDesc = len(ns)-2 if len(ns) > 2: globalRes = {} nDone = 1 descNames = {} for composite in composites: if verbose > 0: print('#------------------------------------') print('Doing: ', nDone) nModels = len(composite) nDone += 1 res = {} for i in range(len(composite)): model = composite.GetModel(i) if isinstance(model, Tree.TreeNode): levels = TreeUtils.CollectLabelLevels( model, {}, 0, nToConsider) TreeUtils.CollectDescriptorNames(model, descNames, 0, nToConsider) for descId in levels.keys(): v = res.get(descId, numpy.zeros(nToConsider, numpy.float)) v[levels[descId]] += 1. / nModels res[descId] = v for k in res: v = globalRes.get(k, numpy.zeros(nToConsider, numpy.float)) v += res[k] / nComposites globalRes[k] = v if verbose > 0: for k in res.keys(): name = descNames[k] strRes = ', '.join(['%4.2f' % x for x in res[k]]) print('%s,%s,%5.4f' % (name, strRes, sum(res[k]))) print() if verbose >= 0: print('# Average Descriptor Positions') retVal = [] for k in globalRes: name = descNames[k] if verbose >= 0: strRes = ', '.join(['%4.2f' % x for x in globalRes[k]]) print('%s,%s,%5.4f' % (name, strRes, sum(globalRes[k]))) tmp = [name] tmp.extend(globalRes[k]) tmp.append(sum(globalRes[k])) retVal.append(tmp) if verbose >= 0: print() else: retVal = [] return retVal
def test_TreeUtils(self): # Tree is d1(d2,d3(d2,d4)) t1 = Node(None, 'd1', 1) t2 = Node(None, 'd2', 2) t1.AddChildNode(t2) t2 = Node(None, 'd3', 3) t1.AddChildNode(t2) t3 = Node(None, 'd4', 4) t2.AddChildNode(t3) t3 = Node(None, 'd2', 2) t2.AddChildNode(t3) r = TreeUtils.CollectLabelLevels(t1, {}) self.assertEqual(r, {1: 0, 2: 1, 3: 1, 4: 2}) # Only to depth 2 r = TreeUtils.CollectLabelLevels(t1, {}, 0, 2) self.assertEqual(r, {1: 0, 2: 1, 3: 1}) # Check that we can handle subtrees: r = TreeUtils.CollectLabelLevels(t1, {}, 1, 2) self.assertEqual(r, {1: 1}) names = TreeUtils.CollectDescriptorNames(t1, {}) self.assertEqual(names, {1: 'd1', 2: 'd2', 3: 'd3', 4: 'd4'}) names = TreeUtils.CollectDescriptorNames(t1, {}, 0, 2) self.assertEqual(names, {1: 'd1', 2: 'd2', 3: 'd3'}) names = TreeUtils.CollectDescriptorNames(t1, {}, 1, 2) self.assertEqual(names, {1: 'd1'})