text:
@testset "Test Optimization" begin

	# test 1D method

	f(x) = -(x^2 - x) # the function has a global maximum at x = -.5
	a = -3
	b = 5

	@testset "Golden Method, 1D" begin
        xstar, fstar = golden_method(f, a, b, maxit=10_000)
        @test maxabs(0.5 - xstar) <= 1e-6
    	@test maxabs(0.25 - fstar) <= 1e-6
    end

	# test multiD method

	g(x) = [f(x[1]),-abs(x[2])] # the 1st function has a global maximum at x = -.5, the 2nd has a global maximum at x = 0
	a = [-3.0, -5.0]
	b = [5.0, 2.0]

	@testset "Golden Method, multiD" begin
        xstar, fstar = golden_method(g, a, b, maxit=10_000)
        @test maxabs([0.5, 0.0] - xstar) <= 1e-6
        @test maxabs([0.25, 0.0] - fstar) <= 1e-6
    end

end


text:
struct QFermionicBasis <: AbstractQBasis
	Dim::Int
	N::Int
	Np::Int
	OccRep::Vector{Tuple{Int,Int}}
end

function fermionicHashfunc(n::Int)
	i::Int = 1
	I::Int = 0
	while n>0
		idx = findFirstBit(n)
		n -= Int(2^(idx-1))

		I += binomial((idx-1),i)
		i += 1
	end
	return I
end

function QFermionicBasis(N::Int, Np::Int)
	OccRep = Vector{Tuple{Int,Int}}()

	n = [0 for i in 1:N]
	n[1:Np] = [1 for i in 1:Np]
	for x in combinations(1:N,Np)
		nidx = 0
		x = N.-x
		nidx = Int(sum(2.0.^(x)))
		#@show nidx

		nReducedBasis = Int(fermionicHashfunc(nidx)+1)
		push!(OccRep,(nidx,nReducedBasis))
	end

	OccRep = OccRep[end:-1:1]
	return QFermionicBasis(size(OccRep)[1], N, Np, OccRep)
end

function fermionicCrMatrix(i::Int, N::Int, Np::Int; preQBasis::Union{AbstractQBasis, Nothing} = nothing)
	N  >= Np+1 || throw(DomainError("you cannot creat more particles $(Np+1) than sites $N"))
	if preQBasis == nothing
		preQBasis = QFermionicBasis(N, Np)
	else
	 	N == preQBasis.N || throw(DomainError("N $N and number of sites of QBasis mismatch!"))
		Np == preQBasis.Np || throw(DomainError("Np $Np and number of particles of QBasis mismatch!"))
	end

	#creationMatrix = spzeros(Float64,newQBasis.Dim, QState.QBasis.Dim)

	rowVec = Vector{Int}([])
	colVec = Vector{Int}([])
	dataVec = Vector{Float64}([])
	for Idx in preQBasis.OccRep
		#@show Idx
		rowIdx = Idx[1] ⊻ (1<<(i-1))
		#@show rowIdx
		if rowIdx > Idx[1]
			rowIdx = Int(fermionicHashfunc(rowIdx)+1)
			#@show rowIdx
			push!(rowVec, rowIdx)
			push!(colVec, Idx[2])

			remainderBits = Idx[1] & Int(sum(2.0.^[j-1 for j = 1:(i-1)]))
			NpreParticles = bitCount(remainderBits)

			c = NpreParticles%2 == 0 ? 1 : -1
	 		push!(dataVec, c)
	 	end
	end

	creationMatrix = sparse(rowVec, colVec, dataVec, binomial(N,Np+1), preQBasis.Dim)

	return creationMatrix
end

function fermionicCr(State::AbstractVector{<:Number}, preQBasis::QFermionicBasis, i::Int, N::Int, Np::Int)

	#@show State.Coef
	N == preQBasis.N || throw(DomainError("system size of state does notmatch with N"))
	Np == preQBasis.Np || throw(DomainError("particle numb	er of state does not match with Np"))

	dataVec = spzeros(ComplexF64, binomial(N,Np+1))

	#if sparse I can reduce the for loop to non-zero elements
	nzind = 0
	try
		nzind = State.nzind
		#println("sparse")
	catch
		nzind = 1:size(State)[1]
	end

	for n in nzind
		Idx = preQBasis.OccRep[n]
		#@show Idx
		rowIdx = Idx[1] ⊻ (1<<(i-1))
		#@show rowIdx
		if rowIdx > Idx[1]   # if particle was at i rowIdx will be lower than initial Idx[1]
			rowIdx  = Int(fermionicHashfunc(rowIdx)+1)
			remainderBits = Idx[1] & Int(sum(2.0.^[j-1 for j = 1:(i-1)]))
			NpreParticles = bitCount(remainderBits)
			c = NpreParticles%2 == 0 ? 1 : -1
			dataVec[rowIdx] =  c*State[Idx[2]]
		end
	end
	#
	#State = QState(dataVec,QFermionicBasis(N,Np+1				))
	#
	return dataVec
end

fermionicCr(State::QState, i::Int, N::Int, Np::Int) = fermionicCr(State.Coef, State.QBasis, i, N, Np)

function addParticleFermionic(singleCoef::AbstractVector{<:Number}, preState::QState)
 	N = preState.QBasis.N
	Np = preState.QBasis.Np

	N >= Np+1 || throw(DomainError("you cannot creat more particles $(Np+1) than sites $N"))

	newQBasis = QFermionicBasis(N,Np+1)
	newCoef = zeros(ComplexF64,newQBasis.Dim)
	preQBasis = preState.QBasis

	for i in 1:N

		for Idx in preQBasis.OccRep

			rowIdx = Idx[1] ⊻ (1<<(i-1))

			if rowIdx > Idx[1]
				rowIdx = Int(fermionicHashfunc(rowIdx)+1)
				remainderBits = Idx[1] & Int(sum(2.0.^[j-1 for j = 1:(i-1)]))
				NpreParticles = bitCount(remainderBits)

				c = NpreParticles%2 == 0 ? 1 : -1
				newCoef[rowIdx] += c*singleCoef[i]*preState.Coef[Idx[2]]
			end
		end
	end

	return QState(newCoef, newQBasis)
end

function constructFermionicState(blochWaveFunc::Vector{<:Any})
	@show size(blochWaveFunc[1,:,1])
	State = QState(blochWaveFunc[1][:,1], QFermionicBasis(size(blochWaveFunc[1])[1],1))
	for i in 2:size(blochWaveFunc)[1]
		State = addParticleFermionic(blochWaveFunc[i][:,1], State)
	end

	return State
end

ext: jl

max_stars_repo_name: Gawatz/exactQmodule

max_issues_repo_name: Gawatz/exactQmodule

max_forks_repo_name: Gawatz/exactQmodule


text:
## Basis
"""
    Basis

A `basis` function is an element of a particular basis for a function space.
Every continuous function in the function space can be represented as a
linear combination of basis functions:
```math
f(x_1, \\cdots, x_n) = \\sum_i \\beta_i\\text{basis}_i(x_1, \\cdots, x_n)
```
So far the only implemented basis is [`Monomial`](@ref).
"""
abstract type Basis end

# evaluate : Matrix -> vector
function evaluate(X::Matrix{Float64}, basis::Basis)
    X_views, N, p = matrix2subviews(X)
    y = Vector{Float64}(N)
    evaluate!(y, X_views, basis, N, p)
    return y
end

# evaluate : Matrix -> Matrix
function evaluate{T<:Basis}(X::Matrix{Float64}, basis::Vector{T})
    X_views, N, p = matrix2subviews(X)
    q = length(basis)
    Y = Matrix{Float64}(N, q)
    Y_views, _, _ = matrix2subviews(Y)
    aux = Vector{Float64}(p)
    evaluate!(Y_views, X_views, basis, N, p)
    Y = subviews2matrix(Y_views, N, q)
    return Y
end

# evaluate! : Matrix -> Matrix (With Overide)
function evaluate!{T <: Basis}(
        Y::Vector{Vector{Float64}}, X::Vector{Vector{Float64}},
        basis::Vector{T}, N::Int, p::Int)
    for (i, y) in enumerate(Y)
        evaluate!(y, X, basis[i], N, p)
    end
end

# Print basis
function Base.show(io::IO, basis::Basis)
    names = [string("x_", i) for i=1:basis.n]
    str = function_display(basis, names)
    print(io, str)
end

# Separate basis terms containing certain terms
function detach_terms{T<:Basis}(basis::Vector{T},
                      list_of_inputs::BitVector)
    n_basis = length(basis)
    basis_containing_inputs = trues(n_basis)
    for (i, m) in enumerate(basis)
        if contains_inputs(basis[i], list_of_inputs)
            basis_containing_inputs[i] = false
        end
    end
    return (basis[basis_containing_inputs],
            basis[.~basis_containing_inputs])
end


text:
using SMC

