Skip to content

Commit

Permalink
[Altair] Sync exported histogram code with new bin determination (#304)
Browse files Browse the repository at this point in the history
* Merge upstream

* Sync with master

* Fix bin size variance from #217

* Format and test

* Change labelOverlap to True

Co-authored-by: Dominik Moritz <domoritz@gmail.com>

* Modify markbar; currently questioning whether or not it's needed

* Remove markbar enitrely, rely on Altair automatic bin detection https://altair-viz.github.io/user_guide/generated/core/altair.BinParams.html

* Modify code snippet

* Revert "Remove markbar enitrely, rely on Altair automatic bin detection https://altair-viz.github.io/user_guide/generated/core/altair.BinParams.html"

This reverts commit 9cb9418.

* Implement bin size estimation via Freedman Diaconis's Rule

* Use numpy to compute IQR for better performance (pandas too slow)

* Add tests

* Add test cases for histogram binning

* Address changes from @domoritz review (small optimizations)

* Black and format

* Move histogram bin width computation to pandas executor (execute_binning)

* Center bars between ticks in distribution setting

* Renaming in execute_binning

* Bin width computed accurately in execute_binning; no need for get_bin_size()

* Revert to Freedman rule; maintain correct ticks

* Sync exported code with new histogram bin determination rules

* Sync exported code with new histogram bin determination rules

* Modify histogram code test case

Co-authored-by: Micah Yong <micahyong@Micahs-MacBook-Pro.local>
Co-authored-by: Dominik Moritz <domoritz@gmail.com>
  • Loading branch information
3 people authored Mar 15, 2021
1 parent 4de06dc commit 1c93057
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
18 changes: 10 additions & 8 deletions lux/vislib/altair/Histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def initialize_chart(self):
y=alt.Y(
str(msr_attr.attribute),
title=axis_title,
bin=alt.Bin(binned=True, step=markbar),
bin=alt.Bin(binned=True, step=step),
type=msr_attr.data_type,
axis=alt.Axis(title=axis_title),
),
y2=end_attr_abv,
Expand All @@ -99,20 +100,21 @@ def initialize_chart(self):
#####################################

self.code += "import altair as alt\n"
# self.code += f"visData = pd.DataFrame({str(self.data.to_dict(orient='records'))})\n"
self.code += f"visData = pd.DataFrame({str(self.data.to_dict())})\n"
if measure.channel == "x":
self.code += f"""
chart = alt.Chart(visData).mark_bar(size={markbar}).encode(
alt.X('{msr_attr.attribute}', title='{axis_title}',bin=alt.Bin(binned=True), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}'), scale=alt.Scale(domain=({x_min}, {x_max}))),
alt.Y("Number of Records", type="quantitative")
chart = alt.Chart(visData).mark_bar().encode(
x=alt.X('{msr_attr.attribute}', title='{axis_title}',bin=alt.Bin(binned=True, step={step}), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}'), scale=alt.Scale(domain=({x_min}, {x_max}))),
x2='{end_attr_abv}',
y=alt.Y("Number of Records", type="quantitative")
)
"""
elif measure.channel == "y":
self.code += f"""
chart = alt.Chart(visData).mark_bar(size={markbar}).encode(
alt.Y('{msr_attr.attribute}', title='{axis_title}',bin=alt.Bin(binned=True), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}'), scale=alt.Scale(domain=({x_min}, {x_max}))),
alt.X("Number of Records", type="quantitative")
chart = alt.Chart(visData).mark_bar().encode(
y=alt.Y('{msr_attr.attribute}', title='{axis_title}', bin=alt.Bin(binned=True, step={step}), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}')),
y2='{end_attr_abv}',
x=alt.X("Number of Records", type="quantitative")
)
"""
return chart
Expand Down
5 changes: 1 addition & 4 deletions tests/test_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import pandas as pd
from lux.vis.VisList import VisList
from lux.vis.Vis import Vis
from lux.vislib.altair.Histogram import compute_bin_width


def test_vis(global_var):
Expand Down Expand Up @@ -341,11 +340,9 @@ def test_histogram_chart(global_var):
lux.config.plotting_backend = "vegalite"
vis = Vis(["Displacement"], df)
vis_code = vis.to_Altair()
expected_bin_size = compute_bin_width(vis.data["Displacement"])
assert "alt.Chart(visData).mark_bar" in vis_code
assert str(expected_bin_size) in vis_code
assert (
"alt.X('Displacement', title='Displacement (binned)',bin=alt.Bin(binned=True), type='quantitative', axis=alt.Axis(labelOverlap=True, title='Displacement (binned)'), scale=alt.Scale(domain=(68.0, 455.0)))"
"alt.X('Displacement', title='Displacement (binned)',bin=alt.Bin(binned=True, step=38.7), type='quantitative', axis=alt.Axis(labelOverlap=True, title='Displacement (binned)'), scale=alt.Scale(domain=(68.0, 455.0)))"
in vis_code
)
assert 'alt.Y("Number of Records", type="quantitative")' in vis_code
Expand Down

0 comments on commit 1c93057

Please sign in to comment.