text:
SemiCircle(dlr, grid, type) = Sample.SemiCircle(dlr.Euv, dlr.β, dlr.isFermi, grid, type, dlr.symmetry; rtol = dlr.rtol, degree = 24, regularized = true)

@testset "GreenFunc" begin
    # @testset "Green2" begin
    #     tgrid = [0.0,1.0]
    #     sgrid = [0.0,1.0]
    #     color_n = [0.0,1.0]
    #     beta = 20.0
    #     green_simple = GreenBasic.Green2{Float64}(:freq,:mom,true,:ph,nothing,beta,color_n,tgrid,sgrid)
    #     println(green_simple.dynamic)
    # end

    @testset "Green2DLR" begin
        sgrid = [0.0, 1.0]
        color_n = [0.0]
        β = 10.0
        isFermi = true
        Euv = 1000.0

        green_freq = Green2DLR{ComplexF64}(:green, GreenFunc.IMFREQ, β, isFermi, Euv, sgrid)
        rtol = green_freq.dlrGrid.rtol
        println(green_freq.timeType)
        Gτ = SemiCircle(green_freq.dlrGrid, green_freq.dlrGrid.τ, :τ)
        Gn = SemiCircle(green_freq.dlrGrid, green_freq.dlrGrid.n, :n)
        green_dum = zeros(ComplexF64, (green_freq.color, green_freq.color, green_freq.spaceGrid.size, green_freq.timeGrid.size))
        for (ti, t) in enumerate(green_freq.timeGrid)
            for (qi, q) in enumerate(green_freq.spaceGrid)
                for (c1i, c1) in enumerate(color_n)
                    for (c2i, c2) in enumerate(color_n)
                        green_dum[c1i, c2i, qi, ti] = Gn[ti]
                    end
                end
            end
        end
        green_freq.dynamic = green_dum
        green_tau = toTau(green_freq)
        err = maximum(abs.(green_tau.dynamic[1, 1, 1, :] .- Gτ))
        printstyled("SemiCircle Fourier ωn->τ $err\n", color = :white)
        @test err < 50 * rtol

        green_freq_compare = toMatFreq(green_tau)
        err = maximum(abs.(green_freq_compare.dynamic[1, 1, 1, :] .- Gn))
        printstyled("SemiCircle Fourier τ->ωn $err\n", color = :white)
        @test err < 50 * rtol

        green_dlr = toDLR(green_freq)
        green_tau = toTau(green_dlr)
        err = maximum(abs.(green_tau.dynamic[1, 1, 1, :] .- Gτ))
        printstyled("SemiCircle Fourier ωn->dlr->τ $err\n", color = :white)
        @test err < 50 * rtol

        #test JLD2
        ############# FileIO API #################
        save("example.jld2", Dict("green" => green_freq), compress = true)
        d = load("example.jld2")
        green_read = d["green"]
        @test green_read.dynamic == green_freq.dynamic
        #deeptest(green_read, green_freq)
        rm("example.jld2")
    end

    @testset "find" begin
        sgrid = [0.0, 1.0]
        color_n = [0.0]
        β = 10.0
        isFermi = true
        Euv = 1000.0

        green_linear = Green2DLR{Float64}(:green, GreenFunc.IMTIME, β, isFermi, Euv, sgrid)
        rtol = green_linear.dlrGrid.rtol
        green_dum = zeros(Float64, (green_linear.color, green_linear.color, green_linear.spaceGrid.size, green_linear.timeGrid.size))
        for (ti, t) in enumerate(green_linear.timeGrid)
            for (qi, q) in enumerate(green_linear.spaceGrid)
                for (c1i, c1) in enumerate(color_n)
                    for (c2i, c2) in enumerate(color_n)
                        green_dum[c1i, c2i, qi, ti] = t * q
                    end
                end
            end
        end

        green_linear.dynamic = green_dum
        green_dum_ins = zeros(Float64, (green_linear.color, green_linear.color, green_linear.spaceGrid.size))
        for (qi, q) in enumerate(green_linear.spaceGrid)
            for (c1i, c1) in enumerate(color_n)
                for (c2i, c2) in enumerate(color_n)
                    green_dum_ins[c1i, c2i, qi] = q
                end
            end
        end

        green_linear.instant = green_dum_ins

        τ = 0.5
        x = 0.3
        interp_dym = dynamic(green_linear, τ, x, 1, 1)
        @test interp_dym - τ * x < 1e-8
        interp_ins = instant(green_linear, x, 1, 1)
        @test interp_ins - x < 1e-8
        interp_ins = dynamic(green_linear, τ, x, 1, 1, GreenFunc.DEFAULTINTERP, GreenFunc.DEFAULTINTERP)
        @test interp_ins - x < 1e-8
        interp_ins = dynamic(green_linear, τ, x, 1, 1, GreenFunc.DLRINTERP, GreenFunc.DEFAULTINTERP)
        @test interp_ins - x < 1e-8
    end
end


text:
import LinearAlgebra
using ColorTypes, FixedPointNumbers

abstract type AbstractRgbdData end

struct RGBDFrame
    id::Int64
    image::Array{RGB{Normed{UInt8,8}},2}
    depth::Array{UInt16,2}
    pose::Matrix{Float32}
end

function RGBDFrame(rgb::Array{RGB{Normed{UInt8,8}},2}, depth::Array{UInt16,2})
    # depthf = convert(Array{Float32}, depth)
    RGBDFrame(1, copy(rgb), depth, Matrix{Float32}(LinearAlgebra.I,4,4))
end

ext: jl


text:
using BlackBoxOptim

# Create the fitness graph object and serve it
const Vlg = BlackBoxOptim.VegaLiteGraphFitnessGraph(false)
BlackBoxOptim.serve(Vlg)

# We will use a callback function to add fitness data to the graph.
# Since no "Time" is given the graph object will set time 0.0 for the first
# time we add data and then will count relative to that.
function callback(oc)
    BlackBoxOptim.add_data!(Vlg, Dict("Fitness" => best_fitness(oc)))
end

# Func to optimize.
function rosenbrock(x)
    sum(i -> 100*abs2(x[i+1] - x[i]^2) + abs2(x[i] - 1), Base.OneTo(length(x)-1))
end

# Now optimize for 2 minutes.
# Go to http://127.0.0.1:8081 to view fitness progress!
res = bboptimize(rosenbrock;
        SearchRange=(-10.0,10.0), NumDimensions = 500,
        PopulationSize=100, MaxTime=2*60.0,
        CallbackFunction = callback, CallbackInterval = 2.0);
println("Best fitness = ", best_fitness(res))


text:
include(Pkg.dir("MethylUtils", "src","Config.jl"))
local_store_path =  Pkg.dir("MethylUtils", "testdata","store", "annotation")
println(LOCAL_STORE_PATH_STATUS())
SET_LOCAL_STORE_PATH(local_store_path)
println("1. ", CURRENT_ANNOTATION_ORG_NAMES)
println("2. ", CURRENT_ANNOTATION_CHR_NAMES)
println("3. ", CURRENT_ANNOTATION_CHR_INDEX)
SET_LOCAL_STORE_PATH("nothing")


text:
######################################################################
# field types and parsers
######################################################################

"""
Parsers of type `T` return `Nullable{parsedtype(T)}`.
"""
parsedtype{T}(::Type{T}) = T    # fallback

"Representing gender."
@enum Gender male female

function Base.tryparse(::Type{Gender}, string)
    string == "M" ? Nullable(male) :
        string == "F" ? Nullable(female) : Nullable{Gender}()
end

"Some dates end with 00, recode as to 1st day of the month.

Dates coded as 00000000 are considered unparsed."
abstract AMDB_Date

parsedtype(::Type{AMDB_Date}) = Date

function Base.tryparse(::Type{AMDB_Date}, string)
    if !isascii(string) || (string == "00000000")
        Nullable{Date}()
    elseif endswith(string, "00")  # day is corrected to 1
        @catch_as_Nullable Date(string[1:end-2], "yyyymm")::Date
    else
        @catch_as_Nullable Date(string, "yyyymmdd")::Date
    end
end

"Fields which are possibly an empty string. Parsed as `Nullable{T}`."
abstract MaybeEmpty{T}

parsedtype{T}(::Type{MaybeEmpty{T}}) = Nullable{parsedtype(T)}

function Base.tryparse{T}(::Type{MaybeEmpty{T}}, string)
    S = parsedtype(T)
    if string == ""
        Nullable(Nullable{S}())
    else
        v = tryparse(T, string)
        isnull(v) ? Nullable{Nullable{S}}() : Nullable(v)
    end
end

