def save_prediction(): ''' This router function saves the prediction results generated from a computed svm or svr prediction session. During its attempt, it returns a json string, with the following value: - integer, codified indicator of save attempt: - 0, successfully stored the prediction result - 1, unsuccessfully stored the prediction result - 2, status was not 'valid' - 3, improper request submitted ''' if request.method == 'POST': # programmatic-interface if request.get_json(): results = request.get_json() data = results['data'] # web-interface: double decoder required, since nested encoding elif request.form: results = request.form data = json.loads(json.loads(results['data'])) # invalid request else: return json.dumps({'status': 3}) # local variables status = results['status'] type = results['model_type'] title = results['title'] # save prediction if status == 'valid': prediction = Prediction() result = prediction.save(data, type, title)['result'] # notification: prediction status if result == 0: return json.dumps({'status': 0}) else: return json.dumps({'status': 1}) # notification: status not valid else: return json.dumps({'status': 2})
def save_prediction(): ''' This router function saves the prediction results generated from a computed svm or svr prediction session. During its attempt, it returns a json string, with the following value: - integer, codified indicator of save attempt: - 0, successfully stored the prediction result - 1, unsuccessfully stored the prediction result - 2, status was not 'valid' - 3, improper request submitted ''' if request.method == 'POST': # programmatic-interface if request.get_json(): results = request.get_json() data = results['data'] # web-interface: double decoder required, since nested encoding elif request.form: results = request.form data = json.loads(json.loads(results['data'])) # invalid request else: return json.dumps({'status': 3}) # local variables status = results['status'] type = results['model_type'] title = results['title'] # save prediction if status == 'valid': prediction = Prediction() result = prediction.save(data, type, title)['result'] # notification: prediction status if result == 0: return json.dumps({'status': 0}) else: return json.dumps({'status': 1}) # notification: status not valid else: return json.dumps({'status': 2})
def retrieve_prediction_titles(): ''' This router function retrieves all prediction titles, stored via the 'save_prediction' router function. During its attempt, it returns a json string, with the following value: - integer, codified indicator of database query: - 0, successful retrieval of prediction titles - 1, unsuccessful retrieval of prediction titles - 2, improper request submitted - string, array of prediction titles ''' if request.method == 'POST': # programmatic-interface if request.get_json(): results = request.get_json() model_type = results['model_type'] # web-interface elif request.form: results = request.form args = json.loads(results['args']) model_type = args['model_type'] # invalid request else: return json.dumps({'status': 2}) # query database prediction = Prediction() response = prediction.get_all_titles(model_type) # return results: datetime is not serializable, without 'default' # string serializer, for incompatible objects. # if response['status']: return json.dumps({ 'status': 0, 'titles': response['result'] }, default=str) else: return json.dumps({'status': 1, 'titles': None})
def retrieve_prediction_titles(): ''' This router function retrieves all prediction titles, stored via the 'save_prediction' router function. During its attempt, it returns a json string, with the following value: - integer, codified indicator of database query: - 0, successful retrieval of prediction titles - 1, unsuccessful retrieval of prediction titles - 2, improper request submitted - string, array of prediction titles ''' if request.method == 'POST': # programmatic-interface if request.get_json(): results = request.get_json() model_type = results['model_type'] # web-interface elif request.form: results = request.form args = json.loads(results['args']) model_type = args['model_type'] # retrieve all titles else: model_type = 'all' # query database prediction = Prediction() response = prediction.get_all_titles(model_type) # return results: datetime is not serializable, without 'default' # string serializer, for incompatible objects. # if response['status']: return json.dumps({ 'status': 0, 'titles': response['result'] }, default=str) else: return json.dumps({'status': 1, 'titles': None})
def retrieve_prediction(): ''' This router function retrieves a specified prediction parameter. - integer, codified indicator of save attempt: - 0, successful retrieval of specified prediction parameter - 1, unsuccessful retrieval of specified prediction parameter - 2, improper request submitted - 3, invalid 'model_type' - string, prediction parameter ''' if request.method == 'POST': # programmatic-interface if request.get_json(): results = request.get_json() id_result = results['id_result'] # web-interface elif request.form: results = request.form id_result = json.loads(results['id_result']) # invalid request else: return json.dumps({'status': 2}) # query database and return results prediction = Prediction() result = prediction.get_result(id_result) model_type = prediction.get_model_type(id_result)['result'] if model_type == 'svm': classes = prediction.get_value(id_result, model_type, 'class') df = prediction.get_value(id_result, model_type, 'decision_function') prob = prediction.get_value(id_result, model_type, 'probability') if ( result['status'] and classes['status'] and df['status'] and prob['status'] ): # return results: queried 'decimal' database values, are not # json serializable, without using the 'default' # string serializer. # return json.dumps({ 'status': 0, 'result': result['result'], 'classes': classes['result'], 'decision_function': df['result'], 'probability': prob['result'] }, default=str) else: return json.dumps({'status': 1}) elif model_type == 'svr': coefficient = prediction.get_value(id_result, model_type, 'r2') if coefficient['status']: # return results: queried 'decimal' database values, are not # json serializable, without using the 'default' # string serializer. # return json.dumps({ 'status': 0, 'result': result['result'], 'r2': coefficient['result'] }, default=str) else: return json.dumps({'status': 1}) else: return json.dumps({'status': 3})
def retrieve_prediction(): ''' This router function retrieves a specified prediction parameter. - integer, codified indicator of save attempt: - 0, successful retrieval of specified prediction parameter - 1, unsuccessful retrieval of specified prediction parameter - 2, improper request submitted - 3, invalid 'model_type' - string, prediction parameter ''' if request.method == 'POST': # programmatic-interface if request.get_json(): results = request.get_json() id_result = results['id_result'] # web-interface elif request.form: results = request.form args = json.loads(results['args']) id_result = args['id_result'] # invalid request else: return json.dumps({'status': 2}) # query database and return results prediction = Prediction() result = prediction.get_result(id_result) model_type = prediction.get_model_type(id_result)['result'] if model_type == 'svm': classes = prediction.get_value(id_result, model_type, 'class') df = prediction.get_value(id_result, model_type, 'decision_function') prob = prediction.get_value(id_result, model_type, 'probability') if (result['status'] and classes['status'] and df['status'] and prob['status']): # return results: queried 'decimal' database values, are not # json serializable, without using the 'default' # string serializer. # return json.dumps( { 'status': 0, 'result': result['result'], 'classes': classes['result'], 'decision_function': df['result'], 'probability': prob['result'] }, default=str) else: return json.dumps({'status': 1}) elif model_type == 'svr': coefficient = prediction.get_value(id_result, model_type, 'r2') if coefficient['status']: # return results: queried 'decimal' database values, are not # json serializable, without using the 'default' # string serializer. # return json.dumps( { 'status': 0, 'result': result['result'], 'r2': coefficient['result'] }, default=str) else: return json.dumps({'status': 1}) else: return json.dumps({'status': 3})