text:
import PyPlot.plot

function plot(cycle::Cycle)
    time = linrange(cycle.t_s,cycle.t_e,25)
    amplitude = abs(cycle.v_s-cycle.v_e)/2
    dt = cycle.t_s-cycle.t_e
    if cycle.count==1
        plot(time, cycle.mean+sign(cycle.v_s-cycle.v_e)*amplitude*cos(2π/dt*(time-cycle.t_s)))
    else
        plot(time, cycle.mean+sign(cycle.v_s-cycle.v_e)*amplitude*cos(π/dt*(time-cycle.t_s)))
    end
end

plot(cycles::Array{Cycle,1}) = for cyc in cycles plot(cyc) end

ext: jl


text:
module TSSOS

using MosekTools
using JuMP
using Graphs
using ChordalGraph
using MultivariatePolynomials
using Ipopt
using LinearAlgebra
using MetaGraphs
using SemialgebraicSets
using COSMO
# using SDPT3

export tssos_first, tssos_higher!, cs_tssos_first, cs_tssos_higher!, local_solution, refine_sol,
nctssos_first, nctssos_higher!, cs_nctssos_first, cs_nctssos_higher!

include("clique_merge.jl")
include("blockpop_uncons.jl")
include("blockpop_cons.jl")
include("nblockmix.jl")
include("complex.jl")
include("local_solution.jl")
include("NCTSSOS/NCTSSOS.jl")
using .NCTSSOS

end


text:
@dict_readable struct ErrorResponseArguments <: Outbound
    error::Union{Missing,Message}
end

@dict_readable struct RunInTerminalResponseArguments <: Outbound
    processId::Union{Missing,Int}
    shellProcessId::Union{Missing,Int}
end

@dict_readable struct InitializeResponseArguments <: Outbound
end

@dict_readable struct ConfigurationDoneResponseArguments <: Outbound
end

@dict_readable struct LaunchResponseArguments <: Outbound
end

@dict_readable struct AttachResponseArguments <: Outbound
end

@dict_readable struct RestartResponseArguments <: Outbound
end

@dict_readable struct DisconnectResponseArguments <: Outbound
end

@dict_readable struct TerminateResponseArguments <: Outbound
end

@dict_readable struct SetBreakpointsResponseArguments <: Outbound
    breakpoints::Vector{Breakpoint}
end

@dict_readable struct SetFunctionBreakpointsResponseArguments <: Outbound
    breakpoints::Vector{Breakpoint}
end

@dict_readable struct SetExceptionBreakpointsResponseArguments <: Outbound
    filters::Vector{String}
    exceptionOptions::Union{Missing,Vector{ExceptionOptions}}
end

@dict_readable struct DataBreakpointInfoResponseArguments <: Outbound
    data::Union{Missing,String}
    description::String
    accessTypes::Union{Missing,Vector{DataBreakpointAccessType}}
    canPersist::Union{Missing,Bool}
end

@dict_readable struct SetDataBreakpointsResponseArguments <: Outbound
    breakpoints::Vector{Breakpoint}
end

@dict_readable struct ContinueResponseArguments <: Outbound
    allThreadsContinued::Union{Missing,Bool}
end

@dict_readable struct NextResponseArguments <: Outbound
end

@dict_readable struct StepInResponseArguments <: Outbound
end

@dict_readable struct StepOutResponseArguments <: Outbound
end

@dict_readable struct StepBackResponseArguments <: Outbound
end

@dict_readable struct ReverseContinueResponseArguments <: Outbound
end

@dict_readable struct RestartFrameResponseResponseArguments <: Outbound
end

@dict_readable struct GotoResponseArguments <: Outbound
end

@dict_readable struct PauseResponseArguments <: Outbound
end

@dict_readable struct StackTraceResponseArguments <: Outbound
    stackFrames::Vector{StackFrame}
    totalFrames::Union{Missing,Int}
end

@dict_readable struct ScopesResponseArguments <: Outbound
    scopes::Vector{Scope}
end

@dict_readable struct VariablesResponseArguments <: Outbound
    variables::Vector{Variable}
end

@dict_readable struct SetVariableResponseArguments <: Outbound
    value::String
    type::Union{Missing,String}
    variablesReference::Union{Missing,Int}
    namedVariables::Union{Missing,Int}
    indexedVariables::Union{Missing,Int}
end

@dict_readable struct SourceResponseArguments <: Outbound
    content::String
    mimeType::Union{Missing,String}
end

@dict_readable struct ThreadsResponseArguments <: Outbound
    threads::Vector{Thread}
end

@dict_readable struct TerminateThreadsResponseArguments <: Outbound
end

@dict_readable struct ModulesResponseArguments <: Outbound
    startModule::Union{Missing,Int}
    moduleCount::Union{Missing,Int}
end

@dict_readable struct LoadedSourcesResponseArguments <: Outbound
    sources::Vector{Source}
end

@dict_readable struct EvaluateResponseArguments <: Outbound
    result::String
    type::Union{Missing,String}
    presentationHint::Union{Missing,VariablePresentationHint}
    variablesReference::Int
    namedVariables::Union{Missing,Int}
    indexedVariables::Union{Missing,Int}
    memoryReference::Union{Missing,String}
end

@dict_readable struct SetExpressionResponseArguments <: Outbound
    value::String
    type::Union{Missing,String}
    presentationHint::Union{Missing,VariablePresentationHint}
    variablesReference::Int
    namedVariables::Union{Missing,Int}
    indexedVariables::Union{Missing,Int}
end

@dict_readable struct StepInTargetsResponseArguments <: Outbound
    targets::Vector{StepInTarget}
end

@dict_readable struct GotoTargetsResponseArguments <: Outbound
    targets::Vector{GotoTarget}
end

@dict_readable struct CompletionsResponseArguments <: Outbound
    targets::Vector{CompletionItem}
end

@dict_readable struct ExceptionInfoResponseArguments <: Outbound
    exceptionId::String
    description::Union{Missing,String}
    breakMode::ExceptionBreakMode
    details::Union{Missing,ExceptionDetails}
end

@dict_readable struct ReadMemoryResponseArguments <: Outbound
    address::String
    unreadableBytes::Union{Missing,Int}
    data::Union{Missing,String}
end

@dict_readable struct DisassembleResponseArguments <: Outbound
    instructions::Union{Missing,DisassembledInstruction}
end


text:
using Gen

include("../gp_structure/shared.jl")

@gen function covariance_prior(cur::Int)
    node_type = @trace(categorical(node_dist), (cur, :type))

    if node_type == CONSTANT
        param = @trace(uniform_continuous(0, 1), (cur, :param))
        node = Constant(param)

    # linear kernel
    elseif node_type == LINEAR
        param = @trace(uniform_continuous(0, 1), (cur, :param))
        node = Linear(param)

    # squared exponential kernel
    elseif node_type == SQUARED_EXP
        length_scale= @trace(uniform_continuous(0, 1), (cur, :length_scale))
        node = SquaredExponential(length_scale)

    # periodic kernel
    elseif node_type == PERIODIC
        scale = @trace(uniform_continuous(0, 1), (cur, :scale))
        period = @trace(uniform_continuous(0, 1), (cur, :period))
        node = Periodic(scale, period)

    # plus combinator
    elseif node_type == PLUS
        child1 = get_child(cur, 1, max_branch)
        child2 = get_child(cur, 2, max_branch)
        left = @trace(covariance_prior(child1))
        right = @trace(covariance_prior(child2))
        node = Plus(left, right)

    # times combinator
    elseif node_type == TIMES
        child1 = get_child(cur, 1, max_branch)
        child2 = get_child(cur, 2, max_branch)
        left = @trace(covariance_prior(child1))
        right = @trace(covariance_prior(child2))
        node = Times(left, right)

    # unknown node type
    else
        error("Unknown node type: $node_type")
    end

    return node
end

@gen function model(xs::Vector{Float64})
    n = length(xs)

    # sample covariance function
    covariance_fn::Node = @trace(covariance_prior(1), :tree)

    # sample diagonal noise
    noise = @trace(gamma(1, 1), :noise) + 0.01

    # compute covariance matrix
    cov_matrix = compute_cov_matrix_vectorized(covariance_fn, noise, xs)

    # sample from multivariate normal
    @trace(mvnormal(zeros(n), cov_matrix), :ys)

    return covariance_fn
end

##############
# noise move #
##############

@gen function noise_proposal(prev_trace)
    @trace(gamma(1, 1), :noise)
end

noise_move(trace) = metropolis_hastings(trace, noise_proposal, ())[1]

########################
# replace subtree move #
########################

@gen function subtree_proposal(prev_trace)
    prev_subtree_node::Node = get_retval(prev_trace)
    (subtree_idx::Int, depth::Int) = @trace(pick_random_node(prev_subtree_node, 1, 0), :choose_subtree_root)
    new_subtree_node::Node = @trace(covariance_prior(subtree_idx), :subtree) # mixed discrete / continuous
    (subtree_idx, depth, new_subtree_node)
end

@involution function walk_previous_subtree(cur::Int)
    @copy_model_to_proposal(:tree => (cur, :type), :subtree => (cur, :type))
    node_type = @read_discrete_from_model(:tree => (cur, :type))
    if node_type == CONSTANT
        @copy_model_to_proposal(:tree => (cur, :param), :subtree => (cur, :param))
    elseif node_type == LINEAR
        @copy_model_to_proposal(:tree => (cur, :param), :subtree => (cur, :param))
    elseif node_type == SQUARED_EXP
        @copy_model_to_proposal(:tree => (cur, :length_scale), :subtree => (cur, :length_scale))
    elseif node_type == PERIODIC
        @copy_model_to_proposal(:tree => (cur, :scale), :subtree => (cur, :scale))
        @copy_model_to_proposal(:tree => (cur, :period), :subtree => (cur, :period))
    elseif (node_type == PLUS) || (node_type == TIMES)
        child1 = get_child(cur, 1, max_branch)
        child2 = get_child(cur, 2, max_branch)
        @invcall(walk_previous_subtree(child1)) # use same namespace
        @invcall(walk_previous_subtree(child2))
    else
        error("Unknown node type: $node_type")
    end
end

@involution function walk_new_subtree(cur::Int)
    @copy_proposal_to_model(:subtree => (cur, :type), :tree => (cur, :type))
    node_type = @read_discrete_from_proposal(:subtree => (cur, :type))
    if node_type == CONSTANT
        @copy_proposal_to_model(:subtree => (cur, :param), :tree => (cur, :param))
    elseif node_type == LINEAR
        @copy_proposal_to_model(:subtree => (cur, :param), :tree => (cur, :param))
    elseif node_type == SQUARED_EXP
        @copy_proposal_to_model(:subtree => (cur, :length_scale), :tree => (cur, :length_scale))
    elseif node_type == PERIODIC
        @copy_proposal_to_model(:subtree => (cur, :scale), :tree => (cur, :scale))
        @copy_proposal_to_model(:subtree => (cur, :period), :tree => (cur, :period))
    elseif (node_type == PLUS) || (node_type == TIMES)
        child1 = get_child(cur, 1, max_branch)
        child2 = get_child(cur, 2, max_branch)
        @invcall(walk_new_subtree(child1)) # use same namespace
        @invcall(walk_new_subtree(child2))
    else
        error("Unknown node type: $node_type")
    end
end

@involution function subtree_involution(model_args::Tuple, proposal_args::Tuple, fwd_ret::Tuple)

    (subtree_idx, subtree_depth, new_subtree_node) = fwd_ret

    # populate backward assignment with choice of root
    @copy_proposal_to_proposal(:choose_subtree_root => :recurse_left, :choose_subtree_root => :recurse_left)
    for depth=0:subtree_depth-1
        @write_discrete_to_proposal(:choose_subtree_root => :done => depth, false)
    end
    if !isa(new_subtree_node, LeafNode)
        @write_discrete_to_proposal(:choose_subtree_root => :done => subtree_depth, true)
    end

    # populate constraints with proposed subtree
    @invcall(walk_previous_subtree(subtree_idx))

    # populate backward assignment with the previous subtree
    @invcall(walk_new_subtree(subtree_idx))
end

replace_subtree_move(trace) = metropolis_hastings(
    trace, subtree_proposal, (), subtree_involution; check=true)[1]

#####################
# inference program #
#####################

function inference(xs::Vector{Float64}, ys::Vector{Float64}, num_iters::Int, callback)

    # observed data
    constraints = choicemap()
    constraints[:ys] = ys

    # generate initial trace consistent with observed data
    (trace, _) = generate(model, (xs,), constraints)

    # do MCMC
    local covariance_fn::Node
    local noise::Float64
    for iter=1:num_iters

        covariance_fn = get_retval(trace)
        noise = trace[:noise]
        #callback(covariance_fn, noise)

        # do MH move on the subtree
        trace = replace_subtree_move(trace)

        # do MH move on the top-level white noise
        trace = noise_move(trace)
    end
    (covariance_fn, noise)
end

