Error in reading the input GDS to extract the layers used

edited November 21 in KLayout Support

Dear All,
I find difficulty in reading the GDS through the command line to extract the used GDS layer numbers, may I have your support pls, Kindly let m eknow
Command
klayout -b -r code1bk.py -rd input=/home/vlsicad3/CAD_VERF/DRYRUN_15Nov/TA.gds

Error: No GDS file specified. Run with '-rd input=/home/vlsicad3/CAD_VERF/DRYRUN_15Nov/TA.gds'.

CODE:

# Import pya (KLayout Python API)
import pya
import sys

# Print sys.argv for debugging
print("Command-line arguments received:", sys.argv)

# Check command-line arguments directly for the GDS file path
input_gds = None
for arg in sys.argv:
    if arg.startswith("input="):
        input_gds = arg.split("=")[1]

# Debugging message to verify input_gds
if not input_gds:
    print("Error: No GDS file specified. Run with '-rd input=/home/vlsicad3/CAD_VERF/DRYRUN_15Nov/TA.gds'.")
    sys.exit(1)
else:
    print(f"Using GDS file path: {input_gds}")

# Create a new layout and load the specified GDS file
ly = pya.Layout()
ly.read(input_gds)

# Specify the output text file path
output_file = "/home/vlsicad3/CAD_VERF/DRYRUN_15Nov/layer_info.txt"  # Update with your desired path

# Gather, sort, and filter layers
layers = sorted([str(l) for l in ly.layer_infos() if l])

# Write layer information to the file
with open(output_file, "w") as f:
    f.write("Layers in Layout:\n")
    for layer in layers:
        f.write(layer + "\n")

print(f"Layer information has been written to {output_file}")
print(f"Processed GDS file path: {input_gds}")

