Skip to content

GeoBayesianNetwork

The primary user-facing class. Load a .bif file with geobn.load(), attach data sources to evidence nodes, configure discretization, and call infer().

Factory function

load

load(path)

Load a Bayesian network from a BIF file.

Parameters

path: Path to a .bif file.

Returns

GeoBayesianNetwork Ready to accept inputs via :meth:~GeoBayesianNetwork.set_input.

GeoBayesianNetwork class

GeoBayesianNetwork

A Bayesian network wired to geographic data sources.

Typical usage::

bn = geobn.load("model.bif")
bn.set_input("slope",    geobn.RasterSource("slope.tif"))
bn.set_input("rainfall", geobn.ConstantSource(50.0))
bn.set_discretization("slope",    [0, 10, 30, 90], ["flat", "moderate", "steep"])
bn.set_discretization("rainfall", [0, 25, 75, 200], ["low", "medium", "high"])
result = bn.infer(query=["fire_risk"])
result.to_geotiff("output/")

Real-time / repeated inference

When only a subset of inputs change between calls (e.g. static terrain, changing weather), use :meth:freeze to cache static node arrays::

bn.freeze("slope_angle", "aspect")          # terrain is static
result = bn.infer(query=["avalanche_risk"]) # first call: fetches & caches terrain

# Subsequent calls re-process only weather inputs:
bn.set_input("recent_snow", geobn.ConstantSource(35.0))
result = bn.infer(query=["avalanche_risk"]) # terrain reused from cache

For maximum throughput, pre-run all evidence combinations once::

bn.precompute(query=["avalanche_risk"])      # one-time cost: all combos
result = bn.infer(query=["avalanche_risk"])  # O(H×W) numpy indexing, no pgmpy

set_input

set_input(node, source)

Attach a data source to a BN evidence node.

Parameters

node: Name of a root node (no parents) in the BN. source: Any :class:~geobn.sources.DataSource subclass.

fetch_raw

fetch_raw(source)

Fetch a data source using the BN's grid and return a plain numpy array.

Useful when you need the raw values to derive additional inputs — for example, fetching a DEM to compute slope and aspect before registering them via :meth:set_input. Requires :meth:set_grid to be called first.

Parameters

source: Any :class:~geobn.sources.DataSource to fetch. The source is not registered as an input.

Returns

np.ndarray Float32 array of shape (H, W), aligned to the BN's grid. NaN where the source has no data.

set_discretization

set_discretization(node, breakpoints, labels=None)

Define how continuous values for node are mapped to BN states.

Parameters

node: Node name (must match an input node already registered via :meth:set_input). breakpoints: Monotonically increasing list of len(labels) + 1 boundary values. The first and last are the documented range limits; the interior values define the bin edges. labels: State names that exactly match the state names in the BN. If omitted, state names are read from the BN node in their definition order — the breakpoints must then produce exactly as many bins as the node has states.

set_grid

set_grid(crs, resolution, extent)

Override the reference grid instead of deriving it from the first input.

Parameters

crs: Target CRS as EPSG string (e.g. "EPSG:32632") or WKT. resolution: Pixel size in CRS units. extent: (xmin, ymin, xmax, ymax) in CRS units.

freeze

freeze(*node_names)

Mark one or more input nodes as static.

On the first :meth:infer call after freezing, each frozen node is fetched, aligned to the grid, and discretised normally; the resulting integer index array is then cached in memory. On all subsequent calls the cached array is reused, skipping fetch, alignment, and discretisation for those nodes.

Calling :meth:freeze with a different set of nodes invalidates any previously cached data.

Parameters

*node_names: Names of input nodes whose data will not change between :meth:infer calls.

precompute

precompute(query)

Pre-run all evidence-state combinations and store a lookup table.

After :meth:precompute, subsequent :meth:infer calls for the same query nodes bypass pgmpy entirely: probabilities are fetched from the table via numpy fancy indexing — O(H×W) rather than O(n_unique_combos) pgmpy queries.

One-time cost: ∏ n_states_i pgmpy queries. For the Lyngen Alps BN (3×2×3×3 state space) this is 54 queries, typically completing in well under a second.

Parameters

query: BN node names to precompute posteriors for. Must match the query passed to :meth:infer for the table path to activate.

Notes

All inputs must have discretizations configured via :meth:set_discretization before calling :meth:precompute.

clear_cache

clear_cache()

Invalidate all cached discrete arrays and the inference table.

Call this if a frozen input actually changes (e.g. you replaced the terrain source), or after calling :meth:freeze with a different set of nodes. The next :meth:infer call will re-fetch and re-cache all frozen nodes.

infer

infer(query)

Run pixel-wise Bayesian inference and return probability rasters.

Parameters

query: List of BN node names whose posterior distributions are requested. These nodes do not need to be root nodes.

Returns

InferenceResult Contains per-pixel probability arrays for each query node plus Shannon entropy. Write to disk with .to_geotiff() or convert to xarray with .to_xarray().

Notes

If :meth:precompute has been called with the same query, this method uses numpy fancy indexing instead of pgmpy queries. If :meth:freeze has been called, cached discrete arrays are reused for frozen nodes.


Real-time optimisation

For repeated inference with static terrain inputs or pre-computed state tables, see the dedicated Real-time optimisation guide covering freeze(), precompute(), and clear_cache().