-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstreamlit_app.py
1342 lines (1062 loc) · 51.3 KB
/
streamlit_app.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
"""
@file streamlit_app.py
A web app for deploying sex estimation machine learning
models
Language: python
Chrysovalantis Constantinou
The Cyprus Institute
+ 10/28/21 (cc): Created.
+ 11/16/21 (cc): Basic functional version completed
+ 11/23/21 (cc): Added a configuration file to beautify the app
+ 12/18/21 (cc): Added missing data tabs and functionality
+ 03/29/22 (cc): Added Google Analytics functionality
+ 05/30/22 (cc): Added 3 independent variables to the postcranial models
"""
from datetime import time
import numpy as np
import pickle
import pandas as pd
import streamlit as st
import streamlit.components.v1 as components
import toml
import os
import pathlib
from bs4 import BeautifulSoup
import logging
import shutil
# Web app wide configuration
st.set_page_config(
page_title="SexEst",
page_icon="skull",
layout="wide"
# menu_items =
# {
# 'Get Help': 'https://www.extremelycoolapp.com/help',
# 'Report a bug': "https://www.extremelycoolapp.com/bug",
# 'About': "# This is a header. This is an *extremely* cool app!"
# }
)
# Fix the button's sizes by passing CSS rules to the web app
primaryColor = toml.load(".streamlit/config.toml")["theme"]["primaryColor"]
# button_configuration = f"""
# <style>
# div.stButton > button:first-child {{ border: 1px solid {primaryColor}; border-radius: 4px 4px 4px 4px; width: 80px;}}
# <style>
# """
def inject_ga():
GA_ID = "google_analytics"
# Note: Please replace the id from G-XXXXXXXXXX to whatever your
# web application's id is. You will find this in your Google Analytics account
GA_JS = """
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
"""
# Insert the script in the head tag of the static template inside your virtual
index_path = pathlib.Path(st.__file__).parent / "static" / "index.html"
logging.info(f'editing {index_path}')
soup = BeautifulSoup(index_path.read_text(), features="html.parser")
if not soup.find(id=GA_ID): # if cannot find tag
bck_index = index_path.with_suffix('.bck')
if bck_index.exists():
shutil.copy(bck_index, index_path) # recover from backup
else:
shutil.copy(index_path, bck_index) # keep a backup
html = str(soup)
new_html = html.replace('<head>', '<head>\n' + GA_JS)
index_path.write_text(new_html)
#inject_ga()
button_configuration = f"""
<style>
div.stButton > button:first-child
{{
justify-content: justify;
font-size:13px;
width: 298px;
background: transparent;
height:45.5px;
box-sizing: border-box;
width: 100%;
}}
<style>
"""
st.markdown(button_configuration, unsafe_allow_html=True)
expander_configuration = f"""
<style>
.streamlit-expanderHeader
{{
width: 300px;
justify-content: justify;
font-size:13px;
}}
<style>
"""
st.markdown(expander_configuration, unsafe_allow_html=True)
# paragraph_configuration = f"""
# <style>
# div.stMarkdownContainer {{ width: 10px;}}
# <style>
# """
# st.markdown(paragraph_configuration, unsafe_allow_html=True)
download_button_configuration = f"""
<style>
div.stDownloadButton > button:first-child
{{
border: 1px solid {primaryColor};
border-radius: 4px 4px 4px 4px;
width: 180px;
margin: 20px
}}
<style>
"""
st.markdown(download_button_configuration, unsafe_allow_html=True)
browse_button_configuration = f"""
<style>
div.stUploadButton > button:first-child
{{
border: 1px solid {primaryColor};
border-radius: 4px 4px 4px 4px;
width: 180px;
}}
<style>
"""
st.markdown(browse_button_configuration, unsafe_allow_html=True)
# Loading the Goldman models and their accuracy
xgb_model_goldman = pickle.load(open("./models_goldman/model_xgb_goldman.dat", "rb"))
accuracy_file_xgb_model_goldman = open("./models_goldman/model_xgb_goldman.txt", "r")
accuracy_xgb_model_goldman = float(accuracy_file_xgb_model_goldman.read()) * 100
lgb_model_goldman = pickle.load(open("./models_goldman/model_lgb_goldman.dat", "rb"))
accuracy_file_lgb_model_goldman = open("./models_goldman/model_lgb_goldman.txt", "r")
accuracy_lgb_model_goldman = float(accuracy_file_lgb_model_goldman.read()) * 100
lda_model_goldman = pickle.load(open("./models_goldman/model_lda_goldman.dat", "rb"))
accuracy_file_lda_model_goldman = open("./models_goldman/model_lda_goldman.txt", "r")
accuracy_lda_model_goldman = float(accuracy_file_lda_model_goldman.read()) * 100
# Goldman independent variables needed for the DataFrames
columns_goldman = ["BIB", "HML", "HHD", "RML", "FML", "FBL", "FHD", "TML", "FEB", "TPB", "HEB"]
# Loading the Howells models and their accuracy
xgb_model_howell = pickle.load(open("./models_howell/model_xgb_howell.dat", "rb"))
accuracy_file_xgb_model_howell = open("./models_howell/model_xgb_howell.txt", "r")
accuracy_xgb_model_howell = float(accuracy_file_xgb_model_howell.read()) * 100
lgb_model_howell = pickle.load(open("./models_howell/model_lgb_howell.dat", "rb"))
accuracy_file_lgb_model_howell = open("./models_howell/model_lgb_howell.txt", "r")
accuracy_lgb_model_howell = float(accuracy_file_lgb_model_howell.read()) * 100
lda_model_howell = pickle.load(open("./models_howell/model_lda_howell.dat", "rb"))
accuracy_file_lda_model_howell = open("./models_howell/model_lda_howell.txt", "r")
accuracy_lda_model_howell = float(accuracy_file_lda_model_howell.read()) * 100
# logreg_model_howell = pickle.load(open("logreg_model_howell.dat", "rb"))
# svm_model_howell = pickle.load(open("svm_model_howell.dat", "rb"))
# lda_model_howell = pickle.load(open("lda_model_howell.dat", "rb"))
# Howell independent variables needed for the DataFrames
columns_howell = [
"GOL",
"NOL",
"BNL",
"BBH",
"XCB",
"XFB",
"ZYB",
"AUB",
"WCB",
"ASB",
"BPL",
"NPH",
"NLH",
"JUB",
"NLB",
"MAB",
"MDH",
"MDB",
"OBH",
"OBB",
"DKB",
"ZMB",
"FMB",
"EKB",
"IML",
"XML",
"WMH",
"STB",
"FRC",
"PAC",
"OCC",
"FOL",
]
title = """
<h1 style = "padding-top:0px; padding-bottom:0px; text-align: center; margin: 10px;" >
SexEst: A sex estimation web-application (beta)
</h1>
"""
st.markdown(title, unsafe_allow_html=True)
# st.title("SexEst: A sex estimation web application")
welcome_text = """
<p>
<style>
p {text-align: justify; margin: 10px}
</style>
<hr style = "height:5px; border:none; color:#333; background-color:#333; margin: 10px;" />
Welcome to SexEst, a free, interactive, web application designed
to estimate sex using cranial or postcranial linear measurements.
Users can either enter manually the measurements for single skeletons
or upload data for multiple skeletons stored in a CSV file.
Sex estimation is based on three different machine learning
classification algorithms:
[Linear Discriminant Analysis](https://scikit-learn.org/stable/modules/generated/sklearn.discriminant_analysis.LinearDiscriminantAnalysis.html) (LDA),
[Extreme Gradient Boosting](https://xgboost.readthedocs.io/en/stable/) (XGB) and
[Light Gradient Boosting](https://lightgbm.readthedocs.io/en/latest/) (LGB).
The training [datasets](http://volweb.utk.edu/~auerbach/DATA.htm)
used in these machine learning classifiers are
the William W. Howells craniometric dataset (Howells 1973, 1989, 1995)
for cranial measurements and the Goldman dataset (Auerbach and Ruff 2004, 2006)
for postcranial measurements. Both datasets include thousands of
individuals from various geographic locations dating throughout the
Holocene, hence they represent several broad geographic ancestral
backgrounds and account for inter-population variability in sexual
dimorphism. SexEst can generate a prediction even when a single variable
is given; hence, it is applicable even on highly fragmented remains or
remains where not all measurements can be accurately obtained due to
pathological or other alterations.
Instructions on how to use SexEst can be found by pressing the
**How to** button, while the contact details of the creators of the
application appear when pressing the **Contact** button.
SexEst was supported by the NI4OS-Europe project, funded by the
European Commission under the Horizon 2020 European Research
Infrastructures grant agreement no. 857645. EN's contribution was
co-funded by the European Regional Development Fund and the
Republic of Cyprus through the Research and Innovation Foundation
[Project: EXCELLENCE/1216/0023]. This project has also received
funding from the European Union's Horizon 2020 Research and
Innovation Program under the grant agreement no. 811068.
**Disclaimer:** This application is freely provided as an aid for
skeletal sex estimation. The authors hold no responsibility for
its ultimate use or misuse. As creators we try to ensure that the
software is theoretically grounded and statistically accurate, we
provide no warranty and make no specific claims as to its
performance or its appropriateness for use in any
particular situation.
**References**
Auerbach BM, Ruff CB. 2004. Human body mass estimation: A comparison of “morphometric” and “mechanical” methods. American Journal of Physical Anthropology 125: 331-342.
DOI: [10.1002/ajpa.20032](https://doi.org/10.1002/ajpa.20032)
Auerbach BM, Ruff CB. 2006. Limb bone bilateral asymmetry: variability and commonality among modern humans. Journal of
Human Evolution 50: 203-218.
DOI: [10.1016/j.jhevol.2005.09.004](https://doi.org/10.1016/j.jhevol.2005.09.004)
Howells WW. 1973. Cranial Variation in Man: A Study by Multivariate Analysis of Patterns of Difference among Recent Human Populations. Papers of the Peabody Museum of Archaeology and Ethnology, vol. 67: Harvard University, Cambridge, Mass.
Howells WW. 1989. Skull Shapes and the Map: Craniometric Analysis in the Dispersion of Modern Homo. Papers of the Peabody Museum of Archaeology and Ethnology, vol. 79: Harvard University, Cambridge, Mass.
Howells WW. 1995. Who's Who in Skulls. Ethnic Identification of Crania from Measurements. Papers of the Peabody Museum of Archaeology and Ethnology, vol. 82: Harvard University, Cambridge, Mass.
<hr style = "height:5px; border:none; color: #333; background-color:#333; margin: 10px;" />
</p>
"""
# html_temp = """
# <div style="background-color:tomato;padding:8px">
# <h3 style="color:white;text-align:center;"> Results section </h3>
# </div>
# """
# st.markdown(html_temp, unsafe_allow_html=True)
padding = 0.98
st.markdown(
f""" <style>
.reportview-container .main .block-container{{
padding-top: {padding}rem;
padding-right: {padding}rem;
padding-left: {padding}rem;
padding-bottom: {padding}rem;
}} </style> """,
unsafe_allow_html=True,
)
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {
position: relative;
left: -374px;
bottom: 0px;
text-align: center;
}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
placeholder_write_welcome = st.empty()
placeholder_write_input = st.empty()
placeholder_write_result = st.empty()
placeholder_write_warning = st.empty()
placeholder_write_welcome.markdown(welcome_text, unsafe_allow_html=True)
# h1 {text-align: center; font-family:"Calibri, sans-serif";}
contact_section = """
<p>
<style>
p {text-align: justify; margin: 10px; width: 744px}
</style>
<hr style = "height:5px; border:none; color:#333; background-color:#333; margin: 10px;" />
[Efthymia Nikita](https://cyi.academia.edu/EfthymiaNikita),
Assistant Professor in Bioarchaeology, Science and Technology in Archaeology
and Culture Research Center (STARC), The Cyprus Institute (<a href="mailto:[email protected]">[email protected]</a>)
Chrysovalantis Constantinou, Computational Scientist, Computation-based Science and Technology Research Center (CaSToRC), The Cyprus Institute (<a href="mailto:[email protected]">[email protected]</a>)
<hr style = "height:5px; border:none; color:#333; background-color:#333; margin: 10px;" />
</p>
"""
how_to_section = """
<p>
<style>
p {text-align: justify; margin: 10px}
</style>
<hr style = "height:5px; border:none; color:#333; background-color:#333; margin: 10px;" />
SexEst has three modes of operation: a single skeleton mode, a
multiple skeleton mode, and a missing data mode. For each mode
of operation, the application accepts either postcranial or cranial measurements.
Selecting one of the two single skeleton modes
(**Osteometric Prediction (single skeleton)** or **Craniometric Prediction (single skeleton)**)
reveals input boxes where the user can manually enter the postcranial or cranial
measurements. All measurements must be in millimeters (mm). The plus and minus
buttons allow the user to increase or decrease an input measurement by 0.5 mm.
Once the measurements are input, the machine learning model can be selected
among three available options: Extreme Gradient Boosting (XGB), Light Gradient
Boosting (LGB), and Linear Discriminant Analysis (LDA). Upon pressing the
Calculate button, the sex of the individual is estimated along with the
probability of being male or female; the accuracy of the model on which the
prediction was based is also given. Note that these modes require all
measurements to be present in order for the models to run; that is, they
cannot handle missing data. In cases of missing data, SexEst provides
alternative modes (see details below).
Selecting one of the multiple skeleton modes from the sidebar
(**Osteometric Prediction (multiple skeletons)** or **Craniometric Prediction (multiple skeletons)**)
allows users to upload a CSV file containing measurements from multiple
skeletons so that sex is predicted for all of them simultaneously.
The file format must follow the example files given at the bottom of this page.
The file header must contain the names of the 11 postcranial variables or the
32 cranial variables specified in the example files. All measurements must be in
millimeters (mm). The CSV file can be uploaded using the drag and
drop feature or by
browsing the computer's file system. Subsequently, the user
must select a model (XGB, LGB, LDA), and press the Calculate button.
The application will output the predicted result as a table divided into a
Male and a Female column containing the probability that each skeleton in the
file belongs to a male or a female individual. Note that any rows containing
missing data will be dropped as the models are optimized to use all 11 variables
for osteometric and all 32 variables for craniometric datasets, respectively.
In cases of missing data, SexEst provides alternative modes (see details below).
The missing data modes (**Osteometric Prediction (missing data)** or **Craniometric
Prediction (missing data)**) offer the possibility to make predictions when
one or more variables are missing. These tabs are similar to the single
skeleton modes; however, the user can now enter even a single variable and
get a sex prediction. For postcranial data, the user can enter any combination
of one, two, etc. out of the 11 measurements, while for the craniometric data we
have selected 10 out of the original 32 variables, which are most commonly
employed in other studies of population-specific metric sex estimation, namely
**GOL, BNL, BBH, XCB, ZYB, BPL, NLH, NLB, MDH,** and **FOL**. All entered measurements
must be in millimeters (mm). As these modes are more computationally intensive,
they have been trained using only Linear Discriminant Analysis.
If you encounter any problems, please contact the creators of the
application using the email addresses provided in the **Contact** tab.
<hr style = "height:5px; border:none; color:#333; background-color:#333; margin: 10px;" />
</p>
"""
if st.sidebar.button("How to", key="how_to_button"):
with st.container():
placeholder_write_welcome.empty()
placeholder_write_result.empty()
placeholder_write_warning.empty()
placeholder_write_input.markdown(how_to_section, unsafe_allow_html=True)
osteometric_sample_dataframe = pd.read_csv(
"sample_dataset_osteometric.csv", header=0, encoding="unicode_escape"
)
osteometric_csv = osteometric_sample_dataframe.to_csv(index=False)
craniometric_sample_dataframe = pd.read_csv(
"sample_dataset_craniometric.csv", header=0, encoding="unicode_escape"
)
craniometric_csv = craniometric_sample_dataframe.to_csv(index=False)
st.download_button(
label="Download sample osteometric csv file",
data=osteometric_csv,
file_name="sample_dataset_osteometric.csv",
mime="text/csv",
)
st.download_button(
label="Download sample craniometric csv file",
data=craniometric_csv,
file_name="sample_dataset_craniometric.csv",
mime="text/csv",
)
if st.sidebar.button("Contact", key="contact_button"):
with st.container():
placeholder_write_welcome.empty()
placeholder_write_result.empty()
placeholder_write_warning.empty()
placeholder_write_input.markdown(contact_section, unsafe_allow_html=True)
with st.sidebar.expander("Osteometric Prediction (single skeleton)"):
BIB = st.number_input("BIB", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
HML = st.number_input("HML", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
HHD = st.number_input("HHD", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
RML = st.number_input("RML", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
FML = st.number_input("FML", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
FBL = st.number_input("FBL", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
FHD = st.number_input("FHD", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
TML = st.number_input("TML", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
FEB = st.number_input("FEB", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
TPB = st.number_input("TPB", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
HEB = st.number_input("HEB", min_value=0.0, max_value=599.0, value=0.0, step=0.5)
input_vector_goldman = [[BIB, HML, HHD, RML, FML, FBL, FHD, TML, FEB, TPB, HEB]]
input_vector_goldman = np.asarray(input_vector_goldman, dtype="float64")
is_all_zero_osteometric = not input_vector_goldman.all()
# print(is_all_zero_osteometric)
models_goldman_list = ["<Not selected>", "XGB", "LGB", "LDA"]
default_goldman = models_goldman_list.index("<Not selected>")
model_selection_goldman = st.selectbox(
"Select a model",
options=models_goldman_list,
index=default_goldman,
key="model_selection_goldman",
)
if st.button("Calculate", key="osteometric_single_entry_button"):
placeholder_write_welcome.empty()
placeholder_write_input.empty()
placeholder_write_result.empty()
placeholder_write_warning.empty()
goldman_df = pd.DataFrame(input_vector_goldman, columns=columns_goldman)
goldman_df = goldman_df.round(2)
goldman_df = goldman_df.astype(str)
predict_proba_goldman = 0
accuracy_goldman = 0
if model_selection_goldman == "<Not selected>" and is_all_zero_osteometric:
placeholder_write_warning.warning(
"""
Some variables are missing and no model is selected. Please enter
your variables and select a model.
Please note that these models have been optimized to work with
all 11 variables submitted. You can try
the **Osteometric Prediction (missing data)** mode
if you have missing data.
"""
)
if model_selection_goldman != "<Not selected>" and is_all_zero_osteometric:
placeholder_write_warning.warning(
"""
Some variables are missing.
Please note that these models have been optimized to work with
all 11 variables submitted. You can try
the **Osteometric Prediction (missing data)** mode
if you have missing data.
"""
)
if model_selection_goldman == "<Not selected>" and not is_all_zero_osteometric:
placeholder_write_warning.warning(
"""
Please select a model.
"""
)
if model_selection_goldman != "<Not selected>" and not is_all_zero_osteometric:
placeholder_write_input.dataframe(goldman_df)
if model_selection_goldman == "XGB":
predict_proba_goldman = xgb_model_goldman.predict_proba(
input_vector_goldman
)
accuracy_goldman = accuracy_xgb_model_goldman
if model_selection_goldman == "LGB":
predict_proba_goldman = lgb_model_goldman.predict_proba(
input_vector_goldman
)
accuracy_goldman = accuracy_lgb_model_goldman
if model_selection_goldman == "LDA":
predict_proba_goldman = lda_model_goldman.predict_proba(
input_vector_goldman
)
accuracy_goldman = accuracy_lda_model_goldman
predict_proba_goldman = 100 * predict_proba_goldman
predict_proba_goldman = predict_proba_goldman[0].tolist()
# placeholder_write_result.success(
# "The probability for the sex being male is {male:.2f}% and female {female:.2f}%".format(
# male=predict_proba_goldman[0], female=predict_proba_goldman[1]
# )
# )
placeholder_write_result.success(
"""
###### The probability of the individual being male is {male:.2f}% and the probability of being female is {female:.2f}%
__The model was trained using 1528 cases. The dataset was
split into a training set and a test set with
proportions 70% and 30%, respectively. The model was then
trained using the training
set and [GridSearchCV](https://bit.ly/3F8s50E),
which optimized the model's
hyperparameters and cross-validated it. The trained
model was then tested using the test set,
achieving an accuracy of {accuracy:.2f}%.__
""".format(
male=predict_proba_goldman[0],
female=predict_proba_goldman[1],
accuracy=accuracy_goldman,
)
)
with st.sidebar.expander("Craniometric Prediction (single skeleton)"):
GOL = st.number_input("GOL", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
NOL = st.number_input("NOL", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
BNL = st.number_input("BNL", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
BBH = st.number_input("BBH", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
XCB = st.number_input("XCB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
XFB = st.number_input("XFB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
ZYB = st.number_input("ZYB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
AUB = st.number_input("AUB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
WCB = st.number_input("WCB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
ASB = st.number_input("ASB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
BPL = st.number_input("BPL", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
NPH = st.number_input("NPH", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
NLH = st.number_input("NLH", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
JUB = st.number_input("JUB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
NLB = st.number_input("NLB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
MAB = st.number_input("MAB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
MDH = st.number_input("MDH", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
MDB = st.number_input("MDB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
OBH = st.number_input("OBH", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
OBB = st.number_input("OBB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
DKB = st.number_input("DKB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
ZMB = st.number_input("ZMB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
FMB = st.number_input("FMB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
EKB = st.number_input("EKB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
IML = st.number_input("IML", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
XML = st.number_input("XML", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
WMH = st.number_input("WMH", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
STB = st.number_input("STB", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
FRC = st.number_input("FRC", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
PAC = st.number_input("PAC", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
OCC = st.number_input("OCC", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
FOL = st.number_input("FOL", min_value=0.0, max_value=500.0, value=0.0, step=0.5)
input_vector_howell = [
[
GOL,
NOL,
BNL,
BBH,
XCB,
XFB,
ZYB,
AUB,
WCB,
ASB,
BPL,
NPH,
NLH,
JUB,
NLB,
MAB,
MDH,
MDB,
OBH,
OBB,
DKB,
ZMB,
FMB,
EKB,
IML,
XML,
WMH,
STB,
FRC,
PAC,
OCC,
FOL,
]
]
input_vector_howell = np.asarray(input_vector_howell, dtype="float64")
is_all_zero_craniometric = not input_vector_howell.all()
models_howell_list = ["<Not selected>", "XGB", "LGB", "LDA"]
default_howell = models_howell_list.index("<Not selected>")
model_selection_howell = st.selectbox(
"Select a model",
options=models_howell_list,
index=default_howell,
key="model_selection_howell",
)
if st.button("Calculate", key="craniometric_single_entry_button"):
placeholder_write_welcome.empty()
placeholder_write_input.empty()
placeholder_write_result.empty()
placeholder_write_warning.empty()
howell_df = pd.DataFrame(input_vector_howell, columns=columns_howell)
howell_df = howell_df.round(2)
howell_df = howell_df.astype(str)
predict_proba_howell = 0
accuracy_howell = 0
if model_selection_howell == "<Not selected>" and is_all_zero_craniometric:
placeholder_write_warning.warning(
"""
Some variables are missing and no model is selected. Please enter
your variables and select a model.
Please note that these models have been optimized to work with
all 32 variables submitted. You can try
the **Craniometric Prediction (missing data)** mode
if you have missing data.
"""
)
if model_selection_howell != "<Not selected>" and is_all_zero_craniometric:
placeholder_write_warning.warning(
"""
Some variables are missing.
Please note that these models have been optimized to work with
all 32 variables submitted. You can try
the **Craniometric Prediction (missing data)** mode
if you have missing data.
"""
)
if model_selection_howell == "<Not selected>" and not is_all_zero_craniometric:
placeholder_write_warning.warning(
"""
Please select a model.
"""
)
if model_selection_howell != "<Not selected>" and not is_all_zero_craniometric:
placeholder_write_input.dataframe(howell_df)
if model_selection_howell == "XGB":
predict_proba_howell = xgb_model_howell.predict_proba(
input_vector_howell
)
accuracy_howell = accuracy_xgb_model_howell
if model_selection_howell == "LGB":
predict_proba_howell = lgb_model_howell.predict_proba(
input_vector_howell
)
accuracy_howell = accuracy_lgb_model_howell
if model_selection_howell == "LDA":
predict_proba_howell = lda_model_howell.predict_proba(
input_vector_howell
)
accuracy_howell = accuracy_lda_model_howell
predict_proba_howell = 100 * predict_proba_howell
predict_proba_howell = predict_proba_howell[0].tolist()
# placeholder_write_result.success(
# "The probability for the sex being male is {male:.2f}% and female {female:.2f}%".format(
# male=predict_proba_howell[0], female=predict_proba_howell[1]
# )
# )
placeholder_write_result.success(
"""
###### The probability of the individual being male is {male:.2f}% and the probability of being female is {female:.2f}%
_The model was trained using 3048 cases. The dataset was
split into a training set and a test set with
proportions 70% and 30%, respectively. The model was then
trained using the training
set and [GridSearchCV](https://bit.ly/3F8s50E),
which optimized the model's
hyperparameters and cross-validated it. The trained
model was then tested using the test set,
achieving an accuracy of {accuracy:.2f}%._
""".format(
male=predict_proba_howell[0],
female=predict_proba_howell[1],
accuracy=accuracy_howell,
)
)
with st.sidebar.expander("Osteometric Prediction (multiple skeletons)"):
df_goldman_file = pd.DataFrame()
uploaded_file_goldman = st.file_uploader(
"Choose a CSV file", type=["csv"], key="osteometric_uploader"
)
if uploaded_file_goldman is not None:
try:
df_goldman_file = pd.read_csv(
uploaded_file_goldman, usecols=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
)
except ValueError:
placeholder_write_welcome.empty()
placeholder_write_input.empty()
placeholder_write_result.empty()
placeholder_write_warning.warning(
"""
Please check that your file's format complies with the example file format provided
in the **How to** section
"""
)
# raise ValueError("The file format is not supported")
models_goldman_file_list = ["<Not selected>", "XGB", "LGB", "LDA"]
default_goldman_file = models_goldman_file_list.index("<Not selected>")
model_selection_goldman_file = st.selectbox(
"Select a model",
options=models_goldman_file_list,
index=default_goldman_file,
key="model_selection_goldman_file",
)
if st.button("Calculate ", key="osteometric_file_entry_button"):
placeholder_write_welcome.empty()
placeholder_write_input.empty()
placeholder_write_result.empty()
placeholder_write_warning.empty()
if df_goldman_file.empty == True and (
model_selection_goldman_file != "<Not selected>"
):
placeholder_write_warning.warning("Please upload a file")
if df_goldman_file.empty == True and (
model_selection_goldman_file == "<Not selected>"
):
placeholder_write_warning.warning("Please upload a file and choose a model")
if (df_goldman_file.empty != True) and (
model_selection_goldman_file == "<Not selected>"
):
placeholder_write_warning.warning("Please select a model")
if (df_goldman_file.empty != True) and (
model_selection_goldman_file != "<Not selected>"
):
df_goldman_file = df_goldman_file[(df_goldman_file != 0).all(1)]
df_goldman_file = df_goldman_file.dropna()
df_goldman_file.columns = columns_goldman
df_goldman_file = df_goldman_file.round(2)
df_goldman_file = df_goldman_file.astype(str)
predict_proba_goldman_file = []
accuracy_goldman_file = 0
placeholder_write_input.dataframe(df_goldman_file.reset_index(drop=True))
if model_selection_goldman_file == "XGB":
predict_proba_goldman_file = xgb_model_goldman.predict_proba(
df_goldman_file.values
)
accuracy_goldman_file = accuracy_xgb_model_goldman
if model_selection_goldman_file == "LGB":
predict_proba_goldman_file = lgb_model_goldman.predict_proba(
df_goldman_file.values
)
accuracy_goldman_file = accuracy_lgb_model_goldman
if model_selection_goldman_file == "LDA":
predict_proba_goldman_file = lda_model_goldman.predict_proba(
df_goldman_file.values
)
accuracy_goldman_file = accuracy_lda_model_goldman
predict_proba_goldman_file = pd.DataFrame(predict_proba_goldman_file * 100)
predict_proba_goldman_file = predict_proba_goldman_file.round(2)
predict_proba_goldman_file = predict_proba_goldman_file.astype(str)
predict_proba_goldman_file.columns = ["Male", "Female"]
placeholder_write_result.dataframe(predict_proba_goldman_file)
placeholder_write_warning.info(
"""
Please note that any rows containing missing data
will be dropped as these models have been optimized to
work with all 11 variables submitted. You can try the
**Osteometric Prediction (missing data)** mode for
any cases/rows containing missing data.
_The model was trained using 1528 cases. The dataset was
split into a training set and a test set with
proportions 70% and 30%, respectively. The model was then
trained using the training
set and [GridSearchCV](https://bit.ly/3F8s50E),
which optimized the model's
hyperparameters and cross-validated it. The trained
model was then tested using the test set,
achieving an accuracy of {accuracy:.2f}%._
""".format(
accuracy=accuracy_goldman_file,
)
)
with st.sidebar.expander("Craniometric Prediction (multiple skeletons)"):
df_howell_file = pd.DataFrame()
uploaded_file_howell = st.file_uploader(
"Choose a CSV file", type=["csv"], key="craniometric_uploader"
)
if uploaded_file_howell is not None:
try:
df_howell_file = pd.read_csv(
uploaded_file_howell,
usecols=[
0,
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,
],
)
except ValueError:
placeholder_write_warning.warning(
"""
Please check that your file's format complies with the example file format provided
in the **How to** section
"""
)
models_howell_file_list = ["<Not selected>", "XGB", "LGB", "LDA"]
default_howell_file = models_howell_file_list.index("<Not selected>")