Comments

  • Attached the code FYR

  • Hi Kumaran,

    First, please

      make sure to surround your code block with triple-back quotes (```) like this for readability.
    

    I modified your original code in two ways:

    Both generated the identical layer information.:

    Layers in Layout:
    1/0
    10/0
    1000/0
    1001/0
    11/0
    13/0
    2/0
    3/0
    4/0
    5/0
    8/0
    9/0
    

    However, I prefer to code2.py.

  • edited November 21

    Dear sekigawa,
    Thanks for the prompt response, i did a few enhancements to setenv for the GDS and I run the code,
    But my concern,
    Say,

    INPUT_GDS1 = 1.gds
    INPUT_GDS2 =  TOP.gds(which is the integration of few chip(1.gd,2.gds,3.gds)
    

    how can I extract the used layer file of 1.gds from TOP.GDSfrom INPUT_GDS2 , and compare the output file of 1.gds and the output file of TOP.gds(which should extract the used layer only of the 1.gds)

    May I have your guidance pls

    import pya
    import os
    import sys
    import csv
    
    # Get the GDS file paths from environment variables
    input_gds1 = os.getenv("INPUT_GDS1")
    input_gds2 = os.getenv("INPUT_GDS2")
    csv_file = os.getenv("ET_CSV")
    
    if not input_gds1 or not input_gds2 or not csv_file:
        print("Error: One or more GDS files or the CSV file are not specified. Set INPUT_GDS1, INPUT_GDS2, and ET_CSV environment variables.")
        sys.exit(1)
    
    # Function to process GDS and output layer information
    def process_gds(input_gds, output_file):
        try:
            # Create a new layout and load the specified GDS file
            ly = pya.Layout()
            ly.read(input_gds)
    
            # Gather and sort layers, then return the list of layers
            layers = sorted([str(l).replace(";", "/") for l in ly.layer_infos() if l])
    
            # Write the layer information to the output file
            with open(output_file, "w") as f:
                f.write("Layers in Layout:\n")
                for layer in layers:
                    f.write(layer + "\n")
    
            print(f"Layer information has been written to {output_file}")
            return set(layers)  # Return the layers as a set for comparison
    
        except Exception as e:
            print(f"Error processing {input_gds}: {e}")
            sys.exit(1)
    
    # Function to read the CSV and return a set of layer values in CSV format (e.g., "76;0")
    def read_csv(csv_file):
        layers_set = set()
        try:
            with open(csv_file, newline='') as csvfile:
                reader = csv.reader(csvfile, delimiter=';')
                for row in reader:
                    if len(row) == 2:
                        layer_number = row[0].strip()  # Example: '76'
                        value = row[1].strip()  # Example: '0'
                        layers_set.add(f"{layer_number};{value}")
            print(f"CSV data has been read from {csv_file}")
            return layers_set
        except Exception as e:
            print(f"Error reading CSV file {csv_file}: {e}")
            sys.exit(1)
    
    # Function to compare the layers in the CSV file to those in the output GDS layer file
    def compare_csv_to_gds(csv_layers, gds_layers, output_file):
        comparison_result = []
        for layer in csv_layers:
            if layer.replace(";", "/") not in gds_layers:
                comparison_result.append(f"{layer} Missing in layer_info_TO.txt")
        for layer in gds_layers:
            if layer.replace("/", ";") not in csv_layers:
                comparison_result.append(f"{layer.replace('/', ';')} Missing in et.csv")
    
        with open(output_file, "w") as f:
            for line in comparison_result:
                f.write(line + "\n")
    
        print(f"CSV comparison has been written to {output_file}")
    # Function to compare text files for missing layers
    def compare_txt_files(file1, file2, output_file):
        try:
            with open(file1, "r") as f1, open(file2, "r") as f2:
                layers1 = set(line.strip() for line in f1 if "/" in line)
                layers2 = set(line.strip() for line in f2 if "/" in line)
    
            # Find differences
            missing_in_file2 = layers1 - layers2
            missing_in_file1 = layers2 - layers1
    
            # Write differences to the output file
            with open(output_file, "w") as f:
                f.write("Layer differences between the files:\n\n")
                f.write(f"Layers in {file1} but not in {file2}:\n")
                for layer in sorted(missing_in_file2):
                    f.write(layer + "\n")
                f.write(f"\nLayers in {file2} but not in {file1}:\n")
                for layer in sorted(missing_in_file1):
                    f.write(layer + "\n")
    
            print(f"Text comparison has been written to {output_file}")
    
        except Exception as e:
            print(f"Error comparing text files: {e}")
            sys.exit(1)
    
    # Specify the output text file paths for layer maps
    output_file1 = "/home/vlsicad3/CAD_VERF/lay_out/layer_info_sub.txt"
    output_file2 = "/home/vlsicad3/CAD_VERF/lay_out/layer_info_TO.txt"
    
    # Process the first GDS file and write its layer map
    print(f"Processing GDS file 1: {input_gds1}")
    layers1 = process_gds(input_gds1, output_file1)
    
    # Process the second GDS file and write its layer map
    print(f"Processing GDS file 2: {input_gds2}")
    layers2 = process_gds(input_gds2, output_file2)
    
    # Read the CSV data (et.csv)
    csv_data = read_csv(csv_file)
    
    # Compare CSV data to the layers from layer_info_TO.txt
    csv_to_gds_comparison = compare_csv_to_gds(csv_data, layers2, "/home/vlsicad3/CAD_VERF/lay_out/csv_to.csv")
    
    print("Comparison complete.")
    # Compare layer_info_sub.txt and layer_info_TO.txt for missing layers
    layer_diff_output_file = "/home/vlsicad3/CAD_VERF/lay_out/layer_diff_output.txt"
    compare_txt_files(output_file1, output_file2, layer_diff_output_file)
    
    print("Text file comparison complete.")
    
  • edited November 18


    A code block should have been looked like this.

    Please attach the tidy code.
    BTW, can you provide a dummy data set for "INPUT_GDS1", "INPUT_GDS2", and "ET_CSV"?


    If my understanding is correct, you

    1. have two GDS files (gds1, gds2) and one CSV file containing Layer and Datatype separated with ';'
    2. want to perform the three-way comparison (exclusive-OR) regarding the (L, D) information.
      • more precisely (1) gds1-vs-CSV, (2) gds2-vs-CSV, and (3) gds1-vs-gds2

    If the above is OK, I can prepare a dummy data set.
    Please confirm.

    Again, please fix the code block in the post above so other visitors can read it quickly and correctly.

  • Cont.

    Assuming that my above understanding is OK, I wrote code3.py for this problem.

    Some changes from your code include:

    1. I use the pandas module instead of csv.
    2. I take the three file names from the command line; not from environment variables.
    Mint203 {sekigawa}(1)$ ./code3.py
    ----------------------------------------------------------------
    Usage:
      $ [python3] ./code3.py <INPUT_GDS1> <INPUT_GDS2> <ET_CSV>
    example:
      $ ./code3.py  ringo.gds  nand-DMOS4.gds  et_csv.csv
    ----------------------------------------------------------------
    
    Mint203 {sekigawa}(2)$ ./code3.py  ringo.gds  nand-DMOS4.gds  et_csv.csv
    ### Processing GDS file 1: <ringo.gds>
        Layer information has been written to <layer_info_gds1.txt>
    
    ### Processing GDS file 2: <nand-DMOS4.gds>
        Layer information has been written to <layer_info_gds2.txt>
    
        CSV data has been read from <et_csv.csv>
        CSV-vs-GDS comparison has been written to <csv_to_gds1.txt>
        CSV-vs-GDS comparison has been written to <csv_to_gds2.txt>
    ### Comparison CSV-vs-GDS complete.
    
        GDS-vs-GDS comparison has been written to <gds1_to_gds2.txt>
    ### Comparison GDS-vs-GDS complete.
    

  • @sekigawa Thanks for your competent support!

    I took the liberty and my admin power to reformat the code of @Kumaran above for readability. @Kumaran: put a single line with three backticks before and after the code to make it formatted as code.

    Matthias

Sign In or Register to comment.