-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
157 lines (131 loc) · 4.85 KB
/
main.py
File metadata and controls
157 lines (131 loc) · 4.85 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
"""
FastAPI Backend for TDA Financial Backtesting Application.
Endpoints:
POST /api/analyze — Upload CSV, run TDA pipeline, return results
GET /api/health — Health check
GET / — Serve frontend
"""
import os
import io
import traceback
import pandas as pd
import numpy as np
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from tda_pipeline import run_pipeline
app = FastAPI(
title="TDA Market Risk Analyzer",
description="Topological Data Analysis for financial crash prediction",
version="1.0.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
STATIC_DIR = os.path.join(os.path.dirname(__file__), "static")
if os.path.isdir(STATIC_DIR):
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
@app.get("/api/health")
async def health():
"""Health-check endpoint."""
from tda_pipeline import TDA_BACKEND
return {"status": "ok", "tda_backend": TDA_BACKEND or "fallback"}
@app.post("/api/analyze")
async def analyze(
file: UploadFile = File(...),
window_size: int = Form(50),
embedding_dim: int = Form(3),
time_delay: int = Form(1),
):
"""
Upload a financial CSV and run the TDA pipeline.
Expected CSV columns: Date, Open, High, Low, Close, Volume
Returns JSON with ohlcv data, risk_index, threshold, and warning_zones.
"""
if not file.filename.lower().endswith(".csv"):
raise HTTPException(status_code=400, detail="Only CSV files are supported.")
if window_size < 20:
raise HTTPException(status_code=400, detail="Window size must be at least 20.")
if window_size > 500:
raise HTTPException(status_code=400, detail="Window size must be at most 500.")
if embedding_dim < 2:
raise HTTPException(status_code=400, detail="Embedding dimension must be at least 2.")
if embedding_dim > 10:
raise HTTPException(status_code=400, detail="Embedding dimension must be at most 10.")
if time_delay < 1:
raise HTTPException(status_code=400, detail="Time delay must be at least 1.")
if time_delay > 10:
raise HTTPException(status_code=400, detail="Time delay must be at most 10.")
try:
content = await file.read()
text = content.decode("utf-8")
df = pd.read_csv(io.StringIO(text))
except Exception as e:
raise HTTPException(status_code=400, detail=f"Failed to parse CSV: {str(e)}")
try:
results = run_pipeline(
df,
window_size=window_size,
embedding_dim=embedding_dim,
time_delay=time_delay,
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
traceback.print_exc()
raise HTTPException(
status_code=500,
detail=f"TDA pipeline error: {str(e)}",
)
return JSONResponse(content=results)
@app.post("/api/generate-sample")
async def generate_sample():
"""
Generate a synthetic stock dataset with a simulated crash.
Returns CSV text that the frontend can offer as a downloadable file.
"""
np.random.seed(42)
n_days = 500
dates = pd.bdate_range(start="2022-01-03", periods=n_days)
price = 100.0
prices = []
for i in range(n_days):
if i < 300:
ret = np.random.normal(0.0005, 0.012)
elif i < 330:
ret = np.random.normal(0.0002, 0.025)
elif i < 370:
ret = np.random.normal(-0.008, 0.035)
else:
ret = np.random.normal(0.001, 0.015)
price *= (1 + ret)
prices.append(price)
prices = np.array(prices)
daily_range = prices * np.random.uniform(0.005, 0.02, n_days)
opens = np.where(np.random.rand(n_days) > 0.5,
np.round(prices - daily_range * 0.3, 2),
np.round(prices + daily_range * 0.3, 2))
df = pd.DataFrame({
"Date": dates.strftime("%Y-%m-%d"),
"Open": opens,
"High": np.round(prices + daily_range * 0.5, 2),
"Low": np.round(prices - daily_range * 0.5, 2),
"Close": np.round(prices, 2),
"Volume": np.random.randint(1_000_000, 50_000_000, n_days),
})
csv_text = df.to_csv(index=False)
return JSONResponse(content={"csv": csv_text, "filename": "sample_stock_data.csv"})
@app.get("/{rest_of_path:path}")
async def serve_frontend(rest_of_path: str):
"""Serve the frontend SPA."""
index_path = os.path.join(STATIC_DIR, "index.html")
if os.path.isfile(index_path):
return FileResponse(index_path)
return JSONResponse(
status_code=404,
content={"detail": "Frontend not found. Place index.html in /static."},
)