|
| 1 | +import numpy as np |
| 2 | +import plotly.graph_objects as go |
| 3 | +import pytest |
| 4 | +from plotly.subplots import make_subplots |
| 5 | + |
| 6 | +import kaleido |
| 7 | + |
| 8 | +pytestmark = pytest.mark.asyncio(loop_scope="function") |
| 9 | + |
| 10 | +rng = np.random.default_rng() # creates a Generator instance |
| 11 | + |
| 12 | + |
| 13 | +async def test_complex_plotly_encoder(): |
| 14 | + """Test that kaleido can handle complex Plotly figures with numpy arrays.""" |
| 15 | + |
| 16 | + # Create complex numpy arrays |
| 17 | + x = np.linspace(0, 4 * np.pi, 100) |
| 18 | + y1 = np.sin(x) * np.exp(-x / 10) |
| 19 | + |
| 20 | + # Create a 2D array for heatmap |
| 21 | + z = np.outer( |
| 22 | + np.sin(np.linspace(0, np.pi, 20)), |
| 23 | + np.cos(np.linspace(0, np.pi, 30)), |
| 24 | + ) |
| 25 | + |
| 26 | + # Create subplot figure |
| 27 | + fig = make_subplots( |
| 28 | + rows=2, |
| 29 | + cols=2, |
| 30 | + subplot_titles=( |
| 31 | + "Complex Scatter", |
| 32 | + "Heatmap", |
| 33 | + "Bar with Color", |
| 34 | + "3D Surface", |
| 35 | + ), |
| 36 | + specs=[ |
| 37 | + [{"type": "scatter"}, {"type": "heatmap"}], |
| 38 | + [{"type": "bar"}, {"type": "surface"}], |
| 39 | + ], |
| 40 | + ) |
| 41 | + |
| 42 | + # Scatter with numpy marker sizes and complex styling |
| 43 | + fig.add_trace( |
| 44 | + go.Scatter( |
| 45 | + x=x, |
| 46 | + y=y1, |
| 47 | + mode="lines+markers", |
| 48 | + line={"color": "blue", "width": 2}, |
| 49 | + marker={ |
| 50 | + "size": [rng.integers(2, 12) for _ in range(len(x))], |
| 51 | + "color": np.abs(y1), |
| 52 | + "colorscale": "Viridis", |
| 53 | + "showscale": True, |
| 54 | + }, |
| 55 | + ), |
| 56 | + row=1, |
| 57 | + col=1, |
| 58 | + ) |
| 59 | + |
| 60 | + # Heatmap with 2D numpy array |
| 61 | + fig.add_trace( |
| 62 | + go.Heatmap( |
| 63 | + z=z, |
| 64 | + colorscale="Plasma", |
| 65 | + ), |
| 66 | + row=1, |
| 67 | + col=2, |
| 68 | + ) |
| 69 | + |
| 70 | + # Bar chart with numpy data and color mapping |
| 71 | + categories = np.array(["A", "B", "C", "D", "E", "F"]) |
| 72 | + values = rng.normal(50, 20, len(categories)) |
| 73 | + |
| 74 | + fig.add_trace( |
| 75 | + go.Bar( |
| 76 | + x=categories, |
| 77 | + y=values, |
| 78 | + marker={ |
| 79 | + "color": np.abs(values), |
| 80 | + "colorscale": "Blues", |
| 81 | + "line": {"width": 2, "color": "black"}, |
| 82 | + }, |
| 83 | + ), |
| 84 | + row=2, |
| 85 | + col=1, |
| 86 | + ) |
| 87 | + |
| 88 | + # 3D surface with complex numpy operations |
| 89 | + x_surf = np.linspace(-3, 3, 30) |
| 90 | + y_surf = np.linspace(-3, 3, 30) |
| 91 | + X, Y = np.meshgrid(x_surf, y_surf) # noqa: N806 |
| 92 | + Z = np.sin(np.sqrt(X**2 + Y**2)) * np.exp(-(X**2 + Y**2) / 10) # noqa: N806 |
| 93 | + |
| 94 | + fig.add_trace( |
| 95 | + go.Surface( |
| 96 | + x=X, |
| 97 | + y=Y, |
| 98 | + z=Z, |
| 99 | + colorscale="Cividis", |
| 100 | + ), |
| 101 | + row=2, |
| 102 | + col=2, |
| 103 | + ) |
| 104 | + |
| 105 | + # Complex layout with numpy-based annotations |
| 106 | + fig.update_layout( |
| 107 | + title="Complex Numpy Figure for Encoder Testing", |
| 108 | + height=800, |
| 109 | + width=1200, |
| 110 | + ) |
| 111 | + |
| 112 | + # Render with kaleido |
| 113 | + img_bytes = await kaleido.calc_fig(fig) |
| 114 | + |
| 115 | + assert isinstance(img_bytes, bytes) |
0 commit comments