lodviz_core 0.3.0

Core visualization primitives and data structures for lodviz
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
//! Data validation for chart rendering
//!
//! Provides centralized validation logic to ensure data compatibility
//! with chart encodings before rendering.

use crate::core::cardinality::{CardinalityConstraint, ValidationSeverity};
use crate::core::column_inference::{infer_column_types, InferenceConfig};
use crate::core::compatibility::MarkCompatibilityRules;
use crate::core::data::{DataType, FieldRole};
use crate::core::encoding::{Encoding, Field};
use crate::core::field_value::{DataTable, FieldValue};
use crate::core::mark::Mark;

/// Validation error types
#[derive(Debug, Clone, PartialEq)]
pub enum DataValidationError {
    /// Dataset is completely empty (no rows)
    EmptyDataset,
    /// Required columns are missing from the dataset
    MissingColumns(Vec<String>),
    /// Column exists but has incompatible type
    IncompatibleType {
        column: String,
        expected: String,
        found: String,
    },
    /// Column exists but has insufficient numeric data
    InsufficientNumericData {
        column: String,
        numeric_count: usize,
        total_rows: usize,
    },
    /// Field role mismatch (e.g., Measure on categorical axis)
    RoleMismatch {
        column: String,
        axis: String,
        expected_role: FieldRole,
        actual_role: FieldRole,
    },
    /// High cardinality warning/error
    HighCardinality {
        column: String,
        cardinality: usize,
        threshold: usize,
        suggestion: String,
    },
    /// Mark incompatible with data types
    MarkIncompatible {
        mark: Mark,
        column: String,
        axis: String,
        required_types: Vec<DataType>,
        actual_type: DataType,
    },
}

impl std::fmt::Display for DataValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EmptyDataset => write!(f, "Dataset is empty"),
            Self::MissingColumns(cols) => {
                write!(f, "Missing columns: {}", cols.join(", "))
            }
            Self::IncompatibleType {
                column,
                expected,
                found,
            } => {
                write!(
                    f,
                    "Column '{}' has incompatible type: expected {}, found {}",
                    column, expected, found
                )
            }
            Self::InsufficientNumericData {
                column,
                numeric_count,
                total_rows,
            } => {
                write!(
                    f,
                    "Column '{}' has insufficient numeric data: {}/{} rows ({:.1}%)",
                    column,
                    numeric_count,
                    total_rows,
                    (*numeric_count as f64 / *total_rows as f64) * 100.0
                )
            }
            Self::RoleMismatch {
                column,
                axis,
                expected_role,
                actual_role,
            } => {
                write!(
                    f,
                    "{} axis expects {:?} but column '{}' is classified as {:?}. Try using a {} column instead.",
                    axis,
                    expected_role,
                    column,
                    actual_role,
                    match expected_role {
                        FieldRole::Dimension => "categorical (text/date)",
                        FieldRole::Measure => "numeric",
                    }
                )
            }
            Self::HighCardinality {
                column,
                cardinality,
                threshold,
                suggestion,
            } => {
                write!(
                    f,
                    "Column '{}' has {} unique values (threshold: {}). {}",
                    column, cardinality, threshold, suggestion
                )
            }
            Self::MarkIncompatible {
                mark,
                column,
                axis,
                required_types,
                actual_type,
            } => {
                write!(
                    f,
                    "{:?} charts require {:?} for {} axis, but column '{}' has {:?}",
                    mark, required_types, axis, column, actual_type
                )
            }
        }
    }
}

/// Result of data validation
#[derive(Debug, Clone)]
pub struct ValidationResult {
    /// Whether the data is valid for rendering
    pub is_valid: bool,
    /// Critical errors that prevent rendering
    pub errors: Vec<DataValidationError>,
    /// Non-critical warnings (e.g., partial data loss)
    pub warnings: Vec<String>,
    /// Overall severity level
    pub severity: ValidationSeverity,
}

impl ValidationResult {
    /// Create a successful validation result
    pub fn ok() -> Self {
        Self {
            is_valid: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            severity: ValidationSeverity::Ok,
        }
    }

