-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
149 lines (100 loc) · 4.22 KB
/
plot.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
import matplotlib.pyplot as plt
import pandas as pd
import sys
import numpy as np
from scipy.interpolate import make_interp_spline, BSpline
from scipy.optimize import curve_fit
import statistics as stat
import argparse
SMOOTHING_FACTOR = 5
COLOR_1 = 'tab:blue'
COLOR_2 = 'tab:red'
TITLE = 'GEL-MaxSAT: SAT proportion and time'
X_LABEL = 'm/n'
Y_LABEL_LEFT = '%GEL-MaxSAT'
Y_LABEL_RIGHT = 'time (s)'
DF_SAT_MEAN_LABEL = 'SAT proportion mean'
DF_SAT_STD_LABEL = 'SAT proportion std'
DF_TIME_MEAN_LABEL = 'Time mean'
DF_TIME_STD_LABEL = 'Time std'
def main():
parser = init_argparse()
args = parser.parse_args()
experiment_path = args.experiment[0]
axioms_counts, sats, times = get_data_from_experiment(experiment_path)
ax1, ax2 = init_plot(args)
sats_mean, _ = sats
plot_curve(axioms_counts, sats_mean, args, ax1, COLOR_1)
times_mean, _ = times
plot_curve(axioms_counts, times_mean, args, ax2, COLOR_2)
if not args.no_objective_curves:
plot_logit_fit(axioms_counts, sats_mean, ax1)
plot_linear_fit(axioms_counts, times_mean, ax2)
filename = extract_filename(experiment_path)
plt.savefig(f'data/plots/{filename}.png', bbox_inches='tight')
if args.show:
plt.show()
plt.close()
def init_argparse():
parser = argparse.ArgumentParser(
usage='%(prog)s [options] experiment',
description='Plot experiment for GEL-MaxSAT algorithm.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('experiment', nargs=1, type=str,
help='path of the CSV file of the experiment')
parser.add_argument('-w', '--moving-average-size', nargs='?',
default=5, type=int, help='size of the moving average smoothing')
parser.add_argument('-p', '--prob-axioms-count', nargs='?', default=10,
type=int, help='number of probabilistic axioms tested')
parser.add_argument('-f', '--font-size', nargs='?',
default=12, type=int, help='font size for labels')
parser.add_argument('--no-title', action='store_true',
help='remove the title')
parser.add_argument('--no-objective-curves', action='store_true',
help='remove the objecive curves from the plot')
parser.add_argument('--show', action='store_true', help='show the plot')
return parser
def get_data_from_experiment(experiment_path):
df = pd.read_csv(experiment_path)
gp = df.groupby(['Concepts count', 'Axioms count'])
axioms_counts = [j for i, j in gp.groups.keys()]
means = gp.mean()
def get_vals(label): return means.get(label).values
sats = map(get_vals, [DF_SAT_MEAN_LABEL, DF_SAT_STD_LABEL])
times = map(get_vals, [DF_TIME_MEAN_LABEL, DF_TIME_STD_LABEL])
return axioms_counts, sats, times
def init_plot(args):
plt.rcParams.update({'font.size': args.font_size})
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.set_xlabel(X_LABEL)
if not args.no_title:
ax1.set_title(f'{TITLE}')
ax1.set_ylabel(Y_LABEL_LEFT, color=COLOR_1)
ax2.set_ylabel(Y_LABEL_RIGHT, color=COLOR_2)
fig.tight_layout()
return ax1, ax2
def running_average(data_list, size):
window = np.ones(size)/size
def smooth_data(data):
return np.convolve(data, window, mode='valid')
return list(map(smooth_data, data_list))
def plot_curve(axioms_counts, values_mean, args, ax, color):
axioms_counts, values_mean = running_average(
[axioms_counts, values_mean], args.moving_average_size)
ax.plot(axioms_counts, values_mean, color=color)
def plot_logit_fit(axioms_counts, sats_mean, ax1):
def logit_fn(x, k, x0, A, off):
return A / (1 + np.exp(k * (x - x0))) + off
popt, _ = curve_fit(logit_fn, axioms_counts, sats_mean)
logit_vals = logit_fn(axioms_counts, *popt)
ax1.plot(axioms_counts, logit_vals, color=COLOR_1, ls='--')
def plot_linear_fit(axioms_counts, times_mean, ax2):
coef = np.polyfit(axioms_counts, times_mean, 1)
poly1d_fn = np.poly1d(coef)
ax2.plot(axioms_counts, poly1d_fn(axioms_counts), color=COLOR_2, ls='--')
def extract_filename(path):
return path.split('/')[-1].split('.')[0]
if __name__ == '__main__':
main()