Skip to content

Commit

Permalink
return nan instead of inf for distances (QGIS vis)
Browse files Browse the repository at this point in the history
  • Loading branch information
songololo committed Nov 27, 2023
1 parent 56d3e42 commit 2929e75
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "cityseer"
version = '4.5.4'
version = '4.5.5'
description = "Computational tools for network-based pedestrian-scale urban analysis"
readme = "README.md"
requires-python = ">=3.10, <3.12"
Expand Down
4 changes: 2 additions & 2 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl DataMap {
MetricResult::new(
distances.clone(),
network_structure.node_count(),
f32::INFINITY,
f32::NAN,
),
)
})
Expand Down Expand Up @@ -390,7 +390,7 @@ impl DataMap {
.fetch_add(val_wt.unwrap(), Ordering::Relaxed);
let current_dist =
dists[&lu_class].metric[i][*netw_src_idx].load(Ordering::Relaxed);
if data_dist < current_dist {
if current_dist.is_nan() || data_dist < current_dist {
dists[&lu_class].metric[i][*netw_src_idx]
.store(data_dist, Ordering::Relaxed);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/metrics/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,23 @@ def test_compute_accessibilities(primal_graph):
accessibility_data[acc_key].unweighted[dist_key],
atol=config.ATOL,
rtol=config.RTOL,
equal_nan=True,
)
acc_data_key_wt = config.prep_gdf_key(f"{acc_key}_{dist_key}_weighted")
assert np.allclose(
nodes_gdf[acc_data_key_wt].values,
accessibility_data[acc_key].weighted[dist_key],
atol=config.ATOL,
rtol=config.RTOL,
equal_nan=True,
)
acc_data_key_dist = config.prep_gdf_key(f"{acc_key}_{dist_key}_distance")
assert np.allclose(
nodes_gdf[acc_data_key_dist].values,
accessibility_data[acc_key].distance[dist_key],
atol=config.ATOL,
rtol=config.RTOL,
equal_nan=True,
)
# most integrity checks happen in underlying method
with pytest.raises(ValueError):
Expand Down
24 changes: 12 additions & 12 deletions tests/rustalgos/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ def test_accessibility(primal_graph):
b_wt = 0
c_wt = 0
z_wt = 0
a_dist = np.inf
b_dist = np.inf
c_dist = np.inf
z_dist = np.inf
a_dist = np.nan
b_dist = np.nan
c_dist = np.nan
z_dist = np.nan
# iterate reachable
reachable_entries = data_map.aggregate_to_src_idx(src_idx, network_structure, max_dist)
for data_key, data_dist in reachable_entries.items():
Expand All @@ -131,22 +131,22 @@ def test_accessibility(primal_graph):
if data_class == "a":
a_nw += 1
a_wt += np.exp(-beta * data_dist)
if data_dist < a_dist:
if np.isnan(a_dist) or data_dist < a_dist:
a_dist = data_dist
elif data_class == "b":
b_nw += 1
b_wt += np.exp(-beta * data_dist)
if data_dist < b_dist:
if np.isnan(b_dist) or data_dist < b_dist:
b_dist = data_dist
elif data_class == "c":
c_nw += 1
c_wt += np.exp(-beta * data_dist)
if data_dist < c_dist:
if np.isnan(c_dist) or data_dist < c_dist:
c_dist = data_dist
elif data_class == "z":
z_nw += 1
z_wt += np.exp(-beta * data_dist)
if data_dist < z_dist:
if np.isnan(z_dist) or data_dist < z_dist:
z_dist = data_dist
# assertions
assert accessibilities["a"].unweighted[dist][src_idx] - a_nw < config.ATOL
Expand All @@ -160,19 +160,19 @@ def test_accessibility(primal_graph):
if np.isfinite(a_dist):
assert accessibilities["a"].distance[dist][src_idx] - a_dist < config.ATOL
else:
assert np.isposinf(a_dist) and np.isposinf(accessibilities["a"].distance[dist][src_idx])
assert np.isnan(a_dist) and np.isnan(accessibilities["a"].distance[dist][src_idx])
if np.isfinite(b_dist):
assert accessibilities["b"].distance[dist][src_idx] - b_dist < config.ATOL
else:
assert np.isposinf(b_dist) and np.isposinf(accessibilities["b"].distance[dist][src_idx])
assert np.isnan(b_dist) and np.isnan(accessibilities["b"].distance[dist][src_idx])
if np.isfinite(c_dist):
assert accessibilities["c"].distance[dist][src_idx] - c_dist < config.ATOL
else:
assert np.isposinf(c_dist) and np.isposinf(accessibilities["c"].distance[dist][src_idx])
assert np.isnan(c_dist) and np.isnan(accessibilities["c"].distance[dist][src_idx])
if np.isfinite(z_dist):
assert accessibilities["z"].distance[dist][src_idx] - z_dist < config.ATOL
else:
assert np.isposinf(z_dist) and np.isposinf(accessibilities["z"].distance[dist][src_idx])
assert np.isnan(z_dist) and np.isnan(accessibilities["z"].distance[dist][src_idx])
# check for deduplication
assert z_nw in [0, 1]
assert z_wt <= 1
Expand Down

0 comments on commit 2929e75

Please sign in to comment.