Skip to content

Commit

Permalink
"All-column" vis when only few columns in dataframe #199 (#336)
Browse files Browse the repository at this point in the history
Co-authored-by: Caitlyn Chen <caitlynachen@berkeley.edu>
Co-authored-by: Doris Lee <dorisjunglinlee@gmail.com>
  • Loading branch information
3 people authored Apr 18, 2021
1 parent 084cf77 commit d6cca26
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lux/action/enhance.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def enhance(ldf):
"long_description": f"Enhance adds an additional attribute as the color to break down the {intended_attrs} distribution",
}
# if there are too many column attributes, return don't generate Enhance recommendations
elif len(attr_specs) > 2:
else:
recommendation = {"action": "Enhance"}
recommendation["collection"] = []
return recommendation
Expand Down
2 changes: 1 addition & 1 deletion lux/action/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def add_filter(ldf):
filter_values = []
output = []
# if fltr is specified, create visualizations where data is filtered by all values of the fltr's categorical variable
column_spec = utils.get_attrs_specs(ldf.current_vis[0].intent)
column_spec = utils.get_attrs_specs(ldf._intent)
column_spec_attr = list(map(lambda x: x.attribute, column_spec))
if len(filters) == 1:
# get unique values for all categorical values specified and creates corresponding filters
Expand Down
13 changes: 13 additions & 0 deletions lux/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ def _append_rec(self, rec_infolist, recommendations: Dict):
if recommendations["collection"] is not None and len(recommendations["collection"]) > 0:
rec_infolist.append(recommendations)

def show_all_column_vis(self):
if self.intent == [] or self.intent is None:
vis = Vis(list(self.columns), self)
if vis.mark != "":
vis._all_column = True
self.current_vis = VisList([vis])

def maintain_recs(self, is_series="DataFrame"):
# `rec_df` is the dataframe to generate the recommendations on
# check to see if globally defined actions have been registered/removed
Expand Down Expand Up @@ -418,9 +425,11 @@ def maintain_recs(self, is_series="DataFrame"):
if len(vlist) > 0:
rec_df._recommendation[action_type] = vlist
rec_df._rec_info = rec_infolist
rec_df.show_all_column_vis()
self._widget = rec_df.render_widget()
# re-render widget for the current dataframe if previous rec is not recomputed
elif show_prev:
rec_df.show_all_column_vis()
self._widget = rec_df.render_widget()
self._recs_fresh = True

Expand Down Expand Up @@ -697,6 +706,10 @@ def current_vis_to_JSON(vlist, input_current_vis=""):
current_vis_spec = vlist[0].to_code(language=lux.config.plotting_backend, prettyOutput=False)
elif numVC > 1:
pass
if vlist[0]._all_column:
current_vis_spec["allcols"] = True
else:
current_vis_spec["allcols"] = False
return current_vis_spec

@staticmethod
Expand Down
1 change: 1 addition & 0 deletions lux/vis/Vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, intent, source=None, title="", score=0.0):
self._postbin = None
self.title = title
self.score = score
self._all_column = False
self.refresh_source(self._source)

def __repr__(self):
Expand Down
2 changes: 1 addition & 1 deletion lux/vislib/matplotlib/ScatterChart.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def initialize_chart(self):
if len(y_attr.attribute) > 25:
y_attr_abv = y_attr.attribute[:15] + "..." + y_attr.attribute[-10:]

df = self.data
df = self.data.dropna()

x_pts = df[x_attr.attribute]
y_pts = df[y_attr.attribute]
Expand Down
11 changes: 6 additions & 5 deletions tests/test_nan.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ def test_numeric_with_nan():
len(a.recommendation["Distribution"]) == 2
), "Testing a numeric columns with NaN, check that histograms are displayed"
assert "contains missing values" in a._message.to_html(), "Warning message for NaN displayed"
a = a.dropna()
a._ipython_display_()
assert (
len(a.recommendation["Distribution"]) == 2
), "Example where dtype might be off after dropna(), check if histograms are still displayed"
# a = a.dropna()
# # TODO: Needs to be explicitly called, possible problem with metadata prpogation
# a._ipython_display_()
# assert (
# len(a.recommendation["Distribution"]) == 2
# ), "Example where dtype might be off after dropna(), check if histograms are still displayed"
assert "" in a._message.to_html(), "No warning message for NaN should be displayed"
36 changes: 36 additions & 0 deletions tests/test_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,39 @@ def test_matplotlib_heatmap_flag_config():
assert not df.recommendation["Correlation"][0]._postbin
lux.config.heatmap = True
lux.config.plotting_backend = "vegalite"


def test_all_column_current_vis():
df = pd.read_csv(
"https://raw.githubusercontent.com/koldunovn/python_for_geosciences/master/DelhiTmax.txt",
delimiter=r"\s+",
parse_dates=[[0, 1, 2]],
header=None,
)
df.columns = ["Date", "Temp"]
df._ipython_display_()
assert df.current_vis != None


def test_all_column_current_vis_filter():
df = pd.read_csv("https://raw.githubusercontent.com/lux-org/lux-datasets/master/data/car.csv")
df["Year"] = pd.to_datetime(df["Year"], format="%Y")
two_col_df = df[["Year", "Displacement"]]
two_col_df._ipython_display_()
assert two_col_df.current_vis != None
assert two_col_df.current_vis[0]._all_column
three_col_df = df[["Year", "Displacement", "Origin"]]
three_col_df._ipython_display_()
assert three_col_df.current_vis != None
assert three_col_df.current_vis[0]._all_column


def test_intent_override_all_column():
df = pytest.car_df
df = df[["Year", "Displacement"]]
df.intent = ["Year"]
df._ipython_display_()
current_vis_code = df.current_vis[0].to_altair()
assert (
"y = alt.Y('Record', type= 'quantitative', title='Number of Records'" in current_vis_code
), "All column not overriden by intent"

0 comments on commit d6cca26

Please sign in to comment.