Skip to content

Commit 29778fd

Browse files
committed
a lot of refactoring and modularization
1 parent 3be5857 commit 29778fd

8 files changed

Lines changed: 950 additions & 1274 deletions

File tree

File renamed without changes.
Lines changed: 405 additions & 345 deletions
Large diffs are not rendered by default.

‎src/krige/mod.rs‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! Kriging: the global kriging matrix operations and local
2+
//! (moving-neighborhood) kriging.
3+
//!
4+
//! ```text
5+
//! global.rs global kriging: contract a pre-inverted kriging matrix
6+
//! local.rs local kriging of one or many fields (weights cached
7+
//! per target and neighbor mask)
8+
//!
9+
//! used by local kriging:
10+
//! neighbors.rs k-d tree radius search
11+
//! solve.rs linear algebra: Cholesky + Schur complement,
12+
//! dense LU / SVD fallback
13+
//!
14+
//! test_util.rs test helpers, incl. the reference system builders
15+
//! build_lhs / build_rhs
16+
//! ```
17+
//!
18+
//! Dependencies only point downwards: local kriging uses `neighbors` and
19+
//! `solve`; `solve` knows nothing about kriging.
20+
//!
21+
//! The public API is re-exported here, so callers use
22+
//! `crate::krige::calc_field_krige_local` etc.
23+
24+
use std::fmt;
25+
26+
pub mod global;
27+
pub mod local;
28+
mod neighbors;
29+
pub mod solve;
30+
#[cfg(test)]
31+
mod test_util;
32+
33+
pub use global::{calculator_field_krige, calculator_field_krige_and_variance};
34+
pub use local::{calc_field_krige_local, drift_terms_used, LocalKrigeResult, LocalKrigeStats};
35+
pub use solve::{solve_dense, solve_kriging, SingularSystem};
36+
37+
/// Errors of local kriging.
38+
#[derive(Debug, Clone, PartialEq, Eq)]
39+
pub enum KrigeError {
40+
/// Inconsistent array shapes or non-finite positions / radius.
41+
InvalidInput(String),
42+
/// A local system could not be solved, not even by the least-squares
43+
/// fallback of [`solve_dense`] (in practice essentially unreachable).
44+
SingularSystem {
45+
/// index of the target point
46+
target_index: usize,
47+
/// index of the field (e.g. day)
48+
field_index: usize,
49+
},
50+
}
51+
52+
impl fmt::Display for KrigeError {
53+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
match self {
55+
Self::InvalidInput(msg) => write!(f, "invalid input: {msg}"),
56+
Self::SingularSystem {
57+
target_index,
58+
field_index,
59+
} => write!(
60+
f,
61+
"local kriging matrix for target point {target_index} and field \
62+
{field_index} is singular even for the least-squares fallback"
63+
),
64+
}
65+
}
66+
}
67+
68+
impl std::error::Error for KrigeError {}

‎src/krige/neighbors.rs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//! Radius search for local kriging: a k-d tree over the conditioning points.
2+
3+
use kdtree::{distance::squared_euclidean, KdTree};
4+
use ndarray::{ArrayView1, ArrayView2};
5+
6+
/// k-d tree over the conditioning points with a fixed search radius.
7+
pub(crate) struct Neighbors {
8+
tree: KdTree<f64, usize, Vec<f64>>,
9+
/// squared radius: the tree compares squared distances (no square roots)
10+
radius_sq: f64,
11+
}
12+
13+
impl Neighbors {
14+
/// Build the tree over the columns of `cond_pos` (shape `(dim, n)`).
15+
pub(crate) fn new(cond_pos: ArrayView2<'_, f64>, radius: f64) -> Self {
16+
let mut tree = KdTree::new(cond_pos.nrows());
17+
for i in 0..cond_pos.ncols() {
18+
tree.add(cond_pos.column(i).to_vec(), i)
19+
.expect("cond_pos columns are finite and match `dim`");
20+
}
21+
Self {
22+
tree,
23+
radius_sq: radius * radius,
24+
}
25+
}
26+
27+
/// Conditioning points within the radius of `target`, sorted by
28+
/// distance: `(indices, distances)`, both empty if there are none.
29+
pub(crate) fn within(&self, target: ArrayView1<'_, f64>) -> (Vec<usize>, Vec<f64>) {
30+
self.tree
31+
.within(&target.to_vec(), self.radius_sq, &squared_euclidean)
32+
.expect("target point is finite and matches `dim`")
33+
.into_iter()
34+
.map(|(sq_dist, &idx)| (idx, sq_dist.sqrt()))
35+
.unzip()
36+
}
37+
}

0 commit comments

Comments
 (0)