Scientific Computing

Generate vectors of datetime in Python

Generating a range of datetime data is a common data analysis and simulation task. Here we show examples of generating datetime vectors for Python datetime and numpy.datetime64

Python datetime

Python datetime is often used as timezone-naïve with UTC as the assumed timezone. This custom avoids ambiguities when working with Pandas and Numpy, which are foundational for Python data science.

Generate a range of Python datetime like:

from __future__ import annotation
from datetime import datetime, timedelta


def datetime_range(start: datetime, end: datetime, step: timedelta) -> list[datetime]:
    """like range() for datetime"""
    return [start + i * step for i in range((end - start) // step)]


dt = datetime_range(datetime(2019, 12, 1), datetime(2020, 4, 1), timedelta(days=1))

Numpy datetime64

Numpy datetime64 generates a range of times like:

dt = numpy.arange('2019-12-01', '2020-04-01', dtype='datetime64[D]')

Pandas has the date_range function to generate time vectors.

Matlab pcolor datetime plots

Matlab datetime works much like Python datetime, and is generally recommended. Matlab plotting functions generally support “datetime” class. It is sometimes necessary to manipulate Matlab plots involving datetime for the desired result.

dt = datetime(2019, 12, 1):datetime(2020, 3, 1);

dat = rand([100, length(dt)]);
y = 1:size(dat, 1);

pcolor(dt, y, dat)

If the plot doesn’t have the desired datetime axis formatting, try datetick.

datetick('x', 'yyyy-mm-dd', 'keepticks')

Generate vector of datetime. Python matplotlib supports datetime.datetime and numpy.datetime64 in most plot functions.

Fix Matlab graphics error after driver update

After updating an operating system graphics driver while Matlab was open, an error may occur upon plotting in Matlab like:

MATLAB has experienced a low-level graphics error, and may not have drawn correctly. Read about what you can do to prevent this issue at Resolving Low-Level Graphics Issues then restart MATLAB.

The solution to this issue typically is, as the message suggests, just restarting Matlab. A full reboot is not always necessary, but try that if the error persists.

Skip test with return code 77

Skipping a test with Meson or CMake by returning error code 77 is a de facto practice. Sometimes it is only feasible to know a test should be skipped by attempting to run that test.

Meson build system accepts return code 77 by default as a signal to skip the test. Configure CMake to skip return code:

add_test(NAME MyTest COMMAND mytest)  # arbitrary

set_property(TEST MyTest PROPERTY SKIP_RETURN_CODE 77)

Check temperature of Raspberry Pi CPU

The vcgencmd utility allows reading a few dozen hardware measurements on the Raspberry Pi boards.

CPU Temperature is checked by:

vcgencmd measure_temp

Typical temperatures in office environment, with case:

Pi Model heatsink usage temp [C]
2B yes light-moderate 40..45
4B no light-moderate 65..70
4B no YouTube 720p60 85

A red thermometer icon GPU-superimposed on the Raspberry Pi display output means the Raspberry Pi is overheating and is throttling the CPU and GPU to avoid self-destruction.

Raspberry Pi 0, 1, 2, 3 temperature thresholds:

CPU temp. [C] icon throttle
< 80 none none
80 - 85
half full thermometer red
CPU
> 85
full thermometer red
CPU & GPU

Raspberry Pi 4 temperature thresholds:

CPU temp. [C] icon throttle
80 - 85 none CPU: 1000 MHz
85 - 90
half full thermometer red
CPU
> 90
full thermometer red
CPU & GPU

The current Raspberry Pi CPU clock speed is obtained from

vcgencmd measure_clock arm

The output is in units of Hertz. The Raspberry Pi CPU clock speed and power consumption is dynamic:

clock speed [MHz] Raspi 2 Raspi 3 Raspi 3+ Raspi 4
idle 600 600 600 600
100% one or more cores 900 1200 1400 1500

Log temperature measurements with crontab -e. This can periodically log temperature and CPU frequency, e.g. add a line like:

@hourly  vcgencmd measure_temp | /usr/bin/logger

logger writes the measured parameters into the system log.


Related: Monitor Raspberry Pi DC input voltage

Create a blank/orphan Git branch

Many basic Git use cases involve a main branch with feature branches periodically merged into the main branch. For certain purposes, totally distinct branch without a common history can exist in the same Git repo. One of the most common uses of this is for documentation. For example, GitHub will build a website from the gh-pages branch.

Setup blank Git branch

Do NOT force push during this procedure, you may accidentally erase years of work!

This example assumes you want to create a gh-pages empty branch for documentation on GitHub, but will of course work for other purposes too.

From the repo directory create a blank Git branch:

git switch --orphan gh-pages

Remove the unneeded files from this branch (by default, all existing files are staged from the previous branch)

git rm --cached -r .

git clean -id

This leaves the .git/ directory, which should not be disturbed.

Copy documentation files

What happens next depends on if your documentation files were already added to another branch (tracked) or were not added to Git (untracked). Assume wanted files for the blank gh-pages branch are in docs/ on main branch.

copy over the files to gh-pages

git checkout main -- docs/

git commit -am "moved docs"

Upload files

  1. push the documentation

    git push -u origin gh-pages
  2. Enable the documentation builds from github.invalid/username/myrepo/settings → GitHub Pages under Source select gh-pages.

  3. In a few minutes, the webpages at username.github.io/myrepo/ should be active.

Once everything is working, the old docs/ folder isn’t needed.

Python f-string benchmarks

Python 3.6 f-strings have been shown to be the fastest string formatting method in microbenchmarks by Python core dev Raymond Hettinger in relative speed factors:

  • f-string: 1.0
  • concatenate string: 1.33
  • join sequence of strings: 1.73
  • %s formatting operator: 2.40
  • .format() method: 3.62
  • Template() method: 11.48

The reason for this speedy performance was described by Python core dev Serhiy Storchaka.

Goldwave audio editor on Linux WINE

Goldwave 5.x is stable and the recommended version to use on Linux via WINE. An open source alternative to Goldwave is Audacity. If using Goldwave on Windows directly:

winget install --id=GoldWave.GoldWave

To use Goldwave on Linux via WINE:

Install Goldwave prereqs. 32-bit WINEPREFIX is mandatory due to wmp10 prereq.

WINEPREFIX=~/.wine32 WINEARCH=win32 winetricks wmp10

This creates a 32-bit winearch (default .wine directory is 64-bit). The default install options are fine.

Install Goldwave in WINE.

WINEPREFIX=~/.wine32 wine gwave5*.exe

This also creates a Goldwave icon in the Ubuntu apps menu.

Configure soundcard in Goldwave by:

  1. F11 key → Control Properties → System tab
  2. click Use DirectSound AP radio button
  3. click OK to save configuration

Select “loopback” what you hear audio under F11 → Device → Record and pick Loopback Pulseaudio.

Test Goldwave 5 by F11 key → Device tab and click Test playback button to hear a brief test tone. Create new/save/play sound files with Goldwave 5.7 in Linux WINE

These settings are useful for Goldwave on Windows as well as WINE/Linux.

  • Constantly monitor audio levels: F11 → Record → monitor input
  • label VU graph axes in dB: Right click VU meter → Properties → Show Axis

It’s recommended to use Goldwave 5.x as above on WINE instead. There are some errors but Goldwave 6 still works to record and playback on WINE ≥ 2 using a Windows 7 Wineprefix.

  1. Download Goldwave 6
  2. configure Wineprefix for Windows 7 using winecfg → Applications → Windows Version → Windows 7
  3. Install Goldwave 6 on WINE
wine InstallGoldwave6*.exe

As of WINE 2.1, error upon opening Goldwave 6 include

Floating point overflow

and there are also

Unhandled exception

on closing Goldwave 6 and it will keep asking to reset Goldwave settings. Nonetheless Goldwave 6 does record/play on WINE 2.1.

git pull after remote forced update

When collaborating in teams with Git, someone else may do a “force push” on a feature branch, that conflicts with local revisions previously pulled. Here are a few simple scenarios to resolve this situation quickly. For simplicity, in this article we assume work in a Git branch feat1.

First, make a copy of the Git repo in case a mistake is made.

No new local work

Erase local changes in feat1 and match the remote Git repo.

git switch feat1

git pull --rebase

update and preserve local work

To preserve work in feat1:

git switch feat1

git fetch

git reset origin/feat1 --soft

The work can be committed as usual after the reset.

Git bug for Windows nanorc

Git 2.24.1.windows.2 seems to have a bug in git.nanorc within Git Bash. Specifically, the wrong (Windows/DOS) \r\n line endings seem to have been introduced. A simple fix is to use dos2unix to correct the line endings.

Open Git Bash using “Run as Administrator” via the Windows Start menu and type into this Terminal:

dos2unix /usr/share/nano/git.nanorc

Symptom: Git operations using the “nano” editor, such as git commit without the -m option or git rebase -i invoke errors as below. This is despite the git.nanorc matching templates as old as 2016. The problem is, in Git 2.24.1.windows.2, the line endings were accidentally set to DOS \r\n instead of \n.

/usr/share/nano/git.nanorc on line 1: Regex strings must begin and end with a " character
" not understoodare/nano/git.nanorc on line 2: Command "
" not understoodare/nano/git.nanorc on line 10: Command "
" not understoodare/nano/git.nanorc on line 15: Command "
Error in /usr/share/nano/git.nanorc on line 19: Regex strings must begin and end with a " character