Example #1
0
def plot_production_history_with_fit_and_predict():
    df = pd.read_csv(predict_file)
    starting_index = producer_starting_indicies[1]
    producer = producers[1][starting_index:]
    injectors_tmp = [injector[starting_index:] for injector in injectors]
    X, y = production_rate_dataset(producer, *injectors_tmp)
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.5,
                                                        shuffle=False)
    crmp = CRMP().fit(X_train, y_train)
    for i in range(len(producer_names)):
        producer_df = producer_rows_from_df(df, i + 1)
        starting_index = producer_starting_indicies[i]
        producer = producers[i][starting_index:]
        injectors_tmp = [injector[starting_index:] for injector in injectors]
        X, y = production_rate_dataset(producer, *injectors_tmp)
        X_train, X_test, y_train, y_test = train_test_split(X,
                                                            y,
                                                            test_size=0.5,
                                                            shuffle=False)
        producer_length = len(producer)
        t = np.linspace(1, producer_length, producer_length)
        train_length = len(y_train)
        train_time = t[:train_length]
        test_time = t[train_length:][1:]

        empty = []
        plt.plot(empty, empty, c='r', label='Fit')
        plt.plot(empty, empty, c='g', label='Predict')
        plt.plot(t, producer, c='k')

        for index, row in producer_df.iterrows():
            tau = row['tau_final']
            f1 = row['f1_final']
            f2 = row['f2_final']
            f3 = row['f3_final']
            f4 = row['f4_final']
            crmp.tau_ = tau
            crmp.gains_ = [f1, f2, f3, f4]

            # Fitting
            y_hat = crmp.predict(X_train)
            plt.plot(train_time, y_hat, '--', alpha=0.02, c='r', linewidth=2)

            # Prediction
            y_hat = crmp.predict(X_test)
            plt.plot(test_time, y_hat, ':', alpha=0.02, c='g', linewidth=2)

        plt.vlines(test_time[0],
                   0,
                   1.1 * max(producer),
                   linewidth=2,
                   alpha=0.8)
        plot_helper(FIG_DIR,
                    title=producer_names[i],
                    xlabel='Time [days]',
                    ylabel='Production Rate [bbls/day]',
                    legend=True,
                    save=True)
Example #2
0
def objective_function():
    X, y = production_rate_dataset(producers[0], *injectors)
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.5,
                                                        shuffle=False)
    crmp = CRMP().fit(X_train, y_train)
    for i in range(number_of_producers):
        X, y = production_rate_dataset(producers[i], *injectors)
        X_train, X_test, y_train, y_test = train_test_split(X,
                                                            y,
                                                            test_size=0.5,
                                                            shuffle=False)
        for p0 in param_grid['p0']:
            crmp.tau_ = p0[0]
            crmp.gains_ = p0[1:]
            y_hat = crmp.predict(X_test)
            r2, mse = fit_statistics(y_hat, y_test)
            objective_function_data['Producer'].append(i + 1)
            objective_function_data['tau'].append(p0[0])
            objective_function_data['f1'].append(p0[1])
            objective_function_data['f2'].append(p0[2])
            objective_function_data['r2'].append(r2)
            objective_function_data['MSE'].append(mse)

    objective_function_df = pd.DataFrame(objective_function_data)
    objective_function_df.to_csv(objective_function_file)