<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Posts on Ay up! . . . </title>
        <link>https://www.guxsousa.com/posts/</link>
        <description>Recent content in Posts on Ay up! . . . </description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-gb</language>
        <copyright>&lt;a href=&#34;https://creativecommons.org/licenses/by-nc/4.0/&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;CC BY-NC 4.0&lt;/a&gt;</copyright>
        <lastBuildDate>Tue, 30 Apr 2019 11:55:26 +0100</lastBuildDate>
        <atom:link href="https://www.guxsousa.com/posts/index.xml" rel="self" type="application/rss+xml" />
        
        <item>
            <title>Start off a tmux environment</title>
            <link>https://www.guxsousa.com/posts/2019/04/start-off-a-tmux-environment/</link>
            <pubDate>Tue, 30 Apr 2019 11:55:26 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2019/04/start-off-a-tmux-environment/</guid>
            <description>The following commands create a tmux environment with specific windows and panes.
By way of demonstration, my environment will create a window for music, and a second window, split in two panes, for podcasts. I use cmus for music, and the command line version of vlc for podcasts.
Description First, check if the session exists already
tmux has-session -t Audiophile If not, then execute something; otherwise exit and (probably) attach the session.</description>
            <content type="html"><![CDATA[

<p>The following commands create a <code>tmux</code> environment with specific windows and panes.</p>

<p><img src="/img/tmxenv.png" alt="tmxenvwindow" /></p>

<p>By way of demonstration, my environment will create a window for <em>music</em>, and a second window, split in two panes, for <em>podcasts</em>. I use <code>cmus</code> for music, and the command line version of <code>vlc</code> for podcasts.</p>

<h4 id="description">Description</h4>

<p>First, check if the session exists already</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">tmux has-session -t Audiophile</code></pre></div>
<p>If not, then execute something; otherwise exit and (probably) attach the session.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#66d9ef">if</span> <span style="color:#f92672">[</span> $? !<span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">]</span>
<span style="color:#66d9ef">then</span>
 &lt;execute something&gt;
<span style="color:#66d9ef">fi</span></code></pre></div>
<p>The first command creates a new session called &lsquo;Audiophile&rsquo;, and immediately names the only existing window as &lsquo;Music&rsquo;.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"> tmux new-session -s Audiophile -n Music -d</code></pre></div>
<p>In the same session/window (since there is only one), call <code>cmus</code>, and divide the screen. The size of the split is defined to 70 lines. Once divided, return the selection to the main screen.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"> tmux send-keys -t Audiophile <span style="color:#e6db74">&#39;cmus&#39;</span> C-m
 tmux split-window -v -p <span style="color:#ae81ff">70</span> -t Audiophile
 tmux <span style="color:#66d9ef">select</span>-layout -t Audiophile main-vertical</code></pre></div>
<p>Without moving the selection of pane, go to a specific folder in the smaller one.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"> tmux send-keys -t Audiophile:1.2 <span style="color:#e6db74">&#39;cd &lt;some-folder&gt;; clear&#39;</span> C-m</code></pre></div>
<p>Now create a new window named &lsquo;Podcasts&rsquo;.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"> tmux new-window -n Podcasts -t Audiophile</code></pre></div>
<p>Likewise the first window, create a second pane, go the podcast folder, and display its content. In the main pane, call <code>podui</code>, which is an alias for the command line version of <code>vlc</code>.</p>

<blockquote>
<p>podui=&lsquo;/Applications/VLC.app/Contents/MacOS/VLC &ndash;intf ncurses .&rsquo;</p>
</blockquote>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">tmux send-keys -t Audiophile:2 <span style="color:#e6db74">&#39;cd &lt;pod-folder&gt;; clear; ls -lh&#39;</span> C-m
tmux split-window -v -t Audiophile:2
tmux <span style="color:#66d9ef">select</span>-layout -t Audiophile:2 main-horizontal
tmux send-keys -t Audiophile:2.2 <span style="color:#e6db74">&#39;podui&#39;</span> C-m</code></pre></div>
<p>Once the second window is made, return to the first window and attach to the session. This will exit the script.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">tmux <span style="color:#66d9ef">select</span>-window -t Audiophile:1
tmux attach -t Audiophile</code></pre></div>
<p>As aforementioned, the <code>else</code> section of the condition will immediately attach to the session.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">tmux attach -t Audiophile</code></pre></div>
<hr />

<h4 id="script">Script</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">tmux has-session -t Audiophile
<span style="color:#66d9ef">if</span> <span style="color:#f92672">[</span> $? !<span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span> <span style="color:#f92672">]</span>
<span style="color:#66d9ef">then</span>
 tmux new-session -s Audiophile -n Music -d
 tmux send-keys -t Audiophile <span style="color:#e6db74">&#39;cmus&#39;</span> C-m
 tmux split-window -v -p <span style="color:#ae81ff">70</span> -t Audiophile
 tmux <span style="color:#66d9ef">select</span>-layout -t Audiophile main-vertical
 tmux send-keys -t Audiophile:1.2 <span style="color:#e6db74">&#39;cd &lt;some-folder&gt;; clear&#39;</span> C-m
 tmux new-window -n Podcasts -t Audiophile
 tmux send-keys -t Audiophile:2 <span style="color:#e6db74">&#39;cd &lt;pod-folder&gt;; clear; ls -lh&#39;</span> C-m
 tmux split-window -v -t Audiophile:2
 tmux <span style="color:#66d9ef">select</span>-layout -t Audiophile:2 main-horizontal
 tmux send-keys -t Audiophile:2.2 <span style="color:#e6db74">&#39;podui&#39;</span> C-m
 tmux <span style="color:#66d9ef">select</span>-window -t Audiophile:1
 tmux attach -t Audiophile
<span style="color:#66d9ef">fi</span>
tmux attach -t Audiophile</code></pre></div>
<hr />

<p>The script might be called from an <em>alias</em> in a new terminal session (outside tmux). For example:</p>

<blockquote>
<p>$: tmxenv</p>
</blockquote>

<p>where,</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"> tmxenv<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;sh &lt;folder-with-script&gt;/tmux_env_maker&#39;</span>    </code></pre></div>]]></content>
        </item>
        
        <item>
            <title>My First Post using Hugo</title>
            <link>https://www.guxsousa.com/posts/2019/04/my-first-post-using-hugo/</link>
            <pubDate>Sat, 27 Apr 2019 14:22:46 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2019/04/my-first-post-using-hugo/</guid>
            <description>Welcome to my new blog.
alert(&amp;#39;Hello, world!&amp;#39;); </description>
            <content type="html"><![CDATA[<p>Welcome to my new blog.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js"><span style="color:#a6e22e">alert</span>(<span style="color:#e6db74">&#39;Hello, world!&#39;</span>);
</code></pre></div>]]></content>
        </item>
        
        <item>
            <title>Filtered bibliography: Mendeley &#43; Biblatex</title>
            <link>https://www.guxsousa.com/posts/2018/05/filtered-bibliography-mendeley-biblatex/</link>
            <pubDate>Sat, 05 May 2018 11:55:26 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2018/05/filtered-bibliography-mendeley-biblatex/</guid>
            <description>This template filters a bibliography&amp;mdash;in this case generated in Mendeley&amp;mdash;and displays title, key, citation, notes and abstract. This is quite convenient for sharing latex source files with other authors, and for drafting a document.
+ setup The template uses the class article and a pretty basic library. A double-side document is defined in the geometry library, but can be edited easily. The biblatex library is the important one here (see the library documentation).</description>
            <content type="html"><![CDATA[

<p>This template filters a bibliography&mdash;in this case generated in <strong>Mendeley</strong>&mdash;and displays <em>title, key, citation, notes and abstract</em>. This is quite convenient for sharing <code>latex</code> source files with other authors, and for drafting a document.</p>

<p><img src="/img/texfilter.png" alt="texfiltered" /></p>

<h4 id="setup">+ setup</h4>

<p>The template uses the class <em>article</em> and a pretty basic library. A double-side document is defined in the <em>geometry</em> library, but can be edited easily. The <em>biblatex</em> library is the important one here (see the library <a href="https://cran.r-project.org/web/packages/bibtex/index.html" target="_blank">documentation</a>). Here the parameters to be displayed are selected, as well as some pre-formatting commands. For instance, the <em>style</em> may be defined as <em>numeric, reading, alphabetic or authoryear</em>; likewise, the sorting method may be defined as <em>anyt, ynt, none or ydnt</em>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-latex" data-lang="latex"><span style="color:#66d9ef">\documentclass</span><span style="color:#a6e22e">[a4paper,9pt,twoside]</span>{article}

<span style="color:#66d9ef">\usepackage</span><span style="color:#a6e22e">[utf8]</span>{inputenc}
<span style="color:#66d9ef">\usepackage</span><span style="color:#a6e22e">[T1]</span>{fontenc}
<span style="color:#66d9ef">\usepackage</span>{csquotes}
<span style="color:#66d9ef">\usepackage</span>{hyperref}
<span style="color:#66d9ef">\usepackage</span>{multicol}
<span style="color:#66d9ef">\usepackage</span>[top=1.6cm,
            bottom=2cm,
            outer=0.9cm,
            inner=1.8cm,
            headsep=14pt]{geometry}
<span style="color:#66d9ef">\usepackage</span>[backend=bibtex,
            style=reading,
            entryhead=full,
            entrykey=true,
            annotation=true,
            abstract = true,
            library=true,
            file=false,
            doi=false,
            eprint=false,
            url=false,
            sorting=ynt]{biblatex}</code></pre></div>
<p>In addition, some metadata is defined in the header to be then later displayed in the document (i.e. title, author, date, &hellip;). The bibliography file: <code>./library.bib</code> is also referenced in the header.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-latex" data-lang="latex"><span style="color:#66d9ef">\title</span>{myBibliography: <span style="color:#66d9ef">\texttt</span>{Biblatex} <span style="color:#66d9ef">\&amp;</span> <span style="color:#66d9ef">\texttt</span>{Mendeley} - <span style="color:#66d9ef">\LaTeX</span>}
<span style="color:#66d9ef">\author</span>{author name}
<span style="color:#66d9ef">\date</span>{any date}

<span style="color:#66d9ef">\bibliography</span>{library}</code></pre></div>
<h4 id="mendeley-hints">+ mendeley (&hellip;hints)</h4>

<blockquote>
<p>Mendeley (Elsevier) is a free reference management application and an academic social network tool&hellip; Mendeley helps to manage references and generate bibliographies.</p>
</blockquote>

<p>One of the main features of <strong>Mendeley</strong> is its multi-platform capability. A second feature shoudl be its ability to customise the stored references. In this case, the value assigned to <em>tags</em> or personal <em>keywords</em> can be employed to filter bibliographies. These values can add another layer of categorisation, either in the <code>.bib</code> file or firectly in the <code>.tex</code> file.</p>

<p>In case <em>tags</em> are used, these need to be converted in the generated <code>mendeley-tag</code> value into keywords or other fields that can be recognised by <code>biblatex</code>.
Otherwise, <code>biblatex</code> will include <em>keywords</em> values into the <em>mendeley-tags</em>.
The following defintion can be used to manage these values during the <code>latex</code> compilation; it converts &lsquo;mendeley-tags&rsquo; to &lsquo;keywords&rsquo;.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-latex" data-lang="latex"><span style="color:#66d9ef">\DeclareSourcemap</span>{
  <span style="color:#66d9ef">\maps</span><span style="color:#a6e22e">[datatype=bibtex]</span>{
    <span style="color:#66d9ef">\map</span><span style="color:#a6e22e">[overwrite=false]</span>{
       <span style="color:#66d9ef">\step</span><span style="color:#a6e22e">[fieldsource=mendeley-tags, fieldtarget=keywords, append]</span>
       <span style="color:#66d9ef">\step</span><span style="color:#a6e22e">[fieldset=keywords, fieldvalue=freud]</span>
    }
  }
}</code></pre></div>
<h4 id="template">+ template</h4>

<p>Finally, the <code>tex</code> file can be structured as usual, with sections and subsections. The result: a filtered bibliography with annotations.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-latex" data-lang="latex"><span style="color:#66d9ef">\begin</span>{document}

<span style="color:#66d9ef">\maketitle</span>

<span style="color:#66d9ef">\medskip</span>
<span style="color:#66d9ef">\tableofcontents</span>
<span style="color:#66d9ef">\nocite</span>{*}
<span style="color:#66d9ef">\medskip</span>


<span style="color:#66d9ef">\section</span>{Themes / Keywords}

<span style="color:#66d9ef">\begin</span>{multicols}{3}

<span style="color:#66d9ef">\subsection</span>{Cognition}
<span style="color:#66d9ef">\printbibliography</span><span style="color:#a6e22e">[keyword={Cognition},heading=none]</span>

<span style="color:#66d9ef">\subsection</span>{Literature}
<span style="color:#66d9ef">\printbibliography</span><span style="color:#a6e22e">[keyword={Literature},heading=none]</span>

<span style="color:#66d9ef">\subsection</span>{Methods}
<span style="color:#66d9ef">\printbibliography</span><span style="color:#a6e22e">[keyword={Methods},heading=none]</span>

<span style="color:#66d9ef">\subsection</span>{Latex}
<span style="color:#66d9ef">\printbibliography</span><span style="color:#a6e22e">[keyword={Latex},heading=none]</span>

<span style="color:#66d9ef">\end</span>{multicols}


<span style="color:#66d9ef">\clearpage</span>

<span style="color:#66d9ef">\section</span>{Type of Work}

<span style="color:#66d9ef">\subsection</span>{Articles}
<span style="color:#66d9ef">\printbibliography</span><span style="color:#a6e22e">[type=article, title={Articles only}]</span>

<span style="color:#66d9ef">\subsection</span>{Theses}
<span style="color:#66d9ef">\printbibliography</span><span style="color:#a6e22e">[type=thesis, title={Thesis only}]</span>

<span style="color:#66d9ef">\end</span>{document}</code></pre></div>
<p>I hope this has been useful.</p>

<hr />

<p></br></br></br></p>

<h3 id="source-file">Source file</h3>

<p>The source file has been included as a template in <a href="https://www.overleaf.com/latex/examples/biblatex-plus-mendeley/vvpqnkyhrync" target="_blank">Overleaf</a></p>

<p><img src="https://images.ctfassets.net/nrgyaltdicpt/6DEXmiP0xGqamuwaKc0woS/9c21c4f5312c6870292c471ad3ecaa5e/overleaf_wide_colour_light_bg.svg" alt="overleaf" /></p>
]]></content>
        </item>
        
        <item>
            <title>How to make a tree?</title>
            <link>https://www.guxsousa.com/posts/2018/02/how-to-make-a-tree/</link>
            <pubDate>Sat, 24 Feb 2018 11:55:26 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2018/02/how-to-make-a-tree/</guid>
            <description>The following script displays a useful tree structure of a directory; it&amp;rsquo;s similar the old DOS-command: tree.
find . -type d | sed -e &amp;#34;s/[^-][^\/]*\// |/g&amp;#34; -e &amp;#34;s/|\([^ ]\)/|-\1/&amp;#34; | tee output_.txt  As noted, three parts are combined:
 find : This command searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.</description>
            <content type="html"><![CDATA[

<p>The following script displays a useful tree structure of a directory; it&rsquo;s similar the old DOS-command: <code>tree</code>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">  find . -type d | sed -e <span style="color:#e6db74">&#34;s/[^-][^\/]*\//  |/g&#34;</span> -e <span style="color:#e6db74">&#34;s/|\([^ ]\)/|-\1/&#34;</span> | tee output_.txt       </code></pre></div>
<p>As noted, three parts are combined:</p>

<ol>
<li><p><code>find</code> :
This command searches the directory tree rooted at each given file name by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.</p></li>

<li><p><code>sed</code>  :
(s)tream (ed)itor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed&rsquo;s ability to filter text in a pipeline which particularly distinguishes it from other types of editors. See ‘sed &ndash;help’ for usage information.</p></li>

<li><p><code>tee</code>  :
The command reads <em>standard input</em>, then writes its content to <em>standard output</em>. It simultaneously copies the result into the specified file(s) or variables.</p></li>
</ol>

<hr />

<h3 id="example">Example:</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">  cd myDirectory/                                                                                  
  find . -type d | sed -e <span style="color:#e6db74">&#34;s/[^-][^\/]*\//  |/g&#34;</span> -e <span style="color:#e6db74">&#34;s/|\([^ ]\)/|-\1/&#34;</span> | tee output_.txt       </code></pre></div>
<blockquote>
<pre><code>|-B.B. King
|  |-Kansas City 1972
|-Bob Forrest
|  |-Modern folk and blues
|-Eric Clapton
|  |-MTV Unplugged
|  |-Old Sock
|  |-On the crossroad
|  |-Wynton Marsalis &amp; Eric Clapton Play The Blues
|-Eric Gales Band
|  |-The Psychedelic Underground
|-Eric Johnson
|  |-Acoustic Bootleg
|  |-Venus Isle
|-J.J. Cale &amp; Eric Clapton
|  |-The Road to Escondido
|-James Brown
|  |-At Studio 54
|-Jeff Beck
|  |-Emotion And Commotion
|-Jimi Hendrix
|  |-Are You Experienced
|  |-Axis Bold as Love
|  |-Band of Gypsys
|  |-BBC Sessions
|  |-Electric Ladyland
|  |-Experience Hendrix- The Best of Jimi Hendrix
|  |-The Early Years
|-Jimi Hendrix &amp; Stephen Stills
|  |-At Stills Basement
|-John Mayall
|  |-Blues From Laurel Canyon
|  |-Life In The Jungle
|-Johnny Cash
|  |-American Recordings
|  |-Unchained
|-Muddy Waters
|  |-They Call Me Muddy Waters
|-Robert Johnson
|  |-Genius Of The Blues
|-Steve Ray Vaughan
|  |-albert king &amp; steve ray vaughan
|  |-Live at Pier
|  |-texas flood
|-Stevie Ray Vaughan &amp; Jeff Beck
|  |-The Fire Meets The Fury
|-The Yardbirds
|  |-Blue Eyed Blues
</code></pre>
</blockquote>

<p>Including files?</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">  find . -print | sed -e <span style="color:#e6db74">&#39;s;[^/]*/;|____;g;s;____|; |;g&#39;</span> | tee output_.txt       </code></pre></div>]]></content>
        </item>
        
        <item>
            <title>Climate map using leaflet</title>
            <link>https://www.guxsousa.com/posts/2018/02/climate-map-using-leaflet/</link>
            <pubDate>Sat, 10 Feb 2018 11:55:26 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2018/02/climate-map-using-leaflet/</guid>
            <description>The following tutorial explains how to generate an interactive map employing the R-library: leaflet.
library(leaflet) library(geojsonio) # required to read json file library(leaflet.extras) # simplifies addition of features The following example displays the Köppen-Geiger climate classification map1, which categorises world regions based on observed temperature and precipitation conditions. In the study1, a set of underlying data regarding both observed and projected climates shifts is made freely available2. This example only utilises the observed conditions for four different periods: 1900-1925, 1926-1950, 1951-1975, 1976-2000.</description>
            <content type="html"><![CDATA[

<p><img src="/img/MapLeafa.png" alt="KG region" /></p>

<p>The following tutorial explains how to generate an <strong>interactive map</strong> employing the <code>R</code>-library: <code>leaflet</code>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-r" data-lang="r"><span style="color:#f92672">library</span>(leaflet)
<span style="color:#f92672">library</span>(geojsonio)      <span style="color:#75715e"># required to read json file</span>
<span style="color:#f92672">library</span>(leaflet.extras) <span style="color:#75715e"># simplifies addition of features</span></code></pre></div>
<p>The following example displays the <strong>Köppen-Geiger climate classification map</strong><sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup>, which categorises world regions based on observed temperature and precipitation conditions. In the study<sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup>, a set of underlying data regarding both observed and projected climates shifts is made freely available<sup class="footnote-ref" id="fnref:2"><a href="#fn:2">2</a></sup>. This example only utilises the observed conditions for four different periods: 1900-1925, 1926-1950, 1951-1975, 1976-2000. Each of these files with observed conditions contains lines of grid box centre coordinates (latitude, longitude), with their corresponding climate class.</p>

<p>To simplify the processing of information, the example employs a set of processed <code>topo-json</code> files <sup class="footnote-ref" id="fnref:3"><a href="#fn:3">3</a></sup>, as well as a <code>json</code> file outlining the countries of the world <sup class="footnote-ref" id="fnref:4"><a href="#fn:4">4</a></sup>.</p>

<h2 id="br"><br></h2>

<h3 id="hands-on">Hands-on</h3>

<p>First, a table of colours is loaded, whose values are those defined in the aforementioned study <sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-r" data-lang="r">tblColors <span style="color:#f92672">&lt;-</span> read.table(<span style="color:#e6db74">&#34;myData/koeppen/koeppen_lgd.txt&#34;</span>, header <span style="color:#f92672">=</span> <span style="color:#66d9ef">TRUE</span>, stringsAsFactors <span style="color:#f92672">=</span> F)</code></pre></div>
<p>Now, these datasets contain the <em>class</em> for each polygon/square, but do not contain the preferred colour. This can be achieved in two ways: 1) by comparing the class with the table of colours when the <code>leaflet</code> object is called, or 2) by assigning the colour to the <em>class</em> in the dataset. The later is expectedly faster, and the process is developed in the function <code>fnAddFeatures</code>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-r" data-lang="r">fnAddFeatures <span style="color:#f92672">&lt;-</span> <span style="color:#66d9ef">function</span>(dtaJson, dtaPal<span style="color:#f92672">=</span>tblColors){

  dtaJson<span style="color:#f92672">$</span>style <span style="color:#f92672">=</span> <span style="color:#66d9ef">list</span>(
    weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>,
    color <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;#555555&#34;</span>,
    opacity <span style="color:#f92672">=</span> <span style="color:#ae81ff">1</span>,
    fillOpacity <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.8</span>
  )

  geometries <span style="color:#f92672">&lt;-</span> <span style="color:#66d9ef">sapply</span>(dtaJson<span style="color:#f92672">$</span>features, <span style="color:#66d9ef">function</span>(feat) {
    feat<span style="color:#f92672">$</span>properties<span style="color:#f92672">$</span>gridcode}
  )

  dtaJson<span style="color:#f92672">$</span>features <span style="color:#f92672">&lt;-</span> <span style="color:#66d9ef">lapply</span>(dtaJson<span style="color:#f92672">$</span>features, <span style="color:#66d9ef">function</span>(feat){
    feat<span style="color:#f92672">$</span>properties<span style="color:#f92672">$</span>style <span style="color:#f92672">&lt;-</span> <span style="color:#66d9ef">list</span>(
      fillColor <span style="color:#f92672">=</span> dtaPal<span style="color:#f92672">$</span><span style="color:#66d9ef">col</span>[dtaPal<span style="color:#f92672">$</span>class <span style="color:#f92672">%in%</span> feat<span style="color:#f92672">$</span>properties<span style="color:#f92672">$</span>gridcode]
    )
    feat

  })

  <span style="color:#66d9ef">return</span>(dtaJson)
}</code></pre></div>
<p>The function <code>fnAddFeatures</code> adds a general <em>style</em> attribute. It then extracts the <em>gridcode</em> class, and stores it in the object <code>geometries</code>. Finally, a new feature called <em>style</em> compares the <em>gridcode</em> with the table of colours for each element of the list&mdash;therefore the use of <code>lapply</code>.</p>

<p>Now the four datasets are loaded, and the <em>style</em> feature is added.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-r" data-lang="r">Clim1900 <span style="color:#f92672">&lt;-</span> fnAddFeatures(geojson_read(<span style="color:#66d9ef">paste</span>(path.Project,<span style="color:#e6db74">&#34;myData/ClimateMap/climate-map-master/1901-1925.topo.json&#34;</span>,sep<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;/&#34;</span>)))
Clim1926 <span style="color:#f92672">&lt;-</span> fnAddFeatures(geojson_read(<span style="color:#66d9ef">paste</span>(path.Project,<span style="color:#e6db74">&#34;myData/ClimateMap/climate-map-master/1926-1950.topo.json&#34;</span>,sep<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;/&#34;</span>)))
Clim1951 <span style="color:#f92672">&lt;-</span> fnAddFeatures(geojson_read(<span style="color:#66d9ef">paste</span>(path.Project,<span style="color:#e6db74">&#34;myData/ClimateMap/climate-map-master/1951-1975.topo.json&#34;</span>,sep<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;/&#34;</span>)))
Clim1976 <span style="color:#f92672">&lt;-</span> fnAddFeatures(geojson_read(<span style="color:#66d9ef">paste</span>(path.Project,<span style="color:#e6db74">&#34;myData/ClimateMap/climate-map-master/1976-2000.topo.json&#34;</span>,sep<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;/&#34;</span>)))</code></pre></div>
<h2 id="br-1"><br></h2>

<h3 id="the-map">The map</h3>

<p>A random location is defined for the <em>setview</em>, and the extent to which the map can be zoomed-out is defined.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-r" data-lang="r">setView(lng <span style="color:#f92672">=</span> <span style="color:#ae81ff">-105</span>, lat <span style="color:#f92672">=</span> <span style="color:#ae81ff">23</span>, zoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">3</span>) <span style="color:#f92672">%&gt;%</span>

setMaxBounds(lng1 <span style="color:#f92672">=</span> <span style="color:#ae81ff">-178.5241</span>, lat1 <span style="color:#f92672">=</span> <span style="color:#ae81ff">-72.05115</span>,
             lng2 <span style="color:#f92672">=</span> <span style="color:#ae81ff">178.52414</span>, lat2 <span style="color:#f92672">=</span> <span style="color:#ae81ff">65.05115</span> ) <span style="color:#f92672">%&gt;%</span></code></pre></div>
<p>Two base maps are defined here: light and dark, which can be selected in the control panel.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-r" data-lang="r">addProviderTiles(<span style="color:#e6db74">&#34;CartoDB.DarkMatterNoLabels&#34;</span>,
                 options <span style="color:#f92672">=</span> providerTileOptions(minZoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>, maxZoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">7</span>),
                 group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Dark&#34;</span>) <span style="color:#f92672">%&gt;%</span>
addProviderTiles(<span style="color:#e6db74">&#34;OpenStreetMap.BlackAndWhite&#34;</span>,
                 options <span style="color:#f92672">=</span> providerTileOptions(minZoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>, maxZoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">7</span>, noWrap <span style="color:#f92672">=</span> <span style="color:#66d9ef">TRUE</span>),
                 group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Light&#34;</span>) <span style="color:#f92672">%&gt;%</span>
addLayersControl(baseGroups <span style="color:#f92672">=</span> <span style="color:#66d9ef">c</span>(<span style="color:#e6db74">&#34;Dark&#34;</span>, <span style="color:#e6db74">&#34;Light&#34;</span>),
                 options <span style="color:#f92672">=</span> layersControlOptions(collapsed <span style="color:#f92672">=</span> <span style="color:#66d9ef">FALSE</span>))</code></pre></div>
<p>Each dataset is aded to a <code>addGeoJSON</code> object, and a group is assigned to be included in the control menu/legend.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-r" data-lang="r">addGeoJSON(Clim1900, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.4</span>, group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;1900&#34;</span>) <span style="color:#f92672">%&gt;%</span> hideGroup(<span style="color:#e6db74">&#34;1900&#34;</span>) <span style="color:#f92672">%&gt;%</span>
addGeoJSON(Clim1926, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.4</span>, group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;1926&#34;</span>) <span style="color:#f92672">%&gt;%</span> hideGroup(<span style="color:#e6db74">&#34;1926&#34;</span>) <span style="color:#f92672">%&gt;%</span>
addGeoJSON(Clim1951, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.4</span>, group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;1951&#34;</span>) <span style="color:#f92672">%&gt;%</span> hideGroup(<span style="color:#e6db74">&#34;1951&#34;</span>) <span style="color:#f92672">%&gt;%</span>
addGeoJSON(Clim1976, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.4</span>, group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;1976&#34;</span>) <span style="color:#f92672">%&gt;%</span> hideGroup(<span style="color:#e6db74">&#34;1976&#34;</span>) <span style="color:#f92672">%&gt;%</span>
addGeoJSONv2(World, color <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;white&#34;</span>, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.6</span>, popupProperty <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;popup&#34;</span>, labelProperty <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;ADMIN&#34;</span>) <span style="color:#f92672">%&gt;%</span>

addLegend(position <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;bottomleft&#34;</span>, colors <span style="color:#f92672">=</span> tblColors<span style="color:#f92672">$</span><span style="color:#66d9ef">col</span>,
          labels <span style="color:#f92672">=</span> tblColors<span style="color:#f92672">$</span><span style="color:#66d9ef">class</span>, opacity <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.75</span>,
          labFormat <span style="color:#f92672">=</span> labelFormat(
            prefix <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;(&#34;</span>, suffix <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;)%&#34;</span>, between <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;, &#34;</span>),
          title <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;koeppen-geiger&#34;</span>) <span style="color:#f92672">%&gt;%</span></code></pre></div>
<p>The following action wraps up the definition of the <code>leaflet</code> object.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-r" data-lang="r">m <span style="color:#f92672">&lt;-</span> leaflet() <span style="color:#f92672">%&gt;%</span>
  setView(lng <span style="color:#f92672">=</span> <span style="color:#ae81ff">-105</span>, lat <span style="color:#f92672">=</span> <span style="color:#ae81ff">23</span>, zoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">3</span>) <span style="color:#f92672">%&gt;%</span>
  addProviderTiles(<span style="color:#e6db74">&#34;CartoDB.DarkMatterNoLabels&#34;</span>,
                   options <span style="color:#f92672">=</span> providerTileOptions(minZoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>, maxZoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">7</span>),
                   group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Dark&#34;</span>) <span style="color:#f92672">%&gt;%</span>
  addProviderTiles(<span style="color:#e6db74">&#34;OpenStreetMap.BlackAndWhite&#34;</span>,
                   options <span style="color:#f92672">=</span> providerTileOptions(minZoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>, maxZoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">7</span>, noWrap <span style="color:#f92672">=</span> <span style="color:#66d9ef">TRUE</span>),
                   group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;Light&#34;</span>) <span style="color:#f92672">%&gt;%</span>
  setMaxBounds( lng1 <span style="color:#f92672">=</span> <span style="color:#ae81ff">-178.5241</span>, lat1 <span style="color:#f92672">=</span> <span style="color:#ae81ff">-72.05115</span>,
                lng2 <span style="color:#f92672">=</span> <span style="color:#ae81ff">178.52414</span>, lat2 <span style="color:#f92672">=</span> <span style="color:#ae81ff">65.05115</span> ) <span style="color:#f92672">%&gt;%</span>
  addLegend(position <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;bottomleft&#34;</span>, colors <span style="color:#f92672">=</span> tblColors<span style="color:#f92672">$</span><span style="color:#66d9ef">col</span>,
            labels <span style="color:#f92672">=</span> tblColors<span style="color:#f92672">$</span><span style="color:#66d9ef">class</span>, opacity <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.75</span>,
            labFormat <span style="color:#f92672">=</span> labelFormat(
              prefix <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;(&#34;</span>, suffix <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;)%&#34;</span>, between <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;, &#34;</span>),
            title <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;koeppen-geiger&#34;</span>) <span style="color:#f92672">%&gt;%</span>
  addGeoJSON(Clim1900, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.4</span>, group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;1900&#34;</span>) <span style="color:#f92672">%&gt;%</span> hideGroup(<span style="color:#e6db74">&#34;1900&#34;</span>) <span style="color:#f92672">%&gt;%</span>
  addGeoJSON(Clim1926, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.4</span>, group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;1926&#34;</span>) <span style="color:#f92672">%&gt;%</span> hideGroup(<span style="color:#e6db74">&#34;1926&#34;</span>) <span style="color:#f92672">%&gt;%</span>
  addGeoJSON(Clim1951, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.4</span>, group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;1951&#34;</span>) <span style="color:#f92672">%&gt;%</span> hideGroup(<span style="color:#e6db74">&#34;1951&#34;</span>) <span style="color:#f92672">%&gt;%</span>
  addGeoJSON(Clim1976, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.4</span>, group <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;1976&#34;</span>) <span style="color:#f92672">%&gt;%</span> hideGroup(<span style="color:#e6db74">&#34;1976&#34;</span>) <span style="color:#f92672">%&gt;%</span>
  addGeoJSONv2(World, color <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;white&#34;</span>, weight <span style="color:#f92672">=</span> <span style="color:#ae81ff">0.6</span>, popupProperty <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;popup&#34;</span>, labelProperty <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;ADMIN&#34;</span>) <span style="color:#f92672">%&gt;%</span>
  addLayersControl(overlayGroups <span style="color:#f92672">=</span> <span style="color:#66d9ef">c</span>(<span style="color:#e6db74">&#34;1900&#34;</span>, <span style="color:#e6db74">&#34;1926&#34;</span>, <span style="color:#e6db74">&#34;1951&#34;</span>, <span style="color:#e6db74">&#34;1976&#34;</span>),
                   baseGroups <span style="color:#f92672">=</span> <span style="color:#66d9ef">c</span>(<span style="color:#e6db74">&#34;Dark&#34;</span>, <span style="color:#e6db74">&#34;Light&#34;</span>),
                   options <span style="color:#f92672">=</span> layersControlOptions(collapsed <span style="color:#f92672">=</span> <span style="color:#66d9ef">FALSE</span>))

m</code></pre></div>
<p><img src="/img/MapLeafb.png" alt="KG map" /></p>

<h2 id="br-2"><br></h2>

<p>One last thing, how about a different projection?</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-r" data-lang="r">crs.molvidde <span style="color:#f92672">&lt;-</span> leafletCRS(
  crsClass<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;L.Proj.CRS&#34;</span>, code<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;ESRI:53009&#39;</span>,
  proj4def<span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;+proj=moll +lon_0=0 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m +no_defs&#39;</span>,
  resolutions <span style="color:#f92672">=</span> <span style="color:#66d9ef">c</span>(<span style="color:#ae81ff">65536</span>, <span style="color:#ae81ff">32768</span>, <span style="color:#ae81ff">16384</span>, <span style="color:#ae81ff">8192</span>, <span style="color:#ae81ff">4096</span>, <span style="color:#ae81ff">2048</span>))

leaflet(options <span style="color:#f92672">=</span> leafletOptions(maxZoom <span style="color:#f92672">=</span> <span style="color:#ae81ff">2</span>,
  crs<span style="color:#f92672">=</span> crs.molvidde, attributionControl <span style="color:#f92672">=</span> <span style="color:#66d9ef">FALSE</span>)) <span style="color:#f92672">%&gt;%</span>                           </code></pre></div>
<p><img src="/img/MapLeafc.png" alt="KG proj" /></p>

<p>I hope this has been useful.</p>

<hr />

<p>[- Download <a href="/img/koeppen_lgd.txt">table of colours</a>]</p>

<hr />

<p>.</p>

<p>Maps ©<a href="http://openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors unless otherwise noted.</p>
<div class="footnotes">

<hr />

<ol>
<li id="fn:1">Rubel, F., and M. Kottek, 2010: Observed and projected climate shifts 1901-2100 depicted by world maps of the Köppen-Geiger climate classification. Meteorol. Z., 19, 135-141. DOI: 10.<sup>1127</sup>&frasl;<sub>0941</sub>-2948/2010/0430.
 <a class="footnote-return" href="#fnref:1"><sup>[return]</sup></a></li>
<li id="fn:2"><a href="http://koeppen-geiger.vu-wien.ac.at/shifts.htm" target="_blank">http://koeppen-geiger.vu-wien.ac.at/shifts.htm</a>
 <a class="footnote-return" href="#fnref:2"><sup>[return]</sup></a></li>
<li id="fn:3"><a href="https://github.com/circleofconfusion/climate-map" target="_blank">https://github.com/circleofconfusion/climate-map</a>
 <a class="footnote-return" href="#fnref:3"><sup>[return]</sup></a></li>
<li id="fn:4">Country Polygons as GeoJSON, in <a href="http://datahub.io" target="_blank">http://datahub.io</a>
 <a class="footnote-return" href="#fnref:4"><sup>[return]</sup></a></li>
</ol>
</div>
]]></content>
        </item>
        
        <item>
            <title>Re-format tables in VIM</title>
            <link>https://www.guxsousa.com/posts/2017/11/re-format-tables-in-vim/</link>
            <pubDate>Mon, 20 Nov 2017 13:21:16 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2017/11/re-format-tables-in-vim/</guid>
            <description>A handy command to re-format tables separated by common elements: column
man column DESCRIPTION The column utility formats its input into multiple columns. Rows are filled before columns. Input is taken from file operands, or, by default, from the standard input. Empty lines are ignored.  
Say there is a table separated by commas as following:
object,categoryA,categoryB,categoryC,categoryD,categoryE,categoryF nameA,3553,1200,100,0,150,240 nameB,1000,500,736,0,0,0 nameC,4882,500,0,400,0,0 nameD,500,350,0,0,0,1000 nameE,250,0,500,0,175,0 nameF,60,250,0,0,786,560 nameG,0,0,1000,0,3000,0 nameH,491,200,0,120,277,0 In vim, this can be then converted using the column utility:</description>
            <content type="html"><![CDATA[<p>A handy command to re-format tables separated by <em>common elements</em>: <strong>column</strong></p>

<pre><code>man column

DESCRIPTION
     The column utility formats its input into multiple columns. Rows are filled before columns. Input is taken from file operands, or, by default, from the standard input. Empty lines are ignored.
</code></pre>

<p><br></p>

<p>Say there is a table separated by commas as following:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cs" data-lang="cs"><span style="color:#66d9ef">object</span>,categoryA,categoryB,categoryC,categoryD,categoryE,categoryF
nameA,<span style="color:#ae81ff">3553</span>,<span style="color:#ae81ff">1200</span>,<span style="color:#ae81ff">100</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">150</span>,<span style="color:#ae81ff">240</span>
nameB,<span style="color:#ae81ff">1000</span>,<span style="color:#ae81ff">500</span>,<span style="color:#ae81ff">736</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">0</span>
nameC,<span style="color:#ae81ff">4882</span>,<span style="color:#ae81ff">500</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">400</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">0</span>
nameD,<span style="color:#ae81ff">500</span>,<span style="color:#ae81ff">350</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">1000</span>
nameE,<span style="color:#ae81ff">250</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">500</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">175</span>,<span style="color:#ae81ff">0</span>
nameF,<span style="color:#ae81ff">60</span>,<span style="color:#ae81ff">250</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">786</span>,<span style="color:#ae81ff">560</span>
nameG,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">1000</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">3000</span>,<span style="color:#ae81ff">0</span>
nameH,<span style="color:#ae81ff">491</span>,<span style="color:#ae81ff">200</span>,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">120</span>,<span style="color:#ae81ff">277</span>,<span style="color:#ae81ff">0</span></code></pre></div>
<p>In <code>vim</code>, this can be then converted using the
<code>column</code> utility:</p>

<pre><code>:%!column -t -s ','
</code></pre>

<p>And the table changes to:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-cs" data-lang="cs"><span style="color:#66d9ef">object</span>  categoryA  categoryB  categoryC  categoryD  categoryE  categoryF
nameA   <span style="color:#ae81ff">3553</span>       <span style="color:#ae81ff">1200</span>       <span style="color:#ae81ff">100</span>        <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">150</span>        <span style="color:#ae81ff">240</span>
nameB   <span style="color:#ae81ff">1000</span>       <span style="color:#ae81ff">500</span>        <span style="color:#ae81ff">736</span>        <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">0</span>
nameC   <span style="color:#ae81ff">4882</span>       <span style="color:#ae81ff">500</span>        <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">400</span>        <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">0</span>
nameD   <span style="color:#ae81ff">500</span>        <span style="color:#ae81ff">350</span>        <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">1000</span>
nameE   <span style="color:#ae81ff">250</span>        <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">500</span>        <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">175</span>        <span style="color:#ae81ff">0</span>
nameF   <span style="color:#ae81ff">60</span>         <span style="color:#ae81ff">250</span>        <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">786</span>        <span style="color:#ae81ff">560</span>
nameG   <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">1000</span>       <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">3000</span>       <span style="color:#ae81ff">0</span>
nameH   <span style="color:#ae81ff">491</span>        <span style="color:#ae81ff">200</span>        <span style="color:#ae81ff">0</span>          <span style="color:#ae81ff">120</span>        <span style="color:#ae81ff">277</span>        <span style="color:#ae81ff">0</span></code></pre></div>]]></content>
        </item>
        
        <item>
            <title>Lyrics Fetcher in CLI</title>
            <link>https://www.guxsousa.com/posts/2017/08/lyrics-fetcher-in-cli/</link>
            <pubDate>Sat, 05 Aug 2017 11:55:26 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2017/08/lyrics-fetcher-in-cli/</guid>
            <description>Just for fun, a lyric retriever in the command-line interpreter (CLI).
It fetches lyrics of current song playing in CMus or directly typed in the terminal.
A few comments about the figure above:
 tmux session with two panes and status bar
 the status bar displays the current song with the same script used by cmus-lyrics
 there is an alias in the corresponding BASH profile to call cmus-lyrics</description>
            <content type="html"><![CDATA[<p>Just for fun, a <strong>lyric retriever</strong>  in the command-line interpreter (<strong>CLI</strong>).</p>

<p>It fetches lyrics of current song playing in <a href="C* Music Player" target="_blank"><a href="https://cmus.github.io/" target="_blank">CMus</a></a> or directly typed in the terminal.</p>

<p><img src="/img/cmuslyrics.png" alt="cmus-lyrics-session" /></p>

<p>A few comments about the figure above:</p>

<ul>
<li><p>tmux session with two panes and <em>status bar</em></p></li>

<li><p>the status bar displays the current song with the same script used by <code>cmus-lyrics</code></p></li>

<li><p>there is an alias in the corresponding BASH profile to call <code>cmus-lyrics</code></p></li>
</ul>

<hr />

<p>See details in <a href="https://github.com/guxsousa/cmus-lyrics" target="_blank">cmus-lyrics</a> at github</p>
]]></content>
        </item>
        
        <item>
            <title>Manipulation of Latex-bib using regular expressions in VIM</title>
            <link>https://www.guxsousa.com/posts/2017/03/manipulation-of-latex-bib-using-regular-expressions-in-vim/</link>
            <pubDate>Sun, 12 Mar 2017 11:55:26 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2017/03/manipulation-of-latex-bib-using-regular-expressions-in-vim/</guid>
            <description>Occasionally, although the possibility of submitting LaTeX source files is great, it results convenient to clean these files before.
Particularly, here is an example of how to edit bibliography-related fields within the *.bib file using vim.
Matches are made with regular expressions (aka regex). Hence, it is convenient to check for consistency along the fields in the document (i.e. What kind of format is used? What kind of separators?).</description>
            <content type="html"><![CDATA[

<p>Occasionally, although the possibility of submitting <em>LaTeX</em> source files is great, it results convenient to clean these files before.</p>

<p>Particularly, here is an example of how to edit bibliography-related fields within the <code>*.bib</code> file using <strong>vim</strong>.</p>

<p><em>Matches</em> are made with regular expressions (aka <em>regex</em>). Hence, it is convenient to check for consistency along the fields in the document (i.e. What kind of format is used? What kind of separators?).</p>

<p>Please don&rdquo;t forget to use <code>:help</code> to find out more about matches and patterns in <strong>vim</strong> (<code>:help regex</code>, <code>:h search</code>, etc).</p>

<h2 id="preliminaries">Preliminaries</h2>

<p>A <code>*.bib</code> file contains the following structure.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-tex" data-lang="tex">    @article{keyentry,
       author    = {author},
       abstract  = {some text},
       annote    = {some text},
       url       = {someurlinwww},
       title     = {some text},
       journal   = {some text},
       year      = {some year},
    }</code></pre></div>
<p>Alternative structures may suppress brackets or use quotation marks instead. For example,</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-tex" data-lang="tex">    @article{keyentry,
       author    = &#34;author&#34;,
       abstract  = &#34;some text&#34;,
       annote    = &#34;some text&#34;,
       url       = &#34;someurlinwww&#34;,
       title     = &#34;some text&#34;,
       journal   = &#34;some text&#34;,
       year      = some year,
    }</code></pre></div>
<p>The <em>search + replace</em> methods implemented in <strong>vim</strong> follow the structure: <code>:range s[ubstitute]/pattern/string/cgiI</code> where <em>c</em> confirms each substitution; <em>g</em> replaces all occurrences in the line (without g - only first); <em>i</em> ignores case for the pattern; and <em>I</em> doesn&rdquo;t ignore case for the pattern.</p>

<p>.</p>

<h2 id="example">Example</h2>

<p>Here is the bibliography file used for the example
- <a href="/img/sample.bib">Sample.bib</a></p>

<p>In normal mode, <code>/</code> is used as a preliminary search to check for the pattern recognition. For instance, the following command searches for fields containing <em>url</em>.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">    / url<span style="color:#ae81ff">\s</span>*<span style="color:#f92672">=</span></code></pre></div>
<p>Here <code>\s</code> means &ldquo;any space&rdquo; and <code>*</code> means &ldquo;any number of occurrences&rdquo;.</p>

<p>To display all occurrences in the file, the following command can be used:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">    :g/ url<span style="color:#ae81ff">\s</span>*<span style="color:#f92672">=</span> <span style="color:#f92672">{</span>.*<span style="color:#ae81ff">\}</span></code></pre></div>
<p>where <code>.*\</code> means &ldquo;any number of characters excluding the new line&rdquo;. Therefore, if the <em>bib</em> entry is expected to be a single line, this expression is sufficient; however for an entry with multiple lines, such as the abstract section, the command may require an additional method. The following command includes <code>\{-}</code> which stands for &ldquo;null or more matches of the preceding atom&rdquo;. As a result, the search is made iterative and everything contained between the brackets is thus found.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">    :g/ url<span style="color:#ae81ff">\s</span>*<span style="color:#f92672">=</span> <span style="color:#f92672">{</span><span style="color:#ae81ff">\_</span>.<span style="color:#ae81ff">\{</span>-<span style="color:#f92672">}}</span>,</code></pre></div>
<p>The final <em>search + replace</em> command results as following:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">    :%s/ url<span style="color:#ae81ff">\s</span>*<span style="color:#f92672">=</span> <span style="color:#f92672">{</span><span style="color:#ae81ff">\_</span>.<span style="color:#ae81ff">\{</span>-<span style="color:#f92672">}}</span>,/url <span style="color:#f92672">=</span> <span style="color:#f92672">{}</span>,/g</code></pre></div>
<p>Using a similar <em>search + replace</em> command is then used to replace other fields; however, if more than one field must be changed, then a set of <em>vim</em>  instructions may be called from the terminal, using <code>vim -c</code>. The following command (i) replaces information contained in 4 fields, (ii) saves the edited document in a new one, and (iii) exits without saving in the original file.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">  vim -c <span style="color:#e6db74">&#34;%s/ url\s*= {\_.\{-}}/url = {}/g&#34;</span> <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span>      -c <span style="color:#e6db74">&#34;%s/ annote\s*= {\_.\{-}}/ annote = {}/g&#34;</span> <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span>      -c <span style="color:#e6db74">&#34;%s/abstract\s*= {\_.\{-}}/ abstract = {}/g&#34;</span> <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span>      -c <span style="color:#e6db74">&#34;%s/ file\s*= {\_.\{-}}/ file = {}/g&#34;</span> <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span>      -c <span style="color:#e6db74">&#34;wq temp.bib&#34;</span> <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span>      -c <span style="color:#e6db74">&#34;q\!&#34;</span> <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span>      sample.bib</code></pre></div>
<p>.</p>

<hr />

<p>Of course, as is common practice in <strong>vim</strong>, such a command might be replaced by a more sophisticated regex method which reduces the number of instructions and employs different patterns. By the way, a maximum of 10 instructions is supported in <code>vim -c</code> mode.</p>

<p>To end, cleaning a <code>*.bib</code> file in/with <strong>vim</strong> can be performed in a single command. Pretty cool!</p>

<p>I hope this has been useful.</p>
]]></content>
        </item>
        
        <item>
            <title>Macros with regular expressions in VIM</title>
            <link>https://www.guxsousa.com/posts/2017/01/macros-with-regular-expressions-in-vim/</link>
            <pubDate>Mon, 09 Jan 2017 11:15:26 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2017/01/macros-with-regular-expressions-in-vim/</guid>
            <description>Say we want to replace some values in a file; however, rather than performing a direct replacement, we are interested in replacing values based on their relative position&amp;#151;to their containing objects, or within certain patterns accompanying each of them.
For example, the following file contains some information in a typical fortran structure:
!- An example with objects and variables  Object A, !- Some comment or additional label for objects  Name A1, !</description>
            <content type="html"><![CDATA[

<p>Say we want to replace some values in a file; however, rather than performing a direct replacement, we are interested in replacing values based on their relative position&#151;to their containing objects, or within certain patterns accompanying each of them.</p>

<p>For example, the following file contains some information in a typical <code>fortran</code> structure:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fortran" data-lang="fortran"><span style="color:#75715e">!- An example with objects and variables
</span><span style="color:#75715e"></span>
Object A,  <span style="color:#75715e">!- Some comment or additional label for objects
</span><span style="color:#75715e"></span> Name A1,  <span style="color:#75715e">!- Name of object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">10</span>,  <span style="color:#75715e">!- First variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">9</span>,  <span style="color:#75715e">!- Second variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">8</span>,  <span style="color:#75715e">!- Third variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">7</span>;  <span style="color:#75715e">!- Fourth variable in object
</span><span style="color:#75715e"></span>
Object A,  <span style="color:#75715e">!- Some comment or additional label for objects
</span><span style="color:#75715e"></span> Name A2,  <span style="color:#75715e">!- Name of object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">20</span>,  <span style="color:#75715e">!- First variable in object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">19</span>,  <span style="color:#75715e">!- Second variable in object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">18</span>,  <span style="color:#75715e">!- Third variable in object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">17</span>;  <span style="color:#75715e">!- Fourth variable in object
</span><span style="color:#75715e"></span>
Object A,  <span style="color:#75715e">!- Some comment or additional label for objects
</span><span style="color:#75715e"></span> Name A3,  <span style="color:#75715e">!- Name of object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">10</span>,  <span style="color:#75715e">!- First variable in object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">19</span>,  <span style="color:#75715e">!- Second variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">8</span>,  <span style="color:#75715e">!- Third variable in object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">17</span>;  <span style="color:#75715e">!- Fourth variable in object
</span><span style="color:#75715e"></span>
Object B,  <span style="color:#75715e">!- Some comment or additional label for objects
</span><span style="color:#75715e"></span> Name B1,  <span style="color:#75715e">!- Name of object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">20</span>,  <span style="color:#75715e">!- First variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">9</span>,  <span style="color:#75715e">!- Second variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">8</span>,  <span style="color:#75715e">!- Third variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">7</span>;  <span style="color:#75715e">!- Fourth variable in object
</span><span style="color:#75715e"></span>
Object C,  <span style="color:#75715e">!- Some comment or additional label for objects
</span><span style="color:#75715e"></span> Name C1,  <span style="color:#75715e">!- Name of object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">10</span>,  <span style="color:#75715e">!- First variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">9</span>,  <span style="color:#75715e">!- Second variable in object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">18</span>,  <span style="color:#75715e">!- 3rd variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">7</span>;  <span style="color:#75715e">!- Fourth variable in object
</span><span style="color:#75715e"></span>
Object C,  <span style="color:#75715e">!- Some comment or additional label for objects
</span><span style="color:#75715e"></span> Name C2,  <span style="color:#75715e">!- Name of object
</span><span style="color:#75715e"></span>      <span style="color:#ae81ff">30</span>,  <span style="color:#75715e">!- First variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">9</span>,  <span style="color:#75715e">!- Second variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">8</span>,  <span style="color:#75715e">!- Third variable in object
</span><span style="color:#75715e"></span>       <span style="color:#ae81ff">7</span>;  <span style="color:#960050;background-color:#1e0010">!</span><span style="color:#f92672">-</span> Fourth variable in object</code></pre></div>
<p>And we want to replace the <em>first variable</em> in <em>objects</em> A and B, with 55.</p>

<p>To this end, we need to:
1. Define the logic
2. Translate the logic to a <code>regex</code> command
3. Manage the repetition of this logic.</p>

<h4 id="1-define-the-logic">1. Define the logic</h4>

<p>One way of doing this would be to search fo the common pattern: &ldquo;First variable in object&rdquo;, and then check if it is contained in either &ldquo;Object A&rdquo; or &ldquo;Object B&rdquo;.</p>

<p>Or the other way round, to search for &ldquo;Object A&rdquo; or &ldquo;Object B&rdquo; and delimit the pattern to the presence of &ldquo;First variable in object&rdquo;.</p>

<p><br></p>

<p>Let&rsquo;s use the second alternative. First we need to find &ldquo;Object A&rdquo; or &ldquo;Object B&rdquo;, followed by any text. Second we need to stop the search until the pattern &ldquo;First variable in object&rdquo; appears. Because the later pattern appears multiple times, we delimit the second search to a range of lines so it doesn&rsquo;t match a pattern in the following object.</p>

<h4 id="2-translate-the-logic-to-regex-command">2. Translate the logic to <code>regex</code> command</h4>

<p>A line with &ldquo;Object A&rdquo; or &ldquo;Object B&rdquo;, followed by any text:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vim" data-lang="vim">/<span style="color:#a6e22e">Object</span> [<span style="color:#a6e22e">BC</span>].*\<span style="color:#a6e22e">n</span></code></pre></div>
<pre><code>[BC] : B or C character
\n   : a newline character (line ending)
</code></pre>

<p>Stop until the pattern &ldquo;First variable&rdquo; appears, in a range of 6 lines.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vim" data-lang="vim">.*\(\<span style="color:#a6e22e">_s</span>.*\)\{<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">6</span>\}\<span style="color:#a6e22e">_sFirst</span> <span style="color:#a6e22e">variable</span></code></pre></div>
<pre><code>.*      : any character except new line, multiple times
\_s     : a whitespace (space or tab) or newline character
\{0,6\} : a range of 6 lines
</code></pre>

<p>The full regular expression:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vim" data-lang="vim">/<span style="color:#a6e22e">Object</span> [<span style="color:#a6e22e">BC</span>].*\<span style="color:#a6e22e">n</span> .*\(\<span style="color:#a6e22e">_s</span>.*\)\{<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">6</span>\}\<span style="color:#a6e22e">_sFirst</span> <span style="color:#a6e22e">variable</span></code></pre></div>
<h4 id="3-manage-the-repetition-of-this-logic">3. Manage the repetition of this logic.</h4>

<p>We record a macro (in <em>t</em>) to make the substitution.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vim" data-lang="vim"><span style="color:#a6e22e">qt</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span>/<span style="color:#a6e22e">Object</span> [<span style="color:#a6e22e">BC</span>].*\<span style="color:#a6e22e">n</span> .*\(\<span style="color:#a6e22e">_s</span>.*\)\{<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">6</span>\}\<span style="color:#a6e22e">_sFirst</span> <span style="color:#a6e22e">variable</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#ae81ff">2</span><span style="color:#a6e22e">jwcw55</span>&lt;<span style="color:#a6e22e">esc</span>&gt;<span style="color:#a6e22e">j</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">q</span></code></pre></div>
<pre><code>2j    : move two lines down
w     : move one word
cw    : change word
55    : new value
&lt;esc&gt; : return to command mode
j     : move one line down
</code></pre>

<p>And recursively run the macro in the file</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vim" data-lang="vim">:%<span style="color:#a6e22e">normal</span> @<span style="color:#a6e22e">t</span></code></pre></div>
<hr />

<p>Let&rsquo;s now replace the <em>third</em> variable in all objects with 99.</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vim" data-lang="vim"><span style="color:#a6e22e">qt</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span>/<span style="color:#a6e22e">Object</span>.*\<span style="color:#a6e22e">n</span> .*\(\<span style="color:#a6e22e">_s</span>.*\)\{<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">3</span>\}\<span style="color:#a6e22e">_svariable</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#ae81ff">2</span><span style="color:#a6e22e">jwcw55</span>&lt;<span style="color:#a6e22e">esc</span>&gt;<span style="color:#a6e22e">j</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span><span style="color:#a6e22e">q</span><span style="color:#960050;background-color:#1e0010">
</span><span style="color:#960050;background-color:#1e0010"></span>:%<span style="color:#a6e22e">normal</span> @<span style="color:#a6e22e">t</span></code></pre></div>
<hr />

<p>I hope this has been useful.</p>
]]></content>
        </item>
        
        <item>
            <title>Focused Principal Component Analysis</title>
            <link>https://www.guxsousa.com/posts/2016/11/focused-principal-component-analysis/</link>
            <pubDate>Thu, 10 Nov 2016 11:55:26 +0100</pubDate>
            
            <guid>https://www.guxsousa.com/posts/2016/11/focused-principal-component-analysis/</guid>
            <description>The Principal Component Analysis (PCA) is a statistical procedure that uses an orthogonal transformation to convert a set of observations of possibly correlated variables into a set of values of linearly uncorrelated variables called principal components. The number of principal components is less than or equal to the number of original variables. This transformation is defined in such a way that the first principal component has the largest possible variance (that is, accounts for as much of the variability in the data as possible), and each succeeding component in turn has the highest variance possible under the constraint that it is orthogonal to the preceding components.</description>
            <content type="html"><![CDATA[

<p>The <strong>Principal Component Analysis (PCA)</strong> is a statistical procedure that uses an orthogonal transformation to convert a set of observations of possibly correlated variables into a set of values of linearly uncorrelated variables called principal components. The number of <em>principal components</em> is less than or equal to the number of original variables. This transformation is defined in such a way that the first principal component has the largest possible variance (that is, accounts for as much of the variability in the data as possible), and each succeeding component in turn has the highest variance possible under the constraint that it is orthogonal to the preceding components. The resulting vectors are an uncorrelated orthogonal basis set. <strong>PCA</strong> is sensitive to the relative scaling of the original variables. <a href="https://en.wikipedia.org/wiki/Principal_component_analysis" target="_blank">&hellip; see more </a></p>

<p>In particular, the <strong>Focused Principal Component Analysis (fPCA)</strong> conveys the structure of a correlation matrix into a low-dimensional diagram but, unlike <em>PCA</em>, it makes it possible to represent accurately the correlations of a given variable with the other variables (and even to test graphically the hypothesis that one of these correlations is equal to zero).</p>

<p>In sum, the <strong>fPCA</strong> provides a <em>focused</em> outcome, so that the distances between the predictors and the outcome can be interpreted as a representation of their correlations. The relative positions of the predictors can give an idea of their correlations and can be interpreted as in a classic <strong>PCA</strong>.</p>

<p><br><br></p>

<h3 id="example">Example:</h3>

<p>The <em>iris</em> dataset is used to exemplify the use of the <em>fPCA</em> method. The dataset contains four measurements for 150 flowers representing three species of iris (i.e. setosa, versicolor and virginica).<a href="http://statlab.uni-heidelberg.de/data/iris/" target="_blank">&hellip; see more </a></p>

<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<tr>
<td><img src="/img/iris-setosa.jpg" alt="" /></td>
<td><img src="/img/iris-versicolor.jpg" alt="" /></td>
<td><img src="/img/iris-virginica.jpg" alt="" /></td>
</tr>

<tr>
<td>setosa</td>
<td>versicolor</td>
<td>virginica</td>
</tr>
</tbody>
</table>

<p>The first step which is usually taken when analysing data is to quickly find a linear correlation. However, when the data contains several factors, the correlation may be unclear and the next step consists on finding clusters between the data to complement the correlation.</p>

<p><img src="/img/fPCAa.svg" alt="lm Plot" /></p>

<p>The following figure shows that there are clearly clusters between <em>versicolor</em> and <em>virginica</em> (green and blue), and a linear correlation between <em>Petal.Length</em>, <em>Petal.Width</em>.</p>

<pre><code>&gt; summary(mod)

  Call:
  lm(formula = iris$Species ~ iris$Sepal.Length + iris$Sepal.Width +
      iris$Petal.Length + iris$Petal.Width)

  Residuals:
       Min       1Q   Median       3Q      Max
  -0.59215 -0.15368  0.01268  0.11089  0.55077

  Coefficients:
                    Estimate Std. Error t value Pr(&gt;|t|)    
  (Intercept)        1.18650    0.20484   5.792 4.15e-08 ***
  iris$Sepal.Length -0.11191    0.05765  -1.941   0.0542 .  
  iris$Sepal.Width  -0.04008    0.05969  -0.671   0.5030    
  iris$Petal.Length  0.22865    0.05685   4.022 9.26e-05 ***
  iris$Petal.Width   0.60925    0.09446   6.450 1.56e-09 ***
  ---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

  Residual standard error: 0.2191 on 145 degrees of freedom
  Multiple R-squared:  0.9304,  Adjusted R-squared:  0.9285
  F-statistic: 484.5 on 4 and 145 DF,  p-value: &lt; 2.2e-16
</code></pre>

<p>As seen, the linear correlation suggests these four variables as predictors with R-squared &gt; 90%. This values indicates how well a regression model predicts responses for the reference observation. But it needs to be used with caution at all times. It might be the case&mdash;and this a very common <del>error</del> case in descriptive statistics&mdash;that the correlation found can be considered as a well fitting method to generate an equation. But in reality, the method is unable to explain the true relationship between the variables.</p>

<p>If the purpose of the preliminary analysis is to provide an overview of the dataset, then the <em>fPCA</em> method is a good candidate due to its ability to provide both correlations and clusters in a fast way.</p>

<p><img src="/img/fPCAb.svg" alt="fPCA Plot" /></p>

<p>In brief, the variables inside the dashed line are significantly associated with the focused outcome (Species). Green dots correspond to positive associations, and yellow dots to negative associations. Dots in opposite quadrants represent negative correlation between them. And finally, the red line represents a significant correlation with the outcome; this is also given by the <em>r</em> values in the axis.</p>

<p>As seen in the figure, the correlation between <em>Petal.Length</em> and <em>Petal.Width</em> is maintained in this method; the <em>lm</em> model supports the further explanation of the correlation. It can also be seen that <em>Sepal.Width</em> is the less accurate predictor as found in both <em>lm</em> and <em>fPCA</em> methods; however, in the former the variable is neglectable (for the model) but in the later is considered because of its relation with the other variables. This is a relevant attribute of the <em>fPCA</em> method.</p>

<hr />
]]></content>
        </item>
        
    </channel>
</rss>
