text:
# Function to translate vector of parameters to struct

"""
    θ2par(θ, model)
Function two convert parameter vector to struct

* `θ`: Parameter vector
* `model`: Model specification CountModel, INGARCHModel, INARMAModel, ...

Parameter vector needs to be of suitable length.

# Example
```julia-repl
model = Model(pastObs = 1) # Specify INARCH(1)
θ2par([10, 0.5], model)
```
"""
function θ2par(θ::Array{T, 1} where T<: AbstractFloat, model::INGARCHModel)
    p = length(model.pastObs)
    q = length(model.pastMean)
    r = length(model.external)
    nb = model.distr == "NegativeBinomial"
    zi = model.zi

    nPar = 1 + p + q + r + nb + zi

    if length(θ) != nPar
        error("Length of θ does not match model specification.")
    end

    β0 = θ[1]
    used = 1
    α = θ[used + 1:used + p]
    used += p
    β = θ[used + 1:used + q]
    used += q
    η = θ[used + 1:used + r]
    used += r
    ϕ = θ[used + 1:used + nb]
    if length(ϕ) == 0
        ϕ = Array{Float64, 1}([])
    end
    if zi
        ω = θ[end]
    else
        ω = 0.0
    end

    parameter(β0, α, β, η, ϕ, ω)
end

function θ2par(θ::Array{T, 1} where T<: AbstractFloat, model::INARCHModel)
    p = length(model.pastObs)
    r = length(model.external)
    nb = model.distr == "NegativeBinomial"
    zi = model.zi

    nPar = 1 + p + r + nb + zi

    if length(θ) != nPar
        error("Length of θ does not match model specification.")
    end

    β0 = θ[1]
    used = 1
    α = θ[used + 1:used + p]
    used += p
    η = θ[used + 1:used + r]
    used += r
    ϕ = θ[used + 1:used + nb]
    if length(ϕ) == 0
        ϕ = Array{Float64, 1}([])
    end
    if zi
        ω = θ[end]
    else
        ω = 0.0
    end

    parameter(β0, α, Array{Float64, 1}([]), η, ϕ, ω)
end

function θ2par(θ::Array{T, 1} where T<: AbstractFloat, model::IIDModel)
    r = length(model.external)
    nb = model.distr == "NegativeBinomial"
    zi = model.zi

    nPar = 1 + r + nb + zi

    if length(θ) != nPar
        error("Length of θ does not match model specification.")
    end

    β0 = θ[1]
    used = 1
    η = θ[used + 1:used + r]
    used += r
    ϕ = θ[used + 1:used + nb]
    if length(ϕ) == 0
        ϕ = Array{Float64, 1}([])
    end
    if zi
        ω = θ[end]
    else
        ω = 0.0
    end

    parameter(β0, Array{Float64, 1}([]), Array{Float64, 1}([]), η, ϕ, ω)
end

function θ2par(θ::Array{T, 1} where T<: AbstractFloat, model::INARMAModel)
    p = length(model.pastObs)
    q = length(model.pastMean)
    r = length(model.external)
    nb1 = model.distr[1] == "NegativeBinomial"
    nb2 = model.distr[2] == "NegativeBinomial"
    if r == 0
        nb2 = false
    else
        if sum(model.external) == 0
            nb2 = false
        end
    end
    zi = model.zi

    nPar = 1 + p + q + r + nb1 + nb2 + zi

    if length(θ) != nPar
        error("Length of θ does not match model specification.")
    end

    β0 = θ[1]
    used = 1
    α = θ[used + 1:used + p]
    used += p
    β = θ[used + 1:used + q]
    used += q
    η = θ[used + 1:used + r]
    used += r
    ϕtemp = θ[used + 1:used + nb1 + nb2]
    if length(ϕtemp) == 2
        ϕ = ϕtemp
    end
    if length(ϕtemp) == 1
        ϕ = repeat(ϕtemp, 2)
    end
    if length(ϕtemp) == 0
        ϕ =  Array{Float64, 1}([])
    end
    used += nb1 + nb2
    if zi
        ω = θ[end]
    else
        ω = 0.0
    end

    parameter(β0, α, β, η, ϕ, ω)
end

function θ2par(θ::Array{T, 1} where T<: AbstractFloat, model::INARModel)
    p = length(model.pastObs)
    r = length(model.external)
    nb1 = model.distr[1] == "NegativeBinomial"
    nb2 = model.distr[2] == "NegativeBinomial"
    if r == 0
        nb2 = false
    else
        if sum(model.external) == 0
            nb2 = false
        end
    end
    zi = model.zi

    nPar = 1 + p + r + nb1 + nb2 + zi

    if length(θ) != nPar
        error("Length of θ does not match model specification.")
    end

    β0 = θ[1]
    used = 1
    α = θ[used + 1:used + p]
    used += p
    η = θ[used + 1:used + r]
    used += r
    ϕtemp = θ[used + 1:used + nb1 + nb2]
    if length(ϕtemp) == 2
        ϕ = ϕtemp
    end
    if length(ϕtemp) == 1
        ϕ = repeat(ϕtemp, 2)
    end
    if length(ϕtemp) == 0
        ϕ = Array{Float64, 1}([])
    end
    used += nb1 + nb2
    if zi
        ω = θ[end]
    else
        ω = 0.0
    end

    parameter(β0, α, Array{Float64, 1}([]), η, ϕ, ω)
end

function θ2par(θ::Array{T, 1} where T<: AbstractFloat, model::INMAModel)
    q = length(model.pastMean)
    r = length(model.external)
    nb1 = model.distr[1] == "NegativeBinomial"
    nb2 = model.distr[2] == "NegativeBinomial"
    if r == 0
        nb2 = false
    else
        if sum(model.external) == 0
            nb2 = false
        end
    end
    zi = model.zi

    nPar = 1 + q + r + nb1 + nb2 + zi

    if length(θ) != nPar
        error("Length of θ does not match model specification.")
    end

    β0 = θ[1]
    used = 1
    β = θ[used + 1:used + q]
    used += q
    η = θ[used + 1:used + r]
    used += r
    ϕtemp = θ[used + 1:used + nb1 + nb2]
    if length(ϕtemp) == 2
        ϕ = ϕtemp
    end
    if length(ϕtemp) == 1
        ϕ = repeat(ϕtemp, 2)
    end
    if length(ϕtemp) == 0
        ϕ = Array{Float64, 1}([])
    end
    used += nb1 + nb2
    if zi
        ω = θ[end]
    else
        ω = 0.0
    end

    parameter(β0, Array{Float64, 1}([]), β, η, ϕ, ω)
end

function θ2par(θ::Array{T, 1} where T<: Integer, model::T where T<:CountModel)
    θ2par(convert(Vector{Float64}, θ), model)
end


text:
type Plot{T<:GraphicsArea}
  graphics::T
  title::(@compat AbstractString)
  xlabel::(@compat AbstractString)
  ylabel::(@compat AbstractString)
  margin::Int
  padding::Int
  border::Symbol
  leftLabels::Dict{Int,(@compat AbstractString)}
  leftColors::Dict{Int,Symbol}
  rightLabels::Dict{Int,(@compat AbstractString)}
  rightColors::Dict{Int,Symbol}
  decorations::Dict{Symbol,(@compat AbstractString)}
  decoColors::Dict{Symbol,Symbol}
  showLabels::Bool
  autocolor::Int
end

function Plot{T<:GraphicsArea}(graphics::T;
                               title::(@compat AbstractString) = "",
                               xlabel::(@compat AbstractString) = "",
                               ylabel::(@compat AbstractString) = "",
                               margin::Int = 3,
                               padding::Int = 1,
                               border::Symbol = :solid,
                               showLabels = true)
  rows = nrows(graphics)
  cols = ncols(graphics)
  leftLabels = Dict{Int,(@compat AbstractString)}()
  leftColors = Dict{Int,Symbol}()
  rightLabels = Dict{Int,(@compat AbstractString)}()
  rightColors = Dict{Int,Symbol}()
  decorations = Dict{Symbol,(@compat AbstractString)}()
  decoColors = Dict{Symbol,Symbol}()
  Plot{T}(graphics, title, xlabel, ylabel,
          margin, padding, border,
          leftLabels, leftColors, rightLabels, rightColors,
          decorations, decoColors, showLabels, 0)
end

function nextColor!{T<:GraphicsArea}(plot::Plot{T})
  curColor = autoColors[plot.autocolor+1]
  plot.autocolor = ((plot.autocolor + 1) % length(autoColors))
  curColor
end

function title{T<:GraphicsArea}(plot::Plot{T})
  plot.title
end

function title!{T<:GraphicsArea}(plot::Plot{T}, title::(@compat AbstractString))
  plot.title = title
  plot
end

function xlabel{T<:GraphicsArea}(plot::Plot{T})
  plot.xlabel
end

function xlabel!{T<:GraphicsArea}(plot::Plot{T}, xlabel::(@compat AbstractString))
  plot.xlabel = xlabel
  plot
end

function ylabel{T<:GraphicsArea}(plot::Plot{T})
  plot.ylabel
end

function ylabel!{T<:GraphicsArea}(plot::Plot{T}, ylabel::(@compat AbstractString))
  plot.ylabel = ylabel
  plot
end

function autoAnnotate!{T<:GraphicsArea}(plot::Plot{T}, where::Symbol, value::(@compat AbstractString), color::Symbol=:white)
  for row = 1:nrows(plot.graphics)
    if where == :l
      if(!haskey(plot.leftLabels, row) || plot.leftLabels[row] == "")
        plot.leftLabels[row] = value
        plot.leftColors[row] = color
        return plot
      end
    elseif where == :r
      if(!haskey(plot.rightLabels, row) || plot.rightLabels[row] == "")
        plot.rightLabels[row] = value
        plot.rightColors[row] = color
        return plot
      end
    else
      throw(ArgumentError("Unknown location: try one of these :l :r"))
    end
  end
  plot
end

function annotate!{T<:GraphicsArea}(plot::Plot{T}, where::Symbol, value::(@compat AbstractString), color::Symbol=:white)
  where == :t || where == :b || where == :tl || where == :tr || where == :bl || where == :br || throw(ArgumentError("Unknown location: try one of these :tl :t :tr :bl :b :br"))
  plot.decorations[where] = value
  plot.decoColors[where] = color
  plot
end

function annotate!{T<:GraphicsArea}(plot::Plot{T}, where::Symbol, value::(@compat AbstractString); color::Symbol=:white)
  annotate!(plot, where, value, color)
end

function annotate!{T<:GraphicsArea}(plot::Plot{T}, where::Symbol, row::Int, value::(@compat AbstractString), color::Symbol=:white)
  if where == :l
    plot.leftLabels[row] = value
    plot.leftColors[row] = color
  elseif where == :r
    plot.rightLabels[row] = value
    plot.rightColors[row] = color
  else
    throw(ArgumentError("Unknown location: try one of these :l :r"))
  end
  plot
end

function annotate!{T<:GraphicsArea}(plot::Plot{T}, where::Symbol, row::Int, value::(@compat AbstractString); color::Symbol=:white)
  annotate!(plot, where, row, value, color)
end

function drawLine!{T<:Canvas}(plot::Plot{T}, args...; vars...)
  drawLine!(plot.graphics, args...; vars...)
  plot
