Skip to content

Instantly share code, notes, and snippets.

View crowding's full-sized avatar

Peter Meilstrup crowding

View GitHub Profile
@crowding
crowding / README.Rmd
Last active December 22, 2015 03:08
Performance tuning rbind.fill
## Performance tuning `rbind.fill`
```{r setup, include=FALSE, cache=FALSE}
library(methods)
opts_chunk$set(cache=TRUE, dev="svg", tidy=FALSE,
fig.width=5, fig.height=5, autodep=TRUE,
fig.path='figure-')
with_dir <- function(dir, expr) {
prev <- getwd()
setwd(dir)
@crowding
crowding / spatial-interactions.R
Created July 16, 2013 01:55
A toy model of a neuron with a gaussian receptive field responding to sparse noise, and recovering parameters with Stan
library(plyr)
library(ggplot2)
library(R.cache)
library(rstan)
#There is a sensor which either emits "yes" or "no." It adds up
#signals over some region of space (The "receptive field", which we
#model as having a Gaussian profile,) and emits a "yes" if the
#summed-up spatially weighted signals, plus unit additive noise, are
#over some threshold.
@crowding
crowding / bdd.R
Last active May 9, 2023 11:56
R Code to simulate and describe a reaction-time decision process
library(ggplot2)
library(plyr)
theme_set(theme_bw())
theme_update(panel.border=element_blank())
# A reaction time experiment works as follows: On each trial, an
# observer is asked to view a stimulus and categorize it into one of
# multiple values. For instance, it may be a noisy motion stimulus,
# and the observer is asked to classify the motion as "leftward" or
@crowding
crowding / latex-math.r
Last active December 17, 2015 08:08 — forked from hadley/latex-math.r
# User facing function
#
# to_math(x_1 + 1^{2 + 4} + 5 + sqrt(y) / 5 %/% 10)
# to_math(paste(x^2, y - 1, z_i))
# to_math(hat(tilde(ring(x))))
# to_math(pi*r^2)
# to_math(unknown_call(x, floor(sqrt(z))))
to_math <- function(x) {
x <- substitute(x)
env <- latex_env(x)
@crowding
crowding / .bash_profile
Created March 11, 2013 22:10
if a command takes longer than some timeout, say "I'm done"
#.bash_profile
TIMEOUT=10
function speak_if_long {
last_command_time=$(HISTTIMEFORMAT="%s " history 1 | awk '{print $2}')
if (( $(date -u +"%s") - $last_command_time > $TIMEOUT ))
then
say "I'm done"
fi
}
@crowding
crowding / expressions.R
Created January 20, 2013 08:37
enumerate all expressions w. some oberands and operators
##
{
## let's go ahead and generate the possibilities.
x <- 2
y <- pi
z <- exp(1)
`%^%` <- `^`
`%/%` <- `/`
@crowding
crowding / flymake-r.el
Created October 31, 2012 04:58
Flymake support for R code
;; R flymake support (if Flymake is available) This will call a script
;; "rflymake" with the path given; make sure it is on emac's exec-path
;; or give a full path.
(when (require 'flymake nil)
(defun flymake-r-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
@crowding
crowding / unicode_ggplot2.R
Created October 23, 2012 09:32
Using Unicode plot symbols in ggplot2
#negative numbers for "symbol" will be interpreted as Unicode values.
#But you must use a fint that supprots the symbols you want.
dataset <- chain(1:16, expand.grid(x=., y=.), mutate(shape=seq_along(x)))
print(ggplot(dataset) + aes(x=x, y=y, shape=factor(shape))
+ geom_point()
+ discrete_scale("shape", "manual", name="shape", palette=function(x) -seq(0x25A0L, length=x))
+ theme(legend.position="none")
)
@crowding
crowding / extract.sh
Created September 20, 2012 06:18
Extracting a number of subdirectories into submodules
#!/bin/bash
set -v verbose
trap exit ERR
#I want to extract a number of submodules from a larger project.
#Also to preserve untracked files (e.g. lengthy data build products)
CWD=`pwd`
REPO_DIR="direction_discrimination_analysis"
SUBMODULES="mothballed_writing previous_draft newer_draft"
@crowding
crowding / .bash_profile
Created September 10, 2012 21:56
Bash prompt for ADD types
#put this in your ~/.bash_profile
#
#If a long-running command terminates, ring the bell and float the
#(OSX Terminal) window.
LONG_RUNNING=20 #how long is long? Enough for me to get bored and tab away.
function popup_if_long {
last_command_time=$(HISTTIMEFORMAT="%s " history 1 | awk '{print $2}')
if (( $(date -u +"%s") - $last_command_time > $LONG_RUNNING ))