Skip to content

Instantly share code, notes, and snippets.

View seabbs's full-sized avatar
🏠
Working from home

Sam Abbott seabbs

🏠
Working from home
View GitHub Profile
@cpfiffer
cpfiffer / pdf-to-structure.py
Last active November 14, 2024 05:48
Get structured output from PDFs. Goes through a PDF one page at a time -- it is not currently build for multiple pages, but could be extended as needed.
"""
pip install outlines torch==2.4.0 transformers accelerate typing-extensions pillow pdf2image rich requests
may need to install tkinter: https://stackoverflow.com/questions/25905540/importerror-no-module-named-tkinter
sudo apt-get install poppler-utils
"""
from enum import Enum
from io import BytesIO
@kathsherratt
kathsherratt / improving_modelling_responses.Rmd
Last active September 13, 2024 12:09
Improving modelling survey: responses
---
title: "Appendix: Responses to improving conditions for outbreak modelling"
date: "2024"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
library(knitr)
library(googlesheets4)
@adamkucharski
adamkucharski / epinow2_infection.R
Last active February 26, 2024 14:18
Simulated infection recovery
# Example code for simulation recovery of infection dynamics from deaths for COVID -----------------------------------------
# Load libraries:
# install.packages("EpiNow2", repos = "https://epiforecasts.r-universe.dev")
library(EpiNow2)
# Simulate data and delays -----------------------------------------
# Simulate infections with a sharp drop
@sbfnk
sbfnk / weekly_forecasts.r
Created October 12, 2023 09:48
Weekly forecast plotting with EpiNow2
#' Aggregate daily forecasts to weekly forecasts
#'
#' Aggregate daily case forecasts from regional_epinow2 to weekly forecasts.
#'
#' @param inf List output from `EpiNow2::regional_epinow()`. No default.
#' @param week_start Week start day. Default is 1 (Monday).
#' @param summary_quantiles Quantiles for forecast summary. Default is median, plus upper and lower bound for 20\%, 50\% and 90\% forecasts.
#' @importFrom dplyr group_by summarise filter mutate select case_when contains
#' @importFrom tidyr pivot_wider
#' @importFrom purrr map_df
@zsusswein
zsusswein / convolve.stan
Last active October 2, 2023 08:41
Convolve two sequences in Stan with the FFT. Matches `convolve(x, rev(y), type = "open")` in R.
functions {
vector convolve(vector a, vector b) {
int na = num_elements(a); // Vector lengths
int nb = num_elements(b);
int n_zero_a = nb - 1; // Zero padding lengths
int n_zero_b = na - 1;
vector[nb] b_rev = reverse(b); // The reversed b vector
vector[na + n_zero_a] a_pad; // Instantiate zero padded vectors
@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]
})
wd <- system("echo $(git rev-parse --show-toplevel)", intern = TRUE)
setwd(wd)
source(paste0(wd, "/influenza/scripts/depends.R"))
output_dir <- fs::path(paste0(wd, "/influenza/papers/univariate/outputs"))
# nhs_data path for getting population sizes:
nhs_data_path <- "" # Removed path to influenza data + population catchments per trust
# Purpose: This is just a play model to think about time spent on large and big grants/opportunities and
# overall likelihood of a successful result (undefined, just general feel).
# 2080 is the hours in a year if working 40 hours (because everyone does that...)
using JuMP
using Ipopt
model = Model(Ipopt.Optimizer)
p1 = .05 # Probability of winning grant 1
sx1 = .4 # Probability of successful work (class 1)
##' Get estimates shown at \url{https://epiforecasts.io/covid} for all times
##'
##' Nowcasts at \url{https://epiforecasts.covid} and the related github repository at
##' \url{https://github.com/epiforecasts/covid-rt-estimates} only cover the last 3
##' months. This function downloads all available estimates and applies a median
##' average to the provided quantiles to provide an estimate of a time series
##' covering all times available
##'
##' @param dataset character; data set corresponding to directories at
##' \url{https://github.com/epiforecasts/covid-rt-estimates}. Default is
@sbfnk
sbfnk / get_utla_estimates.r
Created June 17, 2021 11:32
get all UTLA-level Rt estimates
library("gh")
library("vroom")
owner <- "epiforecasts"
repo <- "covid-rt-estimates"
path <- "subnational/united-kingdom-local/cases/summary/rt.csv"
rt_commits <-
gh("/repos/{owner}/{repo}/commits?path={path}",
owner = owner,