function experiment()

    # load and rescale the airline dataset
    (xs, ys) = get_airline_dataset()
    xs_train = xs[1:100]
    ys_train = ys[1:100]
    xs_test = xs[101:end]
    ys_test = ys[101:end]

    # set seed
    Random.seed!(0)

    # print MSE and predictive log likelihood on test data
    callback = (covariance_fn, noise) -> begin
        pred_ll = predictive_ll(covariance_fn, noise, xs_train, ys_train, xs_test, ys_test)
        mse = compute_mse(covariance_fn, noise, xs_train, ys_train, xs_test, ys_test)
        println("mse: $mse, predictive log likelihood: $pred_ll")
    end

    # do inference, time it
    @time (covariance_fn, noise) = inference(xs_train, ys_train, 1000, callback)

    # sample predictions
    pred_ys = predict_ys(covariance_fn, noise, xs_train, ys_train, xs_test)
end

@time experiment()
@time experiment()


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

using Test
using MozillaCACerts_jll

@testset "MozillaCACerts_jll" begin
    @test isfile(MozillaCACerts_jll.cacert)
end

max_stars_repo_name: vanillajonathan/julia

max_issues_repo_name: vanillajonathan/julia

max_forks_repo_name: vanillajonathan/julia


text:
using Test
using JuliaEuler
using Lazy

@testset "helper" begin
    # Write your tests here.
    @testset "Fib" begin
        @test fib(;under=90) |> collect |> Vector{Int} == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
        @test fib(;terms=11) |> collect |> Vector{Int} == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
        @test take(11, fibs) |> collect |> Vector{Int} == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
        @test takewhile(x-> x < 90,fibs) |> collect |> Vector{Int} == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
        @test take(11,fibs) |> collect |> Vector{Int} == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    end
    @testset "Prime" begin
        @test primes(20) == [2,3,5,7,11,13,17,19]
        @test primes(2) == [2]
        @test primes(5) == [2,3,5]
        @test factors(2309125) == [(5,3), (7,2), (13,1), (29,1)]
        @test factors(2) == [(2,1)]
        @test factors(5) == [(5,1)]
        @test primes_of_num(13195) == [5,7,13,29]
        @test primes_of_num(2) == [2]
        @test primes_of_num(5) == [5]

    end

    @testset "ProperDivisors" begin
        @test proper_divisors(220) == [1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110]
        @test proper_divisors(284) == [1, 2, 4, 71, 142]
    end

    @testset "Permutations" begin
        @test Set(permutations([1,2,3], 2)) == Set([(1,2), (1,3), (2,1), (2,3), (3,1),(3,2)])
        @test Set(permutations([1,2,3], 3)) == Set([(1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1)
            ])

    end

    @testset "Product" begin
        @test Set(product([1,2,3], 2)) == Set([(1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3)])
        @test Set(product([1,2,3], 3)) == Set([
            (1,1,1), (1,1,2), (1,1,3), (1,2,1), (1,2,2), (1,2,3), (1,3,1), (1,3,2), (1,3,3),
            (2,1,1), (2,1,2), (2,1,3), (2,2,1), (2,2,2), (2,2,3), (2,3,1), (2,3,2), (2,3,3),
            (3,1,1), (3,1,2), (3,1,3), (3,2,1), (3,2,2), (3,2,3), (3,3,1), (3,3,2), (3,3,3)
            ])
    end

    @testset "combinations" begin
        @test Set(combinations([1,2,3], 2)) == Set([(1,2), (1,3), (2,3)])
        @test Set(combinations([1,2,3], 2;with_replacement=true)) == Set([(1,1), (1,2), (1,3), (2,2), (2,3), (3,3)])
        @test Set(combinations([1,2,3], 3)) == Set([(1,2,3)])
        @test Set(combinations([1,2,3], 3;with_replacement=true)) == Set([
            (1,1,1), (1,1,2), (1,1,3), (1,2,2), (1,2,3), (1,3,3),
            (2,2,2), (2,2,3), (2,3,3),
            (3,3,3)
    ])
    end

    @testset "unique_sorted" begin
        @test JuliaEuler.unique_sorted([1,2,3]) == true
        @test JuliaEuler.unique_sorted([1,2, 2,3]) == false
    end
end

max_stars_repo_name: binnisb/JuliaEuler

max_issues_repo_name: binnisb/JuliaEuler

max_forks_repo_name: binnisb/JuliaEuler


text:
"""
This `OCIM2` module is used to load the OCIM2 matrices and grid for use in AIBECS.

!!! tip
    To load the default OCIM2 matrix and grid, do
    ```
    julia> grd, T = OCIM2.load()
    ```
    But you can also load the other matrices by specifying which version you want, e.g.,
    ```
    julia> grd, T = OCIM2.load(version="OCIM2_KiHIGH_noHe")
    ```
    See *DeVries and Holzer* (2019) for more details

!!! note
    The files, that are downloaded from a public and persistant URL in FigShare,
    were created with the code available at https://github.com/briochemc/OceanCirculations.
"""
module OCIM2

using SparseArrays          # For sparse matrix
using DataDeps              # For storage location of data
using Downloads
using JLD2                  # For saving circulation as JLD2 format
using CodecZlib             # for JLD2 compression JLD2
using Unitful               # for units
using Reexport
@reexport using OceanGrids            # To store the grid

function fallback_download(remotepath, localdir)
    @assert(isdir(localdir))
    filename = basename(remotepath)  # only works for URLs with filename as last part of name
    localpath = joinpath(localdir, filename)
    Downloads.download(remotepath, localpath)
    return localpath
end

# OCIM2 URLs
function url(; version="CTL_He")
    url_start = "https://files.figshare.com/"
    version == "CTL_He"             ? "$(url_start)28336284/OCIM2_CTL_He.jld2" :
    version == "CTL_noHe"           ? "$(url_start)28336299/OCIM2_CTL_noHe.jld2" :
    version == "KiHIGH_He"          ? "$(url_start)28336302/OCIM2_KiHIGH_He.jld2" :
    version == "KiHIGH_noHe"        ? "$(url_start)28336311/OCIM2_KiHIGH_noHe.jld2" :
    version == "KiLOW_He"           ? "$(url_start)28336317/OCIM2_KiLOW_He.jld2" :
    version == "KiLOW_noHe"         ? "$(url_start)28336323/OCIM2_KiLOW_noHe.jld2" :
    version == "KvHIGH_He"          ? "$(url_start)28336326/OCIM2_KvHIGH_He.jld2" :
    version == "KvHIGH_KiHIGH_noHe" ? "$(url_start)28336329/OCIM2_KvHIGH_KiHIGH_noHe.jld2" :
    version == "KvHIGH_KiLOW_He"    ? "$(url_start)28336332/OCIM2_KvHIGH_KiLOW_He.jld2" :
    version == "KvHIGH_KiLOW_noHe"  ? "$(url_start)28336341/OCIM2_KvHIGH_KiLOW_noHe.jld2" :
    version == "KvHIGH_noHe"        ? "$(url_start)28336353/OCIM2_KvHIGH_noHe.jld2" :
    OCIM2versionerror(version)
end

OCIM2versionerror(version) = error("""`$version` is not a valid OCIM2 version name.

                                   Valid versions are `CTL_He`, `CTL_noHe`, `KiHIGH_He`, `KiHIGH_noHe`, `KiLOW_He`, `KiLOW_noHe`, `KvHIGH_He`, `KvHIGH_KiHIGH_noHe`, `KvHIGH_KiLOW_He`, `KvHIGH_KiLOW_noHe`, and `KvHIGH_noHe`.

                                   See *DeVries and Holzer* (2019) for more details on OCIM2 configurations.""")

# OCIM2 Hashes
function sha(; version="CTL_He")
    version == "CTL_He"             ? "610d6abd269950766b75f48ceeee84eb6461782f88331931233bc8da9b96f69d" :
    version == "CTL_noHe"           ? "87c486ae59c2e7702fe2812004e49164ee83dccf00cc05868de0127fc09af3da" :
    version == "KiHIGH_He"          ? "1a2c115696b88df0191126102cea3ba201042e756ec7ed73d1100f0eddefa6c6" :
    version == "KiHIGH_noHe"        ? "cd6e27786f78dd05497fd2c4eb2fb6d94d715940c81378ecefa3c0da35e2977b" :
    version == "KiLOW_He"           ? "a1cf69ece4dbde9e8bef5b5d80fff4c50c0eaa6e55f43025af4740c7a27c9524" :
    version == "KiLOW_noHe"         ? "5b3de3d3aaad2964f3eeaa8c432bbfca2f2a028c8d62e2f71a64660911a1dc78" :
    version == "KvHIGH_He"          ? "7eb5d3f727c0292ebe91983d6ab8da1f471158cf7e86378f1cde3b487a12f7a8" :
    version == "KvHIGH_KiHIGH_noHe" ? "2037df4277f635d729f1ec681ffcb4b59f818cde0a9ece14ad59c8c3a966988a" :
    version == "KvHIGH_KiLOW_He"    ? "65fc71a09eb019dc3ae54fa069184f3449e5530eb57f68e6e4261786cf74ba5e" :
    version == "KvHIGH_KiLOW_noHe"  ? "bbedae950868639e295056c6b42629593104bf92d55e906b6477d0d77d2b6326" :
    version == "KvHIGH_noHe"        ? "b9a46234e1bcb2da14de6d4fb6e51d7a4ae27b1efbf7df26b47b08718cb30748" :
    OCIM2versionerror(version)
end

# Create registry entry for OCIM in JLD2 format
function register_OCIM2(; version="CTL_He")
    register(
        DataDep(
            "AIBECS-OCIM2_$version",
            """
            References:
            - $(citation())
            """,
            url(version=version),
            sha(version=version),
            fetch_method = fallback_download
        )
    )
    return nothing
end

"""
    load

Returns the grid, the transport matrix, and the He fluxes (in that order).

!!! tip
    To load the default OCIM2 matrix and grid, do
    ```
    julia> grd, T = OCIM2.load()
    ```
    But you can also load the other matrices by specifying which version you want, e.g.,
    ```
    julia> grd, T = OCIM2.load(version="KiHIGH_noHe")
    ```

    Add the `HeFluxes=true` keyword argument if you want the OCIM-produced He fields
    with ³He and ⁴He as 3rd and 4th arguments.
    (3rd and 4th output are returned as `nothing` for "noHe" versions).

    See *DeVries and Holzer* (2019) for more details.
"""
function load(; version="CTL_He", HeFluxes=false)
    register_OCIM2(version=version)
    jld2_file = @datadep_str string("AIBECS-OCIM2_$version/", "OCIM2_$version.jld2")
    @info """You are about to use the OCIM2_$version model.
          If you use it for research, please cite:

          - $(citation())

          You can find the corresponding BibTeX entries in the CITATION.bib file
          at the root of the AIBECS.jl package repository.
          (Look for the "DeVries_Holzer_2019" key.)
          """
    if HeFluxes
        jldopen(jld2_file) do file
            file["grid"], ustrip.(file["T"]), file["He3Flux"], file["He4Flux"]
        end
    else
        jldopen(jld2_file) do file
            file["grid"], ustrip.(file["T"])
        end
    end
end

citation() = "DeVries, T., & Holzer, M. (2019). Radiocarbon and helium isotope constraints on deep ocean ventilation and mantle‐³He sources. Journal of Geophysical Research: Oceans, 124, 3036–3057. https://doi.org/10.1029/2018JC014716"

versions() = ["CTL_He", "CTL_noHe", "KiHIGH_He", "KiHIGH_noHe", "KiLOW_He", "KiLOW_noHe", "KvHIGH_He", "KvHIGH_KiHIGH_noHe", "KvHIGH_KiLOW_He", "KvHIGH_KiLOW_noHe", "KvHIGH_noHe"]

end # end module

export OCIM2


text:
module LLG
# using DifferentialEquations
using StaticArrays:reshape
using Base:Float64
using ModelingToolkit
using Random, Distributions
import LinearAlgebra:cross,similar,det,⋅
import Symbolics:build_function
using GLMakie

## diffeq stuff:

import Symbolics:istree,operation,arguments

export LatticeDescription, generateLatticeShape, Atom, positionsLattice,mappingAtoms, coupleNN, coupleInternal, isless

export plot_atoms,plot_atoms!, plot_connections, plot_connections!, absPositionsParticle
export dmi_interaction_honeycomb, nn_coupling_honeycomb, nnn_coupling_honeycomb
export initBoltzmann

# export custom alg
# export LLGProblem, SIB

# export exchangeCoupling, DMICoupling, setupEOM

include("lattice.jl")
# include("diffeq.jl")
include("plotting.jl")
include("graphene.jl")
include("diffeq_toolkit.jl")
# include("custom_cache.jl")
# include("custom_alg.jl")
# Write your package code here.
end

max_stars_repo_name: pgunnink/LLGTools

max_issues_repo_name: pgunnink/LLGTools

max_forks_repo_name: pgunnink/LLGTools


