Skip to content

Commit

Permalink
Merge pull request #5 from AEL-H/visualisation
Browse files Browse the repository at this point in the history
Visualisation
  • Loading branch information
AEL-H authored Aug 28, 2018
2 parents 3d11fdf + 8599f7c commit 320226f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions insurancesimulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ def save_data(self):

# agent-level data

insurance_firms = [(insurancefirm.cash,insurancefirm.id) for insurancefirm in self.insurancefirms]
reinsurance_firms = [(reinsurancefirm.cash,reinsurancefirm.id) for reinsurancefirm in self.reinsurancefirms]
insurance_firms = [(insurancefirm.cash,insurancefirm.id,insurancefirm.operational) for insurancefirm in self.insurancefirms]
reinsurance_firms = [(reinsurancefirm.cash,reinsurancefirm.id,reinsurancefirm.operational) for reinsurancefirm in self.reinsurancefirms]


self.history_logs['total_cash'].append(total_cash_no)
Expand Down
44 changes: 23 additions & 21 deletions visualisation.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,69 @@
# file to visualise agent-level data per timestep

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# read in data for each agent for each timestep

# read in insurancefirm data
rfile = open("data/two_insurance_firms_cash.dat","r")
# read in data for each agent for each timestep
# read in insurancefirm data
rfile = open("data/insurance_firms_cash.dat","r")
insurance_firms_cash = [eval(k) for k in rfile]
rfile.close()
# read in reinsurancefirm data
rfile = open("data/two_reinsurance_firms_cash.dat","r")
# read in reinsurancefirm data
rfile = open("data/reinsurance_firms_cash.dat","r")
reinsurance_firms_cash = [eval(k) for k in rfile]
rfile.close()

insurance_firms_cash = np.array(insurance_firms_cash)
reinsurance_firms_cash = np.array(reinsurance_firms_cash)

# shape (runs, steps)
print(insurance_firms_cash.shape)
print(reinsurance_firms_cash.shape)

# let's look at only the first run
first_run_insurance = insurance_firms_cash[0][:]
first_run_reinsurance = reinsurance_firms_cash[0][:]

class InsuranceFirmAnimation(object):
'''class takes in a run of insurance data and produces animations '''
def __init__(self, data):
self.data = data
self.fig, self.ax = plt.subplots()
self.stream = self.data_stream()
self.ani = animation.FuncAnimation(self.fig, self.update, interval=40,
self.ani = animation.FuncAnimation(self.fig, self.update, repeat=False, interval=40,
init_func=self.setup_plot)

def setup_plot(self):
"""Initial drawing of the scatter plot."""
"""Initial drawing of the plots."""
casharr,idarr = next(self.stream)

self.pie = self.ax.pie(casharr, labels=idarr)

self.pie = self.ax.pie(casharr, labels=idarr,autopct='%1.0f%%')
return self.pie,

def data_stream(self):
for timestep in self.data:
casharr = []
idarr = []
for (cash, id) in timestep:
casharr.append(cash)
idarr.append(id)
for (cash, id, operational) in timestep:
if operational:
casharr.append(cash)
idarr.append(id)
yield casharr,idarr

def update(self, i):
self.ax.clear()
self.ax.axis('equal')
casharr,idarr = next(self.stream)
self.pie = self.ax.pie(casharr, labels=idarr)
self.ax.set_title("Timestep : " + str(i))
self.pie = self.ax.pie(casharr, labels=idarr,autopct='%1.0f%%')
self.ax.set_title("Timestep : {:,.0f} | Total cash : {:,.0f}".format(i,sum(casharr)))
return self.pie,

def save(self):
self.ani.save('line.mp4', writer='ffmpeg', dpi=80)
def save(self,filename):
self.ani.save(filename, writer='ffmpeg', dpi=80)

def show(self):
plt.show()

anim = InsuranceFirmAnimation(first_run_reinsurance)
anim.show()
anim1 = InsuranceFirmAnimation(first_run_insurance)
anim2 = InsuranceFirmAnimation(first_run_reinsurance)
#anim1.save("insurance.mp4")
#anim2.save("reinsurance.mp4")
plt.show()

0 comments on commit 320226f

Please sign in to comment.