<?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>Patrick's playground</title>
	<atom:link href="http://www.vankouteren.eu/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vankouteren.eu/blog</link>
	<description>Thoughts, problems and solutions</description>
	<lastBuildDate>Mon, 30 Aug 2010 06:28:27 +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>Starting a CLI script from PHP</title>
		<link>http://www.vankouteren.eu/blog/2010/06/starting-a-cli-script-from-php/</link>
		<comments>http://www.vankouteren.eu/blog/2010/06/starting-a-cli-script-from-php/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 10:05:45 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[background process]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[exec]]></category>
		<category><![CDATA[ignore_user_abort]]></category>
		<category><![CDATA[passthru]]></category>
		<category><![CDATA[pcntl]]></category>
		<category><![CDATA[shell_exec]]></category>
		<category><![CDATA[system]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=205</guid>
		<description><![CDATA[For quite some time I've searched for a way to start a command line script from within a page in PHP without using the crontab. Basically I just wanted to start a script which does some processing in the background. The solution is pretty simple, but it took me some time to find it.We all [...]]]></description>
			<content:encoded><![CDATA[<p>For quite some time I've searched for a way to start a command line script from within a page in PHP without using the crontab. Basically I just wanted to start a script which does some processing in the background. The solution is pretty simple, but it took me some time to find it.<span id="more-205"></span>We all know the crontab: it can execute a script at a particular moment for you, based on what you define to be the moment. But what if you want to execute a background at the moment you would like to?</p>
<p>PHP defines four methods which can execute commands: <a title="exec" href="http://www.php.net/manual/en/function.exec.php" target="_blank">exec</a>, <a title="passthru" href="http://www.php.net/manual/en/function.passthru.php" target="_blank">passthru</a>, <a title="shell_exec" href="http://www.php.net/manual/en/function.shell-exec.php" target="_blank">shell_exec</a> and <a title="system" href="http://www.php.net/manual/en/function.system.php" target="_blank">system</a>. These methods wait for the script to finish and collect the output generated by that script. However, when a user leaves the page, closes his browser or stops the script, the background script which is executed is also stopped. To prevent this, people tend to use other options like <a title="pcntl" href="http://php.net/manual/en/book.pcntl.php" target="_blank">pcnt</a>l or <a title="cURL" href="http://www.php.net/manual/en/book.curl.php" target="_blank">cURL</a>. Using these extensions might offer a solution, but for simply starting a script they are much too verbose.</p>
<p>The solution presented is applicable in the situation where you just need to start a script and you don't want users to be able to stop the script after starting. And the solution is....</p>
<p>The function <a title="ignore_user_abort" href="http://www.php.net/manual/en/function.ignore-user-abort.php" target="_blank">ignore_user_abort()</a>.</p>
<p>By putting this function in front of a shell_exec execution, the script still starts the script executed by shell_exec. However, it doesn't care any more if the user aborts the calling script. In my situation, the CLI script updates a status file and a progress file. When running, the status file contains an integer which denotes that the CLI script is running. This prevents the script from being run again while it's already running. Furthermore, the progress file is updated so the progress can be checked by other scripts.</p>
<p><strong>Source</strong></p>
<p><strong><a href="http://stackoverflow.com/questions/265073/php-background-processes">http://stackoverflow.com/questions/265073/php-background-processes</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2010/06/starting-a-cli-script-from-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP multidimensional array to plain text tree structure</title>
		<link>http://www.vankouteren.eu/blog/2010/04/php-multidimensional-array-to-plain-text-tree-structure/</link>
		<comments>http://www.vankouteren.eu/blog/2010/04/php-multidimensional-array-to-plain-text-tree-structure/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 12:33:05 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[multidimensional array]]></category>
		<category><![CDATA[Tree]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=197</guid>
		<description><![CDATA[Not for the first time I've searched for a way to visualize a multidimensional array in a tree view in my web browser without directly using echo or print. I wasn't able to find a concrete solution for this previously. However, this time I was put on the right track by a post of Kevin [...]]]></description>
			<content:encoded><![CDATA[<p>Not for the first time I've searched for a way to visualize a multidimensional array in a tree view in my web browser without directly using echo or print. I wasn't able to find a concrete solution for this previously. However, this time I was put on the right track by a post of <a href="http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/" title="Convert anything to Tree Structures in PHP">Kevin van Zonneveld</a>. <span id="more-197"></span>I've given it my own (quick and dirty as can be seen from the code) twist to get it to work. By just calling
<pre class="php"><a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <a href="http://www.php.net/implode"><span style="color: #000066;">implode</span></a><span style="color: #66cc66;">&#40;</span>ArrayFunctions::<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre>
<p> the array is shown as a tree structure on the screen.</p>
<p>The implementation is as follows:</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span></pre>
<pre class="php"><span style="color: #000000; font-weight: bold;">class</span> ArrayFunctions <span style="color: #66cc66;">&#123;</span></pre>
<pre class="php">  <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> toString<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></pre>
<pre class="php">    <span style="color: #0000ff;">$result</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<pre class="php">    <span style="color: #0000ff;">$depth</span> = <span style="color: #cc66cc;">0</span>;</pre>
<pre class="php">    <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$k</span> =&gt; <span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$show_val</span> = <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #ff0000;">&quot;&quot;</span> : <span style="color: #0000ff;">$v</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">// show the indents</span>
            <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <a href="http://www.php.net/str_repeat"><span style="color: #000066;">str_repeat</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;  &quot;</span>, <span style="color: #0000ff;">$depth</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$depth</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// this is a root node. no parents</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #ff0000;">&quot;O &quot;</span>;
            <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// this is a normal node. parents and children</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #ff0000;">&quot;+ &quot;</span>;
            <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// this is a leaf node. no children</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #ff0000;">&quot;- &quot;</span>;
            <span style="color: #66cc66;">&#125;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">// show the actual node</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$show_val</span> == <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #ff0000;">&quot;&lt;strong&gt;{$k}&lt;/strong&gt;:
&quot;</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #0000ff;">$k</span> . <span style="color: #ff0000;">&quot; (&quot;</span>.<span style="color: #0000ff;">$show_val</span>.<span style="color: #ff0000;">&quot;)&quot;</span>.<span style="color: #ff0000;">&quot;
&quot;</span>;
            <span style="color: #66cc66;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// this is what makes it recursive, rerun for childs</span>
                <span style="color: #0000ff;">$temp</span> = self::<span style="color: #006600;">toTree</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$v</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$depth</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$temp</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #0000ff;">$t</span>;
                <span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> <a href="http://www.php.net/implode"><span style="color: #000066;">implode</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> showtype<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$show_val</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// convert bools to text and quote 'text bools'!</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_string"><span style="color: #000066;">is_string</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$show_val</span><span style="color: #66cc66;">&#41;</span> &amp;&amp;
           <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$show_val</span> == <span style="color: #ff0000;">&quot;true&quot;</span> || <span style="color: #0000ff;">$show_val</span> == <span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>{$show_val}<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_bool"><span style="color: #000066;">is_bool</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$show_val</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #0000ff;">$show_val</span> === <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;true&quot;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_bool"><span style="color: #000066;">is_bool</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$show_val</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #0000ff;">$show_val</span> === <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;false&quot;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">elseif</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_null"><span style="color: #000066;">is_null</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$show_val</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;null&quot;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$show_val</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.php.net/static"><span style="color: #000066;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> toTree<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pieces</span>, <span style="color: #0000ff;">$depth</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pieces</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$k</span> =&gt; <span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// skip the baseval thingy. Not a real node.</span>
            <span style="color: #808080; font-style: italic;">//if($k == &quot;__base_val&quot;) continue;</span>
            <span style="color: #808080; font-style: italic;">// determine the real value of this node.</span>
            <span style="color: #0000ff;">$show_val</span> = <span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #ff0000;">&quot;&quot;</span> : <span style="color: #0000ff;">$v</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #0000ff;">$show_val</span> = self::<span style="color: #006600;">showtype</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$show_val</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">// show the indents</span>
            <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <a href="http://www.php.net/str_repeat"><span style="color: #000066;">str_repeat</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;  &quot;</span>, <span style="color: #0000ff;">$depth</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$depth</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// this is a root node. no parents</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #ff0000;">&quot;O &quot;</span>;
            <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// this is a normal node. parents and children</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #ff0000;">&quot;+ &quot;</span>;
            <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// this is a leaf node. no children</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #ff0000;">&quot;- &quot;</span>;
            <span style="color: #66cc66;">&#125;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">// show the actual node</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$show_val</span> == <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #ff0000;">&quot;&lt;strong&gt;{$k}&lt;/strong&gt;:
&quot;</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #0000ff;">$k</span> . <span style="color: #ff0000;">&quot;: &lt;i&gt;{$show_val}&lt;/i&gt;
&quot;</span>;
            <span style="color: #66cc66;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$v</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #808080; font-style: italic;">// this is what makes it recursive, rerun for childs</span>
                <span style="color: #0000ff;">$temp</span> = self::<span style="color: #006600;">toTree</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$v</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$depth</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #000066;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$temp</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$temp</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$t</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                        <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #0000ff;">$t</span>;
                    <span style="color: #66cc66;">&#125;</span>
                <span style="color: #66cc66;">&#125;</span>
                <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0000ff;">$result</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>= <span style="color: #0000ff;">$t</span>;
                <span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$result</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2010/04/php-multidimensional-array-to-plain-text-tree-structure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>K Means clustering and Histogram equalization source code</title>
		<link>http://www.vankouteren.eu/blog/2010/03/k-means-clustering-and-histogram-equalization-source-code/</link>
		<comments>http://www.vankouteren.eu/blog/2010/03/k-means-clustering-and-histogram-equalization-source-code/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 06:41:54 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[JAVA]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Histogram equalization]]></category>
		<category><![CDATA[k-means]]></category>
		<category><![CDATA[K-means clustering]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=195</guid>
		<description><![CDATA[As there was some interest in source code in two earlier posts (post 1, post 2) I've posted the source code here. It's also available through the download page. Please note that the code itself is at least 4 years old and there are far better ways to solve some things. I therefore suggest that [...]]]></description>
			<content:encoded><![CDATA[<p>As there was some interest in source code in two earlier posts (<a href="http://www.vankouteren.eu/blog/2009/09/k-means-clustering-in-java-code-found/" target="_blank">post 1</a>, <a href="http://www.vankouteren.eu/blog/2007/10/k-means-clustering-implementation-in-java/" target="_blank">post 2</a>) I've posted the source code <a href="http://www.vankouteren.eu/downloads/Ispe-dev.zip" target="_blank">here</a>.</p>
<p>It's also available through the <a href="http://www.vankouteren.eu/blog/downloads/" target="_blank">download page</a>.</p>
<p>Please note that the code itself is at least 4 years old and there are far better ways to solve some things. I therefore suggest that it is used for studying purposes only. Comments and improvments are very much appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2010/03/k-means-clustering-and-histogram-equalization-source-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting YUM to work on CentOS</title>
		<link>http://www.vankouteren.eu/blog/2010/02/getting-yum-to-work-on-centos/</link>
		<comments>http://www.vankouteren.eu/blog/2010/02/getting-yum-to-work-on-centos/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 13:39:33 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Webhosting]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[install yum]]></category>
		<category><![CDATA[rpm]]></category>
		<category><![CDATA[yum]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=190</guid>
		<description><![CDATA[One of my servers is running CentOS. As I wanted to upgrade PHP, I was looking how to do that. I've found the YUM command to be useful (yum upgrade php). However, for being able to upgrade this way, one needs yum to be installed. I was 'fortunate' it wasn't so another task was born: [...]]]></description>
			<content:encoded><![CDATA[<p>One of my servers is running CentOS. As I wanted to upgrade PHP, I was looking how to do that. I've found the YUM command to be useful (yum upgrade php). However, for being able to upgrade this way, one needs yum to be installed.</p>
<p>I was 'fortunate' it wasn't so another task was born: installing YUM on CentOS.<span id="more-190"></span>For installing, I've found <a href="http://wiki.openvz.org/Install_yum" target="_blank">this</a> source to be quite useful. However, I got some warnings because of dependencies. To resolve these dependencies I've upgraded the rpm packages which were denoted by the installer by hand, but the command suggested at the last post in <a href="http://forums.fedoraforum.org/archive/index.php/t-173341.html" target="_blank">this thread</a> might also work directly.</p>
<p>The names of the most recent rpm packages are listed <a href="http://mirror.centos.org/centos-5/5/os/i386/CentOS/" target="_blank">here</a>. As the rpm packages have circular dependencies one of the packages has to be installed with the --nodeps argument so these dependencies are not taken into account.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2010/02/getting-yum-to-work-on-centos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PEAR install on Linux by a n00b for n00bs</title>
		<link>http://www.vankouteren.eu/blog/2010/01/pear-install-on-linux-by-a-n00b-for-n00bs/</link>
		<comments>http://www.vankouteren.eu/blog/2010/01/pear-install-on-linux-by-a-n00b-for-n00bs/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 12:04:23 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Webhosting]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PEAR]]></category>
		<category><![CDATA[PEAR install]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=186</guid>
		<description><![CDATA[Okay, I must admit: I'm a programmer and I know some commands. Because I want to use PEAR packages, I needed to install it. The installing command is quite easy to find, but I had a hard time determining the directory structure. Some searching on the internet and an IRC conversation (EFNet #pear, thanks davidc_!) [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, I must admit: I'm a programmer and I know some commands. Because I want to use PEAR packages, I needed to install it. The installing command is quite easy to find, but I had a hard time determining the directory structure. Some searching on the internet and an IRC conversation (EFNet #pear, thanks davidc_!) later I was able to install PEAR in a quite reasonable directory structure.<span id="more-186"></span></p>
<p>Now that all is done it sounds logical. This blogpost is meant for people who want to install PEAR, but are not sure how to do it or about the directories which need to be specified.</p>
<p><strong>Starting install</strong></p>
<p>Log on to your server on the command line (as root). The install acan then be started as follows:</p>
<pre>lynx -source http://pear.php.net/go-pear | php</pre>
<p><strong>Directories</strong></p>
<p>After some questions (which in 99% of the cases just get the default answer), you'll get the prompt for the directories. After adjustment, my structure looks as follows:</p>
<pre>1. Installation prefix ($prefix) : /usr
2. Temporary files directory     : $prefix/temp
3. Binaries directory            : $prefix/bin
4. PHP code directory ($php_dir) : $prefix/share/PEAR
5. Documentation base directory  : $php_dir/docs
6. Data base directory           : $php_dir/data
7. Tests base directory          : $php_dir/tests</pre>
<p><strong>Finishing installation</strong></p>
<p>When the installation is done, the config (php.ini) file can automatically be adjusted. Note that you have to restart apache to load this new config.</p>
<p><strong>The full overview</strong></p>
<p>A complete overview from my PEAR install in- and output is given below.</p>
<pre>[root@server usr]# lynx -source http://pear.php.net/go-pear | php
Welcome to go-pear!

Go-pear will install the 'pear' command and all the files needed by
it.  This command is your tool for PEAR installation and maintenance.

Go-pear also lets you download and install the following optional PEAR
packages: PEAR_Frontend_Web-beta, PEAR_Frontend_Gtk2, MDB2.

If you wish to abort, press Control-C now, or press Enter to continue:

HTTP proxy (http://user:password@proxy.myhost.com:port), or Enter for none::

Below is a suggested file layout for your new PEAR installation.  To
change individual locations, type the number in front of the
directory.  Type 'all' to change all of them or simply press Enter to
accept these locations.

1. Installation prefix ($prefix) : /usr
2. Temporary files directory     : $prefix/temp
3. Binaries directory            : $prefix/bin
4. PHP code directory ($php_dir) : $prefix/PEAR
5. Documentation base directory  : $php_dir/docs
6. Data base directory           : $php_dir/data
7. Tests base directory          : $php_dir/tests

1-7, 'all' or Enter to continue: 4
PHP code directory ($php_dir) [$prefix/PEAR] : $prefix/share/PEAR

Below is a suggested file layout for your new PEAR installation.  To
change individual locations, type the number in front of the
directory.  Type 'all' to change all of them or simply press Enter to
accept these locations.

1. Installation prefix ($prefix) : /usr
2. Temporary files directory     : $prefix/temp
3. Binaries directory            : $prefix/bin
4. PHP code directory ($php_dir) : $prefix/share/PEAR
5. Documentation base directory  : $php_dir/docs
6. Data base directory           : $php_dir/data
7. Tests base directory          : $php_dir/tests

1-7, 'all' or Enter to continue:
PHP Warning:  putenv(): Safe Mode warning: Cannot set environment variable 'TMPDIR' - it's not in the allowed list in /usr/- on line 1264

The following PEAR packages are bundled with PHP: PEAR_Frontend_Web-beta,
PEAR_Frontend_Gtk2, MDB2.
Would you like to install these as well? [Y/n] : n

Loading zlib: ok

Bootstrapping Installer...................
Bootstrapping PEAR.php............(remote) ok
Bootstrapping Archive/Tar.php............(remote) ok
Bootstrapping Console/Getopt.php............(remote) ok

Extracting installer..................
Downloading package: PEAR.............ok
Downloading package: Structures_Graph....ok

Preparing installer..................
Updating channel "doc.php.net"
Update of Channel "doc.php.net" succeeded
Updating channel "pear.php.net"
Update of Channel "pear.php.net" succeeded
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded

Installing selected packages..................
Downloading and installing package: PEAR.............warning: pear/PEAR requires package "pear/Archive_Tar" (recommended version 1.3.3)
warning: pear/PEAR requires package "pear/Structures_Graph" (recommended version 1.0.2)
warning: pear/PEAR requires package "pear/Console_Getopt" (recommended version 1.2.3)
warning: pear/PEAR requires package "pear/XML_Util" (recommended version 1.2.1)
downloading PEAR-1.9.0.tgz ...
Starting to download PEAR-1.9.0.tgz (291,634 bytes)
.............................................................done: 291,634 bytes
install ok: channel://pear.php.net/PEAR-1.9.0
PEAR: Optional feature webinstaller available (PEAR's web-based installer)
PEAR: Optional feature gtkinstaller available (PEAR's PHP-GTK-based installer)
PEAR: Optional feature gtk2installer available (PEAR's PHP-GTK2-based installer)
PEAR: To install optional features use "pear install pear/PEAR#featurename"
Installing bootstrap package: Structures_Graph.......install ok: channel://pear.php.net/Structures_Graph-1.0.3
Downloading and installing package: Archive_Tar-stable.......downloading Archive_Tar-1.3.5.tgz ...
Starting to download Archive_Tar-1.3.5.tgz (17,184 bytes)
...done: 17,184 bytes
install ok: channel://pear.php.net/Archive_Tar-1.3.5
Downloading and installing package: Console_Getopt-stable.......downloading Console_Getopt-1.2.3.tgz ...
Starting to download Console_Getopt-1.2.3.tgz (4,011 bytes)
...done: 4,011 bytes
install ok: channel://pear.php.net/Console_Getopt-1.2.3

******************************************************************************
WARNING!  The include_path defined in the currently used php.ini does not
contain the PEAR PHP directory you just specified:
&lt;/usr/share/PEAR&gt;
If the specified directory is also not in the include_path used by
your scripts, you will have problems getting any PEAR packages working.

Would you like to alter php.ini &lt;/etc/php.ini&gt;? [Y/n] : Y

php.ini &lt;/etc/php.ini&gt; include_path updated.

Current include path           : .:
Configured directory           : /usr/share/PEAR
Currently used php.ini (guess) : /etc/php.ini
Press Enter to continue:

The 'pear' command is now at your service at /usr/bin/pear
[root@server usr]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2010/01/pear-install-on-linux-by-a-n00b-for-n00bs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upgrading Plesk &#8211; Useful commands</title>
		<link>http://www.vankouteren.eu/blog/2010/01/upgrading-plesk-useful-commands/</link>
		<comments>http://www.vankouteren.eu/blog/2010/01/upgrading-plesk-useful-commands/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 12:46:33 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Plesk]]></category>
		<category><![CDATA[commands]]></category>
		<category><![CDATA[upgrade plesk]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=180</guid>
		<description><![CDATA[I've been upgrading Plesk today and yesterday. Although Plesk has a nice interface, it's not 100% clear in what happens. After some help on the FreeNode #plesk IRC channel and some cyberciti.biz pages, I've composed some details and commands to execute at the command line which can help when upgrading Plesk. To Start Plesk /etc/init.d/psa [...]]]></description>
			<content:encoded><![CDATA[<p>I've been upgrading Plesk today and yesterday. Although Plesk has a nice interface, it's not 100% clear in what happens. After some help on the FreeNode #plesk IRC channel and some cyberciti.biz pages, I've composed some details and commands to execute at the command line which can help when upgrading Plesk.</p>
<p><span id="more-180"></span><strong>To Start Plesk</strong></p>
<pre>/etc/init.d/psa start</pre>
<p><strong>To Stop Plesk</strong></p>
<pre>/etc/init.d/psa stop</pre>
<p><strong>Upgrade through autoinstaller</strong></p>
<pre>/usr/local/psa/admin/bin/autoinstaller</pre>
<p><strong>Restart apache</strong></p>
<pre>/etc/init.d/httpd restart</pre>
<p><strong>Check the running processes.</strong></p>
<p>The first column represents the owner. The second column contains the process id.</p>
<p><a href="http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/" target="_blank">Source</a></p>
<pre>ps aux | grep autoinstall</pre>
<p>or</p>
<pre>ps u</pre>
<p><strong>Killing a process</strong></p>
<pre>kill -9 [processid]</pre>
<p><strong>Backup an RPM database and rebuild it.</strong></p>
<p><a href="http://www.cyberciti.biz/faq/how-can-i-backup-the-rpm-database/" target="_blank"> source 1</a></p>
<p><a href="http://www.cyberciti.biz/tips/tag/rpm-rebuilddb" target="_blank">source 2</a></p>
<p><em>Remove any stale lock file</em></p>
<pre>/bin/rm -f /var/lib/rpm/__db*</pre>
<p><em>Back up the /var/lib/rpm directory</em></p>
<pre>tar czvf $(hostname).rpmdatabase.tar.gz /var/lib/rpm</pre>
<p>If your hostname is linux-server, it will save backup to a file called linux-server.rpmdatabase.tar.gz</p>
<p>Many thanks also go out to 'koor' who helped me at the #plesk IRC channel at FreeNode.</p>
<p><strong>Collision of RPM files (RPM already exists)<br />
</strong></p>
<p>When upgrading I encountered a collision of an RPM file which already existed. Because of that the upgrade could not proceed. A solution to that is presented <a href="http://forums.theplanet.com/index.php?showtopic=85964" target="_blank">here</a> in the last post. It comes down to:</p>
<ul>
<li>Navigating to the folder which holds the rpm file (e.g. cd  /root/psa/PSA_8.1.0/dist-rpm-RedHat-el3-i386/base)</li>
<li>Forcing an update on the RPM. (e.g. rpm -U --force --nodeps psa-8.1.0-rhel3.build81061129.22.i586.rpm)</li>
</ul>
<p>Running the updater after repairing the file will solve the problem.</p>
<p><strong>SSH connection is refused</strong><br />
I had this problem once. After a little search I found <a href="http://coastalweb.ca/system-administration/how-to-start-sshd-on-plesk.html">this</a> post. It's brilliant! From Plesk itself you start a cronjob (run it as root user) which (re)starts (or use restart instead of start) the ssh daemon. After that my problem was solved.</p>
<p><strong>Virtual hosts</strong><br />
By default Plesk has a pre-defined 'behavior' of a domain. This includes some settings for PHP like safe mode and open basedir restrictions. By default these settings are stored in a file called 'httpd.include' in the folder
<pre>/var/www/vhosts/yourdomain.ext/conf/</pre>
<p>You can edit this file to change the behavior of the domain, but changes in this file are just temporary. Once the file is reloaded your changes will be gone.<br />
To permanently make changes you need to create or edit a file called 'vhost.conf' which is located in the same directory. When loading the httpd.include file, the webserver also checks if this vhost.conf file exists and if so, it includes it. How you can create this virtual host file can be found on <a href="http://webhostingneeds.com/Disable_open_basedir_in_plesk">this</a> page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2010/01/upgrading-plesk-useful-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing using WampServer 2: additions</title>
		<link>http://www.vankouteren.eu/blog/2009/12/developing-using-wampserver-2-additions/</link>
		<comments>http://www.vankouteren.eu/blog/2009/12/developing-using-wampserver-2-additions/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 13:42:38 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[crontab]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[WampServer]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=175</guid>
		<description><![CDATA[WampServer 2.0 is a nice tool to help you develop a new site on your local computer. However, you'll probably need more packages/libraries than those included with WampServer. This post is about installing extra common features which are mostly present at hosting servers, but not (directly) on WampServer. PHPUnit One of the features which I [...]]]></description>
			<content:encoded><![CDATA[<p><a title="WampServer 2.0" href="http://www.wampserver.com/en/" target="_blank">WampServer 2.0</a> is a nice tool to help you develop a new site on your local computer. However, you'll probably need more packages/libraries than those included with WampServer. This post is about installing extra common features which are mostly present at hosting servers, but not (directly) on WampServer.</p>
<p><span id="more-175"></span></p>
<p><strong>PHPUnit</strong></p>
<p>One of the features which I need when starting a new project is a Unittest framework. The two most common are <a title="SimpleTest" href="http://www.simpletest.org/" target="_blank">SimpleTest</a> and <a title="PHPUnit" href="http://www.phpunit.de/" target="_blank">PHPUnit</a>. SimpleTest can be downloaded and used right away. The <a title="PHPUnit manual" href="http://www.phpunit.de/manual/current/en/installation.html" target="_blank">PHPUnit manual</a> advises to use PEAR to install PHPUnit. <a title="Install PEAR and PHPUnit" href="http://jsdoodnauth.wordpress.com/2008/11/05/installing-wamp-and-phpunit-on-windows/" target="_blank">Joshua Doodnauth</a> explains how to activate PEAR and how to install PHPUnit on WampServer. However, as I was running WampServer 2.0i with PHP 5.3, the file go-pear.phar in the PEAR subdirectory was not working correctly. The solution to this is presented <a title="Install PEAR and PHPUnit on WampServer 2.0 with PHP 5.3" href="http://blog.pear.php.net/2009/07/01/php-53-windows-and-pear/" target="_blank">here</a>. By executing the command</p>
<p><code>php -d phar.require_hash=0 go-pear.phar</code></p>
<p>the problem is solved and PEAR is installed.</p>
<p>After that however, when I tried to get back to Joshua's how-to, the PEAR version was not correct. <a title="Update PEAR" href="http://www.electricmonk.nl/log/2009/04/12/easy-pear-package-creation/" target="_blank">This site</a> helped me to update PEAR. When PEAR is installed, type</p>
<p><code>pear channel-update pear.php.net</code></p>
<p>in the terminal. This will update the PEAR channel. After that, by typing in</p>
<p><code>pear upgrade PEAR</code></p>
<p>the PEAR installation will be updated. From that point on PHPUnit can be installed as described at Joshua's blog.</p>
<p><strong>Crontab</strong></p>
<p>A feature which I use pretty often is the crontab. It can schedule and execute scripts at particular moments. A crontab is not provided by WampServer. However, there are external solutions available. One such solutions is <a title="nnSoft :: nnCron" href="http://www.nncron.ru/" target="_blank">nnCron of nnSoft</a>. This is a crontab which can be installed by executing the installer file. The LITE (free) version already provides the desired functionality. The way it works is pretty simple: in the install directory there are batch files startcron.bat and stopcron.bat which allow to start the crontab and stop it. The file cron.tab contains the <a title="Crontab lines" href="http://www.willmaster.com/library/web-development/using_cron.php" target="_blank">crontab lines</a>.</p>
<p>N.B. Using WampServer and this Crontab, you can write automated tasks in PHP for executing tasks on your local machine. (Nice!).</p>
<p><strong>Mootools</strong></p>
<p>Today I was unpleasantly surprised that Mootools doesn't work on WampServer. I tried various demo's of AJAX calls, but none of them worked. When I tried one on an online web server it worked directly. So far I haven't been able to find topics about this problem, so any help is appreciated!'</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2009/12/developing-using-wampserver-2-additions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Taverna and R: Rserve</title>
		<link>http://www.vankouteren.eu/blog/2009/10/taverna-and-r-rserve/</link>
		<comments>http://www.vankouteren.eu/blog/2009/10/taverna-and-r-rserve/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 06:58:29 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=167</guid>
		<description><![CDATA[So in my previous post I wanted to do a hypergeometric test in R. Now I want this test to be executed from Taverna (RShell). This means we need to install R as a server. How to do this is explained in detail here. Once installed we need to start Rserve to allow Taverna to [...]]]></description>
			<content:encoded><![CDATA[<p>So in my <a title="A hypergeometric test in R" href="http://www.vankouteren.eu/blog/2009/10/hypergeometric-test-python-or-r/" target="_blank">previous post</a> I wanted to do a hypergeometric test in R. Now I want this test to be executed from Taverna (<a title="RShell" href="http://www.narcis.info/dare/RecordID/oaidocutwentenl67553/repository_id/ut" target="_blank">RShell</a>).<span id="more-167"></span> This means we need to install R as a server. How to do this is explained in detail <a title="Install Rserve" href="http://www.jsoftware.com/jwiki/Interfaces/R/Rserve" target="_blank">here</a>. Once installed we need to start Rserve to allow Taverna to connect to it. This is done as follows:</p>
<pre>&amp;gt; library(Rserve)
&amp;gt; Rserve(args='--no-save')</pre>
<p>Note that the args can be 'save', 'no-save' or 'vanilla'.</p>
<p><strong>Taverna</strong></p>
<p>In Taverna an RShell can be added to the workflow to execute an R script. This script can be edited / defined by right-clicking the node and choosing that particular option.</p>
<p>Currently, when running a workflow in Taverna 2.1 beta2 and R 2.9.2 results in a handshake error. Apparently Rserve uses a newer protocol version than Taverna does.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2009/10/taverna-and-r-rserve/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>K-means clustering in Java code found!</title>
		<link>http://www.vankouteren.eu/blog/2009/09/k-means-clustering-in-java-code-found/</link>
		<comments>http://www.vankouteren.eu/blog/2009/09/k-means-clustering-in-java-code-found/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 13:36:45 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[JAVA]]></category>
		<category><![CDATA[clustering]]></category>
		<category><![CDATA[k-means]]></category>
		<category><![CDATA[K-means clustering]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=144</guid>
		<description><![CDATA[My blogpost on K-means clustering has the highest number of views, so people are probably interested in it. Sadly enough I lost the source code of the K-means action a while ago. Last week I needed an external harddisk to make a back-up of some files. There was already some content on the disk. I [...]]]></description>
			<content:encoded><![CDATA[<p>My <a title="K-means clustering implementation in JAVA" href="http://www.vankouteren.eu/blog/2007/10/k-means-clustering-implementation-in-java/">blogpost on K-means clustering</a> has the highest number of views, so people are probably interested in it. Sadly enough I lost the source code of the K-means action a while ago. Last week I needed an external harddisk to make a back-up of some files. There was already some content on the disk. I found quite some pieces of code including the K-means code. Although it is quite simple code operating on (if I remember correctly 8-bit) greyscale images, it might give some insights in how to do this.</p>
<p><span id="more-144"></span>The whole code file is presented below. For more information you can view <a title="K-means clustering implementation in JAVA" href="http://www.vankouteren.eu/blog/2007/10/k-means-clustering-implementation-in-java/">my earlier blogpost</a> on K-means clustering.</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">package</span> actions;
&nbsp;
<span style="color: #a1a100;">import java.awt.Color;</span>
<span style="color: #a1a100;">import java.awt.image.BufferedImage;</span>
<span style="color: #a1a100;">import java.util.ArrayList;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * This KMeansAction performs a K-means clustering action on a BufferedImage
 * @author Patrick van Kouteren
 *
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> KMeansAction <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABufferedImage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">BufferedImage</span></a> image_temp;
		<span style="color: #993333;">boolean</span> not_terminated;
		<span style="color: #993333;">int</span> loops, changedPixels;
		<span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> histogram;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AArrayList+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a> classes;
		<span style="color: #993333;">int</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> lowerbounds;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span> MEAN_BY_MOD = <span style="color: #cc66cc;">1</span>;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span> MEAN_BY_SPACE = <span style="color: #cc66cc;">2</span>;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">int</span> MEAN_AT_RANDOM = <span style="color: #cc66cc;">3</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Controls the actual work:
		 * - Initialization
		 * - Loop until termination condition is met
		 *  + for each pixel: assign pixel to a class such that the distance from the pixel to the mean of that class is minimized
		 *  + for each class: recalculate the means of the class based on pixels belonging to that class
		 * - End loop
		 * @param image
		 * @param bins (k)
		 * @param histogram
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> KMeansAction<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABufferedImage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">BufferedImage</span></a> image, <span style="color: #993333;">int</span> bins, <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>histogram, <span style="color: #993333;">int</span> initway<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">histogram</span> = histogram;
			lowerbounds = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">int</span><span style="color: #66cc66;">&#91;</span>bins<span style="color: #66cc66;">&#93;</span>;
			initialize<span style="color: #66cc66;">&#40;</span>image, bins, initway<span style="color: #66cc66;">&#41;</span>;
			calculateBounds<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>not_terminated<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				recalculateMeans<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				loops++;
				checkTermination<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
			processImage<span style="color: #66cc66;">&#40;</span>image, bins<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Set the new color values for the image
		 * @param image
		 */</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> processImage<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABufferedImage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">BufferedImage</span></a> image, <span style="color: #993333;">int</span> bins<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #993333;">int</span> delta = <span style="color: #cc66cc;">255</span> / <span style="color: #66cc66;">&#40;</span>bins<span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> h = <span style="color: #cc66cc;">0</span>; h &amp;lt; image.<span style="color: #006600;">getHeight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; h++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> w = <span style="color: #cc66cc;">0</span>; w &amp;lt; image.<span style="color: #006600;">getWidth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; w++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AColor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Color</span></a> rgb = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AColor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Color</span></a><span style="color: #66cc66;">&#40;</span>image.<span style="color: #006600;">getRGB</span><span style="color: #66cc66;">&#40;</span>w, h<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #993333;">int</span> grey = rgb.<span style="color: #006600;">getRed</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span>; i classes.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">lowerbound</span> &amp;amp;&amp;amp; grey &amp;lt; classes.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">upperbound</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
							<span style="color: #993333;">int</span> g = i*delta;
							image_temp.<span style="color: #006600;">setRGB</span><span style="color: #66cc66;">&#40;</span>w,h,<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AColor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Color</span></a><span style="color: #66cc66;">&#40;</span>g, g, g<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getRGB</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
						<span style="color: #66cc66;">&#125;</span>
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Returns the image created by the processImage method
		 * @return the result image
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABufferedImage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">BufferedImage</span></a> getResultImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> image_temp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Just for fun: returns the number of loops which were needed for getting a stable result
		 * @return number of loops for stable result
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> getLoops<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> loops;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Initializes the algorithm. Creates k ClusterClasses and puts them into a LinkedList
		 * @param image
		 * @param bins
		 */</span>
		@SuppressWarnings<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;unchecked&quot;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> initialize<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABufferedImage+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">BufferedImage</span></a> image, <span style="color: #993333;">int</span> bins, <span style="color: #993333;">int</span> initway<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			image_temp = image;
			loops = <span style="color: #cc66cc;">0</span>;
			changedPixels = <span style="color: #cc66cc;">0</span>;
			not_terminated = <span style="color: #000000; font-weight: bold;">true</span>;
			classes = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AArrayList+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ArrayList</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span>; i &amp;lt; bins; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				ClusterClass cc = <span style="color: #000000; font-weight: bold;">new</span> ClusterClass<span style="color: #66cc66;">&#40;</span>createMean<span style="color: #66cc66;">&#40;</span>initway, bins, i, image<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
				classes.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span>cc<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Controls the calculations of the upper- and lowerbounds of ClusterClasses and sets them
		 *
		 */</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> calculateBounds<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span>; i &amp;lt; classes.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				<span style="color: #993333;">int</span> lb = calculateLowerBound<span style="color: #66cc66;">&#40;</span>classes.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
				lowerbounds<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = lb;
				classes.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setBounds</span><span style="color: #66cc66;">&#40;</span>lb,calculateUpperBound<span style="color: #66cc66;">&#40;</span>classes.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Does the actual calculation of the lowerbound
		 * @param ClusterClass
		 * @return Lowerbound
		 */</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> calculateLowerBound<span style="color: #66cc66;">&#40;</span>ClusterClass cc<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #993333;">int</span> cMean = cc.<span style="color: #006600;">getMean</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #993333;">int</span> currentBound = <span style="color: #cc66cc;">0</span>;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span>; i&amp;lt; classes.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 					<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>cMean &amp;gt; classes.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getMean</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
						currentBound = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Math</span></a>.<span style="color: #006600;">max</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cMean + classes.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getMean</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #cc66cc;">2</span>, currentBound<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #66cc66;">&#125;</span>
					<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">return</span> currentBound;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Does the actual calculation of the upperbound
		 * @param ClusterClass
		 * @return Upperbound
		 */</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">int</span> calculateUpperBound<span style="color: #66cc66;">&#40;</span>ClusterClass cc<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #993333;">int</span> cMean = cc.<span style="color: #006600;">getMean</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #993333;">int</span> currentBound = <span style="color: #cc66cc;">255</span>;
				<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span>; i&amp;lt; classes.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
						<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>cMean &amp;lt; classes.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getMean</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
							currentBound = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Math</span></a>.<span style="color: #006600;">min</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cMean + classes.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getMean</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>/<span style="color: #cc66cc;">2</span>, currentBound<span style="color: #66cc66;">&#41;</span>;
						<span style="color: #66cc66;">&#125;</span>
						<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
					<span style="color: #66cc66;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">return</span> currentBound;
				<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Takes care of the recalculation of the means of the ClusterClasses
		 *
		 */</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> recalculateMeans<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span>; i= <span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				not_terminated = <span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>changedPixels &amp;lt;= <span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				not_terminated = <span style="color: #000000; font-weight: bold;">false</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> calculateChangedPixels<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #993333;">int</span> changed = <span style="color: #cc66cc;">0</span>;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = <span style="color: #cc66cc;">0</span>; i&amp;lt; lowerbounds<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> j = c; j lowerbounds<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> j = lowerbounds<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>; j&amp;lt; image.<span style="color: #006600;">getHeight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; h++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> w = <span style="color: #cc66cc;">0</span>; w &amp;lt; image.<span style="color: #006600;">getWidth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; w++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
						pixelindex+=<span style="color: #cc66cc;">1</span>;
						<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>pixelindex % bins == index<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
							<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AColor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Color</span></a> rgb = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AColor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Color</span></a><span style="color: #66cc66;">&#40;</span>image.<span style="color: #006600;">getRGB</span><span style="color: #66cc66;">&#40;</span>w, h<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
							sum+= rgb.<span style="color: #006600;">getRed</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
							value+=<span style="color: #cc66cc;">1</span>;
						<span style="color: #66cc66;">&#125;</span>
					<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">return</span> sum/value;
&nbsp;
			<span style="color: #b1b100;">case</span> MEAN_BY_SPACE:
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span> / <span style="color: #66cc66;">&#40;</span>bins<span style="color: #cc66cc;">-1</span><span style="color: #66cc66;">&#41;</span> * index<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">case</span> MEAN_AT_RANDOM:
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADouble+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Double</span></a> dmean = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Math</span></a>.<span style="color: #006600;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> * <span style="color: #cc66cc;">255</span>;
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Math</span></a>.<span style="color: #006600;">floor</span><span style="color: #66cc66;">&#40;</span>dmean<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">default</span>:
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre>
<p>In addition to this, the custom class ClusterClass is defined as:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">package</span> actions;
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * The ClusterClass is just a class holding the important cluster properties.
 * @author Patrick van Kouteren
 *
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ClusterClass <span style="color: #66cc66;">&#123;</span>
	<span style="color: #993333;">int</span> mean, upperbound, lowerbound;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> ClusterClass<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> m<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		mean = m;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setBounds<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> lb, <span style="color: #993333;">int</span> ub<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		lowerbound = lb;
		upperbound = ub;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setMean<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		mean = i;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> getMean<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> mean;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> getLowerBound<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> lowerbound;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">int</span> getUpperBound<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> upperbound;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> calculateMean<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> histogram<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #993333;">int</span> tempMean = <span style="color: #cc66cc;">0</span>;
		<span style="color: #993333;">int</span> counter = <span style="color: #cc66cc;">0</span>;
		<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = lowerbound; i&amp;lt;= upperbound; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			counter += histogram<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
			tempMean += histogram<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> * i;
		<span style="color: #66cc66;">&#125;</span>
		mean = tempMean / counter;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre>
<p>The source code might not be completely visible. It can be viewed in a blank screen <a title="View code in blank screen" href="http://www.vankouteren.eu/downloads/KMeansAction.java">here</a>. As mentioned in the replies to this post, I forgot to add the ClusterClass. It can be viewed in a blank screen <a title="ClusterClass" href="http://www.vankouteren.eu/downloads/ClusterClass.java" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2009/09/k-means-clustering-in-java-code-found/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>FPDF error: Unable to find xref table</title>
		<link>http://www.vankouteren.eu/blog/2009/07/fpdf-error-unable-to-find-xref-table/</link>
		<comments>http://www.vankouteren.eu/blog/2009/07/fpdf-error-unable-to-find-xref-table/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 14:16:34 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[fpdf]]></category>
		<category><![CDATA[fpdi]]></category>
		<category><![CDATA[xref error]]></category>

		<guid isPermaLink="false">http://www.vankouteren.eu/blog/?p=124</guid>
		<description><![CDATA[Today I faced a problem with FPDF and FPDI when trying to concatenate some PDF files. The only output I got was 'FPDF error: Unable to find xref table.' When I did a search on this error message, I wasn't able to extract the answer from it. After some searching, the problem was located. One of [...]]]></description>
			<content:encoded><![CDATA[<p>Today I faced a problem with FPDF and <a title="FPDI: concatenate PDF files" href="http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/concatenate-fake/" target="_blank">FPDI when trying to concatenate some PDF files</a>. The only output I got was 'FPDF error: Unable to find xref table.' When I did a search on this error message, I wasn't able to extract the answer from it. After some searching, the problem was located.</p>
<p><span id="more-124"></span></p>
<p>One of the PDFs which should be merged was originally created from Word by a PDF creator which placed its signature in the properties of the PDF document. After removing this signature (in this case opening the PDF with Adobe Illustrator and saving it again) the problem was solved.</p>
<p><strong>Compression and stream<br />
</strong></p>
<p>The FPDF / FPDI classes use the input stream of the PDF file to create the concatenated PDF. Setasign <a title="Setasign remark" href="http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/concatenate-fake/" target="_blank">remarks</a> that the stream is only used (and altered) in the free version. The paid version should not give such errors. As I've experimented with the free version, I'll treat that one here.</p>
<p>Because of compression, some applications might leave out or mess up the xref property. Although this results in a PDF file which can be opened in a PDF reader, the stream is not according to the 'standards'. In such a case the document markup is 'lost' and applications / scripts using this property cannot handle these files any more.</p>
<p><strong>PDF merging code example</strong></p>
<p>To show how PDF files can be merged by using FPDF and FPDI I've compiled a little example which can be downloaded <a title="Downloads" href="http://www.vankouteren.eu/blog/downloads/" target="_self">here</a>. The archive consists of FPDF version 1.6, FPDI version 1.3.1, FPDI_TPL and two sample PDF files which were created with Microsoft Word 2007. The executing script is concat.php. Run this script to concatenate the two sample PDF files together in a new PDF file.</p>
<p><strong>Possible solutions</strong></p>
<p>A paid version of FPDI might be an option, but let's have a look at other possible solutions.</p>
<p>Opening the file and saving it again with a better pdf creator might fix the problem as it reformats the stream which is saved.</p>
<p><strong>Final remarks</strong></p>
<p>When you're creating the source PDF files yourself, watch out which pdf creator you use. Although the visual result might be the same, the stream might not be.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vankouteren.eu/blog/2009/07/fpdf-error-unable-to-find-xref-table/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>
