Spatial weights as an immutable graph, mirroring libpysal.graph.Graph.
The adjacency lives in Rust memory as a compressed sparse row (CSR) structure behind a
resource reference. The Elixir struct holds only the reference, the node count n, the
node ids and the current transform:
%Spatex.Graph{ref: ref, n: 3, ids: [:a, :b, :c], transform: :o}Nodes and ordering
Nodes are numbered by their position in ids. Rows are ordered by focal position and,
within a row, by neighbor position (libpysal's canonical order). Every node appears as a
focal node: a node without outgoing edges is an island and is stored, and reported by
to_adjacency/1, as a zero-weight self-loop {id, id, 0.0}. A neighbor only counts when
its weight is non-zero, so cardinalities/1 and isolates/1 ignore zero weights.
Weights and transforms
Weights are numbers (integers are converted to floats); duplicate {focal, neighbor}
pairs are an error and negative weights are kept as given. transform/2 returns a new
graph and applies to the current weights: :r row-standardizes (rows that sum to zero
stay zero), :b makes weights binary, :d scales so all weights sum to one, :v is the
variance-stabilizing transform. :o is the identity; original weights cannot be restored
from a transformed graph.
Examples
iex> g = Spatex.Graph.from_arrays([:a, :a, :b, :c], [:b, :c, :a, :a], [1, 3, 2, 4])
iex> Spatex.Graph.ids(g)
[:a, :b, :c]
iex> Spatex.Graph.to_adjacency(g)
[{:a, :b, 1.0}, {:a, :c, 3.0}, {:b, :a, 2.0}, {:c, :a, 4.0}]
iex> r = Spatex.Graph.transform(g, :r)
iex> Spatex.Graph.to_adjacency(r)
[{:a, :b, 0.25}, {:a, :c, 0.75}, {:b, :a, 1.0}, {:c, :a, 1.0}]
iex> Spatex.Graph.lag(r, [10, 20, 30])
[27.5, 10.0, 10.0]
iex> g |> Spatex.Graph.s0()
10.0
iex> inspect(r)
"#Spatex.Graph<n: 3, nnz: 4, transform: :r>"
Summary
Functions
Builds a block contiguity graph: nodes with the same regime are neighbors of each other.
Builds a contiguity graph from polygon geometries given as WKB binaries.
Builds the distance-band graph of a point set, like libpysal.graph.Graph.build_distance_band.
Builds a fuzzy contiguity graph from polygon WKB binaries: libpysal's
Graph.build_fuzzy_contiguity, which relaxes contiguity for geometries that do not share
exact edges (slivers, gaps, digitizing errors).
Builds a kernel-weighted graph of a point set, like libpysal.graph.Graph.build_kernel.
Builds the k-nearest-neighbor graph of a point set, like libpysal.graph.Graph.build_knn.
Builds a triangulation-based graph of a point set, like
libpysal.graph.Graph.build_triangulation.
Number of non-zero-weight neighbors per node, as a list aligned with ids/1.
Builds a graph from an adjacency list or map.
Builds a graph from parallel focal, neighbor and weight lists.
Node ids in graph order.
Ids of the nodes without any (non-zero-weight) neighbor, in graph order.
Spatial lag (Wy)_i = sum_j w_ij * y_j of y under the current weights.
Number of nodes.
Number of edges with a non-zero weight (island self-loops excluded).
Reads a GAL file. Weights are binary. See Spatex.Graph.IO for formats and the :ids
option.
Reads a GWT file. See Spatex.Graph.IO.
S0 = sum_ij w_ij of the current weights.
S1 = 1/2 sum_ij (w_ij + w_ji)^2 of the current weights.
S2 = sum_i (sum_j w_ij + sum_j w_ji)^2 of the current weights.
True when w_ij == w_ji for every pair within tol (relative to max(1, |w_ij|, |w_ji|),
default 1.0e-12). A missing reverse edge counts as weight zero.
Returns the edges as {focal, neighbor, weight} triples in canonical order, islands included.
Renders the graph as GAL text (iodata). See Spatex.Graph.IO.
Renders the graph as GWT text (iodata). See Spatex.Graph.IO.
Exports the adjacency as the compressed sparse row (CSR) structure of libpysal's
Graph.sparse, as Nx tensors: %{indptr: _, indices: _, data: _, shape: {n, n}}.
Applies a weight transform and returns a new graph.
Writes the graph as a GAL file (weights are not stored). See Spatex.Graph.IO.
Writes the graph as a GWT file (weights with seven decimals). See Spatex.Graph.IO.
Types
@type id() :: term()
A node identifier: any term, unique within the graph.
@type t() :: %Spatex.Graph{ ids: [id()], n: non_neg_integer(), ref: reference(), transform: transform() }
@type transform() :: :o | :r | :b | :d | :v
Weight transform label.
Functions
Builds a block contiguity graph: nodes with the same regime are neighbors of each other.
regimes holds one non-nil label per node (compared with ===); nodes alone in their
regime are islands. Weights are 1.0.
Unlike libpysal, which lists the nodes regime by regime, nodes keep their input order (and
:ids, default 0..n-1, names them); compare against PySAL by id.
Examples
iex> g = Spatex.Graph.build_block_contiguity(["a", "b", "a", "c", "b"])
iex> g |> Spatex.Graph.to_adjacency() |> Enum.reject(fn {_, _, w} -> w == 0.0 end)
[{0, 2, 1.0}, {1, 4, 1.0}, {2, 0, 1.0}, {4, 1, 1.0}]
Builds a contiguity graph from polygon geometries given as WKB binaries.
Follows libpysal's vertex-set intersection (Graph.build_contiguity): geometries sharing an
identical vertex are queen neighbors, geometries sharing an identical edge (two consecutive
vertices) are rook neighbors. Coordinates are compared exactly (-0.0 == 0.0), so a
T-junction is not rook contiguity. Holes count: a polygon filling another's hole is its
neighbor. Weights are 1.0; polygons without neighbors become islands. Node order is the
input order.
Accepts Polygon, MultiPolygon, LineString and MultiLineString WKB (Z/M values are
ignored; lines only with the vertex method); points, a nil entry, malformed WKB or a
non-finite coordinate raise ArgumentError naming the geometry index.
Options
:rook-true(default, like libpysal'sGraph) for shared edges,falsefor queen (shared vertices).:strict-trueuses geometric predicates instead of identical vertices / edges: queen neighbors touch (boundaries meet, interiors disjoint) and rook neighbors share a boundary piece of positive length (DE-9IMF***1****), so T-junctions count. Defaultfalse.:by_perimeter-trueweights every pair by the length of its shared boundary (0.0for a point contact, which stays stored but does not count as a neighbor); raises when two neighbors overlap. Defaultfalse.:ids- unique node ids, one per geometry (default0..n-1).
Builds the distance-band graph of a point set, like libpysal.graph.Graph.build_distance_band.
Two nodes are neighbors when their Euclidean distance is at most threshold (inclusive).
Nodes without a neighbor are islands. points is as for build_knn/3.
Options
:ids- node ids, a unique list with one entry per point. Defaults to0..n-1.:binary-true(default) gives weight1.0;falsegivesdistance ** alpha.:alpha- the power forbinary: false(default-1.0, inverse distance). Coincident points raise for negativealpha(libpysal yields infinite weights); a zero weight (alpha > 0at distance zero) drops the edge.:kernel- withbinary: false, weights come from this kernel (seebuild_kernel/2) applied to the band distances instead ofdistance ** alpha.:alphais then unused.:bandwidth- the kernel bandwidth, a positive number. Defaults to the 25th percentile of the band's distances, as in libpysal.:taper/:decay- as forbuild_kernel/2(defaulttrue/false).:metric-:euclidean(default) or:haversine: points are{lon, lat}in degrees (-180 < lon < 180,-90 < lat < 90) andthresholdand distances are great-circle kilometres (Earth radius 6371 km). libpysal leaves haversine distances in radians; seedocs/parity.md.:max_edges- edge budget (default 50_000_000). A band holding more edges raisesArgumentErroras soon as the budget is passed.
Raises ArgumentError unless threshold is a positive number.
Examples
iex> g = Spatex.Graph.build_distance_band([{0, 0}, {1, 0}, {5, 0}], 1.0)
iex> {Spatex.Graph.to_adjacency(g), Spatex.Graph.isolates(g)}
{[{0, 1, 1.0}, {1, 0, 1.0}, {2, 2, 0.0}], [2]}
Builds a fuzzy contiguity graph from polygon WKB binaries: libpysal's
Graph.build_fuzzy_contiguity, which relaxes contiguity for geometries that do not share
exact edges (slivers, gaps, digitizing errors).
Every pair {i, j} for which predicate(geometry_i, geometry_j) holds becomes an edge with
weight 1.0. By default (:intersects) the polygons must touch or overlap. With :buffer
(or :tolerance) every polygon is first grown by that distance, so polygons at most twice
the buffer apart become neighbors. Polygons without a neighbor become islands; node order is
the input order.
Spatex does not build buffered geometries (there is no GEOS). It uses the fact that two round
buffers of radius b intersect exactly when the polygons are at most 2b apart, computed
with exact polygon-to-polygon distances. GEOS draws arcs with 16 chords per quadrant, which
makes its buffers up to 0.12 % smaller, so a pair whose distance is within about 0.15 % below
2b can be a neighbor here and not in libpysal (see the parity notes). Everything else
matched libpysal on its committed fixtures.
Accepts Polygon and MultiPolygon WKB; anything else, malformed WKB or a non-finite
coordinate raises ArgumentError naming the geometry index.
Options
:tolerance- the buffer as a share of half the shorter side of the total bounds of the geometries:buffer = tolerance * 0.5 * min(width, height). Exclusive with:buffer.:buffer- the buffer distance, in the units of the coordinates (not negative;0means no buffer). Exclusive with:tolerance.:predicate-:intersects(default), ornilfor intersecting bounding boxes (grown by the buffer), or without a buffer also:touches,:overlaps,:within,:contains,:covers,:covered_by,:contains_properlyand:crosses(never true for polygons). The predicates are directed,predicate(focal, neighbor), so:containsand:withingive converse graphs.:dwithinis not ported, and a buffer combined with any predicate other than:intersectsornilraises, because the buffered geometries are never built.:ids- unique node ids, one per geometry (default0..n-1).
Examples
iex> square = fn x ->
...> <<1, 3::little-32, 1::little-32, 5::little-32>> <>
...> for({px, py} <- [{x, 0}, {x + 1, 0}, {x + 1, 1}, {x, 1}, {x, 0}], into: <<>>) do
...> <<px * 1.0::little-float-64, py * 1.0::little-float-64>>
...> end
...> end
iex> wkbs = [square.(0), square.(1.5), square.(5)]
iex> Spatex.Graph.isolates(Spatex.Graph.build_fuzzy_contiguity(wkbs))
[0, 1, 2]
iex> graph = Spatex.Graph.build_fuzzy_contiguity(wkbs, buffer: 0.5)
iex> Spatex.Graph.to_adjacency(graph)
[{0, 1, 1.0}, {1, 0, 1.0}, {2, 2, 0.0}]
Builds a kernel-weighted graph of a point set, like libpysal.graph.Graph.build_kernel.
points is as for build_knn/3. The weight of the edge i -> j is
kernel(d_ij, h) with the Euclidean distance d_ij. Self weights are never present
(add them with assign_self_weight once available). Which pairs are candidates depends on
the options:
- without
:k, all pairs within a fixed:bandwidth(or every pair when the bandwidth is the default ortaper: falsemeets a non-compact kernel, which is O(n^2)); - with
:k, theknearest neighbors of each node (the graph is then generally asymmetric).
Options
:kernel-:gaussian(default),:triangular,:parabolic(alias:epanechnikov),:bisquare,:tricube,:cosine,:boxcar(aliases:uniform,:discrete; weight1.0ford < h, not1/2),:exponentialor:identity(the raw distance).:bandwidth- a positive number (global),:adaptive(each node uses its distance to itsk-th neighbor times1.0000001; requires:k) ornil(default): the 25th percentile of all pairwise distances, or the largestk-nearest distance when:kis given. libpysal's"auto"is not supported.:k- number of nearest neighbors,1 <= k <= n - 1.:taper-true(default) drops weights beyond the bandwidth and stored zeros.:decay-truedivides by the kernel value at distance zero (peak 1). Not defined for:identity.:ids- node ids, a unique list with one entry per point. Defaults to0..n-1.:coincident-:raise(default),:cliqueor:jitter(with:seed), only relevant with:k(seebuild_knn/3); jittered points also give the kernel distances. Without:k, coincident pairs are kept with distance0, whereas libpysal drops them on its fixed-bandwidth fast path (seedocs/parity.md).:metric-:euclidean(default) or:haversine: points are{lon, lat}in degrees (-180 < lon < 180,-90 < lat < 90) and distances (and a fixed:bandwidth) are great-circle kilometres (Earth radius 6371 km). libpysal leaves haversine distances in radians; seedocs/parity.md.:max_edges- edge budget (default 50_000_000). Without:kor a compact kernel with:bandwidthevery ordered pair is an edge (n * (n - 1)); more edges than this raiseArgumentErrorbefore (or while) they are collected.
Raises ArgumentError on invalid options.
Examples
iex> g = Spatex.Graph.build_kernel([{0, 0}, {1, 0}, {5, 0}], kernel: :triangular, bandwidth: 2.0)
iex> Spatex.Graph.to_adjacency(g)
[{0, 1, 0.5}, {1, 0, 0.5}, {2, 2, 0.0}]
@spec build_knn(term(), pos_integer(), keyword()) :: t()
Builds the k-nearest-neighbor graph of a point set, like libpysal.graph.Graph.build_knn.
points is anything Spatex.Input.to_points_binary/1 accepts ([{x, y}], [[x, y]],
an {n, 2} Nx.Tensor or a pair {xs, ys}). Distances are Euclidean. Every node gets
its k nearest other nodes with weight 1.0; the graph is generally asymmetric.
Equidistant candidates are resolved by lowest point index (libpysal follows scikit-learn
tree order there, see docs/parity.md).
Options
:ids- node ids, a unique list with one entry per point. Defaults to0..n-1.:coincident-:raise(default) raisesArgumentErrorwhen two points share a location;:cliquelinks all points on one site to each other and to the neighbors of the site, so a row can hold more thankneighbors;:jittermoves the points of each coincident group by a tiny seeded displacement (radiusmax(|x|, |y|, 1) * 2^-30) until all points are distinct. Distinct points are untouched; libpysal's jitter moves every point, unseeded (seedocs/parity.md).:seed- seed ofcoincident: :jitter(default0).:metric-:euclidean(default) or:haversine: points are{lon, lat}in degrees (-180 < lon < 180,-90 < lat < 90) and distances are great-circle kilometres (Earth radius 6371 km). libpysal leaves haversine distances in radians; seedocs/parity.md.:max_edges- edge budget (default 50_000_000).n * kabove it raisesArgumentErrorbefore the graph is built.
Raises ArgumentError unless 1 <= k <= n - 1 (distinct locations minus one for
coincident: :clique).
Examples
iex> g = Spatex.Graph.build_knn([{0, 0}, {1, 0}, {5, 0}], 1)
iex> Spatex.Graph.to_adjacency(g)
[{0, 1, 1.0}, {1, 0, 1.0}, {2, 1, 1.0}]
Builds a triangulation-based graph of a point set, like
libpysal.graph.Graph.build_triangulation.
points is as for build_knn/3 (at least 3 distinct, non-collinear points). Edges are
weighted by kernel(d_ij, bandwidth) on the Euclidean edge length; with the defaults
(:boxcar, :infinity) every edge has weight 1.0.
Options
:method-:delaunay(default),:gabriel(the Delaunay edges whose diametral circle holds no Delaunay neighbor of an endpoint) or:relative_neighborhood(the edges whose lune holds no point; boundary points prune) or:voronoi(rook contiguity of the Voronoi cells clipped to:clip: the Delaunay edges whose dual Voronoi edge has positive length inside the clip region).:clip- Voronoi clip region::bounding_box(default, the points' bounding box),:convex_hullornil(no clip; as with GEOS, the diagram is still bounded by the bounding box grown by its largest side on every side). Ignored by the other methods.:rook- must betrue(default) with:voronoi; queen cell contiguity is not supported. Ignored by the other methods.:kernel,:bandwidth,:taper,:decay- as forbuild_kernel/2, except that:bandwidthis:infinity(default, no tapering) or a positive number.:coincident-:raise(default) or:clique: the triangulation is built over distinct locations, points on one site take the site's edges and are linked with weightkernel(0, bandwidth).:ids- node labels, one per point (default0..n-1).
Edges are those of the exact Delaunay triangulation for points in general position, matching libpysal (scipy/Qhull). For cocircular points, such as the corners of a square, the diagonal is implementation-defined and Gabriel/relative neighborhood results follow it. Unlike libpysal, a node whose edges are all tapered away by a finite bandwidth stays in the graph as an island instead of disappearing.
Examples
iex> g = Spatex.Graph.build_triangulation([{0, 0}, {3, 0}, {0, 4}])
iex> Spatex.Graph.cardinalities(g)
[2, 2, 2]
iex> g = Spatex.Graph.build_triangulation([{0, 0}, {3, 0}, {0, 4}], method: :relative_neighborhood)
iex> Spatex.Graph.to_adjacency(g)
[{0, 1, 1.0}, {0, 2, 1.0}, {1, 0, 1.0}, {2, 0, 1.0}]
@spec cardinalities(t()) :: [non_neg_integer()]
Number of non-zero-weight neighbors per node, as a list aligned with ids/1.
Examples
iex> g = Spatex.Graph.from_arrays([:a, :a, :b], [:b, :c, :a])
iex> Spatex.Graph.cardinalities(g)
[2, 1, 0]
@spec from_adjacency( [{id(), id(), number()}] | %{required(id()) => [{id(), number()}]}, keyword() ) :: t()
Builds a graph from an adjacency list or map.
Accepts a list of {focal, neighbor, weight} triples (as returned by to_adjacency/1)
or a map %{focal => [{neighbor, weight}]}. For a map the focal keys are taken in term
order, followed by neighbors that are not keys, in order of first appearance; a key with
an empty list is an island. Takes the same options as from_arrays/4; an explicit :ids
overrides the derived order.
Examples
iex> g = Spatex.Graph.from_adjacency(%{b: [{:a, 2}], a: [{:b, 1}, {:c, 3}]})
iex> Spatex.Graph.ids(g)
[:a, :b, :c]
Builds a graph from parallel focal, neighbor and weight lists.
focal and neighbor hold node ids; weight defaults to 1.0 for every edge. A
keyword list may be given in place of weight.
Options
:ids- the node order. Must be unique and contain every focal and neighbor id. Nodes in:idswithout outgoing edges become islands. Defaults to the ids in order of first appearance, scanningfocaland thenneighbor(libpysal'sunique_ids).
Raises ArgumentError on length mismatches, non-numeric weights, duplicate edges, ids
missing from :ids, or a non-unique :ids.
Examples
iex> g = Spatex.Graph.from_arrays([:a, :b], [:b, :c])
iex> {Spatex.Graph.ids(g), Spatex.Graph.isolates(g)}
{[:a, :b, :c], [:c]}
iex> g = Spatex.Graph.from_arrays([1], [2], [0.5], ids: [2, 1, 3])
iex> Spatex.Graph.to_adjacency(g)
[{2, 2, 0.0}, {1, 2, 0.5}, {3, 3, 0.0}]
Node ids in graph order.
Ids of the nodes without any (non-zero-weight) neighbor, in graph order.
Spatial lag (Wy)_i = sum_j w_ij * y_j of y under the current weights.
y is a list of numbers aligned with ids/1, or a binary of native-endian 64-bit floats.
Islands get a lag of 0.0. Returns a list of floats.
Examples
iex> g = Spatex.Graph.from_arrays([0, 0, 1], [1, 2, 0]) |> Spatex.Graph.transform(:r)
iex> Spatex.Graph.lag(g, [1.0, 2.0, 3.0])
[2.5, 1.0, 0.0]
@spec n(t()) :: non_neg_integer()
Number of nodes.
@spec nnz(t()) :: non_neg_integer()
Number of edges with a non-zero weight (island self-loops excluded).
Reads a GAL file. Weights are binary. See Spatex.Graph.IO for formats and the :ids
option.
Reads a GWT file. See Spatex.Graph.IO.
S0 = sum_ij w_ij of the current weights.
S1 = 1/2 sum_ij (w_ij + w_ji)^2 of the current weights.
S2 = sum_i (sum_j w_ij + sum_j w_ji)^2 of the current weights.
True when w_ij == w_ji for every pair within tol (relative to max(1, |w_ij|, |w_ji|),
default 1.0e-12). A missing reverse edge counts as weight zero.
Returns the edges as {focal, neighbor, weight} triples in canonical order, islands included.
Renders the graph as GAL text (iodata). See Spatex.Graph.IO.
Renders the graph as GWT text (iodata). See Spatex.Graph.IO.
@spec to_sparse(t()) :: %{ indptr: Nx.Tensor.t(), indices: Nx.Tensor.t(), data: Nx.Tensor.t(), shape: {non_neg_integer(), non_neg_integer()} }
Exports the adjacency as the compressed sparse row (CSR) structure of libpysal's
Graph.sparse, as Nx tensors: %{indptr: _, indices: _, data: _, shape: {n, n}}.
Row i holds the entries indices[indptr[i]..indptr[i + 1]] with weights
data[indptr[i]..indptr[i + 1]]; rows follow the node order of ids/1 and indices are
sorted within each row. Exactly the stored entries are exported, as in libpysal: islands
appear as an explicit zero self-loop and explicit zero weights are kept. The data reflect
the current transform.
Tensor types: indptr is {:s, 64}, indices is {:u, 32} and data is {:f, 64}
(scipy uses int32 indices; these hold every value losslessly).
Needs the optional :nx dependency; raises ArgumentError when it is not available.
Examples
iex> g = Spatex.Graph.from_arrays([:a, :b], [:b, :a], [1.0, 2.0])
iex> sparse = Spatex.Graph.to_sparse(g)
iex> {Nx.to_flat_list(sparse.indptr), Nx.to_flat_list(sparse.indices)}
{[0, 1, 2], [1, 0]}
iex> {Nx.to_flat_list(sparse.data), sparse.shape}
{[1.0, 2.0], {2, 2}}
Applies a weight transform and returns a new graph.
kind is :o, :r, :b, :d or :v (or the strings "O", "r", ... in any case).
The transform is applied to the graph's current weights, like libpysal; asking for the
graph's current transform returns it unchanged, and :o on a transformed graph raises.
Examples
iex> g = Spatex.Graph.from_arrays([:a, :a, :b], [:b, :c, :a], [2, 2, 5])
iex> g |> Spatex.Graph.transform("B") |> Spatex.Graph.to_adjacency()
[{:a, :b, 1.0}, {:a, :c, 1.0}, {:b, :a, 1.0}, {:c, :c, 0.0}]
Writes the graph as a GAL file (weights are not stored). See Spatex.Graph.IO.
Writes the graph as a GWT file (weights with seven decimals). See Spatex.Graph.IO.