Skip to content

Instantly share code, notes, and snippets.

View CnrLwlss's full-sized avatar

Conor Lawless CnrLwlss

View GitHub Profile
@CnrLwlss
CnrLwlss / Focus.py
Created April 14, 2017 12:12
Image analysis script for finding and visualising the areas of an image that are in focus.
from PIL import Image
from scipy import ndimage
import numpy as np
def getVar(im, sigma = 4.0):
'''Identifies & highlights areas of image fname with high pixel intensity variance (focus)'''
imbw = im.convert("F") # Convert im to greyscale array
arrbw = np.array(imbw)
sub_mean = ndimage.gaussian_filter(arrbw, sigma) # Calculate variance in area around each pixel
@CnrLwlss
CnrLwlss / RectangularArray.R
Created November 30, 2016 16:20
Get best size for graphics array (array dimension that is most perfectly filled by correlation plots) when plotting possible pairwise combinations of variables against each other.
fdefpairs=t(combn(fdefs,2))
npairs=dim(fdefpairs)[1]
a=round(sqrt(npairs))
if((a+1)*(a+1)>=npairs) mfrow=c(a+1,a+1)
if((a+1)*a>=npairs) mfrow=c(a,a+1)
if(a*a>=npairs) mfrow=c(a,a)
@CnrLwlss
CnrLwlss / CustomErrorBars.py
Last active November 6, 2021 14:57
Script for plotting stripplot or swarmplot with barplot overlay, including custom error bars, with matplotlib/Seaborn.
import seaborn as sns
tips = sns.load_dataset("tips")
print(sns.__version__)
print(tips.head())
ax=sns.swarmplot(x="day", y="total_bill", hue="sex", data=tips,split=True)
sns.barplot(x="day", y="total_bill", hue="sex", data=tips,capsize=0.1,errwidth=1.25,alpha=0.25,ci=None)
xcentres=[0,1,2,3]
delt=0.2
xneg=[x-delt for x in xcentres]
@CnrLwlss
CnrLwlss / Limited_logistic_mixture.R
Created October 22, 2016 20:45
Simulating growth of a nutrient-limited population consisting of a mixture of two lineages with different growth rates (but sharing resources, i.e. co-localised).
# Install and load ODE solver package
#install.packages("deSolve")
library(deSolve)
# Named vector of parameter values
parameters=c(r_1=1.0,r_2=1.1,K=1)
# Named vector of initial conditions
state=c(x_1=0.01,x_2=0.01)
@CnrLwlss
CnrLwlss / miniQFA.JAGS
Created October 13, 2016 18:43
Bayesian hierarchical model, written in JAGS, representing relationship between spots on a QFA plate. Constrained, uniform priors all the way through hierarchy.
model {
tau_min <- 10000
tau_max <- 1000000
x0_min <- 0.0
x0_max <- 0.05
r_min <- 0.0
r_max <- 10.0
K_min <- 0.0
K_max <- 0.5
@CnrLwlss
CnrLwlss / ColourContrast.R
Last active November 12, 2019 13:53
Checking colour contrasts
# Visual comparison of the contrast between colours and different solid backgrounds
colourGrid=function(dimval=20,background="gray",colourfun=rainbow,ptsize=1){
cols=colourfun(dimval^2)
yvals=rep(seq(0,1,length.out=dimval),each=dimval)
xvals=rep(seq(0,1,length.out=dimval),dimval)
plot(xvals,yvals,type="n",xaxt="n",yaxt="n",ann=FALSE, bty="n")
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = background,border=NA)
points(xvals,yvals,col=cols,pch=16,cex=ptsize)
}
@CnrLwlss
CnrLwlss / RankPlots.py
Created July 7, 2016 10:50
Visualising changes in rank order. For example, can think of changes in rank order of phenotypes in genetic screen with experiment or with type of analysis.
# Plot comparing ranks
import string
import random
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
def experiment(genes,vmin=0.0,vmax=1.0):
'''Simulate an experiment with (average) measurement for a set of genotypes'''
@CnrLwlss
CnrLwlss / plotDemo.py
Created March 4, 2016 17:18
Preparing differently sized plot panels, labelled with proper genetics nomenclature, using matplotlib.
import matplotlib as mp
import matplotlib.pyplot as plt
import numpy as np
import string
# Set font for figure according to journal specs
mp.rcParams['font.family'] = "Arial"
# Gene deletion names read from file might not necessarily be in lower case
gnames=["EXO1","RAD9","TMA20"]
@CnrLwlss
CnrLwlss / Colormap.py
Last active February 18, 2016 16:41
Continuous colour map in python with matplotlib. Requires matplotlib 1.5?
#http://matplotlib.org/examples/color/colormaps_reference.html
#http://matplotlib.org/users/colormaps.html
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
N = 200
x = np.random.randn(N)
y = np.random.randn(N)
@CnrLwlss
CnrLwlss / hierarchies.R
Created February 5, 2016 22:49
Visualising hierarchies
# Hierarchical uniform distributions
Nsamps=500000
r_min=1
r_max=10
trunc=FALSE
makePlot=function(){
xlim=c(-5,15)
op=par(mfrow=c(1,3))
hist(r,breaks=100,freq=FALSE,xlim=xlim)