"""
Automatically insert new items into a dictionary with `get!`,
assigning a number in the order of insertion. `keys` will retrieve a
vector of the items. Can also be used as a parser with `tryparse`.
"""
immutable AutoEnum{T}
    dict::Dict{T,Int}
    AutoEnum() = new(Dict{T,Int}())
end

parsedtype(::Type{AutoEnum}) = Int

function Base.get!{T}(ae::AutoEnum{T}, key::T)
    if haskey(ae.dict, key)
        ae.dict[key]
    else
        ae.dict[key] = length(ae.dict) + 1
    end
end

Base.length(ae::AutoEnum) = length(ae.dict)

function Base.keys(ae::AutoEnum)
    map(x->x[1], sort(collect(ae.dict), by = x->x[2]))
end

Base.tryparse(ae::AutoEnum, string) = Nullable(get!(ae, string))

parsedtype{T,S}(::Type{Dict{T,S}}) = S

function Base.tryparse{T,S}(dict::Dict{T,S}, string)
    haskey(dict, string) ? Nullable(dict[string]) : Nullable{S}()
end

"""
Parse a line to type R, calling tryparse with the given `types` on the
`positions`.
"""
immutable LineParser{R}
    positions::Vector{Int}
    types::Vector
    function LineParser(positions, types)
        @assert length(positions) == length(types)
        new(positions, types)
    end
end

parsedtype{R}(::Type{LineParser{R}}) = R

function LineParser{R}(::Type{R}, pairs...)
    LineParser{R}([pair[1] for pair in pairs],
                  [pair[2] for pair in pairs])
end

"""
Parse a spell with the given line parser.
"""
function Base.tryparse{R}(parser::LineParser{R}, line::AbstractString)
    raw_fields = split(line, ';')
    @assert raw_fields[end] == "" # check terminating
    raw_fields = raw_fields[parser.positions]
    parsed_fields = tryparse.(parser.types, raw_fields)
    unparsed = find(isnull, parsed_fields)
    if isempty(unparsed)
        Nullable(R(map(get, parsed_fields)...))
    else
        println("unparsed fields $(unparsed) = $(raw_fields[unparsed])")
        Nullable{R}()
    end
end


text:
mutable struct AABB{T<:AbstractFloat} <: Intersectable{T}
    pmin::SVector{3,T}
    pmax::SVector{3,T}
    AABB(pmin::SVector{3,T}, pmax::SVector{3,T}) where T =
        new{T}(compmin(pmin,pmax), compmax(pmin,pmax))
end
AABB(t::T) where T = AABB(SVector(t,t,t),SVector(t,t,t))

AABB(a::AABB{T}, b::AABB{T}) where T =
    AABB(compmin(a.pmin,b.pmin), compmax(a.pmax,b.pmax))

function AABB(bboxes::VA) where {T,VA<:AbstractVector{AABB{T}}}
    # Calculate bbox encompassing all objects
    bbox = first(bboxes)
    for b in bboxes
        bbox = AABB(bbox, b)
    end
    bbox
end
AABB(objs::AbstractVector{Intersectable{T}}) where T = AABB(map(aabb, objs))

Base.show(io::IO, bbox::AABB{T}) where T =
    write(io, "$(T)⟦", join(map(i -> "$(bbox.pmin[i])..$(bbox.pmax[i])", 1:3), "; "), "⟧")

Images.center(a::AABB) = (a.pmin+a.pmax)/2

# http://psgraphics.blogspot.se/2016/02/new-simple-ray-box-test-from-andrew.html
function Base.intersect(ray::Ray{T}, ab::AABB{T}, tₘᵢₙ::T=zero(T), tₘₐₓ::T=one(T)/zero(T)) where T
    for a = 1:3
        D⁻¹ = one(T)/ray.dir[a]
        t₀ = (ab.pmin[a] - ray.pos[a]) * D⁻¹
        t₁ = (ab.pmax[a] - ray.pos[a]) * D⁻¹
        if D⁻¹ < 0
            t₀,t₁ = t₁,t₀
        end
        tₘᵢₙ = t₀ > tₘᵢₙ ? t₀ : tₘᵢₙ
        tₘₐₓ = t₁ < tₘₐₓ ? t₁ : tₘₐₓ
        tₘₐₓ ≤ tₘᵢₙ && return false
    end
    true
end

export AABB, center


text:
# COV_EXCL_START
struct LocalUnitary{S<:Number}<:AbstractSymmetryOperationEmbedding
    operations::Dict{Int, Matrix{S}}
    function LocalUnitary(ops::AbstractDict{<:Integer, <:AbstractMatrix{S}}) where {S<:Number}
        error("Local unitary not implemented")
        return new{S}(ops)
    end

    function LocalUnitary(pairs::Pair{<:Integer, <:AbstractMatrix{<:Number}}...)
        error("Local unitary not implemented")
        LocalUnitary(Dict(pairs...))
    end
end
# COV_EXCL_STOP


text:
module DataStreamsIntegrationTests

using DataStreams, Nulls, Base.Test

const DSTESTDIR = joinpath(dirname(dirname(@__FILE__)), "test")

export Tester, scalartransforms, vectortransforms, DSTESTDIR

type Tester
    name::String
    highlevel::Function
    hashighlevel::Bool
    constructor::Type
    args::Tuple
    scalartransforms::Dict
    vectortransforms::Dict
    sinktodf::Function
    cleanup::Function
end

# transforms
incr(x) = x + 1
incr(::Null) = null

doe_ify(x) = string(x, " Doe")
doe_ify(::Null) = null

getlength(x) = length(x)
getlength(x::Null) = null

div2(x) = x / 2
div2(x::Null) = null

scalartransforms = Dict{String, Function}("id"=> incr, "firstname"=> doe_ify, "lastname"=> getlength, "salary"=> div2)
vectortransforms = Dict{String, Function}("id"=> x->[incr(i) for i in x], "firstname"=> x->[doe_ify(i) for i in x], "lastname"=> x->[getlength(i) for i in x], "salary"=> x->[div2(i) for i in x])

function gettransforms(source, sink)
    streamtypes = Data.streamtypes(sink.constructor)
    for typ in streamtypes
        if Data.streamtype(source.constructor, typ)
            return typ == Data.Column ? source.vectortransforms : source.scalartransforms
        end
    end
end

# tests
typequal(::Type{T}, ::Type{T}) where {T} = true
typequal(::Type{Union{T, Null}}, ::Type{Union{S, Null}}) where {T,S} = typequal(T, S)
typequal(::Type{Union{T, Null}}, ::Type{S}) where {T,S} = typequal(T, S)
typequal(a, b) = (a <: ?AbstractString && b <: ?AbstractString) ||
                 (a <: ?Integer && b <: ?Integer) ||
                 (a <: ?Dates.TimeType && b <: ?Dates.TimeType)

function check(df, rows, appended=false, transformed=false)
    # test size
    cols, rows = 7, appended ? rows * 2 : rows
    @test size(Data.schema(df)) == (rows, cols)

    # test types
    expected_types = transformed ? [Int, String, Int, Float64, Float64, Date, DateTime] : [Int, String, String, Float64, Float64, Date, DateTime]
    types = Data.types(Data.schema(df))
    @test all([DataStreamsIntegrationTests.typequal(types[i], expected_types[i]) for i = 1:length(types)])

    # test values
    if transformed
        @test df[1, 1] == 2
        @test df[1, 2] == "Lawrence Doe"
        @test df[1, 3] == length("Powell")
        @test isapprox(df[1, 4], 87216.8 / 2; atol=0.01)
        @test isapprox(df[1, 5], 26.47; atol=0.01)
        @test df[1, 6] == Date(2002, 4, 9)
        @test df[1, 7] == DateTime(2002, 1, 17, 21, 32, 0)

        @test df[end, 1] == 70001
        @test df[end, 2] == "Craig Doe"
        @test df[end, 3] == length("Robertson")
        @test isapprox(df[end, 4], 33699.215; atol=0.01)
        @test isapprox(df[end, 5], 23.99; atol=0.01)
        @test df[end, 6] == Date(2008, 6, 23)
        @test df[end, 7] == DateTime(2005, 4, 18, 7, 2, 0)
    else
        @test df[1, 1] == 1
        @test df[1, 2] == "Lawrence"
        @test df[1, 3] == "Powell"
        @test isapprox(df[1, 4], 87216.8; atol=0.01)
        @test isapprox(df[1, 5], 26.47; atol=0.01)
        @test df[1, 6] == Date(2002, 4, 9)
        @test df[1, 7] == DateTime(2002, 1, 17, 21, 32, 0)

        @test df[end, 1] == 70000
        @test df[end, 2] == "Craig"
        @test df[end, 3] == "Robertson"
        @test isapprox(df[end, 4], 67398.43; atol=0.01)
        @test isapprox(df[end, 5], 23.99; atol=0.01)
        @test df[end, 6] == Date(2008, 6, 23)
        @test df[end, 7] == DateTime(2005, 4, 18, 7, 2, 0)
    end
