Skip to content

Instantly share code, notes, and snippets.

View atbradley's full-sized avatar

Adam Bradley atbradley

View GitHub Profile
@atbradley
atbradley / svg_link.js
Created May 15, 2015 17:28
Create a link (as a jQuery object) to an SVG on the current page.
@atbradley
atbradley / mapvars.xsl
Last active August 29, 2015 14:20
Alter two SVG US maps for use in the Hall-Hoag catalog project.
<?xml version="1.0" encoding="windows-1252"?>
<xsl:stylesheet xmlns="http://www.w3.org/2000/svg"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
version="1.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyzàèìòùáéíóúýâêîôûãñõäëïöüÿåæœçðø'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÈÌÒÙÁÉÍÓÚÝÂÊÎÔÛÃÑÕÄËÏÖÜŸÅÆŒÇÐØ'" />
@atbradley
atbradley / blog-to-solr.py
Created April 2, 2015 12:40
"Read" a Wordpress blog and update documents indexed in Solr with links to relevant posts, based on tags. Written to index the catalog for the Hall-Hoag collection (http://library.brown.edu/collatoz/info.php?id=62)
import json
from lxml import etree
import requests
from datetime import date
def findId(xml):
for cat in xml.findall('category'):
if cat.text.startswith('HH_'):
return cat.text
@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 / 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);
wordcount <- function(str) {
sapply(gregexpr("\\b\\W+\\b", str, perl=TRUE), function(x) sum(x>0) ) + 1
}
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 <[email protected]>
*
@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 / 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 / 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))