rustalgos

Cityseer high-performance algorithms implemented in Rust.

check_numerical_data

check_numerical_data
(
data_arr
)

Validates that all elements in a 2D numerical array are finite.

Raises

ValueError

If any element is not finite (NaN or infinity).

distances_from_betas

distances_from_betas
(
betas : list[float]
min_threshold_wt : float | None = None
)->[ list[int] ]

Convert decay parameters (betas) to distance thresholds (dmaxd_{max}). Requires betas > 0 and sorted in strictly decreasing order. Uses a default minimum weight threshold.

Parameters

betas
list[float]

β\beta values (> 0, strictly decreasing) to convert.

min_threshold_wt
float | None

Optional cutoff weight wminw_{min} (default: ~0.0183).

Returns

list[int]

Corresponding distance thresholds dmaxd_{max}.

Raises

ValueError

If inputs are invalid (empty, non-positive, not decreasing).

Notes

from cityseer.metrics import networks

# a list of betas
distances = [400, 200]
# convert to betas
betas = networks.beta_from_distance(distances)
print(betas)  # prints: array([0.01, 0.02])

Most networks module methods can be invoked with either distances or betas parameters, but not both. If using the distances parameter, then this function will be called in order to extrapolate the decay parameters implicitly, using:

β=log(wmin)dmax\beta = -\frac{log(w_{min})}{d_{max}}

The default min_threshold_wt of wmin=0.01831563888873418w_{min}=0.01831563888873418 yields conveniently rounded β\beta parameters, for example:

dmaxd_{max}β\beta
200m0.02
400m0.01
800m0.005
1600m0.0025

betas_from_distances

betas_from_distances
(
distances : list[int]
min_threshold_wt : float | None = None
)->[ list[float] ]

Convert distance thresholds (dmaxd_{max}) to decay parameters (betas). Requires distances > 0 and sorted in strictly increasing order. Uses a default minimum weight threshold.

Parameters

distances
list[int]

dmaxd_{max} values (> 0, strictly increasing) to convert.

min_threshold_wt
float | None

Optional cutoff weight wminw_{min} (default: ~0.0183).

Returns

list[float]

Corresponding decay parameters β\beta.

Raises

ValueError

If inputs are invalid (empty, non-positive, not increasing).

Notes

from cityseer import rustalgos

# a list of betas
betas = [0.01, 0.02]
# convert to distance thresholds
d_max = rustalgos.distances_from_betas(betas)
print(d_max)
# prints: [400, 200]

Weighted measures such as the gravity index, weighted betweenness, and weighted land-use accessibilities are computed using a negative exponential decay function in the form of:

weight=exp(βdistance)weight = exp(-\beta \cdot distance)

The strength of the decay is controlled by the β\beta parameter, which reflects a decreasing willingness to walk correspondingly farther distances. For example, if β=0.005\beta=0.005 were to represent a person’s willingness to walk to a bus stop, then a location 100m distant would be weighted at 60% and a location 400m away would be weighted at 13.5%. After an initially rapid decrease, the weightings decay ever more gradually in perpetuity; thus, once a sufficiently small weight is encountered it becomes computationally expensive to consider locations any farther away. The minimum weight at which this cutoff occurs is represented by wminw_{min}, and the corresponding maximum distance threshold by dmaxd_{max}.

Example beta decays

Most networks module methods can be invoked with either distances or betas parameters, but not both. If using the betas parameter, then this function will be called in order to extrapolate the distance thresholds implicitly, using:

dmax=log(wmin)βd_{max} = \frac{log(w_{min})}{-\beta}

The default min_threshold_wt of wmin=0.01831563888873418w_{min}=0.01831563888873418 yields conveniently rounded dmaxd_{max} walking thresholds, for example:

β\betadmaxd_{max}
0.02200m
0.01400m
0.005800m
0.00251600m

Overriding the default wminw_{min} will adjust the dmaxd_{max} accordingly.

distances_from_seconds

distances_from_seconds
(
seconds : list[int]
speed_m_s : float
)->[ list[int] ]

Convert time in seconds to distance thresholds (dmaxd_{max}) based on speed.

It is generally not necessary to utilise this function directly.

The default speed_m_s of 1.3331.333 yields the following dmaxd_{max} walking thresholds:

secondssecondsdmaxd_{max}
300400m
600800m
12001600m

Setting the speed_m_s to a higher or lower number will affect the dmaxd_{max} accordingly.]

Parameters

seconds
list[int]

Time values in seconds.

speed_m_s
float

Speed in meters per second.

