Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@thomasaarholt
thomasaarholt / bilinear_bincount_cupy.py
Last active June 28, 2021 09:19
Bilinear binning with CuPy
def bilinear_bincount_cupy(points, intensities, subpixel=1):
"""Bilinear weighting of points onto a grid.
Extent of grid given by min and max of points in each dimension
points should be a cupy array of shape (N, 2)
intensity should be a cupy array of shape (N,)
"""
points = subpixel * points
floor = cp.floor(points)
ceil = floor + 1
@thomasaarholt
thomasaarholt / bilinear_bincount_numpy.py
Created June 28, 2021 09:14
Bilinear Binning with Numpy
def bilinear_bincount_numpy(points, intensities):
"""Bilinear weighting of points onto a grid.
Extent of grid given by min and max of points in each dimension
points should have shape (N, 2)
intensity should have shape (N,)
"""
floor = np.floor(points)
ceil = floor + 1
floored_indices = np.array(floor, dtype=int)
low0, low1 = floored_indices.min(0)
(gpus) [thomasaar@ml2 KDEpy]$ pip install -e .
Obtaining file:///itf-fi-ml/home/thomasaar/github/KDEpy
Requirement already satisfied: numpy>=1.14.2 in /itf-fi-ml/home/thomasaar/.conda/envs/gpus/lib/python3.9/site-packages (from KDEpy==1.1.0) (1.21.0)
Requirement already satisfied: scipy>=1.0.1 in /itf-fi-ml/home/thomasaar/.conda/envs/gpus/lib/python3.9/site-packages (from KDEpy==1.1.0) (1.6.3)
Requirement already satisfied: matplotlib>=2.2.0 in /itf-fi-ml/home/thomasaar/.conda/envs/gpus/lib/python3.9/site-packages (from KDEpy==1.1.0) (3.4.2)
Requirement already satisfied: pillow>=6.2.0 in /itf-fi-ml/home/thomasaar/.conda/envs/gpus/lib/python3.9/site-packages (from matplotlib>=2.2.0->KDEpy==1.1.0) (8.2.0)
Requirement already satisfied: cycler>=0.10 in /itf-fi-ml/home/thomasaar/.conda/envs/gpus/lib/python3.9/site-packages (from matplotlib>=2.2.0->KDEpy==1.1.0) (0.10.0)
Requirement already satisfied: python-dateutil>=2.7 in /itf-fi-ml/home/thomasaar/.conda/envs/gpus/lib/python3.9/site-packages (from matplotlib>=
@thomasaarholt
thomasaarholt / lm_numpy.py
Created June 17, 2021 10:22
Levenberg Marquardt implementation in numpy
import sympy as sp
import numpy as np
def levenberg(sympy_func, xi, target, sympy_param, guess, weight=None, module='numpy'):
'''
Computes the minimum `guess` so that `sympy_func(guess) - target` is minimized
Arguments:
sympy_func : Should be a sympy function to be minimized
xi : numpy array, the x-axis of `target`
@thomasaarholt
thomasaarholt / pytorch_cpu_mamba.sh
Created June 12, 2021 09:14
Installing CPU pytorch using mamba, which does include cudatoolkit
(base) ~: mamba create --name torchtest pytorch torchvision torchaudio cpuonly -c pytorch
__ __ __ __
/ \ / \ / \ / \
/ \/ \/ \/ \
███████████████/ /██/ /██/ /██/ /████████████████████████
/ / \ / \ / \ / \ \____
/ / \_/ \_/ \_/ \ o \__,
/ _/ \_____/ `
|/
@thomasaarholt
thomasaarholt / pytorch_cpu_conda.sh
Created June 12, 2021 09:13
Installing CPU pytorch using conda, which does not include cudatoolkit
(base) ~: conda create --name torch pytorch torchvision torchaudio cpuonly -c pytorch
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /home/thomasaar/mambaforge/envs/torch
added / updated specs:
- cpuonly
@thomasaarholt
thomasaarholt / torch_LBFGS.py
Last active June 7, 2021 09:10
Function minimizer using PyTorch and L-BFGS
# Let's minimize the function f(x,y) = (x-50)**2 + (y-100)**2
# We can tell from looking at the equation that the minimum should be at (50, 100).
def func(params):
x, y = params
return (x-50)**2 + (y-100)**2
# Optionally, view what it looks like
fortune = 500_000_000
total_americans = 327_000_000
americans_lives_changed = 0
for american in range(total_americans):
if fortune - 1_000_000 >= 0:
fortune -= 1_000_000
americans_lives_changed += 1
else:
raise ValueError(f"Uh oh! He ran out of money! Lives changed: {americans_lives_changed}")
@thomasaarholt
thomasaarholt / unpecked_hens.py
Last active May 23, 2021 08:41
If a number of hens in a circle peck either left or right, what is the expected number of unpecked hens at the end?
import numpy as np
import random
number_of_hens = 100
repeats = 1000 # repeat many times so that we get some decent statistics
sums = []
for _ in range(repeats):
hens = np.zeros(number_of_hens, dtype=bool) # reset an array of False
x_ = np.linspace(0, 12, 1000)
die_y = die_fit_func(x_, *guess)
fig, ax = plt.subplots()
ax.plot(x_, die_y.real)
ax2 = ax.twinx()
ax2.plot(x_, die_y.imag, color='red')
ax.set(xlabel='Energy', ylabel='Real')