text:
function test_main()

  srand(1234)

  A = rand(3,3)
  A = 0.5*(A + A')
  invA = inv(A)

  println("\nMatrix A:")
  println(A)
  println("\nInverse of A")
  println(invA)

  B = rand(3,3)
  B = 0.5*(B + B')
  invB = inv(B)

  println("\nMatrix B")
  println(B)
  println("\nInverse of B")
  println(invB)

  AB = A + B
  invAB = inv(A + B)

  println("\nMatrix A + B")
  println(AB)
  println("\nInverse of A + B")
  println(invAB)

  test_inv_Miller( A, B )

  println(rank(B))

end

function test_inv_Miller( G, H )

  g = trace( H*inv(G) )
  println("g = ", g)

  invGH_mill = inv(G) - 1/(1+g) * inv(G)*H*inv(G)

  println("\nTest Miller")
  println(invGH_mill - inv(G+H))
end

test_main()


text:
export  center_std_diag!, center_std_off!, center_std!

# Set ξ and σ for the diagonal entry, i.e. the last element of C
function center_std_diag!(C::RadialMapComponent, X::AbstractMatrix{Float64}, γ::Float64)
    @get C (Nx, p)
    @assert (p>0 && γ >0.0) || (p==0 && γ>=0.0) "Error scaling factor γ"

    # As long as he have p>1, we have in fact p+2 centers
    if p>0
    ξ′ = view(C.ξ[end],:)
    σ′ = view(C.σ[end],:)
    tmp = view(X,Nx,:)
    quantile!(ξ′, tmp, collect(1:p+2)./(p+3); sorted=true, alpha = 0.5, beta = 0.5)

    σ′[1] = ξ′[2]-ξ′[1]
    σ′[end] = ξ′[end]-ξ′[end-1]
    @inbounds for j=2:p+1
        σ′[j] = 0.5*(ξ′[j+1]-ξ′[j-1])
    end
    rmul!(σ′, γ)

    end
end

# Set ξ and σ for the off-diagonal entries, i.e. i=1:Nx-1 entries of C
function center_std_off!(C::RadialMapComponent, X::AbstractMatrix{Float64}, γ::Float64)
    @get C (Nx, p)
    @assert (p>0 && γ >0.0) || (p==0 && γ>=0.0) "Error scaling factor γ"

    if p==0

    elseif p==1 # only one rbf
        qq = zeros(3)
        p_range = [0.25;0.5;0.75]
        @inbounds for i=1:Nx-1
            ξ = view(C.ξ[i],:)
            σ = view(C.σ[i],:)
            tmp = view(X, i, :)
            quantile!(qq, tmp, p_range; sorted=true, alpha = 0.5, beta = 0.5)
            ξ .= qq[2]
            σ .= (qq[3]-qq[1])*0.5
            rmul!(σ, γ)
        end
    elseif p==2
        p_range = collect(1.0:p)./(p+1.0)
        @inbounds for i=1:Nx-1
            ξ = view(C.ξ[i],:)
            σ = view(C.σ[i],:)
            tmp = view(X,i,:)
            quantile!(ξ, tmp, p_range; sorted=true, alpha = 0.5, beta = 0.5)
            σ[1:2] .= (ξ[2]-ξ[1])*ones(2)
            rmul!(σ, γ)
        end

    else
        p_range = collect(1.0:p)./(p+1.0)
        @inbounds for i=1:Nx-1
            ξ = view(C.ξ[i],:)
            σ = view(C.σ[i],:)
            tmp = view(X,i,:)
            quantile!(ξ, tmp, p_range; sorted=true, alpha = 0.5, beta = 0.5)

            σ[1] = (ξ[2]-ξ[1])
            σ[end] = (ξ[end]-ξ[end-1])
            for j=2:p-1
                σ[j] = 0.5*(ξ[j+1]-ξ[j-1])
            end
            rmul!(σ, γ)
        end
    end
end

# Assume an unsorted array
function center_std!(T::RadialMap, X::AbstractMatrix{Float64};start::Int64=1)
    @get T (Nx, p, γ)
    NxX, Ne = size(X)
    @assert NxX == Nx "Wrong dimension of the ensemble matrix"
    if p>0
    Xsort = deepcopy(X)
    sort!(Xsort; dims = 2)
    if Nx==1
        center_std_diag!(T.C[1], Xsort, γ)
    else
        @inbounds for i=start:Nx
            center_std_diag!(T.C[i], Xsort, γ)
            center_std_off!(T.C[i], Xsort, γ)
        end
    end
    end
end

function center_std!(C::RadialMapComponent, X::AbstractMatrix{Float64}; γ::Float64 = 2.0)
    @get C (Nx, p)
    NxX, Ne = size(X)
    @assert NxX == Nx "Wrong dimension of the ensemble matrix"
    if p>0
        Xsort = deepcopy(X)
        sort!(Xsort; dims = 2)
        if Nx==1
            center_std_diag!(C, Xsort, γ)
        else
            center_std_diag!(C, Xsort, γ)
            center_std_off!(C, Xsort, γ)
        end
    end
end

## Define center_std for Sparse maps

# Set ξ and σ for the diagonal entry, i.e. the last element of SparseC
function center_std_diag!(C::SparseRadialMapComponent, X::AbstractMatrix{Float64}, γ::Float64)
    @get C (Nx, p)
    @assert (p[Nx]>0 && γ >0.0) || (p[Nx]==0 && γ>=0.0) "Error scaling factor γ"

    # As long as he have p>1, we have in fact p+2 centers
    if p[Nx]>0
    ξ′ = view(C.ξ[end],:)
    σ′ = view(C.σ[end],:)
    tmp = view(X,Nx,:)
    quantile!(ξ′, tmp, collect(1:p[Nx]+2)./(p[Nx]+3); sorted=true, alpha = 0.5, beta = 0.5)

    σ′[1] = ξ′[2]-ξ′[1]
    σ′[end] = ξ′[end]-ξ′[end-1]
    @inbounds for j=2:p[Nx]+1
        σ′[j] = 0.5*(ξ′[j+1]-ξ′[j-1])
    end
    rmul!(σ′, γ)

    end
end

# Set ξ and σ for the off-diagonal entries, i.e. i=1:Nx-1 entries of C
function center_std_off!(C::SparseRadialMapComponent, X::AbstractMatrix{Float64}, γ::Float64, dims::Union{Int64, Array{Int64,1}, UnitRange{Int64}})
    @get C (Nx, p)
    @assert γ>=0.0 "Error scaling factor γ"
    # "The maximum of dims must be smaller than Nx-1"
    @inbounds for i in dims
        pidx = p[i]

        ξ = view(C.ξ[i],:)
        σ = view(C.σ[i],:)
        tmp = view(X, i, :)

        # Testcase according to pidx

        if pidx<1
            #There is no rbf
        elseif pidx ==1 #only one rbf
            qq = zeros(3)
            p_range = [0.25;0.5;0.75]
            quantile!(qq, tmp, p_range; sorted=true, alpha = 0.5, beta = 0.5)
            ξ .= qq[2]
            σ .= (qq[3]-qq[1])*0.5
            rmul!(σ, γ)

        elseif pidx ==2
            p_range = collect(1.0:pidx)./(pidx+1.0)
            quantile!(ξ, tmp, p_range; sorted=true, alpha = 0.5, beta = 0.5)
            σ[1:2] .= (ξ[2]-ξ[1])*ones(2)
            rmul!(σ, γ)

        else
            p_range = collect(1.0:pidx)./(pidx+1.0)
            quantile!(ξ, tmp, p_range; sorted=true, alpha = 0.5, beta = 0.5)

            σ[1] = (ξ[2]-ξ[1])
            σ[end] = (ξ[end]-ξ[end-1])
            @inbounds for j=2:pidx-1
                σ[j] = 0.5*(ξ[j+1]-ξ[j-1])
            end
            rmul!(σ, γ)
        end
    end
end

# By default compute the centers and the widths for all the dimensions
center_std_off!(C::SparseRadialMapComponent, X::AbstractMatrix{Float64}, γ::Float64) =
               center_std_off!(C, X, γ, 1:C.Nx-1)

# Assume an unsorted array
function center_std!(T::SparseRadialMap, X::AbstractMatrix{Float64}; start::Int64=1)
    @get T (Nx, p, γ)
    NxX, Ne = size(X)
    @assert NxX == Nx "Wrong dimension of the ensemble matrix"
    Xsort = sort(X; dims = 2)
    if Nx==1 && !allequal(p[Nx], -1)
        center_std_diag!(T.C[1], Xsort, γ)
    else
        @inbounds for i=start:Nx
            if !allequal(p[i], -1)
            center_std_diag!(T.C[i], Xsort, γ)
            center_std_off!(T.C[i], Xsort, γ)
            end
        end
    end
end

# Assume an unsorted array
function center_std!(C::SparseRadialMapComponent, X::AbstractMatrix{Float64}; γ::Float64 = 2.0)
    @get C (Nx, p)
    NxX, Ne = size(X)
    @assert NxX == Nx "Wrong dimension of the ensemble matrix"
    Xsort = sort(X; dims = 2)
    if Nx==1 && !allequal(p, -1)
        center_std_diag!(C, Xsort, γ)
    else
        if !allequal(p, -1)
            center_std_diag!(C, Xsort, γ)
            center_std_off!(C, Xsort, γ)
        end
    end
end

# customquantile equivalent to quantile from Statistics with alpha = beta = 0.5
# function customquantile(v::Array{Float64,1},p::Int64)
#     # From Ricardo Baptista and Youssef Marzouk
#     n= length(v)
#
#     r = zeros(p)
#     r .= collect(1:p)/(p+1)
#
#     rmul!(r,n)
#     k = zeros(Int64, size(r,1))
#     kp1 = zeros(Int64, size(r,1))
#     for (i, ri) in enumerate(r)
#         k[i] = floor(ri + 0.5)
#         kp1[i] = k[i] +1
#     end
#     # k = convert(Array{Int64,1},floor.(r .+ 0.5))
#     # kp1 = k .+1
#     r .-= k
#
#     # Find indices that are out of the range 1 to n and cap them
#     k[k .< 1] .=1
#     kp1 .= min.(kp1, n)
#
#     # Use simple linear interpolation for the valid percentages
#     return @. (0.5 + r)*v[kp1] + (0.5 - r)*v[k]
# end
#
# function customquantile(v::Array{Float64,1},p::Array{Float64,1})
#     # From Ricardo Baptista and Youssef Marzouk
#     n= length(v)
#     r = p
#     rmul!(r,n)
#     k = zeros(Int64, size(r,1))
#     kp1 = zeros(Int64, size(r,1))
#     for (i, ri) in enumerate(r)
#         k[i] = floor(ri + 0.5)
#         kp1[i] = k[i] + 1
#     end
#     # k = convert(Array{Int64,1},floor.(r .+ 0.5))
#
#     # k = convert(Array{Int64,1},floor.(r .+ 0.5))
#     # kp1 = k .+1
#     r .-= k
#
#     # Find indices that are out of the range 1 to n and cap them
#     k[k .< 1] .=1
#     kp1 .= min.(kp1, n)
#
#     # Use simple linear interpolation for the valid percentages
#     return @. (0.5 + r)*v[kp1] + (0.5 - r)*v[k]
# end


text:
using ArgParse
using HTTP

function parse_arguments()
    s = ArgParseSettings()

    @add_arg_table s begin
        "--year", "-y"
        help = "Year of AoC contest"
        arg_type = Int
        "--day", "-d"
        help = "Day of AoC problem"
        arg_type = Int
    end
    parse_args(s)
end

function load_aoc_file(year, day)
    filename = "$(year)/$(day).in"
    if !isfile(filename)
        cookie = Dict("session" => read("cookie", String))
        content = HTTP.request(:GET, "https://adventofcode.com/$(year)/day/$(day)/input", cookies = cookie).body |> String
        open(filename, "w") do io
            print(io, content)
        end
    end
end

function main()
    args = parse_arguments()

    load_aoc_file(args["year"], args["day"])

    prgrm_file = """$(args["year"])/$(args["day"]).jl"""
    input_file = """$(args["year"])/$(args["day"]).in"""

    cmd = `julia --project -t 6 $prgrm_file`
    run(pipeline(cmd, stdin = input_file))
end

main()

max_stars_repo_name: pLOPeGG/JuliaOfCode

max_issues_repo_name: pLOPeGG/JuliaOfCode

max_forks_repo_name: pLOPeGG/JuliaOfCode


text:
function create(c::Client, ::Type{Integration}, g::AbstractGuild; kwargs...)
    return create_guild_integration(c, g.id; kwargs...)
end

retrieve(c::Client, ::Type{Integration}, g::AbstractGuild) = get_guild_integrations(c, g.id)

function update(c::Client, i::Integration, g::AbstractGuild; kwargs...)
    return modify_guild_integration(c, g.id, i.id; kwargs...)
end

function delete(c::Client, i::Integration, g::AbstractGuild)
    return delete_guild_integration(c, g.id, i.id)
end


text:
abstract type AbstractSpectrumDensity{T, N} end

@inline DensityInterface.DensityKind(::AbstractSpectrumDensity) = IsDensity()

Base.eltype(d::AbstractSpectrumDensity{T}) where {T} = T

abstract type UvSpectrumDensity{T} <: AbstractSpectrumDensity{T, 1} end

function evaluate end


text:
using DFUtil
using Test
using DataFrames
using Dates

df = DataFrame([[1,2],[3,4],[4,5]],["a", "b", "c"])
ddf = DataFrame([[1,2,2],[3,4,2],[4,5,3]], ["a", "b", "c"])
dt = DataFrame([[ Date(2000, 1, 1), Date(2000, 2, 1), Date(2000, 3, 1),Date(2001, 1, 1), Date(2001, 2, 1), Date(2001, 3, 1),Date(2001, 12, 1), Date(2001, 7, 1), Date(2001, 12, 31),Date(2002, 1, 1),Date(2002, 2, 1),Date(2002, 3, 1)],[1,1,1,1,1,1,1,1,1,1,1,1],[2,2,2,2,2,2,2,2,2,2,2,2]], ["d", "b", "c"])

dfab = DataFrame([[1,2,3], [4,5,6]], ["a", "b"])
dfac = DataFrame([[1,2,3], [4,5,6]], ["a", "c"])
dfad = DataFrame([[1,2,3], [4,5,6]], ["a", "d"])
dfabcd = DataFrame([[1,2,3], [4,5,6], [4,5,6], [4,5,6]], ["a", "b", "c", "d"])

stdtest = IOBuffer()
eq(_, t) = String(take!(stdtest)) == t

@testset "DFUtil.jl" begin
    @test eq(to_json(stdtest, df, "a"), "{ \"1\" : {\"a\" : \"1\", \"b\" : \"3\", \"c\" : \"4\" } , \"2\" : {\"a\" : \"2\", \"b\" : \"4\", \"c\" : \"5\" }  }")
    @test eq(to_json(stdtest, df, ["a", "b"]), "{ \"1\" : {\"3\" : {\"a\" : \"1\", \"b\" : \"3\", \"c\" : \"4\" }  }, \"2\" : {\"4\" : {\"a\" : \"2\", \"b\" : \"4\", \"c\" : \"5\" }  } }")
    @test eq(to_json_var(stdtest, df, ["a", "b"],"test"), "var test = JSON.parse('{ \"1\" : {\"3\" : {\"a\" : \"1\", \"b\" : \"3\", \"c\" : \"4\" }  }, \"2\" : {\"4\" : {\"a\" : \"2\", \"b\" : \"4\", \"c\" : \"5\" }  } }');\n")
	@test eq(to_js_value(stdtest, df, ["a", "b"],"var test"), "var test = JSON.parse('{ \"1\" : {\"3\" : {\"a\" : \"1\", \"b\" : \"3\", \"c\" : \"4\" }  }, \"2\" : {\"4\" : {\"a\" : \"2\", \"b\" : \"4\", \"c\" : \"5\" }  } }');\n")
	@test eq(to_js_value(stdtest, df, ["a", "b"],"export const test"), "export const test = JSON.parse('{ \"1\" : {\"3\" : {\"a\" : \"1\", \"b\" : \"3\", \"c\" : \"4\" }  }, \"2\" : {\"4\" : {\"a\" : \"2\", \"b\" : \"4\", \"c\" : \"5\" }  } }');\n")
    @test sum_columns(df) == DataFrame([[3],[7],[9]],["as","bs","cs"])
    @test sum_columns(ddf, group_by=["a"]) == DataFrame([[1,2], [3,6], [4,8]], ["a", "bs", "cs"])
    @test sum_columns(ddf, group_by=["a"], replace_with="X") == DataFrame([[1,2], [3,6], [4,8]], ["a", "bX", "cX"])
    @test group_data_into_periods(dt, :d, "Week") == DataFrame([["1999-52", "2000-05", "2000-09", "2001-01", "2001-05", "2001-09", "2001-48", "2001-26", "2002-01", "2002-05", "2002-09"],[1,1,1,2,1,1,1,1,1,1,1],[2,2,2,4,2,2,2,2,2,2,2]], ["ds", "bs", "cs"])
    @test group_data_into_periods(dt, :d, "Month") == DataFrame([["2000-01", "2000-02", "2000-03", "2001-01", "2001-02", "2001-03", "2001-12", "2001-07", "2002-01", "2002-02", "2002-03"],[1,1,1,1,1,1,2,1,1,1,1],[2,2,2,2,2,2,4,2,2,2,2]], ["ds", "bs", "cs"])
    @test group_data_into_periods(dt, "d", :Quarter) == DataFrame([["2000Q1","2001Q1","2001Q4","2001Q3","2002Q1"], [3,3,2,1,3], [6,6,4,2,6]], ["ds", "bs", "cs"])
    @test group_data_into_periods(dt, :d, :Year) == DataFrame([[2000,2001,2002], [3,6,3], [6,12,6]], ["ds", "bs", "cs"])
    @test match_row(dt, :d, Date(2001,3,1)) == DataFrame([[Date(2001,3,1)], [1], [2]], ["d", "b", "c"])
    @test include_or_exclude(df, includes="a") == DataFrame([[1,2]], ["a"])
    @test include_or_exclude(df, includes=[:a]) == DataFrame([[1,2]], ["a"])
    @test include_or_exclude(df, excludes=[:b, :c]) == DataFrame([[1,2]], ["a"])
    @test include_or_exclude(df, excludes=["a", "b", "c"], includes=:a) == DataFrame([[1,2]], ["a"])
	@test de_miss_rows(DataFrame([[1,missing,3], [10,20,30]], ["a", "b"])) == DataFrame([[1,3], [10,30]], ["a", "b"])
    @test to_csv_text(df) == "a,b,c\n1,3,4\n2,4,5\n"
    @test from_csv_text("a,b,c\n1,3,4\n2,4,5\n") == df
    @test from_csv_text(to_csv_text(dt)) == dt
	@test leftjoiner(dfab, :a, dfac, dfad) == dfabcd
	@test concat!(DataFrame(), df, DataFrame([[2],[2],[3]], ["a", "b", "c"])) == ddf
    @test tryRename(dfab, [:b=>:c]) == dfac
    @test tryRename(dfab, "b"=>"c") == dfac
    @test tryRename(df, [:a=>:q, :b=>:z, :Not=>:Present]) == DataFrame([[1,2],[3,4],[4,5]],["q", "z", "c"])
    @test dropByName!(dfab, n->n=="a") == DataFrame([[4,5,6]], ["b"])
    @test to_csv_text(colsToGrid(DataFrame([["a", "b", "c"], ["d", "e", "f"], [1,2,3], [4,5,6]], ["c1", "c2", "c3", "v"]), :c1, :c2, :v, "see")) == "see,d,e,f\na,4,,\nb,,5,\nc,,,6\n"
end


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

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

Associates a specified AWS account with Amazon Macie Classic as a member account.

# Arguments
- `member_account_id`: The ID of the AWS account that you want to associate with Amazon
  Macie Classic as a member account.

"""
associate_member_account(memberAccountId; aws_config::AbstractAWSConfig=global_aws_config()) = macie("AssociateMemberAccount", Dict{String, Any}("memberAccountId"=>memberAccountId); aws_config=aws_config)
associate_member_account(memberAccountId, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = macie("AssociateMemberAccount", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("memberAccountId"=>memberAccountId), params)); aws_config=aws_config)

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

Associates specified S3 resources with Amazon Macie Classic for monitoring and data
classification. If memberAccountId isn't specified, the action associates specified S3
resources with Macie Classic for the current Macie Classic administrator account. If
memberAccountId is specified, the action associates specified S3 resources with Macie
Classic for the specified member account.

# Arguments
- `s3_resources`: The S3 resources that you want to associate with Amazon Macie Classic for
  monitoring and data classification.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"memberAccountId"`: The ID of the Amazon Macie Classic member account whose resources
  you want to associate with Macie Classic.
"""
associate_s3_resources(s3Resources; aws_config::AbstractAWSConfig=global_aws_config()) = macie("AssociateS3Resources", Dict{String, Any}("s3Resources"=>s3Resources); aws_config=aws_config)
associate_s3_resources(s3Resources, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = macie("AssociateS3Resources", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("s3Resources"=>s3Resources), params)); aws_config=aws_config)

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

Removes the specified member account from Amazon Macie Classic.

# Arguments
- `member_account_id`: The ID of the member account that you want to remove from Amazon
  Macie Classic.

"""
disassociate_member_account(memberAccountId; aws_config::AbstractAWSConfig=global_aws_config()) = macie("DisassociateMemberAccount", Dict{String, Any}("memberAccountId"=>memberAccountId); aws_config=aws_config)
disassociate_member_account(memberAccountId, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = macie("DisassociateMemberAccount", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("memberAccountId"=>memberAccountId), params)); aws_config=aws_config)

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

Removes specified S3 resources from being monitored by Amazon Macie Classic. If
memberAccountId isn't specified, the action removes specified S3 resources from Macie
Classic for the current Macie Classic administrator account. If memberAccountId is
specified, the action removes specified S3 resources from Macie Classic for the specified
member account.

# Arguments
- `associated_s3_resources`: The S3 resources (buckets or prefixes) that you want to remove
  from being monitored and classified by Amazon Macie Classic.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"memberAccountId"`: The ID of the Amazon Macie Classic member account whose resources
  you want to remove from being monitored by Macie Classic.
"""
disassociate_s3_resources(associatedS3Resources; aws_config::AbstractAWSConfig=global_aws_config()) = macie("DisassociateS3Resources", Dict{String, Any}("associatedS3Resources"=>associatedS3Resources); aws_config=aws_config)
disassociate_s3_resources(associatedS3Resources, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = macie("DisassociateS3Resources", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("associatedS3Resources"=>associatedS3Resources), params)); aws_config=aws_config)

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

