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

Add updated test from AHW.

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)
    # Executing on these inputs tests all possible bit combinations for inputs
    execute_chromosome(c, [0xC, 0xA])
end


module SCIP

# if SCIP is compiled with Ipopt, libipopt must be in LD_LIBRARY_PATH
if (libipopt = find_library(["libipopt"])) != ""
   @unix_only dlopen(libipopt, RTLD_LAZY|RTLD_DEEPBIND|RTLD_GLOBAL)
end

# dump all code wrapped from C interface
module CInterface
    include("wrapped/scip_defines.jl")
    include("wrapped/scip_enums.jl")
    include("wrapped/scip_typedefs.jl")
    # include("wrapped/scip_structs.jl")
    include("wrapped/scip_functions.jl")
end
using .CInterface

# some convenience functions
module Helper
    using SCIP.CInterface
    include("helper/pointer_types.jl")
    include("helper/convenience.jl")
end
using .Helper

# MathProgBase interface
include("SCIPSolverInterface.jl")

end

Check for ipopt in __init__ function

module SCIP

function __init__()
   # if SCIP is compiled with Ipopt, libipopt must be in LD_LIBRARY_PATH
   if (libipopt = find_library(["libipopt"])) != ""
      @unix_only dlopen(libipopt, RTLD_LAZY|RTLD_DEEPBIND|RTLD_GLOBAL)
   end
end

# dump all code wrapped from C interface
module CInterface
    include("wrapped/scip_defines.jl")
    include("wrapped/scip_enums.jl")
    include("wrapped/scip_typedefs.jl")
    # include("wrapped/scip_structs.jl")
    include("wrapped/scip_functions.jl")
end
using .CInterface

# some convenience functions
module Helper
    using SCIP.CInterface
    include("helper/pointer_types.jl")
    include("helper/convenience.jl")
end
using .Helper

# MathProgBase interface
include("SCIPSolverInterface.jl")

end


# Hypre.jl: Wrapper around the Hypre library (http://acts.nersc.gov/hypre/)
module Hypre
if isfile(joinpath(dirname(dirname(@__FILE__)), "deps", "deps.jl"))
    include(joinpath(dirname(dirname(@__FILE__)), "deps", "deps.jl"))
else
    error("Hypre not properly installed. Please run Pkg.build(\"Hypre\")")
end

using MPI

include("libhypre_h.jl")

include("libhypre.jl")

end # module

Add convert method for MPI.jl compat

# Hypre.jl: Wrapper around the Hypre library (http://acts.nersc.gov/hypre/)
module Hypre
if isfile(joinpath(dirname(dirname(@__FILE__)), "deps", "deps.jl"))
    include(joinpath(dirname(dirname(@__FILE__)), "deps", "deps.jl"))
else
    error("Hypre not properly installed. Please run Pkg.build(\"Hypre\")")
end

using MPI

include("libhypre_h.jl")
Base.convert(MPI_Comm, x::MPI.Comm) = x.val
include("libhypre.jl")

end # module


macro export_all_files(cur_item)
  all_files = get_all_files(eval(cur_item))

  cur_expression = quote end
  for file in all_files
    exported_object = Symbol("$(file)")
    cur_block = quote
      export $(esc(exported_object))
    end
    append!(cur_expression.args, cur_block.args)
  end
  cur_expression
end

Fix bug where files weren’t actually exported

macro export_all_files(cur_item)
  all_files = get_all_files(eval(cur_item))

  cur_expression = quote end
  for file in all_files
    cur_export = split(file, "/")[end]

    !endswith(cur_export, ".jl") && continue

    cur_export = replace(cur_export, ".jl", "")

    exported_object = Symbol(cur_export)

    cur_block = quote
      export $(esc(exported_object))
    end

    append!(cur_expression.args, cur_block.args)
  end

  cur_expression
end


include(joinpath(Pkg.dir("Gadfly"), "src/Gadfly.jl"))
importall Gadfly

Make import of Gadfly conditional

if Pkg.installed("Gadfly") != nothing
	include(joinpath(Pkg.dir("Gadfly"), "src/Gadfly.jl"))
	importall Gadfly
end


#!/usr/bin/julia
import Primes
function sumprimes(n::Int)
  #primnumbers = SharedArray{Int}(n)
  primesum=2
  parasum += @parallel (+) for i in 3:2:n
    Primes.isprime(i)*i
  end
  primesum
end
sumprimes(11)
print(2)
@time prime = sumprimes(7)
print(prime)

Change to the actual test number

#!/usr/bin/julia
import Primes
function sumprimes(n::Int)
  #primnumbers = SharedArray{Int}(n)
  primesum=2
  parasum = @parallel (+) for i in 3:2:n
    Primes.isprime(i)*i
  end
  primesum+=parasum
end
sumprimes(10)
@time prime = sumprimes(2000000)
print(prime)


macro export_all_files(cur_item)
  all_files = get_all_files(eval(cur_item))

  cur_expression = quote end
  for file in all_files
    cur_export = split(file, "/")[end]

    !endswith(cur_export, ".jl") && continue

    cur_export = replace(cur_export, ".jl", "")

    exported_object = Symbol(cur_export)

    cur_block = quote
      export $(esc(exported_object))
    end

    append!(cur_expression.args, cur_block.args)
  end

  cur_expression
end

Allow functions with bangs to be loaded

macro export_all_files(cur_item)
  all_files = get_all_files(eval(cur_item))

  cur_expression = quote end
  for file in all_files
    cur_export = split(file, "/")[end]

    !endswith(cur_export, ".jl") && continue

    cur_export = replace(cur_export, ".jl", "")

    exported_object = Symbol(cur_export)

    for added_string in ["", "!"]
      cur_export *= added_string
      exported_object = Symbol(cur_export)

      cur_block = quote
        export $(esc(exported_object))
      end

      append!(cur_expression.args, cur_block.args)
    end
  end

  cur_expression
end


using Gadfly

n = 10
plot(x=rand(n), y=rand(n), color=1000.^rand(n),
     Scale.color_log10(minvalue=1, maxvalue=1e3))


using Compose

#Run the examples
testdir = joinpath(Pkg.dir("Compose"), "test")
cd(testdir)

exampledir = joinpath(Pkg.dir("Compose"), "examples")
for ex in readdir(exampledir)
    endswith(ex, ".jl") || continue
    srand(1) #Needed so that SVG uuid is reproducible
    include(joinpath(exampledir, ex))
end

#Compare with cached output
cachedout = joinpath(Pkg.dir("Compose"), "test", "data")
differentfiles = String[]
for output in readdir(cachedout)
    cached = open(readall, joinpath(cachedout, output))
    genned = open(readall, joinpath(testdir, output))
    if cached != genned
        push!(differentfiles, output)
    else #Delete generated file
        rm(joinpath(testdir, output))
    end
end

#Print out which files differ and their diffs
if length(differentfiles)>0
    #Capture diffs
    diffs = map(
        output -> output * ":\n" *
            readall(ignorestatus(`diff $(joinpath(cachedout, output)) $(joinpath(testdir, output))`)) *
            "\n\n",
        differentfiles)
    error(string("Generated output differs from cached test output:\n",
        join(differentfiles, "\n"), "\n\n", join(diffs, "\n")))
end


using NK
using Base.Test

const LC = 100 # Landscape count
const PAIRS = [(8, 2), (8, 4), (8, 8),
(16, 2), (16, 4), (16, 8), (16, 16)]
#(24, 2), (24, 4), (24, 8), (24, 16), (24, 24),
#(48, 2), (48, 4), (48, 8), (48, 16), (48, 24), (48, 48),
#(96, 2), (96, 4), (96, 8), (96, 16), (96, 24), (96, 48), (96, 96)]

# Mean walk lengths to local optima (random interactions)
# Kauffmann, p. 57

println("N\tK\tμ\tσ")
for (n, k) = PAIRS
  # TODO: Do we actually need to do this?
  if n == k
    k = k - 1
  end
  lengths = zeros(LC)
  for i = 1:LC
    l = Landscape(n, k)
    g0 = random_genome(l)
    f0 = fitness(g0, l)
    while true
      g1 = best_neighbor(g0, l)
      f1 = fitness(g1, l)
      if f1 > f0
        g0 = g1
        f0 = f1
        lengths[i] += 1
      else
        break
      end
    end
  end
  @printf("%d\t%d\t%.2f\t%.2f\n", n, k, mean(lengths), std(lengths))
end


Make program to only write out vertices of cojam

using DataFrames

lower_idx = int(ARGS[1])
upper_idx = int(ARGS[2])

users = Dict{UTF8String, Dict{UTF8String,Int}}()
user_ids = Set{UTF8String}()
let artists_u = Dict{UTF8String, Vector{UTF8String}}(), user_id2idx = Dict{UTF8String,Int}()
    idx::Int = 1
    let data = readtable("jams.csv", header=false), len = length(data[:,2])
        for i in 1:len
            if isna(data[i,3])
                continue
            end
            user_id::UTF8String = data[i,2]
            if !in(user_ids, user_id)
                push!(user_ids, user_id)
                user_id2idx[user_id] = idx
                idx += 1
            end
            artist::UTF8String = data[i,3]
            if !haskey(artists_u, artist)
                artists_u[artist] = UTF8String[]
            end
            push!(artists_u[artist], user_id)
        end
    end

    len = length(artists_u)
    i = 0
    @printf("Generating edges (u,v) for %d <= u < %d of %d vertices\n", lower_idx, upper_idx, idx)
    for artist in artists_u
        if i % 100 == 0
            @printf("\rProcessing artist crossproduct % 6d/%d (% 3d%%)", i, len, div(i*100,len))
        end
        i += 1
        for user1 in artist[2]
            user1_idx::Int = user_id2idx[user1]
            if user1_idx < lower_idx || user1_idx >= upper_idx
                continue
            end
            for user2 in artist[2]
                if user1 < user2
                    if !haskey(users, user1)
                        users[user1] = Dict{UTF8String,Int}()
                    end
                    if !haskey(users[user1], user2)
                        users[user1][user2] = 0
                    end
                    users[user1][user2] += 1
                end
            end
        end
    end
    println()
end

graph_file = open("cojam3.graphml", "a")

println("Writing edges…")
for (user1,snd) in users
    for (user2,count) in snd
        @printf(graph_file, "\t<edge source=\"%s\" target=\"%s\">\n", user1, user2)
        @printf(graph_file, "\t\t<data key=\"cojams_num\">%d</data>\n", count)
        write(graph_file, "\t</edge>\n")
    end
end
println("…done.")
close(graph_file)


Save a dictionary of all forms, postags, and deprels.

using HDF5,JLD

# Construct mapping from strings to positive integer values
typealias StrSet Set{UTF8String}
form_hash = StrSet()
postag_hash = StrSet()
deprel_hash = StrSet()

for l in eachline(STDIN)
    l == "\n" && continue
    (form, postag, head, deprel, wvec) = split(chomp(l), '\t')
    form = push!(form_hash, form)
    postag = push!(postag_hash, postag)
    deprel = push!(deprel_hash, deprel)
end

save(ARGS[1],
     "form", sort(collect(form_hash)),
     "postag", sort(collect(postag_hash)),
     "deprel", sort(collect(deprel_hash)))


Test that type names are not shadowed by locals.

public class TypeShadowing {
    public TypeShadowing() {
        int TypeShadowing;
        TypeShadowing t = new TypeShadowing();
        t.init(t);
    }

    public void init(TypeShadowing TypeShadowing) {
        TypeShadowing t = new TypeShadowing();
    }
}


Add initial version of neutral nets example.

using NKLandscapes
using DataStructures

