text:
"""
MOS model struct.
"""
mutable struct Model
    interface::Interface
    data::Dict{String, <:Any}
end

function __add_variable_states__(m::Model, var_states::Array{<:Any, 1})
    url = join([m.interface.url, "variable-state/bulk_create/"])
    json_data = JSON.json(var_states)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.post(url, add_auth!(h, m.interface.token), body=json_data)
    if r.status != 201
        error("unable to add variable states")
    end
end

function __add_function_states__(m::Model, func_states::Array{<:Any, 1})
    url = join([m.interface.url, "function-state/bulk_create/"])
    json_data = JSON.json(func_states)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.post(url, add_auth!(h, m.interface.token), body=json_data)
    if r.status != 201
        error("unable to add function states")
    end
end

function __add_constraint_states__(m::Model, constr_states::Array{<:Any, 1})
    url = join([m.interface.url, "constraint-state/bulk_create/"])
    json_data = JSON.json(constr_states)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.post(url, add_auth!(h, m.interface.token),  body=json_data)
    if r.status != 201
        error("unable to add constraint states")
    end
end

function __add_problem_state__(m::Model, p_state::Dict)
    url = join([m.interface.url, "problem-state/"])
    json_data = JSON.json(p_state)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.post(url, add_auth!(h, m.interface.token),  body=json_data)
    if r.status != 201
        error("unable to add problem state")
    end
end

function __add_solver_state__(m::Model, s_state::Dict)
    url = join([m.interface.url, "solver-state/"])
    json_data = JSON.json(s_state)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.post(url, add_auth!(h, m.interface.token), body=json_data)
    if r.status != 201
        error("unable to add solver state")
    end
end

function __delete_input_files__(m::Model)
    for f in __get_interface_files__(m, "input")
        filename = string(f["name"], f["extension"])
        if isfile(filename)
            println("Deleting file $filename")
            rm(filename)
        end
    end
end

function __delete_input_object_files__(m::Model)
    for f in __get_interface_objects__(m, "input")
        filename = string(f["name"], ".json")
        if isfile(filename)
            println("Deleting file $filename")
            rm(filename)
        end
    end
end

function __delete_output_files__(m::Model)
    for f in __get_interface_files__(m, "output")
        filename = string(f["name"], f["extension"])
        if isfile(filename)
            println("Deleting file $filename")
            rm(filename)
        end
    end
end

function __download_input_files__(m::Model)
    for f in __get_interface_files__(m, "input")
        name = f["name"]
        ext = f["extension"]
        filename = "$name$ext"
        println("Downloading file $filename")
        url = join([f["url"], "url/"])
        r = HTTP.get(correct_url(url, m.interface.url),
                     add_auth!(Dict(), m.interface.token))
        if r.status != 200
            error("unable to download files")
        end
        file_url = correct_url(JSON.parse(String(r.body)), m.interface.url)
        HTTP.open("GET", file_url, add_auth!(Dict(), m.interface.token)) do stream
            Base.open(filename, "w") do fh
                while(!eof(stream))
                    write(fh, readavailable(stream))
                end
            end
        end
    end
end

function __download_input_object_files__(m::Model)
    for f in __get_interface_objects__(m, "input")
        name = f["name"]
        ext = ".json"
        filename = "$name$ext"
        println("Downloading file $filename")
        url = correct_url(f["data"], m.interface.url)
        r = HTTP.get(url, add_auth!(Dict(), m.interface.token))
        if r.status != 200
            error("unable to download files")
        end
        Base.open(filename, "w") do fh
            write(fh, String(r.body))
        end
    end
end

function __get_solver__(m::Model)
    return m.data["solver"]
end

function __get_problem__(m::Model)
    return m.data["problem"]
end

function __get_variables__(m::Model)
    return m.data["variables"]
end

function __get_functions__(m::Model)
    return m.data["functions"]
end

function __get_constraints__(m::Model)
    return m.data["constraints"]
end

function __get_helper_objects__(m::Model)
    return m.data["helper_objects"]
end

function __get_interface_objects__(m::Model, type::Any=nothing)
    return [o for o in m.data["interface_objects"] if type === nothing || o["type"] == type]
end

function __get_interface_files__(m::Model, type::Any=nothing)
    return [f for f in m.data["interface_files"] if type === nothing || f["type"] == type]
end

function __set_status__(m::Model, status::AbstractString)
    url = join([m.data["url"], "set_status/"])
    h = Dict("Content-Type" => "application/json")
    r = HTTP.put(correct_url(url, m.interface.url),
                 add_auth!(h, m.interface.token),
                 body=JSON.json(status))
    if r.status != 200
        error("unable to set model status")
    end
    m.data["status"] = status
end

function __set_var_type_and_shape__(m::Model,
                                    v::Dict{<:AbstractString, <:Any},
                                    type::AbstractString,
                                    shape::Union{Array{Int64, 1}, Nothing})
    @assert(type in ["scalar", "array", "hashmap"])
    v["type"] = type
    v["shape"] = shape
    json_v = JSON.json(v)
    url = correct_url(v["url"], m.interface.url)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.put(url, add_auth!(h, m.interface.token),  body=json_v)
    if r.status != 200
        error("unable to set variable type and size")
    end
end

function __set_function_type_and_shape__(m::Model,
                                         f::Dict{<:AbstractString, <:Any},
                                         type::AbstractString,
                                         shape::Union{Array{Int64, 1}, Nothing})
    @assert(type in ["scalar", "array", "hashmap"])
    f["type"] = type
    f["shape"] = shape
    json_f = JSON.json(f)
    url = correct_url(f["url"], m.interface.url)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.put(url, add_auth!(h, m.interface.token),  body=json_f)
    if r.status != 200
        error("unable to set function type and size")
    end
end

function __set_constraint_type_and_shape__(m::Model,
                                           c::Dict{<:AbstractString, <:Any},
                                           type::AbstractString,
                                           shape::Union{Array{Int64, 1}, Nothing})
    @assert(type in ["scalar", "array", "hashmap"])
    c["type"] = type
    c["shape"] = shape
    json_c = JSON.json(c)
    url = correct_url(c["url"], m.interface.url)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.put(url, add_auth!(h, m.interface.token),  body=json_c)
    if r.status != 200
        error("unable to set constraint type and size")
    end
end

function __set_interface_file__(m::Model, f::Dict{<:AbstractString, <:Any}, filepath::AbstractString)
    open(filepath, "r") do data_file
        f["extension"] = splitext(filepath)[end]
        temp = merge(Dict("data_file" => data_file), f)
        temp["data"] = ""
        data = HTTP.Form(temp)
        h = Dict("Content-Type" => "multipart/form-data; boundary=$(data.boundary)")
        url = correct_url(f["url"], m.interface.url)
        r = HTTP.put(url, add_auth!(h, m.interface.token), body=data)
        if r.status != 200
            error("unable to set interface file")
        end
    end
end

function __set_interface_object__(m::Model, o::Dict{<:AbstractString, <:Any}, data::Any)
    json_data = JSON.json(Dict("data" => data))
    url = correct_url(o["url"], m.interface.url)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.put(url, add_auth!(h, m.interface.token),  body=json_data)
    if r.status != 200
        error("unable to set io object")
    end
end

function __set_helper_object__(m::Model, o::Dict{<:AbstractString, <:Any}, data::Any)
    json_data = JSON.json(Dict("data" => data))
    url = correct_url(o["url"], m.interface.url)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.put(url, add_auth!(h, m.interface.token),  body=json_data)
    if r.status != 200
        error("unable to set helper object")
    end
end

function __set_execution_log__(m::Model, log::AbstractString)
    url = join([m.data["url"], "set_execution_log/"])
    url = correct_url(url, m.interface.url)
    h = Dict("Content-Type" => "application/json")
    r = HTTP.put(url, add_auth!(h, m.interface.token), body=JSON.json(log))
    if r.status != 200
        error("unable to set execution log")
    end
    m.data["execution_log"] = log
end

function __write__(m::Model, f::IO)
    url = join([m.data["url"], "write/"])
    h = Dict()
    r = HTTP.get(correct_url(url, m.interface.url),
                 add_auth!(h, m.interface.token))
    if r.status != 200
        error("unable to write model")
    end
    recipe = JSON.parse(String(r.body))
    write(f, recipe)
end

"""
    delete_results(m::Model)

Delete model results.
"""
function delete_results(m::Model)
    url = join([m.data["url"], "delete_results/"])(
    h = Dict())
    r = HTTP.post(correct_url(url, m.interface.url),
                  add_auth!(h, m.interface.token))
    if r.status != 200
        error("error trying to delete results")
    end
end

"""
    set_interface_file(m::Model, name::AbstractString, filepath::AbstractString)

Set interface file having a given name with data from a local file.
"""
function set_interface_file(m::Model, name::AbstractString, filepath::AbstractString)
    for f in __get_interface_files__(m)
        if f["name"] == name
            __set_interface_file__(m, f, filepath)
            return
        end
    end
    error("invalid file name")
end

"""
    set_interface_object(m::Model, name::AbstractString, data::Any)

Set interface object having a given name with JSON-serializable data.
"""
function set_interface_object(m::Model, name::AbstractString, data::Any)
    for f in __get_interface_objects__(m)
        if f["name"] == name
            __set_interface_object__(m, f, data)
            return
        end
    end
    error("invalid file name")
end

"""
    run(m::Model, blocking::Bool=true, poll_time::Number=1)

Run optimization model.
"""
function run(m::Model, blocking::Bool=true, poll_time::Number=1)
    url = join([m.data["url"], "run/"])
    h = Dict()
    r = HTTP.post(correct_url(url, m.interface.url),
                  add_auth!(h, m.interface.token))
    if r.status != 200
        error("error trying to solve model")
    end
    status = get_status(m)
    while status in ("queued", "running") && blocking
        sleep(poll_time)
        status = get_status(m)
    end
    if blocking
        reload(m)
    end
end

"""
    reload(m::Model)

Reload optimization model by pulling its latest data from MOS.
"""
function reload(m::Model)
    h = Dict()
    r = HTTP.get(correct_url(m.data["url"], m.interface.url),
                 add_auth!(h, m.interface.token))
    if r.status != 200
        error("unable to reload model")
    end
    m.data = JSON.parse(String(r.body))
end

"""
    show_components(m::Model)

Display model components.
"""
function show_components(m::Model)

    title = string("Model: ",m.data["name"])

    println("")
    println(title)
    println("----------------")
    println("")
    println("Input Files:")
    for f in __get_interface_files__(m, "input")
        println(string(f["name"],f["extension"]))
    end
    println("Input Objects:")
    for o in __get_interface_objects__(m, "input")
        println(string(o["name"]))
    end
    println("Helper Objects:")
    for o in __get_helper_objects__(m)
        println(string(o["name"]))
    end
    println("Variables:")
    for v in __get_variables__(m)
        println(string(v["name"]))
    end
    println("Functions:")
    for f in __get_functions__(m)
        println(string(f["name"]))
    end
    println("Constraints:")
    for c in __get_constraints__(m)
        println(string(c["name"]))
    end
    println("Solver:")
    println(string(__get_solver__(m)["name"]))
    println("Problem:")
    println(string(__get_problem__(m)["name"]))
    println("Output Files")
    for f in __get_interface_files__(m, "output")
        println(string(f["name"],f["extension"]))
    end
    println("Output Objects:")
    for o in __get_interface_objects__(m, "output")
        if o["type"] == "output"
            println(string(o["name"]))
        end
    end
end

"""
    show_recipe(m::Model)

Show model execution recipe.
"""
function show_recipe(m::Model)
    recipe = IOBuffer()
    __write__(m, recipe)
    println(String(take!(recipe)))
end

"""
    get_id(m::Model)

Get model id.
"""
function get_id(m::Model)
    return m.data["id"]
end

"""
    get_owner_id(m::Model)

Get model owner id.
"""
function get_owner_id(m::Model)
    return m.data["owner"]["id"]
end

"""
    get_system(m::Model)

Get model optimization modeling system.
"""
function get_system(m::Model)
    return m.data["system"]
end

"""
    get_name(m::Model)

Get model name.
"""
function get_name(m::Model)
    return m.data["name"]
end

"""
    get_description(m::Model)

Get model description.
"""
function get_description(m::Model)
    return m.data["description"]
end

"""
    get_status(m::Model)

Get model status.
"""
function get_status(m::Model)

    url = join([m.data["url"], "get_status/"])
    h = Dict()
    r = HTTP.get(correct_url(url, m.interface.url),
                 add_auth!(h, m.interface.token))
    if r.status != 200
        error("unable to get model status")
    end

    status = JSON.parse(String(r.body))
    m.data["status"] = status

    return status
end

max_stars_repo_name: Fuinn/mos-interface-jl

max_issues_repo_name: Fuinn/mos-interface-jl

