Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / mat_anno.tex
Created April 24, 2024 06:48
TikZ example: matrix with annotation
\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,decorations.pathreplacing, backgrounds, positioning}
\begin{document}
\begin{tikzpicture}[
%Styles
Matrix/.style={
matrix of nodes,
text height=2.5ex,
@abikoushi
abikoushi / heatmap.r
Created April 19, 2024 04:15
pheatmap: colored row labels text-by-text
library(pheatmap)
library(grid)
mid0scale <- function(test, paletteLength=120){
myColor <- colorRampPalette(c("orange", "white", "royalblue"))(paletteLength)
myBreaks <- c(seq(min(test), 0, length.out=ceiling(paletteLength/2) + 1),
seq(max(test)/paletteLength, max(test), length.out=floor(paletteLength/2)))
return(list(color=myColor, breaks=myBreaks))
}
set.seed(123)
@abikoushi
abikoushi / heatmap.r
Created April 18, 2024 11:30
set color-bar's mid-point to 0 (pheatmap)
library(pheatmap)
# length(breaks) == length(paletteLength) + 1
# use floor and ceiling to deal with even/odd length pallettelengths
mid0scale <- function(test, paletteLength=120){
myColor <- colorRampPalette(c("orange", "white", "royalblue"))(paletteLength)
myBreaks <- c(seq(min(test), 0, length.out=ceiling(paletteLength/2) + 1),
seq(max(test)/paletteLength, max(test), length.out=floor(paletteLength/2)))
return(list(color=myColor, breaks=myBreaks))
}
@abikoushi
abikoushi / centraldogma.tex
Created April 12, 2024 02:25
Tikz graph example
\documentclass[tikz,border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
%\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}
\node[draw, rounded corners, fill=gray!10](dna) at (0,0){DNA};
\node[draw, rounded corners, fill=gray!10, right = of dna, xshift = +15pt](rna){RNA};
\node[draw, rounded corners, fill=gray!10, right = of rna, xshift = +15pt](protein) {protein};
\node[draw, rounded corners, fill=gray!10, right = of protein](phenotype) {phenotype};
@abikoushi
abikoushi / PoisHMM.R
Created April 3, 2024 12:28
Poisson Hidden Markov Model
# 須山『ベイズ推論による機械学習入門』(講談社)の実装例です
softmax <- function(x){
maxx <- max(x)
exp(x-maxx)/sum(exp(x-maxx))
}
logp_x <-function(x,lambda,loglambda){
x*loglambda-lambda
}
logsumexp <- function(x){
@abikoushi
abikoushi / andrews.R
Created April 2, 2024 07:22
Andrews' curve using geom_function
library(ggplot2)
library(dplyr)
Andrews <- function(t,
Sepal.Length, Sepal.Width,
Petal.Length, Petal.Width,...){
root2 <- sqrt(2)
sapply(t, function(t){Sepal.Length/root2 +
Sepal.Width*sin(t) +
Petal.Length*cos(t) +
@abikoushi
abikoushi / statfuns.R
Created April 2, 2024 05:28
geom_function with facet_wrap
library(dplyr)
library(ggplot2)
set.seed(123)
fdf <- expand_grid(year=1:4, generation=1:4) %>%
mutate(mu=rnorm(16),sigma=abs(rnorm(16)))
layerfun <- split(fdf, interaction(fdf$year, fdf$generation)) %>%
lapply(function(x){
@abikoushi
abikoushi / lwrmodel.R
Created March 26, 2024 14:02
LWR model (deterministic)
#1D
simLWR <- function(r, m, u0,
sys_noise=0,
obs_noise=0,
explicit=FALSE){
n <- length(u0)
if(explicit){
A <- diag(1-r,n)
A[cbind(2:n,1:(n-1))] <- r
}else{
@abikoushi
abikoushi / bob.r
Created March 25, 2024 12:25
Lissajous Curve
library(gganimate)
library(dplyr)
th <- seq(-pi,pi,by=0.01)
a <- seq(1,6, by=0.1)
df <- data.frame(x=c(cos(outer(th,a))),
y=rep(sin(th), length(a)),
a=rep(a, each=length(th)))
ggplot(df, aes(x=x, y=y, group=a, colour=a))+
@abikoushi
abikoushi / diff2d.r
Created March 25, 2024 11:09
Numerical solution of the diffusion equation (finite difference approximation )
library(gganimate)
library(ggplot2)
#install.packages("gganimate")
diffeq <- function(u_ini, r, timestep){
M <- nrow(u_ini)
N <- ncol(u_ini)
u_old <- u_ini
U <- array(0, dim=c(M,N,timestep+1))
U[,,1] <- u_ini
tmp <- matrix(0,M,N)