sklears-tree 0.1.2

Decision tree algorithms for sklears: CART, ID3, C4.5
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
//! Spatial Data Structures for Geographic Tree Algorithms
//!
//! This module provides specialized data structures and algorithms for handling
//! geospatial data in tree-based models, including spatial splits, geographic
//! partitioning, and location-aware tree construction.

use crate::SplitCriterion;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::{Random, thread_rng};
use sklears_core::{
    error::{Result, SklearsError},
    traits::Untrained,
};
use std::collections::{HashMap, VecDeque};
use std::marker::PhantomData;

use crate::decision_tree::{DecisionTreeConfig, SplitCriterion};

/// Spatial coordinate systems
#[derive(Debug, Clone)]
pub enum CoordinateSystem {
    /// Geographic coordinates (latitude, longitude)
    Geographic,
    /// Projected coordinates (x, y)
    Projected { epsg_code: u32 },
    /// Universal Transverse Mercator
    UTM { zone: u8, hemisphere: Hemisphere },
    /// Web Mercator (EPSG:3857)
    WebMercator,
    /// Custom coordinate system
    Custom { name: String },
}

/// Hemisphere for UTM coordinates
#[derive(Debug, Clone, Copy)]
pub enum Hemisphere {
    North,
    South,
}

/// Spatial point representation
#[derive(Debug, Clone, Copy)]
pub struct SpatialPoint {
    /// X coordinate (longitude or easting)
    pub x: f64,
    /// Y coordinate (latitude or northing)
    pub y: f64,
    /// Optional Z coordinate (elevation)
    pub z: Option<f64>,
}

impl SpatialPoint {
    pub fn new(x: f64, y: f64) -> Self {
        Self { x, y, z: None }
    }

    pub fn new_3d(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z: Some(z) }
    }

    /// Calculate Euclidean distance to another point
    pub fn distance(&self, other: &SpatialPoint) -> f64 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;

        match (self.z, other.z) {
            (Some(z1), Some(z2)) => {
                let dz = z1 - z2;
                (dx * dx + dy * dy + dz * dz).sqrt()
            }
            _ => (dx * dx + dy * dy).sqrt(),
        }
    }

    /// Calculate Haversine distance (for geographic coordinates)
    pub fn haversine_distance(&self, other: &SpatialPoint) -> f64 {
        const EARTH_RADIUS_KM: f64 = 6371.0;

        let lat1 = self.y.to_radians();
        let lat2 = other.y.to_radians();
        let delta_lat = (other.y - self.y).to_radians();
        let delta_lon = (other.x - self.x).to_radians();

        let a = (delta_lat / 2.0).sin().powi(2)
            + lat1.cos() * lat2.cos() * (delta_lon / 2.0).sin().powi(2);
        let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());

        EARTH_RADIUS_KM * c
    }
}

/// Spatial bounding box
#[derive(Debug, Clone, Copy)]
pub struct BoundingBox {
    /// Minimum X coordinate
    pub min_x: f64,
    /// Maximum X coordinate
    pub max_x: f64,
    /// Minimum Y coordinate
    pub min_y: f64,
    /// Maximum Y coordinate
    pub max_y: f64,
}

impl BoundingBox {
    pub fn new(min_x: f64, max_x: f64, min_y: f64, max_y: f64) -> Self {
        Self {
            min_x,
            max_x,
            min_y,
            max_y,
        }
    }

    pub fn from_points(points: &[SpatialPoint]) -> Self {
        if points.is_empty() {
            return Self::new(0.0, 0.0, 0.0, 0.0);
        }

        let mut min_x = points[0].x;
        let mut max_x = points[0].x;
        let mut min_y = points[0].y;
        let mut max_y = points[0].y;

        for point in points.iter().skip(1) {
            min_x = min_x.min(point.x);
            max_x = max_x.max(point.x);
            min_y = min_y.min(point.y);
            max_y = max_y.max(point.y);
        }

        Self::new(min_x, max_x, min_y, max_y)
    }

