|
1 | 1 | """Tests visualization operations."""
|
2 | 2 |
|
3 | 3 | import matplotlib.pyplot as plt
|
| 4 | +import pydantic.v1 as pd |
4 | 5 | import pytest
|
5 | 6 | import tidy3d as td
|
6 | 7 | from tidy3d.components.viz import Polygon, set_default_labels_and_title
|
@@ -100,3 +101,151 @@ def test_set_default_labels_title():
|
100 | 101 | ax = set_default_labels_and_title(
|
101 | 102 | axis_labels=axis_labels, axis=2, position=0, ax=ax, plot_length_units="inches"
|
102 | 103 | )
|
| 104 | + |
| 105 | + plt.close() |
| 106 | + |
| 107 | + |
| 108 | +def test_make_viz_spec(): |
| 109 | + """ |
| 110 | + Tests core visualizaton spec creation. |
| 111 | + """ |
| 112 | + viz_spec = td.VisualizationSpec(facecolor="red", edgecolor="green", alpha=0.5) |
| 113 | + viz_spec = td.VisualizationSpec(facecolor="red", alpha=0.5) |
| 114 | + |
| 115 | + |
| 116 | +def test_unallowed_colors(): |
| 117 | + """ |
| 118 | + Tests validator for visualization spec for colors not recognized by matplotlib. |
| 119 | + """ |
| 120 | + with pytest.raises(pd.ValidationError): |
| 121 | + _ = td.VisualizationSpec(facecolor="rr", edgecolor="green", alpha=0.5) |
| 122 | + with pytest.raises(pd.ValidationError): |
| 123 | + _ = td.VisualizationSpec(facecolor="red", edgecolor="gg", alpha=0.5) |
| 124 | + |
| 125 | + |
| 126 | +def test_unallowed_alpha(): |
| 127 | + """ |
| 128 | + Tests validator for disallowed alpha values. |
| 129 | + """ |
| 130 | + with pytest.raises(pd.ValidationError): |
| 131 | + _ = td.VisualizationSpec(facecolor="red", edgecolor="green", alpha=-0.5) |
| 132 | + with pytest.raises(pd.ValidationError): |
| 133 | + _ = td.VisualizationSpec(facecolor="red", edgecolor="green", alpha=2.5) |
| 134 | + |
| 135 | + |
| 136 | +def test_plot_from_structure(): |
| 137 | + """ |
| 138 | + Tests visualization spec can be added to medium and structure plotting function can be run. |
| 139 | + """ |
| 140 | + viz_spec = td.VisualizationSpec(facecolor="blue", edgecolor="pink", alpha=0.5) |
| 141 | + medium = td.Medium(permittivity=2.25, viz_spec=viz_spec) |
| 142 | + geometry = td.Box(size=(2, 0, 2)) |
| 143 | + |
| 144 | + structure = td.Structure(geometry=geometry, medium=medium) |
| 145 | + |
| 146 | + structure.plot(z=0) |
| 147 | + plt.close() |
| 148 | + |
| 149 | + |
| 150 | +def plot_with_viz_spec(alpha, facecolor, edgecolor=None, use_viz_spec=True): |
| 151 | + """ |
| 152 | + Helper function for locally testing different visualization specs in structures through |
| 153 | + structure plotting function. |
| 154 | + """ |
| 155 | + if edgecolor is None: |
| 156 | + viz_spec = td.VisualizationSpec(facecolor=facecolor, alpha=alpha) |
| 157 | + else: |
| 158 | + viz_spec = td.VisualizationSpec(facecolor=facecolor, edgecolor=edgecolor, alpha=alpha) |
| 159 | + |
| 160 | + medium = td.Medium(permittivity=2.25) |
| 161 | + if use_viz_spec: |
| 162 | + medium = td.Medium(permittivity=2.25, viz_spec=viz_spec) |
| 163 | + |
| 164 | + geometry = td.Box(size=(2, 4, 2)) |
| 165 | + |
| 166 | + structure = td.Structure(geometry=geometry, medium=medium) |
| 167 | + |
| 168 | + structure.plot(z=1) |
| 169 | + plt.show() |
| 170 | + |
| 171 | + |
| 172 | +def plot_with_multi_viz_spec(alphas, facecolors, edgecolors, rng, use_viz_spec=True): |
| 173 | + """ |
| 174 | + Helper function for plotting simulations with multiple visulation specs via simluation |
| 175 | + plotting function. |
| 176 | + """ |
| 177 | + viz_specs = [ |
| 178 | + td.VisualizationSpec( |
| 179 | + facecolor=facecolors[idx], edgecolor=edgecolors[idx], alpha=alphas[idx] |
| 180 | + ) |
| 181 | + for idx in range(0, len(alphas)) |
| 182 | + ] |
| 183 | + media = [td.Medium(permittivity=2.25) for idx in range(0, len(viz_specs))] |
| 184 | + if use_viz_spec: |
| 185 | + media = [ |
| 186 | + td.Medium(permittivity=2.25, viz_spec=viz_specs[idx]) |
| 187 | + for idx in range(0, len(viz_specs)) |
| 188 | + ] |
| 189 | + |
| 190 | + structures = [] |
| 191 | + for idx in range(0, len(viz_specs)): |
| 192 | + center = tuple(list(rng.uniform(-3, 3, 2)) + [0]) |
| 193 | + size = tuple(rng.uniform(1, 2, 3)) |
| 194 | + box = td.Box(center=center, size=size) |
| 195 | + |
| 196 | + structures.append(td.Structure(geometry=box, medium=media[idx])) |
| 197 | + |
| 198 | + sim = td.Simulation( |
| 199 | + size=(10.0, 10.0, 10.0), |
| 200 | + run_time=1e-12, |
| 201 | + structures=structures, |
| 202 | + grid_spec=td.GridSpec(wavelength=1.0), |
| 203 | + ) |
| 204 | + |
| 205 | + sim.plot(z=0.0) |
| 206 | + plt.show() |
| 207 | + |
| 208 | + |
| 209 | +@pytest.mark.skip(reason="Skipping test for CI, but useful for debugging locally with graphics.") |
| 210 | +def test_plot_from_structure_local(): |
| 211 | + """ |
| 212 | + Local test for visualizing output when specifying visualization spec. |
| 213 | + """ |
| 214 | + plot_with_viz_spec(alpha=0.5, facecolor="red", edgecolor="blue") |
| 215 | + plot_with_viz_spec(alpha=0.1, facecolor="magenta", edgecolor="cyan") |
| 216 | + plot_with_viz_spec(alpha=0.9, facecolor="darkgreen", edgecolor="black") |
| 217 | + plot_with_viz_spec(alpha=0.8, facecolor="brown", edgecolor="deepskyblue") |
| 218 | + plot_with_viz_spec(alpha=0.2, facecolor="brown", edgecolor="deepskyblue") |
| 219 | + plot_with_viz_spec(alpha=1.0, facecolor="green") |
| 220 | + plot_with_viz_spec(alpha=0.75, facecolor="red", edgecolor="blue") |
| 221 | + plot_with_viz_spec(alpha=0.75, facecolor="red", edgecolor="blue", use_viz_spec=False) |
| 222 | + |
| 223 | + with pytest.raises(pd.ValidationError): |
| 224 | + plot_with_viz_spec(alpha=0.5, facecolor="dark green", edgecolor="blue") |
| 225 | + with pytest.raises(pd.ValidationError): |
| 226 | + plot_with_viz_spec(alpha=0.5, facecolor="red", edgecolor="ble") |
| 227 | + with pytest.raises(pd.ValidationError): |
| 228 | + plot_with_viz_spec(alpha=1.5, facecolor="red", edgecolor="blue") |
| 229 | + with pytest.raises(pd.ValidationError): |
| 230 | + plot_with_viz_spec(alpha=-0.5, facecolor="red", edgecolor="blue") |
| 231 | + |
| 232 | + |
| 233 | +@pytest.mark.skip(reason="Skipping test for CI, but useful for debugging locally with graphics.") |
| 234 | +def test_plot_multi_from_structure_local(rng): |
| 235 | + """ |
| 236 | + Local test for visualizing output when creating multiple structures with variety of |
| 237 | + visualization specs. |
| 238 | + """ |
| 239 | + plot_with_multi_viz_spec( |
| 240 | + alphas=[0.5, 0.75, 0.25, 0.4], |
| 241 | + facecolors=["red", "green", "blue", "orange"], |
| 242 | + edgecolors=["black", "cyan", "magenta", "brown"], |
| 243 | + rng=rng, |
| 244 | + ) |
| 245 | + plot_with_multi_viz_spec( |
| 246 | + alphas=[0.5, 0.75, 0.25, 0.4], |
| 247 | + facecolors=["red", "green", "blue", "orange"], |
| 248 | + edgecolors=["black", "cyan", "magenta", "brown"], |
| 249 | + rng=rng, |
| 250 | + use_viz_spec=False, |
| 251 | + ) |
0 commit comments