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]
    for areatree in areatrees:
        areatree = areatree.getObject()
        layerset = areatree.getLayerSet()
        calibration = layerset.getCalibration()
        affine = areatree.getAffineTransform()
        print areatree

        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)
        cutNodes = getCutNodes(areatree)
        dt = getTreeDistanceTable(areatree)
        topnd, bottomnd = getLongestPathNodes(areatree)
        mnds = set(MeasurePathDistance(areatree, topnd, bottomnd).getPath())

        connectedTrees = findConnectedTree(areatree)
        inputs = connectedTrees['inputs']
        outputs = connectedTrees['outputs']

        # output tree connections
        dtext = {'inputs':'input', 'outputs':'output'}
        for d, cn in connectedTrees.iteritems():
            # d for direction, cn for connection dict
            for nd, targets in cn.iteritems():
                for tree in targets:
                    neuriteConnection.append([neurite.getParent().getTitle(), neurite.getTitle(), areatree.getId(), dtext[d], tree.getId(), treeVsNeurite[tree].getTitle(), treeVsNeurite[tree].getParent().getTitle()])

        markings = markTreeSegment(areatree)
        for i, nds in markings.iteritems():
        # 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:
Пример #4
0
        layerset = areatree.getLayerSet()
        calibration = layerset.getCalibration()
        affine = areatree.getAffineTransform()
        # for calculating subtree length
        dt = getTreeDistanceTable(areatree)
        print areatree

        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)

        # finding the primary 'stem'
        topnd, bottomnd = getLongestPathNodes(areatree)
        mnds = set(MeasurePathDistance(areatree, topnd, bottomnd).getPath())

        synapses = findConnectorsInTree(areatree)
        inputs = synapses['incoming']
        outputs = synapses['outgoing']

        for direction, ndVsConnector in synapses.iteritems():
            print direction, ndVsConnector.values()
            cutNodes = set(ndVsConnector.keys())
            if not cutNodes:
                continue
            markings = markTreeSegment(areatree, cutNodes)
            for i, nds in markings.iteritems():
                # for each segment
                length = sum([d for nd, d in dt.iteritems() if nd in nds])
                mainSegNds = nds.intersection(mnds)
 for nd, connectors in inputs.iteritems():
     for c in connectors:
         p = [
             c.getId(),
             neurite.getParent().getTitle(),
             neurite.getTitle(),
             areatree.getId(),
             nd.getId(), 'incoming'
         ]
         connectorProfile.append(p)
 # calculate distance for each input
 for nd in ndIn:
     ds = []
     for tnd in ndOut:
         # from output
         d = MeasurePathDistance(areatree, nd, tnd).getDistance()
         nodeData = [
             neurite.getParent().getTitle(),
             neurite.getTitle(),
             areatree.getId(),
             nd.getId(), 'incoming', 'outgoing', d
         ]
         foundNeuriteNodes.append(nodeData)
         # for min
         ds.append(d)
         # for connectors with id
         for toCt in inputs[nd]:
             for fromCt in outputs[tnd]:
                 connectorData = [
                     neurite.getParent().getTitle(),
                     neurite.getTitle(),
        # 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
                if 'input_' in tag.toString():
                    posN = tag.toString().find('_')
                    nInputs = int(tag.toString()[posN + 1:])
                    ndIn.append(nd)
        ndIn = set(ndIn)
        ndOut = set(ndOut)

        if 0 == len(ndIn) or 0 == len(ndOut):
            continue

        # calculate shortest distance for each input
        ndInDistance = {}
        for nd in ndIn:
            d = []
            for tnd in ndOut:
                d.append(MeasurePathDistance(areatree, nd, tnd).getDistance())
            ndInDistance[nd] = min(d)

        # calculate shortest distance for each out
        ndOutDistance = {}
        for nd in ndOut:
            d = []
            for tnd in ndIn:
                d.append(MeasurePathDistance(areatree, nd, tnd).getDistance())
            ndOutDistance[nd] = min(d)

        # input synapses
        for nd, d in ndInDistance.iteritems():
            # get node coordinates, from tut on web
            fp = array([nd.getX(), nd.getY()], 'f')
            affine.transform(fp, 0, fp, 0, 1)