Scientist working in the Center for Computational Science & e-Systems, Japan Atomic Energy Agency.

Spotted an error on this site?
Corrections appreciated, please email alex (at) alexmalins.com or leave a comment.


Installing PyNE on Windows via WSL

Although PyNE is officially only supported for Linux and Mac, it is possible to build and use it on Windows using the Windows Subsystem for Linux (WSL) and the Ubuntu terminal app from the Microsoft Store. These instructions describe how to install a basic PyNE build (no MOAB, DAGMC or OpenMC interfaces) on Windows via WSL. They presume only some basic knowledge on using Linux and Windows command lines. They were written for Windows 10 (1909), Ubuntu 20.04 and PyNE 0.7.3 – mileage for other releases may vary.

Step 1 – Enable WSL in Windows

Open Windows PowerShell with administrator privileges (right click: Run as administrator) and run:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

This enables WSL 1 in Windows. Now restart your PC.

(Or you could continue to update to WSL 2 at this point by following these instructions. WSL 2 offers more advanced features that might be useful to you, such as GPU support, but they are not necessary for PyNE).

Step 2 – Install Ubuntu 20.04 LTS from the Microsoft Store

From the Microsoft Store app, search for Ubuntu and install the Ubuntu 20.04 LTS app.

Step 3 – Run Ubuntu 20.04 LTS and install libraries needed by PyNE

Run the Ubuntu 20.04 LTS app for the first time from the Windows Start menu, wait for it to set itself up, then enter a UNIX username and a password. Run the following commands in the terminal:

sudo apt update
sudo apt upgrade
sudo apt install python3-pip build-essential cmake gfortran libblas-dev liblapack-dev libeigen3-dev libhdf5-dev hdf5-tools
pip3 install --user numpy==1.17.4 scipy cython nose tables matplotlib future

This will install the libraries needed for a basic build of PyNE.

Now edit the .bashrc file using a text editor (nano, emacs, vim etc.) and add the following commands to the end of the file:

alias pip=pip3
alias python=python3
export PATH="$HOME/.local/bin:$PATH"
export LD_LIBRARY_PATH="$HOME/.local/lib:/usr/lib/x86_64-linux-gnu/hdf5/serial:$LD_LIBRARY_PATH"

Close the Ubuntu terminal.

Step 4 – Download the PyNE source code

Open your favourite browser in Windows and download a .tar.gz file containing the PyNE source code from https://github.com/pyne/pyne/releases. The latest release at the time of writing is 0.7.3.

Step 5 – Copy the source code to Ubuntu, unpack, and build PyNE

Reopen a Ubuntu 20.04 LTS terminal window and copy the source tarball into the Ubuntu WSL file system via:

cp /mnt/c/DOWNLOAD_DIR/pyne-0.7.3.tar.gz .

Edit the drive letter and DOWNLOAD_DIR as appropriate to match where you downloaded the source file to. Unpack the source code and navigate into the source directory via:

tar -xf pyne-0.7.3.tar.gz
cd pyne-0.7.3/

Now build PyNE and install locally:

python setup.py install --user

Subject to build being successful, you can now run the following commands to make the nuclear data:


An optional final step here is to run the tests:

cd pyne-0.7.3/tests/
Step 6 – Using PyNE in Python on WSL

You should now be able to use PyNE within Python on the Ubuntu 20.04 LTS app, e.g.:

>>> from pyne import data
>>> data.half_life("H-3")

These instructions were based on the PyNE Ubuntu Install Scripts from https://github.com/pyne/install_scripts. An alternative starting from Step 3 onwards would be to download and run the ubuntu.sh install script available from that repository. This will install MOAB, DAGMC and OpenMC as well.

Another alternative that might work well, although I have not tested it, is to install Anaconda on WSL, then install PyNE from conda-forge.

PyNE can be a little finicky to install, but persistence pays off. If you encounter any errors during builds, the Issues and PR sections of the PyNE Github repo, and the pyne-dev and pyne-users mailing lists are great sources of information.

Interactive rebase to switch base branches mid GitHub pull request

