# How to get the times cell shows in the database?

edited July 2012 in General
Hi, Matthias,

Thank you for this friendly software.
I have a question, I want to do the simple statistic with the layout.
There are many cells in the layout and I want to know every cell appears in the layout how many times?
Is there any command can help this calculation?

Regards,
Canny

• edited November -1

Hi Canny.

What precisely do you mean by "how many times"? Do you mean the flat count, i.e. the number of times a cell appears in a given other cell when drawing that cell? This count is not the same than the number of instances, because a cell might be instantiated a number of times in another cell which itself is instantiated multiple times. Hence in that case the count multiplies.

Best regards,

Matthias

• edited November -1
Hi, Matthias,

I'm sorry for in-precisely question.
I want to know under the some specific cell(ex, top-cell),
how many times the cell IDs were used (or instantiated)?
Ex, there are 16 instances under the top_cell, 4 instances are referenced by cell ID "A", 2 referenced by cell ID "B", 8 referenced by cell ID "C" and 2 referenced by cell ID "D". Is there any way to get the result list like,
Top_cell/Cell_ID/Count
top_cell/A/4
top_cell/B/2
top_cell/C/8
top_cell/D/2
?

Regards,
Canny
• edited July 2012
Hi, Matthias,

I'm also curious about the "flat" of brower instance in Klayout.
Why is the number of "Flat" always twice of the "Count"?

Regards,
Hsin Yi
• edited August 2012

Hallo,

Regarding the second question, the flat count is not necessarily two times the count. Consider the following hierarchy, where B is instantiated in A and A itself in TOP. Assume there a three instances of B in A (the instance count is 3). But expanding the two A instances in TOP effectively renders 6 instances of B in TOP (2 times 3). The latter is the "flat" count. In that example the flat count is twice the instance count:

``````TOP
A (2 times)
B (3 times)
``````

The arithmetics gets more complicated if you have multiple instantiation paths of a cell, but in every case the "flat" count is the number of expanded instances of a cell in the current scope. The instance browser will report a total of flat instances and a flat count along each instantiation path.

For the first question: the instance browser does not exactly give you that information but just for one cell. It is possible to create a script that gives you that information.

You'll find a sample script below. The script shows the results in a message box which may not make sense if the list gets very long.

Best regards,

Matthias

``````#
# This program is free software; you can redistribute it and/or modify
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# DESCRIPTION: Dump the child cells of the current cell with their instance count.
# The script installs a corresponding entry in the "Tools" menu, called
# "Count child cells".
#
# Run the script with
#   klayout -rm count_insts.rbm ...
# or put the script as "count_insts.rbm" into the installation path (on Unix for version <=0.21:
# set \$KLAYOUTPATH to the installation folder).
#

def initialize( title, shortcut, &action )
self.title = title
self.shortcut = shortcut
@action = action
end
def triggered
@action.call( self )
end
private
@action
end

app = RBA::Application.instance

mw = app.main_window
view = mw.current_view
cv = view.cellview(view.active_cellview_index)

layout = cv.layout

insts = {}

cv.cell.each_inst do |inst|
cn = layout.cell_name(inst.cell_index)
insts[cn] ||= 0
insts[cn] += 1
end

text = "In cell " + layout.cell_name(cv.cell_index) + ":\n\n"
insts.keys.sort.each do |cn|
text += cn + ":" + insts[cn].to_s + "\n"
end

RBA::MessageBox::info("Instance count" , text, RBA::MessageBox::b_ok)

}

app = RBA::Application.instance
mw = app.main_window

``````
• edited August 2012
Hi, Matthias,

Thank you for your kindly feedback, I totally understand my second question.
I'm sorry for my pool English to make the mistake of my 1st question.
The information I want to get is like the left window of Klayout Browse instance shows.
To take the example you took, the information would be,
B shows 2 times under parent A
B shows 6 times under parent TOP

But the function Browse instance only show information of 1 cell per time.
I want to know this simple statistic information of all cells in the layout.
If there is any command to do it and export the result with the file?

Regards,
Canny
• edited November -1

Hi Canny,

Ok, given that requirement the script looks somewhat different

``````#
# This program is free software; you can redistribute it and/or modify
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# DESCRIPTION: Dump the child cells of the current cell with their instance count.
# The script installs a corresponding entry in the "Tools" menu, called
# "Count child cells".
#
# Run the script with
#   klayout -rm count_insts.rbm ...
# or put the script as "count_insts.rbm" into the installation path (on Unix for version <=0.21:
# set \$KLAYOUTPATH to the installation folder).
#

def initialize( title, shortcut, &action )
self.title = title
self.shortcut = shortcut
@action = action
end
def triggered
@action.call( self )
end
private
@action
end

\$produce_cell_stat_handler = MenuAction.new( "Produce cell statistics", "" ) do

app = RBA::Application.instance

mw = app.main_window
view = mw.current_view
cv = view.cellview(view.active_cellview_index)

layout = cv.layout

cells = {}

layout.each_cell do |cell|
c = layout.cell_name(cell.cell_index)
cells[c] ||= {}
cell.each_parent_inst do |pi|
pc = layout.cell_name(pi.parent_cell_index)
cells[c][pc] ||= 0
cells[c][pc] += 1
end
end

File.open("stat.txt", "w") do |file|
cells.each do |c,pc_stat|
pc_stat.each do |pc,count|
file.puts("#{c}: #{count} instances in #{pc}")
end
end
end

end

app = RBA::Application.instance
mw = app.main_window

``````

The script will write a file called "stat.txt" which contains the information about the number of instances of all cells in other cells. Again it's just a template, you may modify of course.

Best regards,

Matthias

• edited November -1
Hi, Matthias,

Thank you for your kindly help.
It is great and useful for me.

Regards,
Canny
• edited June 2016

Hi, Matthias,

I'm confused with this section and don't know how to modify it

`````` cells.each do |c,pc_stat|
pc_stat.each do |pc,count|
file.puts("#{c}: #{count} instances in #{pc}")
end
end
``````

If I have 2 instances under the layout which top cell is A
one is 1*5 B cell array
the other is a single B cell

I use the above script, and the result would show

``````    B:2 instances in A
``````

B cell shows 6 time under its parents cell A, not 2.

Could you tell me how to modify the script to record the number of times of cell referenced?

Regards,
Canny

• edited November -1

Hi Canny,

that's simple: replace

``````cells[c][pc] += 1
``````

by

``````cells[c][pc] += pi.inst.size
``````

Matthias