Skip to content

Commit

Permalink
Revert changes to tests that check data field
Browse files Browse the repository at this point in the history
  • Loading branch information
marthacryan committed Oct 15, 2024
1 parent 0d0dad2 commit 9c5d112
Show file tree
Hide file tree
Showing 8 changed files with 535 additions and 543 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import base64
import datetime
from plotly.express.imshow_utils import rescale_intensity
from plotly.tests.b64 import _b64

img_rgb = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]]], dtype=np.uint8)
img_gray = np.arange(100, dtype=float).reshape((10, 10))
Expand Down Expand Up @@ -182,7 +181,7 @@ def test_imshow_xarray(binary_string):
assert fig.layout.xaxis.title.text == "dim_cols"
assert fig.layout.yaxis.title.text == "dim_rows"
if not binary_string:
assert np.all(np.array(fig.data[0].x) == _b64(da.coords["dim_cols"]))
assert np.all(np.array(fig.data[0].x) == np.array(da.coords["dim_cols"]))


def test_imshow_xarray_slicethrough():
Expand All @@ -192,7 +191,7 @@ def test_imshow_xarray_slicethrough():
# Dimensions are used for axis labels and coordinates
assert fig.layout.xaxis.title.text == "dim_2"
assert fig.layout.yaxis.title.text == "dim_1"
assert np.all(np.array(fig.data[0].x) == _b64(da.coords["dim_2"]))
assert np.all(np.array(fig.data[0].x) == np.array(da.coords["dim_2"]))


def test_imshow_xarray_facet_col_string():
Expand All @@ -204,7 +203,7 @@ def test_imshow_xarray_facet_col_string():
# Dimensions are used for axis labels and coordinates
assert fig.layout.xaxis.title.text == "dim_2"
assert fig.layout.yaxis.title.text == "dim_1"
assert np.all(np.array(fig.data[0].x) == _b64(da.coords["dim_2"]))
assert np.all(np.array(fig.data[0].x) == np.array(da.coords["dim_2"]))


def test_imshow_xarray_animation_frame_string():
Expand All @@ -216,7 +215,7 @@ def test_imshow_xarray_animation_frame_string():
# Dimensions are used for axis labels and coordinates
assert fig.layout.xaxis.title.text == "dim_2"
assert fig.layout.yaxis.title.text == "dim_1"
assert np.all(np.array(fig.data[0].x) == _b64(da.coords["dim_2"]))
assert np.all(np.array(fig.data[0].x) == np.array(da.coords["dim_2"]))


def test_imshow_xarray_animation_facet_slicethrough():
Expand All @@ -226,7 +225,7 @@ def test_imshow_xarray_animation_facet_slicethrough():
# Dimensions are used for axis labels and coordinates
assert fig.layout.xaxis.title.text == "dim_3"
assert fig.layout.yaxis.title.text == "dim_2"
assert np.all(np.array(fig.data[0].x) == _b64(da.coords["dim_3"]))
assert np.all(np.array(fig.data[0].x) == np.array(da.coords["dim_3"]))


def test_imshow_labels_and_ranges():
Expand Down Expand Up @@ -292,7 +291,8 @@ def test_imshow_dataframe():
assert fig.data[0].x[0] == df.columns[0]
assert fig.data[0].x[0] == "nation"
assert fig.layout.xaxis.title.text is None
assert fig.data[0].y == _b64(df.index)
assert fig.data[0].y[0] == df.index[0]
assert fig.data[0].y[0] == 0
assert fig.layout.yaxis.title.text is None

