Dual display notes LaTeX Beamer presentation

I used Pympress to deliver my Ph.D. defense talk slides using Beamer in dual screen. Be sure you practice ahead of time with YOUR laptop.

Install

  1. install the prereqs for your system:

  2. install Pympress:

    python -m pip install --user pympress

Present

Show the Beamer presentation in dual screen by running:

pympress talk.pdf
  • tap the s key to swap screens

LaTeX setup

In your LaTeX .tex preamble (top of your main .tex file), put this code to make dual-screen PDF work:

\usepackage{pgfpages}
\setbeameroption{show notes on second screen}

Notes

Linux error fixes

ModuleNotFoundError: No module named ‘gi’

  1. install missing prereqs

    apt install python3-gi python3-cairo gir1.2-poppler
  2. Be sure you’re using SYSTEM Python, not Anaconda python or other user Python installs. Remove pympress and pympress*distinfo directories from ~/anaconda3/lib/python3.6/site-packages/pympress* or wherever it might be under your ~ directory. Remove ~/anaconda/bin/pympress and then:

    /usr/bin/pip3 install --user pympress

optimal Conda environment setup

From Conda 4.4 onward, and especially Conda ≥ 4.6, it’s recommended to NOT simply add the Anaconda bin directory to your PATH, as this can cause library version conflicts.

If you have already installed Miniconda or Anaconda, simply type:

conda init --all

reopen Terminal / Command Prompt to use.

Install Anaconda Python in Windows PowerShell

Conda 4.6 added official support for Python in PowerShell. They also improved support in general for a variety of shells, so it’s worth updating conda in any case.

  1. Update to latest conda by from Command Prompt / Terminal:

    conda update conda
  2. setup the new shell support (PowerShell, Bash, Command Prompt, etc.) with

    conda init --dry-run --verbose

    if you’re OK with the changes, make them happen for real by:

    conda init
  3. reopen shell(s) to see new conda environment.

PowerShell Permissions

You may get an error upon opening PowerShell like:

Documents\WindowsPowerShell\profile.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170

If it’s OK for your computing environment, consider ExecutionPolicy like:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Matlab / GNU Octave "isinteractive" function

It’s useful to know if the Matlab or GNU Octave GUI is open for a number of use cases, including

  • pause for each group of a large set of plots–only if user is there to look at them, otherwise save to disk and close thereafter.
  • increase (or decrease) verbosity of print statements or if console output is logged, depending on if it batch mode or not.

We don’t use the Matlab ≥ R2019a batchStartupOptionUsed as it doesn’t detect the -nodesktop case often used for unattended batch processing. Save this code to isinteractive.m for your project.

%!assert(islogical(isinteractive))
function isinter = isinteractive()
 %% tell if the program is being run interactively or not.
 % helpful to say pause after making groups of plots--only if user has GUI desktop open.
 % don't use batchStartupOptionUsed as it neglects the "-nodesktop" case
persistent inter;

if isempty(inter)
    if isoctave
      inter = isguirunning;
    else % matlab, this test below doesn't work for Octave
      inter = usejava('desktop');
    end
end

isinter=inter; % has to be a separate line/variable for matlab

end

Python subprocess on Windows, Mac and Linux

Paths to executables for Python subprocess on Windows and other operating systems are handled cleanly as follows.

A simple boilerplate method to handle these issues follows, typically put in __init__.py for your package or other suitable location

#!/usr/bin/env python
from pathlib import Path
import os

# directory of this Python .py file
R = path(__file__).resolve().parent
# wherever your executable is
EXE = R / '..' / 'build' / 'myprogram'

if os.name == 'nt':
    EXE = EXE.with_suffix('.exe')

if not EXE.is_file():
	raise FileNotFoundError(EXE)


def myfunc():

	# cast EXE to str() in general with subprocess
    cmd = [str(EXE), myparam1, myparam2]

    subprocess.check_call(cmd)

Use ** instead of pow in Python

In Python, x**y is much faster than:

Julia is more than 5 times faster than Python at scalar exponentiation, while Go was in-between Python and Julia in performance.

Python

The test was run on Python 3.5, 3.6 and 3.7 with similar results. In all cases, the timing was the same for integer or float base or exponent.

Python testing done with:

  • Python 3.7.2
  • Ipython 7.4.0
  • Numpy 1.16.1

** operator

The ** operator in Python also has the advantage of returning int if inputs are int and arithmetic result is integer.

%timeit 10**(-3)

18.8 ns ± 0.263 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

pow()

%timeit pow(10, -3)

490 ns ± 5.62 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

math.pow()

%timeit math.pow(10, -3)

572 ns ± 10.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

numpy.power()

Numpy is known in general to be slower at scalar operations than Python-native operators and Python built-in math. But of course Numpy is generally a lot faster and easier for N-dimensional array operations.

%timeit numpy.power(10, -3)

3.85 µs ± 440 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Julia

Julia 1.1.0 was likewise benchmarked for reference on the same computer. First we installed Julia BenchmarkTools:

import Pkg
Pkg.add("BenchmarkTools")

The Julia wallclock time for exponentiation was the same for float and int as with Python.

using BenchmarkTools

function caret(x, y)
  z = x ^ y

  return z
end

o = @benchmark caret(10, -3.)
println(minimum(o).time, " nanoseconds")

3.1 nanoseconds

Go

Create a file my_test.go containing:

package main

