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

export metadata as json #2487

Merged
merged 1 commit into from Jan 27, 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
9 changes: 9 additions & 0 deletions gdsfactory/component.py
Expand Up @@ -20,6 +20,7 @@

import gdstk
import numpy as np
import orjson
import yaml
from omegaconf import DictConfig

Expand Down Expand Up @@ -1811,6 +1812,7 @@
logging: bool = True,
with_oasis: bool = False,
with_metadata: bool = False,
with_metadata_json: bool = False,
with_netlist: bool = False,
netlist_function: Callable | None = None,
**kwargs,
Expand All @@ -1825,6 +1827,7 @@
logging: disable GDS path logging, for example for showing it in KLayout.
with_oasis: If True, file will be written to OASIS. Otherwise, file will be written to GDS.
with_metadata: writes metadata in YAML format.
with_metadata_json: writes metadata in JSON format.
with_netlist: writes netlist in JSON format.
netlist_function: The netlist function to use. You can compose a partial function with the `get_netlist` function for example with your parameters.

Expand Down Expand Up @@ -1962,6 +1965,12 @@
metadata = gdspath.with_suffix(".yml")
metadata.write_text(self.to_dict_yaml(with_cells=True, with_ports=True))
logger.info(f"Write YAML metadata to {str(metadata)!r}")
if with_metadata_json:
metadata = gdspath.with_suffix(".json")
metadata.write_bytes(

Check warning on line 1970 in gdsfactory/component.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/component.py#L1969-L1970

Added lines #L1969 - L1970 were not covered by tests
orjson.dumps(self.to_dict(with_cells=True, with_ports=True))
)
logger.info(f"Write JSON metadata to {str(metadata)!r}")

Check warning on line 1973 in gdsfactory/component.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/component.py#L1973

Added line #L1973 was not covered by tests

if with_netlist:
"""
Expand Down
32 changes: 32 additions & 0 deletions gdsfactory/read/import_gds.py
Expand Up @@ -4,6 +4,7 @@

import gdstk
import numpy as np
import orjson
from omegaconf import OmegaConf

from gdsfactory.cell import cell_import_gds
Expand All @@ -18,6 +19,7 @@
cellname: str | None = None,
gdsdir: str | Path | None = None,
read_metadata: bool = False,
read_metadata_json: bool = False,
keep_name_short: bool = False,
unique_names: bool = True,
max_name_length: int = 250,
Expand All @@ -32,6 +34,7 @@
cellname: cell of the name to import. None imports top cell.
gdsdir: optional GDS directory.
read_metadata: loads metadata (ports, settings) if it exists in YAML format.
read_metadata_json: loads metadata (ports, settings) if it exists in JSON format.
keep_name_short: appends a hash to a shortened component name.
unique_names: appends $ with a number to the name if the cell name is on CACHE. \
This avoids name collisions when importing multiple times the same cell name.
Expand All @@ -43,6 +46,7 @@
raise FileNotFoundError(f"No file {str(gdspath)!r} found")

metadata_filepath = gdspath.with_suffix(".yml")
metadata_json_filepath = gdspath.with_suffix(".json")

if gdspath.suffix.lower() == ".gds":
gdsii_lib = gdstk.read_gds(str(gdspath))
Expand Down Expand Up @@ -138,6 +142,34 @@
port_type=port.port_type,
)

if read_metadata_json and metadata_json_filepath.exists():
logger.info(f"Read JSON metadata from {metadata_json_filepath}")
metadata = orjson.loads(open(metadata_json_filepath, "rb").read())

Check warning on line 147 in gdsfactory/read/import_gds.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/read/import_gds.py#L146-L147

Added lines #L146 - L147 were not covered by tests

if "settings" in metadata:
if metadata.get("settings", {}):
component.settings = CellSettings(**metadata["settings"])

Check warning on line 151 in gdsfactory/read/import_gds.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/read/import_gds.py#L151

Added line #L151 was not covered by tests

if "info" in metadata:
if metadata["info"]:
component.info = Info(**metadata["info"])

Check warning on line 155 in gdsfactory/read/import_gds.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/read/import_gds.py#L155

Added line #L155 was not covered by tests
if "function" in metadata:
component.function_name = metadata["function"]

Check warning on line 157 in gdsfactory/read/import_gds.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/read/import_gds.py#L157

Added line #L157 was not covered by tests
if "module" in metadata:
component.module = metadata["module"]

Check warning on line 159 in gdsfactory/read/import_gds.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/read/import_gds.py#L159

Added line #L159 was not covered by tests

if "ports" in metadata:
for port_name, port in metadata["ports"].items():
if port_name not in component.ports:
component.add_port(

Check warning on line 164 in gdsfactory/read/import_gds.py

View check run for this annotation

Codecov / codecov/patch

gdsfactory/read/import_gds.py#L164

Added line #L164 was not covered by tests
name=port_name,
center=np.array(port["center"], dtype="float64"),
width=port["width"],
orientation=port["orientation"],
layer=tuple(port["layer"]),
port_type=port["port_type"],
)

for k, v in kwargs.items():
component.info[k] = v
component.imported_gds = True
Expand Down