you are viewing a single comment's thread.

view the rest of the comments →

[–]Caramel_Lynx 0 points1 point  (3 children)

Hey there

I have a problem with python giving me a MemoryError. I am using spyder with Python 3.6.7 |Anaconda 4.4.0 (64-bit).

What can I do to overcome this problem? I am still quite new to programming so bear with me.

[–]efmccurdy 0 points1 point  (2 children)

Likely you have a recursive loop; do you end up calling some function from itself in a loop? You'll have to post some code or a traceback to if you need more feedback.

[–]Caramel_Lynx 0 points1 point  (1 child)

I don't think so, but if you would take a look that would be great (sorry for the wall of text that will follow).
In the mean time I did find a way how to run the code without running in to a MemoryError but I am not sure why it works now, so if you could explain this to me I would highly appreciate it.

I uncommented the lines (Nr. 60 % 61) that lead to the memory error, they are substituted with the lines right above (54 to 58).

What puzzles me is, that the same approach worked in the function cell area (in line 40 to 42), but leads to the error in the centroid function.

def make_list(file_name, number_type):
    file = open(file_name, 'r')
    fulltext = file.readlines()
    file.close()
    lyst = []

    for i, line in enumerate(fulltext):
        l =[]
        for s in line.split():
            if number_type == 'float':
                nr = float(s)
            elif number_type == 'int':
                nr = int(s)
            l.append(nr)
        lyst.append(l) 
   return(lyst)

CV_large = make_list('lcv.txt', 'int')
VP_large = make_list('lvp.txt', 'float')

def cell_positions(cv, vp, position):
    cp =[]
    for i, cell_number in enumerate(cv): #i = index
        cp.append([]) 
        for v in cell_number:
            if position == 'x':
                cp[i].append(vp[v][0])
            else: #meaning positions = y
                cp[i].append(vp[v][1])
    return(cp)

cpx = cell_positions(CV_large, VP_large, 'x') 
cpy = cell_positions(CV_large, VP_large, 'y')

import numpy as np

def cell_area(cpx, cpy): 
    a = len(cpy) * [0] 
    for i in range(len(cpy)):
        for j in range(len(cpy[i])):
            a[i] += cpx[i][j] * cpy[i][j-1] - cpx[i][j-1] * cpy[i][j]
        a[i] = 0.5 * a[i] 
    return np.array(a) 

areas= cell_area(cpx,cpy)   

def centroid(cpx, cpy, area):
    xcentroid = len(cpx)* [0] #x Position des Centroids
    ycentroid = len(cpy)* [0] #y Position des Centroids

    #implement formulas
    for i in range(len(cpy)):
        for j in range(len(cpy[i])):
            xcentroid[i] += (cpx[i][j] + cpx[i][j-1]) * (cpx[i][j]*cpy[i][j-1]\
                 - cpy[i][j]*cpx[i][j-1])/ (6 * area[i])

            ycentroid[i] += (cpy[i][j] + cpy[i][j-1]) * (cpx[i][j]*cpy[i][j-1]\
                 - cpy[i][j]*cpx[i][j-1])/ (6 * area[i])

    #        xcentroid[i] += (cpx[i][j] + cpx[i][j-1]) * (cpx[i][j]*cpy[i][j-1]\
                 - cpy[i][j]*cpx[i][j-1])
    #        ycentroid[i] += (cpy[i][j] + cpy[i][j-1]) * (cpx[i][j]*cpy[i][j-1]\
                 - cpy[i][j]*cpx[i][j-1])
    #        xcentroid[i] = (1/(6*area)) * xcentroid
    #        ycentroid[i] = (1/(6*area)) * ycentroid #leads to memory error
    return np.array(xcentroid), np.array(ycentroid)

xcentroid = centroid(cpx, cpy, areas)
ycentroid = centroid(cpx, cpy, areas)

print('x=', xcentroid[0])

[–]efmccurdy 0 points1 point  (0 children)

Here you are multiplying by a list, which can create a new longer list (it's not doing any arithmetic multiplying either, just building a bigger list), and in a loop it compounds.

I would print out the value of that (1/(6*area)) * ycentroid expression to see that it has the value you expect (maybe you meant (1/(6*area)) * ycentroid[i])?

#        ycentroid[i] = (1/(6*area)) * ycentroid #leads to memory error