Skip to content

Instantly share code, notes, and snippets.

@willprice
willprice / rand_index.py
Created December 17, 2020 10:57
Rand index computation
# Heavily inspired by the answer by Tom in https://stats.stackexchange.com/questions/89030/rand-index-calculation
def rand_score(labels_true, labels_pred):
"""
Args:
labels_true: Array of shape :math:`(N)` denoting the class identities of each element.
labels_pred: Array of shape :math:`(N)` denoting the cluster identities of each element.
Returns:
(Unadjusted) Rand index.
@willprice
willprice / mp4_tools.py
Last active October 20, 2020 08:19
Convert video array to mp4 blob
from io import BytesIO
import av
import numpy as np
def video_array_to_mp4_blob(video: np.ndarray, fps: float = 24) -> bytes:
"""
Args:
video: Video array of shape :math:`(T, H, W, 3)` and range :math:`[0, 255]` with type ``np.uint8``.
@willprice
willprice / setup.md
Last active October 1, 2020 21:56
Pi headless setup

RPi headless set up

Set up SD card

$ sudo  dd if=2020-08-20-raspios-buster-armhf.img of=/dev/sda bs=1M
$ sudo sync
  • Pop out and pop SD card back in.
@willprice
willprice / csv_points_to_segments.py
Last active February 20, 2022 19:27
Convert CSV to SRT subtitles
import argparse
from pathlib import Path
import numpy as np
import pandas as pd
parser = argparse.ArgumentParser(
description="",
@willprice
willprice / HTML boilerplate.html
Created September 14, 2020 09:32
HTML boilerplate
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>title</title>
</head>
<body>
</body>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>title</title>
</head>
<body>
</body>
@willprice
willprice / relative_symlink.py
Created August 5, 2020 13:47
Create relative symlinks in python
@willprice
willprice / get_row_ranks.py
Last active June 19, 2020 13:54
Obtain ranks for each row in a 2D array in numpy
import numpy as np
def get_row_ranks(xs):
"""
Args:
xs: array of shape :math:`(N, E)`
Returns:
Ranks for each row of ``xs``. A rank of 0 is the highest value, and a rank of ``E - 1`` is the lowest value.
@willprice
willprice / matplotlibrc
Last active June 11, 2020 12:48
Computer Modern matplotlibrc
#### MATPLOTLIBRC FORMAT
## This is a sample matplotlib configuration file - you can find a copy
## of it on your system in
## site-packages/matplotlib/mpl-data/matplotlibrc. If you edit it
## there, please note that it will be overwritten in your next install.
## If you want to keep a permanent local copy that will not be
## overwritten, place it in the following location:
## unix/linux:
## $HOME/.config/matplotlib/matplotlibrc or
# Snippet from http://jonathansoma.com/lede/data-studio/matplotlib/list-all-fonts-available-in-matplotlib-plus-samples/
# Author: Jonathan Soma
import matplotlib.font_manager
from IPython.core.display import HTML
def make_html(fontname):
return "<p>{font}: <span style='font-family:{font}; font-size: 24px;'>{font}</p>".format(font=fontname)
code = "\n".join([make_html(font) for font in sorted(set([f.name for f in matplotlib.font_manager.fontManager.ttflist]))])