Skip to content

Commit c377111

Browse files
committed
fix: lazy import scipy and matplotlib
1 parent cf98f4e commit c377111

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

tidy3d/components/dispersion_fitter.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Optional, Tuple
66

77
import numpy as np
8-
import scipy
98
from pydantic.v1 import Field, NonNegativeFloat, PositiveFloat, PositiveInt, validator
109
from rich.progress import Progress
1110

@@ -493,6 +492,11 @@ def real_weighted_matrix(self, matrix: ArrayComplex2D) -> ArrayFloat2D:
493492
def iterate_poles(self) -> FastFitterData:
494493
"""Perform a single iteration of the pole-updating procedure."""
495494

495+
try:
496+
import scipy
497+
except ImportError:
498+
raise ImportError("scipy is required to fit the dispersion.")
499+
496500
def compute_zeros(residues: ArrayComplex1D, d_tilde: float) -> ArrayComplex1D:
497501
"""Compute the zeros from the residues."""
498502
size = len(self.real_poles) + 2 * len(self.complex_poles)
@@ -593,6 +597,12 @@ def compute_zeros(residues: ArrayComplex1D, d_tilde: float) -> ArrayComplex1D:
593597

594598
def fit_residues(self) -> FastFitterData:
595599
"""Fit residues."""
600+
601+
try:
602+
import scipy
603+
except ImportError:
604+
raise ImportError("scipy is required to fit the dispersion.")
605+
596606
# build the matrices
597607
if self.optimize_eps_inf:
598608
poly_len = 1
@@ -650,6 +660,11 @@ def iterate_fit(self) -> FastFitterData:
650660
def iterate_passivity(self, passivity_omega: ArrayFloat1D) -> Tuple[FastFitterData, int]:
651661
"""Iterate passivity enforcement algorithm."""
652662

663+
try:
664+
import scipy
665+
except ImportError:
666+
raise ImportError("scipy is required to fit the dispersion.")
667+
653668
size = len(self.real_poles) + 2 * len(self.complex_poles)
654669
constraint_matrix = np.imag(self.pole_matrix_omega(passivity_omega))
655670

tidy3d/components/medium.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import numpy as npo
1616
import pydantic.v1 as pd
1717
import xarray as xr
18-
from scipy import signal
1918

2019
from tidy3d.components.material.tcad.heat import ThermalSpecType
2120

@@ -3482,6 +3481,11 @@ def _real_partial_fraction_decomposition(
34823481
34833482
"""
34843483

3484+
try:
3485+
from scipy import signal
3486+
except ImportError:
3487+
raise ImportError("scipy is required to use this method.")
3488+
34853489
if a.ndim != 1 or np.any(np.iscomplex(a)):
34863490
raise ValidationError(
34873491
"Numerator coefficients must be a one-dimensional array of real numbers."

tidy3d/components/mode/mode_solver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import numpy as np
1212
import pydantic.v1 as pydantic
1313
import xarray as xr
14-
from matplotlib.collections import PatchCollection
15-
from matplotlib.patches import Rectangle
1614

1715
from ...constants import C_0
1816
from ...exceptions import SetupError, ValidationError
@@ -2315,6 +2313,13 @@ def _plot_pml(
23152313
cls, simulation: Simulation, plane: Box, mode_spec: ModeSpec, ax: Ax = None
23162314
) -> Ax:
23172315
"""Plot the mode plane absorbing boundaries."""
2316+
2317+
try:
2318+
from matplotlib.collections import PatchCollection
2319+
from matplotlib.patches import Rectangle
2320+
except ImportError:
2321+
raise ImportError("matplotlib is required to plot the mode plane absorbing boundaries.")
2322+
23182323
# Get the mode plane normal axis, center, and limits.
23192324
_, h_lim, v_lim, _ = cls._center_and_lims(simulation=simulation, plane=plane)
23202325

0 commit comments

Comments
 (0)