Skip to content

Commit

Permalink
Merge pull request #71 from gdsfactory/pydantic2_final
Browse files Browse the repository at this point in the history
port to pydantic 2
  • Loading branch information
joamatab authored Aug 31, 2023
2 parents fda3c3c + 65070e6 commit d3b10a0
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 57 deletions.
1 change: 0 additions & 1 deletion docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ sphinx:
- "sphinx.ext.napoleon"
- "sphinx.ext.viewcode"
- "matplotlib.sphinxext.plot_directive"
- "sphinxcontrib.autodoc_pydantic"
- "sphinxcontrib.bibtex"
config:
#autodoc_typehints: description
Expand Down
10 changes: 4 additions & 6 deletions docs/notebooks/tidy3d_01_tidy3d_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@
) # get the index of a material with a given refractive index float

# %%
gt.materials.get_index(
"SiO2"
) # get the index of a material with a name string, for the case that the refractive index has only one variant
# get the index of a material with a name string, for the case that the refractive index has only one variant
gt.materials.get_index("AlxOy")

# %%
gt.materials.get_index(
("cSi", "Li1993_293K")
) # get the index of a material with a name string, for the case that the refractive index has more than one variant
# get the index of a material with a name string, for the case that the refractive index has more than one variant
gt.materials.get_index(("cSi", "Li1993_293K"))

# %% [markdown]
# ## Waveguides
Expand Down
10 changes: 2 additions & 8 deletions gplugins/devsim/doping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from gdsfactory.generic_tech import LAYER
from gdsfactory.typings import Layer
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict


class DopingLayerLevel(BaseModel):
Expand All @@ -20,13 +20,7 @@ class DopingLayerLevel(BaseModel):
layer: Layer
type: str
z_profile: Callable
# xy_profile: Optional[Callable] = None # not implemented yet

class Config:
"""pydantic config."""

frozen = True
extra = "forbid"
model_config = ConfigDict(frozen=True, extra="forbid")


cm3_to_um3 = 1e-12
Expand Down
8 changes: 2 additions & 6 deletions gplugins/devsim/get_simulation_xsection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import pyvista as pv
import tidy3d as td
from devsim.python_packages import model_create, simple_physics
from pydantic import BaseModel, Extra
from pydantic import BaseModel, ConfigDict
from scipy.interpolate import griddata

from gplugins.tidy3d.materials import get_nk
Expand Down Expand Up @@ -171,11 +171,7 @@ class PINWaveguide(BaseModel):
atol: float = 1e8
rtol: float = 1e-8
max_iter: int = 60

class Config:
"""Enable adding new."""

extra = Extra.allow
model_config = ConfigDict(extra="allow")

# @property
# def t_sim(self):
Expand Down
3 changes: 2 additions & 1 deletion gplugins/tidy3d/materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,6 @@ def get_medium(spec: MaterialSpecTidy3d) -> td.Medium:
# print(get_index(spec="si"))
# print(get_index(spec=3.4))
# m = get_medium("SiO2")
m = get_medium(("cSi", "Li1993_293K"))
# m = get_medium(("cSi", "Li1993_293K"))
# m = td.Medium(permittivity=1.45 ** 2)
m = get_medium(td.material_library["cSi"]["Li1993_293K"])
33 changes: 15 additions & 18 deletions gplugins/tidy3d/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
from typing import Any, Literal

import numpy as np
import pydantic
import pydantic.v1 as pydantic
import tidy3d as td
import xarray
from gdsfactory.config import PATH, logger
from gdsfactory.typings import PathType
from pydantic import BaseModel
from pydantic.v1 import BaseModel, ConfigDict
from tidy3d.plugins import waveguide
from tqdm.auto import tqdm

Expand Down Expand Up @@ -58,7 +58,7 @@ def custom_serializer(data: str | float | BaseModel) -> str:
raise ValueError(f"Unsupported data type: {type(data)}")


