Skip to content

Commit c77b6f4

Browse files
Gregory Robertsyaugenst-flex
authored andcommitted
frontend: only warning on VisualizationSpec color validation when matplotlib is not installed
1 parent f94f607 commit c77b6f4

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
- Warn if more than 20 frequencies are used in EME, as this may lead to slower or more expensive simulations.
2929
- EME now supports 2D simulations.
3030
- 'EMESimulation' now supports 'PermittivityMonitor'.
31+
- Change `VisualizationSpec` validator for checking validity of user specified colors to only issue a warning if matplotlib is not installed instead of an error.
3132

3233
### Fixed
3334
- Fixed issue with `CustomMedium` gradients where other frequencies would wrongly contribute to the gradient.

tests/test_components/test_viz.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from tidy3d.constants import inf
1111
from tidy3d.exceptions import Tidy3dKeyError
1212

13+
from ..utils import AssertLogLevel
14+
1315
pytestmark = pytest.mark.usefixtures("mpl_config_noninteractive")
1416

1517

@@ -218,6 +220,17 @@ def plot_with_multi_viz_spec(alphas, facecolors, edgecolors, rng, use_viz_spec=T
218220
plt.show()
219221

220222

223+
def test_no_matlab_install(monkeypatch):
224+
"""Test that the `VisualizationSpec` only throws a warning on validation if matplotlib is not installed."""
225+
monkeypatch.setattr("tidy3d.components.viz.MATPLOTLIB_IMPORTED", False)
226+
227+
EXPECTED_WARNING_MSG_PIECE = (
228+
"matplotlib was not successfully imported, but is required to validate colors"
229+
)
230+
with AssertLogLevel("WARNING", contains_str=EXPECTED_WARNING_MSG_PIECE):
231+
viz_spec = td.VisualizationSpec(facecolor="green")
232+
233+
221234
@pytest.mark.skip(reason="Skipping test for CI, but useful for debugging locally with graphics.")
222235
def test_plot_from_structure_local():
223236
"""

tidy3d/components/viz.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
from typing import Any, Dict, Optional
88

99
import pydantic.v1 as pd
10+
from numpy import array, concatenate, inf, ones
11+
12+
from ..constants import UnitScaling
13+
from ..exceptions import SetupError, Tidy3dKeyError
14+
from ..log import log
15+
from .base import Tidy3dBaseModel
16+
from .types import Ax, Axis, LengthUnit
1017

18+
MATPLOTLIB_IMPORTED = True
1119
try:
1220
import matplotlib.pyplot as plt
1321
import matplotlib.ticker as ticker
@@ -19,13 +27,7 @@
1927
arrow_style = ArrowStyle.Simple(head_length=12, head_width=9, tail_width=4)
2028
except ImportError:
2129
arrow_style = None
22-
23-
from numpy import array, concatenate, inf, ones
24-
25-
from ..constants import UnitScaling
26-
from ..exceptions import SetupError, Tidy3dKeyError
27-
from .base import Tidy3dBaseModel
28-
from .types import Ax, Axis, LengthUnit
30+
MATPLOTLIB_IMPORTED = False
2931

3032
""" Constants """
3133

@@ -185,8 +187,15 @@ class PlotParams(AbstractPlotParams):
185187

186188

187189
def is_valid_color(value: str) -> str:
188-
if not is_color_like(value):
189-
raise pd.ValidationError(f"{value} is not a valid plotting color")
190+
if not MATPLOTLIB_IMPORTED:
191+
log.warning(
192+
"matplotlib was not successfully imported, but is required "
193+
"to validate colors in the VisualizationSpec. The specified colors "
194+
"have not been validated."
195+
)
196+
else:
197+
if not is_color_like(value):
198+
raise ValueError(f"{value} is not a valid plotting color")
190199

191200
return value
192201

0 commit comments

Comments
 (0)