    /// Create a failed validation result with errors
    pub fn with_errors(errors: Vec<DataValidationError>) -> Self {
        Self {
            is_valid: false,
            errors,
            warnings: Vec::new(),
            severity: ValidationSeverity::Error,
        }
    }

    /// Add a warning to the validation result
    pub fn add_warning(&mut self, warning: String) {
        self.warnings.push(warning);
        if self.severity == ValidationSeverity::Ok {
            self.severity = ValidationSeverity::Warning;
        }
    }

    /// Add an error to the validation result
    pub fn add_error(&mut self, error: DataValidationError) {
        self.errors.push(error);
        self.is_valid = false;
        self.severity = ValidationSeverity::Error;
    }
}

impl DataTable {
    /// Validate that this DataTable is compatible with the given Encoding
    ///
    /// Checks:
    /// - Dataset is not empty
    /// - Required columns exist
    /// - Column types are compatible with field types
    /// - Sufficient numeric data for quantitative/temporal fields
    pub fn validate_for_encoding(&self, encoding: &Encoding) -> ValidationResult {
        let mut result = ValidationResult::ok();

        // Check 1: Dataset is not empty
        if self.rows().is_empty() {
            result.add_error(DataValidationError::EmptyDataset);
            return result; // No point in further validation
        }

        let total_rows = self.rows().len();

        // Collect all required field names from encoding
        let mut required_columns = Vec::new();
        required_columns.push(encoding.x.clone());
        required_columns.push(encoding.y.clone());
        if let Some(ref field) = encoding.color {
            required_columns.push(field.clone());
        }

        // Check 2: Required columns exist
        let mut missing_cols = Vec::new();
        for field in &required_columns {
            if !self.has_column(&field.name) {
                missing_cols.push(field.name.clone());
            }
        }
        if !missing_cols.is_empty() {
            result.add_error(DataValidationError::MissingColumns(missing_cols));
            return result; // Can't validate types if columns don't exist
        }

        // Check 3: Column type compatibility and numeric data sufficiency
        for field in &required_columns {
            self.validate_field(field, total_rows, &mut result);
        }

        result
    }

    /// Validate a single field against the data
    fn validate_field(&self, field: &Field, total_rows: usize, result: &mut ValidationResult) {
        let col_name = &field.name;

        // Count how many rows have valid data for this field
        let mut numeric_count = 0;
        let mut text_count = 0;
        let mut null_count = 0;
        let mut bool_count = 0;

        for row in self.rows() {
            if let Some(val) = row.get(col_name) {
                match val {
                    FieldValue::Numeric(_) | FieldValue::Timestamp(_) => numeric_count += 1,
                    FieldValue::Text(_) => text_count += 1,
                    FieldValue::Bool(_) => bool_count += 1,
                    FieldValue::Null => null_count += 1,
                }
            }
        }

        // Validate based on field type
        match &field.data_type {
            DataType::Quantitative | DataType::Temporal => {
                // Quantitative/Temporal fields require numeric data
                if numeric_count == 0 {
                    result.add_error(DataValidationError::IncompatibleType {
                        column: col_name.clone(),
                        expected: "numeric or timestamp".to_string(),
                        found: format!(
                            "{} text, {} bool, {} null",
                            text_count, bool_count, null_count
                        ),
                    });
                } else if numeric_count < total_rows {
                    // Some rows will be filtered out
                    let percent = (numeric_count as f64 / total_rows as f64) * 100.0;
                    if percent < 50.0 {
                        // Less than 50% numeric data is a critical error
                        result.add_error(DataValidationError::InsufficientNumericData {
                            column: col_name.clone(),
                            numeric_count,
                            total_rows,
                        });
                    } else {
                        // 50-99% is a warning
                        result.add_warning(format!(
                            "Column '{}': {}/{} rows ({:.1}%) will be filtered out (non-numeric data)",
                            col_name,
                            total_rows - numeric_count,
                            total_rows,
                            100.0 - percent
                        ));
                    }
                }
            }
            DataType::Nominal | DataType::Ordinal => {
                // Nominal/Ordinal fields accept any type, but warn if all null
                if null_count == total_rows {
                    result.add_warning(format!("Column '{}': all values are null", col_name));
                }
            }
        }
    }

