-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotting_yelp.py
173 lines (129 loc) · 6.95 KB
/
plotting_yelp.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
171
172
173
import os, glob
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from dotenv import load_dotenv
from pathlib import Path
from utils.load_sentences import load_all_sentences
from utils.evaluation_helper import adapt_df_plot as adapt_df
# load environment variables
load_dotenv()
SAVE_PATH = os.getcwd()
DATASET="yelp"
LEGEND_FONT_SIZE = 10
FONT_SIZE=16
def mean_plots(sentences_factual_manner, sentences_subjective_manner, csv_files, basic_emotions, emotion_dfs, basic_emotions_w_neutral, manner, setting):
for idx, csvfile in enumerate(csv_files):
df = pd.read_csv(csvfile, delimiter=',')
df = adapt_df(df, csvfile, dataset="yelp")
for jdx, emotion in enumerate(basic_emotions):
emotion_dfs[jdx] = pd.concat([emotion_dfs[jdx], df[df['direction'] == emotion]], ignore_index=True)
descriptions_of_prompts=["factual", "subjective"]
dfs_emotional_prompts = [dfe[dfe['input_text'].isin(sentences_subjective_manner)] for dfe in emotion_dfs]
dfs_factual_prompts = [dfe[dfe['input_text'].isin(sentences_factual_manner)] for dfe in emotion_dfs]
fig_path = os.path.join(SAVE_PATH,f"plots/eval/{DATASET}/{setting}/{manner}/")
Path(fig_path).mkdir(parents=True, exist_ok=True)
for description in descriptions_of_prompts:
if description == "factual":
df_prompts = dfs_factual_prompts
else:
df_prompts = dfs_emotional_prompts
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
for idx, emo_df in enumerate(df_prompts):
emotion = emo_df.iloc[0]["direction"]
df_ovr = emo_df
df_ovr_melt = pd.melt(df_ovr, id_vars=['lambda'], value_vars=basic_emotions_w_neutral)
sns.lineplot(data=df_ovr_melt, x='lambda', y='value', hue='variable', ax=axs[idx])
axs[idx].set_xlim(0,1.2)
axs[idx].set_ylim(0,1.0)
axs[idx].set_ylabel("Sentiment score", fontsize=FONT_SIZE)
axs[idx].set_xlabel("λ", fontsize=FONT_SIZE)
axs[idx].set_title(f"towards {emotion}")
one = Line2D([0], [0], label='positive')
two = Line2D([0], [0], label='negative', color='orange')
legend = axs[idx].legend(handles=[one, two], fontsize=LEGEND_FONT_SIZE)
# axs[idx].get_legend().remove()
axs[idx].grid()
fig.tight_layout()
fig.savefig(fig_path+f"{DATASET}_contrastive_source_{setting}_{manner}_lda1_{description}.pdf")
plt.clf()
# for idx, emo_df in enumerate(dfs_factual_prompts):
# emotion = emo_df.iloc[0]["direction"]
# df_ovr = emo_df
# df_ovr_melt = pd.melt(df_ovr, id_vars=['lambda'], value_vars=basic_emotions_w_neutral)
# fig, axs = plt.subplots(1, 1, figsize=(5, 5), dpi=120)
# sns.lineplot(data=df_ovr_melt, x='lambda', y='value', hue='variable', ax=axs)
# axs.set_xlim(0,1.2)
# axs.set_ylim(0,1.0)
# axs.set_ylabel("Sentiment score", fontsize=FONT_SIZE)
# axs.set_xlabel("λ", fontsize=FONT_SIZE)
# # one = Line2D([0], [0], label='positive')
# # two = Line2D([0], [0], label='negative', color='orange')
# # legend = axs[idx].legend(handles=[one, two], fontsize=LEGEND_FONT_SIZE)
# # axs[idx].get_legend().remove()
# axs.grid()
# fig.tight_layout()
# fig.savefig(fig_path+f"{DATASET}_contrastive_source_{setting}_{manner}_{emotion}_lda1_factual.pdf")
# plt.clf()
# def individual_plots(t="subjective"):
# for emotion in basic_emotions:
# fig, ax1 = plt.subplots(1, 1, constrained_layout=True)
# for idx, csvfile in enumerate(csv_files):
# df = pd.read_csv(csvfile, delimiter=',')
# df = adapt_df(df, csvfile, dataset="yelp")
# if t == "subjective":
# df = df[df['input_text'].isin(sentences_subjective_manner)]
# else:
# df = df[df['input_text'].isin(sentences_factual_manner)]
# df_emotion = df[df['direction'] == emotion]
# df_ovr = df_emotion
# # fig.suptitle(f'Steering \"{df_ovr["prompt"][0]}\"\n towards {emotion}')
# for emo in basic_emotions_w_neutral:
# if emo == "pos":
# col="green"
# else:
# col="red"
# ax1.plot(df_ovr['lambda'], df_ovr[emo], label=emo, color=col)
# if emotion == "pos":
# ax1.set_title(f"Yelp - steering to positive")
# else:
# ax1.set_title(f"Yelp - steering to negative")
# ax1.set_xlabel('Lambda')
# ax1.set_ylabel('Emotion Classifier Score')
# red_patch = mpatches.Patch(color='red', label='negative sentiment')
# blue_patch = mpatches.Patch(color='green', label='positive sentiment')
# plt.legend(handles=[red_patch, blue_patch])
# plt.savefig(os.path.join(PATH_TO_REPO,f'plots/eval/{DATASET}/yelp_individual_to_{emotion}_{t}.png'))
if __name__ == "__main__":
technique = "contrastive"
setting_options = ["training_based", "activation_based_fair" , "activation_based_all"]
# SETTING = "training_based" # training-based style vectors
# SETTING = "activation_based_fair" # "fair" activation-based style vectors
SETTINGS = ["activation_based_all"] # all activation-based style vectors
for setting in SETTINGS:
assert setting in setting_options, "Please choose the correct SETTINGS"
factual_prompts, subjective_prompts = load_all_sentences()
manners = ["positive", "negative", "original", "neutral"]
for setting in SETTINGS:
for manner in manners:
# sentences_factual_manner = []
# sentences_subjective_manner = []
# if manner == "original":
# sentences_factual_manner = factual_prompts
# sentences_subjective_manner = subjective_prompts
# else:
# for sent in factual_prompts:
# sentences_factual_manner.append(sent + f" Write the answer in a {manner} manner.")
# for sent in subjective_prompts:
# sentences_subjective_manner.append(sent + f" Write the answer in a {manner} manner.")
path_to_csv=os.path.join(SAVE_PATH,f"scripts/evaluation/results/{DATASET}/{setting}/{technique}/{manner}/")
csv_files = glob.glob(path_to_csv+"*.csv")
if len(csv_files)==0:
continue
basic_emotions = ["pos", "neg"]
basic_emotions_w_neutral = ["pos", "neg"]
emotion_dfs = [pd.DataFrame()] * len(basic_emotions)
mean_plots(factual_prompts, subjective_prompts, csv_files, basic_emotions, emotion_dfs, basic_emotions_w_neutral, manner, setting)