end

source = csvsource
sink = dfsink

function teststream(sources, sinks; rows=70000)
    for source in sources
        for sink in sinks
            try
            if source.hashighlevel
            println("[$(now())]: Test high-level from source to sink; e.g. CSV.read")
            println("[$(now())]: Source: $(source.name) args => Sink: $(sink.name) args")
            si = source.highlevel(source.args..., sink.constructor, sink.args...)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows)
            println("[$(now())]: Source: $(source.name) args => Sink: $(sink.name) args + append")
            si = source.highlevel(source.args..., sink.constructor, sink.args...; append=true)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true)
            println("[$(now())]: Source: $(source.name) args => Sink: $(sink.name) args + transforms")
            si = source.highlevel(source.args..., sink.constructor, sink.args...; transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, false, true)
            println("[$(now())]: Source: $(source.name) args => Sink: $(sink.name) args + append + transforms")
            si = source.highlevel(source.args..., sink.constructor, sink.args...; append=true, transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true, true)
            println("[$(now())]: Source: $(source.name) args => Sink: $(sink.name)")
            sinst = sink.constructor(sink.args...)
            si = source.highlevel(source.args..., sinst)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows)
            println("[$(now())]: Source: $(source.name) args => Sink: $(sink.name) + append")
            sinst = sink.constructor(sink.args...; append=true)
            si = source.highlevel(source.args..., sinst; append=true)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true)
            println("[$(now())]: Source: $(source.name) args => Sink: $(sink.name) + transforms")
            sinst = sink.constructor(sink.args...)
            si = source.highlevel(source.args..., sinst; transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, false, true)
            println("[$(now())]: Source: $(source.name) args => Sink: $(sink.name) + append + transforms`")
            sinst = sink.constructor(sink.args...; append=true)
            si = source.highlevel(source.args..., sinst; append=true, transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true, true)
            println("[$(now())]: Source: $(source.name) => Sink: $(sink.name) args")
            soinst = source.constructor(source.args...)
            si = source.highlevel(soinst, sink.constructor, sink.args...)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows)
            println("[$(now())]: Source: $(source.name) => Sink: $(sink.name) args + append")
            soinst = source.constructor(source.args...)
            si = source.highlevel(soinst, sink.constructor, sink.args...; append=true)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true)
            println("[$(now())]: Source: $(source.name) => Sink: $(sink.name) args + transforms")
            soinst = source.constructor(source.args...)
            si = source.highlevel(soinst, sink.constructor, sink.args...; transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, false, true)
            println("[$(now())]: Source: $(source.name) => Sink: $(sink.name) args + append + transforms")
            soinst = source.constructor(source.args...)
            si = source.highlevel(soinst, sink.constructor, sink.args...; append=true, transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true, true)
            println("[$(now())]: Source: $(source.name) => Sink: $(sink.name)")
            soinst = source.constructor(source.args...)
            sinst = sink.constructor(sink.args...)
            si = source.highlevel(soinst, sinst)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows)
            println("[$(now())]: Source: $(source.name) => Sink: $(sink.name) + append")
            soinst = source.constructor(source.args...)
            sinst = sink.constructor(sink.args...; append=true)
            si = source.highlevel(soinst, sinst; append=true)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true)
            println("[$(now())]: Source: $(source.name) => Sink: $(sink.name) + transforms")
            soinst = source.constructor(source.args...)
            sinst = sink.constructor(sink.args...)
            si = source.highlevel(soinst, sinst; transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, false, true)
            println("[$(now())]: Source: $(source.name) => Sink: $(sink.name) + append + transforms")
            soinst = source.constructor(source.args...)
            sinst = sink.constructor(sink.args...; append=true)
            si = source.highlevel(soinst, sinst; append=true, transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true, true)
            end

            if sink.hashighlevel
            println("[$(now())]: Test high-level to sink from source; e.g. CSV.write")
            println("[$(now())]: Sink: $(sink.name) args => Source: $(source.name) args")
            si = sink.highlevel(sink.args..., source.constructor, source.args...)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows)
            println("[$(now())]: Sink: $(sink.name) args => Source: $(source.name) args + append")
            si = sink.highlevel(sink.args..., source.constructor, source.args...; append=true)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true)
            println("[$(now())]: Sink: $(sink.name) args => Source: $(source.name) args + transforms")
            si = sink.highlevel(sink.args..., source.constructor, source.args...; transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, false, true)
            println("[$(now())]: Sink: $(sink.name) args => Source: $(source.name) args + append + transforms")
            si = sink.highlevel(sink.args..., source.constructor, source.args...; append=true, transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true, true)
            println("[$(now())]: Sink: $(sink.name) => Source: $(source.name) args")
            sinst = sink.constructor(sink.args...)
            si = sink.highlevel(sinst, source.constructor, source.args...)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows)
            println("[$(now())]: Sink: $(sink.name) => Source: $(source.name) args + append")
            sinst = sink.constructor(sink.args...; append=true)
            si = sink.highlevel(sinst, source.constructor, source.args...; append=true)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true)
            println("[$(now())]: Sink: $(sink.name) => Source: $(source.name) args + transforms")
            sinst = sink.constructor(sink.args...)
            si = sink.highlevel(sinst, source.constructor, source.args...; transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, false, true)
            println("[$(now())]: Sink: $(sink.name) => Source: $(source.name) args + append + transforms")
            sinst = sink.constructor(sink.args...; append=true)
            si = sink.highlevel(sinst, source.constructor, source.args...; append=true, transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true, true)
            println("[$(now())]: Sink: $(sink.name) args => Source: $(source.name)")
            soinst = source.constructor(source.args...)
            si = sink.highlevel(sink.args..., soinst)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows)
            println("[$(now())]: Sink: $(sink.name) args => Source: $(source.name) + append")
            soinst = source.constructor(source.args...)
            si = sink.highlevel(sink.args..., soinst; append=true)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true)
            println("[$(now())]: Sink: $(sink.name) args => Source: $(source.name) + transforms")
            soinst = source.constructor(source.args...)
            si = sink.highlevel(sink.args..., soinst; transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, false, true)
            println("[$(now())]: Sink: $(sink.name) args => Source: $(source.name) + append + transforms")
            soinst = source.constructor(source.args...)
            si = sink.highlevel(sink.args..., soinst; append=true, transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true, true)
            println("[$(now())]: Sink: $(sink.name) => Source: $(source.name)")
            sinst = sink.constructor(sink.args...)
            soinst = source.constructor(source.args...)
            si = sink.highlevel(sinst, soinst)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows)
            println("[$(now())]: Sink: $(sink.name) => Source: $(source.name) + append")
            sinst = sink.constructor(sink.args...; append=true)
            soinst = source.constructor(source.args...)
            si = sink.highlevel(sinst, soinst; append=true)
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true)
            println("[$(now())]: Sink: $(sink.name) => Source: $(source.name) + transforms")
            sinst = sink.constructor(sink.args...)
            soinst = source.constructor(source.args...)
            si = sink.highlevel(sinst, soinst; transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, false, true)
            println("[$(now())]: Sink: $(sink.name) => Source: $(source.name) + append + transforms")
            sinst = sink.constructor(sink.args...; append=true)
            soinst = source.constructor(source.args...)
            si = sink.highlevel(sinst, soinst; append=true, transforms=DataStreamsIntegrationTests.gettransforms(source, sink))
            DataStreamsIntegrationTests.check(sink.sinktodf(si), rows, true, true)
            println("[$(now())]: finished...")
            end
            catch e
                rethrow(e)
            finally
            sink.cleanup(sink.args...)
            end
        end
    end
end

end # module


text:
using Test, StructTypes, Tables, Strapping

tbl = (a=[10], b=[3.14])
tbl2 = (id=[10, 10, 10], floats=[3.14, 3.15, 3.16])

struct AB
    a::Int
    b::Float64
end
StructTypes.StructType(::Type{AB}) = StructTypes.Struct()

ab = AB(10, 3.14)
@test Strapping.construct(AB, tbl) == ab
@test Strapping.construct(Vector{AB}, tbl) == [ab]

mutable struct ABM
    a::Int
    b::Float64
    ABM() = new()
    ABM(a::Int, b::Float64) = new(a, b)
end
Base.:(==)(a::ABM, b::ABM) = a.a == b.a && a.b == b.b
StructTypes.StructType(::Type{ABM}) = StructTypes.Mutable()

abm = ABM(10, 3.14)
@test Strapping.construct(ABM, tbl) == abm
@test Strapping.construct(Vector{ABM}, tbl) == [abm]

