-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathbar-demo.py
78 lines (52 loc) · 1.83 KB
/
bar-demo.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
import marimo
__generated_with = "0.9.17"
app = marimo.App(width="medium")
@app.cell
def __():
import marimo as mo
from drawdata import BarWidget
return BarWidget, mo
@app.cell
def __(BarWidget, mo):
widget = mo.ui.anywidget(BarWidget(height=400, width=700, n_bins=24))
widget
return (widget,)
@app.cell(hide_code=True)
def __(widget):
import numpy as np
df = widget.data_as_pandas
subset = df.loc[lambda d: d["collection"].isin(["collection3", "collection1"])]
return df, np, subset
@app.cell(hide_code=True)
def __(np, subset):
import pandas as pd
def concat_days(dfin, days=4):
out = dfin.copy()
for day in range(1, days + 1):
adapted = dfin.assign(bin=lambda d: d['bin'] + 24 * day + 1)
out = pd.concat([out, adapted])
return out
def add_stats(dfin):
return (
dfin
.pivot(columns="collection", index="bin", values="value")
.assign(balance=lambda d: d["collection3"] - d["collection1"])
.assign(cs=lambda d: d["balance"].cumsum())
.assign(battery=lambda d: np.where(d["cs"] > 0, d["cs"], 0))
.reset_index()
)
pltr = subset.pipe(concat_days).sort_values(["collection", "bin"]).pipe(add_stats)
return add_stats, concat_days, pd, pltr
@app.cell(hide_code=True)
def __(pltr):
import altair as alt
p1 = alt.Chart(pltr).mark_line(color="red", interpolate="step-after").encode(x="bin", y="cs")
p2 = alt.Chart(pltr).mark_area(interpolate="step-after").encode(x="bin", y="battery")
total = pltr["battery"].sum()
(p2 + p1).properties(width=700, title=f"Max battery cap: {pltr['battery'].max(): .2f} kWh, total: {total :.2f} kWh")
return alt, p1, p2, total
@app.cell
def __():
return
if __name__ == "__main__":
app.run()