    /// Check if a column exists in the dataset
    fn has_column(&self, col_name: &str) -> bool {
        // Check if any row has this column
        self.rows().iter().any(|row| row.contains_key(col_name))
    }

    /// Validate that this DataTable is compatible with the given Encoding and Mark type
    ///
    /// This is the enhanced validation method that includes:
    /// - All checks from `validate_for_encoding()`
    /// - Mark-specific compatibility rules (e.g., bar charts need categorical X)
    /// - Cardinality constraints (e.g., too many categories for a pie chart)
    ///
    /// # Examples
    /// ```ignore
    /// use lodviz_core::core::field_value::DataTable;
    /// use lodviz_core::core::encoding::{Encoding, Field};
    /// use lodviz_core::core::mark::Mark;
    ///
    /// let table = DataTable::from_csv("data.csv")?;
    /// let encoding = Encoding::new(Field::nominal("category"), Field::quantitative("value"));
    /// let result = table.validate_for_mark(&encoding, Mark::Bar);
    ///
    /// if !result.is_valid {
    ///     for error in &result.errors {
    ///         println!("Error: {}", error);
    ///     }
    /// }
    /// ```
    pub fn validate_for_mark(&self, encoding: &Encoding, mark: Mark) -> ValidationResult {
        // Start with basic validation
        let mut result = self.validate_for_encoding(encoding);

        // If basic validation failed, don't do mark-specific checks
        if !result.is_valid {
            return result;
        }

        // Get mark compatibility rules
        let rules = MarkCompatibilityRules::for_mark(mark);

        // Get column names for inference
        let columns = vec![encoding.x.name.clone(), encoding.y.name.clone()];

        // Infer column types for cardinality checking
        let inferred = infer_column_types(self, &columns, InferenceConfig::default());

        // Validate X axis
        let x_inferred = &inferred[0];
        self.validate_axis(
            &encoding.x,
            &rules.x_axis,
            x_inferred.cardinality,
            mark,
            &mut result,
        );

        // Validate Y axis
        let y_inferred = &inferred[1];
        self.validate_axis(
            &encoding.y,
            &rules.y_axis,
            y_inferred.cardinality,
            mark,
            &mut result,
        );

        // Validate color channel if present
        if let Some(ref color_field) = encoding.color {
            if let Some(ref color_req) = rules.color_channel {
                let color_columns = vec![color_field.name.clone()];
                let color_inferred =
                    infer_column_types(self, &color_columns, InferenceConfig::default());
                self.validate_axis(
                    color_field,
                    color_req,
                    color_inferred[0].cardinality,
                    mark,
                    &mut result,
                );
            }
        }

        result
    }

