text:
get_times(v::AxisArray{T}) where {T} = axisvalues(v[Axis{:time}])[1]

#This version plots groups in separate windows
#function plot(coms::Vector{ImagineSignal}; sampmap=:world)
#    used_idxs = find(x->length(x) > 0, coms)
#    used_coms = coms[used_idxs]
#    nsamps = length(used_coms[1])
#    @assert all(map(length, used_coms).==nsamps)
#    #NOTE: submitted bug report because plotting of matrices with UnitfulPlots isn't working
#    p = []
#    for (comgroup, groupname) in zip((getpositioners(used_coms), getcameras(used_coms), getlasers(used_coms), getstimuli(used_coms)), ("Positioners","Cameras", "Lasers", "Stimuli"))  #assumes each as the same world units within-group
#        ncoms = length(comgroup)
#        if ncoms == 0
#            continue
#        end
#        times = [get_times(get_samples(comgroup[1]))...]
#        for i = 1:ncoms
#            cursamps = get_samples(comgroup[i])
#            if eltype(cursamps) == Bool #work around another plotting bug
#                cursamps = map(UInt8, cursamps)
#            end
#            if i == 1
#                p = plot(times, cursamps, lab=name(comgroup[i]), title=groupname, reuse=false, show=true)
#            else
#                plot!(p, times, cursamps, lab=name(comgroup[i]), show=true)
#            end
#        end
#    end
#end

#This plots all signals in a single plot via subplots
function plot(coms::Vector{T}; sampmap=:world) where T<:ImagineSignal
    used_idxs = findall(x->length(x) > 0, coms)
    used_coms = coms[used_idxs]
    nsamps = length(used_coms[1])
    @assert all(map(length, used_coms).==nsamps)
    plts = []
    nuc = length(used_coms)
    ct = 1
    clrs = Colors.distinguishable_colors(nuc+1, colorant"white")[2:end]
    for (comgroup, groupname) in zip((getpositioners(used_coms), getcameras(used_coms), getlasers(used_coms), getstimuli(used_coms)), ("Positioners","Cameras", "Lasers", "Stimuli"))  #assumes each as the same world units within-group
        ncoms = length(comgroup)
        if ncoms == 0
            continue
        end
        times = [get_times(get_samples(comgroup[1]))...]
        for i = 1:ncoms
            cursamps = get_samples(comgroup[i])
            if eltype(cursamps) == Bool #work around another plotting bug
                cursamps = map(UInt8, cursamps)
            end
            #push!(plts, plot(times, cursamps, lab=name(comgroup[i]), title=groupname, reuse=false, show=false))
            push!(plts, plot(times, cursamps, lab=name(comgroup[i]), color = clrs[ct], show=false))
            ct+=1
        end
    end
    xts = Array{Any}(undef, 1, nuc)
    hts = fill(0.1, nuc)
    fill!(xts, nothing)
    xts[end] = :auto
    yts = :auto
    hts[findpositioners(used_coms)] .= 0.4
    output = plot(plts..., layout = grid(nuc,1,heights=hts), link=:x, xticks=xts, yticks=yts, reuse=false, show=true)
    return output
end

function plot(com::ImagineSignal; sampmap=:world)
    samps = get_samples(com)
    times = get_times(samps)
    if eltype(samps) == Bool #work around another plotting bug
        samps = map(UInt8, samps)
    end

    plot(times, samps, show=true, reuse=false)
end

max_stars_repo_name: HolyLab/ImaginePlots

max_issues_repo_name: HolyLab/ImaginePlots

max_forks_repo_name: HolyLab/ImaginePlots


text:
using PSCOPF

using .PSCOPFFixtures

using Test
using JuMP
using Dates
using DataStructures
using Printf

@testset verbose=true "test_tso_constraints" begin

    #=
        ECH = 10h
        TS: [11h, 11h15]
        S: [S1,S2]
                            bus 1                   bus 2
                            |                      |
        (limitable) wind_1_1|       "1_2"          |
        Pmin=0, Pmax=100    |                      |
        Csta=0, Cprop=1     |                      |
        S1: 20    S1: 15    |----------------------|
        S2: 30    S2: 30    |         35           |
                            |                      |
                            |                      |
        (pilotable) prod_1_1|                      |(pilotable) prod_2_1
        Pmin=10, Pmax=100   |                      | Pmin=10, Pmax=100
        Csta=45k, Cprop=10  |                      | Csta=80k, Cprop=15
    INIT: ON                |                      |INIT: ON
                            |                      | DP=DMO=2h
                            |                      |
            load(bus_1)     |                      |load(bus_2)
        S1: 10     S1: 17   |                      | S1: 40  S1: 48
        S2: 10     S2: 13   |                      | S2: 45  S2: 52
    =#

    TS = [DateTime("2015-01-01T11:00:00"), DateTime("2015-01-01T11:15:00")]
    ech = DateTime("2015-01-01T07:00:00")

    tso = PSCOPF.TSOOutFO()
    context = PSCOPFFixtures.context_2buses_2TS_2S(TS, ech)

    next_ech = DateTime("2015-01-01T07:30:00") # all decisions will be free
    result, firmness = PSCOPF.run_step!(context, tso, ech, next_ech)

    @testset "tso_successful_launch" begin
        # Solution is optimal
        @test PSCOPF.get_status(result) == PSCOPF.pscopf_OPTIMAL
    end

    @testset "tso_updated_schedule_respects_the_input_firmness" begin
        @test context.tso_schedule.decision_time == ech
        @test PSCOPF.verify_firmness(firmness, context.tso_schedule,
                        excluded_ids=PSCOPF.get_limitables_ids(context))
    end

    @testset "tso_respects_EOD" begin
        @test context.tso_schedule.decision_time == ech
        @test PSCOPF.verify_firmness(firmness, context.tso_schedule,
                        excluded_ids=PSCOPF.get_limitables_ids(context))
        for ts in TS
            for s in ["S1", "S2"]
                @test ( abs( PSCOPF.compute_eod(PSCOPF.get_uncertainties(context),
                                        PSCOPF.get_tso_schedule(context),
                                        PSCOPF.get_network(context),
                                        ech, ts, s) )
                        < 1e-09 )
            end
        end
    end

    @testset "tso_respects_RSO_constraints" begin
        # Solution is optimal
        @test PSCOPF.get_status(result) == PSCOPF.pscopf_OPTIMAL

        #prod_2_1 is used for RSO constraints
        @test PSCOPF.ON == PSCOPF.get_commitment_value(context.tso_schedule, "prod_2_1", TS[1], "S1")
        @test PSCOPF.ON == PSCOPF.get_commitment_value(context.tso_schedule, "prod_2_1", TS[2], "S1")
        @test PSCOPF.ON == PSCOPF.get_commitment_value(context.tso_schedule, "prod_2_1", TS[1], "S2")
        @test PSCOPF.ON == PSCOPF.get_commitment_value(context.tso_schedule, "prod_2_1", TS[2], "S2")
        #prod_1_1 might be used to deviate the least from market schedule

        # Note : compute_flow does not consider loss_of_load (valid here cause loss_of_load=0, proven by pscopf_OPTIMAL)
        for ts in TS
            for s in ["S1", "S2"]
                flow = PSCOPF.compute_flow("branch_1_2",
                                        PSCOPF.get_uncertainties(context),
                                        PSCOPF.get_tso_schedule(context),
                                        PSCOPF.get_network(context),
                                        ech, ts, s)
                @test PSCOPF.in_bounds(flow, -35., 35.)
            end
        end
    end

end

ext: jl

max_stars_repo_name: klorel/pscopf

max_issues_repo_name: klorel/pscopf

max_forks_repo_name: klorel/pscopf


text:
using Distributions: Distributions
using Bijectors: Bijectors
using Distributions: Univariate, Multivariate, Matrixvariate

"""
A named distribution that carries the name of the random variable with it.
"""
struct NamedDist{variate,support,Td<:Distribution{variate,support},Tv<:VarName} <:
       Distribution{variate,support}
    dist::Td
    name::Tv
end

NamedDist(dist::Distribution, name::Symbol) = NamedDist(dist, VarName{name}())

struct NoDist{variate,support,Td<:Distribution{variate,support}} <:
       Distribution{variate,support}
    dist::Td
end
NoDist(dist::NamedDist) = NamedDist(NoDist(dist.dist), dist.name)

Distributions.rand(rng::Random.AbstractRNG, d::NoDist) = rand(rng, d.dist)
Distributions.logpdf(d::NoDist{<:Univariate}, ::Real) = 0
Distributions.logpdf(d::NoDist{<:Multivariate}, ::AbstractVector{<:Real}) = 0
function Distributions.logpdf(d::NoDist{<:Multivariate}, x::AbstractMatrix{<:Real})
    return zeros(Int, size(x, 2))
end
Distributions.logpdf(d::NoDist{<:Matrixvariate}, ::AbstractMatrix{<:Real}) = 0
Distributions.minimum(d::NoDist) = minimum(d.dist)
Distributions.maximum(d::NoDist) = maximum(d.dist)

Bijectors.logpdf_with_trans(d::NoDist{<:Univariate}, ::Real) = 0
Bijectors.logpdf_with_trans(d::NoDist{<:Multivariate}, ::AbstractVector{<:Real}) = 0
function Bijectors.logpdf_with_trans(d::NoDist{<:Multivariate}, x::AbstractMatrix{<:Real})
    return zeros(Int, size(x, 2))
end
Bijectors.logpdf_with_trans(d::NoDist{<:Matrixvariate}, ::AbstractMatrix{<:Real}) = 0

max_stars_repo_name: TuringLang/MicroPPL

max_issues_repo_name: TuringLang/MicroPPL

max_forks_repo_name: TuringLang/MicroPPL


text:
#
# Makes backup of custom data type and stores it in a file.
# May load a backup from file also.
#
# @author DeadbraiN
#
module Backup
  import Config.@if_debug
  import Helper

  export folder
  export save
  export load
  export FOLDER_NAME
  #
  # Defoult postfix of backup files
  #
  const FILE_POSTFIX = "-jevo-bkup.data"
  #
  # Default name of the folders with backups
  #
  const FOLDER_NAME  = "backup"
  #
  # creates backup directory if it doesn't exists
  # @return {Bool}
  #
  function folder(folder::String = FOLDER_NAME)
    try
      if !isdir(folder) mkdir(folder) end
      return true
    catch e
      Helper.err("Backup.folder(): $e")
      @if_debug showerror(STDOUT, e, catch_backtrace())
    end
    return false
  end
  #
  # Makes a backup of application and stores it in file
  # @param data Data to save
  # @param directory Folder name
  # @param postfix Postfix for backup files
  # @param asText true if data need to be saved as a text
  # @return {Bool} Saving status
  #
  function save(data::Any, directory::String = FOLDER_NAME, postfix::String = FILE_POSTFIX, asText::Bool = false)
    local file::String = string(directory, "/", replace(string(now()), ":", "-"), postfix)

    if !folder(directory) return false end
    if isfile(file)
      Helper.warn("Backup file is already exists: $file")
      return false
    end

    Helper.save(data, file, asText)
  end
  #
  # Loads a backup from file and returns it
  # @param directory Folder name
  # @param file File name to load. If "" is set, last backup
  # will be loaded.
  # @return {Any|nothing} Loaded data or nothing if error
  #
  function load(directory::String = FOLDER_NAME, file::String = "")
    if !folder(directory) return nothing end
    if isempty(file)
      file = _getOldestFile(directory)
      if isempty(file)
        Helper.warn("No backup files")
        return nothing
      end
    end

    Helper.load(directory * "/" * file)
  end
  #
  # Returns array of backup files
  # @param directory Folder we are looking in
  # @return {Array{String, 1}}
  #
  function getFiles(directory::String = FOLDER_NAME)
    try
      if !isdir(directory) return [] end
      local rd::Array{AbstractString, 1} = readdir(directory)

      sort!(rd, alg = QuickSort, lt = (f1, f2) -> f1 < f2)
      return rd
    catch e
      Helper.warn("Backup.getFiles(): $e")
      @if_debug showerror(STDOUT, e, catch_backtrace())
    end
    []
  end
  #
  # Returns name of the last modified file in a folder
  # @param directory Folder with backup files
  # @return {String}
  #
  function lastFile(directory::String = FOLDER_NAME)
    _getOldestFile(directory)
  end

  #
  # Returns full path to the oldest (last modified file) file
  # in a folder "folder".
  # @param directory
  # @return {String} File path or "" if error
  #
  function _getOldestFile(directory::String)
    try
      if !isdir(directory) return "" end
      local rd::Array{AbstractString, 1} = readdir(directory)
      if length(rd) < 1 return "" end
      return rd[indmax(map((f) -> mtime(string(directory, "/", f)), rd))]
    catch e
      Helper.warn("Backup._getOldestFile(): $e")
      @if_debug showerror(STDOUT, e, catch_backtrace())
    end
    ""
  end
