Spaces:
Sleeping
Sleeping
Delete visual2.py
#9
by SVanc - opened
- visual2.py +0 -1283
visual2.py
DELETED
|
@@ -1,1283 +0,0 @@
|
|
| 1 |
-
# /// script
|
| 2 |
-
# requires-python = ">=3.11"
|
| 3 |
-
# dependencies = [
|
| 4 |
-
# "marimo>=0.23.1",
|
| 5 |
-
# "numpy==2.4.4",
|
| 6 |
-
# "pandas==3.0.2",
|
| 7 |
-
# "svgwrite>=1.4.3",
|
| 8 |
-
# "openpyxl>=3.1.0",
|
| 9 |
-
# "cairosvg==2.9.0",
|
| 10 |
-
# ]
|
| 11 |
-
# ///
|
| 12 |
-
|
| 13 |
-
import marimo
|
| 14 |
-
|
| 15 |
-
__generated_with = "0.23.0"
|
| 16 |
-
app = marimo.App(
|
| 17 |
-
width="full",
|
| 18 |
-
css_file="/usr/local/_marimo/custom.css",
|
| 19 |
-
auto_download=["html"],
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
@app.cell
|
| 24 |
-
def _():
|
| 25 |
-
import math
|
| 26 |
-
from pathlib import Path
|
| 27 |
-
|
| 28 |
-
import marimo as mo
|
| 29 |
-
import numpy as np
|
| 30 |
-
import pandas as pd
|
| 31 |
-
import svgwrite
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return Path, math, mo, np, pd, svgwrite
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
@app.cell
|
| 38 |
-
def _(mo):
|
| 39 |
-
mo.md(r"""
|
| 40 |
-
<style>
|
| 41 |
-
/* Main container for the whole notebook UI */
|
| 42 |
-
.coffee-sites-notebook {
|
| 43 |
-
/* Color variables (used throughout for consistency) */
|
| 44 |
-
--bg: #f7f4ee;
|
| 45 |
-
--card: #fffdf9;
|
| 46 |
-
--ink: #1f2937;
|
| 47 |
-
--muted: #6b7280;
|
| 48 |
-
--line: #d8d1c3;
|
| 49 |
-
--accent: #6f4e37;
|
| 50 |
-
--shadow: 0 10px 28px rgba(17, 24, 39, 0.08);
|
| 51 |
-
|
| 52 |
-
/* Default text color and font */
|
| 53 |
-
color: var(--ink);
|
| 54 |
-
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
/* Shared style for hero (top banner) and cards */
|
| 58 |
-
.coffee-sites-notebook .hero,
|
| 59 |
-
.coffee-sites-notebook .card {
|
| 60 |
-
background: var(--card); /* white-ish background */
|
| 61 |
-
border: 1px solid rgba(111, 78, 55, 0.14); /* subtle border */
|
| 62 |
-
border-radius: 18px; /* rounded corners */
|
| 63 |
-
box-shadow: var(--shadow); /* soft shadow */
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
/* Top banner styling */
|
| 67 |
-
.coffee-sites-notebook .hero {
|
| 68 |
-
padding: 1.35rem 1.5rem; /* space inside */
|
| 69 |
-
margin-bottom: 1rem; /* space below */
|
| 70 |
-
/* Gradient background */
|
| 71 |
-
background: linear-gradient(135deg, rgba(111,78,55,0.08), rgba(217,180,74,0.12) 55%, rgba(90,169,230,0.08));
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
/* Small label above title */
|
| 75 |
-
.coffee-sites-notebook .eyebrow {
|
| 76 |
-
display: inline-block;
|
| 77 |
-
margin-bottom: 0.55rem;
|
| 78 |
-
padding: 0.25rem 0.6rem;
|
| 79 |
-
border-radius: 999px; /* pill shape */
|
| 80 |
-
font-size: 0.78rem;
|
| 81 |
-
font-weight: 700;
|
| 82 |
-
letter-spacing: 0.04em;
|
| 83 |
-
text-transform: uppercase; /* ALL CAPS */
|
| 84 |
-
color: #6f4e37;
|
| 85 |
-
background: rgba(111, 78, 55, 0.1);
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
/* Remove default spacing above headings and paragraphs */
|
| 89 |
-
.coffee-sites-notebook h1,
|
| 90 |
-
.coffee-sites-notebook h2,
|
| 91 |
-
.coffee-sites-notebook h3,
|
| 92 |
-
.coffee-sites-notebook p {
|
| 93 |
-
margin-top: 0;
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
/* Main title styling */
|
| 97 |
-
.coffee-sites-notebook .hero h1 {
|
| 98 |
-
font-size: 1.9rem;
|
| 99 |
-
line-height: 1.15;
|
| 100 |
-
margin-bottom: 0.4rem;
|
| 101 |
-
}
|
| 102 |
-
|
| 103 |
-
/* Description text */
|
| 104 |
-
.coffee-sites-notebook .hero p {
|
| 105 |
-
margin-bottom: 0;
|
| 106 |
-
max-width: 72ch; /* limits line width for readability */
|
| 107 |
-
color: var(--muted); /* softer color */
|
| 108 |
-
line-height: 1.55;
|
| 109 |
-
}
|
| 110 |
-
|
| 111 |
-
/* Bullet list styling */
|
| 112 |
-
.coffee-sites-notebook .hero ul {
|
| 113 |
-
margin: 0.85rem 0 0 1rem;
|
| 114 |
-
color: var(--ink);
|
| 115 |
-
}
|
| 116 |
-
|
| 117 |
-
/* Section title styling */
|
| 118 |
-
.coffee-sites-notebook .section-title {
|
| 119 |
-
font-size: 1.05rem;
|
| 120 |
-
font-weight: 700;
|
| 121 |
-
margin: 0 0 0.8rem 0;
|
| 122 |
-
color: var(--accent);
|
| 123 |
-
}
|
| 124 |
-
|
| 125 |
-
/* Grid layout for stats */
|
| 126 |
-
.coffee-sites-notebook .stat-grid {
|
| 127 |
-
display: grid;
|
| 128 |
-
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); /* responsive grid */
|
| 129 |
-
gap: 0.8rem;
|
| 130 |
-
margin: 0.35rem 0 1rem 0;
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
/* Individual stat card */
|
| 134 |
-
.coffee-sites-notebook .stat {
|
| 135 |
-
padding: 0.95rem 1rem;
|
| 136 |
-
}
|
| 137 |
-
|
| 138 |
-
/* Stat label (small text) */
|
| 139 |
-
.coffee-sites-notebook .stat-label {
|
| 140 |
-
font-size: 0.78rem;
|
| 141 |
-
text-transform: uppercase;
|
| 142 |
-
letter-spacing: 0.06em;
|
| 143 |
-
color: var(--muted);
|
| 144 |
-
margin-bottom: 0.25rem;
|
| 145 |
-
}
|
| 146 |
-
|
| 147 |
-
/* Stat main number */
|
| 148 |
-
.coffee-sites-notebook .stat-value {
|
| 149 |
-
font-size: 1.35rem;
|
| 150 |
-
font-weight: 750;
|
| 151 |
-
color: var(--ink);
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
/* Extra note under stat */
|
| 155 |
-
.coffee-sites-notebook .stat-note {
|
| 156 |
-
margin-top: 0.15rem;
|
| 157 |
-
color: var(--muted);
|
| 158 |
-
font-size: 0.86rem;
|
| 159 |
-
}
|
| 160 |
-
|
| 161 |
-
/* Frame around the SVG figure */
|
| 162 |
-
.coffee-sites-notebook .figure-frame {
|
| 163 |
-
background: #fff;
|
| 164 |
-
border: 1px solid rgba(31, 41, 55, 0.08);
|
| 165 |
-
border-radius: 20px;
|
| 166 |
-
padding: 1rem;
|
| 167 |
-
overflow-x: auto; /* scroll if too wide */
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
/* Helper text style */
|
| 171 |
-
.coffee-sites-notebook .helper {
|
| 172 |
-
color: var(--muted);
|
| 173 |
-
font-size: 0.93rem;
|
| 174 |
-
line-height: 1.5;
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
/* Code style formatting for file paths */
|
| 178 |
-
.coffee-sites-notebook .download-note code {
|
| 179 |
-
background: rgba(17, 24, 39, 0.05);
|
| 180 |
-
padding: 0.12rem 0.35rem;
|
| 181 |
-
border-radius: 6px;
|
| 182 |
-
}
|
| 183 |
-
|
| 184 |
-
/* ===== SVG chart styling ===== */
|
| 185 |
-
|
| 186 |
-
.coffee-sites-svg {
|
| 187 |
-
max-width: 100%;
|
| 188 |
-
height: auto;
|
| 189 |
-
display: block;
|
| 190 |
-
}
|
| 191 |
-
|
| 192 |
-
/* Horizontal row lines */
|
| 193 |
-
.coffee-sites-svg .row-line {
|
| 194 |
-
stroke: #ece7de;
|
| 195 |
-
stroke-width: 1;
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
/* Panel background */
|
| 199 |
-
.coffee-sites-svg .panel-card {
|
| 200 |
-
fill: #fffdfa;
|
| 201 |
-
stroke: #e1d7c9;
|
| 202 |
-
stroke-width: 1;
|
| 203 |
-
}
|
| 204 |
-
|
| 205 |
-
/* Panel header background */
|
| 206 |
-
.coffee-sites-svg .panel-cap {
|
| 207 |
-
fill: #f3ece1;
|
| 208 |
-
}
|
| 209 |
-
|
| 210 |
-
/* Grid and axis lines */
|
| 211 |
-
.coffee-sites-svg .axis-line,
|
| 212 |
-
.coffee-sites-svg .grid-line {
|
| 213 |
-
vector-effect: non-scaling-stroke;
|
| 214 |
-
}
|
| 215 |
-
|
| 216 |
-
.coffee-sites-svg .grid-line {
|
| 217 |
-
stroke: #d9d2c6;
|
| 218 |
-
stroke-width: 0.9;
|
| 219 |
-
stroke-dasharray: 4 4; /* dashed lines */
|
| 220 |
-
}
|
| 221 |
-
|
| 222 |
-
.coffee-sites-svg .axis-line {
|
| 223 |
-
stroke: #6b6257;
|
| 224 |
-
stroke-width: 1.2;
|
| 225 |
-
}
|
| 226 |
-
|
| 227 |
-
/* Interactive elements (bars + bubbles) */
|
| 228 |
-
.coffee-sites-svg .yield-bar,
|
| 229 |
-
.coffee-sites-svg .csi-bar,
|
| 230 |
-
.coffee-sites-svg .bio-bubble {
|
| 231 |
-
transition: opacity 160ms ease, transform 160ms ease, stroke-width 160ms ease;
|
| 232 |
-
cursor: pointer; /* shows it's interactive */
|
| 233 |
-
}
|
| 234 |
-
|
| 235 |
-
/* Hover effect */
|
| 236 |
-
.coffee-sites-svg .yield-bar:hover,
|
| 237 |
-
.coffee-sites-svg .csi-bar:hover,
|
| 238 |
-
.coffee-sites-svg .bio-bubble:hover {
|
| 239 |
-
opacity: 1;
|
| 240 |
-
stroke: #111827;
|
| 241 |
-
stroke-width: 1.2;
|
| 242 |
-
}
|
| 243 |
-
|
| 244 |
-
.coffee-sites-svg text {
|
| 245 |
-
user-select: none; /* prevents selecting text */
|
| 246 |
-
}
|
| 247 |
-
|
| 248 |
-
/* Tooltip (hover popup) */
|
| 249 |
-
.coffee-tooltip {
|
| 250 |
-
position: fixed;
|
| 251 |
-
z-index: 9999;
|
| 252 |
-
pointer-events: none;
|
| 253 |
-
opacity: 0; /* hidden by default */
|
| 254 |
-
transform: translate(12px, 12px);
|
| 255 |
-
transition: opacity 120ms ease;
|
| 256 |
-
background: rgba(17, 24, 39, 0.96);
|
| 257 |
-
color: white;
|
| 258 |
-
padding: 0.55rem 0.7rem;
|
| 259 |
-
border-radius: 10px;
|
| 260 |
-
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
|
| 261 |
-
font-size: 0.84rem;
|
| 262 |
-
line-height: 1.35;
|
| 263 |
-
max-width: 280px;
|
| 264 |
-
white-space: pre-line;
|
| 265 |
-
font-family: Inter, "Segoe UI", Arial, sans-serif;
|
| 266 |
-
}
|
| 267 |
-
</style>
|
| 268 |
-
|
| 269 |
-
<!-- Main container -->
|
| 270 |
-
<div class="coffee-sites-notebook">
|
| 271 |
-
|
| 272 |
-
<!-- Top banner -->
|
| 273 |
-
<div class="hero">
|
| 274 |
-
|
| 275 |
-
<!-- Small label -->
|
| 276 |
-
<div class="eyebrow">Marimo · SVG · HTML/CSS</div>
|
| 277 |
-
|
| 278 |
-
<!-- Title -->
|
| 279 |
-
<h1>Coffee sites visualization</h1>
|
| 280 |
-
|
| 281 |
-
<!-- Description -->
|
| 282 |
-
<p>
|
| 283 |
-
This marimo notebook shows our visualization for the coffeesites
|
| 284 |
-
</p>
|
| 285 |
-
|
| 286 |
-
<!-- Explanation of panels -->
|
| 287 |
-
<ul>
|
| 288 |
-
<li><strong>Panel 1:</strong> yield bars for 2017, 2018, 2019, and mean yield</li>
|
| 289 |
-
<li><strong>Panel 2:</strong> coffee structure index bar per site</li>
|
| 290 |
-
<li><strong>Panel 3:</strong> biodiversity circles with <em>x = standardized richness</em> and <em>size = abundance</em></li>
|
| 291 |
-
</ul>
|
| 292 |
-
|
| 293 |
-
</div>
|
| 294 |
-
</div>
|
| 295 |
-
""")
|
| 296 |
-
return
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
@app.cell
|
| 300 |
-
def _(Path, mo):
|
| 301 |
-
# Possible default locations where the Excel file might exist
|
| 302 |
-
default_candidates = [
|
| 303 |
-
"/mnt/data/merged_species_dataset.xlsx", # uploaded dataset
|
| 304 |
-
"/__modal/volumes/vo-qWmx7cdqVT9K9vk42U6q5b/merged_species_dataset.xlsx", # voor Molab, maar exacte link aan te passen
|
| 305 |
-
]
|
| 306 |
-
|
| 307 |
-
# Code neemt het eerste bestaande path, en als hij er geen vindt, dan neemt hij het eerst path als default
|
| 308 |
-
default_file = next((p for p in default_candidates if Path(p).exists()), default_candidates[0])
|
| 309 |
-
|
| 310 |
-
# naam voor de stem van de output, zonder de extensie
|
| 311 |
-
default_stem = "/__modal/volumes/vo-qWmx7cdqVT9K9vk42U6q5b/coffee_sites_visualisation_marimo_v3"
|
| 312 |
-
|
| 313 |
-
# Input box for excel path
|
| 314 |
-
data_path = mo.ui.text(
|
| 315 |
-
value=default_file,
|
| 316 |
-
label="Excel dataset path",
|
| 317 |
-
full_width=True
|
| 318 |
-
)
|
| 319 |
-
|
| 320 |
-
# Voor (bijkomende) png output, check the box
|
| 321 |
-
export_png = mo.ui.checkbox(
|
| 322 |
-
value=False,
|
| 323 |
-
label="Also export PNG (optional; requires cairosvg)"
|
| 324 |
-
)
|
| 325 |
-
|
| 326 |
-
# box voor output stem
|
| 327 |
-
output_stem = mo.ui.text(
|
| 328 |
-
value=default_stem,
|
| 329 |
-
label="Output file stem",
|
| 330 |
-
full_width=True
|
| 331 |
-
)
|
| 332 |
-
|
| 333 |
-
# Display of input controls
|
| 334 |
-
mo.vstack(
|
| 335 |
-
[
|
| 336 |
-
mo.md(
|
| 337 |
-
'<div class="coffee-sites-notebook"><div class="card" style="padding: 1rem 1.1rem;">'
|
| 338 |
-
'<div class="section-title">Inputs</div>'
|
| 339 |
-
'<div class="helper">These controls stay simple so the notebook reruns cleanly. The visual itself is fully data-driven.</div>'
|
| 340 |
-
'</div></div>'
|
| 341 |
-
),
|
| 342 |
-
data_path,
|
| 343 |
-
mo.hstack(
|
| 344 |
-
[export_png, output_stem],
|
| 345 |
-
justify="start",
|
| 346 |
-
gap=1.5
|
| 347 |
-
),
|
| 348 |
-
]
|
| 349 |
-
)
|
| 350 |
-
return data_path, export_png, output_stem
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
@app.cell
|
| 354 |
-
def _():
|
| 355 |
-
# ===== DATA setup =====
|
| 356 |
-
|
| 357 |
-
YEAR_COLUMNS = ["CC_Yield_2017", "CC_Yield_2018", "CC_Yield_2019"]
|
| 358 |
-
MEAN_COLUMN = "Mean_CC_Yield"
|
| 359 |
-
CSI_COLUMN = "Coffee structure index"
|
| 360 |
-
SITE_COLUMN = "Site_ID"
|
| 361 |
-
|
| 362 |
-
SPECIES_GROUPS = [ # This is a list of dictionaries for the biodiversity panel
|
| 363 |
-
{
|
| 364 |
-
"name": "Woody",
|
| 365 |
-
"richness_col": "Woody_Spps",
|
| 366 |
-
"abundance_col": "Woody_abundance",
|
| 367 |
-
"sum_col": "woody_amount",
|
| 368 |
-
"x_col": "woody_x",
|
| 369 |
-
"color": "#3B7A57",
|
| 370 |
-
},
|
| 371 |
-
{
|
| 372 |
-
"name": "Herbaceous",
|
| 373 |
-
"richness_col": "Veg_Spps",
|
| 374 |
-
"abundance_col": "Herbaceous_abundance",
|
| 375 |
-
"sum_col": "herbaceous_amount",
|
| 376 |
-
"x_col": "herbaceous_x",
|
| 377 |
-
"color": "#5AA9E6",
|
| 378 |
-
},
|
| 379 |
-
{
|
| 380 |
-
"name": "Bryophyte",
|
| 381 |
-
"richness_col": "Bryo_Spps",
|
| 382 |
-
"abundance_col": "Bryophyte_frequency",
|
| 383 |
-
"sum_col": "bryophyte_amount",
|
| 384 |
-
"x_col": "bryophyte_x",
|
| 385 |
-
"color": "#D9B44A",
|
| 386 |
-
},
|
| 387 |
-
]
|
| 388 |
-
|
| 389 |
-
# ===== COLOR PALETTE FOR THE VISUALIZATION =====
|
| 390 |
-
|
| 391 |
-
PALETTE = {
|
| 392 |
-
"text": "#1F2937",
|
| 393 |
-
"muted": "#6B7280",
|
| 394 |
-
"axis": "#5D554B",
|
| 395 |
-
"grid": "#D9D2C6",
|
| 396 |
-
"background": "#F7F4EE",
|
| 397 |
-
"card": "#FFFDF9",
|
| 398 |
-
"card_cap": "#F3ECE1",
|
| 399 |
-
"row": "#ECE7DE",
|
| 400 |
-
"year_2017": "#C9D6EA",
|
| 401 |
-
"year_2018": "#93B5E1",
|
| 402 |
-
"year_2019": "#4E79A7",
|
| 403 |
-
"mean": "#1F2937",
|
| 404 |
-
"csi": "#D97B66",
|
| 405 |
-
"accent": "#6F4E37",
|
| 406 |
-
}
|
| 407 |
-
return (
|
| 408 |
-
CSI_COLUMN,
|
| 409 |
-
MEAN_COLUMN,
|
| 410 |
-
PALETTE,
|
| 411 |
-
SITE_COLUMN,
|
| 412 |
-
SPECIES_GROUPS,
|
| 413 |
-
YEAR_COLUMNS,
|
| 414 |
-
)
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
@app.cell
|
| 418 |
-
def _(Path, data_path, mo, pd):
|
| 419 |
-
# ====== DATA loading ======
|
| 420 |
-
xlsx_path = Path(data_path.value)
|
| 421 |
-
if not xlsx_path.exists():
|
| 422 |
-
mo.stop(
|
| 423 |
-
True,
|
| 424 |
-
mo.md(
|
| 425 |
-
f'<div class="coffee-sites-notebook"><div class="card" style="padding:1rem 1.1rem;">'
|
| 426 |
-
f'<div class="section-title">Dataset not found</div>'
|
| 427 |
-
f'<div class="helper">The file <code>{xlsx_path}</code> does not exist. Update the input path above and rerun.</div>'
|
| 428 |
-
f'</div></div>'
|
| 429 |
-
),
|
| 430 |
-
)
|
| 431 |
-
|
| 432 |
-
site_df = pd.read_excel(xlsx_path, sheet_name="site_level_merged")
|
| 433 |
-
long_df = pd.read_excel(xlsx_path, sheet_name="all_data_long")
|
| 434 |
-
return long_df, site_df, xlsx_path
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
@app.cell
|
| 438 |
-
def _(long_df, mo, site_df):
|
| 439 |
-
mo.accordion(
|
| 440 |
-
{
|
| 441 |
-
"Data preview": mo.vstack(
|
| 442 |
-
[
|
| 443 |
-
mo.md(
|
| 444 |
-
f'<div class="coffee-sites-notebook helper">'
|
| 445 |
-
f'<strong>Loaded rows:</strong> {len(site_df)} site-level rows and {len(long_df)} long-table rows.'
|
| 446 |
-
f'</div>'
|
| 447 |
-
),
|
| 448 |
-
mo.hstack([site_df.head(), long_df.head()], widths=[1, 1]),
|
| 449 |
-
]
|
| 450 |
-
)
|
| 451 |
-
}
|
| 452 |
-
)
|
| 453 |
-
return
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
@app.cell
|
| 457 |
-
def _(
|
| 458 |
-
CSI_COLUMN,
|
| 459 |
-
MEAN_COLUMN,
|
| 460 |
-
SITE_COLUMN,
|
| 461 |
-
SPECIES_GROUPS,
|
| 462 |
-
YEAR_COLUMNS,
|
| 463 |
-
long_df,
|
| 464 |
-
math,
|
| 465 |
-
pd,
|
| 466 |
-
site_df,
|
| 467 |
-
):
|
| 468 |
-
def build_plot_table(site_df: pd.DataFrame, long_df: pd.DataFrame) -> pd.DataFrame:
|
| 469 |
-
df = site_df.copy()
|
| 470 |
-
|
| 471 |
-
# we group the data by site and then sum abundance values per site gving a summary row per side with
|
| 472 |
-
# the total abundance values
|
| 473 |
-
abundance_table = long_df.groupby(SITE_COLUMN).agg(
|
| 474 |
-
woody_amount=("Woody_abundance", "sum"),
|
| 475 |
-
herbaceous_amount=("Herbaceous_abundance", "sum"),
|
| 476 |
-
bryophyte_amount=("Bryophyte_frequency", "sum"),
|
| 477 |
-
)
|
| 478 |
-
# Merge the summed abundance values into the site-level table
|
| 479 |
-
# left_on=SITE_COLUMN means use the Site_ID column from df
|
| 480 |
-
# right_index=True means use the index of abundance_table (the grouped site IDs)
|
| 481 |
-
# how="left" means keep all rows from df even if some sites have missing abundance data
|
| 482 |
-
df = df.merge(abundance_table, left_on=SITE_COLUMN, right_index=True, how="left")
|
| 483 |
-
|
| 484 |
-
# Build a list of all columns that should be treated as numeric
|
| 485 |
-
# This includes yearly yields, mean yield, CSI, richness columns, and the new abundance-sum columns
|
| 486 |
-
numeric_cols = YEAR_COLUMNS + [MEAN_COLUMN, CSI_COLUMN] + [
|
| 487 |
-
g["richness_col"] for g in SPECIES_GROUPS
|
| 488 |
-
] + [g["sum_col"] for g in SPECIES_GROUPS]
|
| 489 |
-
|
| 490 |
-
# Convert all those columns to numeric values
|
| 491 |
-
# errors="coerce" means invalid values are turned into NaN instead of crashing
|
| 492 |
-
for col in numeric_cols:
|
| 493 |
-
df[col] = pd.to_numeric(df[col], errors="coerce")
|
| 494 |
-
|
| 495 |
-
# For each species group, create a standardized richness value between 0 and 1.
|
| 496 |
-
# This is used to place biodiversity bubbles horizontally in the chart.
|
| 497 |
-
# Bubble size is now controlled by abundance instead.
|
| 498 |
-
for group in SPECIES_GROUPS:
|
| 499 |
-
series = df[group["richness_col"]] # Get the richness values for this species group
|
| 500 |
-
min_val = series.min() # Find the minimum and maximum richness across all sites
|
| 501 |
-
max_val = series.max()
|
| 502 |
-
# If values are missing, or all values are the same,
|
| 503 |
-
# place everything at 0.5 (so in the middle of the axis) to avoid division errors
|
| 504 |
-
if pd.isna(min_val) or pd.isna(max_val) or math.isclose(min_val, max_val):
|
| 505 |
-
df[group["x_col"]] = 0.5
|
| 506 |
-
else:
|
| 507 |
-
# Standardize richness to the range 0–1 using min-max normalization
|
| 508 |
-
df[group["x_col"]] = (series - min_val) / (max_val - min_val)
|
| 509 |
-
# Sort sites from highest to lowest mean yield
|
| 510 |
-
# reset_index(drop=True) gives the table a clean new row index
|
| 511 |
-
df = df.sort_values(MEAN_COLUMN, ascending=False).reset_index(drop=True)
|
| 512 |
-
return df
|
| 513 |
-
|
| 514 |
-
plot_df = build_plot_table(site_df, long_df)
|
| 515 |
-
return (plot_df,)
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
@app.cell
|
| 519 |
-
def _(MEAN_COLUMN, SITE_COLUMN, SPECIES_GROUPS, mo, plot_df):
|
| 520 |
-
# ===== CALCULATE SUMMARY STATISTICS =====
|
| 521 |
-
|
| 522 |
-
n_sites = len(plot_df) # Total number of sites
|
| 523 |
-
yield_min = float(plot_df[MEAN_COLUMN].min()) # Minimum mean yield across all sites
|
| 524 |
-
yield_max = float(plot_df[MEAN_COLUMN].max()) # Maximum mean yield across all sites
|
| 525 |
-
|
| 526 |
-
# Find the largest species richness value across ALL species groups
|
| 527 |
-
# For each group, take its richness column, find its max, then take the overall max
|
| 528 |
-
richest = max(float(plot_df[g["richness_col"]].max()) for g in SPECIES_GROUPS)
|
| 529 |
-
|
| 530 |
-
# Get the site with the highest mean yield
|
| 531 |
-
# plot_df is already sorted (highest first), so we take the first row
|
| 532 |
-
# If there are no sites, show a dash instead
|
| 533 |
-
top_site = str(plot_df.iloc[0][SITE_COLUMN]) if n_sites else "—"
|
| 534 |
-
|
| 535 |
-
# ===== BUILD HTML FOR DISPLAY =====
|
| 536 |
-
# Create an HTML string using f-string formatting
|
| 537 |
-
# This inserts the calculated values directly into the HTML
|
| 538 |
-
stats_html = f"""
|
| 539 |
-
<div class="coffee-sites-notebook">
|
| 540 |
-
<div class="stat-grid">
|
| 541 |
-
<div class="card stat">
|
| 542 |
-
<div class="stat-label">Sites</div>
|
| 543 |
-
<div class="stat-value">{n_sites}</div>
|
| 544 |
-
<div class="stat-note">Sorted by mean coffee yield</div>
|
| 545 |
-
</div>
|
| 546 |
-
<div class="card stat">
|
| 547 |
-
<div class="stat-label">Top site</div>
|
| 548 |
-
<div class="stat-value">{top_site}</div>
|
| 549 |
-
<div class="stat-note">Highest mean yield in the figure</div>
|
| 550 |
-
</div>
|
| 551 |
-
<div class="card stat">
|
| 552 |
-
<div class="stat-label">Mean yield range</div>
|
| 553 |
-
<div class="stat-value">{yield_min:,.0f}–{yield_max:,.0f}</div>
|
| 554 |
-
<div class="stat-note">Across site-level means</div>
|
| 555 |
-
</div>
|
| 556 |
-
<div class="card stat">
|
| 557 |
-
<div class="stat-label">Largest richness</div>
|
| 558 |
-
<div class="stat-value">{richest:,.0f}</div>
|
| 559 |
-
<div class="stat-note">Maximum species richness encoded by bubble size</div>
|
| 560 |
-
</div>
|
| 561 |
-
</div>
|
| 562 |
-
</div>
|
| 563 |
-
"""
|
| 564 |
-
mo.Html(stats_html) # Needed to render the HTML in the marimo notebook
|
| 565 |
-
return
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
@app.cell
|
| 569 |
-
def _(math, pd):
|
| 570 |
-
def scale_linear(value: float, domain: tuple[float, float], rng: tuple[float, float]) -> float:
|
| 571 |
-
# Unpack the input range (domain) → the range of your data
|
| 572 |
-
d0, d1 = domain
|
| 573 |
-
|
| 574 |
-
# Unpack the output range → the range on the screen (so the pixel positions)
|
| 575 |
-
r0, r1 = rng
|
| 576 |
-
|
| 577 |
-
# If the value is missing (NaN), return NaN
|
| 578 |
-
# This prevents errors later when plotting
|
| 579 |
-
if pd.isna(value):
|
| 580 |
-
return float("nan")
|
| 581 |
-
|
| 582 |
-
# If the input range has no variation (min == max),
|
| 583 |
-
# avoid division by zero by placing the value in the middle of the output range
|
| 584 |
-
if math.isclose(d0, d1):
|
| 585 |
-
return (r0 + r1) / 2
|
| 586 |
-
|
| 587 |
-
# Perform linear scaling (min-max normalization + stretching to new range)
|
| 588 |
-
return r0 + (value - d0) * (r1 - r0) / (d1 - d0)
|
| 589 |
-
|
| 590 |
-
return (scale_linear,)
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
@app.cell
|
| 594 |
-
def _(
|
| 595 |
-
CSI_COLUMN,
|
| 596 |
-
MEAN_COLUMN,
|
| 597 |
-
PALETTE,
|
| 598 |
-
SITE_COLUMN,
|
| 599 |
-
SPECIES_GROUPS,
|
| 600 |
-
YEAR_COLUMNS,
|
| 601 |
-
np,
|
| 602 |
-
pd,
|
| 603 |
-
scale_linear,
|
| 604 |
-
svgwrite,
|
| 605 |
-
):
|
| 606 |
-
class CoffeeSiteFigure:
|
| 607 |
-
# this class is used for building the full SVG figure
|
| 608 |
-
def __init__(self, df: pd.DataFrame):
|
| 609 |
-
self.df = df
|
| 610 |
-
self.font = 'Inter, "Segoe UI", Arial, sans-serif' # Fonts used in the SVG text
|
| 611 |
-
self.tooltip_font = 'Inter, "Segoe UI", Arial, sans-serif'
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
self.margin = {"top": 110, "right": 44, "bottom": 118, "left": 104} # Margins around the full
|
| 615 |
-
# figure (top, right, bottom,
|
| 616 |
-
# left)
|
| 617 |
-
|
| 618 |
-
self.row_h = 18 # Vertical size of each site row
|
| 619 |
-
self.site_gap = 10 # Vertical gap between rows
|
| 620 |
-
self.panel_gap = 28 # Horizontal gap between the 3 panels
|
| 621 |
-
self.site_label_gap = 12 # Space between site labels and the first panel
|
| 622 |
-
self.year_bar_h = 3.4 # Heights of the year bars
|
| 623 |
-
self.mean_bar_h = 4.2 # Mean bar in panel 1
|
| 624 |
-
|
| 625 |
-
self.panel1_w = 340 # Width of panel 1
|
| 626 |
-
self.panel2_w = 190 # Width of panel 2
|
| 627 |
-
self.panel3_w = 390 # Width of panel 3
|
| 628 |
-
|
| 629 |
-
# Total figure width:
|
| 630 |
-
# left margin + panel 1 + gap + panel 2 + gap + panel 3 + right margin
|
| 631 |
-
self.width = (
|
| 632 |
-
self.margin["left"]
|
| 633 |
-
+ self.panel1_w
|
| 634 |
-
+ self.panel_gap
|
| 635 |
-
+ self.panel2_w
|
| 636 |
-
+ self.panel_gap
|
| 637 |
-
+ self.panel3_w
|
| 638 |
-
+ self.margin["right"]
|
| 639 |
-
)
|
| 640 |
-
|
| 641 |
-
# x-position where each panel begins
|
| 642 |
-
self.x1 = self.margin["left"]
|
| 643 |
-
self.x2 = self.x1 + self.panel1_w + self.panel_gap
|
| 644 |
-
self.x3 = self.x2 + self.panel2_w + self.panel_gap
|
| 645 |
-
|
| 646 |
-
# y-position for the panel header area
|
| 647 |
-
self.y0 = self.margin["top"]
|
| 648 |
-
|
| 649 |
-
# Top axis labels should sit below each panel title/subtitle,
|
| 650 |
-
# not above the panel header. The data rows start lower so the
|
| 651 |
-
# top axis does not overlap the first site row.
|
| 652 |
-
self.top_axis_label_y = self.y0 + 22
|
| 653 |
-
self.top_axis_line_y = self.y0 + 29
|
| 654 |
-
self.data_y0 = self.y0 + 38
|
| 655 |
-
|
| 656 |
-
# Height occupied by all site rows
|
| 657 |
-
self.rows_h = len(df) * (self.row_h + self.site_gap) - self.site_gap
|
| 658 |
-
|
| 659 |
-
# Bottom of the plotting area
|
| 660 |
-
self.plot_bottom = self.data_y0 + self.rows_h
|
| 661 |
-
|
| 662 |
-
# Total figure height:
|
| 663 |
-
# plot bottom + bottom margin
|
| 664 |
-
self.height = self.plot_bottom + self.margin["bottom"]
|
| 665 |
-
|
| 666 |
-
# Data ranges used for scaling values onto the chart
|
| 667 |
-
self.yield_min = 0
|
| 668 |
-
self.yield_max = float(np.nanmax(df[YEAR_COLUMNS + [MEAN_COLUMN]].to_numpy()))
|
| 669 |
-
|
| 670 |
-
# Fixed CSI scale used in panel 2
|
| 671 |
-
self.csi_min = 1
|
| 672 |
-
self.csi_max = 3
|
| 673 |
-
|
| 674 |
-
# Minimum and maximum bubble radius for biodiversity points
|
| 675 |
-
self.size_min = 4
|
| 676 |
-
self.size_max = 12
|
| 677 |
-
|
| 678 |
-
# Collect the abundance-total columns for all species groups
|
| 679 |
-
abundance_cols = [g["sum_col"] for g in SPECIES_GROUPS]
|
| 680 |
-
|
| 681 |
-
# Find the overall minimum and maximum abundance values across all groups.
|
| 682 |
-
# These values now control biodiversity bubble size.
|
| 683 |
-
self.abundance_min = float(df[abundance_cols].min().min())
|
| 684 |
-
self.abundance_max = float(df[abundance_cols].max().max())
|
| 685 |
-
|
| 686 |
-
# Return the vertical center position for a site row
|
| 687 |
-
def site_y(self, idx: int) -> float:
|
| 688 |
-
return self.data_y0 + idx * (self.row_h + self.site_gap) + self.row_h / 2
|
| 689 |
-
|
| 690 |
-
# Convert a yield value into an x-position inside panel 1
|
| 691 |
-
def yield_x(self, value: float) -> float:
|
| 692 |
-
return scale_linear(value, (self.yield_min, self.yield_max), (self.x1, self.x1 + self.panel1_w))
|
| 693 |
-
|
| 694 |
-
# Convert a CSI value into an x-position inside panel 2
|
| 695 |
-
def csi_x(self, value: float) -> float:
|
| 696 |
-
return scale_linear(value, (self.csi_min, self.csi_max), (self.x2, self.x2 + self.panel2_w))
|
| 697 |
-
|
| 698 |
-
# Convert a standardized biodiversity value (0 to 1) into an x-position inside panel 3
|
| 699 |
-
def bio_x(self, value: float) -> float:
|
| 700 |
-
return scale_linear(value, (0, 1), (self.x3, self.x3 + self.panel3_w))
|
| 701 |
-
|
| 702 |
-
# Convert an abundance value into a bubble radius
|
| 703 |
-
def bubble_r(self, value: float) -> float:
|
| 704 |
-
return scale_linear(value, (self.abundance_min, self.abundance_max), (self.size_min, self.size_max))
|
| 705 |
-
|
| 706 |
-
# Attach a tooltip/title to an SVG element --> shows up later on hover
|
| 707 |
-
def _add_title(self, element, text: str):
|
| 708 |
-
element.set_desc(title=text)
|
| 709 |
-
return element
|
| 710 |
-
|
| 711 |
-
# Main drawing method: creates the SVG and calls all sub-drawing methods
|
| 712 |
-
def draw(self, output_svg):
|
| 713 |
-
# Create an SVG drawing object with the full figure size
|
| 714 |
-
dwg = svgwrite.Drawing(str(output_svg), size=(self.width, self.height))
|
| 715 |
-
|
| 716 |
-
# Set the viewbox so the SVG scales properly
|
| 717 |
-
dwg.viewbox(0, 0, self.width, self.height)
|
| 718 |
-
|
| 719 |
-
# Add a CSS class so the SVG can be styled from the notebook CSS
|
| 720 |
-
dwg.attribs["class"] = "coffee-sites-svg"
|
| 721 |
-
|
| 722 |
-
# Embed the SVG styling directly in the exported SVG.
|
| 723 |
-
# This is important because downloaded SVG/PNG files cannot rely on
|
| 724 |
-
# the notebook page CSS. Without these embedded styles, some SVG
|
| 725 |
-
# viewers render class-based fills as black.
|
| 726 |
-
dwg.defs.add(
|
| 727 |
-
dwg.style(
|
| 728 |
-
"""
|
| 729 |
-
.coffee-sites-svg .row-line {
|
| 730 |
-
stroke: #ECE7DE;
|
| 731 |
-
stroke-width: 1;
|
| 732 |
-
}
|
| 733 |
-
.coffee-sites-svg .panel-card {
|
| 734 |
-
fill: #FFFDFA;
|
| 735 |
-
stroke: #E1D7C9;
|
| 736 |
-
stroke-width: 1;
|
| 737 |
-
}
|
| 738 |
-
.coffee-sites-svg .panel-cap {
|
| 739 |
-
fill: #F3ECE1;
|
| 740 |
-
}
|
| 741 |
-
.coffee-sites-svg .grid-line {
|
| 742 |
-
stroke: #D9D2C6;
|
| 743 |
-
stroke-width: 0.9;
|
| 744 |
-
stroke-dasharray: 4 4;
|
| 745 |
-
vector-effect: non-scaling-stroke;
|
| 746 |
-
}
|
| 747 |
-
.coffee-sites-svg .axis-line {
|
| 748 |
-
stroke: #5D554B;
|
| 749 |
-
stroke-width: 1.2;
|
| 750 |
-
vector-effect: non-scaling-stroke;
|
| 751 |
-
}
|
| 752 |
-
.coffee-sites-svg .yield-bar,
|
| 753 |
-
.coffee-sites-svg .csi-bar,
|
| 754 |
-
.coffee-sites-svg .bio-bubble {
|
| 755 |
-
cursor: pointer;
|
| 756 |
-
}
|
| 757 |
-
"""
|
| 758 |
-
)
|
| 759 |
-
)
|
| 760 |
-
|
| 761 |
-
# Draw a full solid background rectangle so exported SVG/PNG files
|
| 762 |
-
# never fall back to a transparent or black background.
|
| 763 |
-
dwg.add(
|
| 764 |
-
dwg.rect(
|
| 765 |
-
insert=(0, 0),
|
| 766 |
-
size=(self.width, self.height),
|
| 767 |
-
fill=PALETTE["background"],
|
| 768 |
-
stroke="none",
|
| 769 |
-
)
|
| 770 |
-
)
|
| 771 |
-
|
| 772 |
-
# Draw the different parts of the figure in order
|
| 773 |
-
self._draw_shell(dwg)
|
| 774 |
-
self._draw_title(dwg)
|
| 775 |
-
self._draw_panel_headers(dwg)
|
| 776 |
-
self._draw_reference_grid(dwg)
|
| 777 |
-
self._draw_vertical_axes(dwg)
|
| 778 |
-
self._draw_sites(dwg)
|
| 779 |
-
self._draw_legends(dwg)
|
| 780 |
-
self._draw_footer(dwg)
|
| 781 |
-
|
| 782 |
-
dwg.save()
|
| 783 |
-
|
| 784 |
-
# Draw background cards & panels behind the 3 chart sections
|
| 785 |
-
def _draw_shell(self, dwg):
|
| 786 |
-
# Keep one shared geometry for all three panels so their backgrounds,
|
| 787 |
-
# header bands, and plot areas line up exactly.
|
| 788 |
-
panel_top = self.y0 - 36
|
| 789 |
-
panel_bottom = self.plot_bottom + 18
|
| 790 |
-
panel_h = panel_bottom - panel_top
|
| 791 |
-
|
| 792 |
-
# Header band height is tall enough to contain title, subtitle,
|
| 793 |
-
# top-axis labels, and the top-axis line.
|
| 794 |
-
# It uses only top-rounded corners; the bottom edge is straight,
|
| 795 |
-
# which avoids uneven curved overlaps between the panel header boxes.
|
| 796 |
-
header_h = (self.top_axis_line_y + 8) - panel_top
|
| 797 |
-
corner_r = 16
|
| 798 |
-
|
| 799 |
-
panels = [
|
| 800 |
-
(self.x1 - 14, self.panel1_w + 28),
|
| 801 |
-
(self.x2 - 14, self.panel2_w + 28),
|
| 802 |
-
(self.x3 - 14, self.panel3_w + 28),
|
| 803 |
-
]
|
| 804 |
-
|
| 805 |
-
for x, w in panels:
|
| 806 |
-
# Main rounded panel body
|
| 807 |
-
dwg.add(
|
| 808 |
-
dwg.rect(
|
| 809 |
-
insert=(x, panel_top),
|
| 810 |
-
size=(w, panel_h),
|
| 811 |
-
rx=corner_r,
|
| 812 |
-
ry=corner_r,
|
| 813 |
-
class_="panel-card",
|
| 814 |
-
)
|
| 815 |
-
)
|
| 816 |
-
|
| 817 |
-
# Top header band with rounded top corners only and a straight bottom.
|
| 818 |
-
cap_path = dwg.path(
|
| 819 |
-
d=(
|
| 820 |
-
f"M {x},{panel_top + header_h} "
|
| 821 |
-
f"L {x},{panel_top + corner_r} "
|
| 822 |
-
f"Q {x},{panel_top} {x + corner_r},{panel_top} "
|
| 823 |
-
f"L {x + w - corner_r},{panel_top} "
|
| 824 |
-
f"Q {x + w},{panel_top} {x + w},{panel_top + corner_r} "
|
| 825 |
-
f"L {x + w},{panel_top + header_h} Z"
|
| 826 |
-
),
|
| 827 |
-
class_="panel-cap",
|
| 828 |
-
)
|
| 829 |
-
dwg.add(cap_path)
|
| 830 |
-
|
| 831 |
-
# Draw the big figure title at the top
|
| 832 |
-
def _draw_title(self, dwg):
|
| 833 |
-
dwg.add(
|
| 834 |
-
dwg.text(
|
| 835 |
-
"Coffee sites: yield, structure, and biodiversity",
|
| 836 |
-
insert=(self.margin["left"], 38),
|
| 837 |
-
font_size=24,
|
| 838 |
-
font_family=self.font,
|
| 839 |
-
font_weight=700,
|
| 840 |
-
fill=PALETTE["text"],
|
| 841 |
-
)
|
| 842 |
-
)
|
| 843 |
-
|
| 844 |
-
# Draw section headers above the 3 panels
|
| 845 |
-
def _draw_panel_headers(self, dwg):
|
| 846 |
-
headers = [
|
| 847 |
-
(self.x1, self.panel1_w, "Yield per site", "2017, 2018, 2019, and mean"),
|
| 848 |
-
(self.x2, self.panel2_w, "Coffee structure index", "Single bar per site"),
|
| 849 |
-
(self.x3, self.panel3_w, "Biodiversity", "x = standardized richness · size = abundance"),
|
| 850 |
-
]
|
| 851 |
-
for x, w, title, subtitle in headers:
|
| 852 |
-
# Header title
|
| 853 |
-
dwg.add(
|
| 854 |
-
dwg.text(
|
| 855 |
-
title,
|
| 856 |
-
insert=(x, self.y0 - 16),
|
| 857 |
-
font_size=13,
|
| 858 |
-
font_family=self.font,
|
| 859 |
-
font_weight=700,
|
| 860 |
-
fill=PALETTE["text"],
|
| 861 |
-
)
|
| 862 |
-
)
|
| 863 |
-
# Header subtitle
|
| 864 |
-
dwg.add(
|
| 865 |
-
dwg.text(
|
| 866 |
-
subtitle,
|
| 867 |
-
insert=(x, self.y0 - 2),
|
| 868 |
-
font_size=10.5,
|
| 869 |
-
font_family=self.font,
|
| 870 |
-
fill=PALETTE["muted"],
|
| 871 |
-
)
|
| 872 |
-
)
|
| 873 |
-
# Thin separator line under header
|
| 874 |
-
dwg.add(
|
| 875 |
-
dwg.line(
|
| 876 |
-
start=(x, self.y0 + 8),
|
| 877 |
-
end=(x + w, self.y0 + 8),
|
| 878 |
-
stroke=PALETTE["grid"],
|
| 879 |
-
stroke_width=1,
|
| 880 |
-
)
|
| 881 |
-
)
|
| 882 |
-
|
| 883 |
-
# Draw the vertical baseline axis for each panel
|
| 884 |
-
def _draw_vertical_axes(self, dwg):
|
| 885 |
-
y1 = self.top_axis_line_y
|
| 886 |
-
|
| 887 |
-
# Extend the vertical axes slightly below the plotting rows.
|
| 888 |
-
# This moves the arrowheads out of the data area so they do not
|
| 889 |
-
# overlap the last site row.
|
| 890 |
-
axis_extension = 16
|
| 891 |
-
y2 = self.plot_bottom + axis_extension
|
| 892 |
-
|
| 893 |
-
for x in [self.x1, self.x2, self.x3]:
|
| 894 |
-
dwg.add(dwg.line(start=(x, y1), end=(x, y2), class_="axis-line"))
|
| 895 |
-
dwg.add(
|
| 896 |
-
dwg.polygon(
|
| 897 |
-
points=[(x - 4, y2 - 8), (x + 4, y2 - 8), (x, y2)],
|
| 898 |
-
fill=PALETTE["axis"],
|
| 899 |
-
)
|
| 900 |
-
)
|
| 901 |
-
|
| 902 |
-
# Draw vertical reference/grid lines and tick labels in each panel
|
| 903 |
-
def _draw_reference_grid(self, dwg):
|
| 904 |
-
# Top axis baselines, placed below the panel title/subtitle
|
| 905 |
-
for x, w in [
|
| 906 |
-
(self.x1, self.panel1_w),
|
| 907 |
-
(self.x2, self.panel2_w),
|
| 908 |
-
(self.x3, self.panel3_w),
|
| 909 |
-
]:
|
| 910 |
-
dwg.add(
|
| 911 |
-
dwg.line(
|
| 912 |
-
start=(x, self.top_axis_line_y),
|
| 913 |
-
end=(x + w, self.top_axis_line_y),
|
| 914 |
-
class_="axis-line",
|
| 915 |
-
)
|
| 916 |
-
)
|
| 917 |
-
|
| 918 |
-
# Panel 1: yield ticks
|
| 919 |
-
for tick in np.linspace(self.yield_min, self.yield_max, 5):
|
| 920 |
-
x = self.yield_x(float(tick))
|
| 921 |
-
dwg.add(dwg.line(start=(x, self.top_axis_line_y), end=(x, self.plot_bottom), class_="grid-line"))
|
| 922 |
-
# Bottom axis label
|
| 923 |
-
dwg.add(
|
| 924 |
-
dwg.text(
|
| 925 |
-
f"{tick:,.0f}",
|
| 926 |
-
insert=(x, self.plot_bottom + 30),
|
| 927 |
-
text_anchor="middle",
|
| 928 |
-
font_size=10,
|
| 929 |
-
font_family=self.font,
|
| 930 |
-
fill=PALETTE["muted"],
|
| 931 |
-
)
|
| 932 |
-
)
|
| 933 |
-
|
| 934 |
-
# Top axis label
|
| 935 |
-
dwg.add(
|
| 936 |
-
dwg.text(
|
| 937 |
-
f"{tick:,.0f}",
|
| 938 |
-
insert=(x, self.top_axis_label_y),
|
| 939 |
-
text_anchor="middle",
|
| 940 |
-
font_size=10,
|
| 941 |
-
font_family=self.font,
|
| 942 |
-
fill=PALETTE["muted"],
|
| 943 |
-
)
|
| 944 |
-
)
|
| 945 |
-
|
| 946 |
-
# Panel 2: CSI ticks
|
| 947 |
-
for tick in [1, 2, 3]:
|
| 948 |
-
x = self.csi_x(tick)
|
| 949 |
-
dwg.add(dwg.line(start=(x, self.top_axis_line_y), end=(x, self.plot_bottom), class_="grid-line"))
|
| 950 |
-
# Bottom axis label
|
| 951 |
-
dwg.add(
|
| 952 |
-
dwg.text(
|
| 953 |
-
str(tick),
|
| 954 |
-
insert=(x, self.plot_bottom + 30),
|
| 955 |
-
text_anchor="middle",
|
| 956 |
-
font_size=10,
|
| 957 |
-
font_family=self.font,
|
| 958 |
-
fill=PALETTE["muted"],
|
| 959 |
-
)
|
| 960 |
-
)
|
| 961 |
-
|
| 962 |
-
# Top axis label
|
| 963 |
-
dwg.add(
|
| 964 |
-
dwg.text(
|
| 965 |
-
str(tick),
|
| 966 |
-
insert=(x, self.top_axis_label_y),
|
| 967 |
-
text_anchor="middle",
|
| 968 |
-
font_size=10,
|
| 969 |
-
font_family=self.font,
|
| 970 |
-
fill=PALETTE["muted"],
|
| 971 |
-
)
|
| 972 |
-
)
|
| 973 |
-
|
| 974 |
-
# Panel 3: biodiversity axis labels
|
| 975 |
-
for tick, label in zip([0, 0.5, 1.0], ["low", "mid", "high"]):
|
| 976 |
-
x = self.bio_x(tick)
|
| 977 |
-
dwg.add(dwg.line(start=(x, self.top_axis_line_y), end=(x, self.plot_bottom), class_="grid-line"))
|
| 978 |
-
# Bottom axis label
|
| 979 |
-
dwg.add(
|
| 980 |
-
dwg.text(
|
| 981 |
-
label,
|
| 982 |
-
insert=(x, self.plot_bottom + 30),
|
| 983 |
-
text_anchor="middle",
|
| 984 |
-
font_size=10,
|
| 985 |
-
font_family=self.font,
|
| 986 |
-
fill=PALETTE["muted"],
|
| 987 |
-
)
|
| 988 |
-
)
|
| 989 |
-
|
| 990 |
-
# Top axis label
|
| 991 |
-
dwg.add(
|
| 992 |
-
dwg.text(
|
| 993 |
-
label,
|
| 994 |
-
insert=(x, self.top_axis_label_y),
|
| 995 |
-
text_anchor="middle",
|
| 996 |
-
font_size=10,
|
| 997 |
-
font_family=self.font,
|
| 998 |
-
fill=PALETTE["muted"],
|
| 999 |
-
)
|
| 1000 |
-
)
|
| 1001 |
-
|
| 1002 |
-
# Draw all site rows: labels, yield bars, CSI bars, and biodiversity bubbles
|
| 1003 |
-
def _draw_sites(self, dwg):
|
| 1004 |
-
# Colors for the 3 yearly yield bars
|
| 1005 |
-
year_colors = [PALETTE["year_2017"], PALETTE["year_2018"], PALETTE["year_2019"]]
|
| 1006 |
-
bar_offsets = [-5.5, -1.7, 2.1, 6.2] # Small vertical offsets so the four bars do not overlap
|
| 1007 |
-
|
| 1008 |
-
# Loop through each site row in the dataframe
|
| 1009 |
-
for i, row in self.df.iterrows():
|
| 1010 |
-
# Vertical position for this site
|
| 1011 |
-
y = self.site_y(i)
|
| 1012 |
-
# Horizontal row separator line
|
| 1013 |
-
dwg.add(
|
| 1014 |
-
dwg.line(
|
| 1015 |
-
start=(self.x1 - 10, y),
|
| 1016 |
-
end=(self.x3 + self.panel3_w + 8, y),
|
| 1017 |
-
class_="row-line",
|
| 1018 |
-
)
|
| 1019 |
-
)
|
| 1020 |
-
|
| 1021 |
-
# Site label shown to the left of the first panel
|
| 1022 |
-
dwg.add(
|
| 1023 |
-
dwg.text(
|
| 1024 |
-
str(row[SITE_COLUMN]),
|
| 1025 |
-
insert=(self.x1 - self.site_label_gap, y + 3.6),
|
| 1026 |
-
text_anchor="end",
|
| 1027 |
-
font_size=10.5,
|
| 1028 |
-
font_family=self.font,
|
| 1029 |
-
font_weight=600,
|
| 1030 |
-
fill=PALETTE["text"],
|
| 1031 |
-
)
|
| 1032 |
-
)
|
| 1033 |
-
|
| 1034 |
-
# Gather yearly yield values plus mean yield
|
| 1035 |
-
values = [row[c] for c in YEAR_COLUMNS] + [row[MEAN_COLUMN]]
|
| 1036 |
-
|
| 1037 |
-
# Colors, heights, and text labels for those 4 bars
|
| 1038 |
-
colors = year_colors + [PALETTE["mean"]]
|
| 1039 |
-
heights = [self.year_bar_h, self.year_bar_h, self.year_bar_h, self.mean_bar_h]
|
| 1040 |
-
labels = ["2017", "2018", "2019", "Mean"]
|
| 1041 |
-
|
| 1042 |
-
# Draw each yield bar
|
| 1043 |
-
for v, color, h, off, label in zip(values, colors, heights, bar_offsets, labels):
|
| 1044 |
-
if pd.isna(v):
|
| 1045 |
-
continue
|
| 1046 |
-
x_end = self.yield_x(float(v))
|
| 1047 |
-
|
| 1048 |
-
# Draw horizontal bar from left axis to scaled x-position
|
| 1049 |
-
rect = dwg.rect(
|
| 1050 |
-
insert=(self.x1, y + off - h / 2),
|
| 1051 |
-
size=(max(0, x_end - self.x1), h),
|
| 1052 |
-
fill=color,
|
| 1053 |
-
rx=1.6,
|
| 1054 |
-
ry=1.6,
|
| 1055 |
-
class_="yield-bar",
|
| 1056 |
-
opacity=0.94,
|
| 1057 |
-
)
|
| 1058 |
-
|
| 1059 |
-
# Add tooltip text
|
| 1060 |
-
self._add_title(rect, f"Site {row[SITE_COLUMN]} · {label} yield: {float(v):,.2f}")
|
| 1061 |
-
dwg.add(rect)
|
| 1062 |
-
|
| 1063 |
-
# Draw the CSI bar
|
| 1064 |
-
csi = row[CSI_COLUMN]
|
| 1065 |
-
if not pd.isna(csi):
|
| 1066 |
-
x_end = self.csi_x(float(csi))
|
| 1067 |
-
rect = dwg.rect(
|
| 1068 |
-
insert=(self.x2, y - 3.9),
|
| 1069 |
-
size=(max(0, x_end - self.x2), 7.8),
|
| 1070 |
-
fill=PALETTE["csi"],
|
| 1071 |
-
rx=2,
|
| 1072 |
-
ry=2,
|
| 1073 |
-
class_="csi-bar",
|
| 1074 |
-
opacity=0.92,
|
| 1075 |
-
)
|
| 1076 |
-
self._add_title(rect, f"Site {row[SITE_COLUMN]} · Coffee structure index: {float(csi):.2f}")
|
| 1077 |
-
dwg.add(rect)
|
| 1078 |
-
|
| 1079 |
-
# Draw biodiversity bubbles for the 3 species groups
|
| 1080 |
-
# All biodiversity circles for a site are aligned on the same horizontal row.
|
| 1081 |
-
for group in SPECIES_GROUPS:
|
| 1082 |
-
richness = row[group["richness_col"]]
|
| 1083 |
-
abundance = row[group["sum_col"]]
|
| 1084 |
-
x_std = row[group["x_col"]]
|
| 1085 |
-
|
| 1086 |
-
# Skip missing values
|
| 1087 |
-
if pd.isna(richness) or pd.isna(abundance) or pd.isna(x_std):
|
| 1088 |
-
continue
|
| 1089 |
-
|
| 1090 |
-
# Draw one circle for this group
|
| 1091 |
-
bubble = dwg.circle(
|
| 1092 |
-
center=(self.bio_x(float(x_std)), y),
|
| 1093 |
-
r=self.bubble_r(float(abundance)),
|
| 1094 |
-
fill=group["color"],
|
| 1095 |
-
fill_opacity=0.78,
|
| 1096 |
-
stroke="white",
|
| 1097 |
-
stroke_width=1,
|
| 1098 |
-
class_="bio-bubble",
|
| 1099 |
-
)
|
| 1100 |
-
|
| 1101 |
-
# Add tooltip text with more detail
|
| 1102 |
-
self._add_title(
|
| 1103 |
-
bubble,
|
| 1104 |
-
(
|
| 1105 |
-
f"Site {row[SITE_COLUMN]} · {group['name']}\n"
|
| 1106 |
-
f"Standardized richness: {float(x_std):.2f}\n"
|
| 1107 |
-
f"Richness: {float(richness):.0f}\n"
|
| 1108 |
-
f"Abundance: {float(abundance):,.0f}"
|
| 1109 |
-
),
|
| 1110 |
-
)
|
| 1111 |
-
dwg.add(bubble)
|
| 1112 |
-
|
| 1113 |
-
# Draw the legends at the bottom of the figure
|
| 1114 |
-
def _draw_legends(self, dwg):
|
| 1115 |
-
# Give the legend a little more vertical space so labels do not overlap.
|
| 1116 |
-
# Row 1: year legend + biodiversity group legend.
|
| 1117 |
-
# Row 2: bubble-size key for the biodiversity panel.
|
| 1118 |
-
legend_y = self.height - 72
|
| 1119 |
-
bubble_key_y = self.height - 46
|
| 1120 |
-
|
| 1121 |
-
# Legend for yearly yield colors + mean
|
| 1122 |
-
for idx, (color, label) in enumerate([
|
| 1123 |
-
(PALETTE["year_2017"], "2017"),
|
| 1124 |
-
(PALETTE["year_2018"], "2018"),
|
| 1125 |
-
(PALETTE["year_2019"], "2019"),
|
| 1126 |
-
(PALETTE["mean"], "Mean"),
|
| 1127 |
-
]):
|
| 1128 |
-
x = self.x1 + idx * 62
|
| 1129 |
-
dwg.add(dwg.rect(insert=(x, legend_y - 10), size=(18, 8), fill=color, rx=2, ry=2))
|
| 1130 |
-
dwg.add(
|
| 1131 |
-
dwg.text(
|
| 1132 |
-
label,
|
| 1133 |
-
insert=(x + 24, legend_y - 3),
|
| 1134 |
-
font_size=10.5,
|
| 1135 |
-
font_family=self.font,
|
| 1136 |
-
fill=PALETTE["text"],
|
| 1137 |
-
)
|
| 1138 |
-
)
|
| 1139 |
-
|
| 1140 |
-
# Legend for the biodiversity groups
|
| 1141 |
-
# Use slightly tighter spacing and keep this on its own row.
|
| 1142 |
-
for idx, group in enumerate(SPECIES_GROUPS):
|
| 1143 |
-
x = self.x3 + idx * 100
|
| 1144 |
-
dwg.add(
|
| 1145 |
-
dwg.circle(
|
| 1146 |
-
center=(x + 7, legend_y - 5),
|
| 1147 |
-
r=5.5,
|
| 1148 |
-
fill=group["color"],
|
| 1149 |
-
fill_opacity=0.78,
|
| 1150 |
-
)
|
| 1151 |
-
)
|
| 1152 |
-
dwg.add(
|
| 1153 |
-
dwg.text(
|
| 1154 |
-
group["name"],
|
| 1155 |
-
insert=(x + 18, legend_y - 1.5),
|
| 1156 |
-
font_size=10.5,
|
| 1157 |
-
font_family=self.font,
|
| 1158 |
-
fill=PALETTE["text"],
|
| 1159 |
-
)
|
| 1160 |
-
)
|
| 1161 |
-
|
| 1162 |
-
# Bubble size key on a separate row to prevent overlap with the group labels.
|
| 1163 |
-
key_x = self.x3
|
| 1164 |
-
dwg.add(dwg.circle(center=(key_x + 7, bubble_key_y - 5), r=4.5, fill="none", stroke=PALETTE["muted"], stroke_width=1))
|
| 1165 |
-
dwg.add(dwg.circle(center=(key_x + 36, bubble_key_y - 5), r=9.5, fill="none", stroke=PALETTE["muted"], stroke_width=1))
|
| 1166 |
-
dwg.add(
|
| 1167 |
-
dwg.text(
|
| 1168 |
-
"Bubble size = abundance",
|
| 1169 |
-
insert=(key_x + 54, bubble_key_y - 1.5),
|
| 1170 |
-
font_size=10.5,
|
| 1171 |
-
font_family=self.font,
|
| 1172 |
-
fill=PALETTE["muted"],
|
| 1173 |
-
)
|
| 1174 |
-
)
|
| 1175 |
-
|
| 1176 |
-
# Draw small footer text at the bottom
|
| 1177 |
-
def _draw_footer(self, dwg):
|
| 1178 |
-
dwg.add(
|
| 1179 |
-
dwg.text(
|
| 1180 |
-
"Hover over bars and bubbles for exact values.",
|
| 1181 |
-
insert=(self.margin["left"], self.height - 18),
|
| 1182 |
-
font_size=10,
|
| 1183 |
-
font_family=self.font,
|
| 1184 |
-
fill=PALETTE["muted"],
|
| 1185 |
-
)
|
| 1186 |
-
)
|
| 1187 |
-
|
| 1188 |
-
return (CoffeeSiteFigure,)
|
| 1189 |
-
|
| 1190 |
-
|
| 1191 |
-
@app.cell
|
| 1192 |
-
def _(
|
| 1193 |
-
CoffeeSiteFigure,
|
| 1194 |
-
PALETTE,
|
| 1195 |
-
Path,
|
| 1196 |
-
export_png,
|
| 1197 |
-
mo,
|
| 1198 |
-
output_stem,
|
| 1199 |
-
plot_df,
|
| 1200 |
-
xlsx_path,
|
| 1201 |
-
):
|
| 1202 |
-
# output paths
|
| 1203 |
-
output_svg = Path(f"{output_stem.value}.svg")
|
| 1204 |
-
output_png = Path(f"{output_stem.value}.png")
|
| 1205 |
-
|
| 1206 |
-
# drawing the figure
|
| 1207 |
-
figure = CoffeeSiteFigure(plot_df)
|
| 1208 |
-
figure.draw(output_svg)
|
| 1209 |
-
|
| 1210 |
-
png_message = "PNG export skipped."
|
| 1211 |
-
if export_png.value:
|
| 1212 |
-
try:
|
| 1213 |
-
import cairosvg # type: ignore
|
| 1214 |
-
|
| 1215 |
-
cairosvg.svg2png(
|
| 1216 |
-
url=str(output_svg),
|
| 1217 |
-
write_to=str(output_png),
|
| 1218 |
-
dpi=220,
|
| 1219 |
-
background_color=PALETTE["background"],
|
| 1220 |
-
)
|
| 1221 |
-
png_message = f"PNG written to `{output_png}`"
|
| 1222 |
-
except Exception as exc: # pragma: no cover
|
| 1223 |
-
png_message = f"PNG export skipped: {exc}"
|
| 1224 |
-
|
| 1225 |
-
# Build the final HTML preview shown inside the notebook
|
| 1226 |
-
preview_html = f"""
|
| 1227 |
-
<div class="coffee-sites-notebook">
|
| 1228 |
-
<div class="card" style="padding: 1rem 1.1rem;">
|
| 1229 |
-
<div class="section-title">Final figure</div>
|
| 1230 |
-
<p class="helper download-note">
|
| 1231 |
-
Excel input: <code>{xlsx_path}</code><br>
|
| 1232 |
-
SVG written to <code>{output_svg}</code><br>
|
| 1233 |
-
{png_message}
|
| 1234 |
-
</p>
|
| 1235 |
-
<div class="figure-frame">
|
| 1236 |
-
{output_svg.read_text(encoding='utf-8')}
|
| 1237 |
-
</div>
|
| 1238 |
-
<div id="coffee-tooltip" class="coffee-tooltip"></div>
|
| 1239 |
-
<script>
|
| 1240 |
-
(function () {{
|
| 1241 |
-
const root = document.currentScript.closest('.coffee-sites-notebook');
|
| 1242 |
-
if (!root) return;
|
| 1243 |
-
const tooltip = root.querySelector('#coffee-tooltip');
|
| 1244 |
-
if (!tooltip) return;
|
| 1245 |
-
|
| 1246 |
-
const selectors = '.yield-bar, .csi-bar, .bio-bubble';
|
| 1247 |
-
const marks = root.querySelectorAll(selectors);
|
| 1248 |
-
|
| 1249 |
-
const showTooltip = (event) => {{
|
| 1250 |
-
const titleNode = event.currentTarget.querySelector('title');
|
| 1251 |
-
const text = titleNode ? titleNode.textContent : '';
|
| 1252 |
-
if (!text) return;
|
| 1253 |
-
tooltip.textContent = text;
|
| 1254 |
-
tooltip.style.opacity = '1';
|
| 1255 |
-
tooltip.style.left = `${{event.clientX}}px`;
|
| 1256 |
-
tooltip.style.top = `${{event.clientY}}px`;
|
| 1257 |
-
}};
|
| 1258 |
-
|
| 1259 |
-
const moveTooltip = (event) => {{
|
| 1260 |
-
tooltip.style.left = `${{event.clientX}}px`;
|
| 1261 |
-
tooltip.style.top = `${{event.clientY}}px`;
|
| 1262 |
-
}};
|
| 1263 |
-
|
| 1264 |
-
const hideTooltip = () => {{
|
| 1265 |
-
tooltip.style.opacity = '0';
|
| 1266 |
-
}};
|
| 1267 |
-
|
| 1268 |
-
marks.forEach((mark) => {{
|
| 1269 |
-
mark.addEventListener('mouseenter', showTooltip);
|
| 1270 |
-
mark.addEventListener('mousemove', moveTooltip);
|
| 1271 |
-
mark.addEventListener('mouseleave', hideTooltip);
|
| 1272 |
-
}});
|
| 1273 |
-
}})();
|
| 1274 |
-
</script>
|
| 1275 |
-
</div>
|
| 1276 |
-
</div>
|
| 1277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