    /// Validate a single axis against compatibility rules and cardinality constraints
    fn validate_axis(
        &self,
        field: &Field,
        axis_req: &crate::core::compatibility::AxisRequirement,
        cardinality: usize,
        mark: Mark,
        result: &mut ValidationResult,
    ) {
        // Check role compatibility
        let field_role = FieldRole::from_data_type(field.data_type);
        if !axis_req.accepts_role(field_role) {
            result.add_error(DataValidationError::RoleMismatch {
                column: field.name.clone(),
                axis: axis_req.axis_name.to_string(),
                expected_role: axis_req.required_roles[0],
                actual_role: field_role,
            });
        }

        // Check data type compatibility
        if !axis_req.accepts_data_type(field.data_type) {
            result.add_error(DataValidationError::MarkIncompatible {
                mark,
                column: field.name.clone(),
                axis: axis_req.axis_name.to_string(),
                required_types: axis_req.allowed_data_types.clone(),
                actual_type: field.data_type,
            });
        }

        // Check cardinality for categorical axes
        if axis_req.axis_type == crate::core::compatibility::AxisType::Categorical {
            let constraint = CardinalityConstraint::for_chart(mark, axis_req.axis_name);
            match constraint.validate(cardinality) {
                ValidationSeverity::Error => {
                    result.add_error(DataValidationError::HighCardinality {
                        column: field.name.clone(),
                        cardinality,
                        threshold: constraint.max_allowed,
                        suggestion: constraint.warning_message.clone(),
                    });
                }
                ValidationSeverity::Warning => {
                    result.add_warning(format!(
                        "High cardinality: column '{}' has {} values (recommended: <{}). {}",
                        field.name,
                        cardinality,
                        constraint.max_recommended,
                        constraint.warning_message
                    ));
                }
                ValidationSeverity::Ok => {}
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::field_value::DataRow;
    use std::collections::HashMap;

    fn make_row(x: f64, y: f64) -> DataRow {
        let mut row = HashMap::new();
        row.insert("x".to_string(), FieldValue::Numeric(x));
        row.insert("y".to_string(), FieldValue::Numeric(y));
        row
    }

    #[test]
    fn test_validate_empty_dataset() {
        let table = DataTable::new(vec![]);
        let encoding = Encoding::new(Field::quantitative("x"), Field::quantitative("y"));

        let result = table.validate_for_encoding(&encoding);

        assert!(!result.is_valid);
        assert_eq!(result.errors.len(), 1);
        assert!(matches!(
            result.errors[0],
            DataValidationError::EmptyDataset
        ));
    }

    #[test]
    fn test_validate_missing_columns() {
        let table = DataTable::new(vec![make_row(1.0, 2.0)]);
        let encoding = Encoding::new(Field::quantitative("missing_x"), Field::quantitative("y"));

        let result = table.validate_for_encoding(&encoding);

        assert!(!result.is_valid);
        assert!(matches!(
            &result.errors[0],
            DataValidationError::MissingColumns(cols) if cols.contains(&"missing_x".to_string())
        ));
    }

    #[test]
    fn test_validate_valid_data() {
        let table = DataTable::new(vec![make_row(1.0, 2.0), make_row(3.0, 4.0)]);
        let encoding = Encoding::new(Field::quantitative("x"), Field::quantitative("y"));

        let result = table.validate_for_encoding(&encoding);

        assert!(result.is_valid);
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_validate_insufficient_numeric_data() {
        let mut rows = vec![];
        for i in 0..10 {
            let mut row = HashMap::new();
            row.insert("x".to_string(), FieldValue::Numeric(i as f64));
            // Only 30% numeric data for y
            row.insert(
                "y".to_string(),
                if i < 3 {
                    FieldValue::Numeric(i as f64)
                } else {
                    FieldValue::Text("not a number".to_string())
                },
            );
            rows.push(row);
        }
        let table = DataTable::new(rows);
        let encoding = Encoding::new(Field::quantitative("x"), Field::quantitative("y"));

        let result = table.validate_for_encoding(&encoding);

        assert!(!result.is_valid);
        assert!(result.errors.iter().any(|e| matches!(
            e,
            DataValidationError::InsufficientNumericData { column, .. } if column == "y"
        )));
    }

    // --- Tests for validate_for_mark() ---

    #[test]
    fn test_validate_for_mark_bar_chart_with_quantitative_x() {
        // Bar charts need categorical X, not quantitative
        let table = DataTable::new(vec![make_row(1.0, 10.0), make_row(2.0, 20.0)]);
        let encoding = Encoding::new(Field::quantitative("x"), Field::quantitative("y"));

        let result = table.validate_for_mark(&encoding, Mark::Bar);

        assert!(!result.is_valid);
        assert!(result.errors.iter().any(|e| matches!(
            e,
            DataValidationError::RoleMismatch { column, .. } if column == "x"
        )));
    }

    #[test]
    fn test_validate_for_mark_bar_chart_with_nominal_x() {
        // Bar chart with correct types should pass
        let mut rows = vec![];
        for (cat, val) in [("A", 10.0), ("B", 20.0), ("C", 15.0)] {
            let mut row = HashMap::new();
            row.insert("category".to_string(), FieldValue::Text(cat.to_string()));
            row.insert("value".to_string(), FieldValue::Numeric(val));
            rows.push(row);
        }
        let table = DataTable::new(rows);
        let encoding = Encoding::new(Field::nominal("category"), Field::quantitative("value"));

        let result = table.validate_for_mark(&encoding, Mark::Bar);

        assert!(result.is_valid);
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_validate_for_mark_bar_chart_high_cardinality_warning() {
        // Create bar chart with 75 categories (should warn at >50)
        let mut rows = vec![];
        for i in 0..75 {
            let mut row = HashMap::new();
            row.insert(
                "category".to_string(),
                FieldValue::Text(format!("cat_{}", i)),
            );
            row.insert("value".to_string(), FieldValue::Numeric(i as f64));
            rows.push(row);
        }
        let table = DataTable::new(rows);
        let encoding = Encoding::new(Field::nominal("category"), Field::quantitative("value"));

        let result = table.validate_for_mark(&encoding, Mark::Bar);

        // Should be valid but with warnings
        assert!(result.is_valid);
        assert_eq!(result.severity, ValidationSeverity::Warning);
        assert!(!result.warnings.is_empty());
        assert!(result.warnings[0].contains("High cardinality"));
    }

    #[test]
    fn test_validate_for_mark_bar_chart_high_cardinality_error() {
        // Create bar chart with 250 categories (should error at >200)
        let mut rows = vec![];
        for i in 0..250 {
            let mut row = HashMap::new();
            row.insert(
                "category".to_string(),
                FieldValue::Text(format!("cat_{}", i)),
            );
            row.insert("value".to_string(), FieldValue::Numeric(i as f64));
            rows.push(row);
        }
        let table = DataTable::new(rows);
        let encoding = Encoding::new(Field::nominal("category"), Field::quantitative("value"));

        let result = table.validate_for_mark(&encoding, Mark::Bar);

        assert!(!result.is_valid);
        assert!(result.errors.iter().any(|e| matches!(
            e,
            DataValidationError::HighCardinality { cardinality, .. } if *cardinality == 250
        )));
    }

    #[test]
    fn test_validate_for_mark_scatter_with_nominal_data() {
        // Scatter plots need quantitative X and Y
        let mut rows = vec![];
        for (cat, val) in [("A", 10.0), ("B", 20.0)] {
            let mut row = HashMap::new();
            row.insert("category".to_string(), FieldValue::Text(cat.to_string()));
            row.insert("value".to_string(), FieldValue::Numeric(val));
            rows.push(row);
        }
        let table = DataTable::new(rows);
        let encoding = Encoding::new(Field::nominal("category"), Field::quantitative("value"));

        let result = table.validate_for_mark(&encoding, Mark::Point);

        assert!(!result.is_valid);
        assert!(result.errors.iter().any(|e| matches!(
            e,
            DataValidationError::MarkIncompatible { mark, .. } if *mark == Mark::Point
        )));
    }

    #[test]
    fn test_validate_for_mark_pie_chart_too_many_slices() {
        // Pie chart with 35 slices (should error at >30)
        let mut rows = vec![];
        for i in 0..35 {
            let mut row = HashMap::new();
            row.insert("slice".to_string(), FieldValue::Text(format!("s_{}", i)));
            row.insert("value".to_string(), FieldValue::Numeric(i as f64));
            rows.push(row);
        }
        let table = DataTable::new(rows);
        let encoding = Encoding::new(Field::nominal("slice"), Field::quantitative("value"));

        let result = table.validate_for_mark(&encoding, Mark::Arc);

        assert!(!result.is_valid);
        assert!(result.errors.iter().any(|e| matches!(
            e,
            DataValidationError::HighCardinality { cardinality, .. } if *cardinality == 35
        )));
    }

    #[test]
    fn test_validate_for_mark_line_chart_with_temporal_x() {
        // Line chart with temporal X should pass
        let mut rows = vec![];
        for i in 0..10 {
            let mut row = HashMap::new();
            row.insert(
                "date".to_string(),
                FieldValue::Timestamp(1_000_000.0 + (i as f64 * 86400.0 * 1000.0)),
            );
            row.insert("value".to_string(), FieldValue::Numeric(i as f64 * 10.0));
            rows.push(row);
        }
        let table = DataTable::new(rows);
        let encoding = Encoding::new(Field::temporal("date"), Field::quantitative("value"));

        let result = table.validate_for_mark(&encoding, Mark::Line);

        assert!(result.is_valid);
        assert!(result.errors.is_empty());
    }
}