@test Strapping.construct(NamedTuple, tbl) == (a=10, b=3.14)
@test Strapping.construct(NamedTuple{(:a, :b), Tuple{Int, Float64}}, tbl) == (a=10, b=3.14)
@test Strapping.construct(NamedTuple{(:a, :b)}, tbl) == (a=10, b=3.14)
@test Strapping.construct(Dict, tbl) == Dict("a" => 10, "b" => 3.14)
@test Strapping.construct(Dict{String, Float64}, tbl) == Dict("a" => 10.0, "b" => 3.14)
@test Strapping.construct(Dict{Symbol, Any}, tbl) == Dict(:a => 10, :b => 3.14)
@test Strapping.construct(Array, tbl) == [10, 3.14]
@test Strapping.construct(Vector{Float64}, tbl) == [10.0] # because Float64 is scalar, only 1st field of result is used, other fields are ignored
@test Strapping.construct(Set, tbl) == Set([10, 3.14])
@test Strapping.construct(Tuple, tbl) == (10, 3.14)
@test Strapping.construct(Tuple{Int, Float64}, tbl) == (10, 3.14)
@test Strapping.construct(Vector{Any}, tbl) == [Dict("a" => 10, "b" => 3.14)]

struct AB2
    id::Int
    floats::Vector{Float64}
end

Base.:(==)(a::AB2, b::AB2) = a.id == b.id && a.floats == b.floats
StructTypes.StructType(::Type{AB2}) = StructTypes.Struct()
StructTypes.idproperty(::Type{AB2}) = :id

ab2 = AB2(10, [3.14, 3.15, 3.16])
@test Strapping.construct(AB2, tbl2) == ab2
@test Strapping.construct(Vector{AB2}, tbl2) == [ab2]

struct AB3
    a::Int
    ab::AB
end

StructTypes.StructType(::Type{AB3}) = StructTypes.Struct()

tbl3 = (a=[1], ab_a=[10], ab_b=[3.14])

ab3 = AB3(1, AB(10, 3.14))
@test Strapping.construct(AB3, tbl3) == ab3
@test Strapping.construct(Vector{AB3}, tbl3) == [ab3]

mutable struct AB4
    a::Int
    b::Float64
    abs::Vector{AB}
    AB4() = new()
    AB4(a::Int, b::Float64, abs::Vector{AB}) = new(a, b, abs)
end

Base.:(==)(a::AB4, b::AB4) = a.a == b.a && a.b == b.b && a.abs == b.abs
StructTypes.StructType(::Type{AB4}) = StructTypes.Mutable()
StructTypes.idproperty(::Type{AB4}) = :a

tbl4 = (a=[10, 10, 10], b=[3.14, 3.14, 3.14], abs_a=[10, 10, 10], abs_b=[3.14, 3.15, 3.16])

ab4 = AB4(10, 3.14, AB[AB(10, 3.14), AB(10, 3.15), AB(10, 3.16)])
@test Strapping.construct(AB4, tbl4) == ab4
@test Strapping.construct(Vector{AB4}, tbl4) == [ab4]

# multiple levels of nesting
struct AB5
    a::Int
    ab::AB3
end

StructTypes.StructType(::Type{AB5}) = StructTypes.Struct()

tbl5 = (a=[10], ab_a=[10], ab_ab_a=[10], ab_ab_b=[3.14])

ab5 = AB5(10, AB3(10, AB(10, 3.14)))
@test Strapping.construct(AB5, tbl5) == ab5
@test Strapping.construct(Vector{AB5}, tbl5) == [ab5]

# deconstruction
@test columntable(Strapping.deconstruct(ab)) == tbl
@test columntable(Strapping.deconstruct(ab2)) == tbl2
@test columntable(Strapping.deconstruct(ab3)) == tbl3
@test columntable(Strapping.deconstruct(ab4)) == tbl4
@test columntable(Strapping.deconstruct(ab5)) == tbl5

struct AB6
    a::Int
    b::AB
    c::Vector{AB}
end

Base.:(==)(a::AB6, b::AB6) = a.a == b.a && a.b == b.b && a.c == b.c
StructTypes.StructType(::Type{AB6}) = StructTypes.Struct()
StructTypes.idproperty(::Type{AB6}) = :a

tbl6 = (a=[1, 1, 1], b_a=[2, 2, 2], b_b=[0.01, 0.01, 0.01], c_a=[10, 11, 12], c_b=[1.1, 2.2, 3.3])

ab6 = AB6(1, AB(2, 0.01), [AB(10, 1.1), AB(11, 2.2), AB(12, 3.3)])
@test Strapping.construct(AB6, tbl6) == ab6
@test Strapping.construct(Vector{AB6}, tbl6) == [ab6]
@test columntable(Strapping.deconstruct(ab6)) == tbl6

# https://github.com/JuliaData/Strapping.jl/issues/12
struct AB7
    id::Int
    values::Vector{Float64}
end

Base.:(==)(a::AB7, b::AB7) = a.id == b.id && a.values == b.values
StructTypes.StructType(::Type{AB7}) = StructTypes.Struct()
StructTypes.idproperty(::Type{AB7}) = :id

ab7 = AB7(1, Float64[])
tbl = columntable(Strapping.deconstruct(ab7))
@test tbl.id[1] == 1
@test tbl.values[1] === missing

struct AB9
    a::Int
    b::String
    c::Float64
    d::String
    e::Int
    f::String
end
StructTypes.StructType(::Type{AB9}) = StructTypes.Struct()

struct AB10
    id::Int
    ab9::AB9
end
StructTypes.StructType(::Type{AB10}) = StructTypes.Struct()

struct AB11
    id::Int
    ab10::AB10
end
StructTypes.StructType(::Type{AB11}) = StructTypes.Struct()

ab11 = AB11(1, AB10(2, AB9(3, "4", 5.0, "6", 7, "8")))
tbl = columntable(Strapping.deconstruct(ab11))
@test length(tbl) == 8
@test tbl.id[1] == 1
@test tbl[end][1] == "8"

# https://github.com/JuliaData/Strapping.jl/issues/3
struct TestStruct
    a::Float64
    b::Float64
    id::Int
end

StructTypes.StructType(::Type{TestStruct}) = StructTypes.Struct()
StructTypes.idproperty(::Type{TestStruct}) = :id

data = [ TestStruct(rand(2)..., n) for n = 1:5]

tbl = Strapping.deconstruct(data)
@test length(Tables.columntable(tbl)[1]) == 5

struct AA1
    a::Int64
    b::Dict{Symbol, Any}
end

StructTypes.StructType(::Type{AA1}) = StructTypes.Struct()

data = [ AA1(1, Dict{Symbol, Any}(:aa => 2, :bb => 3)) ]
tbl = Strapping.deconstruct(data)
tbl2 = Tables.columntable(tbl)
@test tbl2.a == [1]
@test tbl2.b_aa == [2]
@test tbl2.b_bb == [3]

#8
struct TestResult
    id::Int
    values::Vector{Float64}
end
StructTypes.StructType(::Type{TestResult}) = StructTypes.Struct()
StructTypes.idproperty(::Type{TestResult}) = :id

tbl = (id=[1, 1, 1, 2, 2, 2], values=[3.14, 3.15, 3.16, 40.1, 0.01, 2.34])
testresult = Strapping.construct(Vector{TestResult}, tbl)

struct Experiment
    id::Int
    name::String
    testresults::TestResult
end
StructTypes.StructType(::Type{Experiment}) = StructTypes.Struct()
StructTypes.idproperty(::Type{Experiment}) = :id

StructTypes.fieldprefix(::Type{Experiment}, nm::Symbol) = nm == :testresults ? :testresults_ : :_

tbl2 = (id=[1, 1, 1], name=["exp1", "exp1", "exp1"], testresults_id=[1, 1, 1], testresults_values=[3.14, 3.15, 3.16])
experiment = Strapping.construct(Experiment, tbl2)
@test experiment.id == 1
@test experiment.name == "exp1"
@test experiment.testresults.values == [3.14, 3.15, 3.16]

struct Service
    id::String
    data::NamedTuple
end
StructTypes.StructType(::Type{Service}) = StructTypes.Struct()
StructTypes.idproperty(::Type{Service}) = :id
StructTypes.fieldprefix(::Type{Service}, nm::Symbol) = Symbol()

Tables.isrowtable(::Type{Service}) = true

function Tables.getcolumn(row::Service,i::Int)
    if i == 1
        return getfield(row,1)
    else
        return row.data[i-1]
    end
end

function Tables.getcolumn(row::Service,nm::Symbol)
    if nm == :id
        return row.id
    else
        return getproperty(row.data,nm)
    end
end