end

function setPixel!{T<:Canvas}(plot::Plot{T}, args...; vars...)
  setPixel!(plot.graphics, args...; vars...)
  plot
end

function setPoint!{T<:Canvas}(plot::Plot{T}, args...; vars...)
  setPoint!(plot.graphics, args...; vars...)
  plot
end

function drawTitle(io::IO, padding::(@compat AbstractString), title::(@compat AbstractString); plotWidth::Int=0)
  if title != ""
    offset = safeRound(plotWidth / 2 - length(title) / 2)
    offset = offset > 0 ? offset: 0
    tpad = repeat(spceStr, offset)
    print_with_color(:white, io, padding, tpad, title, "\n")
  end
end

function drawBorderTop(io::IO, padding::(@compat AbstractString), length::Int, border = :solid)
  b = borderMap[border]
  border == :none || print(io, padding, b[:tl], repeat(b[:t], length), b[:tr])
end

function drawBorderBottom(io::IO, padding::(@compat AbstractString), length::Int, border = :solid)
  b = borderMap[border]
  border == :none || print(io, padding, b[:bl], repeat(b[:b], length), b[:br])
end

function show(io::IO, p::Plot)
  b = borderMap[p.border]
  c = p.graphics
  borderLength = ncols(c)

  # get length of largest strings to the left and right
  maxLen = p.showLabels && !isempty(p.leftLabels) ? maximum([length(string(l)) for l in values(p.leftLabels)]) : 0
  maxLenR = p.showLabels && !isempty(p.rightLabels) ? maximum([length(string(l)) for l in values(p.rightLabels)]) : 0
  if p.showLabels && p.ylabel != ""
    maxLen += length(p.ylabel) + p.padding
  end

  # offset where the plot (incl border) begins
  plotOffset = maxLen + p.margin + p.padding

  # padding-string from left to border
  plotPadding = repeat(spceStr, p.padding)

  # padding-string between labels and border
  borderPadding = repeat(spceStr, plotOffset)

  # plot the title and the top border
  drawTitle(io, borderPadding, p.title, plotWidth = borderLength)
  if p.showLabels
    topLeftStr = haskey(p.decorations, :tl) ? p.decorations[:tl] : ""
    topLeftCol = haskey(p.decoColors, :tl) ? p.decoColors[:tl] : :white
    topMidStr = haskey(p.decorations, :t) ? p.decorations[:t] : ""
    topMidCol = haskey(p.decoColors, :t) ? p.decoColors[:t] : :white
    topRightStr = haskey(p.decorations, :tr) ? p.decorations[:tr] : ""
    topRightCol = haskey(p.decoColors, :tr) ? p.decoColors[:tr] : :white
    if topLeftStr != "" || topRightStr != "" || topMidStr != ""
      topLeftLen = length(topLeftStr)
      topMidLen = length(topMidStr)
      topRightLen = length(topRightStr)
      print_with_color(topLeftCol, io, borderPadding, topLeftStr)
      cnt = safeRound(borderLength / 2 - topMidLen / 2 - topLeftLen)
      pad = cnt > 0 ? repeat(spceStr, cnt) : ""
      print_with_color(topMidCol, io, pad, topMidStr)
      cnt = borderLength - topRightLen - topLeftLen - topMidLen + 2 - cnt
      pad = cnt > 0 ? repeat(spceStr, cnt) : ""
      print_with_color(topRightCol, io, pad, topRightStr, "\n")
    end
  end
  drawBorderTop(io, borderPadding, borderLength, p.border)
  print(io, repeat(spceStr, maxLenR), plotPadding, "\n")

  # compute position of ylabel
  ylabRow = safeFloor(nrows(c) / 2)

  # plot all rows
  for row in 1:nrows(c)
    # Current labels to left and right of the row and their length
    tleftLabel = haskey(p.leftLabels,row) ? p.leftLabels[row] : ""
    tleftCol = haskey(p.leftColors,row) ? p.leftColors[row] : :white
    tRightLabel = haskey(p.rightLabels,row) ? p.rightLabels[row] : ""
    tRightCol = haskey(p.rightColors,row) ? p.rightColors[row] : :white
    tLen = length(tleftLabel)
    tLenR = length(tRightLabel)
    # print left annotations
    print(io, repeat(spceStr, p.margin))
    if p.showLabels
      if row == ylabRow
        # print ylabel
        print_with_color(:white, io, p.ylabel)
        print(io, repeat(spceStr, maxLen - length(p.ylabel) - tLen))
      else
        # print padding to fill ylabel length
        print(io, repeat(spceStr, maxLen - tLen))
      end
      # print the left annotation
      print_with_color(tleftCol, io, tleftLabel)
    end
    # print left border
    print(io, plotPadding, b[:l])
    # print canvas row
    printRow(io, c, row)
    #print right label and padding
    print(io, b[:r])
    if p.showLabels
      print(io, plotPadding)
      print_with_color(tRightCol, io, tRightLabel)
      print(io, repeat(spceStr, maxLenR - tLenR))
    end
    print(io, "\n")
  end

  # draw bottom border and bottom labels
  drawBorderBottom(io, borderPadding, borderLength, p.border)
  print(io, repeat(spceStr, maxLenR), plotPadding, "\n")
  if p.showLabels
    botLeftStr = haskey(p.decorations, :bl) ? p.decorations[:bl] : ""
    botLeftCol = haskey(p.decoColors, :bl) ? p.decoColors[:bl] : :white
    botMidStr = haskey(p.decorations, :b) ? p.decorations[:b] : ""
    botMidCol = haskey(p.decoColors, :b) ? p.decoColors[:b] : :white
    botRightStr = haskey(p.decorations, :br) ? p.decorations[:br] : ""
    botRightCol = haskey(p.decoColors, :br) ? p.decoColors[:br] : :white
    if botLeftStr != "" || botRightStr != "" || botMidStr != ""
      botLeftLen = length(botLeftStr)
      botMidLen = length(botMidStr)
      botRightLen = length(botRightStr)
      print_with_color(botLeftCol, io, borderPadding, botLeftStr)
      cnt = safeRound(borderLength / 2 - botMidLen / 2 - botLeftLen)
      pad = cnt > 0 ? repeat(spceStr, cnt) : ""
      print_with_color(botMidCol, io, pad, botMidStr)
      cnt = borderLength - botRightLen - botLeftLen - botMidLen + 2 - cnt
      pad = cnt > 0 ? repeat(spceStr, cnt) : ""
      print_with_color(botRightCol, io, pad, botRightStr, "\n")
    end
    # abuse the drawtitle function to print the xlabel. maybe refactor this
    drawTitle(io, borderPadding, p.xlabel, plotWidth = borderLength)
  end
end

ext: jl


text:
include("lib.jl")

function simulate(d, n = 10_000, n_sims = 20_000)
    x = Array(Float64, n)
    y = Array(Float64, n)

    methods = Dict(
        "unadjusted" => unadjusted,
        "trim_shared_threshold" => trim_shared_threshold,
        "trim_separate_thresholds" => trim_separate_thresholds,
        "trim_threshold_from_A" => trim_threshold_from_A,
        "winsorize_shared_threshold" => winsorize_shared_threshold,
        "winsorize_separate_thresholds" => winsorize_separate_thresholds,
        "winsorize_threshold_from_A" => winsorize_threshold_from_A,
    )

    header = (
        "n",
        "method",
        "w",
        "te",
        "se",
        "delta",
        "sigma",
        "pvalue",
        "lower",
        "upper",
    )
    println(join(header, "\t"))

    for te in (0.00, 0.05, 0.10)
        se = sqrt(var(d) / n + var(d) / n)
        for i in 1:n_sims
            gen_data!(x, y, d, te)
            for w in 0.60:0.01:0.99
                τ_x = threshold(x, w)
                τ_y = threshold(y, w)
                τ_xy = threshold(vcat(x, y), w)
                for (method, method_func) in methods
                    x′, y′ = method_func(x, y, τ_x, τ_y, τ_xy)
                    δ, σ, p, lower, upper = analyze(x′, y′)
                    row = (n, method, 1 - w, te, se, δ, σ, p, lower, upper)
                    println(join(row, "\t"))
                end
            end
        end
    end
end

d = LogNormal(0, 1)
simulate(d)


text:
# These functions set visual attributes of HObjects

export set_color, set_radius, set_thickness, set_line_style
export set_fill_alpha, set_fill_color, set_no_fill

export copy_attr

"""
`set_color(X,col)` sets the color of the hyperbolic object `X`.
"""
function set_color(P::HPoint, col=:black)
    P[:markercolor] = col
    P[:markerstrokecolor] = col
    nothing
end

function set_color(P::HObject, col=:black)
    P[:linecolor] = col
end

"""
`set_radius(P,rad)` sets the radius for an `HPoint`.
The default is 1.
"""
function set_radius(P::HPoint, rad=1)
    P[:marker] = rad
end

"""
`set_thickness(X,thk)` sets the thickness of the line used
to draw the hyperbolic object `X`. Default is 1.
"""
function set_thickness(P::HObject, thk = 1)
    P[:linewidth] = thk
end

"""
`set_line_style(X,style)` sets the line style for drawing
`X`. Default is `:solid`.
"""
function set_line_style(P::HObject, style = :solid)
    P[:linestyle] = style
end

"""
`set_fill_color(X,col)` sets the color used to fill
`X`. Works for `HPlane` and `HCircle`. Default is `:yellow`
"""
function set_fill_color(P::HRound, col = :yellow)
    P[:fillrange] = 1
    P[:fillcolor] = col
end

"""
`set_fill_alpha(X,alpha)` sets the alpha value for the object's
fill. Only works for `HPlane` and `HCircle`. Default value is 1
(not transparent)
"""
function set_fill_alpha(P::HRound, alf = 1)  # 1 = solid color
    P[:fillalpha] = alf
end

"""
`set_no_fill(X)` removes fill from the `HObject`. It now
draws as unfilled.
"""
function set_no_fill(P::HRound)
    delete!(P.attr,:fillcolor)
    delete!(P.attr,:fillalpha)
    delete!(P.attr,:fillrange)
    nothing
end

"""
`copy_attr(A,B)` copies the attributes assigned to `B` into `A`.
"""
function copy_attr(A::HObject, B::HObject)
    for k in keys(B.attr)
        A[k] = B[k]
    end
end


text:
# Tensordot.contract implements numpy.tensordot for Julia
# numpy.einsum-like interface is available in einengine.jl.

"""
    contract(T1, T2, axesL, axesR)
Contracts tensor 'T1' with 'T2' in axes specified in axesL and axesR.
"""
contract(TL, TR, axesL::Array{Int}, axesR::Array{Int}) = begin
    # Gets shape and axes information from helper.
    shapeInfo = contractprep(size(TL), size(TR), axesL, axesR)

    # Apply transformation
    contractraw(TL, TR, shapeInfo...)
end

# Raw contraction function.
function contractraw(TL, TR, permL::Array{Int}, permR::Array{Int},
        shapeL::Tuple, shapeR::Tuple, extL::Array, extR::Array)

    # Multiply and restore to original shape.
    reshape((reshape(permutedims(TL, permL), shapeL) *
        reshape(permutedims(TR, permR), shapeR)), (extL..., extR...))
