-
Notifications
You must be signed in to change notification settings - Fork 47
/
NPYViewer.py
465 lines (372 loc) · 15.4 KB
/
NPYViewer.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python3
import os.path
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import *
from pathlib import Path
import numpy as np
from numpy import asarray
from numpy import savetxt
import pandas as pd
import csv
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import os
from scipy.io import savemat
import networkx as nx
version="1.28"
def isint(s):
try:
print(int(s))
return True
except ValueError:
return False
def isfloat(s):
try:
print(float(s))
return True
except ValueError:
return False
def openNPY_CLI_noGUI (filename):
if ".npy" in filename:
data = np.load(filename, allow_pickle=True)
else:
data = np.array(pd.read_csv(filename).values.tolist())
npyfile = NPYfile(data, filename)
print(npyfile)
print(data)
class NPYfile():
def __init__(self, data, filename):
self.data = data
self.filename = filename
def __str__(self):
if hasattr(self.data, 'dtype'):
return "Filename = " + str(self.filename) + " \nDtype = " + str(self.data.dtype) + "\nShape = " + str(
self.data.shape)
else:
return "Filename = " + str(self.filename) + " \nDtype = " + "\nShape = " + str(self.data.shape)
class MainApp(QMainWindow):
def __init__(self):
super().__init__()
self.left = 0
self.top = 0
self.width = 800
self.height = 640
# np.save('/home/user/tst.npy', np.array([255.0,50.0,10.0,5.0]))
self.npyfile = None
self.initUI()
def saveAs(self):
home = str(Path.home())
if self.npyfile is not None:
home = os.path.dirname(self.npyfile.filename)
path = QFileDialog.getSaveFileName(
self, 'Save File', home, 'NPY (*.npy);;CSV(*.csv);;MAT(*.mat)')[0]
# path = QFileDialog.getSaveFileName(
# self, 'Save File', home, 'CSV(*.csv)')[0]
if path != "" and ".csv" in path:
with open((path.replace(".csv", "") + ".csv"), 'w') as stream:
writer = csv.writer(stream)
for row in range(self.tableWidget.rowCount()):
rowdata = []
for column in range(self.tableWidget.columnCount()):
item = self.tableWidget.item(row, column)
if item is not None:
rowdata.append(item.text())
else:
rowdata.append('')
writer.writerow(rowdata)
else:
OutMatrix = []
for row in range(self.tableWidget.rowCount()):
rowdata = []
for column in range(self.tableWidget.columnCount()):
item = self.tableWidget.item(row, column)
if item is not None:
#if item.text().isnumeric():
rowdata.append(float(item.text()))
if rowdata != []:
OutMatrix.append(rowdata)
OutMatrix = np.array(OutMatrix)
if ".csv" in path:
np.save(path, np.array(OutMatrix))
if ".mat" in path:
mdic = {"ans": OutMatrix}
print(OutMatrix)
savemat(path, mdic)
def openNPY(self):
if os.path.exists("lastpath.npy"):
home = str(np.load("lastpath.npy"))
print(home)
else:
home = str(Path.home())
if self.npyfile is not None:
home = os.path.dirname(self.npyfile.filename)
filename = QFileDialog.getOpenFileName(self, 'Open .NPY file', home, ".NPY files (*.npy);;.CSV files (*.csv)")[
0]
if filename != "":
if ".npy" in filename:
data = np.load(filename, allow_pickle=True)
else:
data = np.array(pd.read_csv(filename).values.tolist())
npyfile = NPYfile(data, filename)
print(npyfile)
self.setWindowTitle("NPYViewer v." +version+": "+ npyfile.filename)
self.infoLb.setText("NPY Properties:\n" + str(npyfile))
self.tableWidget.clear()
# initialise table
self.tableWidget.setRowCount(data.shape[0])
dtype_dim = len(npyfile.data.dtype) # 0, if plain dtype, 1 or bigger if compound dtype
if data.ndim > 1:
self.tableWidget.setColumnCount(data.shape[1])
elif dtype_dim > 0:
self.tableWidget.setColumnCount(dtype_dim)
else:
self.tableWidget.setColumnCount(1)
# fill data
if data.ndim > 1:
for i, value1 in enumerate(npyfile.data): # loop over items in first column
for j, value in enumerate(value1):
self.tableWidget.setItem(i, j, QTableWidgetItem(str(value)))
elif dtype_dim > 0:
for i, value1 in enumerate(npyfile.data):
for j, col_name in enumerate(npyfile.data.dtype.names):
self.tableWidget.setItem(i, j, QTableWidgetItem(str(value1[col_name])))
else:
for i, value1 in enumerate(npyfile.data): # loop over items in first column
self.tableWidget.setItem(i, 0, QTableWidgetItem(str(value1)))
self.npyfile = npyfile
path = os.path.dirname(filename)
np.save("lastpath.npy", path)
def openNPY_CLI(self,filename):
if ".npy" in filename:
data = np.load(filename, allow_pickle=True)
else:
data = np.array(pd.read_csv(filename).values.tolist())
npyfile = NPYfile(data, filename)
print(npyfile)
self.setWindowTitle("NPYViewer v." +version+": " +npyfile.filename)
self.infoLb.setText("NPY Properties:\n" + str(npyfile))
self.tableWidget.clear()
# initialise table
self.tableWidget.setRowCount(data.shape[0])
dtype_dim = len(npyfile.data.dtype) # 0, if plain dtype, 1 or bigger if compound dtype
if data.ndim > 1:
self.tableWidget.setColumnCount(data.shape[1])
elif dtype_dim > 0:
self.tableWidget.setColumnCount(dtype_dim)
else:
self.tableWidget.setColumnCount(1)
# fill data
if data.ndim > 1:
for i, value1 in enumerate(npyfile.data): # loop over items in first column
for j, value in enumerate(value1):
self.tableWidget.setItem(i, j, QTableWidgetItem(str(value)))
elif dtype_dim > 0:
for i, value1 in enumerate(npyfile.data):
for j, col_name in enumerate(npyfile.data.dtype.names):
self.tableWidget.setItem(i, j, QTableWidgetItem(str(value1[col_name])))
else:
for i, value1 in enumerate(npyfile.data): # loop over items in first column
self.tableWidget.setItem(i, 0, QTableWidgetItem(str(value1)))
self.npyfile = npyfile
path = os.path.dirname(filename)
np.save("lastpath.npy", path)
def createMenu(self):
exitAct = QAction(QIcon('exit.png'), '&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')
exitAct.triggered.connect(qApp.quit)
openAct = QAction(QIcon('Open.png'), '&Open', self)
openAct.setShortcut('Ctrl+O')
openAct.setStatusTip('Open .NPY file')
openAct.triggered.connect(self.openNPY)
self.statusBar()
saveAct = QAction(QIcon('Save.png'), '&Save As', self)
saveAct.setShortcut('Ctrl+S')
saveAct.setStatusTip('Save As')
saveAct.triggered.connect(self.saveAs)
self.statusBar()
grayscalevVewAct = QAction(QIcon(None), '&View as Grayscale Image', self)
grayscalevVewAct.setShortcut('Ctrl+V')
grayscalevVewAct.setStatusTip('View as Grayscale')
grayscalevVewAct.triggered.connect(self.grayscaleView)
self.statusBar()
View3dAct = QAction(QIcon(None), 'View &3D Point Cloud', self)
View3dAct.setShortcut('Ctrl+3')
View3dAct.setStatusTip('View 3D Point Cloud')
View3dAct.triggered.connect(self.View3dPoints)
self.statusBar()
View3dImgAct = QAction(QIcon(None), 'View as &HeightMap', self)
View3dImgAct.setShortcut('Ctrl+H')
View3dImgAct.setStatusTip('View as HeightMap')
View3dImgAct.triggered.connect(self.ViewImageHeightMap)
self.statusBar()
ViewTimeSeriesAct= QAction(QIcon(None), 'View as &Time Series', self)
ViewTimeSeriesAct.setShortcut('Ctrl+S')
ViewTimeSeriesAct.setStatusTip('View as TimeSeries')
ViewTimeSeriesAct.triggered.connect(self.ViewTimeseries)
self.statusBar()
ViewGraphSeriesAct= QAction(QIcon(None), 'View as Directional &Graph', self)
ViewGraphSeriesAct.setShortcut('Ctrl+G')
ViewGraphSeriesAct.setStatusTip('View as Graph')
ViewGraphSeriesAct.triggered.connect(self.ViewGraphSeriesAct)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&Functionalities')
fileMenu.addAction(openAct)
fileMenu.addAction(saveAct)
fileMenu.addAction(grayscalevVewAct)
fileMenu.addAction(View3dAct)
fileMenu.addAction(View3dImgAct)
fileMenu.addAction(ViewTimeSeriesAct)
fileMenu.addAction(ViewGraphSeriesAct)
fileMenu.addAction(exitAct)
def grayscaleView(self):
OutMatrix = []
for row in range(self.tableWidget.rowCount()):
rowdata = []
for column in range(self.tableWidget.columnCount()):
item = self.tableWidget.item(row, column)
# print(item.text())
if item is not None:
rowdata.append(np.float32(item.text()))
if len(rowdata) > 0 and rowdata != None:
OutMatrix.append(rowdata)
OutMatrix = np.array(OutMatrix)
plt.imshow(OutMatrix, cmap='gray')
plt.show()
return
def ViewGraphSeriesAct(self):
OutMatrix = []
for row in range(self.tableWidget.rowCount()):
rowdata = []
for column in range(self.tableWidget.columnCount()):
item = self.tableWidget.item(row, column)
# print(item.text())
if item is not None:
rowdata.append(np.float32(item.text()))
if len(rowdata) > 0 and rowdata != None:
OutMatrix.append(rowdata)
OutMatrix = np.array(OutMatrix)
G = nx.DiGraph(OutMatrix)
# Set the position of the nodes in the graph
pos = nx.spring_layout(G)
# Draw the graph with labels and edges
labels = {node: str(int(node)+1) for node in G.nodes()}
nx.draw_networkx_labels(G, pos,labels=labels)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_edge_labels(G, pos, edge_labels={(i, j): OutMatrix[i][j] for i, j in G.edges()})
# Draw the nodes with the same color
nx.draw_networkx_nodes(G, pos, node_color='b')
# Set the title and show the plot
plt.title("Directional Graph")
plt.show()
def ViewImageHeightMap(self):
OutMatrix = []
for row in range(self.tableWidget.rowCount()):
rowdata = []
for column in range(self.tableWidget.columnCount()):
item = self.tableWidget.item(row, column)
if item is not None:
if item.text():
rowdata.append(np.float32(item.text()))
if len(rowdata) > 0 and rowdata != None:
OutMatrix.append(rowdata)
# print(OutMatrix)
HeightMap = []
for x, row in enumerate(OutMatrix):
for y, val in enumerate(row):
HeightMap.append([x, y, val])
OutMatrix = np.array(HeightMap)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xs = OutMatrix[:, 0]
ys = OutMatrix[:, 1]
zs = OutMatrix[:, 2]
ax.plot_trisurf(xs, ys, zs,
cmap='Greys_r', edgecolor='none');
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
plt.show()
return
def View3dPoints(self):
OutMatrix = []
for row in range(self.tableWidget.rowCount()):
rowdata = []
for column in range(self.tableWidget.columnCount()):
item = self.tableWidget.item(row, column)
if item is not None:
if item.text():
rowdata.append(np.float32(item.text()))
if len(rowdata) > 0 and rowdata != None:
OutMatrix.append(rowdata)
# print(OutMatrix)
OutMatrix = np.array(OutMatrix)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xs = OutMatrix[:, 0]
ys = OutMatrix[:, 1]
zs = OutMatrix[:, 2]
ax.scatter(xs, ys, zs, c='r', marker='o')
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
plt.show()
return
def ViewTimeseries(self):
OutMatrix = []
for row in range(self.tableWidget.rowCount()):
rowdata = []
for column in range(self.tableWidget.columnCount()):
item = self.tableWidget.item(row, column)
if item is not None:
if item.text():
rowdata.append(np.float32(item.text()))
if len(rowdata) > 0 and rowdata != None:
OutMatrix.append(rowdata)
# print(OutMatrix)
#OutMatrix = np.array(OutMatrix)
fig = plt.figure()
ax = fig.add_subplot(111)
indices=range(0,len(OutMatrix))
plt.plot(indices, OutMatrix, label='values', linewidth=3)
ax.set_xlabel('Time Unit')
ax.set_ylabel('Values')
plt.show()
return
def initUI(self):
self.createMenu()
self.infoLb = QLabel("NPY Properties:")
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(100)
self.tableWidget.setColumnCount(100)
# table selection change
# self.tableWidget.doubleClicked.connect(self.on_click)
self.setGeometry(0, 0, 800, 600)
self.setWindowTitle("NPYViewer v."+version)
self.widget = QWidget(self)
layout = QGridLayout()
layout.addWidget(self.infoLb)
layout.addWidget(self.tableWidget)
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
# self.tableWidget.setItesetTextAlignmentmDelegate(AlignDelegate())
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.setWindowIcon(QIcon('npyviewer_128x128.png'))
self.show()
def main():
if "-noGUI" not in sys.argv:
app = QApplication(sys.argv)
ex = MainApp()
if len(sys.argv) ==2:
ex.openNPY_CLI(sys.argv[1])
sys.exit(app.exec_())
if len(sys.argv) ==3:
if sys.argv[2]=="-noGUI":
print("GUI not displayed")
openNPY_CLI_noGUI(sys.argv[1])
if __name__ == '__main__':
main()