arrow-graph 0.6.0

Arrow-native graph processing engine with SQL interface
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
use crate::error::Result;
use crate::graph::ArrowGraph;
use crate::ml::embeddings::{NodeEmbeddings, EmbeddingModel};
use crate::ml::features::{FeatureExtractor, MLFeatureSet};
use arrow::array::Array;
use std::collections::HashMap;
use serde::{Serialize, Deserialize};

/// Bridge for PyTorch Geometric integration
/// Provides seamless conversion between Arrow-Graph and PyTorch Geometric formats
#[derive(Debug)]
pub struct PyTorchGeometricBridge {
    graph: Option<ArrowGraph>,
    node_features: Option<Vec<Vec<f64>>>,
    edge_index: Option<Vec<(usize, usize)>>,
    edge_attr: Option<Vec<Vec<f64>>>,
    node_mapping: HashMap<String, usize>,
    reverse_mapping: HashMap<usize, String>,
}

/// Graph Neural Network processor for running GNN models
#[derive(Debug)]
pub struct GNNProcessor {
    model_config: GNNConfig,
    bridge: PyTorchGeometricBridge,
    trained_embeddings: Option<NodeEmbeddings>,
}

/// Configuration for GNN models
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GNNConfig {
    pub model_type: GNNModelType,
    pub hidden_channels: usize,
    pub num_layers: usize,
    pub dropout: f64,
    pub learning_rate: f64,
    pub epochs: usize,
    pub batch_size: usize,
    pub task_type: TaskType,
}

/// Supported GNN model types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GNNModelType {
    GCN,        // Graph Convolutional Network
    GraphSAGE,  // GraphSAGE
    GAT,        // Graph Attention Network
    GIN,        // Graph Isomorphism Network
    Custom(String), // Custom model specification
}

/// ML task types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TaskType {
    NodeClassification,
    EdgePrediction,
    GraphClassification,
    NodeRegression,
    Embedding,
}

/// Model export formats
#[derive(Debug, Clone)]
pub struct ModelExport {
    pub format: ExportFormat,
    pub model_data: Vec<u8>,
    pub metadata: ModelMetadata,
}

/// Supported export formats
#[derive(Debug, Clone)]
pub enum ExportFormat {
    PyTorchJIT,
    ONNX,
    TensorFlow,
    JSON,
    Custom(String),
}

/// Model metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelMetadata {
    pub model_type: String,
    pub input_dim: usize,
    pub output_dim: usize,
    pub num_parameters: usize,
    pub training_accuracy: Option<f64>,
    pub validation_accuracy: Option<f64>,
    pub created_at: String,
    pub framework_version: String,
}

impl PyTorchGeometricBridge {
    pub fn new() -> Self {
        Self {
            graph: None,
            node_features: None,
            edge_index: None,
            edge_attr: None,
            node_mapping: HashMap::new(),
            reverse_mapping: HashMap::new(),
        }
    }

    /// Initialize with Arrow Graph
    pub fn initialize(&mut self, graph: ArrowGraph) -> Result<()> {
        self.graph = Some(graph);
        self.build_node_mapping()?;
        self.extract_edge_index()?;
        Ok(())
    }

    /// Build mapping between string node IDs and integer indices
    fn build_node_mapping(&mut self) -> Result<()> {
        let graph = self.graph.as_ref()
            .ok_or_else(|| crate::error::GraphError::graph_construction("Graph not initialized"))?;

        let nodes_batch = &graph.nodes;
        if nodes_batch.num_rows() > 0 {
            let node_ids = nodes_batch.column(0)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .ok_or_else(|| crate::error::GraphError::graph_construction("Expected string array for node IDs"))?;

            for (i, node_id) in (0..node_ids.len()).enumerate() {
                let id = node_ids.value(i).to_string();
                self.node_mapping.insert(id.clone(), i);
                self.reverse_mapping.insert(i, id);
            }
        }

        Ok(())
    }

