Skip to content
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

better naming #2434

Merged
merged 11 commits into from Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion gdsfactory/add_pins.py
Expand Up @@ -531,7 +531,6 @@ def add_pins(

add_pins_optical = partial(add_pins, select_ports=select_ports_optical)
add_pins_electrical = partial(add_pins, select_ports=select_ports_electrical)
add_pins_container = partial(container, function=add_pins)

add_pins_triangle = partial(add_pins, function=add_pin_triangle)
add_pins_center = partial(add_pins, function=add_pin_rectangle)
Expand All @@ -542,6 +541,10 @@ def add_pins(
add_pins_inside1nm = partial(add_pins, function=add_pin_inside1nm)
add_pins_inside2um = partial(add_pins, function=add_pin_inside2um)

add_pins_container = partial(container, function=add_pins)
add_pins_container_center = partial(container, function=add_pins_center)
add_pins_container_siepic = partial(container, function=add_pins_siepic)


def add_settings_label(
component: Component,
Expand Down
62 changes: 42 additions & 20 deletions gdsfactory/component.py
Expand Up @@ -195,13 +195,28 @@
with_uuid: bool = False,
max_name_length: int | None = None,
) -> None:
"""Initialize the Component object."""
"""Initialize the Component object.

Args:
name: component_name. Use @cell decorator for auto-naming.
with_uuid: adds unique identifier.
max_name_length: maximum number of characters for component name.
"""

self.uid = str(uuid.uuid4())[:8]
if with_uuid or name == "Unnamed":

if with_uuid:
warnings.warn("with_uuid is deprecated. Use @cell decorator instead.")
name += f"_{self.uid}"

self._cell = gdstk.Cell("Unnamed")
if name == "Unnamed":
name = f"Unnamed_{self.uid}"

name_counters[name] += 1
if name_counters[name] > 1:
name = f"{name}${name_counters[name]-1}"

self._cell = gdstk.Cell(name)
self.rename(name, max_name_length=max_name_length)
self.info: Info = Info()

Expand Down Expand Up @@ -1199,8 +1214,11 @@
self.is_unlocked()
self._cell.add(*polygons)

def copy(self) -> Component:
return copy(self)
def copy(self, name: str | None = None) -> Component:
c = copy(self)
if name:
c.rename(name)

Check warning on line 1220 in gdsfactory/component.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/component.py#L1220

Added line #L1220 was not covered by tests
return c

def add_ref_container(self, component: Component) -> ComponentReference:
"""Add reference, ports and copy_child_info."""
Expand Down Expand Up @@ -1375,19 +1393,10 @@
_cell = _cell.flatten()
component_flat._cell = _cell
if single_layer is not None:
from gdsfactory import get_layer

layer, datatype = get_layer(single_layer)
for polygon in _cell.polygons:
polygon.layer = layer
polygon.datatype = datatype
for path in _cell.paths:
path.set_layers(layer)
path.set_datatypes(datatype)
warnings.warn("flatten on single layer is deprecated")

Check warning on line 1396 in gdsfactory/component.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/component.py#L1396

Added line #L1396 was not covered by tests

component_flat.copy_child_info(self)
component_flat.add_ports(self.ports)
component_flat.child = self.child
return component_flat

def flatten_reference(self, ref: ComponentReference) -> None:
Expand All @@ -1403,7 +1412,7 @@
from gdsfactory.functions import transformed

self.remove(ref)
new_component = transformed(ref, decorator=None)
new_component = transformed(ref)
self.add_ref(new_component, alias=ref.name)

def flatten_invalid_refs(self, *args, **kwargs) -> Component:
Expand All @@ -1419,19 +1428,22 @@
grid_size: float | None = None,
updated_components=None,
traversed_components=None,
keep_names: bool = False,
) -> Component:
"""Returns new component with flattened references so that they snap to grid.

Args:
grid_size: snap to grid size.
updated_components: set of updated components.
traversed_components: set of traversed components.
keep_names: True for writing to GDS, False for internal use.
"""
return flatten_offgrid_references_recursive(
self,
grid_size=grid_size,
updated_components=updated_components,
traversed_components=traversed_components,
keep_names=keep_names,
)

def add_ref(
Expand Down Expand Up @@ -1870,7 +1882,7 @@
)

if write_settings.flatten_offgrid_references:
top_cell = flatten_offgrid_references_recursive(self)
top_cell = flatten_offgrid_references_recursive(self, keep_names=True)
else:
top_cell = self
if not has_valid_transformations(self):
Expand Down Expand Up @@ -2732,6 +2744,7 @@
grid_size: float | None = None,
updated_components=None,
traversed_components=None,
keep_names: bool = False,
) -> Component:
"""Recursively flattens component references which have invalid transformations
(i.e. non-90 deg rotations or sub-grid translations)
Expand All @@ -2750,6 +2763,7 @@
Should always be None, except for recursive.
traversed_components: the set of component names which have been traversed.
Should always be None, except for recursive invocations.
keep_names: True for writing to GDS, False for internal use.
"""
from gdsfactory.decorators import is_invalid_ref