function make_prior_proposal_smc_sampler(hmm::HiddenMarkovModel,
                                         observations::Vector{Int},
                                         num_particles::Int)
    function log_joint_evaluate(latent_sample::Vector{Int})
        return hmm_log_joint_probability(hmm, latent_sample, observations)
    end
    smc_scheme = HMMPriorSMCScheme(hmm, observations, num_particles)
    return SMCSampler(smc_scheme, log_joint_evaluate)
end

function make_optimal_proposal_smc_sampler(hmm::HiddenMarkovModel,
                                           observations::Vector{Int},
                                           num_particles::Int)
    function log_joint_evaluate(latent_sample::Vector{Int})
        return hmm_log_joint_probability(hmm, latent_sample, observations)
    end
    smc_scheme = HMMConditionalSMCScheme(hmm, observations, num_particles)
    return SMCSampler(smc_scheme, log_joint_evaluate)
end


text:
"""
$(SIGNATURES)

Take a `LxCom` object `lxc` and try to resolve it. Provided a definition exists etc, the definition
is plugged in then sent forward to be re-parsed (in case further latex is present).
"""
function resolve_lxcom(lxc::LxCom, lxdefs::Vector{LxDef}; inmath::Bool=false)::String
    # try to find where the first argument of the command starts (if any)
    i = findfirst("{", lxc.ss)
    # extract the name of the command e.g. \\cite
    name = isnothing(i) ? lxc.ss : subs(lxc.ss, 1:(first(i)-1))

    # sort special commands where the input depends on context (see hyperrefs and inputs)
    haskey(LXCOM_HREF, name)  && return LXCOM_HREF[name](lxc)
    name == "\\input"         && return resolve_lx_input(lxc)
    name ∈ keys(LXCOM_SIMPLE) && return LXCOM_SIMPLE[name](lxc)

    # In subsequent case, whatever the command inserts will be re-parsed (in case the insertion
    # contains further commands or markdown); partial corresponds to what the command corresponds
    # to before re-processing.
    partial = ""

    if name ∈ keys(LXCOM_SIMPLE_REPROCESS)
        partial = LXCOM_SIMPLE_REPROCESS[name](lxc)
    else
        # retrieve the definition attached to the command
        lxdef = getdef(lxc)
        # lxdef = nothing means we're inmath & not found -> let KaTeX deal with it
        isnothing(lxdef) && return lxc.ss
        # lxdef = something -> maybe inmath + found; retrieve & apply
        partial = lxdef
        for (argnum, β) ∈ enumerate(lxc.braces)
            # space sensitive "unsafe" one
            # e.g. blah/!#1 --> blah/blah but note that
            # \command!#1 --> \commandblah and \commandblah would not be found
            partial = replace(partial, "!#$argnum" => content(β))
            # non-space sensitive "safe" one
            # e.g. blah/#1 --> blah/ blah but note that
            # \command#1 --> \command blah and no error.
            partial = replace(partial, "#$argnum" => " " * content(β))
        end
        partial = ifelse(inmath, mathenv(partial), partial) * EOS
    end

    # reprocess (we don't care about jd_vars=nothing)
    plug, _ = convert_md(partial, lxdefs, isrecursive=true, isconfig=false, has_mddefs=false)
    return plug
end

#= ===============
Hyper references
================== =#

"""
PAGE_EQREFS

Dictionary to keep track of equations that are labelled on a page to allow references within the
page.
"""
const PAGE_EQREFS = Dict{String, Int}()

"""
PAGE_EQREFS_COUNTER

Counter to keep track of equation numbers as they appear along the page, this helps with equation
referencing. (The `_XC0q` is just a random string to avoid clashes).
"""
const PAGE_EQREFS_COUNTER = "COUNTER_XC0q"

"""
$(SIGNATURES)

Reset the PAGE_EQREFS dictionary.
"""
@inline function def_PAGE_EQREFS!()
    empty!(PAGE_EQREFS)
    PAGE_EQREFS[PAGE_EQREFS_COUNTER] = 0
    return nothing
end

"""
PAGE_BIBREFS

Dictionary to keep track of bibliographical references on a page to allow citation within the page.
"""
const PAGE_BIBREFS = Dict{String, String}()

"""
$(SIGNATURES)

Reset the PAGE_BIBREFS dictionary.
"""
def_PAGE_BIBREFS!() = (empty!(PAGE_BIBREFS); nothing)

"""
$(SIGNATURES)

Given a `label` command, replace it with an html anchor.
"""
add_label(λ::LxCom)::String = ""

"""
$(SIGNATURES)

Given a `biblabel` command, update `PAGE_BIBREFS` to keep track of the reference so that it
can be linked with a hyperreference.
"""
function add_biblabel(λ::LxCom)::String
    name = refstring(strip(content(λ.braces[1])))
    PAGE_BIBREFS[name] = content(λ.braces[2])
    return ""
end

"""
$(SIGNATURES)

Given a latex command such as `\\eqref{abc}`, hash the reference (here `abc`), check if the given
dictionary `d` has an entry corresponding to that hash and return the appropriate HTML for it.
"""
function form_href(lxc::LxCom, dname::String; parens="("=>")", class="href")::String
    ct = content(lxc.braces[1])   # "r1, r2, r3"
    refs = strip.(split(ct, ",")) # ["r1", "r2", "r3"]
    names = refstring.(refs)
    nkeys = length(names)
    # construct the partial link with appropriate parens, it will be
    # resolved at the second pass (HTML pass) whence the introduction of {{..}}
    # inner will be "{{href $dn $hr1}}, {{href $dn $hr2}}, {{href $dn $hr3}}"
    # where $hr1 is the hash of r1 etc.
    in = prod("{{href $dname $name}}$(ifelse(i < nkeys, ", ", ""))"
                    for (i, name) ∈ enumerate(names))
    # encapsulate in a span for potential css-styling
    return "$(parens.first)$in$(parens.second)"
end

"""
LXCOM_HREF

Dictionary for latex commands related to hyperreference for which a specific replacement that
depends on context is constructed.
"""
const LXCOM_HREF = Dict{String, Function}(
    "\\eqref"    => (λ -> form_href(λ, "EQR";  class="eqref")),
    "\\cite"     => (λ -> form_href(λ, "BIBR"; parens=""=>"", class="bibref")),
    "\\citet"    => (λ -> form_href(λ, "BIBR"; parens=""=>"", class="bibref")),
    "\\citep"    => (λ -> form_href(λ, "BIBR"; class="bibref")),
    "\\biblabel" => add_biblabel,
    "\\label"    => add_label,
    )

"""
$(SIGNATURES)

Internal function to resolve a relative path to `/assets/` and return the resolved dir as well
as the file name; it also checks that the dir exists. It returns the full path to the file, the
path of the directory containing the file, and the name of the file without extension.
See also [`resolve_lx_input`](@ref).
Note: rpath here is always a UNIX path while the output correspond to SYSTEM paths.
"""
function check_input_rpath(rpath::AS, lang::AS="")::NTuple{3,String}
    # find the full system path to the asset
    fpath = resolve_assets_rpath(rpath; canonical=true)

    # check if an extension is given, if not, consider it's `.xx` with language `nothing`
    if isempty(splitext(fpath)[2])
        ext, _ = get(CODE_LANG, lang) do
            (".xx", nothing)
        end
        fpath *= ext
    end
    dir, fname = splitdir(fpath)

    # TODO: probably better to not throw an "argumenterror" here but maybe use html_err
    # (though the return type needs to be adjusted accordingly and it shouldn't be propagated)

    # see fill_extension, this is the case where nothing came up
    if endswith(fname, ".xx")
        # try to find a file with the same root and any extension otherwise throw an error
        files = readdir(dir)
        fn = splitext(fname)[1]
        k = findfirst(e -> splitext(e)[1] == fn, files)
        if k === nothing
            throw(ArgumentError("Couldn't find a relevant file when trying to " *
                                "resolve an \\input command. (given: $rpath)"))
        end
        fname = files[k]
        fpath = joinpath(dir, fname)
    elseif !isfile(fpath)
        throw(ArgumentError("Couldn't find a relevant file when trying to resolve an \\input " *
                            "command. (given: $rpath)"))
    end
    return fpath, dir, splitext(fname)[1]
end

"""
$(SIGNATURES)

Internal function to read the content of a script file. See also [`resolve_lx_input`](@ref).
"""
function resolve_lx_input_hlcode(rpath::AS, lang::AS)::String
    fpath, _, _ = check_input_rpath(rpath)
    # Read the file while ignoring lines that are flagged with something like `# HIDE`
    _, comsym = CODE_LANG[lang]
    hide = Regex(raw"(?:^|[^\S\r\n]*?)#(\s)*?(?i)hide(all)?")
    hideall = false
    io_in = IOBuffer()
    open(fpath, "r") do f
        for line ∈ readlines(f)
            # - if there is a \s#\s+HIDE , skip that line
            m = match(hide, line)
            if m === nothing
                write(io_in, line)
                write(io_in, "\n")
            else
                # does it have a "all" or not?
                (m.captures[2] === nothing) && continue
                # it does have an "all"
                hideall = true
                break
            end

        end
    end
    hideall && take!(io_in) # discard the content
    code = String(take!(io_in))
    endswith(code, "\n") && (code = chop(code, tail=1))
    return html_code(code, lang)