Returns

list[int]

Corresponding distance thresholds dmaxd_{max}.

seconds_from_distances

seconds_from_distances
(
distances : list[int]
speed_m_s : float
)->[ list[int] ]

Convert distance thresholds (dmaxd_{max}) to time in seconds based on speed.

It is generally not necessary to utilise this function directly.

The default speed_m_s of 1.333331.33333 yields the following walking times:

dmaxd_{max}secondsseconds
400m300
800m600
1600m1200

Setting the speed_m_s to a higher or lower number will affect the walking time accordingly.

Parameters

distances
list[int]

Distance thresholds dmaxd_{max}.

speed_m_s
float

Speed in meters per second.

Returns

list[int]

Corresponding time values in seconds.

pair_distances_betas_time

pair_distances_betas_time
(
speed_m_s : float
distances : list[int] | None = None
betas : list[float] | None = None
minutes : list[float] | None = None
min_threshold_wt : float | None = None
)->[ list[int] list[float] list[int] ]

Calculate distances, betas, and seconds, given exactly one of them. Requires exactly one of distances, betas, or minutes to be provided.

Parameters

speed_m_s
float

Walking speed in meters per second.

distances
list[int] | None

Distance thresholds (dmaxd_{max}).

betas
list[float] | None

Decay parameters (β\beta).

minutes
list[float] | None

Time in minutes.

min_threshold_wt
float | None

Optional cutoff weight wminw_{min} for conversions.

Returns

tuple[list[int], list[float], list[int]]

A tuple containing (distances, betas, seconds).

Raises

ValueError

If not exactly one of distances, betas, minutes is provided, or if inputs are invalid.

Notes

Networks should be buffered according to the largest distance threshold that will be used for analysis. This protects nodes near network boundaries from edge falloffs. Nodes outside the area of interest but within these buffered extents should be set to ‘dead’ so that centralities or other forms of measures are not calculated. Whereas metrics are not calculated for ‘dead’ nodes, they can still be traversed by network analysis algorithms when calculating shortest paths and landuse accessibilities.

avg_distances_for_betas

avg_distances_for_betas
(
betas : list[float]
min_threshold_wt : float | None = None
)->[ list[float] ]

Calculate the mean distance corresponding to given beta parameters.

Parameters

betas
list[float]

β\beta parameters.

min_threshold_wt
float | None

Optional cutoff weight wminw_{min}.

Returns

list[float]

The average walking distance for each beta.

Notes

from cityseer.metrics import networks
import numpy as np

distances = [100, 200, 400, 800, 1600]
print("distances", distances)
# distances [ 100  200  400  800 1600]

betas = networks.beta_from_distance(distances)
print("betas", betas)
# betas [0.04   0.02   0.01   0.005  0.0025]

print("avg", networks.avg_distance_for_beta(betas))

clip_wts_curve

clip_wts_curve
(
distances : list[int]
betas : list[float]
spatial_tolerance : int
)->[ list[float] ]

Calculate upper weight bounds for clipping distance decay curves based on spatial tolerance. Used when data point location has uncertainty defined by spatial_tolerance. Determine the upper weights threshold of the distance decay curve for a given β\beta based on the spatial_tolerance parameter. This is used by downstream functions to determine the upper extent at which weights derived for spatial impedance functions are flattened and normalised. This functionality is only intended for situations where the location of datapoints is uncertain for a given spatial tolerance.

Use distance based clipping with caution for smaller distance thresholds. For example, if using a 200m distance threshold clipped by 100m, then substantial distortion is introduced by the process of clipping and normalising the distance decay curve. More generally, smaller distance thresholds should generally be avoided for situations where datapoints are not located with high spatial precision.

Parameters

distances
list[int]

Distance thresholds (dmaxd_{max}).

betas
list[float]

Decay parameters (β\beta).

spatial_tolerance
int

Spatial buffer distance (uncertainty).

Returns

list[float]

Maximum weights for clipping the decay curve for each beta.

clipped_beta_wt

clipped_beta_wt
(
beta : float
max_curve_wt : float
data_dist : float
)->[ float ]

Calculate a single weight using beta decay, clipped by a maximum weight. Applies weight=exp(βdistance)weight = exp(-\beta \cdot distance), ensuring the result does not exceed max_curve_wt.

Parameters

beta
float

The decay parameter β\beta.

max_curve_wt
float

The maximum allowed weight (from clip_wts_curve).

data_dist
float

The distance to the data point.

Returns

float

The calculated (potentially clipped) weight. Returns 0.0 if calculation fails.