Skip to content

Commit 1dea1ea

Browse files
Merge branch 'OPCODE-Open-Spring-Fest:main' into aditya
2 parents 2f2bf84 + d0d1d26 commit 1dea1ea

File tree

4 files changed

+915
-12
lines changed

4 files changed

+915
-12
lines changed

examples/csv_validation_example.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
"""
2+
Example usage of the CSV validator for price and signals files.
3+
4+
This script demonstrates how to use the validate_input_csv function
5+
to validate user-provided CSV files before backtesting.
6+
"""
7+
8+
import pandas as pd
9+
from quant_research_starter.data import validate_input_csv, validate_price_csv
10+
11+
12+
def example_validate_price_file():
13+
"""Example: Validate a price CSV file."""
14+
print("=" * 60)
15+
print("Example 1: Validating Price CSV File")
16+
print("=" * 60)
17+
18+
# Simulate a price file path
19+
price_file = "data/sample_prices.csv"
20+
21+
# Validate using the general function
22+
result = validate_input_csv(price_file, csv_type="price")
23+
24+
# Check results
25+
if result["valid"]:
26+
print(f"✓ File is valid!")
27+
print(f" Rows: {result['row_count']}")
28+
print(f" Columns: {result['column_count']}")
29+
else:
30+
print(f"✗ File has {len(result['errors'])} error(s):")
31+
for error in result["errors"]:
32+
print(f" - [{error['type']}] {error['message']}")
33+
34+
# Check warnings
35+
if result["warnings"]:
36+
print(f"\n{len(result['warnings'])} warning(s):")
37+
for warning in result["warnings"]:
38+
print(f" - {warning['message']}")
39+
40+
print()
41+
42+
43+
def example_validate_with_required_columns():
44+
"""Example: Validate with specific required columns."""
45+
print("=" * 60)
46+
print("Example 2: Validating with Required Columns")
47+
print("=" * 60)
48+
49+
# Validate price file requiring specific symbols
50+
price_file = "data/sample_prices.csv"
51+
required_symbols = ["AAPL", "GOOGL", "MSFT"]
52+
53+
is_valid, errors = validate_price_csv(price_file, required_symbols=required_symbols)
54+
55+
if is_valid:
56+
print(f"✓ All required symbols present: {', '.join(required_symbols)}")
57+
else:
58+
print(f"✗ Validation failed:")
59+
for err in errors:
60+
print(f" {err}")
61+
62+
print()
63+
64+
65+
def example_error_handling():
66+
"""Example: Proper error handling in production code."""
67+
print("=" * 60)
68+
print("Example 3: Error Handling in Production")
69+
print("=" * 60)
70+
71+
def load_and_validate_prices(file_path: str):
72+
"""Load price data with validation."""
73+
# First validate
74+
result = validate_input_csv(file_path, csv_type="price")
75+
76+
if not result["valid"]:
77+
# Handle errors
78+
error_messages = [err["message"] for err in result["errors"]]
79+
raise ValueError(f"Invalid price file:\n" + "\n".join(error_messages))
80+
81+
# If valid, proceed with loading
82+
prices = pd.read_csv(file_path, index_col=0, parse_dates=True)
83+
print(f"✓ Loaded {len(prices)} rows of price data")
84+
return prices
85+
86+
# Try with a file
87+
try:
88+
prices = load_and_validate_prices("data/sample_prices.csv")
89+
except ValueError as e:
90+
print(f"✗ Error: {e}")
91+
except FileNotFoundError:
92+
print("✗ File not found (expected for this example)")
93+
94+
print()
95+
96+
97+
def example_detailed_error_info():
98+
"""Example: Accessing detailed error information."""
99+
print("=" * 60)
100+
print("Example 4: Detailed Error Information")
101+
print("=" * 60)
102+
103+
result = validate_input_csv("invalid_file.csv", csv_type="price")
104+
105+
print(f"Validation Summary:")
106+
print(f" Valid: {result['valid']}")
107+
print(f" Errors: {len(result['errors'])}")
108+
print(f" Warnings: {len(result['warnings'])}")
109+
print(f" File: {result['file_path']}")
110+
111+
# Access structured error data
112+
for error in result["errors"]:
113+
print(f"\nError Type: {error['type']}")
114+
print(f"Message: {error['message']}")
115+
if error.get("column"):
116+
print(f"Column: {error['column']}")
117+
if error.get("sample"):
118+
print(f"Sample data: {error['sample']}")
119+
120+
print()
121+
122+
123+
def main():
124+
"""Run all examples."""
125+
print("\n")
126+
print("=" * 60)
127+
print("CSV VALIDATOR USAGE EXAMPLES")
128+
print("=" * 60)
129+
print()
130+
131+
example_validate_price_file()
132+
example_validate_with_required_columns()
133+
example_error_handling()
134+
example_detailed_error_info()
135+
136+
print("=" * 60)
137+
print("For more information, see the documentation:")
138+
print("src/quant_research_starter/data/validator.py")
139+
print("=" * 60)
140+
141+
142+
if __name__ == "__main__":
143+
main()
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
"""Data module public API."""
2-
3-
from .downloaders import AlphaVantageDownloader, YahooDownloader
4-
from .sample_loader import SampleDataLoader
5-
from .synthetic import SyntheticDataGenerator
6-
7-
__all__ = [
8-
"SyntheticDataGenerator",
9-
"SampleDataLoader",
10-
"YahooDownloader",
11-
"AlphaVantageDownloader",
12-
]
1+
"""Data module public API."""
2+
3+
from .downloaders import AlphaVantageDownloader, YahooDownloader
4+
from .sample_loader import SampleDataLoader
5+
from .synthetic import SyntheticDataGenerator
6+
from .validator import (
7+
CSVValidator,
8+
ValidationError,
9+
validate_input_csv,
10+
validate_price_csv,
11+
validate_signals_csv,
12+
)
13+
14+
__all__ = [
15+
"SyntheticDataGenerator",
16+
"SampleDataLoader",
17+
"YahooDownloader",
18+
"AlphaVantageDownloader",
19+
"CSVValidator",
20+
"ValidationError",
21+
"validate_input_csv",
22+
"validate_price_csv",
23+
"validate_signals_csv",
24+
]

0 commit comments

Comments
 (0)