Lists all Amazon Macie Classic member accounts for the current Macie Classic administrator
account.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: Use this parameter to indicate the maximum number of items that you want
  in the response. The default value is 250.
- `"nextToken"`: Use this parameter when paginating results. Set the value of this
  parameter to null on your first call to the ListMemberAccounts action. Subsequent calls to
  the action fill nextToken in the request with the value of nextToken from the previous
  response to continue listing data.
"""
list_member_accounts(; aws_config::AbstractAWSConfig=global_aws_config()) = macie("ListMemberAccounts"; aws_config=aws_config)
list_member_accounts(params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = macie("ListMemberAccounts", params; aws_config=aws_config)

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

Lists all the S3 resources associated with Amazon Macie Classic. If memberAccountId isn't
specified, the action lists the S3 resources associated with Macie Classic for the current
Macie Classic administrator account. If memberAccountId is specified, the action lists the
S3 resources associated with Macie Classic for the specified member account.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: Use this parameter to indicate the maximum number of items that you want
  in the response. The default value is 250.
- `"memberAccountId"`: The Amazon Macie Classic member account ID whose associated S3
  resources you want to list.
- `"nextToken"`: Use this parameter when paginating results. Set its value to null on your
  first call to the ListS3Resources action. Subsequent calls to the action fill nextToken in
  the request with the value of nextToken from the previous response to continue listing
  data.
"""
list_s3_resources(; aws_config::AbstractAWSConfig=global_aws_config()) = macie("ListS3Resources"; aws_config=aws_config)
list_s3_resources(params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = macie("ListS3Resources", params; aws_config=aws_config)

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

Updates the classification types for the specified S3 resources. If memberAccountId isn't
specified, the action updates the classification types of the S3 resources associated with
Amazon Macie Classic for the current Macie Classic administrator account. If
memberAccountId is specified, the action updates the classification types of the S3
resources associated with Macie Classic for the specified member account.

# Arguments
- `s3_resources_update`: The S3 resources whose classification types you want to update.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"memberAccountId"`: The AWS ID of the Amazon Macie Classic member account whose S3
  resources' classification types you want to update.
"""
update_s3_resources(s3ResourcesUpdate; aws_config::AbstractAWSConfig=global_aws_config()) = macie("UpdateS3Resources", Dict{String, Any}("s3ResourcesUpdate"=>s3ResourcesUpdate); aws_config=aws_config)
update_s3_resources(s3ResourcesUpdate, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = macie("UpdateS3Resources", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("s3ResourcesUpdate"=>s3ResourcesUpdate), params)); aws_config=aws_config)


text:
export GeneralizedConjugateResidualMethod

"""
    GeneralizedConjugateResidualMethod(; M=30, K=10)

# Conjugate Residual
This object represents an iterative Krylov method for solving a linear system.
The constructor parameter `M` is the number of steps after which the algorithm
is restarted (if it has not converged), `K` is the maximal number of restarts.
The amount of memory required by the solver state is roughly `(2K + 2) * N`, where `N`
is the the number of unknowns. This uses the restarted Generalized Conjugate
Residual method of Eisenstat (1983).

## References
    @article{eisenstat1983variational,
        title={Variational iterative methods for nonsymmetric systems of linear equations},
        author={Eisenstat, Stanley C and Elman, Howard C and Schultz, Martin H},
        journal={SIAM Journal on Numerical Analysis},
        volume={20},
        number={2},
        pages={345--357},
        year={1983},
        publisher={SIAM}
    }
"""
struct GeneralizedConjugateResidualMethod <: AbstractKrylovMethod
    "Maximum number of Krylov iterations"
    M::Int
    "Maximum number of restarts"
    K::Int
    function GeneralizedConjugateResidualMethod(; M=30, K=10)
        return new(M, K)
    end
end

mutable struct GCRCache{M, T, AT} <: AbstractLinearSolverCache
    residual::AT
    L_residual::AT
    p::NTuple{M, AT}
    L_p::NTuple{M, AT}
    alpha::Vector{T}
    normsq::Vector{T}
end

function cache(
    krylov_alg::GeneralizedConjugateResidualMethod,
    Q::AT,
) where {AT}
    T = eltype(Q)
    M = krylov_alg.M
    residual = similar(Q)
    L_residual = similar(Q)
    p = ntuple(i -> similar(Q), M)
    L_p = ntuple(i -> similar(Q), M)
    alpha = zeros(M)
    normsq = zeros(M)

    return GCRCache{M, T, AT}(
        residual,
        L_residual,
        p,
        L_p,
        alpha,
        normsq,
    )
end

function LSinitialize!(
    ::GeneralizedConjugateResidualMethod,
    solver::LinearSolver,
    Q,
    Qrhs,
    args...,
)
    linearoperator! = solver.linop!
    pc = solver.pc

    cache = solver.cache
    residual = cache.residual
    p = cache.p
    L_p = cache.L_p

    @assert size(Q) == size(residual)

    rtol = solver.rtol
    atol = solver.atol

    threshold = rtol * norm(Qrhs)
    linearoperator!(residual, Q, args...)
    @. residual -= Qrhs

    converged = false
    residual_norm = norm(residual)
    if residual_norm < threshold
        converged = true
        return converged, threshold
    end

    @. p[1] = residual
    linearoperator!(L_p[1], p[1], args...)

    threshold = max(atol, threshold)

    converged, threshold
end

function LSsolve!(
    krylov_alg::GeneralizedConjugateResidualMethod,
    solver::LinearSolver,
    threshold,
    Q,
    Qrhs,
    args...,
)
    converged = false
    iter = 0
    total_iters = 0
    residual_norm = typemax(eltype(Q))

    while !converged && iter < krylov_alg.K
        converged, cycle_iters, residual_norm = gcr_cycle!(solver, threshold, Q, Qrhs, args...)

        iter += 1
        total_iters += cycle_iters

        # If we blow up, we want to know about it
        if !isfinite(residual_norm)
            error("norm of residual is not finite after $total_iters GCR iterations.")
        end
    end

    (converged, total_iters, residual_norm)
end

function gcr_cycle!(
    solver::LinearSolver,
    threshold,
    Q,
    Qrhs,
    args...,
)
    linearoperator! = solver.linop!
    pc = solver.pc
    cache = solver.cache
    residual = cache.residual
    p = cache.p
    L_residual = cache.L_residual
    L_p = cache.L_p
    normsq = cache.normsq
    alpha = cache.alpha
    rtol = solver.rtol
    atol = solver.atol

    converged = false
    residual_norm = typemax(eltype(Q))
    k = 1
    M = solver.krylov_alg.M
    for outer k in 1:M

        normsq[k] = norm(L_p[k])^2
        beta = -dot(residual, L_p[k]) / normsq[k]

        @. Q += beta * p[k]
        @. residual += beta * L_p[k]

        residual_norm = norm(residual)

        if residual_norm < threshold
            converged = true
            break
        end

        linearoperator!(L_residual, residual, args...)

        for l in 1:k
            alpha[l] = -dot(L_residual, L_p[l]) / normsq[l]
        end

        if k < M
            rv_nextp = realview(p[k + 1])
            rv_L_nextp = realview(L_p[k + 1])
        else # restart
            rv_nextp = realview(p[1])
            rv_L_nextp = realview(L_p[1])
        end

        rv_residual = realview(residual)
        rv_p = realview.(p)
        rv_L_p = realview.(L_p)
        rv_L_residual = realview(L_residual)

        groupsize = 256
        T = eltype(alpha)

        event = Event(array_device(Q))
        event = linearcombination!(array_device(Q), groupsize)(
            rv_nextp,
            (one(T), alpha[1:k]...),
            (rv_residual, rv_p[1:k]...),
            false;
            ndrange = length(rv_nextp),
            dependencies = (event,),
        )

        event = linearcombination!(array_device(Q), groupsize)(
            rv_L_nextp,
            (one(T), alpha[1:k]...),
            (rv_L_residual, rv_L_p[1:k]...),
            false;
            ndrange = length(rv_nextp),
            dependencies = (event,),
        )
        wait(array_device(Q), event)
    end

    (converged, k, residual_norm)
end

max_stars_repo_name: CliMA/SystemSolvers

max_issues_repo_name: CliMA/SystemSolvers


text:
## Julia program for pool-adjacent-violators algorithm
## author: weiya <szcfweiya@gmail.com>
## date: 2018-08-19

function pooladv(f::Array, w::Array)
   n = size(f, 1)
   lag = diff(f)
   if all(i -> i >= 0, lag)
        return(f)
   end
   while true
        for i = 1:(n-1)
            global idx
            idx = i+1
            lag[i] < 0 && break
        end
        newf = (w[idx]*f[idx] + w[idx-1]*f[idx-1])/(w[idx]+w[idx-1])
        f[idx] = newf
        f[idx-1] = newf
        lag = diff(f)
        if all(i -> i>=0, lag)
            return(f)
        end
   end
end

## example
g = pooladv([23, 27, 25, 28], ones(4))
for i in eachindex(g)
    println(g[i])
end

max_stars_repo_name: szcf-weiya/MCMC

max_issues_repo_name: szcf-weiya/MCMC

max_forks_repo_name: szcf-weiya/MCMC


text:
using BinningAnalysis
using Test

@testset "Checking converging data (Real)" begin
    BA = LogBinner()

    # block of maximally correlated values:
    N_corr = 16

    # number of blocks
    N_blocks = 131_072 # 2^17

    for _ in 1:N_blocks
        x = rand()
        for __ in 1:N_corr
            push!(BA, x)
        end
    end

    # BA must diverge until 16 values are binned
    for lvl in 1:4
        @test !has_converged(BA, lvl)
    end

    # Afterwards it must converge (for a while)
    for lvl in 5:8
        @test has_converged(BA, lvl)
    end
    # Later values may fluctuate due to small samples / small threshold in
    # has_converged

    # means should be consistent
    means = BinningAnalysis.all_means(BA)
    for x in means
        @test x ≈ means[1]
    end
end

@testset "Check variance for Complex values" begin
    # NOTE
    # Due to the different (mathematically equivalent) versions of the variance
    # calculated here, the values are onyl approximately the same. (Float error)
    xs = rand(ComplexF64, 1_000_000)
    BA = LogBinner(ComplexF64)

    # Test small set (off by one errors are large here)
    for x in xs[1:10]; push!(BA, x) end
    @test var(BA, 0) ≈ var(xs[1:10])
    @test varN(BA, 0) ≈ var(xs[1:10])/10

    # Test full set
    for x in xs[11:end]; push!(BA, x) end
    @test var(BA, 0) ≈ var(xs)
    @test varN(BA, 0) ≈ var(xs)/1_000_000
end

@testset "Checking converging data (Complex)" begin
    BA = LogBinner(ComplexF64)

    # block of maximally correlated values:
    N_corr = 16

    # number of blocks
    N_blocks = 131_072 # 2^17

    for _ in 1:N_blocks
        x = rand(ComplexF64)
        for __ in 1:N_corr
            push!(BA, x)
        end
    end

    # BA must diverge until 16 values are binned
    for lvl in 1:4
        @test !has_converged(BA, lvl)
    end

    # Afterwards it must converge (for a while)
    for lvl in 5:8
        @test has_converged(BA, lvl)
    end
    # Later values may fluctuate due to small samples / small threshold in
    # has_converged

    # means should be consistent
    means = BinningAnalysis.all_means(BA)
    for x in means
        @test x ≈ means[1]
    end
end

@testset "Checking converging data (Vector)" begin
    BA = LogBinner(zeros(3))

    # block of maximally correlated values:
    N_corr = 16

    # number of blocks
    N_blocks = 131_072 # 2^17

    for _ in 1:N_blocks
        x = rand(Float64, 3)
        for __ in 1:N_corr
            push!(BA, x)
        end
    end

    # BA must diverge until 16 values are binned
    for lvl in 1:4
        @test !has_converged(BA, lvl)
    end

    # Afterwards it must converge (for a while)
    for lvl in 5:8
        @test has_converged(BA, lvl)
    end
    # Later values may fluctuate due to small samples / small threshold in
    # has_converged

    # means should be consistent
    means = BinningAnalysis.all_means(BA)
    for i in eachindex(means)
        @test means[i] ≈ means[1]
    end
end

@testset "Type promotion" begin
    Bf = LogBinner(zero(1.)) # Float64 LogBinner
    Bc = LogBinner(zero(im)) # Float64 LogBinner

    # Check that this doesn't throw (TODO: is there a better way?)
    @test (append!(Bf, rand(1:10, 10000)); true)
    @test (append!(Bc, rand(10000)); true)
end

@testset "Sum-type heuristic" begin
    # numbers
    @test typeof(LogBinner(zero(Int64))) == LogBinner{32,Float64}
    @test typeof(LogBinner(zero(ComplexF16))) == LogBinner{32,ComplexF64}

    # arrays
    @test typeof(LogBinner(zeros(Int64, 2,2))) == LogBinner{32,Matrix{Float64}}
    @test typeof(LogBinner(zeros(ComplexF16, 2,2))) == LogBinner{32,Matrix{ComplexF64}}
end


text:
function _set_ticks(ax, axtypes, minorticks, ticklabels)

    xypairs = zip([ax.xaxis, ax.yaxis], axtypes, minorticks, ticklabels)
    for (axis, axtype, minorticks, ticklabels) in xypairs

        turn_off_minorticks() = axis.set_minor_locator(mpl.ticker.NullLocator())

        if axtype == :range
            # Because we set the rcParam `autolimit_mode` to `data`, xlim/ylim == data range.
            a, b = axis.get_view_interval()
            digits = 2
            axis.set_ticks([round(a, RoundDown; digits), round(b, RoundUp; digits)])
            # Turn off all gridlines.
            axis.grid(which = "major", visible = false)
            turn_off_minorticks()

        elseif axtype == :categorical
            # Do not mess with ticklocs. Except:
            turn_off_minorticks()

        elseif axis.get_scale() == "log"
            # Mpl default is good, do nothing.

        else
            axis.set_major_locator(mpl.ticker.MaxNLocator(nbins = 7, steps = [1, 2, 5, 10]))
            #   `nbins` should probably depend on figure size, i.e. how large texts are wrt
            #   other graphical elements.
            if minorticks
                axis.set_minor_locator(mpl.ticker.AutoMinorLocator())
            else
                turn_off_minorticks()
            end
        end

        # LogLocator places ticks outside limits. So we trim those.
        ticklocs = axis.get_ticklocs()
        a, b = axis.get_view_interval()
        ticklocs = ticklocs[a .≤ ticklocs .≤ b]

        if isnothing(ticklabels)
            ticklabels = [@sprintf "%.4g" t for t in ticklocs]
        end

        units = hasproperty(axis, :unitful_units) ? axis.unitful_units : NoUnits
        if units != NoUnits
            suffix = " " * repr("text/plain", units)
            if axis == ax.xaxis
                prefix_width = round(Int, length(suffix) * 1.6)
                prefix = repeat(" ", prefix_width)  # Imprecise hack to shift label to the
                                                    # right, to get number back under tick.
            else
                prefix = ""
            end
            ticklabels[end] = prefix * ticklabels[end] * suffix
        end

        bbox = Dict(
            :facecolor => mpl.rcParams["figure.facecolor"],
            :edgecolor => "none",
            :pad => 3,  # Relative to fontsize (google "bbox mutation scale").
        )
        # Goal: labels stay visible when overlapping with elements of an adjactent Axes.

        axis.set_ticks(ticklocs, ticklabels; bbox)
    end
end


text:
module Repulsion

using StaticArrays

import JuLIP: decode_dict
import JuLIP.Potentials: @pot, evaluate, evaluate_d, MPairPotential, @D, cutoff,
                         @analytic, evaluate!, evaluate_d!,
                         alloc_temp, alloc_temp_d,
                         i2z, z2i
import Base: Dict, convert, ==

# ----------------------------------------------------------------------
# The repulsive core is built from shifted Buckingham potentials

"""
e0 + B * exp( - A * (r/ri-1) ) * ri/r
"""
struct BuckPot{T}
   e0::T
   A::T
   ri::T
   B::T
end

@pot BuckPot

evaluate(V::BuckPot, r) = V.e0 + V.B * exp( - V.A * (r/V.ri-1) ) * V.ri/r

evaluate_d(V::BuckPot, r) = V.B * exp( - V.A * (r/V.ri-1) ) * V.ri * (
                            - V.A / V.ri / r  - 1/r^2  )

Dict(V::BuckPot) = Dict("__id__" => "PolyPairPots_BuckPot",
                        "e0" => V.e0, "A" => V.A, "ri" => V.ri, "B" => V.B )

BuckPot(D::Dict) = BuckPot(D["e0"], D["A"], D["ri"], D["B"])

convert(::Val{:PolyPairPots_BuckPot}, D::Dict) = BuckPot(D)

==(V1::BuckPot, V2::BuckPot) =
   all(getfield(V1, x) == getfield(V2, x) for x in fieldnames(BuckPot))

# ----------------------------------------------------------------------

struct RepulsiveCore{T, TOUT, NZ} <: MPairPotential
   Vout::TOUT
   Vin::SMatrix{NZ, NZ, BuckPot{T}}
end

==(V1::RepulsiveCore, V2::RepulsiveCore) =
   (V1.Vout == V2.Vout) && (V1.Vin == V2.Vin)

@pot RepulsiveCore

cutoff(V::RepulsiveCore) = cutoff(V.Vout)

alloc_temp(V::RepulsiveCore, N::Integer) = alloc_temp(V.Vout, N)
alloc_temp_d(V::RepulsiveCore, N::Integer) = alloc_temp_d(V.Vout, N)

function evaluate!(tmp, V::RepulsiveCore, r::Number, z, z0)
   iz, iz0 = z2i(V.Vout, z), z2i(V.Vout, z0)
   Vin = V.Vin[iz, iz0]
   if r > Vin.ri
      return evaluate!(tmp, V.Vout, r, z, z0)
   else
      return evaluate(Vin, r)
   end
end

function evaluate_d!(tmp, V::RepulsiveCore, r::Number, z, z0)
   iz, iz0 = z2i(V.Vout, z), z2i(V.Vout, z0)
   Vin = V.Vin[iz, iz0]
   if r > Vin.ri
      return evaluate_d!(tmp, V.Vout, r, z, z0)
   else
      return evaluate_d(Vin, r)
   end
end

function _simple_repulsive_core(Vout, ri, e0, verbose, z, z0)
   v = Vout(ri, z, z0)
   dv = @D Vout(ri, z, z0)
   if dv >= 0.0
      @warn("The slope `Vout'(ri)` should be negative")
   end
   if dv > -1.0
      @warn("""The slope `Vout'(ri) = $dv` may not be steep enough to attach a
               repulsive core. Proceed at your own risk.""")
   end
   if v-e0 <= 0.0
      @warn("it is recommended that `Vout(ri) > 0`.")
   end
   if v-e0 <= 1.0
      @warn("""Ideally the repulsive core should not be attached at small
               values of `Vout(ri) = $v`. Proceed at your own risk.""")
   end
   # e0 + B e^{-A (r/ri-1)} * ri/r
   #    => e0 + B = Vout(ri) => = Vout(ri) - e0 = v - e0
   # dv = - A*B/ri e^{-A (r/ri-1)} * ri/r - B*ri*e^{...} / r^2
   #    = - A/ri * (v - 1/ri * (v = - (1+A)/ri * (v-e0)
   #    => -(1+A)/ri * (v-e0) = dv
   #    => 1+A = - ri dv / (v-e0)
   #    => A = -1 - ri dv / (v-e0)
   A = -1 - ri * dv / (v-e0)
   B = v-e0
   Vin = BuckPot(e0, A, ri, B)
   if verbose
      @show ri
      @show Vout(ri), (@D Vout(ri))
      @show Vin(ri), (@D Vin(ri))
   end
   return Vin
end

function RepulsiveCore(Vout, ri::Number, e0=0.0; verbose=false)
   nz = length(Vout.zlist)
   Vin = Matrix{Any}(undef, nz, nz)
   for i0 = 1:nz, i1 = 1:i0
      z0, z1 = i2z(Vout, i0), i2z(Vout, i1)
      Vin[i0, i1] = Vin[i1, i0] =
            _simple_repulsive_core(Vout, ri, e0, verbose, z1, z0)
   end
   # construct the piecewise potential
   return RepulsiveCore(Vout, Vin)
end

function RepulsiveCore(Vout, Vin::AbstractArray)
   nz = length(Vout.zlist)
   return RepulsiveCore(Vout, SMatrix{nz, nz}(Vin...))
end

# ----------------------------------------------------
#  File IO
# ----------------------------------------------------

Dict(V::RepulsiveCore) = Dict("__id__" => "PolyPairPots_RepulsiveCore",
                              "Vout" => Dict(V.Vout),
                              "Vin" => Dict.(V.Vin[:]) )

function RepulsiveCore(D::Dict)
   Vout = decode_dict(D["Vout"])
   nz = length(Vout.zlist)
   Vin = BuckPot.(D["Vin"])
   return RepulsiveCore(Vout, reshape(Vin, (nz, nz)))
end

convert(::Val{:PolyPairPots_RepulsiveCore}, D::Dict) = RepulsiveCore(D)

end


text:
module POMDPTesting

using POMDPs
using Random

export test_solver,
    probability_check,
    obs_prob_consistency_check,
    trans_prob_consistency_check,
    has_consistent_distributions,
    has_consistent_initial_distribution,
    has_consistent_transition_distributions,
    has_consistent_observation_distributions

include("model.jl")
include("solver.jl")

end # module


text:
using Dates

"""
    $(TYPEDEF)

Wraps a `TensorBoardLogger.TBLogger` to construct a callback to be passed to
`Turing.sample`.

# Usage

    TensorBoardCallback(; kwargs...)
    TensorBoardCallback(directory::string[, stats]; kwargs...)
    TensorBoardCallback(lg::TBLogger[, stats]; kwargs...)

Constructs an instance of a `TensorBoardCallback`, creating a `TBLogger` if `directory` is
provided instead of `lg`.

## Arguments
- `stats = nothing`: `OnlineStat` or lookup for variable name to statistic estimator.
  If `stats isa OnlineStat`, we will create a `DefaultDict` which copies `stats` for unseen
  variable names.
  If `isnothing`, then a `DefaultDict` with a default constructor returning a
  `OnlineStats.Series` estimator with `Mean()`, `Variance()`, and `KHist(num_bins)`
  will be used.

## Keyword arguments
- `num_bins::Int = 100`: Number of bins to use in the histograms.
- `variable_filter = nothing`: Filter determining whether or not we should log stats for a
  particular variable.
  If `isnothing` a default-filter constructed from `exclude` and
  `include` will be used.
- `exclude = String[]`: If non-empty, these variables will not be logged.
- `include = String[]`: If non-empty, only these variables will be logged.
- `include_extras::Bool = true`: Include extra statistics from transitions.
- `directory::String = nothing`: if specified, will together with `comment` be used to
   define the logging directory.
- `comment::String = nothing`: if specified, will together with `directory` be used to
   define the logging directory.

# Fields
$(TYPEDFIELDS)
"""
struct TensorBoardCallback{F, L}
    "Underlying logger."
    logger::TBLogger
    "Filter determining whether or not we should log stats for a particular variable."
    variable_filter::F
    "Include extra statistics from transitions."
    include_extras::Bool
    "Lookup for variable name to statistic estimate."
    stats::L
end

function TensorBoardCallback(directory::String, args...; kwargs...)
    TensorBoardCallback(args...; directory = directory, kwargs...)
end

function TensorBoardCallback(args...; comment = "", directory = nothing, kwargs...)
    log_dir = if isnothing(directory)
        "runs/$(Dates.format(now(), dateformat"Y-m-d_H-M-S"))-$(gethostname())$(comment)"
    else
        directory
    end

    # Set up the logger
    lg = TBLogger(log_dir, min_level=Logging.Info; step_increment=0)

    return TensorBoardCallback(lg, args...; kwargs...)
end

function TensorBoardCallback(
    lg::TBLogger,
    stats = nothing;
    num_bins::Int = 100,
    exclude = String[],
    include = String[],
    include_extras::Bool = true,
    variable_filter = nothing,
    kwargs...
)
    # Create the filter
    filter = if !isnothing(variable_filter)
        variable_filter
    else
        varname -> (
            (isempty(exclude) || varname ∉ exclude) &&
            (isempty(include) || varname ∈ include)
        )
    end

    # Lookups: create default ones if not given
    stats_lookup = if stats isa OnlineStat
        # Warn the user if they've provided a non-empty `OnlineStat`
        nobs(stats) > 0 && @warn("using statistic with observations as a base: $(stats)")
        let o = stats
            DefaultDict{String, typeof(o)}(() -> deepcopy(o))
        end
    elseif !isnothing(stats)
        # If it's not an `OnlineStat` nor `nothing`, assume user knows what they're doing
        stats
    else
        # This is default
        let o = OnlineStats.Series(Mean(), Variance(), KHist(num_bins))
            DefaultDict{String, typeof(o)}(() -> deepcopy(o))
        end
    end

    return TensorBoardCallback(
        lg, filter, include_extras, stats_lookup
    )
end

function (cb::TensorBoardCallback)(rng, model, sampler, transition, iteration, state; kwargs...)
    stats = cb.stats
    lg = cb.logger
    filter = cb.variable_filter

    with_logger(lg) do
        for (ksym, val) in zip(Turing.Inference._params_to_array([transition])...)
            k = string(ksym)
            if !filter(k)
                continue
            end
            stat = stats[k]

            # Log the raw value
            @info k val

            # Update statistic estimators
            fit!(stat, val)

            # Need some iterations before we start showing the stats
            @info k stat
        end

        # Transition statstics
        if cb.include_extras
            names, vals = Turing.Inference.get_transition_extras([transition])
            for (name, val) in zip(string.(names), vec(vals))
                @info ("extras/" * name) val
            end
        end
        @info "" log_step_increment=1
    end
end


text:
module TableIOInterface

export get_file_type, get_example_code

## definition of file formats and extensions

abstract type AbstractFormat end

struct CSVFormat <: AbstractFormat end
struct ZippedFormat <: AbstractFormat end
struct JDFFormat <: AbstractFormat end
struct ParquetFormat <: AbstractFormat end
struct ExcelFormat <: AbstractFormat end
struct SQLiteFormat <: AbstractFormat end
struct StataFormat <: AbstractFormat end
struct SPSSFormat <: AbstractFormat end
struct SASFormat <: AbstractFormat end
struct JSONFormat <: AbstractFormat end
struct ArrowFormat <: AbstractFormat end

# data base only formats - not completely integrated yet
struct PostgresFormat <: AbstractFormat end

# define if copy of columns is required when converting Tables.jl interface to DataFrame
require_copycols(::AbstractFormat) = false
require_copycols(::JSONFormat) = true

# mapping of file extensions to table file formats
# multiple extensions can be mapped to the same format
const FILE_EXTENSIONS = Dict{String, DataType}(
    "zip" => ZippedFormat,
    "csv" => CSVFormat,
    "jdf" => JDFFormat,
    "parquet" => ParquetFormat,
    "xlsx" => ExcelFormat,
    "xlsm" => ExcelFormat,
    "db" => SQLiteFormat,
    "sqlite" => SQLiteFormat,
    "sqlite3" => SQLiteFormat,
    "dta" => StataFormat,
    "sav" => SPSSFormat,
    "sas7bdat" => SASFormat,
    "json" => JSONFormat,
    "arrow" => ArrowFormat,
)

_get_file_extension(filename:: AbstractString) = lowercase(splitext(filename)[2][2:end])

get_file_type(filename:: AbstractString) = FILE_EXTENSIONS[_get_file_extension(filename)]()

const import_table_io = "import TableIO"
const import_dataframes = "import DataFrames"

function get_example_code(t::T, directory:: AbstractString, filename:: AbstractString) where {T <: AbstractFormat}
    """
    df_$(_get_varname(filename)) = let
        $import_table_io
        $import_dataframes
        DataFrames.DataFrame(TableIO.read_table($(_get_path_code(directory, filename))); copycols=$(require_copycols(t)))
    end"""
end

function get_example_code(t::ExcelFormat, directory:: AbstractString, filename:: AbstractString)
    """
    df_$(_get_varname(filename)) = let
        $import_table_io
        $import_dataframes
        df_$(_get_varname(filename)) = DataFrames.DataFrame(TableIO.read_table($(_get_path_code(directory, filename)),
            # "Sheet1", # uncomment this to specify the sheet name to be imported
            ); copycols=$(require_copycols(t)))
    end"""
end

function get_example_code(t::SQLiteFormat, directory:: AbstractString, filename:: AbstractString)
    """
    df_$(_get_varname(filename)) = let
        $import_table_io
        $import_dataframes
        df_$(_get_varname(filename)) = DataFrames.DataFrame(TableIO.read_table($(_get_path_code(directory, filename)),
            "tablename", # define the table name here
            ); copycols=$(require_copycols(t)))
    end"""
end

function get_example_code(t::ZippedFormat, directory:: AbstractString, filename:: AbstractString)
    """
    df_$(_get_varname(filename)) = let
        $import_table_io
        $import_dataframes
        df_$(_get_varname(filename)) = DataFrames.DataFrame(TableIO.read_table($(_get_path_code(directory, filename)),
            # "filename_in_archive", # uncomment this to specify a specific file name in the archive
            ); copycols=$(require_copycols(t)))
    end"""
end

_get_path_code(directory, filename) = """joinpath(dirname(@__FILE__), "$directory", "$filename")"""
_get_varname(filename) = replace(filename, r"[\"\-,\.#@!\%\s+\;()\$&*\[\]\{\}'^]" => "_")

get_example_code(directory:: AbstractString, filename:: AbstractString, args...; kwargs...) = get_example_code(get_file_type(filename), directory, filename, args...; kwargs...)
end


text:
using Test
using DifferentialAlgebra

@test 1+1==2


text:
include("file1.jl")
module TestModule
    include("file2.jl")
end


text:
using Test, LinearMaps, LinearAlgebra

@testset "block maps" begin
    @testset "hcat" begin
        for elty in (Float32, Float64, ComplexF64), n2 = (0, 20)
            A11 = rand(elty, 10, 10)
            A12 = rand(elty, 10, n2)
            L = @inferred hcat(LinearMap(A11), LinearMap(A12))
            @test @inferred(LinearMaps.MulStyle(L)) === matrixstyle
            @test L isa LinearMaps.BlockMap{elty}
            A = [A11 A12]
            x = rand(10+n2)
            @test size(L) == size(A)
            @test Matrix(L) ≈ A
            @test L * x ≈ A * x
            L = @inferred hcat(LinearMap(A11), LinearMap(A12), LinearMap(A11))
            A = [A11 A12 A11]
            @test Matrix(L) ≈ A
            A = [I I I A11 A11 A11]
            L = @inferred hcat(I, I, I, LinearMap(A11), LinearMap(A11), LinearMap(A11))
            @test L == [I I I LinearMap(A11) LinearMap(A11) LinearMap(A11)]
            x = rand(elty, 60)
            @test L isa LinearMaps.BlockMap{elty}
            @test L * x ≈ A * x
            A11 = rand(elty, 11, 10)
            A12 = rand(elty, 10, n2)
            @test_throws DimensionMismatch hcat(LinearMap(A11), LinearMap(A12))
        end
    end

    @testset "vcat" begin
        for elty in (Float32, Float64, ComplexF64)
            A11 = rand(elty, 10, 10)
            L = @inferred vcat(LinearMap(A11))
            @test L == [LinearMap(A11);]
            @test Matrix(L) ≈ A11
            A21 = rand(elty, 20, 10)
            L = @inferred vcat(LinearMap(A11), LinearMap(A21))
            @test L isa LinearMaps.BlockMap{elty}
            @test @inferred(LinearMaps.MulStyle(L)) === matrixstyle
            A = [A11; A21]
            x = rand(10)
            @test size(L) == size(A)
            @test Matrix(L) ≈ A
            @test L * x ≈ A * x
            A = [I; I; I; A11; A11; A11]
            L = @inferred vcat(I, I, I, LinearMap(A11), LinearMap(A11), LinearMap(A11))
            @test L == [I; I; I; LinearMap(A11); LinearMap(A11); LinearMap(A11)]
            x = rand(elty, 10)
            @test L isa LinearMaps.BlockMap{elty}
            @test L * x ≈ A * x
            A11 = rand(elty, 10, 11)
            A21 = rand(elty, 20, 10)
            @test_throws DimensionMismatch vcat(LinearMap(A11), LinearMap(A21))
        end
    end

    @testset "hvcat" begin
        for elty in (Float32, Float64, ComplexF64)
            A11 = rand(elty, 10, 10)
            A12 = rand(elty, 10, 20)
            A21 = rand(elty, 20, 10)
            A22 = rand(elty, 20, 20)
            A = [A11 A12; A21 A22]
            @inferred hvcat((2,2), LinearMap(A11), LinearMap(A12), LinearMap(A21), LinearMap(A22))
            L = [LinearMap(A11) LinearMap(A12); LinearMap(A21) LinearMap(A22)]
            @test @inferred(LinearMaps.MulStyle(L)) === matrixstyle
            @test @inferred !issymmetric(L)
            @test @inferred !ishermitian(L)
            x = rand(30)
            @test L isa LinearMaps.BlockMap{elty}
            @test size(L) == size(A)
            @test L * x ≈ A * x
            @test Matrix(L) ≈ A
            A = [I A12; A21 I]
            @inferred hvcat((2,2), I, LinearMap(A12), LinearMap(A21), I)
            L = @inferred hvcat((2,2), I, LinearMap(A12), LinearMap(A21), I)
            @test L isa LinearMaps.BlockMap{elty}
            @test size(L) == (30, 30)
            @test Matrix(L) ≈ A
            @test L * x ≈ A * x
            A = rand(elty, 10,10); LA = LinearMap(A)
            B = rand(elty, 20,30); LB = LinearMap(B)
            @test [LA LA LA; LB] isa LinearMaps.BlockMap{elty}
            @test Matrix([LA LA LA; LB]) ≈ [A A A; B]
            @test [LB; LA LA LA] isa LinearMaps.BlockMap{elty}
            @test Matrix([LB; LA LA LA]) ≈ [B; A A A]
            @test [I; LA LA LA] isa LinearMaps.BlockMap{elty}
            @test Matrix([I; LA LA LA]) ≈ [I; A A A]
            A12 = LinearMap(rand(elty, 10, 21))
            A21 = LinearMap(rand(elty, 20, 10))
            @test_throws DimensionMismatch A = [I A12; A21 I]
            @test_throws DimensionMismatch A = [I A21; A12 I]
            @test_throws DimensionMismatch A = [A12 A12; A21 A21]
            @test_throws DimensionMismatch A = [A12 A21; A12 A21]

            # basic test of "misaligned" blocks
            M = ones(elty, 3, 2) # non-square
            A = LinearMap(M)
            B = [I A; A I]
            C = [I M; M I]
            @test B isa LinearMaps.BlockMap{elty}
            @test Matrix(B) == C
            @test Matrix(transpose(B)) == transpose(C)
            @test Matrix(adjoint(B)) == C'
        end
    end

    @testset "adjoint/transpose" begin
        for elty in (Float32, Float64, ComplexF64), transform in (transpose, adjoint)
            A12 = rand(elty, 10, 10)
            A = [I A12; transform(A12) I]
            L = [I LinearMap(A12); transform(LinearMap(A12)) I]
            @test @inferred(LinearMaps.MulStyle(L)) === matrixstyle
            if elty <: Complex
                if transform == transpose
                    @test @inferred issymmetric(L)
                else
                    @test @inferred ishermitian(L)
                end
            end
            if elty <: Real
                @test @inferred ishermitian(L)
                @test @inferred issymmetric(L)
            end
            x = rand(elty, 20)
            @test L isa LinearMaps.LinearMap{elty}
            @test size(L) == size(A)
            @test L * x ≈ A * x
            @test Matrix(L) ≈ A
            Lt = @inferred transform(L)
            @test Lt isa LinearMaps.LinearMap{elty}
            @test Lt * x ≈ transform(A) * x
            Lt = @inferred transform(LinearMap(L))
            @test Lt * x ≈ transform(A) * x
            @test Matrix(Lt) ≈ Matrix(transform(A))
            A21 = rand(elty, 10, 10)
            A = [I A12; A21 I]
            L = [I LinearMap(A12); LinearMap(A21) I]
            Lt = @inferred transform(L)
            @test Lt isa LinearMaps.LinearMap{elty}
            @test Lt * x ≈ transform(A) * x
            @test Matrix(Lt) ≈ Matrix(transform(A))
        end
    end
end


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

mutable struct TokenBucket <: SwaggerModel
    size::Union{ Nothing, Int64 } # size
    one_time_burst::Union{ Nothing, Int64 } # one_time_burst
    refill_time::Union{ Nothing, Int64 } # refill_time

    function TokenBucket(;size=nothing, one_time_burst=nothing, refill_time=nothing)
        o = new()
        set_field!(o, :size, size)
        set_field!(o, :one_time_burst, one_time_burst)
        set_field!(o, :refill_time, refill_time)
        o
    end
end # type TokenBucket

const _name_map_TokenBucket = Dict{String,Symbol}(["size"=>:size, "one_time_burst"=>:one_time_burst, "refill_time"=>:refill_time])
const _field_map_TokenBucket = Dict{Symbol,String}([:size=>"size", :one_time_burst=>"one_time_burst", :refill_time=>"refill_time"])
Swagger.name_map(::Type{ TokenBucket }) = _name_map_TokenBucket
Swagger.field_map(::Type{ TokenBucket }) = _field_map_TokenBucket

function check_required(o::TokenBucket)
    (o.size === nothing) && (return false)
    (o.refill_time === nothing) && (return false)
    true
end

function validate_field(o::TokenBucket, name::Symbol, val)
    if name === :size
        Swagger.validate_param(name, "TokenBucket", :minimum, val, 0, false)
    end
    if name === :one_time_burst
        Swagger.validate_param(name, "TokenBucket", :minimum, val, 0, false)
    end
    if name === :refill_time
        Swagger.validate_param(name, "TokenBucket", :minimum, val, 0, false)
    end
end


text:
using Distributions, Random

@testset "test Gamma kde function" begin
    res = DualOptimalFiltering.create_gamma_kde_mixture_parameters_one_β([1,4,6])
    for i in eachindex(res[1])
        @test res[1][i] ≈ [1.46703, 2.86814, 3.80221][i] atol=10^(-5)
    end
    @test res[2] ≈ 0.4670348611479834 atol=10^(-10)
    res = DualOptimalFiltering.create_gamma_kde_mixture_parameters([1,4,6])
    @test length(res[1]) == length(res[2])
end;

@testset "test Dirichlet kde function" begin
    Random.seed!(0);
    xdata = rand(Dirichlet([0.3,5.,2.3]), 5)'
    @test_nowarn DualOptimalFiltering.create_Dirichlet_kde_mixture_parameters(xdata)
    xdata_list = [xdata[i,:] for i in 1:size(xdata,1)]
    @test_nowarn DualOptimalFiltering.create_Dirichlet_kde_mixture_parameters(xdata_list)
end;

max_stars_repo_name: konkam/DualOptimalFiltering

max_issues_repo_name: konkam/DualOptimalFiltering

max_forks_repo_name: konkam/DualOptimalFiltering


text:
using Test

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

@testset "MessageFrame Tests" begin

    @testset "test_should_be_able_to_read_int" begin
        frame = MessageFrame(Base.view(Array(b"\x00\x03ABC\x00\x00"), :), Tuple{Integer, Integer}[(2,5)])

        values = [read_int(frame) for _ in 1:4]

        @test values == [65, 66, 67, -1]
    end

    @testset "test_should_be_able_to_read_int_across_chunks" begin
        frame = MessageFrame(Base.view(Array(b"\x00\x03ABC\x00\x03DEF\x00\x00"), :), Tuple{Integer, Integer}[(2, 5), (7, 10)])

        values = [read_int(frame) for _ in 1:7]

        @test values == [65, 66, 67, 68, 69, 70, -1]
    end

    @testset "test_should_be_able_to_read_one" begin
        frame = MessageFrame(Base.view(Array(b"\x00\x03ABC\x00\x00"), :), Tuple{Integer, Integer}[(2, 5)])

        value = read(frame, 1)

        @test value == b"A"
        @test typeof(value) <: SubArray
    end

    @testset "test_should_be_able_to_read_some" begin
        frame = MessageFrame(Base.view(Array(b"\x00\x03ABC\x00\x00"), :), Tuple{Integer, Integer}[(2, 5)])

        value = read(frame, 2)

        @test value == b"AB"
        @test typeof(value) <: SubArray
    end

    @testset "test_should_be_able_to_read_all" begin
        frame = MessageFrame(Base.view(Array(b"\x00\x03ABC\x00\x00"), :), Tuple{Integer, Integer}[(2, 5)])

        value = read(frame, 3)

        @test value == b"ABC"
        @test typeof(value) <: SubArray
    end

    @testset "test_should_read_empty_if_exhausted" begin
        frame = MessageFrame(Base.view(Array(b"\x00\x03ABC\x00\x00"), :), Tuple{Integer, Integer}[(2, 5)])
        value = read(frame, 3)

        value = read(frame, 3)

        @test value == b""
    end

    @testset "test_should_be_able_to_read_beyond" begin
        frame = MessageFrame(Base.view(Array(b"\x00\x03ABC\x00\x00"), :), Tuple{Integer, Integer}[(2, 5)])

        value = read(frame, 4)

        @test value == b"ABC"
    end

    @testset "test_should_be_able_to_read_across_chunks" begin
        frame = MessageFrame(Base.view(Array(b"\x00\x03ABC\x00\x03DEF\x00\x00"), :), Tuple{Integer, Integer}[(2, 5), (7, 10)])

        value = read(frame, 4)

        @test value == b"ABCD"
    end

    @testset "test_should_be_able_to_read_all_across_chunks" begin
        frame = MessageFrame(Base.view(Array(b"\x00\x03ABC\x00\x03DEF\x00\x00"), :), Tuple{Integer, Integer}[(2, 5), (7, 10)])

        value = read(frame, 6)

        @test value == b"ABCDEF"
    end
end

max_stars_repo_name: virtualgraham/JuliaBolt

max_issues_repo_name: virtualgraham/JuliaBolt

max_forks_repo_name: virtualgraham/JuliaBolt


text:
using SerializedElementArrays
using Documenter

DocMeta.setdocmeta!(
  SerializedElementArrays, :DocTestSetup, :(using SerializedElementArrays); recursive=true
)

makedocs(;
  modules=[SerializedElementArrays],
  authors="Matthew Fishman <mfishman@flatironinstitute.org> and contributors",
  repo="https://github.com/mtfishman/SerializedElementArrays.jl/blob/{commit}{path}#{line}",
  sitename="SerializedElementArrays.jl",
  format=Documenter.HTML(;
    prettyurls=get(ENV, "CI", "false") == "true",
    canonical="https://mtfishman.github.io/SerializedElementArrays.jl",
    assets=String[],
  ),
  pages=["Home" => "index.md"],
)

deploydocs(; repo="github.com/mtfishman/SerializedElementArrays.jl")


text:
import ..UncertainDatasets: UncertainIndexValueDataset
import ..SamplingConstraints: SamplingConstraint

# Some summary documentaiton for the online documentation
"""
	resample(udata::UncertainIndexValueDataset,
		constraint::Union{SamplingConstraint, Vector{SamplingConstraint}}) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample an uncertain index-value dataset in an element-wise manner.

Enforces the provided sampling `constraint` to all uncertain values in the dataset, both
indices and data values.

If a single constraint is provided, then that constraint will be applied to all values. If a
vector of constraints (as many as there are values) is provided, then the constraints are
applied element-wise to both the indices and the data values.
"""
resample(udata::UncertainIndexValueDataset,
	constraint::Union{SamplingConstraint, Vector{SamplingConstraint}})

"""
	resample(udata::UncertainIndexValueDataset,
		constraint::Union{SamplingConstraint, Vector{SamplingConstraint}},
		n::Int) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample `n` realizations of an uncertain index-value dataset in an element-wise manner.

Enforces the provided sampling `constraint` to all uncertain values in the dataset, both
indices and data values.

If a single constraint is provided, that constraint will be applied to all values. If a
vector of constraints (as many as there are values) is provided, then the constraints are
applied element-wise to both the indices and the data values.
"""
resample(udata::UncertainIndexValueDataset,
	constraint::Union{SamplingConstraint, Vector{SamplingConstraint}},
	n::Int)

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Union{SamplingConstraint, Vector{SamplingConstraint}},
		constraint_vals::Union{SamplingConstraint, Vector{SamplingConstraint}}) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample an uncertain index-value dataset in an element-wise manner.

