forked from OPCODE-Open-Spring-Fest/QuantResearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_validation_example.py
More file actions
144 lines (109 loc) · 4.15 KB
/
csv_validation_example.py
File metadata and controls
144 lines (109 loc) · 4.15 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
"""
Example usage of the CSV validator for price and signals files.
This script demonstrates how to use the validate_input_csv function
to validate user-provided CSV files before backtesting.
"""
import pandas as pd
from quant_research_starter.data import validate_input_csv, validate_price_csv
def example_validate_price_file():
"""Example: Validate a price CSV file."""
print("=" * 60)
print("Example 1: Validating Price CSV File")
print("=" * 60)
# Simulate a price file path
price_file = "data/sample_prices.csv"
# Validate using the general function
result = validate_input_csv(price_file, csv_type="price")
# Check results
if result["valid"]:
print("✓ File is valid!")
print(f" Rows: {result['row_count']}")
print(f" Columns: {result['column_count']}")
else:
print(f"✗ File has {len(result['errors'])} error(s):")
for error in result["errors"]:
print(f" - [{error['type']}] {error['message']}")
# Check warnings
if result["warnings"]:
print(f"\n⚠ {len(result['warnings'])} warning(s):")
for warning in result["warnings"]:
print(f" - {warning['message']}")
print()
def example_validate_with_required_columns():
"""Example: Validate with specific required columns."""
print("=" * 60)
print("Example 2: Validating with Required Columns")
print("=" * 60)
# Validate price file requiring specific symbols
price_file = "data/sample_prices.csv"
required_symbols = ["AAPL", "GOOGL", "MSFT"]
is_valid, errors = validate_price_csv(price_file, required_symbols=required_symbols)
if is_valid:
print(f"✓ All required symbols present: {', '.join(required_symbols)}")
else:
print("✗ Validation failed:")
for err in errors:
print(f" {err}")
print()
def example_error_handling():
"""Example: Proper error handling in production code."""
print("=" * 60)
print("Example 3: Error Handling in Production")
print("=" * 60)
def load_and_validate_prices(file_path: str):
"""Load price data with validation."""
# First validate
result = validate_input_csv(file_path, csv_type="price")
if not result["valid"]:
# Handle errors
error_messages = [err["message"] for err in result["errors"]]
raise ValueError("Invalid price file:\n" + "\n".join(error_messages))
# If valid, proceed with loading
prices = pd.read_csv(file_path, index_col=0, parse_dates=True)
print(f"✓ Loaded {len(prices)} rows of price data")
return prices
# Try with a file
try:
prices = load_and_validate_prices("data/sample_prices.csv")
except ValueError as e:
print(f"✗ Error: {e}")
except FileNotFoundError:
print("✗ File not found (expected for this example)")
print()
def example_detailed_error_info():
"""Example: Accessing detailed error information."""
print("=" * 60)
print("Example 4: Detailed Error Information")
print("=" * 60)
result = validate_input_csv("invalid_file.csv", csv_type="price")
print("Validation Summary:")
print(f" Valid: {result['valid']}")
print(f" Errors: {len(result['errors'])}")
print(f" Warnings: {len(result['warnings'])}")
print(f" File: {result['file_path']}")
# Access structured error data
for error in result["errors"]:
print(f"\nError Type: {error['type']}")
print(f"Message: {error['message']}")
if error.get("column"):
print(f"Column: {error['column']}")
if error.get("sample"):
print(f"Sample data: {error['sample']}")
print()
def main():
"""Run all examples."""
print("\n")
print("=" * 60)
print("CSV VALIDATOR USAGE EXAMPLES")
print("=" * 60)
print()
example_validate_price_file()
example_validate_with_required_columns()
example_error_handling()
example_detailed_error_info()
print("=" * 60)
print("For more information, see the documentation:")
print("src/quant_research_starter/data/validator.py")
print("=" * 60)
if __name__ == "__main__":
main()