{{ message }}

Instantly share code, notes, and snippets.

# Thomas Aarholt thomasaarholt

• University of Oslo
• Oslo, Norway
Created Jul 8, 2021
Bilinearly interpolate points from an image using NumPy or CuPy
View bilinear_interpolate.py
 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:
Last active Jul 6, 2021
Bilinear binning, supports numpy and CuPy inputs using the np.array(..., like=arr) synax
View bilinear_binning.py
 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 """
Last active Jun 28, 2021
Bilinear binning with CuPy
View bilinear_bincount_cupy.py
 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
Created Jun 28, 2021
Bilinear Binning with Numpy
View bilinear_bincount_numpy.py
 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)
Created Jun 28, 2021
KDEpy error
View kdepyerror.sh
 (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>=
Created Jun 17, 2021
Levenberg Marquardt implementation in numpy
View lm_numpy.py
 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`
Created Jun 12, 2021
Installing CPU pytorch using mamba, which does include cudatoolkit
View pytorch_cpu_mamba.sh
 (base) ~: mamba create --name torchtest pytorch torchvision torchaudio cpuonly -c pytorch __ __ __ __ / \ / \ / \ / \ / \/ \/ \/ \ ███████████████/ /██/ /██/ /██/ /████████████████████████ / / \ / \ / \ / \ \____ / / \_/ \_/ \_/ \ o \__, / _/ \_____/ ` |/
Created Jun 12, 2021
Installing CPU pytorch using conda, which does not include cudatoolkit
View pytorch_cpu_conda.sh
 (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
Last active Jun 7, 2021
Function minimizer using PyTorch and L-BFGS
View torch_LBFGS.py
 # 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
Created May 23, 2021
View save_the_americans.py
 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}")