    /// Check if point is inside bounding box
    pub fn contains(&self, point: &SpatialPoint) -> bool {
        point.x >= self.min_x
            && point.x <= self.max_x
            && point.y >= self.min_y
            && point.y <= self.max_y
    }

    /// Calculate area of bounding box
    pub fn area(&self) -> f64 {
        (self.max_x - self.min_x) * (self.max_y - self.min_y)
    }

    /// Calculate intersection with another bounding box
    pub fn intersection(&self, other: &BoundingBox) -> Option<BoundingBox> {
        let min_x = self.min_x.max(other.min_x);
        let max_x = self.max_x.min(other.max_x);
        let min_y = self.min_y.max(other.min_y);
        let max_y = self.max_y.min(other.max_y);

        if min_x <= max_x && min_y <= max_y {
            Some(BoundingBox::new(min_x, max_x, min_y, max_y))
        } else {
            None
        }
    }
}

/// Spatial split criteria for geographic data
#[derive(Debug, Clone)]
pub enum SpatialSplitCriterion {
    /// Standard split criteria applied to spatial features
    Standard(SplitCriterion),
    /// Geographic distance-based splitting
    GeographicDistance {
        distance_metric: DistanceMetric,
        distance_weight: f64,
    },
    /// Spatial autocorrelation-aware splitting
    SpatialAutocorrelation {
        lag_distance: f64,
        autocorr_weight: f64,
    },
    /// Geographically weighted splitting
    GeographicallyWeighted {
        bandwidth: f64,
        kernel: SpatialKernel,
    },
    /// Quadtree-based spatial splitting
    Quadtree {
        max_depth: usize,
        min_points_per_cell: usize,
    },
    /// Voronoi diagram-based splitting
    Voronoi {
        n_sites: usize,
        site_selection: VoronoiSiteSelection,
    },
    /// Elevation-aware splitting
    ElevationAware {
        elevation_weight: f64,
        slope_threshold: f64,
    },
}

/// Distance metrics for spatial calculations
#[derive(Debug, Clone, Copy)]
pub enum DistanceMetric {
    /// Euclidean distance
    Euclidean,
    /// Manhattan distance
    Manhattan,
    /// Haversine distance (for geographic coordinates)
    Haversine,
    /// Great circle distance
    GreatCircle,
    /// Geodesic distance
    Geodesic,
}

/// Spatial kernels for weighted calculations
#[derive(Debug, Clone, Copy)]
pub enum SpatialKernel {
    /// Gaussian kernel
    Gaussian,
    /// Exponential kernel
    Exponential,
    /// Linear kernel
    Linear,
    /// Uniform kernel
    Uniform,
    /// Tricube kernel
    Tricube,
}

/// Voronoi site selection strategies
#[derive(Debug, Clone, Copy)]
pub enum VoronoiSiteSelection {
    /// Random selection
    Random,
    /// K-means clustering
    KMeans,
    /// Farthest point sampling
    FarthestPoint,
    /// Grid-based selection
    Grid,
}

/// Spatial data structure for geographic data
#[derive(Debug, Clone)]
pub struct SpatialDataStructure {
    /// Spatial points
    pub points: Vec<SpatialPoint>,
    /// Feature values at each point
    pub features: Array2<f64>,
    /// Target values
    pub targets: Array1<f64>,
    /// Coordinate system
    pub coordinate_system: CoordinateSystem,
    /// Spatial index for fast queries
    pub spatial_index: Option<SpatialIndex>,
    /// Spatial metadata
    pub metadata: SpatialMetadata,
}

/// Spatial metadata
#[derive(Debug, Clone)]
pub struct SpatialMetadata {
    /// Bounding box of all points
    pub bounding_box: BoundingBox,
    /// Spatial resolution (if applicable)
    pub resolution: Option<f64>,
    /// Spatial clustering information
    pub clusters: Vec<SpatialCluster>,
    /// Elevation statistics
    pub elevation_stats: Option<ElevationStats>,
    /// Spatial autocorrelation statistics
    pub autocorrelation_stats: Option<AutocorrelationStats>,
}