    /// Extract edge index in PyTorch Geometric format
    fn extract_edge_index(&mut self) -> Result<()> {
        let graph = self.graph.as_ref()
            .ok_or_else(|| crate::error::GraphError::graph_construction("Graph not initialized"))?;

        let edges_batch = &graph.edges;
        let mut edge_index = Vec::new();
        let mut edge_attr = Vec::new();

        if edges_batch.num_rows() > 0 {
            let source_ids = edges_batch.column(0)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .ok_or_else(|| crate::error::GraphError::graph_construction("Expected string array for source IDs"))?;
            let target_ids = edges_batch.column(1)
                .as_any()
                .downcast_ref::<arrow::array::StringArray>()
                .ok_or_else(|| crate::error::GraphError::graph_construction("Expected string array for target IDs"))?;
            let weights = edges_batch.column(2)
                .as_any()
                .downcast_ref::<arrow::array::Float64Array>()
                .ok_or_else(|| crate::error::GraphError::graph_construction("Expected float64 array for weights"))?;

            for i in 0..source_ids.len() {
                let source_str = source_ids.value(i);
                let target_str = target_ids.value(i);
                let weight = weights.value(i);

                if let (Some(&source_idx), Some(&target_idx)) = 
                    (self.node_mapping.get(source_str), self.node_mapping.get(target_str)) {
                    edge_index.push((source_idx, target_idx));
                    edge_attr.push(vec![weight]);

                    // Add reverse edge for undirected graphs
                    edge_index.push((target_idx, source_idx));
                    edge_attr.push(vec![weight]);
                }
            }
        }

        self.edge_index = Some(edge_index);
        self.edge_attr = Some(edge_attr);
        Ok(())
    }

    /// Set node features from feature extractor
    pub fn set_node_features(&mut self, feature_set: &MLFeatureSet) -> Result<()> {
        let mut node_features = vec![vec![0.0; feature_set.node_feature_names.len()]; self.node_mapping.len()];

        for (i, node_id) in feature_set.node_ids.iter().enumerate() {
            if let Some(&node_idx) = self.node_mapping.get(node_id) {
                if i < feature_set.node_features_matrix.len() {
                    node_features[node_idx] = feature_set.node_features_matrix[i].clone();
                }
            }
        }

        self.node_features = Some(node_features);
        Ok(())
    }

    /// Export to PyTorch Geometric format (JSON representation)
    pub fn to_pyg_format(&self) -> Result<PyGData> {
        let edge_index = self.edge_index.as_ref()
            .ok_or_else(|| crate::error::GraphError::graph_construction("Edge index not built"))?;

        let mut source_nodes = Vec::new();
        let mut target_nodes = Vec::new();

        for &(source, target) in edge_index {
            source_nodes.push(source);
            target_nodes.push(target);
        }

        Ok(PyGData {
            edge_index: vec![source_nodes, target_nodes],
            node_features: self.node_features.clone(),
            edge_attr: self.edge_attr.clone(),
            num_nodes: self.node_mapping.len(),
            num_edges: edge_index.len(),
            node_mapping: self.node_mapping.clone(),
        })
    }

    /// Create from PyTorch Geometric data
    pub fn from_pyg_format(&mut self, pyg_data: PyGData) -> Result<()> {
        // Reconstruct edge index
        if pyg_data.edge_index.len() >= 2 {
            let source_nodes = &pyg_data.edge_index[0];
            let target_nodes = &pyg_data.edge_index[1];
            
            let mut edge_index = Vec::new();
            for (i, &source) in source_nodes.iter().enumerate() {
                if i < target_nodes.len() {
                    edge_index.push((source, target_nodes[i]));
                }
            }
            self.edge_index = Some(edge_index);
        }

        self.node_features = pyg_data.node_features;
        self.edge_attr = pyg_data.edge_attr;
        self.node_mapping = pyg_data.node_mapping;

        // Rebuild reverse mapping
        self.reverse_mapping.clear();
        for (node_id, &idx) in &self.node_mapping {
            self.reverse_mapping.insert(idx, node_id.clone());
        }

        Ok(())
    }