end

max_stars_repo_name: tmptrash/jevo

max_issues_repo_name: tmptrash/jevo

max_forks_repo_name: tmptrash/jevo


text:
using SafeTestsets

include("my_f_tests.jl")

@safetestset "My_f_tests" begin include("my_f_tests.jl") end


text:
mutable struct CircMatBuffer{T} <: AbstractVector{T}
    capacity::Int
    width::Int
    first::Int
    length::Int
    buffer::Matrix{T}
end

CircMatBuffer{T}() where {T} = CircMatBuffer{T}(0,0,1,0,Matrix{T}(undef,0,0))

CircMatBuffer{T}(width, capacity) where {T} =
    CircMatBuffer{T}(capacity, width, 1, 0, zeros(T, width, capacity))

Base.length(x::CircMatBuffer) = x.length
Base.size(x::CircMatBuffer) = (x.length,)
bufwidth(x::CircMatBuffer) = x.width

"""
    empty!(cb::CircMatBuffer)

Reset the buffer.
"""
function Base.empty!(x::CircMatBuffer)
    x.length=0
    x.first = 1
    return x
end

Base.@propagate_inbounds function _buffer_index_checked(cb::CircMatBuffer, i::Int)
    @boundscheck if i < 1 || i > cb.length
        throw(BoundsError(cb, i))
    end
    _buffer_index(cb, i)
end

@inline function _buffer_index(cb::CircMatBuffer, i::Int)
    n = cb.capacity
    idx = cb.first + i - 1
    return ifelse(idx > n, idx - n, idx)
end

"""
    cb[i]

Get the i-th element of CircMatBuffer.

* `cb[1]` to get the element at the front
* `cb[end]` to get the element at the back
"""
@inline Base.@propagate_inbounds function Base.getindex(cb::CircMatBuffer, i::Int)
    view(cb.buffer, :, _buffer_index_checked(cb, i))
end

"""
    cb[i] = data

Store data to the `i`-th element of `CirMatBuffer`.
"""
@inline Base.@propagate_inbounds function Base.setindex!(cb::CircMatBuffer, data, i::Int)
    b = cb[i]
    b .= data
    return cb
end

"""
    pop!(cb::CircMatBuffer)

Remove the element at the back.
"""
@inline function Base.pop!(cb::CircMatBuffer)
    @boundscheck (cb.length == 0) && throw(ArgumentError("array must be non-empty"))
    i = _buffer_index(cb, cb.length)
    cb.length -= 1
    return @inbounds view(cb.buffer, :, i)
end

"""
    push!(cb::CircMatBuffer, data)

Add an element to the back and overwrite front if full.
"""
@inline function Base.push!(cb::CircMatBuffer, data)
    # if full, increment and overwrite, otherwise push
    if cb.length == cb.capacity
        cb.first = (cb.first == cb.capacity ? 1 : cb.first + 1)
    else
        cb.length += 1
    end
    @inbounds view(cb.buffer, :, _buffer_index(cb, cb.length)) .= data
    return cb
end

"""
    `nextbuffer(cb::CircularMatBuffer)`

Return memory block to the next buffer slot.
"""
function nextbuffer(cb::CircMatBuffer)
    # if full, increment and overwrite, otherwise push
    if cb.length == cb.capacity
        cb.first = (cb.first == cb.capacity ? 1 : cb.first + 1)
    else
        cb.length += 1
    end
    return view(cb.buffer, :, _buffer_index(cb, cb.length))
end

"""
    popfirst!(cb::CircMatBuffer)

Remove the element from the front of the `CircMatBuffer`.
"""
function Base.popfirst!(cb::CircMatBuffer)
    @boundscheck (cb.length == 0) && throw(ArgumentError("array must be non-empty"))
    i = cb.first
    cb.first = (cb.first + 1 > cb.capacity ? 1 : cb.first + 1)
    cb.length -= 1
    return @inbounds view(cb.buffer, :, i)
end

"""
    pushfirst!(cb::CircMatBuffer, data)

Insert one or more items at the beginning of CircularBuffer
and overwrite back if full.
"""
function Base.pushfirst!(cb::CircMatBuffer, data)
    # if full, decrement and overwrite, otherwise pushfirst
    cb.first = (cb.first == 1 ? cb.capacity : cb.first - 1)
    if length(cb) < cb.capacity
        cb.length += 1
    end
    @inbounds cb.buffer[:,i] .= data
    return cb
end

"""
    append!(cb::CircMatBuffer, datavec::AbstractMatrix)

Push at most last `capacity` items.
"""
function Base.append!(cb::CircMatBuffer, datavec::AbstractMatrix)
    # push at most last `capacity` items
    n = size(datavec,2)
    for i in max(1, n-capacity(cb)+1):n
        push!(cb, view(datavec,:,i))
    end
    return cb
end

"""
    fill!(cb::CircMatBuffer, data)

Grows the buffer up-to capacity, and fills it entirely.
It doesn't overwrite existing elements.
"""
function Base.fill!(cb::CircMatBuffer, data)
    for i in 1:capacity(cb)-length(cb)
        push!(cb, data)
    end
    return cb
end

Base.eltype(::Type{CircMatBuffer{T}}) where T = T

function Base.convert(::Type{Array}, cb::CircMatBuffer{T}) where {T}

    n = length(cb)
    w = bufwidth(cb)
    a = zeros(T, w, n)

    for i in 1:n
        a[:,i] .= view(cb.buffer, :, _buffer_index(cb, i))
    end

    return a
end

flatten(cb::CircMatBuffer) = convert(Array, cb)

function flatten!(cb::CircMatBuffer{T}) where {T}

    a = flatten(cb)

    for i in 1:length(cb)
        cb.buffer[:,i] .= view(a, :, i)
    end
    cb.first = 1
    return cb
end

"""
    capacity(cb::CircMatBuffer)

Return capacity of CircMatBuffer.
"""
capacity(cb::CircMatBuffer) = cb.capacity

"""
    isfull(cb::CircMatBuffer)

Test whether the buffer is full.
"""
isfull(cb::CircMatBuffer) = length(cb) == cb.capacity

"""
    first(cb::CircMatBuffer)

Get the first element of CircMatBuffer.
"""
Base.@propagate_inbounds function Base.first(cb::CircMatBuffer)
    @boundscheck (cb.length == 0) && throw(BoundsError(cb, 1))
    return view(cb.buffer, :, cb.first)
end

"""
    last(cb::CircMatBuffer)

Get the last element of CircMatBuffer.
"""
Base.@propagate_inbounds function Base.last(cb::CircMatBuffer)
    @boundscheck (cb.length == 0) && throw(BoundsError(cb, 1))
    return view(cb.buffer, :, _buffer_index(cb, cb.length))
end

"""
    resize!(cb::CircMatBuffer, n)

Resize CircMatBuffer to the maximum capacity of n elements.
If n is smaller than the current buffer length, the first n elements will be retained.
"""
function Base.resize!(cb::CircMatBuffer, n::Integer)
    if n != capacity(cb) || size(cb.buffer, 1) == 0
        w = bufwidth(cb)
        buf_new = Matrix{eltype(cb)}(undef, w, n)
        cb.capacity = n
        cb.first = 1
        cb.length = 0
        cb.buffer = buf_new
    end
    return cb
end

function Base.resize!(cb::CircMatBuffer, w::Integer, n::Integer)

    if w == bufwidth(cb)
        return resize!(cb, n)
    else
        cb.capacity = n
        cb.width = w
        cb.length = 0
        cb.first = 1
        cb.buffer = zeros(eltype(cb), w, n)
        return cb
    end
end


text:
module TestMST

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

using Test
import .GraphIdx: kruskal_mst, prim_mst_edges

function from_networkx(graph; print_it = false)
    graph = reshape(reinterpret(Int, graph), 3, length(graph))'
    if print_it
        for i in 1:size(graph, 1)
            e = graph[i, :]
            println("$('A' + e[1]) -- $('A' + e[2]): $(e[3])")
        end
    end
    edges = vec(mapslices(Tuple, graph[:, 1:2] .+ 1, dims = [2]))
    weights = Float64.(graph[:, 3])
    n = maximum(map(maximum, edges))
    return n, edges, weights
end

# Adapted from
# https://github.com/networkx/networkx/blob/master/
#   networkx/algorithms/tree/tests/test_mst.py
const graph_x = [
    (0, 1, 7),
    (0, 3, 5),
    (1, 2, 8),
    (1, 3, 9),
    (1, 4, 7),
    (2, 4, 5),
    (3, 4, 15),
    (3, 5, 6),
    (4, 5, 8),
    (4, 6, 9),
    (5, 6, 11),
]

const expected_min_x = [
    (0, 1, 7),
    (0, 3, 5),
    (1, 4, 7),
    (2, 4, 5),
    (3, 5, 6),
    (4, 6, 9),
]

const expected_max_x = [
    (0, 1, 7),
    (1, 2, 8),
    (1, 3, 9),
    (3, 4, 15),
    (4, 6, 9),
    (5, 6, 11),
]

@testset "MST: Kruskal Wikipedia         " begin
    n, edges, weights = from_networkx(graph_x)

    selected = kruskal_mst(n, edges, +weights)
    expected = [e in expected_min_x for e in graph_x]
    # println(Int.(selected .⊻ expected))
    @test selected == expected

    selected = kruskal_mst(n, edges, -weights)
    expected = [e in expected_max_x for e in graph_x]
    @test selected == expected
end

@testset "MST: Prim    Wikipedia         " begin
    n, edges, weights = from_networkx(graph_x)

    _, selected = prim_mst_edges(n, edges, +weights)
    expected = Set(i for (i, e) in enumerate(graph_x) if e in expected_min_x)
    @test Set(selected) == expected

    _, selected = prim_mst_edges(n, edges, -weights)
    expected = Set(i for (i, e) in enumerate(graph_x) if e in expected_max_x)
    @test Set(selected) == expected
end

function capture_stdout(f::Function)
    STDOLD = stdout
    rd, wr = redirect_stdout()
    local ret
    try
        ret = f()
    finally
        redirect_stdout(STDOLD)
    end
    close(wr)
    (read(rd, String), ret)
end

@testset "MST: Own 3x7 Graph             " begin
    n1, n2 = 3, 7
    n = n1 * n2
    m = 32
    root = 1
    edges = GraphIdx.Edge{Int}[
        (1,  2),  (4,  5),  (7, 8), (10, 11), (13, 14), (16, 17),
        (19, 20), (2,  3),  (5, 6), (8,   9), (11, 12), (14, 15),
        (17, 18), (20, 21), (1, 4), (4,   7), (7,  10), (10, 13),
        (13, 16), (16, 19), (2, 5), (5,   8), (8,  11), (11, 14),
        (14, 17), (17, 20), (3, 6), (6,   9), (9,  12), (12, 15),
        (15, 18), (18, 21)
    ]
    @test GraphIdx.Grid.GridGraph(n1, n2) |> collect |> g -> g.edges == edges
    γ = [0.0, 1.0, 0.24, 0.04, 0.43, 0.73, 0.7, 0.28, 1.41, 1.43, 0.51, 0.52, 1.14,
    0.02, 0.11, 0.02, 0.79, 0.33, 0.74, 0.65, 1.11, 0.78, 0.51, 0.14, 0.44, 0.78, 0.58,
    0.80, 1.43, 0.13, 1.06, 0.38]

    @testset "prim_mst" begin
        pi, selected = GraphIdx.prim_mst_edges(n, edges, -γ, root)
        @test pi == [1, 5, 6, 1, 4, 5, 8, 9, 6, 7, 8, 9, 10, 15, 18, 13, 16, 17, 20, 17, 18]
        @test sum(γ[selected]) ≈ 15.79
        @test sum(
            γ[e] for (e, (u, v)) in enumerate(edges) if pi[u] == v || pi[v] == u
        ) ≈ 15.79
    end

    @testset "prim_mst_dbg" begin
        mem = GraphIdx.PrimMstMem(edges, n)
        dist = zeros(Float64, m)
        idx = GraphIdx.IncidenceIndex(n, edges)
        pi = zeros(Int, n)
        pq = GraphIdx.PriorityQueue{Int, Float64}(n)
        out, pi = capture_stdout() do
            GraphIdx._prim_mst_dbg(-γ, dist, pi, idx, pq)
        end
        @test pi == [1, 5, 6, 1, 4, 5, 8, 9, 6, 7, 8, 9, 10, 15, 18, 13, 16, 17, 20, 17, 18]
        @test out == """
            fin: 0 pi = 0 was -Inf
            new: 1 --> -0.0
            new: 3 --> -0.11
            fin: 3 pi = 0 was -0.11
            new: 4 --> -1.0
            new: 6 --> -0.02
            fin: 4 pi = 3 was -1.0
            new: 5 --> -1.41
            upd: 1 --> -1.11 was -0.0
            new: 7 --> -0.78
            fin: 5 pi = 4 was -1.41
            new: 2 --> -0.58
            new: 8 --> -0.8
            fin: 1 pi = 4 was -1.11
            fin: 8 pi = 5 was -0.8
            upd: 7 --> -1.43 was -0.78
            new: 11 --> -1.43
            fin: 7 pi = 8 was -1.43
            upd: 6 --> -0.24 was -0.02
            new: 10 --> -0.51
            fin: 11 pi = 8 was -1.43
            new: 14 --> -0.13
            fin: 2 pi = 5 was -0.58
            fin: 10 pi = 7 was -0.51
            new: 9 --> -0.04
            new: 13 --> -0.14
            fin: 6 pi = 7 was -0.24
            upd: 9 --> -0.79 was -0.04
            fin: 9 pi = 6 was -0.79
            new: 12 --> -0.33
            fin: 12 pi = 9 was -0.33
            upd: 13 --> -0.43 was -0.14
            new: 15 --> -0.74
            fin: 15 pi = 12 was -0.74
            new: 16 --> -0.73
            new: 18 --> -0.65
            fin: 16 pi = 15 was -0.73
            new: 17 --> -1.14
            upd: 13 --> -0.44 was -0.43
            new: 19 --> -0.78
            fin: 17 pi = 16 was -1.14
            upd: 14 --> -1.06 was -0.13
            new: 20 --> -0.38
            fin: 14 pi = 17 was -1.06
            upd: 13 --> -0.52 was -0.44
            fin: 19 pi = 16 was -0.78
            upd: 18 --> -0.7 was -0.65
            fin: 18 pi = 19 was -0.7
            fin: 13 pi = 14 was -0.52
            fin: 20 pi = 17 was -0.38
            """
    end
end

end

max_stars_repo_name: EQt/graphidx

max_issues_repo_name: EQt/graphidx

max_forks_repo_name: EQt/graphidx


text:
module test_h3_api_traversal

using Test
using H3.API # kRing maxKringSize kRingDistances hexRange hexRanges h3Line h3LineSize h3Distance experimentalH3ToLocalIj CoordIJ experimentalLocalIjToH3

@test kRing(0x085283473fffffff, 0) == [0x085283473fffffff]
@test kRing(0x085283473fffffff, 1) == [0x085283473fffffff, 0x085283447fffffff, 0x08528347bfffffff, 0x085283463fffffff, 0x085283477fffffff, 0x08528340ffffffff, 0x08528340bfffffff]

@test maxKringSize(0) == 1
@test maxKringSize(1) == 7

@test kRingDistances(0x08928308280fffff, 0) == (out = [0x08928308280fffff], distances = [0])
@test kRingDistances(0x08928308280fffff, 1) == (out = [0x08928308280fffff, 0x08928308280bffff, 0x089283082873ffff, 0x089283082877ffff, 0x08928308283bffff, 0x089283082807ffff, 0x089283082803ffff], distances = [0, 1, 1, 1, 1, 1, 1])

@test hexRange(0x085283473fffffff, 0) == [0x085283473fffffff]
@test hexRange(0x085283473fffffff, 1) == [0x085283473fffffff, 0x085283447fffffff, 0x08528347bfffffff, 0x085283463fffffff, 0x085283477fffffff, 0x08528340ffffffff, 0x08528340bfffffff]

@test hexRangeDistances(0x08928308280fffff, 0) == (out = [0x08928308280fffff], distances = [0])
@test hexRangeDistances(0x08928308280fffff, 1) == (out = [0x08928308280fffff, 0x08928308280bffff, 0x089283082873ffff, 0x089283082877ffff, 0x08928308283bffff, 0x089283082807ffff, 0x089283082803ffff], distances = [0, 1, 1, 1, 1, 1, 1])

@test hexRanges([0x08928308280fffff], 0) == [0x08928308280fffff]
@test hexRanges([0x08928308280fffff], 1) == [0x08928308280fffff, 0x08928308280bffff, 0x089283082873ffff, 0x089283082877ffff, 0x08928308283bffff, 0x089283082807ffff, 0x089283082803ffff]

@test hexRing(0x08928308280fffff, 0) == []
@test hexRing(0x08928308280fffff, 1) == [0x089283082803ffff, 0x08928308280bffff, 0x089283082873ffff, 0x089283082877ffff, 0x08928308283bffff, 0x089283082807ffff]

@test h3Line(0x08928308280fffff, 0x08928308280fffff) == [0x08928308280fffff]
@test h3Line(0x08928308280fffff, 0x08928308283bffff) == [0x08928308280fffff, 0x08928308283bffff]

@test h3LineSize(0x08928308280fffff, 0x08928308280fffff) == 1
@test h3LineSize(0x08928308280fffff, 0x08928308283bffff) == 2

@test h3Distance(0x08928308280fffff, 0x08928308280fffff) == 0
@test h3Distance(0x08928308280fffff, 0x08928308283bffff) == 1

@test experimentalH3ToLocalIj(0x08828308281fffff, 0x0882830828dfffff) == CoordIJ(393, 337)
@test experimentalLocalIjToH3(0x08828308281fffff, CoordIJ(393, 337)) == 0x0882830828dfffff

end # module test_h3_api_traversal


text:
isdefined(Base, :__precompile__) && __precompile__()

module BeaData

using Requests
using DataFrames, DataStructures
using DocStringExtensions
using Compat
import Base.show

export
    Bea,
    BeaNipaTable,
    get_nipa_table,
    nipa_metadata_tex,
    table_metadata_tex

const DEFAULT_API_URL   = "http://www.bea.gov/api/data"
const API_KEY_LENGTH    = 36
const DEFAULT_DATASET   = "NIPA"

"""
$(TYPEDEF)

A connection to the U.S. Bureau of Economic Analysis (BEA) Data API.

Constructors
------------
* `Bea()`
* `Bea(key::AbstractString)`

Arguments
---------
* `key`: Registration key provided by the BEA.

A valid registration key is required to retrieve data from the BEA's API.  A key can be obtained by registering at the BEA website.

A default API key can be specified in a ~/.beadatarc file.

Fields
------
$(FIELDS)

"""
type Bea
    url::AbstractString
    key::AbstractString
    dataset::AbstractString
end

function Bea(key="")
    if isempty(key)
        try
            open(joinpath(homedir(),".beadatarc"), "r") do f
                key = readstring(f)
            end
            key = rstrip(key)
            @printf "API key loaded.\n"
        catch
            error("No API key found, connection not initialized")
        end
    end

    # Key validation
    if length(key) > API_KEY_LENGTH || length(key) < API_KEY_LENGTH
        error("Invalid key length (≠ ", API_KEY_LENGTH, " chars), connection not initialized")
    end

    url = DEFAULT_API_URL
    dataset = DEFAULT_DATASET
    Bea(url, key, dataset)
end

function Base.show(io::IO, b::Bea)
    @printf io "BEA API Connection\n"
    @printf io "\turl: %s\n" b.url
    @printf io "\tkey: %s\n" b.key
    @printf io "\tdataset: %s\n" b.dataset
end

"""
$(TYPEDEF)

A NIPA table with data and metadata returned from a [`get_nipa_table`](@ref) call.

Fields
---
$(FIELDS)

"""
type BeaNipaTable
    "- NIPA table number"
    tablenum::AbstractString
    "- API TableID"
    tableid::Int
    "- The table title (e.g., 'Real Gross Domestic Product, Chained Dollars' for Table 1.1.6)"
    tabledesc::AbstractString
    "- `OrderedDict` of descriptions for each line of the table"
    linedesc::OrderedDict
    "- Table notes, if any"
    tablenotes::Any
    frequency::AbstractString
    startyear::Int
    endyear::Int
    "- `DataFrame` containing the data values from the table; column names are the line numbers from the table, the first column contains the date for each observation in Julia `Date` format"
    df::DataFrame
end

function Base.show(io::IO, b::BeaNipaTable)
    @printf io "BEA NIPA Table\n"
    @printf io "\tTable: %s\n" b.tablenum
    @printf io "\tTableID: %s\n" b.tableid
    @printf io "\tDescription: %s\n" b.tabledesc
    @printf io "\tCoverage: %s, from %s to %s\n" b.frequency b.startyear b.endyear
end

include("get_nipa_table.jl")
include("nipa_metadata_tex.jl")
include("table_metadata_tex.jl")

end # module


text:
function rkrp2tn(Ũ::Vector{Matrix{Float64}},ϵ::Float64)
    # repeated Kathti-Rao product into tensor network
    # Source: Batselier et al.: Tensor network subspace identification of polynomial state space models, Algorithm 2

    N    = length(Ũ);
    M    = size(Ũ[1],1);
    P    = size(Ũ[1],2);
    U    = Vector{Array{Float64,4}}(undef,N);
    U[1] = reshape(Ũ[N],(1,M,P,1));
    r1   = 1; err2 = 0;
    δ    = ϵ / sqrt(N-1) * norm(.*(Ũ));
    for i = 1:N-1
        T = reshape(U[i],(r1*M,P));
        Ttmp = KathriRao(Ũ[N-i],T,2)

        T = reshape(Ttmp,(r1*M,M*P));
        F = svd!(T);
        r2 = length(F.S);
        sv2 = cumsum(reverse(F.S).^2);
        tr  = findfirst(sv2 .> δ^2);
        if typeof(tr) == Nothing
            tr = length(sv2)-1;
        else
            tr = Int(tr)-1;
        end
        if tr > 0
            r2 = r2 - tr;
            err2 += sv2[tr];
        end

        U[i] = reshape(F.U[:,1:r2],(r1,M,1,r2));
        U[i+1] = reshape(diagm(F.S[1:r2])*F.Vt[1:r2,:],(r2,M,P,1));
        r1 = r2;
    end
    return MPT(U), err2
end

max_stars_repo_name: clarazen/BigMat

max_issues_repo_name: clarazen/BigMat

max_forks_repo_name: clarazen/BigMat


text:
@testset "utilities" begin
    s = rounded_star(1, 0.1, 7, 100)
    @test ParticleScattering.pInPolygon([0.0 0.0], s.ft)
    @test !ParticleScattering.pInPolygon([0.84 0.35], s.ft)
    @test ParticleScattering.pInPolygon([0.84 0.35], s.ft .+ 0.1)

    sp = ScatteringProblem([s],[1],[0.1 0.1], [15π])
    border = find_border(sp)
    border2 = find_border(sp,[0.1 0.1])
    @test border == border2
    border3 = find_border(sp,[0.1 0.9999])
    @test border != border3

    vs = [1.1im, -1.2, 1 + 324.0im, 1.1im, 1 - 324im, 1.0 + 324im]
    is, vs2 = uniqueind(vs)
    @test all(vs2[is] .== vs)
    @test length(vs2) == 4
end


text:
# Contents: Functions for creating/handling secure cookies.

################################################################################
# Create secure cookie
#
# The scheme:
#     const_key, const_iv     = global constants, output from a cryptographically secure random number generator
#                               (used to encrypt session-specific secret keys)
#     timestamp               = milliseconds since epoch, represented as a string
#     session_key, session_iv = output from a cryptographic random number generator, unique for each session
#     encrypted_session_key   = AES CBC encrypt(const_key, const_iv, session_key)
#     data blob               = AES CBC encrypt(session_key, session_iv, arbitrary data)
#     hmac signature          = HMAC(session_key, timestamp * data_blob)
#     unencoded cookie_value  = session_iv * encrypted_secret_key * hmac signature * timestamp * data blob
#     cookie_value            = base64encode(unencoded cookie value)
#                               (the encoding is for transport in an http header)
#
################################################################################

"""
Create a secure session cookie for the response.
The cookie value includes the encryption of the supplied data.
The Secure and HttpOnly attributes are set according to global variables.
"""
function set_securecookie!(res::Response, cookie_name::AbstractString, data::AbstractString, attr = config[:securecookie][:cookie_attr])
    cookie_value = create_securecookie_value(data)
    setcookie!(res, cookie_name, utf8(cookie_value), attr)
end

