oxigeo-temporal 0.2.4

Multi-temporal raster analysis for OxiGeo - Time series, change detection, phenology, and data cube operations
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
//! Integration tests for oxigeo-temporal
#![allow(clippy::expect_used)]

use chrono::{DateTime, NaiveDate};
use oxigeo_temporal::{
    aggregation::{AggregationConfig, AggregationStatistic, TemporalAggregator, TemporalWindow},
    change::{ChangeDetectionConfig, ChangeDetectionMethod, ChangeDetector},
    compositing::{CompositingConfig, CompositingMethod, TemporalCompositor},
    gap_filling::{GapFillMethod, GapFiller},
    phenology::{PhenologyConfig, PhenologyExtractor},
    stack::RasterStack,
    timeseries::{TemporalMetadata, TemporalResolution, TimeSeriesRaster},
    trend::{TrendAnalyzer, TrendMethod},
};
use scirs2_core::ndarray::Array3;

fn create_test_timeseries(n: usize) -> TimeSeriesRaster {
    let mut ts = TimeSeriesRaster::new();

    for i in 0..n {
        let dt = DateTime::from_timestamp(1640995200 + (i as i64) * 86400, 0).expect("valid");
        let date = NaiveDate::from_ymd_opt(2022, 1, 1 + i as u32).expect("valid");
        let metadata = TemporalMetadata::new(dt, date)
            .with_cloud_cover((i * 5) as f32)
            .with_quality_score(1.0 - (i as f32 * 0.05));

        let mut data = Array3::zeros((10, 10, 2));
        for j in 0..10 {
            for k in 0..10 {
                data[[j, k, 0]] = (i + j + k) as f64;
                data[[j, k, 1]] = (i + j + k) as f64 * 2.0;
            }
        }

        ts.add_raster(metadata, data).expect("should add");
    }

    ts
}

#[test]
fn test_timeseries_creation_and_queries() {
    let ts = create_test_timeseries(10);

    assert_eq!(ts.len(), 10);
    assert!(!ts.is_empty());
    assert_eq!(ts.expected_shape(), Some((10, 10, 2)));

    let (start, end) = ts.time_range().expect("should have range");
    assert!(start < end);

    let timestamps = ts.timestamps();
    assert_eq!(timestamps.len(), 10);
}

#[test]
fn test_timeseries_filtering() {
    let mut ts = create_test_timeseries(10);

    let removed = ts.filter_by_cloud_cover(15.0).expect("should filter");
    assert!(removed > 0);
    assert!(ts.len() < 10);
}

#[test]
fn test_pixel_timeseries_extraction() {
    let ts = create_test_timeseries(10);

    let pixel_ts = ts
        .extract_pixel_timeseries(5, 5, 0)
        .expect("should extract");

    assert_eq!(pixel_ts.len(), 10);

    // Values should be increasing
    for i in 1..pixel_ts.len() {
        assert!(pixel_ts[i] >= pixel_ts[i - 1]);
    }
}

#[test]
fn test_mean_compositing() {
    let ts = create_test_timeseries(10);

    let config = CompositingConfig {
        method: CompositingMethod::Mean,
        max_cloud_cover: None,
        min_quality: None,
        ..Default::default()
    };

    let composite = TemporalCompositor::composite(&ts, &config).expect("should composite");

    assert_eq!(composite.data.shape(), &[10, 10, 2]);
    assert!(composite.count[[0, 0, 0]] > 0);
}

#[test]
fn test_median_compositing() {
    let ts = create_test_timeseries(10);

    let config = CompositingConfig {
        method: CompositingMethod::Median,
        max_cloud_cover: Some(30.0),
        ..Default::default()
    };

    let composite = TemporalCompositor::composite(&ts, &config).expect("should composite");

    assert_eq!(composite.data.shape(), &[10, 10, 2]);
}

#[test]
fn test_maximum_value_composite() {
    let ts = create_test_timeseries(10);

    let config = CompositingConfig {
        method: CompositingMethod::Maximum,
        max_cloud_cover: None,
        min_quality: None,
        ..Default::default()
    };

    let composite = TemporalCompositor::composite(&ts, &config).expect("should composite");

    assert_eq!(composite.data.shape(), &[10, 10, 2]);
}