@doc """
Returns a list of information about the connected neutral nets at each non-empty fitness level
Each list element is a 5-tuple with the following components
*  The fit level (empty fit levels are not included)
*  The lower bound of the fit level
*  The number of genotypes at the fit level
*  The maximum size of a neutral net at the fit level
*  The ratio of maximum size neutral net over the number of genotypes
Note that if this ratio is close to 1.0, then a high proportion of the genotypes at the fitness level
  are in a "giant component" neutral net.
"""
function neutral_net_summary(counts, num_intervals)
  fit_increment = 1.0/num_intervals
  nn3 = map(x->x[3],nn_list)
  lb = minimum(nn3)
  ub = maximum(nn3)
  summary = []
  for i = lb:ub
    filtered_nn = [y[2] for y in filter(x->x[3]==i,nn_list)]
    if length(filtered_nn) > 0
      max_nn = maximum(filtered_nn)
      sum_nn = sum(filtered_nn)
      push!(summary,(i, i*fit_increment, sum_nn, max_nn, max_nn/sum_nn))
    end
  end
  return summary
end

@doc """
Prints the the list of fitness levels returned by neutral_net_summary in CSV format.
"""
function print_neutral_nets(summary)
  @printf("fit lev\tfit_lb\t total count\t   max count\tpct max\n")
  for s in summary
    @printf("%2d\t%.3f\t%12d\t%12d\t%.5f\n",s[1],s[2],convert(Int,s[3]),convert(Int,s[4]),s[5])
  end
end


sources = ["im2col.cpp", "pooling.cpp"]

compiler = "g++"
flags    = ["-fPIC", "-Wall", "-O3", "-shared"]
libname  = "libmochaext.so"
openmp   = "-fopenmp"

@osx? begin
  if !haskey(ENV, "MOCHA_FORCE_OMP")
    println("OpenMP is currently not officially supported by OS X Clang compiler yet.")
    println("(see http://clang-omp.github.io/ to install OpenMP clang extension, or")
    println("install gcc.")
    println("")
    println("Disabling OpenMP. To force enable OpenMP, set MOCHA_FORCE_OMP environment")
    println("variable.")
    println("")
    openmp = ""
  end
end : nothing

cmd = `$compiler $flags $openmp -o $libname $sources`
println("Running $cmd")
run(cmd)

Allow users to override the choice of compiler for native extensions via CXX environment variable

sources = ["im2col.cpp", "pooling.cpp"]

compiler = get(ENV, "CXX", "g++")
flags    = ["-fPIC", "-Wall", "-O3", "-shared"]
libname  = "libmochaext.so"
openmp   = "-fopenmp"

@osx? begin
  if !haskey(ENV, "MOCHA_FORCE_OMP")
    println("OpenMP is currently not officially supported by OS X Clang compiler yet.")
    println("(see http://clang-omp.github.io/ to install OpenMP clang extension, or")
    println("install gcc.")
    println("")
    println("Disabling OpenMP. To force enable OpenMP, set MOCHA_FORCE_OMP environment")
    println("variable and set the CXX environment variable to the appropriate value")
    println("to invoke GCC's g++ frontend, such as g++-5")
    println("")
    openmp = ""
  end
end : nothing

cmd = `$compiler $flags $openmp -o $libname $sources`
println("Running $cmd")
run(cmd)


Test case with circular constructors, that should be kicked out by Polyglot and javac (but isn't)

// I believe that this test case should be illegal, based on the JLC 2nd ed,
// Section 8.8.5: "It is a compile-time error for a constructor to directly
// or indirectly invoke itself through a series of one or more explicit
// constructor invocations involving <this>."
//
// javac does not catch this, although it does catch a constructor that
// calls itself, e.g.
//    CircularConstructor(String s) { this(s); }
class CircularConstructor {
    CircularConstructor(String s) {
	this(5);
    }
    CircularConstructor(int i) {
	this("s");
    }
    public static void main(String[] args) {
	new CircularConstructor("df");
    }
}


Add Julia implementation of Runge-Kutta 4th Order

function runge_kutta(y0::Float64, t0::Float64, h::Float64, steps::Int, func::AbstractString)
    result = Float64[]
    t_array = Float64[]
    expression = parse(func)
    global y = y0
    global t = t0
    push!(result, y0)
    push!(t_array, t0)

    for i = 2:(steps+1)
        tn = t
        kn1 = eval(expression)
        t = tn + h/2
        y = result[i-1] + kn1*h/2
        kn2 = eval(expression)
        t = tn + h/2
        y = result[i-1] + kn2*h/2
        kn3 = eval(expression)
        t = tn + h
        y = result[i-1] + kn3*h
        kn4 = eval(expression)
        push!(result,result[i-1] + ((kn1+2*kn2+2kn3+kn4)/6)*h)

        t = tn+h
        push!(t_array, t)
        y = result[i];
    end
    return result, t_array
end

result, t_array = runge_kutta(0.0, 0.0, 0.1, 20, "1+2t")
for i in result
	println(i)
end


using DataFrames

user_ids = Set{UTF8String}()
# In the end we will only write out user_ids as vertices if they have at least one neighbour
used_user_ids = Set{UTF8String}()
let tracks_u = Dict{UTF8String, Vector{UTF8String}}()
    let data = readtable("../jams.csv", header=false), len = length(data[:,2])
        for i in 1:len
            if isna(data[i,3]) || isna(data[i,4])
                continue
            end
            user_id::UTF8String = data[i,2]
            push!(user_ids, user_id)
            a::UTF8String = data[i,3]
            track::UTF8String = string(data[i,3], data[i,4])
            if !haskey(tracks_u, track)
                tracks_u[track] = UTF8String[]
            end
            push!(tracks_u[track], user_id)
        end
    end
    @printf("Number of vertices: %d\n", length(user_ids))

    len = length(tracks_u)
    i = 0
    for track in tracks_u
        if i % 100 == 0
            @printf("\rProcessing track crossproduct % 7d/%d (% 3d%%)", i, len, div(i*100,len))
        end
        i += 1
        if length(track[2]) > 0
            for user in track[2]
                push!(used_user_ids, user)
            end
        end
    end
    println()
end

graph_file = open("cojam-tracks.graphml", "a")

print("Writing vertices…")
for user in user_ids
    @printf(graph_file, "\t<node id=\"%s\" />\n", user)
end
println("done.")
close(graph_file)


Add test case for lower_of and upper_of properties.

include("../../src/SparseAccelerator.jl")
using SparseAccelerator

set_options(SA_ENABLE, SA_VERBOSE, SA_USE_SPMP, SA_CONTEXT)

include("utils.jl")

function foo()
    set_matrix_property(:A, SA_LOWER_OF, :B)

    m = 10
    A = generate_symmetric_nonzerodiagonal_sparse_matrix(m)
    A = generate_symmetric_nonzerodiagonal_sparse_matrix(m)

    for i = 1:2
        B = generate_symmetric_nonzerodiagonal_sparse_matrix(m)
        m = size(A, 1)
        B = generate_symmetric_nonzerodiagonal_sparse_matrix(m)
    end

    for i = 1:2
        set_matrix_property(:B, SA_UPPER_OF, :C)
        B = generate_symmetric_nonzerodiagonal_sparse_matrix(m)
        m = size(A, 1)
        C = generate_symmetric_nonzerodiagonal_sparse_matrix(m)
    end
end

@acc foo()


Test case showing some problems with multiple variable declarations in a for loop initialization, followed by a declaration in the body of the for loop.

// Polyglot as of Oct 1 03 doesn't compile this. The multiple
// declarations in the for loop initialization seem to cause
// problems.
//
class ForDecl {
    void m() {
	for (int i = 0, j = 0; i < 10; i++) {
	    boolean quad = false;
	}
    }
}


Add a new sql repl mode that uses a global dsn variable that the user can set

import Base: LineEdit, REPL

type SQLCompletionProvider <: LineEdit.CompletionProvider
    l::REPL.LineEditREPL
end

function return_callback(p::LineEdit.PromptState)
    # TODO ... something less dumb
    buf = take!(copy(LineEdit.buffer(p)))
    # local sp = 0
    # for c in buf
    #   c == '(' && (sp += 1; continue)
    #   c == ')' && (sp -= 1; continue)
    # end
    # sp <= 0
    return true
end

function evaluate_sql(s::String)
    local res
    global dsn
    try
        res = ODBC.query(dsn, s)
    catch e
        print(STDOUT, "error during sql evaluation: ", e)
        return nothing
    end
    println(STDOUT, res)
end

function setup_repl(enabled::Bool)
    # bail out if we don't have a repl
    !isdefined(Base, :active_repl) && return

    repl = Base.active_repl
    main_mode = Base.active_repl.interface.modes[1]

    # disable repl if requested
    if (!enabled)
        delete!(main_mode.keymap_dict, ']')
        return
    end

    panel = LineEdit.Prompt("sql> ";
                            prompt_prefix = Base.text_colors[:white],
                            prompt_suffix = main_mode.prompt_suffix,
                            on_enter = return_callback)

    hp = main_mode.hist
    hp.mode_mapping[:sql] = panel
    panel.hist = hp
    panel.on_done = REPL.respond(evaluate_sql, repl, panel;
                                 pass_empty = false)
    panel.complete = nothing

    const sql_keymap = Dict{Any,Any}(
        ']' =>
            function (s,args...)
                if isempty(s) || position(LineEdit.buffer(s)) == 0
                    buf = copy(LineEdit.buffer(s))
                    LineEdit.transition(s, panel) do
                        LineEdit.state(s, panel).input_buffer = buf
                    end
                else
                    LineEdit.edit_insert(s, ']')
                end
            end)

    search_prompt, skeymap = LineEdit.setup_search_keymap(hp)
    mk = REPL.mode_keymap(main_mode)
    b = Dict{Any,Any}[skeymap, mk, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults]
    panel.keymap_dict = LineEdit.keymap(b)

    main_mode.keymap_dict = LineEdit.keymap_merge(main_mode.keymap_dict, sql_keymap)
    nothing
end

toggle_sql_repl(; enabled::Bool = true) = setup_repl(enabled)


Test case for anonymous classes inheriting methods.

/* The Polyglot compiler as at Sept 29th 2003 fails to compile this code.
 * In particular, it cannot find an appropriate method foo.
 *
 */
class C {
    void m() {
	Object o = new D() {
            public void bar()
            {
                foo(5); // cannot find foo.
            }

	 };
    }
}

class D {
    void foo(int i) { }
}


load("../src/cindex")
using cindex
import cindex.CurKind
import cindex.TypKind

is_struct(cu::CXCursor) = (cu_kind(cu) == CurKind.STRUCTDECL)
is_struct(ct::CXType) = (ty_kind(cu) == TypKind.RECORD)

#function get_td_struct(cu::CXCursor)

function get_struct(cu::CXCursor)
  if cu_kind(cu) == CurKind.STRUCTDECL
    # Stupid hack because libclang doesn't expose enough information
    # to determine whether a declaration is anonymous.
    if name(cu) != "" return cu end
  elseif cu_kind(cu) == CurKind.TYPEDEFDECL
    td = cindex.getTypedefDeclUnderlyingType(cu)
    tdcu = cindex.getTypeDeclaration(td)
    if cu_kind(tdcu) == CurKind.STRUCTDECL
      return cu
    end
  end
  return None
end

function dump_structs(cl::CursorList, restrict_hdr::Any)
  for i=1:cl.size
    cu = cl[i]
    if (restrict_hdr != None && cu_file(cu) != restrict_hdr)
      continue
    end
    cur = get_struct(cu)
    if (cur != None)
      println(i, " ", name(cur))
    end
  end
end
function dump_structs(hdr::String)
  tu = tu_init(hdr)
  topcu = tu_cursor(tu)
  topcl = children(topcu)
  dump_structs(topcl, hdr)
end
dump_structs(cl::CursorList) = dump_structs(cl, None)


Add rudimentary types for the Extended Kalman Filter formulation

module EKF

abstract ExtendedKalmanFilter <: KalmanFilter

type NonlinearModel <: Model
    f::Function
    g::Function
    q::Matrix
end

type NonlinearObservationModel <: ObservationModel
    h::Function
    r::Matrix
end

type BasicExtendedKalmanFilter <: ExtendedKalmanFilter
    x::State
    f::NonlinearModel
    z::NonlinearObservationModel
end

end


module loop

export Loop

type Loop

  instance::Function
  start::Function
  stop::Function
  attach_periodic_callback::Function
  dettach_periodic_callback::Function

  function Loop()
    this = new()
    this
  end

  function instance()
  end

  function start()
  end

  function stop()
  end

  function attach_periodic_callback()
  end

  function dettach_periodic_callback()
  end

end

end # module loop


Add beginnings of the Kalman filter type hierarchy

abstract KalmanFilter

abstract LinearKalmanFilter <: KalmanFilter

abstract Model

abstract ObservationModel

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

type LinearModel <: Model
    a::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
    x0::State
    x1::State
    f::LinearModel
    z::LinearObservationModel
end


Test case for string coercion and += operator.

/* Test that we can only coerce an expression to a string if there
 * is a toString method.
 *
 */
public class ToString2 {

    public void foo() {
	String s = null;
	int i = 3;
	Object o = null;
	ToString2 o2 = null;

	s += s;
	s += o;
	s += i;
	s += o2;
       	o += s; // this should be ruled out.

    }
}


using KUnet
using MAT
include("../src/KUparser.jl")
include("../src/flist.jl")
isdefined(:d1mat) || (f=matopen("d1.mat");d1mat=read(f,"d1");close(f))

function readcorpus(m)
    d = KUparser.Sentence[]
    for ss in m
        s = KUparser.Sentence()
        s.wvec = ss["wvec"]
        s.head = isa(ss["head"], Number) ? [ss["head"]] : vec(ss["head"])
        push!(d, s)
    end
    return d
end

dev=readcorpus(d1mat["dev"])
trn=readcorpus(d1mat["trn"])
tst=readcorpus(d1mat["tst"])
s=dev[1]
p=KUparser.ArcHybrid(size(s.wvec,2))
v=KUparser.valid(p)
feats=Flist.fv021a
net = [KUnet.Layer("m11.h5"), KUnet.Layer("m12.h5")]
x=KUparser.features(p,s,feats)
y=KUnet.predict(net,x)

Load the net on cpu by default.

using KUnet
using MAT
include("../src/KUparser.jl")
include("../src/flist.jl")
isdefined(:d1mat) || (f=matopen("d1.mat");d1mat=read(f,"d1");close(f))

function readcorpus(m)
    d = KUparser.Sentence[]
    for ss in m
        s = KUparser.Sentence()
        s.wvec = ss["wvec"]
        s.head = isa(ss["head"], Number) ? [ss["head"]] : vec(ss["head"])
        push!(d, s)
    end
    return d
end

dev=readcorpus(d1mat["dev"])
trn=readcorpus(d1mat["trn"])
tst=readcorpus(d1mat["tst"])
s=dev[1]
p=KUparser.ArcHybrid(size(s.wvec,2))
v=KUparser.valid(p)
feats=Flist.fv021a
KUnet.gpu(false)
net = [KUnet.Layer("m11.h5"), KUnet.Layer("m12.h5")]
x=KUparser.features(p,s,feats)
y=KUnet.predict(net,x)


Implement simple root finding functions in julia

"""
Scientific computation, finding roots of functions.
"""

line = function(x)
    return (3 * x - 2)
end

function bisect(fn, a, b, err)
    x = a + (b - a) / 2
    y = fn(x)
    i = 1
    while abs(y) > err
        if y * fn(a) > 0
            a = x
        else
            b = x
        end
        x = a + (b - a) / 2
        y = fn(x)
        i += 1
    end
    return (x, i)
end

function regula_falsi(fn, a, b, err)
    x = (a * fn(b) - b * fn(a))/(fn(b) - fn(a))
    y = fn(x)
    i = 1
    while abs(y) > err
        if y * fn(a) > 0
            a = x
        else
            b = x
        end
        x = (a * fn(b) - b * fn(a))/(fn(b) - fn(a))
        y = fn(x)
        i += 1
    end
    return (x, i)
end

function secant_method(fn, a, b, err)
    x = b - fn(b)*(a - b) / (fn(a) - fn(b))
    i = 1
    while abs(fn(x)) > err
        b = a
        a = x
        x = b - fn(b)*(a - b) / (fn(a) - fn(b))
        println(x)
        i += 1
    end
    return (x, i)
end

println("Methods with total number of iterations")
println("Bisection")
println(bisect(line, -10, 10, 0.001))
println(bisect(line, -20, 30, 0.001))
println(bisect(line, -1, 10, 0.001))

println("Regula falsi")
println(regula_falsi(line, -10, 10, 0.001))
println(regula_falsi(line, -20, 30, 0.001))
println(regula_falsi(line, -1, 10, 0.001))

println("Secant method")
println(secant_method(line, -10, 10, 0.001))
println(secant_method(line, -20, 30, 0.001))
println(secant_method(line, -1, 10, 0.001))


addprocs()
@time using Alembic

using AWSCore
using AWSSQS
using JSON

function receive_message(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)
    # This is a hack, using https://github.com/jingpengw/AWSCore.jl
    # + expanding the sqs_get_queue function. Should fork AWSCore
    # for Alembic.
    r = AWSSQS.sqs(env, "GetQueueUrl", QueueName = queue_name)
    url = r["QueueUrl"]
    q = merge(env, Dict(:resource => HTTP.URI(url).path))
    t = 0
    while t == 0
        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)
            t += 1
        else
            sleep(5)
        end
    end
