oxigdal-ml 0.1.5

Machine learning capabilities for OxiGDAL - ONNX Runtime integration for geospatial ML workflows
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
//! Image classification for geospatial data
//!
//! This module provides scene classification, land cover classification,
//! and multi-label classification capabilities.

use oxigdal_core::buffer::RasterBuffer;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::debug;

use crate::error::{PostprocessingError, Result};

/// Classification result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassificationResult {
    /// Predicted class ID
    pub class_id: usize,
    /// Class label
    pub class_label: Option<String>,
    /// Confidence score (0.0 to 1.0)
    pub confidence: f32,
    /// All class probabilities
    pub probabilities: HashMap<usize, f32>,
}

/// Multi-label classification result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiLabelResult {
    /// Predicted labels with confidence scores
    pub labels: Vec<LabelPrediction>,
    /// Threshold used for predictions
    pub threshold: f32,
}

/// A single label prediction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LabelPrediction {
    /// Class ID
    pub class_id: usize,
    /// Class label
    pub class_label: Option<String>,
    /// Confidence score
    pub confidence: f32,
}

/// Land cover classification result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LandCoverResult {
    /// Classification result
    pub classification: ClassificationResult,
    /// Land cover type
    pub land_cover_type: LandCoverType,
}

/// Land cover types (simplified taxonomy)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LandCoverType {
    /// Water bodies
    Water,
    /// Developed/urban areas
    Developed,
    /// Barren land
    Barren,
    /// Forest
    Forest,
    /// Shrubland
    Shrubland,
    /// Herbaceous/grassland
    Herbaceous,
    /// Planted/cultivated
    Cultivated,
    /// Wetlands
    Wetlands,
    /// Ice/snow
    IceSnow,
    /// Unknown/other
    Unknown,
}

/// Performs single-label classification from probability buffer
///
/// # Errors
/// Returns an error if classification fails
pub fn classify_single_label(
    probabilities: &RasterBuffer,
    class_labels: Option<&[String]>,
    confidence_threshold: f32,
) -> Result<ClassificationResult> {
    if !(0.0..=1.0).contains(&confidence_threshold) {
        return Err(PostprocessingError::InvalidThreshold {
            value: confidence_threshold,
        }
        .into());
    }

    debug!(
        "Classifying image with {} classes",
        probabilities.pixel_count()
    );

    // For single-label classification, we expect a 1D probability vector
    // This is a simplified implementation
    let num_pixels = probabilities.pixel_count();

    let mut max_prob = 0.0f32;
    let mut max_class = 0usize;
    let mut prob_map = HashMap::new();

    // Collect probabilities
    // In a real implementation, this would properly handle the probability tensor
    for i in 0..num_pixels.min(1000) {
        let x = i % probabilities.width();
        let y = i / probabilities.width();

        let prob = probabilities
            .get_pixel(x, y)
            .map_err(|e| PostprocessingError::ExportFailed {
                reason: format!("Failed to get probability: {}", e),
            })? as f32;

        prob_map.insert(i as usize, prob);

        if prob > max_prob {
            max_prob = prob;
            max_class = i as usize;
        }
    }

    let class_label = class_labels.and_then(|labels| labels.get(max_class).cloned());

    Ok(ClassificationResult {
        class_id: max_class,
        class_label,
        confidence: max_prob,
        probabilities: prob_map,
    })
}