    /// Get node features in tensor-ready format
    pub fn get_node_features_tensor(&self) -> Option<Vec<Vec<f64>>> {
        self.node_features.clone()
    }

    /// Get edge index in tensor format
    pub fn get_edge_index_tensor(&self) -> Option<Vec<Vec<usize>>> {
        self.edge_index.as_ref().map(|edges| {
            let mut sources = Vec::new();
            let mut targets = Vec::new();
            
            for &(source, target) in edges {
                sources.push(source);
                targets.push(target);
            }
            
            vec![sources, targets]
        })
    }

    /// Get node mapping
    pub fn get_node_mapping(&self) -> &HashMap<String, usize> {
        &self.node_mapping
    }

    /// Get reverse node mapping
    pub fn get_reverse_mapping(&self) -> &HashMap<usize, String> {
        &self.reverse_mapping
    }
}

/// PyTorch Geometric data structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PyGData {
    pub edge_index: Vec<Vec<usize>>,
    pub node_features: Option<Vec<Vec<f64>>>,
    pub edge_attr: Option<Vec<Vec<f64>>>,
    pub num_nodes: usize,
    pub num_edges: usize,
    pub node_mapping: HashMap<String, usize>,
}

impl GNNProcessor {
    pub fn new(config: GNNConfig) -> Self {
        Self {
            model_config: config,
            bridge: PyTorchGeometricBridge::new(),
            trained_embeddings: None,
        }
    }

    /// Initialize with graph and features
    pub fn initialize(&mut self, graph: ArrowGraph, feature_set: Option<MLFeatureSet>) -> Result<()> {
        self.bridge.initialize(graph)?;
        
        if let Some(features) = feature_set {
            self.bridge.set_node_features(&features)?;
        }

        Ok(())
    }

    /// Train GNN model (simplified simulation)
    pub fn train_model(&mut self) -> Result<TrainingResult> {
        // This is a simplified simulation of GNN training
        // In a real implementation, this would interface with PyTorch

        let num_nodes = self.bridge.node_mapping.len();
        let hidden_dim = self.model_config.hidden_channels;

        // Simulate training process
        let mut training_losses = Vec::new();
        let mut validation_accuracies = Vec::new();

        for epoch in 0..self.model_config.epochs {
            // Simulate training step
            let loss = 1.0 / (epoch + 1) as f64; // Decreasing loss
            training_losses.push(loss);

            // Simulate validation
            let accuracy = 0.5 + 0.4 * (1.0 - (-epoch as f64 / 100.0).exp());
            validation_accuracies.push(accuracy);
        }

        // Generate mock embeddings
        let mut embeddings = HashMap::new();
        let mut rng = rand::thread_rng();
        use rand::Rng;

        for (node_id, _) in &self.bridge.node_mapping {
            let embedding: Vec<f64> = (0..hidden_dim)
                .map(|_| rng.gen::<f64>() - 0.5)
                .collect();
            embeddings.insert(node_id.clone(), embedding);
        }

        self.trained_embeddings = Some(NodeEmbeddings {
            embeddings,
            dimension: hidden_dim,
        });

        Ok(TrainingResult {
            final_loss: training_losses.last().copied().unwrap_or(1.0),
            final_accuracy: validation_accuracies.last().copied().unwrap_or(0.5),
            training_losses,
            validation_accuracies,
            num_epochs: self.model_config.epochs,
            model_type: self.model_config.model_type.clone(),
        })
    }

    /// Get trained embeddings
    pub fn get_embeddings(&self) -> Option<&NodeEmbeddings> {
        self.trained_embeddings.as_ref()
    }

