wack0 commited on
Commit
3f1feba
·
verified ·
1 Parent(s): 4aa75e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -12,6 +12,11 @@ async def _():
12
  import random
13
  import marimo as mo
14
  import pandas as pd
 
 
 
 
 
15
  import openpyxl
16
 
17
  return io, math, mo, pd
@@ -53,10 +58,12 @@ def _(io, mo, pd, upload_decomp, upload_species, upload_yield):
53
 
54
  # Hardcoded column names used in the data files
55
  COL_SPECIES_NAME = "Species name"
56
- COL_SPECIES_YIELD = "Average coffee yield (kg ha-1)"
57
  COL_SPECIES_GROUP = "Species group"
58
  COL_DECOMP_SPECIES = df_decomposition.columns[0]
59
 
 
 
 
60
  # In `Plant_species_and_average_yield.xlsx`, empty group cells are filled
61
  df_species[COL_SPECIES_GROUP] = df_species[COL_SPECIES_GROUP].ffill()
62
  GROUPS = df_species[COL_SPECIES_GROUP].dropna().unique().tolist()
@@ -65,27 +72,28 @@ def _(io, mo, pd, upload_decomp, upload_species, upload_yield):
65
  df_decomposition.set_index(COL_DECOMP_SPECIES, inplace=True)
66
  ALL_SITES = [str(col) for col in df_decomposition.columns]
67
 
68
- ###### DATA EXTRACTION/PARSING ######
69
  # Build the species_data dictionary >> dictionary used to build the visual later
70
  species_data = []
71
  for idx, row in df_species.iterrows():
72
  sp_id = str(row[COL_SPECIES_NAME])
73
- avg_yield = float(row[COL_SPECIES_YIELD])
74
-
75
  group = str(row[COL_SPECIES_GROUP]) if COL_SPECIES_GROUP in df_species.columns else GROUPS[idx % len(GROUPS)]
76
 
 
77
  present_in = []
78
-
79
  if sp_id in df_decomposition.index:
80
  species_row = df_decomposition.loc[sp_id]
81
  sites_with_species = species_row[species_row == 1]
82
  present_in = sites_with_species.index.astype(str).tolist()
83
 
 
 
 
 
84
  species_data.append({
85
  "id": sp_id,
86
  "group": group,
87
  "yield": avg_yield,
88
- "num_sites": len(present_in),
89
  "sites": present_in
90
  })
91
 
@@ -93,7 +101,6 @@ def _(io, mo, pd, upload_decomp, upload_species, upload_yield):
93
  species_data.sort(key=lambda x: x["num_sites"], reverse=True)
94
  return ALL_SITES, GROUPS, species_data
95
 
96
-
97
  @app.cell
98
  def _(ALL_SITES, mo, species_data):
99
  # Create UI controls
 
12
  import random
13
  import marimo as mo
14
  import pandas as pd
15
+
16
+ if "pyodide" in sys.modules: # this is for running the resulting html visual in a browser (WebAssembly)
17
+ import micropip
18
+ await micropip.install("openpyxl")
19
+
20
  import openpyxl
21
 
22
  return io, math, mo, pd
 
58
 
59
  # Hardcoded column names used in the data files
60
  COL_SPECIES_NAME = "Species name"
 
61
  COL_SPECIES_GROUP = "Species group"
62
  COL_DECOMP_SPECIES = df_decomposition.columns[0]
63
 
64
+ # Build a site -> yield lookup from Coffee_yield.xlsx
65
+ site_yield_map = dict(zip(df_yield["Site ID"].astype(str), df_yield["Mean_CC_Yield"]))
66
+
67
  # In `Plant_species_and_average_yield.xlsx`, empty group cells are filled
68
  df_species[COL_SPECIES_GROUP] = df_species[COL_SPECIES_GROUP].ffill()
69
  GROUPS = df_species[COL_SPECIES_GROUP].dropna().unique().tolist()
 
72
  df_decomposition.set_index(COL_DECOMP_SPECIES, inplace=True)
73
  ALL_SITES = [str(col) for col in df_decomposition.columns]
74
 
 
75
  # Build the species_data dictionary >> dictionary used to build the visual later
76
  species_data = []
77
  for idx, row in df_species.iterrows():
78
  sp_id = str(row[COL_SPECIES_NAME])
 
 
79
  group = str(row[COL_SPECIES_GROUP]) if COL_SPECIES_GROUP in df_species.columns else GROUPS[idx % len(GROUPS)]
80
 
81
+ # First: find which sites this species occurs in
82
  present_in = []
 
83
  if sp_id in df_decomposition.index:
84
  species_row = df_decomposition.loc[sp_id]
85
  sites_with_species = species_row[species_row == 1]
86
  present_in = sites_with_species.index.astype(str).tolist()
87
 
88
+ # Then: compute mean yield across those sites using Coffee_yield.xlsx
89
+ site_yields = [site_yield_map[site] for site in present_in if site in site_yield_map]
90
+ avg_yield = sum(site_yields) / len(site_yields) if site_yields else 0.0
91
+
92
  species_data.append({
93
  "id": sp_id,
94
  "group": group,
95
  "yield": avg_yield,
96
+ "num_sites": len(present_in),
97
  "sites": present_in
98
  })
99
 
 
101
  species_data.sort(key=lambda x: x["num_sites"], reverse=True)
102
  return ALL_SITES, GROUPS, species_data
103
 
 
104
  @app.cell
105
  def _(ALL_SITES, mo, species_data):
106
  # Create UI controls