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 / show-git-branch-in-bash-prompt
Last active October 10, 2021 17:55
Code for your .bashrc file. Show current git branch on bash shell prompt....with color! (This was copied and modified from similar code you can find out there on the 'tubes.)
# Colors for the prompt
blue="\033[0;34m"
white="\033[0;37m"
green="\033[0;32m"
# Brackets needed around non-printable characters in PS1
ps1_blue='\['"$blue"'\]'
ps1_green='\['"$green"'\]'
ps1_white='\['"$white"'\]'
@stuarteberg
stuarteberg / relabel_benchmarks.py
Last active December 21, 2015 10:49
Benchmarking alternate implementations for relabeling a label image with a mapping specified as a dict.
import numpy
original_labels = None
mapping = None
# TODO: Ensure that all labels are present in mapping dict
# because none of these functions work otherwise.
def using_index_array():
consecutivized_labels = numpy.searchsorted( sorted( mapping.iterkeys() ), original_labels )
@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 )
@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 / 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 / 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 / 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 / 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 / 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 / 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