Scientific Computing

Find largest directories one-liner

Find the directories consuming the most hard drive space.

Whole hard drive big file search:

du -hd1 / 2>/dev/null | sort -rh | head

Home directory big file search:

du -hd1 ~ 2>/dev/null | sort -rh | head

“ncdu” is a widely-available graphical Ncurses based program showing the largest files. NCDU is more user-friendly than the plain text method above. NCDU allows deleting files interactively. When using on WSL, specify the desired drive like (for C: in this example):

ncdu /mnt/c

JAVA_HOME environment variable

Java programs like ImageJ may refuse to start because they cannot find JAVA_HOME environment variable. Be sure Java JRE is installed.

apt install default-jre

Try commands:

which default-java

suppose it’s at /usr/lib/jvm/default-java, then:

JAVA_HOME=/usr/lib/jvm/default-java imagej

where imagej is the Java program of interest. If the command now works, in ~/.profile set

export JAVA_HOME=/usr/lib/jvm/default-java

non-sudo Homebrew on Linux

Homebrew is for Linux and macOS, allowing install of the latest compilers and other packages without sudo.

Use the Homebrew install script without sudo.

Install desired packages such as Gfortran:

brew install gcc

Generally the latest compilers are available from Homebrew for Linux and macOS.

Instead of putting Homebrew environment variables into ~/.profile, consider putting them into ~/brew.sh activate them when needed, instead of constantly being active. Either choice is fine of course.

Create ~/brew.sh containing:

PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
MANPATH="/home/linuxbrew/.linuxbrew/share/man:$MANPATH"
INFOPATH="/home/linuxbrew/.linuxbrew/share/info:$INFOPATH"
HOMEBREW_EDITOR="nano"

To use Homebrew, type

source ~/brew.sh

Upon closing the terminal, the configuration falls back to default (non-Homebrew)

Prolific driver version on Windows

Some older Prolific-based USB interfaces–such as used to program two-way radios–can fail to be recognized on Windows upon first plugin. Looking in Windows Device Manager, the device shows up with a yellow caution sign, and an error message like:

This device cannot start. (Code 10) {Not Enough Quota} Not enough virtual memory or paging file quota is available to complete the specified operation.

Usually the fix is to install an older version of the Prolific driver that is less sensitive to Prolific chip version. We have experienced that the Prolific 3.6 - 3.8 drivers are subject to this issue.

We can’t vouch for the older Prolific 3.2 drivers available but they do work.

Easy install PyPy3

PyPy:

  • allows running Python code up to multiple times faster.
  • particularly suited for heavy “pure Python” workloads.
  • does not significantly help for workloads heavy in Numpy array operations, for example.
  • slower to start, so if a program isn’t “big enough”, PyPy can be slower than standard CPython.
  • available for Linux, macOS and Windows.
  1. download and extract PyPy3 for your operating system.
  2. add the pypy3 directory containing the PyPy3 executable to the PATH and reopen Terminal

Install/upgrade Pip so that you can install other packages:

pypy3 -m ensurepip

pypy3 -m pip install --upgrade pip setuptools wheel

Many PyPI packages can be installed with pip as usual. On Windows, the appropriate version of Microsoft Visual Studio may be required to compile packages from source.

pypy3 -m pip install numpy

Some packages don’t work yet easily, particularly on Windows where the compilers and prerequisite libraries may need to be manually built.

Lossless convert PNG image stack to PDF

Converting an image to a PDF file with ImageMagick can be as simple as:

magick in.png out.pdf

For single images -page letter can leave big margins, which may not be desirable.

Convert image stack to PDF: one PNG image per page:

magick -page letter -adjoin *.png joined.pdf
-page letter
does not upsize images, so tiny images will be tiny on the page, one image per page.

Print PDF from any Linux program

On Linux, the CUPS PDF program saves printed PDFs to the ~/PDF directory from any program.

apt install cups-pdf

The PDF output directory can be configured by editing the “Out” directory in /etc/cups/cups-pdf.conf

However, note that higher quality PDF can be obtained by using the PDF print options in the specific program, if available.


Related: convert image stack to PDF

Python pkg_resources.VersionConflict

When using Python project scripts, if one gets:

pkg_resources.VersionConflict: (mypkg 1.1.2 (~/mypkg), Requirement.parse(‘mypkg==1.1.0’))

This can come from an old script in Python bin directory, e.g. ~/miniconda3/bin. Check in the directory coming from:

python -c "import sys; print(sys.executable)"

After finding the old scripts, delete them (be sure they’re not something needed).

Example: suppose package “mypkg” previously contained myprog.py and now the project is moving to the more robust entry_points technique like

[project.scripts]
myprog = mypkg.__main__:cli

Fix by deleting ~/miniconda3/bin/myprog.py.