end

"""
$(SIGNATURES)

Internal function to read the content of a script file and highlight it using `highlight.js`. See
also [`resolve_lx_input`](@ref).
"""
function resolve_lx_input_othercode(rpath::AS, lang::AS)::String
    fpath, _, _ = check_input_rpath(rpath)
    return html_code(read(fpath, String), lang)
end

"""
$(SIGNATURES)

Internal function to read the raw output of the execution of a file and display it in a pre block.
See also [`resolve_lx_input`](@ref).
"""
function resolve_lx_input_plainoutput(rpath::AS, reproc::Bool=false)::String
    # will throw an error if rpath doesn't exist
    _, dir, fname = check_input_rpath(rpath)
    out_file = joinpath(dir, "output", fname * ".out")
    # check if the output file exists
    isfile(out_file) || throw(ErrorException("I found an \\input but no relevant output file."))
    # return the content in a pre block
    reproc || return html_code(read(out_file, String))
    return read(out_file, String) * EOS
end

"""
$(SIGNATURES)

Internal function to read a file at a specified path and just plug it in. (Corresponds to what a
simple `\\input` command does in LaTeX).
See also [`resolve_lx_textinput`](@ref).
"""
function resolve_lx_input_textfile(rpath::AS)::String
    # find the full system path to the asset
    fpath = resolve_assets_rpath(rpath; canonical=true)
    isempty(splitext(fpath)[2]) && (fpath *= ".md")
    isfile(fpath) || throw(ErrorException("I found a \\textinput but no relevant file."))
    return read(fpath, String) * EOS
end

"""
$(SIGNATURES)

Internal function to read a plot outputted by script `rpath`, possibly named with `id`. See also
[`resolve_lx_input`](@ref). The commands `\\fig` and `\\figalt` should be preferred.
"""
function resolve_lx_input_plotoutput(rpath::AS, id::AS="")::String
    # will throw an error if rpath doesn't exist
    _, dir, fname = check_input_rpath(rpath)
    plt_name = fname * id
    # find a plt in output that has the same root name
    out_path = joinpath(dir, "output")
    isdir(out_path) || throw(ErrorException("I found an input plot but not output dir."))
    out_file = ""
    for (root, _, files) ∈ walkdir(out_path)
        for (f, e) ∈ splitext.(files)
            lc_e = lowercase(e)
            if f == plt_name && lc_e ∈ (".gif", ".jpg", ".jpeg", ".png", ".svg")
                out_file = unixify(joinpath("/assets", dirname(rpath), "output", plt_name * lc_e))
                break
            end
        end
        out_file == "" || break
    end
    # error if no file found
    out_file != "" || throw(ErrorException("I found an input plot but no relevant output plot."))
    # wrap it in img block
    return html_img(out_file)
end

"""
$(SIGNATURES)

Resolve a command of the form `\\input{code:julia}{path_to_script}` where `path_to_script` is a
valid subpath of `/assets/`. All paths should be expressed relatively to `/assets/` i.e.
`/assets/[subpath]/script.jl`. If things are not in `assets/`, start the path
with `../` to go up one level then go down for instance `../src/pages/script1.jl`.
Forward slashes should also be used on windows.

Different actions can be taken based on the first bracket:
1. `{julia}` or `{code:julia}` will insert the code in the script with appropriate highlighting
2. `{output}` or `{output:plain}` will look for an output file in `/scripts/output/path_to_script` and will just print the content in a non-highlighted code block
3. `{plot}` or `{plot:id}` will look for a displayable image file (gif, png, jp(e)g or svg) in
`/assets/[subpath]/script1` and will add an `img` block as a result. If an `id` is specified,
it will try to find an image with the same root name ending with `id.ext` where `id` can help
identify a specific image if several are generated by the script, typically a number will be used.
"""
function resolve_lx_input(lxc::LxCom)::String
    qualifier = lowercase(strip(content(lxc.braces[1])))  # `code:julia`
    rpath = strip(content(lxc.braces[2])) # [assets]/subpath/script{.jl}

    if occursin(":", qualifier)
        p1, p2 = split(qualifier, ":")
        # code:julia
        if p1 == "code"
            if p2 ∈ keys(CODE_LANG)
                # these are codes for which we know how they're commented and can use the
                # HIDE trick (see HIGHLIGHT and resolve_lx_input_hlcode)
                return resolve_lx_input_hlcode(rpath, p2)
            else
                # another language descriptor, let the user do that with highlights.js
                # note that the HIDE trick will not work here.
                return resolve_lx_input_othercode(rpath, qualifier)
            end
        # output:plain
        elseif p1 == "output"
            if p2 == "plain"
                return resolve_lx_input_plainoutput(rpath)
            # elseif p2 == "table"
            #     return resolve_lx_input_tableoutput(rpath)
            else
                throw(ArgumentError("I found an \\input but couldn't interpret \"$qualifier\"."))
            end
        # plot:id
        elseif p1 == "plot"
            return resolve_lx_input_plotoutput(rpath, p2)
        else
            throw(ArgumentError("I found an \\input but couldn't interpret \"$qualifier\"."))
        end
    else
        if qualifier == "output"
            return resolve_lx_input_plainoutput(rpath)
        elseif qualifier == "plot"
            return resolve_lx_input_plotoutput(rpath)
        elseif qualifier ∈ ("julia", "fortran", "julia-repl", "matlab", "r", "toml")
            return resolve_lx_input_hlcode(rpath, qualifier)
        else # assume it's another language descriptor, let the user do that with highlights.js
            return resolve_lx_input_othercode(rpath, qualifier)
        end
    end
end


text:
using Documenter, Conductor

makedocs(sitename="Conductor.jl",
         pages = ["Home" => "index.md"])

deploydocs(
    repo = "github.com/wsphillips/Conductor.jl.git",
    devbranch = "main")


text:
let
    @test LDrawParser.parse_meta_command(["0", "ROTSTEP", "END"]) == LDrawParser.ROTSTEP_END
    @test LDrawParser.parse_meta_command(["0", "ROTSTEP", ""]) == LDrawParser.ROTSTEP_BEGIN
    @test LDrawParser.parse_meta_command(["0", "STEP", ""]) == LDrawParser.STEP
end


text:
## Download and cache management utilities ##

const CACHE_DIRS = Dict{String,String}()

"Return or create local cache directory for a remote planning repository."
function get_cache!(repo::T) where {T <: PlanningRepository}
    return get!(CACHE_DIRS, string(T)) do
        get_scratch!(@__MODULE__, string(T))
    end
end

"Clear local cache directory associated with a remote planning repository."
function clear_cache!(repo::T) where {T <: PlanningRepository}
    delete_scratch!(@__MODULE__, string(T))
    delete!(CACHE_DIRS, string(T))
    return nothing
end

"Clear all local caches of remote planning repositories."
function clear_all_caches!()
    for repo in keys(CACHE_DIRS)
        delete_scratch!(@__MODULE__, repo)
    end
    empty!(CACHE_DIRS)
    return nothing
end

"Download `src` URL to `dst` path with a fixed timeout."
function timed_download(src, dst, timeout)
    tmp_path, tmp_io = mktemp()
    @info "Downloading $src to local cache..."
    Downloads.download(src, tmp_io, timeout=timeout)
    close(tmp_io)
    mkpath(dirname(dst))
    mv(tmp_path, dst)
end

"Query a JSON endpoint if a local cache is not available."
function cached_json_query(endpoint, cache)
    if isfile(cache)
        resp = open(cache) do f
            JSON.parse(f)
        end
    else
        @info "Downloading JSON response from $endpoint..."
        json = HTTP.get(endpoint).body |> String
        resp = JSON.parse(json)
        if get(resp, "error", false)
            error(resp["message"])
        end
        open(cache, "w") do f
            write(f, json)
        end
    end
    return resp
end


text:
# a sampler is either a probability distribution on the choice point, or a sampler that modifies other samplers.
abstract Sampler

#
# the expected interface of a sampler is:
#
# numparams(s::Sampler)
#		- the number parameters (including those of any samplers this sampler modifies)
# setparams!(s::Sampler, params::Vector{Real})
#		- updates the parameters of the sampler (if the sampler modifies another, this sets the parameter of both)
#		- if parameters pass checks on their number and ranges (e.g. using the checkparamranges function below), then
#		  the values should be accommodated in a sensible way even if they break other constraints (a warning may
#         still be raised, though); this is to facilate the use of search on the parameters
# paramranges(s::Sampler)
#		- returns an array specifying the valid ranges for each parameter of the sampler (if the sampler modifies others,
#         returns the parameter ranges of the modified sampler as well)
#		- the number and range of the parameter are assumed to remain constant even if the params are changed
# getparams(s::Sampler)
#		- returns the current parameters (currently used only to default parameters of a modifying sampler)
# sample(s::Sampler, support, cc::ChoiceContext)
#		- sample a Gödel number from the sampler
#		- samplers need not honour the support constraints; they are provided only to samplers that do take this into account
# estimateparams!(s::Sampler, traces)
#		- estiamte the parameters of the sampler from the passed traces
#
#
# note also that for consistency, samplers allow parameters to be specified in their constructor, but if
# the parameter array is empty, sensible defaults are set instead - however, this is not required
#

