File size: 20,510 Bytes
3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 224f15f 3bf7bf3 224f15f 3bf7bf3 148c712 224f15f 3bf7bf3 224f15f 3bf7bf3 5a49222 3bf7bf3 5a49222 3bf7bf3 5a49222 3bf7bf3 5a49222 224f15f 5a49222 224f15f 3bf7bf3 5a49222 3bf7bf3 5a49222 3bf7bf3 5a49222 224f15f 5a49222 3bf7bf3 224f15f 3bf7bf3 224f15f 3bf7bf3 224f15f 5a49222 3bf7bf3 5a49222 224f15f 3bf7bf3 224f15f 3bf7bf3 224f15f 148c712 3bf7bf3 148c712 3bf7bf3 224f15f 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 5a49222 3bf7bf3 5a49222 3bf7bf3 5a49222 3bf7bf3 5a49222 3bf7bf3 5a49222 224f15f 3bf7bf3 224f15f 3bf7bf3 224f15f 3bf7bf3 224f15f 148c712 3bf7bf3 5a49222 148c712 3bf7bf3 148c712 3bf7bf3 5a49222 3bf7bf3 5a49222 3bf7bf3 5a49222 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 148c712 3bf7bf3 |
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 |
from typing import List, Callable, Any, Tuple
import json
import numpy as np
import streamlit as st
from text_sections import (
design_text,
)
def _add_select_box(
key: str,
label: str,
default: Any,
options: List[Any],
get_setting_fn: Callable,
type_=str,
):
"""
Add selectbox with selection of default value from setting function.
"""
chosen_default = get_setting_fn(
key=key,
default=default,
type_=type_,
options=options,
)
return st.selectbox(
label, options=options, index=options.index(chosen_default), key=key
)
def design_section(
num_classes,
predictions_are_probabilities,
design_settings_store_path,
):
output = {}
with st.form(key="settings_upload_form"):
design_text()
uploaded_settings_path = st.file_uploader(
"Upload design settings", type=["json"]
)
# TODO: Allow resetting settings!
if st.form_submit_button(label="Apply settings"):
if uploaded_settings_path is not None:
uploaded_design_settings = json.load(uploaded_settings_path)
else:
st.warning("No settings were uploaded. Uploading settings is optional.")
def get_uploaded_setting(key, default, type_=None, options=None):
# NOTE: Must be placed here, to have `uploaded_design_settings` in locals
if "uploaded_design_settings" in locals() and key in uploaded_design_settings:
out = uploaded_design_settings[key]
if type_ is not None:
if not isinstance(out, type_):
st.warning(
f"An uploaded setting ({key}) had the wrong type. Using default value."
)
return default
if options is not None:
if out not in options:
st.warning(
f"An uploaded setting ({key}) was not a valid choice. Using default value."
)
return default
return out
return default
with st.form(key="settings_form"):
col1, col2 = st.columns(2)
with col1:
selected_classes = st.multiselect(
"Select classes (min=2, order is respected)",
options=st.session_state["classes"],
default=st.session_state["classes"],
help="Select the classes to create the confusion matrix for. "
"Any observation with either a target or prediction "
"of another class is excluded.",
)
with col2:
if (
st.session_state["input_type"] == "data"
and predictions_are_probabilities
):
prob_of_class = st.selectbox(
"Probabilities are of (not working)",
options=st.session_state["classes"],
index=1,
)
else:
prob_of_class = None
# Color palette
output["palette"] = _add_select_box(
key="palette",
label="Color Palette",
default="Blues",
options=["Blues", "Greens", "Oranges", "Greys", "Purples", "Reds"],
get_setting_fn=get_uploaded_setting,
type_=str,
)
# Ask for output parameters
col1, col2, col3 = st.columns(3)
with col1:
output["width"] = st.number_input(
"Width (px)",
value=get_uploaded_setting(
key="width", default=1200 + 100 * (num_classes - 2), type_=int
),
step=50,
)
with col2:
output["height"] = st.number_input(
"Height (px)",
value=get_uploaded_setting(
key="width", default=1200 + 100 * (num_classes - 2), type_=int
),
step=50,
)
with col3:
output["dpi"] = st.number_input(
"DPI (not working)",
value=get_uploaded_setting(key="dpi", default=320, type_=int),
step=10,
)
st.write(" ") # Slightly bigger gap between the two sections
col1, col2, col3 = st.columns(3)
with col1:
output["show_counts"] = st.checkbox(
"Show Counts",
value=get_uploaded_setting(key="show_counts", default=True, type_=bool),
)
with col2:
output["show_normalized"] = st.checkbox(
"Show Normalized (%)",
value=get_uploaded_setting(
key="show_normalized", default=True, type_=bool
),
)
with col3:
output["show_sums"] = st.checkbox(
"Show Sum Tiles",
value=get_uploaded_setting(key="show_sums", default=False, type_=bool),
help="Show extra row and column with the "
"totals for that row/column.",
)
st.markdown("""---""")
st.markdown("**Advanced**:")
with st.expander("Elements"):
col1, col2 = st.columns(2)
with col1:
output["rotate_y_text"] = st.checkbox(
"Rotate y-axis text",
value=get_uploaded_setting(
key="rotate_y_text", default=True, type_=bool
),
)
output["place_x_axis_above"] = st.checkbox(
"Place x-axis on top",
value=get_uploaded_setting(
key="place_x_axis_above", default=True, type_=bool
),
)
output["counts_on_top"] = st.checkbox(
"Counts on top (not working)",
value=get_uploaded_setting(
key="counts_on_top", default=False, type_=bool
),
help="Whether to switch the positions of the counts and normalized counts (%). "
"The counts become the big centralized numbers and the "
"normalized counts go below with a smaller font size.",
)
with col2:
output["num_digits"] = st.number_input(
"Digits",
value=get_uploaded_setting(key="num_digits", default=2, type_=int),
help="Number of digits to round percentages to.",
)
st.markdown("""---""")
col1, col2 = st.columns(2)
with col1:
st.write("Row and column percentages:")
output["show_row_percentages"] = st.checkbox(
"Show row percentages",
value=get_uploaded_setting(
key="show_row_percentages", default=num_classes < 6, type_=bool
),
)
output["show_col_percentages"] = st.checkbox(
"Show column percentages",
value=get_uploaded_setting(
key="show_col_percentages", default=num_classes < 6, type_=bool
),
)
output["show_arrows"] = st.checkbox(
"Show arrows",
value=get_uploaded_setting(
key="show_arrows", default=True, type_=bool
),
)
output["diag_percentages_only"] = st.checkbox(
"Diagonal row/column percentages only",
value=get_uploaded_setting(
key="diag_percentages_only", default=False, type_=bool
),
)
with col2:
output["arrow_size"] = (
st.slider(
"Arrow size",
value=get_uploaded_setting(
key="arrow_size", default=0.048 * 10, type_=float
),
min_value=0.03 * 10,
max_value=0.06 * 10,
step=0.001 * 10,
)
/ 10
)
output["arrow_nudge_from_text"] = (
st.slider(
"Arrow nudge from text",
value=get_uploaded_setting(
key="arrow_nudge_from_text", default=0.065 * 10, type_=float
),
min_value=0.00,
max_value=0.1 * 10,
step=0.001 * 10,
)
/ 10
)
with st.expander("Tiles"):
col1, col2 = st.columns(2)
with col1:
output["intensity_by"] = _add_select_box(
key="intensity_by",
label="Intensity based on",
default="Counts",
options=["Counts", "Normalized (%)"],
get_setting_fn=get_uploaded_setting,
type_=str,
)
with col2:
output["darkness"] = st.slider(
"Darkness",
min_value=0.0,
max_value=1.0,
value=get_uploaded_setting(
key="darkness", default=0.8, type_=float
),
step=0.01,
help="How dark the darkest colors should be, between 0 and 1, where 1 is darkest.",
)
st.markdown("""---""")
output["show_tile_border"] = st.checkbox(
"Add tile borders",
value=get_uploaded_setting(
key="show_tile_border", default=False, type_=bool
),
)
col1, col2, col3 = st.columns(3)
with col1:
output["tile_border_color"] = st.color_picker(
"Border color",
value=get_uploaded_setting(
key="tile_border_color", default="#000000", type_=str
),
)
with col2:
output["tile_border_size"] = st.slider(
"Border size",
min_value=0.0,
max_value=3.0,
value=get_uploaded_setting(
key="tile_border_size", default=0.1, type_=float
),
step=0.01,
)
with col3:
output["tile_border_linetype"] = _add_select_box(
key="tile_border_linetype",
label="Border linetype",
default="solid",
options=[
"solid",
"dashed",
"dotted",
"dotdash",
"longdash",
"twodash",
],
get_setting_fn=get_uploaded_setting,
type_=str,
)
st.markdown("""---""")
st.write("Sum tile settings:")
col1, col2 = st.columns(2)
with col1:
output["sum_tile_palette"] = _add_select_box(
key="sum_tile_palette",
label="Color Palette",
default="Greens",
options=["Greens", "Oranges", "Greys", "Purples", "Reds", "Blues"],
get_setting_fn=get_uploaded_setting,
type_=str,
)
with col2:
output["sum_tile_label"] = st.text_input(
"Label",
value=get_uploaded_setting(
key="sum_tile_label", default="Σ", type_=str
),
key="sum_tiles_label",
)
# tile_fill = NULL,
# font_color = NULL,
# tile_border_color = NULL,
# tile_border_size = NULL,
# tile_border_linetype = NULL,
# tc_tile_fill = NULL,
# tc_font_color = NULL,
# tc_tile_border_color = NULL,
# tc_tile_border_size = NULL,
# tc_tile_border_linetype = NULL
with st.expander("Zero Counts"):
st.write("Special settings for tiles where the count is 0:")
col1, col2, col3 = st.columns(3)
with col1:
output["show_zero_shading"] = st.checkbox(
"Add shading",
value=get_uploaded_setting(
key="show_zero_shading", default=True, type_=bool
),
)
with col2:
output["show_zero_text"] = st.checkbox(
"Show text",
value=get_uploaded_setting(
key="show_zero_text", default=False, type_=bool
),
help="Whether to show counts, normalized (%), etc.",
)
with col3:
output["show_zero_percentages"] = st.checkbox(
"Show row/column percentages",
value=get_uploaded_setting(
key="show_zero_percentages", default=False, type_=bool
),
help="Only relevant when row/column percentages are enabled.",
)
if True:
with st.expander("Fonts"):
# Specify available settings and defaults per font
font_types = {
"Top Font": {
"key_prefix": "font_top",
"description": "The big text in the middle (normalized (%) by default).",
"settings": {
"size": 4.3, # 2.8
"color": "#000000",
"alpha": 1.0,
"bold": False,
"italics": False,
},
},
"Bottom Font": {
"key_prefix": "font_bottom",
"description": "The text just below the top font (counts by default).",
"settings": {
"size": 2.8,
"color": "#000000",
"alpha": 1.0,
"bold": False,
"italics": False,
},
},
"Percentages Font": {
"key_prefix": "font_percentage",
"description": "The row and column percentages.",
"settings": {
"size": 2.35,
"color": "#000000",
"alpha": 0.85,
"bold": False,
"italics": True,
"suffix": "%",
"prefix": "",
},
},
"Normalized (%)": {
"key_prefix": "font_normalized",
"description": "Special settings for the normalized (%) text.",
"settings": {"suffix": "%", "prefix": ""},
},
"Counts": {
"key_prefix": "font_counts",
"description": "Special settings for the counts text.",
"settings": {"suffix": "", "prefix": ""},
},
}
for font_type_title, font_type_spec in font_types.items():
st.markdown(f"**{font_type_title}**")
st.markdown(font_type_spec["description"])
num_cols = 3
font_settings = create_font_settings(
key_prefix=font_type_spec["key_prefix"],
get_setting_fn=get_uploaded_setting,
settings_to_get=list(font_type_spec["settings"].keys()),
)
for i, (setting_name, setting_widget) in enumerate(
font_settings.items()
):
if i % num_cols == 0:
cols = st.columns(num_cols)
with cols[i % num_cols]:
default = font_type_spec["settings"][
setting_name[len(font_type_spec["key_prefix"]) + 1 :]
]
output[setting_name] = setting_widget(
k=setting_name, d=default
)
if font_type_title != list(font_types.keys())[-1]:
st.markdown("""---""")
st.markdown("""---""")
if st.form_submit_button(label="Generate plot"):
st.session_state["step"] = 3
if output["show_sums"] and output["sum_tile_palette"] == output["palette"]:
# Save settings as json
with open(design_settings_store_path, "w") as f:
json.dump(output, f)
design_ready = False
if st.session_state["step"] >= 3:
design_ready = True
if output["show_sums"] and output["sum_tile_palette"] == output["palette"]:
st.error(
"The color palettes (background colors) "
"for the tiles and sum tiles are identical. "
"Please select a different color palette for "
"the sum tiles under **Tiles** >> *Sum tile settings*."
)
design_ready = False
return output, design_ready, selected_classes, prob_of_class
# defaults: dict,
def create_font_settings(
key_prefix: str, get_setting_fn: Callable, settings_to_get: List[str]
) -> Tuple[dict, dict]:
# TODO: Defaults must be set based on font type! Also,
# we probably need to allow not setting the argument so the
# plotting function can handle the defaulting?
def make_key(key):
return f"{key_prefix}_{key}"
font_settings = {
make_key("color"): lambda k, d: st.color_picker(
"Color",
key=k,
value=get_setting_fn(key=k, default=d, type_=str),
),
make_key("bold"): lambda k, d: st.checkbox(
"Bold",
key=k,
value=get_setting_fn(key=k, default=d, type_=bool),
),
make_key("italics"): lambda k, d: st.checkbox(
"Italics",
key=k,
value=get_setting_fn(key=k, default=d, type_=bool),
),
make_key("size"): lambda k, d: st.number_input(
"Size",
key=k,
value=get_setting_fn(key=k, default=float(d), type_=float),
),
make_key("nudge_x"): lambda k, d: st.number_input(
"Nudge on x-axis",
key=k,
value=get_setting_fn(key=k, default=d, type_=float),
),
make_key("nudge_y"): lambda k, d: st.number_input(
"Nudge on y-axis",
key=k,
value=get_setting_fn(key=k, default=d, type_=float),
),
make_key("alpha"): lambda k, d: st.slider(
"Transparency",
min_value=0.0,
max_value=1.0,
value=get_setting_fn(key=k, default=d, type_=float),
step=0.01,
key=k,
),
make_key("prefix"): lambda k, d: st.text_input(
"Prefix",
key=k,
value=get_setting_fn(key=k, default=d, type_=str),
),
make_key("suffix"): lambda k, d: st.text_input(
"Suffix",
key=k,
value=get_setting_fn(key=k, default=d, type_=str),
),
}
# Filter settings
font_settings = {
k: v
for k, v in font_settings.items()
if f"{k[len(key_prefix)+1:]}" in settings_to_get
}
return font_settings
|