Skip to content

Commit 095c8b3

Browse files
committed
more updates to spectrum viewer
1 parent adf6be0 commit 095c8b3

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

examples/serve/panels-demo/demo_panels/panel_spectrum.py

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ def __init__(self):
3838

3939
def add_place_data(self, new_data: pd.DataFrame):
4040
if self._is_duplicate(new_data):
41-
print("Duplicate data detected. Not adding.")
42-
print("self.all_data", self.all_data["places"].unique())
43-
print("new_data", new_data["places"].unique())
4441
return
4542

4643
place_names = new_data["places"].unique()
@@ -64,19 +61,18 @@ def delete_all_data(self):
6461

6562

6663
manager = DataManager()
64+
previous_mode = ""
6765

6866

6967
@panel.layout(
7068
State("@app", "selectedDatasetId"),
7169
State("@app", "selectedTimeLabel"),
72-
State("@app", "selectedPlaceGroup"),
7370
State("@app", "themeMode"),
7471
)
7572
def render_panel(
7673
ctx: Context,
7774
dataset_id: str,
7875
time_label: str,
79-
place_group: list[dict[str, Any]],
8076
theme_mode: str,
8177
) -> Component:
8278

@@ -125,10 +121,12 @@ def render_panel(
125121

126122
# Make line chart and bar chart
127123

128-
add_button = Button(id="add_button", text="Add", style={"maxWidth": 100})
124+
delete_button = Button(
125+
id="delete_button", text="Delete last point", style={"maxWidth": 100}
126+
)
129127

130128
controls = Box(
131-
children=[exploration_radio_group, add_button],
129+
children=[exploration_radio_group, delete_button],
132130
style={
133131
"display": "flex",
134132
"flexDirection": "row",
@@ -181,7 +179,6 @@ def get_spectra(
181179

182180
grid_mapping = GridMapping.from_dataset(dataset)
183181

184-
# if place_geometry is not None and not grid_mapping.crs.is_geographic:
185182
project = pyproj.Transformer.from_crs(
186183
CRS_CRS84, grid_mapping.crs, always_xy=True
187184
).transform
@@ -212,11 +209,9 @@ def get_spectra(
212209

213210
variables = list(selected_values.keys())
214211
values = [selected_values[var]["data"] for var in variables]
215-
print("reflectance values::", values)
216212
cleaned_values = [
217213
0 if isinstance(x, float) and math.isnan(x) else x for x in values
218214
]
219-
print("reflectance values after::", cleaned_values)
220215

221216
wavelengths = [
222217
dataset_place[var].attrs.get("wavelength", None) for var in variables
@@ -258,7 +253,7 @@ def update_text(
258253
Input("@app", "selectedPlaceGeometry"),
259254
State("@app", "selectedPlaceGroup"),
260255
State("exploration_radio_group", "value"),
261-
Input("add_button", "clicked"),
256+
# Input("add_button", "clicked"),
262257
Input("plot", "chart"),
263258
Output("plot", "chart"),
264259
Output("error_message", "children"),
@@ -270,10 +265,11 @@ def update_plot(
270265
place_geo: dict[str, Any] | None = None,
271266
place_group: list[dict[str, Any]] | None = None,
272267
exploration_radio_group: bool | None = None,
273-
_clicked: bool | None = None,
268+
# _clicked: bool | None = None,
274269
previous_chart: bool | None = None,
275270
) -> tuple[alt.Chart | None, str]:
276-
print("exploration_radio_group", exploration_radio_group)
271+
global manager, previous_mode
272+
# print("clicked::", _clicked)
277273
if exploration_radio_group is None:
278274
return None, "Missing exploration mode choice"
279275
dataset = get_dataset(ctx, dataset_id)
@@ -291,9 +287,8 @@ def update_plot(
291287

292288
if label is None:
293289
return None, "There is no label for the selected point"
294-
print("place_geo", place_geo)
295290
if place_geo.get("type") == "Point":
296-
place_group = gpd.GeoDataFrame(
291+
place_group_geodf = gpd.GeoDataFrame(
297292
[
298293
{
299294
"name": label,
@@ -309,18 +304,26 @@ def update_plot(
309304
else:
310305
return None, "Selected geometry must be a point"
311306

312-
place_group["time"] = pd.to_datetime(time_label).tz_localize(None)
307+
place_group_geodf["time"] = pd.to_datetime(time_label).tz_localize(None)
313308
places_select = [label]
314-
source = get_spectra(dataset, place_group, places_select)
309+
source = get_spectra(dataset, place_group_geodf, places_select)
315310

316311
if source is None:
317312
return None, "No reflectances found in Variables"
318-
319313
if previous_chart is None:
320314
manager.delete_all_data()
321315

316+
# Delete points that are removed by the user from the viewer:
317+
valid_labels = {
318+
feature["properties"]["label"]
319+
for item in place_group
320+
for feature in item.get("features", [])
321+
}
322+
manager.all_data = manager.all_data[manager.all_data["places"].isin(valid_labels)]
323+
322324
if exploration_radio_group == "active":
323-
manager.remove_last_added_place()
325+
if previous_mode != "save":
326+
manager.remove_last_added_place()
324327
manager.add_place_data(source)
325328
chart = (
326329
alt.Chart(manager.all_data)
@@ -335,7 +338,6 @@ def update_plot(
335338
).properties(width="container", height="container")
336339
else:
337340
manager.add_place_data(source)
338-
print("in save else case::", manager.all_data["places"].unique())
339341
chart = (
340342
alt.Chart(manager.all_data)
341343
.mark_bar()
@@ -347,22 +349,43 @@ def update_plot(
347349
tooltip=["variable", "wavelength", "reflectance"],
348350
)
349351
).properties(width="container", height="container")
350-
print("chart should be ready", chart)
351-
352+
previous_mode = exploration_radio_group
352353
return chart, ""
353354

354355

356+
@panel.callback(
357+
Input("delete_button", "clicked"),
358+
Output("plot", "chart"),
359+
)
360+
def delete_places(
361+
ctx: Context,
362+
_clicked: bool | None = None,
363+
) -> alt.Chart:
364+
global manager
365+
manager.remove_last_added_place()
366+
return (
367+
alt.Chart(manager.all_data)
368+
.mark_bar()
369+
.encode(
370+
x="wavelength:N",
371+
y="reflectance:Q",
372+
xOffset="places:N",
373+
color="places:N",
374+
tooltip=["variable", "wavelength", "reflectance"],
375+
)
376+
).properties(width="container", height="container")
377+
378+
355379
@panel.callback(
356380
Input("@app", "selectedPlaceGeometry"),
357381
Input("exploration_radio_group", "value"),
358-
Output("add_button", "disabled"),
382+
Output("delete_button", "disabled"),
359383
)
360384
def set_button_disablement(
361385
_ctx: Context,
362386
place_geometry: str | None = None,
363387
exploration_radio_group: str | None = None,
364388
) -> bool:
365-
print("in set_button_disablement", place_geometry)
366389
return not place_geometry and exploration_radio_group != "save"
367390

368391

0 commit comments

Comments
 (0)