# it is assumed that the parameters ranges and number remains constant after construction, and so, these methods
# are provided for convenience:

numparams(s::Sampler) = length(paramranges(s))

# called from setparams to validate passed parameters
function checkparamranges(s::Sampler, params::Vector{Float64})
	pranges = paramranges(s)
	length(params) == length(pranges) || error("expected $(length(pranges)) parameters but got $(length(params))")
	for i in 1:length(pranges)
		prange = pranges[i]
		params[i] >= prange[1] || error("parameter $(i) must be >= $(prange[1]) but was $(params[i])")
		params[i] <= prange[2] || error("parameter $(i) must be <= $(prange[2]) but was $(params[i])")
	end
end

# identifies whether the sampler supports estimating the model, i.e. setting features other than the parameters, such
# as conditionality on other choice points or on the recursion depth (either of which may change the number of parameters)
supportsconditionalmodelestimation(s::Sampler) = method_exists(estimateconditionalmodel, (typeof(s), Any, Any, Any))

# supports pretty printing
function getsamplertypename(s::Sampler)
	samplertypename = "$(typeof(s))"
	if startswith(samplertypename, "DataGenerators.")
		samplertypename = samplertypename[length("DataGenerators.")+1:end]
	end
	samplertypename
end

include(joinpath("distribution_samplers", "distribution_sampler.jl"))
include(joinpath("modifying_samplers", "modifying_sampler.jl"))


text:
using Gtk

win = GtkWindow("BPM meter",100,50)
b1 = GtkButton("Tick")
hbox1 = GtkButtonBox(:h)
push!(hbox1, b1)
vbox = GtkBox(:v)
label0 = GtkLabel("BPM Meter")
label = GtkLabel("")
GAccessor.text(label,"0.")
push!(vbox, label0)
push!(vbox, label)
push!(vbox, hbox1)
push!(win, vbox)

text = "default text"
global t1 = 999999

function calculate_bpm()
    t2 = time()
    bpm = 60 / (t2-t1)
    global t1 = t2
    bpm = round(bpm, digits=2)
    return string(bpm)
end

function button_clicked_callback(widget)
    if widget == b1
       	global text = calculate_bpm()
        GAccessor.text(label, text)
    end
end

id1 = signal_connect(button_clicked_callback, b1, "clicked")

c = Condition()
endit(w) = notify(c)
signal_connect(endit, win, :destroy)
showall(win)
wait(c)

max_stars_repo_name: leroy-walton/playgrounds

max_issues_repo_name: leroy-walton/playgrounds

max_forks_repo_name: leroy-walton/playgrounds


text:
pids = addprocs(10)

@everywhere begin
    include("voiConsAuc.jl")
    f(n, p) = evsi_sim([1, 0], [1, .2], [n * p, n * (1 - p)], nsims = 10000000)
end

x = @parallel hcat for i = linspace(1, 50, 50)
  [f(i, j) for j = linspace(.99, .01, 50)]
end

rmprocs(pids)

writedlm("evsi2sim.csv", x, ',')


text:
#=

"""
   locf(vec [, fillback]) is "last observation carry forward"

Overwrite NaNs [Nulls] with the prior non-NaN [non-Null] values.

If fillback=true (default) and vec starts with NaNs [Nulls],
vec starts with NaNs [Nulls], those values will be overwritten
with the first non-NaN [non-Null] value.
"""
function locf(vec::AbstractFloatVec, fillback::Bool)
   idx = index_first_nonnan(vec)
   v = locf(vec)
   if fillback==false && idxhttps://github.com/JuliaArrays/EndpointRanges.jl > 1
      v[1:idx-1] = NaN
   end
   return v
end

function locf(vec::AbstractFloatVec)
   v = copy(vec)
   locf!(v)
   return v
end

"""
   locf!(vec [, fillback]) is "last observation carry forward" in place

Overwrite NaNs [Nulls] with the prior non-NaN [non-Null] values

If fillback=true (default) and vec starts with NaNs [Nulls],
those values will be overwritten with the first non-NaN [non-Null] value.
"""
function locf!(vec::AbstractFloatVec, fillback::Bool)
   idx = index_first_nonnan(vec)
   locf!(vec)
   if fillback==false && dx > 1
      vec[1:idx-1] = NaN
   end
   return nothing
end

function locf!(vec::AbstractFloatVec)
    n = length(vec)
    vecidxs = 1:n

    if isnan(vec[1])
       idx = index_first_nonnan(view(vec, vecidxs))
       if idx != 0
           vec[1:idx-1] = vec[idx]
       end
    end
    if isnan(vec[end])
       idx = index_final_nonnan(view(vec, vecidxs))
       if idx != 0
          vec[idx+1:end] = vec[idx]
       end
    end

    if any(isnan.(vec))
       n = length(vec)
       nans_at = index_nans(vec)
       if n > length(nans_at)
           vec[nans_at] = locf_values(view(vec, vecidxs), view(nans_at,1:length(nans_at)))
      end
    end

    return nothing
end

function locf_values(vec::AbstractFloatVec, nans_at::AbstractIntVec)
    augment = 0
    deltas = [nans_at[1]-1, diff(nans_at)...]

    for i in 2:length(deltas)
        if deltas[i] == 1
            augment += 1
            deltas[i] = 0
        else
            deltas[i] += augment
            augment = 0
        end
    end

    return cumsum(deltas)
end

function index_nans(vec::AbstractFloatVec)
    idxs = 1:length(vec)
    nans = map(isnan, view(vec,idxs))
    return idxs[nans]
end

"""
    index_first_nonnan(vec)

returns 0 iff all elements of vec are NaN
"""
function index_first_nonnan(vec::AbstractFloatVec)
   result = 0
   for i in 1:length(vec)
      if !isnan(vec[i])
         result = i
         break
      end
   end
   return result
end

"""
    index_final_nonnan(vec)

returns 0 iff all elements of vec are NaN
"""
function index_final_nonnan(vec::AbstractFloatVec)
   result = 0
   for i in length(vec):-1:1
      if !isnan(vec[i])
         result = i
         break
      end
   end
   return result
end

=#


text:
struct ALKernel
    nn_stability::Int
    stability_window::Int
    stability_threshold::Float64
    C_steps::Int
    max_iteration::Int
    initial_n::Int
    max_gamma::Float64
    adjusted::Bool
end

function ALKernel(;nn_stability, stability_window, stability_threshold, C_steps, max_iteration, initial_n, max_gamma, adjusted)
    ALKernel(nn_stability, stability_window, stability_threshold, C_steps, max_iteration, initial_n, max_gamma, adjusted)
end

function Base.string(m::ALKernel)
    return "ALKernel_nns=$(m.nn_stability)_adj=$(m.adjusted)"
end

function find_parameters(method::ALKernel, data, labels, oracle, solver, qm, seed)
    nnpool, gamma_history, stability_history, query_history = search_gamma(data, labels, oracle, method.nn_stability,
                                                                           method.stability_window, method.stability_threshold, method.max_iteration,
                                                                           method.initial_n, method.max_gamma, seed, method.adjusted)
    gamma = gamma_history.values[end]

    max_C = find_max_feasible_C(data, gamma, solver)
    min_C = find_min_feasible_C(data, gamma, solver)

    C_range, model_range = calculate_C_grid(data, labels, oracle, gamma, min_C, max_C, method.C_steps, solver)
    qm = calculate_qm(nnpool, data, labels, model_range, qm)
    qm_best_idx = findmax(qm)[2]
    C = C_range[qm_best_idx]
    return (gamma, C)
end

max_stars_repo_name: holtri/lama

max_issues_repo_name: holtri/lama

max_forks_repo_name: holtri/lama


text:
module ExtendedVariantModeTheoCrystal

using ..Ahorn, Maple

@mapdef Entity "ExtendedVariantMode/TheoCrystal" ExtendedVariantTheoCrystal(x::Integer, y::Integer, allowThrowingOffscreen::Bool=false, allowLeavingBehind::Bool=false)

const placements = Ahorn.PlacementDict(
    "Extended Variant Theo Crystal\n(Extended Variant Mode)" => Ahorn.EntityPlacement(
        ExtendedVariantTheoCrystal
    )
)

sprite = "characters/theoCrystal/idle00.png"

function Ahorn.selection(entity::ExtendedVariantTheoCrystal)
    x, y = Ahorn.position(entity)

    return Ahorn.getSpriteRectangle(sprite, x, y - 10)
end

