Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +159 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,161 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
from plotly.subplots import make_subplots
|
| 6 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 7 |
+
from sklearn.linear_model import LinearRegression
|
| 8 |
+
from sklearn.preprocessing import StandardScaler
|
| 9 |
+
import xgboost as xgb
|
| 10 |
+
from tensorflow.keras.models import Sequential
|
| 11 |
+
from tensorflow.keras.layers import Conv1D, Dense, Flatten, Input
|
| 12 |
+
import multiprocessing
|
| 13 |
+
import joblib
|
| 14 |
+
|
| 15 |
+
# Page configuration
|
| 16 |
+
st.set_page_config(page_title="Well Log Analysis", layout="wide")
|
| 17 |
+
|
| 18 |
+
# Sidebar for file upload
|
| 19 |
+
uploaded_file = st.sidebar.file_uploader("Upload your log.csv file", type=["csv"])
|
| 20 |
+
|
| 21 |
+
if uploaded_file is not None:
|
| 22 |
+
data = pd.read_csv(uploaded_file).dropna().reset_index(drop=True)
|
| 23 |
+
st.sidebar.success("File uploaded successfully!")
|
| 24 |
+
else:
|
| 25 |
+
st.sidebar.warning("Please upload a CSV file.")
|
| 26 |
+
st.stop()
|
| 27 |
+
|
| 28 |
+
# Page 1: Visualizer
|
| 29 |
+
def page_visualizer(data):
|
| 30 |
+
st.title("Well Log Visualizer")
|
| 31 |
+
curve_columns = st.multiselect("Select columns to plot", data.columns, default=data.columns.tolist())
|
| 32 |
+
depth_column = st.selectbox("Select depth column", data.columns, index=0)
|
| 33 |
+
|
| 34 |
+
if curve_columns:
|
| 35 |
+
fig = make_subplots(rows=1, cols=len(curve_columns), shared_yaxes=True, subplot_titles=curve_columns)
|
| 36 |
+
for i, col in enumerate(curve_columns, start=1):
|
| 37 |
+
fig.add_trace(go.Scatter(x=data[col], y=data[depth_column], mode='lines', name=col), row=1, col=i)
|
| 38 |
+
fig.update_xaxes(title_text=col, row=1, col=i)
|
| 39 |
+
fig.update_yaxes(autorange='reversed', title_text=depth_column, row=1, col=1)
|
| 40 |
+
fig.update_layout(height=800, width=2000, showlegend=False)
|
| 41 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 42 |
+
|
| 43 |
+
# Page 2: Trainer
|
| 44 |
+
def page_trainer(data):
|
| 45 |
+
st.title("Model Trainer")
|
| 46 |
+
target_column = st.selectbox("Select target column", data.columns, index=0)
|
| 47 |
+
model_type = st.selectbox("Select model type", ["Random Forest", "XGBoost", "Linear Regression", "1D CNN"])
|
| 48 |
+
|
| 49 |
+
# Prepare data
|
| 50 |
+
df = data.dropna().reset_index(drop=True)
|
| 51 |
+
features = df.drop(columns=[target_column])
|
| 52 |
+
target = df[target_column].values
|
| 53 |
+
|
| 54 |
+
# Train/test split
|
| 55 |
+
split_idx = int(0.8 * len(df))
|
| 56 |
+
X_train, X_test = features.iloc[:split_idx], features.iloc[split_idx:]
|
| 57 |
+
y_train, y_test = target[:split_idx], target[split_idx:]
|
| 58 |
+
|
| 59 |
+
if model_type in ["Random Forest", "XGBoost", "Linear Regression"]:
|
| 60 |
+
if model_type == "Random Forest":
|
| 61 |
+
model = RandomForestRegressor(n_estimators=500, n_jobs=-1, random_state=42)
|
| 62 |
+
elif model_type == "XGBoost":
|
| 63 |
+
model = xgb.XGBRegressor(n_estimators=200, n_jobs=-1, random_state=42)
|
| 64 |
+
elif model_type == "Linear Regression":
|
| 65 |
+
model = LinearRegression()
|
| 66 |
+
|
| 67 |
+
model.fit(X_train, y_train)
|
| 68 |
+
score = model.score(X_test, y_test)
|
| 69 |
+
st.success(f"{model_type} R^2 Score: {score:.4f}")
|
| 70 |
+
|
| 71 |
+
# Save model
|
| 72 |
+
st.session_state.model = model
|
| 73 |
+
st.session_state.is_cnn = False
|
| 74 |
+
st.session_state.features = features
|
| 75 |
+
st.session_state.target_column = target_column
|
| 76 |
+
|
| 77 |
+
elif model_type == "1D CNN":
|
| 78 |
+
scaler = StandardScaler()
|
| 79 |
+
X_scaled = scaler.fit_transform(features)
|
| 80 |
+
X = X_scaled.reshape((X_scaled.shape[0], X_scaled.shape[1], 1))
|
| 81 |
+
|
| 82 |
+
X_train, X_test = X[:split_idx], X[split_idx:]
|
| 83 |
+
y_train, y_test = y_train, y_test
|
| 84 |
+
|
| 85 |
+
model = Sequential([
|
| 86 |
+
Input(shape=(X.shape[1], 1)),
|
| 87 |
+
Conv1D(32, 3, activation='relu'),
|
| 88 |
+
Flatten(),
|
| 89 |
+
Dense(64, activation='relu'),
|
| 90 |
+
Dense(1)
|
| 91 |
+
])
|
| 92 |
+
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
|
| 93 |
+
model.fit(X_train, y_train, epochs=30, batch_size=16, verbose=0)
|
| 94 |
+
|
| 95 |
+
loss, mae = model.evaluate(X_test, y_test, verbose=0)
|
| 96 |
+
st.success(f"1D CNN MAE: {mae:.4f}")
|
| 97 |
+
|
| 98 |
+
st.session_state.model = model
|
| 99 |
+
st.session_state.scaler = scaler
|
| 100 |
+
st.session_state.is_cnn = True
|
| 101 |
+
st.session_state.features = features
|
| 102 |
+
st.session_state.target_column = target_column
|
| 103 |
+
|
| 104 |
+
# Download model
|
| 105 |
+
if st.button("Download Trained Model"):
|
| 106 |
+
if not st.session_state.get("is_cnn", False):
|
| 107 |
+
joblib.dump(st.session_state.model, "trained_model.pkl")
|
| 108 |
+
with open("trained_model.pkl", "rb") as f:
|
| 109 |
+
st.download_button("Download sklearn model", f, file_name="trained_model.pkl")
|
| 110 |
+
else:
|
| 111 |
+
model.save("cnn_model.h5")
|
| 112 |
+
with open("cnn_model.h5", "rb") as f:
|
| 113 |
+
st.download_button("Download CNN model", f, file_name="cnn_model.h5")
|
| 114 |
+
|
| 115 |
+
# Page 3: Prediction
|
| 116 |
+
|
| 117 |
+
def page_prediction(data):
|
| 118 |
+
st.title("Prediction Comparison")
|
| 119 |
+
if 'model' not in st.session_state:
|
| 120 |
+
st.warning("Please train a model first.")
|
| 121 |
+
return
|
| 122 |
+
|
| 123 |
+
features = st.session_state.features
|
| 124 |
+
target_column = st.session_state.target_column
|
| 125 |
+
|
| 126 |
+
if st.session_state.get("is_cnn", False):
|
| 127 |
+
scaler = st.session_state.scaler
|
| 128 |
+
X_scaled = scaler.transform(features)
|
| 129 |
+
X_input = X_scaled.reshape((X_scaled.shape[0], X_scaled.shape[1], 1))
|
| 130 |
+
predicted = st.session_state.model.predict(X_input).flatten()
|
| 131 |
+
else:
|
| 132 |
+
predicted = st.session_state.model.predict(features)
|
| 133 |
+
|
| 134 |
+
data_plot = data.copy()
|
| 135 |
+
data_plot['Predicted'] = predicted
|
| 136 |
+
depth_column = st.selectbox("Select depth column", data.columns, index=0)
|
| 137 |
+
|
| 138 |
+
fig = go.Figure()
|
| 139 |
+
fig.add_trace(go.Scatter(x=data_plot[target_column], y=data_plot[depth_column],
|
| 140 |
+
mode='lines+markers', name='Original'))
|
| 141 |
+
fig.add_trace(go.Scatter(x=data_plot['Predicted'], y=data_plot[depth_column],
|
| 142 |
+
mode='lines+markers', name='Predicted'))
|
| 143 |
+
fig.update_yaxes(autorange='reversed', title_text=depth_column)
|
| 144 |
+
fig.update_layout(height=600, width=800, title="Original vs Predicted")
|
| 145 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 146 |
+
|
| 147 |
+
# Main navigation
|
| 148 |
+
|
| 149 |
+
def main():
|
| 150 |
+
st.sidebar.title("Navigation")
|
| 151 |
+
page = st.sidebar.radio("Go to", ["Visualizer", "Trainer", "Prediction"])
|
| 152 |
+
|
| 153 |
+
if page == "Visualizer":
|
| 154 |
+
page_visualizer(data)
|
| 155 |
+
elif page == "Trainer":
|
| 156 |
+
page_trainer(data)
|
| 157 |
+
elif page == "Prediction":
|
| 158 |
+
page_prediction(data)
|
| 159 |
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|