Repository navigation
Selection vector improve #1061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Selection vector improve #1061
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c6308e3
Full selection vector implementation
apasarkar 393b48d
Includes some documentation at top of SelectionVector
apasarkar e80f963
Minor typing fix in linear selector selection setter
apasarkar 174f861
First working version with selection vector
apasarkar 3a4dfde
Reworks the logic for adding selectors, improves some documentation, …
apasarkar 701250d
Fixes casting bug in the integer version of the code
apasarkar aa88e30
Update fastplotlib/graphics/selectors/_selection_vector.py
kushalkolar 94242c7
Update fastplotlib/graphics/selectors/_selection_vector.py
kushalkolar 878ea94
Merge branch 'ndwidget' into selection_vector_improve
kushalkolar cf1c380
Removes unused init time parameter and also updates the typing on the…
apasarkar d686a3e
Adds a nonneg check in selection vector selection setter
apasarkar 8a397d8
Adds nonneg test for inverse handler
apasarkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Full selection vector implementation
- Loading branch information
commit c6308e340ae07e58c921c888514b4acecd74c64b
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,89 +1,108 @@ | ||
| from collections.abc import Callable | ||
| from functools import partial | ||
| from typing import Any, Sequence | ||
|
|
||
| import numpy as np | ||
| from ._protocols import SelectorProtocol, MultiSelectorProtocol | ||
|
|
||
|
|
||
| def identity(val: Any) -> Any: | ||
| return val | ||
|
|
||
|
|
||
| class SelectionVector: | ||
| """ | ||
| A class for performing coordinated selections across multiple selectors. | ||
| The user specifies the selectors (via add_selector) and mappings from global selection indices to each individual selector's local indices. | ||
| """ | ||
| def __init__(self, max_size: int = None): | ||
| # selector -> (map, map_inv) | ||
| self._selectors: dict[ | ||
| SelectorProtocol | MultiSelectorProtocol, tuple[Callable, Callable] | ||
| SelectorProtocol | MultiSelectorProtocol, tuple[Callable, Callable, list] | ||
|
kushalkolar marked this conversation as resolved.
Outdated
|
||
| ] = dict() | ||
| self._selection: list[Any] = list() | ||
| self._block_reentrance = False | ||
|
|
||
| @property | ||
| def selection(self) -> tuple[Any]: | ||
| return tuple(self._selection) | ||
|
|
||
| @selection.setter | ||
| def selection(self, new: Sequence[Any]): | ||
| # iterate through each selector that operates in its own "local" space | ||
| for selector_local, (map_, map_inv) in self._selectors.items(): | ||
| indices_local = map_(new) | ||
| selector_local.selection = indices_local | ||
| if self._block_reentrance: | ||
| return | ||
| else: | ||
| self._block_reentrance = True | ||
| # iterate through each selector that operates in its own "local" space | ||
| for selector_local, (map_, map_inv) in self._selectors.items(): | ||
| indices_local = map_(new) | ||
| selector_local.selection = indices_local | ||
| self._block_reentrance = False | ||
|
|
||
| def append(self, index): | ||
| self._selection.append(index) | ||
| for selector, (map_, map_inv) in self._selectors.items(): | ||
| for selector, (map_, map_inv, handler_list) in self._selectors.items(): | ||
| if not isinstance(selector, MultiSelectorProtocol): | ||
| continue | ||
|
|
||
| index_local = map_([index]) | ||
|
kushalkolar marked this conversation as resolved.
Outdated
|
||
| selector.append(index_local[0]) | ||
|
|
||
| def clear(self): | ||
| self._selection.clear() | ||
| # TODO: clear selectors | ||
| selector.append(index_local) | ||
|
|
||
| def add_selector( | ||
| self, | ||
| new: ( | ||
| SelectorProtocol | ||
| | tuple[SelectorProtocol, Callable] | ||
| | tuple[SelectorProtocol, Callable, Callable] | ||
| | tuple[SelectorProtocol, np.ndarray | dict[int, int]] | ||
| ), | ||
| ): | ||
| """ | ||
| User specifies (1) the selector and (2) The master --> local index mapping. This | ||
|
kushalkolar marked this conversation as resolved.
|
||
| mapping is given either as: | ||
| - A 1D np.ndarray of integers. The array index is the global index, and the array value is the local index | ||
| - A dictionary where keys (master indices) and values (local indices) are both integers | ||
| """ | ||
| selector: SelectorProtocol | ||
| map_: Callable | ||
| map_inv: Callable | ||
|
|
||
| if isinstance(new, (tuple, list)): | ||
| if not isinstance(new[0], SelectorProtocol): | ||
| raise TypeError | ||
|
|
||
| if len(new) not in (2, 3): | ||
| raise TypeError | ||
|
|
||
| if not all(callable(c) for c in new[1:]): | ||
| if len(new) != 2: | ||
| raise TypeError | ||
|
|
||
| selector = new[0] | ||
| map_ = new[1] | ||
| map_inv = new[2] if len(new) == 3 else identity | ||
| master_to_local = new[1] | ||
| if isinstance(master_to_local, np.ndarray): | ||
| if not master_to_local.ndim == 1: | ||
| raise ValueError("If you pass in an array mapping, it must be 1-D") | ||
| master_to_local = dict(enumerate(master_to_local)) | ||
|
|
||
| ## Construct inverse mapping | ||
| inverse_dict = dict() | ||
| for key, val in master_to_local.items(): | ||
| inverse_dict[val] = key | ||
|
|
||
| ## Define the partial functions | ||
| master_to_local_map = lambda x:master_to_local[x] if x in master_to_local else None | ||
| local_to_master_map = lambda x:inverse_dict[x] if x in inverse_dict else None | ||
|
|
||
| elif isinstance(new, SelectorProtocol): | ||
| selector, map_, map_inv = new, identity, identity | ||
| selector, master_to_local_map, local_to_master_map = new, identity, identity | ||
|
|
||
| else: | ||
| raise ValueError | ||
|
|
||
| selector.add_event_handler(partial(self._inv_handler, map_inv)) | ||
|
|
||
| self._selectors[selector] = (map_, map_inv) | ||
| handler = selector.add_event_handler(partial(self._inv_handler, local_to_master_map)) | ||
| self._selectors[selector] = (master_to_local_map, local_to_master_map, [handler]) | ||
|
|
||
| def _inv_handler(self, map_inv: Callable, local_selection): | ||
| return | ||
| # when a selectable changes its selection, set global index change using map inverse | ||
| # self._selection = map_inv(local_selection) | ||
|
|
||
| def remove(self): | ||
| pass | ||
|
|
||
| def clear_selectables(self): | ||
| self._selectors.clear() | ||
| self._selection = map_inv(local_selection) | ||
|
|
||
| def remove_selector(self, selector: SelectorProtocol | MultiSelectorProtocol): | ||
| if selector in self._selectors: | ||
| map, map_inv, handler_list = self._selectors.pop(selector) | ||
| for handler in handler_list: | ||
| selector.remove_event_handler(handler) | ||
| if isinstance(selector, MultiSelectorProtocol): | ||
| selector.clear() | ||
|
|
||
| def clear_selectors(self): | ||
| for selector in self._selectors.keys(): | ||
| if isinstance(selector, MultiSelectorProtocol): | ||
| selector.clear() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.