oxigdal-analytics 0.1.4

Advanced geospatial analytics for OxiGDAL - Time series, clustering, hotspot analysis, and interpolation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Advanced Zonal Statistics
//!
//! Calculate statistics for regions defined by zone masks.

use crate::error::{AnalyticsError, Result};
use scirs2_core::ndarray::{ArrayView2, ArrayView3};
use std::collections::HashMap;

/// Zonal statistics to calculate
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ZonalStatistic {
    /// Mean value
    Mean,
    /// Median value
    Median,
    /// Minimum value
    Min,
    /// Maximum value
    Max,
    /// Sum of values
    Sum,
    /// Count of pixels
    Count,
    /// Standard deviation
    StdDev,
    /// Variance
    Variance,
    /// Coefficient of variation
    CoeffVar,
    /// Percentile (requires parameter)
    Percentile(u8),
}

/// Zonal statistics result
#[derive(Debug, Clone)]
pub struct ZonalResult {
    /// Statistics per zone
    pub zones: HashMap<i32, HashMap<ZonalStatistic, f64>>,
    /// Zone IDs
    pub zone_ids: Vec<i32>,
}

/// Zonal statistics calculator
pub struct ZonalCalculator {
    statistics: Vec<ZonalStatistic>,
    no_data_value: Option<f64>,
}

impl ZonalCalculator {
    /// Create a new zonal calculator
    pub fn new() -> Self {
        Self {
            statistics: vec![
                ZonalStatistic::Mean,
                ZonalStatistic::Min,
                ZonalStatistic::Max,
                ZonalStatistic::Count,
            ],
            no_data_value: None,
        }
    }

    /// Set statistics to calculate
    pub fn with_statistics(mut self, stats: Vec<ZonalStatistic>) -> Self {
        self.statistics = stats;
        self
    }

    /// Set no-data value
    pub fn with_no_data(mut self, value: f64) -> Self {
        self.no_data_value = Some(value);
        self
    }

    /// Calculate zonal statistics
    ///
    /// # Arguments
    /// * `values` - Value raster (height × width)
    /// * `zones` - Zone raster with integer zone IDs (height × width)
    ///
    /// # Errors
    /// Returns error if dimensions don't match
    pub fn calculate(
        &self,
        values: &ArrayView2<f64>,
        zones: &ArrayView2<i32>,
    ) -> Result<ZonalResult> {
        if values.dim() != zones.dim() {
            return Err(AnalyticsError::dimension_mismatch(
                format!("{:?}", values.dim()),
                format!("{:?}", zones.dim()),
            ));
        }

        // Group values by zone
        let mut zone_values: HashMap<i32, Vec<f64>> = HashMap::new();

        for ((i, j), &zone_id) in zones.indexed_iter() {
            let value = values[[i, j]];

            // Skip no-data values
            if let Some(no_data) = self.no_data_value {
                if (value - no_data).abs() < f64::EPSILON {
                    continue;
                }
            }

            zone_values.entry(zone_id).or_default().push(value);
        }

        // Calculate statistics for each zone
        let mut result_zones = HashMap::new();
        let mut zone_ids: Vec<i32> = zone_values.keys().copied().collect();
        zone_ids.sort_unstable();

        for (&zone_id, values_in_zone) in &zone_values {
            let mut stats = HashMap::new();

            for &statistic in &self.statistics {
                let value = self.calculate_statistic(statistic, values_in_zone)?;
                stats.insert(statistic, value);
            }

            result_zones.insert(zone_id, stats);
        }

        Ok(ZonalResult {
            zones: result_zones,
            zone_ids,
        })
    }

    /// Calculate multi-band zonal statistics
    ///
    /// # Arguments
    /// * `values` - Multi-band value raster (height × width × bands)
    /// * `zones` - Zone raster (height × width)
    ///
    /// # Errors
    /// Returns error if dimensions don't match
    pub fn calculate_multiband(
        &self,
        values: &ArrayView3<f64>,
        zones: &ArrayView2<i32>,
    ) -> Result<Vec<ZonalResult>> {
        let (height, width, n_bands) = values.dim();

        if (height, width) != zones.dim() {
            return Err(AnalyticsError::dimension_mismatch(
                format!("{}x{}", height, width),
                format!("{:?}", zones.dim()),
            ));
        }

        let mut results = Vec::with_capacity(n_bands);

        for band in 0..n_bands {
            let band_values = values.slice(s![.., .., band]);
            let result = self.calculate(&band_values, zones)?;
            results.push(result);
        }

        Ok(results)
    }

