Skip to content

fix(tidy3d): lazy load scipy to reduce import time #2543

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

Merged
merged 4 commits into from
Jun 10, 2025

Conversation

frederikschubertflex
Copy link
Contributor

@frederikschubertflex frederikschubertflex commented Jun 5, 2025

Move scipy into the functions where it is used to reduce the import time. Related to #2422 and #2087. pandas or other libraries are more involved due to cross-dependencies via xarray, so I focused on this instead.

python -X importtime -c "from tidy3d import Simulation"

Before

import time: self [us] | cumulative | imported package
...
import time:      1429 |    1165589 | tidy3d

After

import time: self [us] | cumulative | imported package
...
import time:      1523 |     919501 | tidy3d

Copy link
Contributor

github-actions bot commented Jun 5, 2025

Diff Coverage

Diff: origin/develop...HEAD, staged and unstaged changes

  • tidy3d/components/dispersion_fitter.py (100%)
  • tidy3d/components/geometry/base.py (0.0%): Missing lines 1389
  • tidy3d/components/medium.py (100%)
  • tidy3d/components/mode/derivatives.py (100%)
  • tidy3d/components/mode/solver.py (84.6%): Missing lines 32,904
  • tidy3d/material_library/parametric_materials.py (60.0%): Missing lines 233-234
  • tidy3d/plugins/autograd/primitives/interpolate.py (100%)
  • tidy3d/plugins/design/method.py (87.5%): Missing lines 17
  • tidy3d/plugins/dispersion/fit.py (100%)
  • tidy3d/plugins/microwave/lobe_measurer.py (100%)
  • tidy3d/plugins/resonance/resonance.py (100%)

Summary

  • Total: 44 lines
  • Missing: 6 lines
  • Coverage: 86%

tidy3d/components/geometry/base.py

  1385         """
  1386         import gdstk
  1387 
  1388         if not isinstance(cell, gdstk.Cell):
! 1389             if "gdstk" in cell.__class__.__name__.lower():
  1390                 raise Tidy3dImportError(
  1391                     "Module 'gdstk' not found. It is required to export shapes to gdstk cells."
  1392                 )
  1393             raise Tidy3dImportError("Argument 'cell' must be an instance of 'gdstk.Cell'.")

tidy3d/components/mode/solver.py

  28 # double precision. This value is very heuristic.
  29 GOOD_CONDUCTOR_CUT_OFF = 1e70
  30 
  31 if TYPE_CHECKING:
! 32     from scipy import sparse as sp
  33 
  34 # Consider a material to be good conductor if |ep| (or |mu|) > GOOD_CONDUCTOR_THRESHOLD * |pec_val|
  35 GOOD_CONDUCTOR_THRESHOLD = 0.9

  900         if isinstance(vec_or_mat, csr_matrix):
  901             mat_norm = spl.norm(vec_or_mat)
  902             mat_imag_norm = spl.norm(vec_or_mat.imag)
  903             return mat_imag_norm / (mat_norm + fp_eps) > tol
! 904         raise RuntimeError(
  905             f"Variable type should be either numpy array or scipy.sparse.csr_matrix, got {type(vec_or_mat)}."
  906         )
  907 
  908     @classmethod

tidy3d/material_library/parametric_materials.py

  229         try:
  230             from scipy import integrate
  231 
  232             INTEGRATE_AVAILABLE = True
! 233         except ImportError:
! 234             INTEGRATE_AVAILABLE = False
  235 
  236         def fermi(E: float) -> float:
  237             """Fermi distribution."""
  238             # catch overflow warning

tidy3d/plugins/design/method.py

  13 
  14 from .parameter import ParameterAny, ParameterFloat, ParameterInt, ParameterType
  15 
  16 if TYPE_CHECKING:
! 17     from scipy.stats import qmc as qmc_type
  18 
  19 
  20 class Method(Tidy3dBaseModel, ABC):
  21     """Spec for a sweep algorithm, with a method to run it."""

@frederikschubertflex frederikschubertflex requested review from daquinteroflex and removed request for yaugenst-flex June 9, 2025 14:11
@daquinteroflex
Copy link
Collaborator

Looks good! One in

@frederikschubertflex frederikschubertflex enabled auto-merge (squash) June 10, 2025 08:45
@frederikschubertflex frederikschubertflex merged commit 105d757 into develop Jun 10, 2025
23 checks passed
@frederikschubertflex frederikschubertflex deleted the frederikschubertflex/lazy-imports branch June 10, 2025 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants