This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsistency.py
132 lines (118 loc) · 5.53 KB
/
consistency.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
from plot import plt, sns
import pandas as pd
import argparse
import numpy as np
from collections import defaultdict, OrderedDict
import metadata
import os
from sklearn import metrics
FONT_SIZE=25
plt.rcParams.update({
"font.size": FONT_SIZE,
"axes.labelsize" : FONT_SIZE,
"font.size" : FONT_SIZE,
"text.fontsize" : FONT_SIZE,
"legend.fontsize": FONT_SIZE,
"xtick.labelsize" : FONT_SIZE * 0.8,
"ytick.labelsize" : FONT_SIZE * 0.8,
})
SERVICE_TABLE = {
"track-changes": "track-ch.",
"doc-updater": "doc-upd.",
"postgresql": "postgres",
}
def load_cluster_assignments(measurements):
all_measurements = defaultdict(list)
for m in measurements:
data = metadata.load(m)
for srv in data["services"]:
if srv["name"] == "loadgenerator": continue
assignment = OrderedDict()
selected = max(srv["clusters"], key=lambda x: srv["clusters"][x]['silhouette_score'])
for k, c in srv["clusters"].items():
all_clusters = []
all_columns = []
for idx, f in enumerate(c["filenames"]):
df = pd.read_csv(os.path.join(m, f), sep="\t")
columns = list(df.columns)
columns.remove("time")
columns.remove("centroid")
all_clusters.extend([idx] * len(columns))
all_columns.extend(columns)
assignment = OrderedDict(sorted(zip(all_columns, all_clusters)))
all_measurements["measurement"].append(m)
all_measurements["service"].append(SERVICE_TABLE.get(srv["name"], srv["name"]))
all_measurements["cluster_size"].append(int(k))
all_measurements["assignment"].append(assignment)
all_measurements["selected"].append(k == selected)
all_measurements["silhouette_score"].append(srv["clusters"][k]["silhouette_score"])
return pd.DataFrame(all_measurements)
def distplot(df, column, filename):
g = sns.FacetGrid(df, col="measurement_a", col_wrap=2, row_order=["1", "2", "3", "4", "5", "all"])
g.map(sns.distplot, column, norm_hist=False, kde=False)
g.set_titles("{col_name}")
print(filename)
plt.gcf().savefig(filename)
def main():
args = parse_args()
df = load_cluster_assignments(args.measurements)
SERVICES = "Services"
INFO_SCORE = "AMI"
SPLIT_JOIN_A = "Split-Join Index a"
SPLIT_JOIN_B = "Split-Join Index b"
measurements = df.measurement.unique()
df2 = df[df.measurement.isin(measurements) & df.selected]
df3 = df2.merge(df2, on=["service"], how="inner")
df4 = df3[df3.measurement_x != df3.measurement_y]
df4["unique"] = df4.apply(lambda row: ", ".join(sorted([row.service, row.measurement_x, row.measurement_y])), axis=1)
df5 = df4.drop_duplicates(["unique"])
scores = []
for _, row in df5.iterrows():
diff = set(row.assignment_x.keys()).intersection(set(row.assignment_y.keys()))
assignment_x = list([row.assignment_x[k] for k in sorted(diff)])
assignment_y = list([row.assignment_y[k] for k in sorted(diff)])
scores.append(metrics.adjusted_mutual_info_score(assignment_x, assignment_y))
df5[INFO_SCORE] = scores
data = defaultdict(list)
for _, combinations in df5.drop_duplicates(["measurement_x", "measurement_y"]).iterrows():
x, y = combinations.measurement_x, combinations.measurement_y
df6 = df5[(df5.measurement_x == x) & (df5.measurement_y == y)]
#df6 = df6.rename(columns={"service": SERVICES})
for service in df["service"]:
data[SERVICES].append(service)
data["measurements"].append("(%s,%s)" % (x[-1], y[-1]))
data[INFO_SCORE].append(df6[df6.service == service][INFO_SCORE].iloc[0])
data["silhouette_score_x"].append(df6[df6.service == service]["silhouette_score_x"].iloc[0])
data["silhouette_score_y"].append(df6[df6.service == service]["silhouette_score_y"].iloc[0])
df7 = pd.DataFrame(data)
## Print table
#df7[INFO_SCORE] = df7.apply(lambda row: "%.2f (%.2f/%.2f)" % (row[INFO_SCORE], row["silhouette_score_x"], row["silhouette_score_y"]), axis=1)
#df8 = df7.groupby(["measurements", "Services"]).first().unstack(0)
#print(df8[INFO_SCORE].to_latex())
sns.set(font_scale=1.3)
sns.set_style("whitegrid")
for _, combinations in df5.drop_duplicates(["measurement_x", "measurement_y"]).iterrows():
x, y = combinations.measurement_x, combinations.measurement_y
df6 = df5[(df5.measurement_x == x) & (df5.measurement_y == y)]
df6 = df6[(df6.service != "postgresql") &
(df6.service != "filestore") &
(df6.service != "mongodb")]
df6 = df6.rename(columns={"service": SERVICES})
g = sns.factorplot(x=SERVICES,
y=INFO_SCORE,
data=df6,
kind="bar",
palette=sns.color_palette(palette="gray", n_colors=6, desat=0.5))
g.despine(left=True)
g.set_xticklabels(rotation=65, ha="right")
filename = "mutual-information-score-%s-%s.pdf" % (x[-1], y[-1])
g.fig.suptitle("AMI(%s, %s)" % (x[-1], y[-1]))
g.set(xlabel="")
print(filename)
plt.gcf().savefig(filename, dpi=300)
def parse_args():
parser = argparse.ArgumentParser(prog='graph', usage='%(prog)s [options]')
parser.add_argument('measurements', nargs="+", help="benchmark data")
return parser.parse_args()
if __name__ == "__main__":
main()