forked from RUBi-ZA/MD-TASK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avg_network.py
executable file
·170 lines (119 loc) · 5.16 KB
/
avg_network.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
#
# Calculate average network measurements over the course of the simulation as
# well as standard deviation of the measurements
#
# Script distributed under GNU GPL 3.0
#
# Author: David Brown
# Date: 17-11-2016
import numpy as np
from lib.cli import CLI
from lib.utils import Logger
import sys, argparse, matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
def split_array(arr, pos):
return arr[:pos], arr[pos:]
def combine_arrays(array_files):
matrix = None
for network in array_files:
if matrix is None:
matrix = np.loadtxt(network)
else:
matrix = np.vstack([matrix, np.loadtxt(network)])
return matrix
def plot_graph(network, err=None, start_x=1, color="black", ecolor="red", title="Title", x_label="X", y_label="Y", ylim=None):
start_x = int(start_x)
num_nodes = network.shape[0]
nodes_axis = range(start_x, num_nodes + start_x)
plt.axhline(0, color='black')
if err is not None:
plt.errorbar(nodes_axis, network, err, color="black", ecolor="red")
else:
plt.plot(nodes_axis, network, color="black")
if ylim:
axes = plt.gca()
axes.set_ylim(ylim)
plt.title(title, fontsize=18)
plt.xlabel(x_label, fontsize=16)
plt.ylabel(y_label, fontsize=16)
def plot_graph_split(split_pos, avg_matrix, std, initial_x_1, initial_x_2, title_1, title_2, x_label, y_label, ylim=None):
pos = int(split_pos)
avg_matrix_1,avg_matrix_2 = split_array(avg_matrix, pos)
std_1,std_2 = split_array(std, pos)
plt.subplots(figsize=(25, 20))
#first plot
plt.subplot(211)
plot_graph(avg_matrix_1, std_1, args.initial_x_1, title=title_1,
x_label=x_label, y_label=y_label, ylim=ylim)
#second plot
plt.subplot(212)
plot_graph(avg_matrix_2, std_2, args.initial_x_2, title=title_2,
x_label=x_label, y_label=y_label, ylim=ylim)
def main(args):
prefix = args.prefix
if not args.data_type in ["BC", "delta-BC", "L", "delta-L"]:
log.error("Unrecognized data type. Exiting...\n")
sys.exit(1)
# average BC
if args.data_type == "BC":
prefix += "_BC"
# delta average BC
elif args.data_type == "delta-BC":
prefix += "_delta_BC"
# average L
elif args.data_type == "L":
prefix += "_L"
# delta average L
elif args.data_type == "delta-L":
prefix += "_delta_L"
# calculate matrices
matrix = combine_arrays(args.data)
np.savetxt("%s_combined.dat" % prefix, matrix)
std = matrix.std(0)
np.savetxt("%s_std_dev.dat" % prefix, std)
avg_matrix = np.mean(matrix, axis=0)
np.savetxt("%s_avg.dat" % prefix, avg_matrix)
if args.generate_plots:
log.info("Generating graph: %s_avg.png\n" % prefix)
ylim = None
if args.y_max is not None and args.y_min is not None:
ylim = [float(args.y_min), float(args.y_max)]
if args.split_pos:
plot_graph_split(
int(args.split_pos), avg_matrix, std, args.initial_x_1, args.initial_x_2,
args.title_1, args.title_2, args.x_label, args.y_label, ylim=ylim
)
else:
plot_graph(
avg_matrix, std, args.initial_x, title=args.title, x_label=args.x_label,
y_label=args.y_label, ylim=ylim
)
plt.savefig("%s_avg.png" % prefix, dpi=300, bbox_inches='tight')
plt.close()
log = Logger()
if __name__ == "__main__":
#parse cmd arguments
parser = argparse.ArgumentParser()
#custom arguments
parser.add_argument("--data", help="The .dat files that will be averaged", nargs="*")
parser.add_argument("--data-type", help="Type of data - BC/delta-BC/L/delta-L")
parser.add_argument("--prefix", help="Prefix used to name outputs", default="network")
parser.add_argument("--generate-plots", help="Generate figures/plots", action='store_true', default=False)
# plot arguments (only used with --generate-plots)
parser.add_argument("--x-label", help="Label for x-axis (use $\Delta$ for delta sign)", default=None)
parser.add_argument("--y-label", help="Label for y-axis (use $\Delta$ for delta sign)", default=None)
parser.add_argument("--y-max", help="Maximum value on y-axis", default=None)
parser.add_argument("--y-min", help="Minimum value on y-axis", default=None)
# argument if generating a single plot
parser.add_argument("--title", help="Title of plot (use $\Delta$ for delta sign)", default=None)
parser.add_argument("--initial-x", help="The start index of the x-axis", default=1)
# arguments if splitting the plot in two
parser.add_argument("--split-pos", help="Position to split the network at (for large networks)", default=None)
parser.add_argument("--title-1", help="Title of first network", default="Plot 1")
parser.add_argument("--title-2", help="Title of second network", default="Plot 2")
parser.add_argument("--initial-x-1", help="The start index of the x-axis for the first network", default=1)
parser.add_argument("--initial-x-2", help="The start index of the x-axis for the second network", default=1)
args = parser.parse_args()
CLI(parser, main, log)