Ahorn.render(ctx::Ahorn.Cairo.CairoContext, entity::ExtendedVariantTheoCrystal, room::Maple.Room) = Ahorn.drawSprite(ctx, sprite, 0, -10)

end


text:
using Sunny
using LinearAlgebra
using MPI

# MPI rank is 0-based index
rank, N_ranks = init_MPI()

kT_min = 0.1
kT_max = 10.0

# Geometric temperature schedule - make smallest β first entry
kT = kT_min * (kT_max/kT_min) ^ ((N_ranks-1-rank)/(N_ranks-1))

# Linear temperature schedule
#kT = collect(range(kT_min, kT_max, length=N_ranks))[N_ranks-rank]

# Nearest-neighbor ferromagnetic Ising (z-dir) interactions
J = -1.0
FM = exchange(diagm([J, J, J]), Bond(1, 1, [1, 0, 0]))
interactions = [FM]

# SC lattice -- extend lattice vector in z-direction to emulate 2D
lvecs = Sunny.lattice_vectors(1.0, 1.0, 2.0, 90, 90 ,90)
bvecs = [[0.0, 0.0, 0.0]]
crystal = Crystal(lvecs, bvecs)

# Make system and randomize spins
extent = (20, 20, 1)
g = 1 / Sunny.BOHR_MAGNETON
system = SpinSystem(crystal, interactions, extent, [SiteInfo(1,1,g)])
randflips!(system)

# Make replica for REMC
α = 1/kT
replica = Replica(IsingSampler(system, kT, 1), α)

# Define how the setting of α's changes
#  the sampling distribution or the system
function set_α!(replica::Replica, α::Float64)
    replica.α = α
    set_temp!(replica.sampler, 1/α)
end

# Run feedback-optimized replica exchange
α_opt = run_FBO!(
    replica,
    set_α!;
    max_mcs = 50_000,
    rex_interval = 1,
    update_interval = 10_000,
    w = 0.0
)

# Run Replica Exchange MC (as PT) using optimized α schedule
run_REMC!(
    replica;
    therm_mcs=5000,
    measure_interval=50,
    rex_interval=10,
    max_mcs=500_000,
    bin_size=1.0,
    print_hist=true
)


text:
using Documenter, FOOBAR

const PAGES = ["Home" => "index.md", "API" => "api.md"]

makedocs(;
    modules = [FOOBAR],
    format = Documenter.HTML(prettyurls = get(ENV, "GITHUB_ACTIONS", nothing) == "true"),
    pages = PAGES,
    sitename = "FOOBAR.jl",
    authors = "Colin Summers",
    clean = true,
    doctest = true,
    checkdocs = :exports,
    linkcheck = :true,
    linkcheck_ignore = [r"^https://github.com/Lyceum/.*/actions"],
)

deploydocs(repo = "github.com/Lyceum/FOOBAR.jl.git")


text:
# Mag
set!(res::MagLike, ::Irrational{:π}) = const_pi!(res)

# Arb
function set!(res::ArbLike, x::Union{MagLike,BigFloat})
    set!(radref(res), UInt64(0))
    set!(midref(res), x)
    return res
end

function set!(res::ArbLike, x::Rational; prec::Integer = precision(res))
    set!(res, numerator(x))
    return div!(res, res, denominator(x), prec = prec)
end

for (irr, suffix) in ((:π, "pi"), (:ℯ, "e"), (:γ, "euler"), (:catalan, "catalan"))
    jlf = Symbol("const_$suffix", "!")
    IrrT = Irrational{irr}
    @eval begin
        set!(res::ArbLike, ::$IrrT; prec::Integer = precision(res)) = $jlf(res, prec = prec)
    end
end

function set!(res::ArbLike, ::Irrational{:φ}; prec::Integer = precision(res))
    set!(res, 5)
    sqrt!(res, res, prec)
    add!(res, res, 1, prec)
    return div!(res, res, 2, prec)
end

# Acb
function set!(res::AcbLike, x::Union{Real,arf_struct,mag_struct})
    set!(realref(res), x)
    set!(imagref(res), 0)
    return res
end

function set!(
    res::AcbLike,
    re::Union{Real,arb_struct,arf_struct,mag_struct},
    im::Union{Real,arb_struct,arf_struct,mag_struct},
)
    set!(realref(res), re)
    set!(imagref(res), im)
    return res
end

set!(res::AcbLike, z::Complex) = set!(res, real(z), imag(z))
set!(res::AcbLike, ::Irrational{:π}) = const_pi!(res)


text:
import OrdinaryDiffEq
import QuantumOpticsBase

"""
    liouvilleVonNeumann(rho0, tspan, H;
    \treltol=1.0e-12, abstol=1.0e-12, fout=nothing, alg=OrdinaryDiffEq.Tsit5())

Integrate Liouville-von Neumann equation to evolve states or compute propagators

``\\frac{d}{d t} \\rho(t) = - \\frac{i}{\\hbar} [ \\hat H, \\rho(t) ], \\quad \\hbar = 1``.

# Arguments
* `rho0`: Initial state vector (can be a bra or a ket) or initial propagator.
* `tspan`: Vector specifying the points of time for which output should be displayed.
* `H`: Arbitrary operator specifying the Hamiltonian.
* `reltol`: Relative tolerance for OrdinaryDiffEq solver and its inner states.
* `abstol`: Absolute tolerance for OrdinaryDiffEq solver and its inner states.
* `fout=nothing`: If given, this function `fout(t, rho)` is called every time
        an output should be displayed. ATTENTION: The state `rho` is neither
        normalized nor permanent! It is still in use by the ode solver and
        therefore must not be changed.
* `alg`: Algorithm with which OrdinaryDiffEq will solve LvN equation.
"""
function liouvilleVonNeumann(
    rho0::T,
    tspan::Array,
    H::AbstractOperator{B,B};
    reltol::Float64 = 1.0e-12,
    abstol::Float64 = 1.0e-12,
    alg::OrdinaryDiffEq.OrdinaryDiffEqAlgorithm = OrdinaryDiffEq.DP5(),
    fout::Union{Function,Nothing} = nothing,
    kwargs...,
) where {B<:Basis,T<:Operator{B,B}}
    # tmp = copy(rho0)
    dliouvilleVonNeumann_(t, rho::T, drho::T) = dliouvilleVonNeumann(rho, H, drho)
    tspan_ = convert(Vector{float(eltype(tspan))}, tspan)
    x0 = rho0.data
    state = T(rho0.basis_l, rho0.basis_r, rho0.data)
    dstate = T(rho0.basis_l, rho0.basis_r, rho0.data)
    OpenQuantumSystems.integrate(
        tspan_,
        dliouvilleVonNeumann_,
        x0,
        state,
        dstate,
        fout;
        reltol = reltol,
        abstol = abstol,
        alg = alg,
        kwargs...,
    )
end

function dliouvilleVonNeumann(
    rho::T,
    H::AbstractOperator{B,B},
    drho::T,
) where {B<:Basis,T<:Operator{B,B}}
    QuantumOpticsBase.mul!(drho, H, rho, -eltype(rho)(im), zero(eltype(rho)))
    QuantumOpticsBase.mul!(drho, rho, H, eltype(rho)(im), one(eltype(rho)))
    return drho
end


text:
module Aligator

export aligator

using MacroTools
using Recurrences
using AlgebraicDependencies
using Singular
using AbstractAlgebra
using Nemo
using SymEngine

include("looptransform.jl")
include("extract.jl")
include("map.jl")
include("invariants.jl")

aligator(s::String) = aligator(Meta.parse(s))

function aligator(x::Expr)
    _, total = @timed begin
        (init, branches), etime = @timed transform(x)
        init = ValueMap(k=>v for (k,v) in init if v isa Int)
        vars = Base.unique(Iterators.flatten(map(Recurrences.symbols, branches)))
        @debug "Extracting branches" branches vars

        closedforms = extract(branches, init)
        @debug "Recurrence Systems" closedforms

        invs, itime = @timed invariants(closedforms, vars, init)
        @debug "Invariant ideal" invs
    end
    @debug "Time needed" total etime itime

    return InvariantIdeal(invs)
end

struct InvariantIdeal
    ideal::sideal
end

function Base.show(io::IO, I::InvariantIdeal)
    println(io, "Invariant ideal with $(Singular.ngens(I.ideal))-element basis:")
    Base.print_array(io, gens(I.ideal))
end

module Examples
include("../benchmark/singlepath.jl")
include("../benchmark/multipath.jl")
end

end # module


text:
# This file is a part of BAT.jl, licensed under the MIT License (MIT).

"""
    NamedTupleTransform <: VariateTransform

*BAT-internal, not part of stable public API.*
"""
struct NamedTupleTransform{
    names,
    ST <: VariateSpace,
    SF <: VariateSpace,
    N,
    TT <: NTuple{N,VariateTransform{<:VariateForm}},
    AT <: NTuple{N,ValueShapes.ValueAccessor},
} <: VariateTransform{ValueShapes.NamedTupleVariate{names},ST,SF}
    _internal_transforms::NamedTuple{names,TT}
    _internal_shape::NamedTupleShape{names,AT}
