Skip to content

Placer

placer

Placer of bends/straights from a route.

cells_from_yaml

cells_from_yaml(
    inp: Path,
    kcl: KCLayout = stdkcl,
    additional_classes: list[object] | None = None,
    verbose: bool = False,
) -> None

Recreate cells from a yaml file.

Parameters:

Name Type Description Default
inp Path

Input file path.

required
kcl KCLayout

KCLayout to load the cells into.

kcl
additional_classes list[object] | None

Additional yaml classes that should be registered. This is used for example to enable loading additional yaml files etc.

None
verbose bool

Print more verbose errors etc.

False
Source code in kfactory/placer.py
 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
def cells_from_yaml(
    inp: Path,
    kcl: KCLayout = stdkcl,
    additional_classes: list[object] | None = None,
    verbose: bool = False,
) -> None:
    """Recreate cells from a yaml file.

    Args:
        inp: Input file path.
        kcl: KCLayout to load the cells into.
        additional_classes: Additional yaml classes that should be registered.
            This is used for example to enable loading additional yaml files etc.
        verbose: Print more verbose errors etc.
    """
    yaml = get_yaml_obj()
    yaml.register_class(
        include_from_loader(inp.parent, kcl, additional_classes, verbose)
    )

    register_classes(
        yaml,
        kcl,
        additional_classes,
        verbose,
    )
    yaml.load(inp)

cells_to_yaml

cells_to_yaml(
    output: PathLike,
    cells: Sequence[ProtoTKCell[Any]]
    | ProtoTKCell[Any]
    | Sequence[TKCell]
    | TKCell,
) -> None

Convert cell(s) to a yaml representations.

Parameters:

Name Type Description Default
output PathLike

A stream or string of a path where to dump the yaml. Can also be set to sys.stdout

required
cells Sequence[ProtoTKCell[Any]] | ProtoTKCell[Any] | Sequence[TKCell] | TKCell

A single KCell or a list of them.

required

Returns:

Type Description
None

yaml dump

Source code in kfactory/placer.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def cells_to_yaml(
    output: PathLike,
    cells: Sequence[ProtoTKCell[Any]] | ProtoTKCell[Any] | Sequence[TKCell] | TKCell,
) -> None:
    """Convert cell(s) to a yaml representations.

    Args:
        output: A stream or string of a path where to dump the yaml. Can also be
            set to sys.stdout
        cells: A single [KCell][kfactory.kcell.KCell] or a list of them.


    Returns:
        yaml dump
    """
    _cells = [cells] if isinstance(cells, ProtoTKCell | TKCell) else list(cells)
    _cells.sort(key=lambda c: c.hierarchy_levels())
    yaml = YAML()
    yaml.register_class(KCell)
    yaml.register_class(Port)
    yaml.register_class(Ports)
    yaml.indent(sequence=4, offset=2)
    yaml.dump(_cells, output)

exploded_yaml

exploded_yaml(
    inp: PathLike[Any],
    library: KCLayout = stdkcl,
    additional_classes: list[object] | None = None,
    verbose: bool = False,
) -> Any

Expanded yaml.

Expand cross-references. Same syntax as 🇵🇾func:~cells_from_yaml

Source code in kfactory/placer.py
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
def exploded_yaml(
    inp: os.PathLike[Any],
    library: KCLayout = stdkcl,
    additional_classes: list[object] | None = None,
    verbose: bool = False,
) -> Any:
    """Expanded yaml.

    Expand cross-references. Same syntax as :py:func:~`cells_from_yaml`
    """
    yaml = YAML(pure=True)

    class ModKCell(KCell):
        def __init__(
            self, name: str | None = None, library: KCLayout = library
        ) -> None:
            KCell.__init__(self, name=name, kcl=library)

        @classmethod
        def from_yaml(
            cls, constructor: SafeConstructor, node: Any, verbose: bool = False
        ) -> Self:
            return super().from_yaml(constructor, node, verbose=verbose)

    return yaml.dump(yaml.load(inp), sys.stdout)

get_yaml_obj

get_yaml_obj() -> YAML

New global yaml object.

Source code in kfactory/placer.py
51
52
53
def get_yaml_obj() -> YAML:
    """New global yaml object."""
    return YAML()

include_from_loader

include_from_loader(
    folder: Path,
    library: KCLayout,
    additional_classes: list[object] | None,
    verbose: bool,
) -> Any

Expand ruamel to support the !include keyword.

Source code in kfactory/placer.py
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
def include_from_loader(
    folder: Path,
    library: KCLayout,
    additional_classes: list[object] | None,
    verbose: bool,
) -> Any:
    """Expand ruamel to support the `!include` keyword."""

    @dataclass
    class Include:
        filename: str
        yaml_tag: str = "!include"

        @classmethod
        def from_yaml(cls, constructor: SafeConstructor, node: Any) -> None:
            d = SafeConstructor.construct_mapping(constructor, node)

            f = Path(d["filename"])
            if f.is_absolute():
                cells_from_yaml(f, library, additional_classes, verbose)
            else:
                cells_from_yaml(
                    (folder / f).resolve(), library, additional_classes, verbose
                )

    return Include

register_classes

register_classes(
    yaml: YAML,
    kcl: KCLayout = stdkcl,
    additional_classes: list[object] | None = None,
    verbose: bool = False,
) -> None

Register a new KCell class compatible with ruamel yaml.

Source code in kfactory/placer.py
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
def register_classes(
    yaml: YAML,
    kcl: KCLayout = stdkcl,
    additional_classes: list[object] | None = None,
    verbose: bool = False,
) -> None:
    """Register a new KCell class compatible with ruamel yaml."""

    class ModKCell(KCell):
        def __init__(self, name: str | None = None, library: KCLayout = kcl) -> None:
            KCell.__init__(self, name=name, kcl=library)

        @classmethod
        def from_yaml(
            cls,
            constructor: SafeConstructor,
            node: Any,
            verbose: bool = False,
        ) -> Self:
            return super().from_yaml(constructor, node, verbose=verbose)

    yaml.register_class(ModKCell)
    yaml.register_class(Port)
    yaml.register_class(Ports)
    yaml.register_class(LayerEnclosure)

    if additional_classes is not None:
        for c in additional_classes:
            yaml.register_class(c)