-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
99 lines (74 loc) · 2.78 KB
/
main.py
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
import pandas as pd
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import json
from sklearn.preprocessing import MinMaxScaler
from kdtree import KDTreeDS, KnnQuery, BallQuery
from patient import Patient
import os
app = FastAPI()
# Configuración de CORS
origins = [
"http://localhost",
"http://localhost:4200",
"https://basefront-ksdp.vercel.app"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
df = pd.read_csv('data.csv')
index = df['index'].astype(str)
levels = df['Level']
age = df['Age']
features = df[['Air Pollution', 'Alcohol use', 'Dust Allergy', 'OccuPational Hazards', 'Genetic Risk',
'chronic Lung Disease', 'Balanced Diet' ,'Obesity', 'Smoking', 'Passive Smoker', 'Chest Pain', 'Coughing of Blood',
'Fatigue', 'Weight Loss', 'Shortness of Breath', 'Wheezing', 'Swallowing Difficulty',
'Clubbing of Finger Nails', 'Frequent Cold', 'Dry Cough', 'Snoring']]
scaler = MinMaxScaler(feature_range=(1,9))
data = features.to_numpy()
scaled_data = scaler.fit_transform(data)
#Patience is a list of Patient objects
patients: list[Patient] = []
for i in range (len(scaled_data)):
patients.append(Patient(index = index[i], level = levels[i], age = age[i], characteristics = scaled_data[i]))
tree: KDTreeDS = KDTreeDS(scaled_data)
query_point = ['1000',47,6,4,1,5,4,4,3,2,3,5,7,7,5,3,2,7,8,2,4,6,3,'high']
level:str = query_point.pop()
age:int = query_point.pop(1)
new_index:str = query_point.pop(0)
new_point = scaler.transform([query_point])
@app.post("/knn_query")
async def knn_query(query: KnnQuery):
print(query)
query_point = scaler.transform([query.new_point])
indices, distances = tree.knn_query(query_point, query.k)
similar_patients_json = []
for i in indices:
similar_patients_json.append(patients[i].model_dump_json())
print(similar_patients_json)
return similar_patients_json
@app.post("/ball_query")
async def ball_query(query: BallQuery):
query_point = scaler.transform([query.new_point])
indices = tree.ball_query(query_point, query.radius)
similar_patients_json = []
for i in indices:
similar_patients_json.append(patients[i].model_dump_json())
try:
json.dumps(similar_patients_json)
except TypeError:
return {"error": "El objeto similar_patients_json no es serializable a JSON"}
return similar_patients_json
@app.get("/patients")
async def get_patients():
patients_json = []
for patient in patients:
patients_json.append(patient.model_dump_json())
return patients_json
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port= int(os.environ.get("PORT", 5000)))