<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>anderswallin.net &#187; python</title>
	<atom:link href="http://www.anderswallin.net/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.anderswallin.net</link>
	<description></description>
	<lastBuildDate>Fri, 30 Jul 2010 09:39:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Octree in python</title>
		<link>http://www.anderswallin.net/2010/03/octree-in-python/</link>
		<comments>http://www.anderswallin.net/2010/03/octree-in-python/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 17:48:03 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[CAM]]></category>
		<category><![CDATA[octree]]></category>
		<category><![CDATA[opencamlib]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[vtk]]></category>

		<guid isPermaLink="false">http://www.anderswallin.net/?p=2890</guid>
		<description><![CDATA[Recursion and &#8216;divide-and-conquer&#8216; must be two of the greatest ideas ever in computer science. Threw together some code in python today for building an octree, given a function isInside() which the tree-builder evaluates to see if a node is inside or outside of the object. Nodes completely outside the interesting volume(here a sphere, for simplicity) [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/38LdfiMTVw0&amp;hl=en_US&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube.com/v/38LdfiMTVw0&amp;hl=en_US&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><a href="http://en.wikipedia.org/wiki/Recursion">Recursion</a> and &#8216;<a href="http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm">divide-and-conquer</a>&#8216; must be two of the greatest ideas ever in computer science.</p>
<p>Threw together some code in python today for building an <a href="http://en.wikipedia.org/wiki/Octree">octree</a>, given a function isInside() which the tree-builder evaluates to see if a node is inside or outside of the object. Nodes completely outside the interesting volume(here a sphere, for simplicity) are rendered grey. Nodes completely inside the object are coloured, green for low tree-depth, and red for high. Where the algorithm can&#8217;t decide if the node is inside or outside we subdivide (until we reach a decision, or the maximum tree depth).</p>
<p>In the end our sphere-approximation looks something like this:</p>
<p><a href="http://www.anderswallin.net/wp-content/uploads/2010/03/octfinal.png"><img class="alignnone size-medium wp-image-2891" title="octfinal" src="http://www.anderswallin.net/wp-content/uploads/2010/03/octfinal-625x303.png" alt="" width="625" height="303" /></a></p>
<p>These tests were done with a maximum tree depth of 6 or 7. If we imagine we want a 100 mm long part represented with 0.01 mm precision we will need about depth 14, or a subdivision into 16384 parts. That&#8217;s impractical to render right now, but seems doable. In terms of memory-use, the octree is much better than a voxel-representation where space is uniformly subdivided into tiny voxels. The graph below shows how the number of nodes grows with tree-depth in the actual octree, versus a voxel-model (i.e. a complete octree). For large tree-depths there are orders-of-magnitude differences, and the octree only uses a number of nodes roughly proportional to the surface area of the part.</p>
<p><a href="http://www.anderswallin.net/wp-content/uploads/2010/03/octree_depth.jpg"><img class="alignnone size-medium wp-image-2892" title="octree_depth" src="http://www.anderswallin.net/wp-content/uploads/2010/03/octree_depth-625x395.jpg" alt="" width="625" height="395" /></a></p>
<p>Now, the next trick is to implement union (addition) and difference (subtraction) operations for octrees. Then you represent your stock material with one octree, and the tool-swept-volume of your machining operation with another octree -&gt; lo and behold we have a cutting-simulator!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anderswallin.net/2010/03/octree-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Offset ellipse, part 2</title>
		<link>http://www.anderswallin.net/2010/03/offset-ellipse-part-2/</link>
		<comments>http://www.anderswallin.net/2010/03/offset-ellipse-part-2/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 10:38:16 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[CAM]]></category>
		<category><![CDATA[drop-cutter]]></category>
		<category><![CDATA[offset-ellipse]]></category>
		<category><![CDATA[opencamlib]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[vtk]]></category>

		<guid isPermaLink="false">http://www.anderswallin.net/?p=2880</guid>
		<description><![CDATA[More on the offset-ellipse calculation, which is related to contacting toroidal cutters against edges(lines). An ellipse aligned with the x- and y-axes, with axes a and b can be given in parametric form as (a*cos(theta) , b*sin(theta) ). The ellipse is shown as the dotted oval, in four different colours. Now the sin() and cos() are [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.anderswallin.net/wp-content/uploads/2010/03/oe_screen.png"><img class="alignnone size-medium wp-image-2879" title="oe_screen" src="http://www.anderswallin.net/wp-content/uploads/2010/03/oe_screen-625x353.png" alt="" width="625" height="353" /></a></p>
<p>More on the offset-ellipse calculation, which is related to contacting toroidal cutters against edges(lines). An ellipse aligned with the x- and y-axes, with axes <strong>a</strong> and <strong>b</strong> can be given in parametric form as (a*cos(theta) , b*sin(theta) ). The ellipse is shown as the dotted oval, in four different colours.</p>
<p>Now the sin() and cos() are a bit expensive the calculate every time you are running this algorithm, so we replace them with parameters (s,t) which are both in [-1,1] and constrain them so s^2 + t^2 = 1, i.e. s = cos(theta) and t=sin(theta). Points on the ellipse are calculated as (a*s, b*t).</p>
<p>Now we need a way of moving around our ellipse to find the one point we are seeking. At point (s,t) on the ellipse, for example the point with the red sphere in the picture, the tangent(shown as a cyan line) to the ellipse will be given by (-a*t, b*s). Instead of worrying about different quadrants in the (s,t) plane, and how the signs of s and t vary, I thought it would be simplest always to take a step in the direction of the tangent. That seems to work quite well, we update s (or t) with a new value according to the tangent, and then t (or s) is calculated from s^2+t^2=1, keeping the sign of t (or s) the same as it was before.</p>
<p>Now for the <a href="http://en.wikipedia.org/wiki/Newton's_method">Newton-Rhapson</a> search we also need a measure of the error, which in this case is the difference in the y-coordinate of the offset-ellipse point (shown as the green small sphere, and obviously calculated as the ellipse-point plus the offset-radius times a normal vector) and where we want that point. Then we just run the algorithm, always stepping either in the positive or negative direction of the tangent along the ellipse, until we reach the required precision (or max number of iterations).</p>
<p>Here&#8217;s an animation which first shows moving around the ellipse, and then at the end a slowed-down Newton-Rhapson search which in reality converges to better than 8 decimal-places in just seven (7) iterations, but as the animation is slowed down it takes about 60-frames in the movie.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/rD1h8S0lG7g&amp;hl=en_US&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube.com/v/rD1h8S0lG7g&amp;hl=en_US&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>I wonder if all this should be done in Python for the general case too, where the axes of the ellipse are not parallel to the x- and y-axes, before embarking on the c++ version?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anderswallin.net/2010/03/offset-ellipse-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VTK test</title>
		<link>http://www.anderswallin.net/2010/01/vtk-test/</link>
		<comments>http://www.anderswallin.net/2010/01/vtk-test/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 18:19:33 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[vtk]]></category>

		<guid isPermaLink="false">http://www.anderswallin.net/?p=2452</guid>
		<description><![CDATA[Tried to make the code from last time a bit clearer by splitting it into two files: vtkscreen.py and test2.py conversion to video again by first converting PNG to JPEG: mogrify -format jpg -quality 97 *.png And then encoding JPEGs into a movie: mencoder mf://*.jpg -mf fps=25:type=jpg -ovc lavc -lavcopts vcodec=mpeg4 -ac copy -o output.avi [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="560" height="340" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/IDxzJzUwJU0&amp;hl=en_US&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="560" height="340" src="http://www.youtube.com/v/IDxzJzUwJU0&amp;hl=en_US&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>Tried to make <a href="http://www.anderswallin.net/2009/11/learning-vtk/">the code from last time</a> a bit clearer by splitting it into two files:</p>
<p><a href="http://www.anderswallin.net/wp-content/uploads/2010/01/vtkscreen.py_.txt">vtkscreen.py</a> and <a href="http://www.anderswallin.net/wp-content/uploads/2010/01/test2.py_.txt">test2.py</a></p>
<p>conversion to video again by first converting PNG to JPEG:</p>
<blockquote><p>mogrify -format jpg -quality 97 *.png</p></blockquote>
<p>And then encoding JPEGs into a movie:</p>
<blockquote><p>mencoder mf://*.jpg -mf fps=25:type=jpg -ovc lavc -lavcopts vcodec=mpeg4 -ac copy -o output.avi -ffourcc DX50</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.anderswallin.net/2010/01/vtk-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning VTK</title>
		<link>http://www.anderswallin.net/2009/11/learning-vtk/</link>
		<comments>http://www.anderswallin.net/2009/11/learning-vtk/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 09:31:28 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[CAM]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[vtk]]></category>

		<guid isPermaLink="false">http://www.anderswallin.net/?p=1930</guid>
		<description><![CDATA[I&#8217;m trying to learn how to render and animate things using VTK. This is the result of a python-script which outputs a series of PNG-frames. These are then converted to jpegs by this command: mogrify -format jpg -quality 97 *.png mogrify -format jpg -quality 97 *.png and converted to a DIVX movie like this: mencoder [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/lcfLVduK-L0&amp;hl=en&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube.com/v/lcfLVduK-L0&amp;hl=en&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>I&#8217;m trying to learn how to render and animate things using <a href="http://www.vtk.org/">VTK</a>. This is the result of a <a href="http://www.anderswallin.net/wp-content/uploads/2009/11/cone.python.txt">python-script</a> which outputs a series of PNG-frames. These are then converted to jpegs by this command:</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">mogrify -format jpg -quality 97 *.png</div>
<blockquote><p><code>mogrify -format jpg -quality 97 *.png</code></p></blockquote>
<div>and converted to a DIVX movie like this:</div>
<div>
<blockquote>
<div><code>mencoder mf://*.jpg -mf fps=25:type=jpg -ovc lavc -lavcopts vcodec=mpeg4 -ac copy -o output.avi -ffourcc DX50</code></div>
</blockquote>
</div>
<p>This should lead to the revival of my old <a href="http://www.anderswallin.net/2007/08/drop-cutter-in-c-v2/">Drop-Cutter code</a> in the near future. This time it&#8217;s going to be in C++, with Python bindings, and hopefully use OpenMP. Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anderswallin.net/2009/11/learning-vtk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More pystones with shedskin</title>
		<link>http://www.anderswallin.net/2008/06/more-pystones-with-shedskin/</link>
		<comments>http://www.anderswallin.net/2008/06/more-pystones-with-shedskin/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 21:02:17 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.anderswallin.net/?p=462</guid>
		<description><![CDATA[As I&#8217;m very much an amateur programmer with not too much time to learn new stuff I&#8217;ve decided my CAM-algorithms are going to be written in Python (don&#8217;t hold your breath, they&#8217;ll be online when they&#8217;ll be online&#8230;). The benefits of rapid development will more than outweigh the performance issues of Python at this stage. [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;m very much an amateur programmer with not too much time to learn new stuff I&#8217;ve decided my CAM-algorithms are going to be written in Python (don&#8217;t hold your breath, they&#8217;ll be online when they&#8217;ll be online&#8230;). The benefits of rapid development will more than outweigh the performance issues of Python at this stage.</p>
<p>But then I found Mark Dufour&#8217;s project <a href="http://code.google.com/p/shedskin/">shedskin</a> (see also <a href="http://shed-skin.blogspot.com/">blog here</a> and <a href="http://mark.dufour.googlepages.com/shedskin.pdf">Mark&#8217;s MSc thesis here</a>), a Python to C++ compiler! Can you have the best of both worlds? Develop and debug your code interactively with Python and then, when you&#8217;re happy with it, translate it automagically over to C++ and have it run as fast as native code?</p>
<p>Well, shedskin doesn&#8217;t support any and all python constructs (yet?), and only a limited number of modules from the standard library are supported. But still I think it&#8217;s a pretty cool tool. For someone who doesn&#8217;t look forward to learning C++ from the ground up typing &#8216;<code>shedskin -e myprog.py</code>&#8216; followed by &#8216;<code>make</code>&#8216; is just a very simple way to create fast python extensions! As a test, I ran shedskin on the pystone benchmark and called both the python and c++ version from my <a href="http://www.anderswallin.net/2008/06/multiprocessing-pystone-benchmark/">multiprocessing test-code</a>:</p>
<p>Python version</p>
<pre>
Processes	Pystones	Wall time	pystones/s	Speedup
1		50000		0.7		76171		1.0X
2		100000		0.7		143808		1.9X
3		150000		0.7		208695		2.7X
4		200000		0.8		264410		3.5X
5		250000		1.0		244635		3.2X
6		300000		1.2		259643		3.4X
</pre>
<p>&#8216;shedskinned&#8217; C++ version</p>
<pre>
Processes	Pystones	Wall time		pystones/s	Speedup
1		5000000			2.9		1696625		1.0X
2		10000000		3.1		3234625		1.9X
3		15000000		3.1		4901829		2.9X
4		20000000		3.4		5968676		3.5X
5		25000000		4.4		5714151		3.4X
6		30000000		5.1		5890737		3.5X
</pre>
<p>A speedup of around 20x.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anderswallin.net/2008/06/more-pystones-with-shedskin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>multiprocessing pystone benchmark</title>
		<link>http://www.anderswallin.net/2008/06/multiprocessing-pystone-benchmark/</link>
		<comments>http://www.anderswallin.net/2008/06/multiprocessing-pystone-benchmark/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 19:40:53 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.anderswallin.net/?p=447</guid>
		<description><![CDATA[A simple pystone benchmark using the python multiprocessing package. Seems to scale quite well &#8211; guess how many cores my machine has! " Simple multiprocessing test.pystones benchmark " " Anders Wallin 2008Jun15 anders.e.e.wallin (at) gmail.com " from test import pystone import processing import time STONES_PER_PROCESS= 10*pystone.LOOPS def f(q): t=pystone.pystones(STONES_PER_PROCESS) q.put(t,block=True) if __name__ == '__main__': print [...]]]></description>
			<content:encoded><![CDATA[<p>A simple pystone benchmark using the python <a href="http://www.python.org/dev/peps/pep-0371/">multiprocessing</a> package. Seems to scale quite well &#8211; guess how many cores my machine has! <img src='http://www.anderswallin.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre><code>
" Simple multiprocessing test.pystones benchmark "
" Anders Wallin 2008Jun15 anders.e.e.wallin (at) gmail.com "
from test import pystone
import processing
import time

STONES_PER_PROCESS= 10*pystone.LOOPS

def f(q):
    t=pystone.pystones(STONES_PER_PROCESS)
    q.put(t,block=True)

if __name__ == '__main__':
    print 'multiprocessing test.pystones() benchmark'
    print 'You have '+str(processing.cpuCount()) + ' CPU(s)'
    print 'Processes\tPystones\tWall time\tpystones/s'

    results = processing.Queue()
    for N in range(1,processing.cpuCount()+3):
        p=[]
        q=processing.Queue()
        results=[]

        for m in range(1,N+1):
            p.append( processing.Process(target=f,args=(q,)) )

        start=time.time()
        for pr in p:
            pr.start()
        for r in p:
            results.append( q.get() )
        stop=time.time()

        cputime = stop-start    

        print str(N)+'\t\t'+str(N*STONES_PER_PROCESS) \
              +'\t\t'+ str(cputime)+'\t'+str( N*STONES_PER_PROCESS / cputime )
</code></pre>
<p><a href="http://www.anderswallin.net/wp-content/uploads/2008/06/multi31.png"><img class="alignnone size-medium wp-image-449" title="multi31" src="http://www.anderswallin.net/wp-content/uploads/2008/06/multi31.png" alt="" width="450" height="336" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.anderswallin.net/2008/06/multiprocessing-pystone-benchmark/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
