Skip to content

Instantly share code, notes, and snippets.

Gary Feng garyfeng

  • Educational Testing Service
  • United States
Block or report user

Report or block garyfeng

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@garyfeng
garyfeng / 2017Writing_descFeature_g4.py
Last active Jul 17, 2018
The following scripts were used on 2018-07-09 to generate descriptive writing features for 2017 reporting. It's done on Kurt, using the pdia docker. Steps:
View 2017Writing_descFeature_g4.py
# Functions for extracting the descriptive features for the 2017 operational analysis
# Changes from make2017FinalFeatures.py:
# - min
#############
import StringIO
import os
import traceback
View CKEditorCursorPosition.js
// sample code for calculating the caret cursor position in CKEditor
// including functions to deal with cross-browser inconsistencies for end-of-the-line
// A hack to get text from HTML
// http://stackoverflow.com/questions/5002111/javascript-how-to-strip-html-tags-from-string
var html2text = function (html) {
// testing the idea of using .toString() with a replacement trick
// knowing that .textContent doesn't preserve the newlines, let's replace </p> with 2 newline chars
// This is ignoring all other HTML/CSS markups, such as <br>, tables, etc.
View Keystroke logging implementation.markdown
@garyfeng
garyfeng / HeatmapWithTransparentBackground.svg
Created Jun 2, 2016
Adding a transparent background image to Plot.ly's SVG output. Two changes: (a) set opacity in `<g class="subplot xy" style = "opacity:0.5">`, and (b) adding an image in the "layer-below >> imagelayer" group.
View HeatmapWithTransparentBackground.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View garyfengJupyterStyle.css
<style>
/*
Taken from http://moderndata.plot.ly/custom-styling-for-ipython-notebooks-with-3-lines-of-code/
Modified for Gary Feng's Jupyter notebooks
*/
/*
html {
font-size: 62.5% !important; }
View Keystroke logging implementation.markdown
@garyfeng
garyfeng / getListAttribute.r
Last active Sep 23, 2015
Creating an operator to extract a named member of a list that is in a list. Why? Read on. [Now part of the "pdata" library at github.com/garyfeng/pdata.
View getListAttribute.r
# To create an operator to extract a named member of a list that is in a list.
# This may sound confusing, but imagine you have a data.frame where a variable/column is a list of lists,
# and the lists have named members, e.g.,
# df$itinary <- list(list(from="NYC", to="LA", via="train"), list(from="LA", to="SF"), ...)
# You want to get the "from" value of itinary as a vector. You can do
# df$itinary@"from" or `@`(df$itinary, "via")
# Typically you'd use ```sapply(x, function(m) {m[["from"]]})```. The following is an extention to the idea in 2 ways:
# 1). We define a in-fix operator `@` that does so in a way that is syntactically more natural
# 2). We added error handling, in the case of bad indecies, etc.
require(testthat)
@garyfeng
garyfeng / interp.r
Created May 30, 2015
R tricks with lazyeval::interp():
View interp.r
# taken from
# http://cran.rstudio.com/web/packages/dplyr/vignettes/nse.html
# What if you need to mingle constants and variables? Use the handy lazyeval::interp():
library(lazyeval)
# Interp works with formulas, quoted calls and strings (but formulas are best)
interp(~ x + y, x = 10)
#> ~10 + y
interp(quote(x + y), x = 10)
#> 10 + y
@garyfeng
garyfeng / backFillNA.r
Last active Jan 13, 2016
A function to back fill NAs in a vector with the last non-NA value
View backFillNA.r
# A function to back fill NAs in a vector with the last non-NA value
# https://gist.github.com/garyfeng/27e7f8e406192a8cb33a
backFillNA<- function (x) {
nas<- which(is.na(x))
# trick from http://stackoverflow.com/questions/24837401/find-consecutive-values-in-vector-in-r
naList<-split(nas, cumsum(c(1, diff(nas) != 1)))
# get the backfill values
valueList<-lapply(naList, function(nalist) {
prev<- nalist[1]-1
@garyfeng
garyfeng / importData2TraMineR.r
Created May 30, 2015
TraMineR seqdef() is picky. Sometimes you need to define a new data frame just to get it recognized.
View importData2TraMineR.r
# I am mystified by how Traminer handles the data format.
# Here's the data from the 2015 NAEP Reading tryout study, SC block
# where I transformed the data to simplify the structure.
# Then I try to use seqformat() or seqdef() to turn the existing data frame
# in to a TraMineR object.
sc %>% filter(readingTime>0) %>% group_by(student) %>% arrange(readingTime) %>%
mutate(c1=1:n(), c2=2:(n()+1), t0=c(1, readingTime[1:n()-1])) %>%
select(student, c1, c2, state2, t0, readingTime, event2) %>% na.omit() ->scSPELL
You can’t perform that action at this time.