Enforces separate sampling constraints to the indices and to the data values.

If a single constraint is provided, that constraint will be applied to all values. If a
vector of constraints (as many as there are values) is provided, then the constraints are
applied element-wise.
"""
resample(udata::UncertainIndexValueDataset,
	constraint_idxs::Union{SamplingConstraint, Vector{SamplingConstraint}},
	constraint_vals::Union{SamplingConstraint, Vector{SamplingConstraint}})

	"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Union{SamplingConstraint, Vector{SamplingConstraint}},
		constraint_vals::Union{SamplingConstraint, Vector{SamplingConstraint}},
		n::Int) -> Vector{Tuple{Vector{Float64}, Vector{Float64}}}

Resample `n` realizations of an uncertain index-value dataset in an element-wise manner.

Enforces separate sampling constraints to the indices and to the data values.

If a single constraint is provided, that constraint will be applied to all values. If a
vector of constraints (as many as there are values) is provided, then the constraints are
applied element-wise.
"""
resample(udata::UncertainIndexValueDataset,
	constraint_idxs::Union{SamplingConstraint, Vector{SamplingConstraint}},
	constraint_vals::Union{SamplingConstraint, Vector{SamplingConstraint}},
	n::Int)

###########################################
# The documentation for the actual methods
###########################################

