Skip to content
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

Allow different sorters to work with Vis Object #350

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2cef000
coalesce data_types into data_type_lookup
jinimukh Dec 23, 2020
cad3e84
Merge branch 'master' of https://github.com/lux-org/lux
jinimukh Dec 27, 2020
f197884
merge
jinimukh Jan 8, 2021
1ed9655
merge fixed
jinimukh Jan 8, 2021
c56f79d
merge conflicts
jinimukh Jan 8, 2021
c0388df
merged
jinimukh Jan 8, 2021
cf045de
Merge branch 'master' of https://github.com/jinimukh/lux
jinimukh Jan 8, 2021
6ae9767
Merge branch 'master' into foo
jinimukh Jan 8, 2021
0db6376
merge upstream
jinimukh Jan 9, 2021
1e6f572
first commit
jinimukh Jan 15, 2021
7836abf
conflicts
jinimukh Feb 20, 2021
9c17abb
requirements.txt updated for pandas 1.2.2
jinimukh Feb 21, 2021
b3509f6
Merge branch 'master' of https://github.com/jinimukh/lux
jinimukh Feb 21, 2021
d428289
Merge branch 'master' of https://github.com/lux-org/lux
jinimukh Mar 12, 2021
9716088
Merge branch 'master' of https://github.com/lux-org/lux
jinimukh Mar 23, 2021
912f3cc
Merge branch 'master' of https://github.com/lux-org/lux
jinimukh Mar 30, 2021
8041f84
Merge branch 'master' of https://github.com/lux-org/lux
jinimukh Apr 8, 2021
9548b8a
config sorters implemented; tests passing when run together
jinimukh Apr 9, 2021
e5f5aa4
should fix failing interestingness tests
jinimukh Apr 9, 2021
0aa92a2
black
jinimukh Apr 9, 2021
0b8ee08
Merge branch 'master' of https://github.com/lux-org/lux into features…
jinimukh Aug 11, 2021
31ec5a6
structural changes, some refactoring
jinimukh Aug 11, 2021
5aa2178
black
jinimukh Aug 11, 2021
bd6d4b4
bug
jinimukh Aug 11, 2021
79e4a41
add action specific sorting
jinimukh Aug 12, 2021
f9e34bc
tests working
jinimukh Sep 22, 2021
ecdd3ea
documentation
jinimukh Sep 22, 2021
0141fa1
Merge branch 'master' into features/sort_interface
jinimukh Sep 22, 2021
d08c17c
fix bug
jinimukh Sep 22, 2021
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
19 changes: 19 additions & 0 deletions lux/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Callable, Dict, Iterable, List, Optional, Union
import lux
import warnings
from lux.utils.sorters import Sorter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename Sorter to something more specific? Currently, the sorter actually doesn't perform any sorting, but just returns the sort key, e.g. interestingness score, or sort key for alphabetical sorting.


RegisteredOption = namedtuple("RegisteredOption", "name action display_condition args")

Expand All @@ -32,6 +33,7 @@ def __init__(self):
self._plotting_backend = "vegalite"
self._topk = 15
self._sort = "descending"
self._sorter = Sorter.interestingness()
self._pandas_fallback = True
self._interestingness_fallback = True
self.heatmap_bin_size = 40
Expand Down Expand Up @@ -83,6 +85,23 @@ def sort(self, flag: Union[str]):
stacklevel=2,
)

@property
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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 sorter(self):
return self._sorter

@sorter.setter
def sorter(self, sorting_function):
"""
Setting parameter to determine sort function of each action

Parameters
----------
flag : Union[str]
"none", "ascending","descending"
No sorting, sort by ascending order, sort by descending order
"""
self._sorter = sorting_function

@property
def pandas_fallback(self):
return self._pandas_fallback
Expand Down
31 changes: 31 additions & 0 deletions lux/utils/sorters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.


class Sorter:
@staticmethod
def interestingness():
dorisjlee marked this conversation as resolved.
Show resolved Hide resolved
return lambda x: x.score

@staticmethod
def title():
return lambda x: x.title

@staticmethod
def x_alpha():
return lambda x: x.get_attr_by_channel("x")[0].attribute

@staticmethod
def y_alpha():
return lambda x: x.get_attr_by_channel("y")[0].attribute
2 changes: 1 addition & 1 deletion lux/vis/VisList.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def sort(self, remove_invalid=True, descending=True):
elif lux.config.sort == "descending":
descending = True
# sort in-place by “score” by default if available, otherwise user-specified field to sort by
self._collection.sort(key=lambda x: x.score, reverse=descending)
dorisjlee marked this conversation as resolved.
Show resolved Hide resolved
self._collection.sort(key=lux.config.sorter, reverse=descending)

def showK(self):
k = lux.config.topk
Expand Down
28 changes: 28 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import pandas as pd
import time
from lux.vis.VisList import VisList
from lux.utils.sorters import Sorter
import lux


Expand Down Expand Up @@ -289,6 +290,33 @@ def test_sort(global_var):
lux.config.sort = "descending"


def test_sorter(global_var):
df = pd.read_csv("lux/data/college.csv")
lux.config.topk = 5
df._ipython_display_()
assert len(df.recommendation["Correlation"]) == 5, "Show top 5"
score = float("inf")
for vis in df.recommendation["Correlation"]:
assert vis.score <= score
score = vis.score
lux.config.sorter = Sorter.x_alpha()
df = pd.read_csv("lux/data/college.csv")
string = df.recommendation["Correlation"][0].get_attr_by_channel("x")[0].attribute
assert len(df.recommendation["Correlation"]) == 5, "Show top 5"
for vis in df.recommendation["Correlation"]:
assert vis.get_attr_by_channel("x")[0].attribute <= string
string = vis.get_attr_by_channel("x")[0].attribute
lux.config.sorter = Sorter.y_alpha()
df = pd.read_csv("lux/data/college.csv")
string = df.recommendation["Correlation"][0].get_attr_by_channel("y")[0].attribute
assert len(df.recommendation["Correlation"]) == 5, "Show top 5"
for vis in df.recommendation["Correlation"]:
assert vis.get_attr_by_channel("y")[0].attribute <= string
string = vis.get_attr_by_channel("y")[0].attribute
lux.config.sorter = Sorter.interestingness()
lux.config.topk = 15


# TODO: This test does not pass in pytest but is working in Jupyter notebook.
# def test_plot_setting(global_var):
# df = pytest.car_df
Expand Down