This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
import re | |
st.title("A regex checker") | |
s = st.text_input("string") | |
r = re.compile(st.text_input("regex")) | |
st.write(re.findall(string=s, pattern=r)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import altair as alt | |
import numpy as np | |
import polars as pl | |
import streamlit as st | |
def logistic(x, y0, scale, x0, k): | |
return y0 + scale / (1 + np.exp(-k * (x - x0))) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sympy import * | |
import functools | |
a, b, z = var('a b z') | |
# discrete uniform pgf | |
G_du = (z ** a - z ** (b + 1)) / ((b - a + 1) * (1 - z)) | |
# pgf for 3d6 + 1d4 | |
G = G_du.subs(a, 1).subs(b, 6) ** 3 * G_du.subs(a, 1).subs(b, 4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass: | |
def __init__(self): | |
self.report() | |
def report(self): | |
print("Initializing MyClass") | |
class MySubClass(MyClass): | |
def __init__(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' @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) | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
}) |
NewerOlder