Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move spanning tree counter to updaters
- Loading branch information
Showing
5 changed files
with
44 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Updaters that compute spanning tree statistics.""" | ||
import math | ||
import numpy | ||
import networkx | ||
|
||
|
||
def _num_spanning_trees_in_district(partition, district): | ||
"""Given a district ID, returns the number of spanning trees in the | ||
subgraph of self corresponding to the district. | ||
Uses Kirchoff's theorem to compute the number of spanning trees. | ||
:param partition: :class:`gerrychain.Partition` | ||
:param district: A district label (part) in the partition. | ||
:return: The number of spanning trees in the subgraph of the | ||
partition corresponding to district | ||
""" | ||
graph = partition.subgraphs[district] | ||
laplacian = networkx.laplacian_matrix(graph) | ||
L = numpy.delete(numpy.delete(laplacian.todense(), 0, 0), 1, 1) | ||
return math.exp(numpy.linalg.slogdet(L)[1]) | ||
|
||
|
||
def num_spanning_trees(partition): | ||
"""Returns the number of spanning trees in each part (district) of a partition.""" | ||
return { | ||
part: _num_spanning_trees_in_district(partition, part) | ||
for part in partition.parts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from gerrychain import Partition | ||
from gerrychain.updaters import num_spanning_trees | ||
|
||
|
||
def test_get_num_spanning_trees(three_by_three_grid): | ||
assignment = {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1} | ||
partition = Partition( | ||
three_by_three_grid, | ||
assignment, | ||
{"num_spanning_trees": num_spanning_trees} | ||
) | ||
assert 192 == round(partition["num_spanning_trees"][1]) | ||
assert [1] == list(partition["num_spanning_trees"].keys()) |