end

"Index preparations for contracting."
function contractprep(shapeL::Tuple, shapeR::Tuple, axesL::Array{Int}, axesR::Array{Int})
    shapeL = [shapeL...]
    shapeR = [shapeR...]
    # TODO: Check axes boundary.

    # Dumb index permuting & size extraction
    dumbperm(shape::Array{Int}, pick::Array{Int}) = begin
        sbarrier = sort(pick) .+ 1
        ebarrier = sort(pick) .- 1
        sbarrier = vcat([1], sbarrier)
        ebarrier = vcat(ebarrier, length(shape))
        regular = Int[]
        for i = 1:length(sbarrier)
            append!(regular, sbarrier[i]:ebarrier[i])
        end # for
        return regular
    end # dumbperm

    # External permutation
    permL = dumbperm(shapeL, axesL)
    permR = dumbperm(shapeR, axesR)
    # External shape
    extL = [shapeL[i] for i in permL]
    extR = [shapeR[i] for i in permR]
    # Contractional permutation
    append!(permL, axesL)
    prepend!(permR, axesR)
    outerL = if (length(extL)==0) 1 else reduce(*, extL) end
    outerR = if (length(extR)==0) 1 else reduce(*, extR) end
    innerL = reduce(*, [shapeL[i] for i in axesL])
    innerR = reduce(*, [shapeR[i] for i in axesR])

    permL, permR, (outerL, innerL), (innerR, outerR), extL, extR
end # contract

# Adjoint of contract should refrain from digging into index processing.
@adjoint contractprep(shapeL::Tuple, shapeR::Tuple, axesL::Array{Int}, axesR::Array{Int}) = begin
    contractprep(shapeL, shapeR, axesL, axesR), _ -> nothing
end

#= Further simplification for multiple gradients.
   Disabled by default.

@adjoint permutedims(A, iperm) = begin
    permutedims(A, iperm), dA -> begin
        rperm = invperm(iperm)
        (permutedims(dA, rperm), nothing)
    end
end

@adjoint invperm(iperm) = begin
    invperm(iperm), _ -> nothing
end
=#


text:
include("src/base.jl")
include("src/envs/classiccontrol_mdp.jl")
include("src/policies/dqn_policy.jl")
include("src/solvers/dqn_policy_solver.jl")

gym_env = ClassicControlMDP(gym.make("CartPole-v0")[:env])
solver = DQNPolicySolver(500; nnet_spec = Int64[4], epsilon = 0.25, learning_rate = 0.00001, discount = 0.1)
policy = solve(solver, gym_env; verbose = true)

max_stars_repo_name: dmitrijsc/practical-rl

max_issues_repo_name: dmitrijsc/practical-rl

max_forks_repo_name: dmitrijsc/practical-rl


text:
#####
##### Invalidations (needed first due to package loading effects)
#####

import SnoopCompileCore
invalidations = SnoopCompileCore.@snoopr begin

    include("rep_workload.jl") # change to `include("test/rep_workload.jl")` for local run

end;

if !("." in LOAD_PATH)
    push!(LOAD_PATH, ".")
end
import ReportMetrics
ReportMetrics.report_invalidations(;
    job_name = "RA_example_inv",
    invalidations,
    process_filename = x -> last(split(x, "packages/")),
)
ReportMetrics.report_invalidations(;
    job_name = "RA_example_inv",
    invalidations,
    process_filename = x -> last(split(x, "packages/")),
    n_rows = 3,
)

using Test

ma_dir = pkgdir(ReportMetrics)

@testset "ReportMetrics" begin
    ReportMetrics.report_allocs(;
        job_name = "RA_example",
        run_cmd = `$(Base.julia_cmd()) --project --track-allocation=all $(joinpath(ma_dir, "test", "rep_workload.jl"))`,
        deps_to_monitor = [ReportMetrics],
        dirs_to_monitor = [joinpath(ma_dir, "test")],
        process_filename = x -> replace(x, dirname(ma_dir) => ""),
    )

    ReportMetrics.report_allocs(;
        job_name = "RA_example_csv",
        run_cmd = `$(Base.julia_cmd()) --project --track-allocation=all $(joinpath(ma_dir, "test", "rep_workload.jl"))`,
        deps_to_monitor = [ReportMetrics],
        dirs_to_monitor = [joinpath(ma_dir, "test")],
        process_filename = x -> replace(x, dirname(ma_dir) => ""),
        write_csv = true,
    )

    ReportMetrics.report_allocs(;
        job_name = "RA_example",
        run_cmd = `$(Base.julia_cmd()) --project --track-allocation=all $(joinpath(ma_dir, "test", "rep_workload.jl"))`,
        deps_to_monitor = [ReportMetrics],
        dirs_to_monitor = [joinpath(ma_dir, "test")],
        process_filename = x -> replace(x, dirname(ma_dir) => ""),
        n_unique_allocs = 1,
    )
end


text:
using Flux, RoME, RoMEPlotting
using ImageMagick, Images, ImageView, ImageDraw
using Colors
using JSON2

using FreeTypeAbstraction, AprilTags

function imdrawline!(img, col; color=RGB(1.0,0.0,0.0) )
  for i in 1:size(img, 1)
    img[i, col] = color
  end
end

cd("/media/dehann/logs3/caesar/2020-08-19T11:45:38.074")

fg = loadDFG("fg_final_resolve.tar.gz")

# wire up the blob store
getSolverParams(fg).logpath = pwd()
storeDir = joinLogPath(fg,"data")
mkpath(storeDir)
datastore = FolderStore{Vector{UInt8}}(:default_folder_store, storeDir)
addBlobStore!(fg, datastore)

# Retrieve camera calibration
listDataEntries(fg, :x0)
rD = getData(fg, :x0, :camCalib)
camCalib = JSON2.read(IOBuffer(rD[2]))
K = reshape(camCalib[:vecK], camCalib[:size]...) .|> Float64

# get image from x1
imgE ,imgD = getData(fg, :x1, :KEYFRAME_IMG)
img = ImageMagick.readblob(imgD);
# check that we have the image
imshow(img)

# detect and draw tags
detector = AprilTagDetector()
tags = detector(img)

img_ = drawTags(img,K, tags)
imshow(img_)

ls(fg, :x1)

x1l2f1 = getFactorType(fg, :x1l2f1)

x1l2f1.z.μ

function calcBearingRange(dfg::AbstractDFG, from::Symbol, to::Symbol)
  getSofttype(dfg, from)
end

measimgcol = round(Int,camc-camf*tan(getfnctype(ve).bearing.μ)) # measurement center
imdrawline!(img, measimgcol)

# deptdist = Base.mean(depthcloud[260:270,measimgcol,3])
# push!(DISTS[fct], deptdist)


text:
export method, signature

method(class::Class, sel::Selector) =
  ccall(:class_getInstanceMethod, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}),
        class, sel)

method(obj::Object, sel::Selector) =
  method(class(obj), sel)

types(m::Ptr) =
  ccall(:method_getTypeEncoding, Ptr{Cchar}, (Ptr{Cvoid},), m) |> unsafe_string

implementation(m::Ptr) =
  ccall(:method_getImplementation, Ptr{Cvoid}, (Ptr{Cvoid},),
        m)

# From https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
const typeencodings = Dict('c' => Cchar,
                           'i' => Cint,
                           's' => Cshort,
                           'l' => Clong,
                           'q' => Clonglong,
                           'C' => Cuchar,
                           'I' => Cuint,
                           'S' => Cushort,
                           'L' => Culong,
                           'Q' => Culonglong,
                           'f' => Cfloat,
                           'd' => Cdouble,
                           'B' => Bool,
                           'v' => Cvoid,
                           '*' => Ptr{Cchar},
                           '@' => Object,
                           '#' => Class,
                           ':' => Selector,
                           '^' => Ptr)

# Other modifiers
# r const
# n in
# N inout
# o out
# O bycopy
# R byref
# V oneway
# Numbers a stack size + offset, now obsolete
const skip = Set(['r', 'V',
                  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])

function nexttype(io::IO)
  c = read(io, Char)
  c in skip && return
  haskey(typeencodings, c) || error("Unsupported method type: $(takebuf_string(io))")
  t = typeencodings[c]
  t == Ptr && (t = Ptr{nexttype(io)})
  return t
end

function parseencoding(io::IO)
  types = []
  while !eof(io)
    t = nexttype(io)
    t == nothing || push!(types, t)
  end
  return types
end

parseencoding(s::String) = parseencoding(IOBuffer(s))

signature(m::Ptr) = m |> types |> parseencoding

function signature(class::Class, sel::Selector)
  m = method(class, sel)
  m == C_NULL && error("$class doesn't respond to $sel")
  signature(m)
end

signature(obj::Object, sel::Selector) =
  signature(class(obj), sel)

# Creating Methods

const revtypeencodings = Dict(v => k for (k, v) in typeencodings)

function encodetype(ts...)
  buf = IOBuffer()
  for t in ts
    (t <: Ptr) && (print(buf, "^"); t = eltype(t))
    haskey(revtypeencodings, t) || error("$t isn't a valid ObjectiveC type")
    print(buf, revtypeencodings[t])
  end
  return String(take!(buf))
end


text:
using HoJDiscordBot
using Test

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

max_stars_repo_name: dzonimn/HoJDiscordBot

max_issues_repo_name: dzonimn/HoJDiscordBot

max_forks_repo_name: dzonimn/HoJDiscordBot


text:
# This file was generated by the Julia Swagger Code Generator
# Do not modify this file directly. Modify the swagger specification instead.

include("model_AclStatus.jl")
include("model_AclStatusResult.jl")
include("model_AdlsError.jl")
include("model_AdlsRemoteException.jl")
include("model_ContentSummary.jl")
include("model_ContentSummaryResult.jl")
include("model_FileOperationResult.jl")
include("model_FileStatusProperties.jl")
include("model_FileStatusResult.jl")
include("model_FileStatuses.jl")
include("model_FileStatusesResult.jl")
include("model_AdlsAccessControlException.jl")
include("model_AdlsBadOffsetException.jl")
include("model_AdlsFileAlreadyExistsException.jl")
include("model_AdlsFileNotFoundException.jl")
include("model_AdlsIOException.jl")
include("model_AdlsIllegalArgumentException.jl")
include("model_AdlsRuntimeException.jl")
include("model_AdlsSecurityException.jl")
include("model_AdlsThrottledException.jl")
include("model_AdlsUnsupportedOperationException.jl")


text:
module progress

using Dates
import MyterialColors: pink, yellow_dark, teal

import Term: int, textlen, truncate
import ..Tprint: tprint, tprintln
import ..style: apply_style
import ..consoles: console_width,
                clear,
                hide_cursor,
                show_cursor,
                line,
                erase_line,
                beginning_previous_line,
                down

import ..renderables: AbstractRenderable
import ..measure: Measure
import ..segment: Segment
import ..color: RGBColor

export ProgressBar, update, track, start, stop

# ---------------------------------------------------------------------------- #
#                                    columns                                   #
# ---------------------------------------------------------------------------- #
abstract type AbstractColumn <: AbstractRenderable end

