-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathapi.html
2774 lines (2639 loc) · 218 KB
/
api.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>API Reference — GerryChain documentation</title><link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/copybutton.css" type="text/css" />
<link rel="stylesheet" href="_static/css/custom.css" type="text/css" />
<!--[if lt IE 9]>
<script src="_static/js/html5shiv.min.js"></script>
<![endif]-->
<script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/language_data.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script src="_static/js/theme.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Reproducibility" href="topics/reproducibility.html" />
<link rel="prev" title="Working With Geometries" href="user/geometries.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #0099cd" >
<a href="index.html" class="icon icon-home"> GerryChain
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption"><span class="caption-text">User Guide</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="user/intro.html">Overview of the Chain</a></li>
<li class="toctree-l1"><a class="reference internal" href="user/install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="user/quickstart.html">Getting started with GerryChain</a></li>
<li class="toctree-l1"><a class="reference internal" href="user/recom.html">Running a chain with ReCom</a></li>
<li class="toctree-l1"><a class="reference internal" href="user/partitions.html">Working with Partitions</a></li>
<li class="toctree-l1"><a class="reference internal" href="user/updaters.html">Updaters</a></li>
<li class="toctree-l1"><a class="reference internal" href="user/data.html">Good Data Practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="user/geometries.html">Working With Geometries</a></li>
</ul>
<p class="caption"><span class="caption-text">API Reference</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">API Reference</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#adjacency-graphs">Adjacency graphs</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-gerrychain.partition">Partitions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#markov-chains">Markov chains</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-gerrychain.proposals">Proposals</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-gerrychain.constraints">Binary constraints</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-gerrychain.updaters">Updaters</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-gerrychain.updaters.election">Elections</a></li>
<li class="toctree-l2"><a class="reference internal" href="#grids">Grids</a></li>
<li class="toctree-l2"><a class="reference internal" href="#spanning-tree-methods">Spanning tree methods</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-gerrychain.metrics">Metrics</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-gerrychain.meta.diversity">Diversity stats</a></li>
</ul>
</li>
</ul>
<p class="caption"><span class="caption-text">Topics</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="topics/reproducibility.html">Reproducibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="topics/tools.html">Evaluating districting plans in the real world</a></li>
<li class="toctree-l1"><a class="reference internal" href="topics/contributing.html">Contributing to GerryChain</a></li>
</ul>
<p class="caption"><span class="caption-text">Index</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="full_ref.html">Full Package Reference</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #0099cd" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">GerryChain</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html" class="icon icon-home"></a> »</li>
<li>API Reference</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/api.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="module-gerrychain">
<span id="id1"></span><span id="api-reference"></span><h1>API Reference<a class="headerlink" href="#module-gerrychain" title="Permalink to this headline">¶</a></h1>
<div class="contents local topic" id="table-of-contents">
<p class="topic-title">Table of Contents</p>
<ul class="simple">
<li><p><a class="reference internal" href="#adjacency-graphs" id="id6">Adjacency graphs</a></p></li>
<li><p><a class="reference internal" href="#module-gerrychain.partition" id="id7">Partitions</a></p></li>
<li><p><a class="reference internal" href="#markov-chains" id="id8">Markov chains</a></p></li>
<li><p><a class="reference internal" href="#module-gerrychain.proposals" id="id9">Proposals</a></p></li>
<li><p><a class="reference internal" href="#module-gerrychain.constraints" id="id10">Binary constraints</a></p></li>
<li><p><a class="reference internal" href="#module-gerrychain.updaters" id="id11">Updaters</a></p></li>
<li><p><a class="reference internal" href="#module-gerrychain.updaters.election" id="id12">Elections</a></p></li>
<li><p><a class="reference internal" href="#grids" id="id13">Grids</a></p></li>
<li><p><a class="reference internal" href="#spanning-tree-methods" id="id14">Spanning tree methods</a></p></li>
<li><p><a class="reference internal" href="#module-gerrychain.metrics" id="id15">Metrics</a></p></li>
<li><p><a class="reference internal" href="#module-gerrychain.meta.diversity" id="id16">Diversity stats</a></p></li>
</ul>
</div>
<div class="section" id="adjacency-graphs">
<h2><a class="toc-backref" href="#id6">Adjacency graphs</a><a class="headerlink" href="#adjacency-graphs" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="gerrychain.graph.graph.Graph">
<em class="property">class </em><code class="sig-prename descclassname">gerrychain.graph.graph.</code><code class="sig-name descname">Graph</code><span class="sig-paren">(</span><em class="sig-param">incoming_graph_data=None</em>, <em class="sig-param">**attr</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a graph to be partitioned, extending the <code class="xref py py-class docutils literal notranslate"><span class="pre">networkx.Graph</span></code>.</p>
<p>This class includes additional class methods for constructing graphs from shapefiles,
and for saving and loading graphs in JSON format.</p>
<p>Initialize a graph with edges, name, or graph attributes.</p>
<dl class="simple">
<dt>incoming_graph_data<span class="classifier">input graph (optional, default: None)</span></dt><dd><p>Data to initialize graph. If None (default) an empty
graph is created. The data can be an edge list, or any
NetworkX graph object. If the corresponding optional Python
packages are installed the data can also be a 2D NumPy array, a
SciPy sparse matrix, or a PyGraphviz graph.</p>
</dd>
<dt>attr<span class="classifier">keyword arguments, optional (default= no attributes)</span></dt><dd><p>Attributes to add to graph as key=value pairs.</p>
</dd>
</dl>
<p>convert</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">G</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">Graph</span><span class="p">()</span> <span class="c1"># or DiGraph, MultiGraph, MultiDiGraph, etc</span>
<span class="gp">>>> </span><span class="n">G</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">Graph</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"my graph"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">e</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)]</span> <span class="c1"># list of edges</span>
<span class="gp">>>> </span><span class="n">G</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">Graph</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
</pre></div>
</div>
<p>Arbitrary graph attribute pairs (key=value) may be assigned</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">G</span> <span class="o">=</span> <span class="n">nx</span><span class="o">.</span><span class="n">Graph</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="s2">"Friday"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">G</span><span class="o">.</span><span class="n">graph</span>
<span class="go">{'day': 'Friday'}</span>
</pre></div>
</div>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.add_data">
<code class="sig-name descname">add_data</code><span class="sig-paren">(</span><em class="sig-param">df: <Mock name='mock.DataFrame' id='140260702472032'></em>, <em class="sig-param">columns: Optional[Iterable[str]] = None</em><span class="sig-paren">)</span> → None<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.add_data"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.add_data" title="Permalink to this definition">¶</a></dt>
<dd><p>Add columns of a DataFrame to a graph as node attributes
by matching the DataFrame’s index to node ids.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>df</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">pandas.DataFrame</span></code>) – Dataframe containing given columns.</p></li>
<li><p><strong>columns</strong> (<em>Optional</em><em>[</em><em>Iterable</em><em>[</em><em>str</em><em>]</em><em>]</em><em>, </em><em>optional</em>) – List of dataframe column names to add. Default is None.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>None</p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.from_file">
<em class="property">classmethod </em><code class="sig-name descname">from_file</code><span class="sig-paren">(</span><em class="sig-param">filename: str</em>, <em class="sig-param">adjacency: str = 'rook'</em>, <em class="sig-param">cols_to_add: Optional[List[str]] = None</em>, <em class="sig-param">reproject: bool = False</em>, <em class="sig-param">ignore_errors: bool = False</em><span class="sig-paren">)</span> → gerrychain.graph.graph.Graph<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.from_file"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.from_file" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a <a class="reference internal" href="#gerrychain.graph.graph.Graph" title="gerrychain.graph.graph.Graph"><code class="xref py py-class docutils literal notranslate"><span class="pre">Graph</span></code></a> from a shapefile (or GeoPackage, or GeoJSON, or
any other library that <code class="xref py py-mod docutils literal notranslate"><span class="pre">geopandas</span></code> can read. See <a class="reference internal" href="#gerrychain.graph.graph.Graph.from_geodataframe" title="gerrychain.graph.graph.Graph.from_geodataframe"><code class="xref py py-meth docutils literal notranslate"><span class="pre">from_geodataframe()</span></code></a>
for more details.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>filename</strong> (<em>str</em>) – Path to the shapefile / GeoPackage / GeoJSON / etc.</p></li>
<li><p><strong>adjacency</strong> (<em>str</em><em>, </em><em>optional</em>) – The adjacency type to use (“rook” or “queen”). Default is “rook”</p></li>
<li><p><strong>cols_to_add</strong> (<em>Optional</em><em>[</em><em>List</em><em>[</em><em>str</em><em>]</em><em>]</em><em>, </em><em>optional</em>) – The names of the columns that you want to
add to the graph as node attributes. Default is None.</p></li>
<li><p><strong>reproject</strong> (<em>bool</em><em>, </em><em>optional</em>) – Whether to reproject to a UTM projection before
creating the graph. Default is False.</p></li>
<li><p><strong>ignore_errors</strong> (<em>bool</em><em>, </em><em>optional</em>) – Whether to ignore all invalid geometries and try to continue
creating the graph. Default is False.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The Graph object of the geometries from <cite>filename</cite>.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.graph.graph.Graph" title="gerrychain.graph.graph.Graph">Graph</a></p>
</dd>
</dl>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>This method requires the optional <code class="docutils literal notranslate"><span class="pre">geopandas</span></code> dependency.
So please install <code class="docutils literal notranslate"><span class="pre">gerrychain</span></code> with the <code class="docutils literal notranslate"><span class="pre">geo</span></code> extra
via the command:</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">pip install gerrychain[geo]</span>
</pre></div>
</div>
<p>or install <code class="docutils literal notranslate"><span class="pre">geopandas</span></code> separately.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.from_geodataframe">
<em class="property">classmethod </em><code class="sig-name descname">from_geodataframe</code><span class="sig-paren">(</span><em class="sig-param">dataframe: <Mock name='mock.DataFrame' id='140260702472032'></em>, <em class="sig-param">adjacency: str = 'rook'</em>, <em class="sig-param">cols_to_add: Optional[List[str]] = None</em>, <em class="sig-param">reproject: bool = False</em>, <em class="sig-param">ignore_errors: bool = False</em><span class="sig-paren">)</span> → gerrychain.graph.graph.Graph<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.from_geodataframe"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.from_geodataframe" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates the adjacency <a class="reference internal" href="#gerrychain.graph.graph.Graph" title="gerrychain.graph.graph.Graph"><code class="xref py py-class docutils literal notranslate"><span class="pre">Graph</span></code></a> of geometries described by <cite>dataframe</cite>.
The areas of the polygons are included as node attributes (with key <cite>area</cite>).
The shared perimeter of neighboring polygons are included as edge attributes
(with key <cite>shared_perim</cite>).
Nodes corresponding to polygons on the boundary of the union of all the geometries
(e.g., the state, if your dataframe describes VTDs) have a <cite>boundary_node</cite> attribute
(set to <cite>True</cite>) and a <cite>boundary_perim</cite> attribute with the length of this “exterior”
boundary.</p>
<p>By default, areas and lengths are computed in a UTM projection suitable for the
geometries. This prevents the bizarro area and perimeter values that show up when
you accidentally do computations in Longitude-Latitude coordinates. If the user
specifies <cite>reproject=False</cite>, then the areas and lengths will be computed in the
GeoDataFrame’s current coordinate reference system. This option is for users who
have a preferred CRS they would like to use.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>dataframe</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">geopandas.GeoDataFrame</span></code>) – The GeoDateFrame to convert</p></li>
<li><p><strong>adjacency</strong> (<em>str</em><em>, </em><em>optional</em>) – The adjacency type to use (“rook” or “queen”).
Default is “rook”.</p></li>
<li><p><strong>cols_to_add</strong> (<em>Optional</em><em>[</em><em>List</em><em>[</em><em>str</em><em>]</em><em>]</em><em>, </em><em>optional</em>) – The names of the columns that you want to
add to the graph as node attributes. Default is None.</p></li>
<li><p><strong>reproject</strong> (<em>bool</em><em>, </em><em>optional</em>) – Whether to reproject to a UTM projection before
creating the graph. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p></li>
<li><p><strong>ignore_errors</strong> (<em>bool</em><em>, </em><em>optional</em>) – Whether to ignore all invalid geometries and
attept to create the graph anyway. Default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The adjacency graph of the geometries from <cite>dataframe</cite>.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.graph.graph.Graph" title="gerrychain.graph.graph.Graph">Graph</a></p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.from_json">
<em class="property">classmethod </em><code class="sig-name descname">from_json</code><span class="sig-paren">(</span><em class="sig-param">json_file: str</em><span class="sig-paren">)</span> → gerrychain.graph.graph.Graph<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.from_json"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.from_json" title="Permalink to this definition">¶</a></dt>
<dd><p>Load a graph from a JSON file in the NetworkX json_graph format.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>json_file</strong> (<em>str</em>) – Path to JSON file.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The loaded graph as an instance of this class.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.graph.graph.Graph" title="gerrychain.graph.graph.Graph">Graph</a></p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.from_networkx">
<em class="property">classmethod </em><code class="sig-name descname">from_networkx</code><span class="sig-paren">(</span><em class="sig-param">graph: networkx.classes.graph.Graph</em><span class="sig-paren">)</span> → gerrychain.graph.graph.Graph<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.from_networkx"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.from_networkx" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a Graph instance from a networkx.Graph object.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>graph</strong> (<em>networkx.Graph</em>) – The networkx graph to be converted.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The converted graph as an instance of this class.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.graph.graph.Graph" title="gerrychain.graph.graph.Graph">Graph</a></p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.islands">
<em class="property">property </em><code class="sig-name descname">islands</code><a class="headerlink" href="#gerrychain.graph.graph.Graph.islands" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>The set of degree-0 nodes.</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>Set</p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.issue_warnings">
<code class="sig-name descname">issue_warnings</code><span class="sig-paren">(</span><span class="sig-paren">)</span> → None<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.issue_warnings"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.issue_warnings" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>None</p>
</dd>
<dt class="field-even">Raises</dt>
<dd class="field-even"><p>UserWarning if the graph has any red flags (right now, only islands).</p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.join">
<code class="sig-name descname">join</code><span class="sig-paren">(</span><em class="sig-param">dataframe: <Mock name='mock.DataFrame' id='140260702472032'></em>, <em class="sig-param">columns: Optional[List[str]] = None</em>, <em class="sig-param">left_index: Optional[str] = None</em>, <em class="sig-param">right_index: Optional[str] = None</em><span class="sig-paren">)</span> → None<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.join"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.join" title="Permalink to this definition">¶</a></dt>
<dd><p>Add data from a dataframe to the graph, matching nodes to rows when
the node’s <cite>left_index</cite> attribute equals the row’s <cite>right_index</cite> value.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>dataframe</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">pandas.DataFrame</span></code>) – DataFrame.</p>
</dd>
<dt class="field-even">Columns</dt>
<dd class="field-even"><p>The columns whose data you wish to add to the graph.
If not provided, all columns are added. Default is None.</p>
</dd>
<dt class="field-odd">Left_index</dt>
<dd class="field-odd"><p>The node attribute used to match nodes to rows.
If not provided, node IDs are used. Default is None.</p>
</dd>
<dt class="field-even">Right_index</dt>
<dd class="field-even"><p>The DataFrame column name to use to match rows
to nodes. If not provided, the DataFrame’s index is used. Default is None.</p>
</dd>
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>None</p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.lookup">
<code class="sig-name descname">lookup</code><span class="sig-paren">(</span><em class="sig-param">node: Any</em>, <em class="sig-param">field: Any</em><span class="sig-paren">)</span> → Any<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.lookup"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.lookup" title="Permalink to this definition">¶</a></dt>
<dd><p>Lookup a node/field attribute.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>node</strong> (<em>Any</em>) – Node to look up.</p></li>
<li><p><strong>field</strong> (<em>Any</em>) – Field to look up.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The value of the attribute <cite>field</cite> at <cite>node</cite>.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>Any</p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.to_json">
<code class="sig-name descname">to_json</code><span class="sig-paren">(</span><em class="sig-param">json_file: str</em>, <em class="sig-param">*</em>, <em class="sig-param">include_geometries_as_geojson: bool = False</em><span class="sig-paren">)</span> → None<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.to_json"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.to_json" title="Permalink to this definition">¶</a></dt>
<dd><p>Save a graph to a JSON file in the NetworkX json_graph format.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>json_file</strong> (<em>str</em>) – Path to target JSON file.</p></li>
<li><p><strong>include_geometry_as_geojson</strong> (<em>bool</em>) – Whether to include
any <code class="xref py py-mod docutils literal notranslate"><span class="pre">shapely</span></code> geometry objects encountered in the graph’s node
attributes as GeoJSON. The default (<code class="docutils literal notranslate"><span class="pre">False</span></code>) behavior is to remove
all geometry objects because they are not serializable. Including the
GeoJSON will result in a much larger JSON file.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>None</p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.graph.graph.Graph.warn_for_islands">
<code class="sig-name descname">warn_for_islands</code><span class="sig-paren">(</span><span class="sig-paren">)</span> → None<a class="reference internal" href="_modules/gerrychain/graph/graph.html#Graph.warn_for_islands"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.graph.graph.Graph.warn_for_islands" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>None</p>
</dd>
<dt class="field-even">Raises</dt>
<dd class="field-even"><p>UserWarning if the graph has any islands (degree-0 nodes).</p>
</dd>
</dl>
</dd></dl>
</dd></dl>
</div>
<div class="section" id="module-gerrychain.partition">
<span id="partitions"></span><h2><a class="toc-backref" href="#id7">Partitions</a><a class="headerlink" href="#module-gerrychain.partition" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="gerrychain.partition.Partition">
<em class="property">class </em><code class="sig-prename descclassname">gerrychain.partition.</code><code class="sig-name descname">Partition</code><span class="sig-paren">(</span><em class="sig-param">graph=None</em>, <em class="sig-param">assignment=None</em>, <em class="sig-param">updaters=None</em>, <em class="sig-param">parent=None</em>, <em class="sig-param">flips=None</em>, <em class="sig-param">use_default_updaters=True</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/partition/partition.html#Partition"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.partition.Partition" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></p>
<p>Partition represents a partition of the nodes of the graph. It will perform
the first layer of computations at each step in the Markov chain - basic
aggregations and calculations that we want to optimize.</p>
<dl class="field-list simple">
<dt class="field-odd">Variables</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>graph</strong> – The underlying graph.</p></li>
<li><p><strong>assignment</strong> – Maps node IDs to district IDs.</p></li>
<li><p><strong>parts</strong> – Maps district IDs to the set of nodes in that district.</p></li>
<li><p><strong>subgraphs</strong> – Maps district IDs to the induced subgraph of that district.</p></li>
</ul>
</dd>
<dt class="field-even">Parameters</dt>
<dd class="field-even"><ul class="simple">
<li><p><strong>graph</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">Graph</span></code>) – Underlying graph.</p></li>
<li><p><strong>assignment</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">Assignment</span></code>) – Dictionary assigning nodes to districts.</p></li>
<li><p><strong>updaters</strong> – Dictionary of functions to track data about the partition.
The keys are stored as attributes on the partition class,
which the functions compute.</p></li>
<li><p><strong>use_default_updaters</strong> – If <cite>False</cite>, do not include default updaters.</p></li>
</ul>
</dd>
</dl>
<dl class="method">
<dt id="gerrychain.partition.Partition.crosses_parts">
<code class="sig-name descname">crosses_parts</code><span class="sig-paren">(</span><em class="sig-param">edge: Tuple</em><span class="sig-paren">)</span> → bool<a class="reference internal" href="_modules/gerrychain/partition/partition.html#Partition.crosses_parts"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.partition.Partition.crosses_parts" title="Permalink to this definition">¶</a></dt>
<dd><dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>edge</strong> (<em>Tuple</em>) – tuple of node IDs</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>True if the edge crosses from one part of the partition to another</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>bool</p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.partition.Partition.flip">
<code class="sig-name descname">flip</code><span class="sig-paren">(</span><em class="sig-param">flips: Dict</em><span class="sig-paren">)</span> → gerrychain.partition.partition.Partition<a class="reference internal" href="_modules/gerrychain/partition/partition.html#Partition.flip"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.partition.Partition.flip" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the new partition obtained by performing the given <cite>flips</cite>
on this partition.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>flips</strong> – dictionary assigning nodes of the graph to their new districts</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>the new <a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><code class="xref py py-class docutils literal notranslate"><span class="pre">Partition</span></code></a></p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition">Partition</a></p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.partition.Partition.from_districtr_file">
<em class="property">classmethod </em><code class="sig-name descname">from_districtr_file</code><span class="sig-paren">(</span><em class="sig-param">graph: gerrychain.graph.graph.Graph</em>, <em class="sig-param">districtr_file: str</em>, <em class="sig-param">updaters: Optional[Dict[str</em>, <em class="sig-param">Callable]] = None</em><span class="sig-paren">)</span> → gerrychain.partition.partition.Partition<a class="reference internal" href="_modules/gerrychain/partition/partition.html#Partition.from_districtr_file"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.partition.Partition.from_districtr_file" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a Partition from a districting plan created with <a class="reference external" href="https://mggg.org/Districtr">Districtr</a>,
a free and open-source web app created by MGGG for drawing districts.</p>
<p>The provided <code class="docutils literal notranslate"><span class="pre">graph</span></code> should be created from the same shapefile as the
Districtr module used to draw the districting plan. These shapefiles may
be found in a repository in the <a class="reference external" href="https://github.com/mggg-states">mggg-states</a> GitHub organization, or by
request from MGGG.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>graph</strong> (<code class="xref py py-class docutils literal notranslate"><span class="pre">Graph</span></code>) – The graph to create the Partition from</p></li>
<li><p><strong>districtr_file</strong> (<em>str</em>) – the path to the <code class="docutils literal notranslate"><span class="pre">.json</span></code> file exported from Districtr</p></li>
<li><p><strong>updaters</strong> (<em>Optional</em><em>[</em><em>Dict</em><em>[</em><em>str</em><em>, </em><em>Callable</em><em>]</em><em>]</em><em>, </em><em>optional</em>) – dictionary of updaters</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The partition created from the Districtr file</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition">Partition</a></p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.partition.Partition.plot">
<code class="sig-name descname">plot</code><span class="sig-paren">(</span><em class="sig-param">geometries=None</em>, <em class="sig-param">**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/partition/partition.html#Partition.plot"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.partition.Partition.plot" title="Permalink to this definition">¶</a></dt>
<dd><p>Plot the partition, using the provided geometries.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>geometries</strong> (<em>geopandas.GeoDataFrame</em><em> or </em><em>geopandas.GeoSeries</em>) – A <code class="xref py py-class docutils literal notranslate"><span class="pre">geopandas.GeoDataFrame</span></code> or <code class="xref py py-class docutils literal notranslate"><span class="pre">geopandas.GeoSeries</span></code>
holding the geometries to use for plotting. Its <code class="xref py py-class docutils literal notranslate"><span class="pre">Index</span></code> should match
the node labels of the partition’s underlying <code class="xref py py-class docutils literal notranslate"><span class="pre">Graph</span></code>.</p></li>
<li><p><strong>**kwargs</strong> – Additional arguments to pass to <code class="xref py py-meth docutils literal notranslate"><span class="pre">geopandas.GeoDataFrame.plot()</span></code>
to adjust the plot.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The matplotlib axes object. Which plots the Partition.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>matplotlib.axes.Axes</p>
</dd>
</dl>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="gerrychain.partition.GeographicPartition">
<em class="property">class </em><code class="sig-prename descclassname">gerrychain.partition.</code><code class="sig-name descname">GeographicPartition</code><span class="sig-paren">(</span><em class="sig-param">graph=None</em>, <em class="sig-param">assignment=None</em>, <em class="sig-param">updaters=None</em>, <em class="sig-param">parent=None</em>, <em class="sig-param">flips=None</em>, <em class="sig-param">use_default_updaters=True</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/partition/geographic.html#GeographicPartition"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.partition.GeographicPartition" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <code class="xref py py-class docutils literal notranslate"><span class="pre">gerrychain.partition.partition.Partition</span></code></p>
<p>A <a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><code class="xref py py-class docutils literal notranslate"><span class="pre">Partition</span></code></a> with areas, perimeters, and boundary information included.
These additional data allow you to compute compactness scores like
<a class="reference external" href="https://en.wikipedia.org/wiki/Polsby-Popper_Test">Polsby-Popper</a>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>graph</strong> – Underlying graph.</p></li>
<li><p><strong>assignment</strong> – Dictionary assigning nodes to districts.</p></li>
<li><p><strong>updaters</strong> – Dictionary of functions to track data about the partition.
The keys are stored as attributes on the partition class,
which the functions compute.</p></li>
<li><p><strong>use_default_updaters</strong> – If <cite>False</cite>, do not include default updaters.</p></li>
</ul>
</dd>
</dl>
</dd></dl>
</div>
<div class="section" id="markov-chains">
<h2><a class="toc-backref" href="#id8">Markov chains</a><a class="headerlink" href="#markov-chains" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="gerrychain.MarkovChain">
<em class="property">class </em><code class="sig-prename descclassname">gerrychain.</code><code class="sig-name descname">MarkovChain</code><span class="sig-paren">(</span><em class="sig-param">proposal: Callable, constraints: Union[Iterable[Callable], gerrychain.constraints.validity.Validator, Iterable[gerrychain.constraints.bounds.Bounds], Callable], accept: Callable, initial_state: Optional[gerrychain.partition.partition.Partition], total_steps: int</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/chain.html#MarkovChain"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.MarkovChain" title="Permalink to this definition">¶</a></dt>
<dd><p>MarkovChain is a class that creates an iterator for iterating over the states
of a Markov chain run in a gerrymandering analysis context.</p>
<p>It allows for the generation of a sequence of partitions (states) of a political
districting plan, where each partition represents a possible state in the Markov chain.</p>
<p>Example usage:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">chain</span> <span class="o">=</span> <span class="n">MarkovChain</span><span class="p">(</span><span class="n">proposal</span><span class="p">,</span> <span class="n">constraints</span><span class="p">,</span> <span class="n">accept</span><span class="p">,</span> <span class="n">initial_state</span><span class="p">,</span> <span class="n">total_steps</span><span class="p">)</span>
<span class="k">for</span> <span class="n">state</span> <span class="ow">in</span> <span class="n">chain</span><span class="p">:</span>
<span class="c1"># Do whatever you want - print output, compute scores, ...</span>
</pre></div>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>proposal</strong> (<em>Callable</em>) – Function proposing the next state from the current state.</p></li>
<li><p><strong>constraints</strong> (<em>Union</em><em>[</em><em>Iterable</em><em>[</em><em>Callable</em><em>]</em><em>, </em><a class="reference internal" href="#gerrychain.constraints.Validator" title="gerrychain.constraints.Validator"><em>Validator</em></a><em>, </em><em>Iterable</em><em>[</em><a class="reference internal" href="#gerrychain.constraints.Bounds" title="gerrychain.constraints.Bounds"><em>Bounds</em></a><em>]</em><em>, </em><em>Callable</em><em>]</em>) – A function with signature <code class="docutils literal notranslate"><span class="pre">Partition</span> <span class="pre">-></span> <span class="pre">bool</span></code> determining whether
the proposed next state is valid (passes all binary constraints). Usually
this is a <a class="reference internal" href="#gerrychain.constraints.Validator" title="gerrychain.constraints.Validator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Validator</span></code></a> class instance.</p></li>
<li><p><strong>accept</strong> (<em>Callable</em>) – Function accepting or rejecting the proposed state. In the most basic
use case, this always returns <code class="docutils literal notranslate"><span class="pre">True</span></code>. But if the user wanted to use a
Metropolis-Hastings acceptance rule, this is where you would implement it.</p></li>
<li><p><strong>initial_state</strong> (<em>Optional</em><em>[</em><a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a><em>]</em>) – Initial <a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><code class="xref py py-class docutils literal notranslate"><span class="pre">gerrychain.partition.Partition</span></code></a> class.</p></li>
<li><p><strong>total_steps</strong> (<em>int</em>) – Number of steps to run.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>None</p>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><p><strong>ValueError</strong> – If the initial_state is not valid according to the constraints.</p>
</dd>
</dl>
<dl class="method">
<dt id="gerrychain.MarkovChain.constraints">
<em class="property">property </em><code class="sig-name descname">constraints</code><a class="headerlink" href="#gerrychain.MarkovChain.constraints" title="Permalink to this definition">¶</a></dt>
<dd><p>Read_only alias for the is_valid property.
Returns the constraints of the Markov chain.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>The constraints of the Markov chain.</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>String</p>
</dd>
</dl>
</dd></dl>
<dl class="method">
<dt id="gerrychain.MarkovChain.with_progress_bar">
<code class="sig-name descname">with_progress_bar</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/chain.html#MarkovChain.with_progress_bar"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.MarkovChain.with_progress_bar" title="Permalink to this definition">¶</a></dt>
<dd><p>Wraps the Markov chain in a tqdm progress bar.</p>
<p>Useful for long-running Markov chains where you want to keep track
of the progress. Requires the <cite>tqdm</cite> package to be installed.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>A tqdm-wrapped Markov chain.</p>
</dd>
</dl>
</dd></dl>
</dd></dl>
</div>
<div class="section" id="module-gerrychain.proposals">
<span id="proposals"></span><h2><a class="toc-backref" href="#id9">Proposals</a><a class="headerlink" href="#module-gerrychain.proposals" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="gerrychain.proposals.recom">
<code class="sig-prename descclassname">gerrychain.proposals.</code><code class="sig-name descname">recom</code><span class="sig-paren">(</span><em class="sig-param">partition: gerrychain.partition.partition.Partition, pop_col: str, pop_target: Union[int, float], epsilon: float, node_repeats: int = 1, weight_dict: Optional[Dict] = None, method: Callable = <function bipartition_tree></em><span class="sig-paren">)</span> → gerrychain.partition.partition.Partition<a class="reference internal" href="_modules/gerrychain/proposals/tree_proposals.html#recom"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.proposals.recom" title="Permalink to this definition">¶</a></dt>
<dd><p>ReCom (short for ReCombination) is a Markov Chain Monte Carlo (MCMC) algorithm
used for redistricting. At each step of the algorithm, a pair of adjacent districts
is selected at random and merged into a single district. The region is then split
into two new districts by generating a spanning tree using the Kruskal/Karger
algorithm and cutting an edge at random. The edge is checked to ensure that it
separates the region into two new districts that are population balanced, and,
if not, a new edge is selected at random and the process is repeated.</p>
<p>Example usage:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">partial</span>
<span class="kn">from</span> <span class="nn">gerrychain</span> <span class="kn">import</span> <span class="n">MarkovChain</span>
<span class="kn">from</span> <span class="nn">gerrychain.proposals</span> <span class="kn">import</span> <span class="n">recom</span>
<span class="c1"># ...define constraints, accept, partition, total_steps here...</span>
<span class="c1"># Ideal population:</span>
<span class="n">pop_target</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">partition</span><span class="p">[</span><span class="s2">"population"</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">())</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">partition</span><span class="p">)</span>
<span class="n">proposal</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span>
<span class="n">recom</span><span class="p">,</span> <span class="n">pop_col</span><span class="o">=</span><span class="s2">"POP10"</span><span class="p">,</span> <span class="n">pop_target</span><span class="o">=</span><span class="n">pop_target</span><span class="p">,</span> <span class="n">epsilon</span><span class="o">=.</span><span class="mi">05</span><span class="p">,</span> <span class="n">node_repeats</span><span class="o">=</span><span class="mi">10</span>
<span class="p">)</span>
<span class="n">chain</span> <span class="o">=</span> <span class="n">MarkovChain</span><span class="p">(</span><span class="n">proposal</span><span class="p">,</span> <span class="n">constraints</span><span class="p">,</span> <span class="n">accept</span><span class="p">,</span> <span class="n">partition</span><span class="p">,</span> <span class="n">total_steps</span><span class="p">)</span>
</pre></div>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>partition</strong> (<a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a>) – The initial partition.</p></li>
<li><p><strong>pop_col</strong> (<em>str</em>) – The name of the population column.</p></li>
<li><p><strong>pop_target</strong> (<em>Union</em><em>[</em><em>int</em><em>,</em><em>float</em><em>]</em>) – The target population for each district.</p></li>
<li><p><strong>epsilon</strong> (<em>float</em>) – The epsilon value for population deviation as a percentage of the
target population.</p></li>
<li><p><strong>node_repeats</strong> (<em>int</em><em>, </em><em>optional</em>) – The number of times to repeat the bipartitioning step. Default is 1.</p></li>
<li><p><strong>weight_dict</strong> (<em>Optional</em><em>[</em><em>Dict</em><em>]</em><em>, </em><em>optional</em>) – The weight dictionary for the graph used for region-aware
partitioning of the grid. Default is None.</p></li>
<li><p><strong>method</strong> (<em>Callable</em><em>, </em><em>optional</em>) – The method used for bipartitioning the tree. Default is
<a class="reference internal" href="#gerrychain.tree.bipartition_tree" title="gerrychain.tree.bipartition_tree"><code class="xref py py-func docutils literal notranslate"><span class="pre">bipartition_tree()</span></code></a>.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The new partition resulting from the ReCom algorithm.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition">Partition</a></p>
</dd>
<dt class="field-even">Raises</dt>
<dd class="field-even"><p><strong>ValueWarning</strong> – Raised when the sum of the weights in the weight dictionary is
greater than 1.</p>
</dd>
</dl>
</dd></dl>
<dl class="function">
<dt id="gerrychain.proposals.reversible_recom">
<code class="sig-prename descclassname">gerrychain.proposals.</code><code class="sig-name descname">reversible_recom</code><span class="sig-paren">(</span><em class="sig-param">partition: gerrychain.partition.partition.Partition, pop_col: str, pop_target: Union[int, float], epsilon: float, balance_edge_fn: Callable = <function find_balanced_edge_cuts_memoization>, M: int = 1, repeat_until_valid: bool = False, choice: Callable = <bound method Random.choice of <random.Random object>></em><span class="sig-paren">)</span> → gerrychain.partition.partition.Partition<a class="reference internal" href="_modules/gerrychain/proposals/tree_proposals.html#reversible_recom"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.proposals.reversible_recom" title="Permalink to this definition">¶</a></dt>
<dd><p>Reversible ReCom algorithm for redistricting.</p>
<p>This function performs the reversible ReCom algorithm, which is a Markov Chain Monte
Carlo (MCMC) algorithm used for redistricting. For more information, see the paper
“Spanning Tree Methods for Sampling Graph Partitions” by Cannon, et al. (2022) at
<a class="reference external" href="https://arxiv.org/abs/2210.01401">https://arxiv.org/abs/2210.01401</a></p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>partition</strong> (<a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a>) – The initial partition.</p></li>
<li><p><strong>pop_col</strong> (<em>str</em>) – The name of the population column.</p></li>
<li><p><strong>pop_target</strong> (<em>Union</em><em>[</em><em>int</em><em>,</em><em>float</em><em>]</em>) – The target population for each district.</p></li>
<li><p><strong>epsilon</strong> (<em>float</em>) – The epsilon value for population deviation as a percentage of the
target population.</p></li>
<li><p><strong>balance_edge_fn</strong> (<em>Callable</em><em>, </em><em>optional</em>) – The balance edge function. Default is
find_balanced_edge_cuts_memoization.</p></li>
<li><p><strong>M</strong> (<em>int</em><em>, </em><em>optional</em>) – The maximum number of balance edges. Default is 1.</p></li>
<li><p><strong>repeat_until_valid</strong> (<em>bool</em><em>, </em><em>optional</em>) – Flag indicating whether to repeat until a valid partition is
found. Default is False.</p></li>
<li><p><strong>choice</strong> (<em>Callable</em><em>, </em><em>optional</em>) – The choice function for selecting a random element. Default is random.choice.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The new partition resulting from the reversible ReCom algorithm.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition">Partition</a></p>
</dd>
</dl>
</dd></dl>
<dl class="function">
<dt id="gerrychain.proposals.spectral_recom">
<code class="sig-prename descclassname">gerrychain.proposals.</code><code class="sig-name descname">spectral_recom</code><span class="sig-paren">(</span><em class="sig-param">partition: gerrychain.partition.partition.Partition</em>, <em class="sig-param">weight_type: Optional[str] = None</em>, <em class="sig-param">lap_type: str = 'normalized'</em><span class="sig-paren">)</span> → gerrychain.partition.partition.Partition<a class="reference internal" href="_modules/gerrychain/proposals/spectral_proposals.html#spectral_recom"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.proposals.spectral_recom" title="Permalink to this definition">¶</a></dt>
<dd><p>Spectral ReCom proposal.</p>
<p>Uses spectral clustering to bipartition a subgraph of the original graph
formed by merging the nodes corresponding to two adjacent districts.</p>
<p>Example usage:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">partial</span>
<span class="kn">from</span> <span class="nn">gerrychain</span> <span class="kn">import</span> <span class="n">MarkovChain</span>
<span class="kn">from</span> <span class="nn">gerrychain.proposals</span> <span class="kn">import</span> <span class="n">recom</span>
<span class="c1"># ...define constraints, accept, partition, total_steps here...</span>
<span class="n">proposal</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span>
<span class="n">spectral_recom</span><span class="p">,</span> <span class="n">weight_type</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lap_type</span><span class="o">=</span><span class="s2">"normalized"</span>
<span class="p">)</span>
<span class="n">chain</span> <span class="o">=</span> <span class="n">MarkovChain</span><span class="p">(</span><span class="n">proposal</span><span class="p">,</span> <span class="n">constraints</span><span class="p">,</span> <span class="n">accept</span><span class="p">,</span> <span class="n">partition</span><span class="p">,</span> <span class="n">total_steps</span><span class="p">)</span>
</pre></div>
</div>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>partition</strong> (<a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a>) – The initial partition.</p></li>
<li><p><strong>weight_type</strong> (<em>Optional</em><em>[</em><em>str</em><em>]</em><em>, </em><em>optional</em>) – The type of weight to be used in the Laplacian. Default is None.</p></li>
<li><p><strong>lap_type</strong> (<em>str</em><em>, </em><em>optional</em>) – The type of Laplacian to be used. Default is “normalized”.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The new partition resulting from the spectral ReCom algorithm.</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition">Partition</a></p>
</dd>
</dl>
</dd></dl>
<dl class="function">
<dt id="gerrychain.proposals.propose_chunk_flip">
<code class="sig-prename descclassname">gerrychain.proposals.</code><code class="sig-name descname">propose_chunk_flip</code><span class="sig-paren">(</span><em class="sig-param">partition: gerrychain.partition.partition.Partition</em><span class="sig-paren">)</span> → gerrychain.partition.partition.Partition<a class="reference internal" href="_modules/gerrychain/proposals/proposals.html#propose_chunk_flip"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.proposals.propose_chunk_flip" title="Permalink to this definition">¶</a></dt>
<dd><p>Chooses a random boundary node and proposes to flip it and all of its neighbors</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>partition</strong> (<a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a>) – The current partition to propose a flip from.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>A possible next <cite>~gerrychain.Partition</cite></p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition">Partition</a></p>
</dd>
</dl>
</dd></dl>
<dl class="function">
<dt id="gerrychain.proposals.propose_random_flip">
<code class="sig-prename descclassname">gerrychain.proposals.</code><code class="sig-name descname">propose_random_flip</code><span class="sig-paren">(</span><em class="sig-param">partition: gerrychain.partition.partition.Partition</em><span class="sig-paren">)</span> → gerrychain.partition.partition.Partition<a class="reference internal" href="_modules/gerrychain/proposals/proposals.html#propose_random_flip"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.proposals.propose_random_flip" title="Permalink to this definition">¶</a></dt>
<dd><p>Proposes a random boundary flip from the partition.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>partition</strong> (<a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a>) – The current partition to propose a flip from.</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>A possible next <cite>~gerrychain.Partition</cite></p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p><a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition">Partition</a></p>
</dd>
</dl>
</dd></dl>
</div>
<div class="section" id="module-gerrychain.constraints">
<span id="binary-constraints"></span><h2><a class="toc-backref" href="#id10">Binary constraints</a><a class="headerlink" href="#module-gerrychain.constraints" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#module-gerrychain.constraints" title="gerrychain.constraints"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gerrychain.constraints</span></code></a> module provides a collection of constraint
functions and helper classes for the validation step in GerryChain.</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 44%" />
<col style="width: 56%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head" colspan="2"><p>Helper classes</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><a class="reference internal" href="#gerrychain.constraints.Validator" title="gerrychain.constraints.Validator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Validator</span></code></a></p></td>
<td><p>Collection of constraints</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#gerrychain.constraints.Bounds" title="gerrychain.constraints.Bounds"><code class="xref py py-class docutils literal notranslate"><span class="pre">Bounds</span></code></a></p></td>
<td><p>Bounds on numeric constraints</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#gerrychain.constraints.UpperBound" title="gerrychain.constraints.UpperBound"><code class="xref py py-class docutils literal notranslate"><span class="pre">UpperBound</span></code></a></p></td>
<td><p>Upper bounds on numeric constraints</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#gerrychain.constraints.LowerBound" title="gerrychain.constraints.LowerBound"><code class="xref py py-class docutils literal notranslate"><span class="pre">LowerBound</span></code></a></p></td>
<td><p>Lower bounds on numeric constraints</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#gerrychain.constraints.SelfConfiguringUpperBound" title="gerrychain.constraints.SelfConfiguringUpperBound"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelfConfiguringUpperBound</span></code></a></p></td>
<td><p>Automatic upper bounds on numeric constraints</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#gerrychain.constraints.SelfConfiguringLowerBound" title="gerrychain.constraints.SelfConfiguringLowerBound"><code class="xref py py-class docutils literal notranslate"><span class="pre">SelfConfiguringLowerBound</span></code></a></p></td>
<td><p>Automatic lower bounds on numeric constraints</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#gerrychain.constraints.WithinPercentRangeOfBounds" title="gerrychain.constraints.WithinPercentRangeOfBounds"><code class="xref py py-class docutils literal notranslate"><span class="pre">WithinPercentRangeOfBounds</span></code></a></p></td>
<td><p>Percentage bounds for numeric constraints</p></td>
</tr>
</tbody>
</table>
<div class="line-block">
<div class="line"><br /></div>
</div>
<table class="docutils align-default">
<colgroup>
<col style="width: 40%" />
<col style="width: 60%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head" colspan="2"><p>Binary constraint functions</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">no_worse_L1_reciprocal_polsby_popper()</span></code></p></td>
<td><p>Lower bounded L1-reciprocal Polsby-Popper</p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">no_worse_L_minus_1_reciprocal_polsby_popper()</span></code></p></td>
<td><p>Lower bounded L(-1)-reciprocal Polsby-Popper</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#gerrychain.constraints.contiguous" title="gerrychain.constraints.contiguous"><code class="xref py py-meth docutils literal notranslate"><span class="pre">contiguous()</span></code></a></p></td>
<td><p>Districts are contiguous (with NetworkX methods)</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#gerrychain.constraints.single_flip_contiguous" title="gerrychain.constraints.single_flip_contiguous"><code class="xref py py-meth docutils literal notranslate"><span class="pre">single_flip_contiguous()</span></code></a></p></td>
<td><p>Districts are contiguous (optimized for <code class="docutils literal notranslate"><span class="pre">propose_random_flip</span></code> proposal)</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#gerrychain.constraints.no_vanishing_districts" title="gerrychain.constraints.no_vanishing_districts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">no_vanishing_districts()</span></code></a></p></td>
<td><p>No districts may be completely consumed</p></td>
</tr>
</tbody>
</table>
<p>Each new step proposed to the chain is passed off to the “validator” functions
here to determine whether or not the step is valid. If it is invalid (breaks
contiguity, for instance), then the step is immediately rejected.</p>
<p>A validator should take in a <a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><code class="xref py py-class docutils literal notranslate"><span class="pre">Partition</span></code></a> instance,
and should return whether or not the instance is valid according to their
rules. Many top-level functions following this signature in this module are
examples of this.</p>
<dl class="class">
<dt id="gerrychain.constraints.LowerBound">
<em class="property">class </em><code class="sig-prename descclassname">gerrychain.constraints.</code><code class="sig-name descname">LowerBound</code><span class="sig-paren">(</span><em class="sig-param">func: Callable</em>, <em class="sig-param">bound: float</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/constraints/bounds.html#LowerBound"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.constraints.LowerBound" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrapper for numeric-validators to enforce lower limits.</p>
<p>This class is meant to be called as a function after instantiation; its
return is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the numeric validator is within a set lower limit,
and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>func</strong> (<em>Callable</em>) – Numeric validator function. Should return a comparable value.</p></li>
<li><p><strong>bounds</strong> (<em>float</em>) – Comparable lower bound.</p></li>
</ul>
</dd>
</dl>
</dd></dl>
<dl class="class">
<dt id="gerrychain.constraints.SelfConfiguringLowerBound">
<em class="property">class </em><code class="sig-prename descclassname">gerrychain.constraints.</code><code class="sig-name descname">SelfConfiguringLowerBound</code><span class="sig-paren">(</span><em class="sig-param">func: Callable</em>, <em class="sig-param">epsilon: float = 0.05</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/constraints/bounds.html#SelfConfiguringLowerBound"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.constraints.SelfConfiguringLowerBound" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrapper for numeric-validators to enforce automatic lower limits.</p>
<p>When instantiated, the initial lower bound is set as the initial value of
the numeric-validator minus some configurable ε.</p>
<p>This class is meant to be called as a function after instantiation; its
return is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the numeric validator is within a set lower limit,
and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>func</strong> (<em>Callable</em>) – Numeric validator function.</p></li>
<li><p><strong>epsilon</strong> (<em>float</em><em>, </em><em>optional</em>) – Initial population deviation allowable by the validator
as a percentage of the ideal population. Defaults to 0.05.</p></li>
</ul>
</dd>
</dl>
</dd></dl>
<dl class="class">
<dt id="gerrychain.constraints.SelfConfiguringUpperBound">
<em class="property">class </em><code class="sig-prename descclassname">gerrychain.constraints.</code><code class="sig-name descname">SelfConfiguringUpperBound</code><span class="sig-paren">(</span><em class="sig-param">func: Callable</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/constraints/bounds.html#SelfConfiguringUpperBound"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.constraints.SelfConfiguringUpperBound" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrapper for numeric-validators to enforce automatic upper limits.</p>
<p>When instantiated, the initial upper bound is set as the initial value of
the numeric-validator.</p>
<p>This class is meant to be called as a function after instantiation; its
return is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the numeric validator is within a set upper limit,
and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>func</strong> (<em>Callable</em>) – Numeric validator function.</p>
</dd>
</dl>
</dd></dl>
<dl class="class">
<dt id="gerrychain.constraints.UpperBound">
<em class="property">class </em><code class="sig-prename descclassname">gerrychain.constraints.</code><code class="sig-name descname">UpperBound</code><span class="sig-paren">(</span><em class="sig-param">func: Callable</em>, <em class="sig-param">bound: float</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/constraints/bounds.html#UpperBound"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.constraints.UpperBound" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrapper for numeric-validators to enforce upper limits.</p>
<p>This class is meant to be called as a function after instantiation; its
return is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the numeric validator is within a set upper limit,
and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>func</strong> (<em>Callable</em>) – Numeric validator function. Should return a comparable value.</p></li>
<li><p><strong>bounds</strong> (<em>float</em>) – Comparable upper bound.</p></li>
</ul>
</dd>
</dl>
</dd></dl>
<dl class="class">
<dt id="gerrychain.constraints.WithinPercentRangeOfBounds">
<em class="property">class </em><code class="sig-prename descclassname">gerrychain.constraints.</code><code class="sig-name descname">WithinPercentRangeOfBounds</code><span class="sig-paren">(</span><em class="sig-param">func: Callable</em>, <em class="sig-param">percent: float</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/constraints/bounds.html#WithinPercentRangeOfBounds"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.constraints.WithinPercentRangeOfBounds" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrapper for numeric-validators to enforce upper and lower limits
determined by a percentage of the initial value.</p>
<p>When instantiated, the initial upper and lower bounds are set as the
initial value of the numeric-validator times (1 ± percent).</p>
<p>This class is meant to be called as a function after instantiation; its
return is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the numeric validator is within the desired
percentage range of the initial value, and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>func</strong> (<em>Callable</em>) – Numeric validator function.</p></li>
<li><p><strong>percent</strong> (<em>float</em>) – Percentage of the initial value to use as the bounds.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>None</p>
</dd>
</dl>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The percentage is assumed to be in the range [0.0, 100.0].</p>
</div>
</dd></dl>
<dl class="function">
<dt id="gerrychain.constraints.L1_polsby_popper">
<code class="sig-prename descclassname">gerrychain.constraints.</code><code class="sig-name descname">L1_polsby_popper</code><span class="sig-paren">(</span><em class="sig-param">partition: gerrychain.partition.partition.Partition</em><span class="sig-paren">)</span> → float<a class="reference internal" href="_modules/gerrychain/constraints/compactness.html#L1_polsby_popper"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.constraints.L1_polsby_popper" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the <span class="math notranslate nohighlight">\(L^1\)</span> norm of the Polsby-Popper scores
for the given partition</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>partition</strong> (<a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a>) – Partition representing a districting plan</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><span class="math notranslate nohighlight">\(L^1\)</span> norm of the reciprocal Polsby-Popper scores</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>float</p>
</dd>
</dl>
</dd></dl>
<dl class="function">
<dt id="gerrychain.constraints.L1_reciprocal_polsby_popper">
<code class="sig-prename descclassname">gerrychain.constraints.</code><code class="sig-name descname">L1_reciprocal_polsby_popper</code><span class="sig-paren">(</span><em class="sig-param">partition: gerrychain.partition.partition.Partition</em><span class="sig-paren">)</span> → float<a class="reference internal" href="_modules/gerrychain/constraints/compactness.html#L1_reciprocal_polsby_popper"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.constraints.L1_reciprocal_polsby_popper" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the <span class="math notranslate nohighlight">\(L^1\)</span> norm of the reciprocal Polsby-Popper scores
for the given partition</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>partition</strong> (<a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a>) – Partition representing a districting plan</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><span class="math notranslate nohighlight">\(L^1\)</span> norm of the reciprocal Polsby-Popper scores</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>float</p>
</dd>
</dl>
</dd></dl>
<dl class="function">
<dt id="gerrychain.constraints.L2_polsby_popper">
<code class="sig-prename descclassname">gerrychain.constraints.</code><code class="sig-name descname">L2_polsby_popper</code><span class="sig-paren">(</span><em class="sig-param">partition: gerrychain.partition.partition.Partition</em><span class="sig-paren">)</span> → float<a class="reference internal" href="_modules/gerrychain/constraints/compactness.html#L2_polsby_popper"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.constraints.L2_polsby_popper" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the <span class="math notranslate nohighlight">\(L^2\)</span> norm of the Polsby-Popper scores
for the given partition.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>partition</strong> (<a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a>) – Partition representing a districting plan</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><span class="math notranslate nohighlight">\(L^2\)</span> norm of the Polsby-Popper scores</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>float</p>
</dd>
</dl>
</dd></dl>
<dl class="function">
<dt id="gerrychain.constraints.L_minus_1_polsby_popper">
<code class="sig-prename descclassname">gerrychain.constraints.</code><code class="sig-name descname">L_minus_1_polsby_popper</code><span class="sig-paren">(</span><em class="sig-param">partition</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/gerrychain/constraints/compactness.html#L_minus_1_polsby_popper"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#gerrychain.constraints.L_minus_1_polsby_popper" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the <span class="math notranslate nohighlight">\(L^{-1}\)</span> norm of the Polsby-Popper scores
for the given partition.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>partition</strong> (<a class="reference internal" href="#gerrychain.partition.Partition" title="gerrychain.partition.Partition"><em>Partition</em></a>) – Partition representing a districting plan</p>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><span class="math notranslate nohighlight">\(L^{-1}\)</span> norm of the Polsby-Popper scores</p>
</dd>
<dt class="field-odd">Return type</dt>
<dd class="field-odd"><p>float</p>