    /// Predict on new data
    pub fn predict(&self, node_features: Vec<Vec<f64>>) -> Result<Vec<f64>> {
        // Simplified prediction simulation
        let output_dim = match self.model_config.task_type {
            TaskType::NodeClassification => 5, // 5 classes
            TaskType::NodeRegression => 1,
            TaskType::Embedding => self.model_config.hidden_channels,
            _ => 1,
        };

        let mut predictions = Vec::new();
        let mut rng = rand::thread_rng();
        use rand::Rng;

        for _ in 0..node_features.len() {
            let prediction: Vec<f64> = (0..output_dim)
                .map(|_| rng.gen::<f64>())
                .collect();
            predictions.extend(prediction);
        }

        Ok(predictions)
    }

    /// Export model in specified format
    pub fn export_model(&self, format: ExportFormat) -> Result<ModelExport> {
        let metadata = ModelMetadata {
            model_type: format!("{:?}", self.model_config.model_type),
            input_dim: self.bridge.node_features.as_ref()
                .and_then(|features| features.first().map(|f| f.len()))
                .unwrap_or(0),
            output_dim: self.model_config.hidden_channels,
            num_parameters: self.estimate_num_parameters(),
            training_accuracy: None, // Would be filled from training results
            validation_accuracy: None,
            created_at: chrono::Utc::now().to_rfc3339(),
            framework_version: "arrow-graph-0.5.0".to_string(),
        };

        let model_data = match format {
            ExportFormat::JSON => {
                let pyg_data = self.bridge.to_pyg_format()?;
                serde_json::to_vec(&pyg_data)
                    .map_err(|e| crate::error::GraphError::graph_construction(&format!("JSON serialization error: {}", e)))?
            }
            ExportFormat::PyTorchJIT => {
                // Placeholder for PyTorch JIT export
                b"# PyTorch JIT model placeholder".to_vec()
            }
            ExportFormat::ONNX => {
                // Placeholder for ONNX export
                b"# ONNX model placeholder".to_vec()
            }
            ExportFormat::TensorFlow => {
                // Placeholder for TensorFlow export
                b"# TensorFlow model placeholder".to_vec()
            }
            ExportFormat::Custom(name) => {
                format!("# Custom export for {}", name).into_bytes()
            }
        };

        Ok(ModelExport {
            format,
            model_data,
            metadata,
        })
    }

    /// Get PyTorch Geometric data
    pub fn get_pyg_data(&self) -> Result<PyGData> {
        self.bridge.to_pyg_format()
    }

    /// Create GNN processor from embeddings
    pub fn from_embeddings(embeddings: NodeEmbeddings, config: GNNConfig) -> Self {
        let mut processor = Self::new(config);
        processor.trained_embeddings = Some(embeddings);
        processor
    }

    fn estimate_num_parameters(&self) -> usize {
        // Simplified parameter count estimation
        let input_dim = self.bridge.node_features.as_ref()
            .and_then(|features| features.first().map(|f| f.len()))
            .unwrap_or(64);
        
        let hidden_dim = self.model_config.hidden_channels;
        let num_layers = self.model_config.num_layers;

        // Rough estimate for typical GNN architectures
        let layer_params = input_dim * hidden_dim + hidden_dim * hidden_dim * (num_layers - 1);
        layer_params + hidden_dim // Add bias terms
    }
}

/// Training result information
#[derive(Debug, Clone)]
pub struct TrainingResult {
    pub final_loss: f64,
    pub final_accuracy: f64,
    pub training_losses: Vec<f64>,
    pub validation_accuracies: Vec<f64>,
    pub num_epochs: usize,
    pub model_type: GNNModelType,
}

impl Default for GNNConfig {
    fn default() -> Self {
        Self {
            model_type: GNNModelType::GCN,
            hidden_channels: 64,
            num_layers: 2,
            dropout: 0.5,
            learning_rate: 0.01,
            epochs: 100,
            batch_size: 32,
            task_type: TaskType::NodeClassification,
        }
    }
}

