This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
271 lines (231 loc) · 9.19 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
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
"""
Simple application to provide freq from images of seismic.
Freq code by endolith https://gist.github.com/endolith/255291
"""
from io import BytesIO
import uuid
import base64
from flask import Flask
from flask import request, jsonify, render_template
import requests
import numpy as np
from PIL import Image
import geophysics
from segy import write_segy
import utils
from errors import InvalidUsage
application = Flask(__name__)
@application.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
#
# Seismic frequency and SEGY bot
#
@application.route('/freq')
def freq():
# Params from inputs.
url = request.args.get('url')
b64 = request.args.get('image')
method = request.args.get('method') or 'xing'
avg = request.args.get('avg') or 'mean'
region = request.args.get('region')
ntraces = request.args.get('ntraces') or '10'
trace_spacing = request.args.get('trace_spacing') or 'regular'
bins = request.args.get('bins') or '11'
t_min = request.args.get('tmin') or '0'
t_max = request.args.get('tmax') or '1'
dt_param = request.args.get('dt') or 'auto'
# Booleans.
spectrum = request.args.get('spectrum') or 'false'
segy = request.args.get('segy') or 'false'
nope = {i: False for i in ('none', 'false', 'no', '0')}
spectrum = nope.get(spectrum.lower(), True)
segy = nope.get(segy.lower(), True)
# Condition or generate params.
ntraces = int(ntraces)
bins = int(bins)
t_min = float(t_min)
t_max = float(t_max)
uuid1 = str(uuid.uuid1())
if region:
region = [int(n) for n in region.split(',')]
else:
region = []
# Fetch and crop image.
if url:
try:
r = requests.get(url)
im = Image.open(BytesIO(r.content))
except Exception:
payload = {'job_uuid': uuid1}
payload['parameters'] = utils.build_params(method, avg,
t_min, t_max, dt_param,
region,
trace_spacing,
url=url)
mess = 'Unable to open image from target URI.'
raise InvalidUsage(mess, status_code=410, payload=payload)
elif b64:
try:
im = Image.open(BytesIO(base64.b64decode(b64)))
except Exception:
payload = {'job_uuid': uuid1}
payload['parameters'] = utils.build_params(method, avg,
t_min, t_max, dt_param,
region,
trace_spacing,
url=url)
mess = 'Could not decode payload image. Check base64 encoding.'
raise InvalidUsage(mess, status_code=410, payload=payload)
else:
payload = {'job_uuid': uuid1}
payload['parameters'] = utils.build_params(method, avg,
t_min, t_max, dt_param,
region,
trace_spacing,
url=url)
mess = 'You must provide an image.'
raise InvalidUsage(mess, status_code=410, payload=payload)
if region:
try:
im = im.crop(region)
except Exception:
mess = 'Improper crop parameters '
raise InvalidUsage(mess, status_code=410)
width, height = im.size[0], im.size[1]
# Calculate dt and interpolate if necessary.
if dt_param[:4].lower() == 'orig':
dt = (t_max - t_min) / (height - 1)
else:
if dt_param[:4].lower() == 'auto':
dts = [0.0005, 0.001, 0.002, 0.004, 0.008]
for dt in sorted(dts, reverse=True):
target = int(1 + (t_max - t_min) / dt)
# Accept the first one that is larger than the current height.
if target >= height:
break # dt and target are set
else:
dt = float(dt_param)
target = int((t_max - t_min) / dt)
# If dt is not orig, we need to inpterpolate.
im = im.resize((width, target), Image.ANTIALIAS)
# Set up the image.
grey = geophysics.is_greyscale(im)
i = np.asarray(im) - 128
i = i.astype(np.int8)
if (not grey) and (i.ndim == 3):
r, g, b = i[..., 0], i[..., 1], i[..., 2]
i = np.sqrt(0.299 * r**2. + 0.587 * g**2. + 0.114 * b**2.)
elif i.ndim == 3:
i = i[..., 0]
else:
i = i
# Get SEGY file link, if requested.
if segy:
try:
databytes = BytesIO()
write_segy(i, databytes, dt, t_min)
databytes.seek(0)
except:
print('Write SEGY failed')
else:
file_link = utils.get_url(databytes, uuid1)
# Do analysis.
print("Starting analysis")
m = {'auto': geophysics.freq_from_autocorr,
'fft': geophysics.freq_from_fft,
'xing': geophysics.freq_from_crossings}
traces = geophysics.get_trace_indices(i.shape[1], ntraces, trace_spacing)
specs, f_list, p_list, snr_list, mis, mas = geophysics.analyse(i,
t_min,
t_max,
traces,
m[method])
print("Finished analysis")
# Compute statistics.
print("***** f_list:", f_list)
fsd, psd = np.nanstd(f_list), np.nanstd(p_list)
fn, pn = len(f_list), len(p_list)
if avg.lower() == 'trim' and fn > 4:
f = geophysics.trim_mean(f_list, 0.2)
if np.isnan(f):
f = 0
elif avg.lower() == 'mean' or (avg == 'trim' and fn <= 4):
f = np.nanmean(f_list)
else:
mess = 'avg parameter must be trim or mean'
raise InvalidUsage(mess, status_code=410)
if avg.lower() == 'trim' and pn > 4:
p = geophysics.trim_mean(p_list, 0.2)
elif avg.lower() == 'mean' or (avg == 'trim' and pn <= 4):
p = np.nanmean(p_list)
else:
mess = 'avg parameter must be trim or mean'
raise InvalidUsage(mess, status_code=410)
snrsd = np.nanstd(snr_list)
snr = np.nanmean(snr_list)
# Spectrum.
print("Starting spectrum")
try:
spec = np.nanmean(np.dstack(specs), axis=-1)
fs = i.shape[0] / (t_max - t_min)
freq = np.fft.rfftfreq(i.shape[0], 1/fs)
f_min = np.amin(mis)
f_max = np.amax(mas)
except:
print("Failed spectrum")
# Probably the image is not greyscale.
payload = {'job_uuid': uuid1}
payload['parameters'] = utils.build_params(method, avg,
t_min, t_max, dt_param,
region,
trace_spacing,
url=url)
mess = 'Analysis error. Probably the colorbar is not greyscale.'
raise InvalidUsage(mess, status_code=410, payload=payload)
# Histogram.
if bins:
hist = np.histogram(i, bins=bins)
else:
hist = None
# Construct the result and return.
result = {'job_uuid': uuid1}
result['status'] = 'success'
result['message'] = ''
result['result'] = {}
result['result']['freq'] = {'peak': np.round(f, 2),
'sd': np.round(fsd, 2),
'n': fn,
'min': np.round(f_min, 2),
'max': np.round(f_max, 2)}
result['result']['phase'] = {'avg': np.round(p, 2),
'sd': np.round(psd, 2),
'n': pn}
result['result']['snr'] = {'avg': np.round(snr, 2),
'sd': np.round(snrsd, 2)}
result['result']['greyscale'] = grey
result['result']['dt'] = dt
result['result']['img_size'] = {'original_height': height,
'width': width,
'resampled_height': target}
if segy:
result['result']['segy'] = file_link
if spectrum:
result['result']['spectrum'] = spec.tolist()
result['result']['frequencies'] = freq.tolist()
if hist:
result['result']['histogram'] = {'counts': hist[0].tolist(),
'bins': hist[1].tolist()
}
result['parameters'] = utils.build_params(method, avg,
t_min, t_max, dt_param,
region,
trace_spacing,
url=url)
return jsonify(result)
@application.route('/')
def main():
return render_template('index.html',
title='Home')