max_forks_repo_name: Fuinn/mos-interface-jl


text:
using PathTracing
using Test

@testset "PathTracing.jl" begin
    # Write your tests here.
end

ext: jl


text:
ioslength(io::IOBuffer) = (io.seekable ? io.size : nb_available(io))

let io = IOBuffer()
@test eof(io)
@test_throws EOFError read(io,Uint8)
@test write(io,"abc") == 3
@test ioslength(io) == 3
@test position(io) == 3
@test eof(io)
seek(io, 0)
@test read(io,Uint8) == 'a'
a = Array(Uint8, 2)
@test read!(io, a) == a
@test a == ['b','c']
@test bytestring(io) == "abc"
seek(io, 1)
truncate(io, 2)
@test position(io) == 1
@test !eof(io)
seekend(io)
@test position(io) == 2
truncate(io, 0)
@test position(io) == 0
truncate(io, 10)
@test position(io) == 0
@test all(io.data .== 0)
@test write(io,Int16[1,2,3,4,5,6]) == 12
seek(io, 2)
truncate(io, 10)
@test ioslength(io) == 10
io.readable = false
@test_throws ErrorException read!(io,Uint8[0])
truncate(io, 0)
@test write(io,"boston\ncambridge\n") > 0
@test takebuf_string(io) == "boston\ncambridge\n"
@test takebuf_string(io) == ""
close(io)
@test_throws ErrorException write(io,Uint8[0])
@test_throws ErrorException seek(io,0)
@test eof(io)
end

let io = IOBuffer("hamster\nguinea pig\nturtle")
@test position(io) == 0
@test readline(io) == "hamster\n"
@test readall(io) == "guinea pig\nturtle"
@test_throws EOFError read(io,Uint8)
seek(io,0)
@test read(io,Uint8) == 'h'
@test_throws ErrorException truncate(io,0)
@test_throws ErrorException write(io,uint8(0))
@test_throws ErrorException write(io,Uint8[0])
@test takebuf_string(io) == "hamster\nguinea pig\nturtle"
@test takebuf_string(io) == "hamster\nguinea pig\nturtle" #should be unchanged
close(io)
end

let io = PipeBuffer()
@test_throws EOFError read(io,Uint8)
@test write(io,"pancakes\nwaffles\nblueberries\n") > 0
@test position(io) == 0
@test readline(io) == "pancakes\n"
Base.compact(io)
@test readline(io) == "waffles\n"
@test write(io,"whipped cream\n") > 0
@test readline(io) == "blueberries\n"
@test_throws ErrorException seek(io,0)
@test_throws ErrorException truncate(io,0)
@test readline(io) == "whipped cream\n"
Base.compact(io)
@test position(io) == 0
@test ioslength(io) == 0
Base.ensureroom(io,50)
@test position(io) == 0
@test ioslength(io) == 0
@test length(io.data) == 50
Base.ensureroom(io,10)
@test ioslength(io) == 0
@test length(io.data) == 50
io.maxsize = 75
Base.ensureroom(io,100)
@test ioslength(io) == 0
@test length(io.data) == 75
seekend(io)
@test ioslength(io) == 0
@test position(io) == 0
write(io,zeros(Uint8,200))
@test ioslength(io) == 75
@test length(io.data) == 75
write(io,1)
@test ioslength(io) == 75
@test length(io.data) == 75
write(io,[1,2,3])
@test ioslength(io) == 75
@test length(io.data) == 75
skip(io,1)
@test write(io,uint8(104)) == 1
skip(io,3)
@test write(io,"apples".data) == 3
skip(io,71)
@test write(io,'y') == 1
@test readall(io) == "happy"
@test eof(io)
write(io,zeros(Uint8,73))
write(io,'a')
write(io,'b')
write(io,'c')
write(io,'d')
write(io,'e')
@test ioslength(io) == 75
@test length(io.data) == 75
@test position(io) == 0
skip(io,72)
@test takebuf_string(io) == "\0ab"
@test takebuf_string(io) == ""

# issues 4021
print(io, true)
close(io)
end

# issue 5453
let io=IOBuffer("abcdef")
a = Array(Uint8,1024)
@test_throws EOFError read!(io,a)
@test eof(io)
end

@test isempty(readlines(IOBuffer()))

max_stars_repo_name: jakebolewski/julia

max_issues_repo_name: jakebolewski/julia

max_forks_repo_name: jakebolewski/julia


text:
@testset "Matrix multiplication" begin
    @testset "Matrix-vector" begin
        m = @SMatrix [1 2; 3 4]
        v = @SVector [1, 2]
        @test m*v === @SVector [5, 11]
        # More complicated eltype inference
        v = @SVector [CartesianIndex((1,3)), CartesianIndex((3,1))]
        x = @inferred(m*v)
        @test isa(x, SVector{2,CartesianIndex{2}})
        @test x == @SVector [CartesianIndex((7,5)), CartesianIndex((15,13))]

        m = @MMatrix [1 2; 3 4]
        v = @MVector [1, 2]
        @test (m*v)::MVector == @MVector [5, 11]

        m = @SArray [1 2; 3 4]
        v = @SArray [1, 2]
        @test m*v === @SArray [5, 11]

        m = @MArray [1 2; 3 4]
        v = @MArray [1, 2]
        @test (m*v)::MArray == @MArray [5, 11]
    end

    @testset "Vector-matrix" begin
        m = @SMatrix [1 2 3 4]
        v = @SVector [1, 2]
        @test v*m === @SMatrix [1 2 3 4; 2 4 6 8]
    end

    @testset "Matrix-matrix" begin
        m = @SMatrix [1 2; 3 4]
        n = @SMatrix [2 3; 4 5]
        nc = @SMatrix [CartesianIndex((2,2)) CartesianIndex((3,3));
                       CartesianIndex((4,4)) CartesianIndex((5,5))]
        @test m*n  === @SMatrix [10 13; 22 29]
        @test m*nc === @SMatrix [CartesianIndex((10,10)) CartesianIndex((13,13));
                                 CartesianIndex((22,22)) CartesianIndex((29,29))]

        @test m'*n === @SMatrix [14 18; 20 26]
        @test m*n' === @SMatrix [8 14; 18 32]
        @test m'*n' === @SMatrix [11 19; 16 28]
        @test m.'*n === @SMatrix [14 18; 20 26]
        @test m*n.' === @SMatrix [8 14; 18 32]
        @test m.'*n.' === @SMatrix [11 19; 16 28]

        m = @MMatrix [1 2; 3 4]
        n = @MMatrix [2 3; 4 5]
        @test (m*n)::MMatrix == @MMatrix [10 13; 22 29]

        m = @SArray [1 2; 3 4]
        n = @SArray [2 3; 4 5]
        @test m*n === @SArray [10 13; 22 29]

        m = @MArray [1 2; 3 4]
        n = @MArray [2 3; 4 5]
        @test (m*n)::MArray == @MArray [10 13; 22 29]

        # Alternative methods used between 8 < n <= 14 and n > 14
        m_array = rand(1:10, 10, 10)
        n_array = rand(1:10, 10, 10)
        a_array = m_array*n_array

        m = SMatrix{10,10}(m_array)
        n = SMatrix{10,10}(n_array)
        @test m*n === SMatrix{10,10}(a_array)

        m_array = rand(1:10, 16, 16)
        n_array = rand(1:10, 16, 16)
        a_array = m_array*n_array

        m = SMatrix{16,16}(m_array)
        n = SMatrix{16,16}(n_array)
        @test m*n === SMatrix{16,16}(a_array)

        # Mutating types follow different behaviour
        m_array = rand(1:10, 10, 10)
        n_array = rand(1:10, 10, 10)
        a_array = m_array*n_array

        m = MMatrix{10,10}(m_array)
        n = MMatrix{10,10}(n_array)
        @test (m*n)::MMatrix == a_array

        m_array = rand(1:10, 16, 16)
        n_array = rand(1:10, 16, 16)
        a_array = m_array*n_array

        m = MMatrix{16,16}(m_array)
        n = MMatrix{16,16}(n_array)
        @test (m*n)::MMatrix == a_array

        # Mutating BLAS types follow yet different behaviour
        m_array = randn(4, 4)
        n_array = randn(4, 4)
        a_array = m_array*n_array

        m = MMatrix{4,4}(m_array)
        n = MMatrix{4,4}(n_array)
        @test m*n::MMatrix ≈ a_array

        m_array = randn(10, 10)
        n_array = randn(10, 10)
        a_array = m_array*n_array

        m = MMatrix{10,10}(m_array)
        n = MMatrix{10,10}(n_array)
        @test m*n::MMatrix ≈ a_array

        m_array = randn(16, 16)
        n_array = randn(16, 16)
        a_array = m_array*n_array

        m = MMatrix{16,16}(m_array)
        n = MMatrix{16,16}(n_array)
        @test m*n::MMatrix ≈ a_array
    end

    @testset "A_mul_B!" begin
        m = @SMatrix [1 2; 3 4]
        n = @SMatrix [2 3; 4 5]

        a = MMatrix{2,2,Int,4}()
        A_mul_B!(a, m, n)
        @test a::MMatrix{2,2,Int,4} == @MMatrix [10 13; 22 29]

        Ac_mul_B!(a, m, n)
        @test a::MMatrix{2,2,Int,4} == @MMatrix [14 18; 20 26]
        A_mul_Bc!(a, m, n)
        @test a::MMatrix{2,2,Int,4} == @MMatrix [8 14; 18 32]
        Ac_mul_Bc!(a, m, n)
        @test a::MMatrix{2,2,Int,4} == @MMatrix [11 19; 16 28]
        At_mul_B!(a, m, n)
        @test a::MMatrix{2,2,Int,4} == @MMatrix [14 18; 20 26]
        A_mul_Bt!(a, m, n)
        @test a::MMatrix{2,2,Int,4} == @MMatrix [8 14; 18 32]
        At_mul_Bt!(a, m, n)
        @test a::MMatrix{2,2,Int,4} == @MMatrix [11 19; 16 28]

        a2 = MArray{(2,2),Int,2,4}()
        A_mul_B!(a2, m, n)
        @test a2::MArray{(2,2),Int,2,4} == @MArray [10 13; 22 29]

        # Alternative builtin method used for n > 8
        m_array = rand(1:10, 10, 10)
        n_array = rand(1:10, 10, 10)
        a_array = m_array*n_array

        m = MMatrix{10,10}(m_array)
        n = MMatrix{10,10}(n_array)
        a = MMatrix{10,10,Int}()
        A_mul_B!(a, m, n)
        @test a == a_array

        # BLAS used for n > 14
        m_array = randn(4, 4)
        n_array = randn(4, 4)
        a_array = m_array*n_array

        m = MMatrix{4,4}(m_array)
        n = MMatrix{4,4}(n_array)
        a = MMatrix{4,4,Float64}()
        A_mul_B!(a, m, n)
        @test a ≈ a_array

        m_array = randn(10, 10)
        n_array = randn(10, 10)
        a_array = m_array*n_array

        m = MMatrix{10,10}(m_array)
        n = MMatrix{10,10}(n_array)
        a = MMatrix{10,10,Float64}()
        A_mul_B!(a, m, n)
        @test a ≈ a_array

        m_array = rand(1:10, 16, 16)
        n_array = rand(1:10, 16, 16)
        a_array = m_array*n_array

        m = MMatrix{16,16}(m_array)
        n = MMatrix{16,16}(n_array)
        a = MMatrix{16,16,Int}()
        A_mul_B!(a, m, n)
        @test a ≈ a_array
    end
end


text:
module TestUtils

using Test, DataFrames

@testset "make_unique" begin
    @test DataFrames.make_unique([:x, :x, :x_1, :x2], makeunique=true) == [:x, :x_2, :x_1, :x2]
    @test_throws ArgumentError DataFrames.make_unique([:x, :x, :x_1, :x2], makeunique=false)
    @test DataFrames.make_unique([:x, :x_1, :x2], makeunique=false) == [:x, :x_1, :x2]
end

@testset "repeat count" begin
    df = DataFrame(a = 1:2, b = 3:4)
    ref = DataFrame(a = repeat(1:2, 2),
                    b = repeat(3:4, 2))
    @test repeat(df, 2) == ref
    @test repeat(view(df, 1:2, :), 2) == ref

    @test size(repeat(df, 0)) == (0, 2)
    @test size(repeat(df, false)) == (0, 2)
    @test_throws ArgumentError repeat(df, -1)
end

