-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
288 lines (238 loc) · 10 KB
/
application.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from flask import Flask
from flask_cors import CORS, cross_origin
from flasgger import Swagger
from flasgger.utils import swag_from
from quickanalysis.utils.useful import NumpyEncoder
application = app = Flask(__name__)
Swagger(app)
app.config['CORS_HEADERS'] = 'Content-Type'
app.json_encoder = NumpyEncoder
from flask import Flask, request, jsonify, render_template, render_template_string
import json
from astropy.io import fits
from marshmallow import Schema, fields, ValidationError, validates_schema
from quickanalysis.utils.load_data import check_if_s3_image_exists
from quickanalysis.utils.load_data import get_image_data
from quickanalysis.utils.load_data import get_subregion_rect
from quickanalysis.analysis.profile_line import get_intensity_profile
from quickanalysis.analysis.profile_line import get_intensity_profile_input_plot
from quickanalysis.analysis.histogram import get_histogram
from quickanalysis.analysis.region_stats import get_mode, get_median, get_mean, get_min, get_max, get_std, get_median_abs_deviation
class LineProfileInput(Schema):
"""Parse and validate input for the line profile endpoint."""
full_filename = fields.Str(required=True)
s3_directory = fields.Str(required=True)
start = fields.Dict(keys=fields.Str(), values=fields.Float(), required=True)
end = fields.Dict(keys=fields.Str(), values=fields.Float(), required=True)
@validates_schema(skip_on_field_errors=True)
def validate_catalog(self, data, **kwargs):
point_coords = [
data['start']['x'],
data['start']['y'],
data['end']['x'],
data['end']['y']
]
print(point_coords)
for val in point_coords:
if val < 0 or val > 1:
raise ValidationError(
'Input coordinates must be between 0 and 1')
@app.route('/', methods=['GET', 'POST'])
def home():
return jsonify({"data":"welcome"})
@app.route("/lineprofiledisplay", methods=["GET"])
def plotView():
"""This is a route to visualize the line requested for the line profile.
Args:
x0 (float): 'x' value for the line start point
x1 (float): 'x' value for the line end point
y0 (float): 'y' value for the line start point
y1 (float): 'y' value for the line end point
filename (str): Photon Ranch filename in S3, including the extension.
s3_directory (str): The 'folder' that the image resides in s3. [ data | info-images | allsky ]
Returns:
Str: PNG of line intensity plot represented by a base 64 string
"""
filename = request.args.get('filename')
s3_directory = request.args.get('s3_directory')
x0 = float(request.args.get('x0'))
x1 = float(request.args.get('x1'))
y0 = float(request.args.get('y0'))
y1 = float(request.args.get('y1'))
data = get_image_data(filename, s3_directory)
start = (x0, y0)
end = (x1, y1)
profile = get_intensity_profile(data, start, end)
selection_plot = get_intensity_profile_input_plot(data, start, end)
return render_template_string("<img src='{{ image }}'/><div>{{data}}</div>", image=selection_plot, data=profile)
@app.route('/lineprofile', methods=['POST'])
@cross_origin()
def lineprofile():
"""Return a line profile.
Args:
start (dict): 'x' and 'y' values for the line start point, in [0, 1]
end (dict): Same as start
full_filename (str): Photon Ranch filename in S3, including the extension.
s3_directory (str): The 'folder' that the image resides in s3. [ data | info-images | allsky ]
Returns:
success (boolean): Successful line profile
start (dict): Same as request body
end (dict): Same as request body
data (list): List of intensity values between start and end point
Example post request:
curl -X POST http://localhost:5000/lineprofile -F \
'data={"start":{"x": 0, "y":0}, "end": {"x": 1, "y": 1},
"full_filename": "tst-test-20201112-00000058-EX01.fits.bz2", "s3_directory": "data"}'
"""
try:
# Validate and parse args
args = LineProfileInput().load(json.loads(request.data))
start = (args['start']['x'], args['start']['y'])
end = (args['end']['x'], args['end']['y'])
full_filename = args['full_filename']
s3_directory = args['s3_directory']
# Make sure the requested file exists
if not check_if_s3_image_exists(full_filename, s3_directory):
return jsonify({
"success": False,
"message": f"Image does not exist: {s3_directory}/{full_filename}."
}), 400
# Get the image data and compute a line profile
data = get_image_data(full_filename, s3_directory)
profile = get_intensity_profile(data, start, end)
response = jsonify({
"success": True,
"start": start,
"end": end,
"data": profile,
})
return response
except ValidationError as e:
return jsonify({
"success": False,
"message": f"Validation error: {str(e)}",
}), 400
except Exception as e:
return jsonify({
"success": False,
"message": f"Error: {str(e)}",
}), 500
@app.route('/statistics', methods=['POST'])
@cross_origin()
@swag_from('endpoint_docs/image_statistics.yml')
def image_statistics():
"""Return statistics for an image or subregion.
Args:
subregion (dict): Optional description (in range [0,1]) of the subregion
subregion['x0'] (float): 'x' value for the rectangle left edge
subregion['x1'] (float): 'x' value for the rectangle right edge
subregion['y0'] (float): 'y' value for the rectangle top edge
subregion['y1'] (float): 'y' value for the rectangle bottom edge
full_filename (str): Photon Ranch filename in S3, including the extension.
s3_directory (str): The 'folder' that the image resides in s3. [ data | info-images | allsky ]
Example Response:
success (bool): True,
stats (dict): {
"median": 158,
"mean": 176.539,
"mode": 155,
"min": 51,
"max": 64963,
"std": 218.401,
"median_abs_deviation": 12,
}
params: json.loads(request.data)
}
"""
# TODO: validation
args = json.loads(request.data)
full_filename = args['full_filename']
s3_directory = args['s3_directory']
# Make sure the requested file exists
if not check_if_s3_image_exists(full_filename, s3_directory):
return jsonify({
"success": False,
"message": f"Image does not exist: {s3_directory}/{full_filename}."
}), 400
image_data = get_image_data(full_filename, s3_directory)
if 'subregion' in args.keys():
coords = args['subregion']
image_data = get_subregion_rect(image_data, coords['x0'], coords['x1'], coords['y0'], coords['y1'] )
stats = {
"median": get_median(image_data),
"mean": get_mean(image_data),
"mode": get_mode(image_data),
"min": get_min(image_data),
"max": get_max(image_data),
"std": get_std(image_data),
"median_abs_deviation": get_median_abs_deviation(image_data),
}
return jsonify({
"success": True,
"stats": stats,
"params": json.loads(request.data)
}), 200
@app.route('/histogram-clipped', methods=['POST'])
@cross_origin()
@swag_from('endpoint_docs/histogram.yml')
def histogram():
"""Return statistics for an image or subregion.
POST Args:
full_filename (str): full file name for analysis, including the file extensions.
s3_directory (str): the 'folder' that the image resides in s3. [ data | info-images | allsky ]
clip_percent (float): percentile value of intensity to define min and max range of histogram
subregion (dict): optional, analyze subregion of image
subregion['shape'] (str): type of shape. Currently only supports 'rect'.
subregion['x0'] (float): 'x' value for the rectangle left edge
subregion['x1'] (float): 'x' value for the rectangle right edge
subregion['y0'] (float): 'y' value for the rectangle top edge
subregion['y1'] (float): 'y' value for the rectangle bottom edge
Example Response:
"success": True,
"histogram": {
"edges": [106, 107, ... 1405],
"counts": [415, 438, ....4152],
"stats": {
"median": 158,
"mean": 176.539,
"mode": 155,
"min": 51,
"max": 64963,
}
},
"params": json.loads(request.data)
"""
# TODO: input documentation and validation
args = json.loads(request.data)
full_filename = args['full_filename']
s3_directory = args['s3_directory']
clip_percent = args['clip_percent']
print(full_filename, clip_percent)
# Make sure the requested file exists
if not check_if_s3_image_exists(full_filename, s3_directory):
return jsonify({
"success": False,
"message": f"Image does not exist: {s3_directory}/{full_filename}."
}), 400
image_data = get_image_data(full_filename, s3_directory)
if 'subregion' in args.keys():
coords = args['subregion']
image_data = get_subregion_rect(image_data, coords['x0'], coords['x1'], coords['y0'], coords['y1'] )
counts, edges = get_histogram(image_data, clip_percent=clip_percent)
return jsonify({
"success": True,
"histogram": {
"counts": counts,
"edges": edges,
"stats": {
"median": get_median(image_data),
"mean": get_mean(image_data),
"mode": get_mode(image_data),
"min": get_min(image_data),
"max": get_max(image_data),
}
},
"params": json.loads(request.data)
}), 200
if __name__ == "__main__":
application.run()