-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimate_market.py
75 lines (65 loc) · 2.3 KB
/
animate_market.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
"""This example generates a movie of all the market clearing events"""
import os, sys
import olypen
import matplotlib.pyplot as pl
import numpy as np
import pandas as pd
import datetime as dt
from moviepy.editor import VideoClip
from moviepy.video.io.bindings import mplfig_to_npimage
db = olypen.Olypen()
# get the bids
bids = pd.read_csv("join_bids.csv.gz",index_col=['mkt_id'])
index = bids.index.get_level_values(0).unique()
# get the metered load
loads = pd.read_csv("metered_load.csv.gz",index_col=['mkt_id'])
# generate frames
fps = 12*6 # 6 hours/second at 12 frames/hour
start_frame = 0
duration = len(index)
fig, ax = pl.subplots()
def make_frame(t):
try:
# read data for this frame
frame = index[int(t*fps+start_frame)]
data = bids.loc[frame]
# construct supply curve
sells = (data.quantity_bid<0).tolist()
sell = data[sells if type(sells) is list else [sells]].sort_values('price_bid',ascending=True)
sell_quantities = (-sell.quantity_bid).cumsum().to_list()
sell_prices = sell.price_bid.to_list()
sell_quantities.insert(0,0)
sell_quantities.append(9999)
sell_prices.append(9999)
# construct demand curve
buys = (data.quantity_bid>0).tolist()
buy = data[buys if type(buys) is list else [buys]].sort_values('price_bid',ascending=False)
unresponsive = data.iloc[0].quantity_clear - buy[buy.price_bid>=buy.price_clear]['quantity_bid'].sum()
buy_quantities = (buy.quantity_bid.cumsum() + unresponsive).to_list()
buy_quantities.insert(0,unresponsive)
buy_prices = buy.price_bid.to_list()
buy_prices.insert(0,9999)
buy_quantities.insert(0,0)
# generate frame
title = data.iloc[0].posttime_clear
quantity_clear = data.iloc[0].quantity_clear
price_clear = data.iloc[0].price_clear
actual_load = unresponsive+loads.loc[frame]
ax.clear()
ax.set_xlabel('Quantity (kW)')
ax.set_ylabel('Price ($/MWh)')
ax.set_xlim([0,1750])
ax.set_ylim([0,1500])
ax.stairs(buy_prices,buy_quantities)
ax.stairs(sell_prices,sell_quantities)
ax.plot(quantity_clear,price_clear,'*')
ax.plot(actual_load,0,'^')
ax.set_title(title)
except:
# zero-order hold frame
pass
return mplfig_to_npimage(fig)
# creating animation
animation = VideoClip(make_frame, duration = duration/fps)
# displaying animation with auto play and looping
animation.write_videofile("animate_market.mp4", fps=fps)