-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.py
More file actions
140 lines (116 loc) · 4.01 KB
/
app.py
File metadata and controls
140 lines (116 loc) · 4.01 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
# Flask server
# https://flask.palletsprojects.com/en/2.2.x/
# python3 flask_api.py
import json
from flask import Flask, request
from flask_cors import CORS
from processing import (
generate_spectrum,
generate_background,
process_spectrum,
find_peaks
)
from processing_utils import param_check
app = Flask(__name__)
CORS(app)
try:
with open("version.txt","r") as f:
version = f.read()
app.config["VERSION"] = version
except:
print("no version file found")
@app.route("/", methods=["GET"])
def ftir() -> str:
if "VERSION" not in app.config:
app.config["VERSION"] = "0.0.0"
return "<h1 style='color:blue'>Raston Lab FTIR API%s</h1>" % (" - Version "+app.config["VERSION"])
@app.route("/sample", methods=["POST"])
def sample() -> dict[bool, list[float], list[float]]:
# put incoming JSON into a dictionary
params = json.loads(request.data)
# verify user input is valid
if not param_check(params):
return {
"success": False,
"text": "One of the given parameters was invalid. Please change some settings and try again.",
}
# perform:
# --> transmission spectrum of gas sample (calc_spectrum)
spectrum, error, message = generate_spectrum(params)
if error:
return {
"success": False,
"text": message,
}
# perform:
# --> blackbody spectrum of source (sPlanck)
# --> transmission spectrum of beamsplitter and cell windows
# --> detector response spectrum
processed_spectrum = process_spectrum(params, spectrum)
# https://radis.readthedocs.io/en/latest/source/radis.spectrum.spectrum.html#radis.spectrum.spectrum.Spectrum.get
x_value, y_value = processed_spectrum.get("transmittance_noslit")
# convert dictionary values to strings and return as JSON
return {
"success": True,
"x": list(x_value),
"y": list(map(str, y_value)),
}
@app.route("/background", methods=["POST"])
def background() -> dict[bool, list[float], list[float]]:
# put incoming JSON into a dictionary
data = json.loads(request.data)
# verify user input is valid
if not param_check(data):
return {
"success": False,
"text": "One of the given parameters was invalid. Please change some settings and try again.",
}
# perform:
# --> transmission spectrum of gas sample (calc_spectrum)
spectrum, error, message = generate_spectrum(data)
if error:
return {
"success": False,
"text": message,
}
# perform:
# --> set all y-values to one
try:
background_spectrum = generate_background(spectrum)
except:
return {
"success": False,
"text": "There was an issue collecting the background spectra."
}
# perform:
# --> blackbody spectrum of source (sPlanck)
# --> transmission spectrum of beamsplitter and cell windows
# --> detector response spectrum
processed_spectrum = process_spectrum(data, background_spectrum)
if processed_spectrum is None:
return {
"success": False,
"text": "Issue Processing Data"
}
# https://radis.readthedocs.io/en/latest/source/radis.spectrum.spectrum.html#radis.spectrum.spectrum.Spectrum.get
x_value, y_value = processed_spectrum.get("transmittance_noslit")
# convert dictionary values to strings and return as JSON
return {
"success": True,
"x": list(x_value),
"y": list(map(str, y_value)),
}
@app.route("/find_peaks", methods=["POST"])
def handle_peaks() -> dict[bool, dict[float, float], str]:
data = json.loads(request.data)
peaks, error = find_peaks(
data["x"],
data["y"],
float(data["threshold"]),
)
if (peaks):
return {"success": True, "peaks": peaks, "text": error}
return {"success": False, "peaks": peaks, "text": error}
# set debug to false in production environment
if __name__ == "__main__":
app.run(host="0.0.0.0")