Skip to content

Instantly share code, notes, and snippets.

@danstowell
danstowell / ReservoirSampleMatrix.py
Created April 26, 2018 09:38
Algorithm to sample random rows from a matrix that is too large to store in memory (Reservoir Random Sampling, Algorithm "R") https://en.wikipedia.org/wiki/Reservoir_sampling#Algorithm_R
# by Dan Stowell 2014.
# This particular file implementing ReservoirSampleMatrix is published under the MIT Public Licence
# http://opensource.org/licenses/MIT
import numpy as np
import random
class ReservoirSampleMatrix:
"""This class helps you to take a uniform random sample from streaming data, using the 'Reservoir Sampling' technique.
# simple audio sync example by Dan Stowell Nov 2018
import librosa # lib... Rosa!
import os
import numpy as np
###############################################
maxlagsecs = 10 # the maximum offset between two audio files that will be considered
@danstowell
danstowell / gbifparakeet.py
Created October 25, 2019 13:49
Plot GBIF parakeet observation data
import os, sys, csv
import numpy as np
import pandas as pd
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import imageio
###################################################################################
@danstowell
danstowell / bootstrap_example.py
Created July 28, 2022 06:44
example of bootstrap sampling to estimate confidence intervals on an accuracy measure
# example of bootstrap sampling to estimate confidence intervals on an accuracy measure
import numpy as np
nbootstraps = 500 # 50 # 500 # note that 50 is fast enough for development purposes, but I use 500 for final evaluation
# here's a VERY SHORT list of outcomes, each one reflecting whether sound X was correctly predicted or not
outcomes = np.array([1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0])
import os
import sys
import imageio
import numpy as np
import librosa as lr
import matplotlib.pyplot as plt
from tqdm import tqdm
@danstowell
danstowell / trim_audio_dataset.py
Last active August 6, 2024 10:18
Python script to enforce a maximum duration on a set of audio files. Long files are trimmed, without altering the sample data. The script can either convert all to FLAC, or preserve exact container type for each file.
#!/bin/env python
# Script to enforce a maximum duration on a set of audio files.
# Long files are trimmed, without altering their file format.
# Uses ffmpeg. (So, that needs to be installed on your system).
# Tested in Ubuntu Linux v22.04.
# Written by Dan Stowell 2024.
# CC0: This work has been marked as dedicated to the public domain.
@danstowell
danstowell / linf_land_1.tal
Last active March 16, 2025 10:33
UXN code for "linf_land" demo by me 2024: https://www.youtube.com/watch?v=5dqfhiMWZdk
( linf_land_1.tal
~/dev/uxn/uxnasm linf_land_1.tal linf_land_1.rom && ~/dev/uxn/uxncli linf_land_1.rom
~/dev/uxn/uxnasm linf_land_1.tal linf_land_1.rom && ~/dev/uxn/uxnemu -3x linf_land_1.rom
)
( devices )
|00 @System [ &vector $2 &pad $6 &r $2 &g $2 &b $2 &debug $1 &state $1 ]
|20 @Screen [ &vector $2 &width $2 &height $2 &pad $2 &x $2 &y $2 &addr $2 &pixel $1 &sprite ]
|c0 @DateTime [ &year $2 &month $1 &day $1 &hour $1 &minute $1 &second $1 &dotw $1 &doty $2 &isdst $1 ]