Skip to content

Instantly share code, notes, and snippets.

View swo's full-sized avatar

Scott Olesen swo

View GitHub Profile
@swo
swo / italic_facets.R
Last active August 30, 2024 02:37
Italics in ggplot facet labels
# Say I have some short-hand labels like "Ec/q",
# which actually stand for "E. coli & quinolones",
# but I want "E. coli" to be in italics
library(tidyverse)
n <- 100
tibble(
x = rnorm(n),
@swo
swo / sigfig.R
Created December 2, 2019 19:34
significant figures
sigfig <- function(x, n = 2) {
formatC(signif(x, digits = n), digits = n, format = "fg", flag = "#")
}
@swo
swo / two-axis-example.R
Last active March 18, 2022 15:54
Making a second axis on ggplot
library(tidyverse)
# "faithful" is a dataset about a geyser, showing size of eruption vs. waiting
# time before the eruption
data <- as_tibble(datasets::faithful) %>%
mutate(index = 1:n()) %>%
head(10)
data
@swo
swo / polars_cheat_sheet.py
Last active December 6, 2023 03:24
Polars cheat sheet
import polars as pl
# Making data frames ------------------------------------------
# with dictionary
df = pl.DataFrame({
'name': ['foo', 'bar', 'baz'],
'bar': [0, 1, 2],
'qux': [0.0, 1.0, 2.0]
})
@swo
swo / multipage_pdf.R
Created November 7, 2023 15:26
Make a multipage pdf with ggplot
#' @param list_of_plots plots to save, one per page
#' @param path path to output pdf
#' @param ... additional arguments to `ggplot2::ggsave()`
save_multipage_pdf <- function(list_of_plots, path, ...) {
# path must end with .pdf
stopifnot(grepl("\\.pdf$", path))
ggplot2::ggsave(
path,
gridExtra::marrangeGrob(list_of_plots, ncol = 1, nrow = 1)
...
@swo
swo / gold.py
Created November 12, 2024 18:44
import polars as pl
import numpy as np
baseline_pars = {
"N": int(1e3),
"p_hospitalized": 0.10,
"hosp_time_fun": lambda rng, size: rng.normal(loc=0.0, scale=1.0, size=size),
"p_vax": 0.25,
"vax_time_fun": lambda rng, size: rng.normal(loc=0.0, scale=1.0, size=size),
"ve": 0.50,
@swo
swo / rng.py
Last active November 13, 2024 16:16
Why use `np.random.Generator` objects
import numpy as np
class MyRNG:
def __init__(self, seed):
# store the global state, so we can reset it
global_state = np.random.get_state()
# set the seed and get the state
np.random.seed(seed)
self.state = np.random.get_state()
@swo
swo / super.py
Created November 22, 2024 19:40
Python `__super__` calls
class MyClass:
def __init__(self):
self.report()
def report(self):
print("Initializing MyClass")
class MySubClass(MyClass):
def __init__(self):
@swo
swo / peaks.R
Created December 3, 2024 19:25
Find peaks in a vector
#' Find peaks in a vector
#'
#' Which elements of a vector are greater than the value to their left and
#' right? First and last values need only be greater than their adjacent value.
#'
#' @param x vector of values
#' @return boolean vector of peak positions
is_peak <- function(x) {
n <- length(x)
x_i_minus_1 <- c(-Inf, x[1:(n - 1)])
@swo
swo / diff0.py
Created December 6, 2024 23:50
polars diff with leading zero
def diff0(x: pl.Expr, first_value=0.0) -> pl.Expr:
"""
Get the differenced vector, putting a certain value (not null)
in the first place
"""
i = pl.arange(0, x.len())
return pl.when(i == 0).then(first_value).otherwise(x.diff())