-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.py
1995 lines (1705 loc) · 82.4 KB
/
plot.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
996
997
998
999
1000
import matplotlib.pyplot as plt
from numpy import polyfit as pf
import math
import ply.lex as lex
import sys
import ply.yacc as yacc
import numpy as np
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage, AnnotationBbox)
from matplotlib.patches import Rectangle
from collections import defaultdict
import collections
import pickle
import os
def plotter (fig,canvas,v):
# plotter function called after update button is pressed
# plots the graph on the canvas
# fig is the figure object of matplotlib
# canvas is the canvas object of matplotlib
# fig and canvas object passed to the function are to be maniulated to draw the graph (fig/canvas can be considered the place where the graph is drawn)
# variables used ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
details=[] # details for hovering
line = [] # list of line objects of matplotlib
plotPointsX = [] # list of list of x,y,z points that is passed to plot function of matplotlib to plot the graph
plotPointsY = []
plotPointsZ = []
partitionedPoints = [] # lift of lists of points partitioned by shape and colour
fieldLengthList = [] # list of lengths of fieldnames in the .csv (input file)
fieldList = [] # list fields in the .csv (input file)
fieldNumber = 1 # no. of fields in .csv (input file)
fileRow = [] # a row in .csv (input file)
fileRowNumber = 0 # no. of rows in .csv (input file)
dataBase = [] # 2d list which stores the whole .csv (input file) (except the first row that contains field names)
style = ['.','^','h','H','>','<','x','+','p','d','8'] # list of initialized marker styles
colour = ['r','g','b','c','m','y','k','chartreuse'] # list of initialized marker colors
patterns = [ "/" , "\\" , "-" , "x", "o", "O","." "*" ]
distinctVals3 = [] #for bar graph(the distinct values)
distinctValues1 = 0 # no. of 3rd para (shape) values
distinctValues2 = 0 # no. of 3rd para (shape) values
distinctValues3 = 0 # no. of x-axis values (needed if we are making a bar graph)
plotLines1 = []
plotLines2 = []
plotULimit = [] # upper limits selected on fields in column filtering / upper limit in .csv
plotLLimit = [] # lower limits selected on fields in column filtering / lower limit in .csv
numberFields = [] # fieldname of columns containing numerical data
configLines=[] # list holding data for settings and options selected in UI
yPoints = []
xPoints = []
zPoints = []
colNumX = -1 # column no. for x,y,z axes
colNumY = -1
colNumZ = -1
xDict = {} # dictionary to hold co-ordinates in case of non numerical data
yDict = {}
zDict = {}
filterZPoints = []
filterPoints = []
paretoPoints = []
y_start_prev = 0 #for retaining the y and x axis limit
x_start_prev = 0
y_end_prev = 0
x_end_prev = 0
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def stringParse(x):
# function that parses the input entered for custom formula for y axis (based on lex and yacc; alternatively regex can be used)
# x is the string to be parsed
if sys.version_info[0] >= 3:
raw_input = input
tokens = (
'NAME', 'NUMBER',
)
literals = ['=', '+', '-', '*', '/', '(', ')']
# Tokens
@lex.TOKEN('|'.join(numberFields)) # adding filed names as token values
def t_NAME(t):
#print (t.value)
for j in numberFields :
if t.value == j:
t.value = [float(i[fieldList.index(j)]) for i in dataBase] # setting the value as the whole column that is specified in the formula
break
return t
def t_NUMBER(t):
r'\d+'
t.value = int(t.value) # setting the value as the integer specified in the formula
return t
t_ignore = " \t"
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
def t_error(t):
print("Illegal character '%s'" % t.value[0])
f = open("erLog.txt",'w') # setting up the flag in the erLog file in case of incorrect formula
f.write(x) # this is done as the variable scope is diff. in this function and return values go somewhere else
f.close()
t.lexer.skip(1)
# Build the lexer
lex.lex()
# Parsing rules
precedence = (
('left', '+', '-'),
('left', '*', '/'),
('right', 'UMINUS'),
)
# dictionary of names
names = {}
def p_statement_expr(p):
'statement : expression'
f = open("yP.txt",'w') # setting the final values in the yP.txt
for i in p[1] :
f.write("%s," % i)
f.close()
def p_expression_binop(p):
'''expression : expression '+' expression
| expression '-' expression
| expression '*' expression
| expression '/' expression'''
if p[2] == '+':
p[0] = [i + j for i, j in zip(p[1], p[3])] # calculating the values according the formula
elif p[2] == '-':
p[0] = [i - j for i, j in zip(p[1], p[3])]
elif p[2] == '*':
p[0] = [i * j for i, j in zip(p[1], p[3])]
elif p[2] == '/':
p[0] = [i / j for i, j in zip(p[1], p[3])]
def p_expression_uminus(p):
"expression : '-' expression %prec UMINUS"
p[0] = [-i for i in p[2]] # calculating the values according the formula
def p_expression_group(p):
"expression : '(' expression ')'"
p[0] = p[2] # calculating the values according the formula
def p_expression_number(p):
"expression : NUMBER"
p[0] = [p[1] for i in range(fileRowNumber)] # calculating the values according the formula
def p_expression_name(p):
"expression : NAME" # calculating the values according the formula
p[0] = p[1]
def p_error(p):
if p:
print("Syntax error at '%s'" % p.value)
else:
print("Syntax error at EOF")
f = open("erLog.txt",'w') # setting the flag in erLog.txt
f.write(x)
f.close()
yacc.yacc()
yacc.parse(x)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# reading out_cfg.csv to get the settings from UI
f = open("out_cfg.csv",'rU')
while True :
configLine = f.readline().split(",") # settings are stored as comma seperated values
if configLine == [""]:
break
configLines.append(configLine) # settings are stored in the list configLines[0]
# configLine details
# configLines[0][0] --> name of input/.csv file
# configLines[0][1] --> name of x axis column/field
# configLines[0][2] --> name of y axis column/field
# configLines[0][3] --> if 1 then 3d enabled
# configLines[0][4] --> name of the z axis column/field
# configLines[0][5] --> name of fourth para
# configLines[0][6] --> name of third para
# configLines[0][7] --> title if not empty
# configLines[0][8] --> custom formula enabled if t
# configLines[0][9] --> lower limit x axis
# configLines[0][10] --> upper limit x axis
# configLines[0][11] --> lower limit y axis
# configLines[0][12] --> upper limit y axis
# configLines[0][13] --> lower limit z axis
# configLines[0][14] --> upper limit z axis
# configLines[0][15] --> no of numerical fields in the .csv(input file)
# configLines[0][16] to configLines[0][x] --> fieldname , upper limit (selected in column filtering) , lower limit (selected in column filtering) (for numerical fields)
# configLines[0][-12] --> color or pattern for bars in bar graph and histogram
# configLines[0][-11] --> empty if resize axes automatically ( default case )
# configLines[0][-10] --> empty if y value start value is not given
# configLines[0][-9] --> empty if no value on bars
# configLines[0][-8] --> x label if not empty
# configLines[0][-7] --> y label if not empty
# configLines[0][-6] --> Normalization or not in histogram
# configLines[0][-5] --> Minimum, Average, Maximum for bargraph
# configLines[0][-4] --> type of graph (line/scatter/histogram/bar)
# configLines[0][-3] --> empty for curve fit disabled else degree of polynomial
# configLines[0][-2] --> Interval range(for histogram)
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
fig.clf()
# clear the previous graph
if(os.path.isfile("scriptdump.py") == True):
os.remove("scriptdump.py")
the_file = open('scriptdump.py', 'a')
os.chmod('scriptdump.py', 0777)
#everystatement with the_file is for the script(containing the code for plot) that is to be dumped
the_file.write('import matplotlib.pyplot as plt\n')
the_file.write('from numpy import polyfit as pf\n')
the_file.write('import math\n')
the_file.write('import ply.lex as lex\n')
the_file.write('import sys\n')
the_file.write('import ply.yacc as yacc\n')
the_file.write('import numpy as np\n')
the_file.write('from PyQt4.QtCore import *\n')
the_file.write('from PyQt4.QtGui import *\n')
the_file.write('from PyQt4 import QtGui, QtCore\n')
the_file.write('from mpl_toolkits.mplot3d import Axes3D\n')
the_file.write('from mpl_toolkits.mplot3d import proj3d\n')
the_file.write('from matplotlib.offsetbox import (TextArea, DrawingArea, OffsetImage, AnnotationBbox)\n')
the_file.write('from matplotlib.patches import Rectangle\n')
the_file.write('from collections import defaultdict\n')
the_file.write('import collections\n')
the_file.write('import pickle\n')
the_file.write('import os\n')
the_file.write('\n')
the_file.write('fig = plt.figure()\n')
# self.canvas = FigureCanvas(self.figure)
if configLines[0][3] == '1' : # check if 3d
ax = fig.add_subplot(111,projection = '3d')
the_file.write('ax = fig.add_subplot(111,projection = \'3d\')\n') # add 3d plot
else :
ax = fig.add_subplot(111)
the_file.write('ax = fig.add_subplot(111)\n')
if(os.path.isfile('objs.pkl') == True):
with open('objs.pkl') as f: # Python 3: open(..., 'rb')
y_start_prev, y_end_prev,x_start_prev,x_end_prev = pickle.load(f)
# add 2d plot
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# this part handles the on hovering details
offsetbox = TextArea("Test 1",textprops=dict(size=7.5) ,minimumdescent=False) # offset box object initially set to invisible
xybox=(75., 75.)
ab = AnnotationBbox(offsetbox, (0,0), xybox=xybox, xycoords='data', boxcoords="offset points", pad=0.3, arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab)
ab.set_visible(False)
# function that displays the offset/annotation box when hovering
def hover(event):
j = 0
strr = ""
if v == 0:
for i in line :
index = -1
if i.contains(event)[0]:
l = len(dataBase[details[j][i.contains(event)[1]["ind"][0]]])
for k in range (l-1):
strr += fieldList[k] + " : " + dataBase[details[j][i.contains(event)[1]["ind"][0]]][k] + "\n"
strr+= fieldList[k+1] + " : " + dataBase[details[j][i.contains(event)[1]["ind"][0]]][k+1]
index = details[j][i.contains(event)[1]["ind"][0]]
w,h = fig.get_size_inches()*fig.dpi
ws = (event.x > w/2.)*-1 + (event.x <= w/2.)
hs = (event.y > h/2.)*-1 + (event.y <= h/2.)
ab.xybox = (xybox[0]*ws, xybox[1]*hs)
ab.set_visible(True)
ab.xy = (xPoints[details[j][i.contains(event)[1]["ind"][0]]],yPoints[details[j][i.contains(event)[1]["ind"][0]]])
offsetbox.set_text(strr)
break
else:
ab.set_visible(False)
j += 1
fig.canvas.draw_idle()
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
i = 0
while i < float(configLines[0][15]) : # setting the values of numberFields,plotLLimit,plotULimit using configLines[0] (see related descriptions above)
numberFields.append(configLines[0][3*i+16])
plotLLimit.append(float(configLines[0][3*i+17]))
plotULimit.append(float(configLines[0][3*i+18]))
i += 1
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# open the input(.csv ) file to get the data
try: # parse the first line of the input file to get field/column names, no. of fields etc. (see description of variables above)
f = open(configLines[0][0],'r')
fileFlag = 1 # variable to know if end of 1st line has been reached
fieldLength = 0
while fileFlag == 1:
ch = f.read(1)
if ch == ',':
# add fieldLength to the list
fieldLengthList.append(fieldLength)
fieldLength = 0
fieldNumber = fieldNumber + 1
elif ch == '\n':
# add fieldLength to the list
fieldLengthList.append(fieldLength)
fileFlag = 0
else:
fieldLength = fieldLength + 1
finally:
f.close()
try:
f = open(configLines[0][0],'r')
i = 0
while i < fieldNumber :
st = f.read(fieldLengthList[i])
fieldList.append(st) # read the field/column names into a list
f.read(1)
i = i + 1
while True : # read the entire .csv into a 2d list
fileRow=f.readline().split(",")
if fileRow == [""]:
break
fileRowNumber = fileRowNumber + 1
try:
fileRow.remove('\n')
except:
pass
dataBase.append(fileRow)
finally:
f.close()
#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cType = configLines[0][-4] # type of graph - line or scatter
if configLines[0][-3] !='': # set the degree of polynomial for curve fitting
curveFit = 'True'
deg = configLines[0][-3]
#print "deg = " + deg
else:
curveFit = 'False'
#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# getting the x,y,z values that are to be parsed
# X
colNumX = fieldList.index(configLines[0][1])
try :
numberFields.index(configLines[0][1]) # check if the x axis selected is numerical
xPoints = [i[colNumX] for i in dataBase]
except ValueError : # if non numerical make key value pair in dict (key = x value; value = x co-ordinate)
i = 0
j = 0
while i < fileRowNumber :
if xDict.get(dataBase[i][colNumX]) == None :
xDict[dataBase[i][colNumX]] = j
xPoints.append(j)
j += 1
else :
xPoints.append(xDict.get(dataBase[i][colNumX]))
i += 1
# Y
try :
colNumY = fieldList.index(configLines[0][2]) # check if the y axis supplied is a field or custom frmula
try :
numberFields.index(configLines[0][2]) # if field then check if it is numerical
yPoints = [i[colNumY] for i in dataBase]
except ValueError : # if non numerical make key value pair in dict (key = y value; value = y co-ordinate)
i = 0
j = 0
while i < fileRowNumber :
if yDict.get(dataBase[i][colNumY]) == None :
yDict[dataBase[i][colNumY]] = j
yPoints.append(j)
j += 1
else :
yPoints.append(yDict.get(dataBase[i][colNumY]))
i += 1
except : # if custom formula call stringParse function and open the yP.txt to get processed y values
stringParse(configLines[0][2])
f = open('yP.txt','r')
yPoints = f.readline().split(",")
yPoints.pop()
f.close()
# Z
if configLines[0][3] == '1' : # if 3d enabled then same procedure as for x values
colNumZ = fieldList.index(configLines[0][4])
try :
numberFields.index(configLines[0][4])
zPoints = [i[colNumZ] for i in dataBase]
except ValueError :
i = 0
j = 0
while i < fileRowNumber :
if zDict.get(dataBase[i][colNumZ]) == None :
zDict[dataBase[i][colNumZ]] = j
zPoints.append(j)
j += 1
else :
zPoints.append(zDict.get(dataBase[i][colNumZ]))
i += 1
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# checking for third(shape) parameter (only works for scatter graph)
if configLines[0][5] != '' and (cType == 'scatter' or cType == 'bar-graph') : # check if 3rd para enabled
enaDiff1 = fieldList.index(configLines[0][5]) # enaDiff1 - variable to store the field name of 3rd para
distinctVals1 = [] # list of distinct values in the 3rd para field
i = 0
try: # set distinctVals1 and distinctValues1
while i < fileRowNumber :
try:
tempIndex = distinctVals1.index(dataBase[i][enaDiff1])
except:
distinctVals1.append(dataBase[i][enaDiff1])
distinctValues1 += 1
i += 1
except IndexError :
print (i)
else :
distinctValues1 = 1
# checking for fourth(color) parameter
distinctVals2 = []
if configLines[0][6] != '' : # check if 4th para enabled
enaDiff2 = fieldList.index(configLines[0][6]) # enaDiff2 - variable to store the field name of 4th para
i = 0
while i < fileRowNumber : # set distinctVals2 and distinctValues2
try:
tempIndex = distinctVals2.index(dataBase[i][enaDiff2])
except:
distinctVals2.append(dataBase[i][enaDiff2])
distinctValues2 += 1
i += 1
else :
distinctValues2 = 1
distinctVals2.sort()
i = 0
# based on the distinctValues1 and distinctValues2 plotPoints in partioned according to shape and color combination
if (configLines[0][3] == '1') :
while i < distinctValues1 * distinctValues2 :
plotPointsX.append([])
plotPointsY.append([])
plotPointsZ.append([])
partitionedPoints.append([])
details.append([])
i += 1
else :
while i < distinctValues1 * distinctValues2 :
plotPointsX.append([])
plotPointsY.append([])
partitionedPoints.append([])
details.append([])
i += 1
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# points stored in xPoints,yPoints and zPoints are detrmined to be valid or not based on column filtering, x axis sliders, y axis sliders
# predicate value is evaluated - if true then the point satisfies the restrictions of column filtering, x axis sliders anf y axis sliders else false
# store these points in filterPoints or filterZPoints
i = 0
m=0
if configLines[0][3] != '1':
while i < fileRowNumber :
x = float(xPoints[i])
y = float(yPoints[i])
tValue = 'True'
if configLines[0][8] == 't':
if bool(xDict) :
predicate = 'True'
else :
predicate = (x >= (float)(configLines[0][9]) and x <= (float)(configLines[0][10]))
else :
if bool(xDict) and bool(yDict) :
predicate = 'True'
elif bool(xDict) and not bool(yDict) :
predicate = ( y >= (float)(configLines[0][11]) and y <= (float)(configLines[0][12]) )
elif not bool(xDict) and bool(yDict) :
predicate = (x >= (float)(configLines[0][9]) and x <= (float)(configLines[0][10]))
else :
predicate = (x >= (float)(configLines[0][9]) and x <= (float)(configLines[0][10]) and y >= (float)(configLines[0][11]) and y <= (float)(configLines[0][12]) )
if predicate :
j = 0
while j < float(configLines[0][15]) :
dat = float(dataBase[i][fieldList.index(numberFields[j])])
if (dat > plotULimit[j] or dat < plotLLimit[j]) :
tValue = 'False'
j += 1
else :
tValue = 'False'
if tValue == 'True':
filterPoints.append((x,y,i))
m += 1
i += 1
else :
while i < fileRowNumber :
x = float(xPoints[i])
y = float(yPoints[i])
z = float(zPoints[i])
tValue = 'True'
if configLines[0][8] == 't':
if not bool(xDict) :
if bool(zDict) :
predicate = (x >= (float)(configLines[0][9]) and x <= (float)(configLines[0][10]))
else :
predicate = (x >= (float)(configLines[0][9]) and x <= (float)(configLines[0][10]) and z >= (float)(configLines[0][13]) and z <= (float)(configLines[0][14]))
else :
if bool(zDict) :
predicate = 'True'
else :
predicate = (z >= (float)(configLines[0][13]) and z <= (float)(configLines[0][14]))
else:
if not bool(xDict) :
if bool(yDict) :
if bool(zDict) :
predicate = (x >= (float)(configLines[0][9]) and x <= (float)(configLines[0][10]))
else :
predicate = (x >= (float)(configLines[0][9]) and x <= (float)(configLines[0][10]) and z >= (float)(configLines[0][13]) and z <= (float)(configLines[0][14]))
else:
if bool(zDict) :
predicate = (x >= (float)(configLines[0][9]) and x <= (float)(configLines[0][10]) and y >= (float)(configLines[0][11]) and y <= (float)(configLines[0][12]))
else :
predicate = (x >= (float)(configLines[0][9]) and x <= (float)(configLines[0][10]) and y >= (float)(configLines[0][11]) and y <= (float)(configLines[0][12]) and z >= (float)(configLines[0][13]) and z <= (float)(configLines[0][14]))
else :
if bool(yDict) :
if bool(zDict) :
predicate = 'True'
else :
predicate = (z >= (float)(configLines[0][13]) and z <= (float)(configLines[0][14]))
else:
if bool(zDict) :
predicate = ( y >= (float)(configLines[0][11]) and y <= (float)(configLines[0][12]))
else :
predicate = ( y >= (float)(configLines[0][11]) and y <= (float)(configLines[0][12]) and z >= (float)(configLines[0][13]) and z <= (float)(configLines[0][14]))
if predicate :
j = 0
while j < float(configLines[0][15]) :
dat = float(dataBase[i][fieldList.index(numberFields[j])])
if (dat > plotULimit[j] or dat < plotLLimit[j]) :
tValue = 'False'
j += 1
else :
tValue = 'False'
if tValue == 'True':
filterPoints.append((x,y,i))
filterZPoints.append((z,i))
m += 1
i += 1
# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
i = 0
if configLines[0][3] == '1' :
if distinctValues1 > 1 and distinctValues2 > 1 :
for k in filterPoints :
dex = distinctVals1.index(dataBase[k[2]][enaDiff1])*distinctValues2 + distinctVals2.index(dataBase[k[2]][enaDiff2])
plotPointsX[dex].append(k[0])
plotPointsY[dex].append(k[1])
plotPointsZ[dex].append(filterZPoints[i][0])
details[dex].append(k[2])
i+=1
elif distinctValues1 > 1:
for k in filterPoints :
dex = distinctVals1.index(dataBase[k[2]][enaDiff1])
plotPointsX[dex].append(k[0])
plotPointsY[dex].append(k[1])
plotPointsZ[dex].append(filterZPoints[i][0])
details[dex].append(k[2])
i += 1
elif distinctValues2 > 1:
for k in filterPoints :
dex = distinctVals2.index(dataBase[k[2]][enaDiff2])
plotPointsX[dex].append(k[0])
plotPointsY[dex].append(k[1])
plotPointsZ[dex].append(filterZPoints[i][0])
details[dex].append(k[2])
i += 1
else :
for k in filterPoints :
plotPointsX[0].append(k[0])
plotPointsY[0].append(k[1])
plotPointsZ[0].append(filterZPoints[i][0])
details[0].append(k[2])
i += 1
if configLines[0][3] == '4' :
if distinctValues1 > 1 and distinctValues2 > 1 :
for k in filterPoints :
dex = distinctVals1.index(dataBase[k[2]][enaDiff1])*distinctValues2 + distinctVals2.index(dataBase[k[2]][enaDiff2])
plotPointsX[dex].append(k[0])
plotPointsY[dex].append(k[1])
details[dex].append(k[2])
elif distinctValues1 > 1:
for k in filterPoints :
dex = distinctVals1.index(dataBase[k[2]][enaDiff1])
plotPointsX[dex].append(k[0])
plotPointsY[dex].append(k[1])
details[dex].append(k[2])
elif distinctValues2 > 1:
for k in filterPoints :
dex = distinctVals2.index(dataBase[k[2]][enaDiff2])
plotPointsX[dex].append(k[0])
plotPointsY[dex].append(k[1])
details[dex].append(k[2])
else :
for k in filterPoints :
plotPointsX[0].append(k[0])
plotPointsY[0].append(k[1])
details[0].append(k[2])
if configLines[0][3] == '3' :
if distinctValues1 > 1 and distinctValues2 > 1 :
for k in filterPoints :
dex = distinctVals1.index(dataBase[k[2]][enaDiff1])*distinctValues2 + distinctVals2.index(dataBase[k[2]][enaDiff2])
partitionedPoints[dex].append(k)
elif distinctValues1 > 1:
for k in filterPoints :
dex = distinctVals1.index(dataBase[k[2]][enaDiff1])
partitionedPoints[dex].append(k)
elif distinctValues2 > 1:
for k in filterPoints :
dex = distinctVals2.index(dataBase[k[2]][enaDiff2])
partitionedPoints[dex].append(k)
else :
for k in filterPoints :
partitionedPoints[0].append(k)
ind = 0
for colourList in partitionedPoints:
paretoPoints =[]
if configLines[0][4] == '1' :
tempList = sorted(colourList)
paretoPoints.append(tempList[0])
j = 0
for x_,y_,z_ in tempList :
if y_ >= paretoPoints[j][1] :
paretoPoints.append((x_,y_,z_))
j+=1
paretoPoints.pop(0)
for k in paretoPoints:
plotPointsX[ind].append(k[0])
plotPointsY[ind].append(k[1])
details[ind].append(k[2])
ind += 1
elif configLines[0][4] == '2' :
tempList = sorted(colourList)
paretoPoints.append(tempList[0])
j = 0
for x_,y_,z_ in tempList :
if y_ <= paretoPoints[j][1] :
paretoPoints.append((x_,y_,z_))
j+=1
paretoPoints.pop(0)
for k in paretoPoints:
plotPointsX[ind].append(k[0])
plotPointsY[ind].append(k[1])
details[ind].append(k[2])
ind += 1
elif configLines[0][4] == '3' :
tempList = sorted(colourList,reverse=True)
paretoPoints.append(tempList[0])
j = 0
for x_,y_,z_ in tempList :
if y_ >= paretoPoints[j][1] :
paretoPoints.append((x_,y_,z_))
j+=1
paretoPoints.pop(0)
for k in paretoPoints:
plotPointsX[ind].append(k[0])
plotPointsY[ind].append(k[1])
details[ind].append(k[2])
ind += 1
elif configLines[0][4] == '4' :
tempList = sorted(colourList,reverse=True)
paretoPoints.append(tempList[0])
j = 0
for x_,y_,z_ in tempList :
if y_ <= paretoPoints[j][1] :
paretoPoints.append((x_,y_,z_))
j+=1
paretoPoints.pop(0)
for k in paretoPoints:
plotPointsX[ind].append(k[0])
plotPointsY[ind].append(k[1])
details[ind].append(k[2])
ind += 1
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# checking for number of distinct values of x-axis for making a bar-graph or histogram or bar-scatter(should be smaller than 8 for this to be possible)
if cType == "bar-scatter":
enaDiff3 = fieldList.index(configLines[0][1])
distinctVals3 = [] # list of distinct values in the 4th para field
i = 0
while i < fileRowNumber : # set distinctVals2 and distinctValues2
try:
tempIndex = distinctVals3.index(dataBase[i][enaDiff3])
except:
distinctVals3.append(dataBase[i][enaDiff3])
distinctValues3 += 1
if distinctValues3 > 8: #if we get more than 8 values we cannot make the bar-graph
distinctValues3 = float("inf")
break
i += 1
if distinctValues3 < 8 and distinctValues3 > 1:
distinctVals3.sort()
min_diff_x = float("inf")
for index_diff in range(1,len(distinctVals3)):
if float(distinctVals3[index_diff]) - float(distinctVals3[index_diff-1]) < min_diff_x:
min_diff_x = float(distinctVals3[index_diff]) - float(distinctVals3[index_diff-1])
elif(cType == "bar-graph" or cType == "histogram"):
len_dict = {}
len_dict = defaultdict(lambda:0,len_dict)
for pp in plotPointsX:
for value in pp:
len_dict[value] = len_dict[value] + 1
if(len(len_dict)>8):
distinctValues3 = float("inf")
break;
else:
distinctValues3 = len(len_dict)
final_vals = collections.OrderedDict(sorted(len_dict.items()))
distinctVals3 = list(final_vals.keys())
else :
distinctValues3 = float("inf")
# check if any error else make the graph and display
yString = ''
try:
f = open('erLog.txt','r')
yString = f.readline()
f.close()
except:
pass # check if incorrect formula
if yString == configLines[0][2]:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Incorrect Formula")
msg.setWindowTitle("Error")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
elif distinctValues1 > 8: # check if 3rd para exceeds limits
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Third Parameter has too many values")
msg.setWindowTitle("Error")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
elif distinctValues2 > 8: # check if 4th para exceeds limits
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Fourth Parameter has too many values")
msg.setWindowTitle("Error")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
elif distinctValues3 > 8 and cType == "bar-graph":
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("X-Axis has too many values for a bar-graph")
msg.setWindowTitle("Error")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
elif distinctValues3 > 8 and cType == "bar-scatter":
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("X-Axis has too many values for a bar-scatter")
msg.setWindowTitle("Error")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
elif configLines[0][-10] !='' and configLines[0][-11] != '':
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Cannot select both \'Set y-start value\'' and \'Do not automatically resize axes\'")
msg.setWindowTitle("Error")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
elif configLines[0][-11] != '' and y_start_prev == 0 and y_end_prev == 0 :
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setText("Cannot select \'Do not automatically resize axes\' for the first plot as no previous plot found")
msg.setWindowTitle("Error")
msg.setStandardButtons(QMessageBox.Ok)
msg.exec_()
else: # no error plot graph
the_file.write('patterns = ' + str(patterns) + '\n')
the_file.write('colour = ' + str(colour) + '\n' )
the_file.write('style = [\'.\',\'^\',\'h\',\'H\',\'>\',\'<\',\'x\',\'+\',\'p\',\'d\',\'8\']\n')
#------------------------------------------------------------------------------------------------------------------------------------------------
if cType == 'histogram':
if(configLines[0][-8] != ''):
ax.set_xlabel(configLines[0][-8])
the_file.write('ax.set_xlabel(' + '\'' + configLines[0][-8] + '\')\n')
else:
ax.set_xlabel(configLines[0][1])
the_file.write('ax.set_xlabel(' + '\'' + configLines[0][1] + '\')\n')
ax.set_ylabel("Frequency")
the_file.write('ax.set_ylabel("Frequency")\n')
ax.set_title(configLines[0][7])
the_file.write('ax.set_title(' + '\'' + configLines[0][7] + '\')\n')
#with third parameter(color or pattern)
if(configLines[0][6]!=''):
#when x distinct values < 8, we will have bars
if (distinctValues3 < 8) :
opacity = 0.8
bar_width = (0.2 *16) /(len(distinctVals3)*len(distinctVals2))
y_pos = np.arange(len(distinctVals3))
ax1 = plt.subplot(111)
ax1.bar(y_pos,y_pos)
rects_temp = ax1.patches
for rect in rects_temp:
bar_width = rect.get_width()
break;
ax1.clear()
bar_width = bar_width/len(distinctVals2)
the_file.write('bar_width = ' + str(bar_width) + '\n')
the_file.write('distinctVals2 = ' + str(distinctVals2) + '\n')
the_file.write('distinctVals3 = ' + str(distinctVals3) + '\n')
y_list = []
if(configLines[0][-6] == "Yes"): #when normalizations true
ax.set_ylabel("Probability")
the_file.write('ax.set_ylabel("Probability")\n')
for q in range(len(distinctVals2)):
dict_x = {}
dict_x = defaultdict(lambda:0,dict_x)
for key in distinctVals3:
dict_x[key] = 0
for point in plotPointsX[q]:
dict_x[point] = dict_x[point] + 1
if(configLines[0][-6] == "Yes"):
for key in dict_x:
dict_x[key] = float(1.0*dict_x[key]/len(plotPointsX[0]))
final_d = collections.OrderedDict(sorted(dict_x.items()))
ys = list(final_d.values())
y_list.append(ys)
if configLines[0][-12] == 'Pattern':
ax.bar(y_pos + q*bar_width,ys, bar_width,
alpha = opacity,
color='yellow', edgecolor='black', hatch=patterns[q],
label = distinctVals2[q])
else: ax.bar(y_pos + q*bar_width,ys, bar_width,
alpha = opacity,
color= colour[q],
label = distinctVals2[q])
the_file.write('opacity = ' + str(opacity) + '\n')
with open('objs_sd.pkl', 'w') as f:
pickle.dump([y_pos, y_list], f)
the_file.write('with open(\'objs_sd.pkl\') as f:'+'\n'+'\t'+'y_pos, y_list = pickle.load(f)\n')
the_file.write('for q in range(len(distinctVals2)):\n')
the_file.write('\t')
if configLines[0][-12] == 'Pattern':
the_file.write('ax.bar(y_pos + q*bar_width, y_list[q], bar_width, alpha = opacity, color=\'yellow\', edgecolor=\'black\', hatch=patterns[q], label = distinctVals2[q])\n')
else:
the_file.write('ax.bar(y_pos + q*bar_width, y_list[q], bar_width, alpha = opacity, color= colour[q], label = distinctVals2[q])\n')
if(configLines[0][-9] != ''):
the_file.write('rects = ax.patches\n')
rects = ax.patches
# For each bar: Place a label
the_file.write('for rect in rects:\n')
the_file.write('\ty_value = rect.get_height()\n')
the_file.write('\tx_value = rect.get_x() + rect.get_width() / 2\n')
the_file.write('\tlabel = "{:.2f}".format(y_value)\n')
the_file.write('\tplt.annotate(label,(x_value, y_value),xytext=(0, 12),textcoords="offset points",ha=\'center\',va=\'top\')\n')
for rect in rects:
y_value = rect.get_height()
x_value = rect.get_x() + rect.get_width() / 2
label = "{:.2f}".format(y_value)
plt.annotate(
label, # Use `label` as label
(x_value, y_value), # Place label at end of the bar
xytext=(0, 12), # Vertically shift label by `space`
textcoords="offset points", # Interpret `xytext` as offset in points
ha='center', # Horizontally center label
va='top')
plt.xticks(y_pos + len(distinctVals2)*bar_width/2, distinctVals3) #to place the labels at correct positions on x axis
the_file.write('plt.xticks(y_pos + len(distinctVals2)*bar_width/2, distinctVals3) #to place the labels at correct positions on x axis\n')
ax.legend(loc = 'best',title = configLines[0][6])
the_file.write('ax.legend(loc = \'best\',title = \'' + configLines[0][6] + '\')\n')
#when continues data on x axis
else:
num_bins = 5
lb = min(plotPointsX[0])
ub = max(plotPointsX[0])
if(configLines[0][-2] != ''):
interval_size = float(configLines[0][-2])
num_bins = int ( math.ceil(1.0*(ub - lb)/interval_size) )
else:
interval_size = (ub - lb)/num_bins
the_file.write('num_bins = ' + str(num_bins) + '\n')
the_file.write('distinctVals2 = ' + str(distinctVals2) + '\n')
# if(configLines[0][-12] == 'Pattern'):
# ax1 = plt.subplot(111)
# the_file.write('ax1 = plt.subplot(111)\n')
colr = ['yellow']*len(distinctVals2)
if(configLines[0][-6] == 'Yes'):
ax.set_ylabel("Probability")
the_file.write('ax.set_ylabel("Probability")\n')
weights = []
for q in range(len(distinctVals2)):
weights.append(np.ones_like(plotPointsX[q])/float(len(plotPointsX[q])))
with open('objs_sd.pkl', 'w') as f:
pickle.dump([plotPointsX, weights], f)
the_file.write('with open(\'objs_sd.pkl\') as f:'+'\n'+'\t'+'plotPointsX, weights = pickle.load(f)\n')
if(configLines[0][-12] == 'Pattern'):
counts, bins, patches = ax.hist(plotPointsX, weights=weights, bins=num_bins, color = colr, align = 'mid', label = distinctVals2)
the_file.write('colr = [\'yellow\']*len(distinctVals2)\n')
the_file.write('counts, bins, patches = ax.hist(plotPointsX, weights=weights, bins=num_bins, color = colr, align = \'mid\', label = distinctVals2)\n')
else:
counts, bins, patches = ax.hist(plotPointsX, weights=weights, bins=num_bins, align = 'mid', label = distinctVals2)
the_file.write('counts, bins, patches = ax.hist(plotPointsX, weights=weights, bins=num_bins, align = \'mid\', label = distinctVals2)\n')
else:
with open('objs_sd.pkl', 'w') as f:
pickle.dump(plotPointsX, f)
the_file.write('with open(\'objs_sd.pkl\') as f:'+'\n'+'\t'+'plotPointsX = pickle.load(f)\n')
if(configLines[0][-12] == 'Pattern'):
counts, bins, patches = ax.hist(plotPointsX, bins=num_bins, color = colr, align = 'mid', label = distinctVals2)
# ax1.clear()
the_file.write('colr = [\'yellow\']*len(distinctVals2)\n')
the_file.write('counts, bins, patches = ax.hist(plotPointsX, bins=num_bins, color = colr, align = \'mid\', label = distinctVals2)\n')
# the_file.write('ax1.clear()\n')
else:
counts, bins, patches = ax.hist(plotPointsX, bins=num_bins, align = 'mid', label = distinctVals2)
the_file.write('counts, bins, patches = ax.hist(plotPointsX, bins=num_bins, align = \'mid\', label = distinctVals2)\n')