cppyabm  1.0.17
An agent-based library to integrate C++ and Python
plot_usage.py
Go to the documentation of this file.
1 import plotly.graph_objs as go
2 import plotly.express as px
3 import numpy as np
4 import pandas as pd
5 from plotly.subplots import make_subplots
6 from copy import deepcopy
7 
8 line_types = ['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
9 colors = ['DarkSlateGrey','black','DarkViolet']
10 markers = ['circle','cross','diamond']
11 def plot(fig,data,i,name,x):
12  means = []
13  max_stds = []
14  min_stds = []
15  for tag in data.columns:
16  tag_data = data[tag]
17  mean_value = np.mean(tag_data)
18  std_upp = max(tag_data) - mean_value
19  std_down = mean_value - min(tag_data)
20  means.append(mean_value)
21  max_stds.append(std_upp)
22  min_stds.append(std_down)
23  fig.add_trace(go.Scatter(
24  x=x,
25  y=means,
26  name = name,
27  error_y=dict(
28  type='data',
29  symmetric=False,
30  array=max_stds,
31  arrayminus=min_stds,
32  width = 10,
33  thickness = 4
34  ),
35  mode='markers',
36  marker = dict(size=10,
37  color=colors[i]),
38  marker_symbol = markers[i]
39  )
40  # line = dict(width=3, dash=line_types[0],color =colors[0] )
41  )
42  return fig
43 
44 
45 def update_layout(fig):
46  fig.update_layout(
47  width=450, #TODO: to go
48  height=500,#TODO: to go
49  xaxis = dict(
50  showgrid=True,
51  mirror=True,
52  showline=True,
53  # zeroline = False,
54  linecolor = 'black',
55  gridwidth = 20,
56  tickfont = dict(
57  family = 'Times New Roman',
58  size = 30,
59  color = 'black'
60  )
61  ),
62 
63 
64  paper_bgcolor='rgba(0,0,0,0)',
65  plot_bgcolor='rgba(0,0,0,0)',
66  legend=dict(
67  orientation="h",
68  x=0,
69  y=1.2,
70  font=dict(
71  family='Times New Roman',
72  size=30,
73  color='#000'
74  ),
75  bordercolor='DarkSeaGreen'
76  # borderwidth=1
77  ),
78  margin=dict(
79  l=50,
80  r=50,
81  b=100,
82  t=100,
83  pad=4
84  )
85  )
86 
87  fig.update_yaxes(
88  automargin=True,
89  showgrid=True,
90  mirror=True,
91  showline=True,
92  linecolor = 'black',
93  gridwidth = 20,
94  dtick=30,
95  tickfont = dict(
96  family = 'Times New Roman',
97  size = 30,
98  color = 'black'
99  ),
100  zeroline = False)
101  return fig
102 def process(postfixes,fig_name, xs ):
103 
104  def extract_data(postfix):
105  data = pd.read_csv('usage_{}.csv'.format(postfix))
106  # process to remove unwanted coloumn
107  if "Unnamed: 0" in data.keys():
108  data = data.drop("Unnamed: 0", axis=1)
109  return data
110  data = map(lambda postfix: extract_data(postfix),postfixes)
111  # memory_data = []
112  # for item in list(data):
113  # memory_data.append()
114  # memory_data =
115  memory_data = pd.concat([item['Memory'].div(10**6) for item in list(deepcopy(data))],axis=1)
116  memory_data.columns = postfixes
117  CPU_data = pd.concat([item['CPU'] for item in list(deepcopy(data))],axis=1)
118  CPU_data.columns = postfixes
119  fig = go.Figure()
120  fig = plot(fig,memory_data,i = 0,name = 'Memory',x=xs)
121  fig = plot(fig,CPU_data,i = 2,name = 'CPU',x=[x-0.2 for x in xs])
122  fig = update_layout(fig)
123  fig.update_xaxes(automargin=True,showgrid=False,zeroline=False,
124  tickvals = xs,ticktext = postfixes, range = [xs[0]-0.5,xs[-1]+0.5])
125  fig.update_yaxes(range = [0,220])
126  fig.write_image(fig_name+'.svg')
127 
128 
129 
130 if __name__ == '__main__':
131  # xs = [1,2]
132  # postfixes = ['Cpp','Py']
133  # process(postfixes,'Cpp_Py',xs)
134  # postfixes = ['Cppy','Pyy']
135  # process(postfixes,'Cppy_Pyy',xs)
136  xs = [1,2,3,4]
137  postfixes = ['Cpp','Py','Cppy','Pyy']
138  process(postfixes,'all',xs)
139 
list
Definition: pytypes.h:1345
dict
Definition: pytypes.h:1299
plot_usage.update_layout
def update_layout(fig)
Definition: plot_usage.py:45
plot_usage.plot
def plot(fig, data, i, name, x)
Definition: plot_usage.py:11
plot_usage.process
def process(postfixes, fig_name, xs)
Definition: plot_usage.py:102