Expand Down Expand Up @@ -2779,7 +2793,10 @@
if invalid_refs or subcell_modified:
# if the cell or subcells need to have references flattened, create an uncached copy of this cell for export
new_component = component.copy()
new_component.rename(component.name, cache=False)
if keep_names:
new_component.rename(component.name, cache=False)
else:
new_component.rename(component.name + "_offgrid")

# make sure all modified cells have their references updated
new_refs = new_component.references.copy()
Expand Down Expand Up @@ -2824,8 +2841,13 @@
# from functools import partial
import gdsfactory as gf

c = gf.components.straight(length=1)
cc = gf.routing.add_fiber_array(c)
c1 = gf.Component()
c2 = gf.Component()
print(c1.name)
print(c2.name)

# c = gf.components.straight(length=1)
# cc = gf.routing.add_fiber_array(c)
# print(c.hash_geometry())
# c2 = c.flatten()

Expand Down
3 changes: 0 additions & 3 deletions gdsfactory/samples/07_flattening_device.py
Expand Up @@ -7,8 +7,6 @@

The Component.flatten() method returns a new flatten Component with all the
polygons inside the Component, and removes all the underlying references.
Also, if you specify the `single_layer` argument it will move all of the
polygons to that single layer.

"""
from __future__ import annotations
Expand All @@ -27,7 +25,6 @@ def flatten_device() -> Component:

assert len(c.references) == 3
c2 = c.flatten()
# c2 = c.flatten(single_layer=(34, 0))
assert len(c2.references) == 0
return c2

Expand Down
5 changes: 3 additions & 2 deletions gdsfactory/serialization.py
Expand Up @@ -45,13 +45,14 @@ def clean_value_json(
value: Any, fast_serialization: bool = False
) -> str | int | float | dict | list | bool | None:
"""Return JSON serializable object."""
from gdsfactory.component import Component
from gdsfactory.path import Path

if isinstance(value, pydantic.BaseModel):
return clean_dict(value.model_dump())

elif fast_serialization and hasattr(value, "hash_geometry"):
return value.hash_geometry()
elif fast_serialization and isinstance(value, Component):
return value.name

elif hasattr(value, "get_component_spec"):
return value.get_component_spec()
Expand Down
2 changes: 1 addition & 1 deletion test-data-regression/test_import_json_label.yml
Expand Up @@ -65,7 +65,7 @@ cell_settings:
doe: null
measurement: null
measurement_settings: {}
name: add_fiber_single_rotate_d179bff8
name: add_fiber_single_rotate_8549233d
port_names:
- opt_te_1530_15-straight_7688267d-loopback2
- opt_te_1530_15-straight_7688267d-1-0
Expand Down
2 changes: 1 addition & 1 deletion test-data-regression/test_import_ports_center.yml
@@ -1,5 +1,5 @@
function: ''
info: {}
module: ''
name: straight_4f1b76fe
name: straight_container_2d36ef1e
settings: {}
2 changes: 1 addition & 1 deletion test-data-regression/test_import_ports_inside.yml
@@ -1,5 +1,5 @@
function: ''
info: {}
module: ''
name: straight_1514d1a3
name: straight_container_ceeea98c
settings: {}
2 changes: 1 addition & 1 deletion test-data-regression/test_import_ports_siepic.yml
@@ -1,5 +1,5 @@
function: ''
info: {}
module: ''
name: straight_7688267d
name: straight_container_53d36f90
settings: {}
96 changes: 48 additions & 48 deletions test-data-regression/test_netlists_sample_doe_function_.yml
Expand Up @@ -53,54 +53,54 @@ warnings:
unconnected_ports:
- message: 56 unconnected optical ports!
ports:
- rings,ring_single_add_fiber_array_2d9b5d8c-o1
- rings,ring_single_add_fiber_array_2d9b5d8c-o2
- rings,ring_single_add_fiber_array_2d9b5d8c-loopback1
- rings,ring_single_add_fiber_array_2d9b5d8c-loopback2
- rings,ring_single_add_fiber_array_c9502bb8-o1
- rings,ring_single_add_fiber_array_c9502bb8-o2
- rings,ring_single_add_fiber_array_c9502bb8-loopback1
- rings,ring_single_add_fiber_array_c9502bb8-loopback2
- rings,ring_single_add_fiber_array_d77ef29b-o1
- rings,ring_single_add_fiber_array_d77ef29b-o2
- rings,ring_single_add_fiber_array_d77ef29b-loopback1
- rings,ring_single_add_fiber_array_d77ef29b-loopback2
- rings,ring_single_add_fiber_array_5275b32c-o1
- rings,ring_single_add_fiber_array_5275b32c-o2
- rings,ring_single_add_fiber_array_5275b32c-loopback1
- rings,ring_single_add_fiber_array_5275b32c-loopback2
- rings,ring_single_add_fiber_array_bdb5bd75-o1
- rings,ring_single_add_fiber_array_bdb5bd75-o2
- rings,ring_single_add_fiber_array_bdb5bd75-loopback1
- rings,ring_single_add_fiber_array_bdb5bd75-loopback2
- rings,ring_single_add_fiber_array_e8b42cfe-o1
- rings,ring_single_add_fiber_array_e8b42cfe-o2
- rings,ring_single_add_fiber_array_e8b42cfe-loopback1
- rings,ring_single_add_fiber_array_e8b42cfe-loopback2
- rings,ring_single_add_fiber_array_db95022f-o1
- rings,ring_single_add_fiber_array_db95022f-o2
- rings,ring_single_add_fiber_array_db95022f-loopback1
- rings,ring_single_add_fiber_array_db95022f-loopback2
- rings,ring_single_add_fiber_array_1b9122c6-o1
- rings,ring_single_add_fiber_array_1b9122c6-o2
- rings,ring_single_add_fiber_array_1b9122c6-loopback1
- rings,ring_single_add_fiber_array_1b9122c6-loopback2
- rings,ring_single_add_fiber_array_f6584048-o1
- rings,ring_single_add_fiber_array_f6584048-o2
- rings,ring_single_add_fiber_array_f6584048-loopback1
- rings,ring_single_add_fiber_array_f6584048-loopback2
- rings,ring_single_add_fiber_array_9171869a-o1
- rings,ring_single_add_fiber_array_9171869a-o2
- rings,ring_single_add_fiber_array_9171869a-loopback1
- rings,ring_single_add_fiber_array_9171869a-loopback2
- rings,ring_single_add_fiber_array_9a749ac3-o1
- rings,ring_single_add_fiber_array_9a749ac3-o2
- rings,ring_single_add_fiber_array_9a749ac3-loopback1
- rings,ring_single_add_fiber_array_9a749ac3-loopback2
- rings,ring_single_add_fiber_array_65354b69-o1
- rings,ring_single_add_fiber_array_65354b69-o2
- rings,ring_single_add_fiber_array_65354b69-loopback1
- rings,ring_single_add_fiber_array_65354b69-loopback2
- rings,ring_single_add_fiber_array_d51f0345-o1
- rings,ring_single_add_fiber_array_d51f0345-o2
- rings,ring_single_add_fiber_array_d51f0345-loopback1
- rings,ring_single_add_fiber_array_d51f0345-loopback2
- rings,ring_single_add_fiber_array_1a6e693e-o1
- rings,ring_single_add_fiber_array_1a6e693e-o2
- rings,ring_single_add_fiber_array_1a6e693e-loopback1
- rings,ring_single_add_fiber_array_1a6e693e-loopback2
- rings,ring_single_add_fiber_array_22fae60b-o1
- rings,ring_single_add_fiber_array_22fae60b-o2
- rings,ring_single_add_fiber_array_22fae60b-loopback1
- rings,ring_single_add_fiber_array_22fae60b-loopback2
- rings,ring_single_add_fiber_array_a3040416-o1
- rings,ring_single_add_fiber_array_a3040416-o2
- rings,ring_single_add_fiber_array_a3040416-loopback1
- rings,ring_single_add_fiber_array_a3040416-loopback2
- rings,ring_single_add_fiber_array_b4f08058-o1
- rings,ring_single_add_fiber_array_b4f08058-o2
- rings,ring_single_add_fiber_array_b4f08058-loopback1
- rings,ring_single_add_fiber_array_b4f08058-loopback2
- rings,ring_single_add_fiber_array_f5917333-o1
- rings,ring_single_add_fiber_array_f5917333-o2
- rings,ring_single_add_fiber_array_f5917333-loopback1
- rings,ring_single_add_fiber_array_f5917333-loopback2
- rings,ring_single_add_fiber_array_8370bce9-o1
- rings,ring_single_add_fiber_array_8370bce9-o2
- rings,ring_single_add_fiber_array_8370bce9-loopback1
- rings,ring_single_add_fiber_array_8370bce9-loopback2
- rings,ring_single_add_fiber_array_fb47609b-o1
- rings,ring_single_add_fiber_array_fb47609b-o2
- rings,ring_single_add_fiber_array_fb47609b-loopback1
- rings,ring_single_add_fiber_array_fb47609b-loopback2
- rings,ring_single_add_fiber_array_2a578449-o1
- rings,ring_single_add_fiber_array_2a578449-o2
- rings,ring_single_add_fiber_array_2a578449-loopback1
- rings,ring_single_add_fiber_array_2a578449-loopback2
- rings,ring_single_add_fiber_array_7514afdd-o1
- rings,ring_single_add_fiber_array_7514afdd-o2
- rings,ring_single_add_fiber_array_7514afdd-loopback1
- rings,ring_single_add_fiber_array_7514afdd-loopback2
- rings,ring_single_add_fiber_array_114579ae-o1
- rings,ring_single_add_fiber_array_114579ae-o2
- rings,ring_single_add_fiber_array_114579ae-loopback1
- rings,ring_single_add_fiber_array_114579ae-loopback2
- rings,ring_single_add_fiber_array_18bb78d9-o1
- rings,ring_single_add_fiber_array_18bb78d9-o2
- rings,ring_single_add_fiber_array_18bb78d9-loopback1
- rings,ring_single_add_fiber_array_18bb78d9-loopback2
- mzis,0_0-o1
- mzis,0_0-o2
- mzis,0_0-loopback1
Expand Down
@@ -1,7 +1,7 @@
function: add_electrical_pads_shortest
info: {}
module: gdsfactory.routing.add_electrical_pads_shortest
name: mzi_add_electrical_pads_shortest_735a9dd9
name: mzi_add_electrical_pads_shortest_componentmzi_2e88f57c
settings:
component:
function: mzi
Expand Down
@@ -1,7 +1,7 @@
function: add_electrical_pads_top
info: {}
module: gdsfactory.routing.add_electrical_pads_top
name: mzi_add_electrical_pads_top_735a9dd9
name: mzi_add_electrical_pads_top_componentmzi_2e88f57c
settings:
component:
function: mzi
Expand Down
2 changes: 1 addition & 1 deletion test-data-regression/test_settings_add_fiber_single_.yml
@@ -1,7 +1,7 @@
function: add_fiber_single
info: {}
module: gdsfactory.routing.add_fiber_single
name: mzi_add_fiber_single_735a9dd9
name: mzi_add_fiber_single_componentmzi_2e88f57c
settings:
bend:
function: bend_euler
Expand Down
@@ -1,7 +1,7 @@
function: container
info: {}
module: gdsfactory.cell
name: mzi_container_af4e3ad7
name: mzi_container_f93074c0
settings:
component:
function: mzi
Expand Down
2 changes: 1 addition & 1 deletion test-data-regression/test_settings_add_pads_bot_.yml
@@ -1,7 +1,7 @@
function: add_pads_bot
info: {}
module: gdsfactory.routing.add_pads
name: mzi_add_pads_bot_735a9dd9
name: mzi_add_pads_bot_componentmzi_2e88f57c
settings:
bend: wire_corner
component:
Expand Down
2 changes: 1 addition & 1 deletion test-data-regression/test_settings_add_pads_top_.yml
@@ -1,7 +1,7 @@
function: add_pads_top
info: {}
module: gdsfactory.routing.add_pads
name: add_pads_bot_add_pads_top_735a9dd9
name: add_pads_bot_add_pads_top_componentmzi_2e88f57c
settings:
component:
function: mzi
Expand Down