/// Spatial cluster information
#[derive(Debug, Clone)]
pub struct SpatialCluster {
    /// Cluster ID
    pub id: usize,
    /// Center point
    pub center: SpatialPoint,
    /// Points in cluster
    pub point_indices: Vec<usize>,
    /// Cluster radius
    pub radius: f64,
}

/// Elevation statistics
#[derive(Debug, Clone)]
pub struct ElevationStats {
    /// Mean elevation
    pub mean: f64,
    /// Standard deviation of elevation
    pub std: f64,
    /// Minimum elevation
    pub min: f64,
    /// Maximum elevation
    pub max: f64,
    /// Slope distribution
    pub slope_distribution: Vec<f64>,
}

/// Spatial autocorrelation statistics
#[derive(Debug, Clone)]
pub struct AutocorrelationStats {
    /// Moran's I statistic
    pub morans_i: f64,
    /// Geary's C statistic
    pub gearys_c: f64,
    /// Local indicators of spatial association
    pub lisa: Vec<f64>,
    /// Spatial lag values
    pub spatial_lag: Vec<f64>,
}

/// Spatial index for fast spatial queries
#[derive(Debug, Clone)]
pub enum SpatialIndex {
    /// Quadtree index
    Quadtree(QuadTree),
    /// R-tree index
    RTree(RTree),
    /// Grid index
    Grid(GridIndex),
    /// KD-tree index
    KDTree(KDTree),
}

/// Quadtree spatial index
#[derive(Debug, Clone)]
pub struct QuadTree {
    /// Root node
    pub root: QuadTreeNode,
    /// Maximum depth
    pub max_depth: usize,
    /// Maximum points per leaf
    pub max_points_per_leaf: usize,
}

/// Quadtree node
#[derive(Debug, Clone)]
pub struct QuadTreeNode {
    /// Bounding box of this node
    pub bounds: BoundingBox,
    /// Points in this node (for leaf nodes)
    pub points: Vec<usize>,
    /// Child nodes (for internal nodes)
    pub children: Option<[Box<QuadTreeNode>; 4]>,
    /// Is this a leaf node?
    pub is_leaf: bool,
    /// Node depth
    pub depth: usize,
}

/// R-tree spatial index (simplified)
#[derive(Debug, Clone)]
pub struct RTree {
    /// Root node
    pub root: RTreeNode,
    /// Maximum entries per node
    pub max_entries: usize,
}

/// R-tree node (simplified)
#[derive(Debug, Clone)]
pub struct RTreeNode {
    /// Bounding box
    pub bounds: BoundingBox,
    /// Child entries
    pub entries: Vec<RTreeEntry>,
    /// Is leaf node
    pub is_leaf: bool,
}

/// R-tree entry
#[derive(Debug, Clone)]
pub struct RTreeEntry {
    /// Bounding box
    pub bounds: BoundingBox,
    /// Either point index (for leaf) or child node
    pub data: RTreeData,
}

/// R-tree data
#[derive(Debug, Clone)]
pub enum RTreeData {
    /// Point index (for leaf nodes)
    Point(usize),
    /// Child node (for internal nodes)
    Node(Box<RTreeNode>),
}

/// Grid-based spatial index
#[derive(Debug, Clone)]
pub struct GridIndex {
    /// Grid cells
    pub cells: Vec<Vec<GridCell>>,
    /// Grid resolution
    pub cell_size: f64,
    /// Grid bounds
    pub bounds: BoundingBox,
    /// Grid dimensions
    pub grid_width: usize,
    pub grid_height: usize,
}

/// Grid cell
#[derive(Debug, Clone)]
pub struct GridCell {
    /// Cell bounds
    pub bounds: BoundingBox,
    /// Points in this cell
    pub point_indices: Vec<usize>,
}

