<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://josephcrispell.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://josephcrispell.github.io/" rel="alternate" type="text/html" /><updated>2026-11-17T20:12:15+00:00</updated><id>https://josephcrispell.github.io/feed.xml</id><title type="html">Joseph Crispell</title><subtitle>Interests of a data scientist</subtitle><entry><title type="html">Using time stamped prints in R and python</title><link href="https://josephcrispell.github.io/2026/10/21/stamped-print.html" rel="alternate" type="text/html" title="Using time stamped prints in R and python" /><published>2026-10-21T00:00:00+00:00</published><updated>2026-10-21T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2026/10/21/stamped-print</id><content type="html" xml:base="https://josephcrispell.github.io/2026/10/21/stamped-print.html"><![CDATA[<p>Well it’s been quite a few years since my last post and I thought I would start back with a nice quick post about print statements! 🤣</p>

<p>I’ve found it really helpful to attach timestamps to my print statements in my code, it helps me quickly (and crudely) see how long each step in my code has taken. I use a simple function in both R and python to do this and I really enjoy how even though the functions are written in different languages they look and feel very similar!</p>

<p>Here’s the R function:</p>

<div class="language-r highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cd">#' Print (cat) text with a timestamp pre-pended</span><span class="w">
</span><span class="cd">#'</span><span class="w">
</span><span class="cd">#' @param text string to print using cat()</span><span class="w">
</span><span class="cd">#' @param stamp_colour plain text colour ("red", "green", etc.). Defaults to "yellow"</span><span class="w">
</span><span class="cd">#'</span><span class="w">
</span><span class="cd">#' @examples</span><span class="w">
</span><span class="cd">#' stamped_print("Try me!")</span><span class="w">
</span><span class="cd">#' stamped_print("Try me!", stamp_colour = "red")</span><span class="w">
</span><span class="n">stamped_print</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="n">stamp_colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"yellow"</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">
  
  </span><span class="c1"># Note reset code</span><span class="w">
  </span><span class="n">reset</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\033[0m"</span><span class="w">
  
  </span><span class="c1"># Get colour ANSI code</span><span class="w">
  </span><span class="n">colour_codes</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nf">list</span><span class="p">(</span><span class="w">
    </span><span class="s2">"red"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\033[31m"</span><span class="p">,</span><span class="w">
    </span><span class="s2">"green"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\033[32m"</span><span class="p">,</span><span class="w">
    </span><span class="s2">"yellow"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\033[33m"</span><span class="p">,</span><span class="w">
    </span><span class="s2">"blue"</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"\033[34m"</span><span class="w">
  </span><span class="p">)</span><span class="w">
  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">stamp_colour</span><span class="w"> </span><span class="o">%in%</span><span class="w"> </span><span class="nf">names</span><span class="p">(</span><span class="n">colour_codes</span><span class="p">))</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">stamp_colour</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">colour_codes</span><span class="p">[[</span><span class="n">stamp_colour</span><span class="p">]]</span><span class="w">
  </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="n">stamp_colour</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">reset</span><span class="w">
  </span><span class="p">}</span><span class="w">
  
  </span><span class="c1"># Get current timestamp</span><span class="w">
  </span><span class="n">current_time</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">format</span><span class="p">(</span><span class="n">Sys.Date</span><span class="p">(),</span><span class="w"> </span><span class="s2">"%Y-%m-%d %H:%M:%S"</span><span class="p">)</span><span class="w">
  
  </span><span class="c1"># Print text</span><span class="w">
  </span><span class="n">cat</span><span class="p">(</span><span class="n">paste0</span><span class="p">(</span><span class="n">stamp_colour</span><span class="p">,</span><span class="w"> </span><span class="n">current_time</span><span class="p">,</span><span class="w"> </span><span class="n">reset</span><span class="p">,</span><span class="w"> </span><span class="s2">"\t"</span><span class="p">,</span><span class="w"> </span><span class="n">text</span><span class="p">,</span><span class="w"> </span><span class="s2">"\n"</span><span class="p">))</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div></div>

<p>Here’s the python one:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">stamped_print</span><span class="p">(</span>
    <span class="n">text</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s">""</span><span class="p">,</span> <span class="n">stamp_colour</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s">"yellow"</span>
<span class="p">):</span>
  <span class="s">"""Add timestamp to print
      
  Args:
      text (str): text to print. Defaults to "".
      stamp_colour (str): colour of timestamp. Defaults to "yellow"
  
  .. code-block:: python

  from datetime import datetime
  from pathlib import Path
  
  stamped_print("Try me!")
  stamped_print("Try me!", stamp_colour = "red")
  
  """</span>
  <span class="c1"># Note reset code
</span>  <span class="n">reset</span> <span class="o">=</span> <span class="s">"</span><span class="se">\033</span><span class="s">[0m"</span>

  <span class="c1"># Get colour ANSI code
</span>  <span class="n">colour_dict</span> <span class="o">=</span> <span class="p">{</span>
      <span class="s">"red"</span><span class="p">:</span> <span class="s">"</span><span class="se">\033</span><span class="s">[31m"</span><span class="p">,</span>
      <span class="s">"green"</span><span class="p">:</span> <span class="s">"</span><span class="se">\033</span><span class="s">[32m"</span><span class="p">,</span>
      <span class="s">"yellow"</span><span class="p">:</span> <span class="s">"</span><span class="se">\033</span><span class="s">[33m"</span><span class="p">,</span>
      <span class="s">"blue"</span><span class="p">:</span> <span class="s">"</span><span class="se">\033</span><span class="s">[34m"</span><span class="p">,</span>
  <span class="p">}</span>
  <span class="k">if</span> <span class="n">stamp_colour</span> <span class="ow">in</span> <span class="n">colour_dict</span><span class="p">:</span>
      <span class="n">stamp_colour</span> <span class="o">=</span> <span class="n">colour_dict</span><span class="p">[</span><span class="n">stamp_colour</span><span class="p">]</span>
  <span class="k">else</span><span class="p">:</span>
      <span class="n">stamp_colour</span> <span class="o">=</span> <span class="n">reset</span>    

  <span class="c1"># Get current timestamp
</span>  <span class="n">current_time</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">.</span><span class="n">now</span><span class="p">().</span><span class="n">strftime</span><span class="p">(</span><span class="s">"%Y-%m-%d %H:%M:%S"</span><span class="p">)</span>

  <span class="c1"># Print text
</span>  <span class="k">print</span><span class="p">(</span><span class="sa">f</span><span class="s">"</span><span class="si">{</span><span class="n">stamp_colour</span><span class="si">}{</span><span class="n">current_time</span><span class="si">}{</span><span class="n">reset</span><span class="si">}</span><span class="se">\t</span><span class="si">{</span><span class="n">text</span><span class="si">}</span><span class="s">"</span><span class="p">)</span>
</code></pre></div></div>

<p>It’s a nice illustration of <a href="https://josephcrispell.github.io/2021/07/26/creating-R-docstring.html">docstring</a> structure in both languages too!</p>]]></content><author><name></name></author><category term="python" /><category term="r" /><category term="function" /><category term="docstring" /><category term="print" /><summary type="html"><![CDATA[Well it’s been quite a few years since my last post and I thought I would start back with a nice quick post about print statements! 🤣]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/stampedPrint/stamped_print.png" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/stampedPrint/stamped_print.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Create and update your python package unit test coverage badge with coverage_shield</title><link href="https://josephcrispell.github.io/2023/10/04/coverage-shield.html" rel="alternate" type="text/html" title="Create and update your python package unit test coverage badge with coverage_shield" /><published>2023-10-04T00:00:00+00:00</published><updated>2023-10-04T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2023/10/04/coverage-shield</id><content type="html" xml:base="https://josephcrispell.github.io/2023/10/04/coverage-shield.html"><![CDATA[<p>While I was creating my <a href="https://josephcrispell.github.io/2023/07/11/timesheet.html"><code class="language-plaintext highlighter-rouge">timesheet</code></a> package, I started learning about writing unit tests in python. I explored ways to add a coverage badge to my <code class="language-plaintext highlighter-rouge">README.md</code> in my repository and I came across quite a few tools:</p>

<ul>
  <li><a href="https://smarie.github.io/python-genbadge/"><code class="language-plaintext highlighter-rouge">genbadge</code></a> - generates badges as an image for a range of python tools including a coverage one</li>
  <li><a href="https://pypi.org/project/coverage-badge/"><code class="language-plaintext highlighter-rouge">coverage-badge</code></a> - generates badge as an image file</li>
  <li><a href="https://pypi.org/project/readme-coverage-badger/"><code class="language-plaintext highlighter-rouge">readme-coverage-badger</code></a> - adds coverage badge to README if you have already run tests</li>
  <li><a href="https://coveralls.io/"><code class="language-plaintext highlighter-rouge">coveralls</code></a> - generates coverage badge for readme but is an external tool that builds and tests codebase from GitHub</li>
  <li><a href="https://smarie.github.io/python-genbadge/"><code class="language-plaintext highlighter-rouge">python-genbadge</code></a> - generates image file for badge for a wide range of packages. For unit tests coverage, you need to run tests and generate coverage report before using</li>
</ul>

<p>For me, the main advantages of my <code class="language-plaintext highlighter-rouge">coverage_shield</code> python package are:</p>

<ul>
  <li>It was a great opportunity for me to learn more about building a python package</li>
  <li>Runs on your local machine with a simple command line interface</li>
  <li>Runs unit tests, generates coverage report, adds/updates badge in README.md file, and pushes updates via <code class="language-plaintext highlighter-rouge">git</code> (if you want it to) all in one step</li>
  <li>Uses the <a href="https://shields.io/badges">shields.io url structure</a> to show badge rather than creating an image of a badge that you have to store in repo</li>
  <li>Has a continuous colour scale (using a <a href="https://seaborn.pydata.org/tutorial/color_palettes.html">seaborn colour palette</a>) from red, through yellow, to green</li>
</ul>

<h1 id="how-it-works">How it works</h1>

<p><code class="language-plaintext highlighter-rouge">coverage_shield</code> has a simple command line interface:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>python <span class="nt">-m</span> coverage_shield <span class="nt">--help</span>
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>usage: coverage_shield [-h] [-d [directory]] [-r [readme_path]] [-t [tester]] [-g]

Welcome to coverage_shield! A tool to create and maintain a python package unit test coverage badge in README.md

options:
  -h, --help            show this help message and exit
  -d [directory], --directory [directory]
                        Provide path to directory to run coverage_shield in. (default: .)
  -r [readme_path], --readme [readme_path]
                        Provide path to README.md relative to directory provided. (default: README.md)
  -t [tester], --tester [tester]
                        Provide name of unit test python package you want to use. Accepts either "unittest" or "pytest" (default: unittest)
  -g, --git_push        Stage, commit, and push the updated README file (-r/--readme) using git. (default: False)
</code></pre></div></div>

<p>So you would run it on your python package repository with:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>python <span class="nt">-m</span> coverage_shield
</code></pre></div></div>

<h1 id="shields-io">Shields io</h1>

<p>The <a href="https://shields.io/badges">shields.io url structure</a> is the key. Using shields.io you can build a url with a simple structure like: <code class="language-plaintext highlighter-rouge">https://img.shields.io/badge/[name_of_badge]-[value]-[colour]</code>.</p>

<p>So <code class="language-plaintext highlighter-rouge">https://img.shields.io/badge/coverage-30%25-orange</code> gives: <img src="/assets/img/blog/coverageShield/badge.png" alt="plans" /></p>

<p>Once you have the url structure it is just a case of calculating your unit test coverage and inserting it into the url along with a colour (based on the coverage value) - and that’s what <code class="language-plaintext highlighter-rouge">coverage_shield</code> does! 💡</p>

<h1 id="wrapping-up">Wrapping up</h1>

<p><code class="language-plaintext highlighter-rouge">coverage_shield</code> was another great opportunity for me to learn more about building python packages. I am hoping to use it a lot in future projects and maybe others will find it useful too. If you want to learn more about <code class="language-plaintext highlighter-rouge">coverage_shield</code> please and take a look at the <a href="https://github.com/JosephCrispell/coverage_shield">repo</a> and give it a try!</p>]]></content><author><name></name></author><category term="python" /><category term="pip" /><category term="package" /><category term="coverage_shield" /><category term="unittest" /><category term="actions" /><summary type="html"><![CDATA[While I was creating my timesheet package, I started learning about writing unit tests in python. I explored ways to add a coverage badge to my README.md in my repository and I came across quite a few tools:]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/coverageShield/coverage_shield_thumbnail.png" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/coverageShield/coverage_shield_thumbnail.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Tracking the hours you work with timesheet - a python package I built</title><link href="https://josephcrispell.github.io/2023/07/11/timesheet.html" rel="alternate" type="text/html" title="Tracking the hours you work with timesheet - a python package I built" /><published>2023-07-11T00:00:00+00:00</published><updated>2023-07-11T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2023/07/11/timesheet</id><content type="html" xml:base="https://josephcrispell.github.io/2023/07/11/timesheet.html"><![CDATA[<p>I’ve been working on a simple python package called <a href="https://github.com/JosephCrispell/timesheet"><code class="language-plaintext highlighter-rouge">timesheet</code></a>, it’s a tool to help you track the hours you work in a simple spreadsheet. My previously role had a brilliant flexible working strategy that relied on tracking the hours you worked, when I decided to leave that role I wanted to take a bit of it with me so I created <code class="language-plaintext highlighter-rouge">timesheet</code>.</p>

<h1 id="how-it-works">How it works</h1>

<p><code class="language-plaintext highlighter-rouge">timesheet</code> has a simple command line interface:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>python3 <span class="nt">-m</span> timesheet <span class="nt">--help</span>
</code></pre></div></div>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>usage: timesheet [-h] [-f [timesheet_file_path]] [-r] [-s [hh:mm]] [-e [hh:mm]]

Welcome to timesheet, a tool to help you log the hours you work. You are using the command line interface for timesheet.

options:
  -h, --help            show this help message and exit
  -f [timesheet_file_path], --file [timesheet_file_path]
                        Provide file for timesheet (note if not created this will create file). (default: outputs/timesheet.csv)
  -r, --reset           Reset the timesheet file provided with file (-f/--file) argument. (default: False)
  -s [hh:mm], --start [hh:mm]
                        Add start time (hh:mm) to timesheet file provided with file (-f/--file) argument. (default: None)
  -e [hh:mm], --end [hh:mm]
                        Add end time (hh:mm) to timesheet file provided with file (-f/--file) argument. (default: None)
</code></pre></div></div>

<p>So you would add a start time with:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>python3 <span class="nt">-m</span> timesheet <span class="nt">--file</span> outputs/timesheet.csv <span class="nt">--start</span>
</code></pre></div></div>

<p>Which updates a timesheet with a simple Comma Separated Values (CSV) structure:</p>

<table>
  <thead>
    <tr>
      <th>date</th>
      <th>start_time</th>
      <th>end_time</th>
      <th>time_worked</th>
      <th>notes</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>2023-03-13</td>
      <td>08:24</td>
      <td>12:00</td>
      <td>03:36</td>
      <td>nothing of note</td>
    </tr>
    <tr>
      <td>2023-03-13</td>
      <td>12:24</td>
      <td>16:24</td>
      <td>04:00</td>
      <td> </td>
    </tr>
    <tr>
      <td>2023-03-14</td>
      <td>08:35</td>
      <td>12:05</td>
      <td>03:30</td>
      <td>nothing of note</td>
    </tr>
    <tr>
      <td>2023-03-14</td>
      <td>12:45</td>
      <td>16:55</td>
      <td>04:10</td>
      <td>nothing of note</td>
    </tr>
    <tr>
      <td>2023-03-15</td>
      <td>08:22</td>
      <td>11:51</td>
      <td>03:29</td>
      <td>nothing of note</td>
    </tr>
    <tr>
      <td>2023-03-15</td>
      <td>12:15</td>
      <td>17:05</td>
      <td>04:50</td>
      <td>nothing of note</td>
    </tr>
    <tr>
      <td>2023-03-16</td>
      <td>08:36</td>
      <td>12:06</td>
      <td>03:30</td>
      <td>a simple note</td>
    </tr>
    <tr>
      <td>2023-03-16</td>
      <td>12:56</td>
      <td>17:03</td>
      <td>04:07</td>
      <td>nothing of note</td>
    </tr>
    <tr>
      <td>2023-07-09</td>
      <td>15:18</td>
      <td> </td>
      <td>00:00</td>
      <td> </td>
    </tr>
  </tbody>
</table>

<h1 id="what-ive-learnt">What I’ve learnt</h1>

<p>The main aim of developing <code class="language-plaintext highlighter-rouge">timesheet</code> was to condense some of my learning so far into a simple example project as well as expand my practical experience of building python packages. <code class="language-plaintext highlighter-rouge">timesheet</code> has the following key elements I want to highlight:</p>
<ul>
  <li>Simple reuseable project structure</li>
  <li>Modular object oriented programming</li>
  <li>Unit testing with a high coverage</li>
  <li>Good documentation</li>
</ul>

<p>I wanted to use this blog post to talk about my experience building <code class="language-plaintext highlighter-rouge">timesheet</code> and reflect on each of the above key elements.</p>

<h2 id="reusable-project-structure">Reusable project structure</h2>

<p>To get me started, I wanted the simplest possible python package structure that I could understand and work within. There are plenty of useful resources on this (for example <a href="https://docs.python-guide.org/writing/structure/">here</a> and <a href="https://github.com/navdeep-G/samplemod">here</a>) and I landed on:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>📦my_package
 ┣ 📂data
 ┃ ┗ Put all your data in here
 ┣ 📂scripts
 ┃ ┗ Python scripts in here designed to called directly in command line
 ┣ 📂tests
 ┃ ┣ 📜__init__.py # required so python code registered within package
 ┃ ┗ Unit testing functions in here
 ┣ 📂my_package
 ┃ ┣ 📜__init__.py # required so python code registered within package
 ┃ ┣ 📜__main__.py # script called when you call package (python -m my_package)
 ┃ ┗ Package classes and modules in here
 ┣ 📜.gitignore # files to ignore (e.g. data folder)
 ┣ 📜.pre-commit-config.yaml # pre-commit workflow
 ┣ 📜LICENSE # open source licence
 ┣ 📜README.md # simple project documentation
 ┣ 📜requirements.txt # package requirements
 ┗ 📜setup.py # required for pip installation
</code></pre></div></div>

<p>The structure above will work nicely for all my python projects and hopefully encourage good practice without being overwhelming. Some cool things to note:</p>
<ul>
  <li>You can install a reactive version of your package (with the above structure) by navigating to the package directory and running:
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip3 <span class="nb">install</span> <span class="nt">-e</span> <span class="nb">.</span>
</code></pre></div>    </div>
    <p>and the package will automatically update as you change the code</p>
  </li>
  <li>Local imports of modules and classes within your package work really well using (this is setup via the <code class="language-plaintext highlighter-rouge">__init__.py</code> scripts in the structure above):
    <div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">from</span> <span class="nn">my_package</span> <span class="kn">import</span> <span class="n">my_package_module_or_class</span>
</code></pre></div>    </div>
  </li>
</ul>

<h2 id="modular-object-oriented-programming">Modular object oriented programming</h2>

<p>I spoke about the value of modularisation in my <a href="https://josephcrispell.github.io/2023/02/01/reproducibility.html">previous blog post on reproducibility</a>. Here’s how I’ve put that into practice in <code class="language-plaintext highlighter-rouge">timesheet</code>:</p>
<ul>
  <li><code class="language-plaintext highlighter-rouge">timesheet</code> is both modular with functions associated with different aspects of <code class="language-plaintext highlighter-rouge">timesheet</code> being stored in separate scripts (each are then linked together and used via local <code class="language-plaintext highlighter-rouge">import</code>s). For example:
    <ul>
      <li><a href="https://github.com/JosephCrispell/timesheet/blob/main/timesheet/data_functions.py"><code class="language-plaintext highlighter-rouge">timesheet/data_functions.py</code></a> - functions for working with the clocking in/out data</li>
      <li><a href="https://github.com/JosephCrispell/timesheet/blob/main/timesheet/timesheet.py"><code class="language-plaintext highlighter-rouge">timesheet/timesheet.py</code></a> - a Timesheet <a href="https://www.w3schools.com/python/python_classes.asp">class/object</a> for storing and interacting with timesheet data</li>
      <li><a href="https://github.com/JosephCrispell/timesheet/blob/main/timesheet/command_line_interface_functions.py"><code class="language-plaintext highlighter-rouge">timesheet/command_line_interface_functions.py</code></a> - functions for <code class="language-plaintext highlighter-rouge">timesheet</code>’s command line interface</li>
    </ul>
  </li>
  <li>All the functions defined throughout the <code class="language-plaintext highlighter-rouge">timesheet</code> codebase have <a href="https://realpython.com/documenting-python-code/#documenting-your-python-code-base-using-docstrings">docstrings</a>, for example here’s one for a simple function to calculate time differences:
    <div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="k">def</span> <span class="nf">calculate_time_difference</span><span class="p">(</span><span class="n">start_time</span><span class="p">:</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">end_time</span><span class="p">:</span> <span class="n">datetime</span><span class="p">)</span> <span class="o">-&gt;</span>    <span class="n">timedelta</span><span class="p">:</span>
  <span class="s">"""Calculate difference between start and end time

  Args:
      start_time (datetime.time): start time
      end_time (datetime.time): end time

  Returns:
      datetime.time: difference between start and end time
  """</span>

  <span class="c1"># Calculate time difference
</span>  <span class="n">difference</span> <span class="o">=</span> <span class="n">end_time</span> <span class="o">-</span> <span class="n">start_time</span>

  <span class="c1"># Check time difference is positive
</span>  <span class="k">if</span> <span class="n">difference</span><span class="p">.</span><span class="n">total_seconds</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
      <span class="k">raise</span> <span class="nb">Exception</span><span class="p">(</span>
          <span class="sa">f</span><span class="s">"The end_time provided (</span><span class="si">{</span><span class="n">end_time</span><span class="si">}</span><span class="s">) is not after the start_time (</span><span class="si">{</span><span class="n">start_time</span><span class="si">}</span><span class="s">)"</span>
      <span class="p">)</span>

  <span class="k">return</span> <span class="n">difference</span>
</code></pre></div>    </div>
  </li>
  <li>I’ve built a simple set of functions to test all of the <code class="language-plaintext highlighter-rouge">timesheet</code> functions that is similarly modular and helps to make my codebase more robust as I continue to develop it</li>
</ul>

<h2 id="unit-testing-with-high-coverage">Unit testing with high coverage</h2>

<p>Until building <code class="language-plaintext highlighter-rouge">timesheet</code> I hadn’t ever written any <a href="https://realpython.com/python-testing/">unit tests</a> for my projects despite them being an <a href="https://www.braydoncoyer.dev/blog/what-makes-a-unit-test-valuable">integral part</a> of developing reproducible and robust codebases. I’ve used developing the <code class="language-plaintext highlighter-rouge">timesheet</code> package as an opportunity to learn how to write unit tests and put that into practice.</p>

<p>The unit tests for <code class="language-plaintext highlighter-rouge">timesheet</code> are found in the <a href="https://github.com/JosephCrispell/timesheet/tree/main/tests"><code class="language-plaintext highlighter-rouge">timesheet/tests</code></a> folder and target each of <code class="language-plaintext highlighter-rouge">timesheet</code>’s functions. These tests use the <a href="https://docs.python.org/3/library/unittest.html"><code class="language-plaintext highlighter-rouge">unittest</code></a> python package.</p>

<p>Now the tests are written, testing <code class="language-plaintext highlighter-rouge">timesheet</code> is as simple as running this command:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>python <span class="nt">-m</span> unittest
</code></pre></div></div>

<p>I’ve linked my test coverage to a badge in my README.md too: <img src="https://img.shields.io/badge/coverage-95.1%25-green" alt="Code Coverage" />.</p>

<p>To see how I’ve done this check out <a href="https://github.com/JosephCrispell/timesheet/blob/main/scripts/update_test_coverage_badge.py"><code class="language-plaintext highlighter-rouge">timesheet/scripts/update_test_coverage_badge.py</code></a> - I’ll be writing a blog about this soon! ⏲</p>

<blockquote>
  <p>My key takeaway here is that unit tests, although intimidating, are easy to write (especially if you do them from the start) and are an incredibly useful tool as you are developing the codebase to ensure what you change isn’t having any unintended consequences.</p>
</blockquote>

<h2 id="good-documentation">Good documentation</h2>

<p>I talked about my writing <a href="https://realpython.com/documenting-python-code/#documenting-your-python-code-base-using-docstrings">docstrings</a> for my functions above, which are a really important part of <code class="language-plaintext highlighter-rouge">timesheet</code>’s documentation. I also wanted to use this opportunity to learn how to build project documentation using <a href="https://www.sphinx-doc.org/en/master/usage/quickstart.html"><code class="language-plaintext highlighter-rouge">Sphinx</code></a>.</p>

<p>For <code class="language-plaintext highlighter-rouge">timesheet</code>, I’ve set <code class="language-plaintext highlighter-rouge">Sphinx</code> up to create some simple documentation web pages by pulling all my code information from <code class="language-plaintext highlighter-rouge">timesheet</code>’s structure and docstrings (see the <a href="https://github.com/JosephCrispell/timesheet/tree/main/docs"><code class="language-plaintext highlighter-rouge">timesheet/docs</code></a> folder to get an idea of how it’s setup). In the future, I’ll get the <code class="language-plaintext highlighter-rouge">timesheet</code> docs hosted on one of the many docs hosting services like <a href="https://readthedocs.org/">readthedocs</a>.</p>

<p>I also spent a fair bit of time on the <a href="https://github.com/JosephCrispell/timesheet"><code class="language-plaintext highlighter-rouge">timesheet/README.md</code></a> (hopefully it shows) adding information on what <code class="language-plaintext highlighter-rouge">timesheet</code> is and how to use it. I added some cool features like an interactive <a href="https://github.com/JosephCrispell/timesheet#workflow">workflow</a> diagram created using <a href="https://mermaid.js.org/">mermaid</a> (to show the structure of the codebase and how it interacts):
<img src="/assets/img/blog/timesheet/mermaid.png" alt="mermaid workflow diagram" /></p>

<p>and a <a href="https://github.com/JosephCrispell/timesheet#package-structure">nice folder structure diagram</a> that I built using Visual Studio Code’s <a href="https://marketplace.visualstudio.com/items?itemName=Shinotatwu-DS.file-tree-generator">file-tree-generator</a> extension (like the one I have shown above).</p>

<h1 id="next-steps">Next steps</h1>

<p>I’ve got some ideas for next steps for <code class="language-plaintext highlighter-rouge">timesheet</code>:</p>
<ul>
  <li>As I noted above I want to host the documentation online using something like <a href="https://readthedocs.org/">readthedocs</a></li>
  <li>I want to add functionality to show total hours worked per day and compare that to a target</li>
  <li>I’d like to build a graphical user interface for <code class="language-plaintext highlighter-rouge">timesheet</code> for people who don’t want to use the command line</li>
  <li>I’d like to add an automatic logging feature that tracks locking and shutting down your computer and uses that to log hours work</li>
</ul>

<h1 id="wrapping-up">Wrapping up</h1>

<p>Anyways I’ve really enjoyed building <code class="language-plaintext highlighter-rouge">timesheet</code> and it’s going to be a brilliant resource for my future python projects. 🚀</p>]]></content><author><name></name></author><category term="python" /><category term="pip" /><category term="package" /><category term="timesheet" /><category term="unittest" /><category term="docs" /><summary type="html"><![CDATA[I’ve been working on a simple python package called timesheet, it’s a tool to help you track the hours you work in a simple spreadsheet. My previously role had a brilliant flexible working strategy that relied on tracking the hours you worked, when I decided to leave that role I wanted to take a bit of it with me so I created timesheet.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/timesheet/timesheet_blog_thumbnail.svg" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/timesheet/timesheet_blog_thumbnail.svg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Reproducibility in my work</title><link href="https://josephcrispell.github.io/2023/02/01/reproducibility.html" rel="alternate" type="text/html" title="Reproducibility in my work" /><published>2023-02-01T00:00:00+00:00</published><updated>2023-02-01T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2023/02/01/reproducibility</id><content type="html" xml:base="https://josephcrispell.github.io/2023/02/01/reproducibility.html"><![CDATA[<p>It’s been a while (over a year) since my last blog post! I’m hoping to get back into the swing of writing them fairly regularly but we’ll see! 😄 In this post, I want to talk a little about reproducibility (how easy it is to repeat and adapt projects) and reflect on one of my own projects.</p>

<h1 id="the-problem">The problem</h1>

<p>A few months ago someone reached out wanting to extend my <a href="https://elifesciences.org/articles/45833">research using genomics to examine the transmission of <em>Mycobacterium bovis</em> (the bacteria that causes bovine tuberculosis) between cattle and badgers in South West England</a> (also see short summary in my <a href="https://josephcrispell.github.io/2019/12/16/woodchester.html">blog</a>). Unfortunately, although my are publicly available on my <a href="https://github.com/JosephCrispell">GitHub account</a>, repeating this work is not easy.</p>

<p>I wanted to reflect on why this is a difficult task and how I can improve the reproducibility of my work.</p>

<p>My first step in helping this person was to make sense of my codebase. I created a diagram using <a href="https://miro.com/">miro</a> showing how each piece of code linked together and how the raw and processed data files moved from one to another:</p>

<iframe width="768" height="432" src="https://miro.com/app/live-embed/uXjVPqsp-rs=/?moveToViewport=-1861,-824,3200,1198&amp;embedId=140865863589" frameborder="0" scrolling="no" allow="fullscreen; clipboard-read; clipboard-write" allowfullscreen=""></iframe>

<p>In the process of exploring and mapping the codebase, I’ve noted a few limitations in the reproducibility of my work, for example:</p>

<blockquote>
  <ul>
    <li>I used multiple languages (Java, perl, python, R, and bash)</li>
    <li>The codebase was split across multiple repositories, none of which were specifically for the project</li>
    <li>There are many manual steps</li>
    <li>There are many input data files required without much detail on the content and source of these files</li>
    <li>I haven’t provided a guide for replicating the work</li>
    <li>There is a lack of code modularisation and documentation</li>
    <li>I’ve used full file paths throughout the codebase that are specific to my, now non-existent, computer</li>
  </ul>
</blockquote>

<h1 id="how-i-could-improve-in-the-future">How I could improve in the future</h1>

<p>All of the above issues are avoidable and, over the last few years, I’ve started to better understand how to do this. As pointers for myself and others, I’ll briefly address each of the above limitations here and consider how I can avoid them in the future.</p>

<hr />
<h2 id="--i-used-multiple-languages-java-perl-python-r-and-bash">- I used multiple languages (Java, perl, python, R, and bash)</h2>
<p>The more languages you use, the harder it is to understand each part and get them to work together. In my case, it also meant lots of manual steps moving from one language to another.</p>

<p>I used multiple languages because of the different types of analyses I was doing:</p>
<ul>
  <li>Perl, python and bash for processing the genomic data and interacting with bioinformatics software like <a href="https://github.com/lh3/bwa">bwa</a></li>
  <li>Java for analysing the large cattle and badger population databases</li>
  <li>R for the statistical analyses and visualisations</li>
</ul>

<p>In reality though, I could have done all of the above in a single language (particularly R or python). In some cases (for example, for efficiency or access to specific methods) it may have been necessary to use a different language and you can build in direct connections between the different languages. For example:</p>
<ul>
  <li>Use <a href="https://rstudio.github.io/reticulate/">reticulate</a> in R to run python code and <a href="https://rpy2.github.io/">rpy2</a> for the other way around</li>
  <li>Use <a href="https://github.com/s-u/rJava">rJava</a> in R and <a href="https://www.py4j.org/index.html">Py4J</a> in python to run Java code (rJava can be temperamental though! 😳)</li>
  <li>Use <a href="https://realpython.com/python-bindings-overview/">invoke</a> and <a href="https://www.rcpp.org/">Rcpp</a> to run c++ code from python and R, respectively</li>
</ul>

<hr />
<h2 id="--the-codebase-was-split-across-multiple-repositories-none-of-which-were-specifically-for-the-project">- The codebase was split across multiple repositories, none of which were specifically for the project</h2>
<p>Navigating across multiple repositories to find the code for a particular project is time consuming, particularly if the repositories contain code from a mixture of projects.</p>

<p>Each project should have a single repository that contains all the code necessary to run the project. <a href="https://best-practice-and-impact.github.io/qa-of-code-guidance/project_structure.html">Read more on simple project structures</a>.</p>

<hr />
<h2 id="--there-are-many-manual-steps">- There are many manual steps</h2>
<p>The more manual steps there are, the more opportunities there are to introduce errors and the longer the process is to replicate. It is important to have opportunities for user input into the process (checking outputs are as expected)but that can be built into the automation process without the need for the user to run multiple pieces of code separately.</p>

<p>Building <a href="https://analysisfunction.civilservice.gov.uk/support/reproducible-analytical-pipelines/">Reproducible Analytical Pipelines</a> are a great example of this and well worth looking into! 🎤🎵</p>

<hr />
<h2 id="--there-are-many-input-data-files-required-without-much-detail-on-the-content-and-source-of-these-files">- There are many input data files required without much detail on the content and source of these files</h2>
<p>Sometimes having many input data files is unavoidable but it is critical that we clearly record information about the data required. We should always include documentation alongside our data files, such as metadata files and data dictionaries.</p>

<p>An example of a really simple <code class="language-plaintext highlighter-rouge">metadata.json</code> file (that could accompany each data file required) might be:</p>
<div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">{</span><span class="w">
    </span><span class="nl">"file_name"</span><span class="p">:</span><span class="w"> </span><span class="s2">"OPN_RAW_Crispell_myHeightData_01-01-2020.csv"</span><span class="p">,</span><span class="w">
    </span><span class="nl">"description"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Information about my height"</span><span class="p">,</span><span class="w">
    </span><span class="nl">"url"</span><span class="p">:</span><span class="w"> </span><span class="s2">"https://josephcrispell.github.io/"</span><span class="p">,</span><span class="w">
    </span><span class="nl">"data_source"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Joseph Crispell"</span><span class="p">,</span><span class="w">
    </span><span class="nl">"source_contact"</span><span class="p">:</span><span class="w"> </span><span class="s2">"my_email@somewhere.com"</span><span class="p">,</span><span class="w">
    </span><span class="nl">"date_accessed"</span><span class="p">:</span><span class="w"> </span><span class="s2">"01-02-2023"</span><span class="p">,</span><span class="w">
    </span><span class="nl">"date_last_updated"</span><span class="p">:</span><span class="w"> </span><span class="s2">"01-01-2020"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div></div>

<p>In our <code class="language-plaintext highlighter-rouge">data/</code> folder we could also maintain a data dictionary (<code class="language-plaintext highlighter-rouge">README.md</code>), which could look something like:</p>
<div class="language-md highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="gh"># Data dictionary</span>
Details of the data files stored in the current <span class="sb">`data/`</span> folder.

<span class="gu">## Naming convention</span>

<span class="gs">**TYPE_STATUS_SOURCE_DESC_DATE.suffix**</span>
<span class="p">
-</span> <span class="gs">**TYPE**</span>: [OPN|SEC] to show whether this is an open (OPN) or secure (SEC) dataset
<span class="p">-</span> <span class="gs">**STATUS**</span>: [RAW|PROC|FINAL] to show whether this is a raw (RAW), processed (PROC) or final (FINAL) dataset
<span class="p">-</span> <span class="gs">**SOURCE**</span>: where the data came from
<span class="p">-</span> <span class="gs">**DESC**</span>: brief <span class="p">[</span><span class="nv">`camelCase`</span><span class="p">](</span><span class="sx">https://en.wikipedia.org/wiki/Camel_case</span><span class="p">)</span> description of dataset
<span class="p">-</span> <span class="gs">**DATE**</span>: [dd-mm-YY] formatted date of when dataset last updated
<span class="p">-</span> <span class="gs">**suffix**</span>: [csv|xlsx|tsv|txt|etc.] file format abbreviation

<span class="gu">## Dataset dictionary</span>

Datasets currently present in data folder:
<span class="p">
-</span> <span class="sb">`OPN_RAW_Crispell_myHeightData_01-01-2020.csv`</span> - information about my height
</code></pre></div></div>

<hr />
<h2 id="--i-havent-provided-a-guide-for-replicating-the-work">- I haven’t provided a guide for replicating the work</h2>
<p>There are amazing tools that can go alongside a repository for documenting our projects. Unfortuanetly, I haven’t used them for the repositories associated with this project 😭. It is important to:</p>
<ul>
  <li>Have a clear and concise <a href="https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-readmes"><code class="language-plaintext highlighter-rouge">README.md</code></a> file in your repository that describes the codebase and sets out important things for the user to know. See some <a href="https://github.com/matiassingers/awesome-readme">great examples</a>! 💕</li>
  <li>Create a <a href="https://docs.github.com/en/communities/documenting-your-project-with-wikis/about-wikis">wiki</a> for more thorough documentation of your project. A wiki is a simple set of web pages that are directly linked to your repo. Here’s my one for <a href="https://github.com/JosephCrispell/homoplasyFinder/wiki">HomoplasyFinder</a>!</li>
  <li>Consider using tools like <a href="https://www.sphinx-doc.org/">sphinx</a> (in python) and <a href="https://cran.r-project.org/web/packages/roxygen2/vignettes/roxygen2.html">roxygen</a> (in R) for automatically generating project documentation that you can build on that you can host online (for example on <a href="https://github.blog/2016-08-22-publish-your-project-documentation-with-github-pages/">GitHub pages</a>)</li>
</ul>

<hr />
<h2 id="--a-lack-of-code-modularisation-and-documentation">- A lack of code modularisation and documentation</h2>
<p>My code is lacking some key features of reproducible coding (or what’s present could be improved):</p>

<ul>
  <li>Modularisation, breaking the code down into functions, libraries, and classes. By doing this we make our code more understandable and maintainable (read more about <a href="https://best-practice-and-impact.github.io/qa-of-code-guidance/modular_code.html">modular code</a>)</li>
  <li>Documenting my functions using docstrings (<a href="https://realpython.com/documenting-python-code/#documenting-your-python-code-base-using-docstrings">python</a> and <a href="https://josephcrispell.github.io/2021/07/26/creating-R-docstring.html">R</a> docstring help) that clearly describe what a function does, what its input parameters are, and what it provides as output</li>
  <li>Approaches to checking the code is working as expected:
    <ul>
      <li>Add in <code class="language-plaintext highlighter-rouge">if</code> statements and checks that the parameters are of the correct type, format, and structure with informative error messages</li>
      <li>Add in some simple <a href="https://towardsdatascience.com/unit-testing-in-r-68ab9cc8d211">unit testing</a> - this is code that you write to check your code is running as expected that we can run each time with change our code</li>
    </ul>
  </li>
</ul>

<hr />
<h2 id="--ive-used-full-file-paths-throughout-the-codebase-that-are-specific-to-my-now-non-existent-computer">- I’ve used full file paths throughout the codebase that are specific to my, now non-existent, computer</h2>
<p>I’ve used absolute file paths throughout my codebase, these are file paths that say specifically on <strong>my computer</strong> where the file is. It is always better to use <a href="https://www.itgeared.com/html-absolute-relative-path-links/#:~:text=Relative%20paths%20are%20very%20useful,not%20include%20the%20domain%20name.">relative paths</a> that aren’t specific to any compuyter but are relative to the location of the code.</p>

<h1 id="some-final-thoughts">Some final thoughts</h1>
<p>There are many more issues with the codebase. Making our projects more reproducible is always an area for development.</p>

<p>Hopefully each of my projects gets easier to reproduce and I’ll stress that it is a critical first step to make the code publicly available - being a part of the open-source community. (I’ve always loved this image below from the <a href="https://the-turing-way.netlify.app/welcome.html">The Turing Way: handbook to reproducible, ethical and collaborative data science</a> that illustrates this!)</p>

<p><img src="https://github.com/the-turing-way/the-turing-way/raw/main/book/figures/community.jpg" alt="Join the open source community (Image credit: Turing Way)" /></p>

<p>Don’t be afraid to put your code out there, it will always need improving but at least you are giving others the opportunity to see, use, adapt, and learn from it! ♻</p>]]></content><author><name></name></author><category term="reproduciblity" /><category term="woodchester" /><category term="coding standards" /><summary type="html"><![CDATA[It’s been a while (over a year) since my last blog post! I’m hoping to get back into the swing of writing them fairly regularly but we’ll see! 😄 In this post, I want to talk a little about reproducibility (how easy it is to repeat and adapt projects) and reflect on one of my own projects.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/reproducibility/miro_map.png" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/reproducibility/miro_map.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Carving a whale spoon</title><link href="https://josephcrispell.github.io/2022/04/18/spoon-carving.html" rel="alternate" type="text/html" title="Carving a whale spoon" /><published>2022-04-18T00:00:00+00:00</published><updated>2022-04-18T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2022/04/18/spoon-carving</id><content type="html" xml:base="https://josephcrispell.github.io/2022/04/18/spoon-carving.html"><![CDATA[<p>Taken me a while to get back to my blog and wanted to start with a quick one about a spoon that I whittled during my Christmas holiday. It’s a big whale (sort-of) spoon whittled out of seasoned sycamore planks.</p>

<p>The sycamore planks were cut by Dad using a portable sawmill a few years ago, so it is perfectly seasoned. Being a hard wood though, it was very difficult to whittle.</p>

<p>I used the following tools:</p>

<ul>
  <li>Whittling knife - great for the back!</li>
  <li>Spoon knife - great for finishing the bowl and it’s sharp to work on back</li>
  <li>Range of gouge chisels - these made cutting out the bowl much easier!!</li>
  <li>Brass back tenon saw - this is a really rigid saw with many fine teeth making it easier to make precise straight cuts</li>
  <li>Range of cloth backed sand paper</li>
</ul>

<p>The photos below show the process to create the spoon, I haven’t made one of these before and it had been a long time since I had even whittled anything, so it took me a long time! Thankfully, I got a lot of help from my brother Daniel who has <a href="https://www.etsy.com/uk/listing/851342680/handcarved-locally-sourced-highland?ref=shop_review">made and sold many beautiful whale spoons</a> 😊.</p>

<div class="slideshow-container">

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_1.jpg" style="width:100%" />
        <div class="slideCaption">Drawn the outline</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_2.jpg" style="width:100%" />
        <div class="slideCaption">Cut the sycamore plank</div>
    </div>

     <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_3.jpg" style="width:100%" />
        <div class="slideCaption">Roughly cut the spoon</div>
    </div>
    
    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_4.jpg" style="width:100%" />
        <div class="slideCaption">Started working on the bowl</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_5.jpg" style="width:100%" />
        <div class="slideCaption">More rough cuts on the back</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_6_tools.jpg" style="width:100%" />
        <div class="slideCaption">All the tools 🛠️</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_7.jpg" style="width:100%" />
        <div class="slideCaption">Wore heavy gloves for whittling the back</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_8.jpg" style="width:100%" />
        <div class="slideCaption">Back taking shape</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_9.jpg" style="width:100%" />
        <div class="slideCaption">Worked on the thumb dimple by the fire 🔥</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_10.jpg" style="width:100%" />
        <div class="slideCaption">Finished the whittling</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_11.jpg" style="width:100%" />
        <div class="slideCaption">Sanded the back</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_12.jpg" style="width:100%" />
        <div class="slideCaption">Sanded the bowl</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_13.jpg" style="width:100%" />
        <div class="slideCaption">Finished the sanding</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_14.jpg" style="width:100%" />
        <div class="slideCaption">Oiled the spoon</div>
    </div>

    <div class="mySlides fade">
        <img src="/assets/img/blog/whaleSpoon/whale_spoon_15.jpg" style="width:100%" />
        <div class="slideCaption">Finished! 🏁</div>
    </div>

    <a class="prevSlide" onclick="plusSlides(-1)">&#10094;</a>
    <a class="nextSlide" onclick="plusSlides(1)">&#10095;</a>

</div>

<p>I purposely made this spoon large and chunky but in the future, as my confidence builds, I’ll aim to make some smaller (possibly more functional!) spoons 🥄.</p>]]></content><author><name></name></author><category term="whittling" /><category term="wood" /><summary type="html"><![CDATA[Taken me a while to get back to my blog and wanted to start with a quick one about a spoon that I whittled during my Christmas holiday. It’s a big whale (sort-of) spoon whittled out of seasoned sycamore planks.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/whaleSpoon/whale_spoon_6_tools.jpg" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/whaleSpoon/whale_spoon_6_tools.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Creating an R function docstring</title><link href="https://josephcrispell.github.io/2021/07/26/creating-R-docstring.html" rel="alternate" type="text/html" title="Creating an R function docstring" /><published>2021-07-26T00:00:00+00:00</published><updated>2021-07-26T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2021/07/26/creating-R-docstring</id><content type="html" xml:base="https://josephcrispell.github.io/2021/07/26/creating-R-docstring.html"><![CDATA[<p>I’m trying to get better at using <a href="https://en.wikipedia.org/wiki/Docstring">docstrings</a> to document my R functions as I write them. A docstring is a block of comments that describe a function, its inputs and output. They are extremely useful tools to help your future self and others understand what your function is doing.</p>

<p>In R a simple template docstring for a function might look like this:</p>

<div class="language-r highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cd">#' Short title for function</span><span class="w">
</span><span class="cd">#'</span><span class="w">
</span><span class="cd">#' Longer description of the function</span><span class="w">
</span><span class="cd">#' @param first An object of class "?". Description of parameter</span><span class="w">
</span><span class="cd">#' @param second An object of class "?". Description of parameter</span><span class="w">
</span><span class="cd">#' @return Returns an object of class "?". Description of what the function returns</span><span class="w">
</span><span class="cd">#' @examples</span><span class="w">
</span><span class="cd">#' # Add some code illustrating how to use the function</span><span class="w">
</span><span class="n">my_new_function</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">first</span><span class="p">,</span><span class="w"> </span><span class="n">second</span><span class="p">){</span><span class="w">
	</span><span class="c1"># Some code here</span><span class="w">

	</span><span class="nf">return</span><span class="p">(</span><span class="n">something</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div></div>

<p>The <code class="language-plaintext highlighter-rouge">#'</code> tells R that this isn’t just any commented line, it is a commented line that forms part of a function’s docstring. The template above can be read by the <a href="https://cran.r-project.org/web/packages/roxygen2/vignettes/roxygen2.html">roxygen</a> R package and used to automatically build a package’s help documentation.</p>

<blockquote>
  <p>In RStudio use <code class="language-plaintext highlighter-rouge">Ctrl</code> + <code class="language-plaintext highlighter-rouge">alt</code> + <code class="language-plaintext highlighter-rouge">shift</code> + <code class="language-plaintext highlighter-rouge">R</code> with your cursor inside a function to automatically generate a template docstring! 🎁</p>
</blockquote>

<p>We can, and should ⚠️, use the template on any function we write. For example:</p>

<div class="language-r highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cd">#' Calculate mean value</span><span class="w">
</span><span class="cd">#'</span><span class="w">
</span><span class="cd">#' Given a numeric vector as input the function will calculate the mean value</span><span class="w">
</span><span class="cd">#' @param numeric_vector A numeric vector to be used to calculate the mean</span><span class="w">
</span><span class="cd">#' @return Returns a numeric value representing the mean of the numeric vector provided</span><span class="w">
</span><span class="cd">#' @examples</span><span class="w">
</span><span class="cd">#' values &lt;- c(1,2,3,4,5,6,4,3)</span><span class="w">
</span><span class="cd">#' mean_value &lt;- calculate_mean(numeric_vector)</span><span class="w">
</span><span class="n">calculate_mean</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">numeric_vector</span><span class="p">){</span><span class="w">
	
  </span><span class="c1"># Check the vector provided is numeric</span><span class="w">
  </span><span class="k">if</span><span class="p">(</span><span class="nf">is.numeric</span><span class="p">(</span><span class="n">numeric_vector</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kc">FALSE</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="n">numeric_vector</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="m">1</span><span class="p">){</span><span class="w">
    </span><span class="n">stop</span><span class="p">(</span><span class="s2">"The input provided was not a numeric vector!"</span><span class="p">)</span><span class="w">
  </span><span class="p">}</span><span class="w">

  </span><span class="c1"># Get the length of the vector</span><span class="w">
  </span><span class="n">n</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="n">numeric_vector</span><span class="p">)</span><span class="w">

  </span><span class="c1"># Calculate sum</span><span class="w">
  </span><span class="n">total</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="nf">sum</span><span class="p">(</span><span class="n">numeric_vector</span><span class="p">)</span><span class="w">

  </span><span class="c1"># Calculate mean</span><span class="w">
  </span><span class="n">mean_value</span><span class="w"> </span><span class="o">&lt;-</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="n">n</span><span class="w">

  </span><span class="nf">return</span><span class="p">(</span><span class="n">mean_value</span><span class="p">)</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></code></pre></div></div>

<p>While <a href="https://cran.r-project.org/web/packages/roxygen2/vignettes/roxygen2.html">roxygen</a> uses the docstring to build a function’s help documentation, the <a href="https://cran.r-project.org/web/packages/docstring/vignettes/docstring_intro.html">docstring</a> package allows you to use function docstrings on the fly! 🛩</p>

<blockquote>
  <p>Install (<code class="language-plaintext highlighter-rouge">install.packages("docstring")</code>) and load the docstring package (<code class="language-plaintext highlighter-rouge">library(docstring)</code>), put your docstring inside your function and run your function code, and then use the <code class="language-plaintext highlighter-rouge">?</code> in the normal way (<code class="language-plaintext highlighter-rouge">?my_function_name</code>) and the help documentation should magically appear!</p>
</blockquote>

<p>I still need to go back to previous code and add docstrings but hopefully any new code I write I’ll add them as I go and they’ll be a great help to me and others that use my code in the future!</p>]]></content><author><name></name></author><category term="R" /><category term="function" /><category term="docstring" /><summary type="html"><![CDATA[I’m trying to get better at using docstrings to document my R functions as I write them. A docstring is a block of comments that describe a function, its inputs and output. They are extremely useful tools to help your future self and others understand what your function is doing.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/docstring/thumbnail.svg" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/docstring/thumbnail.svg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Cleaning up emails in gmail with Google Apps Script</title><link href="https://josephcrispell.github.io/2021/01/03/cleaning-emails.html" rel="alternate" type="text/html" title="Cleaning up emails in gmail with Google Apps Script" /><published>2021-01-03T00:00:00+00:00</published><updated>2021-01-03T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2021/01/03/cleaning-emails</id><content type="html" xml:base="https://josephcrispell.github.io/2021/01/03/cleaning-emails.html"><![CDATA[<p>So Joanna was stuck with more than 17,000 unread emails in her gmail account and I’m struggling to handle it! 😕 So I figured I could use <a href="https://www.google.com/script/start/">Google Apps Script</a> code to help.</p>

<p>Google Apps Script is a programming language based on [javascript] that’s designed to work with the all the different Google apps (gmail, docs, sheets, etc.). I’ve <a href="https://josephcrispell.github.io/2020/06/20/google-scripts.html">previously</a> used Google Apps Script to work with Google sheets.</p>

<p>Today, I’ve been writing some code to remove all the unread ebay alerts from Joanna’s account. Here’s the function I wrote to do it:</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">removeOldEbayEmails</span><span class="p">(){</span>

  <span class="c1">// Define the search string to identify emails </span>
  <span class="kd">var</span> <span class="nx">searchString</span> <span class="o">=</span> <span class="dl">"</span><span class="s2">label:inbox is:unread from:ebay@ebay.co.uk older_than:1m subject:NEW!</span><span class="dl">"</span><span class="p">;</span>

  <span class="c1">// Get all threads matching search string</span>
  <span class="kd">var</span> <span class="nx">threads</span> <span class="o">=</span> <span class="nx">getThreadsMatchingSearch</span><span class="p">(</span><span class="nx">searchString</span><span class="p">);</span>

  <span class="c1">// Check whether thread search returned some results</span>
  <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">threads</span> <span class="o">!==</span> <span class="dl">'</span><span class="s1">undefined</span><span class="dl">'</span> <span class="o">&amp;&amp;</span> <span class="nx">threads</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
    
    <span class="c1">// Mark as read and remove threads</span>
    <span class="nx">moveThreadsToTrash</span><span class="p">(</span><span class="nx">threads</span><span class="p">);</span>
  <span class="p">}</span>  
<span class="p">}</span>
</code></pre></div></div>

<p>I built the <code class="language-plaintext highlighter-rouge">searchString</code> based gmail’s searching key words (loads of info <a href="https://support.google.com/mail/answer/7190?hl=en-GB">here</a>). The search string does the following:</p>
<ul>
  <li><code class="language-plaintext highlighter-rouge">label:inbox</code> - look in your inbox</li>
  <li><code class="language-plaintext highlighter-rouge">is:unread</code> - select unread emails</li>
  <li><code class="language-plaintext highlighter-rouge">from:ebay@ebay.co.uk</code> - select emails from <code class="language-plaintext highlighter-rouge">ebay@ebay.co.uk</code></li>
  <li><code class="language-plaintext highlighter-rouge">older_than:1m</code> - select emails older than 1 month</li>
  <li><code class="language-plaintext highlighter-rouge">subject:NEW!</code> - select emails with “NEW!” in the subject line</li>
</ul>

<p>I wrote the <code class="language-plaintext highlighter-rouge">getThreadsMatchingSearch()</code> function to use a search term to retrieve email threads (email receive/reply chains):</p>

<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">getThreadsMatchingSearch</span><span class="p">(</span><span class="nx">searchString</span><span class="p">){</span>

  <span class="c1">// Get all threads matching search string</span>
  <span class="k">try</span><span class="p">{</span>
    <span class="kd">var</span> <span class="nx">threads</span> <span class="o">=</span> <span class="nx">GmailApp</span><span class="p">.</span><span class="nx">search</span><span class="p">(</span><span class="nx">searchString</span><span class="p">);</span>
  <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">"</span><span class="s2">Found </span><span class="dl">"</span> <span class="o">+</span> <span class="nx">threads</span><span class="p">.</span><span class="nx">length</span> <span class="o">+</span> <span class="dl">"</span><span class="s2"> threads (conversations of emails) matching search string (</span><span class="dl">"</span> <span class="o">+</span> <span class="nx">searchString</span> <span class="o">+</span> <span class="dl">"</span><span class="s2">). Note that size limited to 500.</span><span class="dl">"</span><span class="p">);</span>
  <span class="p">}</span>
  <span class="k">catch</span><span class="p">(</span><span class="nx">error</span><span class="p">){</span>
    <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">"</span><span class="s2">Warning! Unable to complete email search: </span><span class="dl">"</span> <span class="o">+</span> <span class="nx">error</span><span class="p">.</span><span class="nx">message</span><span class="p">);</span>
  <span class="p">}</span> 

  <span class="k">return</span><span class="p">(</span><span class="nx">threads</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<p>The <code class="language-plaintext highlighter-rouge">getThreadsMatchingSearch()</code> function uses the <a href="https://developers.google.com/apps-script/reference/gmail/gmail-app#search(String)"><code class="language-plaintext highlighter-rouge">GmailApp.search()</code></a> function and wraps it in a try catch because Google sets a limit on the number of searches (see more info on the quotas <a href="https://developers.google.com/apps-script/guides/services/quotas">here</a>).</p>

<blockquote>
  <p>Importantly, the maximum number of threads returned is fixed at <code class="language-plaintext highlighter-rouge">500</code>. If you’ve got more than this to handle, you can use a trigger to set multiple runs of your function.</p>
</blockquote>

<p>Once I have retrieved the threads, I use this function to move them to the trash:</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">function</span> <span class="nx">moveThreadsToTrash</span><span class="p">(</span><span class="nx">threads</span><span class="p">,</span> <span class="nx">batchSize</span><span class="o">=</span><span class="mi">100</span><span class="p">){</span>

  <span class="c1">// Work through the threads in batches (100 is the limit that batch operations can work on)</span>
  <span class="k">for</span> <span class="p">(</span><span class="nx">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">j</span> <span class="o">&lt;</span> <span class="nx">threads</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">j</span> <span class="o">+=</span> <span class="nx">batchSize</span><span class="p">)</span> <span class="p">{</span>

    <span class="c1">// Select the current batch of threads</span>
    <span class="kd">var</span> <span class="nx">batch</span> <span class="o">=</span> <span class="nx">threads</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">j</span><span class="p">,</span> <span class="nx">j</span><span class="o">+</span><span class="nx">batchSize</span><span class="p">);</span>
    
    <span class="c1">// Move the threads to the trash</span>
    <span class="k">try</span><span class="p">{</span>
      <span class="nx">GmailApp</span><span class="p">.</span><span class="nx">moveThreadsToTrash</span><span class="p">(</span><span class="nx">batch</span><span class="p">);</span>
      <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">"</span><span class="s2">Moved </span><span class="dl">"</span> <span class="o">+</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">length</span> <span class="o">+</span> <span class="dl">"</span><span class="s2"> threads to trash.</span><span class="dl">"</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">catch</span><span class="p">(</span><span class="nx">error</span><span class="p">){</span>
      <span class="nx">Logger</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">"</span><span class="s2">Warning! Unable to move threads to trash: </span><span class="dl">"</span> <span class="o">+</span> <span class="nx">error</span><span class="p">.</span><span class="nx">message</span><span class="p">);</span>
    <span class="p">}</span>    
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>As with the <code class="language-plaintext highlighter-rouge">GmailApp.search()</code> function the <a href="https://developers.google.com/apps-script/reference/gmail/gmail-app#moveThreadsToTrash(GmailThread)"><code class="language-plaintext highlighter-rouge">GmailApp.moveThreadsToTrash()</code></a> function has a quota limit so I’ve wrapped it in a try catch. It also has a batch size limit of <code class="language-plaintext highlighter-rouge">100</code> so I’ve worked through the <code class="language-plaintext highlighter-rouge">threads</code> array in batches of 100 (thanks to <a href="https://gist.github.com/gene1wood/0f455239490e5342fa49">this</a> GitHub code).</p>

<p>If you think the above code would be useful, then you’ll need to do the following:</p>
<ul>
  <li>Open up the Google Apps Script <a href="https://script.google.com/home/start">editor</a> (click on the New blank project)</li>
  <li>Paste in the three functions above into the <code class="language-plaintext highlighter-rouge">Code.gs</code> script</li>
  <li>Change the <code class="language-plaintext highlighter-rouge">searchString</code> to suit your needs</li>
  <li>Test run the script to check all is well</li>
  <li>If needed, set a trigger to schedule (click the alarm symbol in the left menu) to running the <code class="language-plaintext highlighter-rouge">removeOldEbayEmails()</code> function multiple times (<em>note that there are quotas on the number of services you can use in a day, which I quickly ran into</em>)</li>
</ul>

<p>So far I’ve removed ~8,000 of Joanna’s emails and set my script to run overnight so hopefully I’ll a few more thousand overnight! 🎉 🗑️</p>]]></content><author><name></name></author><category term="google" /><category term="emails" /><category term="javascript" /><summary type="html"><![CDATA[So Joanna was stuck with more than 17,000 unread emails in her gmail account and I’m struggling to handle it! 😕 So I figured I could use Google Apps Script code to help.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/cleaningEmails/thumbnail.svg" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/cleaningEmails/thumbnail.svg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Windows Subsystem for Linux (Ubuntu WSL)</title><link href="https://josephcrispell.github.io/2020/12/21/ubuntu-wsl.html" rel="alternate" type="text/html" title="Windows Subsystem for Linux (Ubuntu WSL)" /><published>2020-12-21T00:00:00+00:00</published><updated>2020-12-21T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2020/12/21/ubuntu-wsl</id><content type="html" xml:base="https://josephcrispell.github.io/2020/12/21/ubuntu-wsl.html"><![CDATA[<p>Today I set about trying to bring a little <a href="https://www.linux.com/what-is-linux/"><code class="language-plaintext highlighter-rouge">linux</code></a> joy into my Windows work life. Although Windows 10 is the world’s most popular operating system, it isn’t mine! 😊</p>

<blockquote>
  <p>If you can, work in a linux, it’s free, <a href="https://opensource.com/resources/what-open-source">open-source</a>, and powerful - putting you in complete control of your computer. 💻 If you can’t, and you are stuck on a Windows 10 computer at work, then this post is for you!</p>
</blockquote>

<p>Microsoft, recognising the brilliance of linux, created the Windows Subsystem Linux (WSL) to allow a linux subsystem to work within your Windows computer. Essentially a linux terminal can be used to install and run linux tools on your Windows file system! 📁</p>

<p>Linux comes in many <a href="https://www.tecmint.com/linux-distro-for-power-users/">flavours</a>, one of the most popular (and my favourite) is <a href="https://ubuntu.com/">Ubuntu</a>.</p>

<p>Today, I am going to walk through the steps I took to install <a href="https://ubuntu.com/community/mission">Ubuntu</a> on my Windows 10 machine:</p>

<ol>
  <li>Enable the Windows Subsystem Linux (WSL) by typing the following into your command line (or powershell):
    <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
</code></pre></div>    </div>

    <p>⚠️ With the a Windows update after May 2020 run the following <strong>instead</strong>:</p>
    <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
</code></pre></div>    </div>
    <p><br /></p>
  </li>
  <li>Install the Ubuntu App - I couldn’t download the app via Microsoft store so I did the following:
    <ul>
      <li>Selected “Ubuntu 20.04” from <a href="https://docs.microsoft.com/en-us/windows/wsl/install-manual">here</a></li>
      <li>Then used the following command to install the file in the command line:
        <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> Add-AppxPackage .\[insertFileNameHereWithoutSuffix].appx
</code></pre></div>        </div>
        <p><br /></p>
      </li>
    </ul>
  </li>
  <li>
    <p>Start the Ubuntu app (I typed “Ubuntu” into the Windows search and double clicked Ubuntu to start)<br /><br /></p>
  </li>
  <li>
    <p>Create a username and password (doesn’t need to match Windows credentials)<br /><br /></p>
  </li>
  <li>Added <code class="language-plaintext highlighter-rouge">cd /mnt/c/Users/username/</code> to <code class="language-plaintext highlighter-rouge">.bashrc</code> so my Ubuntu terminal always starts in Windows file system (replace <code class="language-plaintext highlighter-rouge">"username"</code> with your Windows username). To edit your <code class="language-plaintext highlighter-rouge">.bashrc</code> file, open it with the following:
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> nano <span class="nt">-c</span> ~/.bashrc
</code></pre></div>    </div>
    <p>Make your way to the end of the file (use the down arrow or <code class="language-plaintext highlighter-rouge">Ctrl + V</code>). Add the following text:</p>
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="c"># Move to the Windows file system</span>
 <span class="nb">cd</span> /mnt/c/Users/username/
</code></pre></div>    </div>
    <p>Press <code class="language-plaintext highlighter-rouge">Ctrl + X</code> then <code class="language-plaintext highlighter-rouge">Y</code> to save changes and exit.</p>
  </li>
</ol>

<p>Now you’re all set! 👍</p>

<blockquote>
  <p>Note that you’ll find your Windows file system at <code class="language-plaintext highlighter-rouge">/mnt/c/Users/yourWindowsUsername/</code> - you’re welcome! 😉</p>
</blockquote>

<hr />

<p>Lastly, I wanted to use my Ubuntu terminal to work with <a href="https://git-scm.com/"><code class="language-plaintext highlighter-rouge">git</code></a> so I did a few extra steps:</p>

<blockquote>
  <p>Importantly, to work with git in the Windows Subsystem Linux, I needed to use <a href="https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/connecting-to-github-with-ssh">SSH based repositories</a>. This meant I had to make new clones of my repos, which were set up as https!</p>
</blockquote>

<ol>
  <li>Set up your global configuration parameters:
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nv">$ </span>git config <span class="nt">--global</span> user.name <span class="s2">"Joseph Crispell"</span>
 <span class="nv">$ </span>git config <span class="nt">--global</span> user.email <span class="s2">"myemail@hero.com"</span>
</code></pre></div>    </div>
    <p><strong>Skip to step 6. if you use SSH for git repositories on your Windows machine</strong>
 <br /></p>
  </li>
  <li>I created a new ssh key with the following:
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nb">cd</span> ~
 <span class="nb">mkdir</span> .ssh
 <span class="nb">cd</span> .ssh
 ssh-keygen <span class="nt">-t</span> ed25519 <span class="nt">-C</span> <span class="s2">"your_email@example.com"</span>
</code></pre></div>    </div>
    <p>Create a name for your key. I called mine <code class="language-plaintext highlighter-rouge">id_rsa</code>, not sure why!
 <br /></p>
  </li>
  <li>Added my SSH key to my ssh-agent:
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nb">eval</span> <span class="si">$(</span>ssh-agent <span class="nt">-s</span><span class="si">)</span>
 ssh-add ~/.ssh/id_rsa
</code></pre></div>    </div>
    <p>Change <code class="language-plaintext highlighter-rouge">id_rsa</code> to whatever you called your SSH key.
 <br /></p>
  </li>
  <li>Add the new SSH key to GitHub account:
    <ul>
      <li>Head to <a href="https://github.com/">github</a></li>
      <li>Click into your settings</li>
      <li>Select SSH and GPG keys</li>
      <li>Click “New SSH Key”</li>
      <li>Create a name for your SSH key and paste the contents of <code class="language-plaintext highlighter-rouge">id_rsa.pub</code> into the box provided
 <br /></li>
    </ul>
  </li>
  <li>Copy your SSH key from Ubuntu to Windows:
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nb">cd</span> ~/.ssh
 <span class="nb">cp </span>id_rsa<span class="k">*</span> /mnt/c/Users/username/.ssh/
</code></pre></div>    </div>
    <p>Again, remember to change <code class="language-plaintext highlighter-rouge">id_rsa</code> to whatever you named your key. You also need to change <code class="language-plaintext highlighter-rouge">"username"</code> in the path to your Windows username.
 <br /></p>
  </li>
  <li>[OPTIONAL] <em>If you already used ssh on your Windows computer for git repositories</em>, you can copy the Windows SSH key onto your Ubuntu system with:
    <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="nb">cd</span> ~
 <span class="nb">mkdir</span> .ssh
 <span class="nb">cd</span> .ssh
 <span class="nb">cp</span> /mnt/c/Users/username/.ssh/id_rsa<span class="k">*</span> <span class="nb">.</span>
</code></pre></div>    </div>
    <p>Remember to change <code class="language-plaintext highlighter-rouge">"username"</code> in the path to your Windows username.</p>
  </li>
</ol>

<p>I must admit that it took me much longer than it should of to figure out the above steps, despite a lot of useful resources online:</p>
<ul>
  <li><a href="https://ubuntu.com/tutorials/ubuntu-on-windows#1-overview">Install Ubuntu on Windows 10</a></li>
  <li><a href="https://docs.microsoft.com/en-us/windows/wsl/install-manual">Manually install Ubuntu App</a></li>
  <li><a href="https://peteoshea.co.uk/setup-git-in-wsl/">Set up git for Windows Subsystem Linux</a></li>
  <li><a href="https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/connecting-to-github-with-ssh">Connecting to GitHub with SSH</a></li>
</ul>

<p>For the moment, I’m mainly going to use my Ubuntu terminal for managing my <code class="language-plaintext highlighter-rouge">git</code> repositories. So really it isn’t too necessary but I wanted to get Ubuntu back into my life and the linux terminal is incredibly helpful for developing code and I’m sure to use it more and more from now on! 🐧 🎉</p>]]></content><author><name></name></author><category term="ubuntu" /><category term="windows" /><summary type="html"><![CDATA[Today I set about trying to bring a little linux joy into my Windows work life. Although Windows 10 is the world’s most popular operating system, it isn’t mine! 😊]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/ubuntuWSL/thumbnail.png" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/ubuntuWSL/thumbnail.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Data ethics in practice with reveal.js</title><link href="https://josephcrispell.github.io/2020/11/27/data-ethics-in-practice.html" rel="alternate" type="text/html" title="Data ethics in practice with reveal.js" /><published>2020-11-27T00:00:00+00:00</published><updated>2020-11-27T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2020/11/27/data-ethics-in-practice</id><content type="html" xml:base="https://josephcrispell.github.io/2020/11/27/data-ethics-in-practice.html"><![CDATA[<p>I had a great opportunity to talk about data ethics in practice at the <a href="https://www.scotlandis.com/blog/events/scotsoft-2020/">ScotSoft2020</a> conference. I decided this would be the perfect chance to try out the amazing <a href="https://revealjs.com/">reveal.js</a> html presentation framework.</p>

<p>I’ve tried plenty of different ways to create presentations (like <a href="https://office.live.com/start/powerpoint.aspx">powerpoint</a>, <a href="https://bookdown.org/yihui/rmarkdown/ioslides-presentation.html">Rmarkdown</a>, <a href="https://www.google.co.uk/slides/about/">google slides</a> and <a href="https://www.latextemplates.com/cat/presentations">LaTeX</a>) and I’m always keen to try out new (ideally <a href="https://opensource.com/resources/what-open-source">open-source</a>) tools.</p>

<p>The <a href="https://revealjs.com/">reveal.js</a> framework represents a wacky new way to create presentations - by writing them directly in <a href="https://www.w3schools.com/html/html_intro.asp"><code class="language-plaintext highlighter-rouge">html</code></a> and it is completely free and open-source!!🎉</p>

<p>Here are the <a href="https://josephcrispell.github.io/standalone/data-ethics-in-practice/index.html">slides</a> I put together:</p>
<iframe src="https://josephcrispell.github.io/standalone/data-ethics-in-practice/index.html#/" width="100%" height="480" style="border:none;">Browser not compatible.</iframe>

<p><br />
Here’s a sneak peak at the <a href="https://github.com/JosephCrispell/reveal.js/blob/master/data-ethics-in-practice/index.html"><code class="language-plaintext highlighter-rouge">index.html</code></a> that makes the above presentation:</p>

<div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="cp">&lt;!doctype html&gt;</span>
<span class="nt">&lt;html&gt;</span>
	<span class="nt">&lt;head&gt;</span>
		<span class="nt">&lt;meta</span> <span class="na">charset=</span><span class="s">"utf-8"</span><span class="nt">&gt;</span>
		<span class="nt">&lt;meta</span> <span class="na">name=</span><span class="s">"viewport"</span> <span class="na">content=</span><span class="s">"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"</span><span class="nt">&gt;</span>

		<span class="c">&lt;!-- Set the presentation title --&gt;</span>
		<span class="nt">&lt;title&gt;</span>Data ethics in practice<span class="nt">&lt;/title&gt;</span>

		<span class="c">&lt;!-- Add a favicon--&gt;</span>
		<span class="nt">&lt;link</span> <span class="na">type=</span><span class="s">"image/x-icon"</span> <span class="na">rel=</span><span class="s">"shortcut icon"</span> <span class="na">href=</span><span class="s">"logos/DataScienceCampus_logo_square.png"</span> <span class="nt">/&gt;</span>

		<span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"../dist/reset.css"</span><span class="nt">&gt;</span>
		<span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"../dist/reveal.css"</span><span class="nt">&gt;</span>
		<span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"../dist/theme/black.css"</span> <span class="na">id=</span><span class="s">"theme"</span><span class="nt">&gt;</span>

		<span class="c">&lt;!-- Theme used for syntax highlighted code --&gt;</span>
		<span class="nt">&lt;link</span> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"../plugin/highlight/monokai.css"</span> <span class="na">id=</span><span class="s">"highlight-theme"</span><span class="nt">&gt;</span>
	<span class="nt">&lt;/head&gt;</span>
	<span class="nt">&lt;body&gt;</span>
		<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"reveal"</span><span class="nt">&gt;</span>
			<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"slides"</span><span class="nt">&gt;</span>
				
				<span class="c">&lt;!-- Slide 1: title --&gt;</span>
				<span class="nt">&lt;section</span> <span class="na">data-background=</span><span class="s">"branding/background.png"</span><span class="nt">&gt;</span>

					<span class="nt">&lt;div</span> <span class="na">style=</span><span class="s">"position: fixed; top: 750%; box-shadow: 0 1px 4px rgba(0,0,0,0.5), 0 5px 25px rgba(0,0,0,0.2); background-color: rgba(0, 0, 0, 0.75); color: #fff; padding: 20px; font-size: 20px; text-align: left;"</span><span class="nt">&gt;</span>
						<span class="nt">&lt;h2&gt;</span>Data ethics in practice<span class="nt">&lt;/h2&gt;</span>
						<span class="nt">&lt;p&gt;</span>Created by <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">"https://josephcrispell.github.io/"</span><span class="nt">&gt;</span>Joseph Crispell<span class="nt">&lt;/a&gt;</span> using <span class="nt">&lt;a</span> <span class="na">href=</span><span class="s">"https://revealjs.com/"</span><span class="nt">&gt;</span>reveal.js<span class="nt">&lt;/a&gt;&lt;/p&gt;</span>
					<span class="nt">&lt;/div&gt;</span>

					<span class="nt">&lt;aside</span> <span class="na">class=</span><span class="s">"notes"</span><span class="nt">&gt;</span>
                        Hi everyone<span class="nt">&lt;br&gt;&lt;br&gt;</span>
                        ...

</code></pre></div></div>

<p><br />
I’ve also put a recording of my presentation on <a href="https://www.youtube.com/embed/vcZUtzn3z70">youtube</a>:</p>
<iframe width="100%" height="480" src="https://www.youtube.com/embed/vcZUtzn3z70" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>

<p><br />
I’m not great at writing <code class="language-plaintext highlighter-rouge">html</code>, so it took me a while to get used to the reveal.js framework. Once I did though, I found it amazing and I learnt a lot! 💡</p>

<p>If you’re keen to try itself then head over the <a href="https://revealjs.com/">reveal.js</a> page. Also, here’s the <a href="https://github.com/JosephCrispell/reveal.js/tree/master/data-ethics-in-practice">code</a> for my data ethics presentation on GitHub.</p>]]></content><author><name></name></author><category term="ethics" /><category term="reveal.js" /><summary type="html"><![CDATA[I had a great opportunity to talk about data ethics in practice at the ScotSoft2020 conference. I decided this would be the perfect chance to try out the amazing reveal.js html presentation framework.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/dataEthicsInPractice/thumbnail.png" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/dataEthicsInPractice/thumbnail.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Getting started with GitHub Actions</title><link href="https://josephcrispell.github.io/2020/10/30/github-actions.html" rel="alternate" type="text/html" title="Getting started with GitHub Actions" /><published>2020-10-30T00:00:00+00:00</published><updated>2020-10-30T00:00:00+00:00</updated><id>https://josephcrispell.github.io/2020/10/30/github-actions</id><content type="html" xml:base="https://josephcrispell.github.io/2020/10/30/github-actions.html"><![CDATA[<p>Today is the day I’ve started using <a href="https://docs.github.com/en/free-pro-team@latest/actions">GitHub Actions</a>. I found the following two resources really helpful:</p>

<ul>
  <li><a href="https://github.com/r-lib/actions/tree/master/examples">Workflows specifically designed for R code</a></li>
  <li><a href="https://www.rostrum.blog/2020/08/09/ghactions-pkgs/">A great blog post about R workflows in action</a></li>
</ul>

<p>GitHub Actions allow you to create workflows that will execute tasks on a certain action, like <a href="https://docs.github.com/en/free-pro-team@latest/github/using-git/pushing-commits-to-a-remote-repository"><code class="language-plaintext highlighter-rouge">push</code></a>ing changes onto GitHub.</p>

<p>Each time you complete an action (<code class="language-plaintext highlighter-rouge">push</code> changes to your repository), the workflow will be executed. For example, today I’ve added a workflow (<a href="https://github.com/r-lib/actions/tree/master/examples#quickstart-ci-workflow">Quickstart Continuous Integration workflow</a>) to my <a href="https://github.com/JosephCrispell/basicPlotteR">basicPlotteR</a> package, which does the following:</p>

<ul>
  <li>Boots up a mac OS computer</li>
  <li>Checks R is installed</li>
  <li>Installs two packages:
    <ul>
      <li><a href="https://cran.r-project.org/package=remotes"><code class="language-plaintext highlighter-rouge">remotes</code></a> for installing packages from GitHub</li>
      <li><a href="https://cran.r-project.org/web/packages/rcmdcheck/index.html">rcmdcheck</a> - for checking R code</li>
    </ul>
  </li>
  <li>Installs my <a href="https://github.com/JosephCrispell/basicPlotteR">basicPlotteR</a> package</li>
  <li>Runs <code class="language-plaintext highlighter-rouge">rcmdcheck()</code> from the <a href="https://cran.r-project.org/web/packages/rcmdcheck/index.html">rcmdcheck</a> package, which:
    <ul>
      <li>Checks all the function documentation</li>
      <li>Runs all the examples for the functions to check they work</li>
      <li>Checks all functions dependencies</li>
      <li>Checks the R package structure</li>
      <li>And many more!!</li>
    </ul>
  </li>
</ul>

<p>With this workflow in place, every time I edit the code for my <a href="https://github.com/JosephCrispell/basicPlotteR">basicPlotteR</a> package, the above checks will be run and I’ll know straight away if I have caused any problems!</p>

<p>GitHub Actions require a <a href="https://www.tutorialspoint.com/yaml/yaml_basics.htm">YAML</a> formatted file to define the workflow. The <em>Quickstart Continuous Integration workflow</em> (available <a href="https://github.com/r-lib/actions/tree/master/examples#quickstart-ci-workflow">here</a>) looks like this:</p>
<div class="language-yaml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Define when the action is to be triggered</span>
<span class="na">on</span><span class="pi">:</span>
  <span class="na">push</span><span class="pi">:</span>
    <span class="na">branches</span><span class="pi">:</span>
      <span class="pi">-</span> <span class="s">main</span>
      <span class="pi">-</span> <span class="s">master</span>
  <span class="na">pull_request</span><span class="pi">:</span>
    <span class="na">branches</span><span class="pi">:</span>
      <span class="pi">-</span> <span class="s">main</span>
      <span class="pi">-</span> <span class="s">master</span>

<span class="na">name</span><span class="pi">:</span> <span class="s">R-CMD-check</span>

<span class="na">jobs</span><span class="pi">:</span>
  <span class="na">R-CMD-check</span><span class="pi">:</span> <span class="c1"># Name of job</span>
    <span class="na">runs-on</span><span class="pi">:</span> <span class="s">macOS-latest</span> <span class="c1"># Define OS you want to run action on, you can have multiple!</span>
    <span class="na">steps</span><span class="pi">:</span>

      <span class="c1"># Checkout (download) repository </span>
      <span class="pi">-</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">actions/checkout@v2</span>

      <span class="c1"># Install R</span>
      <span class="pi">-</span> <span class="na">uses</span><span class="pi">:</span> <span class="s">r-lib/actions/setup-r@v1</span>
      
      <span class="c1"># Install required packages</span>
      <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Install dependencies</span>
        <span class="na">run</span><span class="pi">:</span> <span class="pi">|</span>
          <span class="s">install.packages(c("remotes", "rcmdcheck"))</span>
          <span class="s">remotes::install_deps(dependencies = TRUE)</span>
        <span class="na">shell</span><span class="pi">:</span> <span class="s">Rscript {0}</span>
      
      <span class="c1"># Run the rcmdcheck function on R package repository</span>
      <span class="pi">-</span> <span class="na">name</span><span class="pi">:</span> <span class="s">Check</span>
        <span class="na">run</span><span class="pi">:</span> <span class="s">rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "error")</span>
        <span class="na">shell</span><span class="pi">:</span> <span class="s">Rscript {0}</span>
</code></pre></div></div>

<p>I saved the above YAML code in a file called <a href="https://github.com/JosephCrispell/basicPlotteR/blob/master/.github/workflows/R-CMD-check.yaml"><code class="language-plaintext highlighter-rouge">.github/workflows/R-CMD-check.yaml</code></a> in my <a href="https://github.com/JosephCrispell/basicPlotteR">basicPlotteR</a> repository. Note, that I had to create the <code class="language-plaintext highlighter-rouge">.github</code> and <code class="language-plaintext highlighter-rouge">workflows</code> folders and explicitely push them onto GitHub using <code class="language-plaintext highlighter-rouge">git add .github/workflows/R-CMD-check.yaml</code>.</p>

<p>With the workflow pushed online, GitHub automatically started running the workflow!</p>

<p><img src="/assets/img/blog/githubActions/thumbnail.png" alt="actions" /></p>

<p>As the above screenshot shows, the code for my <a href="https://github.com/JosephCrispell/basicPlotteR">basicPlotteR</a> package needed some tidying up to get it to pass the <code class="language-plaintext highlighter-rouge">rcmdcheck()</code> checks! 😨</p>

<p>But I got there in the end!!! 🎉</p>

<p><img src="/assets/img/blog/githubActions/badge.png" alt="actions" /></p>

<p>I added the status badge to the repository <a href="https://github.com/JosephCrispell/basicPlotteR/blob/master/README.md"><code class="language-plaintext highlighter-rouge">README.md</code></a> using the following markdown code in the top line:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>[![R build status](https://github.com/JosephCrispell/basicPlotteR/workflows/R-CMD-check/badge.svg)](https://github.com/JosephCrispell/basicPlotteR/actions?workflow=R-CMD-check)
</code></pre></div></div>
<p>The path to the <code class="language-plaintext highlighter-rouge">badge.svg</code> file is in my repository but you’ll find no trace of it - I am not sure why this is but the badge is working, so I’ll not question it! 🤔</p>

<p>Well, I’ve been amazed by the power of <a href="https://docs.github.com/en/free-pro-team@latest/actions">GitHub Actions</a> and I’ve only just started! I can’t wait to introduce more complex checks and add workflows to all my repositories!!!</p>

<p>Oh, and Happy Halloween!!! 🧡</p>]]></content><author><name></name></author><category term="github" /><category term="actions" /><category term="basicplotteR" /><summary type="html"><![CDATA[Today is the day I’ve started using GitHub Actions. I found the following two resources really helpful:]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://josephcrispell.github.io/assets/img/blog/githubActions/thumbnail.png" /><media:content medium="image" url="https://josephcrispell.github.io/assets/img/blog/githubActions/thumbnail.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>