    /// Calculate a single statistic
    fn calculate_statistic(&self, stat: ZonalStatistic, values: &[f64]) -> Result<f64> {
        if values.is_empty() {
            return Ok(f64::NAN);
        }

        match stat {
            ZonalStatistic::Mean => Ok(values.iter().sum::<f64>() / values.len() as f64),
            ZonalStatistic::Median => self.calculate_median(values),
            ZonalStatistic::Min => values
                .iter()
                .copied()
                .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
                .ok_or_else(|| AnalyticsError::zonal_stats_error("Failed to compute min")),
            ZonalStatistic::Max => values
                .iter()
                .copied()
                .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
                .ok_or_else(|| AnalyticsError::zonal_stats_error("Failed to compute max")),
            ZonalStatistic::Sum => Ok(values.iter().sum()),
            ZonalStatistic::Count => Ok(values.len() as f64),
            ZonalStatistic::StdDev => self.calculate_std_dev(values),
            ZonalStatistic::Variance => self.calculate_variance(values),
            ZonalStatistic::CoeffVar => {
                let mean = values.iter().sum::<f64>() / values.len() as f64;
                let std_dev = self.calculate_std_dev(values)?;
                Ok(if mean.abs() > f64::EPSILON {
                    (std_dev / mean) * 100.0
                } else {
                    f64::NAN
                })
            }
            ZonalStatistic::Percentile(p) => self.calculate_percentile(values, p),
        }
    }

    fn calculate_median(&self, values: &[f64]) -> Result<f64> {
        let mut sorted = values.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let n = sorted.len();
        if n % 2 == 0 {
            Ok((sorted[n / 2 - 1] + sorted[n / 2]) / 2.0)
        } else {
            Ok(sorted[n / 2])
        }
    }

    fn calculate_variance(&self, values: &[f64]) -> Result<f64> {
        let n = values.len() as f64;
        let mean = values.iter().sum::<f64>() / n;
        let variance = values.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / n;
        Ok(variance)
    }

    fn calculate_std_dev(&self, values: &[f64]) -> Result<f64> {
        Ok(self.calculate_variance(values)?.sqrt())
    }

    fn calculate_percentile(&self, values: &[f64], percentile: u8) -> Result<f64> {
        if percentile > 100 {
            return Err(AnalyticsError::invalid_parameter(
                "percentile",
                "must be between 0 and 100",
            ));
        }

        let mut sorted = values.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let n = sorted.len();
        let rank = (percentile as f64 / 100.0) * ((n - 1) as f64);
        let lower_idx = rank.floor() as usize;
        let upper_idx = rank.ceil() as usize;
        let fraction = rank - (lower_idx as f64);

        Ok(sorted[lower_idx] + fraction * (sorted[upper_idx] - sorted[lower_idx]))
    }
}

impl Default for ZonalCalculator {
    fn default() -> Self {
        Self::new()
    }
}

/// Weighted zonal statistics calculator
pub struct WeightedZonalCalculator {
    calculator: ZonalCalculator,
}

impl WeightedZonalCalculator {
    /// Create a new weighted zonal calculator
    pub fn new() -> Self {
        Self {
            calculator: ZonalCalculator::new(),
        }
    }

    /// Set statistics to calculate
    pub fn with_statistics(mut self, stats: Vec<ZonalStatistic>) -> Self {
        self.calculator = self.calculator.with_statistics(stats);
        self
    }

