Ejemplo n.º 1
0
def getLongestPathNodes(areatree):
    # requires ini.trakem2.display.Tree.MeasurePathDistance
    root = areatree.getRoot()
    endNodes = set([nd for nd in areatree.getEndNodes()])
    branchNodes = set([nd for nd in areatree.getBranchNodes()])
    if 2 == root.getChildrenCount():
        branchNodes.discard(root)

    # case 0. root is the only node
    if 0 == root.getChildrenCount():
        print 'root singleton'
        return
    # case 1. else
    else:
        # get longest path from each 'lobe'
        ndVsLen = {}
        ndVsLen[root] = 0.0    # compatibility when root children count is 1
        for loberoot in root.getChildrenNodes():
            lbEndNodes = [nd for nd in loberoot.getEndNodes()]
            lbBranchNodes = [nd for nd in loberoot.getBranchNodes()]
            lbMaxLen = 0.0
            lbMaxPM = None
            lbMaxNd = lbEndNodes[0]
            for nd in lbEndNodes:
                pm = MeasurePathDistance(areatree, nd, root)
                if lbMaxLen <= pm.getDistance():
                    lbMaxLen = pm.getDistance()
                    lbMaxPM = pm
                    lbMaxNd = nd
            ndVsLen[lbMaxNd] = lbMaxLen
            # print 'lobe max', nd, lbMaxLen
        longestNodes = sorted(ndVsLen, key=ndVsLen.get, reverse=True)
        nda = longestNodes[0]
        ndb = longestNodes[1]
        if nda.getLayer().getParent().indexOf(nd.getLayer()) >= ndb.getLayer().getParent().indexOf(nd.getLayer()):
            ndtop = nda
            ndbottom = ndb
        else:
            ndtop = ndb
            ndbottom =nda
    return [ndtop, ndbottom]
 # case 0. root is the only node
 if 0 == root.getChildrenCount():
     print 'root singleton'
 else:
     # get longest path from each half
     ndVsLen = {}
     ndVsLen[root] = 0.0  # compatibility when root children count is 1
     for loberoot in root.getChildrenNodes():
         lbEndNodes = [nd for nd in loberoot.getEndNodes()]
         lbBranchNodes = [nd for nd in loberoot.getBranchNodes()]
         lbMaxLen = 0.0
         lbMaxPM = None
         lbMaxNd = lbEndNodes[0]
         for nd in lbEndNodes:
             pm = MeasurePathDistance(areatree, nd, root)
             if lbMaxLen <= pm.getDistance():
                 lbMaxLen = pm.getDistance()
                 lbMaxPM = pm
                 lbMaxNd = nd
         ndVsLen[lbMaxNd] = lbMaxLen
         # print 'lobe max', nd, lbMaxLen
     longestNodes = sorted(ndVsLen, key=ndVsLen.get, reverse=True)
     nda = longestNodes[0]
     ndb = longestNodes[1]
     if nda.getLayer().getParent().indexOf(
             nd.getLayer()) >= ndb.getLayer().getParent().indexOf(
                 nd.getLayer()):
         ndtop = nda
         ndbottom = ndb
     else:
         ndtop = ndb
 # case 0. root is the only node
 if 0 == root.getChildrenCount():
     print 'root singleton'
 else:
     # get longest path from each half
     ndVsLen = {}
     ndVsLen[root] = 0.0    # compatibility when root children count is 1
     for loberoot in root.getChildrenNodes():
         lbEndNodes = [nd for nd in loberoot.getEndNodes()]
         lbBranchNodes = [nd for nd in loberoot.getBranchNodes()]
         lbMaxLen = 0.0
         lbMaxPM = None
         lbMaxNd = lbEndNodes[0]
         for nd in lbEndNodes:
             pm = MeasurePathDistance(areatree, nd, root)
             if lbMaxLen <= pm.getDistance():
                 lbMaxLen = pm.getDistance()
                 lbMaxPM = pm
                 lbMaxNd = nd
         ndVsLen[lbMaxNd] = lbMaxLen
         # print 'lobe max', nd, lbMaxLen
     longestNodes = sorted(ndVsLen, key=ndVsLen.get, reverse=True)
     nda = longestNodes[0]
     ndb = longestNodes[1]
     if nda.getLayer().getParent().indexOf(nd.getLayer()) >= ndb.getLayer().getParent().indexOf(nd.getLayer()):
         ndtop = nda
         ndbottom = ndb
     else:
         ndtop = ndb
         ndbottom =nda
     pm = MeasurePathDistance(areatree, ndtop, ndbottom)