function Tables.columnnames(row::Service)
    vcat(:id,keys(row.data)...)
end

x = Service("qux",(a=1,b=3))
xd = Strapping.deconstruct(x)
xc = Strapping.construct(Service,xd)

@test xc.id == "qux"
@test xc.data == (a=1, b=3)

x2 = Service("foo",(a=1,b=3))
X = [x,x2]
Xd = Strapping.deconstruct(X)
Xc = Strapping.construct(Vector{Service},Xd)

@test length(Xc) == 2
@test Xc[2].id == "foo"
@test Xc[2].data == (a=1, b=3)

struct Wrapper
    x::NamedTuple{(:a, :b), Tuple{Int, String}}
end

StructTypes.StructType(::Type{Wrapper}) = StructTypes.CustomStruct()
StructTypes.lower(x::Wrapper) = x.x
StructTypes.lowertype(::Type{Wrapper}) = fieldtype(Wrapper, :x)
w = Wrapper((a=1, b="hey"))

tbl = Strapping.deconstruct(w) |> Tables.columntable
@test tbl == (a = [1], b = ["hey"])
w2 = Strapping.construct(Wrapper, tbl)
@test w == w2


text:
"""
ModelUtils.jl provides functionality for analyzing and manipulating
Flux.jl models.
"""
module ModelUtils

using Flux
using AbstractTrees

include("./model.jl")
include("./utils.jl")
include("./map.jl")
include("./hooks.jl")
include("./introspection.jl")
include("./attributes.jl")
include("./initialization.jl")
#include("./plot.jl")

export Model,
    Hook,
    Initialization,
    IterLayers,
    IterModels,
    addhook,
    attrtype,
    backwardstatshook,
    children,
    forwardstatshook,
    gradientsonebatch,
    gradientsdict,
    hooksaveactivations,
    hooksavegrads,
    init!,
    initmodel!,
    init_kaiming_normal,
    init_kaiming_uniform,
    init_zeros,
    mapmodel,
    paramsdict,
    printmodel,
    removehooks
end # module


text:
using CSV
using Plots
using DataFrames
using LinearAlgebra

# using REPL
# const output = IOBuffer()
# const out_terminal = REPL.Terminals.TerminalBuffer(output)
# const basic_repl = REPL.BasicREPL(out_terminal)
# const basic_display = REPL.REPLDisplay(basic_repl)
# Base.pushdisplay(basic_display)
# plotly()

function read_data(path::String)
    dataframe = CSV.read(path)
    x::Array = dataframe.features
    y::Array = dataframe.targets
    return x, y;
end

function set_dimensions(arr::Array)
    if length(size(arr)) == 2
        return arr;
    elseif length(size(arr)) == 1
        return reshape(arr, (:, 1));
    elseif length(size(arr)) > 2
        throw("Error")
    else
        throw("Error")
    end
end

function append_ones(arr::Array)
    one_arr = ones(size(x))
    return hcat(x, one_arr);
end

function fit_ols(X::Array, y::Array)
    return inv(transpose(X)*X)*(transpose(X)*y)
end

wait_for_key(prompt) = (print(stdout, prompt); read(stdin, 1); nothing)

x, y = read_data("../../Test_Data/linear.csv")
x, y = set_dimensions(x), set_dimensions(y)
x = append_ones(x)
beta = fit_ols(x, y)
predictions = x*beta

p1 = plot(x[:, 1], y, seriestype=:scatter, title="My Scatter Plot");
plot!(p1, x[:, 1], predictions);
display(p1)

wait_for_key("Press any key to continue...")


text:
using CompScienceMeshes, BEAST

T = tetmeshsphere(1.0,0.3)
X = nedelecc3d(T)
Γ = boundary(T)

X, Y = raviartthomas(Γ), buffachristiansen(Γ)

ω = 1.0
ϵ, ϵ′  = 1.0, 5.0
μ, μ′  = 1.0, 1.0
κ,  η  = ω*√(ϵ*μ), √(μ/ϵ)
κ′, η′ = ω*√(ϵ′*μ′), √(μ′/ϵ′)

N = NCross()

T  = Maxwell3D.singlelayer(wavenumber=κ)
T′ = Maxwell3D.singlelayer(wavenumber=κ′)
K  = Maxwell3D.doublelayer(wavenumber=κ)
K′ = Maxwell3D.doublelayer(wavenumber=κ′)

E = Maxwell3D.planewave(direction=ẑ, polarization=x̂, wavenumber=κ)
H = -1/(im*κ*η)*curl(E)
e, h = (n × E) × n, (n × H) × n

@hilbertspace j m
@hilbertspace k l

α, α′ = μ/η, μ′/η′
mueller = @discretise(
      (ϵ*η*T-ϵ′*η′*T′)[k,j] -    ((ϵ+ϵ′)/2*N+ϵ*K-ϵ′*K′)[k,m] +
((μ+μ′)/2*N+μ*K-μ′*K′)[l,j] +              (α*T-α′*T′)[l,m] == -ϵ*e[k] - μ*h[l],
j∈X, m∈X, k∈Y, l∈Y)
u,A_mueller,rhs_mueller = solve(mueller)

#postprocessing
using Plots
import Plotly
using LinearAlgebra
fcrj, _ = facecurrents(u[j],X)
fcrm, _ = facecurrents(u[m],X)
Plotly.plot(patch(Γ, norm.(fcrj)),Plotly.Layout(title="j Mueller"))
Plotly.plot(patch(Γ, norm.(fcrm)),Plotly.Layout(title="m Mueller"))

function nearfield(um,uj,Xm,Xj,κ,η,points,
    Einc=(x->point(0,0,0)),
    Hinc=(x->point(0,0,0)))

    K = BEAST.MWDoubleLayerField3D(wavenumber=κ)
    T = BEAST.MWSingleLayerField3D(wavenumber=κ)

    Em = potential(K, points, um, Xm)
    Ej = potential(T, points, uj, Xj)
    E = -Em + η * Ej + Einc.(points)

    Hm = potential(T, points, um, Xm)
    Hj = potential(K, points, uj, Xj)
    H = 1/η*Hm + Hj + Hinc.(points)

    return E, H
end

Z = range(-1,1,length=100)
Y = range(-1,1,length=100)
nfpoints = [point(0,y,z) for z in Z, y in Y]

import Base.Threads: @spawn
task1 = @spawn nearfield(u[m],u[j],X,X,κ,η,nfpoints,E,H)
task2 = @spawn nearfield(-u[m],-u[j],X,X,κ′,η′,nfpoints)

E_ex, H_ex = fetch(task1)
E_in, H_in = fetch(task2)

E_tot = E_in + E_ex
H_tot = H_in + H_ex

contour(real.(getindex.(E_tot,1)))
contour(real.(getindex.(H_tot,2)))

heatmap(Z, Y, real.(getindex.(E_tot,1)),title="E_tot Mueller")
#heatmap(Z, Y, real.(getindex.(H_tot,2)))
#heatmap(Z, Y, real.(getindex.(E_in,1)))
#heatmap(Z, Y, real.(getindex.(E_ex,1)))


text:
@testset "GPS L1" begin
    @test @inferred(Tracking.is_upcoming_integration_new_bit(GPSL1, 0xfffff00000, 50)) == true

    @test @inferred(Tracking.is_upcoming_integration_new_bit(GPSL1, 0xfffff, 10)) == false

    @test @inferred(Tracking.is_upcoming_integration_new_bit(GPSL1, 0xfffff, 40)) == true

    @test @inferred(Tracking.get_default_correlator(GPSL1, NumAnts(1))) == EarlyPromptLateCorrelator(NumAnts(1))
    @test @inferred(Tracking.get_default_correlator(GPSL1, NumAnts(3))) == EarlyPromptLateCorrelator(NumAnts(3))
end


text:
# This file is a part of Julia. License is MIT: https://julialang.org/license

using Test
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays

A = Int64[1, 2, 3, 4]
B = Complex{Int64}[5+6im, 7+8im, 9+10im]
# getindex
@test reinterpret(Complex{Int64}, A) == [1 + 2im, 3 + 4im]
@test reinterpret(Float64, A) == reinterpret.(Float64, A)

@test reinterpret(NTuple{3, Int64}, B) == [(5,6,7),(8,9,10)]

# setindex
let Ac = copy(A), Bc = copy(B)
    reinterpret(Complex{Int64}, Ac)[2] = -1 - 2im
    @test Ac == [1, 2, -1, -2]
    reinterpret(NTuple{3, Int64}, Bc)[2] = (4,5,6)
    @test Bc == Complex{Int64}[5+6im, 7+4im, 5+6im]
    reinterpret(NTuple{3, Int64}, Bc)[1] = (1,2,3)
    @test Bc == Complex{Int64}[1+2im, 3+4im, 5+6im]

    A1 = reinterpret(Float64, A)
    A2 = reinterpret(ComplexF64, A)
    A1[1] = 1.0
    @test real(A2[1]) == 1.0