class Waveguide(pydantic.BaseModel):
class Waveguide(BaseModel):
"""Waveguide Model.
All dimensions must be specified in μm (1e-6 m).
Expand Down Expand Up @@ -155,15 +155,11 @@ class Waveguide(pydantic.BaseModel):

_cached_data = pydantic.PrivateAttr()
_waveguide = pydantic.PrivateAttr()

class Config:
"""pydantic config."""

extra = "forbid"
model_config = ConfigDict(extra="forbid")

@pydantic.validator("wavelength")
def _fix_wavelength_type(cls, value):
return np.array(value, dtype=float)
def _fix_wavelength_type(cls, v):
return np.array(v, dtype=float)

@property
def filepath(self) -> pathlib.Path | None:
Expand Down Expand Up @@ -272,7 +268,7 @@ def _data(self):

wg = self.waveguide

fields = wg.mode_solver.data._centered_fields
fields = wg.mode_solver.data.field_components
self._cached_data = {
f + c: fields[f + c].squeeze(drop=True).values
for f in "EH"
Expand Down Expand Up @@ -377,13 +373,14 @@ def plot_grid(self) -> None:
"""Plot the waveguide grid."""
self.waveguide.plot_grid(z=0)

def plot_index(self, **kwargs) -> None:
def plot_index(self, **kwargs):
"""Plot the waveguide index distribution.
Keyword arguments are passed to xarray.DataArray.plot.
"""
artist = self.index.real.plot(**kwargs)
artist.axes.set_aspect("equal")
return artist

def plot_field(
self,
Expand All @@ -392,7 +389,7 @@ def plot_field(
mode_index: int = 0,
wavelength: float | None = None,
**kwargs,
) -> None:
):
"""Plot the selected field distribution from a waveguide mode.
Parameters:
Expand Down Expand Up @@ -445,6 +442,7 @@ def plot_field(
data_array.name = field_name
artist = data_array.plot(**kwargs)
artist.axes.set_aspect("equal")
return artist

def _ipython_display_(self) -> None:
"""Show index in matplotlib for Jupyter Notebooks."""
Expand Down Expand Up @@ -1006,23 +1004,22 @@ def sweep_coupling_length(
# overwrite=True
# )

import matplotlib.pyplot as plt

strip = Waveguide(
wavelength=1.55,
core_width=1.0,
slab_thickness=0.0,
# core_material="si",
core_material=td.material_library["cSi"]["Li1993_293K"],
# core_material=td.material_library["cSi"]["Li1993_293K"],
core_material=3.47,
clad_material="sio2",
core_thickness=220 * nm,
num_modes=4,
)
# strip._data
# strip.filepath
# strip.plot_index()
strip.plot_field("Ex", mode_index=0, wavelength=1.55, value="dB")
plt.show()
# strip.plot_field("Ex", mode_index=0, wavelength=1.55, value="dB")
# plt.show()
# w = np.linspace(400 * nm, 1000 * nm, 7)
# n_eff = sweep_n_eff(strip, core_width=w)
# fraction_te = sweep_fraction_te(strip, core_width=w)
Expand Down
7 changes: 4 additions & 3 deletions gplugins/tidy3d/tests/test_materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest
import tidy3d as td
from pydantic import ValidationError
from pydantic.v1 import ValidationError

import gplugins.tidy3d as gt

Expand Down Expand Up @@ -66,8 +66,8 @@ def test_material_library_many_variants() -> None:

def test_material_library_single_variant() -> None:
strip = gt.modes.Waveguide(
core_material="SiO2",
clad_material="sio2",
core_material="AlxOy",
clad_material="AlxOy",
**settings,
)
strip._data
Expand All @@ -84,6 +84,7 @@ def test_material_library() -> None:

if __name__ == "__main__":
pytest.main([__file__])
# test_material_validation_error()
# test_material_medium()
# test_material_float()
# test_material_library()
28 changes: 14 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ classifiers = [
"Operating System :: OS Independent"
]
dependencies = [
"gdsfactory[cad]>=7.3.2",
"gdsfactory[cad]>=7.4.0",
"pint"
]
description = "gdsfactory plugins"
Expand All @@ -26,12 +26,11 @@ requires-python = ">=3.10"
version = "0.3.1"

[project.optional-dependencies]
dagster = ["dagster", "dagit"]
database = [
"sqlalchemy",
"sqlalchemy-utils",
"dagster",
"dagit",
"sqlmodel",
# "sqlmodel>=0.0.8,<0.1",
"boto3",
"pymysql"
]
Expand All @@ -44,17 +43,16 @@ dev = [
"mypy",
"pyswarms",
"autograd",
"ray[tune,air]",
"hyperopt"
"hyperopt",
"ray"
]
devsim = [
"devsim",
"pyvista",
"tidy3d"
"tidy3d>=2.4.0rc2,<2.5.0"
]
docs = [
"jupytext",
"autodoc_pydantic",
"matplotlib",
"jupyter-book==0.15.1",
"pyvista[jupyter]"
Expand All @@ -74,14 +72,14 @@ gmsh = [
"meshwell>=0.0.9,<0.3.1"
]
klayout = [
"kfactory[git,ipy]==0.7.5"
"kfactory[git,ipy]>=0.8.4,<0.9"
]
meow = [
"meow-sim>=0.7.1,<0.8.0",
"tidy3d>=2.3.3,<2.4.0"
"meow-sim>=0.7.3,<0.8.0",
"tidy3d>=2.4.0rc2,<2.5.0"
]
sax = [
"sax>=0.8.8,<0.9.0",
"sax>=0.9.2,<0.10.0",
"jaxlib",
"jax",
"scikit-learn"
Expand All @@ -91,12 +89,12 @@ schematic = [
"natsort"
]
tidy3d = [
"tidy3d>=2.3.3,<2.4.0"
"tidy3d>=2.4.0rc2,<2.5.0"
]
web = [
"jinja2",
"python-multipart",
"fastapi",
"fastapi>=0.102.0,<1",
"uvicorn[standard]"
]

Expand Down Expand Up @@ -136,6 +134,8 @@ strict = true
addopts = '--tb=short'
norecursedirs = [
"extra/*.py",
'gplugins/dagster',
'gplugins/database',
'gplugins/devsim',
'gplugins/sax/integrations',
'gplugins/tidy3d/tests/tests_sparameters'
Expand Down

0 comments on commit d3b10a0

Please sign in to comment.