@testset "repeat inner_outer" begin
    df = DataFrame(a = 1:2, b = 3:4)
    ref = DataFrame(a = repeat(1:2, inner = 2, outer = 3),
                    b = repeat(3:4, inner = 2, outer = 3))
    @test repeat(df, inner = 2, outer = 3) == ref
    @test repeat(view(df, 1:2, :), inner = 2, outer = 3) == ref

    @test size(repeat(df, inner = 2, outer = 0)) == (0, 2)
    @test size(repeat(df, inner = 0, outer = 3)) == (0, 2)
    @test size(repeat(df, inner = 2, outer = false)) == (0, 2)
    @test size(repeat(df, inner = false, outer = 3)) == (0, 2)
    @test_throws ArgumentError repeat(df, inner = 2, outer = -1)
    @test_throws ArgumentError repeat(df, inner = -1, outer = 3)
end

@testset "repeat! count" begin
    df = DataFrame(a = 1:2, b = 3:4)
    ref = DataFrame(a = repeat(1:2, 2),
                    b = repeat(3:4, 2))
    a = df.a
    b = df.b
    repeat!(df, 2)
    @test df == ref
    @test a == 1:2
    @test b == 3:4

    for v in (0, false)
        df = DataFrame(a = 1:2, b = 3:4)
        repeat!(df, v)
        @test size(df) == (0, 2)
    end

    df = DataFrame(a = 1:2, b = 3:4)
    @test_throws ArgumentError repeat(df, -1)
    @test df == DataFrame(a = 1:2, b = 3:4)

    @test_throws MethodError repeat!(view(df, 1:2, :), 2)
end

@testset "repeat! inner_outer" begin
    df = DataFrame(a = 1:2, b = 3:4)
    ref = DataFrame(a = repeat(1:2, inner = 2, outer = 3),
                    b = repeat(3:4, inner = 2, outer = 3))
    a = df.a
    b = df.b
    repeat!(df, inner = 2, outer = 3)
    @test df == ref
    @test a == 1:2
    @test b == 3:4

    for v in (0, false)
        df = DataFrame(a = 1:2, b = 3:4)
        repeat!(df, inner = 2, outer = v)
        @test size(df) == (0, 2)

        df = DataFrame(a = 1:2, b = 3:4)
        repeat!(df, inner = v, outer = 3)
        @test size(df) == (0, 2)
    end

    df = DataFrame(a = 1:2, b = 3:4)
    @test_throws ArgumentError repeat(df, inner = 2, outer = -1)
    @test_throws ArgumentError repeat(df, inner = -1, outer = 3)
    @test df == DataFrame(a = 1:2, b = 3:4)

    @test_throws MethodError repeat!(view(df, 1:2, :), inner = 2, outer = 3)
end

@testset "funname" begin
    @test DataFrames.funname(sum ∘ skipmissing ∘ Base.div12) ==
          :sum_skipmissing_div12
end

@testset "pre-Julia 1.3 @spawn replacement" begin
    t = @sync DataFrames.@spawn begin
        sleep(1)
        true
    end
    @test fetch(t) === true
end

@testset "split_indices" begin
    for len in 1:100, basesize in 1:10
        x = DataFrames.split_indices(len, basesize)

        @test length(x) == max(1, div(len, basesize))
        @test reduce(vcat, x) == 1:len
        vmin, vmax = extrema(length(v) for v in x)
        @test vmin + 1 == vmax || vmin == vmax
        @test len < basesize || vmin >= basesize
    end

    @test_throws AssertionError DataFrames.split_indices(0, 10)
    @test_throws AssertionError DataFrames.split_indices(10, 0)

    # Check overflow on 32-bit
    len = typemax(Int32)
    basesize = 100_000_000
    x = collect(DataFrames.split_indices(len, basesize))
    @test length(x) == div(len, basesize)
    @test x[1][1] === 1
    @test x[end][end] === Int(len)
    vmin, vmax = extrema(length(v) for v in x)
    @test vmin + 1 == vmax || vmin == vmax
    @test len < basesize || vmin >= basesize
end

@testset "split_to_chunks" begin
    for lg in 1:100, nt in 1:11
        if lg < nt
            @test_throws AssertionError DataFrames.split_to_chunks(lg, nt)
            continue
        end
        x = collect(DataFrames.split_to_chunks(lg, nt))
        @test reduce(vcat, x) == 1:lg
        @test sum(length, x) == lg
        @test first(x[1]) == 1
        @test last(x[end]) == lg
        @test length(x) == nt
        for i in 1:nt-1
            @test first(x[i+1])-last(x[i]) == 1
        end
    end

    @test_throws AssertionError DataFrames.split_to_chunks(0, 10)
    @test_throws AssertionError DataFrames.split_to_chunks(10, 0)
    @test_throws AssertionError DataFrames.split_to_chunks(10, 11)
end

end # module


text:
const MAX_JOINT_FORCE = 200

generalized_force(r::JointLimitResult) = r.λ * r.scaling

_lower(b::RigidBodyDynamics.Bounds) = b.lower
_upper(b::RigidBodyDynamics.Bounds) = b.upper

function resolve_joint_limit(model::Model, xnext::LinearizedState, joint::Joint)
    mechanism = xnext.linearization_state.mechanism
    total_weight = mass(mechanism) * norm(mechanism.gravitational_acceleration)
    bounds = RigidBodyDynamics.position_bounds(joint)
    N = length(bounds)
    if N > 0
        lb = _lower.(bounds)
        ub = _upper.(bounds)

        λ = @variable(model, [1:N],
                      lowerbound=-MAX_JOINT_FORCE,
                      upperbound=MAX_JOINT_FORCE,
                      basename="{λ_{joint}}")
        q = current_configuration(xnext, joint)

        @constraints model begin
            [i=1:N], q[i] >= lb[i]
            [i=1:N], q[i] <= ub[i]
        end

        for i in 1:length(λ)
            @disjunction(model, (q[i] >= ub[i]), (λ[i] >= 0))
            @disjunction(model, (q[i] <= lb[i]), (λ[i] <= 0))
        end

        JointLimitResult(λ, total_weight)
    else
        JointLimitResult(Vector{Variable}(), total_weight)
    end
end


text:
# data generation file
# action set:
# "mr" -> move right
const complexities = Dict{AbstractString, Int}("copy"=>1,
                                               "reverse"=>2,
                                               "walk"=>1,
                                               "add"=>2,
                                               "radd"=>3,
                                               "mul"=>1)
const goldacts = Dict{Symbol, AbstractString}(:moveright=>"mr",
                                             :moveleft=>"ml",
                                             :up=>"up",
                                              :down=>"down")
const no_op = -1

# walk data
const LEFT = -2
const DOWN = -3
const UP = -4
const ARROWS = Dict{Int64,Symbol}(LEFT => :moveleft, DOWN => :down, UP => :up)

"""
experiment is a string given as lowercase name
"""
function get_data_generator(experiment::AbstractString)
    !haskey(complexities, experiment) && error("wrong key type $experiment")

    comp = complexities[experiment]
    fname = string(experiment, "_data")
    f = eval(parse(fname))
    return f
end

function copy_data(seqlen)
    data = Any[rand(0:9) for i=1:seqlen]
    actions = [goldacts[:moveright] for i =1:seqlen]
    ygold = data
    actions = map(ai->(ai,WRITE), actions)
    return (data, ygold, actions)
end

function reverse_data(seqlen)
    data = Any[ rand(0:9) for i=1:seqlen ]
    ygold = reverse(data)
    push!(data, LEFT)
    actions = [goldacts[:moveright] for i=1:seqlen]
    actions2 = [goldacts[:moveleft] for i=1:seqlen+1]
    acts = append!(actions, actions2)
    return (data, ygold, actions)
end

function add_data(seqlen)
    low = parse("1"*"0"^(seqlen - 1))
    hi = parse("1"*"0"^seqlen) - 1
    n1 = rand(low:hi)
    num2len = rand(1:seqlen)
    num2 = parse("1"*"0"^num2len)-1
    n2 = rand(1:num2)
    data = (n1, n2)
    ygold = n1 + n2
    compound_move = Any[goldacts[:down], goldacts[:moveleft], goldacts[:up]]

    arrived = 1
    if arrived == seqlen
        actions = compound_move
        return (data, ygold, actions)
    end
    actions = []
    while true
        append!(actions, compound_move); arrived += 1;
        (arrived == seqlen) && break
        append!(actions, [goldacts[:moveleft]]); arrived +=1;
        (arrived == seqlen) && break
    end
    if seqlen % 2 == 1
        append!(actions, compound_move)
    else
        append!(actions, [goldacts[:moveleft]])
        append!(actions, [goldacts[:down]])
    end
    return (data, ygold, actions)
end

function walk_data(seqlen)
    direction = rand(-4:-2) # down, up, left
    width = Int(seqlen/2)
    grid = rand(0:9, width, width)

    mainrow = hcat(rand(0:9, 1, width-1), [direction])
    rowind = -1
    if direction == UP
        rowind = width
    elseif direction == DOWN
        rowind = 1
    else
        rowind = rand(1:width)
    end
    grid[rowind,:] = mainrow
    actions = vcat(
        map(x -> goldacts[:moveright], 1:width-1),
        map(x -> goldacts[ARROWS[direction]], 1:width))

    ygold = nothing
    if direction == UP
        ygold = reverse(grid[1:end-1,end])
    elseif direction == DOWN
        ygold = grid[2:end,end]
    else
        ygold = reverse(grid[rowind,1:end-1])
    end

    return (grid,ygold,actions)
end

function mul_data(seqlen)
    hi = parse("1"*"0"^seqlen) - 1
    low = parse("1"*"0"^(seqlen - 1))
    n = rand(low:hi)
    digit = rand(0:9)
    data = (digit, n)
    ygold = n * digit
    actions = append!([goldacts[:down]], [ goldacts[:moveleft] for i=1:seqlen+1])

    return (data, ygold, actions)
end

function radd_data(seqlen)
    hi = parse("1"*"0"^seqlen) - 1
    low = parse("1"*"0"^(seqlen - 1))
    n = rand(low:hi)
    n2 = rand(1:hi)
    n3 = rand(1:hi)

    data = (n, n2, n3)
    ygold = n + n2 + n3
    compound_action = Any[ goldacts[:down], goldacts[:down], goldacts[:moveleft], goldacts[:up], goldacts[:up]]

    arrived = 1
    if arrived == seqlen
        actions = [ goldacts[:down], goldacts[:down], goldacts[:moveleft], goldacts[:up]]
        return (data, ygold, actions)
    end
    if seqlen == 2
        actions = append!(compound_action, [ goldacts[:moveleft], goldacts[:down]])
        return (data, ygold, actions)
    end

    actions = []
    while true
        append!(actions, compound_action); arrived +=1;
        (arrived == seqlen-1) && break
        append!(actions, [goldacts[:moveleft]]); arrived +=1;
        (arrived == seqlen-1) && break
    end

    fs(num) = length(digits(num))
    second_big = ( fs(n2) > fs(n3) ? fs(n2) : fs(n3) )

    if seqlen % 2 == 1
        append!(actions, [ goldacts[:moveleft] ])
        if second_big == seqlen
            append!(actions, [ goldacts[:down] ])
            if fs(n2) == fs(n3)
                append!(actions, [ goldacts[:down], goldacts[:moveleft], goldacts[:up] ])
            else
                append!(actions, [ goldacts[:down], goldacts[:moveleft] ])
            end
        else
            append!(actions, [ goldacts[:down], goldacts[:down] ])
        end
    else
        append!(actions, compound_action)
        append!(actions, [ goldacts[:moveleft], goldacts[:down] ])
    end

    return (data, ygold, actions)
end

max_stars_repo_name: ereday/algorithm-learning

max_issues_repo_name: ereday/algorithm-learning

max_forks_repo_name: ereday/algorithm-learning


text:
using JuMP, EAGO

                     m = Model()

                     EAGO.register_eago_operators!(m)

                     @variable(m, -1 <= x[i=1:3] <= 1)

                     @variable(m, -8.232182974914569 <= q <= 6.070970235703473)

                     add_NL_constraint(m, :(log(1 + exp(-0.17705543456567296 + 0.5634748900178774*log(1 + exp(-0.9628319588335761 + 0.5826659544327617*$(x[1]) + -0.6099922924008303*$(x[2]) + 0.9101904633653426*$(x[3]))) + 0.6106029608527352*log(1 + exp(0.06629760484384972 + -0.7340864295918403*$(x[1]) + 0.2309908240081593*$(x[2]) + 0.7867070707759196*$(x[3]))) + 0.5384388086098779*log(1 + exp(-0.07370832879656497 + -0.32073040453400337*$(x[1]) + -0.07649911788086428*$(x[2]) + 0.03499506906851524*$(x[3]))))) + log(1 + exp(0.08053767290922664 + 0.911477397877237*log(1 + exp(-0.9628319588335761 + 0.5826659544327617*$(x[1]) + -0.6099922924008303*$(x[2]) + 0.9101904633653426*$(x[3]))) + 0.7134526118957916*log(1 + exp(0.06629760484384972 + -0.7340864295918403*$(x[1]) + 0.2309908240081593*$(x[2]) + 0.7867070707759196*$(x[3]))) + -0.34097520379571256*log(1 + exp(-0.07370832879656497 + -0.32073040453400337*$(x[1]) + -0.07649911788086428*$(x[2]) + 0.03499506906851524*$(x[3]))))) + log(1 + exp(0.5992621446321471 + 0.15304126986204158*log(1 + exp(-0.9628319588335761 + 0.5826659544327617*$(x[1]) + -0.6099922924008303*$(x[2]) + 0.9101904633653426*$(x[3]))) + -0.37134199071135*log(1 + exp(0.06629760484384972 + -0.7340864295918403*$(x[1]) + 0.2309908240081593*$(x[2]) + 0.7867070707759196*$(x[3]))) + 0.8747239793986465*log(1 + exp(-0.07370832879656497 + -0.32073040453400337*$(x[1]) + -0.07649911788086428*$(x[2]) + 0.03499506906851524*$(x[3]))))) - $q <= 0.0))

                     @objective(m, Min, q)

                     return m

