This repository has been archived by the owner on Mar 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyvisualize.py
executable file
·995 lines (794 loc) · 30 KB
/
pyvisualize.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#!/usr/bin/env python
'''
Author: John D. Anderson
Email: jander43@vols.utk.edu
Description:
This program allows for quick visuzalition of large
NetLogo BehaviorSpace data sets.
Attributions:
martineau, Wed Oct 05 2016, renegade, "Heat map from data
points in python", Mar 25, 2015 at 22:11.
http://stackoverflow.com/a/29269645/6926917
Legal:
Copyright 2016 John David Anderson
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Usage:
pyvisualize
'''
# libraries for GUI/Threads
import logging
import Tkinter
from tkFileDialog import askopenfilename
import ttk
import sys
import os
import time
import threading
import Queue
import math
import numpy as np
import primefac as pf
# libraries for data visualization
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import h5py
# custom libraries (local directory)
import square_build
# banner
banner = '''
______ _ _ _ _ _
| ___ \ | | | (_) | (_)
| |_/ / _| | | |_ ___ _ _ __ _| |_ _______
| __/ | | | | | | / __| | | |/ _` | | |_ / _ \\
| | | |_| \ \_/ / \__ \ |_| | (_| | | |/ / __/
\_| \__, |\___/|_|___/\__,_|\__,_|_|_/___\___|
__/ |
|___/
'''
# constants
LG_FONT = ('Helvetica', 23)
SM_FONT = ('Verdana', 16)
EXEC = 'exec'
COMPLR_T = '<string>'
FONTDICT = {
'fontsize': 'small',
'fontweight': matplotlib.rcParams['axes.titleweight'],
'verticalalignment': 'baseline',
'horizontalalignment': 'center'
}
# globals
COLOBARDICT = {}
WDIM = {}
SIMPORTDICT = {
'simdat': None,
'canvas': None,
'mplcsize': None,
'mplcnum': None
}
# checking path (used if bundled with PyInstaller)
if getattr(sys, 'frozen', False):
RTPath = os.path.dirname(sys.executable)
else:
# get the right path
RTPath = os.path.dirname(os.path.abspath(__file__))
# set debug to true
logging.basicConfig(level=logging.DEBUG)
# self-explanatory
print banner
# generators and functions
def gen_list(ex_list):
'''
Generator to take items from object and return them.
'''
for val in ex_list:
yield val
def gen_xy_list(ex_list):
'''
Function to take x/y item pairs and generate separate lists for each.
'''
x_list, y_list = [], []
for val in ex_list:
x_list.append(val[0])
y_list.append(val[1])
return x_list, y_list
def min_max(heatmap):
'''
Function to take a 2d list and return the minimum and maximum elements.
Adapted from: martineau, Wed Oct 05 2016, renegade, "Heat map from data
points in python", Mar 25, 2015 at 22:11,
http://stackoverflow.com/a/29269645/6926917
'''
# searching for min and max elements
hmin = min(min(elem for elem in row) for row in heatmap)
hmax = max(max(elem for elem in row) for row in heatmap)
# returning
return hmin, hmax
def crange(value, minval, maxval, palette):
'''
Function to generate a specific color for an element in the heatmap.
Adapted from: martineau, Wed Oct 05 2016, renegade, "Heat map from data
points in python", Mar 25, 2015 at 22:11,
http://stackoverflow.com/a/29269645/6926917
'''
# get max index of palette
max_index = len(palette)-1
# convert elem in range minval - maxval to elem in range 0 to max_index
fval = (float(value-minval) / (maxval-minval)) * max_index
# truncate intermediate pallette value (fval) to an int
ival = int(fval)
# now caculate difference between the two
diff = fval - ival
# now select a color tuple from the palette
c0r, c0g, c0b = palette[ival]
# select color tuple that is +1 of previous but not greater than max_index
c1r, c1g, c1b = palette[min(ival+1, max_index)]
# now get difference of two color tuples
dr, dg, db = c1r-c0r, c1g-c0g, c1b-c0b
# now create color set based of diff and d* values
colorset = (c0r + diff * dr, c0g + diff * dg, c0b + diff * db)
# convert to 0 - 255 range number sets
color = [int(c*255) for c in colorset]
# return color string in '#xxxxxx' format (e.g. #0000ff)
return '#' + '%02x' % color[0] + '%02x' % color[1] + '%02x' % color[2]
def find_group(hdfpath, group_num, gQ, aQ):
'''
Function to open HDF5 file and return data associated with "group_num".
'''
# constants
HDFPATH = '/' + str(group_num)
# open hdf5 file
with h5py.File(hdfpath, 'r') as hdf5file:
# get attributes
attr_list = list(gen_list(hdf5file[HDFPATH].attrs.iteritems()))
# update attributes
aQ.put(attr_list)
aQ.put(None)
# iterate over data sets
for dset in hdf5file[HDFPATH]:
# generate list of data items
data_list = list(gen_list(hdf5file[HDFPATH + '/' + dset]))
# data_list = list(parse_list(data_list))
# create dict
data_dict = {dset: data_list}
# push on Q
gQ.put(data_dict)
# sentinel value
gQ.put(None)
# return
return
def attribute_view(grp, attr_str):
'''
Function to generate a small Toplevel() window to view attributes of a
simulation.
'''
# generate unique name
tm = str(time.clock())
var_num = tm.split('.')
var_name = 'thread_{0}'.format(var_num[1])
# string to be compiled
attr_tplvl_str = (
'{0} = Tkinter.Toplevel()\n'
'{0}.title(\'Simulation {1} Attributes\')\n'
'label = ttk.Label({0}, text=attr_str, font={2}, anchor=\'center\')\n'
'label.pack(expand=True, fill=\'both\', side=\'top\')'
).format(var_name, grp, SM_FONT)
# compile and execute code
attr_tplvl_code = compile(attr_tplvl_str, COMPLR_T, EXEC)
exec attr_tplvl_code
# NOTE: Needs to be refactored to a class
def simulation_data_portfolio(grp_name, grpQ, attrQ, controller):
'''
Function to generate x/y plots for all data sets for a given simulation.
'''
# create attribute string and make "global"
attr_str = ""
for attr_list in iter(attrQ.get, None):
for i, attributes in enumerate(attr_list):
tmp_str = "{0}: {1}\n".format(attributes[0], attributes[1])
attr_str = attr_str + tmp_str
# creating Toplevel w/ attribute button
tplvl = Tkinter.Toplevel()
tplvl.title('Simulation {0} Data'.format(grp_name))
tplvl.configure(background='grey')
attr_btn = ttk.Button(tplvl, text='attributes',
command=lambda: attribute_view(grp_name, attr_str))
attr_btn.pack()
# create canvas, frame
tplvl_canvas = Tkinter.Canvas(tplvl)
can_frame = Tkinter.Frame(tplvl_canvas)
# create scrollbars / add to top level
tplvl_yscrlbr = Tkinter.Scrollbar(tplvl_canvas, orient='vertical')
tplvl_xscrlbr = Tkinter.Scrollbar(tplvl_canvas, orient='horizontal')
# innercanvas for matplotlib data
frame_innercan = Tkinter.Canvas(can_frame,
yscrollcommand=tplvl_yscrlbr.set,
xscrollcommand=tplvl_xscrlbr.set)
# stor object instances in global dict
SIMPORTDICT['simdat'] = frame_innercan
SIMPORTDICT['canvas'] = tplvl_canvas
# callback functions
def on_vertical(event):
SIMPORTDICT['simdat'].yview_scroll(-1 * event.delta, 'units')
def on_horizontal(event):
SIMPORTDICT['simdat'].xview_scroll(-1 * event.delta, 'units')
def on_config(event):
innercan = SIMPORTDICT['simdat']
canvas = SIMPORTDICT['canvas']
print 'innercan: ', innercan.bbox('all')
print 'width, height: ', event.width, event.height
scrollregion = (0, 0, int(event.width), int(event.height))
# get data
mplsize_list = []
for d_index, data_dict in enumerate(iter(grpQ.get, None)):
for dset_name, data_list in data_dict.items():
# generate x/y lists
x_list, y_list = gen_xy_list(data_list)
# row/columns
row = d_index / 3
col = d_index % 3
# max y-value
ymax = int(max(y_list))
xmax = len(y_list)
# calculate padding
ipadx = len(str(ymax)) * 30
ipady = xmax - 1
# creat figure for canvas
fig = Figure(figsize=(2, 2), dpi=90)
a = fig.add_subplot(111)
a.set_title('Data: {0}'.format(dset_name), fontdict=FONTDICT)
a.plot(x_list, y_list)
fig_canvas = FigureCanvasTkAgg(fig, frame_innercan)
fig_canvas.get_tk_widget().grid(row=row, column=col,
ipadx=ipadx,
ipady=ipady, sticky='NSEW')
fig_canvas.show()
width = fig_canvas.get_tk_widget().winfo_width()
height = fig_canvas.get_tk_widget().winfo_height()
# store width/height of fig_canvas
entry = [width, height]
# update list of w/h values
mplsize_list.append(entry)
# sort list
mplsize_list.sort()
# measure canvas
can_width = int(mplsize_list[-1][0] * 3)
can_height = int(mplsize_list[-1][1] * math.ceil((d_index+1)/3.0))
# create scrollregion
scrollregion = (0, 0, int(can_width), int(can_height))
# final window/scrollbar configurations
tplvl_yscrlbr.pack(side='right', fill='y')
tplvl_xscrlbr.pack(side='bottom', fill='both')
frame_innercan.pack(side='left', fill='both', expand=True)
can_frame.pack(side='left', expand=True)
tplvl_yscrlbr.config(command=frame_innercan.yview)
tplvl_xscrlbr.config(command=frame_innercan.xview)
frame_innercan.bind('<MouseWheel>', on_vertical, '+')
frame_innercan.bind('<Shift-MouseWheel>', on_horizontal, '+')
tplvl_canvas.pack()
# frame_innercan.update_idletasks()
# can_frame.update_idletasks()
# tplvl_canvas.create_window(0, 0, anchor='nw', window=can_frame)
# tplvl_canvas.bind_all('<Configure>', on_config)
frame_innercan.config(scrollregion=scrollregion)
# change dimesions
ws = tplvl.winfo_screenwidth()
hs = tplvl.winfo_screenheight()
x = (ws - can_width) / 2
y = (hs - can_height) / 2
# adjust window size
dims = tplvl.winfo_geometry().split('+')[0].split('x')
tplvl.geometry('%dx%d+%d+%d' % (can_width, can_height+40, x, y))
# # center window
# controller.eval('tk::PlaceWindow %s center' %
# tplvl.winfo_pathname(tplvl.winfo_id()))
# return
return
def gen_heatmap(controller, data_queue, hdfpath):
'''
Adapted from: martineau, Wed Oct 05 2016, renegade, "Heat map from data
points in python", Mar 25 2015 at 22:11,
http://stackoverflow.com/a/29269645/6926917
'''
# constants
CDIM = 20
# func def
def heatmap_callback(event):
'''
Function called when mouse event <CLICKED> is triggered on a heatmap
tile.
'''
# read mouse click event
canvas = event.widget
try:
rect = canvas.find_withtag('current')[0]
except IndexError:
print 'IndexError: non-tile area of heatmap was clicked'
return
# getting group number to access data for
grp_num = int(rect)
# queue for thread
grpQ = Queue.Queue()
attrQ = Queue.Queue()
# purely a function implementation
find_group(hdfpath, grp_num, grpQ, attrQ)
# plot simulation data
simulation_data_portfolio(grp_num, grpQ, attrQ, controller)
# return
logging.info('Showing: Data Portfolio for Group {0}'.format(grp_num))
# 2d array from HDF5 file
heat_map = data_queue.get()
# calculating min/max values
# NOTE: adapted from martineau (see docstring at top of function)
heat_min, heat_max = min_max(heat_map)
# collor palette for heatmap
# NOTE: adapted from martineau (see docstring at top of function)
palette = [(0, 0, 1), (0, 0.5, 0), (0, 1, 0), (1, 0.5, 0), (1, 0, 0)]
# rows/columns for heatmap
rows, cols = len(heat_map), len(heat_map[0])
cwidth, cheight = rows * CDIM, cols * CDIM
# get data view frame
dvf = controller.frames['DataView']
# canvas object for heatmap
can = Tkinter.Canvas(dvf)
# update RootWindow's state with canvas object
controller.canvas['DataViewCanvas'] = can
# create inner canvas for scrolling feature
yscrlbr = Tkinter.Scrollbar(can, orient='vertical')
xscrlbr = Tkinter.Scrollbar(can, orient='horizontal')
innercan = Tkinter.Canvas(can, bd=1, width=cwidth, height=cheight,
yscrollcommand=yscrlbr.set,
xscrollcommand=xscrlbr.set)
# functions for scrolling
def on_vertical(event):
innercan.yview_scroll(-1 * event.delta, 'units')
def on_horizontal(event):
innercan.xview_scroll(-1 * event.delta, 'units')
# populate canvas with tiles (i.e. rectangles)
# NOTE: adapted from martineau (see docstring at top of function)
rect_width, rect_height = cwidth // rows, cheight // cols
for y, row in enumerate(heat_map):
for x, temp in enumerate(row):
x0, y0 = x * rect_width, y * rect_height
x1, y1 = x0 + rect_width-1, y0 + rect_height-1
color = crange(temp, heat_min, heat_max, palette)
cr_val = (str(x), str(y))
rect = innercan.create_rectangle(x0, y0, x1, y1,
fill=color, width=0, tags=cr_val)
# gen colorbar list
interval = (heat_max - heat_min)/9
cbarlist = [heat_min + i*interval for i in range(9)]
cbarlist.append(heat_max)
cbarlist.reverse()
# fill dict
COLORBARDICT = {
'colorbarlist': cbarlist,
'rwidth': rect_width,
'rheight': rect_height,
'hmin': heat_min,
'hmax': heat_max
}
# colorbar callback
def gen_colorbar(cbardict):
'''
Function to create a colorbar to interpret meaning of heatmap colors.
'''
# get dict contents
colorbarlist = cbardict['colorbarlist']
rect_width = cbardict['rwidth']
rect_height = cbardict['rheight']
heat_min = cbardict['hmin']
heat_max = cbardict['hmax']
# create toplevel window
colorbar_view = Tkinter.Toplevel()
colorbar_view.title('')
# create frame object
frm = ttk.Frame(colorbar_view)
frm.pack()
# create canvas object
cbarcan = Tkinter.Canvas(frm, width=rect_width, height=10*rect_height)
# color palette for heatmap
palette = [(0, 0, 1), (0, 0.5, 0), (0, 1, 0), (1, 0.5, 0), (1, 0, 0)]
# populate canvas with tiles
# NOTE: adapted from martineau (see docstring at top of function)
x = 0
cbar_values = 'Highest\n'
for y, cbar in enumerate(colorbarlist):
cbar_values += ' |\n'
x0, y0 = x * rect_width, y * rect_height
x1, y1 = x0 + rect_width-1, y0 + rect_height-1
color = crange(cbar, heat_min, heat_max, palette)
rect = cbarcan.create_rectangle(x0, y0, x1, y1, fill=color,
width=0)
cbar_values += 'Lowest'
label = ttk.Label(frm, text=cbar_values, font={2}, anchor='center')
label.pack(expand=True, fill='both', side='left')
# final step
cbarcan.pack(side='left')
# configure colorbar button
dvf.colorbar_button = ttk.Button(dvf.btn_frame, text='Colorbar',
command=lambda: gen_colorbar(COLORBARDICT)
)
dvf.colorbar_button.pack(side='left')
# finish configurations/packing
yscrlbr.pack(side='right', fill='y')
xscrlbr.pack(side='bottom', fill='both')
innercan.pack(side='left', fill='both', expand=True)
innercan.config(scrollregion=innercan.bbox('all'))
# yscrlbr.config(command=innercan.yview)
# xscrlbr.config(command=innercan.xview)
innercan.bind('<Button-1>', heatmap_callback)
innercan.bind_all('<MouseWheel>', on_vertical)
innercan.bind_all('<Shift-MouseWheel>', on_horizontal)
can.pack() # NOTE: must call "pack()" or won't show
# return
return
def update_progbar(progress, Q, popup, controller):
'''
Function to update the progressbar while CSV is converted to HDF5.
'''
progress["value"] = Q.get()
if progress["maximum"] == progress["value"]:
print "CONVERSION FINISHED!!!!"
popup.destroy()
return
controller.after(100, update_progbar, progress, Q, popup, controller)
def csv_linesum(fname):
'''
Function to count lines in CSV file
'''
with open(fname, 'rU') as f:
for i, __ in enumerate(f):
pass
return i
def hdf5_linesum(hdfpath):
'''
Function to count the number of lines in an HDF5 file.
'''
with h5py.File(hdfpath, 'r') as hdf5file:
for count, __ in enumerate(hdf5file):
pass
return count+1
def gen_hdf5_dnames(hdfpath):
'''
Generator to return list of data names from the HDF5 file.
'''
with h5py.File(hdfpath, 'r') as hdf5file:
for grp in hdf5file:
for dset in hdf5file['/' + grp]:
yield dset
return
def dataset_length(hdfpath):
'''
Function to return length of datasets
'''
with h5py.File(hdfpath, 'r') as hdf5file:
for grp in hdf5file:
for dset in hdf5file['/' + grp]:
return hdf5file['/' + grp + '/' + dset].len()
return 0
def get_filename(filepath):
'''
Function to extract filename from a full path.
'''
path_list = filepath.split('/')
filename = path_list[len(path_list)-1]
return filename
# NOTE: Needs to be refactored for use in a subprocess
def csv2hdf5(fpath, Q):
'''
Function to convert CSV data to HDF5.
'''
# limited scope libraries
import csv
# functions
def hdf5_path(argv):
'''
Function to generate path and filename for HDF5 file.
'''
argname = argv.split('/')
csvname = argname[len(argname)-1].rsplit('.', 1)
h5name = csvname[0] + ".hdf5"
pathname = ''
for i, folder in enumerate(argname):
if i == len(argname)-1:
return pathname + h5name
pathname += folder + '/'
# check for empty arg
if fpath == '':
sys.exit()
# find line number
numline = csv_linesum(fpath)
# lists/dicts/containers for data
atlst = []
datasets = {}
# getting path/name of hdf5 file
h5name = hdf5_path(fpath)
# open "TABLE" csv file and copy to HDF5 file
with open(fpath, 'rU') as csvfile, h5py.File(h5name, 'w') as hdf5:
# main loop
for i, line in enumerate(csv.reader(csvfile)):
# push increment to queue (NOTE: for progressbar)
Q.put(i)
# pulling dataset names and attributes
if i == 6:
atlst = [row for j, row in enumerate(line) if j < 17]
datasets = {line[x]: x for x in range(18, len(list(line)))}
if i > 6:
if line[0] in hdf5:
grp = hdf5[line[0]]
for dset_name in grp:
dset = grp[dset_name]
newsize = dset.len()
dset.resize((newsize+1, 2))
dset[newsize, 0] = int(line[17])
dset[newsize, 1] = float(line[datasets[dset_name]])
continue
# else create new group
grp = hdf5.create_group(line[0])
for attr, row in zip(atlst, line):
grp.attrs[attr] = row
for name, index in datasets.iteritems():
inlist = [int(line[17]), float(line[index])]
grp.create_dataset(name, (1, 2), maxshape=(None, 2),
data=inlist)
def read_hdf5(hdf5path, Q, datapath, ticks):
'''
Function to read data from HDF5 file and pass to a Queue.
'''
# dictionary for data
data_dict = {}
# open hdf5 file
with h5py.File(hdf5path, 'r') as hdf5file:
for grp in hdf5file:
fullpath = '/' + grp + datapath
datapoint = hdf5file[fullpath][ticks]
data_dict[int(grp)] = datapoint[1]
# list for 2D array
ls_2d_array = list(gen_list(data_dict.values()))
# building heatmap
list_rows = square_build.square_builder(len(ls_2d_array))
# heatmap list
data_array = square_build.square_list(list_rows, ls_2d_array)
# pass to Thread Queue
Q.put(data_array)
def get_csv(controller):
'''
Function to grab path to CSV file, get number of lines, and start prog bar.
'''
# choose csvfile
csvpath = askopenfilename()
# error check
if csvpath == '':
print 'No File Selected'
return
# count lines
maxprogress = csv_linesum(csvpath)
# get file name
csv_name = get_filename(csvpath)
# error check
if not csv_name.lower().endswith('csv'):
print 'Non-CSV File Selected'
return
# generate unique name
tm = str(time.clock())
var_num = tm.split('.')
var_name = 'thread_{0}'.format(var_num[1])
# open Progressbar with exec() function
exec("%s=Tkinter.Toplevel()" % var_name)
exec("%s.title(\'Conversion Progress\')" % var_name)
exec("%s.geometry(\'500x75\')" % var_name)
exec("progress = ttk.Progressbar(%s,orient=\'horizontal\',"
"mode=\'determinate\')"
% var_name)
progress.pack(expand=True, fill='both', side='top')
progress["maximum"] = maxprogress
progress["value"] = 0
status = 'Converting: {0}'.format(csv_name)
exec("label = ttk.Label(%s,text=status,font=%s,anchor=\'center\')"
% (var_name, LG_FONT))
label.pack(expand=True, fill='both', side='top')
# center window
exec("controller.eval(\'tk::PlaceWindow %%s center\' %% "
"%s.winfo_pathname(%s.winfo_id()))"
% ((var_name,)*2))
# queue for thread values
Q = Queue.LifoQueue()
# run conversion thread
my_thread = threading.Thread(target=csv2hdf5, args=(csvpath, Q))
my_thread.start()
# start controller.after cycle
exec("update_progbar(progress, Q, %s, controller)" % var_name)
def get_hdf5(controller):
'''
Function to allow selecting of HDF5 file, generating heatmap for file (by
calling gen_heatmap()), and navigating GUI page from the "MainView" page
to the "DataView" page.
'''
# choose HDF5 file
hdfpath = askopenfilename()
# error check
if hdfpath == '':
print 'No File Selected'
return
# get file name
hdf_name = get_filename(hdfpath)
# error check
if not hdf_name.lower().endswith(('hdf5', 'h5')):
print 'Non-HDF File Selected'
return
# get dataset names
dnames = [dset for dset in gen_hdf5_dnames(hdfpath)]
# get dataset length
dlen = dataset_length(hdfpath)
# offer user choice of dataset for heatmap coloring
h = HeatmapDataSource(controller, dnames, hdfpath, dlen)
# # count lines
# maxprogress = hdf5_linesum(hdfpath)
def back_to_main(controller):
'''
Function that removes current GUI objects to navigate back to main page.
'''
controller.canvas['DataViewCanvas'].destroy()
controller.frames['DataView'].colorbar_button.destroy()
controller.title('PyVisualize')
controller.show_frame('MainView')
# class def
class RootWindow(Tkinter.Tk):
'''
Class for main Tkinter root object.
'''
# constructor
def __init__(self, *args, **kwargs):
# instantiate Tk() object
Tkinter.Tk.__init__(self, *args, **kwargs)
self.title('PyVisualize')
rootframe = ttk.Frame(self)
rootframe.pack(side='top', fill='both', expand=True)
rootframe.grid_rowconfigure(0, weight=1)
rootframe.grid_columnconfigure(0, weight=1)
# dictionary of frame objects
self.frames = {}
# dictionary of canvas objects
# NOTE: to be filled by gen_heatmap()
self.canvas = {}
# populate frames dict
frame_list = ('DataView', 'MainView')
for frame in frame_list:
exec('obj_frame = %s(rootframe,self)' % frame)
exec('self.frames[\'%s\'] = obj_frame' % frame)
obj_frame.grid(row=0, column=0, stick='nsew')
# show frame
self.show_frame('MainView')
# center window
self.eval('tk::PlaceWindow %s center' %
self.winfo_pathname(self.winfo_id()))
def show_frame(self, cont, msg=''):
'''
Function to move pages of the GUI.
'''
select_frame = self.frames[cont]
select_frame.tkraise()
logging.info('Showing: {0} {1}'.format(cont, msg))
class MainView(ttk.Frame):
'''
Class for main GUI menu, showing options (e.g. convert CSV -> HDF5)
'''
# constructor
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent, padding=((24,)*4))
self.grid(column=5, row=12, sticky='nsew')
# loading python image
self.gif = Tkinter.PhotoImage(file=RTPath + '/media/python_dark.gif')
self.py_logo = ttk.Label(self, image=self.gif)
self.py_logo.pack(pady=20)
# frame for grouping buttons
self.btn_frame = ttk.Frame(self)
self.btn_frame.pack()
# convert file.csv to file.hdf5
self.csv_2_hdf5_button = ttk.Button(self.btn_frame,
text='Convert CSV -> HDF5',
command=lambda: get_csv(controller)
).pack(side='left', padx=5)
# open and view file.hdf5 contents
self.view_hdf5_button = ttk.Button(self.btn_frame, text='Open HDF5',
command=lambda: get_hdf5(controller)
).pack(side='left', padx=5)
class DataView(ttk.Frame):
'''
Class that enables the viewing of the heatmap of NetLogo simulation data.
'''
# constructor
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.pack()
# self.scrlbar = Tkinter.Scrollbar(self, orient='vertical')
# self.scrlbar.config(command=file_list.yview)
# self.scrlbar.pack(side='right', fill='y')
self.btn_frame = ttk.Frame(self)
self.btn_frame.pack()
# button back to main view
self.back_button = ttk.Button(self.btn_frame, text='<- Back',
command=lambda: back_to_main(controller))
self.back_button.pack(side='left')
# colorbar for heatmap
self.colorbar_button = None
class HeatmapDataSource(Tkinter.Toplevel):
'''
Class for generating data source choosing window for heatmap.
'''
# constructor
def __init__(self, root, dlist, fpath, dlen):
# create toplevel window
Tkinter.Toplevel.__init__(self, root)
self.title('Heatmap Data Source: {0}'.format(fpath.rsplit('/', 1)[1]))
# create variable
self.var = Tkinter.StringVar()
# store hdfpath
self.hdfpath = fpath
# store dset length
self.range = dlen - 1
# store ticks value from entry
self.timepoint = Tkinter.IntVar()
# store root window
self.root = root
# create entry box
self.entry = ttk.Entry(self, textvariable=self.timepoint)
self.timepoint.set('Enter Integer from 0 to {0}'.format(self.range))
self.entry.pack()
# loop over dlist to create checkboxes
for text in dlist:
c = Tkinter.Radiobutton(
self, text=text.strip('/'),
variable=self.var,
value=text)
c.pack(anchor='w')
# create submit button
self.submit = ttk.Button(self, text='submit',
command=lambda: self.get_choice())
self.submit.pack(side='bottom')
# bind to HeatmapDataSource widget
self.bind("<Return>", self.get_choice)
# center window
self.root.eval('tk::PlaceWindow %s center' %
self.winfo_pathname(self.winfo_id()))
def get_choice(self, event=None):
'''
Function to generate heatmap from chosen data source.
'''
# check for int
try:
ticks = self.timepoint.get()
except:
return
# check for empty var
dataset = self.var.get()
if dataset is not '' and ticks <= self.range and ticks >= 0:
# generate heatmap canvas
# NOTE: Need to refactor WITHOUT threads ...
dataQ = Queue.Queue()
read_hdf5(self.hdfpath, dataQ, '/'+dataset, ticks)
# generate heatmap
gen_heatmap(self.root, dataQ, self.hdfpath)
# show 'DataView' page
left = 'Heatmap({0})'.format(dataset)
right = 'TimePoint({0})'.format(ticks)
title = 'PyVisualize: ' + left + ' | ' + right
self.root.title(title)
self.root.show_frame('DataView', '{0} Heatmap'.format(dataset))
# then close window
self.destroy()
# executable
if __name__ == '__main__':
# launch
app = RootWindow()
app.mainloop()