#[test]
fn test_temporal_interpolation() {
    let ts = TimeSeriesRaster::with_resolution(TemporalResolution::Daily);

    // Create sparse time series with gaps
    let ts = {
        let mut ts = ts;
        let dt1 = DateTime::from_timestamp(1640995200, 0).expect("valid");
        let date1 = NaiveDate::from_ymd_opt(2022, 1, 1).expect("valid");
        let metadata1 = TemporalMetadata::new(dt1, date1);
        ts.add_raster(metadata1, Array3::from_elem((5, 5, 1), 10.0))
            .expect("should add");

        // Gap of 10 days
        let dt2 = DateTime::from_timestamp(1641859200, 0).expect("valid");
        let date2 = NaiveDate::from_ymd_opt(2022, 1, 11).expect("valid");
        let metadata2 = TemporalMetadata::new(dt2, date2);
        ts.add_raster(metadata2, Array3::from_elem((5, 5, 1), 20.0))
            .expect("should add");
        ts
    };

    let filled_ts = GapFiller::fill_gaps(&ts, GapFillMethod::LinearInterpolation, None)
        .expect("should interpolate");

    // The filled time series should have at least as many entries
    assert!(filled_ts.len() >= ts.len());
}

#[test]
fn test_temporal_aggregation_monthly() {
    let ts = create_test_timeseries(30);

    let config = AggregationConfig {
        window: TemporalWindow::Monthly,
        statistics: vec![AggregationStatistic::Mean, AggregationStatistic::Max],
        ..Default::default()
    };

    let result = TemporalAggregator::aggregate(&ts, &config).expect("should aggregate");

    assert!(result.get("Mean").is_some());
    assert!(result.get("Max").is_some());

    let mean_ts = result.get("Mean").expect("should have mean");
    assert!(!mean_ts.is_empty());
}

#[test]
fn test_temporal_aggregation_rolling() {
    let ts = create_test_timeseries(20);

    let config = AggregationConfig {
        window: TemporalWindow::Rolling(7),
        statistics: vec![AggregationStatistic::Mean],
        min_observations: 5,
        ..Default::default()
    };

    let result = TemporalAggregator::aggregate(&ts, &config).expect("should aggregate");

    let mean_ts = result.get("Mean").expect("should have mean");
    assert!(!mean_ts.is_empty());
}

#[test]
fn test_change_detection_simple_difference() {
    let ts = create_test_timeseries(10);

    let config = ChangeDetectionConfig {
        method: ChangeDetectionMethod::SimpleDifference,
        ..Default::default()
    };

    let result = ChangeDetector::detect(&ts, &config).expect("should detect");

    assert_eq!(result.magnitude.shape(), &[10, 10, 2]);
    assert_eq!(result.direction.shape(), &[10, 10, 2]);

    // Should show positive change (increasing values)
    assert!(result.direction[[0, 0, 0]] > 0);
}

#[test]
fn test_change_detection_relative() {
    let ts = create_test_timeseries(10);

    let config = ChangeDetectionConfig {
        method: ChangeDetectionMethod::RelativeChange,
        ..Default::default()
    };

    let result = ChangeDetector::detect(&ts, &config).expect("should detect");
    assert_eq!(result.magnitude.shape(), &[10, 10, 2]);
}

#[test]
fn test_trend_analysis_linear() {
    let ts = create_test_timeseries(20);

    let result = TrendAnalyzer::analyze(&ts, TrendMethod::Linear).expect("should analyze");

    assert_eq!(result.slope.shape(), &[10, 10, 2]);
    assert_eq!(result.intercept.shape(), &[10, 10, 2]);

    // Slope should be positive (increasing trend)
    assert!(result.slope[[0, 0, 0]] > 0.0);
    assert_eq!(result.direction[[0, 0, 0]], 1);
}

#[test]
fn test_trend_analysis_sens_slope() {
    let ts = create_test_timeseries(15);

    let result = TrendAnalyzer::analyze(&ts, TrendMethod::SensSlope).expect("should analyze");

    assert_eq!(result.slope.shape(), &[10, 10, 2]);
    assert!(result.slope[[0, 0, 0]] > 0.0);
}

#[test]
fn test_raster_stack_operations() {
    let ts = create_test_timeseries(10);
    let stack = RasterStack::from_timeseries(&ts).expect("should create stack");

    assert_eq!(stack.shape(), (10, 10, 10, 2));

    let time_slice = stack.get_time_slice(5).expect("should get slice");
    assert_eq!(time_slice.shape(), &[10, 10, 2]);

    let pixel_ts = stack
        .get_pixel_timeseries(5, 5, 0)
        .expect("should get timeseries");
    assert_eq!(pixel_ts.len(), 10);
}

#[test]
fn test_stack_temporal_statistics() {
    let ts = create_test_timeseries(10);
    let stack = RasterStack::from_timeseries(&ts).expect("should create stack");

    let mean = stack.mean_temporal().expect("should compute mean");
    assert_eq!(mean.shape(), &[10, 10, 2]);

    let median = stack.median_temporal().expect("should compute median");
    assert_eq!(median.shape(), &[10, 10, 2]);

    let min = stack.min_temporal().expect("should compute min");
    let max = stack.max_temporal().expect("should compute max");

    // Max should be >= min for all pixels
    for i in 0..10 {
        for j in 0..10 {
            for k in 0..2 {
                assert!(max[[i, j, k]] >= min[[i, j, k]]);
            }
        }
    }
}