/// KD-tree spatial index (simplified)
#[derive(Debug, Clone)]
pub struct KDTree {
    /// Root node
    pub root: Option<KDTreeNode>,
    /// Dimensionality
    pub dimensions: usize,
}

/// KD-tree node
#[derive(Debug, Clone)]
pub struct KDTreeNode {
    /// Point index
    pub point_index: usize,
    /// Split dimension
    pub split_dim: usize,
    /// Left child
    pub left: Option<Box<KDTreeNode>>,
    /// Right child
    pub right: Option<Box<KDTreeNode>>,
}

impl SpatialDataStructure {
    pub fn new(
        points: Vec<SpatialPoint>,
        features: Array2<f64>,
        targets: Array1<f64>,
        coordinate_system: CoordinateSystem,
    ) -> Self {
        let bounding_box = BoundingBox::from_points(&points);

        Self {
            points,
            features,
            targets,
            coordinate_system,
            spatial_index: None,
            metadata: SpatialMetadata {
                bounding_box,
                resolution: None,
                clusters: Vec::new(),
                elevation_stats: None,
                autocorrelation_stats: None,
            },
        }
    }

    /// Build spatial index for fast queries
    pub fn build_spatial_index(&mut self, index_type: SpatialIndexType) -> Result<()> {
        match index_type {
            SpatialIndexType::Quadtree {
                max_depth,
                max_points_per_leaf,
            } => {
                let quadtree = self.build_quadtree(max_depth, max_points_per_leaf)?;
                self.spatial_index = Some(SpatialIndex::Quadtree(quadtree));
            }
            SpatialIndexType::Grid { cell_size } => {
                let grid = self.build_grid_index(cell_size)?;
                self.spatial_index = Some(SpatialIndex::Grid(grid));
            }
            SpatialIndexType::KDTree => {
                let kdtree = self.build_kdtree()?;
                self.spatial_index = Some(SpatialIndex::KDTree(kdtree));
            }
        }

        Ok(())
    }

    /// Build quadtree index
    fn build_quadtree(&self, max_depth: usize, max_points_per_leaf: usize) -> Result<QuadTree> {
        let point_indices: Vec<usize> = (0..self.points.len()).collect();
        let root = self.build_quadtree_node(
            &point_indices,
            self.metadata.bounding_box,
            0,
            max_depth,
            max_points_per_leaf,
        )?;

        Ok(QuadTree {
            root,
            max_depth,
            max_points_per_leaf,
        })
    }

    /// Build quadtree node recursively
    fn build_quadtree_node(
        &self,
        point_indices: &[usize],
        bounds: BoundingBox,
        depth: usize,
        max_depth: usize,
        max_points_per_leaf: usize,
    ) -> Result<QuadTreeNode> {
        if depth >= max_depth || point_indices.len() <= max_points_per_leaf {
            // Create leaf node
            return Ok(QuadTreeNode {
                bounds,
                points: point_indices.to_vec(),
                children: None,
                is_leaf: true,
                depth,
            });
        }

        // Split into quadrants
        let mid_x = (bounds.min_x + bounds.max_x) / 2.0;
        let mid_y = (bounds.min_y + bounds.max_y) / 2.0;

        let quadrants = [
            BoundingBox::new(bounds.min_x, mid_x, bounds.min_y, mid_y), // SW
            BoundingBox::new(mid_x, bounds.max_x, bounds.min_y, mid_y), // SE
            BoundingBox::new(bounds.min_x, mid_x, mid_y, bounds.max_y), // NW
            BoundingBox::new(mid_x, bounds.max_x, mid_y, bounds.max_y), // NE
        ];

        let mut children = Vec::new();

        for quadrant in &quadrants {
            let quadrant_points: Vec<usize> = point_indices
                .iter()
                .copied()
                .filter(|&idx| quadrant.contains(&self.points[idx]))
                .collect();

            let child = self.build_quadtree_node(
                &quadrant_points,
                *quadrant,
                depth + 1,
                max_depth,
                max_points_per_leaf,
            )?;

            children.push(Box::new(child));
        }

        if children.len() != 4 {
            return Err(SklearsError::InvalidInput(
                "Failed to create quadtree children".to_string(),
            ));
        }

        let children_array: [Box<QuadTreeNode>; 4] = [
            children.remove(0),
            children.remove(0),
            children.remove(0),
            children.remove(0),
        ];

        Ok(QuadTreeNode {
            bounds,
            points: Vec::new(),
            children: Some(children_array),
            is_leaf: false,
            depth,
        })
    }

