ran out of memory...

ingo ingoogni at home.nl
Wed Dec 27 11:55:08 EST 2000


In an attempt to create a 3d dataset, with Python, that can be renderd in 
povray (or, when using a different header, with vtk) I came up with the 
code below.
The initial idea was to use Numeric.fromfunction(), but that didn't work 
out because of the box size and resolution. Or is it possible and I just 
missed it?
In the current approach there is, besides the speed, one problem. When 
making big datasets (150x150x150) the machine runs out of memory while 
working on Dens=map(fnDensity, x, y, z). How can the memory use be 
reduced? I thought about putting the result of fnDensity in a cStringIO 
object. That would probably mean looping through x,y,z 'mannualy', wich 
seems to be no fun either.

Since I'm still very much a programming newbie, any comment on the code is 
appreciated.

---%<------------%<------------%<------------%<---

import math, array, Numeric
def fnx(ix): return (xMin+xLen*(ix/xRes))
def fny(iy): return (yMin+yLen*(iy/yRes))
def fnz(iz): return (zMin+zLen*(iz/zRes))
def fnDensity(x, y, z):
    return (math.cos(Api*z)*math.cos(Api*y) +
        math.cos(Api*x)*math.cos(Api*z) +
        math.cos(Api*x)*math.cos(Api*y))
def fnLimit(Density):
    return int(255*(1/(MMD)*(Density-MinD)))

#size of the box
xMin, yMin, zMin=-math.pi,-math.pi,-math.pi
xMax, yMax, zMax= math.pi, math.pi, math.pi
xLen=xMax-xMin; yLen=yMax-yMin; zLen=zMax-zMin

#x,y,z resolution of the box.
xRes, yRes, zRes = 10.0, 10.0, 10.0
xyRes=xRes*yRes; yzRes=yRes*zRes; xyzRes=xRes*yRes*zRes

'''
x=[];y=[];z=[]
for ix in range(1, xRes+1):
    for iy in range(1, yRes+1):
        for iz in range(1, zRes+1):
            x.append(xMin+xLen*(ix/xRes))
            y.append(yMin+yLen*(iy/yRes))
            z.append(zMin+zLen*(iz/zRes))
'''

#get 'x-axis points'.
Repx=Numeric.ones(int(xRes))*int(yzRes)
x=Numeric.array(map(fnx,range(1, xRes+1)))
x=Numeric.repeat(x,Repx)

#get 'y-axis points'.
Repy=Numeric.ones(int(yRes))*int(zRes)
y=Numeric.array(map(fny,range(1, yRes+1)))
y=Numeric.repeat(y,Repy).tolist()
y=y*int(zRes)

#get 'z-axis points'.
z=map(fnz,range(1, zRes+1))*int(xyRes)

#calculate densities for voxel xyz.
A=0.5; Api=A*math.pi
Dens=map(fnDensity, x, y, z)

#find minimum and maximum densities,
#scale them to fit between 0 - 255.
MaxD=max(Dens); MinD=min(Dens); MMD=MaxD-MinD
Dens=map(fnLimit,Dens)

#write to file.
FileName= "df3Test.df3"
f=open(FileName, 'wb')
f.write(chr(int(xRes)/256)); f.write(chr(int(xRes)%256))
f.write(chr(int(yRes)/256)); f.write(chr(int(yRes)%256))
f.write(chr(int(zRes)/256)); f.write(chr(int(zRes)%256))
array.array('B', Dens).tofile(f)
f.close()

---%<------------%<------------%<------------%<---

groeten,
Ingo

-- 
Photography: http://members.home.nl/ingoogni/
Pov-Ray    : http://members.home.nl/seed7/



More information about the Python-list mailing list