Spaces:
Sleeping
Sleeping
File size: 15,627 Bytes
052f08d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | import pandas as pd
import numpy as np
# import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import altair as alt
import streamlit as st
import streamlit_nested_layout
from streamlit_vega_lite import altair_component
from mLogsFunctions import *
#LOADING DATA------------------------------------------------------------------------------------------
def upload_csv():
df = None
uploaded_file = st.file_uploader(label='Upload *csv file from your drive! Choose a file:', type='csv')
if uploaded_file is not None:
df = pd.read_csv(uploaded_file, na_values=-9999)
st.success("Loading finished!")
st.write('---')
return df
#PLOTTING------------------------------------------------------------------------------------------
# Store the initial value of widgets in session state
def selection_info(df, method, option_w, option_x, option_y, option_c):
if "method" not in st.session_state:
st.session_state.method:str = "Single Well"
st.session_state.option_w:str = "15-1-SNN-3P"
st.session_state.option_x:str = "RHOB"
st.session_state.option_y:str = "DTC"
st.session_state.option_c:str = "WELL"
well_names = np.sort(df.WELL.unique())
st.radio("",
key=method,
options=["All Wells", "Single Well"],)
st.radio(
"WELL",
key=option_w,
options=well_names,)
st.selectbox(
"X Axis",
key=option_x,
options=(df.columns.sort_values().str.upper().drop(["WELL", "DEPTH"])),)
st.selectbox(
"Y Axis",
key=option_y,
options=(df.columns.sort_values().str.upper().drop(["WELL", "DEPTH"])),)
st.selectbox(
"Color Axis",
key=option_c,
options=df.columns.sort_values().str.upper())
return st.session_state
#Interactive Charts-----------------------------------------------------------------------
@st.cache_resource
def interval_define():
return alt.selection_interval()
@st.cache_resource
def make_selection(df, _interval, option_x, option_y, option_c):
def c_(df, _interval, option_x, option_y, x_log:str="linear", y_log:str="linear"):
return alt.Chart(df,
title="Crossplot "+option_x+" vs "+option_y+"",
).mark_point().encode(
x = alt.X(option_x.upper(),
axis=alt.Axis(title=option_x),
scale= alt.Scale(zero=False, type=x_log
)
),
y = alt.Y(option_y.upper(),
axis=alt.Axis(title=option_y),
scale=alt.Scale(zero=False,type=y_log
)
),
color=alt.condition(_interval, option_c, alt.value('lightgray')),
).properties(
selection=_interval,
height=570,
width=600)#.transform_regression(option_x.upper(), option_y.upper()).mark_line()
if option_x in ["LLD", "LLS"]:
x_log = "log"
else:
x_log = "linear"
if option_y in ["LLD", "LLS"]:
y_log = "log"
else:
y_log = "linear"
return c_(df, _interval, option_x, option_y, x_log, y_log)
#Histogram-----------------------------------------------------------------------
def bar_plot(data, option_x):
def c_(data, option_x, _log):
return alt.Chart(title="Histogram of "+option_x+"",
data=data
).mark_bar().encode(
x = alt.X(option_x.upper(),
bin=alt.Bin(maxbins=30),
axis=alt.Axis(title=option_x),
scale=alt.Scale(zero=False)
),
y = alt.Y('count()',
axis=alt.Axis(title='Number of Values'),
scale=alt.Scale(zero=False, type=_log),
),
color = alt.Color('WELL', legend=None
)
).properties(
height=250,
width=250
)
if option_x in ["LLD", "LLS"]:
return c_(data, option_x, "symlog")
else:
return c_(data, option_x, "linear")
#Curve View-----------------------------------------------------------------------
def curve_plot(data,filted_data, x_column):
def c_(data,filted_data, x_column, _log):
color_codes = {"GR":"lime",
"LLD":"red",
"LLS":"dodgerblue",
"NPHI":"blue",
"RHOB":"red",
"DTC":"red",
"DTS":"magenta",
"FRACTURE_ZONE":"lightcoral",
"FRACTURE_ZONE_PRED":"lightgreen"
}
if x_column in color_codes.keys():
color_ = color_codes[x_column]
else:
color_ = "blue"
return alt.Chart(data
).mark_line(size=1,
orient='horizontal',
color=color_,
point=alt.OverlayMarkDef(color="", size=1) #Show raw points
).encode(
x=alt.X(x_column.upper(),
scale=alt.Scale(zero=False, type=_log),
axis=alt.Axis(title=x_column.upper(),
titleAnchor='middle',
orient='top',
labelAngle=0,
titleColor=color_,
labelColor=color_,
tickColor=color_,
)
),
y=alt.Y('DEPTH',
scale=alt.Scale(zero=False,
reverse=True,
),
axis=alt.Axis(title=None,
labelColor=color_,
tickColor=color_,
)
)
).properties(height=500,
width=129
)
if x_column in ["LLD", "LLS"]:
curve = c_(data,filted_data, x_column, "log")
else:
curve = c_(data,filted_data, x_column, "linear")
if filted_data is not None:
point_plot = alt.Chart(filted_data).mark_circle(size=20,
color='red',
opacity=1
).encode(
x=x_column,
y='DEPTH'
)
return curve + point_plot
else:
return curve
# import altair as alt
# def curve_plot(data, filted_data, x_column):
# def c_(data, filted_data, x_column, _log):
# color_codes = {
# "GR": "lime",
# "LLD": "red",
# "LLS": "dodgerblue",
# "NPHI": "blue",
# "RHOB": "red",
# "DTC": "red",
# "DTS": "magenta",
# "FRACTURE_ZONE": "lightcoral",
# "FRACTURE_ZONE_PRED": "lightgreen"
# }
# if x_column in color_codes.keys():
# color_ = color_codes[x_column]
# else:
# color_ = "blue"
# return alt.Chart(data).mark_line(size=1, orient='horizontal', color=color_, point=alt.OverlayMarkDef(color="", size=1)).encode(
# y=alt.X(x_column.upper(),
# scale=alt.Scale(zero=False, type=_log),
# axis=alt.Axis(title=x_column.upper(),
# titleAnchor='middle',
# orient='top',
# labelAngle=0,
# titleColor=color_,
# labelColor=color_,
# tickColor=color_,
# )
# ),
# x=alt.Y('DEPTH',
# scale=alt.Scale(zero=False, reverse=True),
# axis=alt.Axis(title=None, labelColor=color_, tickColor=color_))
# ).properties(
# height=500,
# width=700
# )
# if x_column in ["LLD", "LLS"]:
# curve = c_(data, filted_data, x_column, "log")
# else:
# curve = c_(data, filted_data, x_column, "linear")
# if filted_data is not None:
# point_plot = alt.Chart(filted_data).mark_circle(size=20, color='red', opacity=1).encode(
# y=alt.X(x_column, scale=alt.Scale(zero=False)),
# x=alt.Y('DEPTH', scale=alt.Scale(zero=False, reverse=True))
# )
# return (curve + point_plot).resolve_scale(y='shared')
# else:
# return curve
#MissingBar-----------------------------------------------------------------------
def missing_bar(data, x_title):
return alt.Chart(data).mark_bar().encode(
x=alt.X('Columns', sort='-y', title=x_title),
y='Count missing (%)',
color=alt.condition(
alt.datum['Count missing (%)'] >10, # If count missing is > 10%, returns True,
alt.value('orange'), # which sets the bar orange.
alt.value('steelblue') # And if it's not true it sets the bar steelblue.
)
).properties(
width=500,
height=250
).configure_axis(
grid=False
)
#BoxPLot-----------------------------------------------------------------------
def missing_box(data, curve):
if curve in ["LLD", "LLS"]:
return alt.Chart(data).mark_boxplot(extent='min-max').encode(
x=alt.X('WELL:O', title=None,
),
y=alt.Y(f'{curve}:Q', title=curve,scale=alt.Scale(zero=False, type="log")
),
color='WELL:N'
).properties(
width=500,
height=300
)
else:
return alt.Chart(data).mark_boxplot(extent='min-max').encode(
x=alt.X('WELL:O', title=None
),
y=alt.Y(f'{curve}:Q', title=curve,scale=alt.Scale(zero=False)
),
color='WELL:N'
).properties(
width=500,
height=300
)
#Histogram Line-----------------------------------------------------------------------
def hist_line_plot(data, curve):
st.caption(f"Histogram of {curve}")
if curve in ["LLD", "LLS"]:
fig = sns.displot(data, x=curve, hue="WELL", kind="kde", height=5,aspect=1.2, log_scale=True)
fig.set(ylabel="Values")
st.pyplot(fig)
else:
fig = sns.displot(data, x=curve, hue="WELL", kind="kde", height=5,aspect=1.2)
fig.set(ylabel="Values")
st.pyplot(fig)
#CrossPlot-----------------------------------------------------------------------
def crossplot(data, x_curve, y_curve):
fig = sns.jointplot(data=data, x=x_curve, y=y_curve, hue="WELL")
if x_curve in ["LLD", "LLS"]:
fig.ax_joint.set_xscale('log')
fig.ax_marg_x.set_xscale('log')
if y_curve in ["LLD", "LLS"]:
fig.ax_joint.set_yscale('log')
fig.ax_marg_y.set_yscale('log')
st.pyplot(fig)
#PairPlot-----------------------------------------------------------------------
def pairplot(data, rows, cols,color_):
return alt.Chart(data).mark_circle().encode(
alt.X(alt.repeat("column"), type='quantitative', scale=alt.Scale(zero=False)),
alt.Y(alt.repeat("row"), type='quantitative', scale=alt.Scale(zero=False)),
color=color_
).properties(
width=100,
height=100
).repeat(
row = rows,
column = cols
).configure_axis(
grid=False
)
#Heatmap----------------------------------------------------------------
def heatmap(df):
fig = sns.heatmap(df, annot=True)
st.pyplot(fig)
#Heatmap----------------------------------------------------------------
def plotly_3d(data, x, y, z, color, size, symbol, log_x, log_y, log_z):
#Data slicer
curvs_ = columns_list(data, no_well=True)
def slicer_(data, sli_key, val_key,):
slicer1_, slicer2_ = st.columns([4, 6])
# sli=curvs_[0]
with slicer1_:
sli = st.selectbox("Data slicer", key=sli_key, options=curvs_)
with slicer2_:
values = st.slider('Select a range of values',
min_value = float(data[sli].min()),
max_value = float(data[sli].max()),
value=(float(data[sli].min()), float(data[sli].max())),
key=val_key,
)
data = data.query(f"{sli} >= {values[0]} and {sli} <= {values[1]}")
return data
c1, c2, c3 = st.columns(3)
with c1:
data = slicer_(data, "slicer_1", "sli1_value")
with c2:
data = slicer_(data, "slicer_2", "sli2_value")
with c3:
data = slicer_(data, "slicer_3", "sli3_value")
fig = px.scatter_3d(data, x=x,
y=y,
z=z,
color=color,
size=size,
size_max=18,
symbol=symbol,
opacity=0.7,
log_x=log_x,
log_y=log_y,
log_z = log_z,
width=1000, height=700,
color_continuous_scale="blugrn")
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), #tight layout
# paper_bgcolor="LightSteelBlue"
template="none")
st.plotly_chart(fig) |