    /// Build grid index
    fn build_grid_index(&self, cell_size: f64) -> Result<GridIndex> {
        let bounds = self.metadata.bounding_box;
        let grid_width = ((bounds.max_x - bounds.min_x) / cell_size).ceil() as usize;
        let grid_height = ((bounds.max_y - bounds.min_y) / cell_size).ceil() as usize;

        let mut cells = vec![
            vec![
                GridCell {
                    bounds: BoundingBox::new(0.0, 0.0, 0.0, 0.0),
                    point_indices: Vec::new(),
                };
                grid_height
            ];
            grid_width
        ];

        // Initialize cell bounds
        for i in 0..grid_width {
            for j in 0..grid_height {
                let min_x = bounds.min_x + i as f64 * cell_size;
                let max_x = min_x + cell_size;
                let min_y = bounds.min_y + j as f64 * cell_size;
                let max_y = min_y + cell_size;

                cells[i][j].bounds = BoundingBox::new(min_x, max_x, min_y, max_y);
            }
        }

        // Assign points to cells
        for (point_idx, point) in self.points.iter().enumerate() {
            let grid_x = ((point.x - bounds.min_x) / cell_size).floor() as usize;
            let grid_y = ((point.y - bounds.min_y) / cell_size).floor() as usize;

            if grid_x < grid_width && grid_y < grid_height {
                cells[grid_x][grid_y].point_indices.push(point_idx);
            }
        }

        Ok(GridIndex {
            cells,
            cell_size,
            bounds,
            grid_width,
            grid_height,
        })
    }

    /// Build KD-tree index
    fn build_kdtree(&self) -> Result<KDTree> {
        let mut point_indices: Vec<usize> = (0..self.points.len()).collect();
        let root = self.build_kdtree_node(&mut point_indices, 0, 2)?; // 2D for now

        Ok(KDTree {
            root,
            dimensions: 2,
        })
    }

    /// Build KD-tree node recursively
    fn build_kdtree_node(
        &self,
        point_indices: &mut [usize],
        depth: usize,
        dimensions: usize,
    ) -> Result<Option<KDTreeNode>> {
        if point_indices.is_empty() {
            return Ok(None);
        }

        if point_indices.len() == 1 {
            return Ok(Some(KDTreeNode {
                point_index: point_indices[0],
                split_dim: depth % dimensions,
                left: None,
                right: None,
            }));
        }

        let split_dim = depth % dimensions;

        // Sort points by the split dimension
        point_indices.sort_by(|&a, &b| {
            let coord_a = if split_dim == 0 {
                self.points[a].x
            } else {
                self.points[a].y
            };
            let coord_b = if split_dim == 0 {
                self.points[b].x
            } else {
                self.points[b].y
            };
            coord_a.partial_cmp(&coord_b).expect("operation should succeed")
        });

        let median = point_indices.len() / 2;
        let median_index = point_indices[median];

        let left_child =
            self.build_kdtree_node(&mut point_indices[..median], depth + 1, dimensions)?;
        let right_child =
            self.build_kdtree_node(&mut point_indices[median + 1..], depth + 1, dimensions)?;

        Ok(Some(KDTreeNode {
            point_index: median_index,
            split_dim,
            left: left_child.map(Box::new),
            right: right_child.map(Box::new),
        }))
    }

