Skip to content

Instantly share code, notes, and snippets.

View tlancon's full-sized avatar

Trevor Lancon tlancon

View GitHub Profile
@tlancon
tlancon / us_states_dict.py
Last active May 13, 2022 08:58
Dictionary of All US States and their Abbreviations
# Inspired by https://gist.github.com/JeffPaine/3083347
# Access full state names using us_states.keys()
# Access all state abbreviations using us_states.values()
us_states = {
'Alabama': 'AL',
'Alaska': 'AK',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
@tlancon
tlancon / napari_count_tfpn.py
Last active March 24, 2022 16:01
Call this function to launch a napari window with a given image to begin annotating true positives, false positives, and false negatives on the image with points layers.
import os
import napari
from skimage.io import imread, imsave
def napari_count_tfpn(screenshot_file, point_size=20, save_screenshot=True):
"""
Opens a napari window and initializes three points layers:
- "True Positives" with green points
- "False Positives" with red points
- "False Negatives" with yellow points
@tlancon
tlancon / copy_lut_napari.py
Last active June 8, 2021 15:30
Copies the main visualization properties from one layer to another in napari.
def copy_lut(from_layer, to_layer, opacity=False):
"""
Copies the visualization properties from one layer to another in napari.
Only the colormap, contrast limits, and gamma values are included by default.
The opacity can be optionally copied as well.
Blending and interpolation are ignored.
@tlancon
tlancon / TrainWekaOnMultipleImages.ijm
Created April 2, 2021 18:00
Train a Weka model on multiple images by stacking slices from them together as a single stack. The stack is the same size in XY as the largest image, and all smaller images are padded with 0's to the lower-right corner to keep their original scale.
// Requirements:
// Trainable Weka Segmentation
// The list of files to use for training must be described in a text file,
// with each line containing the full path to one image.
listOfImages = File.openDialog("Choose a file with a list of images:");
imagesString = File.openAsString(listOfImages);
images = split(imagesString, "\n");
for (i=0; i<images.length; i++) {
@tlancon
tlancon / rip_gopro_audio.cmd
Last active September 29, 2020 14:02
Rips the audio from the all MP4s in the current directory to WAV. Requires FFMPEG.
for /r %%f in (*MP4) do (
ffmpeg -i %%f -q:a 0 -map a "%%~nf.wav"
)
@tlancon
tlancon / reject_outliers.py
Created June 5, 2020 18:52
Reject outliers from a 1D signal based on a sliding window and a deviation threshold.
def reject_outliers(arr, window_size, threshold):
"""
Given a 1D signal, replace outliers with the average of the surrounding points.
Does the following for every point:
1. Computes the median of the sliding window
2. Computes the percentage difference between the current point and the
sliding window median
3. If that deviation exceeds the given threshold, replaces that point with
the average of the surrounding two points.
@tlancon
tlancon / InspectLine.py
Created May 28, 2020 21:23
Inspect labeled objects in an image touching either a horizontal or vertical center line. Information is printed to the console.
import os
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage.measure import regionprops
# ----------------------------------------------------------
# Input
wdir = os.path.dirname('<DIRECTORY_CONTAINING_IMAGES>')
fname = '<LABELED_IMAGE_NAME>.tif'
@tlancon
tlancon / distance_between_points.py
Created May 17, 2020 04:19
Calculate the distance between X, Y points saved arrays using NumPy's diff() function without a loop.
import numpy as np
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([4, 5, 6, 7, 8, 9])
displacement = np.sqrt(np.diff(x, prepend=x[0])**2 + np.diff(y, prepend=y[0])**2)
@tlancon
tlancon / sarcastify.py
Last active July 2, 2019 17:01
Make any string sarcastic!
def sarcastify(my_string):
"""
Returns a sarcastic version of the provided string.
Example:
>>> sarcastify('Do you really think that\'s a good idea?')
"dO YoU ReAlLy tHiNk tHaT'S A GoOd iDeA?"
Useful for arguing online or replying all to that email that accidentally
went to the whole company.
@tlancon
tlancon / GetLabelAnalysisCellContents.py
Created July 20, 2018 16:02
Retrieves the contents of a specific cell from a tablelike-object (HxLabelAnalysis, HxSpreadSheet, etc.) in Amira/Avizo.
# Copy/paste into the Avizo/Amira Python console.
# Alternatively, save to a file, add that file to your path, and import.
# See docstring for usage.
def get_cell_contents(tablelike_object,row,column,table=0):
"""
Retrieves contents of cell i,j in a table through
HxSpreadSheetInterface.
You must use the object handle, not just the name of the object! For example,
to get the contents of row 4 from column 5 of a HxLabelAnalysis data object