Skip to content

Instantly share code, notes, and snippets.

View atbradley's full-sized avatar

Adam Bradley atbradley

View GitHub Profile
@atbradley
atbradley / 017-radioParadise.R
Created February 25, 2013 23:17
Setup to analyze playlist data from Radio Paradise, and an HTML chart function adapted from Matt Asher
songs <- read.table('http://data.adamtbradley.com/RadioParadisePlays.txt',
sep='-', skip=1, quote='', strip.white=T,
allowEscapes=T, col.names=c('time', 'artist', 'track'))
songs$artist <- sub('&ndash;', '-', songs$artist)
songs$track <- sub('&ndash;', '-', songs$track)
summ <- table(paste(songs$artist, songs$track, sep=": "))
summ.artists = table(songs$artist)
@atbradley
atbradley / unemploymentVideo.R
Created April 4, 2013 13:41
Depicts changing US unemployment rates as a video (mp4) choropleth.
#Warning: Messy code.
#(It's been repurposed a few times.)
library(plyr)
library(ggplot2)
plotData <- function(date) {
und <- c(unemp[unemp$Date==date, -1])
und <- und[order(names(und))]
und <- t(as.data.frame(und))
@atbradley
atbradley / latts.php
Last active December 16, 2015 17:09
Ensure that an associative array contains values for all required keys and only those keys. (Basically the PHP5 equivalent of Textpattern's lAtts() function. It's one of those things I'm sick of having to figure out every time I need it.)
<?php
$defaults=array('a'=>1, 'b'=>2, 'c'=>3);
$input=array('a'=> 'boo', 'c'=>4, 'd'=>5, 'e'=>6);
$output = array_intersect_key($input+$defaults, $defaults);
//Might want to provide a warning if $unused isn't empty.
$unused = array_diff_key($input, $defaults);
@atbradley
atbradley / statelify.R
Created May 2, 2013 20:56
Generates the HTML necessary to create a map using Stately (http://intridea.github.io/stately/). stately.csv contains state names and abbreviations with the correct letter for the Stately font.
#States is a data.frame with at least the following columns:
# $color: Any string identifiable as a color by CSS (It's included in a `style` attribute.
# $letter: The letter corresponding to this state in the Stately font.
#
# states$id and states$class will be used as the `id` and `class`
# attributes of the list elements for the states. All other columns
# will be inserted as data-<column-name> attributes (in case you want
# to use them with JavaScript).
statelify <- function(states) {
@atbradley
atbradley / smode.R
Created December 17, 2015 20:11
Function to return the statistical mode in R.
smode <- function(x) {
which(tabulate(x)==max(tabulate(x)))
}
define('TEMPLATE_DIR', 'path/to/my/templates');
/**
* Processes a PHP template.
*
* Looks in OCRA_TEMPLATE_DIR for {$template}.tpl, and includes it,
* probably generating an HTML page.
*
* @author Adam Bradley <hisself@adambradley.net>
*
@atbradley
atbradley / array_array_unique.php
Created July 26, 2013 18:12
Run array_unique() on an array containing arrays (array_unique() expects strings).
$outp = array_map('serialize', $outp);
$outp = array_unique($outp);
$outp = array_map('unserialize', $outp);
@atbradley
atbradley / pcp.paper.js
Created September 3, 2013 13:56
Draw an interactive parallel coordinates plot using Paper.js.
//Warning: Ugly code; first project with paper.js.
var lblHeight = 0;
var marX = 30;
var marY = 10;
var marDesc = 3;
var labels = [];
var dataurl = '/data/states.json';
//var dataurl = 'data/mtcars.json';
@atbradley
atbradley / pinboard_remove_unread_tweets.py
Created April 1, 2016 17:24
Remove "unread" tweets from Pinboard.
USER_TOKEN = 'pinboard:ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
import re
import requests
import sys
import time
links = requests.get('https://api.pinboard.in/v1/posts/all?auth_token='+USER_TOKEN+'&format=json')#&count=100')
if links.status_code != 200:
@atbradley
atbradley / find_marc_field.php
Last active July 26, 2016 18:14
Parse Blacklight's JSON representation of MaRC records.
<?php
/**
* Functions for treating (blacklight-based) Josiah as an API.
*/
namespace ocra\josiah;
/**
* Find a field (and optionally subfield) in the JSONified MaRCXML record.
*