gdsfactory / kfactory

gdsfactory with a klayout backend
https://gdsfactory.github.io/kfactory/
MIT License
31 stars 11 forks source link

save name of python function in KCell / metadata #180

Closed gdspaul closed 12 months ago

gdspaul commented 12 months ago

Can we get the name of the python function that created a cell stored in the KCell metadata?

Example:

@kf.cell
def my_cell(length, width):
  ...

c = my_cell(10000, 1000)
print(c.funcname)

Rationale:

Frequently in extracting information from a layout (whether pulling it from a GDS file or from a saved netlist), it's helpful to be able to find all cells generated from the same original python function and handle them together in some way. For instance,

@kf.cell
def my_cell(length, width):
  ...

defines a pcell, and I'd like to open the resulting GDS file somewhere and identify each cell with its originating python function. So, for instance, a cell named my_cell_L10000_W1000 could be associated with the my_cell function.

It is not always possible to split a cell name on underscores and determine the original function name. For instance, assuming I'm remembering the cell naming scheme correctly, if some developer upstream of me wrote a KCell

@kf.cell
def my_cell_L1(width):
  ...

and I was analyzing the GDS file later on, then I wouldn't necessarily know which python function gave rise to the cell named my_cell_L10000_W1000 just by splitting the name on underscores. Function name suffixes with a single capital letter and a couple digits occur from time to time, and especially if a GDS file gets separated from its original Python repository, it would be lovely to be able to get function names out without a headache.

Example use case:

Traverse the hierarchical graph of a layout and collect the names of all the Python functions which created leaf nodes.