-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualisation.py
More file actions
333 lines (310 loc) · 13.7 KB
/
Copy pathVisualisation.py
File metadata and controls
333 lines (310 loc) · 13.7 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
import re
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from gensim.models import Word2Vec
# w2v.init_sims(replace=True)
# russian_model = download_api.load('word2vec-ruscorpora-300')
# KeyedVectors.load_word2vec_format
# russian_model.build_vocab([list(russian_model.vocab.keys())], update=True)
# # russian_model.build_vocab(list(lemma_sent_dict.values()), update=True)
# russian_model.train(list(lemma_sent_dict.values()))
# vocab = list(russian_model.wv.vocab)
# vocab = list(model.wv.vocab)
# X = model[vocab]
# tsne = TSNE(n_components=2)
# coordinates = tsne.fit_transform(X)
# df = pd.DataFrame(coordinates, index=vocab, columns=['x', 'y'])
# fig = plt.figure()
# ax = fig.add_subplot(1, 1, 1)
# ax.scatter(df['x'], df['y'])
# ax.set_xlim(right=200)
# ax.set_ylim(top=200)
# for word, pos in df.iterrows():
# ax.annotate(word, pos)
# plt.show()
from matplotlib import pyplot
from sklearn.manifold import TSNE
from Preprocessing import replace_time_constructions, read_data, read_vidal
from const.Constants import EMPTY_STR, NEW_LINE, VIS_PATH_N2V, MERGED_PATH, SPACE
# dict(sorted(similar_lemmas_dict_filtered.items(), key=lambda item: len(item[1]), reverse=True))
# words_to_cluster = set([item[0] for sublist in similar_lemmas_dict.values() for item in sublist])
# embeddings_to_cluster = [model_2[word] for word in words_to_cluster]
# transformed_embeddings = PCA(n_components=2).fit_transform(embeddings_to_cluster)
# pyplot.scatter(transformed_embeddings[:, 0], transformed_embeddings[:, 1])
# for i, similar_lemma in enumerate(words_to_cluster):
# pyplot.annotate(similar_lemma, xy=(transformed_embeddings[i, 0], transformed_embeddings[i, 1]))
# pyplot.show()
# DBSCAN(metric=cosine_distances).fit(
# [model_2[word] for word in set([item[0] for sublist in similar_lemmas_dict.values() for item in sublist])])
# DBSCAN(metric=cosine_distances).fit(PCA(n_components=2).fit_transform(
# [[model_2[word]] for word in set([item[0] for sublist in similar_lemmas_dict.values() for item in sublist])]))
# X = model_2[only_medical.wv.vocab]
# pca = PCA(n_components=2)
# result = pca.fit_transform(X)
# # create a scatter plot of the projection
# pyplot.scatter(result[:, 0], result[:, 1])
# words = list(only_medical.wv.vocab)
# for i, similar_lemma in enumerate(words):
# pyplot.annotate(similar_lemma, xy=(result[i, 0], result[i, 1]))
# pyplot.show()
# {k: v for k, v in sorted(
# # {i: [item[0] for sublist in similar_lemmas_dict.values() for item in sublist].count(i) for i in
# # [item[0] for sublist in similar_lemmas_dict.values() for item in sublist]}.items(), key=lambda item: item[1],
# # reverse=True)}
#
def visualise_classes():
trees_full_df, trees_df_filtered = read_data()
replace_time_constructions(trees_df_filtered)
replace_time_constructions(trees_full_df)
# part_of_speech_node_id = dict(trees_full_df[['lemma', 'upostag']].groupby(['lemma', 'upostag']).groups.keys())
# dict_lemmas = {lemma: [index] for index, lemma in enumerate(dict.fromkeys(trees_df_filtered['lemma'].to_list()), 1)}
# dict_lemmas_full = {lemma: [index] for index, lemma in
# enumerate(dict.fromkeys(trees_full_df['lemma'].to_list()), 1)}
form_lemma_dict = dict(zip(trees_df_filtered['form'].to_list(), trees_df_filtered['lemma'].to_list()))
try:
with open(VIS_PATH_N2V, encoding='utf-8') as reader:
class_entries = reader.readlines()
finally:
reader.close()
class_count = 1
classes_numbered = {}
all_words = set()
for line in class_entries:
if line == NEW_LINE:
class_count += 1
else:
word = line.split(':')[1].split(NEW_LINE)[0]
# if class_count not in classes_numbered.keys():
# try:
# classes_numbered[class_count].append(form_lemma_dict[word])
# except KeyError as ke:
# dfjkd = []
# else:
# classes_numbered[class_count] = [form_lemma_dict[word]]
words = word.split(" ")
for w in words:
if w != EMPTY_STR:
all_words.add(form_lemma_dict[w])
medical_model = Word2Vec.load("trained.model")
# words_to_cluster = set([item[0] for sublist in similar_lemmas_dict_filtered.values() for item in sublist])
embeddings_to_cluster = [medical_model[word] for word in all_words]
transformed_embeddings = TSNE(n_components=2, perplexity=8).fit_transform(embeddings_to_cluster)
# words = list(only_medical.wv.vocab)
for i, similar_lemma in enumerate(all_words):
pyplot.annotate(similar_lemma, xy=(transformed_embeddings[i, 0], transformed_embeddings[i, 1]))
pyplot.scatter(transformed_embeddings[:, 0], transformed_embeddings[:, 1])
pyplot.show()
# transformed_embeddings = PCA(n_components=2).fit_transform(embeddings_to_cluster)
def draw_histogram():
# alphab = [5071, 3384, 1361, 650, 347, 142]
# frequencies = ['2', '3-5', '6-10', '11-20', '21-50', '51-2.6k']
# pos = np.arange(len(frequencies))
# ax = plt.axes()
# ax.set_xticks(pos)
# ax.set_xticklabels(frequencies)
# ax.set_xlabel('Number of repeats in a group')
# ax.set_ylabel('Number of groups')
# plt.xticks()
# plt.bar(pos, alphab, width=0.9, color='b')
# plt.title('Number of groups with equal number of repeats')
# plt.show()
# import re
#
# MEDIUM_SIZE = 12
# BIGGER_SIZE = 14
#
# # plt.rc('font', size=MEDIUM_SIZE) # controls default text sizes
# plt.rc('axes', titlesize=BIGGER_SIZE) # fontsize of the axes title
# plt.rc('axes', labelsize=BIGGER_SIZE) # fontsize of the x and y labels
# plt.rc('xtick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
# plt.rc('ytick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
# # plt.rc('legend', fontsize=BIGGER_SIZE) # legend fontsize
# plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
# # plt.rc('title', titlesize=BIGGER_SIZE) # fontsize of the figure title
# path = MERGED_PATH
# try:
# with open(path, encoding='utf-8') as reader:
# class_entries = reader.readlines()
# finally:
# reader.close()
# class_count = 1
# local_c = 0
# curr_len = 0
# group_len = {}
# str_len = {}
# for line in class_entries:
# if not re.match('label*', line):
# if line == NEW_LINE:
# group_len[class_count] = local_c
# str_len[class_count] = curr_len
# local_c = 0
# class_count += 1
# else:
# words = [w for w in line.split(':')[1].split(NEW_LINE)[0].split(" ") if w != EMPTY_STR][1::3]
# curr_len = len(words)
# local_c += 1
# res = {}
# for key, val in sorted(group_len.items()):
# if val not in res.keys():
# res[val] = 1
# else:
# res[val] += 1
#
# res2 = dict(sorted(res.items(), key=lambda x: x[0]))
# alphab = list(res2.values())
# frequencies = list(res2.keys())
#
# pos = np.arange(len(frequencies))
# ax = plt.axes()
# ax.set_xticks(pos)
# ax.set_xticklabels(frequencies)
# ax.set_xlabel('Число повторов в классе', fontsize=12)
# ax.set_ylabel('Число классов', fontsize=12)
# plt.xticks()
# plt.bar(pos, alphab, width=0.9, color='b')
# plt.title('Число классов с равным числом повторов', fontsize=12)
# for index, label in enumerate(ax.xaxis.get_ticklabels()):
# if index % 10 != 0:
# label.set_visible(False)
# plt.show()
SMALL_SIZE = 12
MEDIUM_SIZE = 14
BIGGER_SIZE = 16
# plt.rc('font', size=MEDIUM_SIZE) # controls default text sizes
plt.rc('axes', titlesize=BIGGER_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=BIGGER_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
# plt.rc('legend', fontsize=BIGGER_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
# plt.rc('title', titlesize=BIGGER_SIZE) # fontsize of the figure title
path = MERGED_PATH
try:
with open(path, encoding='utf-8') as reader:
class_entries = reader.readlines()
finally:
reader.close()
class_count = 1
local_c = 0
curr_len = 0
group_len = {}
str_len = {}
for line in class_entries:
if not re.match('label*', line):
if line == NEW_LINE:
group_len[class_count] = local_c
str_len[class_count] = curr_len
local_c = 0
class_count += 1
else:
words = [w for w in line.split(':')[1].split(NEW_LINE)[0].split(" ") if w != EMPTY_STR][1::3]
curr_len = len(words)
local_c += 1
res = {}
for key, val in sorted(group_len.items()):
if val not in res.keys():
res[val] = 1
else:
res[val] += 1
res_len = {}
for key, val in sorted(str_len.items()):
if val not in res_len.keys():
res_len[val] = 1
else:
res_len[val] += 1
res2 = dict(sorted(res.items(), key=lambda x: x[0]))
alphab = list(res2.values())
frequencies = list(res2.keys())
pos = np.arange(len(frequencies))
ax = plt.axes()
ax.set_xticks(pos)
ax.set_xticklabels(frequencies)
ax.set_xlabel('Число повторов в классе')
ax.set_ylabel('Число классов')
plt.xticks()
plt.bar(pos, alphab, width=0.9, color='b')
plt.title('Число классов с равным числом повторов')
for index, label in enumerate(ax.xaxis.get_ticklabels()):
if index % 5 != 0:
label.set_visible(False)
plt.show()
def plot_labels_per_sentences(sent_labels):
sent_labels = dict(sorted(sent_labels.items(), key=lambda entry: len(entry[1]), reverse=False))
MEDIUM_SIZE = 12
BIGGER_SIZE = 14
# plt.rc('font', size=MEDIUM_SIZE) # controls default text sizes
plt.rc('axes', titlesize=BIGGER_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=BIGGER_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
count_dict = {}
sent_num_labels = {k: len(v) for k, v in sent_labels.items()}
for sent, num_labels in sent_num_labels.items():
if num_labels not in count_dict.keys():
count_dict[num_labels] = 1
else:
count_dict[num_labels] += 1
numb_of_sentences_for_each_num_of_labels = list(count_dict.values())
frequencies = list(count_dict.keys())
pos1 = np.arange(len(frequencies))
# hardcode to make a plot look nicer
# frequencies = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12-17"] # change values accordingly
# pos1 = np.arange(len(frequencies))
# numb_of_sentences_for_each_num_of_labels = [826, 933, 841, 719, 551, 377, 219, 163, 104, 63, 28, 21]
ax1 = plt.axes()
ax1.grid(False)
ax1.set_xticks(pos1)
ax1.set_xticklabels(frequencies)
ax1.set_xlabel('Number of labels', fontsize=12)
ax1.set_ylabel('Number of sentences', fontsize=12)
plt.title('Number of labels per sentence', fontsize=12)
plt.bar(pos1, numb_of_sentences_for_each_num_of_labels, width=0.9, color='blue')
plt.show()
def plot_repeat_len(results_dict):
MEDIUM_SIZE = 12
BIGGER_SIZE = 14
# plt.rc('font', size=MEDIUM_SIZE) # controls default text sizes
plt.rc('axes', titlesize=BIGGER_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=BIGGER_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=MEDIUM_SIZE) # fontsize of the tick labels
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
count_dict = {}
class_id_len = {k: len(v[0].split(SPACE)) for k, v in results_dict.items()}
for class_id, leng in class_id_len.items():
if leng not in count_dict.keys():
count_dict[leng] = 1
else:
count_dict[leng] += 1
alphab = list(count_dict.values())
frequencies = list(count_dict.keys())
pos1 = np.arange(len(frequencies))
ax1 = plt.axes()
ax1.set_xticks(pos1)
ax1.set_xticklabels(frequencies)
ax1.set_xlabel('Number of words in a repeat', fontsize=12)
ax1.set_ylabel('Number of groups', fontsize=12)
plt.bar(pos1, alphab, width=0.9, color='b')
plt.title('Number of groups with equal number of words in a repeat', fontsize=12)
plt.show()
def plot_top_30_labels(label_classes_sorted):
label_classes_simple = {k: len(v) for k, v in label_classes_sorted.items()}
top_30 = list(label_classes_simple.items())[-30:]
labels = [item[0] for item in top_30]
counts = [item[1] for item in top_30]
joint_dict = dict(sorted({labels[i]: counts[i] for i in range(30)}.items(), key=lambda x: x[1], reverse=True))
joint_df = pd.DataFrame({'label': list(joint_dict.keys()), 'count': list(joint_dict.values())})
ax = sns.barplot(x='count', y='label',
data=joint_df,
palette="crest")
ax.set(xlabel='count', ylabel='label')
plt.figure(figsize=(10, 8))
sns.set_theme(style="whitegrid")
sns.despine(left=True, bottom=True)
plt.tight_layout()
plt.show()
if __name__ == '__main__':
read_vidal()