-
Notifications
You must be signed in to change notification settings - Fork 0
/
curvature_GUI_stats.py
1068 lines (798 loc) · 39.2 KB
/
curvature_GUI_stats.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
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
import os.path
import PySimpleGUI as sg
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter # useful for `logit` scale
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.ticker import MultipleLocator
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
from shape import Shape, MplColorHelper
import math
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import re
from scipy import interpolate, stats
from scipy.signal import savgol_filter
import warnings
from datetime import date
from scipy.interpolate import UnivariateSpline, CubicSpline
figure_canvas_agg = None
img = Image.new("RGBA", (500, 500), (255, 255, 255,0))
fig = None
data = None
fnames = []
csv_filename = ""
#colors = [(20,42,64), (36,77,111), (56,116,167), (84, 173, 240)]
colors = [(20,42,64), (84, 173, 240)]
class MplColorHelper:
def __init__(self, cmap_name, start_val, stop_val):
self.cmap_name = cmap_name
self.cmap = plt.get_cmap(cmap_name)
self.norm = mpl.colors.Normalize(vmin=start_val, vmax=stop_val)
self.scalarMap = cm.ScalarMappable(norm=self.norm, cmap=self.cmap)
def get_rgb(self, val):
return self.scalarMap.to_rgba(val)
def color(self, val):
return self.cmap(self.norm(val))
def filelist(folder, ext):
try:
# Get list of files in folder
file_list = os.listdir(folder)
except:
file_list = []
fnames = [
f
for f in file_list
if os.path.isfile(os.path.join(folder, f)) and f.lower().endswith((ext))
]
return fnames
def fit_scale(p, mask, an, inv_flag):
if inv_flag:
inc = -0.01
scale = -0.01
else:
inc = 0.01
scale = 0.01
uu, vv = vector_transform(an, scale, p=p)
while mask[int(vv),int(uu)]:
scale = scale + inc
uu, vv = vector_transform(an, scale, p=p)
return scale
def dir_number(dir):
r = 0
for root, dirs, files in os.walk(dir):
if len(files)>0:
r += 1
return r
def calc_normals(data0, data1, size, inv_flag = False):
img = Image.new("1", size, color = 0)
draw = ImageDraw.Draw(img)
if inv_flag:
pol1 = [(e[0], e[1]) for e in data1[['x','y']].to_numpy().reshape(len(data1), 2).tolist()]
pol0 = [(e[0], e[1]) for e in data0[['x','y']].to_numpy().reshape(len(data0), 2).tolist()]
draw.polygon(pol1, fill = 1, outline =1)
draw.polygon(pol0, fill = 0, outline =0)
#img.show()
mask_2d = np.asarray(img)
data = data1.copy()
data['an'] = 0
data['l'] = 0
data['u'] = 0
data['v'] = 0
data.loc[0, 'an'] = normal(data1[['x','y']].iloc[-1].values, data1[['x','y']].iloc[0].values, data1[['x','y']].iloc[1].values)
data.loc[0, 'l'] = fit_scale(data1[['x','y']].iloc[0].values, mask_2d, data[['an']].iloc[0].values, inv_flag)
pn1_x, pn1_y = vector_transform(data[['an']].iloc[0].values, data[['l']].iloc[0].values, data[['x','y']].iloc[0].values)
data.loc[0, 'u'] = pn1_x
data.loc[0, 'v'] = pn1_y
data.loc[len(data)-1, 'an'] = normal(data1[['x','y']].iloc[-2].values, data1[['x','y']].iloc[-1].values, data1[['x','y']].iloc[0].values)
data.loc[len(data)-1, 'l'] = fit_scale(data1[['x','y']].iloc[-1].values, mask_2d, data[['an']].iloc[-1].values,inv_flag)
pn1_x, pn1_y = vector_transform(data[['an']].iloc[-1].values, data[['l']].iloc[-1].values, data[['x','y']].iloc[-1].values)
data.loc[len(data)-1, 'u'] = pn1_x
data.loc[len(data)-1, 'v'] = pn1_y
for i in range(1, len(data)-1):
data.loc[i, 'an'] = normal(data1[['x','y']].iloc[i-1].values, data1[['x','y']].iloc[i].values, data1[['x','y']].iloc[i+1].values)
data.loc[i, 'l'] = fit_scale(data1[['x','y']].iloc[i].values, mask_2d, data[['an']].iloc[i].values,inv_flag)
pn1_x, pn1_y = vector_transform(data[['an']].iloc[i].values, data[['l']].iloc[i].values, data[['x','y']].iloc[i].values)
data.loc[i, 'u'] = pn1_x
data.loc[i, 'v'] = pn1_y
else:
pol1 = [(e[0], e[1]) for e in data1[['x','y']].to_numpy().reshape(len(data1), 2).tolist()]
draw.polygon(pol1, fill = 1)
mask_2d = np.asarray(img)
data = data0.copy()
data['an'] = 0
data['l'] = 0
data['u'] = 0
data['v'] = 0
data.loc[0, 'an'] = normal(data0[['x','y']].iloc[-1].values, data0[['x','y']].iloc[0].values, data0[['x','y']].iloc[1].values)
data.loc[0, 'l'] = fit_scale(data0[['x','y']].iloc[0].values, mask_2d, data[['an']].iloc[0].values, inv_flag)
pn1_x, pn1_y = vector_transform(data[['an']].iloc[0].values, data[['l']].iloc[0].values, data[['x','y']].iloc[0].values)
data.loc[0, 'u'] = pn1_x
data.loc[0, 'v'] = pn1_y
data.loc[len(data)-1, 'an'] = normal(data0[['x','y']].iloc[-2].values, data0[['x','y']].iloc[-1].values, data0[['x','y']].iloc[0].values)
data.loc[len(data)-1, 'l'] = fit_scale(data0[['x','y']].iloc[-1].values, mask_2d, data[['an']].iloc[-1].values,inv_flag)
pn1_x, pn1_y = vector_transform(data[['an']].iloc[-1].values, data[['l']].iloc[-1].values, data[['x','y']].iloc[-1].values)
data.loc[len(data)-1, 'u'] = pn1_x
data.loc[len(data)-1, 'v'] = pn1_y
for i in range(1, len(data)-1):
data.loc[i, 'an'] = normal(data0[['x','y']].iloc[i-1].values, data0[['x','y']].iloc[i].values, data0[['x','y']].iloc[i+1].values)
data.loc[i, 'l'] = fit_scale(data0[['x','y']].iloc[i].values, mask_2d, data[['an']].iloc[i].values,inv_flag)
pn1_x, pn1_y = vector_transform(data[['an']].iloc[i].values, data[['l']].iloc[i].values, data[['x','y']].iloc[i].values)
data.loc[i, 'u'] = pn1_x
data.loc[i, 'v'] = pn1_y
return data
def sim_normals(curves, data0, size, l_sim = 10, res = 100, folder = None):
#img = Image.new("1", size, color = 0)
#draw = ImageDraw.Draw(img)
#pol1 = [(e[0], e[1]) for e in data1[['x','y']].to_numpy().reshape(len(data1), 2).tolist()]
#draw.polygon(pol1, fill = 1)
#mask_2d = np.asarray(img)
data = data0.copy()
data['an'] = 0
data['l'] = l_sim
data['u'] = 0
data['v'] = 0
data.loc[0, 'an'] = normal(data0[['x','y']].iloc[-1].values, data0[['x','y']].iloc[0].values, data0[['x','y']].iloc[1].values)
pn1_x, pn1_y = vector_transform(data[['an']].iloc[0].values, data[['l']].iloc[0].values, data[['x','y']].iloc[0].values)
data.loc[0, 'u'] = pn1_x
data.loc[0, 'v'] = pn1_y
data.loc[len(data)-1, 'an'] = normal(data0[['x','y']].iloc[-2].values, data0[['x','y']].iloc[-1].values, data0[['x','y']].iloc[0].values)
pn1_x, pn1_y = vector_transform(data[['an']].iloc[-1].values, data[['l']].iloc[-1].values, data[['x','y']].iloc[-1].values)
data.loc[len(data)-1, 'u'] = pn1_x
data.loc[len(data)-1, 'v'] = pn1_y
for i in range(1, len(data)-1):
data.loc[i, 'an'] = normal(data0[['x','y']].iloc[i-1].values, data0[['x','y']].iloc[i].values, data0[['x','y']].iloc[i+1].values)
pn1_x, pn1_y = vector_transform(data[['an']].iloc[i].values, data[['l']].iloc[i].values, data[['x','y']].iloc[i].values)
data.loc[i, 'u'] = pn1_x
data.loc[i, 'v'] = pn1_y
data['x'] = data['u']
data['y'] = data['v']
data_sm = smooth(data, res = res)
#print(data_sm)
data_sim = curves[0].curvature(data_sm)
data0 = curves[0].data.copy()
data1 = curves[-1].data.copy()
COL = curves[0].COL
out = Image.new("RGBA", size, (255, 255, 255, 0))
img_0 = draw_contour(out, data0, COL, c_day = colors[0])
img_1 = draw_contour(img_0, data_sim, COL)
img_out = draw_contour(img_1, data1, COL, c_day = colors[1])
if folder is None:
img_filename = sg.popup_get_file('Please enter a file name', save_as = True)
else:
img_name = f'l_sim-{l_sim}_res-{res}.png'
img_filename = os.path.join(folder, img_name)
try:
img_out.save(img_filename, "PNG")
except:
sg.popup("Error")
img_out.show()
def draw_contour(out, data, COL, w = 10, scale = 1, c_day = None):
r = 20
flag_30 = False
draw = ImageDraw.Draw(out)
for i in range(data.shape[0]-1):
x1, y1, c1, u1 = data.iloc[i]
x2, y2, c2, u2 = data.iloc[i+1]
if c_day is None:
color = tuple([int(z * 255) for z in COL.get_rgb((c1+c2)/2)])
else:
color = c_day
if i == 0:
draw.ellipse([(int(x1/scale)-r, int(y1/scale)-r), (int(x1/scale)+r, int(y1/scale)+r)], fill=(0,0,0), width = 1)
if (u1 > 0.3) & (flag_30 == False):
draw.rectangle([(int(x1/scale)-r, int(y1/scale)-r), (int(x1/scale)+r, int(y1/scale)+r)], fill=(0,0,0), width = 1)
flag_30 = True
draw.line([(int(x1/scale), int(y1/scale)), (int(x2/scale), int(y2/scale))], fill=color, width = w, joint = 'curve')
x1, y1, c1, u1 = data.iloc[-1]
x2, y2, c2, u2 = data.iloc[0]
draw.line([(int(x1/scale), int(y1/scale)), (int(x2/scale), int(y2/scale))], fill=color, width = w, joint = 'curve')
return out
def vector_transform(an, l, p = [0,0]):
###calculation of angle from slope
x = p[0] + math.sin(an)*int(l)
y = p[1] + math.cos(an)*int(l)
return x,y
def normal(p0, p1, p2):
length = 1
##calculation of tangent vector
def tan_v(p0, p1, length):
x0, y0, xa, ya = p0[0], p0[1], p1[0], p1[1]
dx, dy = xa-x0, ya-y0
norm = math.hypot(dx, dy) * 1/length
dx /= norm
dy /= norm
return dx, dy
dx1, dy1 = tan_v(p0, p1, length)
dx2, dy2 = tan_v(p1, p2, length)
dx = (dx1+dx2)/2
dy = (dy1+dy2)/2
#90degree rotation of tangent vector
#u, v = p1[0]-dy, p1[1]+dx
an = 0
if ((-dy)> 0) & (dx >0):
an = math.acos(dx/length)
if ((-dy)< 0) & (dx >0):
an = 2*math.pi - math.acos(dx/length)
if ((-dy)< 0) & (dx <0):
an = 2*math.pi - math.acos(dx/length)
if ((-dy)> 0) & (dx <0):
an = math.acos(dx/length)
return an
def cacl_lenght(data):
data['lenght'] = 0
data.loc[0, 'lenght'] = 0
for i in range(len(data.index)-1):
data.loc[i+1, 'lenght'] = data.iloc[i]['lenght'] + math.dist((data.iloc[i]['x'], data.iloc[i]['y']), (data.iloc[i+1]['x'], data.iloc[i+1]['y']))
return data
def auto_save(filename, img, figure, data):
name = filename
name = re.search(r'.+(?= )', name).group(0)
if not name:
sg.popup("Cancel", "Error")
try:
img.save(name + "_img.png", "PNG")
figure.savefig(name +'_fig.png', dpi = 300)
df_sum = data.copy()
df_sum = df_sum.drop('dataset', axis=1)
with pd.ExcelWriter(name + '_data.xlsx') as writer:
df_sum.to_excel(writer, sheet_name='Summary', index=True)
for i in range(len(data.index)):
df = data.iloc[i]['dataset'].copy()
time = data.iloc[i]['time']
df.to_excel(writer, sheet_name=f'{time} day', index=False)
sg.popup("Cancel", "Report files were saved")
except:
pass
def add_curve(curves, csv_filename, day, width, height, angle, res, scale):
cur = Shape(csv_filename, day, (width, height), angle, res, scale)
curves.append(cur)
return curves
def update_img(curves, res, width, height, w_line, colormap, c_min, c_max, auto_flag, res_flag = False, day_flag=False, scale = 1, angle = 0):
img = Image.new("RGBA", (width, height), (255, 255, 255,0))
if len(curves)>0:
for i, curve in enumerate(curves):
if res_flag:
curve.set_scale(scale)
curve.set_angle(angle)
curve.set_res(res)
if day_flag:
im = curve.make_img(w_line, colormap, c_min, c_max, auto_flag, colors[i])
else:
im = curve.make_img(w_line, colormap, c_min, c_max, auto_flag)
img = Image.alpha_composite(im, img)
return img
def smooth(data, k=3, s= 0.2, res = 100):
points = data[['x', 'y']].to_numpy()
points = np.append(points,points[:1], axis=0)
#points = np.roll(points, int(self.res/2), axis = 0)
# Linear length along the line:
distance = np.cumsum( np.sqrt(np.sum( np.diff(points, axis=0)**2, axis=1 )) )
distance = np.insert(distance, 0, 0)/distance[-1]
# Build a list of the spline function, one for each dimension:
splines = [UnivariateSpline(distance, coords, k = k) for coords in points.T]
#splines = [CubicSpline(distance, coords) for coords in points.T]
# Computed the spline for the asked distances:
alpha = np.linspace(0, 1, res)
points_fitted = np.stack([spl(alpha) for spl in splines], axis=1)
#np.stack([np.ones(3) for _ in range(3)])
new_data = pd.DataFrame(points_fitted, columns = ['x', 'y'])
return new_data
def create_dataset(curves):
times = []
areas = []
widths = []
heights = []
datasets = []
curv_mean = []
lenght_total = []
res = []
curv_mean_sm = []
circ = []
sol = []
for curve in curves:
#ax[1,0].step(x = [i for i in range(hist_x.size)], y = hist_x, label = f'{curve.time} day')
#ax[1,1].step(x = [j for j in range(hist_y.size)], y = hist_y, label = f'{curve.time} day')
times.append(curve.time)
areas.append(curve.area)
widths.append(curve.width)
heights.append(curve.height)
curv_mean.append(curve.data['c'].mean())
res.append(curve.res)
data = curve.data.reset_index()
cacl_lenght(data)
lenght_total.append(data.iloc[-1]['lenght']*100)
data['lenght'] = data['lenght']/data.iloc[-1]['lenght']*100
circ.append(curve.circ)
sol.append(curve.solidity)
#x_new = np.linspace(0, 100, curve.res)
#bspline = interpolate.make_interp_spline(data['lenght'].values, data['c'].values)
#y_new = bspline(x_new)
c_smooth = savgol_filter(data['c'].values, 5, 2)
curv_mean_sm.append(c_smooth.mean())
data['c_smooth'] = c_smooth
datasets.append(data)
kin = pd.DataFrame(columns=['time', 'area', 'width', 'height', 'width_raw', 'height_raw', 'curv_mean', 'curv_mean_sm', 'len_total', 'circ', 'sol'])
kin['time'] = times
kin['res'] = res
kin['area'] = areas
kin['width_raw'] = widths
kin['height_raw'] = heights
kin['curv_mean'] = curv_mean
kin['curv_mean_sm'] = curv_mean_sm
kin['len_total'] = lenght_total
kin['circ'] = circ
kin['sol'] = sol
kin['dataset'] = datasets
kin['dataset_inv'] = datasets
return kin
def plot_results(kin, img, canvas, curve):
global figure_canvas_agg
if figure_canvas_agg:
figure_canvas_agg.get_tk_widget().forget()
plt.close('all')
parameters = {'xtick.labelsize': 12,
'ytick.labelsize': 12,
'font.family':'sans-serif',
'font.sans-serif':['Arial'],
'font.size': 12}
plt.rcParams.update(parameters)
figure, ax = plt.subplots(3, 2, figsize = (8,12))
kin = kin.sort_values(by = 'time', ignore_index=True)
kin['width'] = kin['width_raw']-kin['width_raw'].values[0]
kin['height'] = kin['height_raw']-kin['height_raw'].values[0]
for i in range(len(kin.index)):
data = kin.iloc[i]['dataset']
time = kin.iloc[i]['time']
ax[0,1].scatter(data['lenght'], data['c'], alpha = 0.4, s = 10)
ax[0,1].plot(data['lenght'], data['c_smooth'], label = f'{time} day')
w, h = img.size
ax[0,0].set_title('Curvature')
#ax[0,0].imshow(img)
ax[0,0].set_xlabel('x')
ax[0,0].set_ylabel('y')
ax[0,0].plot([0,w], [h/2, h/2], '--', color = 'tab:grey', alpha = 0.5, linewidth=0.5 )
ax[0,0].plot([w/2,w/2], [0, h], '--', color = 'tab:grey', alpha = 0.5, linewidth=0.5 )
ax[0,1].set_title('Curvature distribution', fontsize=14)
#ax[0,1].scatter(x = kin['time'], y = kin['width'], label = 'x-size')
#ax[0,1].plot(kin['time'], kin['width'])
#ax[0,1].scatter(x = kin['time'], y = kin['height'], label = 'y-size')
#ax[0,1].plot(kin['time'], kin['height'])
ax[0,1].legend()
ax[0,1].set_xlabel('Perimeter, %')
ax[0,1].set_ylabel('Curvature')
if len(kin.index)>1:
time0 = kin.iloc[0]['time']
time1 = kin.iloc[-1]['time']
COL = curve.COL
data0 = kin.iloc[0]['dataset'].copy()
data1 = kin.iloc[-1]['dataset'].copy()
data = calc_normals(data0, data1, (w,h), False)
for i in range(len(data.index)):
x = data.iloc[i]['x']
y = data.iloc[i]['y']
an = data.iloc[i]['an']
c = data.iloc[i]['c']
u = data.iloc[i]['u']
v = data.iloc[i]['v']
l = data.iloc[i]['l']
uu, vv = vector_transform(an, l)
if c >= 0:
ax[1,0].quiver(x, y, uu, vv, angles = 'uv', color=COL.color(c), units='xy', scale = 1, alpha = 1, width = 2)
#plt.plot((x,u), (y,v), color=cmap(norm(c)))
if i ==0:
ax[1,0].scatter(x, y, s = 40, c = 'black', alpha = 0.5)
elif i ==10:
ax[1,0].scatter(x, y, s = 20, c = 'black', alpha = 0.5)
else:
ax[1,0].scatter(x, y, s = 1)
#pol = data[['x','y']].to_numpy().reshape(len(data), 2)
#polygon = Polygon(pol, closed=True)
pol1 = data1[['x','y']].to_numpy().reshape(len(data1), 2)
polygon1 = Polygon(pol1, closed=True)
collection = PatchCollection([polygon1], alpha=0.00, zorder= 1, color = ["tab:red"])
ax[1,0].add_collection(collection)
ax[1,0].set_xlim(0,w)
ax[1,0].set_ylim(0,h)
ax[1,0].set_title('Growth rate', fontsize=14)
ax[1,0].set_ylabel('y')
ax[1,0].set_xlabel('x')
#ax[1,0].figure.savefig('test.png')
kin['pearson_neg'] = 0
kin['pearson_zero'] = 0
kin['pearson_pos'] = 0
kin['l_mean'] = 0
kin['l_median'] = 0
kin['l_max'] = 0
kin['l_mean_inv'] = 0
kin['l_median_inv'] = 0
kin['l_max_inv'] = 0
l = []
l_m = []
t = []
datasets = [kin.iloc[0]['dataset'].copy()]
for i in range(len(kin.index)-1):
#loading the datasets and time-label
data0 = kin.iloc[i]['dataset'].copy()
data1 = kin.iloc[i+1]['dataset'].copy()
t0 = int(kin.iloc[i]['time'])
t1 = int(kin.iloc[i+1]['time'])
t.append(t1)
###lenght distribution alonge the perimeter
data = calc_normals(data0, data1, (w,h))
data = curve.filter_outliers(data, 'l')
datasets.append(data)
#smoothing the growth data
l_smooth = savgol_filter(data['l'].values, 5, 2)
data['l_sm'] = l_smooth
#calculation of growth stats
kin.loc[i+1, 'l_mean'] = data['l'].mean()
kin.loc[i+1, 'l_median'] = data['l'].median()
kin.loc[i+1, 'l_max'] = data['l'].max()
#plotting the data + smooth line
ax[1,1].scatter(data['lenght'], data['l'], alpha = 0.4, s = 10)
ax[1,1].plot(data['lenght'], l_smooth, label = f'{t0}-{t1}')
l.append(data['l'].values)
l_m.append(data['l'].values.mean())
#filtering point with the negative curvature
data_sub = data[data['c_smooth'] <= -0.005]
#caclulation of pearson correlation coefficientbetween growth and curvature (c<0)
r_neg = np.corrcoef(data_sub['c_smooth'], data_sub['l_sm'])[1,0]
kin.loc[i+1, 'pearson_neg'] = r_neg
data_sub = data[(data['c_smooth'] <= 0.005) & (data['c_smooth'] > -0.005)]
#caclulation of pearson correlation coefficientbetween growth and curvature (c<0)
r_zero = np.corrcoef(data_sub['c_smooth'], data_sub['l_sm'])[1,0]
kin.loc[i+1, 'pearson_zero'] = r_zero
data_sub = data[(data['c_smooth'] <= -0.005)]
#plotting the growth vs curvature
ax[2,1].scatter(data_sub['c_smooth'], data_sub['l_sm'], s = 10, label = f'{t0}-{t1}: Pneg = {round(r_neg, 2)}')
kin['dataset'] = datasets
datasets_inv = [kin.iloc[0]['dataset_inv'].copy()]
for i in range(len(kin.index)-1, 0, -1):
#loading the datasets and time-label
data0 = kin.iloc[i]['dataset_inv'].copy()
data1 = kin.iloc[i-1]['dataset_inv'].copy()
t0 = int(kin.iloc[i]['time'])
t1 = int(kin.iloc[i-1]['time'])
#print(t0)
#print(t1)
#t.append(t0)
#print(data0)
#print(data1)
###lenght distribution alonge the perimeter
data_inv = calc_normals(data1, data0, (w,h), True)
#data_inv = curve.filter_outliers(data_inv, 'l')
datasets_inv.append(data_inv)
#print(data_inv['l'])
#calculation of growth stats
kin.loc[i, 'l_mean_inv'] = data_inv['l'].mean()
kin.loc[i, 'l_median_inv'] = data_inv['l'].median()
kin.loc[i, 'l_max_inv'] = data_inv['l'].max()
#plotting the data + smooth line
#ax[1,1].scatter(data_inv['lenght'], data_inv['l'], alpha = 0.4, s = 10)
#filtering point with the negative curvature
data_sub = data_inv[data_inv['c_smooth'] >= 0.003]
#caclulation of pearson correlation coefficientbetween growth and curvature (c<0)
r_pos = np.corrcoef(data_sub['c_smooth'], data_sub['l'])[1,0]
kin.loc[i, 'pearson_pos'] = r_pos
kin['dataset_inv'] = datasets_inv
ax[1,1].set_title('Growth distribution', fontsize=14)
ax[1,1].legend()
ax[1,1].set_xlabel('Perimeter, %')
ax[1,1].set_ylabel('Growth, px')
ax[2,1].legend()
ax[2,1].set_title('Curvature (c<0) vs growth', fontsize=14)
ax[2,1].set_xlabel('Curvature')
ax[2,1].set_ylabel('Growth')
v = ax[2,0].violinplot(l)
for pc in v['bodies']:
pc.set_facecolor('red')
pc.set_edgecolor('black')
for partname in ('cbars','cmins','cmaxes'):
vp = v[partname]
vp.set_edgecolor('black')
vp.set_linewidth(1)
#ax[2,0].scatter(x = t, y = l_m, s= 20, color = 'white')
ax[2,0].set_title('Growth kinetics', fontsize=14)
xticks = kin['time'].values.tolist()
ax[2,0].xaxis.set_major_locator(MultipleLocator(1))
ax[2,0].set_xticklabels(xticks)
#ax[2,0].set_xlabel('Time, days')
ax[2,0].set_ylabel('Growth, px')
figure.tight_layout()
#figure.show()
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.draw()
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
return figure, kin
def plot_feature(data0, feature, Shapes, label, filename):
fig, ax = plt.subplots(1, len(Shapes), figsize = (12,4))
parameters = {'xtick.labelsize': 12,
'ytick.labelsize': 12,
'font.family':'sans-serif',
'font.sans-serif':['Arial'],
'font.size': 12}
plt.rcParams.update(parameters)
for i, shape in enumerate(Shapes):
data = data0[data0['Shape'] == shape]
data.boxplot(column=feature, by=['time'], ax=ax[i], grid = False, layout= (3,5))
ax[i].set_ylabel(feature)
ax[i].set_title(shape)
for i in range(4):
if label == 'MCF-7':
if feature == "curv_mean":
ax[i].set_ylim(-0.012, 0.0015)
if feature == "pearson":
ax[i].set_ylim(-0.5, 0.5)
if feature == "circ":
ax[i].set_ylim(0.4, 1)
if feature == "sol":
ax[i].set_ylim(0.7, 1.05)
if feature == "l_mean":
ax[i].set_ylim(0, 85)
if feature == "l_median":
ax[i].set_ylim(0, 85)
if feature == "len_total":
ax[i].set_ylim(50000, 330000)
if label == 'Patient':
if feature == "curv_mean":
ax[i].set_ylim(-0.007, 0.0015)
if feature == "pearson":
ax[i].set_ylim(-1, 1)
if feature == "circ":
ax[i].set_ylim(0, 1.05)
if feature == "sol":
ax[i].set_ylim(0.5, 1.05)
if feature == "l_mean":
ax[i].set_ylim(0, 85)
if feature == "l_median":
ax[i].set_ylim(0, 85)
if feature == "len_total":
ax[i].set_ylim(00, 330000)
fig.suptitle(f'{feature}_{label}', fontsize=16)
fig.tight_layout()
root, name = os.path.split(filename)
name = name.replace(".xlsx", f"_{feature}_{label}.png")
today = str(date.today())
filename = f'{root}/{today}/{label}/{name}'
if not os.path.exists(f'{root}/{today}/{label}'):
os.makedirs(f'{root}/{today}/{label}')
fig.savefig(filename)
def main():
global fnames
global csv_filename
warnings.filterwarnings("ignore")
curves = []
layout_files = [
[
sg.Text("Image Folder"),
sg.In(size=(50, 1), enable_events=True, key="-FOLDER-"),
sg.FolderBrowse(),
],
[
sg.Listbox(values=fnames, enable_events=True, size=(80, 10), key="-FILE LIST-")
],
[
sg.Text(""),
],
[
sg.Text("Width"),
sg.In('3216', size=(5, 1), key="-WIDTH-", enable_events=True),
sg.Text("Height"),
sg.In('3216',size=(5, 1), key="-HEIGHT-", enable_events=True),
sg.Text("Day"),
sg.In('0',size=(3, 1), key="-DAY-", enable_events=True),
sg.Text("Line Width"),
sg.In('10',size=(3, 1), key="-WIDTH_LINE-", enable_events=True),
sg.Text("Rotation angle"),
sg.In('0',size=(3, 1), key="-ANGLE-", enable_events=True),
sg.Text("Scale"),
sg.In('0.55',size=(5, 1), key="-SCALE-", enable_events=True),
],
[
sg.Text("Resolution"),
sg.In('70', size=(5, 1), key="-RES-", enable_events=True),
sg.Checkbox('Days',key="-DAYS-", default=False),
sg.Text("Colormap"),
sg.In('rainbow', size=(10, 1), key="-COLORMAP-", enable_events=True),
sg.Text("C-min"),
sg.In('-0.005', size=(5, 1), key="-C_MIN-", enable_events=True),
sg.Text("C-max"),
sg.In('0.005', size=(5, 1), key="-C_MAX-", enable_events=True),
sg.Checkbox('Autorange',key="-AUTO-", default=False),
sg.Text("Sim growth rate"),
sg.In('-1', size=(5, 1), key="-GR_SIM-", enable_events=True),
]
]
layout_right= [
[
sg.Canvas(key='-CANVAS-',size=(50, 50))
],
[
sg.Button('Save plot'),
sg.Button('Save image'),
sg.Button('Save data'),
sg.Text(" "),
sg.Button('Auto save'),
sg.Button('Remove days'),
]
]
layout_buttons = [
[
sg.Button('Add curve'),
sg.Button('Automated analysis'),
sg.Text(""),
sg.Button('Simulation of growth'),
],
[
sg.Button('Update settings'),
sg.Button('Clear all'),
sg.Button('Clear last'),
sg.Button('Quit')
]]
layout_left = layout_files + layout_buttons
layout = [[sg.Column(layout_left), sg.pin(sg.Column(layout_right, key='-PLOT-', visible = False, pad = (0,0)))]]
window = sg.Window('Curvature line', layout)
while True:
event, values = window.read()
if event == 'Quit' or event == sg.WIN_CLOSED :
window.close()
break
elif event == "Clear all":
curves.clear()
window['-PLOT-'].update(visible = False)
#clean canvas
elif event == "Clear last":
day, width, height, angle = int(values["-DAY-"]), int(values["-WIDTH-"]), int(values["-HEIGHT-"]), int(values["-ANGLE-"])
res, w_line, colormap, c_min, c_max, auto_flag, days_flag, scale = int(values["-RES-"]), int(values["-WIDTH_LINE-"]), values["-COLORMAP-"], float(values["-C_MIN-"]), float(values["-C_MAX-"]), values["-AUTO-"], values["-DAYS-"], float(values["-SCALE-"])
if len(curves)>1:
curves.pop()
if len(curves)>0:
img = update_img(curves, res, width, height, w_line, colormap, c_min, c_max, auto_flag, True, days_flag, scale, angle = angle)
kin = create_dataset(curves)
figure, data = plot_results(kin, img, window['-CANVAS-'].TKCanvas, curves[0])
window['-PLOT-'].update(visible = True)
else:
window['-PLOT-'].update(visible = False)
#clean canvas
elif event == "Update settings":
width, height, angle = int(values["-WIDTH-"]), int(values["-HEIGHT-"]), float(values["-ANGLE-"])
res, w_line, colormap, c_min, c_max, auto_flag, days_flag, scale = int(values["-RES-"]), int(values["-WIDTH_LINE-"]), values["-COLORMAP-"], float(values["-C_MIN-"]), float(values["-C_MAX-"]), values["-AUTO-"], values["-DAYS-"], float(values["-SCALE-"])
img = update_img(curves, res, width, height, w_line, colormap, c_min, c_max, auto_flag, True, days_flag, scale, angle = angle)
kin = create_dataset(curves)
figure, data = plot_results(kin, img, window['-CANVAS-'].TKCanvas, curves[0])
window['-PLOT-'].update(visible = True)
elif event == "Add curve":
day, width, height, angle = int(values["-DAY-"]), int(values["-WIDTH-"]), int(values["-HEIGHT-"]), float(values["-ANGLE-"])
res, w_line, colormap, c_min, c_max, auto_flag, days_flag, scale = int(values["-RES-"]), int(values["-WIDTH_LINE-"]), values["-COLORMAP-"], float(values["-C_MIN-"]), float(values["-C_MAX-"]), values["-AUTO-"], values["-DAYS-"], float(values["-SCALE-"])
curves = add_curve(curves, csv_filename, day, width, height, angle, res, scale)
img = update_img(curves, res, width, height, w_line, colormap, c_min, c_max, auto_flag, True, days_flag, scale, angle = angle)
kin = create_dataset(curves)
figure, data = plot_results(kin, img, window['-CANVAS-'].TKCanvas, curves[0])
window['-PLOT-'].update(visible = True)
elif event == "Automated analysis":
root_dir = sg.popup_get_folder('Please enter a folder name')
width, height, angle = int(values["-WIDTH-"]), int(values["-HEIGHT-"]), int(values["-ANGLE-"])
res, w_line, colormap, c_min, c_max, auto_flag, days_flag, scale = int(values["-RES-"]), int(values["-WIDTH_LINE-"]), values["-COLORMAP-"], float(values["-C_MIN-"]), float(values["-C_MAX-"]), values["-AUTO-"], values["-DAYS-"], float(values["-SCALE-"])
curves.clear()
window['-PLOT-'].update(visible = False)
i=0
n = dir_number(root_dir)
for root, dirs, files in os.walk(root_dir):
if len(files)>0:
fnames = filelist(root, ".csv")
curves.clear()
if len(fnames)>0:
try:
for name in fnames:
csv_filename = os.path.join(root, name)
day = int(re.search(r'(?<= )([0-9]*)(?=.csv)', name).group(1))
curves = add_curve(curves, csv_filename, day, width, height, angle, res, scale)
img = update_img(curves, res, width, height, w_line, colormap, c_min, c_max, auto_flag, True, days_flag, scale, angle = angle)
kin = create_dataset(curves)
figure, data = plot_results(kin, img, window['-CANVAS-'].TKCanvas, curves[0])
auto_save(csv_filename, img, figure, data)
i += 1
progress = round(i/n*100)
print(f'{root} - Done!')
print(f'Progress: {progress}%')
except:
i += 1
progress = round(i/n*100)
print(f'{root} - Error')
print(f'Progress: {progress}%')
elif event == "Save image":
name = sg.popup_get_file('Please enter a file name', save_as = True)
try:
img.save(name + ".png", "PNG")
except:
sg.popup("Error")
elif event == "Save plot":
name = sg.popup_get_file('Save Plot', save_as=True)
if not name:
sg.popup("Cancel", "No filename supplied")
else:
try:
figure.savefig(name +'.png', dpi = 300)
except:
sg.popup("Cancel", "Error")
elif event == "Save data":
name = sg.popup_get_file('Save report', save_as=True)
if not name:
sg.popup("Cancel", "No filename supplied")
try:
data.to_csv(name + '.csv',index=True)
for i in range(len(data.index)):
time = data.iloc[i]['time']
rep = data.iloc[i]['dataset']
rep.to_csv(name + '_'+str(time) +'_data.csv',index=True)
rep_inv = data.iloc[i]['dataset_inv']
rep_inv.to_csv(name + '_'+str(time) +'_data_inv.csv',index=True)
except:
pass
elif event == "Auto save":
auto_save(csv_filename, img, figure, data)
elif event == "Simulation of growth":
if len(kin.index)>1:
width, height = int(values["-WIDTH-"]), int(values["-HEIGHT-"])
data0 = data.iloc[0]['dataset'].copy()
res = data.iloc[0]['res']
if int(values["-GR_SIM-"]) < 0 :
l_sim = data.iloc[-1]['l_median']
#kin.to_csv('_data.csv',index=True)
else:
l_sim = values["-GR_SIM-"]