# ---------------------------- description column ---------------------------- #
struct DescriptionColumn <: AbstractColumn
    segments::Vector
    measure::Measure
    text::String
end

function DescriptionColumn(description::String)
    seg = Segment(description)
    return DescriptionColumn([seg], seg.measure, seg.text)
end

update(col::DescriptionColumn, args...)::String = col.text

# ----------------------------- separator column ----------------------------- #
struct SeparatorColumn <: AbstractColumn
    segments::Vector
    measure::Measure
    text::String
end

function SeparatorColumn()
    seg = Segment("●", pink)
    return SeparatorColumn([seg], seg.measure, seg.text)
end

update(col::SeparatorColumn, args...)::String = col.text

# ----------------------------- completed column ----------------------------- #
struct CompletedColumn <: AbstractColumn
    segments::Vector
    measure::Measure
    text::String
end

function CompletedColumn(N::Int)
    width = length("$N")*2+1
    seg = Segment(" "^width)
    text = apply_style("[white bold]/[/white bold][(.1, .8, .4) underline]$N[/(.1, .8, .4) underline]")
    return CompletedColumn([seg], seg.measure, text)
end

function update(col::CompletedColumn, i::Int, N::Int, color::String, args...)::String
    _i = "$(i)"
    _N = "$(N)"
    _i = lpad("$i", length(_N))
    return apply_style("[$color bold]$_i[/$color bold]") * col.text
end

# ----------------------------- percentage column ---------------------------- #
struct PercentageColumn <: AbstractColumn
    segments::Vector
    measure::Measure
end

function PercentageColumn()
    len = 5  # "xxx %
    seg = Segment(" "^len)
    return PercentageColumn([seg], seg.measure)
end

function update(col::PercentageColumn, i::Int, N::Int, args...)::String
    p = string(int(i / N * 100))
    p = lpad(p, col.measure.w-2)
    return "\e[2m$p %\e[0m"
end

# -------------------------------- bar column -------------------------------- #
mutable struct BarColumn <: AbstractColumn
    segments::Vector
    measure::Measure
    nsegs::Int
end

BarColumn() = BarColumn([], Measure(0, 0), 0)

function setwidth!(col::BarColumn, width::Int)
    col.measure = Measure(width, 1)
    col.nsegs = width
end

function update(col::BarColumn, i::Int, N::Int, color::String, args...)::String
    completed = int(col.nsegs * i/N)
    remaining = col.nsegs - completed

    completed = completed < 0 ? 0 : completed
    remaining = remaining < 0 ? 0 : remaining

    return apply_style("[$color bold]" * '━'^(completed) * "[/$color bold]"* " "^(remaining))
end

# ------------------------------ elapsed column ------------------------------ #

struct ElapsedColumn <: AbstractColumn
    segments::Vector
    measure::Measure
    style::String
end

ElapsedColumn(; style=yellow_dark) = ElapsedColumn([], Measure(6+9, 1), style)

function update(col::ElapsedColumn, i::Int, N::Int, color::String, starttime::Union{Nothing, DateTime}, args...)::String
    isnothing(starttime) && return " "^(col.measure.w)
    elapsedtime = (now() - starttime).value  # in ms

    # format elapsed message
    if elapsedtime < 1000
        msg = "$(elapsedtime)ms"
    elseif elapsedtime < (60 * 1000)
        # under a minute
        elapsed = round(elapsedtime/1000; digits=2)
        msg = "$(elapsed)s"
    else
        # show minutes
        elapsed = round(elapsedtime/(60*1000); digits=2)
        msg = "$(elapsed)min"
    end

    msg = truncate(msg, col.measure.w-9)
    msg = lpad(msg, col.measure.w-9)

    return apply_style("[$(col.style)]elapsed: $(msg)[/$(col.style)]")
end

# -------------------------------- ETA column -------------------------------- #

struct ETAColumn <: AbstractColumn
    segments::Vector
    measure::Measure
    style::String
end

ETAColumn(; style=teal) = ETAColumn([], Measure(7+11, 1), style)

function update(col::ETAColumn, i::Int, N::Int, color::String, starttime::Union{Nothing, DateTime}, args...)::String
    isnothing(starttime) && return " "^(col.measure.w)

    # get remaining time in ms
    elapsed = (now() - starttime).value  # in ms
    perc = i/N
    remaining = elapsed * (1 - perc) / perc

     # format elapsed message
    if remaining < 1000
        msg = "$(round(remaining; digits=0))ms"
    elseif remaining < (60 * 1000)
        # under a minute
        remaining = round(remaining/1000; digits=2)
        msg = "$(remaining)s"
    else
        # show minutes
        remaining = round(remaining/(60*1000); digits=2)
        msg = "$r(emaining)min"
    end

    msg = truncate(msg, col.measure.w-11)
    msg = lpad(msg, col.measure.w-11)

    return apply_style("[$(col.style)]remaining: $(msg)[/$(col.style)]")

end

# ---------------------------------------------------------------------------- #
#                                  PROGESS BAR                                 #
# ---------------------------------------------------------------------------- #

function get_columns(columnsset::Symbol, description::String, N::Int)::Vector{AbstractColumn}
    if columnsset == :minimal
        return [
            DescriptionColumn(description),
            BarColumn(),
        ]
    elseif columnsset == :default
        return [
            DescriptionColumn(description),
            SeparatorColumn(),
            BarColumn(),
            SeparatorColumn(),
            CompletedColumn(N),
            PercentageColumn(),
        ]
    else
        # extensive
        return [
            DescriptionColumn(description),
            SeparatorColumn(),
            BarColumn(),
            SeparatorColumn(),
            CompletedColumn(N),
            PercentageColumn(),
            SeparatorColumn(),
            ElapsedColumn(),
            ETAColumn()
        ]
    end
end

"""
    ProgressBar

Progress bar Type, stores information required
to render a progress bar renderable.
"""
mutable struct ProgressBar
    i::Int
    N::Int
    width::Int
    started::Bool
    finished::Bool
    transient::Bool
    update_every::Int
    columns::Vector{AbstractColumn}
    startime::Union{Nothing, DateTime}
    stoptime::Union{Nothing, DateTime}
    redirectstdout::Bool
    originalstdout::Union{Nothing, IO}  # stores a reference to the original stdout
    out  # will be used to store temporarily re-directed stdout
    colors::Vector{RGBColor}

    """
        ProgressBar(;
            N::Int=100,
            width::Int=50,
            description::String="[#F48FB1]Progress...[/#F48FB1]",
            expand::Bool=false,
            transient=false
        )

    Construct a `ProgressBar` with minimal required arguments.

    As part of the construction, compute the size of the bar
    itself, based on the total width and the width of the text
    elements.
    """
    function ProgressBar(;
                N::Int=100,
                width::Int=88,
                description::String="[orange1 italic]Running...[/orange1 italic]",
                expand::Bool=false,
                transient=false,
                redirectstdout=true,
                columns::Union{Symbol, Vector{AbstractColumn}} = :default,
                update_every=1,
                colors::Vector{RGBColor}=[
                    RGBColor("(1, .05, .05)"),
                    RGBColor("(.05, .05, 1)"),
                    RGBColor("(.05, 1, .05)"),
                ]
        )

        # get columns if not provided
        if columns isa Symbol
            columns = get_columns(columns, description, N)
        end

        # check that width is large enough
        width = expand ? console_width()-5 : min(max(width, 20), console_width()-5)

        # get the width of the BarColumn
        spaces = length(columns) -1
        colwidths = sum(map((c)-> c isa BarColumn ? 0 : c.measure.w, columns))
        bcol_width = width - colwidths - spaces

        # if it doesn't have a bar column, add one
        bars = sum(map((c)-> c isa BarColumn ? 1 : 0, columns))
        if bars == 0
            push!(columns, BarColumn())
        elseif bars > 1
            bcol_width /= bars
        end

        # set width of bar columns
        for col in columns
            col isa BarColumn && setwidth!(col, bcol_width)
        end

        # create progress bar
        new(
            1,
            N,
            width,
            false, # started
            false, # finished
            transient,
            update_every,
            columns,
            nothing, # start time
            nothing, # stop time
            redirectstdout,
            nothing, # originalstdout
            nothing, # out
            colors,
        )

    end

    Base.show(io::IO, ::MIME"text/plain", pbar::ProgressBar) = print(io, "Progress bar \e[2m($(pbar.i)/$(pbar.N))\e[0m")

end

"""
pbar_color(pbar::ProgressBar)

Get the RGB color of of a progress bar's bar based on progress.
"""
function pbar_color(pbar::ProgressBar)
    α = .8 * pbar.i/pbar.N
    β = max(sin(π * pbar.i/pbar.N) * .7, .4)

    c1, c2, c3 = pbar.colors
    r = (.8 - α) * c1.r + β * c2.r + α * c3.r
    g = (.8 - α) * c1.g + β * c2.g + α * c3.g
    b = (.8 - α) * c1.b + β * c2.b + α * c3.b

    return "($r, $g, $b)"
end

"""
    TempSTDOUT

Stores information about a temporarily re-directed STDOUT
"""
struct TempSTDOUT
    out_rd::Base.PipeEndpoint
    out_wr::Base.PipeEndpoint
    out_reader::Task
end

function start(pbar::ProgressBar)
    pbar.started && return
    pbar.originalstdout = stdout
    if pbar.redirectstdout
        # re-direct STDOUT
        out_rd, out_wr = redirect_stdout()
        out_reader = @async read(out_rd, String)
        pbar.out = TempSTDOUT(out_rd, out_wr, out_reader)
    end

    # start pbar
    hide_cursor(pbar.originalstdout)
    pbar.started = true
    pbar.startime = now()
end

function stop(pbar::ProgressBar; newline=true)
    pbar.finished && return
    # remove transient progress bars
    pbar.transient && begin
        erase_line(pbar.originalstdout)
        beginning_previous_line(pbar.originalstdout)
        erase_line(pbar.originalstdout)
        # down(pbar.originalstdout)
        # erase_line(pbar.originalstdout)
    end

    # ensure early termination adds a new line
    # pbar.i < pbar.N && line(pbar.originalstdout)
    newline && line(pbar.originalstdout)

    # restore STDOUT
    if pbar.redirectstdout
        redirect_stdout(pbar.originalstdout)
        close(pbar.out.out_wr)
        out = fetch(pbar.out.out_reader)
        tprint(out)
    end
    print("")

    # stop
    pbar.stoptime = now()
    pbar.finished = true
    show_cursor()

end

"""
    update(pbar::ProgressBar)

Update progress bar info and display.
"""
function update(pbar::ProgressBar)
    # start progerss bar
    pbar.started || start(pbar)
    pbar.i > pbar.N && return

    if pbar.i % pbar.update_every == 0
        # get progress bar
        color = pbar_color(pbar)

        # get columns data
        contents = map((c)->update(c, pbar.i, pbar.N, color, pbar.startime), pbar.columns)

        # print content
        pbar.i > 1 && beginning_previous_line(pbar.originalstdout)
        tprint(pbar.originalstdout, contents...)
        pbar.i < pbar. N && line(pbar.originalstdout)
    end

    # update counter
    pbar.i += 1

    # check if done and stop
    pbar.i > pbar.N && stop(pbar)

    return nothing
end

