Skip to content

Instantly share code, notes, and snippets.

View lukereding's full-sized avatar

luke reding lukereding

View GitHub Profile
@lukereding
lukereding / new_python38_39.md
Created June 23, 2021 10:36
New features in python 3.8 and 3.9

What's new in Python 3.8

The walrus operator

:= lets you assign variables as part of larger expressions.

Example:

@lukereding
lukereding / get_issue_comments.py
Created November 24, 2019 15:34
Summary counts of comments on issues by users in a github repo
import requests
import pandas as pd
import altair as alt
from typing import List, Tuple
def download_comments(repo: str, user: str) -> List[str]:
url = f"https://api.github.com/repos/{user}/{repo}/issues/comments"
res = requests.get(url)
if res.ok:
library(tidyverse)
## testing overall incidence of intransitivity
# number of intransitive fish in each experiment per the results
intransitive <- c(9, 6, 6)
# total number of fish in each experiment
total <- c(10, 11, 23) + intransitive
@lukereding
lukereding / party_flyer.R
Created April 14, 2018 21:30
Code to reproduce the flyer for john and I's graduation party
# edited from https://github.com/aschinchon/travelling-salesman-portrait/blob/master/frankenstein_TSP.R
library(imager)
library(dplyr)
library(ggplot2)
library(scales)
library(TSP)
urlfile <- "https://www.dropbox.com/s/4td7zbqohyw0f3g/creepy.jpg?dl=0"
require(tidyverse)
# computes things you want to know across the entire trial
do_it <- function(x) {
name <- x$filename[1]
fish_name <- name %>% stringr::str_match(pattern = "_S[a-z]+?_([A-Za-z]+)_") %>% as.vector %>% .[2]
temp <- x %>%
mutate(time_zone = lead(time) - time) %>%
group_by(code) %>%
@lukereding
lukereding / make_vids_variable_speeds.sh
Created November 3, 2017 15:20
bash script to download an animation and create video with variable speeds
# the goal:
## download the animation
## use ffmpeg to make it playable
## change the speed of the animation
## randomally change the speed of the animation
## concatenate multiple speeds together
# make a directory to keep things tidy
mkdir create_video
cd create_video
@lukereding
lukereding / make_video.sh
Created October 18, 2017 20:53
example shell script
#!/usr/bin/env bash
# this script downloads an example animation from the internet and creates looped version of the animation that you could then use in a mate choice study
# the advantage of the script as opposed to doing this in something like iMovie:
### time. On my computer this script takes <30 seconds to execute. Doing this in iMovie could easily take half an hour.
### repeatability. I can send this script to you and you can make the video yourself.
### easy to remake videos when you make a mistake
### easy to keep track of what you've done. A plain text script like this allows you do _version control_ your work, allowing you to revert to a previous version and always know when you changed parts of your code
# make a directory to keep things tidy
data_frame(x=1:10) %>%
mutate(
# x2 = x*2,
x3 = x*3) %>%
filter(x>5)
@lukereding
lukereding / gist:9bacdc6e42dff610b91d3cdd94a5eb1a
Created August 24, 2017 13:36
cowplot, marginal plots, and coord_flip
library(ggplot2)
library(cowplot) # github version
# create dataframe with x and y with different ranges
df <- data.frame(x = rnorm(20, mean = 5, sd = 2), y = rnorm(20, mean = 20, sd = 5))
# create scatterplot
p <- ggplot(df, aes(x = x, y = y)) +
geom_point()
@lukereding
lukereding / example_ggraph.R
Created August 21, 2017 22:28
example of ggraph usage
df <- read_csv("http://datadryad.org/bitstream/handle/10255/dryad.152300/forDryad.csv?sequence=1")
df %<>% unite(uni, source, target)
edges <- df %>%
group_by(uni) %>%
count %>%
separate(col = "uni", into = c("source", "target"), sep = "_")
edges <- rename(edges, number = n)
net <-graph_from_data_frame(edges)