-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexperiment_3.py
More file actions
391 lines (325 loc) · 14.1 KB
/
Copy pathexperiment_3.py
File metadata and controls
391 lines (325 loc) · 14.1 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# %%
import numpy as np
import json
import os
from scipy.special import softmax
from scipy.stats import ttest_ind
from scipy.stats import mannwhitneyu
from datasets import load_dataset
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
from functions import generate_clip_embeddings
from functions import get_dict_scores
from transformers import CLIPProcessor, CLIPModel
import torch
model_name = "openai/clip-vit-base-patch32"
# # correlations on the dictionary entries
processor = CLIPProcessor.from_pretrained(model_name)
model = CLIPModel.from_pretrained(model_name)
### Last experiment, here we compare imagist to modern love poetry
# %%
# entropy
def calculate_entropy(embedding):
# Convert the embedding values to probabilities using softmax
probabilities = softmax(embedding)
# Calculate the entropy
entropy = -np.sum(probabilities * np.log(probabilities + 1e-9)) # Adding a small constant to avoid log(0)
return entropy
# sparse ratio
# Function to compute the ratio of L1 to L2 norms
def calculate_sparsity_ratio(embedding):
embedding = np.array(embedding)
l1_norm = np.sum(np.abs(embedding)) # L1 norm
l2_norm = np.sqrt(np.sum(embedding**2)) # L2 norm
sparsity_ratio = l1_norm / (np.sqrt(len(embedding)) * l2_norm)
return sparsity_ratio
# %%
# CONFIGS
# we can do the analysis on a poem basis or a sentence basis (default, sentences)
analysis_basis = "sentences"
print(f"Analysis basis: {analysis_basis}")
# %%
# open the imagists
with open('data/some_imagists.json', 'r') as f:
imagists = json.load(f)
poems = []
poem_texts = []
for poet, works in imagists.items():
for work in works:
work['poet'] = poet # Add poet's name to each poem
poems.append(work)
poem_texts.append(work['poem'])
# get len
print('no of poems', len(poems))
# clean it
# use poems
if analysis_basis == "poems":
units = poem_texts
units_clean = []
for poem in poems:
# we want to remove extra spaces
poem = poem['poem'].replace('\n', ' ').replace(' ', ' ')
# the stars we put to seperate the lines
poem = poem.strip().replace('*', '')
# remove leading and trailing spaces
poem = poem.strip()
units_clean.append(poem)
# use sentences
elif analysis_basis == "sentences":
units = [sentence for poem in poem_texts for sentence in poem.split('*')]
units_clean = []
for poem in poems:
# we want to remove extra spaces
poem = poem['poem'].replace('\n', ' ').replace(' ', ' ')
poem = poem.strip().split('*')
# remove leading and trailing spaces
poem = [sentence.strip() for sentence in poem]
units_clean.extend(poem)
else:
raise ValueError("Invalid analysis_basis")
# remove emtpy strings
units_clean = [unit for unit in units_clean if unit != '']
# remove sentences that are just a number
units_clean = [unit for unit in units_clean if not unit.isdigit()]
print(f'no of {analysis_basis.upper()}', len(units_clean))
# get a list of the poets
poets = list(imagists.keys())
print('no of poets', len(poets))
# %%
# now we get the embeddings for each sentence
# check if embeddings are already saved
if os.path.exists(f'data/embeddings/imagists_{analysis_basis}.json'):
with open(f'data/embeddings/imagists_{analysis_basis}.json', 'r') as f:
sentence_embeddings = np.array(json.load(f))
print('loaded')
else:
sentence_embeddings = generate_clip_embeddings(units_clean)
with open(f'data/embeddings/imagists_{analysis_basis}.json', 'w') as f:
json.dump(sentence_embeddings.tolist(), f)
norms = np.linalg.norm(sentence_embeddings, axis=1)
entropies = [calculate_entropy(embedding) for embedding in sentence_embeddings]
variances = np.var(sentence_embeddings, axis=1)
sparsities = [calculate_sparsity_ratio(embedding) for embedding in sentence_embeddings]
# check if the scores are already saved
if os.path.exists(f'data/measures/imagists_{analysis_basis}.json'):
df_sentences = pd.read_json(f'data/measures/imagists_{analysis_basis}.json')
print('loaded')
else:
# get the imageability scores
imag, imag_norm = get_dict_scores(units_clean, 'resources/mrc_psychol_dict.json', 'imag', 'lemma_', normalize_by_tokens=False)
vis, vis_norm = get_dict_scores(units_clean, 'resources/sensorimotor_norms_dict.json', 'Visual.mean', 'lemma_', normalize_by_tokens=False)
conc, conc_norm = get_dict_scores(units_clean, 'resources/concreteness_brysbaert.json', 'Conc.M', 'lemma_', normalize_by_tokens=False)
# make a dataframe
df_sentences = pd.DataFrame({'norm': norms, 'entropy': entropies, 'variance': variances, 'sparsity': sparsities})
df_sentences['imag'] = imag
df_sentences['visual'] = vis
df_sentences['concrete'] = conc
df_sentences['imag_norm'] = imag_norm
df_sentences['visual_norm'] = vis_norm
df_sentences['concrete_norm'] = conc_norm
# save the measures
df_sentences.to_json(f'data/measures/imagists_{analysis_basis}.json', orient='records')
# add the text
df_sentences['text'] = units_clean
# %%
# and we get the poetry from:
# https://huggingface.co/datasets/merve/poetry
ds = load_dataset("merve/poetry")
# make df
df = pd.DataFrame(ds['train'])
print(len(df))
df.head()
# %%
# # take only the modern
df_modern = df[df['age'] == 'Modern']
# take only the poems where type is Love
df_love = df_modern[df_modern['type'] == 'Love']
print('len df', len(df_love))
# remove any others that are in the imagist poets
df_love = df_love[~df_love['author'].isin(poets)]
print('len df after filtering authors', len(df_love))
# more cleaning needed from these cause they are scraped data
# take poems
if analysis_basis == "poems":
units = df_love['content'].tolist()
units_clean_merve = []
for poem in units:
# we want to remove extra spaces
poem = poem.replace('\n', ' ').replace(' ', ' ')
poem = poem.strip().replace('*', '')
# remove leading and trailing spaces
poem = poem.strip()
# remove sentences in the poems with copyright or reprint
poem = poem.split('\n')
poem = [sentence for sentence in poem if 'Copyright' not in sentence]
poem = [sentence for sentence in poem if 'reprint' not in sentence.lower()]
# remove empty strings
poem = [sentence for sentence in poem if sentence != '']
poem = [sentence for sentence in poem if sentence != ' ']
poem = ' '.join(poem)
# take those with len > 5
if len(poem) < 5:
continue
poem = poem.strip()
poem = poem.replace('\r', '')
units_clean_merve.append(poem)
print('no. of poems', len(units_clean_merve))
# take sentences
elif analysis_basis == "sentences":
# get all the sentences, seperated by \n
units = df_love['content'].str.split('\n').explode().tolist()
# save the author and poem names
authors = df_love['author'].str.split('\n').explode().tolist()
print('authors', len(set(authors)))
poems_names = df_love['poem name'].str.split('\n').explode().tolist()
#print('poems', len(set(poems)))
# get the sentences
units_clean_merve = []
for line in units:
# we want to remove extra spaces
line = line.replace('\n', ' ').replace(' ', ' ')
line = line.strip().replace('*', '')
# remove leading and trailing spaces
line = line.strip()
units_clean_merve.append(line)
# remove emtpy strings
units_clean_merve = [unit for unit in units_clean_merve if unit != '']
# remove sentences that are just a number
units_clean_merve = [unit for unit in units_clean_merve if not unit.isdigit()]
# remove empty strings
units_clean_merve = [unit for unit in units_clean_merve if unit != ' ']
units_clean_merve = [unit for unit in units_clean_merve if len(unit) > 5]
# remove \r from strings
units_clean_merve = [unit.replace('\r', '') for unit in units_clean_merve]
# remove all sentences containing "copyright"
units_clean_merve = [unit for unit in units_clean_merve if 'copyright' not in unit.lower()]
# remove all sentence containing "reprint" or "reprinted"
units_clean_merve = [unit for unit in units_clean_merve if 'reprint' not in unit.lower()]
print('no. of lines', len(units_clean_merve))
else:
raise ValueError("Invalid analysis_basis")
# %%
tag = 'love'
# get the embeddings
if os.path.exists(f'data/embeddings/{tag}_{analysis_basis}.json'):
with open(f'data/embeddings/{tag}_{analysis_basis}.json', 'r') as f:
sentence_embeddings = np.array(json.load(f))
print('loaded')
else:
sentence_embeddings = generate_clip_embeddings(units_clean_merve)
with open(f'data/embeddings/{tag}_{analysis_basis}.json', 'w') as f:
json.dump(sentence_embeddings.tolist(), f)
norms_love = np.linalg.norm(sentence_embeddings, axis=1)
entropies_love = [calculate_entropy(embedding) for embedding in sentence_embeddings]
variances_love = np.var(sentence_embeddings, axis=1)
sparsities_love = [calculate_sparsity_ratio(embedding) for embedding in sentence_embeddings]
# check if the scores are already saved
if os.path.exists(f'data/measures/{tag}_{analysis_basis}.json'):
df_sentences_love = pd.read_json(f'data/measures/{tag}_{analysis_basis}.json')
print('loaded')
else:
# get the imageability scores
imag_love, imag_love_norm = get_dict_scores(units_clean_merve, 'resources/mrc_psychol_dict.json', 'imag', 'lemma_', normalize_by_tokens=False)
vis_love, vis_love_norm = get_dict_scores(units_clean_merve, 'resources/sensorimotor_norms_dict.json', 'Visual.mean', 'lemma_', normalize_by_tokens=False)
conc_love, conc_love_norm = get_dict_scores(units_clean_merve, 'resources/concreteness_brysbaert.json', 'Conc.M', 'lemma_', normalize_by_tokens=False)
# make a dataframe
df_sentences_love = pd.DataFrame({'norm': norms_love, 'entropy': entropies_love, 'variance': variances_love, 'sparsity': sparsities_love})
df_sentences_love['imag'] = imag_love
df_sentences_love['visual'] = vis_love
df_sentences_love['concrete'] = conc_love
df_sentences_love['imag_norm'] = imag_love_norm
df_sentences_love['visual_norm'] = vis_love_norm
df_sentences_love['concrete_norm'] = conc_love_norm
# save the measures
df_sentences_love.to_json(f'data/measures/{tag}_{analysis_basis}.json', orient='records')
# add the text
df_sentences_love['text'] = units_clean_merve
# %%
# we get imageable and non imageable sentences as reference points
# cocnat the two dfs
df_sentences_all = pd.concat([df_sentences, df_sentences_love])
# make pd print all
pd.set_option('display.max_colwidth', None)
# print the first 10 most imageable sentences in both dfs
print('most imageable')
# make it a list
imageable = df_sentences_all.sort_values('imag_norm', ascending=True)['text'].tolist()
# print the first 10
for sent in imageable[:300]:
print(sent)
print('\n')
# %%
# we choose these sentences (from topmosts and bottommosts) as examples
imag_example = "Homespun, dyed butternuts dark gold color."
non_imag_example = "Of insidious intent"
# retrieve the measures for these from the df
print('imag example exists:', imag_example)
imag_metrics = df_sentences_all[df_sentences_all['text'] == imag_example]
print('non-imag example exists:', non_imag_example)
non_imag_metrics = df_sentences_all[df_sentences_all['text'] == non_imag_example]
# %%
# make distributions of each group for each measure
measures = ['norm', 'entropy', 'sparsity',
'imag_norm', 'visual_norm', 'concrete_norm']
sns.set(style='whitegrid')
plt.figure(figsize=(12, 5))
for i, measure in enumerate(measures):
plt.subplot(2, 3, i+1)
sns.histplot(df_sentences[measure], label='Imagist', color='blue', kde=True, alpha=0.2, stat="density")#, shade=True)
sns.histplot(df_sentences_love[measure], label='Modern Love', color='red', kde=True, alpha=0.2, stat="density")#, shade=True)
# insert the lines for the examples
plt.axvline(imag_metrics[measure].values[0], color='blue', linestyle='--', label='Imageable')
plt.axvline(non_imag_metrics[measure].values[0], color='red', linestyle='--', label='Abstract')
plt.title(measure)
plt.tight_layout()
# add legend
plt.legend()
plt.show()
# print len of both
print('len imag', len(df_sentences))
print('len love', len(df_sentences_love))
# %%
# do the t-test & mannwhitneyu test
for measure in measures:
# make sure to drop any NaNs
df_sentences_nonan = df_sentences.dropna(subset=[measure])
df_sentences_love_nonan = df_sentences_love.dropna(subset=[measure])
t, p = ttest_ind(df_sentences_nonan[measure], df_sentences_love_nonan[measure])
# also make a mannwhitneyu test
t_m, p_m = mannwhitneyu(df_sentences_nonan[measure], df_sentences_love_nonan[measure])
print(f'{measure}: t = {t:.2f}, p = {p:.2f}', f'mannwhitneyu: t = {t_m:.2f}, p = {p_m:.2f}')
print('--')
# %%
# do the same but with the bonferroni correction
alpha = 0.05 # Original significance level
n_tests = len(measures) # Number of comparisons (tests)
bonferroni_alpha = alpha / n_tests # Adjusted significance level for Bonferroni correction
for measure in measures:
# Make sure to drop any NaNs
df_sentences_nonan = df_sentences.dropna(subset=[measure])
df_sentences_love_nonan = df_sentences_love.dropna(subset=[measure])
# print the group sizes
print('group sizes', len(df_sentences_nonan), len(df_sentences_love_nonan))
# Perform T-test
t, p = ttest_ind(df_sentences_nonan[measure], df_sentences_love_nonan[measure])
# print the group sizes
print('group sizes', len(df_sentences_nonan), len(df_sentences_love_nonan))
# Apply Bonferroni correction
is_significant = p < bonferroni_alpha
print(f'{measure}: t = {t:.2f}, p = {p:.2f}, significant: {is_significant}')
# also make a mannwhitneyu test
t_m, p_m = mannwhitneyu(df_sentences_nonan[measure], df_sentences_love_nonan[measure])
# Apply Bonferroni correction
is_significant = p_m < bonferroni_alpha
print(f'{measure}: mannwhitneyu: t = {t_m:.2f}, p = {p_m:.2f}, significant: {is_significant}')
print('--')
# %%
# get the numbers of means and stds per group
print('Imagists')
print(df_sentences.describe())
print('Modern Love')
print(df_sentences_love.describe())
# %%
print('All done:)')
# %%