max_stars_repo_name: PSORLab/RSActivationFunctions

max_issues_repo_name: PSORLab/RSActivationFunctions

max_forks_repo_name: PSORLab/RSActivationFunctions


text:
@testset "1297.maximum-number-of-occurrences-of-a-substring.jl" begin
    @test max_freq("aababcaab", 2, 3, 4) == 2
    @test max_freq("aaaa", 1, 3, 3) == 2
    @test max_freq("aabcabcab", 2, 2, 3) == 3

    @test max_freq("abcde", 2, 3, 3) == 0
end


text:
abstract type FitSettings end

struct InvertSettings <: FitSettings end

#TODO: Add other fit settings
#TODO: Include settings for noise definitions (e.g. variance = 1 or sqrt2 etc)

abstract type StochasticProcess end

#TODO: Add what setting was used (i.e. what i index)
struct WhiteNoiseProcess{S<:Real,T<:AbstractVector{S}}
    drift::T
    noise::T
    #errors::T
    moments::MomentSolution
    settings::InvertSettings
end

# TODO: Think about general use cases for estimation
function WhiteNoiseProcess(moments::Moments,settings::InvertSettings)
    drift, noise, errors = makef()
    return WhiteNoiseProcess(drift,noise,errors,moments,settings)
end

function WhiteNoiseProcess(moment_sol::MomentSolution;
                           i::Integer=1,
                           settings::InvertSettings=InvertSettings())
    drift, noise = estimateProcess(moment_sol,i)
    #drift, noise, errors = estimateProcess(moment_sol,i)
    #return WhiteNoiseProcess(drift,noise,errors,moment_sol,settings)
    return WhiteNoiseProcess(drift,noise,moment_sol,settings)
end

function estimateProcess(moment_sol::MomentSolution,i::Integer)
    τ_vector = moment_sol.observation.dt *
        moment_sol.settings.time_shift_sample_points
    drift = moment_sol.moments.M1[i,:] ./ τ_vector[i]
    noise = moment_sol.moments.M2[i,:] ./ (2*τ_vector[i])
    #errors = moment_sol.errors[i,:]
    return drift, noise#, errors
end


text:
module mujoco_sim

using Base: RefValue, @lock, @lock_nofail, @propagate_inbounds
# Stdlib
using Printf: @printf

# 3rd party
using GLFW: GLFW, Window, Key, Action, MouseButton, GetKey, RELEASE, PRESS, REPEAT
using PrettyTables: pretty_table
using BangBang: @set!!
using StaticArrays: SVector, MVector
using DocStringExtensions
using Observables: AbstractObservable, Observable, on, off
using FFMPEG
using UnsafeArrays
using Shapes

# Lyceum
using MuJoCo, MuJoCo.MJCore
using MuJoCo.MJCore: mjtNum
using LyceumBase: LyceumBase, Maybe, AbsVec, AbsMat, @mustimplement

const RealVec = AbstractVector{<:Real}

include("mjsim.jl")
include("inverse_kinematics.jl")
export MJSim, zeroctrl!, zerofullctrl!, forward!, reset!
export setaction!, getaction!, getstate!, getaction, getstate
export simulate, activate_mujoco, get_model, get_data
export inverse_kinematics!

const FONTSCALE = MJCore.FONTSCALE_150 # can be 100, 150, 200
const MAXGEOM = 10000 # preallocated geom array in mjvScene
const MIN_REFRESHRATE = 30 # minimum rate when sim cannot run at the native refresh rate
const SIMGAMMA = 0.99
const RNDGAMMA = 0.9
const VIDFPS = 40

const RES_HD = (1280, 720)
const RES_FHD = (1920, 1080)
const RES_XGA = (1024, 768)
const RES_SXGA = (1280, 1024)

include("util.jl")
include("glfw.jl")
include("ratetimer.jl")
include("types.jl")
include("functions.jl")
include("modes.jl")
include("defaulthandlers.jl")

function simulate(
    model::Union{AbstractString,MJSim };
    trajectories::Union{Nothing,AbsMat,AbsVec{<:AbsMat}} = nothing,
    controller = nothing,
    windowsize::NTuple{2,Integer} = default_windowsize(),
    mode = "active"
)
    model isa AbstractString && (model = MJSim(model))
    reset!(model)

    modes = EngineMode[]
    mode == "active" && push!(modes, Controller(controller))
    mode == "passive" && push!(modes, PassiveDynamics())

    run(Engine(windowsize, model, Tuple(modes)))
    return
end

function run(e::Engine)

    # render first frame before opening window
    prepare!(e)
    render(e)
    e.ui.refreshrate = GetRefreshRate()
    e.ui.lastrender = time()
    GLFW.ShowWindow(e.mngr.state.window)

    # run the simulation/mode in second thread
    modetask = Threads.@spawn runphysics(e)

    # println(ASCII)
    # println("Press \"F1\" to show the help message.")

    runui(e)
    wait(modetask)
    return
end

function runui(e::Engine)
    shouldexit = false
    trecord = 0.0
    try
        while !shouldexit
            @lock e.phys.lock begin
                GLFW.PollEvents()
                prepare!(e)
            end

            render(e)
            trender = time()

            rt = 1 / (trender - e.ui.lastrender)
            @lock e.ui.lock begin
                e.ui.refreshrate = RNDGAMMA * e.ui.refreshrate + (1 - RNDGAMMA) * rt
                e.ui.lastrender = trender
                shouldexit = e.ui.shouldexit |= GLFW.WindowShouldClose(e.mngr.state.window)
            end

            tnow = time()
            if e.ffmpeghandle !== nothing && tnow - trecord > 1 / e.min_refreshrate
                trecord = tnow
                recordframe(e)
            end

            yield()
        end
    finally
        @lock e.ui.lock begin
            e.ui.shouldexit = true
        end
        GLFW.DestroyWindow(e.mngr.state.window)
    end
    return
end

function prepare!(e::Engine)
    ui, p = e.ui, e.phys
    sim = getsim(p.model)
    mjv_updateScene(
        sim.m,
        sim.d,
        ui.vopt,
        p.pert,
        ui.cam,
        MJCore.mjCAT_ALL,
        ui.scn,
    )
    prepare!(ui, p, mode(e))
    return e
end

function render(e::Engine)
    w, h = GLFW.GetFramebufferSize(e.mngr.state.window)
    rect = mjrRect(Cint(0), Cint(0), Cint(w), Cint(h))
    mjr_render(rect, e.ui.scn, e.ui.con)
    e.ui.showinfo && overlay_info(rect, e)
    GLFW.SwapBuffers(e.mngr.state.window)
    return
end

function runphysics(e::Engine)
    p = e.phys
    ui = e.ui
    resettime!(p) # reset sim and world clocks to 0

    try
        while true
            shouldexit, lastrender, reversed, paused, refrate, = @lock_nofail ui.lock begin
                ui.shouldexit, ui.lastrender, ui.reversed, ui.paused, ui.refreshrate
            end

            if shouldexit
                break
            elseif (time() - lastrender) > 1 / e.min_refreshrate
                yield()
                continue
            else
                @lock p.lock begin
                    elapsedworld = time(p.timer)

                    # advance sim
                    if ui.paused
                        pausestep!(p, mode(e))
                    elseif ui.reversed && p.elapsedsim > elapsedworld
                        reversestep!(p, mode(e))
                        p.elapsedsim -= timestep(p.model)
                    elseif !ui.reversed && p.elapsedsim < elapsedworld
                        forwardstep!(p, mode(e))
                        p.elapsedsim += timestep(p.model)
                    end
                end
            end
        end
    finally
        @lock ui.lock begin
            ui.shouldexit = true
        end
    end

    return
end

end # module


text:
import JSON

timed_function_call(f, input) = @timed f(input...)

function json_arr_dim(a)
    if length(size(a)) > 0
        return 1 + json_arr_dim(a[1])
    else
        return 0
    end
end

function json_arr_eltype(a)
    if eltype(a) == Any
        return json_arr_eltype(a[1])
    else
        return eltype(a)
    end
end

juliafy_json(a::Array) = convert(Array{{json_arr_eltype(a), json_arr_dim(a)}}, hcat(a...))
juliafy_json(t) = t

input_tuples = JSON.Parser.parsefile("{:s}")

for (i, it) in enumerate(input_tuples)
    it = [juliafy_json(elem) for elem in it]
    ot = $FUNCTION_NAME(it...)
    open("{:s}.output$i.pickle", "w") do f
       JSON.print(f, ot)
    end
end

max_stars_repo_name: benallan/lovelace-engine

max_issues_repo_name: benallan/lovelace-engine

max_forks_repo_name: benallan/lovelace-engine


text:
##==============================================================================
## Printing Variables and Factors
##==============================================================================

printVariable(vert::DFGVariable; kwargs...) = printVariable(stdout::IO, vert; kwargs...)

function printVariable( io::IO, vert::DFGVariable;
                        short::Bool=false,
                        compact::Bool=true,
                        limit::Bool=true,
                        skipfields::Vector{Symbol}=Symbol[],
                        solveKeys::Vector{Symbol}=Symbol[])

    ioc = IOContext(io, :limit=>limit, :compact=>compact)

    if short
        # opmemt = (getVariableType(vert) |> typeof ).name
        vari = getVariableType(vert) |> typeof
        printstyled(ioc, typeof(vert).name.name, "{",bold=true)
        printstyled(ioc, vari.name.name, bold=true, color=:blue)
        printstyled(ioc,"...}", bold=true)
        println(ioc, "")
        # printstyled(ioc, summary(vert),"\n", bold=true)

        try
            print(ioc, "  manifold:   ")
            show(ioc, getManifold(vert))
            println(ioc, "")
        catch e
        end
        vnd = getSolverData(vert, :default)
        println(ioc, "  timestamp:  ", vert.timestamp)
        println(ioc, "   nstime:    ", vert.nstime)
        print(ioc,   "  label:      ")
        printstyled(ioc, vert.label, bold=true)
        println(ioc)
        println(ioc, "  solvable:   ", getSolvable(vert))
        println(ioc, "  tags:       ", getTags(vert))
        solk = listSolveKeys(vert) |> collect
        lsolk = length(solk)
        smsk = (rand(1:lsolk,100) |> unique)[1:minimum([4,lsolk])]
        # list the marginalization status
        ismarg = solk .|> x->isMarginalized(vert, x)
        isinit = solk .|> x->isInitialized(vert, x)
        printstyled(ioc, "  # VND solveKeys=    ($(lsolk))", bold=true)
        println(ioc, "")
        printstyled(ioc, "  # initialized:      ", bold=true)
        println(ioc, "(true=", sum(isinit), ",false=", length(isinit) - sum(isinit), ")" )
        printstyled(ioc, "  # marginalized:     ", bold=true)
        println(ioc, "(true=", sum(ismarg), ",false=", length(ismarg) - sum(ismarg), ")" )

        println(ioc, "    :default <-- VariableNodeData")
        println(ioc, "      initilized:        ", isInitialized(vert, :default))
        println(ioc, "      marginalized:      ", isMarginalized(vert, :default))
        println(ioc, "      size bel. samples: ", size(vnd.val))
        print(ioc, "      kde bandwidths:    ")
        0 < length(vnd.bw) ? println(ioc, round.(vnd.bw[1], digits=4)) : nothing
        printstyled(ioc, "     VNDs: ",bold=true)
        println(ioc, solk[smsk], 4<lsolk ? "..." : "")
        printstyled(ioc, "  # PPE solveKeys=    ($(length(getPPEDict(vert))))", bold=true)
        println(ioc, "")
        if haskey(getPPEDict(vert), :default)
            print(ioc, "    :default ")
            println(ioc, "<-- .suggested:    ", round.(getPPE(vert, :default).suggested,digits=4) )
        end
        maxkeys = 4
        for (key, ppe) in getPPEDict(vert)
            key == :default && continue # skip as default is done separately
            maxkeys -= 1
            maxkeys == 0 && break
            print(ioc, "    :$key ")
            println(ioc, "<-- .suggested:  ", round.(ppe.suggested,digits=4) )
        end
        printstyled(ioc, "  VariableType: ", color=:blue, bold=true)
        println(ioc, vari)
        # println(ioc, "kde max: $(round.(getKDEMax(getKDE(vnd)),digits=4))")
        # println(ioc, "kde max: $(round.(getKDEMax(getKDE(vnd)),digits=4))")
    else

        printstyled(ioc, summary(vert), bold=true, color=:blue)
        println(ioc, "")

        :solver in skipfields && push!(skipfields, :solverDataDict)
        :ppe in skipfields && push!(skipfields, :ppeDict)

        t = typeof(vert)
        fields = setdiff(fieldnames(t), skipfields)
        nf = nfields(vert)

        for f in fields
            printstyled(ioc, f,":", color=:blue)
            println(ioc, "")
            show(ioc, getproperty(vert, f))
            println(ioc)
        end
    end
    nothing
