Skip to content

Commit

Permalink
Merge pull request #2487 from gdsfactory/metadata-json
Browse files Browse the repository at this point in the history
export metadata as json
  • Loading branch information
joamatab committed Jan 27, 2024
2 parents faf6f4e + 134384a commit 2f2a229
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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 @@ def _write_library(
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 @@ def _write_library(
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 @@ def _write_library(
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(
orjson.dumps(self.to_dict(with_cells=True, with_ports=True))
)
logger.info(f"Write JSON metadata to {str(metadata)!r}")

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 @@ def import_gds(
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 @@ def import_gds(
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 @@ def import_gds(
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 @@ def import_gds(
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())

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

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

if "ports" in metadata:
for port_name, port in metadata["ports"].items():
if port_name not in component.ports:
component.add_port(
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

0 comments on commit 2f2a229

Please sign in to comment.