-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsttest.py
158 lines (129 loc) · 4.36 KB
/
sttest.py
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
import altair as alt
import pandas as pd
import streamlit as st
from analysis.accessors import DType
from analysis.data import _ewm_data, _ewm_draw
# Functions
@st.cache_data
def process_csv(csv, dateformat="ISO"):
df = pd.read_csv(csv, header=0, names=["date", "volume"])
kw = dateformats[dateformat][1]
df["date"] = pd.to_datetime(df["date"], **kw)
return df.astype({"volume": DType.U16})
@st.cache_data
def serialize_df(df):
return df.to_csv()
def filter_and_plot_data(df, start, end, windows, show_daily=True):
# provided dates from widget are datetime.date instances
# pandas only plays ball with datetime.datetime instances or strings
mask = (df.date >= start.isoformat()) & (df.date <= end.isoformat())
selected = df.loc[mask]
daily = df.groupby("date", as_index=False)["volume"].sum()
data = _ewm_data(daily, windows, "volume")
data = data.loc[data.date.isin(selected.date)]
chart = _ewm_draw(data, "volume", show_daily=show_daily)
data = data.pivot_table(values="value", index="date", columns="window").reindex()
chart.height = 500
return (chart, data)
def reset_date(name, value):
st.session_state[name] = value
def compare_metric(compare, window, label):
before = compare.at[0, str(window)]
after = compare.at[1, str(window)]
return st.metric(f"{label} Volume ({window} days)", after, after - before)
# App
st.set_page_config(
page_title="Volume Analysis",
page_icon="📊",
layout="wide",
)
st.title("Toms Volume analyser")
auth = st.empty()
help = st.container()
data_sidebar = st.sidebar.container()
data_info = st.expander("Data statistics")
display_selectors = st.container()
chart_display = st.container()
# authentication
# auth.text_input("Activation Code", key="auth_code")
# if "auth_code" not in st.session_state:
# st.session_state["auth_code"] = "1"
# with auth:
# # code = st.text_input("Activation Code", key="auth_code")
# if st.session_state.auth_code == "":
# st.stop()
# if st.session_state.auth_code != st.secrets["auth_code"]:
# st.error("Access Denied")
# del st.session_state.auth_code
# st.rerun()
# st.success("Access Granted")
with help:
st.markdown(
"""
To use, simply upload a csv file of your volumes with two columns, date and volume.
eg:
```csv
date,volume
20/10/2016,200
21/10/2016,100
```
"""
)
dateformats = {
"ISO": ("YYYY-MM-DD", dict(format="ISO8601")),
"Day first": ("DD/MM/YYYY", dict(dayfirst=True)),
"Month first": ("MM/DD/YYYY", dict(dayfirst=False)),
}
with data_sidebar:
st.header("Data Input")
with st.form(key="data-input"):
csv = st.file_uploader("Upload volume csv file here", type=".csv", key="csv")
datefmt = st.selectbox(
"Choose a date format",
[*dateformats.keys()],
format_func=lambda option: f"{option} ({dateformats[option][0]})",
index=1,
key="datefmt",
)
st.form_submit_button("Analyse")
if csv:
st.session_state["df"] = df = process_csv(csv, st.session_state.datefmt)
else:
st.session_state["df"] = df = process_csv("data/Training_Vol18-19.csv", datefmt)
with data_info:
left, right = st.columns(2)
left.table(df["volume"].describe())
# right.table(df.head())
with display_selectors:
st.header("Chart Controls")
date_min = df.date.min().date()
date_max = df.date.max().date()
dates = st.slider(
"Choose start and end dates",
date_min,
date_max,
(date_min, date_max),
)
daily_toggle = st.checkbox("Daily Volumes")
# windows = st.selectbox("Average Windows", [(10, 30, 90), (7, 28, 112)])
with chart_display:
st.header("EWM Average Volume Data")
chart, data = filter_and_plot_data(
df, dates[0], dates[1], windows=(10, 30, 90), show_daily=daily_toggle
)
compare = data.take([0, -1]).reset_index()
_cols = st.columns(3)
with _cols[0]:
compare_metric(compare, 10, "Acute")
with _cols[1]:
compare_metric(compare, 30, "Chronic")
with _cols[2]:
compare_metric(compare, 90, "Baseline")
st.altair_chart(chart, use_container_width=True)
st.dataframe(data)
data_download = st.download_button(
"Download charted data",
serialize_df(data),
file_name="volumes.csv",
mime="text/csv",
)