Source-linked AI summary
netseg: a Python Package for Measuring Structural Polarization and Segregation in Social Networks
Onur Tuncay Bal, Michał Bojanowski
TL;DR
Measuring structural polarization and segregation is an established research practice, but implementations are rarely released or tested. netseg provides a documented Python toolkit with extended measures and flexible null-model comparisons; its examples show metric behavior across simulated networks and identify segregated pockets in a county network.
Problem
Widely used indices for structural polarization and segregation are seldom implemented in publicly released, tested software, making reproducible measurement a persistent methodological gap.
Source: Summary · p. 1, Statement of need · p. 2Method
netseg ports and extends the R toolkit with additional measures, custom graph-ensemble null models, and automatic adjustment for metrics with built-in baselines.
Source: Statement of need · pp. 1–2, State of the field · p. 2, Research impact statement · p. 3Results
Across 1,600 generated networks, the parameter sweep shows metric values over varying conditions, with empty values where a metric’s logical prerequisites are unmet; vertex scores also locate segregated pockets in the county network.
Source: Example on a Generative Statistical… · p. 4, Example Usage on Empirical Data · p. 4Takeaways & Limitations
In the county network, vertices in neighborhoods dominated by their own group receive higher values, locating the most segregated pockets.
Source: Example Usage on Empirical Data · p. 4The generative example fixes the groups’ expected sizes as equal and constrains the absolute values of the two opinion means to match.
Source: Example on a Generative Statistical… · p. 4
Abstract
from arXiv · showhide
The study of structural polarization and segregation in social networks is an established line of research, and the quantification of both phenomena proceeds through a set of widely cited network indices. The code implementing those indices, however, is seldom released and almost never tested. We present netseg, a comprehensively documented Python package implementing these indices, most of them generalized to more than two groups and to directed as well as undirected input. It ports the R package of the same name and adds measures the R version lacks, among them Random Walk Controversy, Boundary Connectivity, Dipole Moment, and Moran's I. The package operates on igraph objects and performs the underlying graph operations (e.g., neighborhood queries and random-walk simulation) through igraph's Python interface, so that they execute in compiled code rather than in interpreted Python. For several of the indices this yields runtimes orders of magnitude below those of the available open-source implementations, which the documentation reports in benchmarks. Most of these indices are defined as a divergence from a null model, and published implementations fix that null model to a uniform random graph of matching density. netseg accepts an ensemble of graphs as a sample from an arbitrary null model, and distinguishes indices that already incorporate a baseline from those that do not, adjusting the comparison accordingly to avoid double subtraction. The documentation provides, for each index, a worked empirical example, its behaviour at the degenerate cases where it is undefined, a benchmark, and the procedure for substituting a custom null model. We report the behaviour of every index over a parameter sweep of a generative opinion model, and apply them to a county-level railroad network built from nineteenth-century operator records joined to full-count census data.
Summary
Polarization describes structural division into opposed groups, while segregation describes separation between groups. Network science offers a framework for studying these relational phenomena as scholarship questions established metrics and examines alternative null models.
- Polarization is structural bifurcation into opposed groups, whereas segregation is relational or spatial separation between groups.Source: Summary · p. 1
- Roughly 8,000 English-language works indexed “network” and “polarization” in titles or abstracts from 2010 to 2026, according to an OpenAlex sweep.Source: Summary · p. 1
- Recent scholarship questions foundational metrics’ validity and uses varying null models to compare measures and study tie-formation mechanisms.Source: Summary · p. 1
Statement of need
netseg2 adapts the R package for Python and extends its measures and null-model support. It addresses the lack of tested, accessible implementations with a unified toolkit and detailed documentation.
- netseg2 adapts the R package for Python and extends it with structural polarization measures and custom null-model support.Source: Statement of need · pp. 1–2
- Fewer than 2% of roughly 8,000 surveyed works linked a repository, while rarely published and untested metric code makes comparison and reproducibility challenging.The package is extensively tested for both Python and R, supporting cross-platform comparisons.Source: Statement of need · p. 2
- Vectorized operations and igraph’s C backend move graph operations out of interpreted Python, addressing interpreter overhead in prior implementations.The package also provides input validation, docstrings, and empirical examples for each metric.Source: Statement of need · p. 2
- Users can supply custom graph ensembles as null models, and most metrics extend to multiple groups and multimode networks without bespoke scripting.Source: Statement of need · p. 2
State of the field
The Python package builds on the established R netseg tool while addressing Python users’ access barriers and adding measures and comparison options. Existing alternatives include specialized scripts and GUI-based software with stated limits.
- The R package netseg implements classical segregation and homophily indices and has accumulated substantial use since its 2021 CRAN release.It had more than 18,000 Posit CRAN mirror downloads since its first release and roughly 3,700 in the twelve months to August 2026.Source: State of the field · p. 2
- Bridging from Python to R requires maintaining an R installation and converting graph objects, which can push Python users toward manual reimplementations with reproducibility costs.Source: State of the field · p. 2
- The Python package adds Random Walk Controversy, Boundary Connectivity, Dipole Moment, and Moran’s I, and supports custom null models.Source: State of the field · p. 2
- Specialized measures such as boundary connectivity and dipole moment often require undocumented or outdated scripts, while UCINET is GUI-based and limited to 32,767 nodes.Source: State of the field · p. 3
Research impact statement
netseg combines vectorized operations and igraph’s C-backed architecture with flexible null-model comparisons. Its documentation also demonstrates custom-model analyses through empirical examples and code snippets.
- Vectorized operations and igraph’s C-backed architecture eliminate interpreter overhead, enabling analysis of large-scale networks described as previously computationally prohibitive.Source: Research impact statement · p. 3
- Custom graph ensembles represent samples from chosen null models, with automatic baseline adjustment that prevents double subtraction for metrics that already include a null model.Examples include degree-preserving networks and counterfactual simulations from generative models.Source: Research impact statement · p. 3
- Documentation code snippets show how custom null models can evaluate counterfactual scenarios to isolate specific network mechanisms’ impact on segregation scores.Source: Research impact statement · p. 3
Installation and Documentation
netseg can be installed from PyPI or from source, and its documentation covers metric use, edge cases, benchmarks, and empirical applications. If the hosted documentation is unavailable, the compiled HTML can be served locally.
- The package can be installed from PyPI or from source using the repository’s installation instructions.Source: Installation and Documentation · p. 3
- The documentation describes function use cases, edge-case behavior, benchmarks, metric behavior, and empirical applications.Source: Installation and Documentation · p. 3
- If Codeberg Pages is unavailable, the compiled HTML documentation can be accessed locally from the pages branch.Source: Installation and Documentation · p. 3
Design and Usage
netseg accepts mixing matrices, igraph graph objects, and vertex attributes as inputs for measures that use group membership. These options let users work from aggregate mixing data or directly from graph data.
- For measures based solely on mixing matrices, users can provide a NumPy array and a membership list.The membership list is needed to calculate the expected number of within-group edges.Source: Design and Usage · p. 3
- With an igraph.Graph input, netseg constructs the mixing matrix using sparse matrix multiplication.Source: Design and Usage · p. 3
- Users can pass a vertex-attribute name as the membership argument to use group labels stored on the graph.Source: Design and Usage · p. 4
Example on a Generative Statistical Model
The example uses a two-component Gaussian mixture to generate agent opinions and a parameterized model to generate ties. A 1,600-network sweep illustrates metric behavior and identifies cases where metrics are undefined.
Example on a Generative Statistical Model
- Opinions come from a two-component Gaussian mixture, are clipped to [−1, 1], and use π=0.5 for equal expected group sizes.The component means and standard deviations control opinion separation and spread; the example fixes |µ1|=|µ2|.Source: Example on a Generative Statistical… · p. 4
- The tie probability uses β1 for opinion-distance homophily and β2 for degree asymmetry across the opinion spectrum.A positive β2 raises expected degree for positive-opinion nodes, while lower β1 penalizes ties between dissimilar nodes; group membership follows the originating mixture component.Source: Example on a Generative Statistical… · p. 4
- The example generates 1,600 networks on a 40-by-40 parameter grid, with β1 from −4 to 4 and β2 from 0 to 0.6.Source: Example on a Generative Statistical… · p. 4
- Figure 1 shows metric values across the parameter space; empty cells indicate graphs that fail a metric’s logical prerequisites.For example, Boundary Connectivity is undefined when a graph has no cross-group ties and therefore no boundary nodes.Source: Example on a Generative Statistical… · p. 4
Example Usage on Empirical Data
The empirical example builds a county network from railroad operator records and census population ratios, then maps vertex-level Spectral Segregation Index scores onto the network.
Example Usage on Empirical Data
- Counties are connected when a railroad operator runs track through both, so ties represent shared operational control rather than geographic adjacency.The network is constructed from historical railroad data and joined to county-level African American population ratios from full-count census data.Source: Example Usage on Empirical Data · p. 4
- The Spectral Segregation Index uses a binary attribute marking counties at or above the sample median African American population ratio.With aggregate=False, each county receives a score; higher values locate pockets in neighborhoods dominated by the county’s own group.Source: Example Usage on Empirical Data · p. 4
AI usage disclosure
Generative AI assisted with documentation and testing tasks, and was limited to debugging and edge-case identification in core package development.
AI usage disclosure
- AI generated boilerplate NumPy-style docstrings from supplied functions, parameters, and intended descriptions.The authors used the generated test templates to help write input-validation tests.Source: AI usage disclosure · p. 5
- Metric measurement tests were done manually where applicable, checking that netseg returned the exact result given in the original paper.Source: AI usage disclosure · p. 5
- In core Python package development, AI use was limited to debugging and identifying potential edge cases for robust error handling.Source: AI usage disclosure · p. 5
Tables
Table 1 summarizes the implemented metrics by network support, multi-group extensibility, computational complexity, and value range.
Tables
- Table 1 compares implemented metrics across network type support, multi-group extensibility, computational complexity, and value range.Source: Table 1 · p. 7
- Its complexity notation defines M as the number of null models, and V, E, and K as nodes, edges, and groups.S, L, and I denote walks, walk length, and iterations; when no null model is supplied, M = 1.Source: Table 1 · p. 7
- The package relaxes classical segregation-index edge-orientation constraints except for Spectral Segregation Index, and generalizes metrics to multiple groups where applicable.Random Walk Controversy complexity excludes the one-off seed-selection cost.Source: Table 1 · p. 7
Figures
The figures show metric values across a generative-model parameter grid and visualize the construction and local segregation of a county railroad network.
Figures
- Figure 1 maps each implemented metric over a 40 × 40 grid of β1 and β2 values; empty cells indicate unmet metric prerequisites.The grid spans β1 ∈ [−4, 4] and β2 ∈ [0, 0.6].Source: Figure 1 · p. 8
- Figure 2 contrasts raw railroad segments colored by operator with a county network linking counties served by a common operator.In the county network, vertex size represents degree and color marks counties above or below the median African American population ratio.Source: Figure 2 · p. 9
- Figure 3 colors counties by per-vertex Spectral Segregation Index, with higher values marking more segregated local neighborhoods.Source: Figure 3 · p. 9