"""
	resample(udata::UncertainIndexValueDataset) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample an uncertain index-value dataset in an element-wise manner.
"""
function resample(udata::UncertainIndexValueDataset)
	n_vals = length(udata)
	indices = zeros(Float64, n_vals)
	values = zeros(Float64, n_vals)

	for i = 1:n_vals
		idx, val = udata[i]
		indices[i] = resample(idx)
		values[i] = resample(val)
	end

	indices, values
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint::SamplingConstraint) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample an uncertain index-value dataset element-wise in an element-wise manner.

Enforces the provided sampling `constraint` to all uncertain values in the dataset, both
indices and data values.
"""
function resample(udata::UncertainIndexValueDataset, constraint::SamplingConstraint)

	n_vals = length(udata)
	indices = zeros(Float64, n_vals)
	values = zeros(Float64, n_vals)

	for i = 1:n_vals
		idx, val = udata[i]
		indices[i] = resample(idx, constraint)
		values[i] = resample(val, constraint)
	end

	indices, values
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::SamplingConstraint,
		constraint_vals::SamplingConstraint) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample an uncertain index-value dataset in an element-wise manner.

Enforces the same sampling constraint `constraint_idxs` to all index values, and the
`constraint_vals` sampling constraint to all data values.
"""
function resample(udata::UncertainIndexValueDataset,
	constraint_idxs::SamplingConstraint,
	constraint_vals::SamplingConstraint)

	n_vals = length(udata)
	indices = zeros(Float64, n_vals)
	values = zeros(Float64, n_vals)

	for i = 1:n_vals
		idx, val = udata[i]
		indices[i] = resample(idx, constraint_idxs)
		values[i] = resample(val, constraint_vals)
	end

	indices, values
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{SamplingConstraint},
		constraint_vals::SamplingConstraint) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample an uncertain index-value dataset in an element-wise manner.

