def _node_centrality_by_synapse(tree, nodes: Dict, totalOutputs: int, totalInputs: int) -> None: """ tree: a DiGraph nodes: a dictionary of treenode ID vs Counts instance totalOutputs: the total number of output synapses of the tree totalInputs: the total number of input synapses of the tree Returns nothing, the results are an update to the Counts instance of each treenode entry in nodes, namely the nPossibleIOPaths. """ # 1. Ensure the root is an end by checking that it has only one child; otherwise reroot at the first end node found if 0 == totalOutputs: # Not computable for counts in nodes.values(): counts.synapse_centrality = -1 return if len(list(tree.successors(find_root(tree)))) > 1: # Reroot at the first end node found tree = tree.copy() endNode = next(nodeID for nodeID in nodes.keys() if not list(tree.successors(nodeID))) reroot(tree, endNode) # 2. Partition into sequences, sorted from small to large sequences = sorted(partition(tree), key=len) # 3. Traverse all partitions counting synapses seen for seq in sequences: # Each seq runs from an end node towards the root or a branch node seenI = 0 seenO = 0 for nodeID in seq: counts = nodes[nodeID] seenI += counts.inputs + counts.seenInputs seenO += counts.outputs + counts.seenOutputs counts.seenInputs = seenI counts.seenOutputs = seenO counts.nPossibleIOPaths = counts.seenInputs * ( totalOutputs - counts.seenOutputs) + counts.seenOutputs * ( totalInputs - counts.seenInputs) counts.synapse_centrality = counts.nPossibleIOPaths / float( totalOutputs)
def _node_centrality_by_synapse(tree, nodes, totalOutputs, totalInputs): """ tree: a DiGraph nodes: a dictionary of treenode ID vs Counts instance totalOutputs: the total number of output synapses of the tree totalInputs: the total number of input synapses of the tree Returns nothing, the results are an update to the Counts instance of each treenode entry in nodes, namely the nPossibleIOPaths. """ # 1. Ensure the root is an end by checking that it has only one child; otherwise reroot at the first end node found if 0 == totalOutputs: # Not computable for counts in nodes.values(): counts.synapse_centrality = -1 return if len(tree.successors(find_root(tree))) > 1: # Reroot at the first end node found tree = tree.copy() endNode = next(nodeID for nodeID in nodes.keys() if not tree.successors(nodeID)) reroot(tree, endNode) # 2. Partition into sequences, sorted from small to large sequences = sorted(partition(tree), key=len) # 3. Traverse all partitions counting synapses seen for seq in sequences: # Each seq runs from an end node towards the root or a branch node seenI = 0 seenO = 0 for nodeID in seq: counts = nodes[nodeID] seenI += counts.inputs + counts.seenInputs seenO += counts.outputs + counts.seenOutputs counts.seenInputs = seenI counts.seenOutputs = seenO counts.nPossibleIOPaths = counts.seenInputs * (totalOutputs - counts.seenOutputs) + counts.seenOutputs * (totalInputs - counts.seenInputs) counts.synapse_centrality = counts.nPossibleIOPaths / float(totalOutputs)