Exemple #1
0
def calcNoderateMultipliers(t, hTarget = None, method = 1) :
  nh = nodeHeights(t,allTipsZero=0)
  if hTarget is None:
    hm = mean([nh[c] for c in t.get_terminals()])
    hTarget = nh[t.root] - hm
  meth = [nodeRateMultiplier,nodeRateMultiplier1][method]
  rt = t.node(t.root)
  for c in rt.succ :
    meth(t, t.node(c), hTarget, nh)
  return hTarget
Exemple #2
0
def _treeBranchAssignmentExprs(tree, hRoot,
                               withDerivative = False, withInit = False) :
  
  allid = set(tree.all_ids())  # taxa
  terms = set(tree.get_terminals())
  # internal nodes
  allint = allid - terms

  #  unrooted has dated tips when rooted somewhere
  nh = nodeHeights(tree, allTipsZero = False)
  # Node before its descendants
  nInOrder = getPreOrder(tree, includeTaxa=True)
  # Reversed, child before parent
  nhInOrder = [(nh[x],x) for x in reversed(nInOrder)]
  nInOrder = [x for x in nInOrder if x not in terms]

  nParams = len(nInOrder) - 1
  
  if withInit :
    htox = []
  
  sr = []
    
  sr.append("h%d = %.15g" % (nInOrder[0], hRoot))
    
  for i,k in enumerate(nInOrder[1:]):
    h = tree.node(k).prev
    
    sr.append("h%d = x[%d] * h%d" % (k, i, h))
    if withInit:
      htox.append("x[%d] = %.15g" % (i, nh[k]/nh[h]))
      
    if withDerivative:
      if h == tree.root :
        sr.append("d_h%d_x = [%s]" % (k,der(i, nParams, -1)))
      else :
        sr.append("d_h%d_x = [%s]" % (k,der(i, nParams, h)))
        
  for h,n in nhInOrder[:-1] :
    assert n != tree.root
    p = tree.node(n).prev
    if n in terms:
      sr.append("b%d=(h%d) # %d" % (n, p, n)) 
       
      if withDerivative:
        if p == tree.root :
          sr.append("d_b%d_x = [0]*%d" % (n,nParams))
        else :
          sr.append("d_b%d_x = d_h%d_x" % (n,p))
    else :
      sr.append("b%d=(h%d - h%d) # %d" % (n, p, n, n))
      
      if withDerivative:
        if p == tree.root :
          sr.append("d_b%d_x = [-v for v in d_h%d_x]" % (n,n))
        else :
          sr.append("d_b%d_x = [u-v for u,v in zip(d_h%d_x, d_h%d_x)]" % (n,p,n))
        
  if withInit:
    return sr, htox
  return sr
Exemple #3
0
def treeHeightEstimate(t) :
  """ Distance between root to mean tip age. """
  nh = nodeHeights(t, allTipsZero=False)
  hm = mean([nh[c] for c in t.get_terminals()])
  return nh[t.root] - hm