Enforces a unique sampling constraint `constraint_idxs[i]` to the i-th index value,
while using the same sampling constraint `constraint_vals` on all data values.
"""
function resample(udata::UncertainIndexValueDataset,
	constraint_idxs::Vector{<:SamplingConstraint},
	constraint_vals::SamplingConstraint)

	n_vals = length(udata)
	indices = zeros(Float64, n_vals)
	values = zeros(Float64, n_vals)

	for i = 1:n_vals
		idx, val = udata[i]
		indices[i] = resample(idx, constraint_idxs[i])
		values[i] = resample(val, constraint_vals)
	end

	indices, values
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{SamplingConstraint},
		constraint_vals::SamplingConstraint) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample an uncertain index-value dataset in an element-wise manner.

Enforces a unique sampling constraint `constraint_idxs[i]` to both the i-th index value
and to the i-th data value.
"""
function resample(udata::UncertainIndexValueDataset,
		constraint::Vector{<:SamplingConstraint})

	n_vals = length(udata)
	indices = zeros(Float64, n_vals)
	values = zeros(Float64, n_vals)

	for i = 1:n_vals
		idx, val = udata[i]
		indices[i] = resample(idx, constraint[i])
		values[i] = resample(val, constraint[i])
	end

	indices, values
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{SamplingConstraint},
		constraint_vals::SamplingConstraint) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample an uncertain index-value dataset in an element-wise manner.