end

# same-size reinterpret where one of the types is non-primitive
let a = NTuple{4,UInt8}[(0x01,0x02,0x03,0x04)]
    @test reinterpret(Float32, a)[1] == reinterpret(Float32, 0x04030201)
    reinterpret(Float32, a)[1] = 2.0
    @test reinterpret(Float32, a)[1] == 2.0
end

# ensure that reinterpret arrays aren't erroneously classified as strided
let A = reshape(1:20, 5, 4)
    V = view(A, :, :)
    R = reinterpret(Int32, V)
    R2 = reinterpret(Int32, A)
    @test !(R isa StridedArray)
    @test !(R2 isa StridedArray)
    @test R * ones(4, 5) == R2 * ones(4,5) == copy(R) * ones(4,5) == copy(R2) * ones(4,5)
end

# but ensure that strided views of strided reinterpret arrays are still strided
let A = collect(reshape(1:20, 5, 4))
    R = reinterpret(Int32, A)
    @test R isa StridedArray
    @test view(R, :, :) isa StridedArray
    @test reshape(R, :) isa StridedArray
end

@testset "strides" begin
    a = rand(10)
    b = view(a,2:2:10)
    A = rand(10,10)
    B = view(A, 2:2:10, 2:2:10)

    @test strides(a) == (1,)
    @test strides(b) == (2,)
    @test strides(A) == (1,10)
    @test strides(B) == (2,20)

    for M in (a, b, A, B)
        @inferred strides(M)
        strides_M = strides(M)

        for (i, _stride) in enumerate(collect(strides_M))
            @test _stride == stride(M, i)
        end
    end
end

# IndexStyle
let a = fill(1.0, 5, 3)
    r = reinterpret(Int64, a)
    @test @inferred(IndexStyle(r)) == IndexLinear()
    fill!(r, 2)
    @test all(a .=== reinterpret(Float64, [Int64(2)])[1])
    @test all(r .=== Int64(2))
    for badinds in (0, 16, (0,1), (1,0), (6,3), (5,4))
        @test_throws BoundsError r[badinds...]
        @test_throws BoundsError r[badinds...] = -2
    end
    for goodinds in (1, 15, (1,1), (5,3))
        r[goodinds...] = -2
        @test r[goodinds...] == -2
    end
    r = reinterpret(Int32, a)
    @test @inferred(IndexStyle(r)) == IndexLinear()
    fill!(r, 3)
    @test all(a .=== reinterpret(Float64, [(Int32(3), Int32(3))])[1])
    @test all(r .=== Int32(3))
    for badinds in (0, 31, (0,1), (1,0), (11,3), (10,4))
        @test_throws BoundsError r[badinds...]
        @test_throws BoundsError r[badinds...] = -3
    end
    for goodinds in (1, 30, (1,1), (10,3))
        r[goodinds...] = -3
        @test r[goodinds...] == -3
    end
    r = reinterpret(Int64, view(a, 1:2:5, :))
    @test @inferred(IndexStyle(r)) == IndexCartesian()
    fill!(r, 4)
    @test all(a[1:2:5,:] .=== reinterpret(Float64, [Int64(4)])[1])
    @test all(r .=== Int64(4))
    for badinds in (0, 10, (0,1), (1,0), (4,3), (3,4))
        @test_throws BoundsError r[badinds...]
        @test_throws BoundsError r[badinds...] = -4
    end
    for goodinds in (1, 9, (1,1), (3,3))
        r[goodinds...] = -4
        @test r[goodinds...] == -4
    end
    r = reinterpret(Int32, view(a, 1:2:5, :))
    @test @inferred(IndexStyle(r)) == IndexCartesian()
    fill!(r, 5)
    @test all(a[1:2:5,:] .=== reinterpret(Float64, [(Int32(5), Int32(5))])[1])
    @test all(r .=== Int32(5))
    for badinds in (0, 19, (0,1), (1,0), (7,3), (6,4))
        @test_throws BoundsError r[badinds...]
        @test_throws BoundsError r[badinds...] = -5
    end
    for goodinds in (1, 18, (1,1), (6,3))
        r[goodinds...] = -5
        @test r[goodinds...] == -5
    end
end

# Error on reinterprets that would expose padding
struct S1
    a::Int8
    b::Int64
end

struct S2
    a::Int16
    b::Int64
end

A1 = S1[S1(0, 0)]
A2 = S2[S2(0, 0)]
@test reinterpret(S1, A2)[1] == S1(0, 0)
@test_throws Base.PaddingError (reinterpret(S1, A2)[1] = S2(1, 2))
@test_throws Base.PaddingError reinterpret(S2, A1)[1]
reinterpret(S2, A1)[1] = S2(1, 2)
@test A1[1] == S1(1, 2)

# Unconventional axes
let a = [0.1 0.2; 0.3 0.4], at = reshape([(i,i+1) for i = 1:2:8], 2, 2)
    v = OffsetArray(a, (-1, 1))
    r = reinterpret(Int64, v)
    @test axes(r) === axes(v)
    @test r[0,2] === reinterpret(Int64, v[0,2])
    @test r[1,2] === reinterpret(Int64, v[1,2])
    @test r[0,3] === reinterpret(Int64, v[0,3])
    @test r[1,3] === reinterpret(Int64, v[1,3])
    @test_throws ArgumentError("cannot reinterpret a `Float64` array to `UInt32` when the first axis is Base.IdentityUnitRange(0:1). Try reshaping first.") reinterpret(UInt32, v)
    v = OffsetArray(a, (0, 1))
    r = reinterpret(UInt32, v)
    axsv = axes(v)
    @test axes(r) === (oftype(axsv[1], 1:4), axsv[2])
    for i = 1:2
        rval = reinterpret(Tuple{UInt32,UInt32}, [v[i,2]])[1]
        @test r[2i-1,2]   == rval[1]
        @test r[2i,2] == rval[2]
        rval = reinterpret(Tuple{UInt32,UInt32}, [v[i,3]])[1]
        @test r[2i-1,3]   == rval[1]
        @test r[2i,3] == rval[2]
    end
    r[4,2] = 7
    @test r[4,2] === UInt32(7)
    @test a[2,1] === reinterpret(Float64, [0x33333333, UInt32(7)])[1]
    offsetvt = (-2, 4)
    vt = OffsetArray(at, offsetvt)
    istr = string(Int)
    @test_throws ArgumentError("cannot reinterpret a `Tuple{$istr,$istr}` array to `$istr` when the first axis is Base.IdentityUnitRange(-1:0). Try reshaping first.") reinterpret(Int, vt)
    vt = reshape(vt, 1:1, axes(vt)...)
    r = reinterpret(Int, vt)
    @test r == OffsetArray(reshape(1:8, 2, 2, 2), (0, offsetvt...))
end

@testset "potentially aliased copies" begin
    buffer = UInt8[1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0]
    mid = length(buffer) ÷ 2
    x1 = reinterpret(Int64, @view buffer[1:mid])
    x2 = reinterpret(Int64, @view buffer[mid+1:end])
    x1 .= x2
    @test x1 == x2 == [2]
    @test x1[] === x2[] === Int64(2)
end

# avoid nesting
@test parent(reinterpret(eltype(A), reinterpret(eltype(B), A))) === A

# Test 0-dimensional Arrays
A = zeros(UInt32)
B = reinterpret(Int32,A)
@test size(B) == ()
@test axes(B) == ()
B[] = Int32(5)
@test B[] === Int32(5)
@test A[] === UInt32(5)


text:
__precompile__()

module WinRPM

if Sys.isunix()
    import HTTP
end

using CodecZlib, LibExpat, URIParser

import Base: show, getindex, wait_close, pipeline_error

#export update, whatprovides, search, lookup, install, deps

if Sys.iswindows()
    const OS_ARCH = Sys.WORD_SIZE == 64 ? "mingw64" : "mingw32"
else
    const OS_ARCH = string(Sys.ARCH)
end

function mkdirs(dir)
    if !isdir(dir)
        mkdir(dir)
    end
end

const packages = ETree[]

function __init__()
    empty!(packages)
    global cachedir = joinpath(dirname(dirname(@__FILE__)), "cache")
    global installdir = joinpath(dirname(dirname(@__FILE__)), "deps")
    global indexpath = joinpath(cachedir, "index")

    mkdirs(cachedir)
    mkdirs(installdir)
    open(joinpath(dirname(@__FILE__), "..", "sources.list")) do f
        global sources = filter!(map(chomp, readlines(f))) do l
            return !isempty(l) && l[1] != '#'
        end
    end
    global installedlist = joinpath(dirname(@__FILE__), "..", "installed.list")
    update(false, false)
