RAM Memory filled up with SimplePolygon(s)

edited January 2016 in General
Dear all,

I have a problem running a python script which reads a txt file containing coordinates of polygons vertices and generating polygons. The text file is 30 Mo, there are around 200 polygons containing a few thousands points each. When I run the script the RAM memory fills up very fast. Also, if I put much less points, so that the memory is still OK, I figure out that the .gds file which is generated has a very large size (more than twenty times the size of the initial .txt file). Is there some kind of bug there, or is it the way I script which is not proper ?

Here is the script, I thank you a lot in advance for your advice.


import pya

# Create Layout
layout = pya.Layout()

# Set layout unit (in micrometer)
layout.dbu = 1

# Create cell
top = layout.create_cell("TOP")

# Create layer
l1 = layout.layer(1, 0)

poly = pya.SimplePolygon.new()
pt = []

f = open('Mask_SiN_T1.dat', 'r')
lines = f.readlines()

j = 0

for line in lines:

if line == ' \n':
pt = []
poly = pya.SimplePolygon.new()
offsety = int(j/(7*4))*5000
offsetx = (int(j/4)%7)*5000
j+=1
else:
x,y = line.split()
pt2 = pya.Point();
pt2.x = float(x) + offsetx
pt2.y = float(y) + offsety
pt.append(pt2);
poly.points = pt
top.shapes(l1).insert(poly)

f.close()

layout.write('Mask_SiN_T1.gds')

Comments

  • edited January 2016

    Hi tjacqmin,

    welcome to the forum :-)

    First of all I'd like to mention that the forum system supports Markdown markup. Specifically you can use four blanks at the beginning of the line to make code displayed as pre-formatted text which makes sense for python in particular. Otherwise I don't see the indentation and for Python indents are crucial for the understanding of your code.

    Right now I can just guess. Here is my hypothesis: I think your file consists of lists of vertices, one vertex per line. An empty line starts a new polygon, right?

    Assuming the empty line is following each block, the code could be like:

    • Prepare an array for collecting points (the vertices)
    • You read the file line by line
      • When there is a non-empty line, decode the line and store the vertex into a list. This list will grow by one vertex on each such line.
      • When you encounter an empty line, create a polygon from the vertices collected in the list and put it into the layout.

    So I am expecting a code like this (please note the Markdown usage):

    # Note that this implementation assumes that an empty line *follows* the vertex block!
    
    pt = []
    poly = pya.SimplePolygon.new()
    offsetx = 0
    offsety = 0
    j = 0
    
    for line in lines:
    
      if line == '\n':
    
        # TODO: check whether we really have at least three points to form a valid polygon
    
        # upon a separator line create a polygon from the points collected
        poly = pya.SimplePolygon.new()
        poly.points = pt
        top.shapes(l1).insert(poly)
    
        # prepare for the next block
        pt = []
        j+=1       
        offsety = int(j/(7*4))*5000
        offsetx = (int(j/4)%7)*5000
    
      else:
    
        # upon a vertex line, decode and collect the points
        x,y = line.split()
        pt2 = pya.Point();
        pt2.x = float(x) + offsetx
        pt2.y = float(y) + offsety
        pt.append(pt2)
    

    Doing some wild guessing about the invisible indentation, your code seems to create a polygon on every line with an incremental number of points. So if you have a polygon with 1k points, instead of a single polygon you will create 1k polygons with 1, 2, 3, ... 1000 points each. That's a huge number of polygons and points and explains why your memory fills up.

    Regards,

    Matthias

  • edited November -1
    It works perfectly ! Sorry for indentation problems.
    Thanks a lot.

    Regards,

    Thibaut
Sign In or Register to comment.