Example #1
0
    def test_lambda(self):
        '''
        Test Lambda function on AWS
        '''
        # prepare input
        json_list_list_x = prepare_to_lambda(dataset)
        # REQUEST
        resp = requests.post(URL_PREDICT, json=json_list_list_x)
        #print("status code : ", resp.status_code)
        #print(resp.json())
        y_multi_pred_out = retrieve_from_lambda(resp)
        # Assert if the result of TFLite model is consistent with the TF model.
        np.testing.assert_almost_equal(y_multi_pred,
                                       y_multi_pred_out,
                                       decimal=3)
        print("Done. PAST DAYS : TensorFlow and TensorFlow Lite matches.")

        # prepare input
        json_list_list_x = prepare_to_lambda_future(dataset)
        # REQUEST
        resp = requests.post(URL_PREDICT, json=json_list_list_x)
        y_future_pred_out = retrieve_from_lambda(resp)
        # Assert if the result of TFLite model is consistent with the TF model.
        np.testing.assert_almost_equal(y_future_pred,
                                       y_future_pred_out,
                                       decimal=3)
        print("Done. FUTURE DAYS : TensorFlow and TensorFlow Lite matches.")
Example #2
0
    def test_lambda_interface(self):
        '''
        Test interface with lambda AWS (IN/OUT)
        '''
        # test past days

        json_list_list_x = prepare_to_lambda(dataset)
        # simulate input to lambda (double dumps ? why ? i don't know yet)
        json_list_list_x = json.dumps(json_list_list_x)
        # simulate lambda
        event = {"body": json_list_list_x}
        # lambda
        context = None
        response = predict(event, context)
        # Retrieve from lambda in App code
        y_multi_pred_out = retrieve_from_lambda(response)
        # Assert if the result of TFLite model is consistent with the TF model.
        np.testing.assert_almost_equal(y_multi_pred,
                                       y_multi_pred_out,
                                       decimal=3)
        print("Done. PAST DAYS : TensorFlow and TensorFlow Lite matches.")

        # test future days

        json_list_list_x = prepare_to_lambda_future(dataset)
        json_list_list_x = json.dumps(
            json_list_list_x)  # dumps again : I dont know why
        event = {"body": json_list_list_x}
        context = None
        response = predict(event, context)
        y_future_pred_out = retrieve_from_lambda(response)
        # Assert if the result of TFLite model is consistent with the TF model.
        np.testing.assert_almost_equal(y_future_pred,
                                       y_future_pred_out,
                                       decimal=3)
        print("Done. FUTURE DAYS : TensorFlow and TensorFlow Lite matches.")
Example #3
0
def update_pred_pos_kr(df_feat_kr, from_disk=False):
    '''
    Update prediction data positive cases France
    '''

    # check if last prediction is after last known date
    if os.path.isfile(PATH_DF_PLOT_PRED_KR):
        df_plot_pred = pd.read_csv(PATH_DF_PLOT_PRED_KR)
        df_plot_pred.index = df_plot_pred["date"]
        if df_plot_pred["date"].min() <= df_feat_kr["date"].max():
            from_disk = False
    else:
        from_disk = False

    # if no prediction or if from disk
    if (not settings.PREDICT) | from_disk:
        df_plot_pred = pd.read_csv(PATH_DF_PLOT_PRED_KR)
        df_plot_pred.index = df_plot_pred["date"]
        return df_plot_pred

    # prepare features
    dataset, data_std, data_mean = prepare_dataset_kr(df_feat_kr)
    # predict next days
    if settings.MODEL_TFLITE:
        json_list_list_x = prepare_to_lambda_future(dataset)
        resp = requests.post(URL_PREDICT_KR, json=json_list_list_x)
        print("status code : ", resp.status_code)
        if resp.status_code == 200:
            y_multi_pred = retrieve_from_lambda(resp)
        else:
            print("AWS Lamdba future pred ERROR!")
            df_plot_pred = pd.read_csv(PATH_DF_PLOT_PRED_KR)
            df_plot_pred.index = df_plot_pred["date"]
            return df_plot_pred
    else:
        # prepare data : very last days
        x_multi = np.array([dataset[-PAST_HISTORY:, :]])
        # load model
        multi_step_model = tf.keras.models.load_model(PATH_MDL_MULTI_STEP_KR)
        y_multi_pred = multi_step_model.predict(x_multi)

    # convert in positive cases
    y_pos_pred = y_multi_pred * data_std[4] + data_mean[4]
    # pos pred next 3 days from last day : date, pos, total (sum)
    str_date_pred_0 = df_feat_kr.date.max()
    str_date_pred_1 = add_days(str_date_pred_0, FUTURE_TARGET)
    list_dates_pred = generate_list_dates(str_date_pred_0, str_date_pred_1)
    # figure
    df_plot_pred = pd.DataFrame(index=list_dates_pred,
                                columns=["date"],
                                data=list_dates_pred)

    df_plot_pred["pos"] = y_pos_pred[0].astype(int)

    arr_nb_pred = df_plot_pred["pos"].cumsum().values
    df_plot_pred["nb_cases"] = df_feat_kr["nb_cases"].max() + arr_nb_pred

    # save for future pred
    df_plot_pred.to_csv(PATH_DF_PLOT_PRED_KR, index=False)

    return df_plot_pred