Skip to content

Instantly share code, notes, and snippets.

View CnrLwlss's full-sized avatar

Conor Lawless CnrLwlss

View GitHub Profile
@CnrLwlss
CnrLwlss / parallelJuliaThreads.jl
Created January 19, 2022 13:52
Parallel Julia code
# Multithreading is the most straightforward type of parallel processing supported by Julia
# https://docs.julialang.org/en/v1/manual/multi-threading/#man-multithreading
# If your parallel computing problem doesn't involve shared memory state
# then this is probably the way to go
Nthreads = Threads.nthreads();
Ncalcs = Nthreads * 5
# Let's distribute Ncalcs operations across Nthreads threads
# We can have a look at which thread carries out each operation
@CnrLwlss
CnrLwlss / ConorsWayIsBest.R
Created November 19, 2021 15:21
Simulating two schemes for irregular sampling from regular timeseries
# Simulating two schemes for irregular sampling from regular timeseries
deltasim = 0.1
deltarec = 1/3
tmax = 10.0
simtimes = seq(0,tmax,0.1)
rectargets = seq(0,tmax,deltarec)
recindex = 1
@CnrLwlss
CnrLwlss / stochPlots.R
Last active October 8, 2021 16:56
Plotting individual results from discrete, dynamic stochastic simulations and their summary statistics.
flength = function(fname) length(readLines(fname))
for(droot in c("agent","gillespie")){
dirname = paste0(droot,"_csv_files")
fnames = file.path(dirname,list.files(dirname,pattern="*.csv"))
nfiles = length(fnames)
ntimes = flength(fnames[1])-1
times = 0:(ntimes-1) # You might want to change this to get the right units
@CnrLwlss
CnrLwlss / WarrenPriors.R
Created September 21, 2021 13:36
Prior beliefs for probabilities that a patient fibre is deficient (data from Warren et al. 2020 https://doi.org/10.1038/s41598-020-70885-3)
# Warren et al. (2020) https://doi.org/10.1038/s41598-020-70885-3
op = par(mfrow=c(2,2))
# Healthy control subjects (all proteins)
# C01, C02, C03
plot(function(x) dbeta(x,1,75),from=0,to=1.0,xlab="pi",ylab="Density",lwd=2, main="Controls")
abline(v=c(0,1),col="red",lwd=3)
# Note that we would expect RC defect to manifest in old age, so, in principle,
# this prior could change with age of patient at biopsy
@CnrLwlss
CnrLwlss / separate_plots.R
Created August 18, 2021 11:52
Separating plot rendering from analysis
# How to separate plotting from CPU intensive analysis
plotfuns = list()
Npts = 1000
Nsims = 12
# Visualising a 1D random walk, for example
makewalk = function(Npts){
# Do some like real heavy computing man
deltas = sample(c(-1,1),Npts,replace=TRUE)
walk = cumsum(deltas)
@CnrLwlss
CnrLwlss / QuantileLevelSetsContours.R
Created August 3, 2021 15:19
Contour plot where levels include quantiles instead of levels of constant density
library(MASS)
xdat = rnorm(1000)
ydat = rnorm(1000)
# https://stackoverflow.com/questions/16225530/contours-of-percentiles-on-level-plot
dens = kde2d(xdat, ydat, n=200); ## estimate the z counts
prob = c(0.95, 0.5)
dx = diff(dens$x[1:2])
dy = diff(dens$y[1:2])
@CnrLwlss
CnrLwlss / random_walk_noise.R
Created July 29, 2021 14:03
Visual representation of difference between stochastic process and samples from random distribution
N=10000
noise = rnorm(N,mean=0,sd=0.5)
random_walk=cumsum(noise)
ylim = range(random_walk)
op = par(mfrow=c(1,2))
plot(noise,type="l",ylim=ylim,xlab="time",cex.lab=1.75,cex.axis=1.75)
plot(random_walk,type="l",ylim=ylim,xlab="time",cex.lab=1.75,cex.axis=1.75)
par(op)
@CnrLwlss
CnrLwlss / zscore2Dmito.R
Last active June 11, 2021 12:27
zscores and 2Dmito scatterplots
# Make some synthetic data, like patient with v-shaped 2Dmito scatterplot
N = 500
mu_x = 5
sd_x = 2.5
intercept = 0.1
slope_normal = 1.0
slope_deficient = 0.0
sd_err = 1.0
x_normal = rnorm(N/2,mu_x,sd_x)
@CnrLwlss
CnrLwlss / gillespie.txt
Last active June 2, 2021 11:57
Gillespie algorithm mtDNA population dynamics
RATE OR STOICHIOMETRY
REACTIONS HAZARD (W) (M) PROBABILITIES
Synthesis W -> 2W ks*W +1 0 ks*W/CH
M -> 2M ks*M 0 +1 ks*M/CH
Degradation W -> NULL kd*W -1 0 kd*W/CH
M -> NULL kd*M 0 -1 kd*M/CH
__________
Combined Hazard (CH): (ks+kd)*(W+M)
@CnrLwlss
CnrLwlss / index.html
Last active May 28, 2021 15:05
Annotating images with annotorious. Live version here: http://mito.ncl.ac.uk/imserv/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Annotation|IMC images</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@recogito/annotorious@2.4.0/dist/annotorious.min.css">
<script src="https://cdn.jsdelivr.net/npm/@recogito/annotorious@2.4.0/dist/annotorious.min.js"></script>
<script language="javascript" type="text/javascript">
var key = 2;