import ("math"
        "testing")

func BenchmarkPower(b *testing.B) {
    for i := 0; i < b.N; i++ {
        math.Pow(10, -3)
    }
}

then run

go test -bench=Power

Will result in something like:

BenchmarkPower-4        20000000                61.9 ns/op

go benchmark reference

Turning off Connected Standby on Windows 10

Disable always-on Windows 10 Wifi by changing:

System → Power & Sleep → Network Connection → Never

Notes

Advantages of disabling Windows Connected Standby:

  • save battery by not having your computer connected to Wifi while sleeping.
  • not connecting to possibly rogue AP as you travel about, or being subject to tracking.
  • some hardware (e.g. Microsoft Surface Pro) often fails to connect to Wifi on wakeup until manually going to Device Manager and disable/enable cycling the Wifi adapter.

Writing multipage TIFF with Python

An easy, robust way to write multipage TIFF on any platform in Python is imageio For all examples:

imgs.shape
(Nimg, y, x) for monochrome. (Nimg, y, x, 3) for RGB color.
python -m pip install imageio
import imageio

imageio.mimwrite('myimgs.tiff',imgs)

tifffile

ImageIO uses tifffile internally, so most don’t need to use tifffile directly. To use tifffile directly, install tifffile.py:

python -m pip install tifffile

Example

import tifffile

tifffile.imsave('myimages.tiff',imgs)

tifffile.imsave() is capable of description and tags arguments and to compress losslessly.

Advanced Python TIFF multi-page writing example: archive/old_tiffile_demo.py.

Read TIFF headers

The de facto TIFF header tags. can be read from the command line with Perl Image::ExifTool

apt install libimage-exiftool-perl

exiftool myfile.tif

Note: tiffinfo doesn’t print tiff tags.

Print all TIFF tags from Python using archive/PrintTiffTags.py

Other methods

Alternative multipage-Tiff method using scikit-image and FreeImage: (we recommend imageio or tifffile instead)

from skimage.io._plugins import freeimage_plugin as freeimg

freeimg.write_multipage(imgs,'myimages.tiff')

Fix scikit-image errors

Due to the large number of image libraries invoked, sometimes scikit-image needs a little tweaking for image I/O:

Windows

if you get error:

RuntimeError: Could not find a FreeImage library

Fix by:

  1. download the FreeImage binaries
  2. extract Dist/x64/FreeImage.dll to
   $(python -c "import skimage; print(skimage.__path__[0])")/io/_plugins/

Linux

If you get error:

freeimage had a problem: Could not find a FreeImage library in any of…

Fix by:

apt install libfreeimage3

reference

Old Numpy

I worked around this issue by

python -m pip install numpy

python -m pip install tifffile

Since there is a Numpy .whl for ARM, the latest Numpy installs quickly on Raspberry Pi.

This fixes tifffile error:

RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa

Using Bash scripts from Windows

For certain use cases, it’s feasible to run a Bash script from within Windows. The obvious ways to do this are via Cygwin and Windows Subsystem for Linux. However, there is a third way to run Bash scripts from within Windows itself, not needing Cygwin or Windows Subsystem for Linux. In this example, we rely on the Bash shell installed with Git on Windows.

The trick is to start the Bash script you want to run from Linux or Windows with the shebang (first line of Bash script file):

#!/bin/bash; C:/Program\ Files/Git/user/bin/sh.exe

This tells the shell (Linux or Windows) to first try /bin/bash which is a Unix-like path, and then try the Git Bash shell on Windows. If Python is on your Windows Path, you can even use Bash scripts that also invoke Python scripts. This is very handy in general.

Install packages in GNU Octave

GNU Octave can install third-party packages in a friendly way, analogous to the Matlab App Store or how Linux repositories work. Regardless of operating system, Octave can install these extension packages from the Octave command line.

Install GNU Octave

Install Packages

Some packages require a compiler or libraries. If you get an error on installing a package, look at the error to see if you need to install a system library.

Packages are installed at the Octave command prompt, and download automatically. Prereqs are not automatically installed, but messages are given telling which package needs to be installed first. signal is a perfect example of this, given below.

Signal

A very popular package is the signal package, which brings many functions found in Matlab’s DSP and Communications Toolbox. We’ll see that signal needs other packages first; let’s walk through the Octave signal install. All commands are from Octave command prompt.

  1. Try using a command that requires signal

    diric(0.2, 5)

    warning: the ‘diric’ function belongs to the signal package from Octave Forge which seems to not be installed in your system.

    • if I had already installed signal, but forgotten to load it since I started Octave, the error would have been:

      warning: the ‘diric’ function belongs to the signal package from Octave Forge which you have installed but not loaded.

  2. install signal Octave package

    pkg install -forge signal

    The -forge option indicates to use the Octave-Forge repo for automatic download.

    This returns a warning saying that control is required.

  3. Install control:

    pkg install -forge control

    this requires the gfortran compiler. If you get a message saying you need gfortran:

    • Linux: apt install gfortran
    • Mac: brew install gcc
    • Windows installer
  4. now signal will install:

    pkg install -forge signal

Using packages

Use Octave packages in a Matlab-compatible way simply by enclosing in try end

function d = twicediric(x)
  try
    pkg load signal
  end

  d = 2*diric(x)
end

If the package isn’t installed for someone’s Octave, they’ll get the error message on reaching the missing function telling them which package they need to install.