end

_nt_acc_nt_type(::NamedTupleShape{names,AT}) where {names,AT} = AT

function NamedTupleTransform(transforms::NamedTuple{names,<:NTuple{N,VariateTransform}}) where {names,N}
    trg = product_varspace(map(target_space, values(transforms))...)
    src = product_varspace(map(source_space, values(transforms))...)
    shape = NamedTupleShape(map(varshape, transforms))

    ST = typeof(trg)
    SF = typeof(src)
    TT = typeof(transforms)
    AT = _nt_acc_nt_type(shape)

    NamedTupleTransform{names,ST,SF,T,TT,AT}(transforms, shape)
end

@inline NamedTupleTransform(;named_dists...) = NamedTupleTransform(values(named_dists))

@inline _transforms(d::NamedTupleTransform) = getfield(d, :_internal_transforms)
@inline _shape(d::NamedTupleTransform) = getfield(d, :_internal_shape)

@inline Base.keys(d::NamedTupleTransform) = keys(_transforms(d))

@inline Base.values(d::NamedTupleTransform) = values(_transforms(d))

@inline function Base.getproperty(d::NamedTupleTransform, s::Symbol)
    # Need to include internal fields of NamedTupleShape to make Zygote happy:
    if s == :_internal_transforms
        getfield(d, :_internal_transforms)
    elseif s == :_internal_shape
        getfield(d, :_internal_shape)
    else
        getproperty(_transforms(d), s)
    end
end

@inline function Base.propertynames(d::NamedTupleTransform, private::Bool = false)
    names = propertynames(_transforms(d))
    if private
        (names..., :_internal_transforms, :_internal_shape)
    else
        names
    end
end

@inline Base.map(f, dist::NamedTupleTransform) = map(f, _transforms(dist))

Base.merge(a::NamedTuple, dist::NamedTupleTransform{names}) where {names} = merge(a, _transforms(dist))
Base.merge(a::NamedTupleTransform) = a
Base.merge(a::NamedTupleTransform, b::NamedTupleTransform, cs::NamedTupleTransform...) = merge(NamedTupleTransform(;a..., b...), cs...)

ValueShapes.varshape(trafo::NamedTupleTransform) = _shape(trafo)

target_space(trafo::NamedTupleTransform) = trafo.target_space
source_space(trafo::NamedTupleTransform) = MixedSpace

# trafo_fwd(trafo::VariateTransformChain, v::Any) = ...

# trafo_inv(trafo::VariateTransformChain, v::Any) = ...

# ToDo: Finish implementation


text:
export FourierGrid
export create_grid, getindex, size

struct FourierGrid{𝒢, 𝒲, 𝒟} <: AbstractGrid
    grid::𝒢
    wavenumbers::𝒲
    domain::𝒟
end

import Base: getindex
getindex(f::FourierGrid, i) = f.grid[i]

function FourierGrid(grid_points, Ω::IntervalDomain; arraytype = Array) # perhaps change to match the product domain
    @assert length(grid_points) == 1
    @assert Ω.periodic
    grid = arraytype(fourier_nodes(grid_points, a = Ω.a, b = Ω.b))
    wavenumbers = arraytype(fourier_wavenumbers(grid_points, L = Ω.b - Ω.a))
    return FourierGrid([grid], [wavenumbers], Ω)
end

"""
FourierGrid(grid_points, Ω::ProductDomain, arraytype=Array)
# Description
Create a numerical grid with grid_points resolution in the domain Ω \n
Only works for fully periodic grids at the moment
# Arguments
- `grid_points`: tuple | a tuple of ints in each direction for product domain
- `Ω`: ProductDomain   | a product domain object
# Keyword Arguments
- arraytype = Array
# Return
A Fourier Grid object
"""
function FourierGrid(grid_points, Ω::ProductDomain; arraytype=Array) # change to be fully general later
    @assert length(grid_points) == length(Ω.domains)
    grid = []
    wavenumbers = []
    @assert check_full_periodicity(Ω)
    for (i,domain) in enumerate(Ω.domains)
        L = domain.b - domain.a
        reshape_dims = appropriate_dims(length(Ω.domains), i, grid_points[i])
        push!(grid, arraytype(reshape(fourier_nodes(grid_points[i], a = domain.a, b = domain.b), reshape_dims)))
        push!(wavenumbers, arraytype(reshape(fourier_wavenumbers(grid_points[i], L = L), reshape_dims)))
    end
    return FourierGrid(grid, wavenumbers, Ω)
end

function Base.show(io::IO, F::FourierGrid)
    println("domain:", F.domain)
    print("gridpoints:")
    if typeof(F.grid[1]) <: AbstractArray
        for (i, grid) in enumerate(F.grid)
            print(length(grid))
            if i != length(F.grid)
                printstyled(io, "×")
            end
        end
    else
        print(length(F.grid))
    end
 end

 size(f::FourierGrid) = length.(f.grid)

max_stars_repo_name: upiterbarg/QuasiGeostrophy

max_issues_repo_name: upiterbarg/QuasiGeostrophy

max_forks_repo_name: upiterbarg/QuasiGeostrophy


text:
module PkgVerifierPrototype

export create_signature_repository, create_user, create_user_certificate, verify_user_access, get_user_certificate, construct_data_certificate, open_data_certificate, traverse_dir

import TweetNaCl
import JSON
import SHA
import Requests

function create_signature_repository(path::AbstractString)
    if ispath(path)
        if !isdir(path)
            error("File exists at specified location.")
        elseif !isempty(readdir(path))
            error("Directory is not empty.")
        end
    else
        mkdir(path)
    end

    mkdir(joinpath(path, "masterkeys"))
    mkdir(joinpath(path, "userkeys"))
    mkdir(joinpath(path, "repo"))
    mkdir(joinpath(path, "repo", "certificates"))

    master_pk, master_sk = TweetNaCl.crypto_sign_keypair()

    secret_key_json = JSON.json(
             Dict([(:public, base64encode(master_pk)),
                   (:secret, base64encode(master_sk))]))

    fh = open(joinpath(path, "masterkeys", "secret.key"), "w")
    print(fh, secret_key_json)
    close(fh)
end

function is_valid_repository(path::AbstractString)
    if !isdir(path)
        return false
    end

    if !isdir(joinpath(path, "masterkeys"))
        return false
    end

    if !isfile(joinpath(path, "masterkeys", "secret.key"))
        return false
    end

    if !isdir(joinpath(path, "repo"))
        return false
    end

    if !isdir(joinpath(path, "repo", "certificates"))
        return false
    end

    return true

end

function create_user(path::AbstractString, user::AbstractString)
    if !is_valid_repository(path)
        error("Path is not a valid signature repository.")
    end

    user_pk, user_sk = TweetNaCl.crypto_sign_keypair()

    secret_key_json = JSON.json(
             Dict([(:public, base64encode(user_pk)),
                   (:secret, base64encode(user_sk))]))

    fh = open(joinpath(path, "userkeys", "$user.key"), "w")
    print(fh, secret_key_json)
    close(fh)
end

function create_user_certificate(path::AbstractString,
                                 user::AbstractString,
                                 directories)

    user_keys = get_user_keys(path, user)
    master_keys = get_master_keys(path)

    certificate = Dict([(:user, base64encode(user_keys[1])),
                        (:name, user),
                        (:directories, directories)])
    certificate_json = JSON.json(certificate)
    certificate_signed = base64encode(TweetNaCl.crypto_sign(
                                           certificate_json, master_keys[2]))

    certificate_wrapper = JSON.json(
           Dict([(:authority, base64encode(master_keys[1])),
                 (:certificate, certificate_signed)]))

    fh = open(joinpath(path, "repo", "certificates", "$user.cert"), "w")
    print(fh, certificate_wrapper)
    close(fh)
end

function get_master_keys(path::AbstractString)
    fh = open(joinpath(path, "masterkeys", "secret.key"))
    master_keys_json = readall(fh)
    close(fh)

    json_parsed = JSON.parse(master_keys_json)
    return base64decode(json_parsed["public"]),
           base64decode(json_parsed["secret"])
end

function get_user_keys(path::AbstractString, user::AbstractString)
    filename = joinpath(path, "userkeys", "$user.key")
    fh = open(filename)
    keys_json = readall(fh)
    close(fh)

    json_parsed = JSON.parse(keys_json)
    return base64decode(json_parsed["public"]),
           base64decode(json_parsed["secret"])
end

function get_user_permissions_from_certificate(path::AbstractString, certificate_wrapped::Dict)

    master_pk, _ = get_master_keys(path)

    claimed_authority = base64decode(certificate_wrapped["authority"])
    if master_pk != claimed_authority
        error("Invalid certificate authority.")
    end

    certificate_json = TweetNaCl.crypto_sign_open(
                             base64decode(certificate_wrapped["certificate"]),
                             master_pk)

    certificate = JSON.parse(ASCIIString(certificate_json))
    return base64decode(certificate["user"]), certificate["name"], certificate["directories"]
