text:
using Nautilus
using Test
using GLMakie
using GPUCompiler
using LLVM
using LLVM.Interop

#@testset "Nautilus.jl" begin
    # Write your tests here.
#end
####
#let
###    fig = Figure()
###    menu = Menu(fig, options = ["viridis", "heat", "blues"])
###    funcs = [sqrt, x->x^2, sin, cos]
###    menu2 = Menu(fig, options = zip(["Square Root", "Square", "Sine", "Cosine"], funcs))
###    fig[1, 1] = vgrid!(
###        Label(fig, "Colormap", width = nothing),
###        menu,
###        Label(fig, "Function", width = nothing),
###        menu2;
###        tellheight = false, width = 200)
###    ax = Axis(fig[1, 2])
###    func = Observable{Any}(funcs[1])
###    ys = lift(func) do f
###        f.(0:0.3:10)
###    end
###    scat = scatter!(ax, ys, markersize = 30px, color = ys)
###    cb = Colorbar(fig[1, 3], scat)
###    on(menu.selection) do s
###        scat.colormap = s
###    end
###    on(menu2.selection) do s
###        func[] = s
###        autolimits!(ax)
###    end
###    menu2.is_open = true
###    fig
###end
##### Toggles
###let
###    using GLMakie
###    fig = Figure()
###    ax = Axis(fig[1, 1])
###    toggles = [Toggle(fig, active = active) for active in [true, false]]
###    labels = [Label(fig, lift(x -> x ? "$l visible" : "$l invisible", t.active))
###        for (t, l) in zip(toggles, ["sine", "cosine"])]
###    fig[1, 2] = grid!(hcat(toggles, labels), tellheight = false)
###    line1 = lines!(0..10, sin, color = :blue, visible = false)
###    line2 = lines!(0..10, cos, color = :red)
###    connect!(line1.visible, toggles[1].active)
###    connect!(line2.visible, toggles[2].active)
###    fig
###end

####
#" These are in LLVM/src/interop"
global PASSES = [
  alloc_opt!,
  speculative_execution_if_has_branch_divergence!,
  loop_unroll!,
  instruction_combining!,
  licm!,
  early_csemem_ssa!,
  dead_store_elimination!,
  cfgsimplification!,
  global_dce!
]

# TODO Don't type pirate
function GPUCompiler.optimize_module!(@nospecialize(job::CompilerJob{PTXCompilerTarget}),
                           mod::LLVM.Module)
     tm = llvm_machine(job.target)
     ModulePassManager() do pm
         add_library_info!(pm, triple(mod))
         add_transform_info!(pm, tm)

         # TODO
         for (pass,toggle) in zip(PASSES, TOGGLES)
            if toggle.active.val
                pass(pm)
            end
        end
        run!(pm, mod)
    end
end

##### GPUCompiler.optimize_module!
# function GPUCompiler.optimize_module!(@nospecialize(job::CompilerJob{PTXCompilerTarget}),
#                           mod::LLVM.Module, nothing)
#     tm = llvm_machine(job.target)
#     ModulePassManager() do pm
#         add_library_info!(pm, triple(mod))
#         add_transform_info!(pm, tm)

#         # needed by GemmKernels.jl-like code
#         speculative_execution_if_has_branch_divergence!(pm)

#         # NVPTX's target machine info enables runtime unrolling,
#         # but Julia's pass sequence only invokes the simple unroller.
#         loop_unroll!(pm)
#         instruction_combining!(pm)  # clean-up redundancy
#         licm!(pm)                   # the inner runtime check might be outer loop invariant

#         # the above loop unroll pass might have unrolled regular, non-runtime nested loops.
#         # that code still needs to be optimized (arguably, multiple unroll passes should be
#         # scheduled by the Julia optimizer). do so here, instead of re-optimizing entirely.
#         early_csemem_ssa!(pm) # TODO: gvn instead? see NVPTXTargetMachine.cpp::addEarlyCSEOrGVNPass
#         dead_store_elimination!(pm)

#         cfgsimplification!(pm)

#         # get rid of the internalized functions; now possible unused
#         global_dce!(pm)

#         run!(pm, mod)
#     end
# end

#### Code highlighting

###using Colors
###using Highlights
###
###using Highlights.Format
###using Highlights.Tokens
###using Highlights.Themes
###
####using Highlights.Themes: has_fg
###css2color(str) = parse(RGBA{Float32}, string("#", str))
###css2color(c::Themes.RGB) = RGBA{Float32}(c.r/255, c.g/255, c.b/255, 1.0)
###
###function style2color(style, default)
###    #if has_fg(style)
###    #    css2color(style.fg)
###    #else
###        default
###    #end
###end
###
###function render_str(
###        ctx::Format.Context, theme::Format.Theme
###    )
###    #defaultcolor = if has_fg(theme.base)
###    #defaultcolor =     css2color(theme.base.fg)
###    #else
###        defaultcolor = RGBA(0f0, 0f0, 0f0, 1f0)
###    #end
###    colormap = map(s-> style2color(s, defaultcolor), theme.styles)
###    tocolor = Dict(zip(Tokens.__TOKENS__, colormap))
###    colors = RGBA{Float32}[]
###    io = IOBuffer()
###    for token in ctx.tokens
###        t = Tokens.__TOKENS__[token.value.value]
###        str = SubString(ctx.source, token.first, token.last)
###        print(io, str)
###        append!(colors, fill(tocolor[t], length(str)))
###        push!(colors, tocolor[t])
###    end
###    String(take!(io)), colors
###end
###
###
###function highlight_text(src::AbstractString, theme = Themes.DefaultTheme)
###    io = IOBuffer()
###    render_str(
###        Highlights.Compiler.lex(src, Lexers.JuliaLexer),
###        Themes.theme(theme)
###    )
###end
###
###
###src = """
###function test(a, b)
###    const a = sin(a) + 2.0
###    return a
###end
###"""
###
###str, colors = highlight_text(src)

#text(str, color=colors)
#text(str)
###### Modify optimize_module!

######
#### TestRuntime

module TestRuntime
    # dummy methods
    signal_exception() = return
    malloc(sz) = C_NULL
    report_oom(sz) = return
    report_exception(ex) = return
    report_exception_name(ex) = return
    report_exception_frame(idx, func, file, line) = return
end

struct TestCompilerParams <: AbstractCompilerParams end
GPUCompiler.runtime_module(::CompilerJob{<:Any,TestCompilerParams}) = TestRuntime

kernel() = begin
    r = Ref{Int}(42)
    r[] = 43
    nothing
end

function main(jobtype = :llvm)
    source = FunctionSpec(kernel)
    target = NativeCompilerTarget()
    params = TestCompilerParams()
    job = CompilerJob(target, source, params)

    jobtype == :llvm && return GPUCompiler.compile(:llvm, job)[1] |> string
    jobtype == :asm  && return GPUCompiler.compile(:asm, job)[1] |> string
end

#isinteractive() || main()
#main()

#### GO GLEN COCO
#    fig = Figure();
#    ax = Axis(fig[1,1]);
#    toggles = [Toggle(fig, active = active.checked) for active in mypasses];
#    labels = [Label(fig, lift( x -> x ? "$(string(l.pass))" : "$(string(l.pass))", t.active); halign = :left, textsize = 48.0f0)
#        for (t, l) in zip(toggles, mypasses)];
#    fig[1, 2] = grid!(hcat(toggles, labels), tellheight = false)
#    fig
#
##text(fig[1,1], main(), position = (0,0), align = :left)
#sspace = campixel(fig.scene);
#width, height = size(sspace)
#width, height = 50, height * .9 # fudge for visibility
#teststr = Observable(main())
#strlistener = on(teststr) do _
#    main()
#
#end
#text!(sspace, teststr, position = (width, height), align = (:left, :top), font = "JuliaMono");
#fig[1, 2] = grid!(hcat(toggles, labels), tellheight = false);
#fig
#cam = Makie.camrelative(fig.scene);
#
#let
    fig = Figure(backgroundcolor = :gray70);
    passbox = fig[1,2] = Axis(fig[1,2], title = "Passes")
    #toggle = Toggle(f)
    #label = Label(f, lift(x -> x ? "ON TOGGLE" : "OFF TOGGLE", toggle.active), textsize = 48f0)
    #f[1,2] = grid!(hcat([toggle], label), tellheight = false)
    toggles = [Toggle(fig) for _ in PASSES];
    labels = [Label(fig, lift(x -> x ? "$(string(pass))" : "$(string(pass))", t.active); halign = :left, textsize = 48.0f0)
        for (t, pass) in zip(toggles, PASSES)];
    fig[1, 2] = grid!(hcat(toggles, labels), tellheight = false)
    # TODO LLVM or asm job compiler
    textbox = fig[1,1] = Axis(fig[1,1], title = "Compiled code")
    #text!(textbox, lift(x -> x ? "ON TOGGLE" : "OFF TOGGLE", toggles[1].active))
    #str = @lift begin
        #for toggle in 1:length(toggles)
            #$(toggle[i].active)
            #end;
            #main()
        #end
    #onany(toggles) do x
    #    str[] = main()
    #end
    #onany(toggles, main)
    #onany(main, toggles...)

    #text!(textbox, lift(x -> x ? "ON TOGGLE" : "OFF TOGGLE", toggles[2].active))
    bools = [t.active.val for t in toggles]
    bobs = Observable(bools)
    #str = Observable(main())
    #on(toggles[2].active) do x
    #    println(x)
    #end
    #str = "$(str[])\n$(string([t.active.val for t in toggles]))" |> Observable
    str = @lift begin
        $(toggles[1].active)
        $(toggles[2].active)
        $(toggles[3].active)
        $(toggles[4].active)
        $(toggles[5].active)
        $(toggles[6].active)
        $(toggles[7].active)
        $(toggles[8].active)
        $(toggles[9].active)
        string([t.active.val for t in toggles])
    end
    height = size(campixel(fig.scene))[2]
    w, h = size(campixel(fig.scene))
    w = 50
    @show h
    @show w
    pos = (w, 2 * h)
    text!(textbox, str, font = "JuliaMono",  align = (:left, :top), position = pos)
    fig
#end


text:
module Interact

using React

import Base: mimewritable, writemime
import React.signal
export signal, statedict, Widget, InputWidget, register_widget,
       get_widget, parse, recv, update_view

# A widget
abstract Widget

# A widget that gives out a signal of type T
abstract InputWidget{T}  <: Widget

signal(w::InputWidget) = w.signal

function statedict(w::Widget)
    msg = Dict()
    attrs = names(w)
    for n in attrs
        if n in [:signal, :label]
            continue
        end
        msg[n] = getfield(w, n)
    end
    msg
end

function parse{T}(msg, ::InputWidget{T})
    # Should return a value of type T, by default
    # msg itself is assumed to be the value.
    return convert(T, msg)
end

# default cases

parse{T <: Integer}(v, ::InputWidget{T}) = int(v)
parse{T <: FloatingPoint}(v, ::InputWidget{T}) = float(v)
parse(v, ::InputWidget{Bool}) = bool(v)

function update_view(w)
    # update the view of a widget.
    # child packages need to override.
end

function recv{T}(widget ::InputWidget{T}, value)
    # Hand-off received value to the signal graph
    parsed = parse(value, widget)
    push!(signal(widget), parsed)
    widget.value = parsed
    if value != parsed
        update_view(widget)
    end
