def test_compute_score_pass(): model = exponential_model(lower_bound=0, upper_bound=[100, 100]) model.fit(np.array([1, 2, 3]), np.array([1, 2, 8])) test_data = pd.DataFrame({"x": np.array([5])}) result = compute_score(model, test_data) expected = pd.DataFrame({"y_pred": np.array([103.300808])}) assert result.round(6).equals(expected)
def test_exponential_model_get_coeff_pass(): exp_model = exponential_model(lower_bound=0, upper_bound=[100, 100]) exp_model.fit(np.array([1, 2, 3]), np.array([1, 2, 8])) expected_pcov = np.array([[0.00822098, -0.01632878], [-0.01632878, 0.03276216]]) expected_popt = np.array([0.17081654, 1.28096207]) result_pcov = exp_model.get_coeff()["pcov"] result_popt = exp_model.get_coeff()["popt"] assert np.allclose(result_pcov, expected_pcov) assert np.allclose(result_popt, expected_popt)
def update_prediction(self): ''' with new curren value and span, update the prediction ''' try: if self.model_type == "exp": self.model = exponential_model( **cattrs.param["prediction_models"]["exponential_model"]) if self.model_type == "lstm": self.model = lstm_model( **cattrs.param["prediction_models"]["lstm_model"]) if self.model_type == "logistic": self.model = logistic_model( **cattrs.param["prediction_models"]["logistic_model"]) # write model details if self.model_type == "none": self.pred_values = [0, 0, 0, 0, 0, 0, 0, 0] self.model_details["model_name"] = "None" self.model_details["r2"] = 0.0 self.model_details["msle"] = 0.0 self.model_details["show_prediction"] = False else: # update prediction values x, y, x_test = np.array(self.keys_use), np.array( self.values_use), np.array(self.pred_keys) try: self.model.fit(x, y) self.pred_values = list(self.model.predict(x_test)) except: # if fail to fit or predict, use the last value as prediction self.pred_values = list( np.ones(x_test.shape) * self.values[-1]) # get last x day performance try: self.evaluate_model() except: # if evaluation fails, show the failure on the web app self.model_details = { "r2": "Evaluation fails", "msle": "Evaluation fails" } self.model_details["model_name"] = self.model.get_name() self.model_details["show_prediction"] = True except Exception as ex: logger.error(Exception)
def test_compute_score_pass(): with pytest.raises(Exception): model = exponential_model(lower_bound=0, upper_bound=[100, 100]) model.fit(np.array([1, 2, 3]), np.array([1, 2, 8])) test_data = pd.DataFrame({"x": np.array(["random"])}) result = compute_score(model, test_data)
def test_exponential_model_get_name_fail(): with pytest.raises(Exception): exp_model = exponential_model(lower_bound=0, upper_bound=[100, 100]) # invalid input result = exp_model.get_name("get")
def test_exponential_model_predict_fail(): with pytest.raises(Exception): exp_model = exponential_model(lower_bound=0, upper_bound=[100, 100]) # predict without fit exp_model.predict(np.array([-1]))
def test_exponential_model_fit_fail(): with pytest.raises(Exception, match=r"Wrong Input Shape"): exp_model = exponential_model(lower_bound=0, upper_bound=[100, 100]) exp_model.fit(np.array([1, 2, 3]), np.array([1, 2, 8, 9]))
def test_exponential_model_init_fail(): with pytest.raises(Exception, match=r"Wrong Input Length"): exp_model = exponential_model(lower_bound=0, upper_bound=[100, 100, 100])
def test_exponential_model_get_name_pass(): exp_model = exponential_model(lower_bound=0, upper_bound=[100, 100]) result = exp_model.get_name() expected = "Exponential Model" assert result == expected
def test_exponential_model_predict_pass(): exp_model = exponential_model(lower_bound=0, upper_bound=[100, 100]) exp_model.fit(np.array([1, 2, 3]), np.array([1, 2, 8])) result = exp_model.predict(np.array([4, 5, 6])) expected = np.array([28.694, 103.301, 371.893]) assert np.allclose(np.round(result, 3), expected)
def test_exponential_model_fit_pass(): exp_model = exponential_model(lower_bound=0, upper_bound=[100, 100]) exp_model.fit(np.array([1, 2, 3]), np.array([1, 2, 8])) expected_pcov = np.array([[0.00822098, -0.01632878], [-0.01632878, 0.03276216]]) assert np.allclose(exp_model.get_coeff()["pcov"], expected_pcov)
def test_exponential_model_init_pass(): exp_model = exponential_model(lower_bound=0, upper_bound=[100, 100]) assert (exp_model.lower_bound == 0) and (exp_model.upper_bound == [100, 100])