    /// Calculate spatial autocorrelation
    pub fn calculate_spatial_autocorrelation(&mut self, lag_distance: f64) -> Result<()> {
        let n = self.points.len();
        let mut weights = Array2::zeros((n, n));

        // Create spatial weights matrix
        for i in 0..n {
            for j in 0..n {
                if i != j {
                    let distance = match self.coordinate_system {
                        CoordinateSystem::Geographic => {
                            self.points[i].haversine_distance(&self.points[j])
                        }
                        _ => self.points[i].distance(&self.points[j]),
                    };

                    if distance <= lag_distance {
                        weights[[i, j]] = 1.0 / distance.max(1e-8);
                    }
                }
            }
        }

        // Normalize weights
        for i in 0..n {
            let row_sum: f64 = weights.row(i).sum();
            if row_sum > 0.0 {
                for j in 0..n {
                    weights[[i, j]] /= row_sum;
                }
            }
        }

        // Calculate Moran's I
        let y_mean = self.targets.mean().unwrap_or(0.0);
        let mut numerator = 0.0;
        let mut denominator = 0.0;
        let mut w_sum = 0.0;

        for i in 0..n {
            for j in 0..n {
                let yi = self.targets[i] - y_mean;
                let yj = self.targets[j] - y_mean;
                numerator += weights[[i, j]] * yi * yj;
                w_sum += weights[[i, j]];
            }
            denominator += (self.targets[i] - y_mean).powi(2);
        }

        let morans_i = if denominator > 0.0 && w_sum > 0.0 {
            (n as f64 / w_sum) * (numerator / denominator)
        } else {
            0.0
        };

        // Calculate Geary's C (simplified)
        let mut geary_numerator = 0.0;
        for i in 0..n {
            for j in 0..n {
                geary_numerator += weights[[i, j]] * (self.targets[i] - self.targets[j]).powi(2);
            }
        }

        let gearys_c = if denominator > 0.0 && w_sum > 0.0 {
            ((n - 1) as f64 / (2.0 * w_sum)) * (geary_numerator / denominator)
        } else {
            1.0
        };

        // Calculate spatial lag
        let mut spatial_lag = Vec::with_capacity(n);
        for i in 0..n {
            let mut lag_value = 0.0;
            for j in 0..n {
                lag_value += weights[[i, j]] * self.targets[j];
            }
            spatial_lag.push(lag_value);
        }

        // Calculate LISA (Local Indicators of Spatial Association) - simplified
        let lisa: Vec<f64> = (0..n)
            .map(|i| {
                let yi = self.targets[i] - y_mean;
                let mut lisa_value = 0.0;
                for j in 0..n {
                    let yj = self.targets[j] - y_mean;
                    lisa_value += weights[[i, j]] * yj;
                }
                yi * lisa_value
            })
            .collect();

        self.metadata.autocorrelation_stats = Some(AutocorrelationStats {
            morans_i,
            gearys_c,
            lisa,
            spatial_lag,
        });

        Ok(())
    }

    /// Perform spatial clustering
    pub fn spatial_clustering(
        &mut self,
        n_clusters: usize,
        method: SpatialClusteringMethod,
    ) -> Result<()> {
        match method {
            SpatialClusteringMethod::KMeans => self.kmeans_clustering(n_clusters),
            SpatialClusteringMethod::DBSCAN { eps, min_points } => {
                self.dbscan_clustering(eps, min_points)
            }
            SpatialClusteringMethod::Hierarchical => self.hierarchical_clustering(n_clusters),
        }
    }