end

uuid4() = string(Base.Random.uuid4())

const id_to_widget = Dict{String, InputWidget}()
const widget_to_id = Dict{InputWidget, String}()

function register_widget(w::InputWidget)
    if haskey(widget_to_id, w)
        return widget_to_id[w]
    else
        id = string(uuid4())
        widget_to_id[w] = id
        id_to_widget[id] = w
        return id
    end
end

function get_widget(id::String)
    if haskey(id_to_widget, id)
        return id_to_widget[id]
    else
        warn("Widget with id $(id) does not exist.")
    end
end

include("widgets.jl")
include("compose.jl")
include("manipulate.jl")
include("html_setup.jl")

if isdefined(Main, :IJulia)
    if Pkg.installed("IJulia") >= v"0.1.3-"
        include("IJulia/setup.jl")
    else
        warn("Interact requires IJulia >= v0.1.3 to work properly.")
    end
end

end # module

ext: jl


text:
using DataStructures
using NCDatasets
using Formatting
using ArgParse, JSON

s = ArgParseSettings()
@add_arg_table s begin

    "--ref-file"
        help = "The 3D temperature or salinity file. Mask should be missing data."
        arg_type = String
        required = true

    "--ref-var"
        help = "Referenced variable name in `ref-file`."
        arg_type = String
        default = "TEMP"

    "--Nz-max"
        help = "Referenced variable name in `ref-file`."
        arg_type = Int64
        default = -1

    "--output-file"
        help = "Name of the output Nz file"
        arg_type = String
        required = true

end

parsed = DataStructures.OrderedDict(parse_args(ARGS, s))

JSON.print(parsed, 4)

Dataset(parsed["ref-file"], "r") do ds
    global ref_var  = permutedims(nomissing(ds[parsed["ref-var"]][:, :, :, 1],  NaN), [3, 1, 2])

    global Nz, Nx, Ny = size(ref_var)

    if parsed["Nz-max"] != -1

        Nz = parsed["Nz-max"]
        ref_var = ref_var[1:Nz, :, :]
    end

end

Nz_bot = zeros(Int64, Nx, Ny)
mask_T = zeros(Float64, Nz, Nx, Ny)
mask_T[isfinite.(ref_var)] .= 1.0
valid_idx = mask_T .== 1.0
Nz_bot .= sum(mask_T, dims=1)[1, :, :]

println("Output file: $(parsed["output-file"]).")

Dataset(parsed["output-file"], "c") do ds

    defDim(ds, "Nx", Nx)
    defDim(ds, "Ny", Ny)
    defVar(ds, "Nz_bot", Nz_bot, ("Nx", "Ny", ), ; attrib = Dict(
        "long_name" => "z-grid idx of the deepest cell",
        "units"     => "none",
    ))

end

max_stars_repo_name: meteorologytoday/IOM

max_issues_repo_name: meteorologytoday/IOM

max_forks_repo_name: meteorologytoday/IOM


text:
using SyntaxTree
@static if VERSION < v"0.7.0-DEV.2005"
    using Base.Test
else
    using Test
end

# write your own tests here
@test Meta.parse("begin x+1 end") |> linefilter! == Expr(:block,:(x+1))
if VERSION < v"0.7"
@test SyntaxTree.sub(Float64,:(7^(x+1)+$(Expr(:macrocall,Symbol("@big_str"),"2")))) == :(7.0^(x+1.0)+2.0)
@test SyntaxTree.abs(:((x-1)^2+$(Expr(:macrocall,Symbol("@big_str"),"2")))) == :((x + 1) ^ 2 + $(Expr(:macrocall,Symbol("@big_str"),"2")))
else
@test SyntaxTree.sub(Float64,:(7^(x+1)+$(Expr(:macrocall,Symbol("@big_str"),nothing,"2")))) == :(7.0^(x+1.0)+2.0)
@test SyntaxTree.abs(:((x-1)^2+$(Expr(:macrocall,Symbol("@big_str"),nothing,"2")))) == :((x + 1) ^ 2 + $(Expr(:macrocall,Symbol("@big_str"),nothing,"2")))
end
@test SyntaxTree.alg(:(x+1)) == :((1 + ϵ) * (x + 1))
@test (f = SyntaxTree.genfun(:x,:x); f(1) == 1)
@test (f = SyntaxTree.genfun(:x,(:x,:y)); f(1,0) == 1)
@test (f = SyntaxTree.genlatest(:x,:x); f(1) == 1)
@test (f = SyntaxTree.genlatest(:x,(:x,:y)); f(1,0) == 1)
@test callcount(:(x+y*z)) == 2
@test SyntaxTree.exprval(:(x^2-2))[1] == 0.0


text:
# Define simple generic types and methods for testing

sprintcompact(x) = sprint(show, x; context=:compact=>true)

struct TestSharpness <: TreatmentSharpness end
struct TestParaCondition <: ParallelCondition end
struct TestParaStrength <: ParallelStrength end

struct TestTreatment <: AbstractTreatment
    time::Symbol
    ref::Int
end

ttreat(time::Term, ref::ConstantTerm) = TestTreatment(time.sym, ref.n)

struct TestParallel{C,S} <: AbstractParallel{C,S}
    e::Int
end

TestParallel(e::Int) = TestParallel{ParallelCondition,ParallelStrength}(e)
tpara(c::ConstantTerm) = TestParallel{ParallelCondition,ParallelStrength}(c.n)

teststep(tr::AbstractTreatment, pr::AbstractParallel) =
    (str=sprint(show, tr), spr=sprint(show, pr))
const TestStep = StatsStep{:TestStep, typeof(teststep), true}
required(::TestStep) = (:tr, :pr)

testnextstep(::AbstractTreatment, str::String) = (next="next"*str,)
const TestNextStep = StatsStep{:TestNextStep, typeof(testnextstep), true}
required(::TestNextStep) = (:tr, :str)

const TestDID = DiffinDiffsEstimator{:TestDID, Tuple{TestStep,TestNextStep}}
const NotImplemented = DiffinDiffsEstimator{:NotImplemented, Tuple{}}

const TR = TestTreatment(:t, 0)
const PR = TestParallel(0)

struct TestResult{TR} <: DIDResult{TR}
    coef::Vector{Float64}
    vcov::Matrix{Float64}
    vce::Nothing
    tr::AbstractTreatment
    nobs::Int
    dof_residual::Int
    yname::String
    coefnames::Vector{String}
    coefinds::Dict{String, Int}
    treatcells::VecColumnTable
    weightname::Symbol
    extra1::Int
    extra2::Symbol
    extra3::String
    extra4::Vector{Float64}
    extra5::Vector{String}
    extra6::Matrix{Float64}
    extra7::Vector{Symbol}
    extra8::Nothing
    extra9::Array{Float64,3}
end

@fieldequal TestResult

function TestResult(n1::Int, n2::Int)
    N = n1*(n2+1)
    coef = collect(Float64, 1:N)
    tr = TR
    tnames = ["rel: $a & c: $b" for a in 1:n1 for b in 1:n2]
    cnames = vcat(tnames, ["c"*string(i) for i in n1*n2+1:N])
    cinds = Dict(cnames .=> 1:N)
    tcells = VecColumnTable((rel=repeat(1:n1, inner=n2), c=repeat(1:n2, outer=n1)))
    return TestResult{typeof(tr)}(coef, coef.*coef', nothing, tr, N, N-1, "y", cnames, cinds,
        tcells, :w, 1, :a, "a", [1.0], ["a"], [1.0 2.0], [:a], nothing, ones(1,1,1))
end

struct TestResultBARE{TR} <: DIDResult{TR}
    coef::Vector{Float64}
    vcov::Matrix{Float64}
    vce::Nothing
    tr::AbstractTreatment
    nobs::Int
    yname::String
    coefnames::Vector{String}
    treatcells::VecColumnTable
    weightname::Symbol
end

@fieldequal TestResultBARE

function TestResultBARE(n1::Int, n2::Int)
    N = n1*n2
    coef = collect(Float64, 1:N)
    tr = dynamic(:time, (-1,))
    tnames = ["rel: $a & c: $b" for a in 1:n1 for b in 1:n2]
    tcells = VecColumnTable((rel=repeat(1:n1, inner=n2), c=repeat(1:n2, outer=n1)))
    return TestResultBARE{typeof(tr)}(coef, coef.*coef', nothing, tr, N, "y", tnames, tcells, :w)
end

function result(::Type{TestDID}, nt::NamedTuple)
    return merge(nt, (result=TestResult(2, 2),))
end


text:
using Documenter
using MPI

makedocs(
    sitename = "MPI.jl",
    format = Documenter.HTML(
        prettyurls = get(ENV, "CI", nothing) == "true"
    ),
    modules = [MPI],
    pages = Any[
        "index.md",
        "installing.md",
        "usage.md",
        "functions.md",
        ]
)

deploydocs(
    repo = "github.com/JuliaParallel/MPI.jl.git"
)


text:
@testset "wasserstein.jl" begin
    @testset "SqWasserstein" begin
        μ1, μ2 = randn(2)
        σ1, σ2 = rand(2)
        normal1 = Normal(μ1, σ1)
        normal2 = Normal(μ2, σ2)

        @test iszero(SqWasserstein()(normal1, normal1))
        @test iszero(SqWasserstein()(normal2, normal2))
        @test SqWasserstein()(normal1, normal2) == (μ1 - μ2)^2 + (σ1 - σ2)^2

        for (d1, d2) in Iterators.product((normal1, normal2), (normal1, normal2))
            mvnormal1 = MvNormal([mean(d1)], fill(var(d1), 1, 1))
            mvnormal2 = MvNormal([mean(d2)], fill(var(d2), 1, 1))
            @test SqWasserstein()(mvnormal1, mvnormal2) == SqWasserstein()(d1, d2)

            mvnormal_fill1 = MvNormal(fill(mean(d1), 10), Diagonal(fill(var(d1), 10)))
            mvnormal_fill2 = MvNormal(fill(mean(d2), 10), Diagonal(fill(var(d2), 10)))
            @test SqWasserstein()(mvnormal_fill1, mvnormal_fill2) ≈
                10 * SqWasserstein()(d1, d2)
        end

        laplace1 = Laplace(μ1, σ1)
        laplace2 = Laplace(μ2, σ2)
        @test iszero(SqWasserstein()(laplace1, laplace1))
        @test iszero(SqWasserstein()(laplace2, laplace2))
        @test SqWasserstein()(laplace1, laplace2) == (μ1 - μ2)^2 + 2 * (σ1 - σ2)^2

        # pairwise computations
        for (m, n) in ((1, 10), (10, 1), (10, 10))
            dists1 = [Normal(randn(), rand()) for _ in 1:m]
            dists2 = [Normal(randn(), rand()) for _ in 1:n]

            # compute distance matrix
            distmat = [SqWasserstein()(x, y) for x in dists1, y in dists2]

            # out-of-place
            @test pairwise(SqWasserstein(), dists1, dists2) ≈ distmat

            # in-place
            z = similar(distmat)
            pairwise!(z, SqWasserstein(), dists1, dists2)
            @test z ≈ distmat
        end
    end

    @testset "Wasserstein" begin
        μ1, μ2 = randn(2)
        σ1, σ2 = rand(2)
        normal1 = Normal(μ1, σ1)
        normal2 = Normal(μ2, σ2)

        @test iszero(Wasserstein()(normal1, normal1))
        @test iszero(Wasserstein()(normal2, normal2))
        @test Wasserstein()(normal1, normal2) == sqrt(SqWasserstein()(normal1, normal2))

        for (d1, d2) in Iterators.product((normal1, normal2), (normal1, normal2))
            mvnormal1 = MvNormal([mean(d1)], fill(var(d1), 1, 1))
            mvnormal2 = MvNormal([mean(d2)], fill(var(d2), 1, 1))
            @test Wasserstein()(mvnormal1, mvnormal2) == Wasserstein()(d1, d2)
            @test Wasserstein()(mvnormal1, mvnormal2) == sqrt(SqWasserstein()(d1, d2))

            mvnormal_fill1 = MvNormal(fill(mean(d1), 10), Diagonal(fill(var(d1), 10)))
            mvnormal_fill2 = MvNormal(fill(mean(d2), 10), Diagonal(fill(var(d2), 10)))
            @test Wasserstein()(mvnormal_fill1, mvnormal_fill2) ≈
                sqrt(10) * Wasserstein()(d1, d2)
            @test Wasserstein()(mvnormal_fill1, mvnormal_fill2) ==
                sqrt(SqWasserstein()(mvnormal_fill1, mvnormal_fill2))
        end

        laplace1 = Laplace(μ1, σ1)
        laplace2 = Laplace(μ2, σ2)
        @test iszero(Wasserstein()(laplace1, laplace1))
        @test iszero(Wasserstein()(laplace2, laplace2))
        @test Wasserstein()(laplace1, laplace2) == sqrt(SqWasserstein()(laplace1, laplace2))

        # pairwise computations
        for (m, n) in ((1, 10), (10, 1), (10, 10))
            dists1 = [Normal(randn(), rand()) for _ in 1:m]
            dists2 = [Normal(randn(), rand()) for _ in 1:n]

            # compute distance matrix
            distmat = [Wasserstein()(x, y) for x in dists1, y in dists2]

            # out-of-place
            @test pairwise(Wasserstein(), dists1, dists2) ≈ distmat

            # in-place
            z = similar(distmat)
            pairwise!(z, Wasserstein(), dists1, dists2)
            @test z ≈ distmat
        end
    end

    @testset "SqMixtureWasserstein" begin
        for T in (Normal, Laplace)
            mixture1 = MixtureModel(T, [(randn(), rand())], [1.0])
            mixture2 = MixtureModel(T, [(randn(), rand())], [1.0])
            @test SqMixtureWasserstein()(mixture1, mixture2) ≈
                SqWasserstein()(first(components(mixture1)), first(components(mixture2)))

            mixture1 = MixtureModel(T, [(randn(), rand()), (randn(), rand())], [1.0, 0.0])
            mixture2 = MixtureModel(T, [(randn(), rand()), (randn(), rand())], [0.0, 1.0])
            @test SqMixtureWasserstein()(mixture1, mixture2) ≈
                SqWasserstein()(first(components(mixture1)), last(components(mixture2)))

            mixture1 = MixtureModel(T, fill((randn(), rand()), 10))
            mixture2 = MixtureModel(T, fill((randn(), rand()), 10))
            @test SqMixtureWasserstein()(mixture1, mixture2) ≈
                SqWasserstein()(first(components(mixture1)), first(components(mixture2))) rtol =
                10 * sqrt(eps())

            mixture1 = MixtureModel(T, fill((randn(), rand()), 10))
            mixture2 = MixtureModel(T, [(randn(), rand())])
            @test SqMixtureWasserstein()(mixture1, mixture2) ≈
                SqWasserstein()(first(components(mixture1)), first(components(mixture2)))
        end
    end

    @testset "MixtureWasserstein" begin
        for T in (Normal, Laplace)
            mixture1 = MixtureModel(T, [(randn(), rand())], [1.0])
            mixture2 = MixtureModel(T, [(randn(), rand())], [1.0])
            @test MixtureWasserstein()(mixture1, mixture2) ≈
                Wasserstein()(first(components(mixture1)), first(components(mixture2)))
            @test MixtureWasserstein()(mixture1, mixture2) ≈
                sqrt(SqMixtureWasserstein()(mixture1, mixture2))

            mixture1 = MixtureModel(T, [(randn(), rand()), (randn(), rand())], [1.0, 0.0])
            mixture2 = MixtureModel(T, [(randn(), rand()), (randn(), rand())], [0.0, 1.0])
            @test MixtureWasserstein()(mixture1, mixture2) ≈
                Wasserstein()(first(components(mixture1)), last(components(mixture2)))
            @test MixtureWasserstein()(mixture1, mixture2) ≈
                sqrt(SqMixtureWasserstein()(mixture1, mixture2))

            mixture1 = MixtureModel(T, fill((randn(), rand()), 10))
            mixture2 = MixtureModel(T, fill((randn(), rand()), 10))
            @test MixtureWasserstein()(mixture1, mixture2) ≈
                Wasserstein()(first(components(mixture1)), first(components(mixture2))) rtol =
                10 * sqrt(eps())
            @test MixtureWasserstein()(mixture1, mixture2) ≈
                sqrt(SqMixtureWasserstein()(mixture1, mixture2))

            mixture1 = MixtureModel(T, fill((randn(), rand()), 10))
            mixture2 = MixtureModel(T, [(randn(), rand())])
            @test MixtureWasserstein()(mixture1, mixture2) ≈
                Wasserstein()(first(components(mixture1)), first(components(mixture2)))
            @test MixtureWasserstein()(mixture1, mixture2) ≈
                sqrt(SqMixtureWasserstein()(mixture1, mixture2))
        end
    end
end


text:
# This tests the Simulation object (not the simulate function)

Random.seed!(1777)
n = 1000               # n observations
p = 5                  # n dimensions (covariates)
X = randn(n, p) .+ 0.1 # feature matrix
w = 5 * rand(p)        # true vector of parameters
# observations according to a logistic thresholded to {-1,1}
y = (logistic.(X * w) .> rand(n)) .* 2.0 .- 1.0
# proxy for N*L upper bound
b = sum( mapslices(e->norm(e)^2, X, dims=1) )/4

# DATA MODEL
dm = LogReg(X, y, b)

# GEOMETRY
ns, a    = diagm(0=>ones(p)), zeros(p)
geom     = Polygonal(ns, a)
nb(x,v)  = nextboundary(geom, x, v)

# GRADIENTS and IPPSampling
gll_cv    = gradloglik_cv(dm, w)
gllstar   = gradloglik(dm, w)
lb        = LinearBound(w, gllstar, dm.b)
nev(x, v) = nextevent_bps(lb, x, v)

# SIMULATION
T           = Inf          # simulation 'time'
maxgradeval = 500000       # max number of gradient evaluations
lref        = 2.           # refreshment rate
x0          = w            # initial point for the trajectory
v0          = randn(dm.p)  # draw velocity from normal distr
v0         /= norm(v0)     # normalise velocity

sim = Simulation(
        x0, v0, T, nev, gll_cv, nb, lref;
        maxgradeval = maxgradeval);

# all the basic ones

xrand = randn(p)
vrand = randn(p)

@test sim.x0 == x0 &&
      sim.v0 == v0 &&
      sim.T  == T

@test (Random.seed!(12); sim.nextevent(xrand, vrand).tau) ==
      (Random.seed!(12); nev(xrand, vrand).tau)
@test (Random.seed!(12); sim.gll(xrand)) == (Random.seed!(12); gll_cv(xrand)) &&
        sim.nextboundary(xrand, vrand) == nb(xrand, vrand) &&
        sim.lambdaref == lref &&
        sim.algname   == "BPS"
@test   sim.dim         == length(x0) &&
        sim.mass        == diagm(0=>ones(0)) &&
        sim.blocksize   == 1000 &&
        sim.maxsimtime  == 4e3 &&
        sim.maxsegments == Int(1e6) &&
        sim.maxgradeval == maxgradeval

simf = Simulation(
        x0, v0, T, nev, gll_cv, nb, lref, "bps";
        maxgradeval = maxgradeval);

@test simf.algname == "BPS"

@test_throws AssertionError Simulation(
        x0, v0, T, nev, gll_cv, nb, lref, "bpsasdf";
        maxgradeval = maxgradeval);

sim2 = Simulation(
        x0 = x0, v0 = v0, T = T, nextevent = nev, gradloglik = gll_cv,
        nextboundary = nb, lambdaref = lref; maxgradeval = maxgradeval)

@test   sim2.x0 == x0 &&
        sim2.v0 == v0 &&
        sim2.T  == T  &&
        (Random.seed!(12);sim2.nextevent(xrand, vrand).tau)==
        (Random.seed!(12);nev(xrand, vrand).tau)
@test   (Random.seed!(12);sim2.gll(xrand))==(Random.seed!(12);gll_cv(xrand)) &&
        sim2.nextboundary(xrand, vrand) == nb(xrand, vrand) &&
        sim2.lambdaref == lref &&
        sim2.algname   == "BPS"
@test   sim2.dim         == length(x0) &&
        sim2.mass        == diagm(0=>ones(0)) &&
        sim2.blocksize   == 1000 &&
        sim2.maxsimtime  == 4e3 &&
        sim2.maxsegments == Int(1e6) &&
        sim2.maxgradeval == maxgradeval

@test_throws AssertionError Simulation(
        x0 = x0, v0 = v0, T = T, gradloglik = gll_cv,
        nextboundary = nb, lambdaref = lref; maxgradeval = maxgradeval)
@test_throws AssertionError Simulation(
        x0 = x0, v0 = v0, T = T, nextevent = nev,
        nextboundary = nb, lambdaref = lref; maxgradeval = maxgradeval)
@test_throws AssertionError Simulation(
        x0 = x0, v0 = v0, T = T, nextevent = nev, gradloglik = gll_cv,
        lambdaref = lref; maxgradeval = maxgradeval)
@test_throws AssertionError Simulation(
        x0 = x0, v0 = v0, T = 0.0, nextevent = nev, gradloglik = gll_cv,
        nextboundary = nb, lambdaref = lref; maxgradeval = maxgradeval)
@test_throws AssertionError Simulation(
        x0 = x0, v0 = v0, T = 1.0, nextevent = nev, gradloglik = gll_cv,
        nextboundary = nb, lambdaref = -0.1; maxgradeval = maxgradeval)
@test_throws AssertionError Simulation(
        v0 = v0, T = T, nextevent = nev, gradloglik = gll_cv,
        nextboundary = nb, lambdaref = lref; maxgradeval = maxgradeval)
@test_throws AssertionError Simulation(
        x0 = x0, T = T, nextevent = nev, gradloglik = gll_cv,
        nextboundary = nb, lambdaref = lref; maxgradeval = maxgradeval)
@test_throws AssertionError Simulation(
        x0 = x0, v0 = v0, nextevent = nev, gradloglik = gll_cv,
        nextboundary = nb, lambdaref = lref; maxgradeval = maxgradeval)


text:
using ApproxFun, BandedMatrices, BlockBandedMatrices, LinearAlgebra, BlockArrays, MultivariateOrthogonalPolynomials, Test
using SparseVolterraExamples

#############################
## Tests for Volterra integral operator
## with non-Clenshaw monomial expansion of the kernel.
####
## Includes tests for 1 to x and 1 to 1-x limits of integration.
#############################

@testset "Volterra Integral operator testing, no kernel" begin
    N=15;
    S = Jacobi(0,1,0..1);
    g = Fun(x -> 1-sin(x)^2+x*exp(x^3), S);
    V = triVolterraFunP01(g,[1.,0.],N,false)(0);
    @test V ≈ triNoKernelDirect(g,1.);
end

@testset "Volterra Integral operator testing, with kernel" begin
    N=15;
    S = Jacobi(0,1,0..1);
    g = Fun(x -> 1-sin(x)^2, S);
    # K given in monomial basis
    K = [1.,0.,2.,1.]; # = K(x,y)=1+2*x+x*y
    val=0.4;
    Kfval = Fun(y -> 1+2*val+val*y, S);
    @test triVolterraFunP01(g,K,N,false)(val) ≈ triNoKernelDirect(Kfval*g,1.0-val);
    #########################################################################
    N=15;
    S = Jacobi(0,1,0..1);
    g = Fun(x -> 1-sin(x)^2, S);
    # K given in monomial basis
    K = [2.3,0.33,2.1,1.2,7.6]; # = K(x,y)=2.3+0.33*y+2.1*x+1.2*x*y+7.6*y^2
    val=0.6;
    Kfval = Fun(y -> 2.3+0.33*y+2.1*val+1.2*val*y+7.6*y^2, S);
    @test triVolterraFunP01(g,K,N,false)(val) ≈ triNoKernelDirect(Kfval*g,1.0-val);
end


text:
@testset "JSFun-1" begin
    @jsf bar(x) = 5x+4
    @test bar isa J.JSFun
    @test bar.name == :bar
    @test bar(5) == 5*5+4
    @test J.str(bar) == "function bar(x){return ((5*x)+4)};"

    @jsf function foo(x)
        x^2+2
    end
    @test foo(3) == 3^2+2
    @test J.str(foo) == "function foo(x){return (Math.pow(x,2)+2)};"

    # Show
    io = IOBuffer()
    Base.show(io, bar)
    @test String(take!(io)) == "JSFun: bar\n"
end

@testset "strf" begin
    @jsf foo(x) = 5x+π
    fs, fss, p = J.strf(foo, "FA")
    @test fs == "function foo(x){return ((5*x)+π)};"
    @test fss isa J.JSExpr.JSString
    @test fss.s == "function(t){return INSERT_FA(t);}"
    @test p == ("INSERT_FA" => "foo")
    @test replace(fss.s, p) == "function(t){return foo(t);}"

    @test J.strf(2) == ("", J.JSExpr.JSString("2"), nothing)
end

@testset "replacefn" begin
    @jsf foo(x) = 5x+3
    fs, fss, p = J.strf(foo, "FA")
    jss = js".create('functiongraph', [$fss, 0, 1])"
    @test J.replacefn(jss.s, p) ==
        ".create('functiongraph', [function(t){return foo(t);}, 0, 1])"
    @jsf bar(x) = x^2
    gs, gss, p2 = J.strf(bar, "FB")
    jss = js".create('functiongraph', [$fss, $gss, 0, 1])"
    @test J.replacefn(jss.s, p, p2) ==
        ".create('functiongraph', [function(t){return foo(t);}, function(t){return bar(t);}, 0, 1])"
end

@testset "isdef" begin
    b = board("b")
    b ++ (@jsf foo(x) = 5x)
    # it's already defined
    @test str(foo, b) == ""
    # it will be on the board
    @test occursin("function foo(x){return (5*x)};", str(b, preamble=false))
end


text:
using Mongo, LibBSON

mutable struct LedgerEngine
    client::Mongo.MongoClient
end

function LedgerEngine(hostname::String)
    client = MongoClient(hostname)
    return LedgerEngine(client)
end

function run(engine::LedgerEngine, projname::String, modelname::String, f, configf, headers)
    ccall(:jl_exit_on_sigint, Void, (Cint,), 0)
    start_time = now()
    try
        exp_log, res = f(;configf...)
        stop_time = now()

        convertedconfig = Dict()
        for k in keys(configf); convertedconfig[String(k)] = configf[k]; end;
        rec = Dict()
        rec["project_name"] = projname
        rec["model_name"] = modelname
        rec["config"] = convertedconfig
        rec["experiment_headers"] = headers
        rec["experiment_log"] = exp_log
        rec["result"] = res
        rec["start_time"] = start_time
        rec["stop_time"] = stop_time
        rec["status"] = "completed"
        runs = MongoCollection(engine.client, "ledger", "runs")
        insert(runs, rec)
    catch ex
        if isa(ex, InterruptException)
            stop_time = now()
            convertedconfig = Dict()
            for k in keys(configf); convertedconfig[String(k)] = configf[k]; end;
            rec = Dict()
            rec["project_name"] = projname
            rec["model_name"] = modelname
            rec["config"] = convertedconfig
            rec["experiment_headers"] = headers
            rec["start_time"] = start_time
            rec["stop_time"] = stop_time
            rec["status"] = "interrupted"
            runs = MongoCollection(engine.client, "ledger", "runs")
            insert(runs, rec)
        end
    end
end


text:
# manual fixes

const TRUE = true #fix for first line in libdwf_common.jl


text:
module JuliaFormatter

using CSTParser
using Tokenize
using DataStructures
using Pkg.TOML: parsefile
using Documenter.DocTests: repl_splitter
import CommonMark: block_modifier
using CommonMark:
    AdmonitionRule,
    CodeBlock,
    enable!,
    FootnoteRule,
    markdown,
    MathRule,
    Parser,
    Rule,
    TableRule

export format, format_text, format_file, format_md, DefaultStyle, YASStyle, BlueStyle

abstract type AbstractStyle end

@inline options(s::AbstractStyle) = NamedTuple()

"""
    DefaultStyle

The default formatting style. See the [Style](@ref) section of the documentation
for more details.

See also: [`BlueStyle`](@ref), [`YASStyle`](@ref)
"""
struct DefaultStyle <: AbstractStyle
    innerstyle::Union{Nothing,AbstractStyle}
end
DefaultStyle() = DefaultStyle(nothing)

@inline getstyle(s::DefaultStyle) = s.innerstyle === nothing ? s : s.innerstyle
function options(s::DefaultStyle)
    return (;
        indent = 4,
        margin = 92,
        always_for_in = false,
        whitespace_typedefs = false,
        whitespace_ops_in_indices = false,
        remove_extra_newlines = false,
        import_to_using = false,
        pipe_to_function_call = false,
        short_to_long_function_def = false,
        always_use_return = false,
        whitespace_in_kwargs = true,
        annotate_untyped_fields_with_any = true,
        format_docstrings = false,
        align_struct_field = false,
        align_assignment = false,
        align_conditional = false,
        align_pair_arrow = false,
        conditional_to_if = false,
    )
end

include("document.jl")
include("options.jl")
include("state.jl")
include("fst.jl")
include("passes.jl")
include("align.jl")
include("nest_utils.jl")

include("styles/default/pretty.jl")
include("styles/default/nest.jl")
include("styles/yas/pretty.jl")
include("styles/yas/nest.jl")
include("styles/blue/pretty.jl")
include("styles/blue/nest.jl")

include("print.jl")

include("markdown.jl")

# on Windows lines can end in "\r\n"
normalize_line_ending(s::AbstractString) = replace(s, "\r\n" => "\n")

"""
    format_text(
        text::AbstractString;
        style::AbstractStyle = DefaultStyle(),
        indent::Int = 4,
        margin::Int = 92,
        always_for_in::Bool = false,
        whitespace_typedefs::Bool = false,
        whitespace_ops_in_indices::Bool = false,
        remove_extra_newlines::Bool = false,
        import_to_using::Bool = false,
        pipe_to_function_call::Bool = false,
        short_to_long_function_def::Bool = false,
        always_use_return::Bool = false,
        whitespace_in_kwargs::Bool = true,
        annotate_untyped_fields_with_any::Bool = true,
        format_docstrings::Bool = false,
        align_struct_field::Bool = false,
        align_conditional::Bool = false,
        align_assignment::Bool = false,
        align_pair_arrow::Bool = false,
        conditional_to_if = false,
    )::String

Formats a Julia source passed in as a string, returning the formatted
code as another string.

## Formatting Options

### `indent`

The number of spaces used for an indentation.

### `margin`

The maximum length of a line. Code exceeding this margin will
be formatted across multiple lines.

### `always_for_in`

If true, `=` is always replaced with `in` if part of a `for` loop condition.
For example, `for i = 1:10` will be transformed to `for i in 1:10`.

### `whitespace_typedefs`

If true, whitespace is added for type definitions. Make this `true`
if you prefer `Union{A <: B, C}` to `Union{A<:B,C}`.

### `whitespace_ops_in_indices`

If true, whitespace is added for binary operations in indices. Make this
`true` if you prefer `arr[a + b]` to `arr[a+b]`. Additionally, if there's
a colon `:` involved, parenthesis will be added to the LHS and RHS.

Example: `arr[(i1 + i2):(i3 + i4)]` instead of `arr[i1+i2:i3+i4]`.

### `remove_extra_newlines`

If true, superflous newlines will be removed. For example:

```julia
module M

a = 1

function foo()

    return nothing

end

b = 2

end
```

is rewritten as

```julia
module M

a = 1

function foo()
    return nothing
end

b = 2

end
```

Modules are the only type of code block allowed to keep a single newline
prior to the intial or after the final piece of code.

### `import_to_using`

If true, `import` expressions are rewritten to `using` expressions
in the following cases:

```julia
import A

import A, B, C
```

is rewritten to:

```julia
using A: A

using A: A
using B: B
using C: C
```

### `pipe_to_function_call`

If true, `x |> f` is rewritten to `f(x)`.

### `short_to_long_function_def`

Transforms a *short* function definition

```julia
f(arg1, arg2) = body
```

to a *long* function definition

```julia
function f(arg2, arg2)
    body
end
```

### `always_use_return`

If true, `return` will be prepended to the last expression where
applicable in function definitions, macro definitions, and do blocks.

Example:

```julia
function foo()
    expr1
    expr2
end
```

to

```julia
function foo()
    expr1
    return expr2
end
```

### `whitespace_in_kwargs`

If true, `=` in keyword arguments will be surrounded by whitespace.

```julia
f(; a=4)
```

to

```julia
f(; a = 4)
```

An exception to this is if the LHS ends with "!" then even if `whitespace_in_kwargs` is
false, `=` will still be surrounded by whitespace. The logic behind this intervention being
on the following parse the `!` will be treated as part of `=`, as in a "not equal" binary
operation. This would change the semantics of the code and is therefore disallowed.

### `annotate_untyped_fields_with_any`

Annotates fields in a type definitions with `::Any` if no type annotation is provided:

```julia
struct A
    arg1
end
```

to

```julia
struct A
    arg1::Any
end
```

### `format_docstrings`

Format code docstrings with the same options used for the code source.

Markdown is formatted with [`CommonMark`](https://github.com/MichaelHatherly/CommonMark.jl) alongside Julia code.

### `align_*`

See `Custom Alignment` documentation.

### `conditional_to_if`

If the conditional `E ? A : B` exceeds the maximum margin converts it into the equivalent `if` block:

```julia
if E
    A
else
    B
end
```
"""
function format_text(text::AbstractString; style::AbstractStyle = DefaultStyle(), kwargs...)
    return format_text(text, style; kwargs...)
end

function format_text(text::AbstractString, style::AbstractStyle; kwargs...)
    isempty(text) && return text
    opts = Options(; merge(options(style), kwargs)...)
    return format_text(text, style, opts)
end

function format_text(text::AbstractString, style::AbstractStyle, opts::Options)
    cst, ps = CSTParser.parse(CSTParser.ParseState(text), true)
    line, offset = ps.lt.endpos
    ps.errored && error("Parsing error for input occurred on line $line, offset: $offset")
    cst.args[1].kind === Tokens.NOTHING && length(cst) == 1 && return text
    return format_text(cst, style, State(Document(text), opts))
end

function format_text(cst::CSTParser.EXPR, style::AbstractStyle, s::State)
    t = pretty(style, cst, s)
    hascomment(s.doc, t.endline) && (add_node!(t, InlineComment(t.endline), s))

    s.opts.pipe_to_function_call && pipe_to_function_call_pass!(t)

    flatten_fst!(t)

    if s.opts.align_struct_field ||
       s.opts.align_conditional ||
       s.opts.align_assignment ||
       s.opts.align_pair_arrow
        align_fst!(t, s.opts)
    end

    nest!(style, t, s)

    s.line_offset = 0
    io = IOBuffer()

    # Print comments and whitespace before code.
    if t.startline > 1
        format_check(io, Notcode(1, t.startline - 1), s)
        print_leaf(io, Newline(), s)
    end

    print_tree(io, t, s)

    if t.endline < length(s.doc.range_to_line)
        print_leaf(io, Newline(), s)
        format_check(io, Notcode(t.endline + 1, length(s.doc.range_to_line)), s)
    end

    text = String(take!(io))
    text = normalize_line_ending(text)

    _, ps = CSTParser.parse(CSTParser.ParseState(text), true)
    line, offset = ps.lt.endpos
    ps.errored &&
        error("Parsing error for formatted text:\n\n$text\n\n Error occured on line $line, offset $offset.")
    return text
end

"""
    format_file(
        filename::AbstractString;
        overwrite::Bool = true,
        verbose::Bool = false,
        format_markdown::Bool = false,
        format_options...,
    )::Bool

Formats the contents of `filename` assuming it's a `.jl` or `.md` file. If it's a
`.md` file, Julia code blocks will be formatted in addition to the markdown being
normalized.

### File Options

If `overwrite` is `true` the file will be reformatted in place, overwriting
the existing file; if it is `false`, the formatted version of `foo.jl` will
be written to `foo_fmt.jl` instead.

If `verbose` is `true` details related to formatting the file will be printed
to `stdout`.

If `format_markdown` is true, `.md` files are formatted.

### Formatting Options

See [`format_text`](@ref) for description of formatting options.

### Output

Returns a boolean indicating whether the file was already formatted (`true`)
or not (`false`).
"""
function format_file(
    filename::AbstractString;
    overwrite::Bool = true,
    verbose::Bool = false,
    format_markdown::Bool = false,
    format_options...,
)::Bool
    path, ext = splitext(filename)
    shebang_pattern = r"^#!\s*/.*\bjulia[0-9.-]*\b"
    formatted_str = if ext == ".md"
        format_markdown || return true
        verbose && println("Formatting $filename")
        str = String(read(filename))
        format_md(str; format_options...)
    elseif ext == ".jl" || match(shebang_pattern, readline(filename)) !== nothing
        verbose && println("Formatting $filename")
        str = String(read(filename))
        format_text(str; format_options...)
    else
        error("$filename must be a Julia (.jl) or Markdown (.md) source file")
    end
    formatted_str = replace(formatted_str, r"\n*$" => "\n")

    if overwrite
        write(filename, formatted_str)
    else
        write(path * "_fmt" * ext, formatted_str)
    end
    return formatted_str == str
end

"""
    format_file(filename::AbstractString, style::AbstractStyle; kwargs...)::Bool
"""
function format_file(filename::AbstractString, style::AbstractStyle; kwargs...)
    return format_file(filename; style = style, kwargs...)
end

if VERSION < v"1.1.0"
    # We define `splitpath` here, copying the definition from base/path.jl
    # because it was only added in Julia 1.1.

    # TODO(odow): remove this definition of splitpath once JuliaFormatter no
    # longer supports Julia 1.0.
    _splitdir_nodrive(path::String) = _splitdir_nodrive("", path)
    function _splitdir_nodrive(a::String, b::String)
        path_dir_splitter = if Sys.isunix()
            r"^(.*?)(/+)([^/]*)$"
        elseif Sys.iswindows()
            r"^(.*?)([/\\]+)([^/\\]*)$"
        else
            error("JuliaFormatter.jl does not work on this OS.")
        end
        m = match(path_dir_splitter, b)
        m === nothing && return (a, b)
        a = string(a, isempty(m.captures[1]) ? m.captures[2][1] : m.captures[1])
        a, String(m.captures[3])
    end
    splitpath(p::AbstractString) = splitpath(String(p))
    function splitpath(p::String)
        drive, p = splitdrive(p)
        out = String[]
        isempty(p) && (pushfirst!(out, p))  # "" means the current directory.
        while !isempty(p)
            dir, base = _splitdir_nodrive(p)
            dir == p && (pushfirst!(out, dir); break)  # Reached root node.
            if !isempty(base)  # Skip trailing '/' in basename
                pushfirst!(out, base)
            end
            p = dir
        end
        if !isempty(drive)  # Tack the drive back on to the first element.
            out[1] = drive * out[1]  # Note that length(out) is always >= 1.
        end
        return out
    end
end

const CONFIG_FILE_NAME = ".JuliaFormatter.toml"

"""
    format(
        paths; # a path or collection of paths
        options...,
    )::Bool

Recursively descend into files and directories, formatting any `.jl`
files by calling `format_file` on them.

See [`format_file`](@ref) and [`format_text`](@ref) for a description of the options.

This function will look for `.JuliaFormatter.toml` in the location of the file being
formatted, and searching *up* the file tree until a config file is (or isn't) found.
When found, the configurations in the file will overwrite the given `options`.
See [Configuration File](@ref) for more details.

### Output

Returns a boolean indicating whether the file was already formatted (`true`)
or not (`false`).
"""
function format(paths; options...)::Bool
    dir2config = Dict{String,Any}()
    already_formatted = true
    function find_config_file(dir)
        next_dir = dirname(dir)
        config = if (next_dir == dir || # ensure to escape infinite recursion
                     isempty(dir)) # reached to the system root
            nothing
        elseif haskey(dir2config, dir)
            dir2config[dir]
        else
            path = joinpath(dir, CONFIG_FILE_NAME)
            isfile(path) ? parse_config(path) : find_config_file(next_dir)
        end
        return dir2config[dir] = config
    end

    for path in paths
        already_formatted &= if isfile(path)
            dir = dirname(realpath(path))
            opts = if (config = find_config_file(dir)) !== nothing
                overwrite_options(options, config)
            else
                options
            end
            format_file(path; opts...)
        else
            reduce(walkdir(path), init = true) do formatted_path, dir_branch
                root, dirs, files = dir_branch
                formatted_path & reduce(files, init = true) do formatted_file, file
                    _, ext = splitext(file)
                    full_path = joinpath(root, file)
                    formatted_file &
                    if ext in (".jl", ".md") && !(".git" in splitpath(full_path))
                        dir = realpath(root)
                        opts = if (config = find_config_file(dir)) !== nothing
                            overwrite_options(options, config)
                        else
                            options
                        end
                        format_file(full_path; opts...)
                    else
                        true
                    end
                end
            end
        end
    end
    return already_formatted
end
format(path::AbstractString; options...) = format((path,); options...)

"""
    format(path, style::AbstractStyle; options...)::Bool
"""
format(path, style::AbstractStyle; options...) = format(path; style = style, options...)

function kwargs(dict)
    ns = (Symbol.(keys(dict))...,)
    vs = (collect(values(dict))...,)
    return pairs(NamedTuple{ns}(vs))
end

function parse_config(tomlfile)
    config_dict = parsefile(tomlfile)
    if (style = get(config_dict, "style", nothing)) !== nothing
        @assert (style == "default" || style == "yas" || style == "blue") "currently $(CONFIG_FILE_NAME) accepts only \"default\" or \"yas\" or \"blue\" for the style configuration"
        config_dict["style"] = if (style == "yas" && @isdefined(YASStyle))
            YASStyle()
        elseif (style == "blue" && @isdefined(BlueStyle))
            BlueStyle()
        else
            DefaultStyle()
        end
    end
    return kwargs(config_dict)
end

overwrite_options(options, config) = kwargs(merge(options, config))

end


text:
using Test
using ColorTypes: RGB, Gray, N0f8
using FileIO, ImageCore, Dates, Statistics
using Statistics, StatsBase

import VideoIO

createmode = false

testdir = dirname(@__FILE__)
videodir = joinpath(testdir, "..", "videos")

VideoIO.TestVideos.available()
VideoIO.TestVideos.download_all()

swapext(f, new_ext) = "$(splitext(f)[1])$new_ext"

isarm() = Base.Sys.ARCH in (:arm,:arm32,:arm7l,:armv7l,:arm8l,:armv8l,:aarch64,:arm64)

#@show Base.Sys.ARCH

@noinline function isblank(img)
    all(c->green(c) == 0, img) || all(c->blue(c) == 0, img) || all(c->red(c) == 0, img) || maximum(rawview(channelview(img))) < 0xcf
end

# Helper functions
function test_compare_frames(test_frame, ref_frame)
    if isarm()
        @test_skip test_frame == ref_frame
    else
        @test test_frame == ref_frame
    end
end

# uses read!
function get_first_frame!(img, v)
    seekstart(v)
    read!(v, img)
    while isblank(img)
        read!(v, img)
    end
end

@testset "Reading of various example file formats" begin
    for name in VideoIO.TestVideos.names()
        @testset "Reading $name" begin
            first_frame_file = joinpath(testdir, swapext(name, ".png"))
            !createmode && (first_frame = load(first_frame_file))

            f = VideoIO.testvideo(name)
            v = VideoIO.openvideo(f)

            time_seconds = VideoIO.gettime(v)
            @test time_seconds == 0

            if !createmode && (size(first_frame, 1) > v.height)
                first_frame = first_frame[1+size(first_frame,1)-v.height:end,:]
            end

            # Find the first non-trivial image
            img = read(v)
            i=1
            while isblank(img)
                read!(v, img)
                i += 1
            end
            # println("$name vs. $first_frame_file - First non-blank frame: $i") # for debugging
            if createmode
                save(first_frame_file,img)
            else
                test_compare_frames(img, first_frame)
            end

            for i in 1:50
                read!(v,img)
            end
            fiftieth_frame = img
            timebase = v.avin.video_info[1].stream.time_base
            tstamp = v.aVideoFrame[1].pkt_dts
            video_tstamp = v.avin.video_info[1].stream.first_dts
            fiftytime = (tstamp-video_tstamp)/(convert(Float64,timebase.den)/convert(Float64,timebase.num))

            while !eof(v)
                read!(v, img)
            end

            seek(v,float(fiftytime))
            read!(v,img)

            @test img == fiftieth_frame

            # read first frames again, and compare
            get_first_frame!(img, v)
            createmode || test_compare_frames(img, first_frame)

            # make sure read! works with both PermutedDimsArray and Array
            # The above tests already use read! for PermutedDimsArray, so just test the type of img
            @test typeof(img) <: PermutedDimsArray

            img_p = parent(img)
            @assert typeof(img_p) <: Array
            # img is a view of img_p, so calling read! on img_p should alter img
            #
            # first, zero img out to be sure we get the desired result from calls to read on img_p!
            fill!(img, zero(eltype(img)))
            # Then get the first frame, which uses read!
            get_first_frame!(img_p, v)
            # Finally compare the result to make sure it's right
            createmode || test_compare_frames(img, first_frame)

            # Skipping & frame counting
            VideoIO.seekstart(v)
            VideoIO.skipframe(v)
            VideoIO.skipframes(v, 10)
            @test VideoIO.counttotalframes(v) == VideoIO.TestVideos.videofiles[name].numframes

            close(v)
        end
    end
end

@testset "IO reading of various example file formats" begin
    for name in VideoIO.TestVideos.names()
        # TODO: fix me?
        (startswith(name, "ladybird") || startswith(name, "NPS")) && continue
        @testset "Testing $name" begin
            first_frame_file = joinpath(testdir, swapext(name, ".png"))
            first_frame = load(first_frame_file)

            filename = joinpath(videodir, name)
            v = VideoIO.openvideo(VideoIO.open(filename))

            if size(first_frame, 1) > v.height
                first_frame = first_frame[1+size(first_frame,1)-v.height:end,:]
            end
            img = read(v)
            # Find the first non-trivial image
            while isblank(img)
                read!(v, img)
            end

            if isarm()
                @test_skip img == first_frame
            else
                @test img == first_frame
            end
            while !eof(v)
                read!(v, img)
            end

            # Iterator interface
            VT = typeof(v)
            @test Base.IteratorSize(VT) === Base.SizeUnknown()
            @test Base.IteratorEltype(VT) === Base.EltypeUnknown()

            VideoIO.seekstart(v)
            i = 0
            local first_frame
            local last_frame
            for frame in v
                i += 1
                if i == 1
                    first_frame = frame
                end
                last_frame = frame
            end
            @test i == VideoIO.TestVideos.videofiles[name].numframes
            # test that the frames returned by the iterator have distinct storage
            if i > 1
                @test first_frame !== last_frame
            end

            ## Test that iterator is mutable, and continues where iteration last
            ## stopped.
            @test iterate(v) === nothing
        end
    end

    VideoIO.testvideo("ladybird") # coverage testing
    @test_throws ErrorException VideoIO.testvideo("rickroll")
    @test_throws ErrorException VideoIO.testvideo("")
end

@testset "Reading video metadata" begin
    @testset "Reading Storage Aspect Ratio: SAR" begin
        # currently, the SAR of all the test videos is 1, we should get another video with a valid SAR that is not equal to 1
        vids = Dict("ladybird.mp4" => 1, "black_hole.webm" => 1, "crescent-moon.ogv" => 1, "annie_oakley.ogg" => 1)
        @test all(VideoIO.aspect_ratio(VideoIO.openvideo(joinpath(videodir, k))) == v for (k,v) in vids)
    end
    @testset "Reading video duration, start date, and duration" begin
        # tesing the duration and date & time functions:
        file = joinpath(videodir, "annie_oakley.ogg")
        @test VideoIO.get_duration(file) == 24224200/1e6
        @test VideoIO.get_start_time(file) == DateTime(1970, 1, 1)
        @test VideoIO.get_time_duration(file) == (DateTime(1970, 1, 1), 24224200/1e6)
        @test VideoIO.get_number_frames(file) === nothing
    end
    @testset "Reading the number of frames from container" begin
        file = joinpath(videodir, "ladybird.mp4")
        @test VideoIO.get_number_frames(file) == 398
        @test VideoIO.get_number_frames(file, 0) == 398
        @test_throws ArgumentError VideoIO.get_number_frames(file, -1)
        @test_throws ErrorException VideoIO.get_number_frames("Not_a_file")
    end
end

@testset "Encoding video across all supported colortypes" begin
    for el in [UInt8, RGB{N0f8}]
        @testset "Encoding $el imagestack" begin
            n = 100
            imgstack = map(x->rand(el,100,100),1:n)
            props = [:priv_data => ("crf"=>"22","preset"=>"medium")]
            encodedvideopath = VideoIO.encodevideo("testvideo.mp4",imgstack,framerate=30,AVCodecContextProperties=props, silent=true)
            @test stat(encodedvideopath).size > 100
            f = VideoIO.openvideo(encodedvideopath)
            @test VideoIO.counttotalframes(f) == n-4 # videos encoded with crf > 0 have 4 fewer frames
            close(f)
            rm(encodedvideopath)
        end
    end
end

@testset "Encoding video with rational frame rates" begin
    n = 100
    fr = 59 // 2 # 29.5
    target_dur = 3.39
    @testset "Encoding with frame rate $(float(fr))" begin
        imgstack = map(x->rand(UInt8,100,100),1:n)
        props = [:priv_data => ("crf"=>"22","preset"=>"medium")]
        encodedvideopath = VideoIO.encodevideo("testvideo.mp4",imgstack,
                                               framerate=fr,
                                               AVCodecContextProperties=props,
                                               silent=true)
        @test stat(encodedvideopath).size > 100
        measured_dur_str = VideoIO.FFMPEG.exe(`-v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $(encodedvideopath)`, command = VideoIO.FFMPEG.ffprobe, collect = true)
        @test parse(Float64, measured_dur_str[1]) == target_dur
        rm(encodedvideopath)
    end
end
@testset "Encoding video with float frame rates" begin
    n = 100
    fr = 29.5 # 59 // 2
    target_dur = 3.39
    @testset "Encoding with frame rate $(float(fr))" begin
        imgstack = map(x->rand(UInt8,100,100),1:n)
        props = [:priv_data => ("crf"=>"22","preset"=>"medium")]
        encodedvideopath = VideoIO.encodevideo("testvideo.mp4",imgstack,
                                               framerate=fr,
                                               AVCodecContextProperties=props,
                                               silent=true)
        @test stat(encodedvideopath).size > 100
        measured_dur_str = VideoIO.FFMPEG.exe(`-v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $(encodedvideopath)`, command = VideoIO.FFMPEG.ffprobe, collect = true)
        @test parse(Float64, measured_dur_str[1]) == target_dur
        rm(encodedvideopath)
    end
end

@testset "Video encode/decode accuracy (read, encode, read, compare)" begin
    file = joinpath(videodir, "annie_oakley.ogg")
    f = VideoIO.openvideo(file)
    imgstack_rgb = []
    imgstack_gray = []
    while !eof(f)
        img = collect(read(f))
        img_gray = convert(Array{Gray{N0f8}},img)
        push!(imgstack_rgb,img)
        push!(imgstack_gray,img_gray)
    end
    @testset "Lossless Grayscale encoding" begin
        file_lossless_gray_copy = joinpath(videodir, "annie_oakley_lossless_gray.mp4")
        prop = [:color_range=>2, :priv_data => ("crf"=>"0","preset"=>"medium")]
        codec_name="libx264"
        VideoIO.encodevideo(file_lossless_gray_copy,imgstack_gray,codec_name=codec_name,AVCodecContextProperties=prop, silent=true)

        fcopy = VideoIO.openvideo(file_lossless_gray_copy,target_format=VideoIO.AV_PIX_FMT_GRAY8)
        imgstack_gray_copy = []
        while !eof(fcopy)
            push!(imgstack_gray_copy,collect(read(fcopy)))
        end
        close(f)
        @test eltype(imgstack_gray) == eltype(imgstack_gray_copy)
        @test length(imgstack_gray) == length(imgstack_gray_copy)
        @test size(imgstack_gray[1]) == size(imgstack_gray_copy[1])
        @test !any(.!(imgstack_gray .== imgstack_gray_copy))
    end

    @testset "Lossless RGB encoding" begin
        file_lossless_rgb_copy = joinpath(videodir, "annie_oakley_lossless_rgb.mp4")
        prop = [:priv_data => ("crf"=>"0","preset"=>"medium")]
        codec_name="libx264rgb"
        VideoIO.encodevideo(file_lossless_rgb_copy,imgstack_rgb,codec_name=codec_name,AVCodecContextProperties=prop, silent=true)

        fcopy = VideoIO.openvideo(file_lossless_rgb_copy)
        imgstack_rgb_copy = []
        while !eof(fcopy)
            img = collect(read(fcopy))
            push!(imgstack_rgb_copy,img)
        end
        close(f)
        @test eltype(imgstack_rgb) == eltype(imgstack_rgb_copy)
        @test length(imgstack_rgb) == length(imgstack_rgb_copy)
        @test size(imgstack_rgb[1]) == size(imgstack_rgb_copy[1])
        @test !any(.!(imgstack_rgb .== imgstack_rgb_copy))
    end

    @testset "UInt8 accuracy during read & lossless encode" begin
        # Test that reading truth video has one of each UInt8 value pixels (16x16 frames = 256 pixels)
        f = VideoIO.openvideo(joinpath(testdir,"precisiontest_gray_truth.mp4"),target_format=VideoIO.AV_PIX_FMT_GRAY8)
        frame_truth = collect(rawview(channelview(read(f))))
        h_truth = fit(Histogram, frame_truth[:], 0:256)
        @test h_truth.weights == fill(1,256) #Test that reading is precise

        # Test that encoding new test video has one of each UInt8 value pixels (16x16 frames = 256 pixels)
        img = Array{UInt8}(undef,16,16)
        for i in 1:256
            img[i] = UInt8(i-1)
        end
        imgstack = []
        for i=1:24
            push!(imgstack,img)
        end
        props = [:color_range=>2, :priv_data => ("crf"=>"0","preset"=>"medium")]
        VideoIO.encodevideo(joinpath(testdir,"precisiontest_gray_test.mp4"), imgstack,
            AVCodecContextProperties = props,silent=true)
        f = VideoIO.openvideo(joinpath(testdir,"precisiontest_gray_test.mp4"),
            target_format=VideoIO.AV_PIX_FMT_GRAY8)
        frame_test = collect(rawview(channelview(read(f))))
        h_test = fit(Histogram, frame_test[:], 0:256)
        @test h_test.weights == fill(1,256) #Test that encoding is precise (if above passes)

        @test VideoIO.counttotalframes(f) == 24
    end

    @testset "Correct frame order when reading & encoding" begin
        @testset "Frame order when reading ground truth video" begin
            # Test that reading a video with frame-incremental pixel values is read in in-order
            f = VideoIO.openvideo(joinpath(testdir,"ordertest_gray_truth.mp4"),target_format=VideoIO.AV_PIX_FMT_GRAY8)
            frame_ids_truth = []
            while !eof(f)
                img = collect(rawview(channelview(read(f))))
                push!(frame_ids_truth,img[1,1])
            end
            @test frame_ids_truth == collect(0:255) #Test that reading is in correct frame order
            @test VideoIO.counttotalframes(f) == 256
        end
        @testset "Frame order when encoding, then reading video" begin
            # Test that writing and reading a video with frame-incremental pixel values is read in in-order
            imgstack = []
            img = Array{UInt8}(undef,16,16)
            for i in 0:255
                push!(imgstack,fill(UInt8(i),(16,16)))
            end
            props = [:color_range=>2, :priv_data => ("crf"=>"0","preset"=>"medium")]
            VideoIO.encodevideo(joinpath(testdir,"ordertest_gray_test.mp4"), imgstack,
                AVCodecContextProperties = props,silent=true)
            f = VideoIO.openvideo(joinpath(testdir,"ordertest_gray_test.mp4"),
                target_format=VideoIO.AV_PIX_FMT_GRAY8)
            frame_ids_test = []
            while !eof(f)
                img = collect(rawview(channelview(read(f))))
                push!(frame_ids_test,img[1,1])
            end
            @test frame_ids_test == collect(0:255) #Test that reading is in correct frame order
            @test VideoIO.counttotalframes(f) == 256
        end
    end
end

@testset "c api memory leak test" begin # Issue https://github.com/JuliaIO/VideoIO.jl/issues/246

    if(Sys.islinux())  # TODO: find a method to get cross platform memory usage, see: https://discourse.julialang.org/t/how-to-get-current-julia-process-memory-usage/41734/4

        function get_memory_usage()
            open("/proc/$(getpid())/statm") do io
                split(read(io, String))[1]
            end
        end

        file = joinpath(videodir, "annie_oakley.ogg")

        @testset "open file test" begin
            check_size = 10
            usage_vec = Vector{String}(undef, check_size)

            for i in 1:check_size

                f = VideoIO.openvideo(file)
                close(f)
                GC.gc()

                usage_vec[i] = get_memory_usage()
            end

            println(usage_vec)

            @test usage_vec[end-1] == usage_vec[end]
        end

        @testset "open and read file test" begin
            check_size = 10
            usage_vec = Vector{String}(undef, check_size)

            for i in 1:check_size

                f = VideoIO.openvideo(file)
                img = read(f)
                close(f)
                GC.gc()

                usage_vec[i] = get_memory_usage()
            end

            println(usage_vec)

            @test usage_vec[end-1] == usage_vec[end]
        end
    end
end

#VideoIO.TestVideos.remove_all()


text:
using Pivots
using FactCheck

max_stars_repo_name: cpritcha/Pivots

max_issues_repo_name: cpritcha/Pivots

max_forks_repo_name: cpritcha/Pivots


text:
# using Pkg; for p in ("Knet","ArgParse"); haskey(Pkg.installed(),p) || Pkg.add(p); end

"""

LinReg is a simple linear regression example using artificially
generated data. You can run the demo using `julia linreg.jl` on the
command line or `julia> LinReg.main()` at the Julia prompt.  Use
`julia linreg.jl --help` or `julia> LinReg.main("--help")` for a list
of options.  The quadratic loss will be printed at every epoch and
optimized parameters will be returned.

"""
module LinReg
using Knet, CUDA, ArgParse, Random

predict(w,x)=(w*x)

loss(w,x,y)=(sum(abs2,y-predict(w,x)) / size(x,2))

lossgradient = grad(loss)

function train(w, data; lr=.02, epochs=10)
    for epoch=1:epochs
        for (x,y) in data
            g = lossgradient(w, x, y)
            w -= lr * g
        end
    end
    return w
end

function test(w, data)
    sumloss = numloss = 0
    for (x,y) in data
        sumloss += loss(w,x,y)
        numloss += 1
    end
    return sumloss/numloss
end

# Data generator:

struct LRData; w; batchsize; epochsize; noise; rng; atype; end

function LRData(outputdims,inputdims; batchsize=20, epochsize=10000, noise=.01, rng=Random.GLOBAL_RNG, atype=Array)
    LRData(convert(atype, randn(rng,outputdims,inputdims)),batchsize,epochsize,noise,rng,atype)
end

function Base.iterate(l::LRData, n=0)
    if n >= l.epochsize; return nothing; end
    (outputdims, inputdims) = size(l.w)
    x = convert(l.atype, rand(l.rng, inputdims, l.batchsize))
    y = l.w * x + convert(l.atype, l.noise * randn(l.rng, outputdims, l.batchsize))
    return ((x,y), n+l.batchsize)
end

# Main loop:

function main(args=ARGS)
    s = ArgParseSettings()
    s.description="linreg.jl (c) Deniz Yuret, 2016. Linear regression example with artificial data."
    s.exc_handler=ArgParse.debug_handler
    @add_arg_table s begin
        ("--atype"; default="$(Knet.array_type[])"; help="array type: Array for cpu, KnetArray for gpu")
        ("--batchsize"; arg_type=Int; default=20; help="number of instances in a minibatch")
        ("--epochs"; arg_type=Int; default=10; help="number of epochs for training")
        ("--epochsize"; arg_type=Int; default=10000; help="number of instances per epoch")
        ("--fast"; action=:store_true; help="skip loss printing for faster run")
        ("--inputdims"; arg_type=Int; default=100; help="input dimensions")
        ("--lr"; arg_type=Float64; default=0.02; help="learning rate")
        ("--noise"; arg_type=Float64; default=0.01; help="noise in data")
        ("--outputdims"; arg_type=Int; default=10; help="output dimensions")
        ("--seed"; arg_type=Int; default=-1; help="random number seed: use a nonnegative int for repeatable results")
        ("--gcheck"; arg_type=Int; default=0; help="check N random gradients")
    end
    isa(args, AbstractString) && (args=split(args))
    if in("--help", args) || in("-h", args)
        ArgParse.show_help(s; exit_when_done=false)
        return
    end
    o = parse_args(args,s; as_symbols=true)
    println(s.description)
    println("opts=",[(k,v) for (k,v) in o]...)
    o[:seed] > 0 && Knet.seed!(o[:seed])
    atype = eval(Meta.parse(o[:atype]))
    data = LRData(o[:outputdims], o[:inputdims]; batchsize=o[:batchsize], epochsize=o[:epochsize], noise=o[:noise], atype=atype)
    w = convert(atype, 0.1*randn(o[:outputdims], o[:inputdims]))
    println((:epoch,0,:loss,test(w,data)))
    if o[:fast]
        @time w = train(w, data; epochs=o[:epochs], lr=o[:lr])
        println((:epoch,o[:epochs],:loss,test(w,data)))
    else
        @time for epoch=1:o[:epochs]
            w = train(w, data; epochs=1, lr=o[:lr])
            println((:epoch,epoch,:loss,test(w,data)))
            if o[:gcheck] > 0
                gradcheck(loss, w, first(data)...; gcheck=o[:gcheck], verbose=true)
            end
        end
    end
    return w
end

# This allows both non-interactive (shell command) and interactive calls like:
# $ julia linreg.jl --epochs 10
# julia> LinReg.main("--epochs 10")
PROGRAM_FILE == "linreg.jl" && main(ARGS)

end

# SAMPLE RUN 65f57ff+ Wed Sep 14 10:02:30 EEST 2016
#
# linreg.jl (c) Deniz Yuret, 2016. Linear regression example with artificial data.
# opts=(:lr,0.02)(:atype,"KnetArray")(:epochsize,10000)(:inputdims,100)(:noise,0.01)(:epochs,10)(:gcheck,0)(:seed,-1)(:outputdims,10)(:batchsize,20)(:fast,true)
# (:epoch,0,:loss,349.84485494071095)
#   1.320391 seconds (1.65 M allocations: 164.061 MB, 1.85% gc time)
# (:epoch,10,:loss,0.0010327464077591123)


text:
using Weave

function hfun_bar(vname)
    val = Meta.parse(vname[1])
    return round(sqrt(val), digits = 2)
end

function hfun_m1fill(vname)
    var = vname[1]
    return pagevar("index", var)
end

function lx_baz(com, _)
    # keep this first line
    brace_content = Franklin.content(com.braces[1]) # input string
    # do whatever you want here
    return uppercase(brace_content)
end

function hfun_insert_weave(params)
    println(params)
    rpath = params[1]
    fullpath = joinpath(Franklin.path(:folder), rpath)
    (isfile(fullpath) && splitext(fullpath)[2] == ".jmd") || return ""
    print("Weaving... ")
    t = tempname()
    weave(fullpath, out_path=t)
    println("✓ [done].")
    fn = splitext(splitpath(fullpath)[end])[1]
    html = read(joinpath(t, fn * ".html"), String)
    start = findfirst("<BODY>", html)
    finish = findfirst("</BODY>", html)
    range = nextind(html, last(start)):prevind(html, first(finish))
    html = html[range]
    return html
end

max_stars_repo_name: TheCedarPrince/DataExplorers

max_issues_repo_name: TheCedarPrince/DataExplorers

max_forks_repo_name: TheCedarPrince/DataExplorers


text:
funcname(::Type{Type{T}}) where T = string(T)

function funcname(T)
  if isdefined(T, :instance)
    string(T.instance)
  else
    "λ"
  end
end

Base.show(io::IO, j::Pullback{S}) where S = print(io, "∂($(funcname(S.parameters[1])))")

Base.show(io::IO, P::Type{<:Pullback{S}}) where S<:Tuple = print(io, "typeof(∂($(funcname(S.parameters[1]))))")


text:
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

#! format:off

# This file is machine generated by running univariate_expressions_generator.jl

const SYMBOLIC_UNIVARIATE_EXPRESSIONS = Tuple{Symbol,Expr,Any}[
    (:+, :(one(x)), :(zero(x))),
    (:-, :(-one(x)), :(zero(x))),
    (:abs, :(ifelse(x >= 0, one(x), -one(x))), :(zero(x))),
    (:sqrt, :(0.5 / sqrt(x)), :((0.5 * -(0.5 / sqrt(x))) / sqrt(x) ^ 2)),
    (:cbrt, :(0.3333333333333333 / cbrt(x) ^ 2), :((0.3333333333333333 * -(2 * (0.3333333333333333 / cbrt(x) ^ 2) * cbrt(x))) / (cbrt(x) ^ 2) ^ 2)),
    (:abs2, :(2x), :((typeof(x))(2))),
    (:inv, :(-(abs2(inv(x)))), :(-(-(abs2(inv(x))) * (2 * inv(x))))),
    (:log, :(1 / x), :(-1 / x ^ 2)),
    (:log10, :((1 / x) / 2.302585092994046), :((-1 / x ^ 2) / 2.302585092994046)),
    (:log2, :((1 / x) / 0.6931471805599453), :((-1 / x ^ 2) / 0.6931471805599453)),
    (:log1p, :(1 / (1 + x)), :(-1 / (1 + x) ^ 2)),
    (:exp, :(exp(x)), :(exp(x))),
    (:exp2, :(0.6931471805599453 * exp2(x)), :(0.6931471805599453 * (0.6931471805599453 * exp2(x)))),
    (:expm1, :(exp(x)), :(exp(x))),
    (:sin, :(cos(x)), :(-(sin(x)))),
    (:cos, :(-(sin(x))), :(-(cos(x)))),
    (:tan, :(1 + tan(x) ^ 2), :(2 * (1 + tan(x) ^ 2) * tan(x))),
    (:sec, :(sec(x) * tan(x)), :((sec(x) * tan(x)) * tan(x) + sec(x) * (1 + tan(x) ^ 2))),
    (:csc, :(-(csc(x)) * cot(x)), :(-(-(csc(x)) * cot(x)) * cot(x) + -(csc(x)) * -((1 + cot(x) ^ 2)))),
    (:cot, :(-((1 + cot(x) ^ 2))), :(-(2 * -((1 + cot(x) ^ 2)) * cot(x)))),
    (:sind, :(0.017453292519943295 * cosd(x)), :(0.017453292519943295 * (-0.017453292519943295 * sind(x)))),
    (:cosd, :(-0.017453292519943295 * sind(x)), :(-0.017453292519943295 * (0.017453292519943295 * cosd(x)))),
    (:tand, :(0.017453292519943295 * (1 + tand(x) ^ 2)), :(0.017453292519943295 * (2 * (0.017453292519943295 * (1 + tand(x) ^ 2)) * tand(x)))),
    (:secd, :(0.017453292519943295 * secd(x) * tand(x)), :(0.017453292519943295 * (0.017453292519943295 * secd(x) * tand(x)) * tand(x) + 0.017453292519943295 * secd(x) * (0.017453292519943295 * (1 + tand(x) ^ 2)))),
    (:cscd, :(-0.017453292519943295 * cscd(x) * cotd(x)), :(-0.017453292519943295 * (-0.017453292519943295 * cscd(x) * cotd(x)) * cotd(x) + -0.017453292519943295 * cscd(x) * (-0.017453292519943295 * (1 + cotd(x) ^ 2)))),
    (:cotd, :(-0.017453292519943295 * (1 + cotd(x) ^ 2)), :(-0.017453292519943295 * (2 * (-0.017453292519943295 * (1 + cotd(x) ^ 2)) * cotd(x)))),
    (:asin, :(1 / sqrt(1 - x ^ 2)), :(-(-(2x) * (0.5 / sqrt(1 - x ^ 2))) / sqrt(1 - x ^ 2) ^ 2)),
    (:acos, :(-1 / sqrt(1 - x ^ 2)), :(-(-(-(2x) * (0.5 / sqrt(1 - x ^ 2)))) / sqrt(1 - x ^ 2) ^ 2)),
    (:atan, :(1 / (1 + x ^ 2)), :(-(2x) / (1 + x ^ 2) ^ 2)),
    (:asec, :((1 / abs(x)) / sqrt(x ^ 2 - 1)), :(nothing)),
    (:acsc, :((-1 / abs(x)) / sqrt(x ^ 2 - 1)), :(nothing)),
    (:acot, :(-1 / (1 + x ^ 2)), :(-(-(2x)) / (1 + x ^ 2) ^ 2)),
    (:asind, :(57.29577951308232 / sqrt(1 - x ^ 2)), :((57.29577951308232 * -(-(2x) * (0.5 / sqrt(1 - x ^ 2)))) / sqrt(1 - x ^ 2) ^ 2)),
    (:acosd, :(-57.29577951308232 / sqrt(1 - x ^ 2)), :((-57.29577951308232 * -(-(2x) * (0.5 / sqrt(1 - x ^ 2)))) / sqrt(1 - x ^ 2) ^ 2)),
    (:atand, :(57.29577951308232 / (1 + x ^ 2)), :((57.29577951308232 * -(2x)) / (1 + x ^ 2) ^ 2)),
    (:asecd, :((57.29577951308232 / abs(x)) / sqrt(x ^ 2 - 1)), :(nothing)),
    (:acscd, :((-57.29577951308232 / abs(x)) / sqrt(x ^ 2 - 1)), :(nothing)),
    (:acotd, :(-57.29577951308232 / (1 + x ^ 2)), :((-57.29577951308232 * -(2x)) / (1 + x ^ 2) ^ 2)),
    (:sinh, :(cosh(x)), :(sinh(x))),
    (:cosh, :(sinh(x)), :(cosh(x))),
    (:tanh, :(sech(x) ^ 2), :(2 * (-(tanh(x)) * sech(x)) * sech(x))),
    (:sech, :(-(tanh(x)) * sech(x)), :(-(sech(x) ^ 2) * sech(x) + -(tanh(x)) * (-(tanh(x)) * sech(x)))),
    (:csch, :(-(coth(x)) * csch(x)), :(-(-(csch(x) ^ 2)) * csch(x) + -(coth(x)) * (-(coth(x)) * csch(x)))),
    (:coth, :(-(csch(x) ^ 2)), :(-(2 * (-(coth(x)) * csch(x)) * csch(x)))),
    (:asinh, :(1 / sqrt(1 + x ^ 2)), :(-((2x) * (0.5 / sqrt(1 + x ^ 2))) / sqrt(1 + x ^ 2) ^ 2)),
    (:acosh, :(1 / sqrt(x ^ 2 - 1)), :(-((2x) * (0.5 / sqrt(x ^ 2 - 1))) / sqrt(x ^ 2 - 1) ^ 2)),
    (:atanh, :(1 / (1 - x ^ 2)), :(-(-(2x)) / (1 - x ^ 2) ^ 2)),
    (:asech, :((-1 / x) / sqrt(1 - x ^ 2)), :(((1 / x ^ 2) * sqrt(1 - x ^ 2) - (-1 / x) * (-(2x) * (0.5 / sqrt(1 - x ^ 2)))) / sqrt(1 - x ^ 2) ^ 2)),
    (:acsch, :((-1 / abs(x)) / sqrt(1 + x ^ 2)), :(nothing)),
    (:acoth, :(1 / (1 - x ^ 2)), :(-(-(2x)) / (1 - x ^ 2) ^ 2)),
    (:deg2rad, :((typeof(x))(0.017453292519943295)), :((typeof(x))(0))),
    (:rad2deg, :((typeof(x))(57.29577951308232)), :((typeof(x))(0))),
    (:erf, :((2 * exp(-x * x)) / 1.7724538509055159), :((2 * ((-x + -x) * exp(-x * x))) / 1.7724538509055159)),
    (:erfinv, :(0.8862269254527579 * exp(erfinv(x) * erfinv(x))), :(0.8862269254527579 * (((0.8862269254527579 * exp(erfinv(x) * erfinv(x))) * erfinv(x) + erfinv(x) * (0.8862269254527579 * exp(erfinv(x) * erfinv(x)))) * exp(erfinv(x) * erfinv(x))))),
    (:erfc, :((-2 * exp(-x * x)) / 1.7724538509055159), :((-2 * ((-x + -x) * exp(-x * x))) / 1.7724538509055159)),
    (:erfcinv, :(-0.8862269254527579 * exp(erfcinv(x) * erfcinv(x))), :(-0.8862269254527579 * (((-0.8862269254527579 * exp(erfcinv(x) * erfcinv(x))) * erfcinv(x) + erfcinv(x) * (-0.8862269254527579 * exp(erfcinv(x) * erfcinv(x)))) * exp(erfcinv(x) * erfcinv(x))))),
    (:erfi, :((2 * exp(x * x)) / 1.7724538509055159), :((2 * ((x + x) * exp(x * x))) / 1.7724538509055159)),
    (:gamma, :(digamma(x) * gamma(x)), :(trigamma(x) * gamma(x) + digamma(x) * (digamma(x) * gamma(x)))),
    (:lgamma, :(digamma(x)), :(trigamma(x))),
    (:digamma, :(trigamma(x)), :(polygamma(2, x))),
    (:invdigamma, :(inv(trigamma(invdigamma(x)))), :((inv(trigamma(invdigamma(x))) * polygamma(2, invdigamma(x))) * -(abs2(inv(trigamma(invdigamma(x))))))),
    (:trigamma, :(polygamma(2, x)), :(nothing)),
    (:airyai, :(airyaiprime(x)), :(x * airyai(x))),
    (:airybi, :(airybiprime(x)), :(x * airybi(x))),
    (:airyaiprime, :(x * airyai(x)), :(airyai(x) + x * airyaiprime(x))),
    (:airybiprime, :(x * airybi(x)), :(airybi(x) + x * airybiprime(x))),
    (:besselj0, :(-(besselj1(x))), :(-((besselj0(x) - besselj(2, x)) / 2))),
    (:besselj1, :((besselj0(x) - besselj(2, x)) / 2), :((-(besselj1(x)) - (besselj(1, x) - besselj(3, x)) / 2) / 2)),
    (:bessely0, :(-(bessely1(x))), :(-((bessely0(x) - bessely(2, x)) / 2))),
    (:bessely1, :((bessely0(x) - bessely(2, x)) / 2), :((-(bessely1(x)) - (bessely(1, x) - bessely(3, x)) / 2) / 2)),
    (:erfcx, :(2 * x * erfcx(x) - 1.1283791670955126), :(2 * erfcx(x) + 2 * x * (2 * x * erfcx(x) - 1.1283791670955126))),
    (:dawson, :(1 - (2x) * dawson(x)), :(-((2 * dawson(x) + (2x) * (1 - (2x) * dawson(x)))))),
]

max_stars_repo_name: JuliaOpt/MathOptInterface

max_issues_repo_name: JuliaOpt/MathOptInterface

max_forks_repo_name: JuliaOpt/MathOptInterface


text:
@inline function ExaTronKernels.daxpy(n::Int,da::Float64,
                       dx,incx::Int,
                       dy,incy::Int,
                       tx)
    if tx <= n
        @inbounds dy[tx] = dy[tx] + da*dx[tx]
    end
    @synchronize

    return
end


text:
using Logging

@debug "debug message"
@info "info message"
@warn "warn message"
@error "error message"

using LarSurf
LarSurf.__fcn_doing_nothing()


text:
function initialize(config::VsConfig, stream::Union{AbstractVsStream, Nothing} = nothing)
    ocr_instances = OrderedDict(
        config.ocr_language => create_ocr_instance(config.ocr_language)
    )
    if config.ocr_language != "eng"
        ocr_instances["eng"] = create_ocr_instance("eng")
    end
    ctx = VsContext(
        config = config,
        stream = stream,
        ocr_instances = ocr_instances,
    )
    vs_init!(ctx)
    ctx
end

function parse_frame!(ctx, frame)
    ctx.current_frame = frame
    ctx.current_scene = vs_parse_frame!(ctx, frame)
    ctx
end

function parse_stream(config::VsConfig, stream::VsStream)
    ctx = initialize(config, stream)
    process_interval = config.process_interval
    fps = VideoIO.framerate(stream)
    while !eof(stream)
        frame = read_frame(stream)
        scene = parse_frame!(ctx, frame)
        if !isnothing(scene)
            vs_update!(ctx, scene)
        end
        to_skip = fps * max(process_interval, ctx.current_time_skippable)
        skipframes(stream, Int(ceil(to_skip)), throwEOF = false)
    end
    vs_result(ctx)
end

parse_video(config::VsConfig, video_file) =
    parse_stream(
        config,
        open_video(video_file; gray = config.use_gray_image)
    )

parse_camera(config::VsConfig, device) =
    parse_stream(
        config,
        open_camera(device; gray = config.use_gray_image)
    )

function parse_frame(config, frame)
    ctx = initialize(config)
    parse_frame!(ctx, frame)
    ctx.current_scene
end

function parse_image(config, image)
    frame = VsFrame(image, 0)
    parse_frame(config, frame)
end


text:
module FastPolynomials

import Base.Math:@horner

if isdefined(:Polynomials)
   import Polynomials:polyval
end

export FastPoly, polyvalHorner, polyvalHornerFMA, @Horner, @HornerFMA

include("fastpoly.jl")

end