Skip to content

Instantly share code, notes, and snippets.

View airalcorn2's full-sized avatar

Michael A. Alcorn airalcorn2

View GitHub Profile
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
csv_url = "https://raw.githubusercontent.com/gradlab/CtTrajectories/main/data/ct_dat_clean.csv"
df = pd.read_csv(csv_url)
person_ids = df["Person.ID"].unique()
(min_x, max_x) = (df["Date.Index"].min(), df["Date.Index"].max())
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
households = 10000
infected_households_prop = 0.01
persons_per_household = 100
sample_prop = 0.001
samples = int(sample_prop * households * persons_per_household)
@airalcorn2
airalcorn2 / pytorch_dataloader_randomness.py
Last active December 21, 2024 15:51
Investigating the behavior of PyTorch's DataLoader when using randomness to generate samples.
# This bug was fixed in PyTorch 1.9. See:
# https://github.com/pytorch/pytorch/commit/aec83ff45ebd2cb3d4890cc97bffb1f367386392.
# See: https://pytorch.org/docs/stable/notes/faq.html#my-data-loader-workers-return-identical-random-numbers
# and: https://pytorch.org/docs/stable/data.html#multi-process-data-loading
# and: https://pytorch.org/docs/stable/data.html#randomness-in-multi-process-data-loading.
import numpy as np
import torch
@airalcorn2
airalcorn2 / grok_einsum.py
Created September 28, 2020 19:56
Grokking Einstein summation with NumPy.
import numpy as np
A = np.array([[1, 1, 1], [2, 2, 2], [5, 5, 5]])
B = np.array([[0, 1, 0], [1, 1, 0], [1, 1, 1]])
(i_s, j_s, k_s) = (len(A), len(A[0]), len(B[0]))
for do_sum in [True, False]:
if do_sum:
C = np.zeros((i_s, k_s))
print(np.einsum("ij,jk->ik", A, B))
@airalcorn2
airalcorn2 / network_outbreak_sim.py
Last active July 30, 2020 01:17
Simulating an outbreak in different social networks.
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import seaborn as sns
def sim_outbreak(G, exps, inf_prob, init_infect, lock):
pop_size = len(G)
total_infected = []
for exp in range(exps):
@airalcorn2
airalcorn2 / imagemagick.sh
Created March 25, 2020 18:36
ImageMagick commands.
# Create PDF montage from a directory of images.
montage -tile 2x100 "test/*" final.png
convert final.png final.pdf
@airalcorn2
airalcorn2 / covid19.py
Last active November 18, 2020 05:38
Trying to make sense of COVID-19 data given the testing strategies.
import matplotlib
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pgeocode
import re
import seaborn as sns
from datetime import datetime
@airalcorn2
airalcorn2 / linear_program.py
Created October 21, 2019 20:48
Solve and plot the level curves of a simple linear program.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import linprog
A_ub = np.eye(3)
b_ub = np.ones(3)
A_eq = np.array([[0.95, 0.9, 0.15]])
b_eq = np.ones(1)
c = -np.array([0.9, 0.02, 0.08])
@airalcorn2
airalcorn2 / make_dicom_gifs.py
Last active September 14, 2023 11:08
Convert DICOM data to GIFs.
# Derived from: https://github.com/pydicom/pydicom/blob/master/examples/input_output/plot_read_dicom_directory.py.
import imageio
import matplotlib.pyplot as plt
import numpy as np
import pydicom
from os import mkdir
from os.path import join
from PIL import Image
import os
from PIL import Image
image_fs = os.listdir()
image_fs.sort()
image_fs = image_fs[:9]
images = map(Image.open, image_fs)
(widths, heights) = zip(*(i.size for i in images))