end

function get_user_permissions(path::AbstractString, user::AbstractString)
    user_pk, user_name, directories = get_user_permissions_from_certificate(
                                             path,
                                             get_user_certificate(path, user))
    if user_name != user
        error("User name does not match certificate.")
    end
    return user_pk, user_name, directories
end

function get_user_certificate(path::AbstractString, user::AbstractString)

    certificate_filename = joinpath(path, "repo", "certificates", "$user.cert")
    fh = open(certificate_filename)
    certificate_wrapped_json = readall(fh)
    close(fh)

    return JSON.parse(certificate_wrapped_json)

end

function verify_user_access(path::AbstractString,
                            user::AbstractString,
                            target::AbstractString)

    _, _, allowable_directories = get_user_permissions(path, user)
    return verify_access(target, allowable_directories)
end

function verify_access(target::AbstractString,
                       allowable_directories::Array)

    stripped_target = strip(target)

    for dir in allowable_directories

        if dir[end] != '/'
            dir = "$dir/"
        end

        if dir[1] != '/'
            dir = "/$dir"
        end

        if length(dir) > length(stripped_target)
            continue
        end

        if stripped_target[1:length(dir)] == dir
            return true
        end
    end

    return false
end

function construct_data_certificate(repo::AbstractString,
                                    user::AbstractString,
                                    data::AbstractString)

    user_pk, user_sk = get_user_keys(repo, user)

    user_certificate = get_user_certificate(repo, user)

    certificate = Dict([(:signer, base64encode(user_pk)),
                        (:certificate, user_certificate),
                        (:data, base64encode(
                                    TweetNaCl.crypto_sign(data, user_sk)))])

    return JSON.json(certificate)
end

function open_data_certificate(repo::AbstractString,
                               certificate_json::AbstractString)

    certificate = JSON.parse(certificate_json)
    user_pk, user_name, permissions = get_user_permissions_from_certificate(
                                  repo, certificate["certificate"])

    if user_pk != base64decode(certificate["signer"])
        error("Certificate public key does not match.")
    end

    return ASCIIString(TweetNaCl.crypto_sign_open(base64decode(certificate["data"]), user_pk))
end

function hash_dir(dir::AbstractString, dir_relpath::AbstractString = "")
    const to_ignore = [".git"]

    results = []

    contents = sort(readdir(dir))
    for file in contents
        path = joinpath(dir, file)
        relpath = joinpath(dir_relpath, file)

        if isfile(path)
            # Find hash
            open(path, "r") do f
                hash = SHA.sha512(f)
                push!(results, [relpath, hash, "SHA-512"])
            end

        elseif isdir(path)
            if !(file in to_ignore)
                results = vcat(results, hash_dir(path, relpath))
            end
        end
    end

    return results
end

function hash_pkg(pkg::AbstractString)
    dir = Pkg.dir(pkg)
    if isdir(dir)
        return hash_dir(dir, "/$pkg")
    else
        error("Package directory does not exist.")
    end
end

function construct_package_certificate(repo::AbstractString,
                                       user::AbstractString,
                                       pkg::AbstractString)

    hashes = hash_pkg(pkg)
    return construct_data_certificate(repo, user, JSON.json(Dict([
               ("package", pkg), ("hashes", hashes)])))
end

function verify_package_certificate(repo::AbstractString,
                                    certificate::AbstractString)

    contents = JSON.parse(open_data_certificate(repo, certificate))

    user_certificate = JSON.parse(certificate)["certificate"]
    user, username, permissions = get_user_permissions_from_certificate(
                                                   repo,
                                                   user_certificate)

    for hash in contents["hashes"]
        if !verify_access(hash[1], permissions)
            error("Package verification failed: permission error.")
        end
    end

    hashes_local = hash_pkg(contents["package"])
    if hashes_local != contents["hashes"]
        error("Package verification failed: hash error.")
    end
end

function get_package_certificate_from_url(repo::AbstractString,
                                          package::AbstractString,
                                          url::AbstractString)

    response = Requests.post("$url/cert/$package")
    if response.status != 200
        error("Failed to retrieve certificate.")
    end

    cert = ASCIIString(response.data)
    if cert == "\"FAILURE\""
        error("Package unavailable.")
    end

    return cert
end

function verify_package_from_url(repo::AbstractString,
                                 package::AbstractString,
                                 url::AbstractString)

    cert = get_package_certificate_from_url(repo, package, url)

    return verify_package_certificate(repo, cert)
end

function sign_package_to_url(repo::AbstractString,
                             user::AbstractString,
                             package::AbstractString,
                             url::AbstractString)

    cert = construct_package_certificate(repo, user, package)

    response = Requests.post("$url/push/$package", data=cert)

    if response.status != 200
        error("Failed to push certificate.")
    end

    response_data = ASCIIString(response.data)
    if response_data != "\"SUCCESS\""
        error("Certificate refused.")
    end
end

end # module

max_stars_repo_name: LachlanGunn/PkgVerifierPrototype

max_issues_repo_name: LachlanGunn/PkgVerifierPrototype

max_forks_repo_name: LachlanGunn/PkgVerifierPrototype


text:
using Pkg;
Pkg.add("OrdinaryDiffEq")
Pkg.add("Optim")
Pkg.add("PackageCompiler")

using PackageCompiler;
PackageCompiler.create_sysimage([:OrdinaryDiffEq, :Optim]; sysimage_path="da-julia.so")

max_stars_repo_name: Typas/Data-Assimilation-Project

max_issues_repo_name: Typas/Data-Assimilation-Project

max_forks_repo_name: Typas/Data-Assimilation-Project


text:
using HDF5
using Test
using LinearAlgebra: norm

@testset "readremote" begin

# check that we can read the official HDF5 example files

# download and save test file via:
# urlbase = "https://support.hdfgroup.org/ftp/HDF5/examples/files/exbyapi/"
test_files = joinpath(@__DIR__, "test_files")
# if !isdir(test_files)
#     mkdir(test_files)
# end
# function joinpath(test_files, name)
#     file = joinpath(test_files, name)
#     if !isfile(file)
#         file = download(urlbase*name, file)
#     end
#     file
# end

fcmp = [0 1 2 3 4 5 6;
    2 1.66667 2.4 3.28571 4.22222 5.18182 6.15385;
    4 2.33333 2.8 3.57143 4.44444 5.36364 6.30769;
    6 3 3.2 3.85714 4.66667 5.54545 6.46154]'
icmp = [0 -1 -2 -3 -4 -5 -6;
    0 0 0 0 0 0 0;
    0 1 2 3 4 5 6;
    0 2 4 6 8 10 12]'
SOLID, LIQUID, GAS, PLASMA = 0, 1, 2, 3
ecmp = [SOLID SOLID SOLID SOLID SOLID SOLID SOLID;
        SOLID LIQUID GAS PLASMA SOLID LIQUID GAS;
        SOLID GAS SOLID GAS SOLID GAS SOLID;
        SOLID PLASMA GAS LIQUID SOLID PLASMA GAS]'
scmp = ["Parting", "is such", "sweet", "sorrow."]
vicmp = Array{Int32}[[3, 2, 1],[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]]
opq = Array{UInt8}[[0x4f, 0x50, 0x41, 0x51, 0x55, 0x45, 0x30],
                   [0x4f, 0x50, 0x41, 0x51, 0x55, 0x45, 0x31],
                   [0x4f, 0x50, 0x41, 0x51, 0x55, 0x45, 0x32],
                   [0x4f, 0x50, 0x41, 0x51, 0x55, 0x45, 0x33]]
# For H5T_ARRAY
AA = Array{Int,2}[
    [0   0   0;
     0  -1  -2;
     0  -2  -4;
     0  -3  -6;
     0  -4  -8],
    [0   1   2;
     1   1   1;
     2   1   0;
     3   1  -1;
     4   1  -2],
    [0   2   4;
     2   3   4;
     4   4   4;
     6   5   4;
     8   6   4],
    [0   3   6;
     3   5   7;
     6   7   8;
     9   9   9;
     12  11  10]]

file = joinpath(test_files, "h5ex_t_floatatt.h5")
fid = h5open(file, "r")
dset = fid["DS1"]
a = a_read(dset, "A1")
@test norm(a - fcmp) < 1.5e-5
close(fid)

file = joinpath(test_files, "h5ex_t_float.h5")
fid = h5open(file, "r")
d = read(fid, "DS1")
@test norm(d - fcmp) < 1.5e-5
close(fid)

file = joinpath(test_files, "h5ex_t_intatt.h5")
fid = h5open(file, "r")
dset = fid["DS1"]
a = a_read(dset, "A1")
@test a == icmp
close(fid)

file = joinpath(test_files, "h5ex_t_int.h5")
fid = h5open(file, "r")
d = read(fid, "DS1")
@test d == icmp
close(fid)