df = px.data.medals_wide(indexed=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import numpy as np
import pytest
from itertools import permutations
from plotly.tests.b64 import _b64


def test_scatter():
iris = px.data.iris()
fig = px.scatter(iris, x="sepal_width", y="sepal_length")
assert fig.data[0].type == "scatter"
assert np.all(fig.data[0].x == _b64(iris.sepal_width))
assert np.all(fig.data[0].y == _b64(iris.sepal_length))
assert np.all(fig.data[0].x == iris.sepal_width)
assert np.all(fig.data[0].y == iris.sepal_length)
# test defaults
assert fig.data[0].mode == "markers"

Expand All @@ -29,11 +28,8 @@ def test_custom_data_scatter():
color="species",
hover_data=["petal_length", "petal_width"],
)
assert fig.data[0].customdata == {
"dtype": "f8",
"bdata": "ZmZmZmZm9j+amZmZmZnJP2ZmZmZmZvY/mpmZmZmZyT/NzMzMzMz0P5qZmZmZmck/AAAAAAAA+D+amZmZmZnJP2ZmZmZmZvY/mpmZmZmZyT8zMzMzMzP7P5qZmZmZmdk/ZmZmZmZm9j8zMzMzMzPTPwAAAAAAAPg/mpmZmZmZyT9mZmZmZmb2P5qZmZmZmck/AAAAAAAA+D+amZmZmZm5PwAAAAAAAPg/mpmZmZmZyT+amZmZmZn5P5qZmZmZmck/ZmZmZmZm9j+amZmZmZm5P5qZmZmZmfE/mpmZmZmZuT8zMzMzMzPzP5qZmZmZmck/AAAAAAAA+D+amZmZmZnZP83MzMzMzPQ/mpmZmZmZ2T9mZmZmZmb2PzMzMzMzM9M/MzMzMzMz+z8zMzMzMzPTPwAAAAAAAPg/MzMzMzMz0z8zMzMzMzP7P5qZmZmZmck/AAAAAAAA+D+amZmZmZnZPwAAAAAAAPA/mpmZmZmZyT8zMzMzMzP7PwAAAAAAAOA/ZmZmZmZm/j+amZmZmZnJP5qZmZmZmfk/mpmZmZmZyT+amZmZmZn5P5qZmZmZmdk/AAAAAAAA+D+amZmZmZnJP2ZmZmZmZvY/mpmZmZmZyT+amZmZmZn5P5qZmZmZmck/mpmZmZmZ+T+amZmZmZnJPwAAAAAAAPg/mpmZmZmZ2T8AAAAAAAD4P5qZmZmZmbk/ZmZmZmZm9j+amZmZmZnJPwAAAAAAAPg/mpmZmZmZuT8zMzMzMzPzP5qZmZmZmck/zczMzMzM9D+amZmZmZnJPwAAAAAAAPg/mpmZmZmZuT/NzMzMzMz0P5qZmZmZmck/AAAAAAAA+D+amZmZmZnJP83MzMzMzPQ/MzMzMzMz0z/NzMzMzMz0PzMzMzMzM9M/zczMzMzM9D+amZmZmZnJP5qZmZmZmfk/MzMzMzMz4z9mZmZmZmb+P5qZmZmZmdk/ZmZmZmZm9j8zMzMzMzPTP5qZmZmZmfk/mpmZmZmZyT9mZmZmZmb2P5qZmZmZmck/AAAAAAAA+D+amZmZmZnJP2ZmZmZmZvY/mpmZmZmZyT8=",
"shape": "50, 2",
}
for data in fig.data:
assert np.all(np.in1d(data.customdata[:, 1], iris.petal_width))
# Hover and custom data, no repeated arguments
fig = px.scatter(
iris,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import numpy as np
import pandas as pd
import pytest
from plotly.tests.b64 import _b64


def _compare_figures(go_trace, px_fig):
Expand All @@ -18,9 +17,9 @@ def _compare_figures(go_trace, px_fig):
del go_fig["layout"]["template"]
del px_fig["layout"]["template"]
for key in go_fig["data"][0]:
assert_array_equal(_b64(go_fig["data"][0][key]), _b64(px_fig["data"][0][key]))
assert_array_equal(go_fig["data"][0][key], px_fig["data"][0][key])
for key in go_fig["layout"]:
assert _b64(go_fig["layout"][key]) == _b64(px_fig["layout"][key])
assert go_fig["layout"][key] == px_fig["layout"][key]


def test_pie_like_px():
Expand Down Expand Up @@ -150,11 +149,11 @@ def test_sunburst_treemap_with_path():
# Values passed
fig = px.sunburst(df, path=path, values="values")
assert fig.data[0].branchvalues == "total"
assert fig.data[0].values == {"bdata": "AQMCBAICAQQGBQQECgkT", "dtype": "i1"}
assert fig.data[0].values[-1] == np.sum(values)
# Values passed
fig = px.sunburst(df, path=path, values="values")
assert fig.data[0].branchvalues == "total"
assert fig.data[0].values == {"bdata": "AQMCBAICAQQGBQQECgkT", "dtype": "i1"}
assert fig.data[0].values[-1] == np.sum(values)
# Error when values cannot be converted to numerical data type
df["values"] = ["1 000", "3 000", "2", "4", "2", "2", "1 000", "4 000"]
msg = "Column `values` of `df` could not be converted to a numerical data type."
Expand All @@ -167,11 +166,9 @@ def test_sunburst_treemap_with_path():
# Continuous colorscale
df["values"] = 1
fig = px.sunburst(df, path=path, values="values", color="values")
# assert "coloraxis" in fig.data[0].marker
assert fig.data[0].values == {"bdata": "AQEBAQEBAQECAgICBAQI", "dtype": "i1"}
# depending on pandas version we get different dtype for marker.colors
assert fig.data[0].marker.colors["bdata"] is not None
assert fig.data[0].marker.colors["dtype"] is not None
assert "coloraxis" in fig.data[0].marker
assert np.all(np.array(fig.data[0].marker.colors) == 1)
assert fig.data[0].values[-1] == 8


def test_sunburst_treemap_with_path_and_hover():
Expand Down Expand Up @@ -228,16 +225,10 @@ def test_sunburst_treemap_with_path_color():
path = ["total", "regions", "sectors", "vendors"]
fig = px.sunburst(df, path=path, values="values", color="calls")
colors = fig.data[0].marker.colors
assert colors == {
"bdata": "AAAAAAAAIEAAAAAAAAAAQAAAAAAAAPA/AAAAAAAACEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAEEAAAAAAAADwP6uqqqqqqgJAmpmZmZmZ+T8AAAAAAAAMQAAAAAAAAABAZmZmZmZmBkAcx3Ecx3H8P2wor6G8hgJA",
"dtype": "f8",
}
assert np.all(np.array(colors[:8]) == np.array(calls))
fig = px.sunburst(df, path=path, color="calls")
colors = fig.data[0].marker.colors
assert colors == {
"bdata": "AAAAAAAAIEAAAAAAAAAAQAAAAAAAAPA/AAAAAAAACEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAEEAAAAAAAADwPwAAAAAAAABAAAAAAAAABEAAAAAAAAAUQAAAAAAAAABAAAAAAAAADEAAAAAAAAACQAAAAAAAAAdA",
"dtype": "f8",
}
assert np.all(np.array(colors[:8]) == np.array(calls))

# Hover info
df["hover"] = [el.lower() for el in vendors]
Expand All @@ -261,10 +252,7 @@ def test_sunburst_treemap_with_path_color():
path = ["total", "regions", "sectors", "vendors"]
fig = px.sunburst(df, path=path, values="values", color="calls")
colors = fig.data[0].marker.colors
assert colors == {
"bdata": "AAAAAAAAIEAAAAAAAAAAQAAAAAAAAPA/AAAAAAAACEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAEEAAAAAAAADwP6uqqqqqqgJAmpmZmZmZ+T8AAAAAAAAMQAAAAAAAAABAZmZmZmZmBkAcx3Ecx3H8P2wor6G8hgJA",
"dtype": "f8",
}
assert np.all(np.array(colors[:8]) == np.array(calls))


def test_sunburst_treemap_column_parent():
Expand Down Expand Up @@ -337,7 +325,7 @@ def test_sunburst_treemap_with_path_non_rectangular():
fig = px.sunburst(df, path=path, values="values")
df.loc[df["vendors"].isnull(), "sectors"] = "Other"
fig = px.sunburst(df, path=path, values="values")
assert fig.data[0].values == {"bdata": "AQMCBAICAQQGBQEBBAQLChU=", "dtype": "i1"}
assert fig.data[0].values[-1] == np.sum(values)


def test_pie_funnelarea_colorscale():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import unittest.mock as mock
from plotly.express._core import build_dataframe
from pandas.testing import assert_frame_equal
from plotly.tests.b64 import b64, _b64
import sys
import warnings

Expand All @@ -29,9 +28,8 @@ def add_interchange_module_for_old_pandas():

def test_numpy():
fig = px.scatter(x=[1, 2, 3], y=[2, 3, 4], color=[1, 3, 9])

assert np.all(fig.data[0].x == b64(np.array([1, 2, 3])))
assert np.all(fig.data[0].y == b64(np.array([2, 3, 4])))
assert np.all(fig.data[0].x == np.array([1, 2, 3]))
assert np.all(fig.data[0].y == np.array([2, 3, 4]))
assert np.all(fig.data[0].marker.color == np.array([1, 3, 9]))


Expand Down Expand Up @@ -103,16 +101,16 @@ def test_several_dataframes():
df = pd.DataFrame(dict(x=[0, 1], y=[3, 4]))
df2 = pd.DataFrame(dict(x=[3, 5], y=[23, 24]))
fig = px.scatter(x=df.y, y=df2.y)
assert np.all(fig.data[0].x == b64(np.array([3, 4])))
assert np.all(fig.data[0].y == b64(np.array([23, 24])))
assert np.all(fig.data[0].x == np.array([3, 4]))
assert np.all(fig.data[0].y == np.array([23, 24]))
assert fig.data[0].hovertemplate == "x=%{x}<br>y=%{y}<extra></extra>"

df = pd.DataFrame(dict(x=[0, 1], y=[3, 4]))
df2 = pd.DataFrame(dict(x=[3, 5], y=[23, 24]))
df3 = pd.DataFrame(dict(y=[0.1, 0.2]))
fig = px.scatter(x=df.y, y=df2.y, size=df3.y)
assert np.all(fig.data[0].x == b64(np.array([3, 4])))
assert np.all(fig.data[0].y == b64(np.array([23, 24])))
assert np.all(fig.data[0].x == np.array([3, 4]))
assert np.all(fig.data[0].y == np.array([23, 24]))
assert (
fig.data[0].hovertemplate
== "x=%{x}<br>y=%{y}<br>size=%{marker.size}<extra></extra>"
Expand All @@ -122,8 +120,8 @@ def test_several_dataframes():
df2 = pd.DataFrame(dict(x=[3, 5], y=[23, 24]))
df3 = pd.DataFrame(dict(y=[0.1, 0.2]))
fig = px.scatter(x=df.y, y=df2.y, hover_data=[df3.y])
assert np.all(fig.data[0].x == b64(np.array([3, 4])))
assert np.all(fig.data[0].y == b64(np.array([23, 24])))
assert np.all(fig.data[0].x == np.array([3, 4]))
assert np.all(fig.data[0].y == np.array([23, 24]))
assert (
fig.data[0].hovertemplate
== "x=%{x}<br>y=%{y}<br>hover_data_0=%{customdata[0]}<extra></extra>"
Expand All @@ -133,8 +131,8 @@ def test_several_dataframes():
def test_name_heuristics():
df = pd.DataFrame(dict(x=[0, 1], y=[3, 4], z=[0.1, 0.2]))
fig = px.scatter(df, x=df.y, y=df.x, size=df.y)
assert np.all(fig.data[0].x == b64(np.array([3, 4])))
assert np.all(fig.data[0].y == b64(np.array([0, 1])))
assert np.all(fig.data[0].x == np.array([3, 4]))
assert np.all(fig.data[0].y == np.array([0, 1]))
assert fig.data[0].hovertemplate == "y=%{marker.size}<br>x=%{y}<extra></extra>"


Expand Down Expand Up @@ -407,27 +405,27 @@ def test_splom_case():
assert len(fig.data[0].dimensions) == len(iris.columns)
dic = {"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
fig = px.scatter_matrix(dic)
assert np.all(fig.data[0].dimensions[0].values == b64(np.array(dic["a"])))
assert np.all(fig.data[0].dimensions[0].values == np.array(dic["a"]))
ar = np.arange(9).reshape((3, 3))
fig = px.scatter_matrix(ar)
assert np.all(fig.data[0].dimensions[0].values == b64(ar[:, 0]))
assert np.all(fig.data[0].dimensions[0].values == ar[:, 0])


def test_int_col_names():
# DataFrame with int column names
lengths = pd.DataFrame(np.random.random(100))
fig = px.histogram(lengths, x=0)
assert np.all(b64(np.array(lengths).flatten()) == fig.data[0].x)
assert np.all(np.array(lengths).flatten() == fig.data[0].x)
# Numpy array
ar = np.arange(100).reshape((10, 10))
fig = px.scatter(ar, x=2, y=8)
assert np.all(fig.data[0].x == b64(ar[:, 2]))
assert np.all(fig.data[0].x == ar[:, 2])


def test_data_frame_from_dict():
fig = px.scatter({"time": [0, 1], "money": [1, 2]}, x="time", y="money")
assert fig.data[0].hovertemplate == "time=%{x}<br>money=%{y}<extra></extra>"
assert np.all(fig.data[0].x == _b64([0, 1]))
assert np.all(fig.data[0].x == [0, 1])


def test_arguments_not_modified():
Expand Down Expand Up @@ -491,11 +489,13 @@ def test_identity_map():

def test_constants():
fig = px.scatter(x=px.Constant(1), y=[1, 2])
assert fig.data[0].x == _b64([1, 1])
assert fig.data[0].x[0] == 1
assert fig.data[0].x[1] == 1
assert "x=" in fig.data[0].hovertemplate

fig = px.scatter(x=px.Constant(1, label="time"), y=[1, 2])
assert fig.data[0].x == _b64([1, 1])
assert fig.data[0].x[0] == 1
assert fig.data[0].x[1] == 1
assert "x=" not in fig.data[0].hovertemplate
assert "time=" in fig.data[0].hovertemplate

Expand All @@ -519,12 +519,15 @@ def test_constants():

def test_ranges():
fig = px.scatter(x=px.Range(), y=[1, 2], hover_data=[px.Range()])
assert fig.data[0].x == _b64([0, 1])
assert fig.data[0].customdata == _b64([[0], [1]])
assert fig.data[0].x[0] == 0
assert fig.data[0].x[1] == 1
assert fig.data[0].customdata[0][0] == 0
assert fig.data[0].customdata[1][0] == 1
assert "x=" in fig.data[0].hovertemplate

fig = px.scatter(x=px.Range(label="time"), y=[1, 2])
assert fig.data[0].x == _b64([0, 1])
assert fig.data[0].x[0] == 0
assert fig.data[0].x[1] == 1
assert "x=" not in fig.data[0].hovertemplate
assert "time=" in fig.data[0].hovertemplate

Expand Down Expand Up @@ -614,55 +617,55 @@ def test_x_or_y(fn):
categorical_df = pd.DataFrame(dict(col=categorical), index=index)

fig = fn(x=numerical)
assert fig.data[0].x == _b64(numerical)
assert fig.data[0].y == _b64(range_4)
assert list(fig.data[0].x) == numerical
assert list(fig.data[0].y) == range_4
assert fig.data[0].orientation == "h"
fig = fn(y=numerical)
assert fig.data[0].x == _b64(range_4)
assert fig.data[0].y == _b64(numerical)
assert list(fig.data[0].x) == range_4
assert list(fig.data[0].y) == numerical
assert fig.data[0].orientation == "v"
fig = fn(numerical_df, x="col")
assert fig.data[0].x == _b64(numerical)
assert fig.data[0].y == _b64(index)
assert list(fig.data[0].x) == numerical
assert list(fig.data[0].y) == index
assert fig.data[0].orientation == "h"
fig = fn(numerical_df, y="col")
assert fig.data[0].x == _b64(index)
assert fig.data[0].y == _b64(numerical)
assert list(fig.data[0].x) == index
assert list(fig.data[0].y) == numerical
assert fig.data[0].orientation == "v"

if fn != px.bar:
fig = fn(x=categorical)
assert list(fig.data[0].x) == categorical
assert fig.data[0].y == _b64(range_4)
assert list(fig.data[0].y) == range_4
assert fig.data[0].orientation == "h"
fig = fn(y=categorical)
assert fig.data[0].x == _b64(range_4)
assert list(fig.data[0].x) == range_4
assert list(fig.data[0].y) == categorical
assert fig.data[0].orientation == "v"
fig = fn(categorical_df, x="col")
assert list(fig.data[0].x) == categorical
assert fig.data[0].y == _b64(index)
assert list(fig.data[0].y) == index
assert fig.data[0].orientation == "h"
fig = fn(categorical_df, y="col")
assert fig.data[0].x == _b64(index)
assert list(fig.data[0].x) == index
assert list(fig.data[0].y) == categorical
assert fig.data[0].orientation == "v"

else:
fig = fn(x=categorical)
assert list(fig.data[0].x) == categorical
assert fig.data[0].y == _b64(constant)
assert list(fig.data[0].y) == constant
assert fig.data[0].orientation == "v"
fig = fn(y=categorical)
assert fig.data[0].x == _b64(constant)
assert list(fig.data[0].x) == constant
assert list(fig.data[0].y) == categorical
assert fig.data[0].orientation == "h"
fig = fn(categorical_df, x="col")
assert list(fig.data[0].x) == categorical
assert fig.data[0].y == _b64(constant)
assert list(fig.data[0].y) == constant
assert fig.data[0].orientation == "v"
fig = fn(categorical_df, y="col")
assert fig.data[0].x == _b64(constant)
assert list(fig.data[0].x) == constant
assert list(fig.data[0].y) == categorical
assert fig.data[0].orientation == "h"

Expand Down
Loading

0 comments on commit 9c5d112

Please sign in to comment.