Skip to content

Instantly share code, notes, and snippets.

View tshort's full-sized avatar

Tom Short tshort

  • Electric Power Research Institute
View GitHub Profile
@tshort
tshort / str.jl
Created May 27, 2012 15:39
str for Julia
function str(io::IOStream, x, n::Int, indent)
T = typeof(x)
print(io, T, " ")
if isa(T, CompositeKind)
println(io)
if n > 0
for field in T.names
print(io, indent, " ", field, ": ")
str(io, getfield(x, field), n - 1, strcat(indent, " "))
end
@tshort
tshort / withdf.md
Created June 19, 2012 15:44
with/transform for DataFrames

Here are versions of with and transform! for DataFrame's. They each operate on columns.

function with(df::DataFrame, ex::Expr)
    # by-column with operation on df
    
    # helper function to replace symbols in ex with a reference to the
    # appropriate column in df
    replace_symbols(x, syms::Dict) = x
    replace_symbols(e::Expr, syms::Dict) = Expr(e.head, isempty(e.args) ? e.args : map(x -> replace_symbols(x, syms), e.args), e.typ)
@tshort
tshort / benchmarks.md
Created July 25, 2012 14:18
Julia benchmarks and bounds checking

Here are results of Julia's benchmarks with and without bounds checking:

DataFrame  (16,3)
                         type       benchmark     time
[1,]     "no bounds checking"           "fib" 0.210762
[2,]               "standard"           "fib" 0.211954
[3,]     "no bounds checking"        "mandel" 0.572634
[4,]               "standard"        "mandel" 0.587797
[5,]     "no bounds checking"     "parse_int" 0.207615
@tshort
tshort / t.md
Created August 20, 2012 22:34
test

test

# test

* bullet 1
  * bullet 1a

    indented=code();
@tshort
tshort / methodswith.md
Created September 5, 2012 21:50
Show method definitions that include a type

Here is the definition:

function methodswith(io, t::Type, m::Module)
    for nm in names(m)
        try
           mt = eval(nm)
           d = mt.env.defs
           while !is(d,())
               if any(map(x -> x == t, d.sig))
@tshort
tshort / julia_type.md
Created October 4, 2012 18:58
Convert a string to a Julia type
@tshort
tshort / csv.md
Created December 19, 2012 02:37
Explorations in reading CSV files in Julia

Matthew Dowle is working on a fast CSV reader for data.table. Here is test data case generated in R along with some timings:

require(data.table)

n=1e6
DT = data.table( a=sample(1:1000,n,replace=TRUE),
                 b=sample(1:1000,n,replace=TRUE),
                 c=rnorm(n),
                 d=sample(c("foo","bar","baz","qux","quux"),n,replace=TRUE),
@tshort
tshort / testing.md
Created April 1, 2013 15:17
JLD experiments with DataFrames

Here is an extension to JLD to try to support reading of DataFrames. Writing seems to be okay.

import HDF5.read, JLD.JldFile, JLD.getrefs

function read{D<:AbstractDataFrame}(obj::HDF5Dataset{JldFile}, ::Type{D})
    kv = getrefs(obj, Any)
    keys = kv[1]
    vals = kv[2]
    d = D()
@tshort
tshort / DFVector.jl
Created April 10, 2013 17:44
Explorations with column keys for DataFrames
importall Base
using DataFrames
type IX
df::AbstractDataFrame
end
IX(args...) = IX(DataFrame([args...]'))
length(x::IX) = 1
type DFVector <: AbstractArray{AbstractDataFrame,1}
df::AbstractDataFrame
end
@tshort
tshort / withmacro.md
Last active August 29, 2015 13:55
Playing with an @with macro

@with macro

Here is an @with macro to evaluate DataFrame (or even Dict) columns using references.

replace_syms(x, membernames) = x
function replace_syms(e::Expr, membernames)
    if e.head != :quote
        return Expr(e.head, (isempty(e.args) ? e.args : map(x -> replace_syms(x, membernames), e.args))...)
    else