A situation occurred the other week that I had not encountered before when working on a GitHub pull request for the PyNE Python package. The PR (#1270) added a new function to initialize materials by supplying activities of radionuclides. This was to complement existing methods to create materials using masses or atom fractions.

The new lines of code were in my branch for the PR which was forked from the default develop branch on PyNE’s GitHub repository. Unbeknown to me at the time, the PyNE team were undertaking a hackathon to prepare for a new PyNE release, and active development was ongoing on a separate branch in the main repository (0.7.0-rc). Normally everything would be brought in line easily by rebasing the branch for PR (#1270) to the 0.7.0-rc branch ready for merging the PR.

In this instance, however, there had previously been another PR (#1264) merged into the develop branch subsequent to 0.7.0-rc being branched off. This meant that after rebasing my branch to 0.7.0-rc, the new PR (#1270) branch contained both the commits for the new activity-based material initializations and the irrelevant PR (#1264) commits. Schematically it looked like this:

origin develop branch: A---C
                        \   \
new PR (#1270) branch:   \   C-D
                          \   /
origin 0.7.0-rc branch:    A-B

After rebasing to 0.7.0-rc, the head branch for PR (#1270) presented both the C commits from PR (#1264) and the D commits associated with the new activity-to-material functionality.

The way to tidy this up so that only the relevant D commits for the new functionality were part of PR (#1270) was to perform a git interactive rebase:

git rebase -i HEAD~x

Interactive rebase, among other things, lets you track back and delete previous git commits from the code lineage. Note x here is the number of commits you choose to track back and process during the interactive rebase. With interactive rebase, the C commits could be deleted from the head branch for PR (#1270), leaving only the relevant D commits, thus tidying everything up.

Credit goes to Paul Wilson and Baptiste Mouginot from the University of Wisconsin-Madison for guidance on how to use git interactive rebase to perform this fix.

Updating the version of a port on MacPorts

This post broadly follows the MacPorts howto/Upgrade guide: https://trac.macports.org/wiki/howto/Upgrade

If you come across a piece of software on MacPorts where the version is out of date, here’s how to update it. First open up a terminal window and identify the port’s maintainer:

port info --maintainers <portname>

If the port has a maintainer, easiest is to send them a polite email requesting the port is brought up to date.

If the port has no maintainer (nomaintainer@macports.org), or allows open maintenance (openmaintainer@macports.org), you can upgrade the port manually yourself with the following steps. If successful, you can then submit a new ticket to http://trac.macports.org/ in order for the update to be propagated onto the MacPorts server for the benefit of other users.

First change directory into the port folder:

cd $(port dir <portname>)

Backup the port build file:

sudo cp Portfile Portfile.orig

Use a text editor to edit the software version in the Portfile to the latest version number (e.g. version 1.4.1 to 1.5):

sudo nano Portfile

If the Portfile revision entry is not set to 0, set it to 0 as this is the first revision for the updated version. Now download the source code of the latest version of the port and find its checksums with the following command:

sudo port -d checksum <portname>

Copy the checksums (rmd160 and sha256) that are printed out for the for the latest version and paste them into place over the old checksums in the Portfile. Similarly, if the Portfile contains a livecheck.md5 entry, fetch this for the latest version and propagate it into the Portfile:

sudo port -d livecheck <portname>

You can now compile and install the latest version on your local machine by:

sudo port -d install <portname>

If the compile is successful and the updated software functions correctly, you can then file an update. First create a diff file for the changes to the Portfile:

diff -u Portfile.orig Portfile | sudo tee <portname>.diff

Then file a new ticket at http://trac.macports.org/. Give the ticket a title like:

<portname> @<current_version>: update to <latest_version>

Cc in the email address coupled to the port which you identified at the beginning of this process. Attach the <portname>.diff file. All being well one of the MacPorts team will use your patch and update the port to the latest version on the MacPorts server within the following days.

Converting a scanned TIFF document to PDF and creating text searchable PDFs

You can follow these steps to create a text searchable PDF document if your scanner only outputs TIFF files. If your scanner creates PDF files but doesn’t perform OCR to make text searchable, skip to the last step.

Convert TIFF to PDF

ImageMagick comes with a command line tool magick to do this.

magick convert scanned.tiff scanned.pdf

Executing this command creates a PDF file from a TIFF created by a scanner.

Optional: Rotate the PDF Pages

Sometimes the scanned pages will need rotating to the correct orientation. Use PDFtk to rotate the pages. Rotating all the pages in the scanned PDF by 90º anti-clockwise is achieved with the following command:

pdftk scanned.pdf cat 1-endwest output rotated.pdf

Individual pages can be selected and rotated as necessary, see PDFtk examples.

Perform Optical Character Recognition

For this step I resort to a copy of Acrobat Pro.

It would have been nice if I had succeeded in achieving good quality output for this step using open source software. Solutions do exist, mainly using Tesseract to do OCR and then forming a new PDF file with a text searchable layer hidden underneath the scanned images. See e.g. Voelkel’s and OCRmyPDF solutions.

However despite reasonable stabs, for various reasons I couldn’t succeed with either. The quality of the OCR output I was getting from Tesseract was lower than Acrobat. Also Acrobat offers the advantage that it performs small rotations to the pages to make sure the text is horizontal. So eventually I gave up on the open source route and now use Acrobat.

Note Acrobat can perform OCR on any PDF file. This is very useful for making old journal articles text searchable if the download offered by the publisher is not.


[Edit: 24 August 2018 – Updated ImageMagick command “convert” to “magick convert”]

Remove pesky PDF title pages and other PDF tricks


PDFtk command to remove a title page

Taking out pages from PDF files, binding PDFs together, and removing PDF security. Here are a couple of free programmes to perform these tasks.

Taking out pages from a PDF

Sometimes PDFs contain a title page or blank pages that you might want to remove. PDFtk Server is a useful command line tool that can take pages out from a PDF file. To remove the title page from a PDF file, run the following command:

pdftk input.pdf cat 2-end output output.pdf

Set the pages you want to maintain within the document after the cat command, and change input.pdf and output.pdf to suit.

Merging PDF files

You may wish to combine a journal article with its supplementary information, or with comment letters and author responses. Again PDFtk can do this. To combine two pdf files into one:

pdftk input1.pdf input2.pdf cat output combined.pdf

PDFtk can also perform a host of other useful tasks – see its examples page.

Removing PDF Security

Sometimes PDF files come with encryption settings that prevent you from doing the above, or from performing OCR on the document. I find that QPDF works well for removing the security settings, even if you do not know the PDF password. To remove the security settings from a file run:

qpdf --decrypt input.pdf output.pdf

However QPDF does not always succeed. An alternative is to use a PDF printer driver to ‘print’ an unencrypted PDF. Examples of such software for Windows include CutePDF, Bullzip, PDFCreater etc. Preview on Mac will also ‘Export as PDF’. Ghostscript can also perform this task:

gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf -c 
.setpdfwrite -f input.pdf

More Powerful Manipulation of PDFs

The python package pyPdf can be used to perform PDF file operations with the benefit of easy scripting ability. For instance when merging many hundreds of PDF sections of a larger document, each with a filename describing one part of the document (front matter, initial pages with Roman numerals, main pages…), I wrote a python script to merge the files in the correct order.

Edit 9th June 2015

Added ghostscript command to print to a new PDF file.