-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
108 lines (82 loc) · 2.73 KB
/
app.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
100
101
102
103
104
105
106
107
108
import json
from typing import Union
import numpy as np
import pandas as pd
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from projection_ensemble import (
DRResult,
ProjectionEnsemble,
ProjectionEnsembleResult,
Point,
TSNEHParams,
TSNEHParamsBody,
UMAPHParams,
UMAPHParamsBody,
UMAPWrapper,
TSNEWrapper,
procrustes,
preset_methods,
PresetMethodNames,
)
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
demo_files = {
"mnist_50000": "label",
"mnist_10000": "label",
# add your data files class column name here like:
# data file name: class column name
}
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/v1/preset")
async def v1_preset(title: str, method: PresetMethodNames):
return FileResponse(f"./data/{title}/{method}.json")
@app.get("/v2/preset")
async def v2_preset(title: str, method: PresetMethodNames):
# generate new embeddings
df = pd.read_csv(f"./data/{title}.csv")
values = df.drop([demo_files[title]], axis=1)
target = df[demo_files[title]]
projection_ensemble = ProjectionEnsemble(values, target)
methods = preset_methods[method]
drs = []
if "tsne" in method:
drs.extend([TSNEWrapper(values.values, hparams) for hparams in preset_methods[method]]) # type: ignore
elif "umap" in method:
drs.extend([UMAPWrapper(values.values, hparams) for hparams in preset_methods[method]]) # type: ignore
drs = [drs[0]] + [procrustes(drs[0], dr) for dr in drs[1:]]
np.save(f"./data/{title}/{method}_drs.npy", np.array(drs))
dr_results = [
DRResult(
[
Point(i, x=float(row[0]), y=float(row[1]), label=str(target[i]))
for i, row in enumerate(drs[j])
],
methods[j],
)
for j in range(len(drs))
]
fsm_result = projection_ensemble.fit(drs)
result = ProjectionEnsembleResult(dr_results, fsm_result)
with open(f"./data/{title}/{method}.json", "w") as f:
json.dump(result.__dict__(), f, cls=NumpyEncoder)
return result.__dict__()
@app.post("/v1/dr")
async def v1_dr(title: str, method: str, body: Union[TSNEHParamsBody, UMAPHParamsBody]):
pass
@app.post("/v2/dr")
async def v2_dr(title: str, method: str, body: Union[TSNEHParamsBody, UMAPHParamsBody]):
pass
app.mount("/", StaticFiles(directory="dist", html=True), name="dist")