Skip to content

Instantly share code, notes, and snippets.

View ttimbers's full-sized avatar

Tiffany A. Timbers ttimbers

View GitHub Profile
@ttimbers
ttimbers / get_worksheet_10.py
Created July 29, 2019 14:18
Example of how to get a homework assignment (name worksheet 10) from Canvas API and put it in the correct directory for grading with nbgrader
import os
import shutil
import requests
import urllib
from urllib.request import urlretrieve
canvas_token = os.environ["CANVAS_TOKEN"]
resp = requests.get(
url=f"https://canvas.ubc.ca/api/v1/courses/19078/assignments/313587/gradeable_students",
headers={
@ttimbers
ttimbers / get_student_id.py
Created July 29, 2019 13:56
Example of how to get student ID's from Canvas API
# assumes instructor's Canvas token is stored as an environmental variable called CANVAS_TOKEN
import os
import requests
canvas_token = os.environ["CANVAS_TOKEN"]
resp = requests.get(
url=f"https://canvas.ubc.ca/api/v1/courses/19078/enrollments",
headers={
"Authorization": f"Bearer {canvas_token}",
"Accept": "application/json+canvas-string-ids"
@ttimbers
ttimbers / nest.py
Last active August 9, 2019 22:39
Example of a simple function to create a nested data frame in python
import pandas as pd
from gapminder import gapminder
def nest(grouped_data):
'''Takes in a grouped data frame'''
groups = []
datas = []
for group in grouped_data.groups:
groups.append(group)
datas.append(grouped_data.get_group(group))
@ttimbers
ttimbers / between_ss_total_SS_ratio_kmeans.R
Created March 29, 2019 21:59
How between_SS/ total_SS changes with sample size
set.seed(123)
for(n in c(50, 200, 500, 1000, 2000, 10000) ) {
x1 <- data.frame(x1 = rnorm(n, 3), x2 = rnorm(n, 3))
x2 <- data.frame(x1 = rnorm(n, -2), x2 = rnorm(n, -2))
x <- rbind(x1, x2)
a <- kmeans(x, centers= 2)
print(paste0('Sample size: ', n, ' - between_SS / total_SS: ', round(100 * a$betweenss/a$totss, 2)))
}
@ttimbers
ttimbers / computational_lab_notebook_platforms.md
Created February 9, 2017 17:48
Computational/Data Science lab notebook platforms/resources
@ttimbers
ttimbers / read_data.R
Last active January 22, 2017 01:31
Read data from arduino into R
library(tidyverse)
library(grid)
f <- file("/dev/cu.usbserial-DA01HLQC", open="r")
data <- data.frame(scan(f, n=60, what = "double", allowEscapes = TRUE, sep = "\n"))
close(f)
colnames(data) <- "all_data"
parsed_data <- separate(data, all_data,
c("voltage_t",
test <- as.character(c(1,1,1,0,0,1,1,1,1,0,1,1,1,1,0,0,0,0,1,1,1))
test_rle <- rle(test)
unique_lengths <- make.unique(as.character(test_rle$lengths))
data.frame(unique_lengths,test_rle$lengths)
group_labels <- rep(unique_lengths, test_rle$lengths)
data.frame(test,group_labels)