Skip to content

Commit

Permalink
Bugfix for all column and temporal action (#373)
Browse files Browse the repository at this point in the history
* don't show all column vis for series
* line chart with single data point plotted as bar
  • Loading branch information
dorisjlee committed May 4, 2021
1 parent 6bda76b commit 0339df6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lux/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def _append_rec(self, rec_infolist, recommendations: Dict):
rec_infolist.append(recommendations)

def show_all_column_vis(self):
if self.intent == [] or self.intent is None:
if len(self.columns) > 1 and len(self.columns) < 4 and self.intent == [] or self.intent is None:
vis = Vis(list(self.columns), self)
if vis.mark != "":
vis._all_column = True
Expand Down
10 changes: 9 additions & 1 deletion lux/processor/Compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,15 @@ def line_or_bar_or_geo(ldf, dimension: Clause, measure: Clause):
if measure.aggregation == "":
measure.set_aggregation("mean")
if dim_type == "temporal" or dim_type == "oridinal":
return "line", {"x": dimension, "y": measure}
if isinstance(dimension.attribute, pd.Timestamp):
# If timestamp, use the _repr_ (e.g., TimeStamp('2020-04-05 00.000')--> '2020-04-05')
attr = str(dimension.attribute._date_repr)
else:
attr = dimension.attribute
if ldf.cardinality[attr] == 1:
return "bar", {"x": measure, "y": dimension}
else:
return "line", {"x": dimension, "y": measure}
else: # unordered categorical
# if cardinality large than 5 then sort bars
if ldf.cardinality[dimension.attribute] > 5:
Expand Down
7 changes: 7 additions & 0 deletions lux/vislib/altair/BarChart.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def initialize_chart(self):
x_attr.attribute = x_attr.attribute.replace(".", "")
if isinstance(y_attr.attribute, str):
y_attr.attribute = y_attr.attribute.replace(".", "")
# To get datetime to display correctly on bar charts
if x_attr.data_type == "temporal":
x_attr.data_type = "nominal"
self.code += "from pandas import Timestamp\n"
if y_attr.data_type == "temporal":
y_attr.data_type = "nominal"
self.code += "from pandas import Timestamp\n"

if x_attr.data_model == "measure":
agg_title = get_agg_title(x_attr)
Expand Down
3 changes: 1 addition & 2 deletions lux/vislib/altair/LineChart.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def initialize_chart(self):
# Remove NaNs only for Line Charts (offsets axis range)
self.data = self.data.dropna(subset=[x_attr.attribute, y_attr.attribute])
self.code += "import altair as alt\n"
self.code += "import pandas._libs.tslibs.timestamps\n"
self.code += "from pandas._libs.tslibs.timestamps import Timestamp\n"
self.code += "from pandas import Timestamp\n"
self.code += f"visData = pd.DataFrame({str(self.data.to_dict())})\n"

if y_attr.data_model == "measure":
Expand Down
11 changes: 11 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ def test_series_recommendation():
assert len(df.recommendation["Distribution"]) > 0, "Recommendation property empty for LuxSeries"


def test_series_multivis_recommendation():
covid = pd.read_csv(
"https://github.com/lux-org/lux-datasets/blob/master/data/covid-stringency.csv?raw=True"
)
covid = covid.rename(columns={"stringency_index": "stringency"})
covid["Day"] = pd.to_datetime(covid["Day"], format="%Y-%m-%d")
series = covid["Day"]
assert len(series.recommendation["Temporal"]) == 4, "Display 4 temporal vis based on `Day`"
assert hasattr(series, "current_vis") == False


def test_unnamed_column():
lux.config.plotting_backend = "matplotlib"
df = pd.read_csv("https://raw.githubusercontent.com/lux-org/lux-datasets/master/data/employee.csv")
Expand Down

0 comments on commit 0339df6

Please sign in to comment.