module KUparser

using Compat
using KUnet

include("util.jl");	  export @date, evalparse, pxyequal, testnet
include("sentence.jl");   export Sentence, Corpus, wdim, wcnt, wtype
include("parser.jl");     export ArcEager13, ArcEagerR1, ArcHybrid13, ArcHybridR1, Parser, reset!
include("features.jl");   export Feature, Fvec, flen, xsize, ysize
include("gparser.jl");    export gparse
include("bparser.jl");    export bparse
include("oparser.jl");    export oparse
include("rparser.jl");    export rparse
include("flist.jl");      export Flist

end # module

include("resetworkers.jl");

Add a choice between dense and sparse features.

module KUparser

using Compat
using KUnet

include("util.jl");	  export @date, evalparse, pxyequal, testnet
include("sentence.jl");   export Sentence, Corpus, wdim, wcnt, wtype
include("parser.jl");     export ArcEager13, ArcEagerR1, ArcHybrid13, ArcHybridR1, Parser, reset!
include("features.jl");   export Feature, Fvec, flen, xsize, ysize
include("gparser.jl");    export gparse
include("bparser.jl");    export bparse
include("oparser.jl");    export oparse
include("rparser.jl");    export rparse
include("flist.jl");      export Flist

# Setting for dense vs sparse features:
features = dfeatures
# features = sfeatures

end # module

include("resetworkers.jl");


function getContent(req::Request, res::Response)
  loginPage = Page(tlaloc,"login.html",Dict())
  Response(render(loginPage))
end

function postContent(req::Request, res::Response)
  loginPage = Page(tlaloc,"login.html",Dict())
  loadModule("user")
  if haskey(dataPost, "username") && dataPost["username"] != "" && haskey(dataPost, "password") && dataPost["password"] != ""
    if length(checkUser(dataPost["username"], sha1(sha1(dataPost["password"])))) == 1
      Response(render(loginPage))
    else
      addArg(loginPage, "error", "Le nom d'utilisateur et le mot de passe ne correspondent pas.")
    end
  else
    addArg(loginPage, "error", "Certains champs sont vides")
  end
  Response(render(loginPage))
end

Add empty error message, which we'll remove when we have conditions in Tlaloc to see if the variable is set

function getContent(req::Request, res::Response)
  loginPage = Page(tlaloc,"login.html",Dict())
  addArg(loginPage, "error", "")
  Response(render(loginPage))
end

function postContent(req::Request, res::Response)
  loginPage = Page(tlaloc,"login.html",Dict())
  loadModule("user")
  if haskey(dataPost, "username") && dataPost["username"] != "" && haskey(dataPost, "password") && dataPost["password"] != ""
    if length(checkUser(dataPost["username"], sha1(sha1(dataPost["password"])))) == 1
      Response(render(loginPage))
    else
      addArg(loginPage, "error", "Le nom d'utilisateur et le mot de passe ne correspondent pas.")
    end
  else
    addArg(loginPage, "error", "Certains champs sont vides")
  end
  Response(render(loginPage))
end


@testset "Include All Files Function Tests" begin

  @test isdefined(Julz, :include_all_files) == true

end

Add test for “include_all_files” function

@testset "Include All Files Function Tests" begin

  @test isdefined(Julz, :include_all_files) == true

  initial_dir = pwd()

  cd("dummy")

  Julz.generate("function", "baz")

  @test isdefined(:baz) != true

  Julz.include_all_files("$(pwd())/src/functions")

  @test isdefined(:baz) == true

  Julz.destroy("function", "baz")

  cd(initial_dir)

end


#!/usr/bin/env julia

function picalc(numsteps)

  println("Calculating PI using:")
  println("  ", numsteps, " slices")
  println("  ", 1, " worker(s)")
  start = time()

  sum = 0.0
  slice = 1.0/numsteps

  sum = 0.0

  for i = 1:numsteps
    x = (i - 0.5) * slice
    sum = sum + (4.0/(1.0 + x^2))
  end

  mypi = sum * slice

  stop = time()

  elapsed = (stop - start)

  println("Obtained value of PI: ", mypi)
  println("Time taken: ", elapsed, " seconds")

end

numsteps=1000000000

if length(ARGS) > 0
  numsteps = parse(Int, ARGS[1])
end

picalc(numsteps)

Use SIMD in Julia serial example

#!/usr/bin/env julia

function _picalc(numsteps)

  slice = 1.0/numsteps

  sum = 0.0

  @simd for i = 1:numsteps
    x = (i - 0.5) * slice
    sum = sum + (4.0/(1.0 + x^2))
  end

  return sum * slice

end

function picalc(numsteps)

  println("Calculating PI using:")
  println("  ", numsteps, " slices")
  println("  ", 1, " worker(s)")
  start = time()

  mypi = _picalc(numsteps)

  elapsed = time() - start

  println("Obtained value of PI: ", mypi)
  println("Time taken: ", elapsed, " seconds")

end

numsteps = if length(ARGS) > 0
    parse(Int, ARGS[1])
else
    1_000_000_000
end

# Warm up kernel
_picalc(10)

# Run the full example
picalc(numsteps)


function transform(input::Dict)
    output = []
    for i = input
        for j = map(lowercase, i[2])
            pair = (j, i[1])
            push!(output, pair)
        end
    end
    Dict(sort(output))
end

Fix exercise etl: removed list sorting

function transform(input::Dict)
    output = []
    for i = input
        for j = map(lowercase, i[2])
            pair = (j, i[1])
            push!(output, pair)
        end
    end
    Dict(output)
end


#### Test Harness

using Morsel
app = Morsel.app()

route(app, GET, "/liba.tar") do req,sys
    path = dirname(@__FILE__)
    path = joinpath(path,"files")
    readall(`tar c liba`)
end

@async start(app, 4653)

#### Test script
using BinDeps

const testuri = URI("http://localhost:4653/liba.tar")
const shlib_ext = BinDeps.shlib_ext

@BinDeps.setup

deps = [
    liba = library_dependency("liba", aliases = ["liba","liba1","liba.1"])
]

provides(Sources,testuri,liba)
provides(BuildProcess,Autotools(libtarget = "liba.$shlib_ext"),liba)

@BinDeps.install [:liba => :jl_liba]

Add SHA line to tests

#### Test Harness

using Morsel
app = Morsel.app()

route(app, GET, "/liba.tar") do req,sys
    path = dirname(@__FILE__)
    path = joinpath(path,"files")
    readall(`tar c liba`)
end

@async start(app, 4653)

#### Test script
using BinDeps

const testuri = URI("http://localhost:4653/liba.tar")
const shlib_ext = BinDeps.shlib_ext

@BinDeps.setup

deps = [
    liba = library_dependency("liba", aliases = ["liba","liba1","liba.1"])
]

provides(Sources,testuri,liba,SHA="769c43644f239d8825cefc998124060cf9f477f94e8e338f6c3e17839470229d")
provides(BuildProcess,Autotools(libtarget = "liba.$shlib_ext"),liba)

@BinDeps.install [:liba => :jl_liba]


export SGD

"""
    SGD

Stochastic Gradient Descent.

* rate: learning rate
* momentum: momentum coefficient
"""
type SGD
    rate::Float64
    momentum::Float64
    states::ObjectIdDict
end

function SGD(rate, momentum=0.0)
    SGD(rate, momentum, ObjectIdDict())
end

@compat function (opt::SGD){T}(data::Array{T}, grad::Array{T})
    if opt.momentum != 0.0
        state = get!(opt.states, data, nothing)
        if state == nothing
            v = zeros(data)
            opt.states[data] = v
        else
            v = state
        end
        BLAS.scal!(length(v), T(opt.momentum), v, 1)
        BLAS.axpy!(T(opt.rate), grad, v)
        broadcast!(-, data, data, v)
    else
        BLAS.axpy!(-T(opt.rate), grad, data)
    end
    fill!(grad, T(0))
end

Change SGD momentum to a keyword argument

export SGD

"""
    SGD

Stochastic Gradient Descent.

* rate: learning rate
* momentum: momentum coefficient
"""
type SGD
    rate::Float64
    momentum::Float64
    states::ObjectIdDict
end

function SGD(rate; momentum=0.0)
    SGD(rate, momentum, ObjectIdDict())
end

@compat function (opt::SGD){T}(data::Array{T}, grad::Array{T})
    if opt.momentum != 0.0
        state = get!(opt.states, data, nothing)
        if state == nothing
            v = zeros(data)
            opt.states[data] = v
        else
            v = state
        end
        BLAS.scal!(length(v), T(opt.momentum), v, 1)
        BLAS.axpy!(T(opt.rate), grad, v)
        broadcast!(-, data, data, v)
    else
        BLAS.axpy!(-T(opt.rate), grad, data)
    end
    fill!(grad, T(0))
end


module Seq

using Compat
using Docile
using Base.Intrinsics

import Base: convert, complement, getindex, show, length, start, next, done, copy, reverse,
             show, endof, ==, isless, clipboard, parse, *, repeat, unsafe_copy!

export Nucleotide, DNANucleotide, RNANucleotide,
       DNA_A, DNA_C, DNA_G, DNA_T, DNA_N,
       RNA_A, RNA_C, RNA_G, RNA_U, RNA_N,
       NucleotideSequence, DNASequence, RNASequence, @dna_str, @rna_str,
       reverse_complement, mismatches, npositions, hasn, eachsubseq,
       eachkmer, NucleotideCounts, Kmer, DNAKmer, RNAKmer, dnakmer, rnakmer,
       kmer, AminoAcid, AminoAcidSequence, @aa_str, translate, ncbi_trans_table,
       AA_A, AA_R, AA_N, AA_D, AA_C, AA_Q, AA_E, AA_G, AA_H, AA_I, AA_L,
       AA_K, AA_M, AA_F, AA_P, AA_S, AA_T, AA_W, AA_Y, AA_V, AA_X

include("nucleotide.jl")
include("aminoacid.jl")
include("geneticcode.jl")
include("util.jl")

# TODO: pattern (pseudo-sequences with ambiguity codes, etc)

end # module Seq

Fix tests on Julia 0.4

module Seq

using Compat
using Docile
using Docile: @doc, @doc_str
using Base.Intrinsics

import Base: convert, complement, getindex, show, length, start, next, done, copy, reverse,
             show, endof, ==, isless, clipboard, parse, *, repeat, unsafe_copy!

export Nucleotide, DNANucleotide, RNANucleotide,
       DNA_A, DNA_C, DNA_G, DNA_T, DNA_N,
       RNA_A, RNA_C, RNA_G, RNA_U, RNA_N,
       NucleotideSequence, DNASequence, RNASequence, @dna_str, @rna_str,
       reverse_complement, mismatches, npositions, hasn, eachsubseq,
       eachkmer, NucleotideCounts, Kmer, DNAKmer, RNAKmer, dnakmer, rnakmer,
       kmer, AminoAcid, AminoAcidSequence, @aa_str, translate, ncbi_trans_table,
       AA_A, AA_R, AA_N, AA_D, AA_C, AA_Q, AA_E, AA_G, AA_H, AA_I, AA_L,
       AA_K, AA_M, AA_F, AA_P, AA_S, AA_T, AA_W, AA_Y, AA_V, AA_X

include("nucleotide.jl")
include("aminoacid.jl")
include("geneticcode.jl")
include("util.jl")

# TODO: pattern (pseudo-sequences with ambiguity codes, etc)

end # module Seq


abstract KalmanFilter

abstract LinearKalmanFilter <: KalmanFilter

abstract Model

abstract ObservationModel

type State{T}
    x::Vector{T}
    p::Matrix
end

Base.(:(==))(x1::State,x2::State) = x1.x==x2.x && x1.p == x2.p

type LinearModel <: Model
    a::Matrix
    g::Matrix
    q::Matrix
end

type Observation{T}
    y::Vector{T}
end

type LinearObservationModel <: ObservationModel
    h::Matrix
    r::Matrix
end

type BasicKalmanFilter <: LinearKalmanFilter
    x::State
    f::LinearModel
    z::LinearObservationModel
end

type BasicKalmanFilterP <: KalmanFilter
    x1::State
    f::LinearModel
    z::LinearObservationModel
end

Add basic conversion for Observations

abstract KalmanFilter

abstract LinearKalmanFilter <: KalmanFilter

abstract Model

abstract ObservationModel

type State{T}
    x::Vector{T}
    p::Matrix
end

Base.(:(==))(x1::State,x2::State) = x1.x==x2.x && x1.p == x2.p

type LinearModel <: Model
    a::Matrix
    g::Matrix
    q::Matrix
end

type Observation{T}
    y::Vector{T}
end

convert(::Observation,y) = Observation([y])

type LinearObservationModel <: ObservationModel
    h::Matrix
    r::Matrix
end

type BasicKalmanFilter <: LinearKalmanFilter
    x::State
    f::LinearModel
    z::LinearObservationModel
end

type BasicKalmanFilterP <: KalmanFilter
    x1::State
    f::LinearModel
    z::LinearObservationModel
end


using BinaryProvider # requires BinaryProvider 0.3.0 or later

