-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
119 lines (95 loc) · 3.19 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json
import numpy as np
import pca
import process_object as po
from flask import Flask, request, jsonify, render_template, url_for
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/supernova')
def supernova():
return render_template('supernova.html')
@app.route('/new')
def splinenew():
return render_template('index_new.html')
@app.route('/plotusers', methods=['post'])
def plotusers():
data = request.get_json()
position = po.project(data['lc'], data['p'])
return jsonify(position)
@app.route('/fastpca')
def fastpca():
return render_template('fastpca.html')
@app.route('/calculatepca', methods=['post'])
def calculatepca():
global LAST_PCA
uids = request.get_json()
idlist, matrix, status = get_data_by_id(uids)
pca_result = {}
if status == 'ok':
pca_result = pca.calculate(idlist, matrix, LAST_PCA)
LAST_PCA = pca_result
final_result = [[{} for x in range(len(pca_result[0]))] for x in range(len(pca_result))]
for i in range(len(pca_result)):
for j in range(len(pca_result[0])):
final_result[i][j] = {'count':pca_result[i][j]}
return jsonify({'status':status, 'data':final_result})
@app.route('/calculate_average_lc', methods=['post'])
def calculate_average_lc():
global LCDATA, LCPHASE
uids = request.get_json()
# TODO: band as parameter
band = 'V'
matrix = []
for i in uids:
i = str(i)
if i in LCDATA:
if band in LCDATA[i]['bands']:
vec = LCDATA[i][band]['mag']
if len(vec) > 0: # some id may not have lc data
matrix.append(LCDATA[i][band]['mag'])
mean = np.mean(np.array(matrix), axis=0)
std = np.std(np.array(matrix), axis=0)
return jsonify({'mean':mean.tolist(),
'std':std.tolist(),
'phase':LCPHASE})
def load_lc_data():
lcdata = {}
lcphase = [] # Assume all phase for different surveys are the same
surveys = json.load(open("./static/data_ogle/list.json"))['surveys']
for s in surveys:
path = "./static/data_ogle/lightcurves/{}/fit.json".format(s)
data = json.load(open(path))
lcphase = data['phase']
for objid in data['data']:
lcdata[objid] = data['data'][objid]
return lcdata, lcphase
def get_data_by_id(ids, band='V'):
global LCDATA
ids_exist = [] # some id may not have lc data
matrix = []
status = 'ok'
for i in ids:
i = str(i)
if i in LCDATA:
if band in LCDATA[i]['bands']:
vec = LCDATA[i][band]['mag']
if len(vec) > 0:
ids_exist.append(i)
matrix.append(vec)
if len(matrix) == 0:
return ids_exist, matrix, 'no light curve data'
c_length = len(matrix[0])
matrix = np.array(matrix)
if len(matrix) < c_length:
status = "numrows < numcols"
else:
try:
matrix.shape = (len(matrix), c_length)
except:
status = "not all rows have same numcols"
return ids_exist, matrix, status
LCDATA, LCPHASE = load_lc_data()
LAST_PCA = []
app.run(port=8080, debug=True)