-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.py
737 lines (445 loc) · 16.6 KB
/
operations.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
import os
import numpy as np
import cv2
from threading import Thread
import time
import math
from pathlib import Path
# Veri seti secildikten sonra kullaniciya gore uzerinde uygulanacak islemler
def DatasetOperations(CSVFile, columnOperation, rawOperation, methodsOperation):
# column bazli silme
if columnOperation!="Not":
for i in CSVFile:
nan=0
for j in CSVFile[i]:
if str(j) == "nan":
nan+=1
yuzdelik = 100 * (nan/len(CSVFile[i]))
if yuzdelik > float(columnOperation[1:]):
CSVFile.drop("{}".format(i), axis=1, inplace=True)
# satir bazli silme
if rawOperation!="Not":
columns = CSVFile.columns
for index, row in CSVFile.iterrows():
nan = 0
for cl in range(0,len(columns)):
if str(row[cl])=="nan":
nan+=1
if nan > int(rawOperation):
CSVFile = CSVFile.drop([int(index)])
# doldurma yontemleri
if methodsOperation!="Not":
if methodsOperation == "Mean":
for i in CSVFile:
count = 0
toplam = 0
for j in CSVFile[i]:
if str(j) != "nan":
toplam += int(j)
count+=1
deger = toplam/count
deger = int(deger)
for z, j in enumerate(CSVFile[i]):
if str(j) == "nan":
CSVFile[i][z] = deger
elif methodsOperation == "Mode":
for i in CSVFile:
dtta = []
dttb = []
for j in CSVFile[i]:
if str(j) == "nan":
continue
flag = None
for k, z in enumerate(dtta):
if j==z:
flag=True
dttb[k] = dttb[k]+1
break
if flag==None:
dtta.append(j)
dttb.append(1)
maxValue = max(dttb)
mode = -99
for k,j in enumerate(dttb):
if j==maxValue:
mode = dtta[k]
mode = int(mode)
for z, j in enumerate(CSVFile[i]):
if str(j) == "nan":
CSVFile[i][z] = mode
elif methodsOperation == "Median":
for name in CSVFile:
vektor = []
for j in CSVFile[name]:
if str(j) != "nan":
vektor.append(j)
vektor = sorted(vektor)
veriAdedi = len(vektor)
deger = -99
if veriAdedi % 2 == 1:
deger = vektor[veriAdedi // 2]
else:
i = veriAdedi // 2
deger = (vektor[i - 1] + vektor[i]) / 2
deger = int(deger)
for z, j in enumerate(CSVFile[name]):
if str(j) == "nan":
CSVFile[name][z] = deger
return CSVFile
def Mean(dicta, CSVFile, deger):
ind = deger.index("Mean")
degTemp = deger[0]
deger[0] = "Mean"
deger[ind] = degTemp
dicta = {"Column Name":[],"Mean":[]}
for i in CSVFile:
top = 0
count = 0
for j in CSVFile[i]:
count += 1
top += j
dicta["Column Name"].append(i)
dicta["Mean"].append(top/count)
return dicta, CSVFile, deger
def Median(dicta, CSVFile, deger):
ind = deger.index("Median")
degTemp = deger[0]
deger[0] = "Median"
deger[ind] = degTemp
dicta = {"Column Name":[],"Median":[]}
for name in CSVFile:
vektor = sorted(CSVFile[name])
veriAdedi = len(vektor)
if veriAdedi % 2 == 1:
dicta["Median"].append(vektor[veriAdedi // 2])
else:
i = veriAdedi // 2
dicta["Median"].append((vektor[i - 1] + vektor[i]) / 2)
dicta["Column Name"].append(name)
return dicta, CSVFile, deger
def Mode(dicta, CSVFile, deger):
ind = deger.index("Mode")
degTemp = deger[0]
deger[0] = "Mode"
deger[ind] = degTemp
dicta = {"Column Name":[],"Mode":[]}
for i in CSVFile:
dtta = []
dttb = []
for j in CSVFile[i]:
flag = None
for k, z in enumerate(dtta):
if j==z:
flag=True
dttb[k] = dttb[k]+1
break
if flag==None:
dtta.append(j)
dttb.append(1)
maxValue = max(dttb)
mode = []
for k,j in enumerate(dttb):
if j==maxValue:
mode.append(dtta[k])
dicta["Mode"].append(mode)
dicta["Column Name"].append(i)
return dicta, CSVFile, deger
def IQR(dicta, CSVFile, deger):
ind = deger.index("Interquartile range value (IQR)")
degTemp = deger[0]
deger[0] = "Interquartile range value (IQR)"
deger[ind] = degTemp
dicta = {"Column Name":[],"Interquartile range value (IQR)":[]}
for i in CSVFile:
IQR = np.percentile(CSVFile[i], 75) - np.percentile(CSVFile[i], 25)
dicta["Column Name"].append(i)
dicta["Interquartile range value (IQR)"].append(IQR)
return dicta, CSVFile, deger
def Outliers(dicta, CSVFile, deger):
ind = deger.index("Outliers")
degTemp = deger[0]
deger[0] = "Outliers"
deger[ind] = degTemp
dicta = {"Column Name":[],"Outliers":[]}
for i in CSVFile:
q1 = np.percentile(CSVFile[i], 25)
q3 = np.percentile(CSVFile[i], 75)
IQR = q3-q1
degerler = []
minn = q1 - IQR*1.5
maxx = q3 + IQR*1.5
for j in CSVFile[i]:
if j<minn or j>maxx:
degerler.append(j)
strr = "Min: "+str(minn)+", Max: "+str(maxx)+", Outliers: "+str(degerler)
dicta["Column Name"].append(i)
dicta["Outliers"].append(strr)
return dicta, CSVFile, deger
def FiveNumber(dicta, CSVFile, deger):
ind = deger.index("Five number summary")
degTemp = deger[0]
deger[0] = "Five number summary"
deger[ind] = degTemp
dicta = {"Column Name":[],"Five number summary":[]}
for i in CSVFile:
q1 = np.percentile(CSVFile[i], 25)
q3 = np.percentile(CSVFile[i], 75)
minn = min(CSVFile[i])
maxx = max(CSVFile[i])
vektor = sorted(CSVFile[i])
veriAdedi = len(vektor)
median = -99
if veriAdedi % 2 == 1:
median = vektor[veriAdedi // 2]
else:
x = veriAdedi // 2
median = (vektor[x - 1] + vektor[x]) / 2
strr = "Min: "+str(minn)+", Q1: "+str(q1)+", Median: "+str(median)+", Q3: "+str(q3)+", Max: "+str(maxx)
dicta["Column Name"].append(i)
dicta["Five number summary"].append(strr)
return dicta, CSVFile, deger
def VarianceStandardDeviation(dicta, CSVFile, deger):
ind = deger.index("Variance and standard deviation")
degTemp = deger[0]
deger[0] = "Variance and standard deviation"
deger[ind] = degTemp
dicta = {"Column Name":[],"Variance and standard deviation":[]}
for i in CSVFile:
varianceD = variance(CSVFile[i])
stdevD = stdev(CSVFile[i])
strr = "Variance: "+str(varianceD)+", Standard Deviation: "+str(stdevD)
dicta["Column Name"].append(i)
dicta["Variance and standard deviation"].append(strr)
return dicta, CSVFile, deger
def BoxChart(CSVFile, deger, dt_string):
ind = deger.index("Box Chart")
degTemp = deger[0]
deger[0] = "Box Chart"
deger[ind] = degTemp
img_np = []
for i in CSVFile:
q1 = np.percentile(CSVFile[i], 25)
q3 = np.percentile(CSVFile[i], 75)
vektor = sorted(CSVFile[i])
veriAdedi = len(vektor)
if veriAdedi % 2 == 1:
median = vektor[veriAdedi // 2]
else:
x = veriAdedi // 2
median = (vektor[x - 1] + vektor[x]) / 2
# resimler ayri bir fonksiyonda cizdirilir ve saklanir
img_np.append(CreateBoxChartImage([min(CSVFile[i]),q1,median, q3, max(CSVFile[i])], i))
# resimler cift halinde yatay sekilde birlestirilir
img_cift = []
for k, img in enumerate(img_np):
# ilk resim saklanir, ikinci resimle birlestirmek icin
if k%2==0:
new_image = img
# eger sutun sayisi tek ise beyaz bir resim olusturulur ve birlesitirlir
if len(img_np)%2==1 and k==len(img_np)-1:
temp = np.zeros((img.shape[0],img.shape[1],img.shape[2]), np.uint8)
temp[:,:] = [255,255,255]
new_image = np.concatenate((new_image, temp), axis=1)
img_cift.append(new_image)
# ilk resim ile ikinci resim yatayda birlestirilir ve liste de saklanır
else:
new_image = np.concatenate((new_image, img), axis=1)
img_cift.append(new_image)
# yatay da birlestirilen resimler dikeyde birlestirilir
cs = 0
for img in img_cift:
# ilk resim saklanır
if cs==0:
final_image = img
cs=1
# ilk ve diger resimler birlestirilir
else:
final_image = np.concatenate((final_image, img), axis=0)
# resim uniq bir sekilde kaydedilir.
img = os.path.join('static', 'imagesBoxChart/{}.png'.format(dt_string))
cv2.imwrite(img, final_image)
# ayri bir thred ile silinir, depolama icin tutmadim, server de sikinti cikartir
Thread(target=Check, args=(img,)).start()
return img, CSVFile, deger
def CreateBoxChartImage(dtt, name):
# boyutlar ve cizim icin gereklilikler
width = 500
height = 600
chanel = 3
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 0.5
color = (255, 0, 0)
thickness = 1
# bos beyaz sayfa
image = np.zeros((width,height,chanel), np.uint8)
image[:,:] = [255,255,255]
# yatay duz cizgi
image = cv2.line(image, (20,360), (500,360), color, 2)
# column name yazdirma
temp = (400-len(name))//2
image = cv2.putText(image, name, (temp, 40), font,
fontScale, color, thickness, cv2.LINE_AA)
# min-max normalizasyonu icin degerler
new_min = 50
new_max = 450
minn = min(dtt)
maxx = max(dtt)
q1_q3 = []
# bos beyaz sayfaya cizim yaptigim dongu
for i in range(len(dtt)):
# min-max normalizasyonu
bl = (dtt[i]-minn)/(maxx-minn)
deg = int(bl*(new_max-new_min)+new_min)
# degerleri yuvarlak ile cizdim
image = cv2.circle(image, (deg,360), 2, color, 2)
# degerleri yazdim
image = cv2.putText(image, str(dtt[i]), (deg-5, 390), font,
fontScale, color, thickness, cv2.LINE_AA)
# min max degerleri icin duz cizgi
if i==0 or i==4:
image = cv2.line(image, (deg,175), (deg,225), color, 2)
# min max dan q1 ve q3 'e duz cizgi
if i==1 or i==3:
if i==1:
image = cv2.line(image, (new_min,200), (int(deg),200), color, 1)
else:
image = cv2.line(image, (new_max,200), (int(deg),200), color, 1)
q1_q3.append(deg)
# median
if i==2:
image = cv2.line(image, (deg,150), (deg,250), (0,0,139), 2)
# dikdörtgen, q1 ve q3 e gore cizilir
image = cv2.rectangle(image, (q1_q3[0],150), (q1_q3[1],250), color, thickness)
return image
def Frequency(dicta, CSVFile, deger, dt_string):
ind = deger.index("Frequency")
degTemp = deger[0]
deger[0] = "Frequency"
deger[ind] = degTemp
dicta = {"Column Name":[],"Frequency":[]}
img_np = []
# frekans hesaplanir
for i in CSVFile:
dtt = []
for j in CSVFile[i]:
flag = None
for k, z in enumerate(dtt):
if j==z[0]:
flag=True
dtt[k][1] = dtt[k][1]+1
break
if flag==None:
dtt.append([j,1])
dicta["Column Name"].append(i)
dicta["Frequency"].append(dtt)
# resim ayri bir fonksiyonda cizdirilir ve bu listede depolanir
img_np.append(CreateFrekansImage(dtt, i))
# resimler cift halinde yatayda birlestirilir ve liste de saklanilir
img_cift = []
for k, img in enumerate(img_np):
# ilk resim saklanir ve ikinci resimle birlesitirmek icin tutulur
if k%2==0:
new_image = img
# eger sutunumuz tek ise bos beyaz resim olusturulur ve birlestirilir
if len(img_np)%2==1 and k==len(img_np)-1:
temp = np.zeros((img.shape[0],img.shape[1],img.shape[2]), np.uint8)
temp[:,:] = [255,255,255]
new_image = np.concatenate((new_image, temp), axis=1)
img_cift.append(new_image)
# sutunumuz cift ise ilk resimle birlestirilir ve listede depolanir
else:
new_image = np.concatenate((new_image, img), axis=1)
img_cift.append(new_image)
# olusturdugumuz yatayda ki cift resimleri dikey sekilde birlestiriyoruz
cs = 0
for img in img_cift:
# ilk resim
if cs==0:
final_image = img
cs=1
# ilk resim ve diger resimlerin birlestirilmesi
else:
final_image = np.concatenate((final_image, img), axis=0)
# uniq bir sekilde bu konuma resim yazdirilir
img = os.path.join('static', 'imagesFrequency/{}.png'.format(dt_string))
cv2.imwrite(img, final_image)
# ayri bir thred ile silinir, depolama icin tutmadim, server de sikinti cikartir
Thread(target=Check, args=(img,)).start()
return dicta, img, CSVFile, deger
def CreateFrekansImage(dtt, name):
# boyutlar ve cizim icin gereklilikler
width = 500
height = 600
chanel = 3
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 0.5
color = (255, 0, 0)
thickness = 1
# bos beyaz sayfa
image = np.zeros((width,height,chanel), np.uint8)
image[:,:] = [255,255,255]
# kordinat duzlemi icin dikeyde ve yatayda cizgi
image = cv2.line(image, (40,40), (40,360), color, 2)
image = cv2.line(image, (40,360), (500-30,360), color, 2)
# column name
temp = (400-len(name))//2
image = cv2.putText(image, name, (temp, 20), font,
fontScale, color, thickness, cv2.LINE_AA)
# values ekseni
image = cv2.putText(image, "Values", (480, 365), font,
fontScale, color, thickness, cv2.LINE_AA)
# frekans ekseni
image = cv2.putText(image, "FREQ", (15, 20), font,
fontScale, color, thickness, cv2.LINE_AA)
# ayni frekans degerlerini sürekli cizdirmemek icin sadece bir kere aldim
listDeger = []
for i in range(0, len(dtt)):
flag = None
for j in listDeger:
if dtt[i][1]==j:
flag=True
break
if flag!=True:
listDeger.append(dtt[i][1])
listDeger = sorted(listDeger)
# frekanslarin esit bir sekilde yazdirilmasi icin
adim = 350/(len(listDeger))
count = adim
temp = []
for i in listDeger:
# frekans yazdirma
image = cv2.putText(image, str(i), (0, int(400-count)), font,
fontScale, color, thickness, cv2.LINE_AA)
# frekans yazilimi belli olsun diye ufak daire
image = cv2.circle(image, (40,int(400-(count+4))), 3, color, 2)
# bu degeri tutuyorum cunki, daha sonra buraya kadar cizgi cekecegiz
temp.append([i, int(400-(count+3))])
count+=adim
# degerler icin olceklenebilir adim
adim = 375/(len(dtt))
count = adim
for i in range(0, len(dtt)):
# deger adi yazdirilir
image = cv2.putText(image, str(dtt[i][0]), (int(450-count), 385), font,
fontScale, color, thickness, cv2.LINE_AA)
# bu deger konumundan frekansa kadar kalin kirmizi cizgi cekilir
for z in temp:
if z[0]==dtt[i][1]:
image = cv2.line(image, (int(450-(count-7)), 360), (int(450-(count-7)),z[1]), (0,0,139), 15)
count+=adim
return image
def variance(data):
n = len(data)
mean = sum(data) / n
return sum((x - mean) ** 2 for x in data) / (n - 1)
def stdev(data):
var = variance(data)
std_dev = math.sqrt(var)
return std_dev
def Check(img):
for i in range(0,999999999999999):
my_file = Path(img)
if my_file.is_file():
time.sleep(5)
os.system("rm {}".format(img))
break