-
Notifications
You must be signed in to change notification settings - Fork 1
/
omega.py
executable file
·7563 lines (6151 loc) · 242 KB
/
omega.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
## webscraper indeed
import urllib.request
from bs4 import BeautifulSoup
import requests
from lxml import html
import requests
p_code="/home/paintedpalms/rdrive/taff/code"
import time
def get_posting_info(url_posting,option,k):
sub_page = requests.get(url_posting)
sub_soup = BeautifulSoup(sub_page.content, 'html.parser')
company_name=""
location=""
contract_type=""
contract_infos=[]
job_description=""
# get job title
job_title=""
nodes=sub_soup.find_all("h1")
for n in nodes:
s="jobsearch-JobInfoHeader-title"
cl=n.get_attribute_list("class")
if s in cl:
job_title=n.get_text()
# get company infos
company_infos=[]
nodes=sub_soup.find_all("div")
for n in nodes:
s="InlineCompanyRating"
cl=n.get_attribute_list("class")
if len(cl)>0:
if cl[0] is not None:
if s in cl[0]:
'''
txt=n.get_text()
company_name=txt.split("-")[0]
location=txt.split("-")[1]
'''
sub_nodes=n.findChildren()
for sn in sub_nodes:
txt=sn.get_text()
if txt not in company_infos and len(txt)>1:
company_infos.append(txt)
# get contract infos
contract_infos=[]
nodes=sub_soup.find_all("div")
for n in nodes:
s="JobMetadataHeader-item"
cl=n.get_attribute_list("class")
if len(cl)>0:
if cl[0] is not None:
if s in cl[0]:
sub_nodes=n.findChildren()
for sn in sub_nodes:
txt=sn.get_text()
if txt not in contract_infos:
contract_infos.append(txt)
# get job description
job_description=""
nodes=sub_soup.find_all("div")
for n in nodes:
s="jobDescriptionText"
cl=n.get_attribute_list("class")
#print(cl)
if len(cl)>0:
if cl[0] is not None:
if s in cl[0]:
job_description=n.get_text()
# get publication date
pub_date=""
nodes=sub_soup.find_all("div")
for n in nodes:
s="jobsearch-JobMetadataFooter"
cl=n.get_attribute_list("class")
if len(cl)>0:
if cl[0] is not None:
if s in cl[0]:
sub_nodes=n.findChildren()
for sn in sub_nodes:
txt=sn.get_text()
if "-" in txt and "continuer" not in txt:
pub_date=txt.split("-")[1]
if option=="print":
print("url_posting",url_posting)
print("")
print("job_title",job_title)
print("")
print("company_name",company_name)
print("")
print("location",location)
print("")
for ci in contract_infos:
print("ci",ci)
print("")
print("job_description",job_description)
print("")
if option=="save":
sep="\n\n"
s=""
s+="####################### url_posting"+sep+url_posting+sep
s+="####################### job_title"+sep+job_title+sep
#s+="#company_name"+sep+company_name+sep
#s+="#location"+sep+location+sep
s+="####################### company_infos"+sep
for ci in company_infos:s+=ci+sep
s+="####################### contract_infos"+sep
for ci in contract_infos:s+=ci+sep
s+="####################### job_description"+sep+job_description+sep
s+="####################### pub_date"+sep+pub_date+" (scrap date "+get_simple_time_str().split("_")[0]+")"+sep
company_name=""
if len(company_infos)>0:
company_name=company_infos[0]
company_name=company_name.replace("/","_")
company_name=company_name.replace(" ","_")
#p="/home/paintedpalms/rdrive/taff/code/forecast"+"/"+get_simple_time_str()+"_"+job_title+".txt"
#p="/home/paintedpalms/rdrive/taff/code/forecast"+"/"+get_simple_time_str()+"_"+get_random_id()+".txt"
p="/home/paintedpalms/rdrive/taff/code/forecast"+"/"+get_simple_time_str()+"_"+get_trigram_count(k)+"_"+company_name+".txt"
file=open(p,"w")
file.write(s)
file.close()
# get job posting urls
def get_posting_urls(soup):
sub_urls=[]
nodes=soup.find_all("a")
for n in nodes:
ok=0
if isinstance(n.get_attribute_list("id"), list):
for v in n.get_attribute_list("id"):
if type(v)!=type(None):
if "jl_" in v or "sja" in v:ok=1
if ok:
ext_url=n.get_attribute_list("href")[0]
new_url="https://fr.indeed.com"+ext_url
print(new_url)
sub_urls.append(new_url)
return sub_urls
if 1==0:
#url="https://fr.indeed.com/emplois?q=Deep+Learning&sort=date"
#url="https://fr.indeed.com/emplois?q=Deep+Learning&sort=date&start=570"
urls=[]
pages=[]
url_start="https://fr.indeed.com/emplois?q=Deep+Learning&sort=date"
##########
start=30 #1
end=57 #58
##########
if start==0:
urls.append(url_start)
pages.append("start page")
for k in range(max(start,1),end):
page=str(k*10)
url=url_start+"&start="+page
print(url)
urls.append(url)
pages.append(page)
#for url in urls:
#url=urls[2]
#if 1==1:
k=0
k_page=0
for url in urls:
m=60
if k_page%7==0 and k_page>0:
print("sleep for 1 hour",time.ctime())
time.sleep(60*m)
#time.sleep(20)
print("\n\n\n\n\n--------------- page",pages[k_page],"\n\n\n\n\n")
k_page+=1
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
urllib.request.install_opener(opener)
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
sub_urls=get_posting_urls(soup)
for url_posting in sub_urls:
#time.sleep(20)
print("next url posting ...")
get_posting_info(url_posting,"save",k)
k+=1
## webscraper
# public imports
import urllib.request
from bs4 import BeautifulSoup
import requests
class WebScrapper():
def __init__(self):
return None
def get_images_from_url(url):
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
urllib.request.install_opener(opener)
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
image_tags = soup.find_all('img')
elem_urls = []
for tag in image_tags:
image_name = tag['src']
image_url = url+'/'+image_name
elem_urls.append(image_url)
return elem_urls
## layout display
import random
import numpy as np
class clay():
def __init__(self):
return None
#------------------------------------------------------------------------------------------------------------------------------------
# visual eval : display
#------------------------------------------------------------------------------------------------------------------------------------
def display_bboxes_and_images_rico(bboxes_rico,start,end):
indexes=range(start,end)
for i_sample in range(len(indexes)):
print("-------------------",i_sample)
bboxes=bboxes_rico[i_sample]
img1=show_bboxes(bboxes,"",2560,1440)
img2=get_rico_image(names[indexes[i_sample]])
display(img2)
display(img1)
def show_synth_sample1(sample):
npa_bg=get_npa_sample(sample)
display(get_image_from_npa(npa_bg))
# poor # sub : rasterize_sample
def show_synth_sample_quick(sample,saving_path,h,w):
alpha=1/10
'''
if h==None:h=int(alpha*600)
if w==None:w=int(alpha*300)
'''
#'''
if h==None:600
if w==None:300
h=int(alpha*h)
w=int(alpha*w)
#'''
npa_bg=get_npa_bg2(h,w)
for i_asset in range(len(sample.assets)):
asset=sample.assets[i_asset]
'''
if i_asset==0:r,g,b=255,100,100
if i_asset==1:r,g,b=100,255,100
if i_asset==2:r,g,b=100,100,255
'''
r,g,b=100,100,100
if asset.type=="red":r,g,b=255,100,100
if asset.type=="green":r,g,b=100,255,100
if asset.type=="blue":r,g,b=100,100,255
if asset.type=="text":r,g,b=100,255,100
if asset.type=="image":r,g,b=100,100,255
if asset.type=="logo":r,g,b=255,100,100
if asset.type=="cta":r,g,b=100,255,255
npa_bg=add_shape(npa_bg,int(asset.top*h/600),int(asset.left*w/300),int(asset.low*h/600),int(asset.right*w/300),r,g,b)
#npa_bg=resize_image(npa_bg,100)
img=get_image_from_npa(npa_bg)
if saving_path=="":return img #display(img)
if saving_path=="npa":return npa_bg
if saving_path not in ["","npa"]:img.save(saving_path)
# show bboxes
def show_bboxes(bboxes,saving_path,h,w):
# bbox : top,left,low,right
npa_bg=get_npa_bg3(h,w)
for bbox in bboxes:
npa_bg=add_shape2(npa_bg,bbox[0],bbox[1],bbox[2],bbox[3],random.randint(0,255),random.randint(0,255),random.randint(0,255))
npa_bg=resize_image(npa_bg,100)
img=get_image_from_npa(npa_bg)
if saving_path=="":return img
if saving_path=="npa":return npa_bg
if saving_path not in ["","npa"]:img.save(saving_path)
def show_synth_sample(sample,saving_path,h,w):
if h==None:h=600
if w==None:w=300
npa_bg=get_npa_bg2(h,w)
for i_asset in range(len(sample.assets)):
asset=sample.assets[i_asset]
'''
if i_asset==0:r,g,b=255,100,100
if i_asset==1:r,g,b=100,255,100
if i_asset==2:r,g,b=100,100,255
'''
r,g,b=100,100,100
if asset.type=="red":r,g,b=255,100,100
if asset.type=="green":r,g,b=100,255,100
if asset.type=="blue":r,g,b=100,100,255
if asset.type=="text":r,g,b=100,255,100
if asset.type=="image":r,g,b=100,100,255
if asset.type=="logo":r,g,b=255,100,100
if asset.type=="cta":r,g,b=100,255,255
npa_bg=add_shape(npa_bg,asset.top,asset.left,asset.low,asset.right,r,g,b)
npa_bg=resize_image(npa_bg,100)
img=get_image_from_npa(npa_bg)
if saving_path=="":return img #display(img)
if saving_path=="npa":return npa_bg
if saving_path not in ["","npa"]:img.save(saving_path)
# ajouter pour show rapidement synth samples
#def show_synth_sample3sample,saving_path,h,w):
# à corriger (à juste vérifier ?)
def show_synth_sample2(sample,saving_path,h,w):
if 1==0:
if h==None:h=600
if w==None:w=300
npa_bg=get_npa_bg2(h,w)
for i_asset in range(len(sample.assets)):
asset=sample.assets[i_asset]
r,g,b=random.randint(0,255),random.randint(0,255),random.randint(0,255)
npa_bg=add_shape(npa_bg,asset.top,asset.left,asset.low,asset.right,r,g,b)
if 1==1:
if h==None:h=600
if w==None:w=300
npa_bg=get_npa_bg3(h,w)
for i_asset in range(len(sample.assets)):
asset=sample.assets[i_asset]
r,g,b=random.randint(0,255),random.randint(0,255),random.randint(0,255)
npa_bg=add_shape2(npa_bg,asset.top,asset.left,asset.low,asset.right,r,g,b)
npa_bg=resize_image(npa_bg,100)
npa_bg=resize_image(npa_bg,100)
img=get_image_from_npa(npa_bg)
if saving_path=="":return img #display(img)
if saving_path=="npa":return npa_bg
if saving_path not in ["","npa"]:img.save(saving_path)
'''
def get_npa_sample(sample):
npa_bg=get_npa_bg()
for i_asset in range(len(sample.assets)):
asset=sample.assets[i_asset]
r,g,b=100,100,100
if asset.type=="text":r,g,b=100,255,100
if asset.type=="image":r,g,b=100,100,255
if asset.type=="logo":r,g,b=255,100,100
if asset.type=="cta":r,g,b=100,255,255
npa_bg=add_shape(npa_bg,asset.top,asset.left,asset.low,asset.right,r,g,b)
return npa_bg
'''
def get_npa_bg():
r,g,b=100,100,100
npa_bg=np.zeros((600,300,4),dtype=np.uint8)
for i in range(600):
for j in range(300):
npa_bg[i,j,0]=r
npa_bg[i,j,1]=g
npa_bg[i,j,2]=b
npa_bg[i,j,3]=255
return npa_bg
def get_npa_bg2(h,w):
r,g,b=100,100,100
npa_bg=np.zeros((h,w,4),dtype=np.uint8)
for i in range(h):
for j in range(w):
npa_bg[i,j,0]=r
npa_bg[i,j,1]=g
npa_bg[i,j,2]=b
npa_bg[i,j,3]=255
return npa_bg
def get_npa_bg3(h,w,option_color):
if option_color==1:r,g,b=100,100,100
if option_color==2:r,g,b=212,230,242 #129,198,245 # 175,210,233
npa_bg=np.zeros((h,w,4),dtype=np.uint8)
npa_bg[:,:,0]=r
npa_bg[:,:,1]=g
npa_bg[:,:,2]=b
npa_bg[:,:,3]=255
return npa_bg
def save_synth_sample_png(sample,saving_path):
npa_bg=get_npa_bg()
for asset in sample.assets:
r,g,b=200,200,200
npa_bg=add_shape(npa_bg,asset.top,asset.left,asset.low,asset.right,r,g,b)
get_image_from_npa(npa_bg).save(saving_path,"PNG")
# left top width height + no type
def create_sample_from_boxes1(boxes):
sample=clay()
sample.assets=[]
for box in boxes:
asset=clay()
asset.left=box[0]
asset.top=box[1]
asset.width=box[2]
asset.height=box[3]
asset.right=asset.left+asset.width
asset.low=asset.top+asset.height
asset.type="text"
sample.assets.append(asset)
return sample
# left top width height + rico asset type
def create_sample_from_boxes2(boxes):
sample=clay()
sample.assets=[]
for box in boxes:
asset=clay()
asset.left=box[0]
asset.top=box[1]
asset.width=box[2]
asset.height=box[3]
asset.type=box[4]
asset.right=asset.left+asset.width
asset.low=asset.top+asset.height
sample.assets.append(asset)
return sample
def get_synth_sample_png(sample):
npa_bg=get_npa_bg()
for i_asset in range(len(sample.assets)):
asset=sample.assets[i_asset]
'''
if i_asset==0:r,g,b=255,100,100
if i_asset==1:r,g,b=100,255,100
if i_asset==2:r,g,b=100,100,255
'''
r,g,b=100,100,100
if asset.type=="text":r,g,b=100,255,100
if asset.type=="image":r,g,b=100,100,255
if asset.type=="logo":r,g,b=255,100,100
if asset.type=="cta":r,g,b=100,255,255
npa_bg=add_shape(npa_bg,asset.top,asset.left,asset.low,asset.right,r,g,b)
npa_bg=resize_image(npa_bg,100)
return npa_bg
# get sample image npa from sample name
def get_screenshot_npa(file_name):
root_path='/home/paintedpalms/rdrive/taff/data/automated_layout_real/pubs_madmix/segm2'
image_path=root_path+'/'+file_name
#image = PIL.Image.open(image_path, "r")
image=Image.open(image_path, "r")
npa = np.asarray(copy.copy(image))
return npa
def visual_eval(samples_test,samples_pred,names):
for i_sample in range(len(samples_test)):
visual_eval_single(samples_test[i_sample],samples_pred[i_sample],names[i_sample])
#def visual_eval_single(sample_test,sample_pred,name):
def visual_eval_single(sample_test,sample_pred,name,display_option):
n_assets=3
saving_path="results"
npa=get_screenshot_npa(name)
npa_bg=get_npa_bg()
for i_asset in range(n_assets):
# predicted asset + original asset
asset_test=sample_test.assets[i_asset]
asset_pred=sample_pred.assets[i_asset]
top_new,left_new,low_new,right_new=asset_pred.top,asset_pred.left,asset_pred.low,asset_pred.right
top_src,left_src,low_src,right_src=int(asset_test.top),int(asset_test.left),int(asset_test.low),int(asset_test.right)
npa_asset=npa[top_src:low_src,left_src:right_src]
npa_asset=resize_image(npa_asset,asset_pred.width)
for i in range(len(npa_asset)):
for j in range(len(npa_asset[0])):
i_bg=top_new+i
j_bg=left_new+j
if 0<i_bg<600 and 0<j_bg<300:
npa_bg[i_bg,j_bg]=npa_asset[i,j]
npa_sample=get_screenshot_npa(name)
npa_couple=np.concatenate((npa_bg,npa_sample),axis=1)
image_couple=get_image_from_npa(npa_couple)
if display_option==1:display(image_couple)
if display_option==2:image_couple.save(saving_path+'/'+str(i_sample)+'.png',"PNG")
# show gan synth layouts
def show_gan_synth_layouts(c,samples_gan,i_epoch,save_option):
for i_sample in range(min(len(samples_gan),100)):
if save_option==0:p=""
if save_option==1:p=c.results_folder+"/ep"+str(i_epoch)+"_sample"+str(i_sample)+".png"
show_synth_sample(samples_gan[i_sample],p,None,None)
def save_visual_inputs_and_outputs(samples_test,names,folder_name):
root_path='/home/paintedpalms/rdrive/taff/data/automated_layout_real/pubs_madmix/segm3'
n_assets=3
for i_sample in range(1):
name=names[i_sample]
saving_path=root_path+'/'+folder_name
npa=get_screenshot_npa(name)
npa_bg=get_npa_bg()
for i_asset in range(n_assets):
# predicted asset + original asset
asset_test=samples_test[i_sample].assets[i_asset]
top_src,left_src,low_src,right_src=int(asset_test.top),int(asset_test.left),int(asset_test.low),int(asset_test.right)
npa_asset=npa[top_src:low_src,left_src:right_src]
npa_asset=resize_image(npa_asset,asset_test.width)
image_asset=get_image_from_npa(npa_asset)
image_asset.save(saving_path+'/'+str(i_sample)+'_'+str(i_asset)+'.png',"PNG")
npa_sample=get_screenshot_npa(name)
image_sample=get_image_from_npa(npa_sample)
image_sample.save(saving_path+'/'+str(i_sample)+'.png',"PNG")
## layout eval
#------------------------------------------------------------------------------------------------------------------------------------
# public imports
#------------------------------------------------------------------------------------------------------------------------------------
# ok
#------------------------------------------------------------------------------------------------------------------------------------
# quant eval : diversity
#------------------------------------------------------------------------------------------------------------------------------------
# measure diversity in samples generated by layout gan
def diversity_eval(samples):
combinaisons=[]
for sample in samples:
combinaison=[]
for asset in sample.assets:
combinaison.append(asset.type)
if combinaison not in combinaisons:combinaisons.append(combinaison)
return len(combinaisons)#/max(1,len(samples))
#------------------------------------------------------------------------------------------------------------------------------------
# quant eval : error
#------------------------------------------------------------------------------------------------------------------------------------
# eval samples concerned by general rules only
# ancien
def quant_eval(samples,str_option,w,h):
#h=600
#w=300
top_overtakings=[]
left_overtakings=[]
right_overtakings=[]
low_overtakings=[]
overtakings=[]
overlaps_btw_assets=[]
n_samples=len(samples)
n_assets=3
n_samples_in_error=0
n_assets_in_error=0
# overlap or exceeding screen limits => error
for sample in samples:
prev_low=0
sample_is_in_error=0
for i_asset in range(n_assets):
asset_is_in_error=0
asset=sample.assets[i_asset]
if i_asset==0 and asset.top<0:
asset_is_in_error=1
sample_is_in_error=1
top_overtakings.append(0-asset.top)
if i_asset==n_assets-1 and asset.low>h:
asset_is_in_error=1
sample_is_in_error=1
low_overtakings.append(asset.low-h)
if asset.left<0:
asset_is_in_error=1
sample_is_in_error=1
left_overtakings.append(0-asset.left)
if asset.right>w:
asset_is_in_error=1
sample_is_in_error=1
right_overtakings.append(asset.right-w)
if i_asset>0 and asset.top<prev_low:
asset_is_in_error=1
sample_is_in_error=1
overlaps_btw_assets.append(prev_low-asset.top)
prev_low=asset.low
n_assets_in_error+=asset_is_in_error
n_samples_in_error+=sample_is_in_error
for values in left_overtakings,top_overtakings,right_overtakings,low_overtakings:
for v in values:overtakings.append(v)
results={}
results["overtakings"]={}
results["overtakings"]["all"]=overtakings
results["overtakings"]["left"]=left_overtakings
results["overtakings"]["top"]=top_overtakings
results["overtakings"]["right"]=right_overtakings
results["overtakings"]["low"]=low_overtakings
results["overlaps"]=overlaps_btw_assets
results["n_samples"]=n_samples
results["n_samples_in_error"]=n_samples_in_error
results["n_assets"]=n_assets*n_samples
results["n_assets_in_error"]=n_assets_in_error
results["samples_in_error"]=n_samples_in_error/max(1,n_samples)
results["assets_in_error"]=n_assets_in_error/max(1,(n_samples*n_assets))
if 0==1:
name="samples in error"
if len(name)<=7:after_name="\t\t\t:"
if len(name)>7:after_name="\t\t:"
if len(name)>15:after_name="\t:"
if 1==1:
s=""
s+="\n"
s+="quant eval"+"\n"
s+="\n"
for k0 in results.keys():
if type(results[k0])!=dict:
if str_option==0:display_stats_line(k0,results[k0])
if str_option==1:
if k0 in ["samples_in_error","assets_in_error"]:
s+=get_str_with_tabs(k0,results[k0],3)#+"\n"
for k0 in results.keys():
if type(results[k0])==dict:
for k1 in results[k0].keys():
if str_option==0:display_stats_line(k1,results[k0][k1])
if str_option==1:
if k1 in ["samples_in_error","assets_in_error"]:
s+=get_str_with_tabs(k1,results[k0][k1],3)#+"\n"
return s,n_samples_in_error/max(1,n_samples)
#------------------------------------------------------------------------------------------------------------------------------------
# quant eval : error
#------------------------------------------------------------------------------------------------------------------------------------
def quant_eval0(samples):
m=0
w=300
h=600
k=0
k_bench=0
ks=0
s=0
k_overlap=0
for sample in samples:
k_bench+=1
i=0
prev_low=0
for asset in sample.assets:
if i>0:prev_low=sample.assets[i-1].low
next_top=h
if i<2:next_top=sample.assets[i+1].top
i+=1
k_temp=k
if asset.top<prev_low-m*2:
k+=1
s+=abs(asset.top-prev_low)
k_overlap+=1
'''
if asset.low>next_top+m*2:
k+=1
s+=abs(asset.low)
k_overlap+=1
'''
if asset.right>w+m:
k+=1
s+=abs(asset.right-w)
if asset.low>h+m*2:
k+=1
s+=abs(asset.low-h)
if asset.left<0-m:
k+=1
s+=abs(asset.left)
'''
if asset.top<0-m*2:
k+=1
s+=abs(asset.top)
'''
if k>k_temp:ks+=1
if ks==0:print("eval",k_bench,k,s)
if ks!=0:print("eval",k_bench,k,s/ks)
#features distributions (no error measurement !!!)
def init_stats(c):
stats={}
stats["lefts"]=[]
stats["tops"]=[]
stats["rights"]=[]
stats["last lows"]=[]
stats["lateral mids"]=[]
stats["vertical mids"]=[]
for i_asset in range(c.n_assets):
stats[i_asset]={}
for tp in c.types:
stats[i_asset][tp]={}
for ft in c.features:
stats[i_asset][tp][ft]=[]
return stats
def get_stats(c,samples):
stats=init_stats(c)
for i_sample in range(len(samples)):
sample=samples[i_sample]
for i_asset in range(c.n_assets):
asset=sample.assets[i_asset]
# main stats
stats["lefts"].append((asset.left))
stats["tops"].append((asset.top))
stats["lateral mids"].append(int(np.round(asset.left+(asset.right-asset.left)/2)))
stats["vertical mids"].append(int(np.round(asset.top+(asset.low-asset.top)/2)))
stats["rights"].append(asset.right)
if i_asset==c.n_assets-1:
stats["last lows"].append(asset.low)
# all stats
for i_feature in range(c.n_features):
#print("okok",i_sample,i_asset,asset.type)
'''
print("---------------------------------------------")
print("i_sample,i_asset",i_sample,i_asset)
print("width",sample.assets[i_asset].width)
print(asset.type)
print(stats[i_asset])
print(stats[i_asset][asset.type])
print(stats[i_asset][asset.type]["width"])
print(sample.assets[i_asset].width)
'''
stats[i_asset][asset.type]["width"].append(sample.assets[i_asset].width)
stats[i_asset][asset.type]["height"].append(sample.assets[i_asset].height)
stats[i_asset][asset.type]["left"].append(sample.assets[i_asset].left)
stats[i_asset][asset.type]["top"].append(sample.assets[i_asset].top)
stats[i_asset][asset.type]["right"].append(sample.assets[i_asset].right)
stats[i_asset][asset.type]["low"].append(sample.assets[i_asset].low)
return stats
#tagtag
def save_stats_str(c,stats):
s=""
s+="------------------ main stats"+"\n"
for name in "lateral mids","vertical mids","lefts","rights","tops","last lows":
values=stats[name]
if name in ["rights","lefts","tops"]:name+="\t"
s+=name+"\t:"+"\t"+str(min(values))+"\t"+str(max(values))+"\t"+str(int(np.round(np.mean(values))))+"\t"+str(int(np.round(np.std(values))))+"\n"
s+="\n"
s+="------------------ all stats"+"\n"
for i_asset in range(c.n_assets):
for tp in c.types:
for ft in c.features:
values=stats[i_asset][tp][ft]
if len(values)==0:
s+=str(i_asset)+" "+str(tp)+" "+str(ft)+"\t:"+"\t no values"+"\n"
if len(values)> 0:
s+=str(i_asset)+" "+str(tp)+" "+str(ft)+"\t:"+"\t"+str(min(values))+"\t"+str(max(values))+"\t"+str(int(np.round(np.mean(values))))+"\t"+str(int(np.round(np.std(values))))+"\n"
s+="\n"
save_text(c.results_folder+"/stats.txt",s)
def save_text(p,s):
file = open(p,"w")
file.write(s)
file.close()
def display_stats(c,stats):
print("------------------ main stats")
for name in "lateral mids","vertical mids","lefts","rights","tops","last lows":
values=stats[name]
if name in ["rights","lefts","tops"]:name+="\t"
print(name,"\t:","\t",min(values),"\t",max(values),"\t",int(np.round(np.mean(values))),"\t",int(np.round(np.std(values))))
print("")
print("------------------ all stats")
for i_asset in range(c.n_assets):
for tp in c.types:
for ft in c.features:
values=stats[i_asset][tp][ft]
if len(values)==0:print(i_asset,tp,ft,"\t:","\t no values")
if len(values)> 0:
print(i_asset,tp,ft,"\t:","\t",min(values),"\t",max(values),"\t",int(np.round(np.mean(values))),"\t",int(np.round(np.std(values))))
print("")
'''
############################################## extract data distributions
def extract_distributions(samples):
w=300
h=600
n_assets=len(samples[0].assets)
params=[]
for asset_type in "text","image","cta","logo":
p=Clay()
n=0
lefts,tops,rights,lows=[],[],[],[]
widths=[]
heights=[]
ranks=[]
abs_ranks=[]
prev_spaces=[]
next_spaces=[]
global_heights=[]
areas=[]
width_height_ratios=[]
for sample in samples:
global_height=0
for i_asset in range(n_assets):
asset=sample.assets[i_asset]
global_height+=asset.height
if asset.type==asset_type:
n+=1
ranks.append(i_asset)
abs_ranks.append(abs(1-i_asset))
lefts.append(asset.left)
rights.append(asset.right)
tops.append(asset.top)
lows.append(asset.low)
widths.append(asset.width)
heights.append(asset.height)
width_height_ratios.append(asset.height/asset.width)
areas.append(asset.width*asset.height)
if i_asset==0:prev_spaces.append(asset.top)
if i_asset==n_assets-1:next_spaces.append(h-asset.low)
global_heights.append(global_height)
zeros=0
ones=0
twos=0
for rank in ranks:
if rank==0:zeros+=1
if rank==1:ones+=1
if rank==2:twos+=1
c.asset_type=asset_type
c.ranks=[zeros,ones,twos]
c.left=np.average(lefts),np.std(lefts)
c.top=np.average(tops),np.std(tops)
c.right=np.average(rights),np.std(rights)
c.low=np.average(lows),np.std(lows)
c.w=np.average(widths),np.std(widths)
c.h=np.average(heights),np.std(heights)
c.hw=np.average(width_height_ratios),np.std(width_height_ratios)
c.th=np.average(global_heights),np.std(global_heights)
c.ps=np.average(prev_spaces),np.std(prev_spaces)
c.ns=np.average(widths),np.std(widths)
params.append(p)
return params
def display_distributions(params):
for p in params:
if sum(p.ranks)>0:
print("------------------",p.asset_type)
names=["ranks","w","h","th","left","right","top","low","ps","ns",]
for name in names:#[1:]:
print(name,getattr(p, name))
'''
print(end="")
## layout creat data
# public imports
import random
import time
import math
from PIL import Image
import numpy as np
import copy
## random array
if 1==0:
npa = np.random.rand(3,2)
#------------------------------------------------------------------------------------------------------------------------------------
# # real
#------------------------------------------------------------------------------------------------------------------------------------
def read_pl(file_path):
file = open(file_path,"r")
text = file.read()
file.close()
lines = text.split('\n')
pl=[]
for line in lines:
pl.append(line)
return pl
def get_samples_from_xy_real(x,y,names):
n_samples=len(x)
n_assets=len(x[0])
samples=[]
for i_sample in range(n_samples):
sample=Clay()
if len(names)>0:sample.name = names[i_sample]
sample.assets=[]
for i_asset in range(n_assets):
asset=Clay()
asset.input_width=int(x[i_sample,i_asset,0])
asset.input_height=int(x[i_sample,i_asset,1])