From 79c981642e0ce31d8ff65138bffe4383764aa399 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 2 Apr 2026 07:14:09 +0000 Subject: [PATCH] fix #330: PolySelection.export_high_res_area falls back to selected_exp_data when list_poly_selection_coors is empty The export_high_res_area method in PolySelection required users to click the 'add' button before exporting, but users who clicked 'export' directly would have self.selected_exp_data populated without list_poly_selection_coors being filled. This caused get_selected_areas() to raise an exception. Added _resolve_selected_areas() that tries list_poly_selection_coors first, then falls back to self.selected_exp_data, with a clear error message if neither is available. Co-authored-by: wanruiwen-genomics-cn --- stereo/plots/interact_plot/poly_selection.py | 26 ++++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/stereo/plots/interact_plot/poly_selection.py b/stereo/plots/interact_plot/poly_selection.py index 780f6bc9..e6900398 100644 --- a/stereo/plots/interact_plot/poly_selection.py +++ b/stereo/plots/interact_plot/poly_selection.py @@ -133,9 +133,8 @@ def _add_selected_area(self, _): self.add_message.value = f'{area_count} area has been added.' self.add.loading = False - # def get_selected_boundary_coors(self) -> list: def get_selected_area_coors(self, drop=False) -> list: - selected_exp_data = self.get_selected_areas(drop) + selected_exp_data = self._resolve_selected_areas(drop) if selected_exp_data is not None: return selected_exp_data.position.tolist() return [] @@ -267,6 +266,23 @@ def __get_filtering_flag(data, bin_size, position, center_coordinates, num_threa selected_gem_df.to_csv(fp, sep='\t', index=False, mode='wb') + def _resolve_selected_areas(self, drop=False): + """Resolve selected areas from either the 'add' queue or the 'export' callback. + + Falls back to ``self.selected_exp_data`` (populated by the 'export' + button callback) when ``list_poly_selection_coors`` is empty, so that + users do not have to click 'add' before calling ``export_high_res_area``. + """ + if len(self.list_poly_selection_coors) > 0: + return self.get_selected_areas(drop) + if self.selected_exp_data is not None: + return self.selected_exp_data + raise Exception( + "No area has been selected. Please either:\n" + " 1) Select an area on the plot and click the 'export' button, or\n" + " 2) Select area(s), click 'add' for each, then call this method." + ) + def export_high_res_area(self, origin_file_path: str, output_path: str, drop: bool = False) -> str: """ export selected area in high resolution @@ -278,8 +294,8 @@ def export_high_res_area(self, origin_file_path: str, output_path: str, drop: bo """ make_dirs(output_path) if self.data.file_format == 'gef': - coors = self.get_selected_area_coors(drop) - # print('coors length: %s' % len(coors)) + selected_areas = self._resolve_selected_areas(drop) + coors = selected_areas.position.tolist() if not coors or len(coors) == 0: raise Exception('Please select the data area in the picture first!') @@ -290,7 +306,7 @@ def export_high_res_area(self, origin_file_path: str, output_path: str, drop: bo else: cg.generate_bgef_by_coordinate(origin_file_path, output_path, coors, self.data.bin_size) elif self.data.file_format == 'gem': - selected_areas = self.get_selected_areas(drop=False) + selected_areas = self._resolve_selected_areas(drop) self.generate_gem_file(selected_areas, origin_file_path, output_path, drop) else: raise Exception('Only supports gef and gem file.')