Skip to content

Instantly share code, notes, and snippets.

View stuarteberg's full-sized avatar

Stuart Berg stuarteberg

  • Ashburn, Virginia, USA
View GitHub Profile
@stuarteberg
stuarteberg / rle-implementations.py
Last active February 8, 2021 03:59
Python code (with IPython timing) for comparing rle implementations.
#
# Python code (with IPython timing) for comparing rle implementations.
# See: https://twitter.com/double_thunk/status/1358509387044827138?s=20
#
import random
import string
import platform
from itertools import groupby
import numpy as np
@stuarteberg
stuarteberg / imread_numsort.py
Created June 8, 2021 01:01
Drop-in replacement for dask_image.imread.imread that handles poorly-named image slices.
# -*- coding: utf-8 -*-
import os
import glob
import numbers
import warnings
import dask.array as da
import numpy as np
import pims
@stuarteberg
stuarteberg / closest_to_point.py
Last active March 24, 2025 19:52
Find the closest point in a mask to an aribtrary point
from itertools import starmap
import numpy as np
def closest_to_point(point, mask, mask_offset=None, voxel_size=None):
"""
Find the point in the mask that is closest to the given point.
Args:
point:
The point to find the closest point to.
@stuarteberg
stuarteberg / pipe_in_python.py
Last active August 19, 2025 02:10
pipe_in_python.py
from collections.abc import Callable, Iterable
from functools import partial
# See discussion in https://news.ycombinator.com/item?id=44942936
def pipe(orig, *transforms):
result = orig
for transform in transforms:
if isinstance(transform, Callable):
result = transform(result)