end

receive_message(ARGS[1])


typealias Sigmas Array{Array{Float64,1},1}

abstract UnscentedKalmanFilter <: KalmanFilter

type AdditiveUnscentedObservationModel <: ObservationModel
    h::Function
    r::Matrix
end

type AdditiveUnscentedModel <: Model
    f::Function
    q::Matrix
end

type AdditiveUnscentedKalmanFilter <: UnscentedKalmanFilter
    x::State
    f::AdditiveUnscentedModel
    z::AdditiveUnscentedObservationModel
    σ::Sigmas
    α::Real
    β::Real
    κ::Real
    wm::Vector
    wc::Vector
    function AdditiveUnscentedKalmanFilter(x::State,f::AdditiveUnscentedModel,z::AdditiveUnscentedObservationModel,α::Real,β::Real,κ::Real)
        σ = sigma(x,α,κ)
        (wm,wc) = sigmaweights(length(x.x),α,β,κ)
        new(x,f,z,σ,α,β,κ,wm,wc)
    end
end


Test case for inheritance of super class fields by anonymous classes.

/* The Polyglot compiler as at Sept 29th 2003 fails to compile this code.
 * In particular, it cannot find an appropriate field j.
 *
 */
class C {
    void m() {
	Object o = new D() {
            public void bar()
            {
                int i = j;
            }

	 };
    }
}

class D {
    int j = 5;
}


Add initial implementation of mu_lambda.

# mu+lambda Evolution-strategy algorthm.

# Given:
# mu:     base population size preserved between generations.  Usually 1 in standard CGP
# lambda:  number of added individuals undergoing fitness evaluation in each generation
# goal:    truth table representation of the Boolean function that the circuit is supposed to compute
# perfect: the perfect fitness of a circuit, i. e., the fitness of a circuit that exactly computes the goal

#   for i in 1:mu+lambda parallel do
#     pop[i]= new Chromosome
#     truth_table = execute_chromosome(pop[i])
#     fit[i] = fitness(truth_table,goal)
#   end
#   for t in 1:num_generations
#     rank = permutation of 1:mu+lambda so that fit[rank[i]] >= fit[rank[j]] for i in 1:mu and all j > i
#     if fit[rank[1]] == perfect return(pop[rank[1]]) end
#     for i in 1:mu
#       shallow copy pop[rank[i]] to newpop[i]  # copy mu most fit chromosomes to first mu positions of newpop
#       newfit[i] = fit[rank[i]]
#     end
#     for j in mu:mu+lambda parallel do
#       deep copy newpop[j % mu + 1] to newpop[j]   # duplicate mu most fit chromosomes
#       mutate newpop[j]
#       truth_table = execute_chromosome(newpop[j])
#       newfit[j] = fitness(truth_table,goal)
#     end
#     pop = newpop
#     fit = newfit
#   end
#   return chromosome[rank[1]]

export mu_lambda

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

    pop = @parallel (vcat) for _ = 1:(μ + λ)
        random_chromosome()
    end
    fit = @parallel (vcat) for c = pop
        fitness(c, g)
    end

    for t = 1:n
        rank = sortperm(fit)

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

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

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

        pop = newpop
        fit = newfit
    end

    return (pop[rank[1]], n)
end


#' @@name AbstractDataFrame
#'
#' @@description
#'
#' An AbstractDataFrame is a Julia abstract type for which all concrete
#' types expose an database-like interface.
abstract AbstractDataFrame <: Associative{String, Any}

#' @@name colmissing
#'
#' @@description
#'
#' Count the number of missing values in every column of an AbstractDataFrame.
#'
#' @@arg adf::AbstractDataFrame An AbstractDataFrame.
#'
#' @@return missing::Vector{Int} The number of missing values in each column.
#'
#' @@examples
#'
#' df = DataFrame(A = 1:3, B = ["x", "y", "z"])
#' colmissing(df)
function colmissing(adf::AbstractDataFrame) # -> Vector{Int}
    nrows, ncols = size(adf)
    missing = zeros(Int, ncols)
    for j in 1:ncols
        missing[j] = countna(adf[j])
    end
    return missing
end

Fix element type declaration of AbstractDataFrame

#' @@name AbstractDataFrame
#'
#' @@description
#'
#' An AbstractDataFrame is a Julia abstract type for which all concrete
#' types expose an database-like interface.
abstract AbstractDataFrame <: Associative{Symbol, Any}

#' @@name colmissing
#'
#' @@description
#'
#' Count the number of missing values in every column of an AbstractDataFrame.
#'
#' @@arg adf::AbstractDataFrame An AbstractDataFrame.
#'
#' @@return missing::Vector{Int} The number of missing values in each column.
#'
#' @@examples
#'
#' df = DataFrame(A = 1:3, B = ["x", "y", "z"])
#' colmissing(df)
function colmissing(adf::AbstractDataFrame) # -> Vector{Int}
    nrows, ncols = size(adf)
    missing = zeros(Int, ncols)
    for j in 1:ncols
        missing[j] = countna(adf[j])
    end
    return missing
end


Add module for dealing with file inuput/output

##
# Functions for file reading and writing
#
# Dan Kolbman 2014
##

module DataIO

# Read a configuration file
# Params:
#   filen - the path for the configuration file
# Returns:
#   A Dict with param, value pairs
function readConf(filen)
  # Reads data into an array separated by ' '
  data = readdlm(filen,' ')
  params = Dict{String, Any}()
  # Each element in the array corresponds to a parameter
  for line in 1:size(data,1)
    # The first element is the parameter name
    key = data[line,1]
    vals = Array(Any,0)
    # Strip all empty entries
    for elem in 2:size(data,2)
      if (data[line,elem] != "")
        push!(vals,data[line,elem])
      end
    end
    # If only one value, assign it to the key
    if(size(vals,1) == 1)
      params[key] = vals[1]
    # If more than one value, assign the array as the value
    elseif(size(vals,1) > 1)
      params[key] = vals
    end
  end
  return params

end

# Write a configuration file
# Params
#   filen - the path to write the configuration file to
#   conf - a Dict of parameters
function writeConf(filen, conf)
  f = open(filen, "w")
  for key in keys(conf)
    line = key
    # If the parameter has multiple values..
    if(typeof(conf[key]) == Array{Any,1})
      # Write each value
      for val in conf[key]
        line = string(line, " ", val)
      end
    else
      # Or just write the single value
      line = string(line, " ", conf[key])
    end
    write(f, string(line,"\n"))
  end
  close(f)
end

#Testing
#conf1 = readConf("defaults.cnf")
#writeConf("newconf.cnf", conf1)
#conf2 = readConf("newconf.cnf")
#println(conf2)

end


# http://homepages.inf.ed.ac.uk/imurray2/pub/10ess/elliptical_slice.m
# https://github.com/jobovy/bovy_mcmc/blob/master/bovy_mcmc/elliptical_slice.py

