-
Notifications
You must be signed in to change notification settings - Fork 65
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
Draft
apasarkar
wants to merge
6
commits into
ndwidget
Choose a base branch
from
selection_vector_improve
base: ndwidget
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Selection vector improve #1061
Changes from all commits
Commits
Show all changes
6 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 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
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
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,161 @@ | ||
| from collections.abc import Callable | ||
| from functools import partial | ||
| from typing import Any, Sequence | ||
| from typing import Any, Sequence, TypeAlias | ||
| from numbers import Integral | ||
|
|
||
| import numpy as np | ||
|
|
||
| from ._protocols import SelectorProtocol, MultiSelectorProtocol | ||
|
|
||
| Mapping: TypeAlias = np.ndarray | dict[int, int] | Callable | ||
|
|
||
| def identity(val: Any) -> Any: | ||
| return val | ||
|
|
||
| def array_map(arr: np.ndarray, index: Integral): | ||
| """ | ||
| Used to map local to global indices | ||
| """ | ||
| return None if np.isnan(arr[index]) else int(arr[index]) | ||
|
|
||
| def inv_array_map(arr: np.ndarray, | ||
| value: int) -> None | Integral: | ||
| """ | ||
| arr[i] gives the global index | ||
| """ | ||
| x = np.flatnonzero(arr == value) | ||
| return None if x.size == 0 else int(x[0]) | ||
|
|
||
| def dict_map(my_dict: dict, key: Integral): | ||
| if key is None: | ||
| return None | ||
| elif int(key) not in my_dict: | ||
| return None | ||
| else: | ||
| return my_dict[key] | ||
|
|
||
|
|
||
| class SelectionVector: | ||
| """ | ||
| A class for performing coordinated selections across multiple selectors. | ||
| For each selector in the selection vector, the user specifies how the global indices (shared across selectors) | ||
| maps to the local indices (each selector has its own local index space). | ||
|
|
||
| The SelectionVector coordinates across individual selectors, including the coordinated updating of indices whenever a selection changes | ||
| """ | ||
| def __init__(self, max_size: int = None): | ||
| # selector -> (map, map_inv) | ||
|
|
||
| ## Key is a selector, value is a (1) local to global index map (2) global to local index map (3) list of event handlers | ||
| self._selectors: dict[ | ||
| SelectorProtocol | MultiSelectorProtocol, tuple[Callable, Callable] | ||
| SelectorProtocol | MultiSelectorProtocol, tuple[Callable, Callable, list[Callable]] | ||
| ] = 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 | ||
| def selection(self, new: Integral | Sequence[Any]): | ||
| if self._block_reentrance: | ||
| return | ||
| else: | ||
| self._block_reentrance = True | ||
| if isinstance(new, Integral): | ||
| new = [new] | ||
| self._selection = [i for i in new] | ||
| # iterate through each selector that operates in its own "local" space | ||
| for selector_local, (map_, map_inv, handler) in self._selectors.items(): | ||
| local_indices = [] | ||
| for value in new: | ||
| curr_indices = map_(value) | ||
| local_indices.append(curr_indices) | ||
| selector_local.selection = local_indices | ||
| 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]) | ||
| selector.append(index_local[0]) | ||
|
|
||
| def clear(self): | ||
| self._selection.clear() | ||
| # TODO: clear selectors | ||
| index_local = map_(index) | ||
| selector.append(index_local) | ||
|
|
||
| def add_selector( | ||
| self, | ||
| new: ( | ||
| SelectorProtocol | ||
| | tuple[SelectorProtocol, Callable] | ||
| | tuple[SelectorProtocol, Callable, Callable] | ||
| | tuple[SelectorProtocol, dict] | ||
| | tuple[SelectorProtocol, np.ndarray] | ||
| |tuple[SelectorProtocol, Callable, Callable] | ||
| ), | ||
| ): | ||
| selector: SelectorProtocol | ||
| map_: Callable | ||
| map_inv: Callable | ||
|
|
||
| """ | ||
| User specifies (1) the selector and (2) The master --> local index mapping. This | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they can also specify a mapping Callable directly |
||
| 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 | ||
| - Two callables. The first callable defines the global index --> local index map, the second specifies the local index --> global index map. | ||
| All callables take as input nonnegative integers and output nonnegative integers. | ||
| """ | ||
| 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:]): | ||
| raise TypeError | ||
| if len(new) == 3: | ||
| if isinstance(new[1], Callable) and isinstance(new[2], Callable): | ||
| master_to_local = new[1] | ||
| local_to_master = new[2] | ||
| else: | ||
| raise ValueError(f"Both index mappings must be Callables, you provided {type(new[1])} and {type(new[2])}") | ||
| elif len(new) == 2: | ||
| if isinstance(new[1], dict): | ||
| ## Construct inverse mapping | ||
| inverse_dict = dict() | ||
| for key, val in new[1].items(): | ||
| inverse_dict[int(val)] = int(key) | ||
| master_to_local = partial(dict_map, new[1]) | ||
| local_to_master = partial(dict_map, inverse_dict) | ||
|
|
||
| elif isinstance(new[1], np.ndarray): | ||
| if not new[1].ndim == 1: | ||
| raise ValueError("If you pass in an array mapping, it must be 1-D") | ||
| master_to_local = partial(array_map, new[1]) | ||
| local_to_master = partial(inv_array_map, new[1]) | ||
| else: | ||
| raise ValueError(f"Must either provide a single dict or numpy array specifying the local to global index mapping, or two callables" | ||
| f"specifying the mapping in both directions") | ||
|
|
||
| selector = new[0] | ||
| map_ = new[1] | ||
| map_inv = new[2] if len(new) == 3 else identity | ||
|
|
||
| elif isinstance(new, SelectorProtocol): | ||
| selector, map_, map_inv = new, identity, identity | ||
| selector, master_to_local, local_to_master = new, identity, identity | ||
|
|
||
| else: | ||
| raise ValueError | ||
|
|
||
| selector.add_event_handler(partial(self._inv_handler, map_inv)) | ||
|
|
||
| self._selectors[selector] = (map_, map_inv) | ||
|
|
||
| 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() | ||
| handler = selector.add_event_handler(partial(self._inv_handler, local_to_master)) | ||
| self._selectors[selector] = (master_to_local, local_to_master, [handler]) | ||
|
|
||
| def _inv_handler(self, map_inv: Callable, local_selection: dict): | ||
| """ | ||
| HighlightSelector and VisibilitySelector emit a dictionary with keys selector and value | ||
| """ | ||
| input_to_map = local_selection['value'] | ||
| self.selection = [map_inv(input_to_map[i]) for i in range(len(input_to_map))] | ||
|
|
||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't do anything, can just be
self._selection = newThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kushalkolar thought about this -- this does something. if the input is any iterable other than a list, it makes the input a list.