-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
597 lines (466 loc) · 19.8 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
import json
import os
import traceback
from datetime import timedelta, datetime
from flask import Flask, render_template, request, session
from imgurpython import ImgurClient
from passlib.hash import sha256_crypt
from sqlalchemy import exc, orm
import src.fileFormat as fileFormat
import src.scanpathUtils as spUtil
from src.config import config
from src.database import db_session
from src.models.Dataset import Dataset
from src.models.DatasetTask import DatasetTask
from src.models.User import User
from src.scanpathAlgs import sta, emine, dotplot
# App configuration
app = Flask(__name__)
app.secret_key = config['FLASK_SECRET_KEY']
app.debug = False
# For uploading & retrieving user-uploaded images
imgur_client = ImgurClient(config['IMGUR_CLIENT_ID'], config['IMGUR_CLIENT_SECRET'])
# For mocking sessions etc.
dev_mode = False
@app.before_request
def make_session_permanent():
session.permanent = True
app.permanent_session_lifetime = timedelta(hours=12)
def is_user_logged_in():
return dev_mode or ('user' in session)
def is_user_authorized(user_id):
return dev_mode or (is_user_logged_in() and (session['user'] == user_id))
# Response methods
def handle_error(message='Internal database error - try again later.'):
return json.dumps({
'success': False,
'message': message
})
def handle_success(load=None):
return json.dumps({
'success': True,
'load': load if load is not None else ''
})
def handle_unauthorized():
return handle_error('Unauthorized access')
# Routing methods
@app.route('/')
def redirect_index():
return render_template('index.html')
@app.route('/api/user/auth', methods=['POST'])
def authenticate():
# Process the request - verify required info
try:
json_data = json.loads(request.data)
email = json_data['email']
password = json_data['password']
except KeyError:
return handle_error('Failed to login - user auth data invalid.')
# Query the database for an user matching the info from the request
try:
user = db_session.query(User).filter(User.email == email).one()
if sha256_crypt.verify(password, user.password):
session['user'] = user.id
return handle_success({
'id': user.id
})
else:
return handle_error('Invalid user credentials - try again.')
except orm.exc.NoResultFound:
return handle_error('Invalid user credentials - try again.')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/user/logout', methods=['POST'])
def logout_user():
# Drop the current session and notify the user
session.pop('user', None)
session.clear()
return handle_success()
@app.route('/api/user/add', methods=['POST'])
def add_user():
try:
json_data = json.loads(request.data)
# Back-end password validation
if len(json_data['password']) < config['MIN_PASSWORD_LEN']:
return handle_error('Password too short - enter at least ' + config['MIN_PASSWORD_LEN'] + ' characters.')
user = User(password=sha256_crypt.hash(json_data['password']), email=json_data['email'],
name=json_data['name'], surname=json_data['surname'])
except KeyError:
return handle_error('Required user attributes are missing')
try:
# Commit DB changes
db_session.add(user)
db_session.commit()
return handle_success()
except exc.IntegrityError:
db_session.rollback()
return handle_error('Integrity error: e-mail address is already taken.')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/user/get_data_tree', methods=['GET'])
def get_data_tree():
""" Get the dataset-task tree structure available to the current user ID which is passed in as parameter """
try:
user_id = request.args.get('userId', type=int)
if not is_user_authorized(user_id):
return handle_unauthorized()
user = db_session.query(User).filter(User.id == user_id).one()
except orm.exc.NoResultFound:
return handle_error('Unknown user ID')
try:
return user.get_data_tree_json()
except:
traceback.print_exc()
return handle_error('Failed to obtain user data tree structure')
@app.route('/api/dataset', methods=['GET'])
def get_dataset():
if not is_user_logged_in():
return handle_unauthorized()
try:
dataset_id = request.args.get('datasetId', type=int)
dataset = db_session.query(Dataset).filter(Dataset.id == dataset_id).one()
if not is_user_authorized(dataset.user_id):
return handle_unauthorized()
else:
return handle_success(dataset.to_json())
except orm.exc.NoResultFound:
return handle_error('Invalid dataset ID.')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/dataset', methods=['POST'])
def add_dataset():
""" Creates a new dataset instance owned by the currently logged in user """
try:
json_data = json.loads(request.data)
user_id = json_data['userId']
# Check authorization & validate attribute values
if not is_user_authorized(user_id):
return handle_unauthorized()
elif len(json_data['name']) == 0:
raise KeyError
dataset = Dataset(
name=json_data['name'], description=json_data['description'], user_id=user_id,
accuracy_degree=json_data['recEnvironment'].get('accDegree'),
screen_size=json_data['recEnvironment'].get('screenSize'),
screen_res_x=json_data['recEnvironment'].get('screenResX'),
screen_res_y=json_data['recEnvironment'].get('screenResY'),
tracker_distance=json_data['recEnvironment'].get('trackerDistance')
)
# Commit DB changes
db_session.add(dataset)
db_session.commit()
# Reflect the changes on the server side - create a new folder named after dataset PK
os.makedirs(os.path.join(config['DATASET_FOLDER'], config['DATASET_PREFIX'] + str(dataset.id)))
return handle_success({
'id': dataset.id
})
except KeyError:
return handle_error('Required attributes are missing')
except OSError:
return handle_error('Error while creating backend dataset hierarchy')
except orm.exc.NoResultFound:
return handle_error('Invalid user credentials - try logging in again.')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/dataset', methods=['PUT'])
def edit_dataset():
""" Updates an existing dataset instance, if owned by the currently logged in user """
try:
json_data = json.loads(request.data)
dataset = db_session.query(Dataset).filter(Dataset.id == json_data['id']).one()
# Check authorization & validate attribute values
if not is_user_authorized(dataset.user_id):
return handle_unauthorized()
elif len(json_data['name']) == 0:
raise KeyError
# Update all attributes of the mapped object
dataset.name = json_data['name']
dataset.date_updated = datetime.now()
dataset.description = json_data['description']
dataset.accuracy_degree = json_data['recEnvironment'].get('accDegree')
dataset.screen_size = json_data['recEnvironment'].get('screenSize')
dataset.screen_res_x = json_data['recEnvironment'].get('screenResX')
dataset.screen_res_y = json_data['recEnvironment'].get('screenResY')
dataset.tracker_distance = json_data['recEnvironment'].get('trackerDistance')
# Commit DB changes
db_session.commit()
return handle_success()
except KeyError:
return handle_error('Required attributes are missing')
except orm.exc.NoResultFound:
return handle_error('Invalid user credentials - try logging in again.')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/dataset', methods=['DELETE'])
def del_dataset():
""" Deletes an existing dataset instance, if owned by the currently logged in user"""
try:
json_data = json.loads(request.data)
dataset_id = json_data['datasetId']
dataset = db_session.query(Dataset).filter(Dataset.id == dataset_id).one()
# Only proceed, if the dataset to be removed belongs to the request owner
if not is_user_authorized(dataset.user_id):
return handle_unauthorized()
# Remove the dataset visuals from static folder
fileFormat.silent_dir_remove(os.path.join(
'static', 'images', 'datasets',
config['DATASET_PREFIX'] + str(dataset_id)),
)
# Commit changes to the DB
for task in dataset.tasks:
db_session.delete(task)
db_session.delete(dataset)
db_session.commit()
return handle_success()
except KeyError:
return handle_error('Required attributes are missing')
except orm.exc.NoResultFound:
return handle_error('Incorrect dataset ID')
except:
return handle_error()
@app.route('/api/task', methods=['GET'])
def get_dataset_task():
""" Returns JSON formatted task data (individual scanpaths, visuals, similarities etc.) """
if not is_user_logged_in():
return handle_unauthorized()
# Load request arguments and return task info
try:
task_id = request.args.get('taskId', type=int)
task = db_session.query(DatasetTask).filter(DatasetTask.id == task_id).one()
task.load_data()
# Check if the task belongs to a dataset owned by the user ID specified in the GET request
if not is_user_authorized(task.dataset.user_id):
return handle_unauthorized()
else:
# Return data formatted as expected by the frontend
return handle_success({
'name': task.name,
'scanpaths': task.scanpath_data_formatted,
'visuals': task.visuals,
'aois': task.aoi_data,
'datasetName': task.dataset.name
})
except orm.exc.NoResultFound:
return handle_error('Incorrect task ID')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/task', methods=['POST'])
def add_dataset_task():
if not is_user_logged_in():
return handle_unauthorized()
try:
# Parse the non-file form data (user inputs)
json_data = request.form.to_dict()
# Multipart forms don't support nested objects - therefore the retarded key names
file_scanpaths = request.files['files[fileScanpaths]']
file_regions = request.files['files[fileRegions]']
file_bg_image = request.files['files[fileBgImage]']
# Find parent dataset and verify its owner
dataset = db_session.query(Dataset).filter(Dataset.id == int(json_data['datasetId'])).one()
# Check authorization & validate attribute values
if not is_user_authorized(dataset.user_id):
return handle_unauthorized()
elif len(json_data['name']) == 0:
raise KeyError
keep_cols = ['ParticipantName', 'FixationIndex', 'GazeEventType', 'GazeEventDuration',
'FixationPointX (MCSpx)', 'FixationPointY (MCSpx)']
# Create a new empty task instance
task = DatasetTask(name=json_data['name'],
url=json_data['url'] if 'url' in json_data else None,
description=json_data['description'],
dataset_id=dataset.id)
# Get relevant data from the scanpaths input file
task.scanpath_data_raw = fileFormat.process_scanpaths(file_scanpaths, keep_cols, 100, task.url)
# Get relevant data from the AOIs input file
task.aoi_data = fileFormat.process_aois(file_regions)
# Additional data to be saved in the DB along with the previously processed raw scanpath data
raw_sequences = spUtil.get_raw_sequences(task)
task.scanpath_data_formatted = spUtil.get_formatted_sequences(raw_sequences)
# Commit DB changes
dataset.tasks.append(task)
db_session.commit()
# Create a folder for this tasks static images
fileFormat.create_task_img_folder(dataset, task, file_bg_image)
return handle_success({
'id': task.id
})
except KeyError:
traceback.print_exc()
return handle_error('Required attributes are missing')
except orm.exc.NoResultFound:
return handle_error('Parent dataset ID not found - create one first')
except ValueError:
traceback.print_exc()
return handle_error('Failed to parse the submitted data format.')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/task', methods=['PUT'])
def edit_task():
""" Updates an existing dataset instance, if owned by the currently logged in user """
try:
json_data = json.loads(request.data)
task = db_session.query(DatasetTask).filter(DatasetTask.id == json_data['id']).one()
# Check authorization & validate attribute values
if not is_user_authorized(task.dataset.user_id):
return handle_unauthorized()
elif len(json_data['name']) == 0:
raise KeyError
# Update all attributes of the mapped object
task.name = json_data['name']
task.date_updated = datetime.now()
task.description = json_data['description']
# Commit DB changes
db_session.commit()
return handle_success()
except KeyError:
return handle_error('Required attributes are missing')
except orm.exc.NoResultFound:
return handle_error('Invalid user credentials - try logging in again.')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/task', methods=['DELETE'])
def del_dataset_task():
if not is_user_logged_in():
return handle_unauthorized()
try:
json_data = json.loads(request.data)
task = db_session.query(DatasetTask).filter(DatasetTask.id == json_data['taskId']).one()
if not is_user_authorized(task.dataset.user_id):
return handle_unauthorized()
# Remove the dataset visuals from static folder
fileFormat.silent_dir_remove(os.path.join(
'static', 'images', 'datasets',
config['DATASET_PREFIX'] + str(task.dataset_id),
config['TASK_PREFIX'] + str(task.id))
)
# Commit changes to the DB
db_session.delete(task)
db_session.commit()
return handle_success()
except KeyError:
return handle_error('Task attributes are missing')
except orm.exc.NoResultFound:
return handle_error('Incorrect task ID')
except:
return handle_error()
@app.route('/api/scanpath/custom', methods=['POST'])
def get_similarity_to_custom():
# TODO consider fixations length [A, B] -> fixation == 50ms, [AAABB] = [A(150), B(100)]
if not is_user_logged_in():
return handle_unauthorized()
# Check if the request data contains the custom scanpath
try:
json_data = json.loads(request.data)
custom_scanpath = json_data['customScanpath']
task_id = json_data['taskId']
# Verify the custom scanpath formatting - only letters describing AOIs
if str(custom_scanpath).isalpha():
task = db_session.query(DatasetTask).filter(DatasetTask.id == task_id).one()
task.load_data()
task.exclude_participants(json_data['excludedScanpaths'])
return handle_success(spUtil.run_custom(task, custom_scanpath))
else:
return handle_error('Wrong custom scanpath format - alphabet characters only.')
except KeyError:
return handle_error('Custom scanpath attributes are missing.')
except orm.exc.NoResultFound:
return handle_error('Incorrect task ID')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/scanpath/sta', methods=['POST'])
def get_sta_common():
if not is_user_logged_in():
return handle_unauthorized()
# Look for the task identifier in request URL
try:
json_data = json.loads(request.data)
task_id = json_data['taskId']
# Load additional required data and perform run_sta
task = db_session.query(DatasetTask).filter(DatasetTask.id == task_id).one()
task.exclude_participants(json_data['excludedScanpaths'])
raw_sequences = spUtil.get_raw_sequences(task)
return handle_success(sta.run_sta(raw_sequences, task.aoi_data))
except KeyError:
return handle_error('Task ID is missing')
except orm.exc.NoResultFound:
return handle_error('Incorrect task ID')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/scanpath/emine', methods=['POST'])
def get_emine_common():
if not is_user_logged_in():
return handle_unauthorized()
# Look for the task identifier in request URL
try:
json_data = json.loads(request.data)
task_id = json_data['taskId']
# Load additional required data and perform run_sta
task = db_session.query(DatasetTask).filter(DatasetTask.id == task_id).one()
task.exclude_participants(json_data['excludedScanpaths'])
raw_sequences = spUtil.get_raw_sequences(task)
return handle_success(emine.run_emine(raw_sequences))
except KeyError:
return handle_error('Task ID is missing')
except orm.exc.NoResultFound:
return handle_error('Incorrect task ID')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/scanpath/dotplot', methods=['POST'])
def get_dotplot_common():
if not is_user_logged_in():
return handle_unauthorized()
# Look for the task identifier in request URL
try:
json_data = json.loads(request.data)
task_id = json_data['taskId']
# Load additional required data and perform run_sta
task = db_session.query(DatasetTask).filter(DatasetTask.id == task_id).one()
task.exclude_participants(json_data['excludedScanpaths'])
raw_sequences = spUtil.get_raw_sequences(task)
return handle_success(dotplot.run_dotplot(raw_sequences))
except KeyError:
return handle_error('Task ID is missing')
except orm.exc.NoResultFound:
return handle_error('Incorrect task ID')
except:
traceback.print_exc()
return handle_error()
@app.route('/api/scanpath/alg-compare', methods=['POST'])
def get_alg_comparison():
if not is_user_logged_in():
return handle_unauthorized()
# Look for the task identifier in request URL
try:
json_data = json.loads(request.data)
task_id = json_data['taskId']
# Load additional required data and perform run_sta
task = db_session.query(DatasetTask).filter(DatasetTask.id == task_id).one()
task.load_data()
# Load scanpaths (for passing them to the algorithms)
raw_sequences = spUtil.get_raw_sequences(task)
# Check for any excluded algorithms and push the rest into the results array
return handle_success([
sta.run_sta(raw_sequences, task.aoi_data) if 'STA' not in json_data['excludedAlgs'] else spUtil.run_empty('STA'),
dotplot.run_dotplot(raw_sequences) if 'Dotplot' not in json_data['excludedAlgs'] else spUtil.run_empty('Dotplot'),
emine.run_emine(raw_sequences) if 'eMINE' not in json_data['excludedAlgs'] else spUtil.run_empty('eMINE')
])
except KeyError:
return handle_error('Task ID is missing')
except orm.exc.NoResultFound:
return handle_error('Incorrect task ID')
except:
traceback.print_exc()
return handle_error()
if __name__ == '__main__':
app.run(host='localhost')