end

if Sys.isunix()
    function download(source::AbstractString)
        x = HTTP.get(source)
        String(x.body), x.status
    end
elseif Sys.iswindows()
    function download(source::AbstractString; retry=5)
        dest = Vector{UInt16}(undef, 261)
        for i in 1:retry
            res = ccall((:URLDownloadToCacheFileW, :urlmon), stdcall, Cuint,
              (Ptr{Cvoid}, Ptr{UInt16}, Ptr{UInt16}, Clong, Cint, Ptr{Cvoid}),
              C_NULL, transcode(UInt16, source), dest, sizeof(dest) >> 1, 0, C_NULL)
            if res == 0
                resize!(dest, findfirst(iszero, dest) - 1)
                filename = transcode(String, dest)
                if isfile(filename)
                    return read(filename, String), 200
                end
            else
                @warn("Unknown download failure, error code: $res")
            end
            @warn("Retry $i/$retry downloading: $source")
        end
        return "", 0
    end
else
    error("Platform not supported: $(Sys.KERNEL)")
end

getcachedir(source) = getcachedir(cachedir, source)
function getcachedir(cachedir, source)
    open(indexpath, isfile(indexpath) ? "r+" : "w+") do cacheindex
        seek(cacheindex,0)
        lines = readlines(cacheindex)
        for (idx,line) in enumerate(lines)
            if !startswith(line,'#') && ' ' in line
                stri, src = split(chomp(line), ' ', limit=2)
                cache = joinpath(cachedir, stri)
                if !isdir(cache)
                    # remove this directory from the list,
                    # write out the new cacheindex file
                    # and restart
                    seek(cacheindex, 0)
                    truncate(cacheindex, 0)
                    for (idx2,l) in enumerate(lines)
                        if idx == idx2
                            write(cacheindex, '#')
                        end
                        write(cacheindex, l)
                    end
                    empty!(lines)
                    return getcachedir(source)
                end
                if source == src
                    return cache
                end
            end
        end
        # not found in existing cache
        i = 0
        local stri, cache
        while true
            i += 1
            stri = string(i)
            cache = joinpath(cachedir, stri)
            if !ispath(cache)
                try
                    mkdir(cache)
                    break
                catch
                end
            end
        end
        seekend(cacheindex)
        println(cacheindex, stri, ' ', source)
        flush(cacheindex)
        return cache
    end
end

function update(ignorecache::Bool=false, allow_remote::Bool=true)
    global sources, packages
    empty!(packages)
    for source in sources
        if source == "" || '\r' in source || '\n' in source
            continue
        end
        cache = getcachedir(source)
        function cacheget(path::AbstractString, never_cache::Bool)
            gunzip = false
            path2 = joinpath(cache,escape(path))
            if endswith(path2, ".gz")
                path2 = path2[1:end-3]
                gunzip = true
            end
            if !(ignorecache || (never_cache && allow_remote)) && isfile(path2)
                return read(path2, String)
            end
            if !allow_remote
                @warn("skipping $path, not in cache -- call WinRPM.update() to download")
                return nothing
            end
            @info("Downloading $source/$path")
            data = download("$source/$path")
            if data[2] != 200
                @warn("received error $(data[2]) while downloading $source/$path")
                return nothing
            end
            body = gunzip ? transcode(GzipDecompressor,Vector{UInt8}(data[1])) : data[1]
            open(path2, "w") do f
                write(f, body)
            end
            return String(body)
        end
        repomd = cacheget("repodata/repomd.xml", true)
        if repomd === nothing
           continue
        end
        xml = xp_parse(repomd)
        try
            for data = xml[xpath"/repomd/data[@type='primary']"]
                primary = cacheget(data[xpath"location/@href"][1], false)
                if primary === nothing
                    continue
                end
                pkgs = xp_parse(primary)[xpath"package[@type='rpm']"]
                pkgs["/"][1].attr["url"] = source
                for pkg in pkgs[xpath".[arch='noarch' or arch='src'][starts-with(name,'mingw32-') or starts-with(name, 'mingw64-')]"]
                    name = pkg[xpath"name"][1]
                    arch = pkg[xpath"arch"][1]
                    new_arch, new_name = split(LibExpat.string_value(name), '-', limit=2)
                    old_arch = LibExpat.string_value(arch)
                    if old_arch != "noarch"
                        new_arch = "$new_arch-$old_arch"
                    end
                    push!(empty!(name.elements), new_name)
                    push!(empty!(arch.elements), new_arch)
                end
                append!(packages,pkgs)
            end
        catch err
            @warn("encounted invalid data while parsing repomd")
            rethrow(err)
            continue
        end
    end
end

struct Package
    pd::ETree
end
Package(p::Package) = p
Package(p::Vector{ETree}) = [Package(pkg) for pkg in p]

getindex(pkg::Package,x) = getindex(pkg.pd, x)

struct Packages{T<:Union{Set{ETree},Vector{ETree}}}
    p::T
end
Packages(pkgs::Vector{Package}) = Packages([p.pd for p in pkgs])
Packages(xpath::LibExpat.XPath) = Packages(packages[xpath])

getindex(pkg::Packages,x) = Package(getindex(pkg.p,x))
Base.length(pkg::Packages) = length(pkg.p)
Base.isempty(pkg::Packages) = isempty(pkg.p)
function Base.iterate(pkg::Packages)
    res = iterate(pkg.p)
    res === nothing && return res
    p, s = res
    (Package(p), s)
end
function Base.iterate(pkg::Packages, x)
    res = iterate(pkg.p, x)
    res === nothing && return res
    p, s = res
    (Package(p), s)
end

function show(io::IO, pkg::Package)
    println(io,"WinRPM Package: ")
    println(io,"  Name: ", names(pkg))
    println(io,"  Summary: ", LibExpat.string_value(pkg["summary"][1]))
    ver = pkg["version"][1]
    println(io,"  Version: ", ver.attr["ver"], " (rel ", ver.attr["rel"], ")")
    println(io,"  Arch: ", LibExpat.string_value(pkg["arch"][1]))
    println(io,"  URL: ", LibExpat.string_value(pkg["url"][1]))
    println(io,"  License: ", LibExpat.string_value(pkg["format/rpm:license"][1]))
    print(io,"  Description: ", replace(LibExpat.string_value(pkg["description"][1]), r"\r\n|\r|\n" => "\n    "))
end

function show(io::IO, pkgs::Packages)
    print(io, "WinRPM Package Set:")
    if isempty(pkgs)
        print(io, "\n  <empty>")
    else
        for (i,pkg) = enumerate(pkgs)
            name = names(pkg)
            summary = LibExpat.string_value(pkg["summary"][1])
            arch = LibExpat.string_value(pkg["arch"][1])
            print(io,"\n  $i. $name ($arch) - $summary")
        end
    end
end

names(pkg::Package) = LibExpat.string_value(pkg["name"][1])
names(pkgs::Packages) = [names(pkg) for pkg in pkgs]

function select(pkgs::Packages, pkg::AbstractString)
    if length(pkgs) == 0
        error("Package candidate for $pkg not found")
    elseif length(pkgs) == 1
        pkg = pkgs[1]
    else
        @info("Multiple package candidates found for $pkg, picking newest.")
        epochs = [getepoch(pkg) for pkg in pkgs]
        pkgs = pkgs[findall(in(maximum(epochs)), epochs)]
        if length(pkgs) > 1
            versions = [convert(RPMVersionNumber, pkg[xpath"version/@ver"][1]) for pkg in pkgs]
            pkgs = pkgs[versions .== Ref(maximum(versions))]
            if length(pkgs) > 1
                release = [VersionNumber(pkg[xpath"version/@rel"][1]) for pkg in pkgs]
                pkgs = pkgs[release .== Ref(maximum(release))]
                if length(pkgs) > 1
                    @warn("Multiple package candidates have the same version, picking one at random")
                end
            end
        end
        pkg = pkgs[1]
    end
    pkg
end

lookup(name::AbstractString, arch::AbstractString=OS_ARCH) =
    Packages(xpath".[name='$name']['$arch'='' or arch='$arch']")

search(x::AbstractString, arch::AbstractString=OS_ARCH) =
    Packages(xpath".[contains(name,'$x') or contains(summary,'$x') or contains(description,'$x')]['$arch'='' or arch='$arch']")

whatprovides(file::AbstractString, arch::AbstractString=OS_ARCH) =
    Packages(xpath".[format/file[contains(text(),'$file')]]['$arch'='' or arch='$arch']")

rpm_provides(requires::AbstractString) =
    Packages(xpath".[format/rpm:provides/rpm:entry[@name='$requires']]")

