Skip to content

Instantly share code, notes, and snippets.

@eric-czech
eric-czech / gist:2784e9ac50745a5d2915
Last active May 29, 2018 20:53
Parallel Cross-Validation Example in R
# Parallel Cross-Validation Example
library(foreach) # install.packages('foreach')
library(caret) # install.packages('caret', dependencies = c("Depends", "Suggests"))
library(doParallel) # install.packages('doParallel')
registerDoParallel(makeCluster(4)) # Use 4 cores for parallel CV
data <- read.csv(…) # Assuming this is your dataset
cv <- caret::createFolds(nrow(data), k=10, list=T) # Create 10 folds
@eric-czech
eric-czech / gist:e243996dcbcbb4ef0ef0
Created February 13, 2016 13:21
Linking R to Oracle JDK/JRE
# As an administrative user, run the following on command line (paths need to be adjusted to local jre version):
root> export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.7.0_91.jdk/Contents/Home/jre'
root> export LD_LIBRARY_PATH='/Library/Java/JavaVirtualMachines/jdk1.7.0_91.jdk/Contents/Home/jre/lib/server'
root> export PATH=$PATH:$JAVA_HOME/bin
root> R CMD javareconf JAVA_CPPFLAGS=-I/System/Library/Frameworks/JavaVM.framework/Headers
# On the R command line (launched as admin), run the following
R> install.packages('rJava','http://www.rforge.net/')
@eric-czech
eric-czech / build_and_run_ops_decon.sh
Last active April 27, 2018 22:01
Instructions for running [ops-experiments](https://github.com/imagej/ops-experiments) deconvolution on EC2
# These instructions were used to create a CUDA 9.1 compatible EC2 instance for the sake of
# running experiments with [imagej-ops-experiments]([ops-experiment](https://github.com/imagej/ops-experiments)
# deconvolution algorithms (via YacuDecu)
# Step 1) Create Instance
# Start instance for image: https://aws.amazon.com/marketplace/pp/B07BFSMQ32
# - Choose p2.xlarge type
# - Make sure to have at least 30G of disk space
# Step 2) Install Java/Maven
@eric-czech
eric-czech / scatter_density.R
Last active January 12, 2019 13:56
R ggplot 2D scatterplot colored by point density (with optional sampling to improve performance as well as edge case handling)
#' Get density associated with points in 2D space
#'
#' @param x Vector of numeric values
#' @param y Vector of numeric values; must have equal length to \code{x}
#' @param nbins Grid size used for 2D kde. Larger numbers mean density bins are
#' more continuous and at around 50 visible discretization begins to disappear
#' in scatterplots (though lower numbers will improve speed)
#' @param limit Maximum number of points to use for kde estimation
#' @param ... Other arguments for MASS::kde2d
#' @return A vector of densities with length equal to length of \code{x} and \code{y}.
@eric-czech
eric-czech / install_itkwidgets
Created March 10, 2019 09:52
Docker snippet for itkwidgets jupyterlab extension installation
RUN mkdir $HOME/.nodeenv && \
cd $HOME/.nodeenv && \
pip install nodeenv && \
nodeenv jupyterlab && \
. jupyterlab/bin/activate && \
pip install itkwidgets && \
jupyter labextension install @jupyter-widgets/jupyterlab-manager itk-jupyter-widgets
@eric-czech
eric-czech / stratified_proportion_split.py
Last active August 28, 2019 22:02
(Python/Sklearn) Function to run stratified split into arbitrary number of label subsets with some desired proportions
from sklearn.model_selection import train_test_split
import numpy as np
def _get_stratified_split(splits, idx, values, proportions):
# Return immediately if there are fewer than 2 proportions use in splits
if len(proportions) <= 1:
splits.append(idx)
return splits
@eric-czech
eric-czech / png_to_numpy.py
Last active May 29, 2024 09:17
Convert png image bytes to numpy array
import numpy as np
from io import BytesIO
from PIL import Image
def png_bytes_to_numpy(png):
"""Convert png bytes to numpy array
Example:
>>> fig = go.Figure(go.Scatter(x=[1], y=[1]))
@eric-czech
eric-czech / dask-kronecker-product.ipynb
Last active March 2, 2020 12:33
Dask kronecker product attempts
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@eric-czech
eric-czech / dask_genetic_data_simulation.ipynb
Last active May 23, 2020 16:18
Genetic data simulations with dask
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.