def test_special_char(): dataset = [ {"special.char": 1, "normal": 2}, {"special.char": 1, "normal": 2}, {"special.char": 1, "normal": 5}, {"special.char": 1, "normal": 2}, {"special.char": 1, "normal": 3}, {"special.char": 1, "normal": 2}, {"special.char": 1, "normal": 6}, {"special.char": 1, "normal": 2}, {"special.char": 1, "normal": 7}, {"special.char": 1, "normal": 2}, {"special.char": 3, "normal": 10}, {"special.char": 1, "normal": 1}, {"special.char": 5, "normal": 2}, {"special.char": 1, "normal": 2}, {"special.char": 1, "normal": 2}, {"special.char": 1, "normal": 2}, {"special.char": 1, "normal": 2}, ] test = pd.DataFrame(dataset) from lux.vis.Vis import Vis # TODO: add assert that checks that the bar chart is rendered correctly in Altair vis = Vis(["special.char"], test) assert vis.mark == "bar" assert vis.intent == ["special.char"] assert vis.get_attr_by_channel("x")[0].attribute == "Record" assert vis.get_attr_by_channel("y")[0].attribute == "special.char" vis = vis.to_Altair() assert ( "alt.Y('specialchar', type= 'nominal', axis=alt.Axis(labelOverlap=True, title='special.char'))" in vis ) assert ( "alt.X('Record', type= 'quantitative', title='Number of Records', axis=alt.Axis(title='Number of Records')" in vis ) # Checking that this works even when there are multiple "." in column test = test.rename(columns={"special.char": "special..char.."}) # TODO: add assert that checks that the bar chart is rendered correctly in Altair vis = Vis(["special..char.."], test) assert vis.mark == "bar" assert vis.intent == ["special..char.."] assert vis.get_attr_by_channel("x")[0].attribute == "Record" assert vis.get_attr_by_channel("y")[0].attribute == "special..char.." vis = vis.to_Altair() assert ( "alt.Y('specialchar', type= 'nominal', axis=alt.Axis(labelOverlap=True, title='special..char..')" in vis ) assert ( "alt.X('Record', type= 'quantitative', title='Number of Records', axis=alt.Axis(title='Number of Records')" in vis )
def test_histogram_uniform(): df = pd.read_csv("lux/data/car.csv") df["Year"] = pd.to_datetime(df["Year"], format="%Y") df["Units"] = 4.0 vis = Vis(["Units"], df) vis_code = vis.to_Altair() assert "y = alt.Y('Units', type= 'nominal'" in vis_code
def test_bar_uniform(): df = pd.read_csv("lux/data/car.csv") df["Year"] = pd.to_datetime(df["Year"], format="%Y") df["Type"] = "A" vis = Vis(["Type"], df) vis_code = vis.to_Altair() assert "y = alt.Y('Type', type= 'nominal'" in vis_code
def test_colored_heatmap_chart(global_var): df = pd.read_csv( "https://raw.githubusercontent.com/lux-org/lux-datasets/master/data/airbnb_nyc.csv" ) lux.config.plotting_backend = "vegalite" vis = Vis(["price", "longitude", "availability_365"], df) vis_code = vis.to_Altair() assert "alt.Chart(visData).mark_rect()" in vis_code assert ( "x=alt.X('xBinStart', type='quantitative', axis=alt.Axis(title='price'), bin = alt.BinParams(binned=True))" in vis_code) assert "x2=alt.X2('xBinEnd')" in vis_code assert ( "y=alt.Y('yBinStart', type='quantitative', axis=alt.Axis(title='longitude'), bin = alt.BinParams(binned=True))" in vis_code) assert "y2=alt.Y2('yBinEnd')" in vis_code assert 'scale=alt.Scale(type="log")' in vis_code assert "chart.encode(color=alt.Color('availability_365',type='quantitative'))" in vis_code lux.config.plotting_backend = "matplotlib" vis = Vis(["price", "longitude", "availability_365"], df) vis_code = vis.to_matplotlib_code() assert "plt.imshow(df, cmap='viridis')" in vis_code assert "index='xBinStart'" in vis_code assert "values='availability_365'" in vis_code assert "columns='yBinStart'" in vis_code assert "plt.colorbar(label='availability_365')" in vis_code
def test_text_not_overridden(): from lux.vis.Vis import Vis df = pd.read_csv("lux/data/college.csv") vis = Vis(["Region", "Geography"], df) vis._repr_html_() code = vis.to_Altair() assert "color = \"#ff8e04\"" in code
def test_vis_to_Altair_custom_named_df(global_var): df = pytest.car_df some_weirdly_named_df = df.dropna() vis = Vis(["Weight", "Horsepower"], some_weirdly_named_df) code = vis.to_Altair() assert ("alt.Chart(some_weirdly_named_df)" in code), "Unable to export to Altair and detect custom df name"
def test_text_not_overridden(): from lux.vis.Vis import Vis df = pd.read_csv("lux/data/college.csv") vis = Vis(["Region", "Geography"], df) vis._ipython_display_() code = vis.to_Altair() assert 'color = "#ff8e04"' in code
def test_vis_to_Altair_custom_named_df(): df = pd.read_csv("lux/data/car.csv") some_weirdly_named_df = df.dropna() vis = Vis(["Weight", "Horsepower"], some_weirdly_named_df) code = vis.to_Altair() assert ( "alt.Chart(some_weirdly_named_df)" in code ), "Unable to export to Altair and detect custom df name"
def test_vis_to_Altair_standalone(global_var): df = pytest.car_df vis = Vis(["Weight", "Horsepower"], df) code = vis.to_Altair(standalone=True) assert ( "chart = alt.Chart(pd.DataFrame({'Weight': {0: 3504, 1: 3693, 2: 3436, 3: 3433, 4: 3449, 5: 43" in code or "alt.Chart(pd.DataFrame({'Horsepower': {0: 130, 1: 165, 2: 150, 3: 150, 4: 140," in code)
def test_vis_to_Altair_standalone(): df = pd.read_csv("lux/data/car.csv") vis = Vis(["Weight", "Horsepower"], df) code = vis.to_Altair(standalone=True) assert ( "chart = alt.Chart(pd.DataFrame({'Weight': {0: 3504, 1: 3693, 2: 3436, 3: 3433, 4: 3449, 5: 43" in code or "alt.Chart(pd.DataFrame({'Horsepower': {0: 130, 1: 165, 2: 150, 3: 150, 4: 140," in code )
def test_period_to_altair(global_var): df = pd.read_csv("lux/data/car.csv") df["Year"] = pd.to_datetime(df["Year"], format="%Y") df["Year"] = pd.DatetimeIndex(df["Year"]).to_period(freq="A") from lux.vis.Vis import Vis vis = Vis(["Acceleration", "Horsepower", "Year=1972"], df) exported_code = vis.to_Altair() assert "Year = 1972" in exported_code
def test_histogram_chart(global_var): df = pytest.car_df lux.config.plotting_backend = "vegalite" vis = Vis(["Displacement"], df) vis_code = vis.to_Altair() assert "alt.Chart(visData).mark_bar" 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)))" in vis_code) assert 'alt.Y("Number of Records", type="quantitative")' in vis_code lux.config.plotting_backend = "matplotlib" vis = Vis(["Displacement"], df) vis_code = vis.to_matplotlib_code() assert "ax.bar(bars, measurements, width=32.25)" in vis_code assert "ax.set_xlabel('Displacement (binned)')" in vis_code assert "ax.set_ylabel('Number of Records')" in vis_code
def test_line_chart(global_var): df = pytest.car_df lux.config.plotting_backend = "vegalite" vis = Vis(["Year", "Acceleration"], df) vis_code = vis.to_Altair() assert "alt.Chart(visData).mark_line()" in vis_code assert ( "y = alt.Y('Acceleration', type= 'quantitative', title='Mean of Acceleration', axis=alt.Axis(title='Acceleration')" in vis_code) assert "x = alt.X('Year', type = 'temporal', axis=alt.Axis(title='Year'))" in vis_code lux.config.plotting_backend = "matplotlib" vis = Vis(["Year", "Acceleration"], df) vis_code = vis.to_matplotlib_code() assert "ax.plot(x_pts, y_pts)" in vis_code assert "ax.set_xlabel('Year')" in vis_code assert "ax.set_ylabel('Mean of Acceleration')" in vis_code
def test_colored_line_chart(global_var): df = pd.read_csv("lux/data/car.csv") lux.config.plotting_backend = "vegalite" vis = Vis(["Year", "Acceleration", "Origin"], df) vis_code = vis.to_Altair() assert "alt.Chart(visData).mark_line()" in vis_code assert ( "y = alt.Y('Acceleration', type= 'quantitative', title='Mean of Acceleration', axis=alt.Axis(title='Acceleration')" in vis_code) assert "x = alt.X('Year', type = 'temporal', axis=alt.Axis(title='Year'))" in vis_code lux.config.plotting_backend = "matplotlib" vis = Vis(["Year", "Acceleration", "Origin"], df) vis_code = vis.to_matplotlib_code() assert "ax.plot" in vis_code assert "title='Origin'" in vis_code assert "ax.set_xlabel('Year')" in vis_code assert "ax.set_ylabel('Mean of Acceleration')" in vis_code
def test_scatter_chart(global_var): df = pytest.car_df lux.config.plotting_backend = "vegalite" vis = Vis(["Acceleration", "Weight"], df) vis_code = vis.to_Altair() assert "alt.Chart(df).mark_circle()" in vis_code assert ( "x=alt.X('Acceleration',scale=alt.Scale(domain=(8.0, 24.8)),type='quantitative', axis=alt.Axis(title='Acceleration'))" in vis_code) assert ( " y=alt.Y('Weight',scale=alt.Scale(domain=(1613, 5140)),type='quantitative', axis=alt.Axis(title='Weight'))" in vis_code) lux.config.plotting_backend = "matplotlib" vis = Vis(["Acceleration", "Weight"], df) vis_code = vis.to_matplotlib_code() assert "ax.scatter(x_pts, y_pts, alpha=0.5)" in vis_code assert "ax.set_xlabel('Acceleration')" in vis_code assert "ax.set_ylabel('Weight')" in vis_code
def test_colored_bar_chart(global_var): df = pytest.car_df lux.config.plotting_backend = "vegalite" vis = Vis(["Cylinders", "Acceleration", "Origin"], df) vis_code = vis.to_Altair() assert "alt.Chart(visData).mark_bar()" in vis_code assert ( "y = alt.Y('Cylinders', type= 'nominal', axis=alt.Axis(labelOverlap=True, title='Cylinders'))" in vis_code) assert ( "x = alt.X('Acceleration', type= 'quantitative', title='Mean of Acceleration', axis=alt.Axis(title='Mean of Acceleration')" in vis_code) lux.config.plotting_backend = "matplotlib" vis = Vis(["Cylinders", "Acceleration", "Origin"], df) vis_code = vis.to_matplotlib_code() assert "ax.barh" in vis_code assert "title='Origin'" in vis_code assert "ax.set_xlabel('Acceleration')" in vis_code assert "ax.set_ylabel('Cylinders')" in vis_code
def test_bar_chart(global_var): df = pytest.car_df lux.config.plotting_backend = "vegalite" vis = Vis(["Origin", "Acceleration"], df) vis_code = vis.to_Altair() assert "alt.Chart(visData).mark_bar()" in vis_code assert ( "y = alt.Y('Origin', type= 'nominal', axis=alt.Axis(labelOverlap=True, title='Origin'))" in vis_code) assert ( "x = alt.X('Acceleration', type= 'quantitative', title='Mean of Acceleration', axis=alt.Axis(title='Mean of Acceleration'))" in vis_code) lux.config.plotting_style = None lux.config.plotting_backend = "matplotlib" vis = Vis(["Origin", "Acceleration"], df) vis_code = vis.to_matplotlib_code() assert "ax.barh(bars, measurements, align='center')" in vis_code assert "ax.set_xlabel('Acceleration')" in vis_code assert "ax.set_ylabel('Origin')" in vis_code
def test_vis_to_Altair_basic_df(global_var): df = pytest.car_df vis = Vis(["Weight", "Horsepower"], df) code = vis.to_Altair() assert "alt.Chart(df)" in code, "Unable to export to Altair"
def test_vis_to_Altair_basic_df(): df = pd.read_csv("lux/data/car.csv") vis = Vis(["Weight", "Horsepower"], df) code = vis.to_Altair() assert "alt.Chart(df)" in code, "Unable to export to Altair"