    /// K-means spatial clustering
    fn kmeans_clustering(&mut self, n_clusters: usize) -> Result<()> {
        // Simple K-means implementation
        let mut rng = scirs2_core::random::thread_rng();

        // Initialize centroids randomly
        let mut centroids: Vec<SpatialPoint> = Vec::new();
        for _ in 0..n_clusters {
            if !self.points.is_empty() {
                let idx = rng.gen_range(0..self.points.len());
                centroids.push(self.points[idx].clone());
            }
        }

        let mut clusters = vec![
            SpatialCluster {
                id: 0,
                center: SpatialPoint::new(0.0, 0.0),
                point_indices: Vec::new(),
                radius: 0.0,
            };
            n_clusters
        ];

        for iteration in 0..100 {
            // Max iterations
            // Assign points to nearest centroid
            for cluster in &mut clusters {
                cluster.point_indices.clear();
            }

            for (point_idx, point) in self.points.iter().enumerate() {
                let mut min_distance = f64::INFINITY;
                let mut closest_cluster = 0;

                for (cluster_idx, centroid) in centroids.iter().enumerate() {
                    let distance = point.distance(centroid);
                    if distance < min_distance {
                        min_distance = distance;
                        closest_cluster = cluster_idx;
                    }
                }

                clusters[closest_cluster].point_indices.push(point_idx);
            }

            // Update centroids
            let mut converged = true;
            for (cluster_idx, cluster) in clusters.iter_mut().enumerate() {
                if cluster.point_indices.is_empty() {
                    continue;
                }

                let mut sum_x = 0.0;
                let mut sum_y = 0.0;
                for &point_idx in &cluster.point_indices {
                    sum_x += self.points[point_idx].x;
                    sum_y += self.points[point_idx].y;
                }

                let new_centroid = SpatialPoint::new(
                    sum_x / cluster.point_indices.len() as f64,
                    sum_y / cluster.point_indices.len() as f64,
                );

                if centroids[cluster_idx].distance(&new_centroid) > 1e-6 {
                    converged = false;
                }

                centroids[cluster_idx] = new_centroid;
                cluster.center = new_centroid;
                cluster.id = cluster_idx;

                // Calculate cluster radius
                let mut max_distance: f64 = 0.0;
                for &point_idx in &cluster.point_indices {
                    let distance = self.points[point_idx].distance(&cluster.center);
                    max_distance = max_distance.max(distance);
                }
                cluster.radius = max_distance;
            }

            if converged {
                break;
            }
        }

        self.metadata.clusters = clusters;
        Ok(())
    }

    /// DBSCAN spatial clustering
    fn dbscan_clustering(&mut self, eps: f64, min_points: usize) -> Result<()> {
        let n = self.points.len();
        let mut cluster_labels = vec![-1i32; n]; // -1 means unassigned
        let mut cluster_id = 0i32;

        for i in 0..n {
            if cluster_labels[i] != -1 {
                continue; // Already processed
            }

            // Find neighbors
            let neighbors = self.find_neighbors(i, eps);

            if neighbors.len() < min_points {
                cluster_labels[i] = -2; // Mark as noise
                continue;
            }

            // Start new cluster
            cluster_labels[i] = cluster_id;
            let mut seed_set = VecDeque::from(neighbors);

            while let Some(neighbor) = seed_set.pop_front() {
                if cluster_labels[neighbor] == -2 {
                    cluster_labels[neighbor] = cluster_id; // Change noise to border point
                } else if cluster_labels[neighbor] == -1 {
                    cluster_labels[neighbor] = cluster_id;
                    let neighbor_neighbors = self.find_neighbors(neighbor, eps);
                    if neighbor_neighbors.len() >= min_points {
                        seed_set.extend(neighbor_neighbors);
                    }
                }
            }

            cluster_id += 1;
        }

        // Convert to cluster structure
        let mut clusters = HashMap::new();
        for (point_idx, &label) in cluster_labels.iter().enumerate() {
            if label >= 0 {
                clusters
                    .entry(label as usize)
                    .or_insert_with(Vec::new)
                    .push(point_idx);
            }
        }

        self.metadata.clusters = clusters
            .into_iter()
            .map(|(id, point_indices)| {
                // Calculate cluster center
                let mut sum_x = 0.0;
                let mut sum_y = 0.0;
                for &point_idx in &point_indices {
                    sum_x += self.points[point_idx].x;
                    sum_y += self.points[point_idx].y;
                }
                let center = SpatialPoint::new(
                    sum_x / point_indices.len() as f64,
                    sum_y / point_indices.len() as f64,
                );

                // Calculate radius
                let mut max_distance: f64 = 0.0;
                for &point_idx in &point_indices {
                    let distance = self.points[point_idx].distance(&center);
                    max_distance = max_distance.max(distance);
                }

                SpatialCluster {
                    id,
                    center,
                    point_indices,
                    radius: max_distance,
                }
            })
            .collect();

        Ok(())
    }

