Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@thomasaarholt
thomasaarholt / gufunc.py
Created August 1, 2021 16:38
gufunc error with whitespace in dask
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/var/folders/yt/z22_f0896l74f8gs3_20vc_00000gn/T/ipykernel_88946/1449311839.py in <module>
7 return np.linalg.inv(x)
8
----> 9 y = gufoo(b)
~/mambaforge/envs/py/lib/python3.9/site-packages/dask/array/gufunc.py in __call__(self, *args, **kwargs)
659
660 def __call__(self, *args, **kwargs):
@thomasaarholt
thomasaarholt / bilinear_interpolate.py
Created July 8, 2021 16:36
Bilinearly interpolate points from an image using NumPy or CuPy
def bilinear_interpolate(img, points, clipped_nan=True):
"""Bilinearly interpolate points from an image using NumPy or CuPy
Args:
img: Image of shape (Y, X) to interpolate from.
points: array of shape (2, N) of (y, x) coordinates
clipped_nan: If True, the value of coordinates outside the image shape
are set to nan. Otherwise they are clipped to the image edge.
Returns:
@thomasaarholt
thomasaarholt / bilinear_binning.py
Last active July 6, 2021 16:09
Bilinear binning, supports numpy and CuPy inputs using the np.array(..., like=arr) synax
def bilinear_binning(points, intensities, subpixel=1, gaussian_blur=False):
"""Bilinear weighting of points onto a grid.
Extent of grid given by min and max of points in each dimension
points should be an array of shape (N, 2)
intensity should be an array of shape (N,)
subpixel will increase the gridsize by its factor
gaussian_blur: blur the binned intensity and weighting images before they are divided, avoiding divide-by-zero warnings
TODO: Give a known grid as input
"""
@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