Skip to content

fix: lazy import scipy and matplotlib #2379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion tidy3d/components/dispersion_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Optional, Tuple

import numpy as np
import scipy
from pydantic.v1 import Field, NonNegativeFloat, PositiveFloat, PositiveInt, validator
from rich.progress import Progress

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

try:
import scipy
except ImportError:
raise ImportError("scipy is required to fit the dispersion.")

def compute_zeros(residues: ArrayComplex1D, d_tilde: float) -> ArrayComplex1D:
"""Compute the zeros from the residues."""
size = len(self.real_poles) + 2 * len(self.complex_poles)
Expand Down Expand Up @@ -593,6 +597,12 @@ def compute_zeros(residues: ArrayComplex1D, d_tilde: float) -> ArrayComplex1D:

def fit_residues(self) -> FastFitterData:
"""Fit residues."""

try:
import scipy
except ImportError:
raise ImportError("scipy is required to fit the dispersion.")

# build the matrices
if self.optimize_eps_inf:
poly_len = 1
Expand Down Expand Up @@ -650,6 +660,11 @@ def iterate_fit(self) -> FastFitterData:
def iterate_passivity(self, passivity_omega: ArrayFloat1D) -> Tuple[FastFitterData, int]:
"""Iterate passivity enforcement algorithm."""

try:
import scipy
except ImportError:
raise ImportError("scipy is required to fit the dispersion.")

size = len(self.real_poles) + 2 * len(self.complex_poles)
constraint_matrix = np.imag(self.pole_matrix_omega(passivity_omega))

Expand Down
6 changes: 5 additions & 1 deletion tidy3d/components/medium.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import numpy as npo
import pydantic.v1 as pd
import xarray as xr
from scipy import signal

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

Expand Down Expand Up @@ -3482,6 +3481,11 @@ def _real_partial_fraction_decomposition(

"""

try:
from scipy import signal
except ImportError:
raise ImportError("scipy is required to use this method.")

if a.ndim != 1 or np.any(np.iscomplex(a)):
raise ValidationError(
"Numerator coefficients must be a one-dimensional array of real numbers."
Expand Down
9 changes: 7 additions & 2 deletions tidy3d/components/mode/mode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import numpy as np
import pydantic.v1 as pydantic
import xarray as xr
from matplotlib.collections import PatchCollection
from matplotlib.patches import Rectangle

from ...constants import C_0
from ...exceptions import SetupError, ValidationError
Expand Down Expand Up @@ -2315,6 +2313,13 @@ def _plot_pml(
cls, simulation: Simulation, plane: Box, mode_spec: ModeSpec, ax: Ax = None
) -> Ax:
"""Plot the mode plane absorbing boundaries."""

try:
from matplotlib.collections import PatchCollection
from matplotlib.patches import Rectangle
except ImportError:
raise ImportError("matplotlib is required to plot the mode plane absorbing boundaries.")

# Get the mode plane normal axis, center, and limits.
_, h_lim, v_lim, _ = cls._center_and_lims(simulation=simulation, plane=plane)

Expand Down