|
1 | 1 | # file to visualise agent-level data per timestep
|
| 2 | + |
2 | 3 | import numpy as np
|
3 | 4 | import matplotlib.pyplot as plt
|
4 | 5 | import matplotlib.animation as animation
|
5 |
| -# read in data for each agent for each timestep |
6 | 6 |
|
7 |
| -# read in insurancefirm data |
8 |
| -rfile = open("data/two_insurance_firms_cash.dat","r") |
| 7 | +# read in data for each agent for each timestep |
| 8 | + # read in insurancefirm data |
| 9 | +rfile = open("data/insurance_firms_cash.dat","r") |
9 | 10 | insurance_firms_cash = [eval(k) for k in rfile]
|
10 | 11 | rfile.close()
|
11 |
| -# read in reinsurancefirm data |
12 |
| -rfile = open("data/two_reinsurance_firms_cash.dat","r") |
| 12 | + # read in reinsurancefirm data |
| 13 | +rfile = open("data/reinsurance_firms_cash.dat","r") |
13 | 14 | reinsurance_firms_cash = [eval(k) for k in rfile]
|
14 | 15 | rfile.close()
|
15 | 16 |
|
16 | 17 | insurance_firms_cash = np.array(insurance_firms_cash)
|
17 | 18 | reinsurance_firms_cash = np.array(reinsurance_firms_cash)
|
18 | 19 |
|
19 | 20 | # shape (runs, steps)
|
20 |
| -print(insurance_firms_cash.shape) |
21 |
| -print(reinsurance_firms_cash.shape) |
22 | 21 |
|
23 | 22 | # let's look at only the first run
|
24 | 23 | first_run_insurance = insurance_firms_cash[0][:]
|
25 | 24 | first_run_reinsurance = reinsurance_firms_cash[0][:]
|
26 | 25 |
|
27 | 26 | class InsuranceFirmAnimation(object):
|
| 27 | + '''class takes in a run of insurance data and produces animations ''' |
28 | 28 | def __init__(self, data):
|
29 | 29 | self.data = data
|
30 | 30 | self.fig, self.ax = plt.subplots()
|
31 | 31 | self.stream = self.data_stream()
|
32 |
| - self.ani = animation.FuncAnimation(self.fig, self.update, interval=40, |
| 32 | + self.ani = animation.FuncAnimation(self.fig, self.update, repeat=False, interval=40, |
33 | 33 | init_func=self.setup_plot)
|
34 | 34 |
|
35 | 35 | def setup_plot(self):
|
36 |
| - """Initial drawing of the scatter plot.""" |
| 36 | + """Initial drawing of the plots.""" |
37 | 37 | casharr,idarr = next(self.stream)
|
38 |
| - |
39 |
| - self.pie = self.ax.pie(casharr, labels=idarr) |
40 |
| - |
| 38 | + self.pie = self.ax.pie(casharr, labels=idarr,autopct='%1.0f%%') |
41 | 39 | return self.pie,
|
42 | 40 |
|
43 | 41 | def data_stream(self):
|
44 | 42 | for timestep in self.data:
|
45 | 43 | casharr = []
|
46 | 44 | idarr = []
|
47 |
| - for (cash, id) in timestep: |
48 |
| - casharr.append(cash) |
49 |
| - idarr.append(id) |
| 45 | + for (cash, id, operational) in timestep: |
| 46 | + if operational: |
| 47 | + casharr.append(cash) |
| 48 | + idarr.append(id) |
50 | 49 | yield casharr,idarr
|
51 | 50 |
|
52 | 51 | def update(self, i):
|
53 | 52 | self.ax.clear()
|
54 | 53 | self.ax.axis('equal')
|
55 | 54 | casharr,idarr = next(self.stream)
|
56 |
| - self.pie = self.ax.pie(casharr, labels=idarr) |
57 |
| - self.ax.set_title("Timestep : " + str(i)) |
| 55 | + self.pie = self.ax.pie(casharr, labels=idarr,autopct='%1.0f%%') |
| 56 | + self.ax.set_title("Timestep : {:,.0f} | Total cash : {:,.0f}".format(i,sum(casharr))) |
58 | 57 | return self.pie,
|
59 | 58 |
|
60 |
| - def save(self): |
61 |
| - self.ani.save('line.mp4', writer='ffmpeg', dpi=80) |
| 59 | + def save(self,filename): |
| 60 | + self.ani.save(filename, writer='ffmpeg', dpi=80) |
62 | 61 |
|
63 | 62 | def show(self):
|
64 | 63 | plt.show()
|
65 | 64 |
|
66 |
| -anim = InsuranceFirmAnimation(first_run_reinsurance) |
67 |
| -anim.show() |
| 65 | +anim1 = InsuranceFirmAnimation(first_run_insurance) |
| 66 | +anim2 = InsuranceFirmAnimation(first_run_reinsurance) |
| 67 | +#anim1.save("insurance.mp4") |
| 68 | +#anim2.save("reinsurance.mp4") |
| 69 | +plt.show() |
0 commit comments