function elliptical_slice(xx, prior::Array{Float64,}, log_like_fn, cur_log_like)
    D = length(xx);
    cur_log_like = log_like_fn(xx);
    angle_range = 0;
    hh = log(rand()) + cur_log_like;

    # Set up a bracket of angles and pick a first proposal.
    # "phi = (theta'-theta)" is a change in angle.
    if angle_range <= 0
        # Bracket whole ellipse with both edges at first proposed point
        phi = rand() * 2*pi;
        phi_min = phi - 2*pi;
        phi_max = phi;
    else
        # Randomly center bracket on current point
        phi_min = -angle_range*rand();
        phi_max = phi_min + angle_range;
        phi = rand()*(phi_max - phi_min) + phi_min;
    end

    xx_prop = xx  # TODO: Need this due to scoping rules?

    # Slice sampling loop
    while true
        # Compute xx for proposed angle difference and check if it's on the slice
        xx_prop = xx*cos(phi) + nu*sin(phi);
        cur_log_like = log_like_fn(xx_prop);
        if cur_log_like > hh
            # New point is on slice, ** EXIT LOOP **
            break;
        end
        # Shrink slice to rejected point
        if phi > 0
            phi_max = phi;
        elseif phi < 0
            phi_min = phi;
        else
            error("BUG DETECTED: Shrunk to current position and still not acceptable.");
        end
        # Propose new angle difference
        phi = rand() * (phi_max - phi_min) + phi_min;
    end
    return xx_prop, cur_log_like
end

