Importing other Python Modules in KLayout Python IDE

Hi, I am currently writing a script that can extract a set of row,col coordinates from a given excel file and I want to be able to convert those coordinates into corresponding points on a gds file. In order to extract information from an excel file, I require the use of the Pandas module. However when I try running the script in the KLayout Python IDE, I get an error saying that there is no module by that name (even though I have installed it!). I've attached an image of my error message and I'll include my code as well. **it should be noted that I had written this code in PyCharm and then tried to run it in KLayout's IDE.

import pandas as pd
import pya
from typing import List, Any, Tuple

df = pd.read_csv("C:\Python Projects\Test_2.csv") # directory of a sample csv file I created

def getIndex(dataframe, value):
# Get index positions of value in dataframe
listOfPositions = list()
# value = 1 # csv file made up of 0s and 1s, we only want points where the 1 value is present
# dfObj = df
# accepting a specific value and returns a bool data frame
result = dataframe.isin([value])

# getting names/index of columns in bool data frame that contain true values
seriesObj = result.any()
columnInd = list(seriesObj[seriesObj == True].index)

# Iterate over list of columns and get the corresponding rows indexes where the value exists
for col in columnInd:
    rows = list(result[col][result[col] == True].index)
    for row in rows:
        listOfPositions.append((row, col))
# Return a list of tuples indicating the positions of value in the dataframe
return listOfPositions

Get list of index positions i.e. row & column of all occurrences of 1 in the dataframe

listOfPositions = getIndex(df, 1) # type: List[Tuple[Any, Any]]
print('Index positions of 1 in current Dataframe : ')
for i in range(len(listOfPositions)):
print('Position ', i, ' (Row index , Column Name) : ', listOfPositions[i])

lay = pya.Layout() # type: object # calling "Layout" class
lay.dbu = 0.001 # setting up the database units to micrometers

create the pcell

TopCell = lay.create_cell("TOP") # creating primary PCell that will contain all other cells + objects

PCell_ID = lay.pcell_id("TOP")

output = "csv_to_gdsApr8.gds"

creating the first layer to put our points on

layer1 = lay.layer(1, 0)

concern: column index appears as: 0.1, 0.2, 0.3 etc

for klayout point make sure to multiply colInd by 10 before assigning it pos.point

list[][] == w --> first bracket is the position of the tuple in the list, second bracket is the pos of element in

tuple

for i in range(len(listOfPositions)):
x = listOfPositions[i][0] # assigning value of x coord
y = 10 * listOfPositions[i][1] # assigning value of y coord
new_point = pya.DPoint(x, y) # creating a new point with the specific x, y coords
TopCell.shapes(layer1).insert(new_point)

lay.write(output)

Comments

  • You could use -jcommand line option to point KLayout to proper Python library.

  • is there any way to write something into the direct python script? I'm try to create this macro for anyone to use, even if they don't have experience with python. I just want them to be able to run the script directly without having to interject anything themselves.

  • edited April 2020

    Hi,

    Please don't paste code into the forum. It's unreadable. Use Markdown: a single line with triple backticks before and after your code to make the forum display it properly.

    What do you mean by "is there any way to write something into the direct python script"?

    Regarding additional Python modules: if you're on Linux, just install the new module with pip. On Windows there is no package manager by default. Even if you have installed a package for one Python interpreter, it's not available to others. Every Python instance on your system needs their own packages. So does KLayout which essentially is a Python interpreter. Some packages are already included in KLayout, but many others are not. As KLayout is built upon the MSYS2 distribution, the available modules are limited.

    So there is no general way by which other users could make use for your script if it depends on other packages not packaged with KLayout on Windows. I don't want to maintain a whole Python ecosystem just for this. KLayout can be built against Anaconda, but I will not ship a binary which speaks Python only if you have Anaconda installed.

    Regards,

    Matthias

  • Are there any readily available modules on KLayout that could help me extract data from an excel file?

Sign In or Register to comment.