Skip to content

Instantly share code, notes, and snippets.

View stefanschmidt's full-sized avatar

Stefan stefanschmidt

  • Hamburg, Germany
View GitHub Profile
@stefanschmidt
stefanschmidt / ndtri_75th_percentile.py
Created May 12, 2024 17:42
Compute the 75th percentile of the standard normal distribution with ndtri algorithm
# Approximate the median of the standard half-normal distribution by
# computing the 75th percentile of the standard normal distribution using
# the algorithm from the original implementation of scipy.special.ndtri
#
# Based on the code from https://stackoverflow.com/questions/41338539#41338979
# which was based on the original implementation in ndtri.c
s2pi = 2.50662827463100050242E0
P0 = [
@stefanschmidt
stefanschmidt / realpath_verbose.md
Last active August 13, 2023 09:16
Resolve symbolic link chains to files and containing folders step-by-step

Gradually resolving symbolic link chains

Let's say one wants to know where pdflatex is located.

$ type pdflatex
pdflatex is /Library/TeX/texbin/pdflatex

We can see that this path is a symbolic link to pdftex.

$ ls -ld /Library/TeX/texbin/pdflatex

@stefanschmidt
stefanschmidt / fix-page-labels-pdf.sh
Created June 20, 2023 11:22
Fix logical page numbering of a PDF document
# cover (1 page)
# table of contents (14 pages)
# result: C1,i,ii,...,xiv,1,2,...
pip install pagelabels
python -m pagelabels --delete doc.pdf
python -m pagelabels --startpage 1 --prefix C doc.pdf
python -m pagelabels --startpage 2 --type 'roman lowercase' doc.pdf
python -m pagelabels --startpage 16 --type arabic doc.pdf
@stefanschmidt
stefanschmidt / extract_fonts_from_pdf.sh
Created June 10, 2023 19:26
Extract embedded fonts from a PDF file as OpenType file
# show information about pdf resources (shows font names used in PDF)
mutool info file.pdf
# extract font and image resources
mutool extract file.pdf
# convert a CFF file (Compact Font Format) to OTF format (OpenType Font)
fontforge -lang=ff -c 'Open($1); Generate($1:r + ".otf")' font.cff
@stefanschmidt
stefanschmidt / linear_regression_numpy.md
Created May 15, 2023 17:58
Simple linear regression in NumPy

Simple linear regression in NumPy

The verbosity of performing a simple linear regression in the current stable release has increased by an astonishing amount.1

In the documentation it is recommended to not use [numpy.polyfit][polyfit].

As noted above, the poly1d class and associated functions defined in numpy.lib.polynomial, such as numpy.polyfit and numpy.poly, are considered legacy and should not be used in new code. Since NumPy version 1.4, the numpy.polynomial package is preferred for working with polynomials.

For polynomial regression, the the documentation [recommends][polynomials] to replace [numpy.polyfit][polyfit] with [numpy.polynomial.polynomial.Polynomial.fit][polynomial].

@stefanschmidt
stefanschmidt / read_csv.md
Created May 15, 2023 17:56
Reading a CSV file with Python

Reading a CSV file with Python

For anything < 50 MB

import csv

with open("data.csv") as fp:
    reader = csv.reader(fp, delimiter=",", quotechar='"')
    csv_data = [row for row in reader]
@stefanschmidt
stefanschmidt / bbc_subtitles_to_plain_text.md
Created May 10, 2023 21:27
Convert BBC subtitles to plain text

Convert BBC subtitles to plain text

Install prerequisites

brew install youtube-dl
pip install pysrt beautifulsoup4
pip install --pre ttconv

Download the subtitles

@stefanschmidt
stefanschmidt / match_text_div.md
Last active May 5, 2023 11:31
Filter rule for matching a text in a div tag with an apostrophe/quote/quotation mark with uBlock Origin

Test with Chrome 112.0.5615.137 and uBlock Origin 1.49.2

Doesn't work (hides all divs)

www.google.com##div:has-text("It looks like there aren't many great matches for your search")
www.google.com##div:has-text(/It looks like there aren\'t many great matches for your search/)

Works

Use a more specific filter rule

Possibly because it narrows down the number of divs to consider.

@stefanschmidt
stefanschmidt / chromiumos_on_macos.md
Created May 1, 2023 00:37
Running ChromiumOS on macOS

Running ChromiumOS on macOS

The ChromiumOS distribution CloudReady was discontinued some time after Google acquired the company that offered it. The successor is ChromeOS Flex but there don't seem to be any official virtual machine images available.

Downloading the virtual machine image

The CloudReady OVA file mentioned in the knowledge base article is available on the Wayback Machine (direct download).

$ sha256sum CloudReady-Home-v83-x64.ova

7e4c7304c4b7363dfd778d3a58765c4d03fcd9fb3a1e5ae97c2582b42e4b66ad CloudReady-Home-v83-x64.ova

@stefanschmidt
stefanschmidt / anti_clickjacking.user.js
Created April 6, 2023 16:35
Restore cmd+click to open an article in a new tab on MIT Technology Review
// ==UserScript==
// @name Anti-Clickjacking
// @namespace http://tampermonkey.net/
// @version 1.0
// @description restore cmd+click on article links to open new tab
// @author Stefan
// @match https://www.technologyreview.com/
// @icon https://www.technologyreview.com/static/media/favicon.1cfcdb44.ico
// @grant none
// ==/UserScript==