# User specified Cholesky of prior covariance:
function elliptical_slice(xx, Sigma::Array{Float64,2}, llk, cur_llk)
    nu = reshape(Sigma'*randn(D, 1), size(xx));
    elliptical_slice(xx, nu, llk, cur_llk)
end


Include Jonathan's function to correct contrast via moving mean

using NearestNeighbors
using Distances

#Given features (e.g. timestamp and mean intensity) and values (e.g. mean intensity),
#compute bias corrected values by subtracting from each point the
#mean of the values of its nearest neighbors in feature space
#The distance_threshold argument sets a cutoff for which neighbours
#to consider.

#features should have shape (nf, np), where nf is the
#number of features and np is the number of points
function f{T}(features::Array{T,2},values::Array,distance_threshold)
	n = size(A,2)
	kdtree=NNTree(features,NNTree.Chebyshev)

	target_values = zeros(size(values))
	for i in 1:n
		nns = map(x->x[1],filter(x->x[2]<distance_threshold,knn(kdtree, A[:,i], 30)))
		target_values[i]=values[i]-mean(values[nns])
	end
	return target_values
end


using Graphs

file = open("likes-10000.csv", "r")
data = readcsv(file,ASCIIString)

# Simple statistics
@printf("Analysing %d likes on %d jams by %d users.\n", length(data[:,1]), length(unique(data[:,1])), length(unique(data[:,2])))

# Generate colike-users graph where users are connected if they liked the same jame
users = Dict()
for i in 1:length(data[:,1])
    jam = data[i,:]
    if haskey(users, jam[2])
        push!(users[jam[2]], jam[1])
    else
        users[jam[2]] = [jam[1]]
    end
end

coliked_users = Set()
colike_users = Dict()
for user1 in users
    for user2 in users
        if user1[1] < user2[1]
            colikes = intersect(user1[2], user2[2])
            if length(colikes) > 0
                colike_users[(user1[1], user2[1])] = colikes
                push!(coliked_users, user1[1])
                push!(coliked_users, user2[1])
            end
        end
    end
end

@printf("There are %d pairs of users who liked at least one common jam.\n", length(colike_users))

colike_users_graph = inclist(KeyVertex{ASCIIString}, ExEdge{KeyVertex{ASCIIString}}, is_directed=false)
vertices = Dict()
for user in coliked_users
    vertex = KeyVertex{ASCIIString}(num_vertices(colike_users_graph) + 1, user)
    vertices[user] = add_vertex!(colike_users_graph, vertex)
end

for pair in colike_users
    u = vertices[pair[1][1]]
    v = vertices[pair[1][2]]
    add_edge!(colike_users_graph, u, v)
    edges = filter(e -> in(v, [e.source, e.target]), out_edges(u, colike_users_graph))
    append!(edges, filter(e -> in(u, [e.source, e.target]), out_edges(v, colike_users_graph)))
    for e in edges
        e.attributes["colikes"] = pair[2]
        e.attributes["coliles_num"] = length(pair[2])
    end
end

@printf("Generated co-like graph with %d vertices and %d edges", num_vertices(colike_users_graph), num_edges(colike_users_graph))


include("bounding_box.jl")

function test_bounding_box()

	X::Float64 = randn()
	Y::Float64 = randn()
	Z::Float64 = randn()
	THETA1::Float64 = randn()
	THETA2::Float64 = randn()
	THETA3::Float64 = randn()
	R1::Array{Float64,1} = 1.0
	R2::Array{Float64,1} = 2.0

	(	lbx::Float64,
		ubx::Float64,
		lby::Float64,
		uby::Float64,
		lbz::Float64,
		ubz::Float64) = bounding_box(	X,
									Y,
									Z,
									THETA1,
									THETA2,
									THETA3,
									R1,
									R2)

	t::Array{Float64, 1} = linspace(0, 2*pi, 100000)
	x = xc + r1*cos(t)*cos(theta2)*cos(theta3) - r2*cos(theta2)*sin(t)*sin(theta3);
y = yc + r1*cos(t)*(cos(theta1)*sin(theta3) + cos(theta3)*sin(theta1)*sin(theta2)) + r2*sin(t)*(cos(theta1)*cos(theta3) - sin(theta1)*sin(theta2)*sin(theta3));
z = zc + r1*cos(t)*(sin(theta1)*sin(theta3) - cos(theta1)*cos(theta3)*sin(theta2)) + r2*sin(t)*(cos(theta3)*sin(theta1) + cos(theta1)*sin(theta2)*sin(theta3));

	nothing
end

test_create_cell_lists()


Test shadowing of method names: an inherited method should shadow a method in an enclosing class.

class B {
      private void m(boolean x) {
            System.out.println("A.B.m");
      }
  }

class A {
  void m(int x) {
        System.out.println("A.m");
  }

  static class C extends B {
    static class D {
      void f() {
        m(1);
      }
    }
  }

  public static void main(String[] args) {
    new A.C.D().f();
  }
}


Add skeleton for BasicKalmanFilter predict-update

function predict(x::BasicKalmanFilter)
    x1 = apply(x.f,x.x)
    # Needs to apply f to the covariance matrix
end

function Base.apply(f::LinearModel,x::State)
    f.a.*x + rand(gmvnormal(f.q))
end

function update(x::BasicKalmanFilterP)

end


#
# Run:
#   julia hello.jl
#
using UnicodePlots
using Distributions
f() = rand(Normal())

println("hello, world")
@show f()
println(lineplot(1:10, (1:10).^2))


Make program to only write out slices of edges of cojam

using DataFrames

user_ids = Set{UTF8String}()
# In the end we will only write out user_ids as vertices if they have at least one neighbour
used_user_ids = Set{UTF8String}()
let artists_u = Dict{UTF8String, Vector{UTF8String}}()
    let data = readtable("jams.csv", header=false), len = length(data[:,2])
        for i in 1:len
            if isna(data[i,3])
                continue
            end
            user_id::UTF8String = data[i,2]
            push!(user_ids, user_id)
            artist::UTF8String = data[i,3]
            if !haskey(artists_u, artist)
                artists_u[artist] = UTF8String[]
            end
            push!(artists_u[artist], user_id)
        end
    end
    @printf("Number of vertices: %d\n", length(user_ids))

    len = length(artists_u)
    i = 0
    for artist in artists_u
        if i % 100 == 0
            @printf("\rProcessing artist crossproduct % 6d/%d (% 3d%%)", i, len, div(i*100,len))
        end
        i += 1
        if length(artist[2]) > 0
            for user in artist[2]
                push!(used_user_ids, user)
            end
        end
    end
    println()
end

graph_file = open("cojam3.graphml", "a")

println("Writing vertices…")
for user in user_ids
    @printf(graph_file, "\t<node id=\"%s\" />\n", user)
end
println("…done.")
close(graph_file)


min_version = v"3.0"

try
    @pyimport yt
    yt_version = convert(VersionNumber, yt.__version__)
    if yt_version < min_version
        println("Your version of yt is not up to date. You need at least version $min_version")
    end
catch import_err
    println("yt is not installed with the Python that is in the current path.")
end


# This demos how real-time audio manipulation can be done using AudioNodes. To
# run it, hook up some input audio to your default recording device and run the
# script. The demo will run for 10 seconds alternating the node between a muted
# and unmuted state
using AudioIO

type MutableNode <: AudioIO.AudioNode
  active::Bool
  deactivate_cond::Condition
  muted::Bool

  function MutableNode(muted::Bool)
    new(false, Condition(), muted)
  end
end

function MutableNode()
  MutableNode(false)
end

import AudioIO.render
function render(node::MutableNode, device_input::AudioIO.AudioBuf, info::AudioIO.DeviceInfo)
  return device_input .* !node.muted, AudioIO.is_active(node)
end

function mute(node::MutableNode)
  node.muted = true
end

function unmute(node::MutableNode)
  node.muted = false
end

mutableNode = MutableNode()
AudioIO.play(mutableNode)
muteTransitions = { true => unmute, false => mute }
for i in 1:10
  sleep(1)
  muteTransitions[mutableNode.muted](mutableNode)
end


function euler0015(n::Integer)
  n=big(n)
  Int(div(prod(n+1:2n,1),factorial(big(n))))
end

function euler0015_2(n::Integer)
  factorial(2*big(n))/factorial(big(n))^2
end

function euler0015_3(n::Integer)
  n=big(n)
  prod(n+1:2n,1)//factorial(big(n))
end

euler0015(3)
@time result=euler0015(big(20))
println(result)

euler0015_2(3)
@time result=euler0015_2(big(20))
println(Int(result))

euler0015_3(3)
@time result=euler0015_3(big(20))
println(result)


Implement abstract agent type and constructor.

type Agent
	alive::Bool

	function Agent()
		return new(Bool(true))
	end
end

function isAlive{T <: Agent}(a::T)
	return a.alive;
end

function setAlive{T <: Agent}(a::T, bv::Bool)
	a.alive = bv;
	nothing;
end


Add preliminary evolution tests. Only stochastic for now.

include("../src/CGP.jl")

using CGP
using Base.Test

function parameters()
    numperlevel = 2
    numlevels = 1
    numlevelsback = 1
    numinputs = 2
    numoutputs = 2

    mu = 100
    lambda = 100
    mutrate = 0.05
    targetfitness = 1.0
    funcs = default_funcs()

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

function goal_half_adder()
    g = Goal(2, (0b0110, 0b1000))

    return g
end

p = parameters()
g = goal_half_adder()
n = 100

# TODO: Add correctness tests
r = mu_lambda(p, g, n)

@assert fitness(r[1], g) == p.targetfitness
@assert r[2] <= n

print_chromosome(r[1])


function f(x::Array{Float64,2})
  x[1]^2 + x[2]^2
end
function gradient(x::Array{Float64,2})
  [2*x[1] 2*x[2]]
end

# Initial conditions
x0 = [0.0 0.0]

# Tuning parameters
epsilon = .001  # step size
L = 10          # number of steps

# Basic tests on regular interface
x,gx = hmc_sampler(x0, f, gradient, epsilon, L)

@assert isa(x,Array{Float64,2})
@assert isa(gx,Float64)

# Basic tests on Density interface
x,gx = hmc_sampler(x0, dd, epsilon, L)

@assert isa(x,Array{Float64,2})
@assert isa(gx,Float64)


# Unit test for QDLDL.jl
using QDLDL, Test, Random, Statistics, LinearAlgebra, SparseArrays

rng = Random.MersenneTwister(131123)

function generateSparsePosDefMatrix(rng,n::Int64,density::Float64)
      X = sprand(rng, n, n, density)
      X = 0.5(X + X')
      # make random  symmetric matrix pos def
      X = X + (2 * n + 1) * sparse(1.0I, n, n);
      return X
  end

dim = rand(rng, 50:1:200);
density = rand(rng, 0.1:0.1:0.4)
A = generateSparsePosDefMatrix(rng, dim, density)
xtrue = rand(rng, dim)
btrue = A * xtrue

F = QDLDL.qdldl(A)

@testset "Basic QDLDL.jl test" begin

  x1 = QDLDL.solve(F, btrue)
  @test norm(xtrue - x1,Inf) < 1e-10

  x2 = F \ btrue
  @test norm(xtrue - x2, Inf) < 1e-10

  x3 = copy(btrue)
  QDLDL.solve!(F, x3)
  @test norm(xtrue - x3, Inf) < 1e-10
end
nothing


Add a script to classify packages' travis usage

#!/usr/bin/env julia
isdir(Pkg.dir("YAML")) || Pkg.add("YAML")

module ClassifyPackages
using YAML

cd(dirname(@__FILE__))
allnames = filter!(p -> isdir(p) && p != ".git" &&
    p != "METADATA", readdir())

type PackageInfo
    name::String
    hastravis::Bool
    travisyml::Dict
    language::String
    usesppa::Bool
    jlminver::VersionNumber
    jlmaxver::VersionNumber # looking just at package require, not
# metadata require, so not a complete indicator of package deprecation
    lastcommit::DateTime
end

pkgs = Array(PackageInfo, length(allnames))

for (i, p) in enumerate(allnames)
    pkgs[i] = PackageInfo(p, false, Dict(), "", false,
        v"0.0", typemax(VersionNumber), now())
    ymlpath = joinpath(p, ".travis.yml")
    if isfile(ymlpath)
        pkgs[i].hastravis = true
        ymlstring = readstring(ymlpath)
        yml = YAML.load(ymlstring)
        pkgs[i].travisyml = yml
        if haskey(yml, "language")
            pkgs[i].language = yml["language"]
        else
            pkgs[i].language = ""
        end
        pkgs[i].usesppa = contains(ymlstring, "ppa:staticfloat")
    end
    requirepath = joinpath(p, "REQUIRE")
    if isfile(requirepath)
        reqs = Pkg.Reqs.parse(requirepath)
        if haskey(reqs, "julia")
            juliareq = reqs["julia"]
            if length(juliareq.intervals) == 1
                interval = juliareq.intervals[1]
                pkgs[i].jlminver = interval.lower
                pkgs[i].jlmaxver = interval.upper
            else
                error("complicated julia requirement for $p: $juliareq")
            end
        end
    end
    cd(p) do
        pkgs[i].lastcommit = Dates.unix2datetime(parse(Int,
            readchomp(`git log -1 --format=%ct`)))
    end
end

#=
# interesting notes:
for f in fieldnames(PackageInfo)
    @eval $f = map(p -> p.$f, pkgs)
end
# or language = getfield.(pkgs, [:language])
unique(language) # julia, cpp, ruby, none
name[language .== "ruby"] # only MsgPackRpcServer
name[(language .== "cpp") & ~usesppa] # only SystemImageBuilder
name[(language .== "") & hastravis] # only Cxx
name[(language .== "julia") & usesppa] # BuildExecutable and Playground for julia-deps patchelf

name[~haskey.(travisyml, ["julia"]) & (language .== "julia")]
# 7 packages: 5 with matrices, CauseMap and LLLplus missing julia entries
=#

end # module


println("Hello World!");

# https://github.com/docker-library/julia/pull/6
download("https://google.com");

Add "Pkg.add" to our Julia test to ensure it continues functioning properly

println("Hello World!");

# https://github.com/docker-library/julia/pull/6
download("https://google.com");

# https://github.com/docker-library/julia/pull/9
Pkg.add("JSON");


Add neural networks zeroing out function

include("abstract_neural_networks.jl")

function zero!(nn::NeuralNetworkStorage)
    # Zeroes out all of the arrays in the neural network type
    names = names(nn)
    types = nn.types
    for t,n in zip(types, names)
        if isa(t, Weights)
        fill!(nn.(n), 0)
        end
    end
end


from libcpp.vector cimport vector
from libcpp.map cimport map
from libc.stddef cimport size_t

from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.common._random_fwd_h cimport rng_t

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda":
    cdef cppclass model_definition:
        model_definition(size_t, size_t) except +

    cdef cppclass state:
        double perplexity()
        size_t nentities()
        size_t ntopics()
        size_t nwords()
        size_t nterms(size_t) except +

        vector[vector[size_t]] assignments()
        vector[vector[size_t]] dish_assignments()
        vector[vector[size_t]] table_assignments()
        vector[vector[float]] document_distribution()
        vector[map[size_t, float]] word_distribution()

        float score_assignment()
        float score_data(rng_t &)

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda::state":
    shared_ptr[state] initialize(const model_definition &defn,
                                 float alpha, float beta, float gamma,
                                 size_t initial_dishes,
                                 vector[vector[size_t]] &docs,
                                 rng_t & rng) except +

Improve dish/table assignment initialization function

from libcpp.vector cimport vector
from libcpp.map cimport map
from libc.stddef cimport size_t

from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.common._random_fwd_h cimport rng_t

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda":
    cdef cppclass model_definition:
        model_definition(size_t, size_t) except +

    cdef cppclass state:
        double perplexity()
        size_t nentities()
        size_t ntopics()
        size_t nwords()
        size_t nterms(size_t) except +

        vector[vector[size_t]] assignments()
        vector[vector[size_t]] dish_assignments()
        vector[vector[size_t]] table_assignments()
        vector[vector[float]] document_distribution()
        vector[map[size_t, float]] word_distribution()

        float score_assignment()
        float score_data(rng_t &)

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda::state":
    shared_ptr[state] initialize(const model_definition &defn,
                                 float alpha, float beta, float gamma,
                                 size_t initial_dishes,
                                 vector[vector[size_t]] &docs,
                                 rng_t & rng) except +

    initialize_explicit "microscopes::lda::state::initialize" (
                                 const model_definition &defn,
                                 float alpa, float beta, float gamma,
                                 const vector[vector[size_t]] &dish_assignments,
                                 const vector[vector[size_t]] &table_assignments,
                                 vector[vector[size_t]] &docs,
                                 rng_t &rng) except +


#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

cimport cython
from libc.stdint cimport int64_t

cdef type Timestamp

@cython.final
cdef class WindowedValue(object):
  cdef public object value
  cdef public object windows
  cdef public int64_t timestamp_micros
  cdef object timestamp_object

  cpdef WindowedValue with_value(self, new_value)

  @staticmethod
  cdef inline bint _typed_eq(WindowedValue left, WindowedValue right) except? -2

@cython.locals(wv=WindowedValue)
cdef inline WindowedValue create(
  object value, int64_t timestamp_micros, object windows)

Remove the inline from WindowedValue.create()

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

cimport cython
from libc.stdint cimport int64_t

cdef type Timestamp

@cython.final
cdef class WindowedValue(object):
  cdef public object value
  cdef public object windows
  cdef public int64_t timestamp_micros
  cdef object timestamp_object

  cpdef WindowedValue with_value(self, new_value)

  @staticmethod
  cdef inline bint _typed_eq(WindowedValue left, WindowedValue right) except? -2

@cython.locals(wv=WindowedValue)
cdef WindowedValue create(
  object value, int64_t timestamp_micros, object windows)


cimport ccygroonga

def init():
    return ccygroonga.grn_init()

cdef class Database:
    cdef ccygroonga.grn_ctx* _c_ctx
    cdef ccygroonga.grn_obj* _c_db

    def __cinit__(self, ccygroonga.grn_ctx* c_ctx, ccygroonga.grn_obj* c_db):
        self._c_ctx = c_ctx
        self._c_db = c_db

cdef class Context:
    cdef ccygroonga.grn_ctx* _c_ctx

    def __cinit__(self):
        self._c_ctx = ccygroonga.grn_ctx_open(0)

    def db_create(self, path):
        c_path = path.encode('UTF-8')
        c_db = ccygroonga.grn_db_create(self._c_ctx, c_path, NULL)
        return Database(self._c_ctx, c_db)

    def fin(self):
        return ccygroonga.grn_ctx_fin(self._c_ctx)

def fin():
    return ccygroonga.grn_fin()

Write a factory function to create a Database instance

cimport ccygroonga

def init():
    return ccygroonga.grn_init()

def fin():
    return ccygroonga.grn_fin()

cdef new_database(ccygroonga.grn_ctx* c_ctx, ccygroonga.grn_obj* c_db):
    db = Database()
    db._c_ctx = c_ctx
    db._c_db = c_db

cdef class Database:
    cdef ccygroonga.grn_ctx* _c_ctx
    cdef ccygroonga.grn_obj* _c_db

cdef class Context:
    cdef ccygroonga.grn_ctx* _c_ctx

    def __cinit__(self):
        self._c_ctx = ccygroonga.grn_ctx_open(0)

    def db_create(self, path):
        c_path = path.encode('UTF-8')
        c_db = ccygroonga.grn_db_create(self._c_ctx, c_path, NULL)
        return new_database(self._c_ctx, c_db)

    def fin(self):
        return ccygroonga.grn_ctx_fin(self._c_ctx)


from math import log

cdef extern from "mtwist-1.1/mtwist.c":
    double mt_drand()
    void mt_seed32new(unsigned int)

def uniform():
    return mt_drand()

def exp(p):
    u = 1.0 - uniform()
    return -log(u)/p

def seed(s):
    mt_seed32new (s)

Use identical names for C and python

from math import log

# cdef extern void c_eject_tomato "eject_tomato" (float speed)
cdef extern from "mtwist-1.1/mtwist.c":
    double c_mt_drand "mt_drand" ()
    void c_mt_seed32new "mt_seed32new" (unsigned int)

#def uniform():
#    return mt_drand()
#
#def exp(p):
#    u = 1.0 - uniform()
#    return -log(u)/p
#
#def seed(s):
#    mt_seed32new (s)

def mt_drand():
    return c_mt_drand()

def mt_seed32new(s):
    c_mt_seed32new (s)


__doc__ = u"""
    >>> test()
    neg False
    pos True
    neg
    pos
    neg
    pos
"""

def test():
    cdef long neg = -1
    cdef unsigned long pos = -2 # will be a large positive number

    print "neg", neg > 0
    print "pos", pos > -

    D = { neg: 'neg', pos: 'pos' }

    print D[<object>neg]
    print D[<object>pos]

    print D[neg]
    print D[pos]

Fix test case syntax error

__doc__ = u"""
    >>> test()
    neg False
    pos True
    neg
    pos
    neg
    pos
"""

def test():
    cdef long neg = -1
    cdef unsigned long pos = -2 # will be a large positive number

    print "neg", neg > 0
    print "pos", pos > 0

    D = { neg: 'neg', pos: 'pos' }

    print D[<object>neg]
    print D[<object>pos]

    print D[neg]
    print D[pos]


from daapserver cimport constants

cimport cython

cdef class TreeRevisionStorage(object):
    cpdef public int revision
    cdef int last_operation
    cdef object storage

    @cython.locals(low=cython.int, middle=cython.int, high=cython.int)
    cdef get_index(self, object key, int revision)

Fix tests for compiled code.

from daapserver cimport constants

cimport cython

cdef class TreeRevisionStorage(object):
    cpdef public int revision
    cdef public int last_operation
    cdef object storage

    @cython.locals(low=cython.int, middle=cython.int, high=cython.int)
    cdef get_index(self, object key, int revision)


cimport libc

cimport libc.stdio
cimport libc.errno
cimport libc.float
cimport libc.limits
cimport libc.locale
cimport libc.signal
cimport libc.stddef
cimport libc.stdint
cimport libc.stdio
cimport libc.stdlib
cimport libc.string

from libc cimport errno
from libc cimport float
from libc cimport limits
from libc cimport locale
from libc cimport signal
from libc cimport stddef
from libc cimport stdint
from libc cimport stdio
from libc cimport stdlib
from libc cimport string

from libc.errno  cimport *
from libc.float  cimport *
from libc.limits cimport *
from libc.locale cimport *
from libc.signal cimport *
from libc.stddef cimport *
from libc.stdint cimport *
from libc.stdio  cimport *
from libc.stdlib cimport *
from libc.string cimport *

libc.stdio.printf("hello %s\n", b"world")
stdio.printf("hello %s\n", b"world")
printf("hello %s\n", b"world")

Disable test for libc.stdint (missing stdint.h in MSVC)

cimport libc

cimport libc.stdio
cimport libc.errno
cimport libc.float
cimport libc.limits
cimport libc.locale
cimport libc.signal
cimport libc.stddef
#cimport libc.stdint # XXX MSVC
cimport libc.stdio
cimport libc.stdlib
cimport libc.string

from libc cimport errno
from libc cimport float
from libc cimport limits
from libc cimport locale
from libc cimport signal
from libc cimport stddef
#from libc cimport stdint # XXX MSVC
from libc cimport stdio
from libc cimport stdlib
from libc cimport string

from libc.errno  cimport *
from libc.float  cimport *
from libc.limits cimport *
from libc.locale cimport *
from libc.signal cimport *
from libc.stddef cimport *
#from libc.stdint cimport * # XXX MSVC
from libc.stdio  cimport *
from libc.stdlib cimport *
from libc.string cimport *

libc.stdio.printf("hello %s\n", b"world")
stdio.printf("hello %s\n", b"world")
printf("hello %s\n", b"world")


cdef extern from "<utility>" namespace "std" nogil:
    cdef cppclass pair[T, U]:
        ctypedef T first_type
        ctypedef U second_type
        T first
        U second
        pair() except +
        pair(pair&) except +
        pair(T&, U&) except +
        bint operator==(pair&, pair&)
        bint operator!=(pair&, pair&)
        bint operator<(pair&, pair&)
        bint operator>(pair&, pair&)
        bint operator<=(pair&, pair&)
        bint operator>=(pair&, pair&)

cdef extern from * namespace "cython_std" nogil:
    """
    #if __cplusplus > 199711L
    #include <type_traits>

    namespace cython_std {
    template <typename T> typename std::remove_reference<T>::type&& move(T& t) noexcept { return std::move(t); }
    template <typename T> typename std::remove_reference<T>::type&& move(T&& t) noexcept { return std::move(t); }
    }

    #endif
    """
    cdef T move[T](T)

Enable usage of std::move() only in the supported versions of MSVC (GH-4191)

cdef extern from "<utility>" namespace "std" nogil:
    cdef cppclass pair[T, U]:
        ctypedef T first_type
        ctypedef U second_type
        T first
        U second
        pair() except +
        pair(pair&) except +
        pair(T&, U&) except +
        bint operator==(pair&, pair&)
        bint operator!=(pair&, pair&)
        bint operator<(pair&, pair&)
        bint operator>(pair&, pair&)
        bint operator<=(pair&, pair&)
        bint operator>=(pair&, pair&)

cdef extern from * namespace "cython_std" nogil:
    """
    #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1600)
    // move should be defined for these versions of MSVC, but __cplusplus isn't set usefully
    #include <type_traits>

    namespace cython_std {
    template <typename T> typename std::remove_reference<T>::type&& move(T& t) noexcept { return std::move(t); }
    template <typename T> typename std::remove_reference<T>::type&& move(T&& t) noexcept { return std::move(t); }
    }

    #endif
    """
    cdef T move[T](T)


#!/usr/bin/python

import json
from Bio import SeqIO

def compute(inputfile, format):
    cdef str line = ''
    cdef int i = 0
    cdef int j = 0
    cdef int line_len = 0
    cdef str token = ''
    cdef int token_cnt = 0
    for seq_record in SeqIO.parse(inputfile, format):
        cnt = {}
        line = str(seq_record.seq)
        line_len = len(line)
        for j in xrange(line_len / 3):
            i = j * 3
            token = line[i:i+3]
            try:
                token_cnt = cnt[token]
            except KeyError:
                cnt[token] = 1
            else:
                cnt[token] = token_cnt + 1
            print json.dumps(
                [{
                    "id": seq_record.id,
                    "ratios": cnt
                }])

Fix bug introduced by pep8 changes


# mode: run

cdef object executable, version_info
cdef long hexversion

ctypedef struct MyStruct:
    int x, y, z

# conversion code for this struct will be generated but not used
# (there used to be a problem getting Cython conversion code generated here)
cdef MyStruct _no_such_name_ = MyStruct(1, 2, 3)

from libc.math cimport NAN

# Danger ahead!
from sys import *

def test_cdefed_objects():
    """
    >>> ex, vi = test_cdefed_objects()
    >>> assert ex is not None
    >>> assert vi is not None
    """
    return executable, version_info

def test_cdefed_cvalues():
    """
    >>> hexver = test_cdefed_cvalues()
    >>> assert hexver is not None
    >>> assert hexver > 0x02020000
    """
    return hexversion

def test_non_cdefed_names():
    """
    >>> mod, pth = test_non_cdefed_names()
    >>> assert mod is not None
    >>> assert pth is not None
    """
    return modules, path

def test_cimported_NAN():
    """
    >>> from math import isnan
    >>> nan = test_cimported_NAN()
    >>> isnan(nan)
    True
    """
    return NAN

Use a different constant (PI instead of NAN) in the "import *" test since NAN does not seem to be defined by MSVC.

# mode: run

cdef object executable, version_info
cdef long hexversion

ctypedef struct MyStruct:
    int x, y, z

# conversion code for this struct will be generated but not used
# (there used to be a problem getting Cython conversion code generated here)
cdef MyStruct _no_such_name_ = MyStruct(1, 2, 3)

from libc.math cimport M_PI

# Danger ahead!
from sys import *

def test_cdefed_objects():
    """
    >>> ex, vi = test_cdefed_objects()
    >>> assert ex is not None
    >>> assert vi is not None
    """
    return executable, version_info

def test_cdefed_cvalues():
    """
    >>> hexver = test_cdefed_cvalues()
    >>> assert hexver is not None
    >>> assert hexver > 0x02020000
    """
    return hexversion

def test_non_cdefed_names():
    """
    >>> mod, pth = test_non_cdefed_names()
    >>> assert mod is not None
    >>> assert pth is not None
    """
    return modules, path

def test_cimported_pi():
    """
    >>> pi = test_cimported_pi()
    >>> 3.14 < pi < 3.15
    True
    """
    return M_PI


from cython.operator cimport dereference as deref
from libcpp.vector cimport vector
from libc.stddef cimport size_t

from microscopes.common.variadic._dataview cimport (
    abstract_dataview,
)
from microscopes.common._rng cimport rng
from microscopes.lda._model_h cimport (
    state as c_state,
    initialize as c_initialize,
)
from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.lda.definition cimport model_definition

cdef class state:
    """The underlying state of a Hierarchial Dirichlet Process LDA

    You should not explicitly construct a state object.
    Instead, use `initialize`.
    """
    cdef shared_ptr[c_state] _thisptr
    # cdef model_definition _defn
    # cdef abstract_dataview _data

Add _defn to state object

from cython.operator cimport dereference as deref
from libcpp.vector cimport vector
from libc.stddef cimport size_t

from microscopes.common.variadic._dataview cimport (
    abstract_dataview,
)
from microscopes.common._rng cimport rng
from microscopes.lda._model_h cimport (
    state as c_state,
    initialize as c_initialize,
)
from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.lda.definition cimport model_definition

cdef class state:
    """The underlying state of a Hierarchial Dirichlet Process LDA

    You should not explicitly construct a state object.
    Instead, use `initialize`.
    """
    cdef shared_ptr[c_state] _thisptr
    cdef model_definition _defn
    # cdef abstract_dataview _data


# -*- coding: utf-8 -*-
"""
Created on Thu Mar 26 17:59:28 2015

@author: sljg2
"""

__all__ = ["friends_of_friends"]
__version__ = "0.1"
__author__ = "Simon Gibbons (sljg2@ast.cam.ac.uk)"
__copyright__ = "Copyright 2015 Simon Gibbons"

cimport numpy as np
from libcpp.list cimport list

cdef extern from "fof.hpp":
    cdef list[list[size_t]] _friends_of_friends "friends_of_friends"(double*, size_t, size_t, double) except +

def friends_of_friends(np.ndarray[double, ndim=2] data, double linking_length):
    """ Computes friends-of-friends clustering of data. Distances are computed
    using a euclidian metric.

        :param data: A numpy array with dimensions (npoints x ndim)

        :param linking_length: The linking length between cluster members

        :rtype: A list of lists of indices in each cluster type
    """

    cdef np.ndarray[double, ndim=2, mode='c'] data_corder

    data_corder = data if data.flags['C_CONTIGUOUS'] else data.copy(order='C')

    return _friends_of_friends(&data_corder[0,0],\
                               data.shape[0],\
                               data.shape[1],\
                               linking_length)

Raise an exception if a NaN is passed to prevent a memory leak

# -*- coding: utf-8 -*-
"""
Created on Thu Mar 26 17:59:28 2015

@author: sljg2
"""

__all__ = ["friends_of_friends"]
__version__ = "0.1.3-dev"
__author__ = "Simon Gibbons (sljg2@ast.cam.ac.uk)"
__copyright__ = "Copyright 2015 Simon Gibbons"

cimport numpy as np
from libcpp.list cimport list

cdef extern from "fof.hpp":
    cdef list[list[size_t]] _friends_of_friends "friends_of_friends"(double*, size_t, size_t, double) except +

def friends_of_friends(np.ndarray[double, ndim=2] data, double linking_length):
    """ Computes friends-of-friends clustering of data. Distances are computed
    using a euclidian metric.

        :param data: A numpy array with dimensions (npoints x ndim)

        :param linking_length: The linking length between cluster members

        :rtype: A list of lists of indices in each cluster type
    """

    cdef np.ndarray[double, ndim=2, mode='c'] data_corder

    data_corder = data if data.flags['C_CONTIGUOUS'] else data.copy(order='C')

    if np.any( np.isnan(data_corder) ):
        raise ValueError("NaN detected in pyfof")

    return _friends_of_friends(&data_corder[0,0],\
                               data.shape[0],\
                               data.shape[1],\
                               linking_length)


from cpython.ref cimport Py_INCREF, Py_DECREF

cdef class PtrValue(Value):

    def __init__(self, obj = None, value_ptr = None):
        if obj is not None:
            Py_INCREF(obj)
            cvalue = createPtrValue(<void*>obj, decref)
            super(PtrValue, self).__init__(ValuePtr.create(cvalue))
        else:
            super(PtrValue, self).__init__(value_ptr)

    def value(self):
        return <object>((<cPtrValue*>self.get_c_value_ptr().get()).value())

cdef void decref(void* obj):
    Py_DECREF(<object>obj)

def valueToPtrValue(value):
    return PtrValue(value_ptr =
                    ValuePtr.create((<Value>value).get_c_value_ptr()))

Fix build after latest API changes from opencog

from cpython.ref cimport Py_INCREF, Py_DECREF

cdef class PtrValue(Value):

    def __init__(self, obj = None, value_ptr = None):
        if obj is not None:
            Py_INCREF(obj)
            cvalue = createPtrValue(<void*>obj, decref)
            super(PtrValue, self).__init__(PtrHolder.create(<shared_ptr[void]&>cvalue))
        else:
            super(PtrValue, self).__init__(value_ptr)

    def value(self):
        return <object>((<cPtrValue*>self.get_c_value_ptr().get()).value())

cdef void decref(void* obj):
    Py_DECREF(<object>obj)

def valueToPtrValue(value):
    return PtrValue(value_ptr = (<Value>value).ptr_holder)


from libc.stdint cimport uint64_t
from libc.stdint cimport uint32_t
from libc.stdint cimport uint16_t
from libc.stdint cimport int32_t

ctypedef float weight_t
ctypedef uint64_t atom_t
ctypedef uint64_t feat_t
ctypedef uint64_t hash_t
ctypedef int32_t class_t
ctypedef uint32_t count_t
ctypedef uint32_t time_t
ctypedef int32_t len_t
ctypedef int32_t idx_t

Fix compile weight_t back to double, not float

from libc.stdint cimport uint64_t
from libc.stdint cimport uint32_t
from libc.stdint cimport uint16_t
from libc.stdint cimport int32_t

ctypedef double weight_t
ctypedef uint64_t atom_t
ctypedef uint64_t feat_t
ctypedef uint64_t hash_t
ctypedef int32_t class_t
ctypedef uint32_t count_t
ctypedef uint32_t time_t
ctypedef int32_t len_t
ctypedef int32_t idx_t


# ticket: 290
# mode: error

cdef extern:
    cdef packed struct MyStruct:
        char a

_ERRORS = u"""
5:9: Cannot declare extern struct as 'packed'
"""

Fix extern packed struct test.

# mode: compile

cdef extern from *:
    cdef packed struct MyStruct:
        char a


EMPTY_PLAYER = ' '
EMPTY_CSA = <Koma>None

cdef class Grid:

    '''Grid class

    Args
    -------------------
    which_player : optional (default = None)
    csa_piece : optional (default = None)
    '''
    def __init__(self, which_player=EMPTY_PLAYER, csa_piece=EMPTY_CSA):
        if which_player in [EMPTY_PLAYER, '+', '-']:
            self.which_player = which_player
        else:
            raise RuntimeError('Invalid which_player:', which_player)

        if csa_piece != EMPTY_CSA:
            self.koma = Koma(csa_piece)
        else:
            self.koma = EMPTY_CSA

    def __str__(self):
        if self.which_player != EMPTY_PLAYER and self.koma != EMPTY_CSA:
            return self.which_player + self.koma.csa
        else:
            return EMPTY_PLAYER + EMPTY_CSA

    cpdef is_empty(self):
        return self.which_player == EMPTY_PLAYER

    cpdef is_of_sente(self):
        return self.which_player == '+'

    cpdef reset(self):
        self.which_player = EMPTY_PLAYER
        self.koma = EMPTY_CSA

Fix return value of Grid.__str__()

EMPTY_PLAYER = ' '
EMPTY_CSA = <Koma>None

cdef class Grid:

    '''Grid class

    Args
    -------------------
    which_player : optional (default = None)
    csa_piece : optional (default = None)
    '''
    def __init__(self, which_player=EMPTY_PLAYER, csa_piece=EMPTY_CSA):
        if which_player in [EMPTY_PLAYER, '+', '-']:
            self.which_player = which_player
        else:
            raise RuntimeError('Invalid which_player:', which_player)

        if csa_piece != EMPTY_CSA:
            self.koma = Koma(csa_piece)
        else:
            self.koma = EMPTY_CSA

    def __str__(self):
        if self.which_player != EMPTY_PLAYER and self.koma != EMPTY_CSA:
            return self.which_player + self.koma.csa
        else:
            return '   '

    cpdef is_empty(self):
        return self.which_player == EMPTY_PLAYER

    cpdef is_of_sente(self):
        return self.which_player == '+'

    cpdef reset(self):
        self.which_player = EMPTY_PLAYER
        self.koma = EMPTY_CSA


cdef extern from "shapes.h" namespace shapes:

    cdef cppclass Shape:
        area()

    cdef cppclass Rectangle(Shape):
        int width
        int height
        __init__(int, int)

    cdef cppclass Square(Shape):
        int side
        __init__(int)

cdef Rectangle *rect = new Rectangle(10, 20)
cdef Square *sqr = new Square(15)

del rect, sqr

Expand and fix cpp tests

__doc__ = u"""
    >>> test_new_del()
    >>> test_rect_area(3, 4)
    12
    >>> test_square_area(15)
    225
"""

cdef extern from "shapes.cpp" namespace shapes:

    cdef cppclass Shape:
        float area()

    cdef cppclass Rectangle(Shape):
        int width
        int height
        __init__(int, int)

    cdef cppclass Square(Shape):
        int side
        __init__(int)

def test_new_del():
    cdef Rectangle *rect = new Rectangle(10, 20)
    cdef Square *sqr = new Square(15)
    del rect, sqr

def test_rect_area(w, h):
    cdef Rectangle *rect = new Rectangle(w, h)
    try:
        return rect.area()
    finally:
        del rect

def test_square_area(w):
    cdef Square *sqr = new Square(w)
    cdef Rectangle *rect = sqr
    try:
        return rect.area(), sqr.area()
    finally:
        del sqr


from cpython.unicode cimport PyUnicode_DecodeUTF8, PyUnicode_AsUTF8String

cdef inline to_unicode(char *s):
	try:
		ns = PyUnicode_DecodeUTF8(s, len(s), NULL)
	except:
		ns = s
	return ns

cdef inline from_unicode(object o):
	if isinstance(o, unicode):
		return PyUnicode_AsUTF8String(o)
	else:
		return o

Fix string constness in to_unicode

from cpython.unicode cimport PyUnicode_DecodeUTF8, PyUnicode_AsUTF8String
from libc.string cimport const_char, strlen

cdef inline to_unicode(const_char *s):
	return PyUnicode_DecodeUTF8(s, strlen(s), "replace")

cdef inline from_unicode(object o):
	if isinstance(o, unicode):
		return PyUnicode_AsUTF8String(o)
	else:
		return o


# it'd be nice to define the type of atomspace but I can't get this working
from opencog.cogserver cimport cAgent, stim_t, string

cdef class MindAgent:
    # TODO add a pointer to the Agent C++ object so that calls to stimulate atoms
    # can be made (among other things)

    def __cinit__(self):
        pass

    property frequency:
        def __get__(self):
            return self.c_obj.frequency()
        def __set__(self,int f):
            self.c_obj.setFrequency(f)

# These methods are not available until we have support for MindAgents running
# continuously in their own threads
#    def start(self, AtomSpace atomspace):
#        pass

#    def end(self):
#        pass

    def run(self, atomspace):
        print "Implement me in your MindAgent subclass: " + atomspace

Add MindAgent::stimulateAtom to the python bindings

# it'd be nice to define the type of atomspace but I can't get this working
from opencog.cogserver cimport cAgent, stim_t, string

from opencog.atomspace cimport cHandle, Atom

cdef class MindAgent:

    def __cinit__(self):
        pass

    property frequency:
        def __get__(self):
            return self.c_obj.frequency()
        def __set__(self,int f):
            self.c_obj.setFrequency(f)

    def stimulate_atom(self, Atom atom, stim_t amount):
        cdef cHandle ch
        ch = cHandle(atom.h.value())

        return self.c_obj.stimulateAtom(ch, amount)

# These methods are not available until we have support for MindAgents running
# continuously in their own threads
#    def start(self, AtomSpace atomspace):
#        pass

#    def end(self):
#        pass

    def run(self, atomspace):
        print "Implement me in your MindAgent subclass: " + atomspace


cimport cython.array
cimport numpy
import numpy

cdef extern from "stdint.h":
    ctypedef unsigned long uint64_t

cdef extern from "inform/time_series.h":
    double inform_active_info(const uint64_t* series, size_t n, uint64_t base, uint64_t k)

def activeinfo(xs, uint64_t k, uint64_t b = 0):
    if not xs:
        raise ValueError("active information not defined on empty containers")

    if b < 2:
        b = max(2,max(xs)+1)

    cdef uint64_t [:] ys = numpy.asarray(xs, dtype=numpy.uint64)
    return inform_active_info(&ys[0], <uint64_t>len(xs), b, k)

Check activeinfo inputs for empty arrays

cimport cython.array
cimport numpy
import numpy

cdef extern from "stdint.h":
    ctypedef unsigned long uint64_t

cdef extern from "inform/time_series.h":
    double inform_active_info(const uint64_t* series, size_t n, uint64_t base, uint64_t k)

def activeinfo(xs, uint64_t k, uint64_t b = 0):
    if len(xs) == 0:
        raise ValueError("active information not defined on empty containers")

    if b < 2:
        b = max(2,max(xs)+1)

    cdef uint64_t [:] ys = numpy.asarray(xs, dtype=numpy.uint64)
    return inform_active_info(&ys[0], <uint64_t>len(xs), b, k)


cdef object[int] buf
cdef class A:
    cdef object[int] buf

def f():
    cdef object[fakeoption=True] buf1
    cdef object[int, -1] buf1b
    cdef object[ndim=-1] buf2
    cdef object[int, 'a'] buf3
    cdef object[int,2,3,4,5,6] buf4
    cdef object[int, 2, 'foo'] buf5
    cdef object[int, 2, well] buf6

_ERRORS = u"""
1:17: Buffer types only allowed as function local variables
3:21: Buffer types only allowed as function local variables
6:31: "fakeoption" is not a buffer option
"""
#TODO:
#7:22: "ndim" must be non-negative
#8:15: "dtype" missing
#9:21: "ndim" must be an integer
#10:15: Too many buffer options
#11:24: Only allowed buffer modes are "full" or "strided" (as a compile-time string)
#12:28: Only allowed buffer modes are "full" or "strided" (as a compile-time string)
#"""

Test for bad buffer types.

cdef object[int] buf
cdef class A:
    cdef object[int] buf

def f():
    cdef object[fakeoption=True] buf1
    cdef object[int, -1] buf1b
    cdef object[ndim=-1] buf2
    cdef object[int, 'a'] buf3
    cdef object[int,2,3,4,5,6] buf4
    cdef object[int, 2, 'foo'] buf5
    cdef object[int, 2, well] buf6
    cdef object[x, 1] buf0

_ERRORS = u"""
1:17: Buffer types only allowed as function local variables
3:21: Buffer types only allowed as function local variables
6:31: "fakeoption" is not a buffer option
"""
#TODO:
#7:22: "ndim" must be non-negative
#8:15: "dtype" missing
#9:21: "ndim" must be an integer
#10:15: Too many buffer options
#11:24: Only allowed buffer modes are "full" or "strided" (as a compile-time string)
#12:28: Only allowed buffer modes are "full" or "strided" (as a compile-time string)
#13:17: Invalid type.
#"""


"A bunch of improper (not normalized) prior distributions"
from libc.math cimport log, fabs

cdef inline double ln_normal(double value, double mean, double stddev):
    cdef double tmp = (value - mean) / stddev
    tmp *= tmp
    return -0.5 * tmp - log(stddev)

cdef inline double ln_laplace(double value, double mean, double stddev):
    return -1.0 * fabs(value - mean) / stddev - log(stddev)

cdef inline double ln_log(double value):
    return -1.0 * log(value)

cdef inline double ln_gamma(double value, double k, double theta):
    return (k - 1.0) * log(value) - value / theta

cdef inline double ln_beta(double value, double p, double q):
    return (p - 1.0) * log(value) + (q - 1.0) * log(1 - value)

Add non-logarithmic normal for mixture model

"A bunch of improper (not normalized) prior distributions"
from libc.math cimport log, fabs, sqrt, exp, M_PI

cdef inline double ln_normal(double value, double mean, double stddev):
    cdef double tmp = (value - mean) / stddev
    tmp *= tmp
    return -0.5 * tmp - log(stddev)

cdef inline double ln_laplace(double value, double mean, double stddev):
    return -1.0 * fabs(value - mean) / stddev - log(stddev)

cdef inline double ln_log(double value):
    return -1.0 * log(value)

cdef inline double ln_gamma(double value, double k, double theta):
    return (k - 1.0) * log(value) - value / theta

cdef inline double ln_beta(double value, double p, double q):
    return (p - 1.0) * log(value) + (q - 1.0) * log(1 - value)

cdef inline double normal(double value, double mean, double stddev):
    cdef double tmp = (value - mean) / stddev
    tmp *= tmp
    return exp(-0.5 * tmp) / sqrt(2. * M_PI) / stddev


cdef extern from *:
    """
    #if defined(WIN32) || defined(MS_WINDOWS)
      #include "windows.h"
      #define myapp_sleep(m)  Sleep(m)
    #else
      #include <unistd.h>
      #define myapp_sleep(m)  ((void) usleep((m) * 1000))
    #endif
    """
    # using "myapp_" prefix in the C code to prevent C naming conflicts
    void msleep "myapp_sleep"(int milliseconds) nogil

msleep(milliseconds=1)

Fix test compile failure in MSVC.

cdef extern from *:
    """
    #if defined(WIN32) || defined(MS_WINDOWS)
      #include "synchapi.h"
      #define myapp_sleep(m)  Sleep(m)
    #else
      #include <unistd.h>
      #define myapp_sleep(m)  ((void) usleep((m) * 1000))
    #endif
    """
    # using "myapp_" prefix in the C code to prevent C naming conflicts
    void msleep "myapp_sleep"(int milliseconds) nogil

msleep(milliseconds=1)


from libcpp cimport bool as cpp_bool
from libcpp.string cimport string
from libcpp.vector cimport vector
from libc.stdint cimport uint32_t
from libc.stdint cimport int64_t
from libc.stdint cimport uint64_t

from status cimport Status
from db cimport DB
from env cimport Env

cdef extern from "utilities/backupable_db.h" namespace "rocksdb":
    ctypedef uint32_t BackupID

    cdef cppclass BackupableDBOptions:
        BackupableDBOptions(const string& backup_dir)

    cdef struct BackupInfo:
        BackupID backup_id
        int64_t timestamp
        uint64_t size

    cdef cppclass BackupEngine:
        Status CreateNewBackup(DB*, cpp_bool) nogil except+
        Status PurgeOldBackups(uint32_t) nogil except+
        Status DeleteBackup(BackupID) nogil except+
        void StopBackup() nogil except+
        void GetBackupInfo(vector[BackupInfo]*) nogil except+
        Status RestoreDBFromBackup(BackupID, string&, string&) nogil except+
        Status RestoreDBFromLatestBackup(string&, string&) nogil except+

    cdef BackupEngine* NewBackupEngine "rocksdb::BackupEngine::NewBackupEngine"(
            Env*,
            BackupableDBOptions&)

Change include path and get rid of the warning.

from libcpp cimport bool as cpp_bool
from libcpp.string cimport string
from libcpp.vector cimport vector
from libc.stdint cimport uint32_t
from libc.stdint cimport int64_t
from libc.stdint cimport uint64_t

from status cimport Status
from db cimport DB
from env cimport Env

cdef extern from "rocksdb/utilities/backupable_db.h" namespace "rocksdb":
    ctypedef uint32_t BackupID

    cdef cppclass BackupableDBOptions:
        BackupableDBOptions(const string& backup_dir)

    cdef struct BackupInfo:
        BackupID backup_id
        int64_t timestamp
        uint64_t size

    cdef cppclass BackupEngine:
        Status CreateNewBackup(DB*, cpp_bool) nogil except+
        Status PurgeOldBackups(uint32_t) nogil except+
        Status DeleteBackup(BackupID) nogil except+
        void StopBackup() nogil except+
        void GetBackupInfo(vector[BackupInfo]*) nogil except+
        Status RestoreDBFromBackup(BackupID, string&, string&) nogil except+
        Status RestoreDBFromLatestBackup(string&, string&) nogil except+

    cdef BackupEngine* NewBackupEngine "rocksdb::BackupEngine::NewBackupEngine"(
            Env*,
            BackupableDBOptions&)


cdef extern from "Python.h":

    ############################################################################
    # 7.2.3
    ############################################################################
    # PyFloatObject
    #
    # This subtype of PyObject represents a Python floating point object.

    # PyTypeObject PyFloat_Type
    #
    # This instance of PyTypeObject represents the Python floating
    # point type. This is the same object as float and
    # types.FloatType.

    bint PyFloat_Check(object p)
    # Return true if its argument is a PyFloatObject or a subtype of
    # PyFloatObject.

    bint PyFloat_CheckExact(object p)
    # Return true if its argument is a PyFloatObject, but not a
    # subtype of PyFloatObject.

    object PyFloat_FromString(object str, char **pend)
    # Return value: New reference.
    # Create a PyFloatObject object based on the string value in str,
    # or NULL on failure. The pend argument is ignored. It remains
    # only for backward compatibility.

    object PyFloat_FromDouble(double v)
    # Return value: New reference.
    # Create a PyFloatObject object from v, or NULL on failure.

    double PyFloat_AsDouble(object pyfloat) except? -1
    # Return a C double representation of the contents of pyfloat.

    double PyFloat_AS_DOUBLE(object pyfloat)
    # Return a C double representation of the contents of pyfloat, but
    # without error checking.

Change the declaration of PyFloat_FromString to the Py3 signature and provide a fallback for Py2.

cdef extern from "Python.h":
    """
    #if PY_MAJOR_VERSION >= 3
      #define __Pyx_PyFloat_FromString(obj)  PyFloat_FromString(obj)
    #else
      #define __Pyx_PyFloat_FromString(obj)  PyFloat_FromString(obj, NULL)
    #endif
    """

    ############################################################################
    # 7.2.3
    ############################################################################
    # PyFloatObject
    #
    # This subtype of PyObject represents a Python floating point object.

    # PyTypeObject PyFloat_Type
    #
    # This instance of PyTypeObject represents the Python floating
    # point type. This is the same object as float and
    # types.FloatType.

    bint PyFloat_Check(object p)
    # Return true if its argument is a PyFloatObject or a subtype of
    # PyFloatObject.

    bint PyFloat_CheckExact(object p)
    # Return true if its argument is a PyFloatObject, but not a
    # subtype of PyFloatObject.

    object PyFloat_FromString "__Pyx_PyFloat_FromString" (object str)
    # Return value: New reference.
    # Create a PyFloatObject object based on the string value in str,
    # or NULL on failure. The pend argument is ignored. It remains
    # only for backward compatibility.

    object PyFloat_FromDouble(double v)
    # Return value: New reference.
    # Create a PyFloatObject object from v, or NULL on failure.

    double PyFloat_AsDouble(object pyfloat) except? -1
    # Return a C double representation of the contents of pyfloat.

    double PyFloat_AS_DOUBLE(object pyfloat)
    # Return a C double representation of the contents of pyfloat, but
    # without error checking.


#/*##########################################################################
#
# The fisx library for X-Ray Fluorescence
#
# Copyright (c) 2014-2016 European Synchrotron Radiation Facility
#
# This file is part of the fisx X-ray developed by V.A. Sole
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
cimport cython

#from libcpp.string cimport string as std_string

from Version cimport fisxVersion as _fisxVersion

def fisxVersion():
    return _fisxVersion()

Return as string and not as bytes.

#/*##########################################################################
#
# The fisx library for X-Ray Fluorescence
#
# Copyright (c) 2014-2016 European Synchrotron Radiation Facility
#
# This file is part of the fisx X-ray developed by V.A. Sole
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
cimport cython

#from libcpp.string cimport string as std_string

from Version cimport fisxVersion as _fisxVersion

def fisxVersion():
    return toString(_fisxVersion())


# tag: cpp
# mode: compile

cdef extern from "point.h" namespace "geometry":

    cdef struct Point:
        double x
        double y
        int color

cdef Point p = Point(0.0, 0.0, 0)
the_point = p

Add a test for namespaced struct with memoryview

# tag: cpp
# mode: compile

from cython.view import array

cdef extern from "point.h" namespace "geometry":

    cdef struct Point:
        double x
        double y
        int color

cdef Point p = Point(0.0, 0.0, 0)
the_point = p

cdef Point[::1] ps = array((10,), itemsize=sizeof(Point), format='ddi')


# cython: warn.maybe_uninitialized=True
# mode: error
# tag: werror

def foo(x):
    a = 1
    del a, b
    b = 2
    return a, b

_ERRORS = """
7:9: Deletion of non-Python, non-C++ object
7:12: local variable 'b' referenced before assignment
7:12: Deletion of non-Python, non-C++ object
9:12: local variable 'a' referenced before assignment
"""

Fix w_uninitilized_del testcase, now inferred as python object

# cython: warn.maybe_uninitialized=True
# mode: error
# tag: werror

def foo(x):
    a = 1
    del a, b
    b = 2
    return a, b

_ERRORS = """
7:12: local variable 'b' referenced before assignment
9:12: local variable 'a' referenced before assignment
"""


from libc.stdio cimport FILE, fopen
from libc.stdlib cimport malloc, free
from cpython.exc cimport PyErr_SetFromErrnoWithFilenameObject

def open_file():
    cdef FILE* p
    p = fopen("spam.txt", "r")
    if p is NULL:
        PyErr_SetFromErrnoWithFilenameObject(OSError, "spam.txt")
    ...

def allocating_memory(number=10):
    cdef double *my_array = <double *> malloc(number * sizeof(double))
    if not my_array:
        raise MemoryError()
    ...
    free(my_array)

Add comment in doc example to explain that two ways to compare pointers are equivalent

from libc.stdio cimport FILE, fopen
from libc.stdlib cimport malloc, free
from cpython.exc cimport PyErr_SetFromErrnoWithFilenameObject

def open_file():
    cdef FILE* p
    p = fopen("spam.txt", "r")
    if p is NULL:
        PyErr_SetFromErrnoWithFilenameObject(OSError, "spam.txt")
    ...

def allocating_memory(number=10):
    cdef double *my_array = <double *> malloc(number * sizeof(double))
    if not my_array:  # same as 'is NULL' above
        raise MemoryError()
    ...
    free(my_array)


from libcpp.vector cimport vector
from libcpp.map cimport map
from libc.stddef cimport size_t

from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.common._random_fwd_h cimport rng_t

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda":
    cdef cppclass model_definition:
        model_definition(size_t, size_t) except +

    cdef cppclass state:
        double perplexity()
        size_t nentities()
        size_t ntopics()
        size_t nwords()
        size_t nterms(size_t) except +

        vector[vector[size_t]] assignments()
        vector[vector[size_t]] dish_assignments()
        vector[vector[size_t]] table_assignments()
        vector[vector[float]] document_distribution()
        vector[map[size_t, float]] word_distribution()

        float score_assignment()
        float score_data(rng_t &)

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda::state":
    shared_ptr[state] initialize(const model_definition &defn,
                                 float alpa, float beta, float gamma,
                                 size_t initial_dishes,
                                 vector[vector[size_t]] &docs,
                                 rng_t & rng) except +

Fix spelling error in variable name

from libcpp.vector cimport vector
from libcpp.map cimport map
from libc.stddef cimport size_t

from microscopes._shared_ptr_h cimport shared_ptr
from microscopes.common._random_fwd_h cimport rng_t

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda":
    cdef cppclass model_definition:
        model_definition(size_t, size_t) except +

    cdef cppclass state:
        double perplexity()
        size_t nentities()
        size_t ntopics()
        size_t nwords()
        size_t nterms(size_t) except +

        vector[vector[size_t]] assignments()
        vector[vector[size_t]] dish_assignments()
        vector[vector[size_t]] table_assignments()
        vector[vector[float]] document_distribution()
        vector[map[size_t, float]] word_distribution()

        float score_assignment()
        float score_data(rng_t &)

cdef extern from "microscopes/lda/model.hpp" namespace "microscopes::lda::state":
    shared_ptr[state] initialize(const model_definition &defn,
                                 float alpha, float beta, float gamma,
                                 size_t initial_dishes,
                                 vector[vector[size_t]] &docs,
                                 rng_t & rng) except +


__doc__ = u"""
    >>> test()
    neg False
    pos True
    neg
    pos
    neg
    pos
"""

def test():
    cdef long neg = -1
    cdef unsigned long pos = -2 # will be a large positive number

    print "neg", neg > 0
    print "pos", pos > -

    D = { neg: 'neg', pos: 'pos' }

    print D[<object>neg]
    print D[<object>pos]

    print D[neg]
    print D[pos]

Fix test case syntax error

__doc__ = u"""
    >>> test()
    neg False
    pos True
    neg
    pos
    neg
    pos
"""

def test():
    cdef long neg = -1
    cdef unsigned long pos = -2 # will be a large positive number

    print "neg", neg > 0
    print "pos", pos > 0

    D = { neg: 'neg', pos: 'pos' }

    print D[<object>neg]
    print D[<object>pos]

    print D[neg]
    print D[pos]