Enforces a unique sampling constraint `constraint_idxs[i]` to the i-th index value.
Also enforces a unique sampling constraint `constraint_vals[i]` to the i-th data value.
"""
function resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{<:SamplingConstraint},
		constraint_vals::Vector{<:SamplingConstraint})

	n_vals = length(udata)
	indices = zeros(Float64, n_vals)
	values = zeros(Float64, n_vals)

	for i = 1:n_vals
		idx, val = udata[i]
		indices[i] = resample(idx, constraint_idxs[i])
		values[i] = resample(val, constraint_vals[i])
	end

	indices, values
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{SamplingConstraint},
		constraint_vals::SamplingConstraint) -> Tuple{Vector{Float64}, Vector{Float64}}

Resample an uncertain index-value dataset in an element-wise manner.

Enforces the same sampling constraint `constraint_idxs` on all index values,
while using the sampling constraint `constraint_vals[i]` to the i-th data value.
"""
function resample(udata::UncertainIndexValueDataset,
		constraint_idxs::SamplingConstraint,
		constraint_vals::Vector{<:SamplingConstraint})

	n_vals = length(udata)
	indices = zeros(Float64, n_vals)
	values = zeros(Float64, n_vals)

	for i = 1:n_vals
		idx, val = udata[i]
		indices[i] = resample(idx, constraint_idxs)
		values[i] = resample(val, constraint_vals[i])
	end

	indices, values
end

"""
	resample(udata::UncertainIndexValueDataset,
		n::Int) -> Vector{Tuple{Vector{Float64}, Vector{Float64}}}

Resample `n` realizations an uncertain index-value dataset in an element-wise manner.
"""
function resample(udata::UncertainIndexValueDataset, n::Int)
	[resample(udata) for i = 1:n]
end

"""
	resample(udata::UncertainIndexValueDataset,
		n::Int) -> Vector{Tuple{Vector{Float64}, Vector{Float64}}}

Resample `n` realizations an uncertain index-value dataset in an element-wise manner.

Enforces the provided sampling `constraint` to all uncertain values in the dataset, both
indices and data values.
"""
function resample(udata::UncertainIndexValueDataset, constraint::SamplingConstraint, n::Int)
	[resample(udata, constraint) for i = 1:n]
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::SamplingConstraint,
		constraint_vals::SamplingConstraint,
		n::Int) -> Vector{Tuple{Vector{Float64}, Vector{Float64}}}

Resample `n` realizations of an uncertain index-value dataset in an element-wise manner.

Enforces the same sampling constraint `constraint_idxs` to all index values, and the
`constraint_vals` sampling constraint to all data values.
"""
function resample(udata::UncertainIndexValueDataset,
		constraint_idxs::SamplingConstraint,
		constraint_vals::SamplingConstraint,
		n::Int)

	[resample(udata, constraint_idxs, constraint_vals) for i = 1:n]
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{SamplingConstraint},
		constraint_vals::SamplingConstraint,
		n::Int) -> Vector{Tuple{Vector{Float64}, Vector{Float64}}}

Resample `n` realizations of an uncertain index-value dataset in an element-wise manner.

Enforces a unique sampling constraint `constraint_idxs[i]` to both the i-th index value
and to the i-th data value.
"""
function resample(udata::UncertainIndexValueDataset,
		constraint::Vector{<:SamplingConstraint},
		n::Int)

	[resample(udata, constraint) for i = 1:n]
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{SamplingConstraint},
		constraint_vals::SamplingConstraint,
		n::Int) -> Vector{Tuple{Vector{Float64}, Vector{Float64}}}

Resample `n` realizations of an uncertain index-value dataset in an element-wise manner.

Enforces a unique sampling constraint `constraint_idxs[i]` to the i-th index value,
while using the same sampling constraint `constraint_vals` on all data values.
"""
function resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{<:SamplingConstraint},
		constraint_vals::SamplingConstraint,
		n::Int)

	[resample(udata, constraint_idxs, constraint_vals) for i = 1:n]
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{SamplingConstraint},
		constraint_vals::SamplingConstraint,
		n::Int) -> Vector{Tuple{Vector{Float64}, Vector{Float64}}}

Resample `n` realizations of an uncertain index-value dataset in an element-wise manner.

Enforces the same sampling constraint `constraint_idxs` on all index values,
while using the sampling constraint `constraint_vals[i]` to the i-th data value.
"""
function resample(udata::UncertainIndexValueDataset,
		constraint_idxs::SamplingConstraint,
		constraint_vals::Vector{<:SamplingConstraint},
		n::Int)

	[resample(udata, constraint_idxs, constraint_vals) for i = 1:n]
end

"""
	resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{SamplingConstraint},
		constraint_vals::SamplingConstraint,
		n::Int) -> Vector{Tuple{Vector{Float64}, Vector{Float64}}}

Resample `n` realizations of an uncertain index-value dataset in an element-wise manner.

Enforces a unique sampling constraint `constraint_idxs[i]` to the i-th index value.
Also enforces a unique sampling constraint `constraint_vals[i]` to the i-th data value.
"""
function resample(udata::UncertainIndexValueDataset,
		constraint_idxs::Vector{<:SamplingConstraint},
		constraint_vals::Vector{<:SamplingConstraint},
		n::Int)

	[resample(udata, constraint_idxs, constraint_vals) for i = 1:n]
end

export resample


text:
using RecyclePermanent
using RNGPool
using JLD2
using BenchmarkTools

include("../models/gkModel.jl")

@load "data/gkData.jld" ys θ
ys = ys[1:100]
model = makeGKModel(θ, ys)

N = 80 * model.n
xs = Vector{Float64Particle}(undef, N)
for i in 1:N
  xs[i] = Float64Particle()
end
gs = Vector{Float64}(undef, N)

function fillμ(μ!::F, xs::Vector{Float64Particle}) where F<:Function
  rng= getRNG()
  for i in 1:length(xs)
    @inbounds μ!(xs[i], rng)
  end
end

function fillG(G::F, xs::Vector{Float64Particle}, gs::Vector{Float64}) where
  F<:Function
  for i in 1:length(xs)
    @inbounds gs[i] = G(1, xs[i])
  end
end

@btime fillμ($model.μ!, $xs)
@btime fillG($model.G, $xs, $gs)


text:
module BetterLongRunningExample

using Matte

const title = "Matte Example :: Long-running Computations"

function ui()
    sidebar_layout(
        side_panel(
            h1("Choose two numbers"),
            br(),
            slider("slider1", "Number 1", 0, 100),
            slider("slider2", "Number 2", 0, 100),
            br(),
            button("calculate", "Calculate now!")
        ),
        main_panel(
            h1("The sum of your numbers is:"),
            br(),
            text_output("my_sum"),
            show_if("loading",
            circular_loader()
            )
        )
    )
end

module Server

using Matte

function my_sum(slider1, slider2, calculate, session)
    if calculate
        update_output("loading", true, session)
        sleep(10)
        result = slider1 + slider2
        update_output("loading", false, session)
        result
    end
end

end

end