File size: 9,676 Bytes
c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf 4134ab0 c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 16ec2cf c7d4394 |
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 |
"""
models/anomaly-detection/src/components/data_validation.py
Data validation component based on schema.yaml
"""
import os
import yaml
import pandas as pd
import logging
from datetime import datetime
from pathlib import Path
from typing import Optional, List, Dict, Any
from ..entity import DataValidationConfig, DataValidationArtifact
logger = logging.getLogger("data_validation")
class DataValidation:
"""
Data validation component that validates feed data against schema.
Checks column types, required fields, and value constraints.
"""
def __init__(self, config: Optional[DataValidationConfig] = None):
"""
Initialize data validation component.
Args:
config: Optional configuration, uses defaults if None
"""
self.config = config or DataValidationConfig()
# Ensure output directory exists
Path(self.config.output_directory).mkdir(parents=True, exist_ok=True)
# Load schema
self.schema = self._load_schema()
logger.info(f"[DataValidation] Initialized with schema: {self.config.schema_file}")
def _load_schema(self) -> Dict[str, Any]:
"""Load schema from YAML file"""
if not os.path.exists(self.config.schema_file):
logger.warning(f"[DataValidation] Schema file not found: {self.config.schema_file}")
return {}
try:
with open(self.config.schema_file, 'r', encoding='utf-8') as f:
return yaml.safe_load(f)
except Exception as e:
logger.error(f"[DataValidation] Failed to load schema: {e}")
return {}
def _validate_required_columns(self, df: pd.DataFrame) -> List[Dict[str, Any]]:
"""
Check that all required columns are present.
Returns:
List of validation errors
"""
errors = []
for col in self.config.required_columns:
if col not in df.columns:
errors.append({
"type": "missing_column",
"column": col,
"message": f"Required column '{col}' is missing"
})
return errors
def _validate_column_types(self, df: pd.DataFrame) -> List[Dict[str, Any]]:
"""
Validate column data types based on schema.
Returns:
List of validation errors
"""
errors = []
if "feed_columns" not in self.schema:
return errors
for col_name, col_spec in self.schema["feed_columns"].items():
if col_name not in df.columns:
continue
expected_dtype = col_spec.get("dtype", "str")
# Check for null values in required columns
if col_spec.get("required", False):
null_count = df[col_name].isna().sum()
if null_count > 0:
errors.append({
"type": "null_values",
"column": col_name,
"count": int(null_count),
"message": f"Column '{col_name}' has {null_count} null values"
})
# Check min/max length for strings
if expected_dtype == "str" and col_name in df.columns:
min_len = col_spec.get("min_length", 0)
max_len = col_spec.get("max_length", float('inf'))
if min_len > 0:
short_count = (df[col_name].fillna("").str.len() < min_len).sum()
if short_count > 0:
errors.append({
"type": "min_length_violation",
"column": col_name,
"count": int(short_count),
"message": f"Column '{col_name}' has {short_count} values shorter than {min_len}"
})
# Check allowed values
allowed = col_spec.get("allowed_values")
if allowed and col_name in df.columns:
invalid_mask = ~df[col_name].isin(allowed) & df[col_name].notna()
invalid_count = invalid_mask.sum()
if invalid_count > 0:
errors.append({
"type": "invalid_value",
"column": col_name,
"count": int(invalid_count),
"allowed": allowed,
"message": f"Column '{col_name}' has {invalid_count} values not in allowed list"
})
return errors
def _validate_numeric_ranges(self, df: pd.DataFrame) -> List[Dict[str, Any]]:
"""
Validate numeric column ranges.
Returns:
List of validation errors
"""
errors = []
if "feed_columns" not in self.schema:
return errors
for col_name, col_spec in self.schema["feed_columns"].items():
if col_name not in df.columns:
continue
expected_dtype = col_spec.get("dtype")
if expected_dtype in ["int", "float"]:
min_val = col_spec.get("min_value")
max_val = col_spec.get("max_value")
if min_val is not None:
try:
below_count = (pd.to_numeric(df[col_name], errors='coerce') < min_val).sum()
if below_count > 0:
errors.append({
"type": "below_minimum",
"column": col_name,
"count": int(below_count),
"min_value": min_val,
"message": f"Column '{col_name}' has {below_count} values below {min_val}"
})
except Exception:
pass
if max_val is not None:
try:
above_count = (pd.to_numeric(df[col_name], errors='coerce') > max_val).sum()
if above_count > 0:
errors.append({
"type": "above_maximum",
"column": col_name,
"count": int(above_count),
"max_value": max_val,
"message": f"Column '{col_name}' has {above_count} values above {max_val}"
})
except Exception:
pass
return errors
def initiate_data_validation(self, data_path: str) -> DataValidationArtifact:
"""
Execute data validation pipeline.
Args:
data_path: Path to input data (parquet or csv)
Returns:
DataValidationArtifact with validation results
"""
logger.info(f"[DataValidation] Validating: {data_path}")
# Load data
if data_path.endswith(".parquet"):
df = pd.read_parquet(data_path)
elif data_path.endswith(".csv"):
df = pd.read_csv(data_path)
else:
raise ValueError(f"Unsupported file format: {data_path}")
total_records = len(df)
logger.info(f"[DataValidation] Loaded {total_records} records")
# Run validations
all_errors = []
all_errors.extend(self._validate_required_columns(df))
all_errors.extend(self._validate_column_types(df))
all_errors.extend(self._validate_numeric_ranges(df))
# Calculate valid/invalid records
invalid_records = 0
for error in all_errors:
if "count" in error:
invalid_records = max(invalid_records, error["count"])
valid_records = total_records - invalid_records
validation_status = len(all_errors) == 0
# Log validation results
if validation_status:
logger.info("[DataValidation] ✓ All validations passed")
else:
logger.warning(f"[DataValidation] ⚠ Found {len(all_errors)} validation issues")
for error in all_errors[:5]: # Log first 5
logger.warning(f" - {error['message']}")
# Save validated data (even with warnings, we continue)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
validated_path = Path(self.config.output_directory) / f"validated_data_{timestamp}.parquet"
df.to_parquet(validated_path, index=False)
# Save validation report
report_path = Path(self.config.output_directory) / f"validation_report_{timestamp}.yaml"
report = {
"validation_timestamp": timestamp,
"input_path": data_path,
"total_records": total_records,
"valid_records": valid_records,
"invalid_records": invalid_records,
"validation_status": validation_status,
"errors": all_errors
}
with open(report_path, 'w') as f:
yaml.dump(report, f, default_flow_style=False)
artifact = DataValidationArtifact(
validated_data_path=str(validated_path),
validation_report_path=str(report_path),
total_records=total_records,
valid_records=valid_records,
invalid_records=invalid_records,
validation_status=validation_status,
validation_errors=all_errors
)
logger.info(f"[DataValidation] ✓ Complete: {valid_records}/{total_records} valid records")
return artifact
|