function update(pbar::ProgressBar, i::Int)
    pbar.i = i
    update(pbar)
end

# ---------------------------------------------------------------------------- #
#                                     track                                    #
# ---------------------------------------------------------------------------- #

"""
    Track

Convenience iterator to add a `ProgressBar` to `for` loops.

```Julia
for i in track(1:100)
    # ... do something
end
```

Adds a progress bar that updates for each iteration through the loop.
"""
struct Track
    itr
    pbar::ProgressBar

    Track(itr; kwargs...) = new(itr, ProgressBar(; kwargs...))
end

"""
Costructor for a `Track` object.
"""
function track(itr; kwargs...)
    Track(
        itr;
        N = length(itr),
        kwargs...
    )
end

"""
Start iteration. Crate `Progress Bar`
"""
function Base.iterate(e::Track)
    it = iterate(e.itr)
    isnothing(it) || start(e.pbar)
    isnothing(it) || update(e.pbar)
    return it
end

"""
Update progress bar and continue iteration.
"""
function Base.iterate(e::Track, state)
    it = iterate(e.itr, state)
    isnothing(it) || update(e.pbar)
    isnothing(it) && stop(e.pbar; newline=false)
    return it
end

Base.length(e::Track) = length(e.itr)
Base.size(e::Track) = size(e.itr)

end


text:
using Plots
using NeutralLandscapes
using EcologicalNetworks
using MetacommunityDynamics

sidelength = 50

# a top to bottom grid of an environmental variable

temp = rand(PlanarGradient(180), sidelength, sidelength)
heatmap(temp,
    c=:thermal,
    aspectratio=1,
    frame=:box,
    size=(500,500)
    )

speciesA =
    LogisticGrowth{:A}(λ=1.4, K=10., dt=0.1) +
    AbioticExtinction{:A}(temp);

speciesB =
    LogisticGrowth{:B}(λ=1.4, K=10., dt=0.1) +
    AbioticExtinction{:B}(temp.*-1 .+ 1);

fullmodel = speciesA + speciesB

ae = AbioticExtinction{:B}(temp.*-1 .+ 1);


text:
box_upper(frame::Frame) = Float64[frame[:box_max]...]
box_lower(frame::Frame) = Float64[frame[:box_min]...]
box_widths(frame::Frame) = box_upper(frame) - box_lower(frame)

function distance_sq{T}(xbw::T, ybw::T, zbw::T, coords::Matrix{T}, i1::Int, i2::Int)
    xhbw = xbw / 2
    yhbw = ybw / 2
    zhbw = zbw / 2

    @inbounds xd = coords[1, i1] - coords[1, i2]
    @inbounds yd = coords[2, i1] - coords[2, i2]
    @inbounds zd = coords[3, i1] - coords[3, i2]

    if xd < -xhbw xd += xbw elseif xd > xhbw xd -= xbw end
    if yd < -yhbw yd += ybw elseif yd > yhbw yd -= ybw end
    if zd < -zhbw zd += zbw elseif zd > zhbw zd -= zbw end

    return xd*xd + yd*yd + zd*zd
end

function wrap(bw, z)
    hbw = bw / 2

    if z < -hbw
        z += bw
    elseif z > hbw
        z -= bw
    end

    return z
end

function distance_cells(box_width, cell1_center, cell1_width, cell2_center, cell2_width)
    bw = box_width
    hcw1, hcw2 = cell1_width / 2, cell2_width / 2
    cc = cell2_center - cell1_center

    d1 = abs(wrap(bw, cc + hcw2 - hcw1))
    d2 = abs(wrap(bw, cc + hcw2 + hcw1))
    d3 = abs(wrap(bw, cc - hcw2 - hcw1))
    d4 = abs(wrap(bw, cc - hcw2 + hcw1))

    return min(d1, d2, d3, d4), max(d1, d2, d3, d4)
end

function neighbor_cell_offsets{T}(frame::Frame, r_cutoff::T, num_cells::Int)
    bw = box_widths(frame)
    cw = bw / num_cells

    xbw, ybw, zbw = bw[1], bw[2], bw[3]
    xcw, ycw, zcw = cw[1], cw[2], cw[3]

    neighbor_offsets = Vector{Int}[]

    for ix in 0:num_cells-1, iy in 0:num_cells-1, iz in 0:num_cells-1
        min_x, max_x = distance_cells(xbw, T(0), xcw, ix*xcw, xcw)
        min_y, max_y = distance_cells(ybw, T(0), ycw, iy*ycw, ycw)
        min_z, max_z = distance_cells(zbw, T(0), zcw, iz*zcw, zcw)

        if min_x^2 + min_y^2 + min_z^2 <= r_cutoff^2
            push!(neighbor_offsets, [ix, iy, iz])
        end
    end

    return neighbor_offsets
end

cell_index{T}(xlo::T, xcw::T, x::T) = floor(Int, (x - xlo) / xcw)::Int + 1

function sort_atoms_into_cells(frame::Frame, num_cells::Int)
    atoms = frame[:atoms]
    num_atoms = length(atoms)::Int
    coords = atoms.props[:coords]::Matrix{Float64}
    indices = atoms.indices

    xlo::Float64, ylo::Float64, zlo::Float64 = box_lower(frame)
    xcw::Float64, ycw::Float64, zcw::Float64 = box_widths(frame) / num_cells

    cells = [Int[] for ix in 1:num_cells, iy in 1:num_cells, iz in 1:num_cells]

    for i in 1:num_atoms
        @inbounds ix = cell_index(xlo, xcw, coords[1, indices[i]])
        @inbounds iy = cell_index(ylo, ycw, coords[2, indices[i]])
        @inbounds iz = cell_index(zlo, zcw, coords[3, indices[i]])

        @inbounds push!(cells[ix, iy, iz], indices[i])
    end

    return cells
end

function ndf!{T, A <: AbstractVector{Int}}(bins::Vector{UInt64}, xbw::T, ybw::T, zbw::T, coords::Matrix{T}, indices1::A, indices2::A, r_cutoff_sq::T, Δr::T)
    for i1 in indices1, i2 in indices2
		# If same cell and same component, iterate over each unique pair of atoms ONCE
		if indices1 === indices2 && i1 >= i2
			continue
		end

		r_sq = distance_sq(xbw, ybw, zbw, coords, i1, i2)

		if r_sq < r_cutoff_sq
			r = sqrt(r_sq)
			b = floor(Int, r / Δr) + 1
			# Iterate over each unique atom pair once to save compute cycles,
			# but count that pair twice (to produce correct density)
			@inbounds bins[b] += 2
		end
    end

    return bins
end

function func_ndf{T}(frame::Frame, r_cutoff::T, Δr::T, num_cells::Int)
    r_cutoff_sq = r_cutoff^2

    coords = frame[:atoms].props[:coords]::Matrix{T}
    xbw::T, ybw::T, zbw::T = box_widths(frame)

    num_bins = ceil(Int, r_cutoff / Δr)
    dim_cells = (num_cells, num_cells, num_cells)
    neighbor_offsets = neighbor_cell_offsets(frame, r_cutoff, num_cells)

    return function (cells1::Array{Vector{Int}, 3}, cells2::Array{Vector{Int}, 3})
        bins = zeros(UInt64, num_bins)

        for i1x in 1:num_cells, i1y in 1:num_cells, i1z in 1:num_cells
            i1 = sub2ind(dim_cells, i1x, i1y, i1z)

            for offset in neighbor_offsets
                @inbounds i2x = mod1(i1x + offset[1], num_cells)
                @inbounds i2y = mod1(i1y + offset[2], num_cells)
                @inbounds i2z = mod1(i1z + offset[3], num_cells)

                i2 = sub2ind(dim_cells, i2x, i2y, i2z)

				# If same components, iterate over each unique pair of cells only ONCE
                if cells1 !== cells2 || i1 >= i2
                    @inbounds indices1 = cells1[i1x, i1y, i1z]
                    @inbounds indices2 = cells2[i2x, i2y, i2z]

                    ndf!(bins, xbw, ybw, zbw, coords, indices1, indices2, r_cutoff_sq, Δr)
                end
            end
        end

        return bins
    end
end

function ndf_to_rdf{T}(bins_ints::Vector{UInt64}, ρ::T, Δr::T)
    num_bins = length(bins_ints)
    bins = zeros(T, num_bins)

    for i in 1:num_bins
        r_mid = i*Δr - Δr/2
        bins[i] = bins_ints[i] / (4π * r_mid^2 * Δr * ρ)
    end

    return bins
end

function rdf_axis{T}(r_cutoff::T, Δr::T)
    num_bins = ceil(Int, r_cutoff / Δr)
    bins = zeros(T, num_bins)

    for i in 1:num_bins
        bins[i] = i*Δr - Δr/2
    end

    return bins
end

function split_by_types(frame::Frame)
    types = group(frame[:atoms], :type)
    Dict(k => Frame(
        :atoms => v,
        :timestep => frame[:timestep],
        :box_min => frame[:box_min],
        :box_max => frame[:box_max]
    ) for (k, v) in types)
end

function rdf{T}(frames::Vector{Frame}, r_cutoff::T, Δr::T, num_cells::Int)
	num_bins = ceil(Int, r_cutoff / Δr)
	bins = zeros(T, num_bins)

	num_frames = length(frames)
	num_threads = Threads.nthreads()

	Threads.@threads for frame in frames
		volume = prod(box_widths(frame))

		num_atoms = length(frame[:atoms])::Int
		cells = sort_atoms_into_cells(frame, num_cells)

		ndf = func_ndf(frame, r_cutoff, Δr, num_cells)
		bins_int = ndf(cells, cells)
		ρ = num_atoms^2 / volume
		bins .+= ndf_to_rdf(bins_int, ρ, Δr)
	end

	return bins / num_frames
end

function rdf_components{T}(frames::Vector{Frame}, component_pairs::Vector{Vector{Symbol}}, r_cutoff::T, Δr::T, num_cells::Int)
    num_component_pairs = length(component_pairs)
    components = sort(unique(vcat(component_pairs...)))
    num_bins = ceil(Int, r_cutoff / Δr)

    bins = zeros(T, (num_bins, num_component_pairs))

	num_frames = length(frames)
    num_threads = Threads.nthreads()

    Threads.@threads for frame in frames
        volume = prod(box_widths(frame))

		num_atoms_total = length(frame[:atoms])::Int
        frame_components = split_by_types(frame)
        num_atoms = Dict(component => length(frame_components[component][:atoms])::Int for component in components)
        cells = Dict(component => sort_atoms_into_cells(frame_components[component], num_cells) for component in components)

        ndf = func_ndf(frame, r_cutoff, Δr, num_cells)

        for (i, component_pair) in enumerate(component_pairs)
            c1, c2 = component_pair[1], component_pair[2]
            bins_ints = ndf(cells[c1], cells[c2])
            #ρ = (num_atoms[c1] * num_atoms[c2]) / volume
            ρ = num_atoms_total^2 / volume
            @inbounds bins[:, i] .+= ndf_to_rdf(bins_ints, ρ, Δr)
        end
    end

    rdfs = Dict(join(component_pair, '-') => bins[:, i] / num_frames for (i, component_pair) in enumerate(component_pairs))

    return rdfs
end

function rdf_components_all{T}(frames::Vector{Frame}, r_cutoff::T, Δr::T, num_cells::Int)
    components = sort(unique(frames[1][:atoms][:type]))::Vector{Symbol}
    component_pairs = unique([sort([c1, c2]) for c1 in components, c2 in components])

    return rdf_components(frames, component_pairs, r_cutoff, Δr, num_cells)
end

# Slow function used only for testing correctness of other functions
function rdf_exact{T}(frames::Vector{Frame}, r_cutoff::T, Δr::T)
    frame = frames[1]
    atoms = frame[:atoms]
    coords = collect(atoms[:coords])::Matrix{T}

    num_atoms = length(atoms)
    xbw::T, ybw::T, zbw::T = box_widths(frame)
    volume = prod((xbw, ybw, zbw))
    ρ = num_atoms^2 / volume

    num_bins = ceil(Int, r_cutoff / Δr)
    bins_ints = zeros(UInt64, num_bins)
    r_cutoff_sq = r_cutoff^2

    Alpine.ndf!(bins_ints, xbw, ybw, zbw, coords, 1:num_atoms, 1:num_atoms, r_cutoff_sq, Δr)
    bins = Alpine.ndf_to_rdf(bins_ints, ρ, Δr)

    return bins
end

max_stars_repo_name: NickMcNutt/Alpine

max_issues_repo_name: NickMcNutt/Alpine

max_forks_repo_name: NickMcNutt/Alpine


text:
# This file is dedicated to CUDA versions of functions that have not yet been ported to the official library

using KernelAbstractions, CUDAKernels

include("repeat.jl")


text:
@testset "1D SpectralConv" begin
    modes = (16, )
    ch = 64 => 128

    m = Chain(
        Dense(2, 64),
        SpectralConv(ch, modes)
    )
    @test ndims(SpectralConv(ch, modes)) == 1
    @test repr(SpectralConv(ch, modes)) == "SpectralConv(64 => 128, (16,), σ=identity, permuted=false)"

    𝐱, _ = get_burgers_data(n=5)
    @test size(m(𝐱)) == (128, 1024, 5)

    loss(x, y) = Flux.mse(m(x), y)
    data = [(𝐱, rand(Float32, 128, 1024, 5))]
    Flux.train!(loss, params(m), data, Flux.ADAM())
end

@testset "permuted 1D SpectralConv" begin
    modes = (16, )
    ch = 64 => 128

    m = Chain(
        Conv((1, ), 2=>64),
        SpectralConv(ch, modes, permuted=true)
    )
    @test ndims(SpectralConv(ch, modes, permuted=true)) == 1
    @test repr(SpectralConv(ch, modes, permuted=true)) == "SpectralConv(64 => 128, (16,), σ=identity, permuted=true)"

    𝐱, _ = get_burgers_data(n=5)
    𝐱 = permutedims(𝐱, (2, 1, 3))
    @test size(m(𝐱)) == (1024, 128, 5)

    loss(x, y) = Flux.mse(m(x), y)
    data = [(𝐱, rand(Float32, 1024, 128, 5))]
    Flux.train!(loss, params(m), data, Flux.ADAM())
end

@testset "1D FourierOperator" begin
    modes = (16, )
    ch = 64 => 128

    m = Chain(
        Dense(2, 64),
        FourierOperator(ch, modes)
    )
    @test repr(FourierOperator(ch, modes)) == "FourierOperator(64 => 128, (16,), σ=identity, permuted=false)"

    𝐱, _ = get_burgers_data(n=5)
    @test size(m(𝐱)) == (128, 1024, 5)

    loss(x, y) = Flux.mse(m(x), y)
    data = [(𝐱, rand(Float32, 128, 1024, 5))]
    Flux.train!(loss, params(m), data, Flux.ADAM())
end

@testset "permuted 1D FourierOperator" begin
    modes = (16, )
    ch = 64 => 128

    m = Chain(
        Conv((1, ), 2=>64),
        FourierOperator(ch, modes, permuted=true)
    )
    @test repr(FourierOperator(ch, modes, permuted=true)) == "FourierOperator(64 => 128, (16,), σ=identity, permuted=true)"

    𝐱, _ = get_burgers_data(n=5)
    𝐱 = permutedims(𝐱, (2, 1, 3))
    @test size(m(𝐱)) == (1024, 128, 5)

    loss(x, y) = Flux.mse(m(x), y)
    data = [(𝐱, rand(Float32, 1024, 128, 5))]
    Flux.train!(loss, params(m), data, Flux.ADAM())
end

@testset "2D SpectralConv" begin
    modes = (16, 16)
    ch = 64 => 64

    m = Chain(
        Dense(1, 64),
        SpectralConv(ch, modes)
    )
    @test ndims(SpectralConv(ch, modes)) == 2

    𝐱, _, _, _ = get_darcy_flow_data(n=5, Δsamples=20)
    @test size(m(𝐱)) == (64, 22, 22, 5)

    loss(x, y) = Flux.mse(m(x), y)
    data = [(𝐱, rand(Float32, 64, 22, 22, 5))]
    Flux.train!(loss, params(m), data, Flux.ADAM())
end

@testset "permuted 2D SpectralConv" begin
    modes = (16, 16)
    ch = 64 => 64

    m = Chain(
        Conv((1, 1), 1=>64),
        SpectralConv(ch, modes, permuted=true)
    )
    @test ndims(SpectralConv(ch, modes, permuted=true)) == 2

    𝐱, _, _, _ = get_darcy_flow_data(n=5, Δsamples=20)
    𝐱 = permutedims(𝐱, (2, 3, 1, 4))
    @test size(m(𝐱)) == (22, 22, 64, 5)

    loss(x, y) = Flux.mse(m(x), y)
    data = [(𝐱, rand(Float32, 22, 22, 64, 5))]
    Flux.train!(loss, params(m), data, Flux.ADAM())
end

@testset "2D FourierOperator" begin
    modes = (16, 16)
    ch = 64 => 64

    m = Chain(
        Dense(1, 64),
        FourierOperator(ch, modes)
    )

    𝐱, _, _, _ = get_darcy_flow_data(n=5, Δsamples=20)
    @test size(m(𝐱)) == (64, 22, 22, 5)

    loss(x, y) = Flux.mse(m(x), y)
    data = [(𝐱, rand(Float32, 64, 22, 22, 5))]
    Flux.train!(loss, params(m), data, Flux.ADAM())
end

@testset "permuted 2D FourierOperator" begin
    modes = (16, 16)
    ch = 64 => 64

    m = Chain(
        Conv((1, 1), 1=>64),
        FourierOperator(ch, modes, permuted=true)
    )

    𝐱, _, _, _ = get_darcy_flow_data(n=5, Δsamples=20)
    𝐱 = permutedims(𝐱, (2, 3, 1, 4))
    @test size(m(𝐱)) == (22, 22, 64, 5)

    loss(x, y) = Flux.mse(m(x), y)
    data = [(𝐱, rand(Float32, 22, 22, 64, 5))]
    Flux.train!(loss, params(m), data, Flux.ADAM())
end


text:
# Adding Some useful functions
import CSV
import DataFrames

function readquadrature(filename::String, delimiter)
    quadFile = CSV.File(joinpath(@__DIR__,"quadDir",filename) , delim = delimiter)|> DataFrames.DataFrame
    return quadFile.r1, quadFile.r2,
           quadFile.r3, quadFile.w
end


text:
@testset "CategoricalLikelihood" begin
    @test CategoricalLikelihood() isa
        CategoricalLikelihood{<:GPLikelihoods.BijectiveSimplexLink}

    @test CategoricalLikelihood(softmax) isa CategoricalLikelihood{SoftMaxLink}
    @test CategoricalLikelihood(SoftMaxLink()) isa CategoricalLikelihood{SoftMaxLink}

    OUT_DIM = 4
    lik_bijective = CategoricalLikelihood()
    test_interface(lik_bijective, Categorical, OUT_DIM)
    lik_nonbijective = CategoricalLikelihood(softmax)
    test_interface(lik_nonbijective, Categorical, OUT_DIM)
end


text:
using PrintFileTree
using Test

@testset "PrintFileTree" begin

@testset "full test" begin
    tmpdir = mktempdir()
    touch(joinpath(tmpdir, "a"))
    touch(joinpath(tmpdir, "b"))
    mkpath(joinpath(tmpdir, "c","a","a"))
    mkpath(joinpath(tmpdir, "c", "wow", "cats", "are", "so", ""))
    mkpath(joinpath(tmpdir, "c", "wow", "cats", "are", "weird"))
    touch(joinpath(tmpdir, "c", "a", "subfiles"))
    touch(joinpath(tmpdir, "c", "a", "a","subfiles"))
    mkpath(joinpath(tmpdir, "c","a","a"))
    mkpath(joinpath(tmpdir, "c","a","a"))
    touch(joinpath(tmpdir, "c", "z"))
    touch(joinpath(tmpdir, "d"))
    mkpath(joinpath(tmpdir, "e", "the", "end"))

    # Run once to make sure it doesn't crash or something.
    printfiletree(tmpdir)

    # Actually @test the output.
    output = read(`$(Base.julia_cmd()) -e "using PrintFileTree; printfiletree(raw\"$tmpdir\")"`, String)
    @test occursin(
        """$tmpdir
        ├── a
        ├── b
        ├── c
        │   ├── a
        │   │   ├── a
        │   │   │   └── subfiles
        │   │   └── subfiles
        │   ├── wow
        │   │   └── cats
        │   │       └── are
        │   │           ├── so
        │   │           └── weird
        │   └── z
        ├── d
        └── e
            └── the
                └── end

        11 directories, 6 files""",
        output)
    println(output)
end

@testset "small test" begin
    d = mktempdir()
    output = read(`$(Base.julia_cmd()) -e "using PrintFileTree; printfiletree(raw\"$d\")"`, String)
    @test occursin(
        """$d

        0 directories, 0 files""",
    output)
    println(output)
end

end


text:
# Create design matrix X and response variable y from swiss data array
swiss = readdlm("swiss.txt", ' ');
covariates = swiss[:, 1:end-1];
nData, nPars = size(covariates);

covariates = (bsxfun(-, covariates, mean(covariates, 1))
  ./repmat(std(covariates, 1), nData, 1));

polynomialOrder = 1;
X = zeros(nData, nPars*polynomialOrder);
for i = 1:polynomialOrder
  X[:, ((i-1)*nPars+1):i*nPars] = covariates.^i;
end

y = swiss[:, end];

# Create data dictionary
data = {"X"=>X, "y"=>y, "priorVar"=>100., "nData"=>nData};


text:
function _parse_julia_file(filename::String)
    return Base.parse_input_line(read(filename, String); filename = filename)
end

function lowercase_drive(a)
    if length(a) >= 2 && a[2] == ':'
        return lowercase(a[1]) * a[2:end]
    else
        return a
    end
end

const SRC_DIR = joinpath(Sys.BINDIR, "..", "..", "base")
const RELEASE_DIR = joinpath(Sys.BINDIR, "..", "share", "julia", "base")
basepath(file) =
  normpath(joinpath((@static isdir(SRC_DIR) ? SRC_DIR : RELEASE_DIR), file))

max_stars_repo_name: cloudgiant/julia-vscode

