2020
2121from cybench .util .geo import get_shapes_from_polygons , world_shape_path
2222from cybench .config import KEY_LOC , KEY_TARGET
23- from cybench .evaluation .aggregated_metrics import calc_r_r2 , get_metrics_dict
23+ from cybench .evaluation .aggregated_metrics import (
24+ calc_median_yearly_r2 ,
25+ calc_r_r2 ,
26+ get_metrics_dict ,
27+ )
2428
2529# -----------------------------
2630# Configuration
@@ -208,21 +212,21 @@ def generate_markdown_table(stats_list: List[dict]) -> str:
208212
209213 md = (
210214 "| Dataset | N_regions | N_years | "
211- "Region-Year | Region-Year | Region-Year | "
215+ "Region-Year | Region-Year | Region-Year | Region-Year | "
212216 "Spatial | Spatial | "
213217 "Temporal | Temporal | "
214218 "Anomaly | Anomaly |\n "
215219 )
216220 md += (
217221 "| | | | "
218- "r | R² | NRMSE | "
222+ "r | R² | NRMSE | med R²/yr | "
219223 "r | R² | "
220224 "r | R² | "
221225 "r | R² |\n "
222226 )
223227 md += (
224228 "| :--- | ---: | ---: | "
225- "---: | ---: | ---: | "
229+ "---: | ---: | ---: | ---: | "
226230 "---: | ---: | "
227231 "---: | ---: | "
228232 "---: | ---: |\n "
@@ -255,6 +259,7 @@ def style(text):
255259 f"| { style (fmt (mod ['r' ]))} "
256260 f"| { style (fmt (mod ['r2' ]))} "
257261 f"| { style (fmt (mod ['nrmse' ]))} "
262+ f"| { style (fmt (s ['r2_yearly_median' ]))} "
258263 f"| { style (fmt (sp ['r' ]))} "
259264 f"| { style (fmt (sp ['r2' ]))} "
260265 f"| { style (fmt (s ['r_time_model' ]))} "
@@ -293,13 +298,13 @@ def fmt(val):
293298 <th class="left" rowspan="2">Dataset</th>
294299 <th rowspan="2">N_regions</th>
295300 <th rowspan="2">N_years</th>
296- <th colspan="3 ">Region-Year</th>
301+ <th colspan="4 ">Region-Year</th>
297302 <th colspan="2">Spatial</th>
298303 <th colspan="2">Temporal</th>
299304 <th colspan="2">Anomaly</th>
300305 </tr>
301306 <tr>
302- <th>r</th><th>R²</th><th>NRMSE</th>
307+ <th>r</th><th>R²</th><th>NRMSE</th><th>med R²/yr</th>
303308 <th>r</th><th>R²</th>
304309 <th>r</th><th>R²</th>
305310 <th>r</th><th>R²</th>
@@ -324,6 +329,7 @@ def fmt(val):
324329 f"<td>{ fmt (mod ['r' ])} </td>"
325330 f"<td>{ fmt (mod ['r2' ])} </td>"
326331 f"<td>{ fmt (mod ['nrmse' ])} </td>"
332+ f"<td>{ fmt (s ['r2_yearly_median' ])} </td>"
327333 f"<td>{ fmt (sp ['r' ])} </td>"
328334 f"<td>{ fmt (sp ['r2' ])} </td>"
329335 f"<td>{ fmt (s ['r_time_model' ])} </td>"
@@ -405,6 +411,7 @@ def generate_local_report_html(stats_list: List[dict], pdf_filename: str) -> str
405411 <td>{ m ['r' ]:.2f} </td>
406412 <td>{ m ['r2' ]:.2f} </td>
407413 <td>{ m ['nrmse' ]:.2f} </td>
414+ <td>{ s ['r2_yearly_median' ]:.2f} </td>
408415 <td>{ sp ['r' ]:.2f} </td>
409416 <td>{ sp ['r2' ]:.2f} </td>
410417 <td>{ s ['r_time_model' ]:.2f} </td>
@@ -445,13 +452,13 @@ def generate_local_report_html(stats_list: List[dict], pdf_filename: str) -> str
445452 <th rowspan="2">Dataset</th>
446453 <th rowspan="2">N_regions</th>
447454 <th rowspan="2">N_years</th>
448- <th colspan="3 ">Region-Year</th>
455+ <th colspan="4 ">Region-Year</th>
449456 <th colspan="2">Spatial</th>
450457 <th colspan="2">Temporal</th>
451458 <th colspan="2">Anomaly</th>
452459 </tr>
453460 <tr>
454- <th>r</th><th>R²</th><th>NRMSE</th>
461+ <th>r</th><th>R²</th><th>NRMSE</th><th>med R²/yr</th>
455462 <th>r</th><th>R²</th>
456463 <th>r</th><th>R²</th>
457464 <th>r</th><th>R²</th>
@@ -681,6 +688,7 @@ def process_dataset(
681688 _as_float_array (ts [KEY_TARGET ]),
682689 _as_float_array (ts [model ]),
683690 )
691+ r2_yearly_median = calc_median_yearly_r2 (df_filtered , KEY_TARGET , model )
684692
685693 # Spatial stats (mean over years per location)
686694 spatial = cast (pd .DataFrame , df_filtered .groupby (KEY_LOC )[[KEY_TARGET , model ]].mean ())
@@ -713,6 +721,7 @@ def process_dataset(
713721 "metrics_baseline" : metrics_base ,
714722 "r_time_model" : r_time_model ,
715723 "r2_time_model" : r2_time_model ,
724+ "r2_yearly_median" : r2_yearly_median ,
716725 "r_time_base" : r_time_base ,
717726 "metrics_spatial" : {"r" : r_spatial_model , "r2" : r2_spatial_model },
718727 }
0 commit comments