Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions stereo/plots/interact_plot/poly_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ def _add_selected_area(self, _):
self.add_message.value = f'<font color="red"><b>{area_count} area has been added.</b></font>'
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 []
Expand Down Expand Up @@ -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
Expand All @@ -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!')

Expand All @@ -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.')
Expand Down
Loading