/
fca2web.py
1198 lines (1038 loc) · 63.8 KB
/
fca2web.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 os
import sys
import numpy as np
import requests
import pandas as pd
from pandas import ExcelFile
from datetime import datetime
from datetime import timedelta
import time, socket
import seaborn as sns
import matplotlib.pyplot as plt
from glob import glob
import textwrap
import socket
from PIL import Image
from random import randrange
import streamlit as st
#from streamlit import caching
import base64
FC_Auto_Analyser_Version = 'fca2web beta 0.96 (2021SET05A) '
#global UserAgents, runningOn, parâmetros, remarks, sizeKB
runningOn = socket.gethostname()
#USerAgents para post das estatísticas
with open("./UserAgents.cfg",encoding='utf-8') as f:
UserAgents = f.readlines(); f.close()
UserAgents = [c.replace('\n','').strip() for c in UserAgents]
if 'WIN11MM23' in runningOn:
import configparser
config_parser = configparser.RawConfigParser()
config_parser.read('./fca2web.ini')
url_post_stat = config_parser.get('RUN', 'url_post_stat')
elif runningOn == 'localhost': #suggested running on streamlit share
url_post_stat = st.secrets["url_post_stat"]
else: #running on Heroku
url_post_stat = os.environ["url_post_stat"]
#caching.clear_cache()
st.set_page_config(
page_title="FCA2 - FCastell Auto Analyser")
def run():
global msg_count, max_freq, hist_bins, max_dups
global hist, boxp, mcorr, noheader, sep_csv, filecst, dec_csv, sampleN
global UserAgents, runningOn, parâmetros, remarks, sizeKB
st.markdown(f'''
<body>
<p style="font-size:50px;line-height: 25px"><i><b>FCA2</b></i><br>
<p style="font-size:30px;line-height: 25px"><b>FCastell Auto Analyser</b><br>
<span style="font-size: 12pt;"><i>{FC_Auto_Analyser_Version} by Fabiano Castello.</i></span>
</p>
</body>
''', unsafe_allow_html=True)
# COLOCAR PARAMETROS NO SIDEBAR
hide_streamlit_style = """<style>#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
max_freq = st.sidebar.slider('Qte máxima de categorias nas colunas de texto:', 1, 20, max_freq,
help=tool_tips('max_freq'))
hist_bins = st.sidebar.slider('Qte de bins no histograma:', 1, 20, hist_bins,
help=tool_tips('hist_bins'))
max_dups = st.sidebar.slider('Qte máxima de exemplos de duplicados:', 1, 10, max_dups,
help=tool_tips('max_dups'))
sampleN = st.sidebar.slider('Qte máxima de itens para amostragem:', 1, 10, sampleN,
help=tool_tips('sampleN'))
hist = st.sidebar.checkbox('Mostrar histograma?', value=False,
help=tool_tips('max_dups'))
boxp = st.sidebar.checkbox('Mostrar boxplot?', value=False,
help=tool_tips('boxp'))
mcorr = st.sidebar.checkbox('Mostrar matriz de correlação?', value=False,
help=tool_tips('mcorr'))
noheader = st.sidebar.checkbox('Marque se o seu arquivo não tem header (cabeçalho)', value=False,
help=tool_tips('noheader'))
filecst = st.sidebar.checkbox('Marque para customizar colunas', value=False,
help=tool_tips('filecst'))
sep_csv = st.sidebar.text_input('Definir separador específico para arquivos CSV?', value='',
help=tool_tips('sep_csv'))
dec_csv = st.sidebar.checkbox('Marque para usar "," em colunas decimais em arquivos CSV(padrão=".")', value=False, help=tool_tips('dec_csv'))
try: del uploaded_files
except: pass
titanic = st.checkbox('Marque para ver uma demonstração (titanic)', value=False,
help=tool_tips('titanic'))
sharing = st.empty()
with st.expander("Considerações sobre formatos de arquivos e campos de data"):
st.write("""
FCA2 aceita arquivos formato CSV, Excel, Feather, Pickle e Pickle compactado (gzip). Se vc tem um arquivo CSV com extensão TXT ou outra, renomeie para que o arquivo seja analisado. O limite para análise é de 200Mb. Localmente é possível rodar arquivos até 1Gb, porém arquivos com mais de 200Mb o processamento é lento e todos os recursos são consumidos, a ponto de você achar que sua máquina travou. """)
st.write("""
**Em termos técnicos**, arquivos XLS muitas vezes são problemátivos, sempre que possível use XLSX; nos casos de arquivos CSV com problema de tokenização o FCA2 automaticamente tenta carregar usando o engine Python e, no caso de falha, o engine C++; Pickle funciona com protocolos 1 a 4, compactados com gzip ou não. Feather "vanilla" roda bem, mas dependendo da versão de pyarrow instalada não há suporte para lz4 e snappy.""")
st.write("""
**Sobre datas**, a partir de setembro de 2021 implementei análise de colunas datetime. Estava fazendo falta e agora está bem completo. No entanto, são raras as situações onde o pandas detecta colunas datetime de forma automática; nestes casos use a customização de colunas. Converter texto em datas é um processo relativamente lento porque são feitos testes por amostragem para vários formatos de datas (por exemplo %Y-%m-%d, %Y%m%d, %d/%m/%Y), e, após o teste, tenta-se a conversão pelo formato que teve menos erros de conversão. Outro ponto, existe uma análise chamada "filling", que considera o range de dias, meses e anos e informa, para todos os ranges, em qual "slots" há dado presente. Última coisa: em todas as análises o formato utilizado é o ISO (YYYY-MM-DD).""")
sharing_message = """
<body><p style="font-size:14px;line-height:16px"><b>Curtiu o FCA2? Compartilhe!</b></span>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<a href="https://www.facebook.com/sharer/sharer.php?title=FCA2+Análise+exploratória+de+dados+gratuita,+segura+e+colaborativa&u=https://www.fabianocastello.com.br/fca2/"
style="text-decoration:none" target="_blank"
class="fa fa-facebook"> </a>
<a href="https://twitter.com/intent/tweet?text=FCA2+Análise+exploratória+de+dados+gratuita,+segura+e+colaborativa&url=https://www.fabianocastello.com.br/fca2/"
style="text-decoration:none" target="_blank"
class="fa fa-twitter"></a>
<a href="whatsapp://send?text=FCA2+Análise+exploratória+de+dados+gratuita,+segura+e+colaborativa https://www.fabianocastello.com.br/fca2/"
style="text-decoration:none" target="_blank"
class="fa fa-whatsapp"></a>
</p></body>
"""
st.markdown(sharing_message, unsafe_allow_html=True)
uploaded_file = st.file_uploader('Informe o arquivo para análise. Acesse a barra lateral (">" à esquerda) para opções. O link para baixar o relatório em formato TXT está no final da análise.', type =['csv','xls','xlsx','feather','pkl','zpkl'],
accept_multiple_files=False, help=tool_tips('uploaded_file'))
# UploadedFile(id=8, name='xxx 000 Test Dates Simplificado.xlsx', type='application/vnd.ms-excel', size=1211)
# ago/21 - restrito para um arquivo apenas
if uploaded_file is not None or titanic:
if titanic:
with open('./titanic.pkl', 'rb') as base:
content = base.read()
with open(os.path.join(datain,"titanic.pkl"),"wb") as f:
f.write(content)
hist=True
boxp = True
mcorr = True
else:
with open(os.path.join(datain,uploaded_file.name),"wb") as f:
f.write(uploaded_file.getbuffer())
files = os.listdir(datain)
if len(files) != 0:
dfs = [i for i in files if ('XLSX' in i[-4:].upper()) or
('XLS' in i[-3:].upper()) or
('CSV' in i[-3:].upper()) or
('FEATHER' in i[-7:].upper()) or
('PKL' in i[-3:].upper()) or
('ZPKL' in i[-4:].upper()) ]
msg_count = 0
filesToWorkOn = sorted(dfs)
for name in filesToWorkOn:
st.markdown(f'''<body><p style="font-size:20px;margin-bottom: -5px;">
Analisando <b>{name}</b></p></body>''', unsafe_allow_html=True)
parâmetros = f'max_freq={max_freq}; hist_bins={hist_bins}; max_dups={max_dups};'+\
f'hist={hist}; boxp={boxp}; mcorr={mcorr}; noheader={noheader}; '
remarks = ''
sizeKB = int(round(os.stat(datain+"/"+name).st_size/1024,0))
global output
output = pd.DataFrame(columns=['TS','Row','Content','NewLine'])
analysis(name)
st.markdown(f'''
<body>
<p style="font-size:30px;margin-bottom: -5px;">
<br>
<b>Sobre FCA2</b></p>
<p style="font-size:16px;margin-bottom: -5px;">
FCA2 é um algoritmo criado em Python para análise exploratória básica de dados, que visa trazer <b>produtividade</b> para analistas. De forma automática, o algoritmo trata arquivos em formato csv, xls e xlsx e realiza diversas análises: <span style="font-size:10px ;margin-bottom: -5px;">
<ul style="margin-bottom: -5px;">
<li>identificação de colunas texto (categóricas), colunas numéricas inteiras e numéricas decimais.</li>
<li>colunas texto: quantidade de registros, registros ausentes, duplicações e categorias, top "n" categorias.</li>
<li>colunas numéricas: quantidade de registros, registros zerados, soma total, média, desvio, máximos e mínimos, amplitude, quartis. Mesmas análises para a base descontando os registros zerados, lembrando que zero é diferente de ausente :-).</li>
<li>colunas datetime: quantidade de registros, registros ausentes, duplicações, datas mais antiga, mais recente e média, amplitude do período de análise e "filling", quantidade de datas por quartis (Q1, Q2, Q3) e uma distribuição visual geral.</li>
</ul></span><span style="font-size:16px ;line-height: 25px"><br>
Desenvolvido originalmente por Fabiano Castello (<a target="_blank" href ="http://www.fabianocastello.com.br">www.fabianocastello.com.br</a>), é disponibilizado <b>gratuitamente</b> sob licença CC BY 4.0 para a comunidade. O código original está registrado sob DOI <a target="_blank" href ="http://doi.org/10.6084/m9.figshare.9902417">doi.org/10.6084/m9.figshare.9902417</a>. A versão web foi criada em streamlit e está disponível em (<a target="_blank" href ="http://www.github.com/fabianocastello/fca2web">www.github.com/fabianocastello/fca2web</a>). Se você usar esta aplicação em um artigo ou publicação pode incluir a citação "Castello, Fabiano (2019): <i>Python Code: FC Auto Analyser (FCA2)</i>. figshare. Software. https://doi.org/10.6084/m9.figshare.9902417.v1".</span> </p><br>
<p style="font-size:20px;margin-bottom: -5px;"><b><i>What's New @ {FC_Auto_Analyser_Version}</i></b></p>
<p style="font-size:16px;margin-bottom: -5px<span style="font-size:10px ;margin-bottom: -5px;">
<ul style="margin-bottom: -5px;">
<li>análises de colunas datetime (set/21).</li>
<li>links para compartilhar no Facebook, Twitter e Whatsapp (set/21).</li>
<li>formatos Feather e Pickle (em beta) (ago/21).</li>
<li>download do relatório da análise em TXT (ago/21).</li>
<li>melhorias na formatação do relatório (ago/21).</li>
<li>mudança do licenciamento para CC BY 4.0 (jul/21).</li>
<li>boxplot, customização de colunas, amostragem e exemplos de duplicados (ago/21).</li>
<li>tentativa automática de abrir arquivo CSV com engine Python e C (ago/21).</li>
</ul></span><span style="font-size:16px ;line-height: 25px"><br>
<p style="font-size:20px;margin-bottom: -5px;"><b><i>Issues</i> conhecidos</b></p>
<p style="font-size:16px;margin-bottom: -5px;">
<ul style="margin-bottom: -5px;">
<li>formatos Feather e Pickle, dependendo da compressão usada no momento da gravação podem apresentar problemas para serem abertos no FCA2.</li>
<li><i>Se vc identificar um problema faça o reporte em <a target="_blank" href ="http://www.github.com/fabianocastello/fca2web/issues">www.github.com/fabianocastello/fca2web/issues</a></i>.</li>
</ul></p><br>
<p style="font-size:20px;margin-bottom: -5px;"><b>O que está no <i>pipeline</i></b></p>
<p style="font-size:16px;margin-bottom: -5px<span style="font-size:10px ;margin-bottom: -5px;">
<ul style="margin-bottom: -5px;">
<li>gerar um arquivo em PDF com a análise consolidada, incluindo os gráficos.</li>
<li>gerar gráficos com informações relevantes, usando seaborn.</li>
<li>analisar colunas do tipo binário.</li>
<li>postar estatísticas da utilização.</li>
</ul></span><span style="font-size:16px ;line-height: 25px"><br>
<p style="font-size:20px;margin-bottom: -5px;"><b>Sobre LGPD, GRPR e confidencialidade de dados</b></p>
<p style="font-size:16px;margin-bottom: -5px;">
FCA2 cria containers a partir dos arquivos carregados para tratamento e destrói a informação assim que o processamento é realizado. Nenhuma informação é retida ou enviada para fora do site (exceto tipo do arquivo, tamanho, tempo de processamento e parâmetros, para efeito de gerar estatísticas). Todos os arquivos tempororários geradaos são apagados.</p><br>
<p style="font-size:20px;margin-bottom: -5px;"><b>Contribuições</b></p>
<p style="font-size:16px;margin-bottom: -5px;">
FCA2 é mantido pelo autor com contribuições da comunidade.<br>Thanks: Marcus Pinto, João Victor Mulle, Mateus Ricci, Vivian Sato.<br><i>Para contribuir com o código do projeto faça um "fork" a partir do repositório <a target="_blank" href ="http://www.github.com/fabianocastello/fca2web">www.github.com/fabianocastello/fca2web</a>. Para sugerir melhorias, mesmo sem ter a menor noção de como fazer isso na linguagem Python, envie um email para <a href ="mailto:fca2@fabianocastello.com.br?Subject:Contribuição_FCA2">fca2@fabianocastello.com.br</a></i>.</p><br>
<p style="font-size:20px;margin-bottom: -5px;"><b>FCA2 na sua organização</b></p>
<p style="font-size:16px;margin-bottom: -5px;">
FCA2 pode ser instalado num servidor da sua organização por um valor fixo mensal. Para mais informações contate a <a target="_blank" target="_blank" href ="http://www.cdatalab.com.br">cDataLab</a>.</p>
</body>
''', unsafe_allow_html=True)
return()
######################################################################
###################################################################### End of Streamlit
######################################################################
#Parâmetros
datain = "./!data.in"
dataout = "./!data.out" #onde analisador vai gravar os resultados
datalog = "./!data.log" #onde o analisador vai gravar os logs do processamento
datatmp = "./!data.tmp" #arquivos temporários. Será limpo após o processamento
max_freq = 10 #numeros de categorias máximas nos campos texto
hist_bins = 10 #qte de bins no histograma
max_dups = 5 #qte de exemplos de duplicados
sampleN = 5 #qte de exemplos da coluna
#Criando diretórios se inexistentes
dirs = ['!data.tmp','!data.out','!data.log','!data.in']
for d in dirs:
if not os.path.exists(d):
os.makedirs(d)
else:
files = glob(f'{d}/*')
for f in files: os.remove(f)
def post_stat_fca2(filename, sizeKB, parâmetros, elap,remarks=''):
form_data = { 'entry.525617926' : runningOn,
'entry.506775480' : filename,
'entry.442023490' : sizeKB,
'entry.2027178672': elap,
'entry.222084486' : parâmetros,
'entry.225285568' : runningOn,
'entry.1052496244': remarks }
return(requests.post(url_post_stat+'/formResponse', data=form_data, headers=
{'Referer':url_post_stat.strip()+'/viewform',
'User-Agent': UserAgents[randrange(len(UserAgents))]}))
global msg_count
msg_count = 0
def msgC():
global msg_count
msg_count += 1
return(str(msg_count).zfill(4))
def log_write(msg, newline=False, addcont=True):
global output
ident = ' '*5
if newline:
st.markdown(f'''<body><p style="font-size:14px;margin-bottom: -5px;
font-family: monospace;"><br></p></body>''', unsafe_allow_html=True)
#msg = msg.replace(' ',' ')
cut = 105
msgCstr = msgC() if addcont else None
if addcont:
full_msg = (f'{msgCstr} {msg}').strip()
else:
full_msg = (f'{ident+msg}').strip()
# output = output.append({
# 'TS' : datetime.today(),
# 'Row' : msgCstr,
# 'Content' : msg,
# 'NewLine' : newline,
# },ignore_index=True)
new_data = pd.DataFrame({
'TS': [datetime.today()],
'Row': [msgCstr],
'Content': [msg],
'NewLine': [newline]
})
output = pd.concat([output, new_data], ignore_index=True)
first_msg = full_msg[:cut].replace(' ', ' ').replace('\n', '<br>')
first_msg = first_msg.replace('<b>', '83e7b7bc412f20').replace('</b>', '8xe7b7bc412f20B')
first_msg = first_msg.replace('<', '<').replace('>', '>')
first_msg = first_msg.replace('83e7b7bc412f20', '<b>').replace('8xe7b7bc412f20B','</b>')
rest_msg = full_msg[cut:].replace(' ', ' ').replace('\n', '<br>')
rest_msg = rest_msg.replace('<b>', '83e7b7bc412f20').replace('</b>', '8xe7b7bc412f20B')
rest_msg = rest_msg.replace('<', '<').replace('>', '>')
rest_msg = rest_msg.replace('83e7b7bc412f20', '<b>').replace('8xe7b7bc412f20B','</b>')
st.markdown(f'''<body><p style="font-size:14px;margin-bottom: -5px;
font-family: monospace">
{first_msg}</p></body>''', unsafe_allow_html=True)
if len(rest_msg) > 0:
msgs = textwrap.wrap(rest_msg, cut)
for msg in msgs:
st.markdown(f'''<body><p style="font-size:14px;margin-bottom: -5px;
font-family: monospace" >
{ident+msg.strip()}</p></body>''', unsafe_allow_html=True)
return() #
def sep(ser):
if ser > 0: return(", ")
return("")
def analysis(file):
global df,ctmp, ctmp_counts, i, x, xqte_corr
global start_time
if True: #try:
log_write("Iniciando análise de "+file)
start_time = time.time()
# open the file
if 'csv' in file.lower():
decimalcsv = ',' if dec_csv else '.'
try:
if len(sep_csv.strip()) == 0:
f = open(datain+"/"+file,encoding='utf-8')
line = f.readline()
f.close()
semicolon = line.count(";")
comma = line.count(",")
if semicolon > comma:
separador = ";"
else:
separador = ","
else:
separador = sep_csv.strip()
log_write("Separador de CSV selecionado [ "+separador+" ]")
log_write("Parâmetro para leitura de números decimais [ "+decimalcsv+" ]")
if separador == decimalcsv:
st.error('ALERTA!!! separador e parâmetro de decimais identicos. Verifique.')
if noheader:
try:
df = pd.read_csv(datain+"/"+file, encoding ='utf-8', engine='python', sep = separador, header=None, decimal=decimalcsv)
except Exception as erro:
log_write("Erro! Tentando com engine C")
df = pd.read_csv(datain+"/"+file, encoding ='utf-8', engine='c', sep = separador, header=None, decimal=decimalcsv)
log_write("Base carregada com sucesso")
for c in df.columns:
df.rename({c:f'Coluna{c}'}, axis=1, inplace=True)
else:
log_write("Abrindo arquivo com engine Python")
try:
df = pd.read_csv(datain+"/"+file, encoding ='utf-8', engine='python',
sep = separador, decimal=decimalcsv)
except Exception as erro:
log_write("Erro! Tentando com engine C")
df = pd.read_csv(datain+"/"+file, encoding ='utf-8', engine='c',
sep = separador, decimal=decimalcsv)
log_write("Base carregada com sucesso")
except Exception as erro:
log_write("Erro "+str(erro))
log_write("Abortando analise "+file)
log_write("Sugestão: verifique se o arquivos está codificado como UTF-8")
return(-1)
elif 'xls' in file.lower():
try:
if noheader:
df = pd.read_excel(datain+"/"+file, header=None)
for c in df.columns:
df.rename({c:f'Coluna{c}'}, axis=1, inplace=True)
else:
df = pd.read_excel(datain+"/"+file)
except Exception as erro:
log_write("Erro "+str(erro))
log_write("Abortando analise "+file+"\n")
return(-1)
elif 'feather' in file.lower():
import lz4
try:
if noheader:
log_write("Feather não tem a opção de carregar sem header")
df = pd.read_feather(datain+"/"+file)
except Exception as erro:
log_write("Erro "+str(erro))
log_write("Abortando analise "+file+"\n")
return(-1)
elif 'zpkl' in file.lower():
import gzip, pickle, pickletools
if noheader:
log_write("Gzip Pickle não tem a opção de carregar sem header")
with gzip.open(datain+"/"+file, 'rb') as f:
df = pickle.Unpickler(f).load()
elif 'pkl' in file.lower():
try:
import gzip, pickle, pickletools
if noheader:
log_write("Pickle não tem a opção de carregar sem header")
try:
df = pd.read_pickle(datain+"/"+file)
except:
import pickle5 as pickle
with open(datain+"/"+file, "rb") as fh:
df = pickle.load(fh)
except Exception as erro:
log_write("Erro "+str(erro))
log_write("Abortando analise "+file+"\n")
return(-1)
else:
log_write("Erro identificando arquivo")
return(-1)
if filecst:
df = custom_df(df)
analysis_df(df,file)
return(None)
def custom_df(df):
custom = pd.DataFrame(columns = ['Índice','Coluna','Type','Tipo','Custom','CustomType'])
for c in df.columns:
# custom = custom.append({'Coluna': c,
# 'Type' : df[c].dtype}, ignore_index=True)
new_data = pd.DataFrame({
'Coluna': [c],
'Type': [df[c].dtype]
})
custom = pd.concat([custom, new_data], ignore_index=True)
custom['Índice'] = custom.index+1
for index, row in custom.iterrows():
if 'Unnamed' in row['Coluna']:
custom.at[index,'Coluna'] = 'Coluna '+str(index+1)
df.rename({row['Coluna']:'Coluna '+str(index+1)}, axis=1, inplace=True)
if row['Type'] == 'object':
custom.at[index, 'Tipo'] = 'Texto'
elif row['Type'] == 'int64':
custom.at[index, 'Tipo'] = 'Numérico(int)'
elif row['Type'] == 'float64':
custom.at[index, 'Tipo'] = 'Numérico(dec)'
else:
custom.at[index, 'Tipo'] = 'Texto'
custom['Custom'] = custom['Tipo']
opt = dict()
opt['Texto'] = 0
opt['Numérico(int)'] = 1
opt['Numérico(dec)'] = 2
opt['Datetime'] = 3
global i
with st.form("my_form"):
st.write("Customização de colunas", help=tool_tips('form_custom'))
for i in range(0,custom.shape[0]):
col = custom['Coluna'].loc[i]
exemplos = 'Exemplos: '+('; '.join(
[str(c) for c in list(df[col].unique()[:3])])).strip()
s = "custom"+str(i)+"=st.radio(f'Coluna {str(i+1)}: '+'["+custom['Coluna'].loc[i]+"] "+exemplos+\
"', options=['Texto', 'Numérico(int)', 'Numérico(dec)', 'Datetime'], "+\
"index="+str(opt[custom['Custom'].loc[i]])+")"
exec(s, globals())
submitted = st.form_submit_button("Processar alterações")
if not submitted:
st.stop()
else:
for i in range(0, custom.shape[0]):
s = "custom.at[i,'Custom'] = custom"+str(i)
exec(s)
log_write("### <b>Analisando e convertendo colunas</b>", newline=True)
changes = custom[custom.Tipo != custom.Custom].shape[0]
log_write(f'{changes} coluna(s) alterada(s)')
opt_rename = dict()
opt_rename['Texto'] = 'Tx'
opt_rename['Numérico(int)'] = 'Ni'
opt_rename['Numérico(dec)'] = 'Nd'
opt_rename['Datetime'] = 'Dt'
for index, row in custom.iterrows():
custom.at[index,'Rename'] = opt_rename[row['Tipo']]+'>'+opt_rename[row['Custom']]
for index, row in custom.iterrows():
if row['Custom'] == 'Texto':
custom.at[index, 'CustomType'] = 'object'
elif row['Custom'] == 'Numérico(int)':
custom.at[index, 'CustomType'] = 'int64'
elif row['Custom'] == 'Numérico(dec)':
custom.at[index, 'CustomType'] = 'float64'
elif row['Custom'] == 'Datetime':
custom.at[index, 'CustomType'] = 'datetime64[ns]'
for index, row in custom.iterrows():
if not row['Type'] == row['CustomType']:
log_write(f"Convertendo [{row['Coluna']}] de [{row['Tipo']}] para [{row['Custom']}]",
addcont=True,newline=False)
if row['CustomType'] == 'object':
df[row['Coluna']] = df[row['Coluna']].astype(row['CustomType'])
if 'float' in str(row['Type']).lower():
df[row['Coluna']] = df[row['Coluna']].apply(lambda x: str(x)[:-2] if str(x)[-2:] == '.0' else x)
log_write(f"[{row['Coluna']}] renomeada para {row['Coluna']+'('+row['Rename']+')'}.",
addcont=False,newline=False)
df.rename({row['Coluna']:row['Coluna']+'('+row['Rename']+')'}, axis=1, inplace=True)
elif row['CustomType'] == 'float64':
NaN_inicial = df[row['Coluna']].isnull().sum()
df[row['Coluna']] = pd.to_numeric(df[row['Coluna']], errors='coerce')
NaN_final = df[row['Coluna']].isnull().sum()
if df[row['Coluna']].dtype == row['CustomType']:
if NaN_inicial == NaN_final:
log_write(f"100% convertido sem erros",
addcont=False,newline=False)
else:
log_write(f"{NaN_final - NaN_inicial:,} erros na conversão (assumidos como missing).",
addcont=False,newline=False)
log_write(f"[{row['Coluna']}] renomeada para {row['Coluna']+'('+row['Rename']+')'}.",
addcont=False,newline=False)
df.rename({row['Coluna']:row['Coluna']+'('+row['Rename']+')'}, axis=1, inplace=True)
else:
log_write(f"*ERRO* convertendo [{row['Coluna']}] de "+\
f"[{row['Tipo']}] para [{row['Custom']}] ({df[row['Coluna']].dtype})",
addcont=False,newline=False)
elif row['CustomType'] == 'int64': ####### INTEIRO
NaN_inicial = df[row['Coluna']].isnull().sum()
df[row['Coluna']] = pd.to_numeric(df[row['Coluna']], errors='coerce')
df[row['Coluna']] = df[row['Coluna']].apply(lambda x: x if pd.isna(x) else int(x))
df[row['Coluna']] = df[row['Coluna']].astype(float).astype('Int64')
NaN_final = df[row['Coluna']].isnull().sum()
if df[row['Coluna']].dtype == 'Int64':
if NaN_inicial == NaN_final:
log_write(f"100% convertido sem erros",
addcont=False,newline=False)
else:
log_write(f"{NaN_final - NaN_inicial:,} erros na conversão (assumidos como missing).",
addcont=False,newline=False)
log_write(f"[{row['Coluna']}] renomeada para {row['Coluna']+'('+row['Rename']+')'}.",
addcont=False,newline=False)
df.rename({row['Coluna']:row['Coluna']+'('+row['Rename']+')'}, axis=1, inplace=True)
else:
log_write(f"*ERRO* convertendo [{row['Coluna']}] de "+\
f"[{row['Tipo']}] para [{row['Custom']}] ({df[row['Coluna']].dtype})",
addcont=False,newline=False)
elif row['CustomType'] == 'datetime64[ns]': ####### DATETIME
global sampleD
try:
sampleD = 50
dfc = df.sample(sampleD)
except:
sampleD = df.shape[0]
dfc = df.sample(sampleD)
best,taxa = best_format(dfc,row['Coluna'])
log_write(f'melhor formato "{best}" com {str(100-taxa)}% de erro de conversão', addcont=False,newline=False)
NaN_inicial = df[row['Coluna']].isnull().sum()
df[row['Coluna']] = pd.to_datetime(df[row['Coluna']],format=best, errors='coerce')
df[row['Coluna']] = df[row['Coluna']].apply(lambda x: None if pd.isna(x) else x)
NaN_final = df[row['Coluna']].isnull().sum()
if df[row['Coluna']].dtype == 'datetime64[ns]':
if NaN_inicial == NaN_final:
log_write(f"100% convertido sem erros",
addcont=False,newline=False)
else:
log_write(f"{NaN_final - NaN_inicial:,} erros na conversão (assumidos como missing).",
addcont=False,newline=False)
log_write(f"[{row['Coluna']}] renomeada para {row['Coluna']+'('+row['Rename']+')'}.",
addcont=False,newline=False)
df.rename({row['Coluna']:row['Coluna']+'('+row['Rename']+')'}, axis=1, inplace=True)
else:
log_write(f"*ERRO* convertendo [{row['Coluna']}] de "+\
f"[{row['Tipo']}] para [{row['Custom']}] ({df[row['Coluna']].dtype})",
addcont=False,newline=False)
else:
log_write('Tipo não identificado'+row['Coluna'],
addcont=True,newline=False)
return(df)
def validate_format(f, s):
lenF = len(f) if 'y' in f else len(f)+2
try:
tmp = datetime.strptime(str(s)[:lenF], f).date()
except Exception as e:
return(0)
sep = '-' if '-' in f else '/' if '/' in f else ''
anoL = '2' if 'y' in f else '4'
ordem = f.replace(sep,'').replace('%','')
import re
d1,d2,d3 = [c for c in ordem.replace('m','2').replace('d','2').replace('y',anoL).replace('Y',anoL)]
string = r'(\d{'+d1+'})'+sep+'(\d{'+d2+'})'+sep+'(\d{'+d3+'})'
try:
result = re.findall(string, str(s))
if len(result) == 0:
valid = 0
else:
ano = int(result[0][ordem.find('y')])
mes = int(result[0][ordem.find('m')])
dia = int(result[0][ordem.find('d')])
valid = 1 if (mes <=12 and dia <=31) else 0
except:
valid = False
return(valid)
def best_format(dfc,c):
formats = [ '%Y-%m-%d', '%Y%m%d' , '%m/%d/%Y', '%m-%d-%Y',
'%d/%m/%Y', '%d-%m-%Y', '%y-%m-%d', '%y%m%d' ,
'%m/%d/%y', '%m-%d-%y', '%d/%m/%y', '%d-%m-%y']
if 'int' in str(dfc[c].dtype).lower():
dfc[c] = dfc[c].astype(object)
elif 'float' in str(df[c].dtype).lower():
dfc[c] = dfc[c].astype(object)
dfc[c] = dfc[c].apply(lambda x: str(x).replace('.0',''))
best = ''; bestN = 0
d = dfc[~dfc[c].isnull()][c].astype(object).rename('c').to_frame()
tot = d.shape[0]
for form in formats:
check = d['c'].apply(lambda s: validate_format(form, s) ).sum()
sample = d['c'].unique()[:5]
if check > bestN:
best = form; bestN = check
taxa = int(round(check/tot*100,0))
# print(f'teste de conversão datetime: sample={sampleD} col={c}, form={form}, erros={str(100-taxa)}%')
return(best,int(round(bestN/tot*100,0)))
def analysis_df(df,file):
if True: #try:
## MORFOLOGIA
reg_total = df.shape[0]
log_write("### <b>Análise de Morfologia</b>", newline=True)
log_write(f'{reg_total:,} registros e {df.shape[1]} colunas')
xext = '' ; xqte = 0
for x in df.columns:
if 'object' in str(df[x].dtype).lower():
xext = xext + sep(xqte) +x ; xqte += 1
if xqte == 1:
log_write(f'1 coluna de texto: [{xext}]' )
elif xqte > 1:
log_write(f'{xqte} colunas de texto: [{xext}]' )
xqte_corr = 0
xext = '' ; xqte = 0
for x in df.columns:
if 'int' in str(df[x].dtype).lower():
xext = xext + sep(xqte) +x ; xqte += 1
if xqte == 1:
log_write(f'1 coluna numérica (inteiro): [{xext}]' )
elif xqte > 1:
log_write(f'{xqte} colunas numéricas (inteiro): [{xext}]' )
xqte_corr += xqte
xext = '' ; xqte = 0
for x in df.columns:
if 'float' in str(df[x].dtype).lower():
xext = xext + sep(xqte) +x ; xqte += 1
if xqte == 1:
log_write(f'1 coluna numérica (decimal): [{xext}]' )
elif xqte > 1:
log_write(f'{xqte} colunas numéricas (decimal): [{xext}]' )
xqte_corr += xqte
xext = '' ; xqte = 0
for x in df.columns:
if 'datetime' in str(df[x].dtype).lower():
xext = xext + sep(xqte) +x ; xqte += 1
if xqte == 1:
log_write(f'1 coluna datetime: [{xext}]' )
elif xqte > 1:
log_write(f'{xqte} colunas datetime: [{xext}]' )
xqte_corr += xqte
xext = '' ; xqte = 0
for x in df.columns:
if not 'object' in str(df[x].dtype).lower() and\
not 'datetime' in str(df[x].dtype).lower() and\
not 'int' in str(df[x].dtype).lower() and\
not 'float' in str(df[x].dtype).lower():
xext = xext + sep(xqte) +x ; xqte += 1
if xqte == 1:
log_write(f'1 coluna de outro tipos: [ {xext} ]' )
elif xqte > 1:
log_write(f'{xqte} colunas de outros tipos: [ {xext} ]' )
dtp = []
for c in df.columns:
dtp.append(str(df[c].dtype))
dtp = ';'.join(set(dtp))
log_write(f'dtypes detalhados: {dtp}.' )
## CAMPOS TEXTO
log_write("### Análise das colunas tipo <b>TEXTO</b>", newline=True)
for x in df.columns:
if df[x].dtype == object:
xext = xext + sep(xqte) +x ; xqte += 1
log_write(str(xqte)+") "+ x + " ["+x.upper()+"]",newline=True)
ctmp = df[x]
ctmp_counts = ctmp.value_counts()
ctmp_total = reg_total
nulos = ctmp.isna().sum()
ctmp = ctmp.dropna().reset_index(drop=True) #CHECK OTHERS
validos = ctmp_total-nulos
nodups = ctmp.drop_duplicates(keep='first')
ctmp_final = nodups.shape[0]
dups = ctmp_total-nulos-ctmp_final
log_write(f"{'registros: ':<15}{ctmp_total :>10,}", addcont=False,newline=True)
log_write(f"{'missing: ':<15}{nulos :>10,}", addcont=False)
log_write(f"{'válidos: ':<15}{validos :>10,}", addcont=False)
log_write(f"{'duplicados: ':<15}{dups :>10,}", addcont=False)
log_write(f"{'categorias: ':<15}{ctmp_final :>10,}", addcont=False)
if (ctmp_total-ctmp_final) == 0:
log_write(f"categorias = registros, zero duplicados", addcont=False,newline=True)
else:
log_write("[ f.abs] [f.rel%] [f.acc%] categorias (max="+'{:n})'.format(max_freq), addcont=False,newline=True)
freq = 0
freq_acc = 0
for key, value in ctmp_counts.items():
key2show = str(key) if len(str(key))<= 35 else str(key)[:35]+'\\'
if freq <= max_freq:
freq += 1
freq_acc = freq_acc + (value/ctmp_total)
log_write(
"[" +'{:>12,.0f}'.format(value)+
"] [" +'{:>5,.1f}'.format(value/ctmp_total*100)+
"%] [" +'{:>5,.1f}'.format(freq_acc*100) +"%] "
+key2show, addcont=False)
base_dup = pd.DataFrame(
df[~df[x].isnull()][x], columns=[x])
sampleN1 = sampleN if base_dup.shape[0] > sampleN else base_dup.shape[0]
ctmpS = base_dup.sample(sampleN1)
log_write(f"Amostra aleatória dos dados (max={sampleN1:,}):", addcont=False, newline=True)
log_write(f"[{']['.join([str(c) for c in ctmpS[x].unique()])}]", addcont=False)
ctmpD = pd.DataFrame(df[~df[x].isnull()][x], columns=[x])
if not dups == 0:
dupsTMP = pd.concat(g for _, g in ctmpD.groupby(x) if len(g) > 1)
dupsTMP = dupsTMP.groupby(x).size().reset_index()
dupsTMP.sort_values(by=[0], ascending=False, inplace = True)
dupsTMP.reset_index(drop=True)
cont = 0
dupsTX = '['
for index, row in dupsTMP.iterrows():
dupsTX += f'{str(row[x]).strip()}({row[0]:,})]['
cont += 1
if cont >= max_dups: break
dupsTX = dupsTX.strip()[:-1]
log_write(f"Duplicações ({cont:,} exemplos):", addcont=False, newline=True)
log_write(f"{dupsTX}", addcont=False)
## CAMPOS NUMÉRICOS (INTEIROS e DECIMAIS)
log_write("### Análise das colunas <b>NUMÉRICAS</b> (INTEIROS E DECIMAIS)",newline=True)
for x in df.columns:
#if df[x].dtype == np.int64 or df[x].dtype == np.float64:
if 'int' in str(df[x].dtype) or\
'float' in str(df[x].dtype):
xqte += 1
tipo = '[INTEIRO]' if df[x].dtype == int else '[DECIMAL]'
log_write(str(xqte)+") "+ x + " ["+x.upper()+f"] {tipo}",newline=True)
if df[x].sum() == 0:
log_write(" <b>Todos os valores zerados</b>")
else:
ctmp = df[x]
nulos = ctmp.isna().sum()
ctmpZ = ctmp[ctmp != 0]
ctmpZ = ctmpZ.dropna()
ctmpZEXC = ctmp[ctmp == 0]
zerados = ctmpZEXC.shape[0]
z = True if zerados == 0 else False
log_write(f"{'registros:':<18}{reg_total :>10,}", addcont=False,newline=True)
log_write(f"{'missing:' :<18}{nulos :>10,}", addcont=False)
log_write(f"{'válidos:' :<18}{reg_total-nulos :>10,}", addcont=False)
log_write(f"{'zerados:' :<18}{zerados :>10,}", addcont=False)
txt = f'{"[válidos]":>35}{" [válidos exc. zero]" if not z else "":>20}'.rstrip()
log_write(txt, addcont=False,newline=True)
txt = 'registros:'.ljust(18)
txt += ' {:>13,} '.format(reg_total-nulos)
txt += ' {:>17,} '.format(ctmpZ.shape[0]) if not z else ''
log_write(txt, addcont=False)
txt = 'soma:'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.sum() ,2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.sum(),2)) if not z else ''
log_write(txt, addcont=False)
txt = 'média:'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.describe()[1],2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.describe()[1],2)) if not z else ''
log_write(txt, addcont=False)
txt = 'desvio:'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.describe()[2],2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.describe()[2],2)) if not z else ''
log_write(txt, addcont=False)
txt = 'médiana (Q2):'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.median(),2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.median(),2)) if not z else ''
log_write(txt, addcont=False)
txt = 'mínimo:'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.describe()[3],2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.describe()[3],2)) if not z else ''
log_write(txt, addcont=False)
txt = 'máximo:'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.describe()[7],2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.describe()[7],2)) if not z else ''
log_write(txt, addcont=False)
txt = 'amplitude:'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.describe()[7]-ctmp.describe()[3],2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.describe()[7]-ctmpZ.describe()[3],2)) if not z else ''
log_write(txt, addcont=False)
txt = '25% (Q1):'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.describe()[4],2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.describe()[4],2)) if not z else ''
log_write(txt, addcont=False)
txt = '50% (Q2):'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.describe()[5],2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.describe()[5],2)) if not z else ''
log_write(txt, addcont=False)
txt = '75% (Q3):'.ljust(18)
txt += ' {:>16,.2f}'.format(round(ctmp.describe()[6],2))
txt += ' {:>20,.2f}'.format(round(ctmpZ.describe()[6],2)) if not z else ''
log_write(txt, addcont=False)
############################### QTE RECORDS
txt = 'registros<Q1:'.ljust(18)
txt += ' {:>13,}'.format((ctmp < ctmp.describe()[4]).sum())
txt += ' {:>20,}'.format((ctmp < ctmpZ.describe()[4]).sum()) if not z else ''
log_write(txt, addcont=False)
txt = 'registros<Q2:'.ljust(18)
txt += ' {:>13,}'.format((ctmp < ctmp.describe()[5]).sum())
txt += ' {:>20,}'.format((ctmp < ctmpZ.describe()[5]).sum()) if not z else ''
log_write(txt, addcont=False)
txt = 'registros>Q2:'.ljust(18)
txt += ' {:>13,}'.format((ctmp > ctmp.describe()[5]).sum())
txt += ' {:>20,}'.format((ctmp > ctmpZ.describe()[5]).sum()) if not z else ''
log_write(txt, addcont=False)
txt = 'registros>Q3:'.ljust(18)
txt += ' {:>13,}'.format((ctmp > ctmp.describe()[6]).sum())
txt += ' {:>20,}'.format((ctmp > ctmpZ.describe()[6]).sum()) if not z else ''
log_write(txt, addcont=False)
base_dup = pd.DataFrame(
df[~df[x].isnull()][x], columns=[x])
sampleN1 = sampleN if base_dup.shape[0] > sampleN else base_dup.shape[0]
ctmpS = base_dup.sample(sampleN1)
ctmpS = pd.DataFrame(df[~df[x].isnull()][x], columns=[x]).sample(sampleN1)
log_write(f"Amostra aleatória dos dados (max={sampleN1:,}):", addcont=False, newline=True)
log_write(f"[{']['.join([f'{c:,}' for c in ctmpS[x].unique()])}]", addcont=False)
if hist:
grf = df[x].dropna()
grfINFO = grf.describe()
max_height = (np.histogram(grf, bins=hist_bins)[0].max())
max_lenght = (grfINFO[7]- grfINFO[3])
sns.set_style("whitegrid")
plt.figure(figsize=(11.7, 8.27))
plt.xlim(grfINFO[3].round(0), grfINFO[7].round(0))
plt.ylim(0, max_height*1.1)
plt.text(0.03*max_lenght,-0.09*max_height,
FC_Auto_Analyser_Version+'\n'+'https://www.fabianocastello.com.br/fca2',
fontsize=10, ha='left')
sns_plot = sns.distplot(grf, bins=hist_bins, kde=False, color="purple",
axlabel=False, rug=True)
sns_plot = sns_plot.set_title("histograma de ["+grf.name+"]", {'size': '18'})
img_file = file+'_HIST_'+grf.name+'.png'
fig = sns_plot.get_figure().savefig(dataout+"/"+img_file) #pdf: trocar sufixo
plt.close()
log_write('', addcont=False,newline=True)
image = Image.open(dataout+"/"+img_file)
st.image(image, caption=img_file)
if boxp:
grf = df[x].dropna()
grfINFO = grf.describe()
max_height = (np.histogram(grf, bins=hist_bins)[0].max())
max_lenght = (grfINFO[7]- grfINFO[3])
sns.set_style("whitegrid")
plt.figure(figsize=(11.7, 8.27))
plt.xlim(grfINFO[3].round(0), grfINFO[7].round(0))
plt.ylim(0, max_height*1.1)
plt.text(0.03*max_lenght,-0.09*max_height,
FC_Auto_Analyser_Version+'\n'+'https://www.fabianocastello.com.br/fca2',
fontsize=10, ha='left')
sns_plot = sns.boxplot(x=grf)
sns_plot = sns_plot.set_title("Boxplot de ["+grf.name+"]", {'size': '18'})
img_file = file+'_BoxPlot_'+grf.name+'.png'
fig = sns_plot.get_figure().savefig(dataout+"/"+img_file)
plt.close()
log_write('', addcont=False,newline=True)
image = Image.open(dataout+"/"+img_file)
st.image(image, caption=img_file)
## CAMPOS DATETIME #XPTO
log_write("### Análise das colunas tipo <b>DATA</b> (em beta!)", newline=True)
for x in df.columns:
if str(df[x].dtype) == 'datetime64[ns]':
xext = xext + sep(xqte) +x ; xqte += 1
log_write(str(xqte)+") "+ x + " ["+x.upper()+"]",newline=True)
df[x] = df[x].apply(lambda x: x.date())
ctmp = df[x]
ctmp_counts = ctmp.value_counts()
ctmp_total = reg_total
nulos = ctmp.isna().sum()
ctmp = ctmp.dropna().reset_index(drop=True) #CHECK OTHERS
validos = ctmp_total-nulos
ctmp_final = ctmp.shape[0]
nodups = ctmp.drop_duplicates(keep='first')
ctmp_final = nodups.shape[0]
dups = ctmp_total-nulos-ctmp_final
log_write(f"{'registros: ':<15}{ctmp_total :>10,}", addcont=False,newline=True)
log_write(f"{'missing: ':<15}{nulos :>10,}", addcont=False)
log_write(f"{'válidos: ':<15}{validos :>10,}", addcont=False)
log_write(f"{'duplicados: ':<15}{dups :>10,}", addcont=False)
log_write(f"{'categorias: ':<15}{ctmp_final :>10,}", addcont=False)
if (ctmp_total-ctmp_final) == 0:
log_write(f"categorias = registros, zero duplicados", addcont=False,newline=True)
else:
log_write("[ f.abs] [f.rel%] [f.acc%] categorias (max="+'{:n})'.format(max_freq), addcont=False,newline=True)
freq = 0
freq_acc = 0
for key, value in ctmp_counts.items():
key = key.strftime('%Y-%m-%d')
key2show = str(key) if len(str(key))<= 35 else str(key)[:35]+'\\'
if freq <= max_freq:
freq += 1
freq_acc = freq_acc + (value/ctmp_total)
log_write(
"[" +'{:>12,.0f}'.format(value)+
"] [" +'{:>5,.1f}'.format(value/ctmp_total*100)+
"%] [" +'{:>5,.1f}'.format(freq_acc*100) +"%] "
+key2show, addcont=False)
min_ = ctmp.min()
max_ = ctmp.max()
cal = pd.DataFrame({'c': pd.date_range(min_, max_)})\
['c'].apply(lambda x: x.date()).to_list()
ampl = len(cal)
mean = min_+timedelta(days=int(ampl))/2
filling = len(list(set(cal) & set(ctmp)))
fillingP = 0 if ampl==0 else int(round(filling/ampl*100,0))
quartis = list(ctmp.astype('datetime64[ns]').quantile([.25, .5, .75]))
Q1,Q2,Q3 = quartis[0].date(),quartis[1].date(),quartis[2].date()
#filling meses
calM = pd.DataFrame({'c': pd.period_range(min_, max_, freq="M")})
calM['Year'] = calM['c'].apply(lambda x: str(x.year))
calM['Period'] = calM['c'].apply(lambda x: f'{x.year}-{str(x.month).zfill(2)}')
cal_meses = sorted(set(calM['Period'].to_list()))
ctmp_meses = sorted(set([f'{x.year}-{str(x.month).zfill(2)}' for x in ctmp]))
amplM = len(cal_meses)
fillingM = len(set(cal_meses) & set(ctmp_meses))
fillingMP = 0 if ampl==0 else int(round(fillingM/amplM*100,0))