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

Remove HSPICE netlist comments for plot_nets #218

Merged
merged 1 commit into from
Nov 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions gplugins/klayout/netlist_spice_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import klayout.db as kdb


class NoCommentReader(kdb.NetlistSpiceReaderDelegate):
"""KLayout Spice reader without comments after $. This allows checking the netlist for HSPICE"""

def parse_element(self, s: str, element: str) -> kdb.ParseElementData:
if "$" in s:
s = s.split("$")[0] # Don't take comments into account
parsed = super().parse_element(s, element)
return parsed
8 changes: 5 additions & 3 deletions gplugins/klayout/plot_nets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import itertools
from logging import warning
from pathlib import Path

import klayout.db as kdb
import matplotlib.pyplot as plt
import networkx as nx
from gdsfactory.config import logger

from gplugins.klayout.netlist_spice_reader import NoCommentReader


def _get_subcircuit_name(subcircuit: kdb.SubCircuit) -> str:
Expand Down Expand Up @@ -81,11 +83,11 @@ def plot_nets(
l2n.read(str(filepath))
netlist = l2n.netlist()
case ".spice":
reader = kdb.NetlistSpiceReader()
reader = kdb.NetlistSpiceReader(NoCommentReader())
netlist = kdb.Netlist()
netlist.read(str(filepath), reader)
case _:
warning("Assuming file is KLayout native LayoutToNetlist file")
logger.warning("Assuming file is KLayout native LayoutToNetlist file")
l2n = kdb.LayoutToNetlist()
l2n.read(str(filepath))
netlist = l2n.netlist()
Expand Down
25 changes: 25 additions & 0 deletions gplugins/klayout/tests/test_netlist_spice_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest

from gplugins.klayout.netlist_spice_reader import NoCommentReader


@pytest.mark.parametrize(
"s,element,expected_name,expected_nets",
[
("1 2 POS", "X", "POS", {"1", "2"}),
("2 3 NEG $ This is a comment", "X", "NEG", {"2", "3"}),
(
"5 4 some_elem some_variable=1 $ This is a comment",
"X",
"some_elem",
{"5", "4"},
),
],
)
def test_NoCommentReader(
s: str, element: str, expected_name: str, expected_nets: set[str]
) -> None:
reader = NoCommentReader()
parsed = reader.parse_element(s, element)
assert set(parsed.net_names) == expected_nets
assert parsed.model_name == expected_name.upper()