function rpm_provides(requires::Union{Vector{T},Set{T}}) where T<:AbstractString
    pkgs = Set{ETree}()
    for x in requires
        pkgs_ = rpm_provides(x)
        if isempty(pkgs_)
            @warn("Package not found that provides $x")
        else
            push!(pkgs, select(pkgs_,x).pd)
        end
    end
    Packages(pkgs)
end

rpm_requires(x::Package) = x[xpath"format/rpm:requires/rpm:entry/@name"]

function rpm_requires(xs::Union{Vector{Package},Set{Package},Packages})
    requires = Set{AbstractString}()
    for x in xs
        union!(requires, rpm_requires(x))
    end
    requires
end

function rpm_url(pkg::Package)
    baseurl = pkg[xpath"/@url"][1]
    arch = pkg[xpath"string(arch)"][1]
    href = pkg[xpath"location/@href"][1]
    url = baseurl, href
end

function rpm_ver(pkg::Union{Package,ETree})
    return (pkg[xpath"version/@ver"][1],
            pkg[xpath"version/@rel"][1],
            pkg[xpath"version/@epoch"][1])
end

struct RPMVersionNumber
    s::AbstractString
end
Base.convert(::Type{RPMVersionNumber}, s::AbstractString) = RPMVersionNumber(s)
Base.:(<)(a::RPMVersionNumber, b::RPMVersionNumber) = false
Base.:(==)(a::RPMVersionNumber, b::RPMVersionNumber) = true
Base.:(<=)(a::RPMVersionNumber, b::RPMVersionNumber) = (a == b) || (a < b)
Base.:(>)(a::RPMVersionNumber, b::RPMVersionNumber) = !(a <= b)
Base.:(>=)(a::RPMVersionNumber, b::RPMVersionNumber) = !(a < b)
Base.:(!=)(a::RPMVersionNumber, b::RPMVersionNumber) = !(a == b)
Base.isless(a::RPMVersionNumber, b::RPMVersionNumber) = (a < b)

function getepoch(pkg::Package)
    epoch = pkg[xpath"version/@epoch"]
    if isempty(epoch)
        0
    else
        parse(Int, epoch[1])
    end
end

deps(pkg::AbstractString, arch::AbstractString=OS_ARCH) = deps(select(lookup(pkg, arch), pkg))
function deps(pkg::Union{Package,Packages})
    add = rpm_provides(rpm_requires(pkg))
    local packages::Vector{ETree}
    reqd = AbstractString[]
    if isa(pkg, Packages)
        packages = ETree[p for p in pkg.p]
    else
        packages = ETree[pkg.pd]
    end
    packages = union(packages, add.p)
    while !isempty(add)
        reqs = setdiff(rpm_requires(add), reqd)
        append!(reqd, reqs)
        add = Packages(setdiff(rpm_provides(reqs).p, packages))
        for p in add
            push!(packages, p.pd)
        end
    end
    return Packages(packages)
end

install(pkg::AbstractString, arch::AbstractString=OS_ARCH; yes=false) = install(select(lookup(pkg, arch), pkg); yes=yes)

function install(pkgs::Vector{T}, arch::AbstractString=OS_ARCH; yes=false) where T<:AbstractString
    todo = Package[]
    for pkg in pkgs
        push!(todo, select(lookup(pkg, arch), pkg))
    end
    install(Packages(todo); yes=yes)
end

function install(pkg::Union{Package,Packages}; yes=false)
    todo, toup = prepare_install(pkg)
    if isempty(todo) && isempty(toup)
        @info("Nothing to do")
    else
        if !isempty(toup)
            @info("Packages to update:  ", join(names(toup), ", "))
            yesold = yes || prompt_ok("Continue with updates")
        else
            yesold = false
        end
        if !isempty(todo)
            @info("Packages to install: ", join(names(todo), ", "))
            yesnew = yes || prompt_ok("Continue with install")
        else
            yesnew = false
        end
        if yesold
            do_install(toup)
        end
        if yesnew
            do_install(todo)
        end
        @info("Complete")
    end
end

function prepare_install(pkg::Union{Package,Packages})
    packages = deps(pkg).p
    open(installedlist, isfile(installedlist) ? "r+" : "w+") do installed
        seek(installed, 0)
        installed_list = Vector{AbstractString}[]
        for line in eachline(installed)
            ln = split(chomp(line), ' ', limit=2)
            if length(ln) == 2
                push!(installed_list, ln)
            end
        end
        toupdate = ETree[]
        filter!(packages) do p
            ver = replace(join(rpm_ver(p), ','), r"\s" => "")
            oldver = false
            for entry in p[xpath"format/rpm:provides/rpm:entry[@name]"]
                provides = entry.attr["name"]
                maybe_oldver = false
                anyver = false
                for installed in installed_list
                    if installed[2] == provides
                        anyver = true
                        if ver == installed[1]
                            maybe_oldver = false
                            break
                        end
                        maybe_oldver = true
                    end
                end
                if !anyver
                    return true
                end
                oldver |= maybe_oldver
            end
            if oldver
                push!(toupdate, p)
            end
            return false
        end
        toup = Packages(reverse!(toupdate))
        todo = Packages(reverse!(packages))
        return todo, toup
    end
end

function do_install(packages::Packages)
    for package in packages
        do_install(package)
    end
end

const exe7z = if Sys.iswindows()
    if isdefined(Base, :LIBEXECDIR)
        joinpath(Sys.BINDIR, Base.LIBEXECDIR, "7z.exe")
    else
        joinpath(Sys.BINDIR, "7z.exe")
    end
else
    if isdefined(Base, :LIBEXECDIR) && ispath(joinpath(Sys.BINDIR, Base.LIBEXECDIR, "7z"))
        joinpath(Sys.BINDIR, Base.LIBEXECDIR, "7z")
    elseif ispath(joinpath(Sys.BINDIR, "7z"))
        joinpath(Sys.BINDIR, "7z")
    else
        "7z"
    end
end

function do_install(package::Package)
    name = names(package)
    source, path = rpm_url(package)
    @info("Downloading: ", name)
    data = download("$source/$path")
    if data[2] != 200
        @info("try running WinRPM.update() and retrying the install")
        error("failed to download $name $(data[2]) from $source/$path.")
    end
    cache = getcachedir(source)
    path2 = joinpath(cache,escape(path))
    open(path2, "w") do f
        write(f, data[1])
    end
    @info("Extracting: ", name)

    cpio = splitext(joinpath(cache, escape(basename(path))))[1] * ".cpio"

    local err = nothing
    for cmd = [`$exe7z x -y $path2 -o$cache`, `$exe7z x -y $cpio -o$installdir`]
        proc = open(cmd, "r")
        out, pc = proc.out, proc.exitcode
        stdoutstr = read(out, String)
        if !success(proc)
            wait_close(out)
            println(stdoutstr)
            err = pc
            if Sys.isunix()
                cd(installdir) do
                    if success(`rpm2cpio $path2` | `cpio -imud`)
                        err = nothing
                    end
                end
            end
            isfile(cpio) && rm(cpio)
            err !== nothing && pipeline_error(err)
            break
        end
    end
    isfile(cpio) && rm(cpio)
    ver = replace(join(rpm_ver(package), ','), r"\s" => "")
    open(installedlist, isfile(installedlist) ? "r+" : "w+") do installed
        for entry in package[xpath"format/rpm:provides/rpm:entry[@name]"]
            provides = entry.attr["name"]
            seekend(installed)
            println(installed, ver, ' ', provides)
        end
        flush(installed)
    end
end

function prompt_ok(question)
    while true
        print(question)
        print(" [y/N]? ")
        ans = strip(readline(stdin))
        if isempty(ans) || ans[1] == 'n' || ans[1] == 'N'
            return false
        elseif ans[1] == 'y' || ans[1] == 'Y'
            return true
        end
        println("Please answer Y or N")
    end
end

include("winrpm_bindeps.jl")

# deprecations
@deprecate help() "Please see the README.md file at https://github.com/JuliaPackaging/WinRPM.jl"

end


text:
# This file was generated, do not modify it. # hide
knn = machine(KnnPipe(), Xtrain, ytrain)
multi = machine(MnPipe(), Xtrain, ytrain)


text:
using Plots

gr()

function main()
    xs=[1,2,3,4,5]

    p1 = plot(xs, xs)
    p2 = plot(xs, xs.^2)
    p3 = plot(xs, xs.^3)
    p4 = plot(xs, xs.^4)

    plot(p1,p2,p3,p4,layout=(2,2))
end

main()

max_stars_repo_name: terasakisatoshi/juliaExer

max_issues_repo_name: terasakisatoshi/juliaExer

max_forks_repo_name: terasakisatoshi/juliaExer