max_issues_repo_name: cloudgiant/julia-vscode

max_forks_repo_name: cloudgiant/julia-vscode


text:
module BroadcastBenchmarks

include(joinpath("..", "utils", "RandUtils.jl"))

using .RandUtils
using BenchmarkTools
using Compat

const SUITE = BenchmarkGroup()

###########################################################################

g = addgroup!(SUITE, "fusion", ["broadcast!", "array"])

f(x,y) = 3x - 4y^2
h(x) = 6x + 2x^2 - 5
perf_bcast!(r, x) = @compat r .= @compat h.(f.(x, h.(x)))
perf_bcast!(R, x, y) = @compat R .= @compat h.(f.(x, h.(y)))
perf_bcast!(R, x, y, z) = @compat R .= @compat f.(X, f.(x, y))

x = randvec(10^3)
y = randvec(10^3)'
z = randvec(10^6)
X = randmat(10^3)
R = Matrix{Float64}(length(x), length(y))
r = similar(z)

g["Float64", size(r), 1] = @benchmarkable perf_bcast!($r, $z)
g["Float64", size(R), 2] = @benchmarkable perf_bcast!($R, $x, $y)
g["Float64", size(R), 3] = @benchmarkable perf_bcast!($R, $X, $x, $y)
g["Float64", size(r), 2] = @benchmarkable perf_bcast!($r, $z, 17.3)

###########################################################################

g = addgroup!(SUITE, "dotop", ["broadcast!", "array"])

perf_op_bcast!(r, x) = @compat r .= 3 .* x .- 4 .* x.^2 .+ x .* x .- x .^ 3
perf_op_bcast!(R, x, y) = @compat R .= 3 .* x .- 4 .* y.^2 .+ x .* y .- x .^ 3

g["Float64", size(r), 1] = @benchmarkable perf_op_bcast!($r, $z)
g["Float64", size(R), 2] = @benchmarkable perf_op_bcast!($R, $x, $y)
g["Float64", size(r), 2] = @benchmarkable perf_op_bcast!($r, $z, 17.3)

###########################################################################

g = addgroup!(SUITE, "sparse", ["broadcast", "array"])

perf_sparse_op(s) = @compat sqrt.(abs.(s .* 2))
perf_sparse_op(s,t) = @compat f.(s,t)

if VERSION < v"0.5.0-dev+763"
    s = samesprand(10^7, 1, 1e-3, randn)
else
    s = samesprand(10^7, 1e-3, randn)
end
S = samesprand(10^3, 10^3, 1e-3, randn)

g[size(s), 1] = @benchmarkable perf_sparse_op($s)
g[size(s), 2] = @benchmarkable perf_sparse_op($s, $s)
g[size(S), 1] = @benchmarkable perf_sparse_op($S)
g[size(S), 2] = @benchmarkable perf_sparse_op($S, $S)

###########################################################################

if VERSION >= v"0.6.0-pre.alpha.65"

g = addgroup!(SUITE, "typeargs", ["broadcast"])

f_round(v) = @compat round.(Int, v)

r1, r2, r3 = rand(3), rand(5), rand(10)
for r in (r1, r2, r3)
    g["array", length(r)] = @benchmarkable f_round($r)
end

t1, t2, t3 = (rand(3)...), (rand(5)...), (rand(10)...)
for t in (t1, t2, t3)
    g["tuple", length(t)] = @benchmarkable f_round($t)
end

###########################################################################

g = addgroup!(SUITE, "mix_scalar_tuple", ["broadcast", "tuple"])

for t in (t1, t2, t3)
    g[length(t), "scal_tup"]    = @benchmarkable broadcast(+, 1, $t)
    g[length(t), "tup_tup"]     = @benchmarkable broadcast(+, $t, $t)
    g[length(t), "scal_tup_x3"] = @benchmarkable broadcast(+, 1, $t, 1, $t, 1, $t)
end

end # VERSION

end # module


text:
struct Model

	X::Array{Variable, 2}
	varsInter::Vector{Variable}

	varsNotFixed::Vector{Variable}

	constraints::Set{Constraint}

	p::Int
	g::Int
	w::Int

end

function Model(X::Array{Variable, 2}, varsInter::Vector{Variable}, constraints::Set{Constraint}, p::Int, g::Int, w::Int)
	return Model(X,
				varsInter,
				append!([X[g, w] for g in 1:g for w in 1:w], varsInter),
				constraints,
				p,
				g,
				w)
end

import Base.show
function Base.show(io::IO, model::Model)

	println(io, "Liste des variables : ")
	if (model.g*model.w + length(model.varsInter) <= 10)
		for g in 1:model.g
			for w in 1:model.w
				println(io, "      ", model.X[g, w])
			end
		end
		for var in model.varsInter
			println(io, "      ", var)
		end
	else
		if model.g * model.w <= 8
			for g in 1:model.g
				for w in 1:model.w
					println(io, "      ", model.X[g, w])
				end
			end
			for i in 1:(8 - model.g*model.w)
				println(io, "      ", model.varsInter[i])
			end
		else
			g = 1
			w = 1
			ind = 1
			while ind <= 8
				println(io, "      ", model.X[g, w])
				w += 1
				if w > model.w
					w = 1
					g += 1
				end
				ind += 1
			end
		end
		println(io, "      ", "...")
	end
	if model.constraints != Set{Constraint}()
		println(io)
		println(io, "Liste des contraintes : ")
		constr = collect(model.constraints)
		if length(constr) <= 10
			for constraint in model.constraints
				println(io, "      ", constraint)
			end
		else
			for ind in 1:8
				println(io, "      ", constr[ind])
			end
			println(io, "      ", "...")
		end

	end

end

#dict{Variable, Set{Constraint}}

function returnParent!(changes::Dict{Variable, Change}, model::Model)

	for var in keys(changes)
		unforced!(var, changes[var], model)
	end
	nothing
end

function ModelTest(p::Int = 12, g::Int = 4, w::Int = 1)
	q = p ÷ g

	X = Array{Variable, 2}(undef, g, w)
	constraints = Set{Constraint}()

	for ind1 in 1:g
		for ind2 in 1:w
			X[ind1, ind2] = Variable(1:p, "X[$(ind1), $(ind2)]")
		end
	end

	model = Model(X,
				Vector{Variable}(),
				Set{Constraint}(),
				p, g, w)

	for indW in 1:w

		for indG in 1:g
			Cardinalite(model, X[indG, indW], q)
		end

		NDisjointUnion(model, X[1:end, indW], 1:p)

		for indG1 in 1:(g-1)
			for indG2 in (indG1+1):g
				EmptyIntersection(model, X[indG1, indW], X[indG2, indW])
			end
		end
	end

	for indW1 in 1:(w-1)
		for indW2 in (indW1+1):w
			for indG1 in 1:g
				for indG2 in 1:g

					Social(model, X[indG1, indW1], X[indG2, indW2])
				end
			end
		end
	end

	for indW in 1:w
		for indG1 in 1:(g-1)
			for indG2 in (indG1+1):g
				SortGroups(model, X[indG1, indW], X[indG2, indW])
			end
		end
	end

	for indW1 in 1:(w-1)
		for indW2 in (indW1+1):w
			SortWeeks(model, X[1, indW1], X[1, indW2])
		end
	end

	return model

end


text:
module Day16

using AdventOfCode2019

function day16(input::String = readInput(joinpath(@__DIR__, "..", "data", "day16.txt")))
    digits = parse.(Int8, split(strip(input), ""))
    p1 = part1!(copy(digits))
    p2 = part2(digits)
    return [p1, p2]
end

function part1!(digits::Array{Int8,1})
    factors = Array{Int8,1}(undef, length(digits))
    newDigits = copy(digits)
    for n = 1:100
        for i = 1:length(digits)
            generate_pattern!(factors, i)
            res = mod(digits .* factors |> sum |> abs, 10)
            newDigits[i] = res
        end
        copy!(digits, newDigits)
    end
    return parse(Int, join(digits[1:8]))
end

function generate_pattern!(pattern::Array{Int8,1}, iter::Int)
    base_pattern = Int8[0, 1, 0, -1]
    bpi = 1
    i = 0
    iteri = 1
    while i <= length(pattern)
        if i != 0
            pattern[i] = base_pattern[bpi]
        end
        iteri += 1
        if iteri > iter
            iteri = 1
            bpi = mod1(bpi + 1, length(base_pattern))
        end
        i += 1
    end
end

function part2(digits::Array{Int8,1})
    tlen = 10_000 * length(digits)
    nskip = parse(Int, join(digits[1:7]))
    rellen = tlen - nskip
    reldigits = Array{Int8,1}(undef, rellen)
    i = rellen
    j = length(digits)
    while i > 0
        reldigits[i] = digits[j]
        i -= 1
        j -= 1
        if j < 1
            j = length(digits)
        end
    end
    for iter = 1:100
        csum = 0
        for i = rellen:-1:1
            csum = mod(csum + reldigits[i], 10)
            reldigits[i] = csum
        end
    end
    return parse(Int, join(reldigits[1:8]))
end

end # module


text:
export changetype

function changetype(type::Array{Int64,1}, mask, ntype::Int64)
    type[mask] .= ntype
    return type
end

function changetype(obj::T, f::Function, ntype::Int64) where T <: Union{Shape, PostOpObj}
    # x, v, y, vol, type
    function func(out)
        mask = vec(f(out))
        type = changetype(out[5], mask, ntype)
        return out[1:4]..., type
    end
    if isa(obj, Shape)
        return PostOpObj(obj, func)
    elseif isa(obj, PostOpObj)
        push!(obj.operations, func)
        return obj
    else
        error("Not allowed.")
    end
end

max_stars_repo_name: ravinderbhattoo/PDMesh

max_issues_repo_name: ravinderbhattoo/PDMesh

max_forks_repo_name: ravinderbhattoo/PDMesh


text:
#
# The grouping combinator.
#

function GroupQuery(qbase::Query, qks::Vector{Query})
    for qk in qks
        @assert fits(output(qbase), input(qk))
        @assert !isplural(qk)
    end
    q = RecordQuery(
            qbase >>
            RecordQuery(ostub(qbase), qks))
    q >>
    Query(
        GroupSig(),
        Input(domain(output(q))),
        Output([output(qbase) |> setoptional(false), (output(qk) for qk in qks)...])
            |> setoptional(isoptional(output(qbase)))
            |> setplural(isplural(output(qbase)) && !isempty(qks)))
end

GroupQuery(qbase::Query, qks::Query...) =
    GroupQuery(qbase, collect(Query, qks))

immutable GroupSig <: AbstractPrimitive
end

describe(io::IO, ::GroupSig) = print(io, "group′")

ev(sig::GroupSig, ::Input, oty::Output, iflow::InputFlow) =
    OutputFlow(oty, ev(sig, values(iflow), fields(domain(iflow))...))

ev(sig::GroupSig, dv::DataVector, oty::Output) =
    group_impl(column(dv, 1), fields(oty))