if HDF5.h5_get_libversion() >= v"1.8.11"
  file = joinpath(test_files, "h5ex_t_enumatt.h5")
  fid = h5open(file, "r")
  dset = fid["DS1"]
  a = a_read(dset, "A1")
  @test a == ecmp
  close(fid)

  file = joinpath(test_files, "h5ex_t_enum.h5")
  fid = h5open(file, "r")
  d = read(fid, "DS1")
  @test d == ecmp
  close(fid)
end

file = joinpath(test_files, "h5ex_t_objrefatt.h5")
fid = h5open(file, "r")
dset = fid["DS1"]
a = a_read(dset, "A1")
g = fid[a[1]]
@test isa(g, HDF5.Group)
ds2 = fid[a[2]]
ds2v = read(ds2)
@test isa(ds2v, HDF5.EmptyArray{Int32})
@test isempty(ds2v)
close(fid)

file = joinpath(test_files, "h5ex_t_objref.h5")
fid = h5open(file, "r")
d = read(fid, "DS1")
g = fid[d[1]]
@test isa(g, HDF5.Group)
ds2 = fid[d[2]]
ds2v = read(ds2)
@test isa(ds2v, HDF5.EmptyArray{Int32})
@test isempty(ds2v)
close(fid)

file = joinpath(test_files, "h5ex_t_stringatt.h5")
fid = h5open(file, "r")
dset = fid["DS1"]
a = a_read(dset, "A1")
@test a == scmp
close(fid)

file = joinpath(test_files, "h5ex_t_string.h5")
fid = h5open(file, "r")
d = read(fid, "DS1")
@test d == scmp
close(fid)

file = joinpath(test_files, "h5ex_t_vlenatt.h5")
fid = h5open(file, "r")
dset = fid["DS1"]
a = a_read(dset, "A1")
@test a == vicmp
close(fid)

file = joinpath(test_files, "h5ex_t_vlen.h5")
fid = h5open(file, "r")
d = read(fid, "DS1")
@test d == vicmp
close(fid)

file = joinpath(test_files, "h5ex_t_vlstringatt.h5")
fid = h5open(file, "r")
dset = fid["DS1"]
a = a_read(dset, "A1")
@test a == scmp
close(fid)

file = joinpath(test_files, "h5ex_t_vlstring.h5")
fid = h5open(file, "r")
d = read(fid, "DS1")
@test d == scmp
close(fid)

file = joinpath(test_files, "h5ex_t_opaqueatt.h5")
fid = h5open(file, "r")
dset = fid["DS1"]
a = a_read(dset, "A1")
@test a.tag == "Character array"
@test a.data == opq
close(fid)

file = joinpath(test_files, "h5ex_t_opaque.h5")
fid = h5open(file, "r")
d = read(fid, "DS1")
@test d.tag == "Character array"
@test d.data == opq
close(fid)

file = joinpath(test_files, "h5ex_t_array.h5")
fid = h5open(file, "r")
A = read(fid, "DS1")
@test A == AA
close(fid)

end # testset readremote


text:
module SymbolicML

using CSV
using DataFrames
using Plots
using StatsPlots

include("dataset.jl")
include("functions.jl")

export discrete_derivative, discrete_derivatives
export siemensDataInfo, siemensData

export select_attributes
export abs_energy
export absolute_maximum
export absolute_sum_of_changes
export agg_autocorrelation
export autocorrelation
export benford_correlation
export binned_entropy
export c3
export change_quantiles
export cid_ce
export count_above
export count_above_mean
export count_below
export count_below_mean
export first_location_of_maximum
export first_location_of_minimum
export has_duplicate
export has_duplicate_max
export has_duplicate_min
export large_standard_deviation
export last_location_of_maximum
export last_location_of_minimum
export mean_abs_change
export mean_change
export mean_n_absolute_max
export longest_strike_above_mean
export longest_strike_below_mean
export mean_second_derivative_central
export number_crossing_m
export percentage_of_reoccurring_datapoints_to_all_datapoints
export percentage_of_reoccurring_values_to_all_values
export range_count
export ratio_beyond_r_sigma
export ratio_value_number_to_time_series_length
export root_mean_square
export sample_entropy
export sum_of_reoccurring_data_points

end


text:
"""
Compute fibonacci numbers by naive recursion... on the GPU. Probably the least
watt efficient calculation possible.
"""

include("../ragamuffin.jl")

function fib(N)
    if 1 <= N <= 2
        return 1
    end
    return fib(N - 1) + fib(N - 2)
end

function expand_helper(x :: Cell{T}, n) :: Cell{T} where T
    return Cell{T}(x.value - n, x.depth, x.output_index)
end

function ragamuffin_fib(min_N, max_N)
    T = Int32
    program = initialize(collect(Int32(i) for i in min_N:max_N), -Int32(1), CuFrame, 8*1024, Int)
    # NB: the true condition for a branching function cannot intersect null detection.
    # Here, our default value (for null objects) is -1, so 2 < x.value is fine, but
    # 2 >= x.value isn't. Any condition can be negated and the destinations swapped.
    add!(program, :root, (:branch, :increment), value_function = x -> 2 < x.value)
    add!(program, :branch, :root, value_function = (expand_helper, collect(1:2)))
    add!(program, :increment, :end, dest_function = x -> 1)

    @time run(program)
    return program.dest
end
max_N = 18
expected = @time collect(fib(i) for i in 1:max_N)
batched = ragamuffin_fib(1, max_N)
@info batched
@assert expected == batched


text:
module Injection

using PilePenetration
using PoingrSimulator.Poingr
using PoingrSimulator.GeometricObjects

function main_output_initialize(args)
    INPUT = args.INPUT

    history_file = joinpath(INPUT.Output.directory, "history.csv")
    PilePenetration.outputhistory_head(history_file)
end

function main_output(args)
    grid         = args.grid
    pointstate   = args.pointstate
    pile         = args.rigidbody
    pile0        = args.rigidbody0
    INPUT        = args.INPUT

    history_file = joinpath(INPUT.Output.directory, "history.csv")
    force_inside_directory = joinpath(INPUT.Output.directory, "force inside directory")
    force_outside_directory = joinpath(INPUT.Output.directory, "force outside directory")

    tip_height = 2 * pile[1][1]              # 1D = 2R
    tapered_height = pile[2][2] - pile[3][2] # tapered length
    if haskey(INPUT, :OutputHistory)
        if haskey(INPUT.OutputHistory, :tip_height)
            tip_height = oftype(tip_height, INPUT.OutputHistory.tip_height)
        end
        if haskey(INPUT.OutputHistory, :tapered_height)
            tapered_height = oftype(tapered_height, INPUT.OutputHistory.tapered_height)
        end
    end

    PilePenetration.outputhistory_append(
        history_file,
        grid,
        pointstate,
        pile,
        tip_height,
        tapered_height,
        sum(layer -> layer.thickness, INPUT.SoilLayer),
        centroid(pile0),
    )

    if args.INPUT.Pile.vacuum
        vacuum_height = INPUT.Pile.vacuum_height
        pile_inside = Polygon(Vec(0.0, pile[1][2]), Vec(0.0, pile[3][2]), pile[3], pile[2], pile[1])
        inds = findall(xₚ -> (xₚ in pile_inside) && xₚ[2] > (PilePenetration.tip_height(pile) + vacuum_height), pointstate.x)
        deleteat!(pointstate, inds)
    end
end

end


text:
using LinearAlgebra
using CVChannel
using Test

"""
This verifies the multiplicativity of the [dephrasure channel](https://arxiv.org/abs/1806.08327)

The dephrasure channel is parameterized by two parameters. We scan over these parameters
and check that it is multiplicative at each point.
...
"""

println("\nVerifying the multiplicativity of channel value for dephrasure channel...")

@testset "Verifying Multiplicativity of Dephrasure Channel" begin
    println("We are going to check the multiplicativity when p,q in {0,0.1,...0.9,1}.")
    p_vals = [0.0:0.1:1;]; q_vals = [0.0:0.1:1;];
    pCtr = 0; qCtr = 0;
    max_diff = 0;
    for q_id in q_vals
        print("\nStarting q= ", q_id,". \n")
        orig_cv = 2 - q_id
        for p_id in p_vals
            #Get Data
            dephrasurepq(ρ) = dephrasureChannel(ρ,p_id,q_id)
            orig_choi = choi(dephrasurepq,2,3);
            par_choi = permuteSubsystems(kron(orig_choi,orig_choi),[1,3,2,4],[2,3,2,3]);

            #This guarantees upper bound which is important since the claim
            #is (effectively) cv = cv_ppt
            par_cv, opt1, opt2 = pptCVDual(par_choi,4,9);

            diff = par_cv - orig_cv^2
            abs(diff) > max_diff ? max_diff = abs(diff) : nothing

            #Iterate
            pCtr = pCtr + 1
        end
        qCtr += 1
        pCtr = 0
    end

    println("The difference between single copy channel value and parallel is upper bounded by:", max_diff)
    @test max_diff <= 5e-6
end

max_forks_repo_name: ChitambarLab/cv-channel