-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomponents.py
201 lines (181 loc) · 6.84 KB
/
components.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
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
import streamlit as st
from streamlit_echarts import st_echarts
import util
import toolz as to
from palette import PALETTE
from PIL import Image
from streamlit_extras.metric_cards import style_metric_cards
from streamlit_extras.app_logo import add_logo as st_add_logo
style_args = {"border_size_px": 0, "border_left_color": PALETTE["4-cerulean"]}
def financial_bans(summary_stats_data, direction="horizontal"):
# TODO: Add Year filter
"""Takes dataframe of financial summary data at the year level and displays BANs
for med spend, pharm spend, member months and average pmpm. Can handle dataframe with
multiple years or a single year. Dataframe should be pre-filtered to the time frame desired.
"""
year_values = sorted(list(set(summary_stats_data["year"])))
summary_stats_data = summary_stats_data.copy(deep=True)
summary_stats_data = summary_stats_data.loc[
summary_stats_data["year"].isin(year_values)
]
med_spend = summary_stats_data["current_period_medical_paid"].sum()
# pharm_spend = summary_stats_data["current_period_pharmacy_paid"].sum()
member_mon_count = summary_stats_data["current_period_member_months"].sum()
avg_pmpm = med_spend / member_mon_count
if direction == "vertical":
st.metric("Medical Spend", util.human_format(med_spend))
# st.metric("Pharmacy Spend", util.human_format(pharm_spend))
st.metric("Member Months", util.human_format(member_mon_count))
st.metric("Average PMPM", util.human_format(avg_pmpm))
style_metric_cards(**style_args)
else:
col1, col2, col3, col4 = st.columns(4)
col1.metric("Medical Spend", util.human_format(med_spend))
# col2.metric("Pharmacy Spend", util.human_format(pharm_spend))
col3.metric("Member Months", util.human_format(member_mon_count))
col4.metric("Average PMPM", util.human_format(avg_pmpm))
style_metric_cards(**style_args)
def year_slider(year_values):
st.divider()
st.markdown(
"""
Use the following time slider to cut the following charts by the year range of your interest.
"""
)
start_year, end_year = st.select_slider(
label="Select a range of years",
options=year_values,
value=(year_values[0], year_values[-1]),
label_visibility="collapsed",
)
selected_range = year_values[
year_values.index(start_year) : year_values.index(end_year) + 1
]
return selected_range
def claim_type_line_chart(df, height="300px", animated=True):
if animated:
t = st.session_state["iteration"]
month_list = sorted(list(set(df["year_month"])))
anim_data = df.loc[df["year_month"] <= month_list[t], :]
list_data = [anim_data.columns.to_list()] + anim_data.values.tolist()
else:
list_data = [df.columns.to_list()] + df.values.tolist()
series = list(set(df["claim_type"]))
datasetWithFilters = [
{
"id": f"dataset_{s}",
"fromDatasetId": "dataset_raw",
"transform": {
"type": "filter",
"config": {
"and": [
{"dimension": "claim_type", "=": s},
]
},
},
}
for s in series
]
seriesList = [
{
"type": "line",
"smooth": True,
"datasetId": f"dataset_{s}",
"showSymbol": False,
"name": s,
"labelLayout": {"moveOverlap": "shiftY"},
"emphasis": {"focus": "series"},
"encode": {
"x": "year_month",
"y": "paid_amount_pmpm",
"label": ["claim_type", "paid_amount_pmpm"],
"itemName": "year_month",
"tooltip": ["paid_amount_pmpm"],
},
}
for s in series
]
option = {
"color": list(
to.keyfilter(
lambda x: x in ["2-light-sky-blue", "4-cerulean", "french-grey"],
PALETTE,
).values()
),
"dataset": [{"id": "dataset_raw", "source": list_data}] + datasetWithFilters,
"title": {"text": "Paid Amount PMPM by Claim Type"},
"tooltip": {"order": "valueDesc", "trigger": "axis"},
"xAxis": {"type": "category", "nameLocation": "middle"},
"yAxis": {"name": "PMPM"},
"grid": {"right": 140},
"series": seriesList,
}
st_echarts(options=option, height=height, key="chart")
def pop_grouped_bar(df):
pivoted_df = (
df.pivot(index="category", columns="display", values="current_period_pmpm")
.reset_index()
.round()
)
list_data = [pivoted_df.columns.to_list()] + pivoted_df.values.tolist()
option = {
"color": list(
to.keyfilter(
lambda x: x
in ["french-grey", "2-light-sky-blue", "3-air-blue", "4-cerulean"],
PALETTE,
).values()
),
"legend": {},
"tooltip": {},
"dataset": {"source": list_data},
"xAxis": {"type": "category", "data": sorted(list(set(df["category"])))},
"yAxis": {"type": "value"},
"series": [{"type": "bar"} for x in list(set(df["display"]))],
}
st_echarts(options=option)
def generic_simple_v_bar(df, x, y, title, color=None, height="300px", top_n=None):
if color is None:
color = ""
df.sort_values(by=x, inplace=True, ascending=False)
if top_n:
df = df.head(top_n)
df.sort_values(by=x, inplace=True, ascending=True)
options = {
"xAxis": {"type": "value"},
"yAxis": {"type": "category", "data": df[y].tolist()},
"series": [{"data": df[x].tolist(), "type": "bar", "color": color}],
"title": {"text": title},
"tooltip": {"position": "top"},
"grid": {"containLabel": True},
}
st_echarts(options=options, height=height)
def add_logo():
st_add_logo(
"https://tuva-public-resources.s3.amazonaws.com/TuvaHealth-Logo-45h.png",
height=100,
)
# st.markdown(
# """
# <style>
# [data-testid="stSidebarNav"] {
# background-image: url(https://tuva-public-resources.s3.amazonaws.com/TuvaHealth-Logo-45h.png);
# background-repeat: no-repeat;
# padding-top: 120px;
# background-position: 20px 20px;
# }
# [data-testid="stSidebarNav"]::before {
# margin-left: 20px;
# margin-top: 0px;
# font-size: 30px;
# position: relative;
# top: 0px;
# }
# </style>
# """,
# unsafe_allow_html=True,
# )
def favicon():
return "https://tuva-public-resources.s3.amazonaws.com/TuvaHealth-Icon.ico"
def tuva_logo():
return "https://tuva-public-resources.s3.amazonaws.com/TuvaHealth-Logo-45h.png"