Skip to content

Instantly share code, notes, and snippets.

@johnhw
johnhw / fft_mul.py
Created February 8, 2024 11:57
FFT based multiplication example in NumPy
from numpy.fft import rfft, irfft
import numpy as np
def int_to_vec(x, base=10):
"""Convert an integer to a list of digits in a given base."""
return [int(s, base) for s in str(x)]
def vec_to_int(v, base=10):
"""Convert a list of digits to an integer in a given base;
values of the list can be any integer, carries will
@johnhw
johnhw / tcd_to_json.py
Last active December 11, 2023 10:03
Python script to convert XTide TCD database files to JSON. Requires libtcd to be installed.
import ctypes
import json
import os
# Load the libtcd shared library using ctypes
# set the path here to the location of the libtcd.so file
lib_path = "/usr/lib"
libtcd = ctypes.CDLL(f"{lib_path}/libtcd.so")
# Path where the xtide TCD files are located
import math
def itr(n, q, v, t):
# Calculate the probability of each choice being selected (assuming equally likely)
p_choice = 1 / q
# Calculate the entropy for each question's choices
entropy_per_question = -p_choice * math.log2(p_choice) * q
# Calculate the total entropy across all questions
This file has been truncated, but you can view the full file.
date,asl
2013-06-02 00:00:00,0.816
2013-06-02 00:15:00,0.845
2013-06-02 00:30:00,0.865
2013-06-02 00:45:00,0.902
2013-06-02 01:00:00,0.94
2013-06-02 01:15:00,0.983
2013-06-02 01:30:00,1.023
2013-06-02 01:45:00,1.086
2013-06-02 02:00:00,1.132
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# Converts Ordnance Survey Terrian 50 United Kingdom data to a simple NumPy array of terrain altitudes
# The original OS data is freely downloadable from: https://www.ordnancesurvey.co.uk/business-government/products/terrain-50
# Directly converts the zipped terrain file to a NPZ file
# Resulting array is a (24600, 13200) array of float64
# Requirements:
# * pycrs
# * lxml
import os, sys
import numpy as np
from sklearn.metrics import confusion_matrix
from collections import defaultdict
def itr(confusion_matrix, timings, eps=1e-8):
"""Take a confusion matrix of the form
(actual) a b c d
(intended)
a 10 0 1 9
@johnhw
johnhw / plot_connected_points.py
Created July 18, 2019 18:14
Matplotlib draw connections between two sets of 2D points efficiently
from matplotlib.collections import LineCollection
def plot_connected_pairs(a,b,*args,**kwargs):
"""
Draw lines between two arrays of 2D points.
a and b must be the same shape, both [N,2] arrays to be plotted.
Parameters:
-----------
a: [N,2] array of points to plot from
@johnhw
johnhw / bivariate_matplotlib.py
Last active October 26, 2023 18:50
Bivariate colormap in matplotlib
# bivariate colormaps in matplotlib
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
## Bivariate colormapping
from scipy.interpolate import RegularGridInterpolator
@johnhw
johnhw / trace_frame.py
Created July 8, 2019 16:09
Determine source of function calls in Python
import inspect
class tracked:
"""Class to remember the execution frame
that a function call was made in."""
def __init__(self, fn):
self.fn = fn