-
Notifications
You must be signed in to change notification settings - Fork 187
/
add_labels.py
306 lines (256 loc) · 9.5 KB
/
add_labels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""Add Label to each component port."""
from __future__ import annotations
from collections.abc import Callable
from functools import partial
import gdsfactory as gf
from gdsfactory.component import Component, ComponentReference
from gdsfactory.port import Port
from gdsfactory.typings import ComponentOrReference, Label, LayerSpec
def get_text_dash(
port: Port,
gc: ComponentReference | Component,
gc_index: int | None = None,
component_name: str | None = None,
prefix: str = "",
suffix: str = "",
) -> str:
"""Returns text with `GratingName-ComponentName-PortName`."""
gc_name = gc.name if isinstance(gc, Component) else gc.parent.name
return f"{prefix}{gc_name}-{component_name or port.parent.name}-{port.name}{suffix}"
def get_text_dash_loopback(
port: Port,
gc: ComponentReference | Component,
gc_index: int | None = None,
component_name: str | None = None,
prefix: str = "",
) -> str:
"""Returns text with `GratingName-ComponentName-Loopback`."""
gc_name = gc.name if isinstance(gc, Component) else gc.parent.name
return f"{prefix}{gc_name}-{component_name or port.parent.name}-loopback_{gc_index}"
def get_text_underscore(
port: Port,
gc: ComponentReference | Component,
gc_index: int | None = None,
component_name: str | None = None,
component_prefix: str = "",
prefix: str = "opt",
suffix: str = "",
) -> str:
"""Returns text string for an optical port based on grating coupler.
{label_prefix}_{polarization}_{wavelength_nm}_({prefix}{component_name})
Args:
port: to label.
gc: grating coupler component or reference.
gc_index: grating_coupler index, which grating_coupler we are labelling.
component_name: optional name.
component_prefix: component prefix.
prefix: prefix to add to the label.
"""
polarization = gc.info.get("polarization") or gc.metadata_child.get("polarization")
wavelength = gc.info.get("wavelength") or gc.metadata_child.get("wavelength")
if polarization not in ["te", "tm"]:
raise ValueError(f"polarization {polarization!r} needs to be [te, tm]")
if not isinstance(wavelength, int | float) or not 0.5 < wavelength < 5.0:
raise ValueError(
f"{wavelength} needs to be > 0.5um and < 5um. Make sure it's in um"
)
component_name = component_name or port.parent.metadata_child.get("name")
text = f"{prefix}_{polarization}_{int(wavelength*1e3)}_({component_prefix}{component_name}){suffix}"
if isinstance(gc_index, int):
text += f"_{gc_index}_{port.name}"
else:
text = f"_{port.name}"
return text
get_input_label_text_loopback = partial(get_text_underscore, prefix="loopback_")
def add_labels(
component: Component,
get_label_function: Callable = get_text_underscore,
layer_label: LayerSpec = "TEXT",
gc: Component | None = None,
**kwargs,
) -> Component:
"""Returns component with labels on ports.
Args:
component: to add labels to.
get_label_function: function to get label.
layer_label: layer_label.
gc: Optional grating coupler.
keyword Args:
layer: port GDS layer.
prefix: with in port name.
suffix: select ports with port name suffix.
orientation: in degrees.
width: for ports to add label.
layers_excluded: List of layers to exclude.
port_type: optical, electrical, ...
clockwise: if True, sort ports clockwise, False: counter-clockwise.
Returns:
original component with labels.
"""
ports = component.get_ports_list(**kwargs)
for i, port in enumerate(ports):
label = get_label_function(
port=port,
gc=gc,
gc_index=i,
component_name=component.name,
layer_label=layer_label,
)
component.add(label)
return component
def add_siepic_labels(
component: Component,
model: str = "auto",
library: str = "auto",
label_layer: LayerSpec = "DEVREC",
spice_params: dict | list | str | None = None,
label_spacing: float = 0.2,
) -> Component:
"""Adds labels and returns the same component.
Args:
component: component.
model: Lumerical Interconnect model.
'auto' attempts to extract this from the cross_section.
library: Lumerical Interconnect library.
'auto' attempts to extract this from the cross_section.
label_layer: layer for writing SiEPIC labels.
spice_params: spice parameters (in microns).
Either pass in a dict with parameter, value pairs, or pass
a list of values to extract from component info.
label_spacing: separation distance between labels in um.
"""
c = component
labels = []
if model:
if model == "auto" and "model" in c.info:
model = c.info["model"]
labels.append(f"Component={model}")
if library:
if library == "auto" and "library" in c.info:
library = c.info["library"]
labels.append(f"Lumerical_INTERCONNECT_library={library}")
if spice_params and c.info["layout_model_property_pairs"]:
if spice_params == "auto":
pairs = c.info["layout_model_property_pairs"]
spice_params = {pair[1]: c.info[pair[0]] for pair in pairs}
param_str = ""
for param in spice_params:
val = spice_params[param]
param_str += f"{param}={val:.3f}u "
labels.append(f"Spice_param:{param_str}")
for i, text in enumerate(labels):
c.add_label(
text=text, position=(0, i * label_spacing), layer=label_layer, anchor="w"
)
return c
def add_labels_to_ports(
component: Component,
label_layer: LayerSpec = "TEXT",
port_type: str | None = None,
**kwargs,
) -> Component:
"""Add labels to component ports.
Args:
component: to add labels.
label_layer: layer spec for the label.
port_type: to select ports.
keyword Args:
layer: select ports with GDS layer.
prefix: select ports with prefix in port name.
suffix: select ports with port name suffix.
orientation: select ports with orientation in degrees.
width: select ports with port width.
layers_excluded: List of layers to exclude.
port_type: select ports with port_type (optical, electrical, vertical_te).
clockwise: if True, sort ports clockwise, False: counter-clockwise.
"""
ports = component.get_ports_list(port_type=port_type, **kwargs)
for port in ports:
component.add_label(text=port.name, position=port.center, layer=label_layer)
return component
def add_labels_to_ports_x_y(
component: Component,
label_layer: LayerSpec = "TEXT",
port_type: str | None = None,
**kwargs,
) -> Component:
"""Add labels to component ports. Prepends -x-y coordinates
Args:
component: to add labels.
label_layer: layer spec for the label.
port_type: to select ports.
keyword Args:
layer: select ports with GDS layer.
prefix: select ports with prefix in port name.
suffix: select ports with port name suffix.
orientation: select ports with orientation in degrees.
width: select ports with port width.
layers_excluded: List of layers to exclude.
port_type: select ports with port_type (optical, electrical, vertical_te).
clockwise: if True, sort ports clockwise, False: counter-clockwise.
"""
ports = component.get_ports_list(port_type=port_type, **kwargs)
for port in ports:
x, y = port.center
component.add_label(
text=f"{port.name}/{int(x)}/{int(y)}",
position=port.center,
layer=label_layer,
)
return component
add_labels_to_ports_electrical = partial(
add_labels_to_ports,
port_type="electrical",
)
add_labels_to_ports_optical = partial(
add_labels_to_ports,
port_type="optical",
)
add_labels_to_ports_vertical_dc = partial(
add_labels_to_ports,
port_type="vertical_dc",
)
add_labels_to_ports_opt = partial(add_labels_to_ports, prefix="opt", port_type=None)
def get_labels(
component: ComponentOrReference,
get_text_function: Callable = get_text_dash,
layer_label: LayerSpec = "TEXT",
gc: Component | None = None,
component_name: str | None = None,
**kwargs,
) -> list[Label]:
"""Returns component labels on ports.
Args:
component: to add labels to.
get_label_text: function to get label.
layer_label: layer_label.
gc: Optional grating coupler.
component_name: optional component name.
keyword Args:
layer: port GDS layer.
prefix: look for prefix in port name.
suffix: select ports with port name suffix.
orientation: in degrees.
width: for ports to add label.
layers_excluded: List of layers to exclude.
port_type: optical, electrical, ...
clockwise: if True, sort ports clockwise, False: counter-clockwise.
Returns:
list of component labels.
"""
labels = []
ports = component.get_ports_list(**kwargs)
component_name = component_name or component.name
for i, port in enumerate(ports):
label = get_text_function(
port=port,
gc=gc,
gc_index=i,
component_name=component_name,
layer_label=layer_label,
)
labels.append(label)
return labels
if __name__ == "__main__":
c = gf.components.pad()
c.show()