Filed Under #coding

Finding cell neighbours in an ISEA3H global grid in dggridR

Finding cell neighbours in an ISEA3H global grid in dggridR In order to model a spatially discretised process on the whole Earth, we need a discrete global grid system (DGGS). Ideally, the DGGS would divide the globe into a tessellation of some regular polygon, analogous to a grid of squares on the plane, where the grid resolution can be increased by further subdivision of the polygon. Another desirable property would be to have equal-distant centres, such as a hexagonal grid, so that the neighbourhood of a grid cell is well defined. Unfortunately, it is not possible to tessellate a sphere with hexagons only, and there are mathematical limits to e.g....

Example debugging mixed Python C++ in VS Code

Example debugging mixed Python C++ in VS Code Visual Studio Code has the ability to debug mixed Python with C++ extensions. In this blog post, I give an example of how to get it working. I’m going to do the example from scratch in five steps: Make virtual environment. Chances are that, if you’re doing this kind of thing, you’ll be wanting to use a virtual environment too. Write code. My toy example is a C++ extension that just adds two numbers together. Set up VS Code project. Important to point the interpreter to virtual environment. Create launch.json. Configure the debugger so it can both run on python...

Extinction of undiscovered butterflies + tutorial

Extinction of undiscovered butterflies + tutorial Meryl Theng just had a new paper published in Biological Conservation, where she estimated that 46% of Singapore’s butterfly species have been extirpated since 1854. The special thing about this estimate is that it includes all species that existed, including species that went extinct before we had a chance to discover them. The trick to estimating undiscovered extinctions is the SEUX model. There is a nice write-up about the paper on Ryan’s blog. The paper has also received a good response in the press - The Straits Times and The Star have covered it - and it is generating some...

Pretty diagram of directory structure

Pretty diagram of directory structure In a recent project, I wanted to create a diagram of a repository for the appendix of a paper, including comments to highlight key files and explain how the folders were organised. I found this answer on Stack Exchange by user Gonzalo Medina, which I tweaked to produce the diagram below. An annotated directory structure. The code is below: % Adapted from https://tex.stackexchange.com/a/270761 \documentclass[tikz,border=5mm]{standalone} \usepackage[edges]{forest} \definecolor{foldercolor}{RGB}{124,166,198} \newcommand{\size}[2]{ {\fontsize{#1}{0}\selectfont#2}} \definecolor{blue}{RGB}{0,50,200} \newcommand{\comment}[0]{\color{blue} \rm} \tikzset{pics/folder/.style={code={ \node[inner sep=0pt, minimum size=#1](-foldericon){}; \node[folder style, inner sep=0pt, minimum width=0.3*#1, minimum height=0.6*#1, above right, xshift=0.05*#1] at (-foldericon.west){}; \node[folder style, inner sep=0pt, minimum size=#1] at (-foldericon.center){};} }, pics/folder/.default={20pt}, folder...

Import self-made Sage modules and functions into Sage script?

Update Jan 2020: E.M. Bray suggests a more elegant solution in the comments, see: https://ask.sagemath.org/question/7867/importing-sage-files/?answer=48947#post-id-48947 - Let’s say that I have created a bunch of Sage code - Sage functions etc. - that I wish to reuse by importing into various Sage scripts. In straight Python, one would put these functions into their own separate file and import them using the usual from foo import funcname as localfuncname type procedure. It is unclear to me what the proper procedure for doing the same in Sage is - one can’t use the same syntax for a .sage file, and for example...

"ValueError: expected a DNF expression" when trying espresso_exprs example from pyeda docs

I’ve recently been working on a qualitative modelling project where I am trying to uncover “truths” about the response of species in an ecosystem to control of invasive species. Long story short, I’ve been looking into various boolean minimisation techniques. I’ve been playing with Python EDA, a Python library that I think provides a front-end to the Robert Brayton and Richard Rudell espresso heuristic logic minimiser, developed at University of California, Berkeley. I was trying out the examples on the Two-level Logic Minimisation docs page, and I had no issues with the second ‘Minimise truth tables’ example. However for the...

A reminder for later

The Automatic Differentiation package in Haskell can do some interesting things. Prelude Numeric.AD> jacobian ([x, y] -> [x*x*y, 5*x + sin y]) [1, 1] [[2.0,1.0],[5.0,0.5403023058681398]]

Add period to end of last author (Biology Letters .bst)

I had an issue today where I wanted to create a custom .bst file for Biology Letters that would format as follows: Parmesan C, Yohe G. 2003 A globally coherent fingerprint of climate change impacts across natural systems. Nature 421, 37–42. I was using makebst, incrementally editing my .dbj file, but I couldn’t seem to get that ‘.’ on the end of the author list to come out. The problem seemed to be some interaction between the default for blocks and authors. My options for author were %<>PUNCTUATION AFTER AUTHORS: So going with the default invoked (I think) this section of...

libXmu.so.6 and libgthread-2.0.so.0 error, or getting Wolfram CDF Player working on Ubuntu

Recently a colleague sent me some information on his model and a bunch of Mathematica .nb files. I don’t use Mathematica (if I felt the need for that kind of thing I’d go for Sage) and I don’t have access to it, so I started looking around for a way to export the notebook, say to pdf. I found an online nb to pdf converter hosted by Wolfram called NBtoPDF, so I uploaded the smallest file (quite a few M) while I kept searching for a solution. I’m glad I did keep searching, because after a good deal of time...

Basic mercurial commands

I’ve started using the Mercurial version control system. I like that it doesn’t leave a whole heap of files around, just one .hg directory. The commands to get started are fairly straightforward: hg init . hg status: Look at what is included or not. hg add *: Add everything in the directory. hg commit: Here you can enter some initial comment about the repository hg view: This allows you to look at the history of the repository through a GUI

Installing Sphinx

Many times I have found someone’s log of an installation of some new software incredibly useful, so here’s my log from installing Sphinx a while ago. First I followed the instructions in the Sphinx tutorial here, namely: Installed using sphinx-quickstart sphinx-build -b html sourcedir builddir make html This gave me a basic html page with no content I Created a python program with some rst in the docstrings, called intercept.py, which went into the source directory I added these lines to ./source/index.rst: .. automodule:: intercept :members: I also added the following lines to ./source/conf.py: * sys.path.insert(0, os.path.abspath('.')): So it could...

Webworld for Python

Webworld for Python A typical Webworld food web I’ve finished up a draft module for Webworld (Drossel et al. 2001, J. Theor. Biol.) in Python that’s available for download: webworld.py It’s also available bundled with what you’ll need for an example run: webworld.tar.gz.

An example linear programming problem in Octave

Tools for solving linear programming problems are useful to me because the necessary condition for permanence in a Lotka-Volterra system can be reduced to a linear programming problem (Jansen 1987, J. Math. Biol.; Law & Morton 1996, Ecology). Below, I’ve adapted an example from Tommi Sotinen’s ORMS 1020 lecture notes (p. 24-38) to demonstrate how to solve a linear programming problem in Octave. – Giapetto’s Woodcarving, Inc., manufactures two types of wooden toys: peace-keepers and trains. A peace-keeper sells for $27 and uses $10 worth of raw materials. Each peace-keeper that is manufactured increases Giapetto’s variable labor and overhead costs...