end

printFactor(vert::DFGFactor; kwargs...) = printFactor(stdout::IO, vert; kwargs...)
function printFactor(   io::IO, vert::DFGFactor;
                        short::Bool=false,
                        compact::Bool=true,
                        limit::Bool=true,
                        skipfields::Vector{Symbol}=Symbol[])

    ioc = IOContext(io, :limit=>limit, :compact=>compact)

    if short
        opmemt = (getSolverData(vert).fnc |> typeof).name.name
        fct = getFactorType(vert)
        fctt = fct |> typeof
        printstyled(ioc, typeof(vert).name.name, "{",opmemt,"{",bold=true)
        printstyled(ioc, fctt.name.name,bold=true, color=:blue)
        printstyled(ioc, "...}}", bold=true)
        println(ioc)
        println(ioc, "  timestamp:     ", vert.timestamp)
        println(ioc, "   nstime:       ", vert.nstime)
        print(ioc,   "  label:         ")
        printstyled(ioc, vert.label, bold=true)
        println(ioc)
        println(ioc, "  solvable:      ", vert.solvable)
        println(ioc, "  VariableOrder: ", vert._variableOrderSymbols)
        println(ioc, "  multihypo:     ", getSolverData(vert).multihypo) # FIXME #477
        println(ioc, "  nullhypo:      ", getSolverData(vert).nullhypo)
        println(ioc, "  tags:          ", vert.tags)
        printstyled(ioc, "  FactorType: ", bold=true, color=:blue)
        println(ioc, fctt)
        # show(ioc, fctt)
        for f in setdiff(fieldnames(fctt), skipfields)
            printstyled(ioc, f,":", color=:magenta)
            println(ioc)
            show(ioc, getproperty(fct, f))
            println(ioc)
        end
    else

        printstyled(ioc, summary(vert), bold=true, color=:blue)
        println(ioc)

        :solver in skipfields && push!(skipfields, :solverData)

        t = typeof(vert)
        fields = setdiff(fieldnames(t), skipfields)
        nf = nfields(vert)

        for f in fields
            printstyled(ioc, f,":", color=:blue)
            println(ioc)
            show(ioc, getproperty(vert, f))
            println(ioc)
        end
    end
    nothing
end

"""
    $SIGNATURES

Display and return to console the user factor identified by tag name.
"""
printFactor(dfg::AbstractDFG, sym::Symbol; kwargs...) = printFactor(getFactor(dfg, sym); kwargs...)

"""
   $SIGNATURES

Display the content of `VariableNodeData` to console for a given factor graph and variable tag`::Symbol`.

Dev Notes
- TODO split as two show macros between AMP and DFG
"""
printVariable(dfg::AbstractDFG, sym::Symbol; kwargs...) = printVariable(getVariable(dfg, sym); kwargs...)

printNode(dfg::AbstractDFG, sym::Symbol; kwargs...) = isVariable(dfg,sym) ? printVariable(dfg, sym; kwargs...) : printFactor(dfg, sym; kwargs...)

##==============================================================================
## Overloading show
##==============================================================================
# Base.show_default(io, v)
Base.show(io::IO, ::MIME"text/plain", v::DFGVariable) = printVariable(io, v, short=true, limit=false)

Base.show(io::IO, ::MIME"text/plain", f::DFGFactor) = printFactor(io, f, short=true, limit=false)

function Base.show(io::IO, dfg::AbstractDFG)
    summary(io, dfg)
    println(io, "\n  UserId: ", dfg.userId)
    println(io, "  RobotId: ", dfg.robotId)
    println(io, "  SessionId: ", dfg.sessionId)
    println(io, "  Description: ", dfg.description)
    println(io, "  Nr variables: ", length(ls(dfg)))
    println(io, "  Nr factors: ",length(lsf(dfg)))
    println(io, "  User Data: ", keys(dfg.userData))
    println(io, "  Robot Data: ", keys(dfg.robotData))
    println(io, "  Session Data: ", keys(dfg.sessionData))
end

Base.show(io::IO, ::MIME"text/plain", dfg::AbstractDFG) = show(io, dfg)

#default for Atom/Juno
Base.show(io::IO, ::MIME"application/prs.juno.inline", x::Union{AbstractDFG, DFGVariable, DFGFactor}) = show(io, x)


text:
using SynchronicBallot
import PeaceCypher: sign
using PeaceCypher

struct BallotOfficer <: Officer
    members
    ballots
end

BallotOfficer() = BallotOfficer([], [])

SynchronicBallot.regulation(bo::BallotOfficer) = Regulation(UInt8(3), UInt8(4), Vector{UInt8}("Hello World"))

SynchronicBallot.audit!(bo::BallotOfficer, ballot::Ballot, signatures) = push!(bo.ballots, (ballot, signatures))

Base.in(id, bo::BallotOfficer) = id in bo.members

#### Now some stuff as usual

notary = Notary()
crypto = CypherSuite(notary)

mixkey = newsigner(notary)
mixid = id(mixkey)

mix = Mix(3000, mixid, crypto)

mixtask = @async SynchronicBallot.serve(mix, mixkey)

sleep(1.)

### Now the gatekeeper

gatekey = newsigner(notary)
gateid = id(gatekey)

gk = GateKeeper(3001, gateid, crypto, mix)

bo = BallotOfficer()

gktask = @async SynchronicBallot.serve!(gk, bo, gatekey)

sleep(1.)

### Now I need to make users

user1key = newsigner(notary)
user2key = newsigner(notary)
user3key = newsigner(notary)

push!(bo.members, id(user1key))
push!(bo.members, id(user2key))
push!(bo.members, id(user3key))

@show istaskstarted(gktask)

@sync begin

    @async vote(Vector{UInt8}("msg1"), gk, user1key, b -> binary(sign(b, user1key)))
    @async vote(Vector{UInt8}("msg2"), gk, user2key, b -> binary(sign(b, user2key)))
    @async vote(Vector{UInt8}("msg3"), gk, user3key, b -> binary(sign(b, user3key)))

end

@show bo.ballots


text:
using StochasticDiffEq, DiffEqDevTools, Test

linear = (u,p,t) -> (p*u)
g = (u,p,t) -> zero(u)
linear_analytic = (u0,p,t,W) -> u0*exp(p*t)
prob = SDEProblem(SDEFunction(linear,g,analytic=linear_analytic),g,
                  1/2,(0.0,1.0),1.01)

dts = (1/2) .^ (7:-1:4) #14->7 good plot
sol = solve(prob,SKenCarp())
sim2 = test_convergence(dts,prob,SKenCarp(),trajectories=20)
@test abs(sim2.𝒪est[:l∞]-3) <.1 #High tolerance since low dts for testing!

linear = (du,u,p,t) -> (du.=p.*u)
g = (du,u,p,t) -> (du.=0)
linear_analytic = (u0,p,t,W) -> u0*exp.(p.*t)
prob = SDEProblem(SDEFunction(linear,g,analytic=linear_analytic),g,
                  rand(4,2),(0.0,1.0),1.01)

dts = (1/2) .^ (7:-1:4) #14->7 good plot
sol = solve(prob,SKenCarp())
sim2 = test_convergence(dts,prob,SKenCarp(),trajectories=20)
@test abs(sim2.𝒪est[:l∞]-3) <.1 #High tolerance since low dts for testing!


text:
import Pkg
Pkg.activate(dirname(dirname(@__FILE__)))

using LinearAlgebra: I, normalize!
using Random
using SimLib
using SpinSymmetry
using XXZNumerics
using Statistics

import SimLib.ED

mutable struct TimeTask{T} <: SimLib.ED.EDTask
    elapsed::UInt64
    allocated::Int
    gctime::Int
    task::T
    TimeTask(task) = new{typeof(task)}(0,0,0,task)
end

SimLib.ED.initialize!(tt::TimeTask, arrayconstructor, spectral_size) = SimLib.ED.initialize!(tt.task, arrayconstructor, spectral_size)
# not thread/multiprocess safe!!!
SimLib.ED.initialize_local(tt::TimeTask) = (tt.task = SimLib.ED.initialize_local(tt.task); tt)
function SimLib.ED.compute_task!(tt::TimeTask, args...)
    gcbefore = Base.gc_num()
    timebefore = Base.time_ns()

    SimLib.ED.compute_task!(tt.task, args...)

    timeafter = Base.time_ns()
    gcdiff = Base.GC_Diff(Base.gc_num(), gcbefore)

    tt.elapsed += timeafter - timebefore
    tt.allocated += gcdiff.allocd
    tt.gctime += gcdiff.total_time
end
SimLib.ED.failed_task!(tt::TimeTask, args...) = SimLib.ED.failed_task!(tt.task, args...)

function SimLib.ED.assemble(tt::TimeTask, edd)
    println("Task time summary:\nElapsed: ", tt.elapsed/10e8, "s\nAllocated: ", tt.allocated, " bytes\nGC time: ", tt.gctime/10e8, "s\nTask: ", typeof(tt.task), "\n")
    SimLib.ED.assemble(tt.task, edd)
end

### positiondata setup
location = SaveLocation(prefix=joinpath(path_prefix(), "zero-field"))
N = 12
k = 5
shots = 30
fields = [0.0]
alpha = 6
basis = symmetrized_basis(N, k)

## generate positions
Random.seed!(5)
pdd = PositionDataDescriptor(:box_pbc, 1, N, shots, [0.5, 1.0, 1.5], location)
posdata = load_or_create(pdd)

## Model
model = RandomPositionsXXZWithXField(pdd, PowerLaw(alpha), fields, :ensemble, basis)

### make ED tasks
hopping_operator = 1/2 * real.(σx⊗σx + σy⊗σy)

evaltask = (Energies())
eontask = (EigenstateOccupation("xpol", symmetrize_state(normalize!(ones(2^N)), basis)))
eevtask = (OperatorDiagonal("xmag", symmetrize_operator(sum(op_list(σx/2, N))/N, basis)))
eltask  = (EigenstateLocality("sz", symmetrize_operator(single_spin_op(σz, 1, N), basis)))
eltask2 = (EigenstateLocality("szsz", symmetrize_operator(correlator(σz, 1, 2, N), basis)))
eltask3 = (EigenstateLocality("hopping", symmetrize_operator(kron(hopping_operator, I(2^(N-2))), basis)))
eltask4 = (EigenstateLocality("sxsx", symmetrize_operator(correlator(σx, 1, 2, N), basis)))
lsrtask = (LevelSpacingRatio())
hcetasks = [(HalfChainEntropyZBlock(;basis, L=i)) for i in 1:6]

tasks = [evaltask, eontask, eevtask, lsrtask, eltask, eltask2, eltask3, eltask4, hcetasks...]

### THREADED RUN

edd = EDDataDescriptor(model, Full(), location)

@time edata = run_ed(edd, tasks, Serial())

save.(edata) # save all data

@time edata2 = run_ed(edd, tasks, Threaded())
@time edata3 = run_ed(edd, tasks, Parallel(4))

compare(a,b) = sum(abs2, a.data - b.data)
compare2(a,b) = sum(abs2, mean(a.data;dims=(1,2,3)) - mean(b.data;dims=(1,2,3)))
@show compare.(edata , edata2)
@show compare.(edata2, edata3)
@show compare2.(edata , edata2)
@show compare2.(edata2, edata3)


text:
using SequentialMonteCarlo
using RNGPool
using SMCExamples.FiniteFeynmanKac
import SMCExamples.Particles.Int64Particle
using Test

function Id(p::Int64Particle)
  return p.x
end

function One(p::Int64Particle)
  return 1.0
end

function IdMinus2(p::Int64Particle)
  return p.x - 2
