-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_data.py
More file actions
680 lines (607 loc) · 23.8 KB
/
validate_data.py
File metadata and controls
680 lines (607 loc) · 23.8 KB
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
import sys
import pandas as pd
from typing import List, Dict, Optional
import argparse
import numpy as np
import os
import glob
# Import validation functions from app.py
from util import (
load_csv, check_missing_columns, check_integer_columns, check_non_empty_strings
)
# Schema configurations for all Africa tables
SCHEMAS = {
'africa_rural_urban': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'status', 'urban_rural', 'share', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'status': str,
'urban_rural': str,
'share': float, # Nullable
'population': int
},
'nullable_columns': ['share']
},
'africa_employed_employment_type': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'sector_group', 'main_job_type', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'sector_group': str, # Nullable
'main_job_type': str, # Nullable
'status': str,
'population': int
},
'nullable_columns': ['sector_group', 'main_job_type'],
'categorical_values': {
'sector_group': ['Industry', 'Agriculture', 'Services']
}
},
'total_working_population': {
'required_columns': ['ccode', 'country', 'year', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'population': int # Nullable
},
'nullable_columns': ['population']
},
'africa_education_inactive': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'education', 'reason_inactive', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'education': str, # Nullable
'reason_inactive': str,
'status': str,
'population': int
},
'nullable_columns': ['education'],
'categorical_values': {
'reason_inactive': ['childcare/pregnancy', 'discouraged', 'health-related', 'homemaker', 'other']
}
},
'africa_education_student': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'education', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'education': str,
'status': str,
'population': int
},
'nullable_columns': []
},
'africa_education_unemployed': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'education', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'education': str,
'status': str,
'population': int
},
'nullable_columns': []
},
'employed_education_by_sector': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'education', 'sector_group', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'education': str,
'sector_group': str, # Nullable
'status': str,
'population': int
},
'nullable_columns': ['sector_group'],
'categorical_values': {
'sector_group': ['Industry', 'Agriculture', 'Services']
}
},
'employed_working_poor': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'status_poor', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'status_poor': str,
'status': str,
'population': int
},
'nullable_columns': [],
'categorical_values': {
'status_poor': ['Extremely poor < USD 2.15 PPP', 'Moderately poor >= USD 2.15 and < USD 3.65 PPP', 'Not poor >= USD 3.65 PPP']
}
},
'africa_sector_employed': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'sector', 'sector_group', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'sector': str,
'sector_group': str,
'status': str,
'population': int
},
'nullable_columns': [],
'categorical_values': {
'sector_group': ['Industry', 'Agriculture', 'Services']
}
},
'employed_education': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'education', 'sector_group', 'informal_formal', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'education': str,
'sector_group': str,
'informal_formal': str,
'status': str,
'population': int
},
'nullable_columns': []
},
'employed_formality_status': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'sector_group', 'informal_formal', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'sector_group': str,
'informal_formal': str,
'status': str,
'population': int
},
'nullable_columns': []
},
'africa_employed_sector_group_income': {
'required_columns': ['ccode', 'country', 'year', 'age', 'gender', 'sector_group', 'status', 'median_annual_income'],
'expected_types': {
'ccode': str,
'country': str,
'year': int,
'age': int,
'gender': str,
'sector_group': str, # Nullable
'status': str,
'median_annual_income': float
},
'nullable_columns': ['sector_group'],
'categorical_values': {
'sector_group': ['Industry', 'Agriculture', 'Services']
}
},
'subnational_student': {
'required_columns': ['ccode', 'country', 'region', 'year', 'age', 'gender', 'education', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'region': str,
'year': int,
'age': int,
'gender': str,
'education': str,
'status': str,
'population': int
},
'nullable_columns': []
},
'subnational_unemployed': {
'required_columns': ['ccode', 'country', 'region', 'year', 'age', 'gender', 'education', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'region': str,
'year': int,
'age': int,
'gender': str,
'education': str,
'status': str,
'population': int
},
'nullable_columns': []
},
'subnational_inactive': {
'required_columns': ['ccode', 'country', 'region', 'year', 'age', 'gender', 'education', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'region': str,
'year': int,
'age': int,
'gender': str,
'education': str,
'status': str,
'population': int
},
'nullable_columns': []
},
'subnational_employed': {
'required_columns': ['ccode', 'country', 'region', 'year', 'age', 'gender', 'sector', 'sector_group', 'status', 'informal_formal', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'region': str,
'year': int,
'age': int,
'gender': str,
'sector': str, # Nullable
'sector_group': str, # Nullable
'status': str,
'informal_formal': str, # Nullable
'population': int
},
'nullable_columns': ['sector', 'sector_group', 'informal_formal']
},
'subnational_employed_working_poor': {
'required_columns': ['ccode', 'country', 'region', 'year', 'age', 'gender', 'status_poor', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'region': str,
'year': int,
'age': int,
'gender': str,
'status_poor': str,
'status': str,
'population': int
},
'nullable_columns': []
},
'subnational_employed_sector_group_income': {
'required_columns': ['ccode', 'country', 'region', 'year', 'age', 'gender', 'sector_group', 'status', 'median_annual_income'],
'expected_types': {
'ccode': str,
'country': str,
'region': str,
'year': int,
'age': int,
'gender': str,
'sector_group': str, # Nullable
'status': str,
'median_annual_income': float
},
'nullable_columns': ['sector_group']
},
'subnational_employed_employment_type': {
'required_columns': ['ccode', 'country', 'region', 'year', 'age', 'gender', 'sector_group', 'main_job_type', 'status', 'population'],
'expected_types': {
'ccode': str,
'country': str,
'region': str,
'year': int,
'age': int,
'gender': str,
'sector_group': str, # Nullable
'main_job_type': str, # Nullable
'status': str,
'population': int
},
'nullable_columns': ['sector_group', 'main_job_type']
}
}
def get_table_name_from_filename(csv_file: str) -> Optional[str]:
"""Extract table name from CSV filename"""
filename = os.path.basename(csv_file)
# Remove .csv extension
table_name = filename.replace('.csv', '')
return table_name if table_name in SCHEMAS else None
def check_column_types(df: pd.DataFrame, expected_types: Dict[str, type], nullable_cols: List[str]) -> bool:
"""Check if columns have the expected types, handling nullable columns appropriately"""
all_valid = True
for col_name, expected_type in expected_types.items():
if col_name not in df.columns:
continue
# For nullable columns, check non-null values only
if col_name in nullable_cols:
non_null_values = df[col_name].dropna()
if len(non_null_values) > 0:
if expected_type == int:
try:
pd.to_numeric(non_null_values, errors='raise').astype('Int64')
except (ValueError, TypeError):
print(f" ❌ Column '{col_name}' contains non-integer values (nullable column)")
all_valid = False
elif expected_type == float:
try:
pd.to_numeric(non_null_values, errors='raise').astype(float)
except (ValueError, TypeError):
print(f" ❌ Column '{col_name}' contains non-float values (nullable column)")
all_valid = False
elif expected_type == str:
non_strings = non_null_values[~non_null_values.apply(lambda x: isinstance(x, str))]
if len(non_strings) > 0:
print(f" ❌ Column '{col_name}' contains non-string values (nullable column)")
all_valid = False
else:
# For non-nullable columns, check all values
if expected_type == int:
try:
pd.to_numeric(df[col_name], errors='raise').astype('Int64')
except (ValueError, TypeError):
print(f" ❌ Column '{col_name}' contains non-integer values")
all_valid = False
elif expected_type == float:
try:
pd.to_numeric(df[col_name], errors='raise').astype(float)
except (ValueError, TypeError):
print(f" ❌ Column '{col_name}' contains non-float values")
all_valid = False
elif expected_type == str:
non_strings = df[~df[col_name].apply(lambda x: isinstance(x, str) or pd.isna(x))]
if len(non_strings) > 0:
print(f" ❌ Column '{col_name}' contains non-string values")
all_valid = False
if all_valid:
print(" ✅ All column types are valid")
return all_valid
def check_nullable_columns(df: pd.DataFrame, nullable_cols: List[str], non_nullable_cols: List[str]) -> bool:
"""Check that non-nullable columns don't have nulls"""
all_valid = True
for col in non_nullable_cols:
if col not in df.columns:
continue
null_count = df[col].isnull().sum()
if null_count > 0:
print(f" ❌ Column '{col}' is non-nullable but contains {null_count} null values")
all_valid = False
if all_valid:
print(" ✅ Nullable/non-nullable constraints are satisfied")
return all_valid
def check_categorical_values_with_nullable(df: pd.DataFrame, categorical_values: Dict[str, List[str]], nullable_cols: List[str]) -> bool:
"""Check categorical values, handling nullable columns appropriately"""
if not categorical_values:
return True
all_valid = True
for column, valid_values in categorical_values.items():
if column not in df.columns:
continue
# For nullable columns, only check non-null values
if column in nullable_cols:
non_null_df = df[df[column].notna()]
if len(non_null_df) > 0:
invalid_values = non_null_df[~non_null_df[column].isin(valid_values)]
if not invalid_values.empty:
print(f" ❌ Column '{column}' contains invalid categorical values (nullable column)")
print(f" Expected: {valid_values}")
unique_invalid = invalid_values[column].unique()
print(f" Found invalid values: {list(unique_invalid)}")
all_valid = False
else:
# For non-nullable columns, check all values
invalid_values = df[~df[column].isin(valid_values)]
if not invalid_values.empty:
print(f" ❌ Column '{column}' contains invalid categorical values")
print(f" Expected: {valid_values}")
unique_invalid = invalid_values[column].unique()
print(f" Found invalid values: {list(unique_invalid)}")
all_valid = False
if all_valid and categorical_values:
print(" ✅ All categorical columns contain valid values")
return all_valid
def validate_africa_csv(file_path: str, table_name: str, sample_size: int = 100000) -> bool:
"""Validate an Africa CSV file against its schema"""
print(f"\n{'='*80}")
print(f"Validating: {table_name}")
print(f"File: {file_path}")
print(f"{'='*80}")
# Get schema configuration
schema = SCHEMAS.get(table_name)
if not schema:
print(f" ❌ Unknown table name: {table_name}")
print(f" Available tables: {', '.join(SCHEMAS.keys())}")
return False
# Load CSV sample
print(f"\n Loading sample data (first {sample_size} rows)...")
df = load_csv(file_path, sample_size=sample_size)
if df is None:
return False
print(f" ✅ Loaded {len(df)} rows, {len(df.columns)} columns")
required_columns = schema['required_columns']
expected_types = schema['expected_types']
nullable_cols = schema['nullable_columns']
categorical_values = schema.get('categorical_values', {})
non_nullable_cols = [col for col in required_columns if col not in nullable_cols]
string_columns = [col for col in required_columns if expected_types[col] == str]
# Run validations
tests = []
# 1. Check missing columns
print(f"\n Checking for missing columns...")
tests.append(check_missing_columns(df, required_columns))
# 2. Check for extra columns (fail if found)
print(f"\n Checking for extra columns...")
extra_columns = [col for col in df.columns if col not in required_columns]
if extra_columns:
print(f" ❌ Found extra columns not in schema: {extra_columns}")
tests.append(False)
else:
print(" ✅ No extra columns found")
tests.append(True)
# 3. Check nullable constraints
print(f"\n Checking nullable constraints...")
tests.append(check_nullable_columns(df, nullable_cols, non_nullable_cols))
# 4. Check column types
print(f"\n Checking column types...")
tests.append(check_column_types(df, expected_types, nullable_cols))
# 5. Check categorical values if defined
if categorical_values:
print(f"\n Checking categorical values...")
tests.append(check_categorical_values_with_nullable(df, categorical_values, nullable_cols))
# 6. Check non-empty strings for non-nullable string columns
non_nullable_string_cols = [col for col in string_columns if col in non_nullable_cols]
if non_nullable_string_cols:
print(f"\n Checking non-empty strings...")
tests.append(check_non_empty_strings(df, non_nullable_string_cols))
# 7. Check integer columns for non-nullable integer columns
non_nullable_int_cols = [col for col in non_nullable_cols
if expected_types[col] == int and col in df.columns]
if non_nullable_int_cols:
print(f"\n Checking integer columns...")
tests.append(check_integer_columns(df, non_nullable_int_cols))
# Summary
all_passed = all(tests)
if all_passed:
print(f"\n ✅ '{table_name}' passed all validation checks!")
else:
print(f"\n ❌ '{table_name}' failed some validation checks!")
return all_passed
def find_csv_files(folder_path: str) -> List[str]:
"""Find all CSV files in the specified folder"""
# Support both .csv and .CSV extensions
csv_patterns = [
os.path.join(folder_path, "*.csv"),
os.path.join(folder_path, "*.CSV")
]
csv_files = []
for pattern in csv_patterns:
csv_files.extend(glob.glob(pattern))
# Remove duplicates and sort
csv_files = sorted(list(set(csv_files)))
return csv_files
def check_all_required_files_exist(folder_path: str) -> bool:
"""Check that all 19 required CSV files exist in the data folder"""
print("\n" + "="*80)
print("Checking for existence of all required files")
print("="*80)
# Get all expected table names from SCHEMAS
expected_tables = list(SCHEMAS.keys())
expected_files = [f"{table_name}.csv" for table_name in expected_tables]
missing_files = []
found_files = []
for table_name in expected_tables:
expected_file = f"{table_name}.csv"
file_path = os.path.join(folder_path, expected_file)
# Check for both .csv and .CSV extensions
if os.path.exists(file_path) or os.path.exists(os.path.join(folder_path, expected_file.upper())):
found_files.append(expected_file)
else:
missing_files.append(expected_file)
print(f"\nExpected files: {len(expected_files)}")
print(f"Found files: {len(found_files)}")
print(f"Missing files: {len(missing_files)}")
if missing_files:
print(f"\n❌ Missing required files:")
for missing_file in missing_files:
print(f" - {missing_file}")
return False
else:
print(f"\n✅ All {len(expected_files)} required files are present")
return True
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Validate all Africa CSV files in a folder against schema definitions"
)
parser.add_argument(
"folder_path",
help="Path to the folder containing CSV files to validate"
)
parser.add_argument(
"--sample-size",
type=int,
default=100000,
help="Number of rows to sample from each CSV for validation (default: 100000)"
)
args = parser.parse_args()
# Check if folder exists
if not os.path.isdir(args.folder_path):
print(f"❌ Error: '{args.folder_path}' is not a valid directory")
sys.exit(1)
# Check for existence of all required files
all_files_exist = check_all_required_files_exist(args.folder_path)
if not all_files_exist:
print("\n❌ Error: Not all required files are present. Proceeding with available files.")
# Find all CSV files in the folder
csv_files = find_csv_files(args.folder_path)
if not csv_files:
print(f"❌ No CSV files found in folder: {args.folder_path}")
sys.exit(1)
print("\n" + "="*80)
print("Africa CSV Files Batch Validation")
print("="*80)
print(f"Folder: {args.folder_path}")
print(f"Found {len(csv_files)} CSV file(s)")
print(f"Sample size: {args.sample_size} rows per file")
print("="*80)
# Validate each CSV file
results = []
skipped_files = []
for csv_file in csv_files:
# Determine table name from filename
table_name = get_table_name_from_filename(csv_file)
if not table_name:
filename = os.path.basename(csv_file)
print(f"\n⚠️ Skipping '{filename}': Could not determine table name")
print(f" Available tables: {', '.join(SCHEMAS.keys())}")
skipped_files.append(csv_file)
continue
# Validate the CSV file
try:
is_valid = validate_africa_csv(csv_file, table_name, args.sample_size)
results.append((csv_file, table_name, is_valid))
except Exception as e:
print(f"\n❌ Error validating {csv_file}: {e}")
import traceback
traceback.print_exc()
results.append((csv_file, table_name, False))
# Summary
print("\n" + "="*80)
print("VALIDATION SUMMARY")
print("="*80)
passed = [r for r in results if r[2]]
failed = [r for r in results if not r[2]]
print(f"\nTotal files processed: {len(results)}")
print(f"✅ Passed: {len(passed)}")
print(f"❌ Failed: {len(failed)}")
if skipped_files:
print(f"⚠️ Skipped: {len(skipped_files)}")
if passed:
print(f"\n✅ Passed files:")
for csv_file, table_name, _ in passed:
filename = os.path.basename(csv_file)
print(f" - {filename} ({table_name})")
if failed:
print(f"\n❌ Failed files:")
for csv_file, table_name, _ in failed:
filename = os.path.basename(csv_file)
print(f" - {filename} ({table_name})")
if skipped_files:
print(f"\n⚠️ Skipped files:")
for csv_file in skipped_files:
filename = os.path.basename(csv_file)
print(f" - {filename}")
print("="*80)
# Exit with error code if any files failed
if failed:
sys.exit(1)
else:
sys.exit(0)