Skip to content

Commit

Permalink
avoid lambda functions
Browse files Browse the repository at this point in the history
  • Loading branch information
joamatab committed Jan 4, 2024
1 parent f653949 commit 6b230a1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
5 changes: 4 additions & 1 deletion gdsfactory/components/crossing_waveguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ def crossing(
return c


_taper = partial(taper, width2=2.5, length=3)


@cell
def crossing_from_taper(taper=lambda: taper(width2=2.5, length=3.0)) -> Component:
def crossing_from_taper(taper=_taper) -> Component:
"""Returns Crossing based on a taper.
The default is a dummy taper.
Expand Down
14 changes: 13 additions & 1 deletion gdsfactory/components/taper_adiabatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@
)


def neff_TE1550SOI_220nm(w: float) -> float:
"""Returns the effective index of the fundamental TE mode for a 220nm-thick core with 3.45 index, fully clad with 1.44 index.
Args:
w: width in um.
Returns:
effective index
"""
return np.poly1d(adiabatic_polyfit_TE1550SOI_220nm)(w)


@gf.cell
def taper_adiabatic(
width1: float = 0.5,
width2: float = 5.0,
length: float = 0,
neff_w: Callable = lambda w: np.poly1d(adiabatic_polyfit_TE1550SOI_220nm)(w),
neff_w: Callable = neff_TE1550SOI_220nm,
alpha: float = 1,
wavelength: float = 1.55,
npoints: int = 200,
Expand Down
11 changes: 11 additions & 0 deletions gdsfactory/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import inspect
import pathlib
from collections.abc import KeysView as dict_keys
from types import FunctionType
from typing import Any

import gdstk
Expand Down Expand Up @@ -71,6 +72,16 @@ def clean_value_json(
value = np.round(value, DEFAULT_SERIALIZATION_MAX_DIGITS)
return orjson.loads(orjson.dumps(value, option=orjson.OPT_SERIALIZE_NUMPY))

# Add a condition to handle lambda functions specifically
elif (
callable(value)
and isinstance(value, FunctionType)
and value.__name__ == "<lambda>"
):
raise ValueError(

Check warning on line 81 in gdsfactory/serialization.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/serialization.py#L81

Added line #L81 was not covered by tests
"Unable to serialize lambda function. Use a named function instead."
)

elif callable(value) and isinstance(value, functools.partial):
return clean_value_partial(value, include_module=True)

Expand Down
6 changes: 5 additions & 1 deletion test-data-regression/test_settings_crossing_from_taper_.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ module: gdsfactory.components.crossing_waveguide
name: crossing_from_taper
settings:
taper:
function: <lambda>
function: taper
module: gdsfactory.components.taper
settings:
length: 3
width2: 2.5
2 changes: 1 addition & 1 deletion test-data-regression/test_settings_taper_adiabatic_.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ settings:
cross_section: xs_sc
length: 0
neff_w:
function: <lambda>
function: neff_TE1550SOI_220nm
npoints: 200
wavelength: 1.55
width1: 0.5
Expand Down

0 comments on commit 6b230a1

Please sign in to comment.