segstats

Residential segregation and diversity indices for urban analysis: the five Massey-Denton dimensions, multigroup and spatial indices (Reardon-O'Sullivan, Wong, Morrill), local measures, permutation tests and bootstrap CIs.

Version 0.1.2 · Python >=3.9 · MIT licence
pip install segstats
PyPISourceIssuesChangelogCite (DOI)

segstats

PyPI version Python versions pipeline status License: MIT DOI Downloads

Residential segregation and diversity indices for urban analysis.

segstats computes the classic segregation indices from a table of population counts by zone (census tracts, neighbourhoods, mahalle) and, when zone polygons are available, their spatial counterparts. It covers the five dimensions of Massey & Denton (1988), multigroup indices, spatially adjusted measures, local indices per zone, and Monte Carlo inference. It needs only NumPy. Geometry comes from plain GeoJSON; shapely and GDAL are not required.

pip install segstats            # numpy only
pip install "segstats[pandas]"  # + DataFrame input

Requires Python 3.9 or newer.

Looking for more? The PySAL segregation package is a fuller alternative. It adds many more indices, decomposition, network distances, multiscalar profiles and comparative inference, but needs the full geospatial stack. segstats is a small, dependency-light toolkit whose numbers are checked against hand calculations and PySAL (see Validation).


Quickstart

import segstats as ss

# counts only: CSV with columns tract, black, total
data = ss.read_csv("tracts.csv", id_column="tract", groups=["black"], total_column="total")
x, t = data.two_group("black")          # group counts and zone totals
ss.dissimilarity(x, t)                   # 0 = even, 1 = complete segregation
ss.profile(data, group="black")          # every aspatial index at once

# counts + polygons (GeoJSON in lon/lat is projected to metres on the fly)
data = ss.read_geojson("tracts.geojson", id_property="GEOID", counts=data)
ss.profile(data, group="black", center=(28.98, 41.01), bandwidth=1000)

# Is D higher than random allocation would give?
res = ss.permutation_test(data, "D", group="black", n_permutations=999, seed=1)
res.statistic, res.null_mean, res.p_value

# 95% bootstrap CI of the spatial information theory index (1 km Gaussian kernel)
ci = ss.bootstrap_ci(data, "SH", bandwidth=1000, seed=1)
ci.ci_low, ci.ci_high

# local measures
ss.location_quotient(data.counts)        # zones x groups
ss.local_entropy(data.counts)            # normalised diversity per zone

See examples/quickstart.py for a runnable synthetic city.

Command line

segstats list                                         # all indices and their inputs
segstats indices tracts.csv --id tract --groups black --total total --group black
segstats indices tracts.csv --id tract --groups black --total total \
    --geojson tracts.geojson --geo-id GEOID --group black \
    --center 28.98,41.01 --bandwidth 1000 --permutations 999 --bootstrap 999 --seed 1
segstats local tracts.geojson --id GEOID --groups black,white,asian -o local.csv

--format table|json|csv controls the output. A GeoJSON file can hold the counts in its feature properties, or a CSV table can be joined to polygons with --geojson.


Indices

Two-group indices compare a group of interest (x) with everyone else (y = t - x). Multigroup indices use all count columns.

Name Index Dimension Inputs Source
D Dissimilarity evenness counts Duncan & Duncan 1955
G Gini evenness counts Massey & Denton 1988
H Entropy / Theil H evenness counts Theil & Finizza 1971
A Atkinson (shape b) evenness counts Atkinson 1970
MD, MH Multigroup D and H evenness counts Reardon & Firebaugh 2002
xPx Isolation exposure counts Bell 1954; M&D 1988
xPy Interaction exposure counts M&D 1988
V Correlation ratio (eta squared) exposure counts Bell 1954; White 1986
DEL Delta concentration area Duncan, Cuzzort & Duncan 1961
ACO, RCO Absolute / relative concentration concentration area M&D 1988
ACE, RCE Absolute / relative centralization centralization centre M&D 1988
ACL, RCL Absolute / relative clustering clustering distances M&D 1988
SP Spatial proximity clustering distances White 1983
D_adj Adjusted D (contiguity) spatial evenness contiguity Morrill 1991
D_w, D_s Boundary-weighted and shape-adjusted D spatial evenness boundaries Wong 1993
SD, SH Spatial dissimilarity and information theory index spatial evenness neighbourhood Reardon & O'Sullivan 2004

Local indices: location_quotient, local_entropy, local_spatial_entropy (entropy of each zone's kernel-weighted local environment), and local_dissimilarity_contribution.

API overview

Area Functions
Plain arrays dissimilarity(x, t), gini, entropy, atkinson(x, t, b), isolation, interaction, correlation_ratio, multigroup_dissimilarity(counts), multigroup_entropy, exposure_matrix, delta(x, t, area), absolute_concentration, relative_concentration, absolute_centralization(x, area, dist), relative_centralization(x, t, dist), absolute_clustering(x, t, c), relative_clustering, spatial_proximity, proximity_matrix(d, area, scale), morrill_dissimilarity(x, t, C), wong_dissimilarity_w(x, t, B), wong_dissimilarity_s(x, t, B, perimeter, area), local_environment(counts, W), spatial_dissimilarity(counts, W), spatial_entropy(counts, W)
Data ZoneData, read_csv, read_geojson, from_dataframe, ZoneGeometry (area, perimeter, centroids, contiguity("queen"/"rook"), boundary_lengths(), distances())
Weights distance_matrix, kernel_weights(d, bandwidth, kernel), with_self, row_standardize
By name compute(data, "D", group), profile(data, group), statistic(...), INDICES, IndexOptions
Inference permutation_test(data, index, group, null="labels"/"zones"), bootstrap_ci(data, index, group, method="multinomial"/"zones"), random_allocation

IndexOptions holds the options shared by the registry: Atkinson b, center or center_distance, decay_scale (distance unit of exp(-d), default 1000 for km when coordinates are metres), clustering ("proximity" or "contiguity" matrix for ACL), bandwidth and kernel (gaussian, exponential, biweight, box) for the Reardon & O'Sullivan neighbourhood (default: the zone plus its contiguous neighbours), and contiguity (queen/rook).


Method and sources

Notation: x_i, y_i, t_i are the group, rest-of-population and total counts of zone i; capitals are city totals; p_i = x_i / t_i, P = X / T; a_i is the zone area.

Validation

The tests check every index against values worked out by hand: complete segregation gives 1, an even distribution gives 0, and a four-zone city has exact fractions (D = 5/9, ACO = 14/15, RCO = 77/92, ACE = 79/120, ...). Thirteen two-group indices and the multigroup D and H also match PySAL segregation 2.6.0 to 1e-9 on an irregular eight-zone layout. Wong's D(w) matches PySAL when given PySAL's boundary matrix. On polygons that meet at T-junctions, PySAL's vertex-based contiguity misses some neighbours, while segstats measures collinear edge overlap directly.

References


Limitations


Development

pip install -e ".[dev,pandas]"
pytest --cov=segstats
ruff check . && ruff format --check src tests && mypy src

Related Geophilo SDKs

License

MIT © 2026 Yusuf Eminoğlu. See LICENSE. To cite segstats, see CITATION.cff.

Academic citation

@software{eminoglu_segstats,
  author  = {Eminoğlu, Yusuf},
  title   = {segstats},
  year    = {2026},
  version = {0.1.2},
  doi     = {10.5281/zenodo.24235590},
  url     = {https://gitlab.com/geophilo1/segstats_sdk}
}

See also CITATION.cff.

Other Geophilo SDKs