rguilcas commited on
Commit
91cb314
·
verified ·
1 Parent(s): 5cae983

Add ensemble members + update source files

Browse files
Files changed (1) hide show
  1. app.py +139 -80
app.py CHANGED
@@ -40,31 +40,42 @@ pn.config.loading_color = '#333333'
40
  pn.extension(design="material", sizing_mode="stretch_width", raw_css=[active_tab_css])
41
 
42
 
43
- @pn.cache()
44
  def get_data():
45
- ds1= xr.open_dataset("data/LENS_precomputed_terms_combined.nc")
46
  gdf_regions = gpd.read_file('data/rainfall_regions.geojson',).set_index('index')
47
- ds1 = ds1.assign_coords(region_id = gdf_regions.index.values)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  # Load precursor pattern GeoJSONs for each variable
49
  gdf_precursors_u850 = gpd.read_file('data/precursor_patterns_U850.json')
50
  gdf_precursors_v850 = gpd.read_file('data/precursor_patterns_V850.json')
51
  gdf_precursors_z500 = gpd.read_file('data/precursor_patterns_Z500.json')
52
- # ds1 = xr.concat([ds1,ds1.sum('source').assign_coords(source='all')], dim='source')
53
-
54
- # Rename and reorder sources
55
- mapping = {'dyn': 'Dynamical', 'therm': 'Conversion', 'nonlinear': 'Non Linear'}
56
- new_sources = [mapping.get(s, s) for s in ds1.source.values]
57
- ds1 = ds1.assign_coords(source=new_sources)
58
 
59
- desired_order = ['Dynamical', 'Conversion', 'Non Linear']
60
- # Reindex to desired order (intersection with existing to be safe)
61
- ds1 = ds1.reindex(source=[s for s in desired_order if s in ds1.source.values])
62
 
63
- return ds1, gdf_regions, gdf_precursors_u850, gdf_precursors_v850, gdf_precursors_z500
64
-
65
-
66
- ds1, gdf_regions, gdf_precursors_u850, gdf_precursors_v850, gdf_precursors_z500 = get_data()
67
 
 
68
 
69
 
70
 
@@ -136,27 +147,19 @@ def get_precursor_plot(season='SON', region='Egypt + East Libya'):
136
 
137
 
138
 
139
- @pn.cache(max_items=32)
140
  def compute_classified_data_v3(model, season, term, nonlinear_pos, source_tuple, min_bias=20, slope_val=0.2):
141
- """Cached computation of classified data with dynamic thresholds."""
142
  # 1. Select data components for all regions
143
- actual_term = 'trend' if term == 'Change in SSP3-7.0' else ('bias' if term == 'Bias from ERA5' else term)
144
-
145
- if actual_term == 'calibrated_trend':
146
- # Sum trend and spurious_trend
147
- ds_trend = ds1.sel(model=model, season=season, term='trend')
148
- ds_spurious = ds1.sel(model=model, season=season, term='spurious_trend')
149
- ds_model = ds_trend + ds_spurious
150
- else:
151
- ds_model = ds1.sel(model=model, season=season, term=actual_term)
152
-
153
- # Sum over synoptic bins and correct units
154
- ds_model = (ds_model.sum("synoptic_bin") / 0.05 * 100).individual_term
155
 
156
- # 2. Extract base components
157
- conversion = ds_model.sel(source='Conversion')
158
- dynamical = ds_model.sel(source='Dynamical')
159
- nonlinear = ds_model.sel(source='Non Linear')
160
 
161
  # 3. Determine X and Y for classification (based on nonlinear_pos setting)
162
  if nonlinear_pos == 'Dynamical':
@@ -206,13 +209,16 @@ class CMIPApp(param.Parameterized):
206
  model = param.Selector(default='CESM2', objects=list(ds1.model.values))
207
  selected_models = param.ListSelector(default=list(ds1.model.values), objects=list(ds1.model.values), label="Models to include")
208
  season = param.Selector(default='DJF', objects=['DJF', 'MAM', 'JJA', 'SON'])
209
- term = param.Selector(default='Bias from ERA5', objects=['Bias from ERA5', 'Change in SSP3-7.0', 'calibrated_trend'])
210
- source = param.ListSelector(default=['Conversion'], objects=list(ds1.source.values), label=f'Source of bias/change to plot')
211
  nonlinear_pos = param.Selector(default='Dynamical', objects=['Dynamical', 'Conversion'], label="Non-linear term with:")
212
  vmax = param.Integer(default=100, bounds=(1, 150), label="Colormap Saturation Value")
213
  min_bias_threshold = param.Number(default=20, bounds=(1, 100), label=f"Minimal Bias/Change Threshold (%)")
214
  slope_control = param.Number(default=0.2, bounds=(0.01, 0.5), label="Sector Slope (shallow)")
215
 
 
 
 
216
  # Master selection state
217
  _selection_index = param.List(default=[])
218
  # User-facing region selector for sidebar
@@ -250,6 +256,10 @@ class CMIPApp(param.Parameterized):
250
  # Link sidebar region selector to selection index
251
  self.param.watch(self._sync_from_sidebar, 'selected_regions')
252
  self.param.watch(self._sync_to_sidebar, '_selection_index')
 
 
 
 
253
 
254
  # To share actual Bokeh range objects for linked axes across tabs
255
  self._shared_x_range = None
@@ -293,13 +303,15 @@ class CMIPApp(param.Parameterized):
293
  source_tuple, self.min_bias_threshold, self.slope_control
294
  )
295
 
296
- @param.depends('model', 'season', 'term', 'source', 'vmax', 'min_bias_threshold', 'slope_control', '_selection_index')
297
  def get_value_map(self):
 
 
298
  return self._render_map_base(self.term, 'RdBu', (-self.vmax, self.vmax),
299
  f"{' + '.join(self.source)} {self.term} (%)",
300
  self.v_map_s, self.map_r)
301
 
302
- @param.depends('model', 'season', 'term', 'source', 'vmax', 'min_bias_threshold', 'slope_control', '_selection_index')
303
  def get_type_map(self):
304
  return self._render_map_base('Type', self.sector_cmap,
305
  title=f"{self.term.capitalize()} Classification: {self.model} {self.season}",
@@ -332,18 +344,22 @@ class CMIPApp(param.Parameterized):
332
  df_class = self._get_classified_data()
333
  gdf = gdf_regions.copy()
334
 
335
- # Match data to the GeoDataFrame
336
- gdf[self.term] = df_class.set_index('Region')['Selected_Sum']
337
- gdf['Conversion'] = df_class.set_index('Region')['Conversion']
338
- gdf['Dynamical'] = df_class.set_index('Region')['Dynamical']
339
- gdf['Non_Linear'] = df_class.set_index('Region')['Non_Linear']
340
- if 'Total' in df_class.columns:
341
- gdf['Total'] = df_class.set_index('Region')['Total']
 
 
 
342
  else:
343
- # Fallback if cache still persistent
344
- gdf['Total'] = gdf['Conversion'] + gdf['Dynamical'] + gdf['Non_Linear']
345
- gdf['Type'] = df_class.set_index('Region')['Type']
346
- gdf['Text_Color'] = df_class.set_index('Region')['Text_Color']
 
347
 
348
  gdf.index = gdf.index.rename('Region_Name')
349
  gdf = gdf.reset_index()
@@ -464,8 +480,8 @@ class CMIPApp(param.Parameterized):
464
  pass
465
 
466
 
467
- @param.depends('_selection_index', 'model', 'season', 'term', 'min_bias_threshold', 'slope_control')
468
- def get_secondary_plot(self):
469
  if not self._selection_index:
470
  return "### Please select a region"
471
 
@@ -473,11 +489,7 @@ class CMIPApp(param.Parameterized):
473
  regions = gdf_regions.iloc[region_iloc_idxs].index.tolist()
474
 
475
  # Select data for all sources because bars typically show decomposition
476
- ds_sel_barplot = (
477
- ds1
478
- .sel(model=self.model, season=self.season, term=self.term, region_id=regions)
479
- .sum("synoptic_bin") / 0.05 * 100
480
- ).individual_term
481
 
482
  # Convert to tidy DataFrame for easier plotting with tooltips
483
  df = ds_sel_barplot.to_dataframe(name=self.term).reset_index()
@@ -629,9 +641,9 @@ class CMIPApp(param.Parameterized):
629
  responsive=True,
630
  aspect=0.85,
631
  hooks=[self.hook_scatter_selection],
632
- # Set initial view to (-100, 100)
633
- xlim=(-100, 100),
634
- ylim=(-100, 100)
635
  )
636
  if current_x_range and all(x is not None for x in current_x_range):
637
  opts['xlim'] = current_x_range
@@ -667,7 +679,7 @@ class CMIPApp(param.Parameterized):
667
  if 'source' in plot.handles:
668
  plot.handles['source'].selected.indices = self._selection_index or []
669
 
670
- @param.depends('_selection_index', 'season', 'term')
671
  def get_bar_plot(self):
672
  if not self._selection_index:
673
  return pn.pane.Markdown(f"### {self.term.capitalize()} Decomposition: No region selected\n*Click on a map or scatter point to see the breakdown*")
@@ -728,7 +740,7 @@ class CMIPApp(param.Parameterized):
728
  invert_axes=True,
729
  color='Component',
730
  cmap=bar_cmap,
731
- xlim=(-100, 100)
732
  )
733
 
734
  # Add vertical lines at vmax and -vmax to show colormap extent
@@ -777,7 +789,7 @@ class CMIPApp(param.Parameterized):
777
  self.sel_map_s.source = plot
778
  return plot
779
 
780
- @param.depends('_selection_index', 'season', 'term', 'selected_models')
781
  def get_multi_model_bar_plot(self):
782
  if not self._selection_index:
783
  return pn.pane.Markdown(f"### {self.term.capitalize()} Multi-model Summary: No region selected\n*Select regions on the map to see the model comparison*")
@@ -794,12 +806,8 @@ class CMIPApp(param.Parameterized):
794
 
795
  tabs = []
796
  for region_idx, region_name in enumerate(region_names):
797
- # Select data for current season and term, and THIS region
798
- actual_term = 'trend' if self.term == 'Change in SSP3-7.0' else ('bias' if self.term == 'Bias from ERA5' else self.term)
799
- ds_region = (
800
- ds1.sel(model=self.selected_models, season=self.season, term=actual_term, region_id=region_name)
801
- .sum("synoptic_bin") / 0.05 * 100
802
- ).individual_term
803
 
804
  # Convert to tidy DataFrame
805
  df = ds_region.to_dataframe(name='Value').reset_index()
@@ -859,7 +867,8 @@ class CMIPApp(param.Parameterized):
859
 
860
  return pn.Tabs(*tabs, sizing_mode='scale_width', margin=(10, 0, 0, 0), dynamic=False)
861
 
862
- def get_multi_model_scatter_plot(self, ds_region, region):
 
863
  # 1. Extract base components
864
  conversion = ds_region.sel(source='Conversion')
865
  dynamical = ds_region.sel(source='Dynamical')
@@ -868,16 +877,16 @@ class CMIPApp(param.Parameterized):
868
  # 2. Determine X and Y for classification
869
  if self.nonlinear_pos == 'Dynamical':
870
  x_val = conversion
871
- y_val = dynamical + nonlinear
872
  x_label = f"Conversion {self.term} (%)"
873
  y_label = f"Dynamical + Non Linear {self.term} (%)"
874
  else:
875
- x_val = conversion + nonlinear
876
  y_val = dynamical
877
  x_label = f"Conversion + Non Linear {self.term} (%)"
878
  y_label = f"Dynamical {self.term} (%)"
879
 
880
- # 3. Create DataFrame
881
  df = pd.DataFrame({
882
  'model': ds_region.model.values,
883
  'Conversion': conversion.values,
@@ -916,29 +925,77 @@ class CMIPApp(param.Parameterized):
916
  """
917
  hover = HoverTool(tooltips=tooltips)
918
 
919
- # 6. Scatter plot
920
  scatter = df.hvplot.scatter(
921
  x='X', y='Y',
922
  hover_cols=['model', 'X', 'Y', 'Conversion', 'Dynamical', 'Non_Linear', 'Total', 'Type'],
923
  tools=['pan', 'wheel_zoom', 'save', 'reset', hover],
924
  color='model',
925
  cmap='Category20',
926
- size=100,
927
  line_color='black',
928
- line_width=0.8,
929
  legend=True
930
  ).opts(
931
  title=f"Multi-model {self.term.capitalize()} Classification ({self.season})",
932
  xlabel=x_label,
933
  ylabel=y_label,
934
- xlim=(-100, 100),
935
- ylim=(-100, 100),
936
  show_legend=True,
937
  legend_position='right',
938
  nonselection_alpha=1.0,
939
  nonselection_line_alpha=1.0
940
  )
941
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
942
  background = self.get_scatter_background()
943
  return (background * scatter).opts(active_tools=['pan', 'wheel_zoom'], margin=0)
944
 
@@ -1052,7 +1109,9 @@ class CMIPApp(param.Parameterized):
1052
 
1053
  def get_multi_model_sidebar(self):
1054
  return pn.Column(
1055
- pn.Param(self.param, parameters=['season', 'term'], show_name=False, margin=(10, 5)),
 
 
1056
  pn.pane.Markdown("### Models", margin=(10, 5, 0, 5)),
1057
  pn.widgets.CheckBoxGroup.from_param(self.param.selected_models, margin=(0, 5, 10, 5)),
1058
  pn.Tabs(
@@ -1098,8 +1157,8 @@ class CMIPApp(param.Parameterized):
1098
  )
1099
 
1100
  map_tabs = pn.Tabs(
1101
- (f'{self.term.capitalize()} values', make_tab_row(self.get_value_map)),
1102
- (f'Type of {self.term}', make_tab_row(self.get_type_map)),
1103
  margin=(0, 0, 0, 20),
1104
  sizing_mode='scale_width',
1105
  max_width=1200
 
40
  pn.extension(design="material", sizing_mode="stretch_width", raw_css=[active_tab_css])
41
 
42
 
 
43
  def get_data():
 
44
  gdf_regions = gpd.read_file('data/rainfall_regions.geojson',).set_index('index')
45
+ region_names = gdf_regions.index.values
46
+
47
+ def process_csv(path):
48
+ df = pd.read_csv(path)
49
+ # Map source names
50
+ mapping = {'conversion': 'Conversion', 'dynamical': 'Dynamical', 'nonlinear': 'Non Linear'}
51
+ df['source'] = df['source'].map(mapping)
52
+ # Map region_id (1-based index) to region names
53
+ df['region_id'] = df['region_id'].apply(lambda x: region_names[x-1] if 0 < x <= len(region_names) else 'Unknown')
54
+
55
+ # Determine pivot columns (member exists in ensemble file)
56
+ pivot_cols = ['model', 'season', 'region_id', 'source', 'term']
57
+ if 'member' in df.columns:
58
+ pivot_cols.append('member')
59
+
60
+ # Convert to Xarray
61
+ obj = df.set_index(pivot_cols)['value'].to_xarray()
62
+
63
+ # Scale units (new CSV data is already aggregated but needs % scaling to match 100% total roughly)
64
+ # Multiply by 10000 (100 * 100) as per user request to reach expected percentage scale
65
+ return obj / 0.05*100
66
+
67
+ ds1 = process_csv("data/LENS_paper_terms_df.csv")
68
+ ds_ens = process_csv("data/LENS_paper_terms_ens_df.csv")
69
+
70
  # Load precursor pattern GeoJSONs for each variable
71
  gdf_precursors_u850 = gpd.read_file('data/precursor_patterns_U850.json')
72
  gdf_precursors_v850 = gpd.read_file('data/precursor_patterns_V850.json')
73
  gdf_precursors_z500 = gpd.read_file('data/precursor_patterns_Z500.json')
 
 
 
 
 
 
74
 
75
+ return ds1, ds_ens, gdf_regions, gdf_precursors_u850, gdf_precursors_v850, gdf_precursors_z500
 
 
76
 
 
 
 
 
77
 
78
+ ds1, ds_ens, gdf_regions, gdf_precursors_u850, gdf_precursors_v850, gdf_precursors_z500 = get_data()
79
 
80
 
81
 
 
147
 
148
 
149
 
 
150
  def compute_classified_data_v3(model, season, term, nonlinear_pos, source_tuple, min_bias=20, slope_val=0.2):
151
+ """Computation of classified data with dynamic thresholds."""
152
  # 1. Select data components for all regions
153
+ try:
154
+ ds_model = ds1.sel(model=model, season=season, term=term)
155
+ except Exception:
156
+ # Return empty DataFrame if selection fails
157
+ return pd.DataFrame(columns=['Region', 'Conversion', 'Dynamical', 'Non_Linear', 'X', 'Y', 'Selected_Sum', 'Total', 'Type', 'Text_Color'])
 
 
 
 
 
 
 
158
 
159
+ # 2. Extract base components and handle NaNs
160
+ conversion = ds_model.sel(source='Conversion').fillna(0)
161
+ dynamical = ds_model.sel(source='Dynamical').fillna(0)
162
+ nonlinear = ds_model.sel(source='Non Linear').fillna(0)
163
 
164
  # 3. Determine X and Y for classification (based on nonlinear_pos setting)
165
  if nonlinear_pos == 'Dynamical':
 
209
  model = param.Selector(default='CESM2', objects=list(ds1.model.values))
210
  selected_models = param.ListSelector(default=list(ds1.model.values), objects=list(ds1.model.values), label="Models to include")
211
  season = param.Selector(default='DJF', objects=['DJF', 'MAM', 'JJA', 'SON'])
212
+ term = param.Selector(default='bias', objects={'Bias from ERA5': 'bias', 'Change in SSP3-7.0': 'change', 'Uncalibrated Change': 'uncalibrated change'})
213
+ source = param.ListSelector(default=['Conversion'], objects=['Dynamical', 'Conversion', 'Non Linear'], label=f'Source of bias/change to plot')
214
  nonlinear_pos = param.Selector(default='Dynamical', objects=['Dynamical', 'Conversion'], label="Non-linear term with:")
215
  vmax = param.Integer(default=100, bounds=(1, 150), label="Colormap Saturation Value")
216
  min_bias_threshold = param.Number(default=20, bounds=(1, 100), label=f"Minimal Bias/Change Threshold (%)")
217
  slope_control = param.Number(default=0.2, bounds=(0.01, 0.5), label="Sector Slope (shallow)")
218
 
219
+ # Toggle for ensemble members in multi-model tab
220
+ show_ensemble_members = param.Boolean(default=False, label="Show Ensemble Members")
221
+
222
  # Master selection state
223
  _selection_index = param.List(default=[])
224
  # User-facing region selector for sidebar
 
256
  # Link sidebar region selector to selection index
257
  self.param.watch(self._sync_from_sidebar, 'selected_regions')
258
  self.param.watch(self._sync_to_sidebar, '_selection_index')
259
+
260
+ # Ensure initial selector state matches data
261
+ self.param.selected_models.objects = list(ds1.model.values)
262
+ self.param.model.objects = list(ds1.model.values)
263
 
264
  # To share actual Bokeh range objects for linked axes across tabs
265
  self._shared_x_range = None
 
303
  source_tuple, self.min_bias_threshold, self.slope_control
304
  )
305
 
306
+ @param.depends('model', 'season', 'term', 'source', 'vmax', 'min_bias_threshold', 'slope_control', '_selection_index', 'nonlinear_pos')
307
  def get_value_map(self):
308
+ # Debug print to help identify issues in single model view
309
+ # print(f"DEBUG: get_value_map for {self.model} {self.season} {self.term}")
310
  return self._render_map_base(self.term, 'RdBu', (-self.vmax, self.vmax),
311
  f"{' + '.join(self.source)} {self.term} (%)",
312
  self.v_map_s, self.map_r)
313
 
314
+ @param.depends('model', 'season', 'term', 'source', 'vmax', 'min_bias_threshold', 'slope_control', '_selection_index', 'nonlinear_pos')
315
  def get_type_map(self):
316
  return self._render_map_base('Type', self.sector_cmap,
317
  title=f"{self.term.capitalize()} Classification: {self.model} {self.season}",
 
344
  df_class = self._get_classified_data()
345
  gdf = gdf_regions.copy()
346
 
347
+ # Use more robust mapping to ensure region alignment
348
+ if not df_class.empty:
349
+ df_to_map = df_class.set_index('Region')
350
+ gdf[self.term] = gdf.index.map(df_to_map['Selected_Sum']).fillna(0)
351
+ gdf['Conversion'] = gdf.index.map(df_to_map['Conversion']).fillna(0)
352
+ gdf['Dynamical'] = gdf.index.map(df_to_map['Dynamical']).fillna(0)
353
+ gdf['Non_Linear'] = gdf.index.map(df_to_map['Non_Linear']).fillna(0)
354
+ gdf['Total'] = gdf.index.map(df_to_map['Total']).fillna(0)
355
+ gdf['Type'] = gdf.index.map(df_to_map['Type']).fillna('Minimal')
356
+ gdf['Text_Color'] = gdf.index.map(df_to_map['Text_Color']).fillna('grey')
357
  else:
358
+ # Fallback for empty data
359
+ for col in [self.term, 'Conversion', 'Dynamical', 'Non_Linear', 'Total']:
360
+ gdf[col] = 0
361
+ gdf['Type'] = 'Minimal'
362
+ gdf['Text_Color'] = 'grey'
363
 
364
  gdf.index = gdf.index.rename('Region_Name')
365
  gdf = gdf.reset_index()
 
480
  pass
481
 
482
 
483
+ @param.depends('_selection_index', 'model', 'season', 'term', 'min_bias_threshold', 'slope_control', 'vmax', 'nonlinear_pos')
484
+ def get_secondary_plot(self, **kwargs):
485
  if not self._selection_index:
486
  return "### Please select a region"
487
 
 
489
  regions = gdf_regions.iloc[region_iloc_idxs].index.tolist()
490
 
491
  # Select data for all sources because bars typically show decomposition
492
+ ds_sel_barplot = ds1.sel(model=self.model, season=self.season, term=self.term, region_id=regions)
 
 
 
 
493
 
494
  # Convert to tidy DataFrame for easier plotting with tooltips
495
  df = ds_sel_barplot.to_dataframe(name=self.term).reset_index()
 
641
  responsive=True,
642
  aspect=0.85,
643
  hooks=[self.hook_scatter_selection],
644
+ # Set initial view to (-vmax, vmax)
645
+ xlim=(-self.vmax, self.vmax),
646
+ ylim=(-self.vmax, self.vmax)
647
  )
648
  if current_x_range and all(x is not None for x in current_x_range):
649
  opts['xlim'] = current_x_range
 
679
  if 'source' in plot.handles:
680
  plot.handles['source'].selected.indices = self._selection_index or []
681
 
682
+ @param.depends('_selection_index', 'model', 'season', 'term', 'vmax', 'nonlinear_pos', 'min_bias_threshold', 'slope_control')
683
  def get_bar_plot(self):
684
  if not self._selection_index:
685
  return pn.pane.Markdown(f"### {self.term.capitalize()} Decomposition: No region selected\n*Click on a map or scatter point to see the breakdown*")
 
740
  invert_axes=True,
741
  color='Component',
742
  cmap=bar_cmap,
743
+ xlim=(-self.vmax, self.vmax)
744
  )
745
 
746
  # Add vertical lines at vmax and -vmax to show colormap extent
 
789
  self.sel_map_s.source = plot
790
  return plot
791
 
792
+ @param.depends('_selection_index', 'season', 'term', 'selected_models', 'show_ensemble_members', 'nonlinear_pos', 'min_bias_threshold', 'slope_control', 'vmax')
793
  def get_multi_model_bar_plot(self):
794
  if not self._selection_index:
795
  return pn.pane.Markdown(f"### {self.term.capitalize()} Multi-model Summary: No region selected\n*Select regions on the map to see the model comparison*")
 
806
 
807
  tabs = []
808
  for region_idx, region_name in enumerate(region_names):
809
+ # Select data for current season and term (self.term already matches CSV keys)
810
+ ds_region = ds1.sel(model=self.selected_models, season=self.season, term=self.term, region_id=region_name)
 
 
 
 
811
 
812
  # Convert to tidy DataFrame
813
  df = ds_region.to_dataframe(name='Value').reset_index()
 
867
 
868
  return pn.Tabs(*tabs, sizing_mode='scale_width', margin=(10, 0, 0, 0), dynamic=False)
869
 
870
+ @param.depends('nonlinear_pos', 'min_bias_threshold', 'slope_control', 'term', 'vmax', 'show_ensemble_members')
871
+ def get_multi_model_scatter_plot(self, ds_region, region, **kwargs):
872
  # 1. Extract base components
873
  conversion = ds_region.sel(source='Conversion')
874
  dynamical = ds_region.sel(source='Dynamical')
 
877
  # 2. Determine X and Y for classification
878
  if self.nonlinear_pos == 'Dynamical':
879
  x_val = conversion
880
+ y_val = dynamical.fillna(0) + nonlinear.fillna(0)
881
  x_label = f"Conversion {self.term} (%)"
882
  y_label = f"Dynamical + Non Linear {self.term} (%)"
883
  else:
884
+ x_val = conversion.fillna(0) + nonlinear.fillna(0)
885
  y_val = dynamical
886
  x_label = f"Conversion + Non Linear {self.term} (%)"
887
  y_label = f"Dynamical {self.term} (%)"
888
 
889
+ # 3. Create DataFrame for model means
890
  df = pd.DataFrame({
891
  'model': ds_region.model.values,
892
  'Conversion': conversion.values,
 
925
  """
926
  hover = HoverTool(tooltips=tooltips)
927
 
928
+ # 6. Scatter plot of model means
929
  scatter = df.hvplot.scatter(
930
  x='X', y='Y',
931
  hover_cols=['model', 'X', 'Y', 'Conversion', 'Dynamical', 'Non_Linear', 'Total', 'Type'],
932
  tools=['pan', 'wheel_zoom', 'save', 'reset', hover],
933
  color='model',
934
  cmap='Category20',
935
+ size=120,
936
  line_color='black',
937
+ line_width=1.0,
938
  legend=True
939
  ).opts(
940
  title=f"Multi-model {self.term.capitalize()} Classification ({self.season})",
941
  xlabel=x_label,
942
  ylabel=y_label,
943
+ xlim=(-max(120, self.vmax), max(120, self.vmax)),
944
+ ylim=(-max(120, self.vmax), max(120, self.vmax)),
945
  show_legend=True,
946
  legend_position='right',
947
  nonselection_alpha=1.0,
948
  nonselection_line_alpha=1.0
949
  )
950
 
951
+ # 7. Add ensemble members if toggled
952
+ if self.show_ensemble_members:
953
+ # Select ensemble data for this region, season, term, and models
954
+ ds_ens_sub = ds_ens.sel(model=self.selected_models, season=self.season, term=self.term, region_id=region)
955
+
956
+ # Extract components and handle NaNs
957
+ c_ens = ds_ens_sub.sel(source='Conversion').fillna(0)
958
+ d_ens = ds_ens_sub.sel(source='Dynamical').fillna(0)
959
+ n_ens = ds_ens_sub.sel(source='Non Linear').fillna(0)
960
+
961
+ if self.nonlinear_pos == 'Dynamical':
962
+ x_ens = c_ens
963
+ y_ens = d_ens + n_ens
964
+ else:
965
+ x_ens = c_ens + n_ens
966
+ y_ens = d_ens
967
+
968
+ # Create ensemble DataFrame in a robust way
969
+ df_ens_x = x_ens.to_dataframe(name='X').reset_index()
970
+ df_ens_y = y_ens.to_dataframe(name='Y').reset_index()
971
+ df_ens = df_ens_x.copy()
972
+ df_ens['Y'] = df_ens_y['Y']
973
+
974
+ ens_tooltips = f"""
975
+ <div>
976
+ <span style="font-size: 13px; font-weight: bold;">@model</span>
977
+ </div>
978
+ <div>
979
+ <span style="font-size: 11px;">Member: @member</span>
980
+ </div>
981
+ <div><span style="font-size: 11px;">{x_label.split(' ')[0]}: <b>@X{{0.0}}%</b></span></div>
982
+ <div><span style="font-size: 11px;">{y_label.split(' ')[0]}: <b>@Y{{0.0}}%</b></span></div>
983
+ """
984
+ ens_hover = HoverTool(tooltips=ens_tooltips)
985
+
986
+ ens_scatter = df_ens.hvplot.scatter(
987
+ x='X', y='Y',
988
+ color='model',
989
+ cmap='Category20',
990
+ size=30,
991
+ alpha=0.4,
992
+ tools=['pan', 'wheel_zoom', ens_hover],
993
+ hover_cols=['model', 'member', 'X', 'Y'],
994
+ legend=False
995
+ ).opts(nonselection_alpha=0.4)
996
+
997
+ scatter = ens_scatter * scatter
998
+
999
  background = self.get_scatter_background()
1000
  return (background * scatter).opts(active_tools=['pan', 'wheel_zoom'], margin=0)
1001
 
 
1109
 
1110
  def get_multi_model_sidebar(self):
1111
  return pn.Column(
1112
+ pn.Param(self.param, parameters=['season', 'term', 'show_ensemble_members'],
1113
+ widgets={'show_ensemble_members': {'type': pn.widgets.Switch, 'align': 'start'}},
1114
+ show_name=False, margin=(10, 5)),
1115
  pn.pane.Markdown("### Models", margin=(10, 5, 0, 5)),
1116
  pn.widgets.CheckBoxGroup.from_param(self.param.selected_models, margin=(0, 5, 10, 5)),
1117
  pn.Tabs(
 
1157
  )
1158
 
1159
  map_tabs = pn.Tabs(
1160
+ (f'Values', make_tab_row(self.get_value_map)),
1161
+ (f'Classification', make_tab_row(self.get_type_map)),
1162
  margin=(0, 0, 0, 20),
1163
  sizing_mode='scale_width',
1164
  max_width=1200