Skip to content

Instantly share code, notes, and snippets.

View CnrLwlss's full-sized avatar

Conor Lawless CnrLwlss

View GitHub Profile
@CnrLwlss
CnrLwlss / simDSLogistic.R
Last active December 10, 2015 11:58
R function for carrying out discrete stochastic simulations of the logistic population model. http://cnr.lwlss.net/DiscreteStochasticLogistic/ #R #Rstats #discrete #stochastic #logistic
simDSLogistic=function(K,r,N0){
# Unusually, for this model, we know the number of events a priori
eventNo=K-N0
# So we can just generate all required random numbers (quickly) in one go
unifs=runif(eventNo)
# Every event produces one cell and consumes one unit of nutrients
clist=(N0+1):K
# Simulate time between events by generating
# exponential random numbers using the inversion method
dts=-log(1-unifs)/(r*clist*(1-clist/K))
@CnrLwlss
CnrLwlss / simDSLogistic.py
Last active December 10, 2015 11:59
Python function for carrying out discrete stochastic simulations of the logistic population model. http://cnr.lwlss.net/DiscreteStochasticLogistic/
import numpy as np
def simDSLogistic(K,r,N0):
'''Discrete stochastic logistic model simulation. Carrying capacity K,
intrinsic growth rate r, initial population size (inoculum) N0. Returns an
array with a (continuous) time column and a (discrete) population size column.'''
# Unusually, for this model, we know the number of events a priori
eventNo=K-N0
# So we can just generate all required random numbers (quickly) in one go
unifs=np.random.uniform(size=eventNo)
@CnrLwlss
CnrLwlss / circlePacking.R
Last active December 4, 2016 15:47
Functions for packing N circles into a rectangle of width W and height H, together with a function for plotting solution and some example code fitting 13 circles into a square. http://cnr.lwlss.net/CircleObjectivesR/
###########
# Optimum circle packing in R
###########
# Function closure, sets up list of possible combinations
# and returns objective function
createObj<-function(N,W,H){
print("Generating combinations...")
# Generate all possible circle pairings
@CnrLwlss
CnrLwlss / circlePackingGlobal.R
Created January 19, 2013 11:10
Testing different algorithms for solving the constrained optimisation problem: packing circles into a rectangle. http://cnr.lwlss.net/GlobOptR/ #R #optimisation #circle #packing #deoptim #snow #rgenoud
library(DEoptim)
library(rgenoud)
library(snow)
# CirclePacking.R from https://gist.github.com/4571810
source("CirclePacking.R")
# Number of circles and dimensions of bounding rectangle
N=100; W=10; H=10
root="N64_100"
@CnrLwlss
CnrLwlss / circleObjective.py
Created January 19, 2013 13:51
Functions for packing N circles into a rectangle of width W and height H, together with a function for plotting solution and some example code fitting 13 circles into a square. http://cnr.lwlss.net/CircleObjectivesPython/ #python #opimisation #circle #packing #svg #function #closure
import random, numpy
def genGuess(N,W,H):
'''Generate sensible initial guess vector (random circle coordinates and radii)'''
z=numpy.zeros(3*N)
for i in xrange(0,N):
z[i*3]=random.random()*W
z[i*3+1]=random.random()*H
z[i*3+2]=0.001*min(W,H)+random.random()*(0.009*min(W,H))
return(z)
@CnrLwlss
CnrLwlss / ConstrainedRandomWalks.R
Last active December 11, 2015 18:38
Demonstrating a function for simulating constrained random walks. Like a discrete Brownian bridge. http://cnr.lwlss.net/ConstrainedRandomWalk
# Simulating n steps of a biased random walk
# starting from x0 and with decreasing steps
# occurring with probability theta
randomWalk=function(n=100,x0=0,theta=0.5){
x=x0
res=array(x0,dim=n+1)
unifs=runif(n)
for(i in 0:(n-1)){
if (unifs[i+1]<=theta){x=x-1}else{x=x+1}
res[i+2]=x
@CnrLwlss
CnrLwlss / simDSLogisticHybrid.R
Created February 19, 2013 23:51
R function for carrying out hybrid continuous deterministic / discrete stochastic simulations of the logistic population model. http://cnr.lwlss.net/DiscreteStochasticLogistic/ #R #Rstats #discrete #stochastic #logistic
simCellsHybrid=function(K,r,N0,NSwitch,detpts=100){
# Every event produces one cell and consumes one unit of nutrients
if(NSwitch>N0){
# Unusually, for this model, we know the number of events a priori
eventNo=NSwitch-N0
# So we can just generate all required random numbers (quickly) in one go
unifs=runif(eventNo)
clist=(N0+1):NSwitch
# Time between events
dts=-log(1-unifs)/(r*clist*(1-clist/K))
@CnrLwlss
CnrLwlss / AnalyzeTimecourse.py
Last active December 14, 2015 23:29
A quick demo, analyzing a batch of files with Colonyzer2. See this page for more info: http://research.ncl.ac.uk/colonyzer/
from colonyzer2 import *
import time, sys
def main(fmt="384"):
# Lydall lab file naming convention
# First 15 characters in filename identify unique plates
# Remaining charaters can be used to store date, time etc.
barcRange=(0,15)
if len(sys.argv)>1:
fmt=sys.argv[1]
@CnrLwlss
CnrLwlss / ColonyCounter.py
Last active May 1, 2021 21:16
Counting colony forming units (CFU) generated from cells plated onto solid agar. Left click to tag cells with red marker, right click to tag with yellow marker. Generates an image record of marked CFUs with red and yellow counts embedded into image record filename.
import pygame, os, sys
from pygame.locals import *
# Runs through all jpgs in current directory
# Opens each in turn, allowing user to click on image features
# Left clicking draws a red dot, right-clicking draws a yellow dot
# Press q or esc to move to next image
# Once an image is finished, a version with click locations overlaid is saved to file
# Numbers of left and right clicks are embedded into filename
@CnrLwlss
CnrLwlss / CellCounter.py
Created April 7, 2013 11:45
Electronic clicker for manual counting of three cell types (G1, G2/M and S). While looking down the eyepiece of a microscope at cells on a glass slide, the user can press "g" to increment the number of cells in G1, "h" to increment the number of cells in G2/M and "j" to increment the number of cells in S. Press space bar to move from one slide t…
import winsound, time, Tkinter
slides=[]
cells=[0,0,0]
labels=["G1","S","G2/M"]
print "Slide: %02d"%(len(slides)+1)
def key(event):
if event.char=="g":
cells[0]+=1