http://mpastell.com/mpastell.com - Articles (Atom Feed)2010-03-24T22:00:00ZMatti Pastellhttp://mpastell.comtag:mpastell.com,2010-03-25:/2010/03/25/create-odf-pdf-and-html-report-from-a-single-sweave-document/Create odf, pdf and html report from a single Sweave document2010-03-24T22:00:00Z2010-03-24T22:00:00Z<p>A lot of us know about Sweave and Latex and they work very well in creating elegant dynamic reports from R computation. However, sometimes we would like to also produce a word processing document for a colleague or a html version of the same report. Now there are tools for producing these like <a href="http://cran.r-project.org/web/packages/odfWeave/index.html">odfWeave</a>. But wouldn't it be nice to get all of the output formats from the source document with same mark up?</p>
<p>Today I'm going to write about a good option how to get a <em>nice looking</em> odf, pdf and html report from the same source: ascii package with <a href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> (reST) markup. reStructuredText is a simple plain text mark up that can be converted to several formats using the Python <a href="http://docutils.sourceforge.net/index.html">Docutils</a>. It is simpler to write than Latex and it also the syntax that is used in <a href="http://sphinx.pocoo.org">Sphinx</a> documents. The <a href="http://eusebe.github.com/ascii/">ascii</a> package has a Sweave driver for weaving R code from documents written with reST and some other mark up languages as well. I have created a simple example document <a href="http://files.mpastell.com/ascii/ascii-example.Rnw">ascii-example.Rnw</a>, which demonstrates the capabilities of the combination. The source file is a Sweave document with reST mark up instead of Latex. I had to make small modifications to the in ReST driver in ascii package to get this example to work<sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup> (download it here: <a href="http://files.mpastell.com/ascii/newRest.R">newRest.R</a>). I then processed the example with the new driver in R:</p>
<div class="highlight"><pre><span class="o">></span> library<span class="p">(</span>ascii<span class="p">)</span>
<span class="o">></span> source<span class="p">(</span><span class="s">'newRest.R'</span><span class="p">)</span>
<span class="o">></span> ReST<span class="p">(</span><span class="s">'ascii-example.Rnw'</span><span class="p">)</span>
</pre></div>
<p>Weaving produced this <a href="http://files.mpastell.com/ascii/ascii-example.rst">ascii-example.rst</a> reST document which I then converted to odf, html, pdf with pdflatex and pdf with <a href="http://code.google.com/p/rst2pdf/">rst2pdf</a> using:</p>
<div class="highlight"><pre>rst2odt ascii-example.rst ascii-example.odt
rst2html ascii-example.rst ascii-example.html
rst2latex ascii-example.rst ascii-example.tex <span class="o">&&</span> pdflatex ascii-example.tex
rst2pdf ascii-example.rst
</pre></div>
<p><strong>And here are the results:</strong> <a href="http://files.mpastell.com/ascii/ascii-example.odt">odt</a>, <a href="http://files.mpastell.com/ascii/ascii-example.html">html</a>, <a href="http://files.mpastell.com/ascii/ascii-example1.pdf">pdf</a> from latex and <a href="http://files.mpastell.com/ascii/ascii-example.pdf">pdf</a> from rst2pdf. You'll need to have <a href="http://docutils.sourceforge.net/index.html">Docutils</a> and <a href="http://www.rexx.com/~dkuhlman/odtwriter.html">odtwriter</a> for the conversion.</p>
<p>I have used the default options, but there are a lot options that can be passed to the docutils writers to customize the output, such as custom stylesheets. The image format in the pdfs is not optimal, but in can be changed with the "res" option in code chunk or you can also choose to use pdf format instead of jpg, but then it won't show up in html documents. I think using reST with ascii package is a good option for producing reports and tutorials in multiple output formats. That is also because I'm already familiar with reST directives and also use Sphinx for other purposes too. The reST syntax is also very fast to learn and I think it is definitely worth exploring. I also recommend using Sphinx if you only want to get html and pdf output, because it has more directives than plain reST e.g. math support.</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>I changed the driver to output code chunks in reST literal environment <code>::</code> instead of <code>.. codeblock:: r</code>, because the codeblock directive is not supported in odt conversion and very poorly supported in latex conversion. <a href="#fnref1" class="footnoteBackLink" title="Jump back to footnote 1">↩</a></p></li>
</ol>
</div>
tag:mpastell.com,2010-03-03:/2010/03/03/pweave-sweave-for-python/Pweave - Sweave for Python2010-03-02T22:00:00Z2010-03-02T22:00:00Z<p>After looking into different options for literate programming with Python I ended up writing my own tool and call it <strong>Pweave</strong>. I have tried to implement the functionality of Sweave, that I really like and use for my R projects. Pweave is a single python script that is able to weave a python code between <code><<>>=</code> and <code>@</code> blocks and include the results in the document. It can be used with reStructured text and Latex documents.</p>
<p><strong>Update:</strong>. I've set up a <a href="http://mpastell.com/pweave">Pweave website</a>, you can also download it <a href="http://files.mpastell.com/Pweave">here</a>.</p>
<p><strong>Codeblock options</strong> Pweave currently allows following options for the codeblocks (e.g. <>=): <em>fig</em> True or False. Whether a matplotlib plot produced in the codeblock should be included in the file. <em>echo</em> True or False. Echo the python code in the codeblock? <em>eval</em> True or False. Should the codeblock be evaluated? <em>results</em> "rst", "tex" or "verbatim". The output format of the printed results. <strong>Example:</strong> The syntax of Pweave is similar to Sweave, here is a small example sourcefile <a href="http://files.mpastell.com/ma.Pnw">ma.Pnw</a> which can be processed with Pweave using:</p>
<div class="highlight"><pre>Pweave ma.Pnw
</pre></div>
<p>which produces a reStructured text document <a href="http://files.mpastell.com/ma.rst">ma.rst</a>, which can in turn be processed to <a href="http://files.mpastell.com/ma.pdf">ma.pdf</a> with rst2latex and pdflatex.</p>
tag:mpastell.com,2010-02-09:/2010/02/09/python-in-sweave-document/Python in Sweave document2010-02-08T22:00:00Z2010-02-08T22:00:00Z<div id="TOC"><h2>Table of contents</h2>
<ul>
<li><a href="#modifications-to-the-custom-driver">Modifications to the custom driver:</a></li>
<li><a href="#example-usage">Example usage</a></li>
</ul>
</div>
<p>Lately I have been using a lot of Python for signal processing and I quite like SciPy. However, I have been missing something like Sweave, which is great literate programming environment for R. Today I managed to look a bit more into it and found this hack on how to use <a href="http://romainfrancois.blog.free.fr/index.php?post/2009/01/21/Python-and-Sweave">Python code in Sweave document</a> by Romain Francois. However, I wanted to be able to catch the input as well as output so I made a small changes to his Sweave Driver.</p>
<h4 id="modifications-to-the-custom-driver"><a href="#TOC">Modifications to the custom driver:</a></h4>
<div class="highlight"><pre>driver <span class="o"><-</span> RweaveLatex<span class="p">()</span>
runcode <span class="o"><-</span> driver<span class="p">$</span>runcode
driver<span class="p">$</span>runcode <span class="o"><-</span> <span class="kr">function</span><span class="p">(</span>object<span class="p">,</span> chunk<span class="p">,</span> options<span class="p">){</span>
<span class="kr">if</span><span class="p">(</span> options<span class="p">$</span>engine <span class="o">==</span> <span class="s">"python"</span><span class="p">)</span>
<span class="p">{</span> driver<span class="p">$</span>writedoc<span class="p">(</span> object<span class="p">,</span> c<span class="p">(</span><span class="s">"\\\\begin{verbatim}"</span><span class="p">,</span> chunk<span class="p">,</span>
<span class="s">"\\\\end{verbatim}"</span><span class="p">,</span> <span class="s">"\\\\begin{python}"</span><span class="p">,</span> chunk<span class="p">,</span><span class="s">"\\\\end{python}"</span><span class="p">)</span> <span class="p">)</span> <span class="p">}</span>
<span class="kr">else</span><span class="p">{</span> runcode<span class="p">(</span> object<span class="p">,</span> chunk<span class="p">,</span> options <span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<p>The driver now catches the input in verbatim environment (which can be easily changed to listings) and the output to python environment. The tex document can then be processed with the <a href="http://files.mpastell.com/python.sty">python latex package</a> to evaluate the python expressions. Tho use the driver you need to put the option "engine="python" in your code chunks.</p>
<h4 id="example-usage"><a href="#TOC">Example usage</a></h4>
<p>My example is python code that calculates and plots the frequency response of a moving average filter. Here is the code in the Sweave document <a href="http://files.mpastell.com/ma.Rnw">ma.Rnw</a>. Process the file in R using the custom driver above:</p>
<div class="highlight"><pre>Sweave<span class="p">(</span> <span class="s">"ma.Rnw"</span><span class="p">,</span> driver <span class="o">=</span> driver<span class="p">)</span>
</pre></div>
<p>It should produce <a href="http://files.mpastell.com/ma.tex">ma.tex</a>. Then run latex (make sure you have the python package installed):</p>
<div class="highlight"><pre>pdflatex -shell-escape ma.tex
</pre></div>
<p>Which should then in turn output this <a href="http://files.mpastell.com/sweave/ma.pdf">ma.pdf</a> file.</p>
tag:mpastell.com,2010-01-18:/2010/01/18/fir-with-scipy/FIR filter design with Python and SciPy2010-01-17T22:00:00Z2010-01-17T22:00:00Z<div id="TOC"><h2>Table of contents</h2>
<ul>
<li><a href="#lowpass-fir-filter">Lowpass FIR filter</a></li>
<li><a href="#highpass-fir-filter">Highpass FIR Filter</a></li>
<li><a href="#bandpass-fir-filter">Bandpass FIR filter</a></li>
<li><a href="#functions-for-frequency-phase-impulse-and-step-response">Functions for frequency, phase, impulse and step response</a></li>
</ul>
</div>
<p>SciPy really has good capabilities for DSP, but the filter design functions lack good examples. A while back I wrote about <a href="http://mpastell.com/2009/11/05/iir-filter-design-with-python-and-scipy/">IIR filter design with SciPy</a>. Today I'm going to implement lowpass, highpass and bandpass example for FIR filters. We use the same functions (mfreqz and impz, shown in the end of this post as well) as in the previous post to get the frequency, phase, impulse and step responses. You can download this example along with the functions here <a href="http://files.mpastell.com/FIR_design.py">FIR_design.py</a>. To begin with we'll import pylab and scipy.signal:</p>
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">pylab</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">import</span> <span class="nn">scipy.signal</span> <span class="kn">as</span> <span class="nn">signal</span>
</pre></div>
<h4 id="lowpass-fir-filter"><a href="#TOC">Lowpass FIR filter</a></h4>
<p>Designing a lowpass FIR filter is very simple to do with SciPy, all you need to do is to define the window length, cut off frequency and the window:</p>
<div class="highlight"><pre><span class="n">n</span> <span class="o">=</span> <span class="mi">61</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">firwin</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">cutoff</span> <span class="o">=</span> <span class="mf">0.3</span><span class="p">,</span> <span class="n">window</span> <span class="o">=</span> <span class="s">"hamming"</span><span class="p">)</span>
<span class="c">#Frequency and phase response</span>
<span class="n">mfreqz</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
<span class="c">#Impulse and step response</span>
<span class="n">figure</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="n">impz</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</pre></div>
<p>Which yields:</p>
<p><img title="" src="http://mpastell.com/images/FIR_lowpass.png" alt="image" /></p>
<p><img title="" src="http://mpastell.com/images/FIR_lowpass_impz.png" alt="image" /></p>
<h4 id="highpass-fir-filter"><a href="#TOC">Highpass FIR Filter</a></h4>
<p>SciPy does not have a function for directly designing a highpass FIR filter, however it is fairly easy design a lowpass filter and use <em>spectral inversion</em> to convert it to highpass. See e.g <a href="http://www.dspguide.com/ch16.htm">Chp 16 of The Scientist and Engineer's Guide to Digital Signal Processing</a> for the theory, the last page has an example code.</p>
<div class="highlight"><pre><span class="n">n</span> <span class="o">=</span> <span class="mi">101</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">firwin</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">cutoff</span> <span class="o">=</span> <span class="mf">0.3</span><span class="p">,</span> <span class="n">window</span> <span class="o">=</span> <span class="s">"hanning"</span><span class="p">)</span>
<span class="c">#Spectral inversion</span>
<span class="n">a</span> <span class="o">=</span> <span class="o">-</span><span class="n">a</span>
<span class="n">a</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
<span class="n">mfreqz</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</pre></div>
<p><img title="" src="http://mpastell.com/images/FIR_highpass.png" alt="image" /></p>
<h4 id="bandpass-fir-filter"><a href="#TOC">Bandpass FIR filter</a></h4>
<p>To get a bandpass FIR filter with SciPy we first need to design appropriate lowpass and highpass filters and then combine them:</p>
<div class="highlight"><pre><span class="n">n</span> <span class="o">=</span> <span class="mi">1001</span>
<span class="c">#Lowpass filter</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">firwin</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">cutoff</span> <span class="o">=</span> <span class="mf">0.3</span><span class="p">,</span> <span class="n">window</span> <span class="o">=</span> <span class="s">'blackmanharris'</span><span class="p">)</span>
<span class="c">#Highpass filter with spectral inversion</span>
<span class="n">b</span> <span class="o">=</span> <span class="o">-</span> <span class="n">signal</span><span class="o">.</span><span class="n">firwin</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">cutoff</span> <span class="o">=</span> <span class="mf">0.5</span><span class="p">,</span> <span class="n">window</span> <span class="o">=</span> <span class="s">'blackmanharris'</span><span class="p">);</span> <span class="n">b</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">b</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
<span class="c">#Combine into a bandpass filter</span>
<span class="n">d</span> <span class="o">=</span> <span class="o">-</span> <span class="p">(</span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="p">);</span> <span class="n">d</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">d</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
<span class="c">#Frequency response</span>
<span class="n">mfreqz</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="n">show</span><span class="p">()</span>
</pre></div>
<p><img title="" src="http://mpastell.com/images/FIR_bandpass.png" alt="image" /></p>
<h4 id="functions-for-frequency-phase-impulse-and-step-response"><a href="#TOC">Functions for frequency, phase, impulse and step response</a></h4>
<div class="highlight"><pre><span class="c">#Plot frequency and phase response</span>
<span class="k">def</span> <span class="nf">mfreqz</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="n">w</span><span class="p">,</span><span class="n">h</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">freqz</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">a</span><span class="p">)</span>
<span class="n">h_dB</span> <span class="o">=</span> <span class="mi">20</span> <span class="o">*</span> <span class="n">log10</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">h</span><span class="p">))</span>
<span class="n">subplot</span><span class="p">(</span><span class="mi">211</span><span class="p">)</span>
<span class="n">plot</span><span class="p">(</span><span class="n">w</span><span class="o">/</span><span class="nb">max</span><span class="p">(</span><span class="n">w</span><span class="p">),</span><span class="n">h_dB</span><span class="p">)</span>
<span class="n">ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">150</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="n">ylabel</span><span class="p">(</span><span class="s">'Magnitude (db)'</span><span class="p">)</span>
<span class="n">xlabel</span><span class="p">(</span><span class="s">r'Normalized Frequency (x$\pi$rad/sample)'</span><span class="p">)</span>
<span class="n">title</span><span class="p">(</span><span class="s">r'Frequency response'</span><span class="p">)</span>
<span class="n">subplot</span><span class="p">(</span><span class="mi">212</span><span class="p">)</span>
<span class="n">h_Phase</span> <span class="o">=</span> <span class="n">unwrap</span><span class="p">(</span><span class="n">arctan2</span><span class="p">(</span><span class="n">imag</span><span class="p">(</span><span class="n">h</span><span class="p">),</span><span class="n">real</span><span class="p">(</span><span class="n">h</span><span class="p">)))</span>
<span class="n">plot</span><span class="p">(</span><span class="n">w</span><span class="o">/</span><span class="nb">max</span><span class="p">(</span><span class="n">w</span><span class="p">),</span><span class="n">h_Phase</span><span class="p">)</span>
<span class="n">ylabel</span><span class="p">(</span><span class="s">'Phase (radians)'</span><span class="p">)</span>
<span class="n">xlabel</span><span class="p">(</span><span class="s">r'Normalized Frequency (x$\pi$rad/sample)'</span><span class="p">)</span>
<span class="n">title</span><span class="p">(</span><span class="s">r'Phase response'</span><span class="p">)</span>
<span class="n">subplots_adjust</span><span class="p">(</span><span class="n">hspace</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
<span class="c">#Plot step and impulse response</span>
<span class="k">def</span> <span class="nf">impz</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="n">impulse</span> <span class="o">=</span> <span class="n">repeat</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span><span class="n">l</span><span class="p">);</span> <span class="n">impulse</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span><span class="mf">1.</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">l</span><span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">signal</span><span class="o">.</span><span class="n">lfilter</span><span class="p">(</span><span class="n">b</span><span class="p">,</span><span class="n">a</span><span class="p">,</span><span class="n">impulse</span><span class="p">)</span>
<span class="n">subplot</span><span class="p">(</span><span class="mi">211</span><span class="p">)</span>
<span class="n">stem</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">response</span><span class="p">)</span>
<span class="n">ylabel</span><span class="p">(</span><span class="s">'Amplitude'</span><span class="p">)</span>
<span class="n">xlabel</span><span class="p">(</span><span class="s">r'n (samples)'</span><span class="p">)</span>
<span class="n">title</span><span class="p">(</span><span class="s">r'Impulse response'</span><span class="p">)</span>
<span class="n">subplot</span><span class="p">(</span><span class="mi">212</span><span class="p">)</span>
<span class="n">step</span> <span class="o">=</span> <span class="n">cumsum</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
<span class="n">stem</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">step</span><span class="p">)</span>
<span class="n">ylabel</span><span class="p">(</span><span class="s">'Amplitude'</span><span class="p">)</span>
<span class="n">xlabel</span><span class="p">(</span><span class="s">r'n (samples)'</span><span class="p">)</span>
<span class="n">title</span><span class="p">(</span><span class="s">r'Step response'</span><span class="p">)</span>
<span class="n">subplots_adjust</span><span class="p">(</span><span class="n">hspace</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
</pre></div>
tag:mpastell.com,2009-11-25:/2009/11/25/ess-on-mac-os-x/ESS on Mac OS X2009-11-24T22:00:00Z2009-11-24T22:00:00Z<p>One of the search terms that bring people frequently to my site is "install ESS on Mac OS X" or something like that. As it turns out installing <a href="http://ess.r-project.org/">ESS</a> on OS X is really easy, but Google search does not really bring up good instructions. There are at least two easy options:</p>
<ol style="list-style-type: decimal">
<li>Use <a href="http://aquamacs.org/">Aquamacs</a>, it comes bundled with ESS</li>
<li>Use <a href="http://homepage.mac.com/zenitani/emacs-e.html">Carbon Emacs</a>, it also comes with ESS, but you need to add the line (require 'ess-site) to your .emacs file in your home directory in order to enable it.</li>
</ol>
<p><strong>That's it!</strong></p>