-
Notifications
You must be signed in to change notification settings - Fork 377
Allow different sorters to work with Vis Object #350
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
base: master
Are you sure you want to change the base?
Changes from all commits
2cef000
cad3e84
f197884
1ed9655
c56f79d
c0388df
cf045de
6ae9767
0db6376
1e6f572
7836abf
9c17abb
b3509f6
d428289
9716088
912f3cc
8041f84
9548b8a
e5f5aa4
0aa92a2
0b8ee08
31ec5a6
5aa2178
bd6d4b4
79e4a41
f9e34bc
ecdd3ea
0141fa1
d08c17c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| *********************************** | ||
| Ordering Outputs | ||
| *********************************** | ||
|
|
||
| By default, Lux is trying to maximize the `interestingness function <interestingness.html>`_. | ||
| However, in some cases, perhaps you would like to sort by a different feature or attribute. | ||
| Here are some default sorters available to use: | ||
|
|
||
| Supported Orderings | ||
| ==================== | ||
|
|
||
|
|
||
| 1. `"interestingness"`: This is the default sorter selected in Lux. It scores by the `interestingness function <interestingness.html>`_. | ||
|
|
||
| 2. `"alphabetical_by_title"`: This sorter allows for alphabetical sorting based on the title. | ||
|
|
||
| 3. `"alphabetical_by_x"` : This sorter allows for alphabetical sorting based on the attribute featured on the x-axis. | ||
|
|
||
| 4. `"alphabetical_by_y"` : This sorter allows for alphabetical sorting based on the attribute featured on the y-axis. | ||
|
|
||
| Custom Orderings | ||
| ==================== | ||
|
|
||
| You can also add a custom sorter by creating a function that takes in a collection and a direction (ascending or descending) | ||
| and returns the sorted collection. | ||
|
|
||
| For example, | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| def sort_by_multiple(collection, desc): | ||
| collection.sort(key=lambda x: (x.get_attr_by_channel("x")[0].attribute, x.get_attr_by_channel("y")[0].attribute), reverse=False) | ||
| lux.config.ordering = sort_by_multiple | ||
|
|
||
| In this example, instead of sorting by just the x attribute or y attribute, we'd like to sort first by the x attribute and then sort by the y attribute. | ||
| The last line sets this to the globally defined ordering, which means that all actions will be ordered using this function. | ||
|
|
||
| Action-Dependent Orderings | ||
| ========================== | ||
| There are some cases where we'd like to sort the outputs of different actions differently. | ||
| To do so, you can add to the :code:`lux.config.ordering_actions` dictionary. To set an action's ordering, | ||
| you can do the following, for example: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| lux.config.ordering_actions["correlation"] = "alphabetical_by_x" | ||
|
|
||
| To remove the ordering for the action, simply reset the dictionary's entry to an empty string, like so: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| lux.config.ordering_actions["correlation"] = "" | ||
|
|
||
| Changing the sorting direction | ||
| ============================== | ||
|
|
||
| By default, Lux is trying to `maximize` an objective function, whether that be `interestingness` or some other custom function you define. | ||
| Thus, the default sorting direction is :code:`"descending"`. In order to toggle it, you can use: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| lux.config.sort = "ascending" # or "descending" or "None" for no sorting order | ||
|
|
||
| This is a globally defined sort order. | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| from typing import Any, Callable, Dict, Iterable, List, Optional, Union | ||
| import lux | ||
| import warnings | ||
| from lux.utils.orderings import Ordering, OrderingDict, resolve_value | ||
| from lux.utils.tracing_utils import LuxTracer | ||
| import os | ||
| from lux._config.template import postgres_template, mysql_template | ||
|
|
@@ -31,9 +32,12 @@ def __init__(self): | |
| self._plotting_backend = "vegalite" | ||
| self._plotting_scale = 1 | ||
| self._topk = 15 | ||
| self._sort = True | ||
| self._ordering = Ordering.interestingness | ||
| self._ordering_actions = OrderingDict({}) | ||
| self._number_of_bars = 10 # max no of bars displayed (rest shown as "+ k more") | ||
| self._label_len = 25 # max length of x and y axis labels | ||
| self._sort = "descending" | ||
| self._sort = True | ||
| self._pandas_fallback = True | ||
| self._interestingness_fallback = True | ||
| self.heatmap_bin_size = 40 | ||
|
|
@@ -127,8 +131,6 @@ def sort(self): | |
| @sort.setter | ||
| def sort(self, flag: Union[str]): | ||
| """ | ||
| Setting parameter to determine sort order of each action | ||
|
|
||
| Parameters | ||
| ---------- | ||
| flag : Union[str] | ||
|
|
@@ -137,13 +139,37 @@ def sort(self, flag: Union[str]): | |
| """ | ||
| flag = flag.lower() | ||
| if isinstance(flag, str) and flag in ["none", "ascending", "descending"]: | ||
| self._sort = flag | ||
| if flag == "none": | ||
| self._sort = None | ||
| elif flag == "ascending": | ||
| self._sort = False | ||
| else: | ||
| self._sort = True | ||
| else: | ||
| warnings.warn( | ||
| "Parameter to lux.config.sort must be one of the following: 'none', 'ascending', or 'descending'.", | ||
| stacklevel=2, | ||
| ) | ||
|
|
||
| @property | ||
|
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. Not sure if sort makes sense as a global option, since it is more common to do one type of sorting for an action, but a different sort mechanism for another action.
Member
Author
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. I added two implementations -- one global ordering (this could be useful for sorting by attribute name or something really general that you want to standardize across all actions) and an action-wise ordering dictionary. If no ordering is defined for the specific action, then we fallback to the global ordering.
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. This sounds great, let's add some docs that explain the two different types of ways to specify sorting! |
||
| def ordering(self): | ||
| return self._ordering | ||
|
|
||
| @ordering.setter | ||
| def ordering(self, value): | ||
|
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. Can we rename all |
||
| """ | ||
| Parameters | ||
| ---------- | ||
| value : Union[str, Callable] | ||
| "interestingness", “alphabetical_by_title”, “alphabetical_by_x”, “alphabetical_by_y” , or Callable | ||
| Default available sorters or custom sorter | ||
| """ | ||
| self._ordering = resolve_value(value) | ||
|
|
||
| @property | ||
| def ordering_actions(self): | ||
| return self._ordering_actions | ||
|
|
||
| @property | ||
| def pandas_fallback(self): | ||
| return self._pandas_fallback | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Copyright 2019-2020 The Lux Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import collections | ||
| import warnings | ||
|
|
||
| import lux | ||
|
|
||
|
|
||
| class Ordering: | ||
| @staticmethod | ||
| def interestingness(collection, desc): | ||
| collection.sort(key=lambda x: x.score, reverse=desc) | ||
|
|
||
| @staticmethod | ||
| def title(collection, desc): | ||
| collection.sort(key=lambda x: x.title, reverse=desc) | ||
|
|
||
| @staticmethod | ||
| def x_alpha(collection, desc): | ||
| collection.sort(key=lambda x: x.get_attr_by_channel("x")[0].attribute, reverse=desc) | ||
|
|
||
| @staticmethod | ||
| def y_alpha(collection, desc): | ||
| collection.sort(key=lambda x: x.get_attr_by_channel("y")[0].attribute, reverse=desc) | ||
|
|
||
|
|
||
| def resolve_value(value): | ||
| if type(value) is str: | ||
| if value == "interestingness": | ||
| return Ordering.interestingness | ||
| elif value == "alphabetical_by_title": | ||
| return Ordering.title | ||
| elif value == "alphabetical_by_x": | ||
| return Ordering.x_alpha | ||
| elif value == "alphabetical_by_y": | ||
| return Ordering.y_alpha | ||
| else: | ||
| assert callable(value), "You must pass in a default string or a custom function." | ||
| return value | ||
|
|
||
|
|
||
| class OrderingDict(collections.MutableMapping, dict): | ||
| def __getitem__(self, key): | ||
| return dict.__getitem__(self, key) | ||
|
|
||
| def __setitem__(self, key, value): | ||
| if key in lux.config.actions or key == "global": | ||
| dict.__setitem__(self, key, resolve_value(value)) | ||
| else: | ||
| warnings.warn( | ||
| f"Key is not a valid action; must be one of the following: {self.actions.keys()}.", | ||
| stacklevel=2, | ||
| ) | ||
|
|
||
| def __delitem__(self, key): | ||
| dict.__delitem__(self, key) | ||
|
|
||
| def __iter__(self): | ||
| return dict.__iter__(self) | ||
|
|
||
| def __len__(self): | ||
| return dict.__len__(self) | ||
|
|
||
| def __contains__(self, x): | ||
| return dict.__contains__(self, x) |
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.
We didn't explain how
lux.config.orderingcan be used in the default case.