# Parse some basic command-line arguments
const verbose = "--verbose" in ARGS
const prefix = Prefix(get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr")))

products = [
    LibraryProduct(prefix, ["libcsdp"], :libcsdp)
]

dependencies = [
    "build_OpenBLAS32.v0.3.9.jl",
    "build_CSDP.v6.2.0.jl"
]

for dependency in dependencies
    # it's a bit faster to run the build in an anonymous module instead of
    # starting a new julia process

    # Build the dependencies
    Mod = @eval module Anon end
    Mod.include(dependency)
end

# Finally, write out a deps.jl file
write_deps_file(joinpath(@__DIR__, "deps.jl"), products)

Use MySQL as example instead

# Inspired from https://github.com/JuliaDatabases/MySQL.jl/blob/36eaf2bfbbdd9a27c408d0b2a734fff0d81b63ad/deps/build.jl
module Anon1 end
module Anon2 end

@static if VERSION < v"1.3.0"

using BinaryProvider # requires BinaryProvider 0.3.0 or later

# Parse some basic command-line arguments
const verbose = "--verbose" in ARGS
const prefix = Prefix(get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr")))

products = [
    LibraryProduct(prefix, ["libcsdp"], :libcsdp)
]

Anon1.include("build_OpenBLAS32.v0.3.9.jl")
Anon2.include("build_CSDP.v6.2.0.jl")

# Finally, write out a deps.jl file
write_deps_file(joinpath(@__DIR__, "deps.jl"), products)

end # VERSION


using JuliaReport
using Base.Test

#Test chunk options and output formats

weave("documents/chunk_options.noweb", plotlib=nothing)
result = readall(open("documents/chunk_options.md"))
ref = readall(open("documents/chunk_options_ref.md"))

@test result == ref

weave("documents/chunk_options.noweb", doctype="tex", plotlib=nothing)
result = readall(open("documents/chunk_options.tex"))
ref = readall(open("documents/chunk_options_ref.tex"))

@test result == ref

weave("documents/chunk_options.noweb", doctype="texminted", plotlib=nothing)
result = readall(open("documents/chunk_options.tex"))
ref = readall(open("documents/chunk_options_ref.texminted"))

@test result == ref

weave("documents/chunk_options.noweb", doctype="rst", plotlib=nothing)
result = readall(open("documents/chunk_options.rst"))
ref = readall(open("documents/chunk_options_ref.rst"))

@test result == ref

#Test Gadfly and markdown reader
weave("documents/gadfly_markdown_test.jmd", doctype="github",plotlib="gadfly", informat="markdown")
result = readall(open("documents/gadfly_markdown_test.md"))
ref = readall(open("documents/gadfly_markdown_test_ref.md"))

@test result == ref

Disable Gadfly test for julia 0.4

using JuliaReport
using Base.Test

#Test chunk options and output formats

weave("documents/chunk_options.noweb", plotlib=nothing)
result = readall(open("documents/chunk_options.md"))
ref = readall(open("documents/chunk_options_ref.md"))

@test result == ref

weave("documents/chunk_options.noweb", doctype="tex", plotlib=nothing)
result = readall(open("documents/chunk_options.tex"))
ref = readall(open("documents/chunk_options_ref.tex"))

@test result == ref

weave("documents/chunk_options.noweb", doctype="texminted", plotlib=nothing)
result = readall(open("documents/chunk_options.tex"))
ref = readall(open("documents/chunk_options_ref.texminted"))

@test result == ref

weave("documents/chunk_options.noweb", doctype="rst", plotlib=nothing)
result = readall(open("documents/chunk_options.rst"))
ref = readall(open("documents/chunk_options_ref.rst"))

@test result == ref

#Test Gadfly and markdown reader, Gadfly only works with 0.3
if VERSION.minor == 3
    weave("documents/gadfly_markdown_test.jmd", doctype="github",plotlib="gadfly", informat="markdown")
    result = readall(open("documents/gadfly_markdown_test.md"))
    ref = readall(open("documents/gadfly_markdown_test_ref.md"))
    @test result == ref
end


export Parameters, default_parameters

immutable Parameters
    mu::Integer
    lambda::Integer
    mutrate::FloatingPoint
    targetfitness::FloatingPoint

    numinputs::Integer
    numoutputs::Integer

    numperlevel::Integer
    numlevels::Integer
    numlevelsback::Integer

    funcs::Vector{Func}
end

function Parameters(numinputs, numoutputs, numperlevel, numlevels, numlevelsback)
    mu = 1
    lambda = 4
    mutrate = 0.05
    targetfitness = 0.0
    funcs = default_funcs()

    return Parameters(mu, lambda, mutrate, targetfitness, numinputs, numoutputs, numperlevel, numlevels, numlevelsback, funcs)
end

Change parameters defaults to be useful.

export Parameters

immutable Parameters
    mu::Integer
    lambda::Integer
    mutrate::FloatingPoint
    targetfitness::FloatingPoint

    numinputs::Integer
    numoutputs::Integer

    numperlevel::Integer
    numlevels::Integer
    numlevelsback::Integer

    funcs::Vector{Func}
end

function Parameters(numinputs, numoutputs, numperlevel, numlevels, numlevelsback)
    mu = 2
    lambda = 2
    mutrate = 0.05
    targetfitness = 0.0
    funcs = default_funcs()

    return Parameters(mu, lambda, mutrate, targetfitness, numinputs, numoutputs, numperlevel, numlevels, numlevelsback, funcs)
end


module CoverageBase
using Coverage
export testnames, runtests

const need_inlining = ["reflection", "meta"]

function julia_top()
    dir = joinpath(JULIA_HOME, "..", "share", "julia")
    if isdir(joinpath(dir,"base")) && isdir(joinpath(dir,"test"))
        return dir
    end
    dir = JULIA_HOME
    while !isdir(joinpath(dir,"base"))
        dir, _ = splitdir(dir)
        if dir == "/"
            error("Error parsing top dir; JULIA_HOME = $JULIA_HOME")
        end
    end
    dir
end
const topdir = julia_top()
const basedir = joinpath(topdir, "base")
const testdir = joinpath(topdir, "test")
include(joinpath(testdir, "choosetests.jl"))

function testnames()
    Base.JLOptions().can_inline == 1 && return need_inlining

    names, _ = choosetests()
    filter!(x -> !in(x, need_inlining), names)

    # Manually add in `pkg`, which is disabled so that `make testall` passes on machines without internet access
    push!(names, "pkg")
    names
end

function runtests(names)
    cd(testdir) do
        for tst in names
            println(tst)
            include(tst*".jl")
        end
    end
end

end # module

Remove need_inline tests and enable coverage run with inlining on

module CoverageBase
using Coverage
export testnames, runtests

const need_inlining = []

function julia_top()
    dir = joinpath(JULIA_HOME, "..", "share", "julia")
    if isdir(joinpath(dir,"base")) && isdir(joinpath(dir,"test"))
        return dir
    end
    dir = JULIA_HOME
    while !isdir(joinpath(dir,"base"))
        dir, _ = splitdir(dir)
        if dir == "/"
            error("Error parsing top dir; JULIA_HOME = $JULIA_HOME")
        end
    end
    dir
end
const topdir = julia_top()
const basedir = joinpath(topdir, "base")
const testdir = joinpath(topdir, "test")
include(joinpath(testdir, "choosetests.jl"))

function testnames()
    names, _ = choosetests()
    if Base.JLOptions().can_inline == 0
        filter!(x -> !in(x, need_inlining), names)
    end

    # Manually add in `pkg`, which is disabled so that `make testall` passes on machines without internet access
    push!(names, "pkg")
    names
end

function runtests(names)
    cd(testdir) do
        for tst in names
            println(tst)
            include(tst*".jl")
        end
    end
end

end # module


using Gadfly

set_default_plot_size(6inch, 6inch)

import Base: show, +, -, /, *, isless, one, zero, isfinite

struct Percent
    value::Float64
end

# Functions necessary for plotting

+(a::Percent, b::Percent) = Percent(a.value + b.value)
-(a::Percent, b::Percent) = Percent(a.value - b.value)
-(a::Percent) = Percent(-a.value)
*(a::Percent, b::Float64) = Percent(a.value * b)
*(a::Float64, b::Percent) = Percent(a * b.value)

# Must return something that can be converted to Float64 with float64(a/b)
/(a::Percent, b::Percent) = a.value / b.value

isless(a::Percent, b::Percent) = isless(a.value, b.value)
one(::Type{Percent}) = Percent(0.01)
zero(::Type{Percent}) = Percent(0.0)
isfinite(a::Percent) = isfinite(a.value)
show(io::IO, p::Percent) = print(io, round(100 * p.value, digits=4), "%")

y=[Percent(0.1), Percent(0.2), Percent(0.3)]
plot(x=collect(1:length(y)), y=y)

Fix failing test by adding necessary convert method

using Gadfly

set_default_plot_size(6inch, 6inch)

import Base: convert, show, +, -, /, *, isless, one, zero, isfinite

struct Percent
    value::Float64
end

# Functions necessary for plotting

+(a::Percent, b::Percent) = Percent(a.value + b.value)
-(a::Percent, b::Percent) = Percent(a.value - b.value)
-(a::Percent) = Percent(-a.value)
*(a::Percent, b::Float64) = Percent(a.value * b)
*(a::Float64, b::Percent) = Percent(a * b.value)

# Must return something that can be converted to Float64 with float64(a/b)
/(a::Percent, b::Percent) = a.value / b.value

isless(a::Percent, b::Percent) = isless(a.value, b.value)
one(::Type{Percent}) = Percent(0.01)
zero(::Type{Percent}) = Percent(0.0)
isfinite(a::Percent) = isfinite(a.value)
convert(::Type{Float64}, x::Percent) = x.value
show(io::IO, p::Percent) = print(io, round(100 * p.value, digits=4), "%")

y=[Percent(0.1), Percent(0.2), Percent(0.3)]
plot(x=collect(1:length(y)), y=y)


export list, image, link, abbr

@api list => List <: Tile begin
    curry(tiles::AbstractArray)
    kwarg(ordered::Bool=false)
end

render(l::List) =
    Elem(l.ordered ? :ol : :ul,
         map(x -> Elem(:li, render(x)), l.tiles))

@api image => Image <: Tile begin
    arg(url::String)
    kwarg(alt::String="")
end

render(i::Image) =
    Elem(:div, Elem(:img, src=i.url, alt=i.alt))

@api link => Hyperlink <: Tile begin
    arg(url::String)
    curry(tiles::TileList)
end

render(a::Hyperlink) =
    Elem(:a, render(a.tiles), href=a.url)

@api abbr => Abbr <: Tile begin
    arg(title::String)
    curry(tiles::TileList)
end

render(a::Abbr) =
    Elem(:abbr, render(a.tiles), title=a.title)

Fix images in flex boxes, but not really :\

export list, image, link, abbr

@api list => List <: Tile begin
    curry(tiles::AbstractArray)
    kwarg(ordered::Bool=false)
end

render(l::List) =
    Elem(l.ordered ? :ol : :ul,
         map(x -> Elem(:li, render(x)), l.tiles))

@api image => Image <: Tile begin
    arg(url::String)
    kwarg(alt::String="")
end

render(i::Image) =
    Elem(:img, src=i.url, alt=i.alt, style=["width"=>"auto", "height"=>"auto", "display" => "block"])

@api link => Hyperlink <: Tile begin
    arg(url::String)
    curry(tiles::TileList)
end

render(a::Hyperlink) =
    Elem(:a, render(a.tiles), href=a.url)

@api abbr => Abbr <: Tile begin
    arg(title::String)
    curry(tiles::TileList)
end

render(a::Abbr) =
    Elem(:abbr, render(a.tiles), title=a.title)


export Func, default_funcs

type Func
    func::Function
    arity::Integer
end

function default_funcs()
    return [Func(&, 2), Func(|, 2)]
end

Add new functions from AHW.

export Func, default_funcs

type Func
    func::Function
    arity::Integer
end

const AND = Func(&, 2)
const OR = Func(|, 2)
const XOR = Func($, 2)
const TRUE = Func(() -> 0xf, 0)

function default_funcs()
    return [AND, OR, XOR, TRUE]
end


module Kalman

export State,
Observation,
KalmanFilter,
LinearKalmanFilter,
BasicKalmanFilter,
BasicKalmanFilterP,
Model,
ObservationModel,
LinearModel,
LinearObservationModel,
predict,
update,
predictupdate,
EKF

include("types.jl")
include("filter.jl")
include("ekf.jl")

end # module

Remove old types and export new methods and types.

module Kalman

export State,
Observation,
KalmanFilter,
LinearKalmanFilter,
BasicKalmanFilter,
Model,
ObservationModel,
LinearModel,
LinearObservationModel,
predict,
update,
predictupdate,
predict!,
update!,
predictupdate!,
BasicExtendedKalmanFilter,
EKF

include("types.jl")
include("filter.jl")
include("ekf.jl")

using .EKF

end # module


module CGP

include("BitString.jl")
include("Func.jl")
include("Parameters.jl")
include("Node.jl")
include("Chromosome.jl")
include("Goal.jl")
include("Fitness.jl")
#include("Mutation.jl")
include("Mutate.jl")
include("Execute.jl")
include("Evolution.jl")

end

Remove reference to old Mutation.jl file.

module CGP

include("BitString.jl")
include("Func.jl")
include("Parameters.jl")
include("Node.jl")
include("Chromosome.jl")
include("Goal.jl")
include("Fitness.jl")
include("Mutate.jl")
include("Execute.jl")
include("Evolution.jl")

end


export mu_lambda

function mu_lambda(p::Parameters, goal::Goal, gens::Integer)
    μ = p.mu
    λ = p.lambda
    funcs = p.funcs
    perfect = p.targetfitness

    pop = @parallel (vcat) for _ = 1:(μ + λ)
        random_chromosome(p)
    end
    fit = @parallel (vcat) for c = pop
        fitness(c, goal)
    end
    perm = sortperm(fit, rev=true)

    for t = 1:gens
        if fit[perm[1]] == perfect # TODO: This is inexact
            return (pop[perm[1]], t)
        end

        mutpop = @parallel (vcat) for x = pop[perm][1:λ]
            mutate(deepcopy(x), funcs)
        end
        mutfit = @parallel (vcat) for x = mutpop
            fitness(x, goal)
        end

        newpop = vcat(pop[perm][1:μ], mutpop)
        newfit = vcat(fit[perm][1:μ], mutfit)

        pop = newpop
        fit = newfit

        perm = sortperm(fit, rev=true)
    end

    return (pop[perm][1], gens)
end

Remove unicode mu and lambda for compatibility.

export mu_lambda

function mu_lambda(p::Parameters, goal::Goal, gens::Integer)
    mu = p.mu
    lambda = p.lambda
    funcs = p.funcs
    perfect = p.targetfitness

    pop = @parallel (vcat) for _ = 1:(mu + lambda)
        random_chromosome(p)
    end
    fit = @parallel (vcat) for c = pop
        fitness(c, goal)
    end
    perm = sortperm(fit, rev=true)

    for t = 1:gens
        if fit[perm[1]] == perfect # TODO: This is inexact
            return (pop[perm[1]], t)
        end

        mutpop = @parallel (vcat) for x = pop[perm][1:lambda]
            mutate(deepcopy(x), funcs)
        end
        mutfit = @parallel (vcat) for x = mutpop
            fitness(x, goal)
        end

        newpop = vcat(pop[perm][1:mu], mutpop)
        newfit = vcat(fit[perm][1:mu], mutfit)

        pop = newpop
        fit = newfit

        perm = sortperm(fit, rev=true)
    end

    return (pop[perm][1], gens)
end


println("Starting...")
using MachineLearning
using DataFrames
using Gadfly
using TextAnalysis
using IJulia
using SQLite
using XGBoost
println("Ok!")

Remove MachineLearning from build test

println("Starting...")
using DataFrames
using Gadfly
using TextAnalysis
using IJulia
using SQLite
using XGBoost
println("Ok!")


counts = Dict{String,Int}()

open(ARGS[1],"r") do f
    for line in eachline(f)
        for word in split(line)
            counts[word] = get(counts,word,0) + 1
        end
    end
end

cut = 10

guys = collect(counts)
sort!( guys, by=(x)->x[2], rev=true )

for x in guys
    if x[2] > cut
        println(x[1], '\t',  x[2])
    end
end

Update Julia code to encapsulate processing into a function.

function vocab(file)
    cut = 10
    counter = Dict{String, Int}()
    open(file, "r") do io
        for line in eachline(io)
            for word in split(line)
                counter[word] = 1 + get(counter, word, 0)
            end
        end
    end

    # Sort and display
    guys = collect(counter)
    sort!(guys, by=(x) -> x[2], rev=true)

    for x in guys
        if x[2] > cut
            println(x[1], '\t',  x[2])
        end
    end
end

vocab(ARGS[1])


@testset "Orders in absolute number fields" begin
  println("NfOrd.jl")
  @time include("NfOrd/NfOrd.jl")

  println("Elem.jl")
  @time include("NfOrd/Elem.jl")
  println("Ideal.jl")
  @time include("NfOrd/Ideal.jl")
  println("FracIdl.jl")
  @time include("NfOrd/FracIdl.jl")
  println("ResidueRing.jl")
  @time include("NfOrd/ResidueRing.jl")
  println("Clgp.jl")
  @time include("NfOrd/Clgp.jl")
  println("RayClassGroup.jl")
  @time include("NfOrd/RayClassGroup.jl")
  println("ResidueRingMultGrp.jl")
  @time include("NfOrd/ResidueRingMultGrp.jl")
  println("Overorders.jl")
  @time include("NfOrd/Overorders.jl")
  println("LinearAlgebra.jl")
  @time include("NfOrd/LinearAlgebra.jl")
  #println("PicardGroup.jl")
  #@time include("NfOrd/PicardGroup.jl")
end

Enable the picard group tests

@testset "Orders in absolute number fields" begin
  println("NfOrd.jl")
  @time include("NfOrd/NfOrd.jl")

  println("Elem.jl")
  @time include("NfOrd/Elem.jl")
  println("Ideal.jl")
  @time include("NfOrd/Ideal.jl")
  println("FracIdl.jl")
  @time include("NfOrd/FracIdl.jl")
  println("ResidueRing.jl")
  @time include("NfOrd/ResidueRing.jl")
  println("Clgp.jl")
  @time include("NfOrd/Clgp.jl")
  println("RayClassGroup.jl")
  @time include("NfOrd/RayClassGroup.jl")
  println("ResidueRingMultGrp.jl")
  @time include("NfOrd/ResidueRingMultGrp.jl")
  println("Overorders.jl")
  @time include("NfOrd/Overorders.jl")
  println("LinearAlgebra.jl")
  @time include("NfOrd/LinearAlgebra.jl")
  println("PicardGroup.jl")
  @time include("NfOrd/PicardGroup.jl")
end


require("options.jl")
import OptionsMod.*

load("src/datavec.jl")
load("src/index.jl")
load("src/namedarray.jl")
load("src/dataframe.jl")
load("src/formula.jl")
load("src/utils.jl")

Order of index.jl and datavec.jl was backwards

require("options.jl")
import OptionsMod.*

load("src/index.jl")
load("src/datavec.jl")
load("src/namedarray.jl")
load("src/dataframe.jl")
load("src/formula.jl")
load("src/utils.jl")


using PyPlot
const plt = PyPlot

function plot_band_structure( evals, kpath; filename="BANDS.pdf" )

  Nkpts = size(kpath)[2]
  Nstates = size(evals)[1]

  Xcoords = zeros(Float64,Nkpts)
  Xcoords[1] = 0.0
  for ik = 2:Nkpts
    dk = kpath[:,ik] - kpath[:,ik-1]
    Xcoords[ik] = Xcoords[ik-1] + norm( dk )
  end

  plt.clf()
  for is = 1:Nstates
    y = evals[is,:]
    plt.plot( Xcoords[:], y, marker="o")
  end
  plt.grid()
  plt.savefig(filename)
end

Use Array{Float64} instead of zeros

using PyPlot
const plt = PyPlot

function plot_band_structure( evals, kpath; filename="BANDS.pdf" )

  Nkpts = size(kpath)[2]
  Nstates = size(evals)[1]

  Xcoords = Array{Float64}(Nkpts)
  Xcoords[1] = 0.0
  for ik = 2:Nkpts
    dk = kpath[:,ik] - kpath[:,ik-1]
    Xcoords[ik] = Xcoords[ik-1] + norm( dk )
  end

  plt.clf()
  for is = 1:Nstates
    y = evals[is,:]
    plt.plot( Xcoords[:], y, marker="o")
  end
  plt.grid()
  plt.savefig(filename)
end


#
# Correctness Tests
#

fatalerrors = length(ARGS) > 0 && ARGS[1] == "-f"
anyerrors = false

using Base.Test
using DataFrames

my_tests = ["utils.jl",
            "data.jl",
            "index.jl",
            "dataframe.jl",
            "io.jl",
            # "formula.jl",
            "constructors.jl",
            # "indexing.jl",
            "RDA.jl",
            "sort.jl",
            "grouping.jl",
            "join.jl",
            "vcat.jl",
            "iteration.jl",
            "duplicates.jl",
            "show.jl"]

println("Running tests:")

for my_test in my_tests
    try
        include(my_test)
        println("\t\033[1m\033[32mPASSED\033[0m: $(my_test)")
    catch
        anyerrors = true
        println("\t\033[1m\033[31mFAILED\033[0m: $(my_test)")
        if fatalerrors
            rethrow()
        end
    end
end

if anyerrors
    throw("Tests failed")
end

stdin = Pkg.dir("DataFrames", "test", "stdin.sh")
run(`bash $stdin`)

Print errors for failed tests by default

#
# Correctness Tests
#

fatalerrors = length(ARGS) > 0 && ARGS[1] == "-f"
quiet = length(ARGS) > 0 && ARGS[1] == "-q"
anyerrors = false

using Base.Test
using DataFrames

my_tests = ["utils.jl",
            "data.jl",
            "index.jl",
            "dataframe.jl",
            "io.jl",
            # "formula.jl",
            "constructors.jl",
            # "indexing.jl",
            "RDA.jl",
            "sort.jl",
            "grouping.jl",
            "join.jl",
            "vcat.jl",
            "iteration.jl",
            "duplicates.jl",
            "show.jl"]

println("Running tests:")

for my_test in my_tests
    try
        include(my_test)
        println("\t\033[1m\033[32mPASSED\033[0m: $(my_test)")
    catch e
        anyerrors = true
        println("\t\033[1m\033[31mFAILED\033[0m: $(my_test)")
        if fatalerrors
            rethrow(e)
        elseif !quiet
            showerror(STDOUT, e, backtrace())
            println()
        end
    end
end

if anyerrors
    throw("Tests failed")
end

stdin = Pkg.dir("DataFrames", "test", "stdin.sh")
run(`bash $stdin`)


using Images: data, Image, load
ENV["PYTHON"] = "/usr/bin/python3"
using PyCall
using PartsBasedDetector: make_estimator, estimate

filename = "dress.jpg"
url = "http://images0.chictopia.com/photos/districtofchic/9466442894/" *
      "black-skinny-jeans-dl1961-jeans-sky-blue-pixie-market-shirt_400.jpg"

if isfile(filename)
    rm(filename)
end

filename_png = replace(filename, ".jpg", ".png")
run(`wget $url -O $filename`)
run(`convert $filename $filename_png`)

estimator = make_estimator()
candidates = estimate(estimator, filename, n_candidates = 3)
parts = candidates[1].parts
x, y = parts[:, 1], parts[:, 2]

@pyimport matplotlib.pyplot as plt
image = plt.imread(filename_png)
plt.imshow(image)
plt.plot(x, y, color="r", marker="o", ls="")
plt.show()

Append the continue option to wget

using Images: data, Image, load
ENV["PYTHON"] = "/usr/bin/python3"
using PyCall
using PartsBasedDetector: make_estimator, estimate

filename = "dress.jpg"
url = "http://images0.chictopia.com/photos/districtofchic/9466442894/" *
      "black-skinny-jeans-dl1961-jeans-sky-blue-pixie-market-shirt_400.jpg"

filename_png = replace(filename, ".jpg", ".png")
run(`wget --continue $url -O $filename`)
run(`convert $filename $filename_png`)

estimator = make_estimator()
candidates = estimate(estimator, filename, n_candidates = 3)
parts = candidates[1].parts
x, y = parts[:, 1], parts[:, 2]

@pyimport matplotlib.pyplot as plt
image = plt.imread(filename_png)
plt.imshow(image)
plt.plot(x, y, color="r", marker="o", ls="")
plt.show()


@testset "Test Function Tests" begin

  @test isdefined(Julz, :test) == true

  dummy_package = "$(dirname(@__FILE__))/../../../../JulzDummy"

  cp("dummy", dummy_package, remove_destination=true)

  err_data = "nil"
  out_data = "nil"
  did_break = false

  cd(dummy_package) do

    originalSTDERR = STDERR
    originalSTDOUT = STDOUT

    (errRead, errWrite) = redirect_stderr()
    (outRead, outWrite) = redirect_stdout()

    println("\ndummy start\n")
    try
      Julz.test()
    catch
      did_break = true
    end
    println("dummy end")

    close(errWrite)
    close(outWrite)

    err_data = readavailable(errRead)
    out_data = readavailable(outRead)

    close(errRead)
    close(outRead)

    redirect_stderr(originalSTDERR)
    redirect_stdout(originalSTDOUT)

  end

  rm(dummy_package, force=true, recursive=true)

  @test contains(String(err_data), "INFO: JulzDummy tests passed")

  if did_break

    println(" ================================ ")
    println("  dummy broken test output (out)  ")
    println(" ================================ ")

    println(String(out_data))

    println(" ================================ ")
    println("  dummy broken test output (err)  ")
    println(" ================================ ")

    println(String(err_data))

    println(" ================================ ")
    println("  dummy broken test output (end)  ")
    println(" ================================ ")

  end

end

Fix bug where colored test printout broke test

@testset "Test Function Tests" begin

  @test isdefined(Julz, :test) == true

  dummy_package = "$(dirname(@__FILE__))/../../../../JulzDummy"

  cp("dummy", dummy_package, remove_destination=true)

  err_data = "nil"
  out_data = "nil"
  did_break = false

  cd(dummy_package) do

    originalSTDERR = STDERR
    originalSTDOUT = STDOUT

    (errRead, errWrite) = redirect_stderr()
    (outRead, outWrite) = redirect_stdout()

    println("\ndummy start\n")
    try
      Julz.test()
    catch
      did_break = true
    end
    println("dummy end")

    close(errWrite)
    close(outWrite)

    err_data = readavailable(errRead)
    out_data = readavailable(outRead)

    close(errRead)
    close(outRead)

    redirect_stderr(originalSTDERR)
    redirect_stdout(originalSTDOUT)

  end

  rm(dummy_package, force=true, recursive=true)

  @test contains(String(err_data), "JulzDummy tests passed")

  if did_break

    println(" ================================ ")
    println("  dummy broken test output (out)  ")
    println(" ================================ ")

    println(String(out_data))

    println(" ================================ ")
    println("  dummy broken test output (err)  ")
    println(" ================================ ")

    println(String(err_data))

    println(" ================================ ")
    println("  dummy broken test output (end)  ")
    println(" ================================ ")

  end

end


@assert typeof(1:10) == UnitRange{Int64}
@assert typeof(0x5 // 0x22) == Rational{UInt8}
@assert typeof(5 // 34) == Rational{Int64}
@assert typeof(8.75im) == Complex{Float64}
@assert typeof([5,3]) == Array{Int64,1}
@assert typeof([3, "abc"]) == Array{Any, 1}
@assert typeof([]) == Array{Any, 1}
@assert typeof([1 0; 0 1]) == Array{Int64, 2}

Include sets and irrationals in the parametric types examples

@assert typeof(Set(4)) == Set{Int64}
@assert typeof(Set(['3', '$'])) == Set{Char}
@assert typeof(1:10) == UnitRange{Int64}
@assert typeof(0x5 // 0x22) == Rational{UInt8}
@assert typeof(5 // 34) == Rational{Int64}
@assert typeof(8.75im) == Complex{Float64}
@assert typeof(e) == Irrational{:e}
@assert typeof([5,3]) == Array{Int64,1}
@assert typeof([3, "abc"]) == Array{Any, 1}
@assert typeof([]) == Array{Any, 1}
@assert typeof([1 0; 0 1]) == Array{Int64, 2}


using RandomGraphs
using Graphs
using Base.Test

n = 100
p = 0.5

let u_er = erdos_renyi_graph(n, m)
	@test num_vertices(u_er) == n
	@test !is_directed(u_er)
end

let d_er = erdos_renyi_graph(n, m, is_directed=true)
	@test num_vertices(d_er) == n
	@test is_directed(d_er)
end

Test of ER runs for directed and undirected version

using RandomGraphs
using Graphs
using Base.Test

n = 100
p = 0.5

let u_er = erdos_renyi_graph(n, m, is_directed=false)
	@test num_vertices(u_er) == n
	@test !is_directed(u_er)
end

let d_er = erdos_renyi_graph(n, m, is_directed=true)
	@test num_vertices(d_er) == n
	@test is_directed(d_er)
end


using Weave
using Base.Test

# Running Changin plotlib in tests segfault, unless they are run
# using separate processes.
#run(`julia --code-coverage=user -e 'include("winston_formats.jl")'`)
#run(`julia --code-coverage=user -e 'include("pyplot_formats.jl")'`)

info("Test: Chunk options")
include("chunk_options.jl")

info("Test: Caching")
include("cache_test.jl")

info("Testing rich output")
include("rich_output.jl")

if VERSION < v"0.5-dev"
  info("Test: Chunk options with Gadfly")
  include("chunk_opts_gadfly.jl")

  info("Test: Weaving with Winston")
  include("winston_formats.jl")

  info("Test: Weaving with Gadfly")
  include("gadfly_formats.jl")

  info("Test: Weaving with PyPlot")
  include("pyplot_formats.jl")

  info("Test: Weaving with Plots.jl")
  include("plotsjl_test.jl")
end

Disable Winston tests on travis

using Weave
using Base.Test

# Running Changin plotlib in tests segfault, unless they are run
# using separate processes.
#run(`julia --code-coverage=user -e 'include("winston_formats.jl")'`)
#run(`julia --code-coverage=user -e 'include("pyplot_formats.jl")'`)

info("Test: Chunk options")
include("chunk_options.jl")

info("Test: Caching")
include("cache_test.jl")

info("Testing rich output")
include("rich_output.jl")

if VERSION < v"0.5-dev"
  info("Test: Chunk options with Gadfly")
  include("chunk_opts_gadfly.jl")

  #Fails on travis, works locally.
  #info("Test: Weaving with Winston")
  #include("winston_formats.jl")

  info("Test: Weaving with Gadfly")
  include("gadfly_formats.jl")

  info("Test: Weaving with PyPlot")
  include("pyplot_formats.jl")

  info("Test: Weaving with Plots.jl")
  include("plotsjl_test.jl")
end


@testset "Misc" begin
  include("Misc/AbelianGrp.jl")
  include("Misc/psibound.jl")
  include("Misc/InfinitePlaces.jl")
end

Correct path to test file

@testset "Misc" begin
  include("Misc/AbelianGrp.jl")
  include("Misc/psibound.jl")
  include("Misc/Places.jl")
end


#### Similarity and dissimilarity measures between images ####

"""
`hausdorff_distance(imgA, imgB)` is the modified Hausdorff distance
between binary images (or point sets).

References
----------
Dubuisson, M-P; Jain, A. K., 1994. A Modified Hausdorff Distance for
Object-Matching.
"""
function hausdorff_distance(imgA::AbstractArray, imgB::AbstractArray)
  @assert size(imgA) == size(imgB) "Images must have the same dimensions"

  # trivial case
  isequal(imgA, imgB) && return 0.

  A, B = find(imgA), find(imgB)

  # return if there is no object to match
  (isempty(A) || isempty(B)) && return Inf

  # grid coordinates (ndims by npoints)
  A = hcat([Float64[ind2sub(size(imgA), a)...] for a in A]...)
  B = hcat([Float64[ind2sub(size(imgB), b)...] for b in B]...)

  m = size(A, 2); n = size(B, 2)

  D = zeros(m, n)
  for j=1:n, i=1:m
    @inbounds D[i,j] = norm(A[:,i] - B[:,j])
  end

  dAB = mean(minimum(D, 2))
  dBA = mean(minimum(D, 1))

  max(dAB, dBA)
end

Save up memory in hausdorff distance

#### Similarity and dissimilarity measures between images ####

"""
`hausdorff_distance(imgA, imgB)` is the modified Hausdorff distance
between binary images (or point sets).

References
----------
Dubuisson, M-P; Jain, A. K., 1994. A Modified Hausdorff Distance for
Object-Matching.
"""
function hausdorff_distance(imgA::AbstractArray, imgB::AbstractArray)
  @assert size(imgA) == size(imgB) "Images must have the same dimensions"

  # trivial case
  isequal(imgA, imgB) && return 0.

  A, B = find(imgA), find(imgB)

  # return if there is no object to match
  (isempty(A) || isempty(B)) && return Inf

  m = length(A); n = length(B)

  D = zeros(m, n)
  for j=1:n
    b = [ind2sub(size(imgB), B[j])...]
    for i=1:m
      a = [ind2sub(size(imgA), A[i])...]
      @inbounds D[i,j] = norm(a - b)
    end
  end

  dAB = mean(minimum(D, 2))
  dBA = mean(minimum(D, 1))

  max(dAB, dBA)
end


macro timeit(ex,name)
    t, i = gensym(2)
    quote
        $t =  @elapsed $ex
        printf("%s took %.1f ms\n", $name, $t*1000)
    end
end

Use average of 20 runs for Julia timings.

macro timeit(ex,name)
    t, i = gensym(2)
    quote
        $t = 0
        for $i=1:20
            $t += @elapsed $ex
        end
        $t = $t/20
        printf("%s took %.1f ms (avg over 20 runs)\n", $name, $t*1000)
    end
end


using Test
using MPI

# Do the MPI tests first so we do not have mpi running inside MPI
# XXX: Currently not working on windows, not sure why
if !Sys.iswindows()
    @testset "mpi tests" begin
        @test mpiexec() do mpi_cmd
            cmd = `$mpi_cmd -n 4 $(Base.julia_cmd()) --project dmda.jl`
            success(pipeline(cmd, stderr = stderr))
        end
    end
end

# Examples with the comment
#   # INCLUDE IN MPI TEST
# will be run here
include("mpi_examples.jl")

include("options.jl")
include("dmda.jl")
include("old_test.jl")

# Run the examples to make sure they are all work
include("examples.jl")

Disable windows + MPI tests

using Test
using MPI

# Do the MPI tests first so we do not have mpi running inside MPI
# XXX: Currently not working on windows, not sure why
if !Sys.iswindows()
    @testset "mpi tests" begin
        @test mpiexec() do mpi_cmd
            cmd = `$mpi_cmd -n 4 $(Base.julia_cmd()) --project dmda.jl`
            success(pipeline(cmd, stderr = stderr))
        end
    end
end

# Examples with the comment
#   # INCLUDE IN MPI TEST
# will be run here
# XXX: Currently not working on windows reliably, not sure why
if !Sys.iswindows()
    include("mpi_examples.jl")
end

include("options.jl")
include("dmda.jl")
include("old_test.jl")

# Run the examples to make sure they are all work
include("examples.jl")


immutable Tag
    rep::AbstractString
end

type TaggedValue
    tag::AbstractString
    value::Any
end

Add equality and hash functions to tagged values.

immutable Tag
    rep::AbstractString
end

type TaggedValue
    tag::AbstractString
    value::Any
end

import Base.==, Base.hash, Base.string

function ==(tv::TaggedValue, other::TaggedValue)
    tv.value == other.value && tv.tag == other.tag
end

const hashttaggedvalue_seed = UInt === UInt64 ? 0x8ee14727cfcae1ce : 0x1f487ece

function hash(tv::TaggedValue, h::UInt)
    h = hash(hashttaggedvalue_seed, h)
    h $= hash(tv.tag)
    h $= hash(tv.value)
    return h
end

function string(tv::TaggedValue)
    "$(tv.tag): $(tv.value)"
end


# double factorial
factorial2(n) = prod(n:-2:1)

dist2(dx,dy,dz) = dx*dx + dy*dy + dz*dz

function pairs(n::Int64,which="diag")
    function _it()
        for j in 1:n
            start = j+1
            if which=="diag"
                start = j
            elseif which=="rect"
                start = 1
            end
            for i in start:n
                produce((j,i))
            end
        end
    end
    Task(_it)
end

triangle(i::Int64) = div(i*(i+1),2)
triangle(i::Int64,j::Int64) = i<j ? triangle(j-1)+i : triangle(i-1)+j

function iiterator(n::Int64)
    function _it()
        for (i,j) in pairs(n)
            ij = triangle(i-1,j)
            for (k,l) in pairs(n)
                kl = triangle(k-1,l)
                if ij <= kl
                    produce((i,j,k,l))
                end
            end
        end
    end
    Task(_it)
end

iindex(i::Int64,j::Int64,k::Int64,l::Int64) = triangle(triangle(i,j),triangle(k,l))

trace2(A,B) = sum(A.*B)

Remove the old code that is not working in Julia-0.7

# double factorial
factorial2(n) = prod(n:-2:1)

dist2(dx,dy,dz) = dx*dx + dy*dy + dz*dz

triangle(i::Int64) = div(i*(i+1),2)
triangle(i::Int64,j::Int64) = i < j ? triangle(j-1)+i : triangle(i-1)+j

iindex(i::Int64,j::Int64,k::Int64,l::Int64) = triangle(triangle(i,j),triangle(k,l))

trace2(A,B) = sum(A.*B)


function nnls(A::AbstractMatrix, b::AbstractVector, ops::Options)
    # Set up the preconditioner as the inverse diagonal of the Hessian
    a = sum(A.^2, 1)
    @defaults ops precondprep=(P, x, l, u, mu)->precondprepnnls(P, x, mu, a)
    ops = copy(ops)
    @set_options ops precondprep=precondprep
    # Create the initial guess (an interior point)
    T = promote_type(eltype(A), eltype(b))
    x = fill(one(T), size(A, 2))
    # Set up constraints
    l = zeros(eltype(x), length(x))
    u = fill(inf(eltype(x)), length(x))
    # Perform the optimization
    func = (g,x) -> nnlsobjective(g, x, A, b)
    fminbox(func, x, l, u, ops)
end
nnls(A::AbstractMatrix, b::AbstractVector) = nnls(A, b, Options())

function nnlsobjective(g, x::AbstractVector, A::AbstractMatrix, b::AbstractVector)
    d = A*x - b
    val = sum(d.^2)/2
    if !(g === nothing)
        At_mul_B(g, A, d)
    end
    val
end

function precondprepnnls(P, x, mu, a)
    for i = 1:length(x)
        P[i] = 1/(mu/x[i]^2 + a[i])
    end
end

export nnls

Fix call to deprecated At_mul_B

function nnls(A::AbstractMatrix, b::AbstractVector, ops::Options)
    # Set up the preconditioner as the inverse diagonal of the Hessian
    a = sum(A.^2, 1)
    @defaults ops precondprep=(P, x, l, u, mu)->precondprepnnls(P, x, mu, a)
    ops = copy(ops)
    @set_options ops precondprep=precondprep
    # Create the initial guess (an interior point)
    T = promote_type(eltype(A), eltype(b))
    x = fill(one(T), size(A, 2))
    # Set up constraints
    l = zeros(eltype(x), length(x))
    u = fill(inf(eltype(x)), length(x))
    # Perform the optimization
    func = (g,x) -> nnlsobjective(g, x, A, b)
    fminbox(func, x, l, u, ops)
end
nnls(A::AbstractMatrix, b::AbstractVector) = nnls(A, b, Options())

function nnlsobjective(g, x::AbstractVector, A::AbstractMatrix, b::AbstractVector)
    d = A*x - b
    val = sum(d.^2)/2
    if !(g === nothing)
        At_mul_B!(g, A, d)
    end
    val
end

function precondprepnnls(P, x, mu, a)
    for i = 1:length(x)
        P[i] = 1/(mu/x[i]^2 + a[i])
    end
end

export nnls


import SVMLightLoader: load_svmlight_file
import SoftConfidenceWeighted: init, fit, predict

# The code is from below:
# http://thirld.com/blog/2015/05/30/julia-profiling-cheat-sheet/
function benchmark(func, args...; filename="trace.def")
    # Any setup code goes here.

    # Run once, to force compilation.
    println("======================= First run:")
    @time func(args...)

    # Run a second time, with profiling.
    println("======================= Second run:")

    Profile.init()
    Profile.clear()
    Profile.clear_malloc_data()

    @profile @time func(args...)

    # Write profile results to profile.bin.
    f = open(filename, "w")
    Profile.print(f)
    close(f)

    println("\n\n")
end

function calc_accuracy(results, answers)
    n_correct_answers = 0
    for (result, answer) in zip(results, answers)
        if result == answer
            n_correct_answers += 1
        end
    end
    accuracy = n_correct_answers / length(y)
end

X, y = load_svmlight_file("data.txt")

model = init(10.0, 10.0)

benchmark(fit, model, X, y, filename="trace.fit.txt")

model = fit(model, X, y)
results = predict(model, X)

accuracy = calc_accuracy(results, y)
println("Accuracy: $accuracy")

Use fit! instead of fit as it is deprecated

import SVMLightLoader: load_svmlight_file
import SoftConfidenceWeighted: init, fit!, predict

# The code is from
# http://thirld.com/blog/2015/05/30/julia-profiling-cheat-sheet/
function benchmark(func, args...; filename="trace.def")
    # Any setup code goes here.

    # Run once, to force compilation.
    println("======================= First run:")
    @time func(args...)

    # Run a second time, with profiling.
    println("======================= Second run:")

    Profile.init()
    Profile.clear()
    Profile.clear_malloc_data()

    @profile @time func(args...)

    # Write profile results to profile.bin.
    f = open(filename, "w")
    Profile.print(f)
    close(f)
end

function calc_accuracy(results, answers)
    n_correct_answers = 0
    for (result, answer) in zip(results, answers)
        if result == answer
            n_correct_answers += 1
        end
    end
    accuracy = n_correct_answers / length(y)
end

X, y = load_svmlight_file("data.txt")

model = init(10.0, 10.0)

benchmark(fit!, model, X, y, filename="trace.fit.txt")

model = init(10.0, 10.0)
model = fit!(model, X, y)
results = predict(model, X)

accuracy = calc_accuracy(results, y)
println("Accuracy: $accuracy")


@testset "Get All Files Function Tests" begin

  @test isdefined(Julz, :get_all_files) == true

end

Add test for “get_all_files” function

@testset "Get All Files Function Tests" begin

  @test isdefined(Julz, :get_all_files) == true

  initial_dir = pwd()

  cd("dummy")

  @test length(Julz.get_all_files("../dummy")) == 1

  Julz.generate("type", "foo")

  @test length(Julz.get_all_files("../dummy")) == 3

  Julz.destroy("type", "foo")

  @test length(Julz.get_all_files("../dummy")) == 1

  cd(initial_dir)

  main_file = Julz.get_all_files("dummy")[1]

  @test main_file == "dummy/src/JulzDummy.jl"

end


Pkg.add("ImageMagick")
Pkg.build("ImageMagick")

Pkg.clone("GR")
Pkg.build("GR")

Pkg.clone("https://github.com/JuliaPlots/PlotReferenceImages.jl.git")

# Pkg.clone("https://github.com/JuliaStats/KernelDensity.jl.git")

Pkg.clone("StatPlots")
Pkg.checkout("PlotUtils")

# Pkg.clone("Blink")
# Pkg.build("Blink")
# import Blink
# Blink.AtomShell.install()
# Pkg.add("Rsvg")
# Pkg.add("PlotlyJS")

# Pkg.checkout("RecipesBase")
# Pkg.clone("VisualRegressionTests")

# need this to use Conda
ENV["PYTHON"] = ""
Pkg.add("PyPlot")
Pkg.build("PyPlot")

# Pkg.add("InspectDR")

Pkg.test("Plots"; coverage=false)

Remove GR clone in travis tests

Pkg.add("ImageMagick")
Pkg.build("ImageMagick")

# Pkg.clone("GR")
# Pkg.build("GR")

Pkg.clone("https://github.com/JuliaPlots/PlotReferenceImages.jl.git")

# Pkg.clone("https://github.com/JuliaStats/KernelDensity.jl.git")

Pkg.clone("StatPlots")
Pkg.checkout("PlotUtils")

# Pkg.clone("Blink")
# Pkg.build("Blink")
# import Blink
# Blink.AtomShell.install()
# Pkg.add("Rsvg")
# Pkg.add("PlotlyJS")

# Pkg.checkout("RecipesBase")
# Pkg.clone("VisualRegressionTests")

# need this to use Conda
ENV["PYTHON"] = ""
Pkg.add("PyPlot")
Pkg.build("PyPlot")

# Pkg.add("InspectDR")

Pkg.test("Plots"; coverage=false)


module session

type Session
  address::String
  timeout::Int
  loop::Loop
  builder::Tcp
  reconnect_limit::Int
  pack_encoding::String
  unpack_encoding::String

  function Session(session::Session)
    this                 = new()
    this.address         = session.address
    this.timeout         = session.timeout
    this.loop            = session.loop
    this.builder         = session.builder
    this.reconnect_limit = session.reconnect_limit
    this.pack_encoding   = session.pack_encoding
    this.unpack_encoding = session.unpack_encoding
    this
  end

  function call(self::Session, method::Method, args::Array)
  end

  function call_async(self::Session, method::Method, args::Array)
  end

  function send_request(self::Session, method::Method, args::Array)
  end

  function notify(self::Session, method::Method, args::Array)
  end

  function close(self::Session)
  end

  function on_connect_failed(self::Session, reason::Reason)
  end

  function on_response(self::Session, msgid::Int, error::Error, result::Result)
  end

  function on_timeout(self::Session, msgid::Int)
  end

  function step_timeout(self::Session)
  end

end

function NoSyncIDGenerator
end

end # session

Add some skeleton codes as comments

module session

type Session
  address::String
  timeout::Int
  loop::Loop
  builder::Tcp
  reconnect_limit::Int
  pack_encoding::String
  unpack_encoding::String

  function Session(session::Session)
    this                 = new()
    this.address         = session.address
    this.timeout         = session.timeout
    this.loop            = session.loop
    this.builder         = session.builder
    this.reconnect_limit = session.reconnect_limit
    this.pack_encoding   = session.pack_encoding
    this.unpack_encoding = session.unpack_encoding
    this
  end

  function call(self::Session, method::Method, args::Array)
    # return self.send_request(method, args).get()
  end

  function call_async(self::Session, method::Method, args::Array)
    # return self.send_request(method, args)
  end

  function send_request(self::Session, method::Method, args::Array)
    # msgid = next(self.generator)
    # future = Future(self.loop, self.timeout)
    # self.request_table[msgid] = future
    # self.transport.send_message([message.REQUEST, msgid, method, args])
    # return future
  end

  function notify(self::Session, method::Method, args::Array)
  end

  function close(self::Session)
  end

  function on_connect_failed(self::Session, reason::Reason)
  end

  function on_response(self::Session, msgid::Int, error::Error, result::Result)
  end

  function on_timeout(self::Session, msgid::Int)
  end

  function step_timeout(self::Session)
  end

end

function NoSyncIDGenerator
end

end # session


function get_all_files(cur_item)
  nested_files = get_nested_files(cur_item)
  clean_files_list(nested_files)

  nested_files
end

function get_nested_files(cur_item)
  nested_files = []

  if isfile(cur_item)
    if endswith(cur_item, ".jl")
      push!(nested_files, cur_item)
    end

    return nested_files
  end

  for sub_item in readdir(cur_item)
    if startswith(sub_item, ".") ; continue ; end

    sub_nested_files = get_nested_files("$cur_item/$sub_item")
    append!(nested_files, sub_nested_files)
  end

  return nested_files
end

function clean_files_list(nested_files)
  delete_list = []

  package_name = rsplit(pwd(), '/'; limit=2)[2]
  push!(delete_list, "$package_name.jl")
  push!(delete_list, "Julz.jl")

  push!(delete_list, "test/runtests.jl")
  push!(delete_list, "src/functions/include_all_files.jl")
  push!(delete_list, "src/functions/get_all_files.jl")

  for deleted_file in delete_list
    for (index, nested_file) in enumerate(nested_files)
      if endswith(nested_file, deleted_file)
        deleteat!(nested_files, index)
        break
      end
    end
  end
end

Fix typo where ‘ was used instead of “

function get_all_files(cur_item)
  nested_files = get_nested_files(cur_item)
  clean_files_list(nested_files)

  nested_files
end

function get_nested_files(cur_item)
  nested_files = []

  if isfile(cur_item)
    if endswith(cur_item, ".jl")
      push!(nested_files, cur_item)
    end

    return nested_files
  end

  for sub_item in readdir(cur_item)
    if startswith(sub_item, ".") ; continue ; end

    sub_nested_files = get_nested_files("$cur_item/$sub_item")
    append!(nested_files, sub_nested_files)
  end

  return nested_files
end

function clean_files_list(nested_files)
  delete_list = []

  package_name = rsplit(pwd(), "/"; limit=2)[2]
  push!(delete_list, "$package_name.jl")
  push!(delete_list, "Julz.jl")

  push!(delete_list, "test/runtests.jl")
  push!(delete_list, "src/functions/include_all_files.jl")
  push!(delete_list, "src/functions/get_all_files.jl")

  for deleted_file in delete_list
    for (index, nested_file) in enumerate(nested_files)
      if endswith(nested_file, deleted_file)
        deleteat!(nested_files, index)
        break
      end
    end
  end
end


module HTMLWriterTests

using Compat.Test
using Compat

import Documenter.Writers.HTMLWriter: jsescape

@testset "HTMLWriter" begin
    @test jsescape("abc123") == "abc123"
    @test jsescape("▶αβγ") == "▶αβγ"
    @test jsescape("") == ""

    @test jsescape("a\nb") == "a\\nb"
    @test jsescape("\r\n") == "\\r\\n"
    @test jsescape("\\") == "\\\\"

    @test jsescape("\"'") == "\\\"\\'"

    # Ref: #639
    @test jsescape("\u2028") == "\\u2028"
    @test jsescape("\u2029") == "\\u2029"
    @test jsescape("policy to
 delete.") == "policy to\\u2028 delete."
end

end

Add a test for generate_version_file

module HTMLWriterTests

using Compat.Test
using Compat

import Documenter.Writers.HTMLWriter: jsescape, generate_version_file

@testset "HTMLWriter" begin
    @test jsescape("abc123") == "abc123"
    @test jsescape("▶αβγ") == "▶αβγ"
    @test jsescape("") == ""

    @test jsescape("a\nb") == "a\\nb"
    @test jsescape("\r\n") == "\\r\\n"
    @test jsescape("\\") == "\\\\"

    @test jsescape("\"'") == "\\\"\\'"

    # Ref: #639
    @test jsescape("\u2028") == "\\u2028"
    @test jsescape("\u2029") == "\\u2029"
    @test jsescape("policy to
 delete.") == "policy to\\u2028 delete."

    mktempdir() do tmpdir
        versions = ["stable", "latest", "release-0.2", "release-0.1", "v0.2.6", "v0.1.1", "v0.1.0"]
        cd(tmpdir) do
            mkdir("foobar")
            for version in versions
                mkdir(version)
            end
        end

        generate_version_file(tmpdir)

        versions_file = joinpath(tmpdir, "versions.js")
        @test isfile(versions_file)
        contents = String(read(versions_file))
        @test !occursin("foobar", contents) # only specific directories end up in the versions file
        # let's make sure they're in the right order -- they should be sorted in the output file
        last = 0:0
        for version in versions
            this = Compat.findfirst(version, contents)
            @test this !== nothing
            @test first(last) < first(this)
            last = this
        end
    end
end

end


using Base.Test

function runtests()
    nprocs = min(4, CPU_CORES)
    exename = joinpath(JULIA_HOME, Base.julia_exename())
    testdir = dirname(@__FILE__)
    testfiles = sort(filter(x->x!="runtests.jl", readdir(testdir)))
    nfail = 0
    print_with_color(:white, "Running MPI.jl tests\n")
    for f in testfiles
        try
            if success(`mpirun -np $nprocs $exename $(joinpath(testdir, f))`)
                Base.with_output_color(:green,STDOUT) do io
                    println(io,"\tSUCCESS: $f")
                end
            else
                Base.with_output_color(:red,STDERR) do io
                    println(io, "\tFAILED: $(joinpath(testdir, f))")
                end
                nfail += 1
            end
        catch ex
            Base.with_output_color(:red,STDERR) do io
                println(io,"\tError: $(joinpath(testdir, f))")
                showerror(io,ex,backtrace())
            end
            nfail += 1
        end
    end
    return nfail
end

exit(runtests())

Make some informative output when tests fail

using Base.Test

function runtests()
    nprocs = min(4, CPU_CORES)
    exename = joinpath(JULIA_HOME, Base.julia_exename())
    testdir = dirname(@__FILE__)
    testfiles = sort(filter(x->x!="runtests.jl", readdir(testdir)))
    nfail = 0
    print_with_color(:white, "Running MPI.jl tests\n")
    for f in testfiles
        try
            run(`mpirun -np $nprocs $exename $(joinpath(testdir, f))`)
            Base.with_output_color(:green,STDOUT) do io
                println(io,"\tSUCCESS: $f")
            end
        catch ex
            Base.with_output_color(:red,STDERR) do io
                println(io,"\tError: $(joinpath(testdir, f))")
                showerror(io,ex,backtrace())
            end
            nfail += 1
        end
    end
    return nfail
end

exit(runtests())


using BinDeps

ENV["JULIA_ROOT"] = abspath(JULIA_HOME, "../../")

cd(joinpath(Pkg.dir(), "Clang", "deps", "src") )
run(`make`)
if (!ispath("../usr"))
  run(`mkdir ../usr`)
end
if (!ispath("../usr/lib"))
  run(`mkdir ../usr/lib`)
end
run(`mv libwrapclang.$(BinDeps.shlib_ext) ../usr/lib`)

Use Base.Sys.shlib_ext instead of pulling from BinDeps

ENV["JULIA_ROOT"] = abspath(JULIA_HOME, "../../")

cd(joinpath(Pkg.dir(), "Clang", "deps", "src") )
run(`make`)
if (!ispath("../usr"))
  run(`mkdir ../usr`)
end
if (!ispath("../usr/lib"))
  run(`mkdir ../usr/lib`)
end
run(`mv libwrapclang.$(Base.Sys.shlib_ext) ../usr/lib`)


using Color

abstract MaterialPrimitive

immutable Material{P<:MaterialPrimitive} <: Compose3DNode
    primitives::Vector{P}
end

immutable MeshColor{T<:ColorValue} <: MaterialPrimitive
    color::T
end

immutable WireFrameMesh <: MaterialPrimitive
    wireframe::Bool
end

function mesh_color(color::ColorValue)
    Material([MeshColor(color)])
end

function wireframe(wireframe::Bool)
    Material([WireFrameMesh(wireframe)])
end

Make MeshColor only RGB to prevent confusion about type

using Color

abstract MaterialPrimitive

immutable Material{P<:MaterialPrimitive} <: Compose3DNode
    primitives::Vector{P}
end

immutable MeshColor <: MaterialPrimitive
    color::RGB{Float64}
end

immutable WireFrameMesh <: MaterialPrimitive
    wireframe::Bool
end

function mesh_color(color::ColorValue)
    Material([MeshColor(color)])
end

function wireframe(wireframe::Bool)
    Material([WireFrameMesh(wireframe)])
end


#######
# Universal Kalman filtering methods

function predict!(kf::KalmanFilter)
    kf.x = ap(kf.f,kf.x)
    kf
end

function predict(kf::KalmanFilter)
    predict!(copy(kf))
end

function update(kf::KalmanFilter,y::Observation)
    update!(copy(kf),y)
end

function update!(kf::KalmanFilter,y::Observation)
    (res,ph,s) = covs(kf,y)
    su = lufact!(s)
    xn = kf.x.x + ph * (su\res)
    pn = kf.x.p - ph * (su'\ph')

    # This is an ugly hack which works for now
    if typeof(kf.x) <: AbstractUnscentedState
        kf.x = UnscentedState(xn,pn,kf.x.α,kf.x.β,kf.x.κ)
    else
        kf.x = State(xn,pn)
    end
    kf
end

function predictupdate(kf::KalmanFilter,y::Observation)
    update(predict(kf),y)
end

function predictupdate!(kf::KalmanFilter,y::Observation)
    update!(predict!(kf),y)
end

Remove LU factorization from update.

#######
# Universal Kalman filtering methods

function predict!(kf::KalmanFilter)
    kf.x = ap(kf.f,kf.x)
    kf
end

function predict(kf::KalmanFilter)
    predict!(copy(kf))
end

function update(kf::KalmanFilter,y::Observation)
    update!(copy(kf),y)
end

function update!(kf::KalmanFilter,y::Observation)
    (res,ph,s) = covs(kf,y)
    xn = kf.x.x + ph * (s\res)
    pn = kf.x.p - ph * (s'\ph')

    # This is an ugly hack which works for now
    if typeof(kf.x) <: AbstractUnscentedState
        kf.x = UnscentedState(xn,pn,kf.x.α,kf.x.β,kf.x.κ)
    else
        kf.x = State(xn,pn)
    end
    kf
end

function predictupdate(kf::KalmanFilter,y::Observation)
    update(predict(kf),y)
end

function predictupdate!(kf::KalmanFilter,y::Observation)
    update!(predict!(kf),y)
end


using DataFrames
using DataArrays

const datafile = "data/distributed/test.csv"
const nloops = 10

function load_pkgs()
    println("loading packages...")
    @everywhere using Blocks
    @everywhere using DataFrames
end

if nprocs() < 4
    addwrkrs = 4 - nprocs()
    println("adding $addwrkrs more processors...")
    addprocs(addwrkrs)
end
println("\tnprocs: $(nprocs())")
load_pkgs()

df = dreadtable(datafile, header=false)
colnames(df)
colnames!(df, ["c1","c2","c3","c4","c5","c6","c7","c8","c9","c10"])

sum_result = df+df
mul_result = 2*df
eq_result = (sum_result .== mul_result)
@assert all(eq_result)

df1 = dreadtable(open(datafile), 1000)
@assert nrow(df1) == nrow(df)
@assert ncol(df1) == ncol(df)
@assert isapprox(sum(matrix(colsums(df1))), sum(matrix(colsums(df))))

Allow ddataframe test to be run from any directory

using DataFrames
using DataArrays

const datafile = joinpath(dirname(@__FILE__), "data", "distributed", "test.csv")
const nloops = 10

function load_pkgs()
    println("loading packages...")
    @everywhere using Blocks
    @everywhere using DataFrames
end

if nprocs() < 4
    addwrkrs = 4 - nprocs()
    println("adding $addwrkrs more processors...")
    addprocs(addwrkrs)
end
println("\tnprocs: $(nprocs())")
load_pkgs()

df = dreadtable(datafile, header=false)
colnames(df)
colnames!(df, ["c1","c2","c3","c4","c5","c6","c7","c8","c9","c10"])

sum_result = df+df
mul_result = 2*df
eq_result = (sum_result .== mul_result)
@assert all(eq_result)

df1 = dreadtable(open(datafile), 1000)
@assert nrow(df1) == nrow(df)
@assert ncol(df1) == ncol(df)
@assert isapprox(sum(matrix(colsums(df1))), sum(matrix(colsums(df))))


#!/usr/bin/env julia

p(n) = (reduce(+,map((x->(4/(1+(x*x)))),(0.5/n):(1/n):1))/n)

function picalc(numsteps)

  # Get number of processes.
  # np = nworkers()
  np = 1

  println("Calculating PI using:")
  println("  ", numsteps, " slices")
  println("  ", np, " worker(s)")
  start = time()

  mypi = p(numsteps)

  stop = time()

  elapsed = (stop - start)

  println("Obtained value of PI: ", mypi)
  println("Time taken: ", elapsed, " seconds")

end

numsteps=50000000

if length(ARGS) > 0
  numsteps = parse(Int, ARGS[1])
end

picalc(numsteps)

Use `mapreduce` for Julia functional example

#!/usr/bin/env julia

p(n) = mapreduce(x -> (4 / (1 + (x * x))), +, (0.5 / n):(1 / n):1) / n

function picalc(numsteps)

  # Get number of processes.
  # np = nworkers()
  np = 1

  println("Calculating PI using:")
  println("  ", numsteps, " slices")
  println("  ", np, " worker(s)")
  start = time()

  mypi = p(numsteps)

  stop = time()

  elapsed = (stop - start)

  println("Obtained value of PI: ", mypi)
  println("Time taken: ", round(elapsed; digits=3), " seconds")

end

const numsteps = if length(ARGS) > 0
    parse(Int, ARGS[1])
else
    1_000_000_000
end

# Warm up kernel
print("  Warming up...")
warms = time()
p(10)
warmt = time() - warms
println("done. [", round(warmt; digits=3), "s]\n")

# Run the full example
picalc(numsteps)


#!/usr/bin/env julia

numsteps=10000000

if length(ARGS) > 0
  numsteps = parse(ARGS[1])
end

# Get number of processes.
np = nworkers()

println("Calculating PI using:")
println("  ", numsteps, " slices")
println("  ", np, " worker(s)")
start = time()

sum = 0.0
slice = 1.0/numsteps

sum = @parallel (+) for i = 1:numsteps
  x = (i - 0.5) * slice
  (4.0/(1.0 + x^2))
end

mypi = sum * slice

stop = time()

elapsed = (stop - start)

println("Obtained value of PI: ", mypi)
println("Time taken: ", elapsed, " seconds")

Split calculation into a function.

#!/usr/bin/env julia

function picalc(numsteps)

  # Get number of processes.
  np = nworkers()

  println("Calculating PI using:")
  println("  ", numsteps, " slices")
  println("  ", np, " worker(s)")
  start = time()

  sum = 0.0
  slice = 1.0/numsteps

  sum = @parallel (+) for i = 1:numsteps
    x = (i - 0.5) * slice
    (4.0/(1.0 + x^2))
  end

  mypi = sum * slice

  stop = time()

  elapsed = (stop - start)

  println("Obtained value of PI: ", mypi)
  println("Time taken: ", elapsed, " seconds")

end

numsteps=1000000000

if length(ARGS) > 0
  numsteps = parse(ARGS[1])
end

picalc(numsteps)


module c

include("../deps/deps.jl")

end # module

Add types for liblinear C binding.

module c

include("../deps/deps.jl")

immutable FeatureNode
  index::Cint
  value::Cdouble
end

immutable Problem
  l::Cint
  n::Cint
  y::Ptr{Cdouble}
  x::Ptr{Ptr{FeatureNode}}
  bias::Cdouble # < 0 if no bias term
end

# solver_type
const L2R_LR Cint = convert(Cint, 0)
const L2R_L2LOSS_SVC_DUAL Cint = convert(Cint, 1)
const L2R_L2LOSS_SVC Cint = convert(Cint, 2)
const L2R_L1LOSS_SVC_DUAL Cint = convert(Cint, 3)
const MCSVM_CS Cint = convert(Cint, 4)
const L1R_L2LOSS_SVC Cint = convert(Cint, 5)
const L1R_LR Cint = convert(Cint, 6)
const L2R_LR_DUAL Cint = convert(Cint, 7)
const L2R_L2LOSS_SVR Cint = convert(Cint, 11)
const L2R_L2LOSS_SVR_DUAL Cint = convert(Cint, 12)
const L2R_L1LOSS_SVR_DUAL Cint = convert(Cint, 13)

immutable Parameter
  solver_type::Cint

# these are for training only
  eps::Cdouble # stopping criteria
  C::Cdouble
  nr_weight::Cint
  weight_label::Ptr{Cint}
  weight::Ptr{Cdouble}
  double p::Cdouble
end

immutable Model
  param::Parameter
  nr_class::Cint # number of classes
  nr_feature::Cint
  w::Ptr{Cdouble}
  label::Ptr{Cint} # label of each class
  bias::Ptr{Cdouble}
end

function train(problem::Ptr{Problem}, param::Ptr{Parameter})
  ccall((:train, liblinear), Ptr{Model}, (Ptr{Problem}, Ptr{Parameter}), problem, param)
end

function free_model_content(model::Ptr{Model})
  ccall((:free_model_content, liblinear), Void, (Ptr{Model}), model)
end

function free_and_destroy_model(model::Ptr{Ptr{Model}})
  ccall((:free_and_destroy_model, liblinear), Void, (Ptr{Ptr{Model}},), model)
end

function destroy_param(param::Ptr{Parameter});
  ccall((:destroy_param, liblinear), Void, (Ptr{Parameter},), param)
end

end # module


@testset "NEQ Kirschmer" begin
  R,x = FlintZZ["x"]
  K,a = number_field(x^2+3x+1,"a")
  kt,t = K["t"]
  E, b = number_field( t^2 + (a+5)* (-2*a+2) )
  @test Hecke.isnorm( E, -a*(-2*a+2) )[1]
end

Add another test for relative norm equation

@testset "NEQ Kirschmer" begin
  R,x = FlintZZ["x"]
  K,a = number_field(x^2+3x+1,"a")
  kt,t = K["t"]
  E, b = number_field( t^2 + (a+5)* (-2*a+2) )
  @test Hecke.isnorm( E, -a*(-2*a+2) )[1]

  K, a = NumberField(x^2 - 2, "a")
  Kt, t = K["t"]
  L, b = number_field(t^2 - 7)
  @test isnorm(L, K(324))[1]
end


function handle_msg(w::InputWidget, msg)
    if msg.content["data"]["method"] == "backbone"
        recv(w, msg.content["data"]["sync_data"]["value"])
    end
end

function handle_msg{T}(w::Button{T}, msg)
    try
        if msg.content["data"]["method"] == "custom" &&
            msg.content["data"]["content"]["event"] == "click"
            # click event occured
            recv(w, convert(T, w.value))
        end
    catch e
        warn(string("Couldn't handle Button message ", e))
    end
end

function handle_msg{view}(w::Options{view}, msg)
    try
        if msg.content["data"]["method"] == "backbone"
            key = msg.content["data"]["sync_data"]["value_name"]
            if haskey(w.options, key)
                recv(w, w.options[key])
            end
        end
    catch e
        warn(string("Couldn't handle ", view, " message ", e))
    end
end

Fix convert Option widget messages to string before hand-off

function handle_msg(w::InputWidget, msg)
    if msg.content["data"]["method"] == "backbone"
        recv(w, msg.content["data"]["sync_data"]["value"])
    end
end

function handle_msg{T}(w::Button{T}, msg)
    try
        if msg.content["data"]["method"] == "custom" &&
            msg.content["data"]["content"]["event"] == "click"
            # click event occured
            recv(w, convert(T, w.value))
        end
    catch e
        warn(string("Couldn't handle Button message ", e))
    end
end

function handle_msg{view}(w::Options{view}, msg)
    try
        if msg.content["data"]["method"] == "backbone"
            key = string(msg.content["data"]["sync_data"]["value_name"])
            if haskey(w.options, key)
                recv(w, w.options[key])
            end
        end
    catch e
        warn(string("Couldn't handle ", view, " message ", e))
    end
end


export Func, default_funcs

type Func
    func::Function
    arity::Integer
end

const AND = Func(&, 2)
const OR = Func(|, 2)
const XOR = Func($, 2)
const TRUE = Func(() -> 0xf, 0)

function default_funcs()
    return [AND, OR, XOR, TRUE]
end

Add function changes from AHW, modified by GTL.

export Func, default_funcs

type Func
    func::Function
    arity::Integer
    name::String
end

Func(f::Function, a::Integer) = Func(f, a, string(f))

const AND = Func(&, 2)
const OR = Func(|, 2)
const XOR = Func($, 2)
const NOT = Func(~, 1)
const ZERO = Func(() -> uint64(0), 0, "0")
const ONE = Func(() -> uint64(~0), 0, "1")

function default_funcs()
    return [AND, OR, XOR, NOT, ZERO, ONE]
end


##
# Type for particle representation
#
# Dan Kolbman
##

import Base.show

type Part
  sp::Int32
  pos::Array{Float32}
  vel::Array{Float32}
  ang::Array{Float32}
  # The total square distance, used for msd
  sqd::Float64
  # The starting coordinate
  org::Array{Float32}

  Part(sp, pos, vel, ang, sqd) = new(sp, pos, vel, ang, sqd, pos)
  Part(sp, pos, vel, ang) = new(sp, pos, vel, ang, 0.0, pos)
end

# The following formats the output of particle data
# TODO this could be cleaned up some
function show(io::IO, p::Part)
  #println("$(p.sp) $(p.vel) $(p.ang) $(p.msd)")
  print(io, "$(p.sp) ")
  print_arr(io, p.pos)
  print_arr(io, p.vel)
  print_arr(io, p.ang)
  print(io, "$(p.sqd)")
end

function print_arr(io, X::AbstractArray)
    for i=1:size(X,1)
      print(io, "$(X[i])\t")
    end
end

function show(io::IO, part::Array{Part})
  for p in part
    show(io, p)
    print(io, "\n")
  end
end

Fix float formatting break due to 0.3 release of Julia

##
# Type for particle representation
#
# Dan Kolbman
##

import Base.show

type Part
  sp::Int32
  pos::Array{Float32}
  vel::Array{Float32}
  ang::Array{Float32}
  # The total square distance, used for msd
  sqd::Float64
  # The starting coordinate
  org::Array{Float32}

  Part(sp, pos, vel, ang, sqd) = new(sp, pos, vel, ang, sqd, pos)
  Part(sp, pos, vel, ang) = new(sp, pos, vel, ang, 0.0, pos)
end

# The following formats the output of particle data
# TODO this could be cleaned up some
function show(io::IO, p::Part)
  #println("$(p.sp) $(p.vel) $(p.ang) $(p.msd)")
  print(io, "$(p.sp) ")
  print_arr(io, float64(p.pos))
  print_arr(io, float64(p.vel))
  print_arr(io, float64(p.ang))
  print(io, "$(float64(p.sqd))")
end

function print_arr(io, X::AbstractArray)
    for i=1:size(X,1)
      print(io, "$(X[i])\t")
    end
end

function show(io::IO, part::Array{Part})
  for p in part
    show(io, p)
    print(io, "\n")
  end
end


function debug(io,pkg::String)
    info("Reading build script...")
    dir = Pkg2.dir(pkg)
    file = joinpath(dir,"deps/build.jl")
    context = BinDeps.PackageContext(false,dir,pkg,{})
    m = Module(:__anon__)
    body = Expr(:toplevel,:(ARGS=[$context]),:(include($file)))
    eval(m,body)
    println(io,"The package declares $(length(context.deps)) dependencies.")
    for dep in context.deps
        print(io," - Library \"$(dep.name)\"")
        if !applicable(dep)
            println(io," (not applicable to this system)")
        else
            lib = _find_library(dep)
            if !isempty(lib)
                println(io," (satisfied by $lib)")
            else
                println(io)
            end
            if length(dep.providers) > 0
                println(io,"    - Providers:")
                for (dep,opts) in dep.providers
                    show_indented(io,dep,6)
                    println(io)
                end
            end
        end
    end
end
debug(pkg::String) = debug(STDOUT,pkg)

Remove a reference to Pkg2.

function debug(io,pkg::String)
    info("Reading build script...")
    dir = Pkg.dir(pkg)
    file = joinpath(dir,"deps/build.jl")
    context = BinDeps.PackageContext(false,dir,pkg,{})
    m = Module(:__anon__)
    body = Expr(:toplevel,:(ARGS=[$context]),:(include($file)))
    eval(m,body)
    println(io,"The package declares $(length(context.deps)) dependencies.")
    for dep in context.deps
        print(io," - Library \"$(dep.name)\"")
        if !applicable(dep)
            println(io," (not applicable to this system)")
        else
            lib = _find_library(dep)
            if !isempty(lib)
                println(io," (satisfied by $lib)")
            else
                println(io)
            end
            if length(dep.providers) > 0
                println(io,"    - Providers:")
                for (dep,opts) in dep.providers
                    show_indented(io,dep,6)
                    println(io)
                end
            end
        end
    end
end
debug(pkg::String) = debug(STDOUT,pkg)


addprocs()
@time using Alembic

using AWSCore
using AWSSQS
using JSON

function receive_messages(queue_name)
    secrets_subpath = ".cloudvolume/secrets/aws-secret.json"
    secret_path = joinpath(homedir(), secrets_subpath)
    aws_secrets = JSON.parsefile(secret_path)
    env = aws_config(creds=AWSCredentials(collect(values(aws_secrets))...))
    q = sqs_get_queue(env, queue_name)
    while true
        m = sqs_receive_message(q)
        if m != nothing
            params = JSON.parse(m[:message])
            task = params["task"]
            println("Received $(task["name"])")
            func = getfield(Main, Symbol(task["method"]))
            func(params)
            sqs_delete_message(q, m)
        else
            sleep(5)
        end
    end
end

receive_messages(ARGS[1])

Include @time on method used in task

addprocs()
@time using Alembic

using AWSCore
using AWSSQS
using JSON

function receive_messages(queue_name)
    secrets_subpath = ".cloudvolume/secrets/aws-secret.json"
    secret_path = joinpath(homedir(), secrets_subpath)
    aws_secrets = JSON.parsefile(secret_path)
    env = aws_config(creds=AWSCredentials(collect(values(aws_secrets))...))
    q = sqs_get_queue(env, queue_name)
    while true
        m = sqs_receive_message(q)
        if m != nothing
            params = JSON.parse(m[:message])
            task = params["task"]
            println("Received $(task["name"])")
            func = getfield(Main, Symbol(task["method"]))
            @time func(params)
            sqs_delete_message(q, m)
        else
            sleep(5)
        end
    end
end

receive_messages(ARGS[1])


module Geom

using Color
using Compose
using DataArrays
using DataStructures
using Gadfly

import Compose.combine # Prevent DataFrame.combine from taking over.
import Gadfly: render, element_aesthetics, inherit, escape_id,
               default_statistic, default_scales, element_coordinate_type,
               ScaleElement, svg_color_class_from_label, isconcrete
import Iterators
import Iterators: cycle, product, distinct, takestrict, chain, repeated

# Geometry that renders nothing.
immutable Nil <: Gadfly.GeometryElement
end

const nil = Nil

function render(geom::Nil, theme::Gadfly.Theme, aes::Gadfly.Aesthetics,
                scales::Dict{Symbol, ScaleElement})
end

# Catchall
function default_statistic(::Gadfly.GeometryElement)
    return Gadfly.Stat.identity()
end

include("geom/bar.jl")
include("geom/boxplot.jl")
include("geom/errorbar.jl")
include("geom/hexbin.jl")
include("geom/hline.jl")
include("geom/label.jl")
include("geom/line.jl")
include("geom/point.jl")
include("geom/rectbin.jl")
include("geom/subplot.jl")
include("geom/vline.jl")
include("geom/ribbon.jl")

end # module Geom

Fix no method error on some plots.

module Geom

using Color
using Compose
using DataArrays
using DataStructures
using Gadfly

import Compose.combine # Prevent DataFrame.combine from taking over.
import Gadfly: render, element_aesthetics, inherit, escape_id,
               default_statistic, default_scales, element_coordinate_type,
               ScaleElement, svg_color_class_from_label, isconcrete
import Iterators
import Iterators: cycle, product, distinct, takestrict, chain, repeated

# Geometry that renders nothing.
immutable Nil <: Gadfly.GeometryElement
end

const nil = Nil

function render(geom::Nil, theme::Gadfly.Theme, aes::Gadfly.Aesthetics,
                data::Gadfly.Data, scales::Dict{Symbol, ScaleElement})
end

# Catchall
function default_statistic(::Gadfly.GeometryElement)
    return Gadfly.Stat.identity()
end

include("geom/bar.jl")
include("geom/boxplot.jl")
include("geom/errorbar.jl")
include("geom/hexbin.jl")
include("geom/hline.jl")
include("geom/label.jl")
include("geom/line.jl")
include("geom/point.jl")
include("geom/rectbin.jl")
include("geom/subplot.jl")
include("geom/vline.jl")
include("geom/ribbon.jl")

end # module Geom


module Kalman

export State,
Observation,
KalmanFilter,
LinearKalmanFilter,
BasicKalmanFilter,
Model,
ObservationModel,
LinearModel,
LinearObservationModel,
predict,
update,
predictupdate,
predict!,
update!,
predictupdate!,
BasicExtendedKalmanFilter,
NonlinearModel,
NonlinearObservationModel,
ExtendedKalmanFilter
AdditiveUnscentedKalmanFilter,
UnscentedKalmanFilter,
AdditiveUnscentedModel,
AdditiveUnscentedObservationModel

include("types.jl")
include("filter.jl")
include("ekf.jl")
include("unscented.jl")

# Nullable Observations work but only for Julia v0.4
if VERSION >= v"0.4.0-"
    include("nullable.jl")
end

end # module

Fix typo that came out of rebase

module Kalman

export State,
Observation,
KalmanFilter,
LinearKalmanFilter,
BasicKalmanFilter,
Model,
ObservationModel,
LinearModel,
LinearObservationModel,
predict,
update,
predictupdate,
predict!,
update!,
predictupdate!,
BasicExtendedKalmanFilter,
NonlinearModel,
NonlinearObservationModel,
ExtendedKalmanFilter,
AdditiveUnscentedKalmanFilter,
UnscentedKalmanFilter,
AdditiveUnscentedModel,
AdditiveUnscentedObservationModel

include("types.jl")
include("filter.jl")
include("ekf.jl")
include("unscented.jl")

# Nullable Observations work but only for Julia v0.4
if VERSION >= v"0.4.0-"
    include("nullable.jl")
end

end # module


# This file is a part of JuliaFEM.
# License is MIT: see https://github.com/JuliaFEM/JuliaFEM.jl/blob/master/LICENSE

using Documenter
using JuliaFEM

deploydocs(
    deps = Deps.pip("mkdocs", "python-markdown-math"),
    repo = "github.com/JuliaFEM/JuliaFEM.jl.git")

Deploy documentation from 0.5 build

# This file is a part of JuliaFEM.
# License is MIT: see https://github.com/JuliaFEM/JuliaFEM.jl/blob/master/LICENSE

using Documenter
using JuliaFEM

deploydocs(
    deps = Deps.pip("mkdocs", "python-markdown-math"),
    repo = "github.com/JuliaFEM/JuliaFEM.jl.git",
    julia = "0.5")


counts = (String => Uint64)[]

for line in eachline(STDIN)
  for word in matchall(r"[a-z\']+", lowercase(line))
    counts[word] = get(counts, word, 0) + 1
  end
end
for (word, count) in sort(collect(counts))
  println("$word $count")
end

Use the more modern Julia dictionary constructor

counts = Dict{String, Uint64}()

for line in eachline(STDIN)
  for word in matchall(r"[a-z\']+", lowercase(line))
    counts[word] = get(counts, word, 0) + 1
  end
end
for (word, count) in sort(collect(counts))
  println("$word $count")
end


@testset "MultiQuad" begin

  include(joinpath(Hecke.pkgdir, "examples", "MultiQuad.jl"))

  c = MultiQuad.multi_quad(fmpz[3,5,7], 10)
  d = MultiQuad.simplify(c)
  @test MultiQuad.Hecke.class_group_get_pivot_info(d) == (4, BitSet([2, 4]))
  e = MultiQuad.saturate(d, 2)
  @test MultiQuad.Hecke.class_group_get_pivot_info(e) == (4, BitSet([2, 4]))
  f = MultiQuad.saturate(e, 2)
  @test MultiQuad.Hecke.class_group_get_pivot_info(f) == (1, BitSet([]))

end

Disable example tests (why are they so slow)?

@testset "MultiQuad" begin

  include(joinpath(Hecke.pkgdir, "examples", "MultiQuad.jl"))

  #@time c = MultiQuad.multi_quad(fmpz[3,5,7], 10)
  #d = MultiQuad.simplify(c)
  #@test MultiQuad.Hecke.class_group_get_pivot_info(d) == (4, BitSet([2, 4]))
  #e = MultiQuad.saturate(d, 2)
  #@test MultiQuad.Hecke.class_group_get_pivot_info(e) == (4, BitSet([2, 4]))
  #f = MultiQuad.saturate(e, 2)
  #@test MultiQuad.Hecke.class_group_get_pivot_info(f) == (1, BitSet([]))

end


@compat function piechart(; x::AbstractArray = Real[], y::AbstractArray = [], holesize::Int = 0)

    v = VegaVisualization(height = 250, width = 250)
    v.legends = [VegaLegend(fill = "color", title = "group")]

    add_data!(v, x = x, y = y)

    v.scales = [VegaScale(name = "color",
                      domain = VegaDataRef("table", "x"),
                      range = "category10",
                      _type = "ordinal")]

    v.marks = [VegaMark(_type = "arc",
                       from = VegaMarkFrom(data = "table",
                                           transform = [VegaTransform(Dict{Any, Any}("type"=> "pie", "field"=> "y"))]),
                       properties = VegaMarkProperties(enter = VegaMarkPropertySet(
                                                                                endAngle = VegaValueRef(field = "layout_end"),
                                                                                fill = VegaValueRef(field = "x", scale = "color"),
                                                                                innerRadius = VegaValueRef(value = holesize),
                                                                                outerRadius = VegaValueRef(value = 250),
                                                                                startAngle = VegaValueRef(field = "layout_start"),
                                                                                stroke = VegaValueRef(value = "white"),
                                                                                x = VegaValueRef(group = "width", mult = 0.5),
                                                                                y = VegaValueRef(group = "height", mult = 0.5)
                                                                                )
                                                      )
                        )
                ]

    #Default to Paired color scale, 12
    colorscheme!(v; palette = ("Paired", 12))
    return v
end

Fix pie chart color to work with legend!

@compat function piechart(; x::AbstractArray = Real[], y::AbstractArray = [], holesize::Int = 0)

    v = VegaVisualization(height = 250, width = 250)
    v.legends = [VegaLegend(fill = "group", title = "group")]

    add_data!(v, x = x, y = y)

    v.scales = [VegaScale(name = "group",
                      domain = VegaDataRef("table", "x"),
                      range = "category10",
                      _type = "ordinal")]

    v.marks = [VegaMark(_type = "arc",
                       from = VegaMarkFrom(data = "table",
                                           transform = [VegaTransform(Dict{Any, Any}("type"=> "pie", "field"=> "y"))]),
                       properties = VegaMarkProperties(enter = VegaMarkPropertySet(
                                                                                endAngle = VegaValueRef(field = "layout_end"),
                                                                                fill = VegaValueRef(field = "x", scale = "group"),
                                                                                innerRadius = VegaValueRef(value = holesize),
                                                                                outerRadius = VegaValueRef(value = 250),
                                                                                startAngle = VegaValueRef(field = "layout_start"),
                                                                                stroke = VegaValueRef(value = "white"),
                                                                                x = VegaValueRef(group = "width", mult = 0.5),
                                                                                y = VegaValueRef(group = "height", mult = 0.5)
                                                                                )
                                                      )
                        )
                ]

    #Default to Paired color scale, 12
    colorscheme!(v; palette = ("Paired", 12))
    return v
end


let
    function find_library(libname,filename)
        try
            dl = dlopen(joinpath(Pkg.dir(),"Clang","deps","usr","lib",filename))
            ccall(:add_library_mapping,Int32,(Ptr{Uint8},Ptr{Uint8}),libname,dl)
        catch
            try
                dl = dlopen(libname)
                dlclose(dl)
            catch
                error("Failed to find required library "*libname*". Try re-running the package script using Pkg.runbuildscript(\"pkg\")")
            end
        end
    end
    find_library("libwrapclang","libwrapclang")
end

Remove deprecated bindeps dlpath call

let
    function find_library(libname,filename)
        try
            push!(DL_LOAD_PATH, joinpath(Pkg.dir(), "Clang", "deps", "usr", "lib"))
            dl = dlopen(libname)
        catch
            try
                dl = dlopen(libname)
                dlclose(dl)
            catch
                error("Failed to find required library "*libname*". Try re-running the package script using Pkg.runbuildscript(\"pkg\")")
            end
        end
    end
    find_library("libwrapclang","libwrapclang")
end


using CGP
using Base.Test

const numinputs = 2
const numoutputs = 1
const nodearity = 2
const numperlevel = 1
const numlevels = 10
const numlevelsback = 10

funcs = default_funcs()
p = Parameters(numinputs, numoutputs, nodearity, numperlevel, numlevels, numlevelsback)

for _ = 1:100
    c = random_chromosome(p, funcs)
    execute_chromosome(c, [true, true])
    execute_chromosome(c, [true, false])
    execute_chromosome(c, [false, true])
    execute_chromosome(c, [false, false])
end

Set PRNG seed before running random tests.

using CGP
using Base.Test

const numinputs = 2
const numoutputs = 1
const nodearity = 2
const numperlevel = 1
const numlevels = 10
const numlevelsback = 10

funcs = default_funcs()
p = Parameters(numinputs, numoutputs, nodearity, numperlevel, numlevels, numlevelsback)

srand(1)

for _ = 1:100
    c = random_chromosome(p, funcs)
    execute_chromosome(c, [true, true])
    execute_chromosome(c, [true, false])
    execute_chromosome(c, [false, true])
    execute_chromosome(c, [false, false])
end


using Weave
using Base.Test

function mmtest(source, resfile, doctype)
  VER = "$(VERSION.major).$(VERSION.minor)"
  weave("documents/$source", out_path = "documents/multimedia/$resfile", doctype=doctype, plotlib=nothing)
  result =  readstring(open("documents/multimedia/$resfile"))
  ref =  readstring(open("documents/multimedia/$VER/$resfile.ref"))
  @test result == ref
  rm("documents/multimedia/$resfile")
end

mmtest("rich_output.jmd", "rich_output.md", "pandoc")
mmtest("rich_output.jmd", "rich_output.tex", "tex")

Fix test cleaning on windows

using Weave
using Base.Test

function mmtest(source, resfile, doctype)
  VER = "$(VERSION.major).$(VERSION.minor)"
  weave("documents/$source", out_path = "documents/multimedia/$resfile", doctype=doctype, plotlib=nothing)
  result =  readstring("documents/multimedia/$resfile")
  ref =  readstring("documents/multimedia/$VER/$resfile.ref")
  @test result == ref
  rm("documents/multimedia/$resfile")
end

mmtest("rich_output.jmd", "rich_output.md", "pandoc")
mmtest("rich_output.jmd", "rich_output.tex", "tex")


using IJulia.CommManager
import IJulia.metadata
import JSON: json

import Base: writemime

if Pkg.installed("Reactive") >= v"0.1.9"
    import Reactive: Signal, value, lift

    function diff(s::Signal)
        prev = value(s)
        lift((x) -> begin
            d = diff(prev, x)
            prev = x
            d
        end, s)
    end

    function send_patch(comm, d)
        jsonpatch = jsonfmt(d)
        if !isempty(jsonpatch)
            send_comm(comm, jsonpatch)
        end
    end

    metadata{ns, name}(x::Signal{Elem{ns, name}}) = Dict()

    function writemime{ns, name}(io::IO, ::MIME"text/html", x::Signal{Elem{ns, name}})
        id = pwid()

        write(io, """
""",
                  """""",
                  """
""")

        comm = Comm(:PatchStream; data=@compat Dict(:pwid => id))
        # lift patch stream
        lift((d) -> send_patch(comm, d), diff(x))
    end
end

Add check if Reactive is not installed at all.

using IJulia.CommManager
import IJulia.metadata
import JSON: json

import Base: writemime

if Pkg.installed("Reactive") !=nothing && Pkg.installed("Reactive") >= v"0.1.9"
    import Reactive: Signal, value, lift

    function diff(s::Signal)
        prev = value(s)
        lift((x) -> begin
            d = diff(prev, x)
            prev = x
            d
        end, s)
    end

    function send_patch(comm, d)
        jsonpatch = jsonfmt(d)
        if !isempty(jsonpatch)
            send_comm(comm, jsonpatch)
        end
    end

    metadata{ns, name}(x::Signal{Elem{ns, name}}) = Dict()

    function writemime{ns, name}(io::IO, ::MIME"text/html", x::Signal{Elem{ns, name}})
        id = pwid()

        write(io, """
""",
                  """""",
                  """
""")

        comm = Comm(:PatchStream; data=@compat Dict(:pwid => id))
        # lift patch stream
        lift((d) -> send_patch(comm, d), diff(x))
    end
end


function match_task(params::Dict)
    load_params(params)
    ms = make_stack()
    println("Match Task $(get_name(ms))")
    match!(ms)
    save(ms, "meshset")
end

"""
Fix the center section, solve in halves, then resolve in range around center
"""
# function solve_task(params::Dict)
# end

function render_task(params::Dict)
    load_params(params)
    ms = load("mesh", get_name())
    println("Render Task $(get_name(ms))")
    render(ms)
end

Update render_task to use mesh dir

function match_task(params::Dict)
    load_params(params)
    ms = make_stack()
    println("Match Task $(get_name(ms))")
    match!(ms)
    save(ms, "meshset")
end

"""
Fix the center section, solve in halves, then resolve in range around center
"""
# function solve_task(params::Dict)
# end

function render_task(params::Dict)
    load_params(params)
    for z in get_z_range()
        ms = load("mesh", string(z))
        println("Render Task $(get_name(ms))")
        render(ms)
    end
end


function plotlybase_syncplot(plt::Plot)
    plt.o = ORCA.PlotlyBase.Plot()
    traces = ORCA.PlotlyBase.GenericTrace[]
    for series_dict in plotly_series(plt)
        plotly_type = pop!(series_dict, :type)
        push!(traces, ORCA.PlotlyBase.GenericTrace(plotly_type; series_dict...))
    end
    ORCA.PlotlyBase.addtraces!(plt.o, traces...)
    layout = plotly_layout(plt)
    w, h = plt[:size]
    ORCA.PlotlyBase.relayout!(plt.o, layout, width = w, height = h)
    return plt.o
end

_show(io::IO, ::MIME{Symbol("image/png")}, plt::Plot{PlotlyBackend}) = ORCA.PlotlyBase.savefig(io, plotlybase_syncplot(plt), format = "png")

Allow saving to pdf, svg and eps for plotly() via ORCA

function plotlybase_syncplot(plt::Plot)
    plt.o = ORCA.PlotlyBase.Plot()
    traces = ORCA.PlotlyBase.GenericTrace[]
    for series_dict in plotly_series(plt)
        plotly_type = pop!(series_dict, :type)
        push!(traces, ORCA.PlotlyBase.GenericTrace(plotly_type; series_dict...))
    end
    ORCA.PlotlyBase.addtraces!(plt.o, traces...)
    layout = plotly_layout(plt)
    w, h = plt[:size]
    ORCA.PlotlyBase.relayout!(plt.o, layout, width = w, height = h)
    return plt.o
end

const _orca_mimeformats = Dict(
    "application/pdf" => "pdf",
    "image/png"       => "png",
    "image/svg+xml"   => "svg",
    "image/eps"       => "eps",
)

for (mime, fmt) in _orca_mimeformats
    @eval _show(io::IO, ::MIME{Symbol($mime)}, plt::Plot{PlotlyBackend}) = ORCA.PlotlyBase.savefig(io, plotlybase_syncplot(plt), format = $fmt)
end


using Cxx

cxx"""
uint64_t foo() {
    return $(1);
}
"""

@assert (@cxx foo()) = uint64(1)

Fix typo in cxx_str test

using Cxx

cxx"""
uint64_t foo() {
    return $(1);
}
"""

x = @cxx foo()
@assert x == uint64(1)


#!/usr/bin/env julia
using Pkg;

# FIXME: Specify versions here :'(
Pkg.add.([
    "PyPlot",
    "PyCall",
    "Roots",
    "Polynomials",
    "DifferentialEquations",
    "Triangle",
    "Optim",
    "Images",
    "Plots"
]);

# Precompiling installed packages
Pkg.precompile()

Add mimi and mimifund to julia hub

#!/usr/bin/env julia
using Pkg;

# FIXME: Specify versions here :'(
Pkg.add.([
    "PyPlot",
    "PyCall",
    "Roots",
    "Polynomials",
    "DifferentialEquations",
    "Triangle",
    "Optim",
    "Images",
    "Plots",
    "Mimi"
]);

# Add support for various MIMI models
Pkg.Registry.add(RegistrySpec(url = "https://github.com/mimiframework/MimiRegistry.git"))

Pkg.add.([
    "MimiFUND"
])
# Precompiling installed packages
Pkg.precompile()


#!/usr/bin/env julia

using Base.Threads, FLoops

function _picalc(numsteps)
    slice = 1.0 / numsteps

    @floop @simd for i = 1:numsteps
        x = (i - 0.5) * slice
        @reduce(sum += 4.0 / (1.0 + x ^ 2))
    end

    return sum * slice
end

function picalc(numsteps)

    println("Calculating PI using:")
    println("  ", numsteps, " slices")
    println("  ", nthreads(), " thread(s)")

    start = time()
    mypi = _picalc(numsteps)
    elapsed = time() - start

    println("Obtained value of PI: ", mypi)
    println("Time taken: ", round(elapsed; digits=3), " seconds")

end

numsteps = if length(ARGS) > 0
    parse(Int, ARGS[1])
else
    1_000_000_000
end

# Warm up kernel
print("  Warming up...")
warms = time()
_picalc(10)
warmt = time() - warms
println("done. [", round(warmt; digits=3), "s]\n")

# Run the full example
picalc(numsteps)

Set `basesize` for threaded executor of Julia FLoops example

#!/usr/bin/env julia

using Base.Threads, FLoops

function _picalc(numsteps)
    slice = 1.0 / numsteps

    @floop ThreadedEx(;simd=true, basesize=numsteps÷1000) for i = 1:numsteps
        x = (i - 0.5) * slice
        @reduce(sum += 4.0 / (1.0 + x ^ 2))
    end

    return sum * slice
end

function picalc(numsteps)

    println("Calculating PI using:")
    println("  ", numsteps, " slices")
    println("  ", nthreads(), " thread(s)")

    start = time()
    mypi = _picalc(numsteps)
    elapsed = time() - start

    println("Obtained value of PI: ", mypi)
    println("Time taken: ", round(elapsed; digits=3), " seconds")

end

numsteps = if length(ARGS) > 0
    parse(Int, ARGS[1])
else
    1_000_000_000
end

# Warm up kernel
print("  Warming up...")
warms = time()
_picalc(10)
warmt = time() - warms
println("done. [", round(warmt; digits=3), "s]\n")

# Run the full example
picalc(numsteps)


using TinySegmenter
using Base.Test

seg = Segmenter()
@test tokenize(seg, "私の名前は中野です") == ["私", "の", "名前", "は", "中野", "です"]

Add test case of empty string

using TinySegmenter
using Base.Test

seg = Segmenter()
@test tokenize(seg, "私の名前は中野です") == ["私", "の", "名前", "は", "中野", "です"]
@test tokenize(seg, "") == []


using Documenter, Compose

makedocs(
    modules = [Compose],
    clean = true,
    format = Documenter.Formats.HTML,
    sitename = "Compose.jl",
    pages = Any[
        "Home" => "index.md"
    ]
)

deploydocs(
    repo   = "github.com/Hydrotoast/Compose.jl.git",
    julia  = "0.5",
    osname = "linux",
    deps = nothing,
    make = nothing,
    target = "build"
)

Change deploydocs repo to dcjones

using Documenter, Compose

makedocs(
    modules = [Compose],
    clean = true,
    format = Documenter.Formats.HTML,
    sitename = "Compose.jl",
    pages = Any[
        "Home" => "index.md"
    ]
)

deploydocs(
    repo   = "github.com/dcjones/Compose.jl.git",
    julia  = "0.5",
    osname = "linux",
    deps = nothing,
    make = nothing,
    target = "build"
)