    /// Calculate weighted zonal statistics
    ///
    /// # Arguments
    /// * `values` - Value raster
    /// * `weights` - Weight raster (same dimensions as values)
    /// * `zones` - Zone raster
    ///
    /// # Errors
    /// Returns error if dimensions don't match
    pub fn calculate(
        &self,
        values: &ArrayView2<f64>,
        weights: &ArrayView2<f64>,
        zones: &ArrayView2<i32>,
    ) -> Result<ZonalResult> {
        if values.dim() != weights.dim() || values.dim() != zones.dim() {
            return Err(AnalyticsError::dimension_mismatch(
                format!("{:?}", values.dim()),
                "all inputs must have same dimensions".to_string(),
            ));
        }

        // Group weighted values by zone
        let mut zone_data: HashMap<i32, (Vec<f64>, Vec<f64>)> = HashMap::new();

        for ((i, j), &zone_id) in zones.indexed_iter() {
            let value = values[[i, j]];
            let weight = weights[[i, j]];

            if weight > 0.0 {
                let entry = zone_data
                    .entry(zone_id)
                    .or_insert_with(|| (Vec::new(), Vec::new()));
                entry.0.push(value);
                entry.1.push(weight);
            }
        }

        // Calculate weighted statistics
        let mut result_zones = HashMap::new();
        let mut zone_ids: Vec<i32> = zone_data.keys().copied().collect();
        zone_ids.sort_unstable();

        for (&zone_id, (values_in_zone, weights_in_zone)) in &zone_data {
            let mut stats = HashMap::new();

            // Weighted mean
            let weighted_sum: f64 = values_in_zone
                .iter()
                .zip(weights_in_zone.iter())
                .map(|(v, w)| v * w)
                .sum();
            let weight_sum: f64 = weights_in_zone.iter().sum();

            if weight_sum > f64::EPSILON {
                stats.insert(ZonalStatistic::Mean, weighted_sum / weight_sum);
            }

            // Count (unweighted)
            stats.insert(ZonalStatistic::Count, values_in_zone.len() as f64);

            // Min/Max (unweighted)
            if let Some(&min) = values_in_zone
                .iter()
                .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            {
                stats.insert(ZonalStatistic::Min, min);
            }

            if let Some(&max) = values_in_zone
                .iter()
                .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            {
                stats.insert(ZonalStatistic::Max, max);
            }

            result_zones.insert(zone_id, stats);
        }

        Ok(ZonalResult {
            zones: result_zones,
            zone_ids,
        })
    }
}

impl Default for WeightedZonalCalculator {
    fn default() -> Self {
        Self::new()
    }
}

// Import ndarray slice macro
use scirs2_core::ndarray::s;

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    use scirs2_core::ndarray::{Array, array};

    #[test]
    fn test_zonal_basic() {
        let values = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];
        let zones = array![[1, 1, 2], [1, 2, 2], [2, 2, 2]];

        let calculator = ZonalCalculator::new();
        let result = calculator
            .calculate(&values.view(), &zones.view())
            .expect("Zonal statistics calculation should succeed");

        assert_eq!(result.zone_ids.len(), 2);
        assert!(result.zones.contains_key(&1));
        assert!(result.zones.contains_key(&2));

        // Zone 1: values 1, 2, 4
        let zone1_stats = &result.zones[&1];
        assert_abs_diff_eq!(
            zone1_stats[&ZonalStatistic::Mean],
            (1.0 + 2.0 + 4.0) / 3.0,
            epsilon = 1e-10
        );
    }

    #[test]
    fn test_zonal_statistics() {
        let values = array![[1.0, 2.0], [3.0, 4.0]];
        let zones = array![[1, 1], [1, 1]];

        let calculator = ZonalCalculator::new().with_statistics(vec![
            ZonalStatistic::Mean,
            ZonalStatistic::Min,
            ZonalStatistic::Max,
            ZonalStatistic::StdDev,
        ]);

        let result = calculator
            .calculate(&values.view(), &zones.view())
            .expect("Zonal statistics with multiple stats should succeed");
        let zone1_stats = &result.zones[&1];

        assert_abs_diff_eq!(zone1_stats[&ZonalStatistic::Mean], 2.5, epsilon = 1e-10);
        assert_abs_diff_eq!(zone1_stats[&ZonalStatistic::Min], 1.0, epsilon = 1e-10);
        assert_abs_diff_eq!(zone1_stats[&ZonalStatistic::Max], 4.0, epsilon = 1e-10);
    }

    #[test]
    fn test_weighted_zonal() {
        let values = array![[1.0, 2.0], [3.0, 4.0]];
        let weights = array![[1.0, 1.0], [1.0, 1.0]];
        let zones = array![[1, 1], [1, 1]];

        let calculator = WeightedZonalCalculator::new();
        let result = calculator
            .calculate(&values.view(), &weights.view(), &zones.view())
            .expect("Weighted zonal statistics should succeed");

        let zone1_stats = &result.zones[&1];
        assert_abs_diff_eq!(zone1_stats[&ZonalStatistic::Mean], 2.5, epsilon = 1e-10);
    }

    #[test]
    fn test_percentile() {
        let values = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        let zones = array![[1, 1, 1], [1, 1, 1]];

        let calculator = ZonalCalculator::new().with_statistics(vec![
            ZonalStatistic::Percentile(50), // Median
            ZonalStatistic::Percentile(25),
            ZonalStatistic::Percentile(75),
        ]);

        let result = calculator
            .calculate(&values.view(), &zones.view())
            .expect("Percentile calculation should succeed");
        let zone1_stats = &result.zones[&1];

        assert_abs_diff_eq!(
            zone1_stats[&ZonalStatistic::Percentile(50)],
            3.5,
            epsilon = 1e-10
        );
    }
}