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 / 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 / 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 / fill_hull.py
Last active December 19, 2023 09:51
Compute a convex hull and create a mask from it
import numpy as np
import scipy
def fill_hull(image):
"""
Compute the convex hull of the given binary image and
return a mask of the filled hull.
Adapted from:
https://stackoverflow.com/a/46314485/162094
@stuarteberg
stuarteberg / lz4-shuffle-example
Last active June 15, 2020 14:23
Quick test of compressing random data with/without shuffling
In [20]: import numpy as np
...: import lz4.frame
...:
...: a = np.random.randint(2**16, size=1_000_000, dtype=int)
...: buf = lz4.frame.compress(a)
...: print("Compressed size without shuffling:", len(buf))
...:
...: shuffled = a.view(np.uint8).reshape(-1, 8).transpose().copy(order='C')
...: buf2 = lz4.frame.compress(shuffled)
...: print("Compressed size with shuffling: ", len(buf2))
@stuarteberg
stuarteberg / py2app-alias-mode-trick
Created June 13, 2020 18:21
How to create a macOS app bundle from a conda environment with py2app in "alias mode"
Normally, if you create an app bundle with py2app, it will copy the files from your
conda environment into the app bundle, but it will completely reorganize the
directory structure that you started with. That has various downsides.
But there's a way to avoid that: You can exploit py2app's "alias mode" to create an
app bundle that doesn't actually change the directory structure of your conda environment.
Just place the environment inside the app bundle, wholesale! Instructions below,
with links to a real project that uses this method.
---
@stuarteberg
stuarteberg / benchmark.ipynb
Last active February 13, 2018 20:03
Quick HDF5 Benchmark, modified from rossant/benchmark.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@stuarteberg
stuarteberg / superpixel-stack-to-body-label-volume.py
Last active September 25, 2017 20:50
Read a stack of superpixel .png files (exported from Raveler), relabel them, and export the volume as hdf5.
"""
Utility function relabel_superpixels_to_bodies(), along with a main() function providing a command-line interface.
See usage documentation in main(), below.
"""
from __future__ import print_function
import collections
import numpy as np
import vigra
def main():
@stuarteberg
stuarteberg / gist:7ecd8cb7b24d12f4ffd9
Last active January 19, 2023 04:34
simpleview.py
# License: public domain
class SimpleView(object):
"""
Simple view-like object of an array-like object.
Slices taken with __getitem__ are combined and stored for later use,
but the actual data is not extracted from the underlying array-like
object until __array__ is called.
Warnings about the current implementation:
@stuarteberg
stuarteberg / draggable_widget.py
Created April 21, 2014 01:42
Draggable QWidget
from PyQt4.QtCore import QRect
from PyQt4.QtGui import QApplication, QWidget, QLabel
class DraggableLabel( QLabel ):
"""
A QLabel subclass that can be dragged around within its parent widget.
Note: Not intended to work if the parent widget has a layout (e.g. QVBoxLayout).
"""
def __init__(self, *args, **kwargs):
@stuarteberg
stuarteberg / draggable_graphicsitem.py
Last active May 3, 2016 23:19
Draggable QGraphicsItem
from PyQt4.QtCore import Qt, QPointF, QRectF
from PyQt4.QtGui import QApplication, QWidget, QGraphicsView, QGraphicsScene, QGraphicsItem, QVBoxLayout, QSpacerItem, QSizePolicy, QCursor
class DraggableBox( QGraphicsItem ):
"""
A simple QGraphicsItem that can be dragged around the scene.
Of course, this behavior is easier to achieve if you simply use the default
event handler implementations in place and call
QGraphicsItem.setFlags( QGraphicsItem.ItemIsMovable )