Skip to content

Commit

Permalink
Merge pull request #1308 from gdsfactory/643port_pandas
Browse files Browse the repository at this point in the history
document `Component.get_ports_pandas()`
  • Loading branch information
joamatab committed Feb 19, 2023
2 parents c5d17bc + 46e7f89 commit 6c13381
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 25 deletions.
103 changes: 82 additions & 21 deletions docs/notebooks/01_references.ipynb
Expand Up @@ -6,9 +6,9 @@
"source": [
"# References and ports\n",
"\n",
"GDS allows your the component once in memory and reference or Instance the component multiple times.\n",
"GDS allows your the component once in memory and Reference or Instance the component multiple times.\n",
"\n",
"`gdstk` calls it `reference` and `klayout` calls them `instance`"
"`gdstk` and `gdspy` calls it `Reference` and `klayout` calls it `Instance`"
]
},
{
Expand All @@ -32,7 +32,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import gdsfactory as gf\n",
Expand Down Expand Up @@ -508,7 +510,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"size = 4\n",
Expand All @@ -519,7 +523,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c = gf.components.straight_heater_metal(length=30)\n",
Expand All @@ -529,7 +535,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.ports"
Expand All @@ -545,7 +553,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.get_ports_dict(layer=(1, 0))"
Expand All @@ -561,7 +571,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.get_ports_dict(width=0.5)"
Expand All @@ -570,7 +582,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c0 = gf.components.straight_heater_metal()\n",
Expand All @@ -580,7 +594,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c1 = c0.copy()\n",
Expand All @@ -591,7 +607,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c2 = c0.copy()\n",
Expand Down Expand Up @@ -628,7 +646,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c = gf.Component(\"demo_ports\")\n",
Expand All @@ -641,7 +661,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ref.get_ports_list() # by default returns ports clockwise starting from bottom left west facing port"
Expand All @@ -650,7 +672,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.auto_rename_ports()\n",
Expand Down Expand Up @@ -678,7 +702,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.auto_rename_ports_counter_clockwise()\n",
Expand All @@ -688,7 +714,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.get_ports_list(clockwise=False)"
Expand All @@ -697,7 +725,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.ports_layer"
Expand All @@ -706,7 +736,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.port_by_orientation_cw(\"W0\")"
Expand All @@ -715,7 +747,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.port_by_orientation_ccw(\"W1\")"
Expand All @@ -731,7 +765,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"cross_section = gf.cross_section.strip()\n",
Expand All @@ -746,12 +782,37 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c.ports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"df = c.get_ports_pandas()\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"df[df.port_type == 'optical']"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
71 changes: 70 additions & 1 deletion docs/notebooks/common_mistakes.ipynb
Expand Up @@ -119,7 +119,7 @@
"source": [
"### 1.3 Intermediate Unnamed cells\n",
"\n",
"While creating a cell, you should not create intermediate cells, because they won't get a name."
"While creating a cell, you should not create intermediate cells, because they won't be Cached and you can end up with duplicated cell names or name conflicts, where one of the cells that has the same name as the other will be replaced."
]
},
{
Expand Down Expand Up @@ -200,6 +200,75 @@
"print(c.references)\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14ca7490-c1fd-4bc7-bf5e-f6253dfe5bed",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import gdsfactory as gf\n",
"\n",
"\n",
"@gf.cell\n",
"def dangerous_intermediate_cells(width=0.5):\n",
" \"\"\"Example that will show the dangers of using intermediate cells.\"\"\"\n",
" c = gf.Component(\"safe\")\n",
"\n",
" c2 = gf.Component(\n",
" \"dangerous\"\n",
" ) # This should be forbidden as it will create duplicated cells\n",
" c2 << gf.components.hline(width=width)\n",
" c << c2\n",
"\n",
" return c\n",
"\n",
"\n",
"@gf.cell\n",
"def using_dangerous_intermediate_cells():\n",
" \"\"\"Example on how things can go wrong.\n",
"\n",
" Here we try to create to lines with different widths\n",
" they end up with two duplicated cells and a name collision on the intermediate cell\n",
" \"\"\"\n",
" c = gf.Component()\n",
" r2 = c << dangerous_intermediate_cells(width=0.5)\n",
" r3 = c << dangerous_intermediate_cells(width=2)\n",
" r3.movey(5)\n",
" return c\n",
"\n",
"\n",
"c = using_dangerous_intermediate_cells()\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "884cf4ef-cbc0-477e-9999-e70e44e84d96",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"for component in c.get_dependencies(recursive=True):\n",
" if not component._locked:\n",
" print(\n",
" f\"Component {component.name!r} was NOT properly locked. \"\n",
" \"You need to write it into a function that has the @cell decorator.\"\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a169f98-ffdd-44fc-bc7e-b20ca7187b0e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
23 changes: 20 additions & 3 deletions gdsfactory/component.py
Expand Up @@ -1283,18 +1283,35 @@ def _ipython_display_(self) -> None:
"""Show geometry in KLayout and in matplotlib for Jupyter Notebooks."""
self.show(show_ports=True) # show in klayout
self.plot_klayout()
print(self)

def plot_klayout(self) -> None:
def plot_klayout(
self,
show_ports: bool = True,
port_marker_layer: Layer = (1, 10),
) -> None:
"""Returns ipython widget for klayout visualization.
Defaults to matplotlib if it fails to import ipywidgets.
Args:
show_ports: shows component with port markers and labels.
port_marker_layer: for the ports.
"""
from gdsfactory.add_pins import add_pins_triangle

if show_ports:
component = self.copy()
component.name = self.name
add_pins_triangle(component=component, layer=port_marker_layer)
else:
component = self
try:
from gdsfactory.pdk import get_layer_views
from gdsfactory.widgets.layout_viewer import LayoutViewer
from IPython.display import display

gdspath = self.write_gds(logging=False)
gdspath = component.write_gds(logging=False)
lyp_path = gdspath.with_suffix(".lyp")

layer_views = get_layer_views()
Expand All @@ -1305,7 +1322,7 @@ def plot_klayout(self) -> None:
print(
"You can install `pip install gdsfactory[full]` for better visualization"
)
self.plot(plotter="matplotlib")
component.plot(plotter="matplotlib")

def plot_jupyter(self):
"""Shows current gds in klayout. Uses Kweb if server running.
Expand Down
2 changes: 2 additions & 0 deletions tests/test_add_ports/test_add_ports_from_pins.yml
Expand Up @@ -8,6 +8,7 @@ ports:
name: o1
orientation: 180
port_type: optical
shear_angle: null
width: 0.5
o2:
center:
Expand All @@ -17,5 +18,6 @@ ports:
name: o2
orientation: 0
port_type: optical
shear_angle: null
width: 0.5
settings: {}

0 comments on commit 6c13381

Please sign in to comment.