Skip to content

Instantly share code, notes, and snippets.

View CnrLwlss's full-sized avatar

Conor Lawless CnrLwlss

View GitHub Profile
@CnrLwlss
CnrLwlss / flatim.py
Created March 12, 2024 10:15
Making single channel image from multichannel images (RGB in this example).
import numpy as np
from PIL import Image
# Open a multichannel image
# In this example, an RGB image
fname = r"C:\Users\Conor\Downloads\PXL_20240304_174814563.MP.jpg"
im = Image.open(fname)
arr = np.array(im,dtype=np.uint8)
@CnrLwlss
CnrLwlss / zero_correlation.R
Created January 12, 2024 12:39
Sometimes zero correlation does not mean "randomly distributed", rather just "no effect".
seed(99)
N = 100
x = runif(N)
noise = rnorm(N,mean=0,sd=0.1)
y_corr = 1*x + noise
y_nocorr = noise
rsq_corr = cor(x,y_corr)
rsq_nocorr = cor(x,y_nocorr)
plot(x,y_corr,xlim=c(0,1),ylim=c(-0.3,1.0),pch=16,xlab="x",ylab="y",cex.lab=1.55)
@CnrLwlss
CnrLwlss / pdfGrid.py
Created January 4, 2024 09:51
Writes a single page pdf of width w mm and height h mm (defaults are A4) with a nrow x ncol grid traced out on it, to file outf. Grid is a thin line (thickness is lwd) coloured grey (default is very light grey) with dashing specified by dash length dl and space length sl.
from fpdf import FPDF
def makeGrid(w=210.0, h=297.0, nrow=5, ncol=4, outf="grid.pdf", grey = 220, dl=3, sl=2, lwd=0.2):
'''Writes a single page pdf of width w mm and height h mm (defaults are A4) with a nrow x ncol grid
traced out on it, to file outf. Grid is a thin line (thickness is lwd) coloured grey (default is very
light grey) with dashing specified by dash length dl and space length sl.'''
pdf = FPDF(orientation="P", unit="mm", format=(w,h))
deltax = float(w)/ncol
deltay = float(h)/nrow
@CnrLwlss
CnrLwlss / Long and wide stripcharts.R
Created November 28, 2023 12:52
Plotting long and wide stripcharts
# Plot wide data
test = data.frame(id=1:4,"A"=1:4,"B"=5:8,"C"=11:14)
# Plot original order
stripchart(test[,2:4])
# Plot updated order
stripchart(test[,c("C","A","B")])
# Plot long data
test2 = reshape(test,direction="long",varying=c("A","B","C"),v.names="Value",idvar=c("id"),timevar=c("Label"),times=c("A","B","C"))
# Plot original order
@CnrLwlss
CnrLwlss / freeze.R
Last active May 31, 2023 11:47
Simulating protein pixel intensities in fibre sections with freezing artefacts
# A possible problem with freezing artefacts and correlations
# Need MASS library to sample from multivariate normal distribution (correlated datasets)
library(MASS)
R_good = matrix(c(0.55, 0.5,
0.5, 0.55),
nrow = 2, ncol = 2, byrow = TRUE)
R_bad = matrix(c(1, 0.5,
0.5, 1),
@CnrLwlss
CnrLwlss / captureVid.py
Created March 3, 2023 15:07
Previewing and then capturing HD video frames from webcam with Python
import cv2
import time
from pathlib import Path
import os
cam = cv2.VideoCapture(1)
cam.set(3, 1920)
cam.set(4, 1080)
cv2.namedWindow("test")
@CnrLwlss
CnrLwlss / Atif_seg.py
Last active June 21, 2022 15:37
Segmenting annotated image
import numpy as np
from PIL import Image
import time
start = time.time()
# Find bounding box using numpy
# https://stackoverflow.com/questions/31400769/bounding-box-of-numpy-array
def bbox2(img):
rows = np.any(img, axis=1)
@CnrLwlss
CnrLwlss / linear_class.R
Last active May 21, 2022 17:04
Investigating linear regression classification of OXPHOS data
library("MASS")
# Generate some synthetic (NPC-corrected) control data
N_ctrl = 666
sigma_ctrl = matrix(c(2,3,3,6),nrow=2,ncol=2)
ctrl = data.frame(mvrnorm(n = N_ctrl, mu=c(5,7.5), sigma_ctrl),stringsAsFactors=FALSE)
colnames(ctrl) = c("VDAC1","NDUFB8")
# Generate some synthetic (NPC-corrected) patient data
# This dataset represents a patient with a nuclear-encoded defect in CI
@CnrLwlss
CnrLwlss / SelectingPoints.R
Last active April 26, 2022 15:09
Interactive selection of points in a scatterplot.
library(sp)
xvals = rnorm(1000)
yvals = rnorm(1000)
alpha = 0.1
# Note you need to terminate by right-clicking and selecting "stop"
plot(xvals,yvals,xlab="x",ylab="y",pch=16,col=rgb(1,0,0,alpha))
res = locator(n=4000, type="o")
selected = point.in.polygon(xvals,yvals,res$x,res$y)==1
@CnrLwlss
CnrLwlss / betaPrior.R
Created January 24, 2022 17:04
Trying to visualise Beta prior predictives.
prior1 = function(n=1) {
runif(n,min=0.0,max=2.0)
}
prior2 = function(n=1) {
15
}
sampleprior = function(n=1){
return(replicate(n,rbeta(n=1,shape1=prior1(),shape2=prior2())))
}