Beispiel #1
0
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
Beispiel #2
0
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()
    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
Beispiel #3
0
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"
Beispiel #4
0
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
Beispiel #5
0
def test_abbrev_title():
    long_content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
    dataset = [
        {
            "long_attr": long_content,
            "normal": 3,
            "normal2": 1
        },
        {
            "long_attr": long_content,
            "normal": 3,
            "normal2": 1
        },
        {
            "long_attr": long_content,
            "normal": 2,
            "normal2": 1
        },
        {
            "long_attr": long_content,
            "normal": 4,
            "normal2": 1
        },
    ]
    df = pd.DataFrame(dataset)
    lux.config.plotting_backend = "matplotlib"
    vis = Vis(["normal2", "normal", f"long_attr={long_content}"], df)
    vis_code = vis.to_matplotlib()
    print(vis_code)
    assert "long_attr = Lor...t laborum.'" in vis_code

    vis_code = vis.to_altair()
    print(vis_code)
    assert "long_attr = Lor...t laborum.'" in vis_code
    lux.config.plotting_backend = "altair"
Beispiel #6
0
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
Beispiel #7
0
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)
Beispiel #8
0
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
Beispiel #9
0
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, 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

    lux.config.plotting_backend = "matplotlib"
    vis = Vis(["Displacement"], df)
    vis_code = vis.to_matplotlib()
    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
Beispiel #10
0
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()
    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
Beispiel #11
0
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()
    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
Beispiel #12
0
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()
    assert "ax.set_xlabel('Acceleration')" in vis_code
    assert "ax.set_ylabel('Origin')" in vis_code
Beispiel #13
0
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()
    assert "ax.scatter(x_pts, y_pts, alpha=0.5)" in vis_code
    assert ("ax.set_xlabel('Acceleration', fontsize='15')" in vis_code
            or "ax.set_xlabel('Acceleration')" in vis_code)
    assert "ax.set_ylabel('Weight', fontsize='15')" in vis_code or "ax.set_ylabel('Weight')" in vis_code
Beispiel #14
0
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"
Beispiel #15
0
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)