|
import pickle |
|
from pathlib import Path |
|
import streamlit as st |
|
import pandas as pd |
|
import plotly.graph_objects as go |
|
from plotly.subplots import make_subplots |
|
from streamlit_plotly_events import plotly_events |
|
import streamlit as st |
|
import pandas as pd |
|
import numpy as np |
|
from sklearn import preprocessing |
|
import pywt |
|
|
|
|
|
def convert_pumping_data_to_df(uploaded_file): |
|
|
|
df = pd.read_excel(uploaded_file) |
|
|
|
try: |
|
|
|
df.iloc[:,0] = pd.to_datetime(df.iloc[:,0]) |
|
|
|
except Exception as e: |
|
st.warning(f"An error occurred while converting the date column to datetime: {e}") |
|
|
|
return df |
|
|
|
def convert_ms_to_df(uploaded_file): |
|
|
|
df = pd.read_excel(uploaded_file) |
|
|
|
try: |
|
|
|
df.iloc[:,0] = pd.to_datetime(df.iloc[:,0]) |
|
|
|
except Exception as e: |
|
st.warning(f"An error occurred while converting the date column to datetime: {e}") |
|
|
|
return df |
|
|
|
def plot_pumping_data(df, date_col, pressure_col, rate_col): |
|
fig = make_subplots(specs=[[{"secondary_y": True}]]) |
|
fig.add_trace(go.Scatter(x=df[date_col], y=df[pressure_col], mode='lines', name='Pressure', line=dict(color='blue')), secondary_y=False) |
|
fig.add_trace(go.Scatter(x=df[date_col], y=df[rate_col], mode='lines', name='Rate', line=dict(color='red')), secondary_y=True) |
|
fig.update_layout(title_text="Pumping Data") |
|
fig.update_xaxes(title_text="Date") |
|
fig.update_yaxes(title_text="Pressure", secondary_y=False) |
|
fig.update_yaxes(title_text="Rate", secondary_y=True) |
|
|
|
fig.update_layout(legend=dict( |
|
orientation="h", |
|
yanchor="bottom", |
|
y=1.02, |
|
xanchor="right", |
|
x=1 |
|
)) |
|
|
|
|
|
fig.update_yaxes(showgrid=False, secondary_y=True) |
|
|
|
|
|
|
|
|
|
return fig |
|
|
|
|
|
def calculate_cwt(df,time_col,pressure_col): |
|
|
|
|
|
|
|
df[time_col] = pd.to_datetime(df[time_col]) |
|
|
|
norm_coef2_a,period,time_data,scales_a,coef_a, freqs_a,pressure_data = continous_wavelet_transformer(df[pressure_col],df[time_col]) |
|
|
|
norm_coef2_a = norm_coef2_a.T |
|
|
|
norm_coef2_a = pd.DataFrame(norm_coef2_a,columns=scales_a) |
|
|
|
norm_coef2_a['t'] = df[time_col] |
|
norm_coef2_a['p'] = df[pressure_col] |
|
norm_coef2_a = norm_coef2_a.dropna(subset=['t']) |
|
|
|
return norm_coef2_a |
|
|
|
|
|
def continous_wavelet_transformer(pressure_data,time_data): |
|
scales_a = np.linspace(1, 256, 256) |
|
coef_a, freqs_a = pywt.cwt(pressure_data, scales_a, "cmor1.5-1.0") |
|
|
|
energy = np.sqrt(coef_a.real**2 + coef_a.imag**2) |
|
coef2_a = np.log2(energy) |
|
period = 1.0 / freqs_a |
|
scaler=preprocessing.MinMaxScaler(feature_range=(0,1)).fit(coef2_a) |
|
norm_coef2_a=scaler.transform(coef2_a) |
|
|
|
return norm_coef2_a,period,time_data,scales_a,coef_a, freqs_a,pressure_data |
|
|
|
def reload_DT_model(): |
|
|
|
with open(Path('DecisionTree.pkl'), 'rb') as file: |
|
model = pickle.load(file) |
|
return model |
|
|
|
def import_min_max(): |
|
|
|
with open(Path('max_min.txt'),'r') as f: |
|
|
|
min_max = f.readlines() |
|
min_max = [x.strip() for x in min_max] |
|
min_max = [x.split(',') for x in min_max] |
|
min_max = [[float(y) for y in x] for x in min_max] |
|
min_max = np.array(min_max) |
|
return min_max |
|
|
|
def predict_microseismic_events(df,x_names,y_names,east_perf,north_perf,depth_perf): |
|
|
|
model = reload_DT_model() |
|
|
|
|
|
df.reset_index(drop=True, inplace=True) |
|
df.rename(columns=dict(zip(df.columns[:256], x_names)), inplace=True) |
|
|
|
ds_test = df[x_names] |
|
|
|
|
|
|
|
|
|
example_data_df = pd.DataFrame(ds_test, columns=x_names) |
|
|
|
|
|
|
|
predictions = model.predict(example_data_df) |
|
|
|
|
|
|
|
predictions_df = pd.DataFrame(predictions, columns=y_names) |
|
|
|
|
|
|
|
predictions_df.columns = [ |
|
str(col) + '_pred' for col in predictions_df.columns] |
|
|
|
final_df = predictions_df.reset_index(drop=True) |
|
|
|
|
|
|
|
min_max = import_min_max() |
|
|
|
final_df['delta_east_pred_denormalized'] = final_df['delta_east_pred'] * \ |
|
(min_max[0][1] - min_max[0][0]) + min_max[0][0] |
|
|
|
final_df['delta_north_pred_denormalized'] = ( |
|
final_df['delta_north_pred'] * (min_max[1][1] - min_max[1][0]) + min_max[1][0]) |
|
|
|
final_df['delta_depth_pred_denormalized'] = final_df['delta_depth_pred'] * \ |
|
(min_max[2][1] - min_max[2][0]) + min_max[2][0] |
|
|
|
final_df['east_pred'] = final_df['delta_east_pred_denormalized'] + east_perf |
|
|
|
final_df['north_pred'] = final_df['delta_north_pred_denormalized'] + north_perf |
|
|
|
final_df['depth_pred'] = final_df['delta_depth_pred_denormalized'] + depth_perf |
|
|
|
final = pd.concat([df, final_df], axis=1) |
|
|
|
return final |
|
|
|
def plot_microseismic_events(final): |
|
|
|
fig = go.Figure(data=[go.Scatter3d( |
|
x=final['east_pred'], |
|
y=final['north_pred'], |
|
z=final['depth_pred'], |
|
mode='markers', |
|
marker=dict( |
|
size=3, |
|
opacity=1, |
|
color='red' |
|
), |
|
name='Predicted' |
|
)]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fig.update_layout(title=f"Predicted Micro Seismic Events", xaxis_title="east", yaxis_title="north",height=800) |
|
return fig |
|
|
|
def compare_microseismic_events(final,actual,east,north,depth,depth_shift): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
actual[depth] = depth_shift - actual[depth] |
|
|
|
|
|
fig = go.Figure(data=[go.Scatter3d( |
|
x=final['east_pred'], |
|
y=final['north_pred'], |
|
z=final['depth_pred'], |
|
mode='markers', |
|
marker=dict( |
|
size=2, |
|
opacity=0.3, |
|
color='red' |
|
), |
|
name='Predicted' |
|
)]) |
|
|
|
fig.add_trace(go.Scatter3d( |
|
x=actual[east], |
|
y=actual[north], |
|
z=actual[depth], |
|
mode='markers', |
|
marker=dict( |
|
size=3, |
|
opacity=1, |
|
color='navy' |
|
), |
|
name='Actual' |
|
)) |
|
fig.update_layout(title=f"Predicted Micro Seismic Events", xaxis_title="east", yaxis_title="north",height=800) |
|
return fig |