    /// Find neighbors within distance
    fn find_neighbors(&self, point_idx: usize, eps: f64) -> Vec<usize> {
        let mut neighbors = Vec::new();
        let query_point = &self.points[point_idx];

        for (i, point) in self.points.iter().enumerate() {
            if i != point_idx && query_point.distance(point) <= eps {
                neighbors.push(i);
            }
        }

        neighbors
    }

    /// Hierarchical spatial clustering (simplified)
    fn hierarchical_clustering(&mut self, n_clusters: usize) -> Result<()> {
        // This is a placeholder for hierarchical clustering
        // In practice, you'd implement a proper hierarchical clustering algorithm
        self.kmeans_clustering(n_clusters)
    }
}

/// Spatial index types
#[derive(Debug, Clone)]
pub enum SpatialIndexType {
    /// Quadtree index
    Quadtree {
        max_depth: usize,
        max_points_per_leaf: usize,
    },
    /// Grid-based index
    Grid { cell_size: f64 },
    /// KD-tree index
    KDTree,
}

/// Spatial clustering methods
#[derive(Debug, Clone)]
pub enum SpatialClusteringMethod {
    /// K-means clustering
    KMeans,
    /// DBSCAN clustering
    DBSCAN { eps: f64, min_points: usize },
    /// Hierarchical clustering
    Hierarchical,
}

/// Spatial Decision Tree for geographic data
pub struct SpatialDecisionTree<State = Untrained> {
    /// Base tree configuration
    config: DecisionTreeConfig,
    /// Spatial-specific configuration
    spatial_config: SpatialTreeConfig,
    /// Spatial data structure
    spatial_data: Option<SpatialDataStructure>,
    /// Trained model
    model: Option<Box<dyn SpatialTreeModel>>,
    /// State marker
    state: PhantomData<State>,
}

/// Configuration for spatial decision trees
#[derive(Debug, Clone)]
pub struct SpatialTreeConfig {
    /// Spatial split criterion
    pub split_criterion: SpatialSplitCriterion,
    /// Coordinate system
    pub coordinate_system: CoordinateSystem,
    /// Enable spatial autocorrelation consideration
    pub consider_autocorrelation: bool,
    /// Spatial regularization strength
    pub spatial_regularization: f64,
    /// Minimum spatial cluster size
    pub min_spatial_cluster_size: usize,
}

impl Default for SpatialTreeConfig {
    fn default() -> Self {
        Self {
            split_criterion: SpatialSplitCriterion::Standard(SplitCriterion::MSE),
            coordinate_system: CoordinateSystem::Geographic,
            consider_autocorrelation: false,
            spatial_regularization: 0.01,
            min_spatial_cluster_size: 5,
        }
    }
}

/// Trait for spatial tree models
pub trait SpatialTreeModel: Send + Sync {
    /// Predict at spatial locations
    fn predict(&self, points: &[SpatialPoint], features: &Array2<f64>) -> Result<Array1<f64>>;

    /// Update model with new spatial data
    fn update(&mut self, spatial_data: &SpatialDataStructure) -> Result<()>;

    /// Get spatial feature importance
    fn get_spatial_importance(&self) -> Result<HashMap<String, f64>>;
}