plot_Vectors.py
7.43 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
# -*- coding: UTF-8 -*-
import os
from optparse import OptionParser
import sys
from time import time
import matplotlib.pyplot as plt
import re
import numpy as np
import matplotlib
from sklearn.metrics.pairwise import cosine_similarity
matplotlib.use('Qt4Agg')
__author__ = 'CMendezC'
# Objective: Plot vectors into 2D and 3D
# with a color for vectors using different transformations
# Parameters:
# 1) --vectorPath Path to read vectors.
# 2) --vectorFile File to read vectors.
# 3) --outputPath Path to place plot files.
# 4) --outputFormat Plot file format: pdf, png
# 5) --absoluteValue Employ absolute values in vectors
# Ouput:
# 1) Plots
# Execution:
# C:\Anaconda3\python plot_Vectors_LSA.py
# --outputPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa\plots
# --vectorPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa
# --vectorFile GU_lsa_local_vectors_2T.txt
# --absoluteValue
# --outputFormat pdf
# C:\Anaconda3\python plot_Vectors_LSA.py --outputPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa\plots --vectorPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa --vectorFile GU_lsa_local_vectors_2T.txt --absoluteValue --outputFormat pdf
# C:\Anaconda3\python plot_Vectors_LSA.py --outputPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa\plots --vectorPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa --vectorFile GU_lsa_local_vectors_10T.txt --absoluteValue --outputFormat pdf
# C:\Anaconda3\python plot_Vectors_LSA.py --outputPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa\plots --vectorPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa --vectorFile GU_lsa_local_vectors_36T.txt --absoluteValue --outputFormat pdf
# C:\Anaconda3\python plot_Vectors_LSA.py --outputPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa\plots --vectorPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa --vectorFile GU_lsa_local_vectors_88T.txt --absoluteValue --outputFormat pdf
# C:\Anaconda3\python plot_Vectors_LSA.py --outputPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa\plots --vectorPath C:\Users\cmendezc\Documents\GENOMICAS\GENSOR_UNITS\wordEmbeddings\lsa --vectorFile GU_lsa_local_vectors_120T.txt --absoluteValue --outputFormat pdf
# python3.4 plot_Vectors.py
# --outputPath /home/cmendezc/gitlab_repositories/sentence-representation-word-embeddings/sentence-representation
# --vectorPath /home/cmendezc/gitlab_repositories/sentence-representation-word-embeddings/sentence-representation
# --vectorFile test.vec --absoluteValue --outputFormat pdf
# python3.4 plot_Vectors.py --outputPath /home/cmendezc/gitlab_repositories/sentence-representation-word-embeddings/sentence-representation --vectorPath /home/cmendezc/gitlab_repositories/sentence-representation-word-embeddings/sentence-representation --vectorFile test.vec --absoluteValue --outputFormat pdf
###########################################################
# MAIN PROGRAM #
###########################################################
if __name__ == "__main__":
# Parameter definition
parser = OptionParser()
parser.add_option("--vectorPath", dest="vectorPath",
help="Path to read vector file", metavar="PATH")
parser.add_option("--vectorFile", dest="vectorFile",
help="File to read vectors", metavar="FILE")
parser.add_option("--outputPath", dest="outputPath",
help="Path to place clustering classified files", metavar="PATH")
parser.add_option("--outputFormat", dest="outputFormat", choices=('pdf', 'png'),
help="Plot output format", metavar="PATH")
parser.add_option("--absoluteValue", default=False,
action="store_true", dest="absoluteValue",
help="Use vector absolute values?")
(options, args) = parser.parse_args()
if len(args) > 0:
parser.error("None parameters indicated.")
sys.exit(1)
# Printing parameter values
print('-------------------------------- PARAMETERS --------------------------------')
print("Path to read vector file: " + str(options.vectorPath))
print("File to read vectors: " + str(options.vectorFile))
print("Path to write plots: " + str(options.outputPath))
print("Plot output format: " + str(options.outputFormat))
print("Use vector absolute values? " + str(options.absoluteValue))
#regexLen = re.compile(r'_(?P<vectorLen>[0-9]+)T')
listVectors = []
listLabels = []
print("Reading vectors...")
#result = regexLen.search(options.vectorFile)
#vectorLen = 0
#if result:
# vectorLen = int(result.group('vectorLen'))
# print("Vector vectorLen: {}".format(vectorLen))
#else:
# print("None vectorLen mentioned within name file!")
# quit()
with open(os.path.join(options.vectorPath, options.vectorFile), mode="r", encoding='utf8') as iFile:
for line in iFile.readlines():
line = line.strip('\r\n')
listLine = line.split()
# print("Len listLine: {}".format(len(listLine)))
label = listLine[0][:12]
# print(" Label: {}".format(label))
vector = []
listValues = listLine[1:]
# print(" Len listValues: {}".format(len(listValues)))
#if len(listValues) != vectorLen:
# print("Vector vectorLen does not match: {}".format(label))
# continue
for elem in listValues:
if options.absoluteValue:
vector.append(abs(float(elem)))
else:
vector.append(float(elem))
listLabels.append(label)
listVectors.append(vector)
print(" Reading vectors done!")
print(" Len vectors: " + str(len(listVectors)))
print(" Len labels: " + str(len(listLabels)))
similarityMatrix = cosine_similarity(np.array(listVectors))
print("similarityMatrix shape: {}".format(similarityMatrix.shape))
t0 = time()
print("Plotting heatmap...")
# fig, ax = plt.subplots()
fig = plt.figure()
ax = fig.add_subplot(111)
# heatmap = ax.pcolor(similarityMatrix, cmap=plt.cm.Reds, alpha=0.8)
heatmap = ax.pcolor(similarityMatrix, cmap=plt.cm.Reds)
fig = plt.gcf()
fig.set_size_inches(16, 16)
ax.set_frame_on(False)
ax.set_yticks(np.arange(similarityMatrix.shape[0]) + 0.5, minor=False)
ax.set_xticks(np.arange(similarityMatrix.shape[1]) + 0.5, minor=False)
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(listLabels, minor=False, size='xx-small')
ax.set_yticklabels(listLabels, minor=False, size='xx-small')
plt.xticks(rotation=90)
ax.grid(False)
# Turn off all the ticks
ax = plt.gca()
for t in ax.xaxis.get_major_ticks():
t.tick1On = False
t.tick2On = False
for t in ax.yaxis.get_major_ticks():
t.tick1On = False
t.tick2On = False
fig.tight_layout()
if options.absoluteValue:
fileName = options.vectorFile+ '.abs.' + options.outputFormat
else:
fileName = options.vectorFile + '.' + options.outputFormat
fig.savefig(os.path.join(options.outputPath, fileName))
# plt.axis('tight')
# plt.show()
# plt.savefig('test.png', bbox_inches='tight')
print(" Plotting heatmap done in %fs" % (time() - t0))