-
Notifications
You must be signed in to change notification settings - Fork 186
/
from_yaml.py
1287 lines (1079 loc) · 34.5 KB
/
from_yaml.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Returns Component from YAML syntax.
name: myComponent
settings:
length: 3
info:
description: just a demo
polarization: TE
...
instances:
mzi:
component: mzi_phase_shifter
settings:
delta_length: ${settings.length}
length_x: 50
pads:
component: pad_array
settings:
n: 2
port_names:
- e4
placements:
mzi:
x: 0
pads:
y: 200
x: mzi,cc
ports:
o1: mzi,o1
o2: mzi,o2
routes:
electrical:
links:
mzi,etop_e1: pads,e4_0
mzi,etop_e2: pads,e4_1
settings:
layer: [31, 0]
width: 10
radius: 10
"""
from __future__ import annotations
import importlib
import io
import pathlib
import warnings
from typing import IO, Any, Callable, Dict, List, Optional, Union
import numpy as np
from omegaconf import DictConfig, OmegaConf
from typing_extensions import Literal
from gdsfactory.add_pins import add_instance_label
from gdsfactory.cell import cell
from gdsfactory.component import Component, ComponentReference
from gdsfactory.routing.factories import routing_strategy as routing_strategy_factories
from gdsfactory.types import Route
valid_placement_keys = [
"x",
"y",
"xmin",
"xmax",
"ymin",
"ymax",
"dx",
"dy",
"rotation",
"mirror",
"port",
]
valid_top_level_keys = [
"name",
"instances",
"placements",
"connections",
"ports",
"routes",
"settings",
"info",
"pdk",
"warnings",
]
valid_anchor_point_keywords = [
"ce",
"cw",
"nc",
"ne",
"nw",
"sc",
"se",
"sw",
"center",
"cc",
]
# refer to an (x,y) Point
valid_anchor_value_keywords = [
"south",
"west",
"east",
"north",
]
# refer to a singular (x or y) value
valid_anchor_keywords = valid_anchor_point_keywords + valid_anchor_value_keywords
# full set of valid anchor keywords (either referring to points or values)
valid_route_keys = [
"links",
"settings",
"routing_strategy",
]
# Recognized keys within a YAML route definition
def _get_anchor_point_from_name(
ref: ComponentReference, anchor_name: str
) -> Optional[np.ndarray]:
if anchor_name in valid_anchor_point_keywords:
return getattr(ref.size_info, anchor_name)
elif anchor_name in ref.ports:
return ref.ports[anchor_name].center
else:
return None
def _get_anchor_value_from_name(
ref: ComponentReference, anchor_name: str, return_value: str
) -> Optional[float]:
if anchor_name in valid_anchor_value_keywords:
return getattr(ref.size_info, anchor_name)
anchor_point = _get_anchor_point_from_name(ref, anchor_name)
if anchor_point is None:
return None
if return_value == "x":
return anchor_point[0]
elif return_value == "y":
return anchor_point[1]
else:
raise ValueError("Expected x or y as return_value.")
def _move_ref(
x: Union[str, float],
x_or_y: Literal["x", "y"],
placements_conf,
connections_by_transformed_inst,
instances,
encountered_insts,
all_remaining_insts,
) -> float:
if not isinstance(x, str):
return x
if len(x.split(",")) != 2:
raise ValueError(
f"You can define {x_or_y} as `{x_or_y}: instanceName,portName` got `{x_or_y}: {x!r}`"
)
instance_name_ref, port_name = x.split(",")
if instance_name_ref in all_remaining_insts:
place(
placements_conf,
connections_by_transformed_inst,
instances,
encountered_insts,
instance_name_ref,
all_remaining_insts,
)
if instance_name_ref not in instances:
raise ValueError(
f"{instance_name_ref!r} not in {list(instances.keys())}."
f" You can define {x_or_y} as `{x_or_y}: instanceName,portName`, got {x_or_y}: {x!r}"
)
if (
port_name not in instances[instance_name_ref].ports
and port_name not in valid_anchor_keywords
):
ports = list(instances[instance_name_ref].ports.keys())
raise ValueError(
f"port = {port_name!r} can be a port_name in {ports}, "
f"an anchor {valid_anchor_keywords} for {instance_name_ref!r}, "
f"or `{x_or_y}: instanceName,portName`, got `{x_or_y}: {x!r}`"
)
return _get_anchor_value_from_name(instances[instance_name_ref], port_name, x_or_y)
def place(
placements_conf: Dict[str, Dict[str, Union[int, float, str]]],
connections_by_transformed_inst: Dict[str, Dict[str, str]],
instances: Dict[str, ComponentReference],
encountered_insts: List[str],
instance_name: Optional[str] = None,
all_remaining_insts: Optional[List[str]] = None,
) -> None:
"""Place instance_name based on placements_conf config.
Args:
placements_conf: Dict of instance_name to placement (x, y, rotation ...).
connections_by_transformed_inst: Dict of connection attributes.
keyed by the name of the instance which should be transformed.
instances: Dict of references.
encountered_insts: list of encountered_instances.
instance_name: instance_name to place.
all_remaining_insts: list of all the remaining instances to place
instances pop from this instance as they are placed.
"""
if not all_remaining_insts:
return
if instance_name is None:
instance_name = all_remaining_insts.pop(0)
else:
all_remaining_insts.remove(instance_name)
if instance_name in encountered_insts:
encountered_insts.append(instance_name)
loop_str = " -> ".join(encountered_insts)
raise ValueError(
f"circular reference in placement for {instance_name}! Loop: {loop_str}"
)
encountered_insts.append(instance_name)
if instance_name not in instances:
raise ValueError(f"{instance_name!r} not in {list(instances.keys())}")
ref = instances[instance_name]
if instance_name in placements_conf:
placement_settings = placements_conf[instance_name] or {}
if not isinstance(placement_settings, dict):
raise ValueError(
f"Invalid placement {placement_settings} from {valid_placement_keys}"
)
for k in placement_settings.keys():
if k not in valid_placement_keys:
raise ValueError(f"Invalid placement {k} from {valid_placement_keys}")
x = placement_settings.get("x")
xmin = placement_settings.get("xmin")
xmax = placement_settings.get("xmax")
y = placement_settings.get("y")
ymin = placement_settings.get("ymin")
ymax = placement_settings.get("ymax")
dx = placement_settings.get("dx")
dy = placement_settings.get("dy")
port = placement_settings.get("port")
rotation = placement_settings.get("rotation")
mirror = placement_settings.get("mirror")
if mirror:
if mirror is True and port:
ref.mirror_x(x0=_get_anchor_value_from_name(ref, port, "x"))
elif mirror is True:
if x:
ref.mirror_x(x0=x)
else:
ref.mirror_x()
elif mirror is False:
pass
elif isinstance(mirror, str):
ref.mirror_x(port_name=mirror)
elif isinstance(mirror, (int, float)):
ref.mirror_x(x0=mirror)
else:
raise ValueError(
f"{mirror!r} can only be a port name {ref.ports.keys()}, "
"x value or True/False"
)
if port:
a = _get_anchor_point_from_name(ref, port)
if a is None:
raise ValueError(
f"Port {port!r} is neither a valid port on {ref.parent.name!r}"
" nor a recognized anchor keyword.\n"
"Valid ports: \n"
f"{list(ref.ports.keys())}. \n"
"Valid keywords: \n"
f"{valid_anchor_point_keywords}",
)
ref.x -= a[0]
ref.y -= a[1]
if x is not None:
ref.x += _move_ref(
x,
x_or_y="x",
placements_conf=placements_conf,
connections_by_transformed_inst=connections_by_transformed_inst,
instances=instances,
encountered_insts=encountered_insts,
all_remaining_insts=all_remaining_insts,
)
# print(instance_name, x, xmin, xmax, y, ymin, ymax)
# print(ymin, y or ymin or ymax)
if y is not None:
ref.y += _move_ref(
y,
x_or_y="y",
placements_conf=placements_conf,
connections_by_transformed_inst=connections_by_transformed_inst,
instances=instances,
encountered_insts=encountered_insts,
all_remaining_insts=all_remaining_insts,
)
if rotation:
if port:
ref.rotate(rotation, center=_get_anchor_point_from_name(ref, port))
else:
x, y = ref.origin
ref.rotate(rotation, center=(x, y))
# ref.rotate(rotation, center=(ref.x, ref.y))
if ymin is not None and ymax is not None:
raise ValueError("You cannot set ymin and ymax")
elif ymax is not None:
ref.ymax = _move_ref(
ymax,
x_or_y="y",
placements_conf=placements_conf,
connections_by_transformed_inst=connections_by_transformed_inst,
instances=instances,
encountered_insts=encountered_insts,
all_remaining_insts=all_remaining_insts,
)
elif ymin is not None:
ref.ymin = _move_ref(
ymin,
x_or_y="y",
placements_conf=placements_conf,
connections_by_transformed_inst=connections_by_transformed_inst,
instances=instances,
encountered_insts=encountered_insts,
all_remaining_insts=all_remaining_insts,
)
if xmin is not None and xmax is not None:
raise ValueError("You cannot set xmin and xmax")
elif xmin is not None:
ref.xmin = _move_ref(
xmin,
x_or_y="x",
placements_conf=placements_conf,
connections_by_transformed_inst=connections_by_transformed_inst,
instances=instances,
encountered_insts=encountered_insts,
all_remaining_insts=all_remaining_insts,
)
elif xmax is not None:
ref.xmax = _move_ref(
xmax,
x_or_y="x",
placements_conf=placements_conf,
connections_by_transformed_inst=connections_by_transformed_inst,
instances=instances,
encountered_insts=encountered_insts,
all_remaining_insts=all_remaining_insts,
)
if dx:
ref.x += dx
if dy:
ref.y += dy
if instance_name in connections_by_transformed_inst:
conn_info = connections_by_transformed_inst[instance_name]
instance_dst_name = conn_info["instance_dst_name"]
if instance_dst_name in all_remaining_insts:
place(
placements_conf,
connections_by_transformed_inst,
instances,
encountered_insts,
instance_dst_name,
all_remaining_insts,
)
make_connection(instances=instances, **conn_info)
# placements_conf.pop(instance_name)
def transform_connections_dict(connections_conf: Dict[str, str]) -> Dict[str, Dict]:
"""Returns Dict with source_instance_name key and connection properties."""
if not connections_conf:
return {}
attrs_by_src_inst = {}
for port_src_string, port_dst_string in connections_conf.items():
instance_src_name, port_src_name = port_src_string.split(",")
instance_dst_name, port_dst_name = port_dst_string.split(",")
attrs_by_src_inst[instance_src_name] = {
"instance_src_name": instance_src_name,
"port_src_name": port_src_name,
"instance_dst_name": instance_dst_name,
"port_dst_name": port_dst_name,
}
return attrs_by_src_inst
def make_connection(
instance_src_name: str,
port_src_name: str,
instance_dst_name: str,
port_dst_name: str,
instances: Dict[str, ComponentReference],
) -> None:
"""Connect instance_src_name,port to instance_dst_name,port.
Args:
instance_src_name: source instance name.
port_src_name: from instance_src_name.
instance_dst_name: destination instance name.
port_dst_name: from instance_dst_name.
instances: dict of instances.
"""
instance_src_name = instance_src_name.strip()
instance_dst_name = instance_dst_name.strip()
port_src_name = port_src_name.strip()
port_dst_name = port_dst_name.strip()
if instance_src_name not in instances:
raise ValueError(f"{instance_src_name!r} not in {list(instances.keys())}")
if instance_dst_name not in instances:
raise ValueError(f"{instance_dst_name!r} not in {list(instances.keys())}")
instance_src = instances[instance_src_name]
instance_dst = instances[instance_dst_name]
if port_src_name not in instance_src.ports:
raise ValueError(
f"{port_src_name} not in {list(instance_src.ports.keys())} for"
f" {instance_src_name!r} "
)
if port_dst_name not in instance_dst.ports:
raise ValueError(
f"{port_dst_name!r} not in {list(instance_dst.ports.keys())} for"
f" {instance_dst_name!r}"
)
port_dst = instance_dst.ports[port_dst_name]
instance_src.connect(port=port_src_name, destination=port_dst)
sample_mmis = """
name: sample_mmis
info:
polarization: te
wavelength: 1.55
description: just a demo on adding metadata
instances:
mmi_long:
component: mmi1x2
settings:
width_mmi: 4.5
length_mmi: 10
mmi_short:
component: mmi1x2
settings:
width_mmi: 4.5
length_mmi: 5
placements:
mmi_long:
rotation: 180
x: 100
y: 100
routes:
route_name1:
links:
mmi_short,o2: mmi_long,o1
ports:
o1: mmi_short,o1
o2: mmi_long,o2
o3: mmi_long,o3
"""
def from_yaml(
yaml_str: Union[str, pathlib.Path, IO[Any], Dict[str, Any], DictConfig],
routing_strategy: Dict[str, Callable] = routing_strategy_factories,
label_instance_function: Callable = add_instance_label,
name: Optional[str] = None,
prefix: Optional[str] = None,
**kwargs,
) -> Component:
"""Returns Component from YAML string or file.
YAML includes instances, placements, routes, ports and connections.
Args:
yaml: YAML string or file.
routing_strategy: for each route.
label_instance_function: to label each instance.
name: Optional name.
prefix: name prefix.
kwargs: function settings for creating YAML PCells.
.. code::
valid variables:
name: Optional Component name
settings: Optional variables
pdk: overrides
info: Optional component info
description: just a demo
polarization: TE
...
instances:
name:
component: (ComponentSpec)
settings (Optional)
length: 10
...
placements:
x: Optional[float, str] str can be instanceName,portName
y: Optional[float, str]
rotation: Optional[float]
mirror: Optional[bool, float] float is x mirror axis
port: Optional[str] port anchor
connections (Optional): between instances
ports (Optional): ports to expose
routes (Optional): bundles of routes
routeName:
library: optical
links:
instance1,port1: instance2,port2
.. code::
settings:
length_mmi: 5
instances:
mmi_bot:
component: mmi1x2
settings:
width_mmi: 4.5
length_mmi: 10
mmi_top:
component: mmi1x2
settings:
width_mmi: 4.5
length_mmi: ${settings.length_mmi}
placements:
mmi_top:
port: o1
x: 0
y: 0
mmi_bot:
port: o1
x: mmi_top,o2
y: mmi_top,o2
dx: 30
dy: -30
routes:
optical:
library: optical
links:
mmi_top,o3: mmi_bot,o1
"""
if isinstance(yaml_str, (str, pathlib.Path, IO)):
yaml_str = (
io.StringIO(yaml_str)
if isinstance(yaml_str, str) and "\n" in yaml_str
else yaml_str
)
conf = OmegaConf.load(
yaml_str
) # nicer loader than conf = yaml.safe_load(yaml_str)
else:
conf = OmegaConf.create(yaml_str)
for key in conf.keys():
if key not in valid_top_level_keys:
raise ValueError(f"{key!r} not in {list(valid_top_level_keys)}")
settings = conf.get("settings", {})
mode = kwargs.pop("mode") if "mode" in kwargs else "layout"
for key, value in kwargs.items():
if key not in settings:
raise ValueError(f"{key!r} not in {settings.keys()}")
else:
conf["settings"][key] = value
return _from_yaml(
conf=OmegaConf.to_container(conf, resolve=True),
routing_strategy=routing_strategy,
label_instance_function=label_instance_function,
prefix=prefix or conf.get("name", "Unnamed"),
name=name,
mode=mode,
)
@cell
def _from_yaml(
conf,
routing_strategy: Dict[str, Callable] = routing_strategy_factories,
label_instance_function: Callable = add_instance_label,
mode: str = "layout",
) -> Component:
"""Returns component from YAML decorated with cell for caching and autonaming.
Args:
conf: dict.
routing_strategy: for each route.
label_instance_function: to label each instance.
"""
from gdsfactory.generic_tech import get_generic_pdk
from gdsfactory.pdk import get_active_pdk
GENERIC = get_generic_pdk()
c = Component()
instances = {}
routes = {}
placements_conf = conf.get("placements")
routes_conf = conf.get("routes")
ports_conf = conf.get("ports")
connections_conf = conf.get("connections")
instances_dict = conf["instances"]
pdk = conf.get("pdk")
c.info = conf.get("info", {})
if pdk and pdk == "generic":
GENERIC.activate()
elif pdk:
module = importlib.import_module(pdk)
pdk = module.PDK
if pdk is None:
raise ValueError(f"'from {pdk} import PDK' failed")
pdk.activate()
pdk = get_active_pdk()
if mode == "layout":
component_getter = pdk.get_component
elif mode == "schematic":
component_getter = pdk.get_symbol
else:
raise ValueError(
f"{mode} is not a recognized mode. Please choose 'layout' or 'schematic'"
)
for instance_name in instances_dict:
instance_conf = instances_dict[instance_name]
component = instance_conf["component"]
settings = instance_conf.get("settings", {})
component_spec = {"component": component, "settings": settings}
component = component_getter(component_spec)
ref = c.add_ref(component, alias=instance_name)
instances[instance_name] = ref
placements_conf = dict() if placements_conf is None else placements_conf
connections_by_transformed_inst = transform_connections_dict(connections_conf)
components_to_place = set(placements_conf.keys())
components_with_placement_conflicts = components_to_place.intersection(
connections_by_transformed_inst.keys()
)
for instance_name in components_with_placement_conflicts:
placement_settings = placements_conf[instance_name]
if "x" in placement_settings or "y" in placement_settings:
warnings.warn(
f"YAML defined: ({', '.join(components_with_placement_conflicts)}) "
"with both connection and placement. Please use one or the other.",
)
all_remaining_insts = list(
set(placements_conf.keys()).union(set(connections_by_transformed_inst.keys()))
)
while all_remaining_insts:
place(
placements_conf=placements_conf,
connections_by_transformed_inst=connections_by_transformed_inst,
instances=instances,
encountered_insts=[],
all_remaining_insts=all_remaining_insts,
)
for instance_name in instances_dict:
label_instance_function(
component=c, instance_name=instance_name, reference=instances[instance_name]
)
if routes_conf:
for route_alias in routes_conf:
route_names = []
ports1 = []
ports2 = []
routes_dict = routes_conf[route_alias]
for key in routes_dict.keys():
if key not in valid_route_keys:
raise ValueError(
f"{route_alias!r} key={key!r} not in {valid_route_keys}"
)
settings = routes_dict.pop("settings", {})
routing_strategy_name = routes_dict.pop("routing_strategy", "get_bundle")
if routing_strategy_name not in routing_strategy:
routing_strategies = list(routing_strategy.keys())
raise ValueError(
f"{routing_strategy_name!r} is an invalid routing_strategy "
f"{routing_strategies}"
)
if "links" not in routes_dict:
raise ValueError(
f"You need to define links for the {route_alias!r} route"
)
links_dict = routes_dict["links"]
for port_src_string, port_dst_string in links_dict.items():
if ":" in port_src_string:
src, src0, src1 = (s.strip() for s in port_src_string.split(":"))
dst, dst0, dst1 = (s.strip() for s in port_dst_string.split(":"))
instance_src_name, port_src_name = (
s.strip() for s in src.split(",")
)
instance_dst_name, port_dst_name = (
s.strip() for s in dst.split(",")
)
src0 = int(src0)
src1 = int(src1)
dst0 = int(dst0)
dst1 = int(dst1)
if src1 > src0:
ports1names = [
f"{port_src_name}{i}" for i in range(src0, src1 + 1)
]
else:
ports1names = [
f"{port_src_name}{i}" for i in range(src0, src1 - 1, -1)
]
if dst1 > dst0:
ports2names = [
f"{port_dst_name}{i}" for i in range(dst0, dst1 + 1)
]
else:
ports2names = [
f"{port_dst_name}{i}" for i in range(dst0, dst1 - 1, -1)
]
if len(ports1names) != len(ports2names):
raise ValueError(f"{ports1names} different from {ports2names}")
route_names += [
f"{instance_src_name},{i}:{instance_dst_name},{j}"
for i, j in zip(ports1names, ports2names)
]
instance_src = instances[instance_src_name]
instance_dst = instances[instance_dst_name]
for port_src_name in ports1names:
if port_src_name not in instance_src.ports:
raise ValueError(
f"{port_src_name!r} not in {list(instance_src.ports.keys())}"
f"for {instance_src_name!r} "
)
ports1.append(instance_src.ports[port_src_name])
for port_dst_name in ports2names:
if port_dst_name not in instance_dst.ports:
raise ValueError(
f"{port_dst_name!r} not in {list(instance_dst.ports.keys())}"
f"for {instance_dst_name!r}"
)
ports2.append(instance_dst.ports[port_dst_name])
else:
instance_src_name, port_src_name = port_src_string.split(",")
instance_dst_name, port_dst_name = port_dst_string.split(",")
instance_src_name = instance_src_name.strip()
instance_dst_name = instance_dst_name.strip()
port_src_name = port_src_name.strip()
port_dst_name = port_dst_name.strip()
if instance_src_name not in instances:
raise ValueError(
f"{instance_src_name!r} not in {list(instances.keys())}"
)
if instance_dst_name not in instances:
raise ValueError(
f"{instance_dst_name!r} not in {list(instances.keys())}"
)
instance_src = instances[instance_src_name]
instance_dst = instances[instance_dst_name]
if port_src_name not in instance_src.ports:
raise ValueError(
f"{port_src_name!r} not in {list(instance_src.ports.keys())} for"
f" {instance_src_name!r} "
)
if port_dst_name not in instance_dst.ports:
raise ValueError(
f"{port_dst_name!r} not in {list(instance_dst.ports.keys())} for"
f" {instance_dst_name!r}"
)
ports1.append(instance_src.ports[port_src_name])
ports2.append(instance_dst.ports[port_dst_name])
route_name = f"{port_src_string}:{port_dst_string}"
route_names.append(route_name)
routing_function = routing_strategy[routing_strategy_name]
route_or_route_list = routing_function(
ports1=ports1,
ports2=ports2,
**settings,
)
# FIXME, be more consistent
if isinstance(route_or_route_list, list):
for route_name, route_dict in zip(route_names, route_or_route_list):
c.add(route_dict.references)
routes[route_name] = route_dict.length
elif isinstance(route_or_route_list, Route):
c.add(route_or_route_list.references)
routes[route_name] = route_or_route_list.length
else:
raise ValueError(f"{route_or_route_list} needs to be a Route or a list")
if ports_conf:
if not hasattr(ports_conf, "items"):
raise ValueError(f"{ports_conf} needs to be a dict")
for port_name, instance_comma_port in ports_conf.items():
if "," in instance_comma_port:
instance_name, instance_port_name = instance_comma_port.split(",")
instance_name = instance_name.strip()
instance_port_name = instance_port_name.strip()
if instance_name not in instances:
raise ValueError(
f"{instance_name!r} not in {list(instances.keys())}"
)
instance = instances[instance_name]
if instance_port_name not in instance.ports:
raise ValueError(
f"{instance_port_name!r} not in {list(instance.ports.keys())} for"
f" {instance_name!r} "
)
c.add_port(port_name, port=instance.ports[instance_port_name])
else:
c.add_port(**instance_comma_port)
c.routes = routes
c.info["instances"] = list(instances.keys())
return c
sample_pdk = """
pdk: ubcpdk
info:
polarization: te
wavelength: 1.55
description: mzi for ubcpdk
instances:
yr:
component: y_splitter
yl:
component: y_splitter
placements:
yr:
rotation: 180
x: 100
y: 100
routes:
route_top:
links:
yl,opt2: yr,opt3
route_bot:
links:
yl,opt3: yr,opt2
routing_strategy: get_bundle_from_steps
ports:
o1: yl,opt1
o2: yr,opt2
o3: yr,opt3
"""
sample_pdk_mzi = """
name: mzi
pdk: ubcpdk
info:
polarization: te
wavelength: 1.55
description: mzi for ubcpdk
instances:
yr:
component: y_splitter
yl:
component: y_splitter
placements:
yr:
rotation: 180
x: 100
y: 0
routes:
route_top:
links:
yl,opt2: yr,opt3
route_bot:
links:
yl,opt3: yr,opt2
routing_strategy: get_bundle_from_steps
settings:
steps: [dx: 30, dy: -40, dx: 20]
ports:
o1: yl,opt1
o2: yr,opt2
o3: yr,opt3
"""
sample_pdk_mzi_settings = """
name: mzi
pdk: ubcpdk
settings:
dy: -70
info:
polarization: te
wavelength: 1.55
description: mzi for ubcpdk
instances:
yr:
component: ebeam_y_1550
yl:
component: ebeam_y_1550
placements:
yr:
rotation: 180
x: 100
y: 0
routes:
route_top:
links:
yl,opt2: yr,opt3
settings:
cross_section: strip
route_bot:
links:
yl,opt3: yr,opt2
routing_strategy: get_bundle_from_steps
settings: