Skip to content

Instantly share code, notes, and snippets.

@wleepang
wleepang / mount_ephemeral.sh
Last active January 11, 2020 01:45
Detect and mount ephemeral storage
#!/bin/bash
set -e
function get_instance_storage_nvme() {
for nvme_device_name in `lsblk | cut --delimiter=" " --fields=1 | grep ^nvme | sort -n`; do
local nvme_device="/dev/${nvme_device_name}"
if ( nvme id-ctrl "${nvme_device}" | grep --quiet "^mn\\s*:\\s*Amazon EC2 NVMe Instance Storage\\s*$" ); then
echo "${nvme_device}"
fi
done
@wleepang
wleepang / read_from_s3.py
Last active November 22, 2022 14:45
read data from cloud object store with biopython
"""
Reads a fastq file directly from the 1000genomes AWS public dataset
into a Bio.SeqRecord set
Requires an AWS Account
"""
from smart_open import open
from Bio import SeqIO
@wleepang
wleepang / PIDControlR_SetpointSinusoid.R
Created February 9, 2013 04:44
Sinusoidal setpoint profile to test PID control in R
SP = sin(0.5*tt) + cos(.8*tt) + 5
@wleepang
wleepang / PIDControlR_SetpointSteps.R
Created February 9, 2013 04:30
Step change setpoint profile for PID control simulation
SP = rep(10, length(tt))
SP[which(tt >= 30)] = 5
SP[which(tt >= 60)] = 20
@wleepang
wleepang / PIDControlR_SimulationLoop.R
Created February 9, 2013 04:19
Simulation loop for a PID controller in R
for (k in 2:length(tt)) {
PV[k] = pv(PV[k-1], U[k-1], tt[k])
E[k] = PV[k] - SP[k]
EI[k] = EI[k-1] + E[k]*dt # integral
ED[k] = (E[k] - E[k-1])/dt # derivative
U[k] = Kp*(E[k] + (1/Ti)*sum(E*dt) + Td*ED[k])
if (U[k] < 0) U[k] = 0
@wleepang
wleepang / MultiSetFun.R
Created February 6, 2013 18:30
Set operations on multiple (e.g. more than 2) sets in R
MultiSetFun = function(fun, x) {
# performs a set function (e.g. intersect, setdiff) on x
# where x is a list of vectors of the same type but not necessarily the same length
fun = tolower(fun)
this.intersect = function(M, v) {
return(names(which(rowSums(M) == sum(v))))
}
@wleepang
wleepang / PIDControlR_Initialize.R
Created February 5, 2013 14:57
Initialization of the PID control simulation
# controller parameters
Kp = 10 # proportional gain
Ti = 1 # integral time
Td = 0.01 # derivative time
# simulation parameters
dt = .1 # time step
tt = seq(0, 100, by=dt) # time vector
# initialize the following to a vector of zeros
@wleepang
wleepang / MMFit.py
Created January 31, 2013 06:51
fitting a michaelis-menton curve in python
# assuming --pylab ipython
import numpy as np
from lmfit import minimize, Parameters
s = np.float_(np.array(range(0,1201,1200/8)))
v = np.round(120*s/(171+s) + np.random.uniform(size=9), 2)
def residual(p, x, data):
vmax = p['vmax'].value
km = p['km'].value
@wleepang
wleepang / PIDControlR_ProcessVariable.R
Last active December 11, 2015 14:59
Implementation of a PID control algorithm in R. A simple process for testing the controller.
pv = function(pv.prev, u, tt) {
out = pv.prev*1.1 + .5 # exponential growth + linear growth
out = out - 0.1*u # the control response
out = out + .5*runif(length(tt)) # a little noise, just for fun
if (out < 0) out = 0 # keep values positive
return(out)
}
@wleepang
wleepang / colorRamp.m
Created October 10, 2012 04:46
ColorRamp:MATLAB
function cmap = colorRamp(colors, varargin)
% colorRamp produces a color ramp for use in figure colormaps
% cmap = colorRamp(colors)
% cmap = colorRamp(colors, nlevels)
%
% Interpolates between color stops specified in the m-by-3 matrix colors.
% Each row represents one color stop.
% Assumes color stops are equally spaced.
%
% By default, produces a cmap with 32-levels, unless the nlevels argument