"""
Create the value of the secure session cookie.

Input:  Data (ASCIIString) to be embedded in the encrypted cookie value.
Output: Cookie value (ASCIIString)

Note: Binary data is base64 encoded for transport in http headers (base64 is more space efficient than hex encoding).
"""
function create_securecookie_value(plaintext::AbstractString)
    key_length, block_size, const_key, const_iv = get_securecookie_config()

    # Encrypt data
    session_key = csrng(key_length)
    session_iv  = csrng(block_size)
    data_blob   = encrypt(CIPHER_AES, session_key, plaintext, session_iv)    # Encryption is done in CBC mode

    # Compute HMAC signature
    timestamp      = string(get_timestamp())    # Millieconds since epoch
    ts_uint8       = convert(Array{UInt8, 1}, timestamp)
    hmac_signature = digest(MD_SHA256, vcat(ts_uint8, data_blob), session_key)

    # Compute cookie value
    encrypted_session_key = encrypt(CIPHER_AES, const_key, session_key, const_iv)
    cookie_value          = base64encode(vcat(session_iv, encrypted_session_key, hmac_signature, ts_uint8, data_blob))
    cookie_value
end

################################################################################
# Validate secure cookie
################################################################################

# The package validates a secure cookie as follows:
# 1. Decode hmac signature.
# 2. Compute HMAC(secret key, timestamp * data blob) and compare it to hmac signature. Fail if they differ.
# 3. Decode timestamp.
# 4. Verify that the current time in milliseconds since the epoch is not greater than timestamp + session timeout.
#
# If the cookie is valid, the package then:
# 1. Decrypts data blob
# 2. Parses or deserializes data blob as appropriate.

"""
Returns the decrypted cookie data.

Returns "" if the cookie doesn't exist.
"""
function get_securecookie_data(req::Request, cookie_name::AbstractString)
    result = ""
    if haskey(req.headers, "Cookie")
	cookie_value = get_cookie_value(req, cookie_name)
	if cookie_value != ""
	    cookie_is_valid, data_blob, session_key, session_iv = securecookie_is_valid(cookie_value)
	    if cookie_is_valid
		result = decrypt(CIPHER_AES, session_key, data_blob, session_iv)
		result = utf8(result)
	    end
	end
    end
    result
end

"""
Returns the cookie value, which is encrypted.

Returns "" if the cookie doesn't exist.
"""
function get_cookie_value(req::Request, cookie_name::AbstractString)
    cookie_value = ""
    ckie         = req.headers["Cookie"]    # ASCIIString: "name1=value1; name2=value2"
    names_values = split(ckie, ";")         # "name=value"
    for nv in names_values
	r = search(nv, cookie_name)         # first_idx:last_idx
	if length(r) > 0                    # cookie_name is in nv
	    r2           = search(nv, "=")
	    cookie_value = nv[(r2[1] + 1):end]
	    break
	end
    end
    convert(ASCIIString, cookie_value)      # Convert SubString to string for base64 decoding
end

"""
Returns: cookie_is_valid (Bool) and session data.

cookie_is_valid is true if session cookie:
1) Has not expired, and
2) hmac_signature == HMAC(secret key, timestamp * data_blob)
"""
function securecookie_is_valid(cookie_value::AbstractString)
    key_length, block_size, const_key, const_iv = get_securecookie_config()
    cookie_max_age = config[:securecookie][:cookie_max_age]

    # Extract cookie data
    cookie_value   = base64decode(cookie_value)
    session_iv     = cookie_value[1:block_size]
    offset         = block_size
    encrypted_session_key = cookie_value[(offset + 1):(offset + key_length + block_size)]
    session_key    = decrypt(CIPHER_AES, const_key, encrypted_session_key, const_iv)
    offset += key_length + block_size
    hmac_signature = slice(cookie_value, (offset + 1):(offset + key_length))
    offset += key_length
    ts_uint8       = cookie_value[(offset + 1):(offset + 13)]
    timestamp      = parse(Int, utf8(ts_uint8))    # Seconds since epoch
    offset += 13
    data_blob      = cookie_value[(offset + 1):end]

    # Determine conditions
    current_time = get_timestamp()
    expired      = current_time > timestamp + cookie_max_age
    hmac_sig2    = digest(MD_SHA256, vcat(ts_uint8, data_blob), session_key)
    hmac_ok      = hmac_sig2 == hmac_signature

    # Prepare results
    cookie_is_valid = false
    if !expired && hmac_ok
        cookie_is_valid = true
    end
    cookie_is_valid, data_blob, session_key, session_iv
end

"""
Invalidates the cookie with name == cookie_name.
Curently this works by setting:
- Max-Age = 0
- Data    = ""
"""
function invalidate_cookie!(res::Response, cookie_name::AbstractString)
    setcookie!(res, cookie_name, utf8(""), Dict("Max-Age" => utf8("0")))
end

################################################################################
# Utils
################################################################################
"""
Returns: Milliseconds since the epoch.
Takes 13 characters (valid until some time around the year 2287).
"""
function get_timestamp()
    1000 * convert(Int, Dates.datetime2unix(now()))
end

"Inserts derived values for keys: :const_key, :const_iv, :cookie_attr."
function update_securecookie_config!(k::Symbol)
    if k == :key_length    # :key_length has changed >> update :const_key
	key_length = config[:securecookie][:key_length]
	config[:securecookie][:const_key] = csrng(key_length)          # Symmetric key for encrypting secret_keys (with 256-bit encryption)
    elseif k == :block_size
	block_size = config[:securecookie][:block_size]
	config[:securecookie][:const_iv]  = csrng(block_size)          # IV for encrypting secret_keys
    elseif k == :cookie_max_age
	max_age     = config[:securecookie][:cookie_max_age]
	timeout_str = utf8(string(convert(Int64, 0.001 * max_age)))    # Session timeout in seconds, represented as a string
	config[:securecookie][:cookie_attr] = Dict("Max-Age" => timeout_str, "Secure" => "", "HttpOnly" => "")
    end
end

function get_securecookie_config()
    d = config[:securecookie]
    d[:key_length], d[:block_size], d[:const_key], d[:const_iv]
end

# EOF


text:
module TestRMachine

using AMLPipelineR
using Test
using DataFrames
using RDatasets

function caretrun()
   crt = CRTLearner(Dict(:learner=>"rf",:fitControl=>"trainControl(method='cv')"))
   iris=dataset("datasets","iris")
   x=iris[:,1:4]
   y=iris[:,5] |> Array{String}
   @test (fit_transform!(crt,x,y) .== y ) |> sum == 150
end
@testset "caret" begin
	caretrun()
end

end

max_stars_repo_name: ppalmes/AMLPipelineR

max_issues_repo_name: ppalmes/AMLPipelineR

max_forks_repo_name: ppalmes/AMLPipelineR


text:
module Qaintmodels

using LinearAlgebra
using SparseArrays
using Qaintessent

include("models.jl")
export
    qft_circuit,
    toffoli_circuit,
    vbe_adder_circuit,
    qcla_out_adder_circuit,
    qcla_inplace_adder_circuit

end # module


text:
export max_a_posteriori, MAP

import DataFrames: DataFrame, mapcols!

#####################
# Circuit MAP/MPE evaluation
#####################

"""
    max_a_posteriori(root::ProbCircuit, data::Union{Bool,Missing}...)
    max_a_posteriori(root::ProbCircuit, data::Union{Vector{<:Union{Bool,Missing}},CuVector{UInt8}})
    max_a_posteriori(circuit::ProbCircuit, data::DataFrame)
    max_a_posteriori(pbc::ParamBitCircuit, data; Float=Float32)

Evaluate maximum a-posteriori state of the circuit for given input(s).

Outputs the states, and the corresponding probabilities (in log domain).
"""
max_a_posteriori(root::ProbCircuit, data::Union{Bool,Missing}...) =
    max_a_posteriori(root, collect(Union{Bool,Missing}, data))

max_a_posteriori(root::ProbCircuit, data::Union{Vector{<:Union{Bool,Missing}},CuVector{UInt8}}) = begin
    map, prob = max_a_posteriori(root, DataFrame(reshape(data, 1, :)))
    example(map, 1), prob[1]
end

max_a_posteriori(circuit::ProbCircuit, data::DataFrame) =
    max_a_posteriori(same_device(ParamBitCircuit(circuit, data), data), data)

function max_a_posteriori(pbc::ParamBitCircuit, data; Float=Float32)
    @assert isgpu(data) == isgpu(pbc) "ParamBitCircuit and data need to be on the same device"
    values = marginal_all(pbc, data)
    return map_down(pbc, data, values; Float)
end

"""
Maximum a-posteriori queries
"""
const MAP = max_a_posteriori

"""
Mode of the distribution
"""
const mode = max_a_posteriori

"Find the MAP child value and node id of a given decision node"
function map_child(params, nodes, elements, ex_id, dec_id, values)
    @inbounds els_start = nodes[1,dec_id]
    @inbounds els_end = nodes[2,dec_id]
    pr_opt = typemin(eltype(values))
    j_opt = 1
    for j = els_start:els_end
        @inbounds prime = elements[2,j]
        @inbounds sub = elements[3,j]
        @inbounds pr = values[ex_id, prime] + values[ex_id, sub] + params[j]
        if pr > pr_opt
            pr_opt = pr
            j_opt = j
        end
    end
    @inbounds return params[j_opt], elements[2,j_opt], elements[3,j_opt]
end

# CPU code

function map_down(pbc, data, values::Array; Float=Float32)
    state = zeros(Bool, num_examples(data), num_features(data))
    logprob = zeros(Float, num_examples(data))
    Threads.@threads for ex_id = 1:size(state,1)
        map_rec(num_leafs(pbc), params(pbc), nodes(pbc), elements(pbc), ex_id, num_nodes(pbc), values, state, logprob)
    end
    df = DataFrame(state)
    mapcols!(c -> BitVector(c), df)
    return df, logprob
end

function map_rec(nl, params, nodes, elements, ex_id, dec_id, values, state, logprob)
    if isleafgate(nl, dec_id)
        if isliteralgate(nl, dec_id)
            l = literal(nl, dec_id)
            @inbounds state[ex_id, lit2var(l)] = (l > 0)
        end
    else
        edge_log_pr, prime, sub = map_child(params, nodes, elements, ex_id, dec_id, values)
        @inbounds logprob[ex_id] += edge_log_pr
        map_rec(nl, params, nodes, elements, ex_id, prime, values, state, logprob)
        map_rec(nl, params, nodes, elements, ex_id, sub,   values, state, logprob)
    end
end

# GPU code

function map_down(pbc, data, values::CuArray; Float=Float32)
    state = CUDA.zeros(Bool, num_examples(data), num_features(data))
    logprob = CUDA.zeros(Float, num_examples(data))
    stack = CUDA.zeros(Int32, num_examples(data), num_features(data)+3)
    @inbounds stack[:,1] .= 1 # start with 1 dec_id in the stack
    @inbounds stack[:,2] .= num_nodes(pbc) # start with the root in the stack
    num_threads = 256
    num_blocks = ceil(Int, size(state,1)/num_threads)
    CUDA.@sync begin
        @cuda threads=num_threads blocks=num_blocks map_cuda_kernel(num_leafs(pbc), params(pbc), nodes(pbc), elements(pbc), values, state, logprob, stack)
    end
    CUDA.unsafe_free!(values) # save the GC some effort
    # do the conversion to a CuBitVector on the CPU...
    df = DataFrame(to_cpu(state))
    mapcols!(c -> to_gpu(BitVector(c)), df)
    return df, logprob
end

function map_cuda_kernel(nl, params, nodes, elements, values, state, logprob, stack)
    index_x = (blockIdx().x - 1) * blockDim().x + threadIdx().x
    stride_x = blockDim().x * gridDim().x
    for ex_id = index_x:stride_x:size(state,1)
        dec_id = pop_cuda!(stack, ex_id)
        while dec_id > zero(eltype(stack))
            if isleafgate(nl, dec_id)
                if isliteralgate(nl, dec_id)
                    l = literal(nl, dec_id)
                    var = lit2var(l)
                    @inbounds state[ex_id, var] = (l > 0)
                end
            else
                edge_log_pr, prime, sub = map_child(params, nodes, elements, ex_id, dec_id, values)
                @inbounds logprob[ex_id] += edge_log_pr
                push_cuda!(stack, prime, ex_id)
                push_cuda!(stack, sub, ex_id)
            end
            dec_id = pop_cuda!(stack, ex_id)
        end
    end
    return nothing
end


text:
module DrugEfficacyPredictor
using Distributed, Printf

include("utils/Utils.jl")
include("data/Data.jl")
include("models/Models.jl")
include("webservice/Webservice.jl")
########################
######### TODO #########
########################
# steps
# cross-validation during training
end # module


text:
# This file is a part of SimilaritySearch.jl
# License is Apache 2.0: https://www.apache.org/licenses/LICENSE-2.0.txt

include("knr.jl")
include("kvp.jl")


text:
#!/usr/local/bin/julia

using Gadfly

using Bullseye

function savePlot{T}(data::Array{Float64,2}, labels::Array{T, 1},
                     filename::String)
    draw(PDF("$(filename).pdf", 6inch, 6inch),
         plot(x = data[1,:], y = data[2,:], color = labels));
end