function group_impl{OPT,PLU}(dvcol::Column{OPT,PLU}, fs::Vector{Output})
    dvoffs = offsets(dvcol)
    dv = values(dvcol)
    perm, offs = group_by_keys_impl(group_skip_impl(dvoffs), dv, fs)
    groupoffs = group_match_impl(dvoffs, offs)
    keyperm = perm[view(offs, 1:endof(offs)-1)]
    width = length(columns(dv)) - 1
    cols = Vector{Column}(width+1)
    cols[1] = Column{false,PLU}(offs, values(dv, 1)[perm])
    for k = 1:width
        cols[k+1] = column(dv, k+1)[keyperm]
    end
    return Column{OPT,PLU}(groupoffs, DataVector(length(offs)-1, cols))
end

function group_skip_impl(offs::AbstractVector{Int})
    empty = 0
    for k = 1:length(offs)-1
        if offs[k] == offs[k+1]
            empty += 1
        end
    end
    if empty == 0
        return offs
    end
    offs′ = Vector{Int}(length(offs)-empty)
    offs′[1] = 1
    n = 1
    for k = 1:length(offs)-1
        if offs[k] < offs[k+1]
            n += 1
            offs′[n] = offs[k+1]
        end
    end
    return offs′
end

function group_by_keys_impl(offs::AbstractVector{Int}, dv::DataVector, fs::Vector{Output})
    len = length(offs) - 1
    width = length(dv.cols) - 1
    vals = values(dv, 1)
    perm = collect(1:length(vals))
    for k = 1:width
        order = SortByOrdering(fs[k+1], column(dv, k+1))
        offs = group_by_key_impl!(offs, perm, order)
    end
    return perm, offs
end

function group_by_key_impl!(
        offs::AbstractVector{Int},
        perm::Vector{Int},
        order::SortByOrdering)
    len = length(offs)-1
    len′ = 0
    for k = 1:len
        l = offs[k]
        r = offs[k+1]
        sort!(view(perm, l:r-1), alg=MergeSort, order=order)
        len′ += 1
        for j = l+1:r-1
            if Base.lt(order, perm[j-1], perm[j])
                len′ += 1
            end
        end
    end
    offs′ = Vector{Int}(len′+1)
    offs′[1] = 1
    n = 1
    for k = 1:len
        l = offs[k]
        r = offs[k+1]
        for j = l+1:r-1
            if Base.lt(order, perm[j-1], perm[j])
                n += 1
                offs′[n] = j
            end
        end
        n += 1
        offs′[n] = r
    end
    return offs′
end

function group_match_impl(offs::AbstractVector{Int}, offs′::AbstractVector{Int})
    groupoffs = Vector{Int}(length(offs))
    n = 1
    for k = 1:length(offs)
        b = offs[k]
        while offs′[n] < b
            n += 1
        end
        groupoffs[k] = n
    end
    return groupoffs
end

max_stars_repo_name: rbt-lang/rbt-proto

max_issues_repo_name: rbt-lang/rbt-proto

max_forks_repo_name: rbt-lang/rbt-proto


text:
using Peridynamics
using Test
using SafeTestsets

@testset "Peridynamics" begin

    @safetestset "Discretization" begin
        include("discretization.jl")
    end

end


text:
using MDCDL
using Plots

function test_analysis_speed(ts::TransformSystem, data::AbstractArray, N::Integer)
    val, t, bytes, gctime, memallocs = @timed for n = 1:N
        analyze(ts, data)
    end
    return t
end

# image size
szxs = [ (8,8), (16,16) ,(32,32), (64,64), (128,128), (256,256), (512,512) ]
lnszxs = length(szxs)

nitrs = 100       # Number of iterations

df = (2,2)      # Decimation factor
ord = (2,2)     # Polyphase order
nch = 8         # Number of channels

nsolt = Rnsolt(df, ord, nch)    # create instance of Real-NSOLT (RNSOLT)
rand!(nsolt)    # randomize RNSOLT angle parameteres

tms = Array{Float64}(undef, lnszxs) # elapsed times
for idx = 1:lnszxs
    transform = createTransform(nsolt, Shapes.Vec(szxs[idx]...))
    x = rand(szxs[idx]...)
    tms[idx] = test_analysis_speed(transform, x, nitrs)
end

max_stars_repo_name: nixir/MDCDL

max_issues_repo_name: nixir/MDCDL

max_forks_repo_name: nixir/MDCDL


text:
module StructuredVariationalBayesTest

using Test
using ForneyLab
using ForneyLab: SoftFactor, generateId, addNode!, associate!, inferUpdateRule!, outboundType, isApplicable, setTargets!, messagePassingSchedule
using ForneyLab: VBGaussianMeanVarianceOut, SVBGaussianMeanPrecisionMGVD, SVBGaussianMeanPrecisionOutVGD, VBGammaOut, SVBGaussianMeanPrecisionW

# Integration helper
mutable struct MockNode <: SoftFactor
    id::Symbol
    interfaces::Vector{Interface}
    i::Dict{Int,Interface}

    function MockNode(vars::Vector{Variable}; id=generateId(MockNode))
        n_interfaces = length(vars)
        self = new(id, Vector{Interface}(undef, n_interfaces), Dict{Int,Interface}())
        addNode!(currentGraph(), self)

        for idx = 1:n_interfaces
            self.i[idx] = self.interfaces[idx] = associate!(Interface(self), vars[idx])
        end

        return self
    end
end

@structuredVariationalRule(:node_type     => MockNode,
                           :outbound_type => Message{PointMass},
                           :inbound_types => (Nothing, Message{PointMass}, ProbabilityDistribution),
                           :name          => SVBMock1VGD)

@structuredVariationalRule(:node_type     => MockNode,
                           :outbound_type => Message{PointMass},
                           :inbound_types => (Message{PointMass}, Nothing, ProbabilityDistribution),
                           :name          => SVBMock2GVD)

@structuredVariationalRule(:node_type     => MockNode,
                           :outbound_type => Message{PointMass},
                           :inbound_types => (ProbabilityDistribution, Nothing),
                           :name          => SVBMock3DV)

@testset "@structuredVariationalRule" begin
    @test SVBMock1VGD <: StructuredVariationalRule{MockNode}
    @test SVBMock2GVD <: StructuredVariationalRule{MockNode}
    @test SVBMock3DV <: StructuredVariationalRule{MockNode}
    @test isApplicable(SVBMock1VGD, [Nothing, Message{PointMass}, ProbabilityDistribution])
    @test !isApplicable(SVBMock1VGD, [Nothing, Message{PointMass}, ProbabilityDistribution, ProbabilityDistribution])
end

@testset "inferUpdateRule!" begin
    g = FactorGraph()
    v1 = Variable()
    n1 = MockNode([v1])
    v2 = Variable()
    n2 = MockNode([v2])
    v3 = Variable()
    n3 = MockNode([v3])
    nd = MockNode([v1, v2, v3])

    pfz = PosteriorFactorization()
    pf12 = PosteriorFactor([v1, v2])
    pf3 = PosteriorFactor(v3)
    setTargets!(pf12, pfz)
    setTargets!(pf3, pfz)

    entry1 = ScheduleEntry(nd.i[1], StructuredVariationalRule{MockNode})
    inferUpdateRule!(entry1, entry1.message_update_rule, Dict{Interface, Type}(nd.i[2].partner => Message{PointMass}))
    @test entry1.message_update_rule == SVBMock1VGD

    entry2 = ScheduleEntry(nd.i[2], StructuredVariationalRule{MockNode})
    inferUpdateRule!(entry2, entry2.message_update_rule, Dict{Interface, Type}(nd.i[1].partner => Message{PointMass}))
    @test entry2.message_update_rule == SVBMock2GVD

    entry3 = ScheduleEntry(nd.i[3], StructuredVariationalRule{MockNode})
    inferUpdateRule!(entry3, entry3.message_update_rule, Dict{Interface, Type}())
    @test entry3.message_update_rule == SVBMock3DV
end

@testset "messagePassingSchedule" begin
    g = FactorGraph()
    s_0 = Variable()
    nd_s_0 = GaussianMeanVariance(s_0, constant(0.0), constant(1.0))
    w = Variable()
    nd_w = Gamma(w, constant(1.0), constant(1.0))

    s = Variable[]
    nd_s = FactorNode[]

    s_min = s_0
    for i = 1:3
        s_i = Variable()
        nd_s_i = GaussianMeanPrecision(s_i, s_min, w)
        push!(s, s_i)
        push!(nd_s, nd_s_i)

        s_min = s_i
    end
    nd_s_i = GaussianMeanVariance(s_min, constant(0.0), constant(huge))
    push!(nd_s, nd_s_i)

    pfz = PosteriorFactorization()
    q_w = PosteriorFactor(w)
    q_s = PosteriorFactor([s_0; s])

    setTargets!(q_s, pfz, external_targets=true)
    schedule_q_s = messagePassingSchedule(q_s)
    @test length(schedule_q_s) == 8
    @test ScheduleEntry(nd_s_0.i[:out], VBGaussianMeanVarianceOut) in schedule_q_s
    @test ScheduleEntry(nd_s[4].i[:out], VBGaussianMeanVarianceOut) in schedule_q_s
    @test ScheduleEntry(nd_s[3].i[:m], SVBGaussianMeanPrecisionMGVD) in schedule_q_s
    @test ScheduleEntry(nd_s[2].i[:m], SVBGaussianMeanPrecisionMGVD) in schedule_q_s
    @test ScheduleEntry(nd_s[1].i[:m], SVBGaussianMeanPrecisionMGVD) in schedule_q_s
    @test ScheduleEntry(nd_s[1].i[:out], SVBGaussianMeanPrecisionOutVGD) in schedule_q_s
    @test ScheduleEntry(nd_s[2].i[:out], SVBGaussianMeanPrecisionOutVGD) in schedule_q_s
    @test ScheduleEntry(nd_s[3].i[:out], SVBGaussianMeanPrecisionOutVGD) in schedule_q_s

    setTargets!(q_w, pfz, external_targets=true)
    schedule_q_w = messagePassingSchedule(q_w)
    @test length(schedule_q_w) == 6
    @test ScheduleEntry(nd_w.i[:out], VBGammaOut) in schedule_q_w
    @test ScheduleEntry(nd_s[1].i[:w], SVBGaussianMeanPrecisionW) in schedule_q_w
    @test ScheduleEntry(nd_s[2].i[:w], SVBGaussianMeanPrecisionW) in schedule_q_w
    @test ScheduleEntry(nd_s[3].i[:w], SVBGaussianMeanPrecisionW) in schedule_q_w
end

@testset "messagePassingAlgorithm" begin
    g = FactorGraph()
    s_0 = Variable()
    nd_s_0 = GaussianMeanVariance(s_0, constant(0.0), constant(1.0))
    w = Variable()
    nd_w = Gamma(w, constant(1.0), constant(1.0))

    s = Variable[]
    nd_s = FactorNode[]

    s_min = s_0
    for i = 1:3
        s_i = Variable()
        nd_s_i = GaussianMeanPrecision(s_i, s_min, w)
        push!(s, s_i)
        push!(nd_s, nd_s_i)

        s_min = s_i
    end
    nd_s_i = GaussianMeanVariance(s_min, constant(0.0), constant(huge))
    push!(nd_s, nd_s_i)

    pfz = PosteriorFactorization([s_0; s], w)
    algo = messagePassingAlgorithm(s)

    @test isa(algo, InferenceAlgorithm)
end

end # module