def predict_point(self, data, method=None, sample_count=const.DEFAULT_SAMPLE_COUNT): x_val = data x_val[0] = self.encoder.transform([x_val[0]])[0] x_val = self.scaler.transform([x_val]) self.x_shared.set_value(x_val) if method == "NS": prediction = self.predict_gp() formatter(prediction[0]) return prediction[0] else: prediction = self.predict(sample_count=sample_count) return prediction
def point_prediction(): if request.method == "POST": poly_regressor = BayesianPolynomialRegressor() data = request.get_json() method = const.DEFAULT_METHOD sample_count = const.DEFAULT_SAMPLE_COUNT try: scenario = data['scenario'].capitalize(); concurrency = data['concurrency'] message_size = data['message_size'] if not (json_value_validator(scenario=scenario,concurrency=concurrency,message_size=message_size, type="point_pred")): return Response(status=const.HTTP_405_METHOD_NOT_ALLOWED) except Exception as e: print(e) return Response(status=const.HTTP_422_UNPROCESSABLE_ENTITY) if "method" in data: if data['method'] == "sampling": method = "sampling" try: if not (data.get('sample_count') is None): sample_count = data['sample_count'] if not (json_value_validator(sample_count=sample_count, type="sampling_check")): return Response(status=const.HTTP_405_METHOD_NOT_ALLOWED) except Exception as e: print(e) return Response(status=const.HTTP_422_UNPROCESSABLE_ENTITY) elif data['method'] == "NS": method = "NS" try: prediction = poly_regressor.predict_point([scenario, concurrency, message_size], method=method, sample_count=sample_count); tps,latency = formatter(tps=prediction, concurrency=concurrency); print("Result Returned") return jsonify( tps=tps, latency=latency ) except Exception as e: print(e) print("Error") return Response(status=const.HTTP_422_UNPROCESSABLE_ENTITY)
def point_prediction(): """ Return Latency and TPS for a given scenario, message size and concurrency :return TPS and Little's law Latency with HTTP 200: """ if request.method == "POST": method = const.DEFAULT_METHOD sample_count = const.DEFAULT_SAMPLE_COUNT data = request.get_json() if data is None: logger.error("Payload is missing.") return jsonify({"error": "Payload is missing"}), \ const.HTTP_400_BAD_REQUEST try: scenario = data.get(const.SCENARIO) concurrency = data.get(const.CONCURRENCY) message_size = data.get(const.MESSAGE_SIZE) is_valid, error = json_value_validator(scenario=scenario, concurrency=concurrency, message_size=message_size, type="point_pred") if not is_valid: logger.error("Invalid values in JSON request: " "constraint violation: point_pred: " + error) return jsonify({"error": "Invalid Request: "+error}), \ const.HTTP_422_UNPROCESSABLE_ENTITY except Exception as e: logger.exception( "Uncaught exception occurred " "in request validation: ", e) return jsonify({ "error": "Uncaught exception occurred in request " "validation" }), const.HTTP_422_UNPROCESSABLE_ENTITY if const.METHOD in data: if data[const.METHOD] == const.SAMPLING: method = const.SAMPLING try: if not (data.get(const.SAMPLE_COUNT) is None): sample_count = data[const.SAMPLE_COUNT] is_valid, error = json_value_validator( sample_count=sample_count, type="sampling_check") if not is_valid: logger.error("Invalid values in JSON request: " "constraint violation for " "sample_count: point_pred " + error) return jsonify({ "error": "Invalid Request: "+error }), \ const.HTTP_422_UNPROCESSABLE_ENTITY except Exception as e: logger.exception( "Uncaught exception occurred " "in request validation block: ", e) return jsonify({"error": "Uncaught exception " "occurred in request validation"}), \ const.HTTP_422_UNPROCESSABLE_ENTITY try: prediction = get_regressor().predict_point( [scenario.capitalize(), concurrency, message_size], method=method, sample_count=sample_count) tps, latency = formatter(tps=prediction, concurrency=concurrency) # Clear PyMC3 cache memoize.clear_cache() return jsonify(tps=tps, latency=latency) except Exception as e: logger.exception("ML Model Error : point_pred: ", e) return jsonify({"error": "Error during prediction " "or post processing"}), \ const.HTTP_500_INTERNAL_SERVER_ERROR else: return Response(status=const.HTTP_405_METHOD_NOT_ALLOWED)
def test_response_formatter_tps_latency(self): tps, latency = formatter(1321.32132433234, 50); self.assertEqual(1321.32, tps) self.assertEqual(37.84, latency)
def test_response_formatter_tps_latency_negetive(self): tps, latency = formatter(-1, 5); self.assertEqual(2.220446049250313e-16, tps) self.assertEqual(2.251799813685248e+19, latency)
def test_response_formatter_tps_negetive(self): tps = formatter(-1); self.assertEqual(2.220446049250313e-16, tps)