Skip to content

Commit

Permalink
Merge pull request #804 from gdsfactory/5471
Browse files Browse the repository at this point in the history
better plot names
  • Loading branch information
joamatab committed Oct 31, 2022
2 parents 728456b + 7c41bb1 commit 307b649
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# [CHANGELOG](https://keepachangelog.com/en/1.0.0/)

## 5.47.1

- improve simulation.plot.plot_sparameter kwargs
- replace with_sparameter_labels with with_simpler_input_keys

## 5.47.0

- integrate flayout to add all of generic components into KLayout [PR](https://github.com/gdsfactory/gdsfactory/pull/797)
Expand Down
10 changes: 9 additions & 1 deletion docs/notebooks/plugins/devsim/01_pin_waveguide.ipynb
Expand Up @@ -48,14 +48,22 @@
"You can change waveguide geometry (core thickness, slab thickness, core width), doping configuration (dopant level, dopant positions), as well as hyperparameters like adaptive mesh resolution at all the interfaces."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from gdsfactory.simulation.devsim import get_simulation_xsection"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%capture\n",
"from gdsfactory.simulation.devsim import get_simulation_xsection\n",
"\n",
"nm = 1e-9\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/notebooks/plugins/meep/001_meep_sparameters.ipynb
Expand Up @@ -785,7 +785,7 @@
},
"outputs": [],
"source": [
"gm.plot.plot_sparameters(sp, keys=[\"s31\"], with_sparameter_keys=True)"
"gm.plot.plot_sparameters(sp, keys=[\"s31\"], with_simpler_input_keys=True)"
]
},
{
Expand All @@ -796,7 +796,7 @@
},
"outputs": [],
"source": [
"gm.plot.plot_sparameters(sp, keys=[\"s41\"], with_sparameter_keys=True)"
"gm.plot.plot_sparameters(sp, keys=[\"s41\"], with_simpler_input_keys=True)"
]
},
{
Expand Down
47 changes: 31 additions & 16 deletions gdsfactory/simulation/plot.py
Expand Up @@ -11,18 +11,17 @@ def plot_sparameters(
sp: Dict[str, np.ndarray],
logscale: bool = True,
keys: Optional[Tuple[str, ...]] = None,
with_sparameter_labels: bool = True,
with_sparameter_keys: bool = False,
with_simpler_input_keys: bool = False,
with_simpler_labels: bool = True,
) -> None:
"""Plots Sparameters from a dict of np.ndarrays.
Args:
sp: Sparameters np.ndarray.
logscale: plots 20*log10(S).
keys: list of keys to plot, plots all by default.
with_sparameter_keys: uses S11, S12 in plot labels.
with_sparameter_keys: use Sparameter keys.
Assumes mode 0 and o1, o2, o3 port naming.
with_simpler_input_keys: You can use S12 keys instead of o1@0,o2@0.
with_simpler_labels: uses S11, S12 in plot labels instead of o1@0,o2@0.
.. plot::
:include-source:
Expand All @@ -38,20 +37,24 @@ def plot_sparameters(
keys = keys or [key for key in sp if not key.lower().startswith("wav")]

for key in keys:

if with_sparameter_keys:
if with_simpler_input_keys:
key = f"o{key[1]}@0,o{key[2]}@0"

if with_sparameter_labels and "o" in key and "@" in key:
port_mode1, port_mode2 = key.split(",")
if key not in sp:
raise ValueError(f"{key!r} not in {list(sp.keys())}")

if with_simpler_labels and "o" in key and "@" in key:
port_mode1_port_mode2 = key.split(",")
if len(port_mode1_port_mode2) != 2:
raise ValueError(f"{key!r} needs to be 'portin@mode,portout@mode'")
port_mode1, port_mode2 = port_mode1_port_mode2
port1, _mode1 = port_mode1.split("@")
port2, _mode2 = port_mode2.split("@")
alias = f"S{port1[1:]}{port2[1:]}"
else:
alias = key

if key not in sp:
raise ValueError(f"{key} not in {sp.keys()}")
raise ValueError(f"{key!r} not in {list(sp.keys())}")
y = sp[key]
y = 20 * np.log10(np.abs(y)) if logscale else np.abs(y) ** 2
plt.plot(w, y, label=alias)
Expand All @@ -68,15 +71,21 @@ def plot_imbalance2x2(
Args:
sp: sparameters dict np.ndarray.
port1: name.
port2: name.
port1: port1Name@modeIndex.
port2: port2Name@modeIndex.
"""
if port1 not in sp:
raise ValueError(f"{port1!r} not in {list(sp.keys())}")

if port2 not in sp:
raise ValueError(f"{port2!r} not in {list(sp.keys())}")

y1 = np.abs(sp[port1])
y2 = np.abs(sp[port2])
imbalance = y1 / y2
x = sp["wavelengths"] * 1e3
plt.plot(x, 100 * abs(imbalance))
plt.plot(x, abs(imbalance))
plt.xlabel("wavelength (nm)")
plt.ylabel("imbalance (%)")
plt.grid()
Expand All @@ -93,6 +102,11 @@ def plot_loss2x2(
port2: port name @ mode index. o1@0 is the fundamental mode for o1 port.
"""
if port1 not in sp:
raise ValueError(f"{port1!r} not in {list(sp.keys())}")

if port2 not in sp:
raise ValueError(f"{port2!r} not in {list(sp.keys())}")
y1 = np.abs(sp[port1])
y2 = np.abs(sp[port2])
x = sp["wavelengths"] * 1e3
Expand All @@ -109,5 +123,6 @@ def plot_loss2x2(
import gdsfactory.simulation as sim

sp = sim.get_sparameters_data_tidy3d(component=gf.components.mmi1x2)
plot_sparameters(sp, logscale=False, keys=["o1@0,o2@0"])
plt.show()
# plot_sparameters(sp, logscale=False, keys=["o1@0,o2@0"])
# plot_sparameters(sp, logscale=False, keys=["S21"])
# plt.show()

0 comments on commit 307b649

Please sign in to comment.