end

function testEtas(smcio, ffkout, verbose::Bool)
  testapproxequal(
    SequentialMonteCarlo.allEtas(smcio, Id, false),
    FiniteFeynmanKac.allEtas(ffkout, Id, false), 0.01, verbose)
  testapproxequal(
    SequentialMonteCarlo.allEtas(smcio, Id, true),
    FiniteFeynmanKac.allEtas(ffkout, Id, true), 0.01, verbose)
end

function testZs(smcio, ffkout, verbose::Bool)
  testapproxequal(smcio.logZhats, ffkout.logZhats, 0.1, verbose)
end

function testapproxequal(x::Vector{Tuple{Bool,Float64}},
  y::Vector{Tuple{Bool,Float64}}, tol::Float64, verbose::Bool)
  xNonneg = (p->p[1]).(x)
  xLogVals = (p->p[2]).(x)
  yNonneg = (p->p[1]).(y)
  yLogVals = (p->p[2]).(y)
  @test xNonneg == yNonneg
  testapproxequal(xLogVals, yLogVals, tol, verbose)
end

function testGammas(smcio, ffkout, verbose::Bool)
  testapproxequal(SequentialMonteCarlo.allGammas(smcio, IdMinus2, false),
    FiniteFeynmanKac.allGammas(ffkout, IdMinus2, false), 0.1, verbose)
  testapproxequal(SequentialMonteCarlo.allGammas(smcio, IdMinus2, true),
    FiniteFeynmanKac.allGammas(ffkout, IdMinus2, true), 0.1, verbose)
end

function testVhat1s(smcio, ffkout, ffk, verbose::Bool)
  testapproxequal(smcio.N * smcio.Vhat1s,
    FiniteFeynmanKac.allavarhat1s(ffk, ffkout, smcio.resample), 0.1, verbose)
end

function _testV(smcio, ffkout, ffk, f, p, tol, verbose::Bool)
  resample = smcio.resample
  testapproxequal(smcio.N * SequentialMonteCarlo.V(smcio, f, false, false, p),
    FiniteFeynmanKac.avar(ffk, ffkout, f, false, false, resample, p), tol,
    verbose)

  testapproxequal(smcio.N * SequentialMonteCarlo.V(smcio, f, false, true, p),
    FiniteFeynmanKac.avar(ffk, ffkout, f, false, true, resample, p), tol,
    verbose)

  testapproxequal(smcio.N * SequentialMonteCarlo.V(smcio, f, true, false, p),
    FiniteFeynmanKac.avar(ffk, ffkout, f, true, false, resample, p), tol,
    verbose)

  testapproxequal(smcio.N * SequentialMonteCarlo.V(smcio, f, true, true, p),
    FiniteFeynmanKac.avar(ffk, ffkout, f, true, true, resample, p), tol,
    verbose)
end

function testV(smcio, ffkout, ffk, verbose::Bool)
  tol::Float64 = 0.3
  _testV(smcio, ffkout, ffk, One, smcio.n, tol, verbose)
  for p = 1:smcio.n
    _testV(smcio, ffkout, ffk, Id, p, tol, verbose)
  end
end

function _testv(smcio, ffkout, ffk, f, p, tol, verbose::Bool)
  resample = smcio.resample
  testapproxequal(SequentialMonteCarlo.v(smcio, f, false, false, p),
    FiniteFeynmanKac.avar(ffk, ffkout, f, false, false, resample, p), tol,
    verbose)
  testapproxequal(SequentialMonteCarlo.v(smcio, f, false, true, p),
    FiniteFeynmanKac.avar(ffk, ffkout, f, false, true, resample, p), tol,
    verbose)
  testapproxequal(SequentialMonteCarlo.v(smcio, f, true, false, p),
    FiniteFeynmanKac.avar(ffk, ffkout, f, true, false, resample, p), tol,
    verbose)
  testapproxequal(SequentialMonteCarlo.v(smcio, f, true, true, p),
    FiniteFeynmanKac.avar(ffk, ffkout, f, true, true, resample, p), tol,
    verbose)
end

function testv(smcio, ffkout, ffk, verbose::Bool)
  tol::Float64 = 0.3
  _testv(smcio, ffkout, ffk, One, smcio.n, tol, verbose)
  for p = 1:smcio.n
    _testv(smcio, ffkout, ffk, Id, p, tol, verbose)
  end
end

function _testvpn(smcio, ffkout, ffk, f, p, tol, verbose::Bool)
  resample = smcio.resample
  testapproxequal(SequentialMonteCarlo.vpns(smcio, f, false, false, p),
    FiniteFeynmanKac.vpns(ffk, ffkout, f, false, false, resample, p), tol,
    verbose)
  testapproxequal(SequentialMonteCarlo.vpns(smcio, f, false, true, p),
    FiniteFeynmanKac.vpns(ffk, ffkout, f, false, true, resample, p), tol,
    verbose)
  testapproxequal(SequentialMonteCarlo.vpns(smcio, f, true, false, p),
    FiniteFeynmanKac.vpns(ffk, ffkout, f, true, false, resample, p), tol,
    verbose)
  testapproxequal(SequentialMonteCarlo.vpns(smcio, f, true, true, p),
    FiniteFeynmanKac.vpns(ffk, ffkout, f, true, true, resample, p), tol,
    verbose)
end

function testvpn(smcio, ffkout, ffk, verbose::Bool)
  tol::Float64 = 0.3
  _testvpn(smcio, ffkout, ffk, One, smcio.n, tol, verbose)
  for p = 1:smcio.n
    _testvpn(smcio, ffkout, ffk, Id, p, tol, verbose)
  end
end

function testFullOutput(ffk::FiniteFeynmanKac.FiniteFK, nthreads::Int64,
  essThreshold::Float64, verbose::Bool)
  ffkout = FiniteFeynmanKac.calculateEtasZs(ffk)
  model = FiniteFeynmanKac.makeSMCModel(ffk)
  n = model.maxn
  smcio = SMCIO{model.particle, model.pScratch}(2^20, n, nthreads, true,
    essThreshold)
  smc!(model, smcio)

  @testset "Finite FK: eta" begin
    testEtas(smcio, ffkout, verbose)
  end

  @testset "Finite FK: Zhat" begin
    testZs(smcio, ffkout, verbose)
  end

  @testset "Finite FK: slgamma" begin
    testGammas(smcio, ffkout, verbose)
  end

  @testset "Finite FK: Vhat1" begin
    testVhat1s(smcio, ffkout, ffk, verbose)
  end

  @testset "Finite FK: V" begin
    testV(smcio, ffkout, ffk, verbose)
  end

  @testset "Finite FK: vpn" begin
    testvpn(smcio, ffkout, ffk, verbose)
  end

  @testset "Finite FK: v" begin
    testv(smcio, ffkout, ffk, verbose)
  end
end

function _getFreqs(model::SMCModel, smcio::SMCIO, m::Int64, d::Int64)
  v::Vector{Int64Particle} = FiniteFeynmanKac.Int642Path(1, d, smcio.n)
  counts = zeros(Int64, d^smcio.n)
  result = Vector{Float64}(undef, d^smcio.n)
  for i = 1:m
    csmc!(model, smcio, v, v)
    counts[FiniteFeynmanKac.Path2Int64(v, d)] += 1
  end
  result .= counts ./ m
  return result
end

function testcsmc(nthreads::Int64, essThreshold::Float64)
  d = 3
  n = 3
  ffk = FiniteFeynmanKac.randomFiniteFK(d, n)

  model = FiniteFeynmanKac.makeSMCModel(ffk)
  densities = Vector{Float64}(undef, d^n)
  for i = 1:length(densities)
    densities[i] = FiniteFeynmanKac.fullDensity(ffk, FiniteFeynmanKac.Int642Path(i, d, n))
  end
  densities ./= sum(densities)

  nsamples = 2^12

  smcio = SMCIO{model.particle, model.pScratch}(max(4,nthreads), model.maxn,
    nthreads, true, essThreshold)
  freqs = _getFreqs(model, smcio, nsamples, d)

  testapproxequal(freqs, densities, 0.05, false)
end

setRNGs(1)

verbose = false

d = 3
ffk = FiniteFeynmanKac.randomFiniteFK(d, 10)

println("\nSerial: always resampling\n")
@time testFullOutput(ffk, 1, 2.0, verbose)
println("\nSerial: adaptive resampling, essThreshold = 0.75\n")
@time testFullOutput(ffk, 1, 0.75, verbose)
println("\nSerial: adaptive resampling, essThreshold = 0.25\n")
@time testFullOutput(ffk, 1, 0.25, verbose)

println("\nParallel: always resampling\n")
@time testFullOutput(ffk, Threads.nthreads(), 2.0, verbose)
println("\nParallel: adaptive resampling, essThreshold = 0.75\n")
@time testFullOutput(ffk, Threads.nthreads(), 0.75, verbose)
println("\nParallel: adaptive resampling, essThreshold = 0.25\n")
@time testFullOutput(ffk, Threads.nthreads(), 0.25, verbose)

@time @testset "Finite FK: csmc" begin
  testcsmc(1, 2.0)
  testcsmc(1, 0.5)
  testcsmc(Threads.nthreads(), 2.0)
  testcsmc(Threads.nthreads(), 0.5)
end


text:
"""
Return the rotation angle between two
vectors of any number of dimensions.

Note that if at least one of the vector is
the zero vector, then it returns 0.
by convention.

Arguments
----------
:Type{RotationAngle}
    must be type ``RotationAngle``

vector_1:AbstractArray
    vector 1

vector_2:AbstractArray
    vector 2

tol_near_zero=1e-7:AbstractFloat
    tolerance for being close to zero
"""
function gage(::Type{RotationAngle}, vector_1::AbstractArray, vector_2::AbstractArray,
    tol_near_zero::AbstractFloat=1e-7)
    @debug @assert length(vector_1) == length(vector_2)

    if nearly_zero(norm(vector_1,2), tol_near_zero)
        return 0.
    elseif nearly_zero(norm(vector_2,2), tol_near_zero)
        return 0.
    else
        #= developer's note: (Yuhang Wang 04/17/2016)
            Sometimes, the normalized dot product is > 1 or < -1 due to
            error in floating point division.
            Without proper handling, Julia will report domain error.
        =#
        normalized_dot = dot(vector_1/norm(vector_1,2), vector_2/norm(vector_2,2))
        if normalized_dot > 1.0
            return acos(floor(normalized_dot))
        elseif normalized_dot < -1.0
            return acos(ceil(normalized_dot))
        else
            return acos(normalized_dot)
        end
    end
end


text:
using ArrayLayouts, LinearAlgebra, FillArrays, Test
import ArrayLayouts: MemoryLayout, DenseRowMajor, DenseColumnMajor, StridedLayout,
                        ConjLayout, RowMajor, ColumnMajor, UnknownLayout,
                        SymmetricLayout, HermitianLayout, UpperTriangularLayout,
                        UnitUpperTriangularLayout, LowerTriangularLayout,
                        UnitLowerTriangularLayout, ScalarLayout,
                        hermitiandata, symmetricdata, FillLayout, ZerosLayout,
                        DiagonalLayout, colsupport, rowsupport

struct FooBar end
struct FooNumber <: Number end