#[test]
fn test_stack_concatenation() {
    let ts1 = create_test_timeseries(5);
    let stack1 = RasterStack::from_timeseries(&ts1).expect("should create");

    let ts2 = create_test_timeseries(3);
    let stack2 = RasterStack::from_timeseries(&ts2).expect("should create");

    let concatenated =
        RasterStack::concatenate_time(vec![stack1, stack2]).expect("should concatenate");

    assert_eq!(concatenated.shape(), (8, 10, 10, 2));
}

#[test]
fn test_stack_subsetting() {
    let ts = create_test_timeseries(10);
    let stack = RasterStack::from_timeseries(&ts).expect("should create");

    let time_subset = stack.subset_time(2, 7).expect("should subset");
    assert_eq!(time_subset.shape(), (5, 10, 10, 2));

    let band_subset = stack.subset_bands(&[0]).expect("should subset");
    assert_eq!(band_subset.shape(), (10, 10, 10, 1));
}

#[test]
fn test_phenology_extraction() {
    let mut ts = TimeSeriesRaster::new();

    // Simulate vegetation phenology with sinusoidal pattern
    for i in 0..36 {
        // 36 observations over a year
        let dt = DateTime::from_timestamp(1640995200 + (i * 10 * 86400), 0).expect("valid");
        let days = i * 10;
        let date =
            NaiveDate::from_ymd_opt(2022, 1, 1).expect("valid") + chrono::Duration::days(days);
        let metadata = TemporalMetadata::new(dt, date);

        // Sinusoidal NDVI-like pattern
        let angle = (days as f64 / 365.0) * 2.0 * std::f64::consts::PI;
        let ndvi = 0.3 + 0.4 * angle.sin(); // NDVI values 0.3-0.7

        let data = Array3::from_elem((5, 5, 1), ndvi);
        ts.add_raster(metadata, data).expect("should add");
    }

    let config = PhenologyConfig::default();
    let metrics = PhenologyExtractor::extract(&ts, &config).expect("should extract");

    // Check that metrics were computed
    assert!(metrics.amplitude[[2, 2, 0]] > 0.0);
}

#[test]
fn test_integration_compositing_and_change_detection() {
    let ts = create_test_timeseries(20);

    // Create composite for first half
    let first_half = {
        let mut ts_first = TimeSeriesRaster::new();
        for idx in 0..10 {
            let entry = ts.get_by_index(idx).expect("should exist");
            if let Some(data) = &entry.data {
                ts_first
                    .add_raster(entry.metadata.clone(), data.clone())
                    .expect("should add");
            }
        }
        ts_first
    };

    // Create composite for second half
    let second_half = {
        let mut ts_second = TimeSeriesRaster::new();
        for idx in 10..20 {
            let entry = ts.get_by_index(idx).expect("should exist");
            if let Some(data) = &entry.data {
                ts_second
                    .add_raster(entry.metadata.clone(), data.clone())
                    .expect("should add");
            }
        }
        ts_second
    };

    let config = CompositingConfig {
        method: CompositingMethod::Mean,
        ..Default::default()
    };

    let composite1 = TemporalCompositor::composite(&first_half, &config).expect("should composite");
    let composite2 =
        TemporalCompositor::composite(&second_half, &config).expect("should composite");

    // Compare composites - should show increase
    for i in 0..10 {
        for j in 0..10 {
            for k in 0..2 {
                assert!(composite2.data[[i, j, k]] >= composite1.data[[i, j, k]]);
            }
        }
    }
}

#[test]
fn test_comprehensive_workflow() {
    // Create time series
    let mut ts = create_test_timeseries(30);

    // Filter by quality
    ts.filter_by_cloud_cover(25.0).expect("should filter");

    // Detect gaps
    let resolution = TemporalResolution::Daily;
    ts.set_resolution(resolution);
    let _gaps = ts.detect_gaps().expect("should detect gaps");
    // Gaps may or may not exist depending on filtering

    // Create stack
    let stack = RasterStack::from_timeseries(&ts).expect("should create stack");

    // Compute temporal statistics
    let mean = stack.mean_temporal().expect("should compute mean");
    let std = stack.std_temporal().expect("should compute std");

    // Detect changes
    let change_config = ChangeDetectionConfig::default();
    let changes = ChangeDetector::detect_stack(&stack, &change_config).expect("should detect");

    // Analyze trends
    let trend = TrendAnalyzer::analyze(&ts, TrendMethod::Linear).expect("should analyze");

    // All operations should complete successfully
    assert_eq!(mean.shape(), &[10, 10, 2]);
    assert_eq!(std.shape(), &[10, 10, 2]);
    assert_eq!(changes.magnitude.shape(), &[10, 10, 2]);
    assert_eq!(trend.slope.shape(), &[10, 10, 2]);
}