Skip to content

Instantly share code, notes, and snippets.

View mistycheney's full-sized avatar

Yuncong Chen mistycheney

View GitHub Profile
@mistycheney
mistycheney / install_pyenv.sh
Created November 11, 2022 18:16
Install PyEnv
# Install pyenv
export PYENV_ROOT=/nfs/intern_data/thsu/.pyenv
export PATH="${PYENV_ROOT}/bin:${PATH}"
curl https://pyenv.run | bash
# Then add in ~/.bashrc:
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
@mistycheney
mistycheney / emd_example.py
Last active March 12, 2019 23:19
Earth mover's distance
import pyemd # https://github.com/garydoranjr/pyemd. Install by running 'pip install pyemd'.
from scipy.spatial.distance import pdist, squareform
x_bins = range(10) # Assume histogram has 10 bins for each axis
y_bins = range(10)
z_bins = range(10)
xs, ys, zs = np.meshgrid(x_bins, y_bins, z_bins)
xs = xs.flatten()
ys = ys.flatten()
@mistycheney
mistycheney / count_anomalies_from_scores.py
Last active September 13, 2018 22:10
Count anomalies based on scores
# Various ways to find peaks in Python
# https://github.com/MonsieurV/py-findpeaks
def count_anomalies_from_scores(scores, plot=False):
from scipy.signal import find_peaks
is_outlier = detect_outlier(scores)
peaks, _ = find_peaks(is_outlier, distance=60) # 60 means peaks must be separated by 60 mins
num_anomalies = np.count_nonzero(peaks)
if plot:
@mistycheney
mistycheney / batch_convert
Created October 12, 2017 23:05
batch convert imagej plugin
// Batch Convert
//
// This macro convert all the files in a folder to TIFF, 8-bit TIFF,
// JPEG, GIF, PNG, PGM, BMP, FITS, Text Image, ZIP or Raw
// format. Three dialog boxes are displayed. Select the source
// folder in the first, the format in the second and the destination
// folder in the third. Batch_Converter, a similar plugin is at
// http://rsb.info.nih.gov/ij/plugins/batch-converter.html
// https://imagej.nih.gov/ij/plugins/batch-converter.html
Cereal Name Manufacturer Type Calories Protein (g) Fat Sodium Dietary Fiber Carbs Sugars Display Shelf Potassium Vitamins and Minerals Serving Size Weight Cups per Serving
100%_Bran Nabisco C 70 4 1 130 10 5 6 3 280 25 1 0.33
100%_Natural_Bran Quaker Oats C 120 3 5 15 2 8 8 3 135 0 1 -1
All-Bran Kelloggs C 70 4 1 260 9 7 5 3 320 25 1 0.33
All-Bran_with_Extra_Fiber Kelloggs C 50 4 0 140 14 8 0 3 330 25 1 0.5
Almond_Delight Ralston Purina C 110 2 2 200 1 14 8 3 -1 25 1 0.75
Apple_Cinnamon_Cheerios General Mills C 110 2 2 180 1.5 10.5 10 1 70 25 1 0.75
Apple_Jacks Kelloggs C 110 2 0 125 1 11 14 2 30 25 1 1
Basic_4 General Mills C 130 3 2 210 2 18 8 3 100 25 1.33 0.75
Bran_Chex Ralston Purina C 90 2 1 200 4 15 6 1 125 25 1 0.67
@mistycheney
mistycheney / incorporating_uncertainty.md
Created May 17, 2016 08:08
incorporating_uncertainty

Incorporating Uncertainty

If the score maps given by the classifier are not extremely reliable, we need to balance between the reference structure locations constrained by the atlas and the evidence from score maps.

We do this in a Bayesian setting. Suppose the atlas gives a prior distribution for the structure's location, represented by $P(\theta)$. An observation distribution $P(s | \Omega)$ models classification uncertainty, where $\Omega$ is the set of voxels that belong to a particular structure, and $s$ is the computed score volume.

Let $\Omega(\theta)$ denote this structure's voxel locations transformed by $\theta$, and let $s$ denote the score volume, then the likelihood of the score volume is $P(s | \Omega(\theta))$. The optimal transform $\theta^*$ should maximize the posterior:

$$ \theta^* = \arg\max P(s | \Omega(\theta)) P(\theta).$$

@mistycheney
mistycheney / bbox.py
Last active April 13, 2016 02:45
bounding box for 2d or 3d binary images
# http://stackoverflow.com/a/31402351
def bbox_2d(img):
rows = np.any(img, axis=1)
cols = np.any(img, axis=0)
rmin, rmax = np.where(rows)[0][[0, -1]]
cmin, cmax = np.where(cols)[0][[0, -1]]
return cmin, cmax, rmin, rmax
@mistycheney
mistycheney / concave_hull.py
Last active March 13, 2016 11:42
concave hull
# https://gist.github.com/hellpanderrr/2c08af0f07eed4782234
from scipy.spatial import Delaunay, ConvexHull
import networkx as nx
def concave_hull(points,alpha_x=150,alpha_y=250):
points = [(i[0],i[1]) if type(i) <> tuple else i for i in points]
de = Delaunay(points)
dec = []
a = alpha_x
@mistycheney
mistycheney / sort_vertices_counterclockwise.py
Created March 13, 2016 11:41
sort polygon vertices counterclockwise
def less(center):
def less_helper(a, b):
if (a[0] - center[0] >= 0 and b[0] - center[0] < 0):
return 1;
if (a[0] - center[0] < 0 and b[0] - center[0] >= 0):
return -1;
if (a[0] - center[0] == 0 and b[0] - center[0] == 0):
if (a[1] - center[1] >= 0 or b[1] - center[1] >= 0):
return 2*int(a[1] > b[1]) - 1;
return 2*int(b[1] > a[1]) - 1
@mistycheney
mistycheney / patches_to_rec.py
Last active January 28, 2016 21:13
Generate mxnet record from image patches
stack = 'MD589'
data_dir = '/oasis/projects/nsf/csd395/yuncong/CSHL_data_patches/%s_byLandmark'%(stack)
all_image_names = []
for label in os.listdir(data_dir):
if label == 'BackG':
# limit the number of BackG examples
for img_filename in os.listdir(data_dir + '/' + label)[:300]:
all_image_names.append((label_dict[label], label + '/' + img_filename))
else: