Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +236 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,238 @@
|
|
| 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 |
-
|
| 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 datetime
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
|
| 8 |
+
import plotly.graph_objects as go
|
| 9 |
+
from plotly.subplots import make_subplots
|
| 10 |
+
|
| 11 |
+
st.set_page_config(layout="wide")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@st.cache_data
|
| 15 |
+
def create_datafram(start_date, num_prices, base_price, price_ratio):
|
| 16 |
+
|
| 17 |
+
# Generate timestamps (assuming daily data)
|
| 18 |
+
dates = pd.date_range(start=start_date, periods=num_prices)
|
| 19 |
+
|
| 20 |
+
# Generate random prices
|
| 21 |
+
prices = [base_price] # Initial value
|
| 22 |
+
for _ in range(num_prices - 1):
|
| 23 |
+
new_value = prices[-1] + (np.random.randn()* price_ratio) # AR(1) process
|
| 24 |
+
prices.append(new_value)
|
| 25 |
+
|
| 26 |
+
# Create the DataFrame
|
| 27 |
+
data = {
|
| 28 |
+
'timestamp': dates,
|
| 29 |
+
'price': prices
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
return pd.DataFrame(data)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
st.header('Create Price Data', divider=True)
|
| 39 |
+
|
| 40 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 41 |
+
|
| 42 |
+
with col1:
|
| 43 |
+
date = st.date_input("Date Start", datetime.date(2019, 7, 6))
|
| 44 |
+
|
| 45 |
+
with col2:
|
| 46 |
+
steps = st.number_input("Time steps (days)", value=365)
|
| 47 |
+
|
| 48 |
+
with col3:
|
| 49 |
+
price = st.number_input("Base Price", value = 100)
|
| 50 |
+
|
| 51 |
+
with col4:
|
| 52 |
+
ratio = st.number_input("Fluctuation factor [0 - 1]", value=0.9)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
if 'data' not in st.session_state:
|
| 56 |
+
st.session_state['data'] = create_datafram(date, steps, price, ratio)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if st.button('Create Time-series data'):
|
| 60 |
+
st.session_state.data = create_datafram(date, steps, price, ratio)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
st.header('Show Price Data', divider=True)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
col5, col6 = st.columns((1,2))
|
| 68 |
+
|
| 69 |
+
with col5:
|
| 70 |
+
|
| 71 |
+
st.dataframe(st.session_state.data, use_container_width=True)
|
| 72 |
+
|
| 73 |
+
with col6:
|
| 74 |
+
|
| 75 |
+
fig = px.line(st.session_state.data, x="timestamp", y="price", title='Time Series Price Data')
|
| 76 |
+
|
| 77 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 78 |
+
#st.line_chart(st.session_state.data, x="timestamp", y="price", use_container_width=True)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def cal_ipa(df_in: pd.DataFrame, window_size: int = 120, std_adjust : float = 2.0, ipa_col: str='IPA', adj_price: bool= False):
|
| 84 |
+
|
| 85 |
+
if adj_price:
|
| 86 |
+
|
| 87 |
+
df_in['price'] = df_in['price'] - (1 - df_in['price'].rolling(window=window_size).std())
|
| 88 |
+
|
| 89 |
+
df_in[ipa_col] = abs((df_in['price'] - df_in['price'].rolling(window=window_size).mean()) / (df_in['price'].rolling(window=window_size).std()*std_adjust))
|
| 90 |
+
|
| 91 |
+
return df_in
|
| 92 |
+
|
| 93 |
+
def cal_anomaly(df_in):
|
| 94 |
+
|
| 95 |
+
return ...
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
st.header('Anomaly IPA Analysis', divider=True)
|
| 99 |
+
|
| 100 |
+
a_col1, a_col2, a_col3, a_col4 = st.columns(4)
|
| 101 |
+
|
| 102 |
+
with a_col1:
|
| 103 |
+
window_quater = st.number_input("Quater window (days)", value=120)
|
| 104 |
+
|
| 105 |
+
with a_col2:
|
| 106 |
+
window_month = st.number_input("Monthly window (days)", value=30)
|
| 107 |
+
|
| 108 |
+
with a_col3:
|
| 109 |
+
std_adj = st.number_input("Standard Deviation Adjustment", value=2.5)
|
| 110 |
+
|
| 111 |
+
with a_col4:
|
| 112 |
+
adj_price = st.toggle("Adjust Price")
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
b_col1, b_col2 = st.columns(2)
|
| 116 |
+
|
| 117 |
+
with b_col1:
|
| 118 |
+
gamma = st.number_input("Gamma adjustment", value=0.8)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
if 'ipa_df' not in st.session_state:
|
| 123 |
+
st.session_state['ipa_df'] = st.session_state.data.copy()
|
| 124 |
+
else:
|
| 125 |
+
st.session_state.ipa_df = st.session_state.data.copy()
|
| 126 |
+
|
| 127 |
+
if not adj_price:
|
| 128 |
+
st.session_state.ipa_df = st.session_state.data.copy()
|
| 129 |
+
|
| 130 |
+
st.session_state.ipa_df = cal_ipa(st.session_state.ipa_df, window_size=window_month, std_adjust=std_adj, ipa_col='IPA_m', adj_price=adj_price)
|
| 131 |
+
st.session_state.ipa_df = cal_ipa(st.session_state.ipa_df, window_size=window_quater, std_adjust=std_adj, ipa_col='IPA_q', adj_price=adj_price)
|
| 132 |
+
|
| 133 |
+
w_adj = np.random.uniform(low=0.9, high=1.0, size=steps)
|
| 134 |
+
|
| 135 |
+
st.session_state.ipa_df['IPA_q'] = st.session_state.ipa_df['IPA_q'] * w_adj
|
| 136 |
+
st.session_state.ipa_df['IPA_m'] = st.session_state.ipa_df['IPA_m'] * w_adj
|
| 137 |
+
|
| 138 |
+
st.session_state.ipa_df['ipa_adj'] = gamma* st.session_state.ipa_df['IPA_m'] + (1-gamma)*st.session_state.ipa_df['IPA_q']
|
| 139 |
+
|
| 140 |
+
st.session_state.ipa_df['Warning'] = 0
|
| 141 |
+
#df.loc[df['IPA'] < 0.5, 'Warning'] = 'Normal'
|
| 142 |
+
st.session_state.ipa_df.loc[st.session_state.ipa_df['ipa_adj'] >= 0.5, 'Warning'] = 1
|
| 143 |
+
st.session_state.ipa_df.loc[st.session_state.ipa_df['ipa_adj'] >= 1.0, 'Warning'] = 2
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
c_col1, c_col2 = st.columns((1,2))
|
| 147 |
+
|
| 148 |
+
with c_col1:
|
| 149 |
+
st.dataframe(st.session_state.ipa_df)
|
| 150 |
+
|
| 151 |
+
with c_col2:
|
| 152 |
+
# fig, ax1 = plt.subplots(figsize=(14, 5), dpi=160)
|
| 153 |
+
# ax2 = ax1.twinx()
|
| 154 |
+
|
| 155 |
+
# ax1.plot(st.session_state.ipa_df['price'])
|
| 156 |
+
# #ax1.set_ylim(90, 150)
|
| 157 |
+
# ax1.set_xlim(120, 365)
|
| 158 |
+
|
| 159 |
+
# #ax2.plot(df['IPA'], c='red')
|
| 160 |
+
# ax2.fill_between(st.session_state.ipa_df.index, st.session_state.ipa_df['ipa_adj'], color='blue', alpha=0.1)
|
| 161 |
+
# ax2.fill_between(st.session_state.ipa_df.index, st.session_state.ipa_df['Warning'], color='orange', alpha=0.3)
|
| 162 |
+
# ax2.set_xlim(120, 365)
|
| 163 |
+
# ax2.set_ylim(0, 2)
|
| 164 |
+
|
| 165 |
+
# st.pyplot(fig)
|
| 166 |
+
|
| 167 |
+
# =================================================================
|
| 168 |
+
|
| 169 |
+
fig = go.Figure()
|
| 170 |
+
|
| 171 |
+
fig.add_trace(go.Scatter(
|
| 172 |
+
x=st.session_state.ipa_df['timestamp'],
|
| 173 |
+
y=st.session_state.ipa_df['price'],
|
| 174 |
+
hoverinfo='x+y',
|
| 175 |
+
mode='lines',
|
| 176 |
+
line=dict(width=1.0, color='rgb(100, 100, 100)'),
|
| 177 |
+
name='Price'
|
| 178 |
+
))
|
| 179 |
+
|
| 180 |
+
fig.add_trace(
|
| 181 |
+
go.Scatter(
|
| 182 |
+
x=st.session_state.ipa_df['timestamp'],
|
| 183 |
+
y=st.session_state.ipa_df['ipa_adj'],
|
| 184 |
+
hoverinfo='x+y',
|
| 185 |
+
mode='lines',
|
| 186 |
+
line=dict(width=1.0, color='rgb(0, 230, 230)'),
|
| 187 |
+
stackgroup='one',
|
| 188 |
+
name='IPA Value',
|
| 189 |
+
yaxis='y2'
|
| 190 |
+
)
|
| 191 |
+
,)
|
| 192 |
+
|
| 193 |
+
fig.add_trace(
|
| 194 |
+
go.Scatter(
|
| 195 |
+
x=st.session_state.ipa_df['timestamp'],
|
| 196 |
+
y=st.session_state.ipa_df['Warning'],
|
| 197 |
+
hoverinfo='x+y',
|
| 198 |
+
mode='lines',
|
| 199 |
+
line=dict(width=1.0, color='rgb(230, 172, 0)'),
|
| 200 |
+
stackgroup='one',
|
| 201 |
+
name='Anomaly Index',
|
| 202 |
+
yaxis='y3'
|
| 203 |
+
)
|
| 204 |
+
,)
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
fig.update_layout(
|
| 208 |
+
|
| 209 |
+
legend=dict(orientation="h"),
|
| 210 |
+
|
| 211 |
+
yaxis=dict(
|
| 212 |
+
title=dict(text="Price"),
|
| 213 |
+
side="left",
|
| 214 |
+
range=(min(st.session_state.ipa_df['price']*0.99), max(st.session_state.ipa_df['price']*1.01)),
|
| 215 |
+
),
|
| 216 |
+
yaxis2=dict(
|
| 217 |
+
title='',
|
| 218 |
+
side="right",
|
| 219 |
+
range=(0, 2),
|
| 220 |
+
overlaying="y",
|
| 221 |
+
showgrid=False,
|
| 222 |
+
),
|
| 223 |
+
yaxis3=dict(
|
| 224 |
+
title='Anomaly Index',
|
| 225 |
+
side="right",
|
| 226 |
+
range=(0, 2),
|
| 227 |
+
overlaying="y",
|
| 228 |
+
showgrid=False,
|
| 229 |
+
),
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
#fig.update_yaxes(title_text="<b>primary</b> yaxis title")
|
| 233 |
+
#fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)
|
| 234 |
|
| 235 |
+
fig.update_layout(
|
| 236 |
+
title_text="IPA Anomaly detection chart",
|
| 237 |
+
)
|
| 238 |
+
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|