-
Notifications
You must be signed in to change notification settings - Fork 0
/
violin_plots.py
137 lines (113 loc) · 4.38 KB
/
violin_plots.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
import matplotlib.pyplot as plt
import seaborn as sns
import scipy
import numpy as np
import argparse
def plot_histograms(deltas):
'''
Plot a histogram of the a_M values calculcated for each data point
in the evaluation set.
The data points that assign a better score in the incongruent visual
context is coloured in red.
'''
mask1 = deltas > 0.
mask2 = deltas <= 0.
ax1 = sns.distplot(deltas[mask1], kde=False, hist=True, color='gray')
ax2 = sns.distplot(deltas[mask2], kde=False, hist=True, color='red')
ax1.tick_params(labelsize=15)
plt.yscale('log')
ax1.xaxis.set_major_locator(plt.MaxNLocator(6))
plt.show()
def significance_test(congruent, incongruent):
return scipy.stats.wilcoxon(congruent, incongruent)
def calculate_awareness(congruent, incongruent, is_logprob=False):
'''
Calculates Eq. 2
'''
deltas = []
awarenesses = []
total_awareness = 0.
for c, i in zip(congruent, incongruent):
if is_logprob:
delta = i - c
else:
delta = c - i
deltas.append(delta)
a = awareness_function(delta) # Equation 1.
total_awareness += a
awarenesses.append(a)
deltas = np.array(deltas)
total_awareness /= len(congruent)
return deltas, total_awareness
def awareness_function(p_value):
'''
Implements Eq (1).
We simply return the delta_p_value, this models the
awareness of a model as a linear relationship: a(x) = x.
'''
return p_value
def clean_data(data):
'''
Transforms all of the data into floats instead of strings.
'''
return [float(x) for x in data.readlines()]
def package_awareness_numbers(congruent, incongruent, args):
congruent = clean_data(congruent)
incongruents = []
deltas = []
awarenesses = []
ps = []
scores = []
for i in incongruent:
incongruent = clean_data(i)
mean_score = np.mean(incongruent)
scores.append(mean_score)
delta, mean_awareness = calculate_awareness(congruent,
incongruent,
args.logprob)
deltas.append(delta)
awarenesses.append(mean_awareness)
plotdata = np.zeros(1014)
for d in deltas:
plotdata = np.concatenate((plotdata, d))
plotdata = plotdata[1014:] # strip off the leading zeros
return plotdata
def main(args):
'''
Loads the scores from the congruent and incongruent text files.
Cleans the scores data, as necessary.
'''
m1_congruent = args.model1[0]
m1_incongruent = args.model1[1:]
model1 = package_awareness_numbers(m1_congruent, m1_incongruent, args)
m2_congruent = args.model2[0]
m2_incongruent = args.model2[1:]
model2 = package_awareness_numbers(m2_congruent, m2_incongruent, args)
m3_congruent = args.model3[0]
m3_incongruent = args.model3[1:]
model3 = package_awareness_numbers(m3_congruent, m3_incongruent, args)
sns.set_context("paper", rc={"grid.linewidth": 0.6, "xtick.labelsize": 12, "ytick.labelsize": 12, "axes.labelsize": 12})
plot = sns.violinplot(data=[model1, model2, model3])
plot.set(xticklabels=['trgmul', 'decinit', 'hierattn'], ylabel='Difference in Meteor score')
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model1",
help="Path to file containing the scores for the model\
with the congruent visual context",
type=argparse.FileType('r'), nargs="+",
required=True)
parser.add_argument("--model2",
help="Path to files containing the scores for the model\
with the incongruent visual contexts",
type=argparse.FileType('r'), nargs="+",
required=True)
parser.add_argument("--model3",
help="Path to files containing the scores for the model\
with the incongruent visual contexts",
type=argparse.FileType('r'), nargs="+",
required=True)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--logprob", action="store_true")
group.add_argument("--meteor", action="store_true")
main(parser.parse_args())