/// Helper function to create PyTorch Geometric bridge from Arrow Graph
pub fn create_pyg_bridge(graph: ArrowGraph) -> Result<PyTorchGeometricBridge> {
    let mut bridge = PyTorchGeometricBridge::new();
    bridge.initialize(graph)?;
    Ok(bridge)
}

/// Helper function to create GNN processor with default config
pub fn create_gnn_processor(
    graph: ArrowGraph, 
    task_type: TaskType
) -> Result<GNNProcessor> {
    let config = GNNConfig {
        task_type,
        ..Default::default()
    };
    
    let mut processor = GNNProcessor::new(config);
    processor.initialize(graph, None)?;
    Ok(processor)
}

/// Integration with existing embedding models
pub fn gnn_from_node2vec<T: EmbeddingModel>(
    mut embedding_model: T,
    graph: &ArrowGraph,
    task_type: TaskType,
) -> Result<GNNProcessor> {
    // Generate embeddings first
    embedding_model.initialize(graph)?;
    let embeddings = embedding_model.generate_embeddings()?;
    
    // Create GNN config based on embedding dimension
    let config = GNNConfig {
        hidden_channels: embeddings.dimension,
        task_type,
        ..Default::default()
    };
    
    Ok(GNNProcessor::from_embeddings(embeddings, config))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::ArrowGraph;
    use crate::ml::features::FeatureExtractor;
    use arrow::array::{StringArray, Float64Array};
    use arrow::record_batch::RecordBatch;
    use arrow::datatypes::{Schema, Field, DataType};
    use std::sync::Arc;

    fn create_test_graph() -> Result<ArrowGraph> {
        let nodes_schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
        ]));
        let node_ids = StringArray::from(vec!["A", "B", "C", "D"]);
        let nodes_batch = RecordBatch::try_new(
            nodes_schema,
            vec![Arc::new(node_ids)],
        )?;

        let edges_schema = Arc::new(Schema::new(vec![
            Field::new("source", DataType::Utf8, false),
            Field::new("target", DataType::Utf8, false),
            Field::new("weight", DataType::Float64, false),
        ]));
        let sources = StringArray::from(vec!["A", "B", "C"]);
        let targets = StringArray::from(vec!["B", "C", "D"]);
        let weights = Float64Array::from(vec![1.0, 1.0, 1.0]);
        let edges_batch = RecordBatch::try_new(
            edges_schema,
            vec![Arc::new(sources), Arc::new(targets), Arc::new(weights)],
        )?;

        ArrowGraph::new(nodes_batch, edges_batch)
    }

    #[test]
    fn test_pyg_bridge_initialization() {
        let graph = create_test_graph().unwrap();
        let mut bridge = PyTorchGeometricBridge::new();
        
        bridge.initialize(graph).unwrap();
        
        assert_eq!(bridge.node_mapping.len(), 4);
        assert_eq!(bridge.reverse_mapping.len(), 4);
        assert!(bridge.edge_index.is_some());
    }

    #[test]
    fn test_pyg_data_conversion() {
        let graph = create_test_graph().unwrap();
        let mut bridge = PyTorchGeometricBridge::new();
        
        bridge.initialize(graph).unwrap();
        let pyg_data = bridge.to_pyg_format().unwrap();
        
        assert_eq!(pyg_data.num_nodes, 4);
        assert!(pyg_data.num_edges > 0);
        assert_eq!(pyg_data.edge_index.len(), 2); // [sources, targets]
    }

    #[test]
    fn test_gnn_processor_initialization() {
        let graph = create_test_graph().unwrap();
        let config = GNNConfig::default();
        let mut processor = GNNProcessor::new(config);
        
        processor.initialize(graph, None).unwrap();
        
        assert_eq!(processor.bridge.node_mapping.len(), 4);
    }

    #[test]
    fn test_gnn_with_features() {
        let graph = create_test_graph().unwrap();
        let mut extractor = FeatureExtractor::default();
        
        extractor.initialize(graph.clone()).unwrap();
        let features = extractor.extract_features().unwrap();
        
        let config = GNNConfig::default();
        let mut processor = GNNProcessor::new(config);
        processor.initialize(graph, Some(features)).unwrap();
        
        assert!(processor.bridge.node_features.is_some());
    }

    #[test]
    fn test_gnn_training_simulation() {
        let graph = create_test_graph().unwrap();
        let mut processor = create_gnn_processor(graph, TaskType::NodeClassification).unwrap();
        
        let result = processor.train_model().unwrap();
        
        assert_eq!(result.num_epochs, 100);
        assert!(!result.training_losses.is_empty());
        assert!(!result.validation_accuracies.is_empty());
        assert!(processor.get_embeddings().is_some());
    }

    #[test]
    fn test_model_export() {
        let graph = create_test_graph().unwrap();
        let mut processor = create_gnn_processor(graph, TaskType::Embedding).unwrap();
        
        processor.train_model().unwrap();
        let export = processor.export_model(ExportFormat::JSON).unwrap();
        
        assert!(!export.model_data.is_empty());
        assert!(export.metadata.num_parameters > 0);
        assert_eq!(export.metadata.model_type, "GCN");
    }

    #[test]
    fn test_prediction() {
        let graph = create_test_graph().unwrap();
        let mut processor = create_gnn_processor(graph, TaskType::NodeRegression).unwrap();
        
        processor.train_model().unwrap();
        
        let node_features = vec![
            vec![1.0, 2.0, 3.0],
            vec![4.0, 5.0, 6.0],
        ];
        
        let predictions = processor.predict(node_features).unwrap();
        assert!(!predictions.is_empty());
    }

    #[test]
    fn test_different_model_types() {
        let graph = create_test_graph().unwrap();
        
        for model_type in [GNNModelType::GCN, GNNModelType::GraphSAGE, GNNModelType::GAT] {
            let config = GNNConfig {
                model_type: model_type.clone(),
                ..Default::default()
            };
            
            let mut processor = GNNProcessor::new(config);
            processor.initialize(graph.clone(), None).unwrap();
            
            let result = processor.train_model().unwrap();
            assert_eq!(result.model_type, model_type);
        }
    }

    #[test]
    fn test_different_task_types() {
        let graph = create_test_graph().unwrap();
        
        for task_type in [TaskType::NodeClassification, TaskType::EdgePrediction, TaskType::GraphClassification] {
            let processor = create_gnn_processor(graph.clone(), task_type.clone()).unwrap();
            assert_eq!(processor.model_config.task_type, task_type);
        }
    }

    #[test]
    fn test_node_mapping_consistency() {
        let graph = create_test_graph().unwrap();
        let mut bridge = PyTorchGeometricBridge::new();
        
        bridge.initialize(graph).unwrap();
        
        // Check that forward and reverse mappings are consistent
        for (node_id, &idx) in &bridge.node_mapping {
            assert_eq!(bridge.reverse_mapping.get(&idx), Some(node_id));
        }
        
        for (&idx, node_id) in &bridge.reverse_mapping {
            assert_eq!(bridge.node_mapping.get(node_id), Some(&idx));
        }
    }

    #[test]
    fn test_edge_index_format() {
        let graph = create_test_graph().unwrap();
        let mut bridge = PyTorchGeometricBridge::new();
        
        bridge.initialize(graph).unwrap();
        let tensor_format = bridge.get_edge_index_tensor().unwrap();
        
        assert_eq!(tensor_format.len(), 2); // [sources, targets]
        assert_eq!(tensor_format[0].len(), tensor_format[1].len()); // Same length
        
        // Check that all indices are valid
        let num_nodes = bridge.node_mapping.len();
        for &source in &tensor_format[0] {
            assert!(source < num_nodes);
        }
        for &target in &tensor_format[1] {
            assert!(target < num_nodes);
        }
    }
}