data, labels = bullseye(3000, 2, [0.1, 0.4, 0.7, 1.0], [0.1, 0.1, 0.1, 0.1]);
savePlot(data, labels, "bullseye");

max_stars_repo_name: tudor-berariu/ml-hacks

max_issues_repo_name: tudor-berariu/ml-hacks

max_forks_repo_name: tudor-berariu/ml-hacks


text:
# Note that this script can accept some limited command-line arguments, run
# `julia build_tarballs.jl --help` to see a usage message.
using BinaryBuilder, Pkg

name = "S2Geometry"
version = v"0.9.0"

# Collection of sources required to complete build
sources = [
    GitSource("https://github.com/google/s2geometry.git", "a4dddf40647c68cd0104eafc31e9c8fb247a6308"),
    DirectorySource("./bundled"),
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/s2geometry
atomic_patch -p1 ../patches/disable-s2testing.patch
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=${prefix} \
    -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_EXAMPLES=OFF \
    ..
make -j${nproc}
make install
"""

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line
platforms = [
    Platform("x86_64", "linux"; libc="glibc"),
    Platform("aarch64", "linux"; libc="glibc"),
    Platform("powerpc64le", "linux"; libc="glibc"),
    Platform("x86_64", "linux"; libc="musl"),
    Platform("aarch64", "linux"; libc="musl"),
    Platform("x86_64", "macos"),
]
platforms = expand_cxxstring_abis(platforms)

# The products that we will ensure are always built
products = [
    LibraryProduct("libs2", :libs2),
]

# Dependencies that must be installed before this package can be built
dependencies = [
    Dependency(PackageSpec(name="OpenSSL_jll", uuid="458c3c95-2e84-50aa-8efc-19380b2a3a95")),
]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies)

max_stars_repo_name: sharanry/Yggdrasil

max_issues_repo_name: sharanry/Yggdrasil

max_forks_repo_name: sharanry/Yggdrasil


text:
using SparseInverseProblems
import SparseInverseProblems: lmo, phi, solveFiniteDimProblem, localDescent,
getStartingPoint, parameterBounds, computeGradient
using SparseInverseProblems.Util
using SpecialFunctions

struct GaussBlur2D <: BoxConstrainedDifferentiableModel
  sigma :: Float64
  n_pixels :: Int64
  grid :: Vector{Float64} #this is small now.
  grid_f
  u :: Vector{Float64}
  l :: Vector{Float64}
  GaussBlur2D(s,np,ng) = new(sqrt(s),np, range(0.0,stop=1.0,length=ng),
    computeFs(range(0.0,stop=1.0,length=ng),np,sqrt(s)),
    zeros(np), zeros(np))
end

function getStartingPoint(model :: GaussBlur2D, v :: Vector{Float64})
  v = reshape(v, model.n_pixels, model.n_pixels)
  ng = length(model.grid)
  grid_objective_values = vec(model.grid_f'*v*model.grid_f)
  best_point_idx = argmin(grid_objective_values)
  # println(best_point_idx)
  # I = LinearIndices((ng,ng));
# grid_objective_values
  # heatmap(grid_objective_values)
  # best_point_idx = I(CartesianIndices(best_point_lin_idx))
  # best_point_idx = ind2sub((ng,ng), best_point_lin_idx)
  best_grid_score = grid_objective_values[best_point_idx]
  println(best_grid_score)
  best_point_idx = CartesianIndices((ng,ng))[best_point_idx].I
   # println([model.grid[best_point_idx[2]];model.grid[best_point_idx[1]]])

  return [model.grid[best_point_idx[2]];model.grid[best_point_idx[1]]]
end

parameterBounds(model :: GaussBlur2D) = ([0.0,0.0],[1.0,1.0])

function computeGradient(model :: GaussBlur2D, weights :: Vector{Float64},
  thetas :: Matrix{Float64}, v :: Vector{Float64})
  gradient=0
  v = reshape(v, model.n_pixels, model.n_pixels)
  #
  gradient = zeros(length(thetas))
  # #allocate temporary variables...
  f_x = zeros(model.n_pixels)
  f_y = zeros(model.n_pixels)
  fpy = zeros(model.n_pixels)
  fpx = zeros(model.n_pixels)
  v_x = zeros(model.n_pixels)
  v_y = zeros(model.n_pixels)
  v_yp = zeros(model.n_pixels)

  # #compute gradient
  for i = 1:size(thetas,2)
    point = vec(thetas[:,i])
    computeFG(model, point[1], f_x, fpx)
    computeFG(model, point[2], f_y, fpy)
    v_x = mul!(v_x,v,f_x)
    v_y = mul!(v_y,v,f_y)
    v_yp = mul!(v_yp,v,fpy)
    function_value = dot(f_y, v_x)
    g_x = dot(v_y, fpx)
    g_y = dot(v_x, fpy)

    gradient[:,i] = weights[i]*[g_x; g_y]
  end
  return gradient
end

##not optimized.
function computeFs(x,n_pixels, sigma; result :: Matrix{Float64}  = zeros(n_pixels,length(x)))
  ng = length(x)
  z = zeros(n_pixels)
  u = zeros(n_pixels)
  l = zeros(n_pixels)
  for i = 1:ng
    computeF!(z,sigma,u,l,x[i])
    for j = 1:n_pixels
      @inbounds result[j,i] = z[j]
    end
  end
  return result
end
function computeFGs!(s :: GaussBlur2D,x,f,g)
  n_pixels = s.n_pixels
  ng = length(x)
  fz = zeros(n_pixels)
  gz = zeros(n_pixels)
  for i = 1:ng
    computeFG(s,x[i],fz,gz)
    @inbounds @simd for j= 1:length(fz)
      f[j,i] = fz[j]
      g[j,i] = gz[j]
    end
  end
end
function computeF!(f,sigma, u,l, x :: Float64)
  n_pixels = length(u)
  inc = 1.0/(sigma*n_pixels)
  xovers = x/sigma
  prefactor = sigma * sqrt(pi) / 2.0
  @fastmath @inbounds @simd for i = 1:n_pixels
    u[i] = i*inc - xovers
    l[i] = (i-1)*inc - xovers
  end
  @fastmath @inbounds @simd for i = 1:n_pixels
    f[i] = prefactor*(erf(u[i]) - erf(l[i]))
  end
  return f
end

function computeFG(s :: GaussBlur2D, x :: Float64, f :: Vector{Float64}  = zeros(s.n_pixels), g :: Vector{Float64}  = zeros(s.n_pixels))
  n_pixels = s.n_pixels
  u = s.u
  l = s.l
  inc = 1.0/(s.sigma*n_pixels)
  xovers = x/s.sigma
  prefactor = s.sigma * sqrt(pi) / 2.0

  @fastmath @inbounds @simd for i = 1:n_pixels
    u[i] = i*inc - xovers#(i - x)/s.sigma
    l[i] = (i-1)*inc - xovers
  end

  #might wanna just bite the bullet and allocate here...
  @fastmath @inbounds @simd  for i = 1:n_pixels
    f[i] = prefactor*(erf(u[i]) - erf(l[i]))
  end

  @fastmath @inbounds @simd for i = 1:n_pixels
    exp_u = exp(-u[i]*u[i])
    exp_l = exp(-l[i]*l[i])
    g[i] = exp_l - exp_u
  end
end

function phi(s :: GaussBlur2D, parameters :: Matrix{Float64},weights :: Matrix{Float64})
  n_pixels = s.n_pixels
  if size(parameters,2) == 0
    return zeros(n_pixels*n_pixels)
  end
  v_x = computeFs(vec(parameters[1,:]),n_pixels,s.sigma).*weights
  v_y = computeFs(vec(parameters[2,:]),n_pixels,s.sigma)
  return vec(v_y*v_x')
end

function solveFiniteDimProblem(model :: GaussBlur2D, loss :: Loss, thetas :: Matrix{Float64}, y :: Vector{Float64}, tau :: Float64)
  nPoints = size(thetas,2)
  y = reshape(y,model.n_pixels,model.n_pixels)
  t_vecs = Any[zeros(model.n_pixels,nPoints) for i = 1:4]
  computeFGs!(model,vec(thetas[1,:]),t_vecs[1:2]...)
  computeFGs!(model,vec(thetas[2,:]),t_vecs[3:4]...)
  K_x = (t_vecs[1]'*t_vecs[1])
  K_y = (t_vecs[3]'*t_vecs[3])
  K = K_x.*K_y
  AtY = vec(sum((t_vecs[3]'*y).*(t_vecs[1]'),2))
  return nnlasso_kernel(K,AtY, tau)
end


text:
# Basic layer defs
#
# (c) A. Dominik, 2020

"""
    abstract type Layer end

Mother type for layers hierarchy.
"""
abstract type Layer
end

"""
    struct Dense  <: Layer

Default Dense layer.

### Constructors:
+ `Dense(w, b, actf)`: default constructor
+ `Dense(i::Int, j::Int; actf=sigm)`: layer of j neurons with
        i inputs.
+ `Dense(h5::HDF5.File, group::String; trainable=false, actf=sigm)`:
+ `Dense(h5::HDF5.File, kernel::String, bias::String;
        trainable=false, actf=sigm)`: layer
        imported from a hdf5-file from tensorflow with the
        hdf-object hdfo and the group name group.
"""
struct Dense  <: Layer
    w
    b
    actf
    Dense(w, b, actf) = new(w, b, actf)
    Dense(i::Int, j::Int; actf=Knet.sigm) = new(Knet.param(j,i), Knet.param0(j), actf)
 end

Dense(h5::HDF5.File, group::String; trainable=false, actf=Knet.sigm) =
    Dense(h5, "$group/$group/kernel:0","$group/$group/bias:0", trainable=trainable, actf=actf)

function Dense(h5::HDF5.File, kernel::String, bias::String; trainable=false, actf=Knet.sigm)

    w = read(h5, kernel)
    b = read(h5, bias)

    if CUDA.functional()
        w = KnetArray(w)
        b = KnetArray(b)
    end
    if trainable
        w = Param(w)
        b = Param(b)
    end

    (o, i) = size(w)
    println("Generating $actf Dense layer from hdf with $o neurons and $i fan-in.")
    return Dense(w, b, actf)
end

(l::Dense)(x) = l.actf.(l.w * x .+ l.b)

function Base.summary(l::Dense; indent=0)
    n = get_n_params(l)
    o,i = size(l.w)
    s1 = "Dense layer $i → $o with $(l.actf),"
    return print_summary_line(indent, s1, n)
end

"""
    struct Linear  <: Layer

Almost standard dense layer, but functionality inspired by
the TensorFlow-layer:
+ capable to work with input tensors of
  any number of dimensions
+ default activation function `indetity`
+ optionally without biases.

The shape of the input tensor is preserved; only the size of the
first dim is changed from in to out.

### Constructors:
+ `Linear(i::Int, j::Int; bias=true, actf=identity)` weher `i` is fan-in
        and `j` is fan-out.

### Keyword arguments:
+ `bias=true`: if false biases are fixed to 0.0
+ `actf=identity`: activation function.
"""
struct Linear  <: Layer
    w
    b
    actf
    Linear(w, b, actf) = new(w, b, actf)
    Linear(i::Int, j::Int; bias=true, actf=identity) = new(Knet.param(j,i),
            bias ? Knet.param0(j) : init0(j), actf)
 end

 function (l::Linear)(x)
     j,i = size(l.w)   # get fan-in and out
     siz = vcat(j, collect(size(x)[2:end]))
     x = reshape(x, i,:)
     y = l.actf.(l.w * x .+ l.b)
     return reshape(y, siz...)
 end

function Base.summary(l::Linear; indent=0)
    n = get_n_params(l)
    o,i = size(l.w)
    s1 = "Linear layer $i → $o, with $(l.actf),"
    return print_summary_line(indent, s1, n)
end

"""
    struct Conv  <: Layer

Default Conv layer.

### Constructors:
+ `Conv(w, b, padding, actf)`: default constructor
+ `Conv(w1::Int, w2::Int,  i::Int, o::Int; actf=relu; kwargs...)`: layer with
    o kernels of size (w1,w2) for an input of i layers.
+ `Conv(h5::HDF5.File, group::String; trainable=false, actf=relu)`:
+ `Conv(h5::HDF5.File, group::String; trainable=false, actf=relu)`: layer
        imported from a hdf5-file from tensorflow with the
        hdf-object hdfo and the group name group.

### Keyword arguments:
+ `padding=0`: the number of extra zeros implicitly concatenated
        at the start and end of each dimension.
+ `stride=1`: the number of elements to slide to reach the next filtering window.
+ `dilation=1`: dilation factor for each dimension.
+ `...` See the Knet documentation for Details:
        https://denizyuret.github.io/Knet.jl/latest/reference/#Convolution-and-Pooling.
        All keywords to the Knet function `conv4()` are supported.
"""
struct Conv  <: Layer
    w
    b
    actf
    kwargs
    Conv(w, b, actf; kwargs...) = new(w, b, actf, kwargs)
    Conv(w1::Int, w2::Int,  i::Int, o::Int; actf=Knet.relu, kwargs...) =
            new(Knet.param(w1,w2,i,o; init=xavier_normal), Knet.param0(1,1,o,1),
                actf, kwargs)
end

(c::Conv)(x) = c.actf.(Knet.conv4(c.w, x; c.kwargs...) .+ c.b)

Conv(h5::HDF5.File, group::String; trainable=false, actf=Knet.relu) =
    Conv(h5, "$group/$group/kernel:0","$group/$group/bias:0", trainable=trainable, actf=actf)

function Conv(h5::HDF5.File, kernel::String, bias::String; trainable=false, actf=Knet.relu)

    w = read(h5, kernel)
    w = permutedims(w, [4,3,2,1])

    b = read(h5, bias)
    b = reshape(b, 1,1,:,1)

    if CUDA.functional()
        w = KnetArray(w)
        b = KnetArray(b)
    end
    if trainable
        w = Param(w)
        b = Param(b)
    end

    (w1, w2, i, o) = size(w)
    pad = (w1-1)÷2
    println("Generating layer from hdf with kernel ($w1,$w2), $i channels, $o kernels and $pad padding.")

    return Conv(w, b, actf; padding=pad)
end

function Base.summary(l::Conv; indent=0)
    n = get_n_params(l)
    k1,k2,i,o = size(l.w)
    if length(l.kwargs) > 0
        kwa = " $(collect(l.kwargs))"
    else
        kwa = ""
    end
    s1 = "Conv layer $i → $o ($k1,$k2)$kwa with $(l.actf),"
    return print_summary_line(indent, s1, n)
end

"""
    struct Pool <: Layer

Pooling layer.

### Constructors:
+ `Pool(;kwargs...)`: max pooling; without kwargs, 2x2-pooling
        is performed.

### Keyword arguments:
+ `window=2`: pooling window size (same for both directions)
+ `...`: See the Knet documentation for Details:
        https://denizyuret.github.io/Knet.jl/latest/reference/#Convolution-and-Pooling.
        All keywords to the Knet function `pool` are supported.
"""
struct Pool    <: Layer
    kwargs
    Pool(;kwargs...) = new(kwargs)
end

(l::Pool)(x) = Knet.pool(x; l.kwargs...)

function Base.summary(l::Pool; indent=0)
    n = get_n_params(l)
    if length(l.kwargs) > 0
        kwa = " $(collect(l.kwargs))"
    else
        kwa = ""
    end
    s1 = "Pool layer$kwa,"
    s2 = @sprintf("%10d params", n)
    return print_summary_line(indent, s1, n)
end

"""
    struct DeConv  <: Layer

Default deconvolution layer.

### Constructors:
+ `DeConv(w, b, actf, kwargs...)`: default constructor
+ `Conv(w1::Int, w2::Int,  i::Int, o::Int; actf=relu, kwargs...)`: layer with
    o kernels of size (w1,w2) for an input of i channels.
+ `Conv(h5::HDF5.File, group::String; trainable=false, actf=relu)`:
+ `Conv(h5::HDF5.File, group::String; trainable=false, actf=relu)`: layer
        imported from a hdf5-file from tensorflow with the
        hdf-object hdfo and the group name group.

### Keyword arguments:
+ `padding=0`: the number of extra zeros implicitly concatenated
        at the start and end of each dimension (applied to the output).
+ `stride=1`: the number of elements to slide to reach the next filtering window
        (applied to the output).
+ `...` See the Knet documentation for Details:
        https://denizyuret.github.io/Knet.jl/latest/reference/#Convolution-and-Pooling.
        All keywords to the Knet function `deconv4()` are supported.

"""
struct DeConv  <: Layer
    w
    b
    actf
    kwargs
    DeConv(w, b, actf; kwargs...) = new(w, b, actf, kwargs)
    DeConv(w1::Int, w2::Int,  i::Int, o::Int; actf=Knet.relu, kwargs...) =
            new(Knet.param(w1,w2,o,i; init=xavier_normal), Knet.param0(1,1,o,1),
            actf, kwargs)
end

(c::DeConv)(x) = c.actf.(Knet.deconv4(c.w, x; c.kwargs...) .+ c.b)

function Base.summary(l::DeConv; indent=0)
    n = get_n_params(l)
    k1,k2,i,o = size(l.w)
    if length(l.kwargs) > 0
        kwa = " $(collect(l.kwargs))"
    else
        kwa = ""
    end
    s1 = "DeConv layer $o → $i ($k1,$k2)$kwa with $(l.actf),"
    return print_summary_line(indent, s1, n)
end

"""
    struct UnPool <: Layer

Unpooling layer.

### Constructors:
+ `UnPool(;kwargs...)`: user-defined unpooling
"""
struct UnPool <: Layer
    kwargs
    UnPool(;kwargs...) = new(kwargs)
end
(l::UnPool)(x) = Knet.unpool(x; l.kwargs...)

function Base.summary(l::UnPool; indent=0)
    n = get_n_params(l)
    if length(l.kwargs) > 0
        kwa = " $(collect(l.kwargs))"
    else
        kwa = ""
    end
    s1 = "UnPool layer$kwa,"
    return print_summary_line(indent, s1, n)
end

"""
    struct Flat <: Layer

Default flatten layer.

### Constructors:
+ `Flat()`: with no options.
"""
struct Flat <: Layer
end
(l::Flat)(x) = Knet.mat(x)

function Base.summary(l::Flat; indent=0)
    n = get_n_params(l)
    s1 = "Flat layer,"
    return print_summary_line(indent, s1, n)
end

"""
    struct PyFlat <: Layer

Flatten layer with optional Python-stype flattening (row-major).
This layer can be used if pre-trained weight matrices from
tensorflow are applied after the flatten layer.

### Constructors:
+ `PyFlat(; python=true)`: if true, row-major flatten is performed.
"""
struct PyFlat <: Layer
    python
    PyFlat(; python=true) = new(python)
end
(l::PyFlat)(x) = l.python ? Knet.mat(permutedims(x, (3,2,1,4))) : mat(x)

function Base.summary(l::PyFlat; indent=0)
    n = get_n_params(l)
    if l.python
        s1 = "PyFlat layer with row-major (Python) flattening,"
    else
        s1 = "PyFlat layer with column-major (Julia) flattening,"
    end
    return print_summary_line(indent, s1, n)
end

"""
    struct Embed <: Layer

Simple type for an embedding layer to embed a virtual onehot-vector
into a smaller number of neurons by linear combination.
The onehot-vector is virtual, because not the vector, but only
the index of the "one" in the vector has to be provided as Integer value
(or a minibatch of integers).

### Fields:
+ w
+ actf

### Constructors:
+ `Embed(v,d; actf=identity):` with
    vocab size v, embedding depth d and default activation function idendity.

### Signatures:
+ `(l::Embed)(x) = l.actf.(w[:,x])` default
  embedding of input tensor x.
"""
struct Embed <: Layer
    w
    actf
    Embed(i, embed; actf=identity) = new(Knet.param(embed,i), actf)
end

(l::Embed)(x) = l.actf.(l.w[:,x])

function Base.summary(l::Embed; indent=0)
    n = get_n_params(l)
    o,i = size(l.w)
    s1 = "Embed layer $i → $o, with $(l.actf),"
    return print_summary_line(indent, s1, n)
end

"""
    struct Softmax <: Layer

Simple softmax layer to compute softmax probabilities.

### Constructors:
+ `Softmax()`
"""
struct Softmax <: Layer
end
(l::Softmax)(x) = Knet.softmax(x)

function Base.summary(l::Softmax; indent=0)
    n = get_n_params(l)
    s1 = "Softmax layer,"
    return print_summary_line(indent, s1, n)
end

"""
    struct Dropout <: Layer

Dropout layer.
Implemented with help of Knet's dropout() function that evaluates
AutoGrad.recording() to detect if in training or inprediction.
Dropouts are applied only if prediction.

### Constructors:
+ `Dropout(p)` with the dropout rate *p*.
"""
struct Dropout <: Layer
    p
end
(l::Dropout)(x) = Knet.dropout(x, l.p)

function Base.summary(l::Dropout; indent=0)
    n = get_n_params(l)
    s1 = "Dropout layer with p = $(l.p),"
    return print_summary_line(indent, s1, n)
end

"""
    struct BatchNorm <: Layer

Batchnormalisation layer.
Implemented with help of Knet's batchnorm() function that evaluates
AutoGrad.recording() to detect if in training or in prediction.
In training the moments are updated to record the running averages;
in prediction the moments are applied, but not modified.

In addition, optional trainable factor `a` and bias `b` are applied:

```math
y = a \\cdot \\frac{(x - \\mu)}{(\\sigma + \\epsilon)} + b
```

### Constructors:
+ `Batchnom(; trainable=false, channels=0)` will initialise
        the moments with `Knet.bnmoments()` and
        trainable parameters `a` and `b` only if
        `trainable==true` (in this case, the number of channels must
        be defined - for CNNs this is the number of feature maps).

### Details:
2d, 4d and 5d inputs are supported. Mean and variance are computed over
dimensions (2), (1,2,4) and (1,2,3,5) for 2d, 4d and 5d arrays, respectively.

If `trainable=true` and `channels != 0`, trainable
parameters `a` and `b` will be initialised for each channel.

If `trainable=true` and `channels == 0` (i.e. `Batchnom(trainable=true)`),
the params `a` and `b` are not initialised by the constructor.
Instead,
the number of channels is inferred when the first minibatch is normalised
as:
2d: `size(x)[1]`
4d: `size(x)[3]`
5d: `size(x)[4]`
or `0` otherwise.
"""
mutable struct BatchNorm <: Layer
    trainable
    moments
    params
end

function BatchNorm(; trainable=false, channels=0)
    if trainable
        p = init_bn_params(channels)
    else
        p = nothing
    end
    return BatchNorm(trainable, Knet.bnmoments(), p)
end

function (l::BatchNorm)(x)
    if l.trainable
        if length(l.params) == 0
            l.params = init_bn_params(x)
        end

        return Knet.batchnorm(x, l.moments, l.params)
    else
        return Knet.batchnorm(x, l.moments)
    end
end

function init_bn_params(x)

    if x isa Int
        channels = x
    elseif x isa Array
        dims = size(x)
        if length(dims) in (2, 4, 5)
            channels = dims[end-1]
        else
            channels = 0
        end
    else
        channels = 0
    end
    p = Knet.bnparams(Float32, channels)
    if !(p isa Param)
        p = Param(p)
    end
    return p
end

function Base.summary(l::BatchNorm; indent=0)
    n = get_n_params(l)
    if l.trainable
        s1 = "Trainable BatchNorm layer,"
    else
        s1 = "BatchNorm layer,"
    end
    return print_summary_line(indent, s1, n)
end

"""
    struct LayerNorm  <: Layer

Simple layer normalisation (inspired by TFs LayerNormalization).
Implementation is from Deniz Yuret's answer to feature request
429 (https://github.com/denizyuret/Knet.jl/issues/492).

The layer performs a normalisation within each sample, *not* batchwise.
Normalisation is modified by two trainable parameters `a` and `b`
(variance and mean)
added to every value of the sample vector.

### Constructors:
+ `LayertNorm(depth; eps=1e-6)`:  `depth` is the number
        of activations for one sample of the layer.

### Signatures:
+ `function (l::LayerNorm)(x; dims=1)`: normalise x along the given dimensions.
        The size of the specified dimension must fit with the initialised `depth`.
"""
struct LayerNorm  <: Layer
    a
    b
    ϵ
end

function LayerNorm(depth; eps=1e-6)
        a = param(depth; init=ones)
        b = param(depth; init=zeros)
        LayerNorm(a, b, Float32(eps))
end

function (l::LayerNorm)(x; dims=1)
    μ = mean(x, dims=dims)
    σ = std(x, mean=μ, dims=dims)
    return l.a .* (x .- μ) ./ (σ .+ l.ϵ) .+ l.b
end

function Base.summary(l::LayerNorm; indent=0)
    n = get_n_params(l)
    s1 = "Trainable LayerNorm layer,"
    return print_summary_line(indent, s1, n)
end

"""
    struct RSeqTagger <: Layer

One layer RNN sequence classifyer that works with minimatches of (time) series data.
minibatch can be a 2- or 3-dimensional Array.
If 2-d, inputs for one step are in one column and the Array has as
many colums as steps.
If 3-d, the last dimension iterates the samples of the minibatch.

Result is an array matrix with the output of the units of all
steps for all smaples of the minibatch (with model depth as first and samples of the minimatch as last dimension).

### Constructors:
+ `RSeqTagger(n_inputs::Int, n_units::Int; u_type=:lstm, o...)`: with
    number of inputs, number of units and unit type.
    Internally the type `Knet.RNN` is used and all keyword arguments
    of `Knet.RNN` may be provided.
"""
struct RSeqTagger <: Layer
    n_inputs
    n_units
    unit_type
    rnn
    RSeqTagger(n_inputs::Int, n_units::Int; u_type=:lstm, o...) =
            new(n_inputs, n_units, u_type, Knet.RNN(n_inputs, n_units; rnnType=u_type, o...))
end

function (rnn::RSeqTagger)(x)
    n_time_steps = size(x)[2]
    x = reshape(x, rnn.n_inputs, n_time_steps, :)
    x = permutedims(x, (1,3,2))
    x = rnn.rnn(x)
    return permutedims(x, (1,3,2)) # [units, time-steps, samples]
end

function Base.summary(l::RSeqTagger; indent=0)
    n = get_n_params(l)
    s1 = "RSeqTagger layer, $(l.n_inputs) → $(l.n_units) of type $(l.unit_type),"
    return print_summary_line(indent, s1, n)
end

"""
    struct RSeqClassifier <: Layer

One layer RNN sequence classifyer that works with minimatches of (time) series data.
minibatch can be a 2- or 3-dimensional Array.
If 2-d, inputs for one step are in one column and the Array has as
many colums as steps.
If 3-d, the last dimension iterates the samples of the minibatch.

Result is always a 2-d matrix with the output of the units of the last
step in each column and one column per sample of the minibatch.

### Constructors:
+ `RSeqClassifer(n_inputs::Int, n_units::Int; u_type=:lstm, o...)`: with
    number of inputs, number of units and unit type.
    Internally the type `Knet.RNN` is used and all keyword arguments
    of `Knet.RNN` may be provided.
"""
struct RSeqClassifier <: Layer
    n_inputs
    n_units
    unit_type
    rnn
    RSeqClassifier(n_inputs::Int, n_units::Int; u_type=:lstm, o...) =
            new(n_inputs, n_units, u_type, Knet.RNN(n_inputs, n_units; rnnType=u_type, o...))
end

function (rnn::RSeqClassifier)(x)
    n_time_steps = size(x)[2]
    x = reshape(x, rnn.n_inputs, n_time_steps, :)
    x = permutedims(x, (1,3,2))
    x = rnn.rnn(x)
    return x[:,:,end]     # [units, samples]
end

function Base.summary(l::RSeqClassifier; indent=0)
    n = get_n_params(l)
    s1 = "RSeqClassifyer layer, $(l.n_inputs) → $(l.n_units) of type $(l.unit_type),"
    return print_summary_line(indent, s1, n)
end

"""
    function hidden_states(l::<RNN_Type>)

Return the hidden states of one or more layers of an RNN.
`<RNN_Type>` is one of `RSeqClassifier`, `RSeqTagger`,
`Knet.RNN`.
"""
function hidden_states(l::Union{RSeqClassifier, RSeqTagger, Knet.RNN})
    if l isa Union{RSeqClassifier, RSeqTagger}
        return l.rnn.h
    elseif l isa Knet.RNN
        return l.h
    else
        return nothing
    end
end

"""
    function cell_states(l::<RNN_Type>)

Return the cell states of one or more layers of an RNN only if
it is a LSTM.
`<RNN_Type>` is one of `RSeqClassifier`, `RSeqTagger`,
`Knet.RNN`.
"""
function cell_states(l::Union{RSeqClassifier, RSeqTagger, Knet.RNN})
    if l isa Union{RSeqClassifier, RSeqTagger} &&
       l.unit_type == :lstm
        return l.rnn.c
    elseif l isa Knet.RNN && l.mode == 2  # i.e. :lstm
        return l.c
    else
        return nothing
    end
end

# return number of params:
#
function get_n_params(mdl)

    n = 0
    for p in params(mdl)
        n += length(p)
    end
    return n
end


text:
abstract type A end
abstract type B <:A end
abstract type C <:B end

@testset "subtypes" begin

    @transform function foo(a, b::subtypes(A), c::T) where {T<:Int64}
        println("a new method")
    end

    @test length(methods(foo)) == 1

end

@testset "allsubtypes" begin
    @transform function foo_all(a, b::allsubtypes(A), c::T) where {T<:Int64}
        println("a new method")
    end

    @test length(methods(foo_all)) == 3

end

@testset "curly" begin
    @transform function foo_curly(a, b::Type{allsubtypes(A)}, c::T) where {T<:Int64}
        println("a new method")
    end

    @test length(methods(foo_curly)) == 3

    @transform function foo_curly2(a, b::Union{T,allsubtypes(A)}, c::T) where {T<:Int64}
        println("a new method")
    end

    @test length(methods(foo_curly2)) == 3

end

@testset "noname arguemnts" begin
    @transform function foo_noname(a, ::allsubtypes(A))
        println("a new method")
    end

    @test length(methods(foo_noname)) == 3
end

@testset "array of functions" begin
    @transform [:subtypes, :allsubtypes], function foo_array(a, b::allsubtypes(A))
        println("a new method")
    end

    @test length(methods(foo_array)) == 3
end


text:
#=

# 103: 1D Convection-diffusion equation
([source code](SOURCE_URL))

Solve the equation

```math
\partial_t u -\nabla ( D \nabla u - v u) = 0
```
in $\Omega=(0,1)$ with homogeneous Neumann boundary conditon
at $x=0$ and outflow boundary condition at $x=1$.
=#

module Example103_ConvectionDiffusion1D
using Printf
using VoronoiFVM
using ExtendableGrids
using GridVisualize

## Bernoulli function used in the exponential fitting discretization
function bernoulli(x)
    if abs(x)<nextfloat(eps(typeof(x)))
        return 1
    end
    return x/(exp(x)-1)
end

function exponential_flux!(f,u0,edge,data)
    u=unknowns(edge,u0)
    vh=project(edge,data.v)
    Bplus= data.D*bernoulli(vh/data.D)
    Bminus=data.D*bernoulli(-vh/data.D)
    f[1]=Bminus*u[1,1]-Bplus*u[1,2]
end

function outflow!(f,u,node,data)
    if node.region==2
        f[1]=data.v[1]*u[1]
    end
end

function main(;n=10,Plotter=nothing,D=0.01,v=1.0,tend=100)

    ## Create a one-dimensional discretization
    h=1.0/n
    grid=VoronoiFVM.Grid(0:h:1)

    data=(v=[v],D=D)

    sys=VoronoiFVM.System(grid,VoronoiFVM.Physics(flux=exponential_flux!, data=data, breaction=outflow!))

    ## Add species 1 to region 1
    enable_species!(sys,1,[1])

    ## Set boundary conditions
    boundary_neumann!(sys,1,1,0.0)

    ## Create a solution array
    inival=unknowns(sys)
    inival[1,:].=map(x->1-2x,grid)

    ## Transient solution of the problem
    control=VoronoiFVM.NewtonControl()
    control.Δt=0.01*h
    control.Δt_min=0.01*h
    control.Δt_max=0.1*tend
    tsol=solve(inival,sys,[0,tend],control=control)

    vis=GridVisualizer(Plotter=Plotter)
    for i=1:length(tsol.t)
        scalarplot!(vis[1,1],grid,tsol[1,:,i],flimits=(0,1))
    end
    tsol
end

function test()
    tsol=main()
    maximum(tsol)<=1.0 && maximum(tsol[end])<1.0e-20
end

end


text:
@testset "Minimum Volume Ellipsoid" begin
    X = [
        -1 -1 1 2
        1 -1 -1 2
    ]

    @testset "Centered" begin
        ϵ = minimum_volume_ellipsoid(X, 1e-10; centered=true)

        @test ϵ.x == [0, 0]
        @test ϵ.H ≈ [5/8 -3/8; -3/8 5/8] atol = 1e-10
    end

    @testset "Not Centered" begin
        ϵ = minimum_volume_ellipsoid(X, 1e-10)

        @test ϵ.x ≈ [0.5, 0.5] atol = 1e-10
        @test ϵ.H ≈ [2/3 -2/9; -2/9 2/3] atol = 1e-10
    end
end


text:
module ParetoSmooth
using Requires
using DocStringExtensions

function __init__()
    @require Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" include("TuringHelpers.jl")
    @require MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d" include(
        "MCMCChainsHelpers.jl"
    )
end

include("AbstractCV.jl")
include("ESS.jl")
include("GPD.jl")
include("InternalHelpers.jl")
include("ImportanceSampling.jl")
include("LeaveOneOut.jl")
include("ModelComparison.jl")
include("PublicHelpers.jl")

end


text:
# Use baremodule to shave off a few KB from the serialized `.ji` file
baremodule KaHyPar_jll
using Base
using Base: UUID
import JLLWrappers

JLLWrappers.@generate_main_file_header("KaHyPar")
JLLWrappers.@generate_main_file("KaHyPar", UUID("87a0c12d-51e1-52a8-b1ed-2b00825fe6a4"))
end  # module KaHyPar_jll

max_stars_repo_name: vovakulikovu/JuliaBinaryWrappers

max_issues_repo_name: vovakulikovu/JuliaBinaryWrappers

max_forks_repo_name: vovakulikovu/JuliaBinaryWrappers


text:
#
# Tests for the TKE + mass flux module
#

function test_default_model_init(N=4, H=4.3)
    model = TKEMassFlux.Model(grid=UniformGrid(N=N, H=H))
    return model.grid.N == N && model.grid.H == H
end

function time_step_tke_mass_flux_model(boundary_layer_depth, mixing_length, nonlocal_flux,
                                       tke_equation, tke_wall_model, eddy_diffusivities)

    model = TKEMassFlux.Model(                grid = UniformGrid(N=4, H=4),
                              boundary_layer_depth = boundary_layer_depth,
                                     mixing_length = mixing_length,
                                     nonlocal_flux = nonlocal_flux,
                                eddy_diffusivities = eddy_diffusivities,
                                      tke_equation = tke_equation,
                                    tke_wall_model = tke_wall_model,
                                           stepper = :BackwardEuler)
    time_step!(model, 1e-16, 3)

    return true
end

eddy_diffusivity_models = (
                           TKEMassFlux.SinglePrandtlDiffusivities(),
                           TKEMassFlux.IndependentDiffusivities(),
                           TKEMassFlux.RiDependentDiffusivities(),
                          )

boundary_layer_depth_models = (
                               nothing,
                               ModularKPP.LMDMixingDepth(),
                               ModularKPP.ROMSMixingDepth(),
                              )

nonlocal_flux_models = (
                        nothing,
                        ModularKPP.LMDCounterGradientFlux(),
                        ModularKPP.DiagnosticPlumeModel(),
                       )

tke_equation_models = (
                       TKEMassFlux.TKEParameters(),
                      )

mixing_length_models = (
                        TKEMassFlux.SimpleMixingLength(),
                        TKEMassFlux.EquilibriumMixingLength(),
                       )

wall_models = (
               nothing,
               TKEMassFlux.PrescribedNearWallTKE(),
               TKEMassFlux.PrescribedSurfaceTKEValue(),
               TKEMassFlux.PrescribedSurfaceTKEFlux(),
              )

@testset "TKEMassFlux" begin
    @test test_default_model_init()

    # Modularity
    for boundary_layer_depth in boundary_layer_depth_models
        for nonlocal_flux in nonlocal_flux_models
            for tke_equation in tke_equation_models
                for mixing_length in mixing_length_models
                    for wall_model in wall_models
                        for eddy_diffusivity_model in eddy_diffusivity_models

                            @test time_step_tke_mass_flux_model(boundary_layer_depth,
                                                                mixing_length,
                                                                nonlocal_flux,
                                                                tke_equation,
                                                                wall_model,
                                                                eddy_diffusivity_model)

                        end
                    end
                end
            end
        end
    end
end


text:
using ColorTypes, FixedPointNumbers
using Test

@testset "rand" begin
    @testset "rand($T)" for T in (
            Gray{N0f8}, Gray{N2f6}, Gray{N0f16}, Gray{N2f14}, Gray{N0f32}, Gray{N2f30},
            Gray{Float16}, Gray{Float32}, Gray{Float64},
            RGB{N0f8}, RGB{N2f6}, RGB{N0f16}, RGB{N2f14}, RGB{N0f32}, RGB{N2f30},
            RGB{Float16}, RGB{Float32}, RGB{Float64},
            AGray{Float32}, GrayA{Float64},
            RGBA{Float32}, ARGB{N0f16}, XRGB{N0f8}, RGBX{Float64},
            BGR{Float16}, ABGR{N0f32}, BGRA{N2f14},
            Gray, AGray, GrayA,
            RGB, ARGB, RGBA, BGR, ABGR, BGRA, XRGB, RGBX,
            HSV, HSL, Lab, LCHab, YIQ)
        a = rand(T)
        @test all(x->x[2]<=getfield(a,x[1])<=x[3],
                    zip(ColorTypes.colorfields(T),gamutmin(T),gamutmax(T)))
        @test isa(a, T)
        a = rand(T, (3, 5))
        if isconcretetype(T)
            @test isa(a, Array{T,2})
        end
        for el in a
            @test all(x->x[2]<=getfield(el,x[1])<=x[3],
                        zip(ColorTypes.colorfields(T),gamutmin(T),gamutmax(T)))
        end
        @test eltype(a) <: T
        @test size(a) == (3,5)
        ap = a'
        @test ap[1,1] == a[1,1]
    end
    @testset "rand($T)" for T in (Gray24, AGray32)
        a = rand(T)
        b = a.color
        @test b&0xff == (b>>8)&0xff == (b>>16)&0xff
        @test isa(a, T)
        a = rand(T,3,5)
        for el in a
            b = el.color
            @test b&0xff == (b>>8)&0xff == (b>>16)&0xff
        end
        @test eltype(a) <: T
        @test size(a) == (3,5)
    end
    @test_broken rand(RGB24)
    @test_broken rand(ARGB32)
end

@testset "mapc" begin
    @test @inferred(mapc(sqrt, Gray{N0f8}(0.04))) === Gray(sqrt(N0f8(0.04)))
    @test @inferred(mapc(sqrt, AGray{N0f8}(0.04, 0.4))) === AGray(sqrt(N0f8(0.04)), sqrt(N0f8(0.4)))
    @test @inferred(mapc(sqrt, GrayA{N0f8}(0.04, 0.4))) === GrayA(sqrt(N0f8(0.04)), sqrt(N0f8(0.4)))
    @test @inferred(mapc(x->2x, RGB{N0f8}(0.04,0.2,0.3))) === RGB(map(x->2*N0f8(x), (0.04,0.2,0.3))...)
    @test @inferred(mapc(sqrt, RGBA{N0f8}(0.04,0.2,0.3,0.7))) === RGBA(map(x->sqrt(N0f8(x)), (0.04,0.2,0.3,0.7))...)
    @test @inferred(mapc(x->1.5f0x, RGBA{N0f8}(0.04,0.2,0.3,0.4))) === RGBA(map(x->1.5f0*N0f8(x), (0.04,0.2,0.3,0.4))...)

    @test @inferred(mapc(max, Gray{N0f8}(0.2), Gray{N0f8}(0.3))) === Gray{N0f8}(0.3)
    @test @inferred(mapc(-, AGray{Float32}(0.3), AGray{Float32}(0.2))) === AGray{Float32}(0.3f0-0.2f0,0.0)
    @test @inferred(mapc(min, RGB{N0f8}(0.2,0.8,0.7), RGB{N0f8}(0.5,0.2,0.99))) === RGB{N0f8}(0.2,0.2,0.7)
    @test @inferred(mapc(+, RGBA{N0f8}(0.2,0.8,0.7,0.3), RGBA{Float32}(0.5,0.2,0.99,0.5))) === RGBA(0.5f0+N0f8(0.2),0.2f0+N0f8(0.8),0.99f0+N0f8(0.7),0.5f0+N0f8(0.3))
    @test @inferred(mapc(+, HSVA(0.1,0.8,0.3,0.5), HSVA(0.5,0.5,0.5,0.3))) === HSVA(0.1+0.5,0.8+0.5,0.3+0.5,0.5+0.3)
    @test_throws ArgumentError mapc(min, RGB{N0f8}(0.2,0.8,0.7), BGR{N0f8}(0.5,0.2,0.99))
    @test @inferred(mapc(abs, -2)) === 2
end

@testset "reducec" begin
    @test @inferred(reducec(+, 0.0, Gray(0.3))) === 0.3
    @test @inferred(reducec(+, 1.0, Gray(0.3))) === 1.3
    @test @inferred(reducec(+, 0, Gray(0.3))) === 0.3
    @test @inferred(reducec(+, 0.0, AGray(0.3, 0.8))) === 0.3 + 0.8
    @test @inferred(reducec(+, 0.0, RGB(0.3, 0.8, 0.5))) === (0.3 + 0.8) + 0.5
    @test @inferred(reducec(+, 0.0, RGBA(0.3, 0.8, 0.5, 0.7))) === ((0.3 + 0.8) + 0.5) + 0.7
    @test @inferred(reducec(&, true, Gray(true)))
    @test !(@inferred(reducec(&, false, Gray(true))))
    @test !(@inferred(reducec(&, true, Gray(false))))

    @test @inferred(reducec(+, 0.0, 0.3)) === 0.3
    @test @inferred(reducec(+, 0, 0.3)) === 0.3
    @test @inferred(reducec(&, true, true))
    @test !(@inferred(reducec(&, false, true)))
    @test !(@inferred(reducec(&, true, false)))
end

@testset "mapreducec" begin
    @test @inferred(mapreducec(x->x^2, +, 0.0, Gray(0.3))) === 0.3^2
    @test @inferred(mapreducec(x->x^2, +, 1.0, Gray(0.3))) === 1 + 0.3^2
    @test @inferred(mapreducec(x->x^2, +, 0, Gray(0.3))) === 0.3^2
    @test @inferred(mapreducec(x->x^2, +, 0.0, AGray(0.3, 0.8))) === 0.3^2 + 0.8^2
    @test @inferred(mapreducec(x->x^2, +, 0.0, RGB(0.3, 0.8, 0.5))) === (0.3^2 + 0.8^2) + 0.5^2
    @test @inferred(mapreducec(x->x^2, +, 0.0, RGBA(0.3, 0.8, 0.5, 0.7))) === ((0.3^2 + 0.8^2) + 0.5^2) + 0.7^2
    @test !(@inferred(mapreducec(x->!x, &, true, Gray(true))))
    @test !(@inferred(mapreducec(x->!x, &, false, Gray(true))))
    @test @inferred(mapreducec(x->!x, &, true, Gray(false)))
    @test !@inferred(mapreducec(x->!x, &, false, Gray(false)))

    @test @inferred(mapreducec(x->x^2, +, 0.0, 0.3)) === 0.3^2
    @test @inferred(mapreducec(x->x^2, +, 1.0, 0.3)) === 1 + 0.3^2
    @test @inferred(mapreducec(x->x^2, +, 0, 0.3)) === 0.3^2
    @test !(@inferred(mapreducec(x->!x, &, true, true)))
    @test !(@inferred(mapreducec(x->!x, &, false, true)))
    @test @inferred(mapreducec(x->!x, &, true, false))
    @test !@inferred(mapreducec(x->!x, &, false, false))
end


text:
function build()
    run(`make clean`)
    run(`make library`)
end
cd(build, "src/gsw_c_v3.03/")

if ~isdir("build")
    mkdir("build")
end

ext = @linux? (".so" : @osx? ( ".dylib" : @windows? ( ".dll" : "" )))
if ext == ""
    error("Platform not linux, OS X, or Windows")
end

run(`cp $("src/gsw_c_v3.03/libgswteos-10"*ext) $("build/libgswteos-10"*ext)`)

# using BinDeps
#
# @BinDeps.setup
#
# gsw = library_dependency("gsw")
# provides(Sources,
#          URI("http://www.teos-10.org/software/gsw_c_v3.03.zip"),
#          gsw,
#          unpacked_dir="gsw")
#
# @build_steps begin
#     GetSources(gsw)
#     CreateDirectory("build")
#     FileUnpacker("gsw_c_v3.03.zip", "build")
#     @build_steps begin
#         ChangeDirectory("build")
#         MakeTargets("gsw_c_v3.03")
#         FileRule(joinpath("build", "gsw-3.03.so"), @build_steps begin
#             `cp gsw_c_v3.03/gsw-3.03.so .`
#         end)
#     end
# end
#
# @BinDeps.install


text:
"""
`utd(yr,r,g,t)`: Share of total trade by region.

```math
\\alpha_{yr,r,g,t}^{utd}
=
\\begin{cases}
\\dfrac{           \\bar{utd}_{yr,r ,g,t}}
       {\\sum_{r'} \\bar{utd}_{yr,r',g,t}}        & notrd\\ni g
\\\\
\\dfrac{\\sum_{yr'}    \\bar{utd}_{yr',r ,g,t}}
       {\\sum_{yr',r'} \\bar{utd}_{yr',r',g,t}}   & notrd\\in g
\\end{cases}
```
"""
function share_utd!(d::Dict, set::Dict)
    println("  Calculating utd(yr,r,g,t), share of total trade by region")
    d[:utd] = fill_zero(d[:utd])
    df = d[:utd] / transform_over(d[:utd], :r)

    df_yr = transform_over(d[:utd], :yr) / transform_over(d[:utd], [:yr,:r])
    df[isnan.(df[:,:value]), :value] .= df_yr[isnan.(df[:,:value]),:value]

    # Check import and export shares.
    verify_over(filter_with(df, (t = "imports",)), :r) !== true && @error("Import shares don't sum to 1.")
    verify_over(filter_with(df, (t = "exports",)), :r) !== true && @error("Export shares don't sum to 1.")

    d[:utd] = dropnan(dropzero(df))
    set[:notrd] = _set_notrd!(d, set)
    return d[:utd]
end

" `notrd`: Goods/sectors not included in imports/exports."
function _set_notrd!(d::Dict, set::Dict)
    !(:utd in keys(d)) && @error("shr[:utd] required to find set[:notrd]")
    set[:notrd] = setdiff(set[:g], d[:utd][:,:g])
    return set[:notrd]
end

max_stars_repo_name: NREL/SLiDE

max_issues_repo_name: NREL/SLiDE

max_forks_repo_name: NREL/SLiDE


text:
module GLVisualize

using ..GLAbstraction
using AbstractPlotting: RaymarchAlgorithm, IsoValue, Absorption, MaximumIntensityProjection, AbsorptionRGBA, IndexedAbsorptionRGBA

using ..GLMakie.GLFW
using ModernGL
using StaticArrays
using GeometryTypes
using Colors
using AbstractPlotting
using FixedPointNumbers
using FileIO
using FreeType
import IterTools
using Markdown
using FreeTypeAbstraction
using ImageCore
import ColorVectorSpace
using Observables

import ImageCore
import AxisArrays, ImageAxes

import Base: merge, convert, show
using Base.Iterators: Repeated, repeated
using LinearAlgebra

using IndirectArrays
const HasAxesArray{T, N} = AxisArrays.AxisArray{T, N}
const AxisMatrix{T} = HasAxesArray{T, 2}

import AbstractPlotting: to_font, glyph_uv_width!, glyph_scale!
import ..GLMakie: get_texture!

const GLBoundingBox = AABB{Float32}

"""
Replacement of Pkg.dir("GLVisualize") --> GLVisualize.dir,
returning the correct path
"""
dir(dirs...) = joinpath(@__DIR__, dirs...)
using ..GLMakie: assetpath, loadasset

include("types.jl")
export CIRCLE, RECTANGLE, ROUNDED_RECTANGLE, DISTANCEFIELD, TRIANGLE

include("visualize_interface.jl")
export visualize # Visualize an object
export visualize_default # get the default parameter for a visualization

include("utils.jl")
export y_partition, y_partition_abs
export x_partition, x_partition_abs
export loop, bounce

include(joinpath("visualize", "lines.jl"))
include(joinpath("visualize", "image_like.jl"))
include(joinpath("visualize", "mesh.jl"))
include(joinpath("visualize", "particles.jl"))
include(joinpath("visualize", "surface.jl"))

end # module


text:
@testset "Shape convertion" begin
    # shapes
    box = Dojo.Box(1.0, 1.0, 1.0)
    cylinder = Dojo.Cylinder(1.0, 1.0)
    capsule = Dojo.Capsule(1.0, 1.0)
    shapes = Dojo.Shapes([box, cylinder, capsule])
    sphere = Dojo.Sphere(1.0)
    pyramid = Dojo.Pyramid(1.0, 1.0)
    mesh = Dojo.Mesh(joinpath(dirname(pathof(DojoEnvironments)), "atlas/deps/mesh/head.obj"))

    # convert
    box_geom = Dojo.convert_shape(box)
    cylinder_geom = Dojo.convert_shape(cylinder)
    capsule_geom = Dojo.convert_shape(capsule)
    shapes_geom = Dojo.convert_shape(shapes)
    sphere_geom = Dojo.convert_shape(sphere)
    pyramid_geom = Dojo.convert_shape(pyramid)
    mesh_geom = Dojo.convert_shape(mesh)
    @test true
end