-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from AEL-H/visualisation
Visualisation
- Loading branch information
Showing
2 changed files
with
25 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |