Spaces:
Sleeping
Sleeping
Faster app v2 with caching
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ active_tab_css = """
|
|
| 20 |
pn.extension(design="material", sizing_mode="stretch_width", raw_css=[active_tab_css])
|
| 21 |
|
| 22 |
|
| 23 |
-
|
| 24 |
def get_data():
|
| 25 |
ds1= xr.open_dataset("data/LENS_precomputed_terms_combined.nc")
|
| 26 |
gdf_regions = gpd.read_file('data/rainfall_regions.geojson',).set_index('index')
|
|
@@ -46,8 +46,18 @@ def get_data():
|
|
| 46 |
ds1, gdf_regions, gdf_precursors_u850, gdf_precursors_v850, gdf_precursors_z500 = get_data()
|
| 47 |
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
filtered = gdf.query("season==@season & region==@region")
|
| 52 |
if len(filtered) == 0:
|
| 53 |
return pn.pane.Markdown(f"No data for {region} in {season}")
|
|
@@ -70,9 +80,9 @@ def plot_precursor_map_gdf(gdf, variable_name, season='SON', region='Egypt + Eas
|
|
| 70 |
|
| 71 |
def get_precursor_plot(season='SON', region='Egypt + East Libya'):
|
| 72 |
"""Get all three precursor maps side-by-side."""
|
| 73 |
-
u850_map =
|
| 74 |
-
v850_map =
|
| 75 |
-
z500_map =
|
| 76 |
|
| 77 |
return (u850_map + v850_map + z500_map).cols(3)
|
| 78 |
|
|
@@ -80,6 +90,62 @@ def get_precursor_plot(season='SON', region='Egypt + East Libya'):
|
|
| 80 |
|
| 81 |
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
class CMIPApp(param.Parameterized):
|
| 84 |
model = param.Selector(default='CESM2', objects=list(ds1.model.values))
|
| 85 |
season = param.Selector(default='DJF', objects=list(ds1.season.values))
|
|
@@ -156,56 +222,9 @@ class CMIPApp(param.Parameterized):
|
|
| 156 |
|
| 157 |
def _get_classified_data(self):
|
| 158 |
"""Helper to unify classification and color mapping for all regions."""
|
| 159 |
-
#
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
ds_model = (ds_model.sum("synoptic_bin") / 0.05 * 100).individual_term
|
| 163 |
-
|
| 164 |
-
# 2. Extract base components
|
| 165 |
-
conversion = ds_model.sel(source='Conversion')
|
| 166 |
-
dynamical = ds_model.sel(source='Dynamical')
|
| 167 |
-
nonlinear = ds_model.sel(source='Non Linear')
|
| 168 |
-
|
| 169 |
-
# 3. Determine X and Y for classification (based on nonlinear_pos setting)
|
| 170 |
-
if self.nonlinear_pos == 'Dynamical':
|
| 171 |
-
x_val = conversion
|
| 172 |
-
y_val = dynamical + nonlinear
|
| 173 |
-
else:
|
| 174 |
-
x_val = conversion + nonlinear
|
| 175 |
-
y_val = dynamical
|
| 176 |
-
|
| 177 |
-
# 4. Create DataFrame
|
| 178 |
-
df = pd.DataFrame({
|
| 179 |
-
'Region': ds_model.region_id.values,
|
| 180 |
-
'Conversion': conversion.values,
|
| 181 |
-
'Dynamical': dynamical.values,
|
| 182 |
-
'Non_Linear': nonlinear.values,
|
| 183 |
-
'X': x_val.values,
|
| 184 |
-
'Y': y_val.values,
|
| 185 |
-
'Selected_Sum': ds_model.sel(source=self.source).sum('source').values
|
| 186 |
-
})
|
| 187 |
-
|
| 188 |
-
# 5. Add classification logic
|
| 189 |
-
def classify(row):
|
| 190 |
-
x, y = row['X'], row['Y']
|
| 191 |
-
if abs(x) + abs(y) <= 20: return 'Minimal'
|
| 192 |
-
if abs(x) < 0.2 * abs(y): return 'Dynamical'
|
| 193 |
-
if abs(y) < 0.2 * abs(x): return 'Conversion'
|
| 194 |
-
return 'Compounding' if x * y > 0 else 'Cancelling'
|
| 195 |
-
|
| 196 |
-
df['Type'] = df.apply(classify, axis=1)
|
| 197 |
-
|
| 198 |
-
# 6. Saturated colors for readable hover text
|
| 199 |
-
text_cmap = {
|
| 200 |
-
'Minimal': 'grey',
|
| 201 |
-
'Conversion': '#b8860b', # DarkGoldenRod
|
| 202 |
-
'Dynamical': 'blue',
|
| 203 |
-
'Compounding': 'red',
|
| 204 |
-
'Cancelling': 'green'
|
| 205 |
-
}
|
| 206 |
-
df['Text_Color'] = df['Type'].map(text_cmap)
|
| 207 |
-
|
| 208 |
-
return df
|
| 209 |
|
| 210 |
@param.depends('model', 'season', 'term', 'source', 'vmax', '_selection_index')
|
| 211 |
def get_value_map(self):
|
|
|
|
| 20 |
pn.extension(design="material", sizing_mode="stretch_width", raw_css=[active_tab_css])
|
| 21 |
|
| 22 |
|
| 23 |
+
@pn.cache()
|
| 24 |
def get_data():
|
| 25 |
ds1= xr.open_dataset("data/LENS_precomputed_terms_combined.nc")
|
| 26 |
gdf_regions = gpd.read_file('data/rainfall_regions.geojson',).set_index('index')
|
|
|
|
| 46 |
ds1, gdf_regions, gdf_precursors_u850, gdf_precursors_v850, gdf_precursors_z500 = get_data()
|
| 47 |
|
| 48 |
|
| 49 |
+
@pn.cache(max_items=128)
|
| 50 |
+
def get_cached_precursor_map(variable_name, season, region, vmax):
|
| 51 |
+
"""Cached version of precursor map plotting."""
|
| 52 |
+
if variable_name == 'U850':
|
| 53 |
+
gdf = gdf_precursors_u850
|
| 54 |
+
elif variable_name == 'V850':
|
| 55 |
+
gdf = gdf_precursors_v850
|
| 56 |
+
elif variable_name == 'Z500':
|
| 57 |
+
gdf = gdf_precursors_z500
|
| 58 |
+
else:
|
| 59 |
+
return None
|
| 60 |
+
|
| 61 |
filtered = gdf.query("season==@season & region==@region")
|
| 62 |
if len(filtered) == 0:
|
| 63 |
return pn.pane.Markdown(f"No data for {region} in {season}")
|
|
|
|
| 80 |
|
| 81 |
def get_precursor_plot(season='SON', region='Egypt + East Libya'):
|
| 82 |
"""Get all three precursor maps side-by-side."""
|
| 83 |
+
u850_map = get_cached_precursor_map('U850', season, region, 10)
|
| 84 |
+
v850_map = get_cached_precursor_map('V850', season, region, 10)
|
| 85 |
+
z500_map = get_cached_precursor_map('Z500', season, region, 2000)
|
| 86 |
|
| 87 |
return (u850_map + v850_map + z500_map).cols(3)
|
| 88 |
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
+
@pn.cache(max_items=32)
|
| 94 |
+
def compute_classified_data(model, season, term, nonlinear_pos, source_tuple):
|
| 95 |
+
"""Cached computation of classified data."""
|
| 96 |
+
# 1. Select data components for all regions
|
| 97 |
+
ds_model = ds1.sel(model=model, season=season, term=term)
|
| 98 |
+
# Sum over synoptic bins and correct units
|
| 99 |
+
ds_model = (ds_model.sum("synoptic_bin") / 0.05 * 100).individual_term
|
| 100 |
+
|
| 101 |
+
# 2. Extract base components
|
| 102 |
+
conversion = ds_model.sel(source='Conversion')
|
| 103 |
+
dynamical = ds_model.sel(source='Dynamical')
|
| 104 |
+
nonlinear = ds_model.sel(source='Non Linear')
|
| 105 |
+
|
| 106 |
+
# 3. Determine X and Y for classification (based on nonlinear_pos setting)
|
| 107 |
+
if nonlinear_pos == 'Dynamical':
|
| 108 |
+
x_val = conversion
|
| 109 |
+
y_val = dynamical + nonlinear
|
| 110 |
+
else:
|
| 111 |
+
x_val = conversion + nonlinear
|
| 112 |
+
y_val = dynamical
|
| 113 |
+
|
| 114 |
+
# 4. Create DataFrame
|
| 115 |
+
# ds_model.sel(source=list(source_tuple)) works because xarray handles lists
|
| 116 |
+
df = pd.DataFrame({
|
| 117 |
+
'Region': ds_model.region_id.values,
|
| 118 |
+
'Conversion': conversion.values,
|
| 119 |
+
'Dynamical': dynamical.values,
|
| 120 |
+
'Non_Linear': nonlinear.values,
|
| 121 |
+
'X': x_val.values,
|
| 122 |
+
'Y': y_val.values,
|
| 123 |
+
'Selected_Sum': ds_model.sel(source=list(source_tuple)).sum('source').values
|
| 124 |
+
})
|
| 125 |
+
|
| 126 |
+
# 5. Add classification logic
|
| 127 |
+
def classify(row):
|
| 128 |
+
x, y = row['X'], row['Y']
|
| 129 |
+
if abs(x) + abs(y) <= 20: return 'Minimal'
|
| 130 |
+
if abs(x) < 0.2 * abs(y): return 'Dynamical'
|
| 131 |
+
if abs(y) < 0.2 * abs(x): return 'Conversion'
|
| 132 |
+
return 'Compounding' if x * y > 0 else 'Cancelling'
|
| 133 |
+
|
| 134 |
+
df['Type'] = df.apply(classify, axis=1)
|
| 135 |
+
|
| 136 |
+
# 6. Saturated colors for readable hover text
|
| 137 |
+
text_cmap = {
|
| 138 |
+
'Minimal': 'grey',
|
| 139 |
+
'Conversion': '#b8860b', # DarkGoldenRod
|
| 140 |
+
'Dynamical': 'blue',
|
| 141 |
+
'Compounding': 'red',
|
| 142 |
+
'Cancelling': 'green'
|
| 143 |
+
}
|
| 144 |
+
df['Text_Color'] = df['Type'].map(text_cmap)
|
| 145 |
+
|
| 146 |
+
return df
|
| 147 |
+
|
| 148 |
+
|
| 149 |
class CMIPApp(param.Parameterized):
|
| 150 |
model = param.Selector(default='CESM2', objects=list(ds1.model.values))
|
| 151 |
season = param.Selector(default='DJF', objects=list(ds1.season.values))
|
|
|
|
| 222 |
|
| 223 |
def _get_classified_data(self):
|
| 224 |
"""Helper to unify classification and color mapping for all regions."""
|
| 225 |
+
# Convert list to tuple for hashing
|
| 226 |
+
source_tuple = tuple(self.source) if self.source else ()
|
| 227 |
+
return compute_classified_data(self.model, self.season, self.term, self.nonlinear_pos, source_tuple)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
@param.depends('model', 'season', 'term', 'source', 'vmax', '_selection_index')
|
| 230 |
def get_value_map(self):
|