@testset "MemoryLayout" begin
    @testset "Trivial" begin
        @test MemoryLayout(Float64) == MemoryLayout(Int) == MemoryLayout(FooNumber) == ScalarLayout()
        @test MemoryLayout(FooBar) == UnknownLayout()

        A = randn(6)
        @test MemoryLayout(typeof(A)) == MemoryLayout(typeof(Base.ReshapedArray(A,(2,3),()))) ==
            MemoryLayout(typeof(reinterpret(Float32,A))) == DenseColumnMajor()

        @test MemoryLayout(typeof(view(A,1:3))) == DenseColumnMajor()
        @test MemoryLayout(typeof(view(A,Base.OneTo(3)))) == DenseColumnMajor()
        @test MemoryLayout(typeof(view(A,:))) == DenseColumnMajor()
        @test MemoryLayout(typeof(view(A,CartesianIndex(1,1)))) == DenseColumnMajor()
        @test MemoryLayout(typeof(view(A,1:2:4))) == StridedLayout()

        A = randn(6,6)
        V = view(A, 1:3,:)
        @test MemoryLayout(typeof(V)) == ColumnMajor()
    end

    @testset "adjoint and transpose MemoryLayout" begin
        A = [1.0 2; 3 4]
        @test MemoryLayout(typeof(A')) == DenseRowMajor()
        @test MemoryLayout(typeof(transpose(A))) == DenseRowMajor()
        B = [1.0+im 2; 3 4]
        @test MemoryLayout(typeof(B')) == ConjLayout{DenseRowMajor}()
        @test MemoryLayout(typeof(transpose(B))) == DenseRowMajor()
        VA = view(A, 1:1, 1:1)
        @test MemoryLayout(typeof(VA')) == RowMajor()
        @test MemoryLayout(typeof(transpose(VA))) == RowMajor()
        VB = view(B, 1:1, 1:1)
        @test MemoryLayout(typeof(VB')) == ConjLayout{RowMajor}()
        @test MemoryLayout(typeof(transpose(VB))) == RowMajor()
        VA = view(A, 1:2:2, 1:2:2)
        @test MemoryLayout(typeof(VA')) == StridedLayout()
        @test MemoryLayout(typeof(transpose(VA))) == StridedLayout()
        VB = view(B, 1:2:2, 1:2:2)
        @test MemoryLayout(typeof(VB')) == ConjLayout{StridedLayout}()
        @test MemoryLayout(typeof(transpose(VB))) == StridedLayout()
        VA2 = view(A, [1,2], :)
        @test MemoryLayout(typeof(VA2')) == UnknownLayout()
        @test MemoryLayout(typeof(transpose(VA2))) == UnknownLayout()
        VB2 = view(B, [1,2], :)
        @test MemoryLayout(typeof(VB2')) == UnknownLayout()
        @test MemoryLayout(typeof(transpose(VB2))) == UnknownLayout()
        VA2 = view(A, 1:2, :)
        @test MemoryLayout(typeof(VA2')) == RowMajor()
        @test MemoryLayout(typeof(transpose(VA2))) == RowMajor()
        VB2 = view(B, 1:2, :)
        @test MemoryLayout(typeof(VB2')) == ConjLayout{RowMajor}()
        @test MemoryLayout(typeof(transpose(VB2))) == RowMajor()
        VA2 = view(A, :, 1:2)
        @test MemoryLayout(typeof(VA2')) == DenseRowMajor()
        @test MemoryLayout(typeof(transpose(VA2))) == DenseRowMajor()
        VB2 = view(B, :, 1:2)
        @test MemoryLayout(typeof(VB2')) == ConjLayout{DenseRowMajor}()
        @test MemoryLayout(typeof(transpose(VB2))) == DenseRowMajor()
        VAc = view(A', 1:1, 1:1)
        @test MemoryLayout(typeof(VAc)) == RowMajor()
        VAt = view(transpose(A), 1:1, 1:1)
        @test MemoryLayout(typeof(VAt)) == RowMajor()
        VBc = view(B', 1:1, 1:1)
        @test MemoryLayout(typeof(VBc)) == ConjLayout{RowMajor}()
        VBt = view(transpose(B), 1:1, 1:1)
        @test MemoryLayout(typeof(VBt)) == RowMajor()

        @test MemoryLayout(typeof(view(randn(5)',[1,3]))) == UnknownLayout()
    end

    @testset "Symmetric/Hermitian MemoryLayout" begin
        A = [1.0 2; 3 4]
        @test MemoryLayout(typeof(Symmetric(A))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Hermitian(A))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Transpose(Symmetric(A)))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Transpose(Hermitian(A)))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Adjoint(Symmetric(A)))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Adjoint(Hermitian(A)))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(view(Symmetric(A),:,:))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(view(Hermitian(A),:,:))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Symmetric(A'))) == SymmetricLayout{DenseRowMajor}()
        @test MemoryLayout(typeof(Hermitian(A'))) == SymmetricLayout{DenseRowMajor}()
        @test MemoryLayout(typeof(Symmetric(transpose(A)))) == SymmetricLayout{DenseRowMajor}()
        @test MemoryLayout(typeof(Hermitian(transpose(A)))) == SymmetricLayout{DenseRowMajor}()

        @test symmetricdata(Symmetric(A)) ≡ A
        @test symmetricdata(Hermitian(A)) ≡ A
        @test symmetricdata(Transpose(Symmetric(A))) ≡ A
        @test symmetricdata(Transpose(Hermitian(A))) ≡ A
        @test symmetricdata(Adjoint(Symmetric(A))) ≡ A
        @test symmetricdata(Adjoint(Hermitian(A))) ≡ A
        @test symmetricdata(view(Symmetric(A),:,:)) ≡ A
        @test symmetricdata(view(Hermitian(A),:,:)) ≡ A
        @test symmetricdata(Symmetric(A')) ≡ A'
        @test symmetricdata(Hermitian(A')) ≡ A'
        @test symmetricdata(Symmetric(transpose(A))) ≡ transpose(A)
        @test symmetricdata(Hermitian(transpose(A))) ≡ transpose(A)

        B = [1.0+im 2; 3 4]
        @test MemoryLayout(typeof(Symmetric(B))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Hermitian(B))) == HermitianLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Transpose(Symmetric(B)))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Transpose(Hermitian(B)))) == UnknownLayout()
        @test MemoryLayout(typeof(Adjoint(Symmetric(B)))) == UnknownLayout()
        @test MemoryLayout(typeof(Adjoint(Hermitian(B)))) == HermitianLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(view(Symmetric(B),:,:))) == SymmetricLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(view(Hermitian(B),:,:))) == HermitianLayout{DenseColumnMajor}()
        @test MemoryLayout(typeof(Symmetric(B'))) == UnknownLayout()
        @test MemoryLayout(typeof(Hermitian(B'))) == UnknownLayout()
        @test MemoryLayout(typeof(Symmetric(transpose(B)))) == SymmetricLayout{DenseRowMajor}()
        @test MemoryLayout(typeof(Hermitian(transpose(B)))) == HermitianLayout{DenseRowMajor}()

        @test symmetricdata(Symmetric(B)) ≡ B
        @test hermitiandata(Hermitian(B)) ≡ B
        @test symmetricdata(Transpose(Symmetric(B))) ≡ B
        @test hermitiandata(Adjoint(Hermitian(B))) ≡ B
        @test symmetricdata(view(Symmetric(B),:,:)) ≡ B
        @test hermitiandata(view(Hermitian(B),:,:)) ≡ B
        @test symmetricdata(Symmetric(B')) ≡ B'
        @test hermitiandata(Hermitian(B')) ≡ B'
        @test symmetricdata(Symmetric(transpose(B))) ≡ transpose(B)
        @test hermitiandata(Hermitian(transpose(B))) ≡ transpose(B)
     end

    @testset "triangular MemoryLayout" begin
        A = [1.0 2; 3 4]
        B = [1.0+im 2; 3 4]
        for (TriType, TriLayout, TriLayoutTrans) in ((UpperTriangular, UpperTriangularLayout, LowerTriangularLayout),
                              (UnitUpperTriangular, UnitUpperTriangularLayout, UnitLowerTriangularLayout),
                              (LowerTriangular, LowerTriangularLayout, UpperTriangularLayout),
                              (UnitLowerTriangular, UnitLowerTriangularLayout, UnitUpperTriangularLayout))
            @test MemoryLayout(typeof(TriType(A))) == TriLayout{DenseColumnMajor}()
            @test MemoryLayout(typeof(TriType(transpose(A)))) == TriLayout{DenseRowMajor}()
            @test MemoryLayout(typeof(TriType(A'))) == TriLayout{DenseRowMajor}()
            @test MemoryLayout(typeof(transpose(TriType(A)))) == TriLayoutTrans{DenseRowMajor}()
            @test MemoryLayout(typeof(TriType(A)')) == TriLayoutTrans{DenseRowMajor}()

            @test MemoryLayout(typeof(TriType(B))) == TriLayout{DenseColumnMajor}()
            @test MemoryLayout(typeof(TriType(transpose(B)))) == TriLayout{DenseRowMajor}()
            @test MemoryLayout(typeof(TriType(B'))) == TriLayout{ConjLayout{DenseRowMajor}}()
            @test MemoryLayout(typeof(transpose(TriType(B)))) == TriLayoutTrans{DenseRowMajor}()
            @test MemoryLayout(typeof(TriType(B)')) == TriLayoutTrans{ConjLayout{DenseRowMajor}}()
        end

        @test MemoryLayout(typeof(UpperTriangular(B)')) == MemoryLayout(typeof(LowerTriangular(B')))
    end

    @testset "Reinterpreted/Reshaped" begin
       @test MemoryLayout(typeof(reinterpret(Float32, UInt32[1 2 3 4 5]))) == DenseColumnMajor()
       @test MemoryLayout(typeof(reinterpret(Float32, UInt32[1 2 3 4 5]'))) == UnknownLayout()
       @test MemoryLayout(typeof(Base.__reshape((randn(6),IndexLinear()),(2,3)))) == DenseColumnMajor()
       @test MemoryLayout(typeof(Base.__reshape((1:6,IndexLinear()),(2,3)))) == UnknownLayout()
    end

    @testset "Fill" begin
        @test MemoryLayout(typeof(Fill(1,10))) == FillLayout()
        @test MemoryLayout(typeof(Ones(10))) == FillLayout()
        @test MemoryLayout(typeof(Zeros(10))) == ZerosLayout()
        @test MemoryLayout(typeof(view(Fill(1,10),1:3))) == FillLayout()
        @test MemoryLayout(typeof(view(Fill(1,10),1:3,1))) == FillLayout()
    end

    @testset "Triangular col/rowsupport" begin
        A = randn(5,5)
        @test colsupport(UpperTriangular(A),3) ≡ Base.OneTo(3)
        @test rowsupport(UpperTriangular(A),3) ≡ 3:5
        @test colsupport(LowerTriangular(A),3) ≡ 3:5
        @test rowsupport(LowerTriangular(A),3) ≡ Base.OneTo(3)
    end
end


text:
#!/bin/bash
#=
exec julia --startup-file=yes --handle-signals=yes --sysimage-native-code=yes --compiled-modules=yes --banner=yes --color=yes --history-file=yes --depwarn=yes --warn-overwrite=yes --optimize=3 -g 2 --project "${BASH_SOURCE[0]}" "$@"
=#

using
    DiffEqFlux,
    DifferentialEquations,
    Flux,
    Flux.Data,
    Flux.Losses,
    MLBase,
    Plots,
    ProgressMeter

n_iter = 2
make_plot = false

prep_x(x) = Float32.(cat(x..., dims=4))

train_data = DataLoader(
    (MNIST.images(:train),
        Flux.onehotbatch(MNIST.labels(:train), 0:9),),
    batchsize=32,
    shuffle=true,
    partial=true,
)
test_data = DataLoader(
    (MNIST.images(:test),
        Flux.onehotbatch(MNIST.labels(:test), 0:9),),
    batchsize=32,
    shuffle=true,
    partial=true,
)

imgsize = (28, 28, 1)
imgsize_afterconvs = (1, 1, 32)
nclasses = 10

mdl = Chain(
    prep_x, # (28, 28, 1, N)
    Conv((3, 3), imgsize[3] => 16, tanh), # (26, 26, 16, N)
    MaxPool((2, 2)), # (13, 13, 16, N)
    Conv((3, 3), 16 => 32, tanh), # (11, 11, 32, N)
    MaxPool((2, 2)), # (5, 5, 32, N)
    Conv((3, 3), 32 => 32, tanh), # (3, 3, 32, N)
    MaxPool((2, 2)), # (1, 1, 32, 32)
    flatten,
    BatchNorm(prod(imgsize_afterconvs), tanh),
    Dropout(1 / 8),
    Dense(prod(imgsize_afterconvs), 64, tanh),
    BatchNorm(64, tanh),
    Dropout(1 / 8),
    Dense(64, 32, tanh),
    BatchNorm(32, tanh),
    Dropout(1 / 8),
    NeuralODE(Chain(
        # BatchNorm(32, tanh),
        # Dropout(1/8),
        Dense(32, 16, tanh),
        # BatchNorm(16, tanh),
        # Dropout(1/8),
        Dense(16, 16, tanh),
        # BatchNorm(16, tanh),
        # Dropout(1/8),
        Dense(16, 32, tanh),
        # BatchNorm(32, tanh),
        # Dropout(1/8),
    ), 0.0:1.0, Tsit5()),
    x -> x.u[1],
    BatchNorm(32, tanh),
    Dropout(1 / 8),
    Dense(32, 16, tanh),
    BatchNorm(16, tanh),
    Dropout(1 / 8),
    Dense(16, nclasses, tanh),
    BatchNorm(nclasses, tanh),
    Dropout(1 / 8),
    softmax,
)

loss(x, y) = Flux.crossentropy(mdl(x), y)
loss(xy::Tuple) = loss(xy...)
loss(xy::DataLoader) = mean(loss.(xy))
accuracy(x, y) = recall(roc(Flux.onecold(y, 0:9), Flux.onecold(mdl(x), 0:9)))
accuracy(xy::Tuple) = accuracy(xy...)
accuracy(xy::DataLoader) = mean(accuracy.(xy))

opt = ADAM()
ps = Flux.params(mdl)

if make_plot
    loss_list_tn = Float64[]
    loss_list_tt = Float64[]
    acc_list_tn = Float64[]
    acc_list_tt = Float64[]

    function record_scores()
        push!(loss_list_tn, loss(first(train_data)))
        push!(loss_list_tt, loss(first(test_data)))
        push!(acc_list_tn, accuracy(first(train_data)))
        push!(acc_list_tt, accuracy(first(test_data)))

        display(plot(
            [acc_list_tn loss_list_tn acc_list_tt loss_list_tt],
            label=["train accuracy" "train loss" "test accuracy" "test loss"],
            title=["accuracy" "loss"],
            layout=2,
            legend=:outertopright,
            size=(1024, 512),
        ))
    end
end

@show accuracy(test_data)
make_plot && record_scores()
Flux.@epochs n_iter begin
    p = Progress(length(train_data))
    function cb()
        make_plot && record_scores()
        next!(p)
    end
    Flux.train!(loss, ps, train_data, opt, cb=cb)
    @show accuracy(test_data)
end
make_plot && savefig(plot(
    [acc_list_tn loss_list_tn acc_list_tt loss_list_tt],
    label=["train accuracy" "train loss" "test accuracy" "test loss"],
    title=["accuracy" "loss"],
    layout=2,
    legend=:outertopright,
    size=(1024, 512),
), "figs.png")

max_stars_repo_name: prbzrg/MNIST-Recognition

max_issues_repo_name: prbzrg/MNIST-Recognition

max_forks_repo_name: prbzrg/MNIST-Recognition


text:
# This file is auto-generated by AWSMetadata.jl
using AWS
using AWS.AWSServices: s3outposts
using AWS.Compat
using AWS.UUIDs

"""
    create_endpoint(outpost_id, security_group_id, subnet_id)
    create_endpoint(outpost_id, security_group_id, subnet_id, params::Dict{String,<:Any})

Amazon S3 on Outposts Access Points simplify managing data access at scale for shared
datasets in S3 on Outposts. S3 on Outposts uses endpoints to connect to Outposts buckets so
that you can perform actions within your virtual private cloud (VPC). For more information,
see  Accessing S3 on Outposts using VPC only access points. This action creates an endpoint
and associates it with the specified Outposts.  It can take up to 5 minutes for this action
to complete.   Related actions include:    DeleteEndpoint     ListEndpoints

# Arguments
- `outpost_id`: The ID of the AWS Outposts.
- `security_group_id`: The ID of the security group to use with the endpoint.
- `subnet_id`: The ID of the subnet in the selected VPC. The endpoint subnet must belong to
  the Outpost that has the Amazon S3 on Outposts provisioned.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"AccessType"`: The type of access for the on-premise network connectivity for the
  Outpost endpoint. To access the endpoint from an on-premises network, you must specify the
  access type and provide the customer owned IPv4 pool.
- `"CustomerOwnedIpv4Pool"`: The ID of the customer-owned IPv4 pool for the endpoint. IP
  addresses will be allocated from this pool for the endpoint.
"""
function create_endpoint(
    OutpostId, SecurityGroupId, SubnetId; aws_config::AbstractAWSConfig=global_aws_config()
)
    return s3outposts(
        "POST",
        "/S3Outposts/CreateEndpoint",
        Dict{String,Any}(
            "OutpostId" => OutpostId,
            "SecurityGroupId" => SecurityGroupId,
            "SubnetId" => SubnetId,
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function create_endpoint(
    OutpostId,
    SecurityGroupId,
    SubnetId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return s3outposts(
        "POST",
        "/S3Outposts/CreateEndpoint",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "OutpostId" => OutpostId,
                    "SecurityGroupId" => SecurityGroupId,
                    "SubnetId" => SubnetId,
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    delete_endpoint(endpoint_id, outpost_id)
    delete_endpoint(endpoint_id, outpost_id, params::Dict{String,<:Any})

Amazon S3 on Outposts Access Points simplify managing data access at scale for shared
datasets in S3 on Outposts. S3 on Outposts uses endpoints to connect to Outposts buckets so
that you can perform actions within your virtual private cloud (VPC). For more information,
see  Accessing S3 on Outposts using VPC only access points. This action deletes an
endpoint.  It can take up to 5 minutes for this action to complete.   Related actions
include:    CreateEndpoint     ListEndpoints

# Arguments
- `endpoint_id`: The ID of the endpoint.
- `outpost_id`: The ID of the AWS Outposts.

"""
function delete_endpoint(
    endpointId, outpostId; aws_config::AbstractAWSConfig=global_aws_config()
)
    return s3outposts(
        "DELETE",
        "/S3Outposts/DeleteEndpoint",
        Dict{String,Any}("endpointId" => endpointId, "outpostId" => outpostId);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function delete_endpoint(
    endpointId,
    outpostId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return s3outposts(
        "DELETE",
        "/S3Outposts/DeleteEndpoint",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}("endpointId" => endpointId, "outpostId" => outpostId),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_endpoints()
    list_endpoints(params::Dict{String,<:Any})

Amazon S3 on Outposts Access Points simplify managing data access at scale for shared
datasets in S3 on Outposts. S3 on Outposts uses endpoints to connect to Outposts buckets so
that you can perform actions within your virtual private cloud (VPC). For more information,
see  Accessing S3 on Outposts using VPC only access points. This action lists endpoints
associated with the Outposts.   Related actions include:    CreateEndpoint
DeleteEndpoint

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The max number of endpoints that can be returned on the request.
- `"nextToken"`: The next endpoint requested in the list.
"""
function list_endpoints(; aws_config::AbstractAWSConfig=global_aws_config())
    return s3outposts(
        "GET",
        "/S3Outposts/ListEndpoints";
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_endpoints(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return s3outposts(
        "GET",
        "/S3Outposts/ListEndpoints",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end


text:
export lars

import ..LARS

using mlpack._Internal.io

import mlpack_jll
const larsLibrary = mlpack_jll.libmlpack_julia_lars

# Call the C binding of the mlpack lars binding.
function lars_mlpackMain()
  success = ccall((:lars, larsLibrary), Bool, ())
  if !success
    # Throw an exception---false means there was a C++ exception.
    throw(ErrorException("mlpack binding error; see output"))
  end
end

" Internal module to hold utility functions. "
module lars_internal
  import ..larsLibrary

import ...LARS

# Get the value of a model pointer parameter of type LARS.
function IOGetParamLARS(paramName::String)::LARS
  LARS(ccall((:IO_GetParamLARSPtr, larsLibrary), Ptr{Nothing}, (Cstring,), paramName))
end

# Set the value of a model pointer parameter of type LARS.
function IOSetParamLARS(paramName::String, model::LARS)
  ccall((:IO_SetParamLARSPtr, larsLibrary), Nothing, (Cstring, Ptr{Nothing}), paramName, model.ptr)
end

# Serialize a model to the given stream.
function serializeLARS(stream::IO, model::LARS)
  buf_len = UInt[0]
  buf_ptr = ccall((:SerializeLARSPtr, larsLibrary), Ptr{UInt8}, (Ptr{Nothing}, Ptr{UInt}), model.ptr, Base.pointer(buf_len))
  buf = Base.unsafe_wrap(Vector{UInt8}, buf_ptr, buf_len[1]; own=true)
  write(stream, buf)
end
# Deserialize a model from the given stream.
function deserializeLARS(stream::IO)::LARS
  buffer = read(stream)
  LARS(ccall((:DeserializeLARSPtr, larsLibrary), Ptr{Nothing}, (Ptr{UInt8}, UInt), Base.pointer(buffer), length(buffer)))
end
end # module

"""
    lars(; [input, input_model, lambda1, lambda2, responses, test, use_cholesky, verbose])

An implementation of LARS: Least Angle Regression (Stagewise/laSso).  This is a
stage-wise homotopy-based algorithm for L1-regularized linear regression (LASSO)
and L1+L2-regularized linear regression (Elastic Net).

This program is able to train a LARS/LASSO/Elastic Net model or load a model
from file, output regression predictions for a test set, and save the trained
model to a file.  The LARS algorithm is described in more detail below:

Let X be a matrix where each row is a point and each column is a dimension, and
let y be a vector of targets.

The Elastic Net problem is to solve

  min_beta 0.5 || X * beta - y ||_2^2 + lambda_1 ||beta||_1 +
      0.5 lambda_2 ||beta||_2^2

If lambda1 > 0 and lambda2 = 0, the problem is the LASSO.
If lambda1 > 0 and lambda2 > 0, the problem is the Elastic Net.
If lambda1 = 0 and lambda2 > 0, the problem is ridge regression.
If lambda1 = 0 and lambda2 = 0, the problem is unregularized linear regression.

For efficiency reasons, it is not recommended to use this algorithm with
`lambda1` = 0.  In that case, use the 'linear_regression' program, which
implements both unregularized linear regression and ridge regression.

To train a LARS/LASSO/Elastic Net model, the `input` and `responses` parameters
must be given.  The `lambda1`, `lambda2`, and `use_cholesky` parameters control
the training options.  A trained model can be saved with the `output_model`.  If
no training is desired at all, a model can be passed via the `input_model`
parameter.

The program can also provide predictions for test data using either the trained
model or the given input model.  Test points can be specified with the `test`
parameter.  Predicted responses to the test points can be saved with the
`output_predictions` output parameter.

For example, the following command trains a model on the data `data` and
responses `responses` with lambda1 set to 0.4 and lambda2 set to 0 (so, LASSO is
being solved), and then the model is saved to `lasso_model`:

```julia
julia> using CSV
julia> data = CSV.read("data.csv")
julia> responses = CSV.read("responses.csv")
julia> lasso_model, _ = lars(input=data, lambda1=0.4, lambda2=0,
            responses=responses)
```

The following command uses the `lasso_model` to provide predicted responses for
the data `test` and save those responses to `test_predictions`:

```julia
julia> using CSV
julia> test = CSV.read("test.csv")
julia> _, test_predictions = lars(input_model=lasso_model,
            test=test)
```

# Arguments

 - `input::Array{Float64, 2}`: Matrix of covariates (X).
 - `input_model::LARS`: Trained LARS model to use.
 - `lambda1::Float64`: Regularization parameter for l1-norm penalty.
      Default value `0`.

 - `lambda2::Float64`: Regularization parameter for l2-norm penalty.
      Default value `0`.

 - `responses::Array{Float64, 2}`: Matrix of responses/observations (y).
 - `test::Array{Float64, 2}`: Matrix containing points to regress on (test
      points).
 - `use_cholesky::Bool`: Use Cholesky decomposition during computation
      rather than explicitly computing the full Gram matrix.  Default value
      `false`.

 - `verbose::Bool`: Display informational messages and the full list of
      parameters and timers at the end of execution.  Default value `false`.

# Return values

 - `output_model::LARS`: Output LARS model.
 - `output_predictions::Array{Float64, 2}`: If --test_file is specified,
      this file is where the predicted responses will be saved.

"""
function lars(;
              input = missing,
              input_model::Union{LARS, Missing} = missing,
              lambda1::Union{Float64, Missing} = missing,
              lambda2::Union{Float64, Missing} = missing,
              responses = missing,
              test = missing,
              use_cholesky::Union{Bool, Missing} = missing,
              verbose::Union{Bool, Missing} = missing,
              points_are_rows::Bool = true)
  # Force the symbols to load.
  ccall((:loadSymbols, larsLibrary), Nothing, ());

  IORestoreSettings("LARS")

  # Process each input argument before calling mlpackMain().
  if !ismissing(input)
    IOSetParamMat("input", input, points_are_rows)
  end
  if !ismissing(input_model)
    lars_internal.IOSetParamLARS("input_model", convert(LARS, input_model))
  end
  if !ismissing(lambda1)
    IOSetParam("lambda1", convert(Float64, lambda1))
  end
  if !ismissing(lambda2)
    IOSetParam("lambda2", convert(Float64, lambda2))
  end
  if !ismissing(responses)
    IOSetParamMat("responses", responses, points_are_rows)
  end
  if !ismissing(test)
    IOSetParamMat("test", test, points_are_rows)
  end
  if !ismissing(use_cholesky)
    IOSetParam("use_cholesky", convert(Bool, use_cholesky))
  end
  if verbose !== nothing && verbose === true
    IOEnableVerbose()
  else
    IODisableVerbose()
  end

  IOSetPassed("output_model")
  IOSetPassed("output_predictions")
  # Call the program.
  lars_mlpackMain()

  return lars_internal.IOGetParamLARS("output_model"),
         IOGetParamMat("output_predictions", points_are_rows)
end


text:
@testset "Test Kantorovich distance calculation" begin
    μ = [1/7, 2/7, 4/7]
    ν = [1/4, 1/4, 1/2]
    @test isapprox(0.107142857, sail_route.kantorovich_distance(μ, ν), rtol=4)

end