/// Performs multi-label classification
///
/// # Errors
/// Returns an error if classification fails
pub fn classify_multi_label(
    probabilities: &RasterBuffer,
    class_labels: Option<&[String]>,
    threshold: f32,
) -> Result<MultiLabelResult> {
    if !(0.0..=1.0).contains(&threshold) {
        return Err(PostprocessingError::InvalidThreshold { value: threshold }.into());
    }

    debug!("Multi-label classification with threshold {}", threshold);

    let mut predictions = Vec::new();
    let num_pixels = probabilities.pixel_count();

    // Check each class probability
    for i in 0..num_pixels.min(1000) {
        let x = i % probabilities.width();
        let y = i / probabilities.width();

        let prob = probabilities
            .get_pixel(x, y)
            .map_err(|e| PostprocessingError::ExportFailed {
                reason: format!("Failed to get probability: {}", e),
            })? as f32;

        if prob >= threshold {
            let class_label = class_labels.and_then(|labels| labels.get(i as usize).cloned());

            predictions.push(LabelPrediction {
                class_id: i as usize,
                class_label,
                confidence: prob,
            });
        }
    }

    // Sort by confidence (descending)
    predictions.sort_by(|a, b| {
        b.confidence
            .partial_cmp(&a.confidence)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    Ok(MultiLabelResult {
        labels: predictions,
        threshold,
    })
}

/// Classifies land cover from model output
///
/// # Errors
/// Returns an error if classification fails
pub fn classify_land_cover(
    probabilities: &RasterBuffer,
    confidence_threshold: f32,
) -> Result<LandCoverResult> {
    let classification = classify_single_label(probabilities, None, confidence_threshold)?;

    let land_cover_type = map_class_to_land_cover(classification.class_id);

    Ok(LandCoverResult {
        classification,
        land_cover_type,
    })
}

/// Maps a class ID to a land cover type
fn map_class_to_land_cover(class_id: usize) -> LandCoverType {
    // This is a simplified mapping
    // In practice, this would be configured per model
    match class_id {
        0 => LandCoverType::Water,
        1 => LandCoverType::Developed,
        2 => LandCoverType::Barren,
        3 => LandCoverType::Forest,
        4 => LandCoverType::Shrubland,
        5 => LandCoverType::Herbaceous,
        6 => LandCoverType::Cultivated,
        7 => LandCoverType::Wetlands,
        8 => LandCoverType::IceSnow,
        _ => LandCoverType::Unknown,
    }
}

/// Computes top-k predictions
///
/// # Errors
/// Returns an error if computation fails
pub fn top_k_predictions(
    probabilities: &RasterBuffer,
    class_labels: Option<&[String]>,
    k: usize,
) -> Result<Vec<LabelPrediction>> {
    if k == 0 {
        return Ok(Vec::new());
    }

    let mut predictions = Vec::new();
    let num_pixels = probabilities.pixel_count();

    // Collect all probabilities
    for i in 0..num_pixels.min(1000) {
        let x = i % probabilities.width();
        let y = i / probabilities.width();

        let prob = probabilities
            .get_pixel(x, y)
            .map_err(|e| PostprocessingError::ExportFailed {
                reason: format!("Failed to get probability: {}", e),
            })? as f32;

        let class_label = class_labels.and_then(|labels| labels.get(i as usize).cloned());

        predictions.push(LabelPrediction {
            class_id: i as usize,
            class_label,
            confidence: prob,
        });
    }

    // Sort by confidence (descending)
    predictions.sort_by(|a, b| {
        b.confidence
            .partial_cmp(&a.confidence)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    // Return top k
    predictions.truncate(k);

    Ok(predictions)
}

/// Computes confusion metrics for evaluation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfusionMetrics {
    /// True positives
    pub true_positives: u64,
    /// False positives
    pub false_positives: u64,
    /// True negatives
    pub true_negatives: u64,
    /// False negatives
    pub false_negatives: u64,
    /// Precision
    pub precision: f64,
    /// Recall
    pub recall: f64,
    /// F1 score
    pub f1_score: f64,
    /// Accuracy
    pub accuracy: f64,
}

/// Computes confusion metrics from predictions and ground truth
///
/// # Errors
/// Returns an error if computation fails
pub fn compute_confusion_metrics(
    predictions: &RasterBuffer,
    ground_truth: &RasterBuffer,
    positive_class: usize,
) -> Result<ConfusionMetrics> {
    if predictions.width() != ground_truth.width() || predictions.height() != ground_truth.height()
    {
        return Err(PostprocessingError::MergingFailed {
            reason: "Prediction and ground truth dimensions don't match".to_string(),
        }
        .into());
    }

    let mut tp = 0u64;
    let mut fp = 0u64;
    let mut tn = 0u64;
    let mut fn_count = 0u64;

    for y in 0..predictions.height() {
        for x in 0..predictions.width() {
            let pred =
                predictions
                    .get_pixel(x, y)
                    .map_err(|e| PostprocessingError::ExportFailed {
                        reason: format!("Failed to get prediction: {}", e),
                    })? as usize;

            let truth =
                ground_truth
                    .get_pixel(x, y)
                    .map_err(|e| PostprocessingError::ExportFailed {
                        reason: format!("Failed to get ground truth: {}", e),
                    })? as usize;

            let pred_positive = pred == positive_class;
            let truth_positive = truth == positive_class;

            match (pred_positive, truth_positive) {
                (true, true) => tp += 1,
                (true, false) => fp += 1,
                (false, false) => tn += 1,
                (false, true) => fn_count += 1,
            }
        }
    }

    let precision = if tp + fp > 0 {
        tp as f64 / (tp + fp) as f64
    } else {
        0.0
    };

    let recall = if tp + fn_count > 0 {
        tp as f64 / (tp + fn_count) as f64
    } else {
        0.0
    };

    let f1_score = if precision + recall > 0.0 {
        2.0 * (precision * recall) / (precision + recall)
    } else {
        0.0
    };

    let total = tp + fp + tn + fn_count;
    let accuracy = if total > 0 {
        (tp + tn) as f64 / total as f64
    } else {
        0.0
    };

    Ok(ConfusionMetrics {
        true_positives: tp,
        false_positives: fp,
        true_negatives: tn,
        false_negatives: fn_count,
        precision,
        recall,
        f1_score,
        accuracy,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use oxigdal_core::types::RasterDataType;

    #[test]
    fn test_classify_single_label() {
        let probs = RasterBuffer::zeros(10, 1, RasterDataType::Float32);
        let result = classify_single_label(&probs, None, 0.5);
        assert!(result.is_ok());
    }

    #[test]
    fn test_classify_multi_label() {
        let probs = RasterBuffer::zeros(10, 1, RasterDataType::Float32);
        let result = classify_multi_label(&probs, None, 0.5);
        assert!(result.is_ok());
    }

    #[test]
    fn test_land_cover_mapping() {
        assert_eq!(map_class_to_land_cover(0), LandCoverType::Water);
        assert_eq!(map_class_to_land_cover(3), LandCoverType::Forest);
        assert_eq!(map_class_to_land_cover(999), LandCoverType::Unknown);
    }

    #[test]
    fn test_top_k_predictions() {
        let probs = RasterBuffer::zeros(10, 1, RasterDataType::Float32);
        let result = top_k_predictions(&probs, None, 3);
        assert!(result.is_ok());
    }

    #[test]
    fn test_confusion_metrics() {
        let preds = RasterBuffer::zeros(10, 10, RasterDataType::UInt8);
        let truth = RasterBuffer::zeros(10, 10, RasterDataType::UInt8);
        let result = compute_confusion_metrics(&preds, &truth, 1);
        assert!(result.is_ok());
    }
}