Functional coverage tree: Difference between revisions

Content added Content deleted
m (→‎Python: Composition of pure functions: (str.isspace() in lieu of `re`))
Line 1,606: Line 1,606:
# TWO COMPUTATIONS BY TRAVERSAL
# TWO COMPUTATIONS BY TRAVERSAL
withResidueShares(1.0)(
withResidueShares(1.0)(
foldTree(weightedTreeAverage)(
foldTree(weightedCoverage)(


# TREE FROM PARSE OF OUTLINE TEXT
# TREE FROM PARSE OF OUTLINE TEXT
Line 1,624: Line 1,624:
# WEIGHTED COVERAGE, AND SHARE OF TOTAL RESIDUE -----------
# WEIGHTED COVERAGE, AND SHARE OF TOTAL RESIDUE -----------


# weightedCoverage :: Tree Dict -> Float
# weightedCoverage :: Tree Dict ->
def weightedCoverage(node):
'''The weighted coverage of a
node in a coverage tree.
'''
nodeRoot = root(node)
return nodeRoot['coverage'] * nodeRoot['weight']


# weightedTreeAverage :: Tree Dict ->
# [Tree Dict] -> Tree Dict
# [Tree Dict] -> Tree Dict
def weightedTreeAverage(x):
def weightedCoverage(x):
'''The weighted average of a tree node,
'''The weighted coverage of a tree node,
as a function of the weighted averages
as a function of the weighted averages
of its children.
of its children.
'''
'''
def go(xs):
def go(xs):
cws = [(r['coverage'], r['weight']) for r in [root(x) for x in xs]]
totalWeight = reduce(lambda a, x: a + x[1], cws, 0)
return Node(dict(
return Node(dict(
x, **{
x, **{
'coverage': round(x['coverage'] + (
'coverage': round(reduce(
reduce(
lambda a, cw: a + (cw[0] * cw[1]),
lambda a, node: weightedCoverage(node) + a,
cws, x['coverage']
xs, 0
) / (totalWeight if 0 < totalWeight else 1), 5)
) / (
reduce(
lambda a, node: root(node)['weight'] + a,
xs, 0
) or 1
)
), 5)
}
}
))(xs)
))(xs)