text:
# Thanks to load order issues, we have to do the documentation seperately

const README, compatability =
    let readme=read("$(dirname(dirname(@__DIR__)))/README.org", String)
        prolouge=match(r"\n([^#\n][\s\S]*)\n\* Progress", readme).captures[1]
        progress=match(r"\n\* Progress\s*((?:\|[^\n]+\n)+)", readme).captures[1]
        Base.Docs.doc!(@__MODULE__,
                       Base.Docs.Binding(@__MODULE__, Symbol(@__MODULE__)),
                       Base.Docs.docstr(parse(OrgDoc, prolouge),
                                        Dict(:path => joinpath(@__DIR__, @__FILE__),
                                             :linenumber => @__LINE__,
                                             :module => @__MODULE__)),
                       Union{})
        (parse(OrgDoc, readme),
         parse(OrgDoc,
               replace(replace(replace(progress,
                                       "| X " => "| =✓="),
                               r"\| +\|" => "| ~⋅~ |"),
                       r"\| +\|" => "| ~⋅~ |")))
    end

@doc org"""
#+begin_src julia
@org_str -> Union{OrgDoc, Component}
#+end_src

Parse the string as Org, sepecifically to ~OrgDoc~ by default.

This supports a /type flag/ listed after the ending quote,
e.g. src_julia{org"A paragraph"p}.
This sets the type of object the string is parsed to.

The following type flags are currently supported:
- =d=, ~OrgDoc~ (/default/)
- =e=, ~Element~
- =o=, ~Object~
- =h=, ~Heading~
- =s=, ~Section~
- =p=, ~Paragraph~
- =ts=, ~Timestamp~
- =m=, ~TextMarkup~
- =t=, ~TextPlain~
""" :@org_str

@doc org"""
#+begin_src julia
orgmatcher(::Type{C}) where {C <: Component}
#+end_src

Return a /matcher/ for components of type ~C~.
This will either be:
+ nothing, if no matcher is defined
+ a regular expression which matcher the entire component
+ a function which takes a string and returns either
  - nothing, if the string does not start with the component
  - the substring which has been identified as an instance of the component
  - a tuple of the substring instance of the component, and the component data structure
""" orgmatcher

@doc org"""
#+begin_src julia
consume(component::Type{<:Component}, text::SubString{String})
#+end_src
Try to /consume/ a ~component~ from the start of ~text~.

Returns a tuple of the consumed text and the resulting component
or =nothing= if this is not possible.
""" consume

@doc org"""
An ~Org~ wrapper type, which when ~iterated~ over yeilds
each component of the ~Org~ document.
""" OrgIterator

@doc org"""
An ~Org~ wrapper type, which when ~iterated~ over yeilds
each element of the ~Org~ document.
""" OrgElementIterator


text:
module TestComponents
using Dash
export html_div, html_a, dcc_input, dcc_graph, html_h1, html_img

function html_div(; kwargs...)
    available_props = Set(Symbol[:children, :id, :n_clicks, :n_clicks_timestamp, :key, :role, :accessKey, :className, :contentEditable, :contextMenu, :dir, :draggable, :hidden, :lang, :spellCheck, :style, :tabIndex, :title, :loading_state])
    wild_props = Set(Symbol[Symbol("data-"), Symbol("aria-")])
    wild_regs = r"^(?<prop>data-|aria-)"

    result = Component("Div", "dash_html_components", Dict{Symbol, Any}(), available_props, Set(Symbol[Symbol("data-"), Symbol("aria-")]))

    for (prop, value) = pairs(kwargs)
        m = match(wild_regs, string(prop))
        if (length(wild_props) == 0 || isnothing(m)) && !(prop in available_props)
            throw(ArgumentError("Invalid property $(string(prop)) for component " * "html_div"))
        end

        push!(result.props, prop => Front.to_dash(value))
    end

return result
end

function html_div(children::Any; kwargs...)
result = html_div(;kwargs...)
push!(result.props, :children => Front.to_dash(children))
return result
end

html_div(children_maker::Function; kwargs...) = html_div(children_maker(); kwargs...)

function html_h1(; kwargs...)
    available_props = Set(Symbol[:children, :id, :n_clicks, :n_clicks_timestamp, :key, :role, :accessKey, :className, :contentEditable, :contextMenu, :dir, :draggable, :hidden, :lang, :spellCheck, :style, :tabIndex, :title, :loading_state])
    wild_props = Set(Symbol[Symbol("data-"), Symbol("aria-")])
    wild_regs = r"^(?<prop>data-|aria-)"

    result = Component("H1", "dash_html_components", Dict{Symbol, Any}(), available_props, Set(Symbol[Symbol("data-"), Symbol("aria-")]))

    for (prop, value) = pairs(kwargs)
        m = match(wild_regs, string(prop))
        if (length(wild_props) == 0 || isnothing(m)) && !(prop in available_props)
            throw(ArgumentError("Invalid property $(string(prop)) for component " * "html_h1"))
        end

        push!(result.props, prop => Front.to_dash(value))
    end

return result
end

function html_h1(children::Any; kwargs...)
result = html_h1(;kwargs...)
push!(result.props, :children => Front.to_dash(children))
return result
end

html_h1(children_maker::Function; kwargs...) = html_h1(children_maker(); kwargs...)

function html_img(; kwargs...)
    available_props = Set(Symbol[:children, :id, :n_clicks, :n_clicks_timestamp, :key, :role, :alt, :crossOrigin, :height, :sizes, :src, :srcSet, :useMap, :width, :accessKey, :className, :contentEditable, :contextMenu, :dir, :draggable, :hidden, :lang, :spellCheck, :style, :tabIndex, :title, :loading_state])
    wild_props = Set(Symbol[Symbol("data-"), Symbol("aria-")])
    wild_regs = r"^(?<prop>data-|aria-)"

    result = Component("Img", "dash_html_components", Dict{Symbol, Any}(), available_props, Set(Symbol[Symbol("data-"), Symbol("aria-")]))

    for (prop, value) = pairs(kwargs)
        m = match(wild_regs, string(prop))
        if (length(wild_props) == 0 || isnothing(m)) && !(prop in available_props)
            throw(ArgumentError("Invalid property $(string(prop)) for component " * "html_img"))
        end

        push!(result.props, prop => Front.to_dash(value))
    end

return result
end

function html_img(children::Any; kwargs...)
result = html_img(;kwargs...)
push!(result.props, :children => Front.to_dash(children))
return result
end

html_img(children_maker::Function; kwargs...) = html_img(children_maker(); kwargs...)

function html_a(; kwargs...)
    available_props = Set(Symbol[:children, :id, :n_clicks, :n_clicks_timestamp, :key, :role, :download, :href, :hrefLang, :media, :rel, :shape, :target, :accessKey, :className, :contentEditable, :contextMenu, :dir, :draggable, :hidden, :lang, :spellCheck, :style, :tabIndex, :title, :loading_state])
    wild_props = Set(Symbol[Symbol("data-"), Symbol("aria-")])
    wild_regs = r"^(?<prop>data-|aria-)"

    result = Component("A", "dash_html_components", Dict{Symbol, Any}(), available_props, Set(Symbol[Symbol("data-"), Symbol("aria-")]))

    for (prop, value) = pairs(kwargs)
        m = match(wild_regs, string(prop))
        if (length(wild_props) == 0 || isnothing(m)) && !(prop in available_props)
            throw(ArgumentError("Invalid property $(string(prop)) for component " * "html_a"))
        end

        push!(result.props, prop => Front.to_dash(value))
    end

return result
end

function html_a(children::Any; kwargs...)
result = html_a(;kwargs...)
push!(result.props, :children => Front.to_dash(children))
return result
end

html_a(children_maker::Function; kwargs...) = html_a(children_maker(); kwargs...)

function dcc_input(; kwargs...)
    available_props = Set(Symbol[:id, :value, :style, :className, :debounce, :type, :autoComplete, :autoFocus, :disabled, :inputMode, :list, :max, :maxLength, :min, :minLength, :multiple, :name, :pattern, :placeholder, :readOnly, :required, :selectionDirection, :selectionEnd, :selectionStart, :size, :spellCheck, :step, :n_submit, :n_submit_timestamp, :n_blur, :n_blur_timestamp, :loading_state, :persistence, :persisted_props, :persistence_type])
    wild_props = Set(Symbol[])
    wild_regs = r"^(?<prop>)"

    result = Component("Input", "dash_core_components", Dict{Symbol, Any}(), available_props, Set(Symbol[]))

    for (prop, value) = pairs(kwargs)
        m = match(wild_regs, string(prop))
        if (length(wild_props) == 0 || isnothing(m)) && !(prop in available_props)
            throw(ArgumentError("Invalid property $(string(prop)) for component " * "dcc_input"))
        end

        push!(result.props, prop => Front.to_dash(value))
    end

return result
end

function dcc_input(children::Any; kwargs...)
result = dcc_input(;kwargs...)
push!(result.props, :children => Front.to_dash(children))
return result
end

dcc_input(children_maker::Function; kwargs...) = dcc_input(children_maker(); kwargs...)

function dcc_graph(; kwargs...)
    available_props = Set(Symbol[:id, :responsive, :clickData, :clickAnnotationData, :hoverData, :clear_on_unhover, :selectedData, :relayoutData, :extendData, :restyleData, :figure, :style, :className, :animate, :animation_options, :config, :loading_state])
    wild_props = Set(Symbol[])
    wild_regs = r"^(?<prop>)"

    result = Component("Graph", "dash_core_components", Dict{Symbol, Any}(), available_props, Set(Symbol[]))

    for (prop, value) = pairs(kwargs)
        m = match(wild_regs, string(prop))
        if (length(wild_props) == 0 || isnothing(m)) && !(prop in available_props)
            throw(ArgumentError("Invalid property $(string(prop)) for component " * "dcc_graph"))
        end

        push!(result.props, prop => Front.to_dash(value))
    end

return result
end

function dcc_graph(children::Any; kwargs...)
result = dcc_graph(;kwargs...)
push!(result.props, :children => Front.to_dash(children))
return result
end

dcc_graph(children_maker::Function; kwargs...) = dcc_graph(children_maker(); kwargs...)

end

ext: jl


text:
function predict_full(krg::Kriging{N,M}, x::AbstractMatrix) where {N,M}
    if size(x, 1) != N
        throw(ArgumentError("`krg` and `x` do not have consistent dimensions"))
    end
    nobs = size(x, 2)
    y = similar(x, M, nobs)
    σ2 = similar(y)
    @inbounds for i in 1:M
        yr, sr = GP.predict_f(krg.gps[i], krg.xscaler * x)
        y[i, :] .= yr
        σ2[i, :] .= sr
    end
    inverse!(krg.yscaler, y)
    inverse_variance!(krg.yscaler, σ2)
    y, σ2
end

function predict_full(krg::Kriging, x::AbstractVector)
    y, σ2 = predict_full(krg, reshape(x, :, 1))
    vec(y), vec(σ2)
end


text:
"""
    SynchronousExecution

Functor object for using synchronous execution in an L-shaped algorithm (assuming multiple Julia cores are available). Create by supplying a [`Synchronous`](@ref) object through `execution` in the `LShapedSolver` factory function and then pass to a `StochasticPrograms.jl` model.

"""
struct SynchronousExecution{F <: AbstractFeasibility,
                            T <: AbstractFloat,
                            A <: AbstractVector,
                            S <: LQSolver} <: AbstractExecution
    subworkers::Vector{SubWorker{F,T,A,S}}
    subobjectives::A
    model_objectives::A
    metadata::Vector{MetaData}
    cutqueue::CutQueue{T}

    function SynchronousExecution(nscenarios::Integer, ::Type{F}, ::Type{T}, ::Type{A}, ::Type{S}) where {F <: AbstractFeasibility, T <: AbstractFloat, A <: AbstractVector, S <: LQSolver}
        return new{F,T,A,S}(Vector{SubWorker{F,T,A,S}}(undef, nworkers()),
                            A(),
                            A(),
                            Vector{MetaData}(undef,nworkers()),
                            RemoteChannel(() -> Channel{QCut{T}}(4*nworkers()*nscenarios)))
    end
end

function initialize_subproblems!(execution::SynchronousExecution,
                                 scenarioproblems::AbstractScenarioProblems,
                                 x::AbstractVector,
                                 subsolver::MPB.AbstractMathProgSolver)
    load_subproblems!(execution.subworkers, scenarioproblems, x, subsolver)
    return nothing
end

function finish_initilization!(lshaped::AbstractLShapedSolver, execution::SynchronousExecution)
    append!(execution.subobjectives, fill(1e10, nthetas(lshaped)))
    append!(execution.model_objectives, fill(-1e10, nthetas(lshaped)))
    for w in workers()
        execution.metadata[w-1] = RemoteChannel(() -> MetaChannel(), w)
    end
    return lshaped
end

function resolve_subproblems!(lshaped::AbstractLShapedSolver, execution::SynchronousExecution{F,T}) where {F <: AbstractFeasibility, T <: AbstractFloat}
    # Update metadata
    for w in workers()
        put!(execution.metadata[w-1], timestamp(lshaped), :gap, gap(lshaped))
    end
    @sync begin
        for (i,w) in enumerate(workers())
            worker_aggregator = remote_aggregator(lshaped.aggregation, scenarioproblems(lshaped.stochasticprogram), w)
            remotecall_fetch(resolve_subproblems!, w, execution.subworkers[w-1], lshaped.x, execution.cutqueue, worker_aggregator, timestamp(lshaped), execution.metadata[w-1])
        end
    end
    # Assume no cuts are added
    added = false
    # Collect incoming cuts
    while isready(execution.cutqueue)
        _, cut::SparseHyperPlane{T} = take!(execution.cutqueue)
        added |= add_cut!(lshaped, cut)
    end
    # Return current objective value and cut_added flag
    return current_objective_value(lshaped), added
end

function calculate_objective_value(lshaped::AbstractLShapedSolver, execution::SynchronousExecution)
    return get_obj(lshaped)⋅decision(lshaped) + eval_second_stage(execution.subworkers, decision(lshaped))
end

function fill_submodels!(lshaped::AbstractLShapedSolver, scenarioproblems, execution::SynchronousExecution)
    return fill_submodels!(execution.subworkers, decision(lshaped), scenarioproblems)
end

# API
# ------------------------------------------------------------
function (execution::Synchronous)(nscenarios::Integer, ::Type{F}, ::Type{T}, ::Type{A}, ::Type{S}) where {F <: AbstractFeasibility, T <: AbstractFloat, A <: AbstractVector, S <: LQSolver}
    return SynchronousExecution(nscenarios, F, T, A, S)
end

function str(::Synchronous)
    return "Synchronous "
end


text:
include("cross_entropy_method_tests.jl")
include("mdp_helper_tests.jl")


text:
using SIMDPirates
using Test

pkgdir(pkg::String) = abspath(joinpath(dirname(Base.find_package(pkg)), ".."))
const W = SIMDPirates.VectorizationBase.pick_vector_width(Float64);

arr(x) = [x[i] for i ∈ 1:length(x)]

@testset "SIMDPirates.jl" begin
    @test isempty(detect_unbound_args(SIMDPirates))
    # pkgs = ["AccurateArithmetic", "MCMCChainSummaries", "VectorizedRNG"]
    pkgs = ["MCMCChainSummaries", "VectorizedRNG"]
    # pkgs = ["AccurateArithmetic", "VectorizedRNG"]
    for pkg ∈ pkgs
        include(joinpath(pkgdir(pkg), "test", "runtests.jl"))
    end

    sxi = SVec(Core.VecElement.((-1,0,1,2)))
    @test sign(sxi) === SVec(Core.VecElement.((-1,0,1,1)))
    @test sum(sxi) == 2
    @test prod(sxi) == 0
    @test maximum(sxi) == 2
    @test minimum(sxi) == -1
    @test SIMDPirates.addscalar(sxi, 2) === SVec(Core.VecElement.((1,0,1,2)))
    @test SIMDPirates.mulscalar(sxi, 2) === SVec(Core.VecElement.((-2,0,1,2)))
    @test SIMDPirates.maxscalar(sxi, 2) === SVec(Core.VecElement.((2,0,1,2)))
    @test SIMDPirates.maxscalar(sxi, -2) === SVec(Core.VecElement.((-1,0,1,2)))
    @test SIMDPirates.minscalar(sxi, 2) === SVec(Core.VecElement.((-1,0,1,2)))
    @test SIMDPirates.minscalar(sxi, -2) === SVec(Core.VecElement.((-2,0,1,2)))
    for T ∈ [Float32,Float64]
        Wt = SIMDPirates.VectorizationBase.pick_vector_width(T)
        sx = SVec(ntuple(_ -> Core.VecElement(rand(T)), Wt))
        @test arr(log(sx)) ≈ log.(arr(sx))
        T === Float64 && @test arr(exp(sx)) ≈ exp.(arr(sx))
    end

    sx = SVec(ntuple(i -> Core.VecElement(i == 2 ? NaN : rand()), Val(W)))
    @test (sx == sx)[2] == false
end


text:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Description
#
#   Tests of Tables.jl API.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

struct TestVec{T} <: AbstractArray{T,1}
    data::Array{T,1}
end
Base.IndexStyle(::Type{A}) where {A<:TestVec} = Base.IndexCartesian()
Base.size(A::TestVec) = size(getfield(A, :data))
Base.getindex(A::TestVec, index::Int) = getindex(getfield(A, :data), index)
Base.collect(::Type{T}, itr::TestVec) where {T} = TestVec(collect(T, getfield(itr, :data)))

struct MinimalTable
    data::Matrix
    colnames::TestVec
end

Tables.istable(x::MinimalTable) = true
Tables.columnaccess(::MinimalTable) = true
Tables.columnnames(x::MinimalTable) = getfield(x, :colnames)
Tables.columns(x::MinimalTable) = x
Base.getindex(x::MinimalTable, i1, i2) = getindex(getfield(x, :data), i1, i2)
Base.getproperty(x::MinimalTable, s::Symbol) = getindex(x, :, findfirst(==(s), Tables.columnnames(x)))
Base.convert(::Type{<:TestVec}, x::Array) = TestVec(x)

@testset "Tables.jl with custom column name vector" begin
    data     = [10.0^(i+j) for i = 1:10, j = 1:5]
    mintable = MinimalTable(data, [:C1, :C2, :C3, :C4, :C5])

    str_data     = pretty_table(String, data, ["C1", "C2", "C3", "C4", "C5"])
    str_mintable = pretty_table(String, mintable)

    @test str_data == str_mintable
end


text:
@doc """
    Wolff_update!(model, param::Parameter)
    Wolff_update!(model, T::Real, Js::AbstractArray)

Updates spin configuration by Wolff algorithm
under temperature `T=param["T"]` and coupling constants `J=param["J"]`
"""
@inline function Wolff_update!(model::Union{Ising, Potts, Clock, XY}, param::Parameter)
    p = convert_parameter(model, param)
    return Wolff_update!(model, p...)
end
function Wolff_update!(model::Ising, T::Real, Js::AbstractArray)
    rng = model.rng
    ps = -expm1.((-2.0/T).*Js)
    nsites = numsites(model)

    clustersize = 0
    st = Stack(Deque{Int}())
    center = rand(rng, 1:nsites)
    sp = model.spins[center]
    model.spins[center] *= -1
    push!(st, center)
    @inbounds while !isempty(st)
        clustersize += 1
        s = pop!(st)
        for (n,b) in neighbors(model, s)
            bt = bondtype(model,b)
            if model.spins[n] == sp && rand(rng) < ps[bt]
                model.spins[n] *= -1
                push!(st, n)
            end
        end
    end

    return nothing
end

function Wolff_update!(model::Potts, T::Real, Js::AbstractArray)
    rng = model.rng
    ps = -expm1.((-1.0/T).*Js)
    nsites = numsites(model)

    clustersize = 0
    st = Stack(Deque{Int}())
    center = rand(rng, 1:nsites)
    sp = model.spins[center]
    newsp = mod1(sp+rand(rng, 1:(model.Q-1)), model.Q)
    model.spins[center] = newsp
    push!(st, center)
    @inbounds while !isempty(st)
        clustersize += 1
        s = pop!(st)
        for (n,b) in neighbors(model, s)
            bt = bondtype(model,b)
            if model.spins[n] == sp && rand(rng) < ps[bt]
                model.spins[n] = newsp
                push!(st, n)
            end
        end
    end
    return nothing
end

function Wolff_update!(model::Clock, T::Real, Js::AbstractArray)
    rng = model.rng
    nsites = numsites(model)
    b2J = (2.0/T).*Js

    clustersize = 0
    st = Stack(Deque{Int}())

    m = rand(rng, 1:model.Q)-1
    center = rand(rng, 1:nsites)
    push!(st, center)
    r = mod1(model.spins[center]-m, model.Q)
    model.spins[center] = mod1(m-r+1, model.Q)

    @inbounds while !isempty(st)
        clustersize += 1
        center = pop!(st)
        cr = mod1(model.spins[center]-m, model.Q)
        for (n,b) in neighbors(model, center)
            nr = mod1(model.spins[n]-m, model.Q)
            bt = bondtype(model,b)
            ## Note: center already flipped
            if rand(rng) < -expm1(b2J[bt]*model.sines_sw[cr]*model.sines_sw[nr])
                push!(st, n)
                model.spins[n] = mod1(m-nr+1,model.Q)
            end
        end
    end

    return nothing
end

function Wolff_update!(model::XY, T::Real, Js::AbstractArray)
    rng = model.rng
    nsites = numsites(model)
    b2J = (2.0/T).*Js

    clustersize = 0
    st = Stack(Deque{Int}())

    m = 0.5*rand(rng)
    center = rand(rng,1:nsites)
    push!(st, center)

    model.spins[center] = mod(2m+0.5-model.spins[center], 1.0)

    @inbounds while !isempty(st)
        clustersize += 1
        center = pop!(st)
        cp = cospi(2(model.spins[center]-m))

        for (n,b) in neighbors(model, center)
            np = cospi(2(model.spins[n]-m))
            bt = bondtype(model,b)
            ## Note: center already flipped
            if rand(rng) < -expm1(b2J[bt]*cp*np)
                push!(st, n)
                model.spins[n] = mod(2m+0.5-model.spins[n], 1.0)
            end
        end
    end

    return nothing
end


text:
export Euler, Euler_naive
export step!

"""
    Euler(arguments;realdata)

Explicit Euler solver.

Construct an object of type `TimeSolver` to be used in `Problem(model, initial, param; solver::TimeSolver)`

Arguments can be either
0. an object of type `AbstractModel`;
1. an `Array` of size `(N,datasize)` where `N` is the number of collocation points and `datasize` the number of equations solved;
2. `(param,datasize)` where `param is a `NamedTuple` containing a key `N`, and `datasize` a integer (optional, by default `datasize=2`).

The keyword argument `realdata` is optional, and determines whether pre-allocated vectors are real- or complex-valued.
By default, they are either determined by the model or the type of the array in case `0.` and `1.`, complex-valued in case `2.`.

"""
struct Euler <: TimeSolver

    U1 :: Array
    label :: String

    function Euler( U :: Array; realdata=nothing )
        U1 = copy(U)
        if realdata==true
            U1 = real.(U1);
        end
        if realdata==false
            U1 = complex.(U1);
        end
        new( U1, "Euler" )
    end

    function Euler( model :: AbstractModel; realdata=nothing )
        U=model.mapto(Init(x->0*x,x->0*x))
        Euler( U; realdata=realdata)
    end
    function Euler( param::NamedTuple, datasize=2::Int; realdata=nothing )
        Euler( zeros(Complex{Float64}, (param.N,datasize)); realdata=realdata)
    end
end

function step!(solver :: Euler,
                model :: AbstractModel,
                U  ,
                dt )

    solver.U1 .= U
    model.f!( solver.U1 )
    U .+= dt .* solver.U1

end

"""
    Euler_naive()

Runge-Kutta fourth order solver.

A naive version of `Euler`, without argument since no pre-allocation is performed.

"""
struct Euler_naive <: TimeSolver
    label :: String

    function Euler_naive() new("Euler (naive)") end
end

function step!(s  :: Euler_naive,
               model :: AbstractModel ,
               U  ,
               dt )

    U0 = copy(U)
    model.f!( U0 )
    U .+= dt * U0

end


text:
"""
	REDCap

Legacy Julia 0.6 frontend for the REDCap API. Handles all available API calls from REDCap through top-level functions.
Must have a valid `REDCap.Config` object set up with the API key and url.
Compatable with REDCap Version 8.1.0
"""
module REDCap

using HTTP
using JSON
using LightXML
using CSV
using DataStructures
using DataFrames

include("Config.jl")
include("Utils.jl")
include("Export.jl")
include("Import.jl")
include("Delete.jl")

export export_field_names,
		export_instruments,
		export_metadata,
		export_project_information,
		export_users,
		export_version,
		export_arms,
		export_events,
		export_pdf,
		export_project,
		export_records,
		export_survey_queue_link,
		export_survey_return_code,
		export_instrument_event_mappings,
		export_survey_participant_list,
		export_file,
		export_report,
		export_survey_link,
		generate_next_record_id,

		import_project_information,
		import_metadata,
		import_users,
		import_arms,
		import_events,
		import_records,
		import_instrument_event_mappings,
		import_file,

		delete_arms,
		delete_events,
		delete_file,
		delete_records,

		create_project

end


text:
"""
    Disjunction(criteria...)

$STOPPING_DOC

Combines the specified stopping `criteria` dijunctively: if any one of
the criteria applies, then stop.

**Syntactic sugar.** `c1 + c2 + ...` is equivalent to
  `Disjunction(c1, c2, ...)`.

"""
struct Disjunction{A,B} <: StoppingCriterion
    a::A
    b::B
    function Disjunction(a::A, b::B) where {A, B}
        a in b && return b # `in` defined post-facto below
        b in a && return a
        return new{A,B}(a, b)
    end
end

Disjunction() = Never()
Disjunction(a) = a
Disjunction(a, b, c...) = Disjunction(Disjunction(a,b), c...)

for f in [:update, :update_training]
    eval(quote
         $f(d::Disjunction, loss) =
             (a = $f(d.a, loss), b = $f(d.b, loss))
         $f(d::Disjunction, loss, state) =
             (a = $f(d.a, loss, state.a),
              b = $f(d.b, loss, state.b))
         end)
end

## RECURSION TO EXTRACT COMPONENTS AND TEST MEMBERSHIP

# fallback for atomic criteria:
_push!(criteria, criterion) = push!(criteria, criterion)

# disjunction:
function _push!(criteria, d::Disjunction)
    _push!(criteria, d.a)
    _push!(criteria, d.b)
end

_criteria(c::StoppingCriterion) = _push!(StoppingCriterion[], c)

Base.in(::Never, ::StoppingCriterion) = true
Base.in(::Never, ::Disjunction) = true
Base.in(c1::StoppingCriterion, c2::StoppingCriterion) = c1 == c2
Base.in(c::StoppingCriterion, d::Disjunction) = c in _criteria(d)
Base.in(d::Disjunction, c::StoppingCriterion) = false

## DISPLAY

function Base.show(io::IO, d::Disjunction)
    list = join(string.(_criteria(d)), ", ")
    print(io, "Disjunction($list)")
end

## RECURSION TO DEFINE `done`

# fallback for atomic criteria:
_done(criterion, state, old_done) = old_done || done(criterion, state)

# disjunction:
_done(d::Disjunction, state, old_done) =
    _done(d.a, state.a, _done(d.b, state.b, old_done))

done(d::Disjunction, state) = _done(d, state, false)

## RECURSION TO BUILD MESSAGE

# fallback for atomic criteria:
function _message(criterion, state, old_message)
    done(criterion, state) && return old_message*message(criterion, state)
    return old_message
end

# disjunction:
_message(d::Disjunction, state, old_message) =
    _message(d.a, state.a,
             _message(d.b, state.b, old_message))

message(d::Disjunction, state) = _message(d, state, "")

## SYNTACTIC SUGAR

Base.zero(::Type{<:StoppingCriterion}) = Never()
+(a::StoppingCriterion, b::StoppingCriterion...) = Disjunction(a, b...)


text:
# This is a Julia script to
# plot the integrated orbits

"Check if the value of the option is the last argument"
function check_last(i)
    if i + 1 == length(ARGS)
        println("The last argument should be the input directory.")
        exit(1)
    end
end

# Define default values for optional arguments
POSTFIX = ""

# Parse the options
for i in eachindex(ARGS)
    # A number of integration iterations used
    if ARGS[i] == "-n"
        check_last(i)
        try
            global N = parse(UInt, ARGS[i+1])
        catch
            println("Couldn't parse the value of the `-n` argument.")
            exit(1)
        end
    end
    # A time step
    if ARGS[i] == "-h"
        check_last(i)
        try
            global H = parse(Float64, ARGS[i+1])
        catch
            println("Couldn't parse the value of the `-h` argument.")
            exit(1)
        end
    end
    # A postfix for the names of output files
    if ARGS[i] == "--postfix"
        check_last(i)
        try
            global POSTFIX = " ($(ARGS[i+1]))"
        catch
            println("Couldn't parse the value of the `--postfix` argument.")
            exit(1)
        end
    end
end

# Check for required arguments (exc)
if !@isdefined(N) || !@isdefined(H) || length(ARGS) == 0
    println("""
        Usage:
        julia --project=. scripts/orbits.jl -n <N> -h <H> <input> [--postfix <POSTFIX>]"""
    )
    exit(1)
end

# Define the input directory
INPUT_DIR = ARGS[end]

println('\n', " "^4, "> Loading the packages...")

using LaTeXStrings
using Plots

# Use the GR backend for plots
gr()

# Change some of the default parameters for plots
default(fontfamily = "Computer Modern", dpi = 300, legend = :topright)

# Define the paths to output directories
CURRENT_DIR = @__DIR__
ROOT_DIR = basename(CURRENT_DIR) == "scripts" ? dirname(CURRENT_DIR) : CURRENT_DIR
PLOTS_DIR = joinpath(ROOT_DIR, "plots")
ORBITS_DIR = joinpath(PLOTS_DIR, "orbits")
OUTPUT_DIR = joinpath(ORBITS_DIR, basename(INPUT_DIR))

# Make sure the needed directories exist
mkpath(OUTPUT_DIR)

# For each path in the input directory
for path in readdir(INPUT_DIR; join = true)
    # Check if the path is a directory
    if isdir(path)
        # Define the paths to the binary files
        r_path = joinpath(path, "r.bin")
        z_path = joinpath(path, "z.bin")
        x_path = joinpath(path, "x.bin")
        y_path = joinpath(path, "y.bin")
        e_path = joinpath(path, "e.bin")

        # Get the ID of the object
        name = basename(path)

        # Define the object directory
        object_dir = joinpath(OUTPUT_DIR, name)
        mkpath(object_dir)

        # Plot the orbit in the RZ plane if the corresponding data files exist
        if isfile(r_path) && isfile(z_path)
            # Prepare the arrays
            r = Vector{Float64}(undef, N + 1)
            z = Vector{Float64}(undef, N + 1)

            # Read the data
            read!(r_path, r)
            read!(z_path, z)

            println(" "^4, "> Plotting the orbit in the RZ plane from \"$(name)\"...")

            # Plot the orbit in the RZ-plane
            p = plot(
                r[1:100:end],
                z[1:100:end];
                label = "",
                title = name,
                xlabel = L"R \; [\mathrm{kpc}]",
                ylabel = L"Z \; [\mathrm{kpc}]",
                size = (400, 400)
            )

            # Point out the starting position
            scatter!(p, [r[1],], [z[1],]; label = "")

            # Save the figure as PDF and PNG
            savefig(p, joinpath(object_dir, "$(name) (Orbit, RZ)$(POSTFIX).pdf"))
            savefig(p, joinpath(object_dir, "$(name) (Orbit, RZ)$(POSTFIX).png"))
        end

        # Plot the orbit in the XY plane if the corresponding data files exist
        if isfile(x_path) && isfile(y_path)
            # Prepare the arrays
            x = Vector{Float64}(undef, N + 1)
            y = Vector{Float64}(undef, N + 1)

            # Read the data
            read!(x_path, x)
            read!(y_path, y)

            println(" "^4, "> Plotting the orbit in the XY plane from \"$(name)\"...")

            # Plot the orbit in the XY-plane
            p = plot(
                x[1:100:end],
                y[1:100:end];
                label = "",
                title = name,
                xlabel = L"X \; [\mathrm{kpc}]",
                ylabel = L"Y \;\, [\mathrm{kpc}]",
                size = (400, 400)
            )

            # Point out the starting position
            scatter!(p, [x[1],], [y[1],]; label = "")

            # Save the figure as PDF and PNG
            savefig(p, joinpath(object_dir, "$(name) (Orbit, XY)$(POSTFIX).pdf"))
            savefig(p, joinpath(object_dir, "$(name) (Orbit, XY)$(POSTFIX).png"))
        end

        # Plot the total energy variation if the corresponding data file exists
        if isfile(e_path)
            # Prepare the arrays
            e = Vector{Float64}(undef, N + 1)

            # Read the data
            read!(e_path, e)

            println(
                " "^4, "> Plotting the total energy variation from \"$(name)\"...", '\n',
                " "^4, "  > max: ", maximum(e), '\n',
                " "^4, "  > min: ", minimum(e), '\n',
                " "^4, "  > Δ: ", maximum(e) - minimum(e),
            )

            # Plot the total energy variation
            p = plot(
                0.0:(H*100/1000):(H*(length(e)-1)/1000),
                e[1:100:end];
                label = "",
                title = name,
                xlabel = L"T \;\, [\mathrm{Gyr}]",
                ylabel = L"E \; [\mathrm{km^2 \, s^{-2}}]",
                size = (400, 400)
            )

            # Point out the initial value
            scatter!(p, [0.0,], [e[1],]; label = "")

            # Save the figure as PDF and PNG
            savefig(p, joinpath(object_dir, "$(name) (Total energy)$(POSTFIX).pdf"))
            savefig(p, joinpath(object_dir, "$(name) (Total energy)$(POSTFIX).png"))
        end
    end
end

println()


text:
using AHPQ
using Test

@testset "searcher" begin
    using Statistics: norm
    n_dp = 1000
    n_dim = 4
    data = rand(n_dim, n_dp)
    data = data ./ mapslices(norm, data, dims=1)
    queries = rand(4, 100)

    stopcond = 1e-1
    @info("Searcher without preclustering and without reorder")
    ahpq = builder(deepcopy(data), T=0.6, a=0, reorder=0)
    MIPS(ahpq, queries, 10)
    MIPS(ahpq, queries[:,1], 10)
    @test true

    @info("Searcher with preclustering and with reorder")
    ahpq = builder(deepcopy(data), T=0.6, a=10, reorder=25)
    MIPS(ahpq, queries, 10)
    MIPS(ahpq, queries[:,1], 10)
    @test true

    @info("Searcher without preclustering and with reorder")
    ahpq = builder(deepcopy(data), T=0.6, a=0, reorder=25)
    MIPS(ahpq, queries, 10)
    MIPS(ahpq, queries[:,1], 10)
    @test true

    @info("Searcher with preclustering and without reorder")
    ahpq = builder(deepcopy(data), T=0.6, a=10, reorder=0)
    MIPS(ahpq, queries, 10)
    MIPS(ahpq, queries[:,1], 10)
    @test true
end


text:
module TeeStreams

export TeeStream
"""
```julia
tee = TeeStream(io::IO...)
```

Construct a tee stream by wrapping multiple writable IO objects.

```julia
TeeStream(f::Function, io::IO...) do tee
    # ...
end
```

Construct a tee stream by wrapping multiple writable IO objects and
call function `f` on the tee. Automatically calls `close` on the tee
before returning.
"""
struct TeeStream{T <: NTuple{<:Any, IO}} <: IO
    streams::T
    opened_idx::Union{<:NTuple{<:Any,Bool}, Nothing}
    function TeeStream{T}(ios::T, opened_idx=nothing) where T <: NTuple{<:Any, IO}
        tee = new{T}(ios, opened_idx)
        # check_writable(tee)
        return tee
    end
end

TeeStream(ios::IO...) = TeeStream{typeof(ios)}(ios)
function TeeStream(f::Function, ios::IO...)
    tee = TeeStream(ios...)
    try
        f(tee)
    finally
        close(tee)
    end
end

# See https://docs.julialang.org/en/v1/base/io-network/#Base.unsafe_write
function Base.unsafe_write(tee::TeeStream, p::Ptr{UInt8}, nb::UInt)
    @sync for s in tee.streams
        @async begin
            # TODO: Is it enough to rely on write locks on each s?
            n = unsafe_write(s, p, nb)
            check_written(n, nb)
        end
    end
    return Int(nb)
end
function Base.write(tee::TeeStream, b::UInt8)
    # check_writable(tee)
    for s in tee.streams
        n = write(s, b)
        check_written(n, 1)
    end
    return 1
end

"""
    close(tee::TeeStream)

Close all streams wrapped in the tee stream.
"""
Base.close(tee::TeeStream) = foreach(close, tee.streams)

"""
    flush(tee::TeeStream)

Flush all streams wrapped in the tee stream.
"""
Base.flush(tee::TeeStream) = foreach(flush, tee.streams)

Base.isreadable(tee::TeeStream) = false
Base.isopen(tee::TeeStream) = all(isopen, tee.streams)
Base.iswritable(tee::TeeStream) = all(iswritable, tee.streams)

# All streams do not define iswritable reliably so just try and throw if things doesn't work
# function check_writable(tee::TeeStream)
#     if !(isopen(tee) && iswritable(tee))
#         error("stream is closed or not writeable")
#     end
# end
function check_written(n, m)
    if n != m
        error("could not write the requested bytes: stream is closed or not writeable?")
    end
    return nothing
end

# maybe_open(io) = io
# maybe_open(io::Tuple) = open(io...)
# function teeopen(args::Union{IO, Tuple}...)
#     opened_idx = ntuple(i -> args[i] isa Tuple, length(args))
#     streams = map(maybe_open, args)
#     return TeeStream{typeof(streams)}(streams, opened_idx)
# end

# function teeopen(f::Function, args::Union{IO,Tuple}...)
#     tee = teeopen(args...)
#     try
#         f(tee)
#     finally
#         close(tee)
#         # teeclose(tee)
#     end
# end

# function teeclose(tee::TeeStream)
#     if tee.opened_idx === nothing
#         return
#     end
#     for i in 1:length(tee.streams)
#         tee.opened_idx[i] || continue
#         close(tee.streams[i])
#     end
# end

end # module


text:
thread_println(t::ThreadManager.Thread, msg) = ThreadManager.safe_call(t, (x) -> print(msg))
thread_push(t::ThreadManager.Thread, a, e) = ThreadManager.safe_call(t, (x) -> push!(a, e))

thread_sleep(sec::Real) = Libc.systemsleep(sec)

thread_init(p::Tuple{String,Function}) = function(t::ThreadManager.Thread)
	thread_println(t, LogManager.logMsg(:Debug, p[1], "start"))
	p[2](t)
end

max_stars_repo_name: Gilga/JGE

max_issues_repo_name: Gilga/JGE

max_forks_repo_name: Gilga/JGE


text:
using Statistics

collect_property(unitList::Array, key::Symbol) = [getproperty(unit, key) for unit in unitList]

function DivisionTemplate(unitList::Array{Unit,1})
    # sum property
    kw_dict = Dict{Symbol, Real}()
    for key in [:HP, :Recov, :Suppr, :Weight, :Supply, :Width, :Manpower, :SoftAtk, :HardAtk, :AirAtk,
                :Defense, :Breakthr, :FuelCapacity, :FuelUsage, :ProdCost]
        kw_dict[key] = sum(collect_property(unitList, key))
    end

    # mean property
    for key in [:Org, :Hardness]
        kw_dict[key] = mean(collect_property(unitList, key))
    end

    # max property
    for key in [:Training]
        kw_dict[key] = maximum(collect_property(unitList, key))
    end

    # min property
    for key in [:Speed]
        kw_dict[key] = minimum(collect_property(unitList, key))
    end

    # speciel
    armor = collect_property(unitList, :Armor)
    pierce = collect_property(unitList, :Pierce)

    kw_dict[:Armor] = 0.3*maximum(armor) + 0.7*mean(armor)
    kw_dict[:Pierce] = 0.4*maximum(pierce) + 0.6*mean(pierce)

    return DivisionTemplate(;unitList=unitList, kw_dict...)
end

function DivisionTemplate(config_list::Tuple{Unit,Int64}...)
    unit_list = Array{Unit,1}()
    for (unit, count) in config_list
        for i in 1:count
            push!(unit_list, unit)
        end
    end
    DivisionTemplate(unit_list)
end

function Base.show(io::IO, ::MIME"text/plain", div::DivisionTemplate)
    print(io, "uniList: ")
    print(io, join([unit.Unit for unit in div.unitList], ","))
    for fn in fieldnames(typeof(div))
        if fn == :unitList
            continue
        end
        print(io, " $fn: $(getproperty(div, fn)),")
    end
end

function Division(template::DivisionTemplate)
    Division(template, template.HP, template.Org)
end

function reset!(div::Division)
    div.HP = div.T.HP
    div.Org = div.T.Org
end

function broken(div::Division)
    return div.Org <= 0
end

function eliminated(div::Division)
    return div.HP <= 0
end

function defeated(div::Division)
    return broken(div) || eliminated(div)
end

function Base.show(io::IO, media::MIME"text/plain", div::Division)
    f(x) = round(x, digits=1)

    println(io, "HP: $(f(div.HP))/$(f(div.T.HP))")
    println(io, "Org: $(f(div.Org))/$(f(div.T.Org))")
    println(io, "SoftAtk, HardAtk: $(f(div.T.SoftAtk)), $(f(div.T.HardAtk))")
    println(io, "Breakthr, Defense: $(f(div.T.Breakthr)), $(f(div.T.Defense))")
    println(io, "Pierce, Armor: $(f(div.T.Pierce)), $(f(div.T.Armor))")
    println(io, "Hardness, Width: $(round(div.T.Hardness, digits=3)), $(f(div.T.Width))")
    print(io, "T:")
    show(io, media, div.T)
end


text:
"""
`ScaledKernel(k::Kernel,σ²::Real)`
Return a kernel premultiplied by the variance `σ²` : `σ² k(x,x')`
"""
struct ScaledKernel{Tk<:Kernel, Tσ²<:Real} <: Kernel
    kernel::Tk
    σ²::Vector{Tσ²}
end

function ScaledKernel(kernel::Tk, σ²::Tσ²=1.0) where {Tk<:Kernel,Tσ²<:Real}
    @check_args(ScaledKernel, σ², σ² > zero(Tσ²), "σ² > 0")
    return ScaledKernel{Tk, Tσ²}(kernel, [σ²])
end

kappa(k::ScaledKernel, x) = first(k.σ²) * kappa(k.kernel, x)

metric(k::ScaledKernel) = metric(k.kernel)

Base.:*(w::Real, k::Kernel) = ScaledKernel(k, w)

Base.show(io::IO, κ::ScaledKernel) = printshifted(io, κ, 0)

function printshifted(io::IO, κ::ScaledKernel, shift::Int)
    printshifted(io, κ.kernel, shift)
    print(io,"\n" * ("\t"^(shift+1)) * "- σ² = $(first(κ.σ²))")
end


text:
struct WingCS{T <: Real} <: Aircraft
    wing :: Union{Wing{T}, HalfWing{T}}
    position :: SVector{3,T}
    orientation :: Matrix{T}
end

mean_aerodynamic_center(comp :: WingCS) = (Translation(comp.position) ∘ LinearMap(comp.orientation)) (mean_aerodynamic_center(comp.wing))

max_forks_repo_name: GodotMisogi/AeroMDAO


text:
@testset "Parallel processing" begin

    W = addprocs(2)
    @everywhere using GigaSOM

    som = initGigaSOM(pbmc8_data, 10, 10, seed = 1234)

    @testset "Check SOM dimensions" begin
        @test size(som.codes) == (100, 10)
        @test som.xdim == 10
        @test som.ydim == 10
        @test som.numCodes == 100
    end

    som = trainGigaSOM(som, pbmc8_data, epochs = 2, rStart = 6.0)

    winners = mapToGigaSOM(som, pbmc8_data)

    embed = embedGigaSOM(som, pbmc8_data)

    @testset "Check results" begin
        codes = som.codes
        @test size(codes) == (100, 10)

        dfCodes = DataFrame(codes, :auto)
        rename!(dfCodes, Symbol.(antigens))
        dfEmbed = DataFrame(embed, :auto)
        CSV.write(genDataPath * "/parallelDfCodes.csv", dfCodes)
        CSV.write(genDataPath * "/parallelWinners.csv", winners)
        CSV.write(genDataPath * "/parallelEmbedded.csv", dfEmbed)

        # load the ref data
        refParallelDfCodes = CSV.File(refDataPath * "/refParallelDfCodes.csv") |> DataFrame
        refParallelWinners = CSV.File(refDataPath * "/refParallelWinners.csv") |> DataFrame
        refParallelEmbedded =
            CSV.File(refDataPath * "/refParallelEmbedded.csv") |> DataFrame

        # load the generated data
        parallelDfCodes = CSV.File(genDataPath * "/parallelDfCodes.csv") |> DataFrame
        parallelDfCodesTest = first(parallelDfCodes, 10)
        parallelWinners = CSV.File(genDataPath * "/parallelWinners.csv") |> DataFrame
        parallelWinnersTest = first(parallelWinners, 10)
        parallelEmbedded = CSV.File(genDataPath * "/parallelEmbedded.csv") |> DataFrame
        parallelEmbeddedTest = first(parallelEmbedded, 10)

        # test the generated data against the reference data
        @test refParallelWinners == parallelWinnersTest
        @test Matrix{Float64}(refParallelDfCodes) ≈ Matrix{Float64}(parallelDfCodesTest)
        @test Matrix{Float64}(refParallelEmbedded) ≈ Matrix{Float64}(parallelEmbeddedTest) atol =
            1e-4
    end

    rmprocs(W)

end


text:
using Statistics
using Knet: Knet, dir, zeroone, progress, sgd, load, save, gc, progress!, Param,
 KnetArray, gpu, Data, nll, relu, training, dropout, minibatch, param, param0,
 conv4, pool, mat, zeroone, sgd, adam, rmsprop, adagrad, sigm, softmax, tanh,
  batchnorm, bnparams, bnmoments, BNMoments, _update_moments!, _lazy_init!,
  accuracy, xavier
using AutoGrad
using Base.Iterators
using DelimitedFiles

include(Knet.dir("data", "cifar.jl"))

include("models.jl")
include("wider.jl")
include("deeper.jl")

# The global device setting (to reduce gpu() calls)
let at = nothing
    global atype
    atype() = (at == nothing) ? (at = (gpu() >= 0 ? KnetArray{Float32} : Array{Float32})) : at
end

"Take every nth element in an iterator"
take_every(n,itr) = (x for (i,x) in enumerate(itr) if i % n == 0)

"""
Trains a model, tests it every epoch on training and testing data.
Saves results to a file and can load them back. Returns the results.
"""
function train_results(dtrn, dtst, file, model, epochs=100, from_scratch=true, cont_from_save=false; o...)
    file = string("saved/", file)
    if !from_scratch
        try
            r, model = Knet.load(file, "results", "model")
            if cont_from_save
                new_r = ((model(dtrn), model(dtst), accuracy(model, dtrn), accuracy(model, dtst))
                     for x in take_every(length(dtrn), progress(adam(model, repeat(dtrn,epochs)))))
                new_r = reshape(collect(Float32,flatten(new_r)),(4,:))
                r = hcat(r, new_r)
                Knet.save(file, "results", r, "model", model)
                Knet.gc() # To save gpu memory
            end
        catch SystemError
            println("File not found. Running from scratch.")
            from_scratch = true
        end
    end

    if from_scratch
        r_first = [model(dtrn), model(dtst), accuracy(model, dtrn), accuracy(model, dtst)]
        r = ((model(dtrn), model(dtst), accuracy(model, dtrn), accuracy(model, dtst))
             for x in take_every(length(dtrn), progress(adam(model, repeat(dtrn,epochs)))))
        r = reshape(collect(Float32,flatten(r)),(4,:))
        r = hcat(r_first, r)
        Knet.save(file, "results", r, "model", model)
        Knet.gc() # To save gpu memory
    end

    println("Trn/Tst loss: ", minimum(r[1:2, :], dims=2),
            "Trn/Tst acc: ", maximum(r[3:4, :], dims=2))
    return r, model
end

"Loads the CIFAR-10 dataset"
function load_data()
    @info("Loading CIFAR 10...")
    xtrn, ytrn, xtst, ytst, = cifar10()
    #= Subtract mean of each feature
    where each channel is considered as
    a single feature following the CNN
    convention=#
    mn = mean(xtrn, dims=(1,2,4))
    xtrn = xtrn .- mn
    xtst = xtst .- mn
    @info("Loaded CIFAR 10")
    return (xtrn, ytrn), (xtst, ytst)
end

"""
Net2WiderNet experiment
-----------------------
Each Inception module in a smaller Inception network is widened by a factor of
sqrt(0.3) using Net2WiderNet and the baseline method random padding.
"""
function wider_experiment(dtrn, dtst)
    teacher = create_inception_bn_sm_model(3, 10)
    results, teacher = train_results(dtrn, dtst, "inception_sm.jld2", teacher, 5, false)
    println("Teacher results: ", results)
    writedlm("res/wider_res_teacher.txt", results)

    growth_ratio = 1.0/sqrt(0.3)
    noise = 0.03

    wider = deepcopy(teacher)
    wider_inceptionA(wider.layers[3], wider.layers[4], growth_ratio, noise)
    wider_inceptionA(wider.layers[4], wider.layers[5], wider.layers[7], growth_ratio, noise)
    wider_inceptionB(wider.layers[5], wider.layers[7], growth_ratio, noise)

    padded = teacher
    random_pad_inceptionA(padded.layers[3], padded.layers[4], growth_ratio)
    random_pad_inceptionA(padded.layers[4], padded.layers[5], padded.layers[7], growth_ratio)
    random_pad_inceptionB(padded.layers[5], padded.layers[7], growth_ratio)

    Knet.gc()

    results, wider = train_results(dtrn, dtst, "inception_sm_wider.jld2", wider, 5, true)
    println("Wider results: ", results)
    writedlm("res/wider_res_wider.txt", results)

    results, padded = train_results(dtrn, dtst, "inception_sm_padded.jld2", padded, 5, true)
    println("Padded results: ", results)
    writedlm("res/wider_res_padded.txt", results)
end

"""
Net2DeeperNet experiment
------------------------
Each Inception module in a smaller Inception network is deepened by 2 layers
using Net2DeeperNet and the baseline method random initialization.
"""
function deeper_experiment(dtrn, dtst)
    teacher = create_inception_bn_sm_model(3, 10)
    results, teacher = train_results(dtrn, dtst, "inception_sm.jld2", teacher, 5, false)
    println("Teacher results: ", results)
    writedlm("res/deeper_res_teacher.txt", results)

    deeper = deepcopy(teacher)
    deeper_inception(deeper.layers, 3, dtrn)
    deeper_inception(deeper.layers, 4, dtrn)
    deeper_inception(deeper.layers, 5, dtrn)

    rand_deeper = create_inception_bn_sm_model(3, 10, true)

    results, deeper = train_results(dtrn, dtst, "inception_sm_deeper.jld2", deeper, 5, true)
    println("Deeper results: ", results)
    writedlm("res/deeper_res_deeper.txt", results)

    results, rand = train_results(dtrn, dtst, "inception_sm_rand_deeper.jld2", rand_deeper, 5, true)
    println("Rand Deeper results: ", results)
    writedlm("res/deeper_res_rand.txt", results)
end

"""
Exploring design space experiment
---------------------------------
The design space is explored by using Net2WiderNet with a factor of sqrt(2) and
Net2DeeperNet with 4 layers per module layer. An additional student that is both
wider and deeper is also explored.
"""
function explore_experiment(dtrn, dtst)
    teacher = create_inception_bn_sm_model(3, 10)
    results, teacher = train_results(dtrn, dtst, "inception_sm.jld2", teacher, 5, false)
    println("Teacher results: ", results)
    writedlm("res/explore_res_teacher.txt", results)

    widening_factor = sqrt(2.0)
    wider = deepcopy(teacher)
    wider_inceptionA(wider.layers[3], wider.layers[4], widening_factor)
    wider_inceptionA(wider.layers[4], wider.layers[5], wider.layers[7], widening_factor)
    wider_inceptionB(wider.layers[5], wider.layers[7], widening_factor)

    deepening_factor = 4
    deeper = deepcopy(teacher)
    deeper_inception(deeper.layers, 3, dtrn, deepening_factor)
    deeper_inception(deeper.layers, 4, dtrn, deepening_factor)
    deeper_inception(deeper.layers, 5, dtrn, deepening_factor)

    results, wider = train_results(dtrn, dtst, "inception_sm_exp_wider.jld2", wider, 5, true)
    println("Wider results: ", results)
    writedlm("res/explore_res_wider.txt", results)

    results, deeper = train_results(dtrn, dtst, "inception_sm_exp_deeper.jld2", deeper, 5, true)
    println("Deeper results: ", results)
    writedlm("res/explore_res_deeper.txt", results)

    # New one with both widening and deepening, but with smaller factors
    bigger = deepcopy(teacher)
    widening_factor = 1/sqrt(0.3)
    wider_inceptionA(bigger.layers[3], bigger.layers[4], widening_factor)
    wider_inceptionA(bigger.layers[4], bigger.layers[5], bigger.layers[7], widening_factor)
    wider_inceptionB(bigger.layers[5], bigger.layers[7], widening_factor)
    deeper_inception(bigger.layers, 3, dtrn, 2)
    deeper_inception(bigger.layers, 4, dtrn, 2)
    deeper_inception(bigger.layers, 5, dtrn, 2)

    results, bigger = train_results(dtrn, dtst, "inception_sm_exp_bigger.jld2", bigger, 5, true)
    println("Bigger results: ", results)
    writedlm("res/explore_res_bigger.txt", results)
end

"""
Added Noise experiment
----------------------
Experiment to find out how much noise one should add after Net2WiderNet. Results
show that Gaussian noise with 0.05 gives good results.
"""
function noise_experiment(dtrn, dtst)
    teacher = create_inception_bn_sm_model(3, 10)
    results, teacher = train_results(dtrn, dtst, "inception_sm.jld2", teacher, 5, false)

    growth_ratio = 1.0/sqrt(0.3)

    wider_no_noise = deepcopy(teacher)
    wider_inceptionA(wider_no_noise.layers[3], wider_no_noise.layers[4], growth_ratio, 0)
    wider_inceptionA(wider_no_noise.layers[4], wider_no_noise.layers[5], wider_no_noise.layers[7], growth_ratio, 0)
    wider_inceptionB(wider_no_noise.layers[5], wider_no_noise.layers[7], growth_ratio, 0)

    wider_noise_1 = deepcopy(teacher)
    wider_inceptionA(wider_noise_1.layers[3], wider_noise_1.layers[4], growth_ratio, 0.01)
    wider_inceptionA(wider_noise_1.layers[4], wider_noise_1.layers[5], wider_noise_1.layers[7], growth_ratio, 0.01)
    wider_inceptionB(wider_noise_1.layers[5], wider_noise_1.layers[7], growth_ratio, 0.01)

    wider_noise_2 = deepcopy(teacher)
    wider_inceptionA(wider_noise_2.layers[3], wider_noise_2.layers[4], growth_ratio, 0.05)
    wider_inceptionA(wider_noise_2.layers[4], wider_noise_2.layers[5], wider_noise_2.layers[7], growth_ratio, 0.05)
    wider_inceptionB(wider_noise_2.layers[5], wider_noise_2.layers[7], growth_ratio, 0.05)

    wider_noise_3 = deepcopy(teacher)
    wider_inceptionA(wider_noise_3.layers[3], wider_noise_3.layers[4], growth_ratio, 0.1)
    wider_inceptionA(wider_noise_3.layers[4], wider_noise_3.layers[5], wider_noise_3.layers[7], growth_ratio, 0.1)
    wider_inceptionB(wider_noise_3.layers[5], wider_noise_3.layers[7], growth_ratio, 0.1)

    results, wider = train_results(dtrn, dtst, "inception_sm_exp_wider_no_noise.jld2", wider_no_noise, 5, true)
    println("Wider no noise results: ", results)
    writedlm("res/noise_res_no.txt", results)

    results, wider = train_results(dtrn, dtst, "inception_sm_exp_wider_noise_2.jld2", wider_noise_2, 5, true)
    println("Wider noise 2 results: ", results)
    writedlm("res/noise_res_2.txt", results)

    results, wider = train_results(dtrn, dtst, "inception_sm_exp_wider_noise_3.jld2", wider_noise_3, 5, true)
    println("Wider noise 3 results: ", results)
    writedlm("res/noise_res_3.txt", results)
end

function run_experiments()
    (xtrn, ytrn), (xtst, ytst) = load_data()

    dtrn = minibatch(xtrn, ytrn, 50, xtype=atype())
    dtst = minibatch(xtst, ytst, 50, xtype=atype())

    wider_experiment(dtrn, dtst)
    deeper_experiment(dtrn, dtst)
    explore_experiment(dtrn, dtst)
    noise_experiment(dtrn, dtst)
end

function perform_tests()
    test_wider_mlp()
    test_wider_conv()
    test_wider_inception()
    test_random_pad_inception()
    test_deeper_conv()
    test_deeper_inception()
end

perform_tests()
run_experiments()


text:
using BinaryBuilder

# Collection of sources required to build Nettle
name = "OpenSSL"
version = v"1.1.1"
sources = [
    ArchiveSource("https://www.openssl.org/source/openssl-1.1.1e.tar.gz",
                  "694f61ac11cb51c9bf73f54e771ff6022b0327a43bbdfa1b2f19de1662a6dcbe"),
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/openssl-*/

# This build system does not like llvm-ranlib
if [[ ${target} == *darwin* ]]; then
    export RANLIB=/opt/${target}/bin/${target}-ranlib
fi

# Manual translation of BB $target to Configure-target
function translate_target()
{
    if [[ ${target} == x86_64-linux* ]]; then
        echo linux-x86_64
    elif [[ ${target} == i686-linux* ]]; then
        echo linux-x86
    elif [[ ${target} == arm-linux* ]]; then
        echo linux-armv4
    elif [[ ${target} == aarch64-linux* ]]; then
        echo linux-aarch64
    elif [[ ${target} == powerpc64le-linux* ]]; then
        echo linux-ppc64le
    elif [[ ${target} == x86_64-apple-darwin* ]]; then
        echo darwin64-x86_64-cc
    elif [[ ${target} == x86_64-unknown-freebsd* ]]; then
        echo BSD-x86_64
    elif [[ ${target} == x86_64*mingw* ]]; then
        echo mingw64
    elif [[ ${target} == i686*mingw* ]]; then
        echo mingw
    else
        if [[ ${nbits} == 32 ]]; then
            echo linux-generic32
        else
            echo linux-generic64
        fi
    fi
}

./Configure shared --prefix=$prefix $(translate_target)
make -j${nproc}
make install_sw
"""

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line
platforms = supported_platforms()

# The products that we will ensure are always built.  What are these naming conventions guys?  Seriously?!
products = [
    LibraryProduct(["libcrypto", "libcrypto-1_1", "libcrypto-1_1-x64"], :libcrypto),
    LibraryProduct(["libssl", "libssl-1_1", "libssl-1_1-x64"], :libssl),
    ExecutableProduct("openssl", :openssl),
]

# Dependencies that must be installed before this package can be built
dependencies = [
]

# Build the tarballs.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies)

max_stars_repo_name: jpsamaroo/Yggdrasil

max_issues_repo_name: jpsamaroo/Yggdrasil

max_forks_repo_name: jpsamaroo/Yggdrasil


text:
using DrWatson
@quickactivate "ExercisesRepo"
include(srcdir("colorscheme.jl"))

# %% The very first code snippet of the course
using DynamicalSystems # load the library

function lorenz_rule(u, p, t)
    σ, ρ, β = p
    x, y, z = u
    dx = σ*(y - x)
    dy = x*(ρ - z) - y
    dz = x*y - β*z
    return SVector(dx, dy, dz) # Static Vector
end

p  = [10.0, 28.0, 8/3] # parameters: σ, ρ, β
u₀ = [0, 10.0, 0]      # initial state
# create an instance of a `DynamicalSystem`
lorenz = ContinuousDynamicalSystem(lorenz_rule, u₀, p)

T  = 100.0 # total time
dt = 0.01  # sampling time
A  = trajectory(lorenz, T; dt)

# %% Animate evolution of trajectories in the Standard map
using InteractiveChaos
using DynamicalSystems
using Makie

# Standard map trajectories
ds = Systems.standardmap(; k = 1.0)
u0s = [[θ, p] for θ ∈ 0:2π for p ∈ 0:2π]
lims = ((0, 2π), (0, 2π))

scene, main, layout, obs = interactive_evolution(
    ds, u0s; tail = 1000, lims,
)
main.xlabel = "θ"
main.ylabel = "p"

# %% Lorenz system trajectories
using OrdinaryDiffEq: Tsit5, Vern9
ds = Systems.lorenz()

u0s =  [[10,20,40.0] .+ rand(3) for _ in 1:7]

diffeq = (alg = Tsit5(), dtmax = 0.01)

scene, main, layout, obs = interactive_evolution(
    ds, u0s; tail = 1000, diffeq, colors = COLORS,
)

# %% Add poincare plane
import AbstractPlotting
using AbstractPlotting.MakieLayout: LAxis
o = AbstractPlotting.Point3f0(-25, 0, 0)
w = AbstractPlotting.Point3f0(50, 0, 50)
p = AbstractPlotting.FRect3D(o, w)

# These lines are necessary for transparent planes
a = AbstractPlotting.RGBAf0(0,0,0,0)
c = AbstractPlotting.RGBAf0(0.2, 0.2, 0.8, 1.0)
img = AbstractPlotting.ImagePattern([c a; a c]);
AbstractPlotting.mesh!(main, p; color = img);

# %% Plot Poincare sos
psosplot = layout[:, 2] = LAxis(scene)
psos = poincaresos(ds, (2, 0.0), 2000.0)
AbstractPlotting.scatter!(psosplot, psos[:, 1], psos[:, 3])

display(scene)

# %% Henon heiles system trajectories
ds = Systems.henonheiles()

u0s = [[0.0, -0.25, 0.42081, 0.0],
[0.0, 0.1, 0.5, 0.0],
[0.0, -0.31596, 0.354461, 0.0591255]]

diffeq = (alg = Vern9(), dtmax = 0.01)
idxs = (1, 2)

scene, main, layout, obs = interactive_evolution(
    ds, u0s; idxs, tail = 2000, diffeq, colors = COLORS,
)
main.scene[AbstractPlotting.Axis][:names, :axisnames] = ("q₁", "q₂", "p₂")

max_stars_repo_name: moritz-g/ExercisesRepo

max_issues_repo_name: moritz-g/ExercisesRepo

max_forks_repo_name: moritz-g/ExercisesRepo


text:
;; ---------------------------------------------------------------------
;;	json_create.jl
;;
;;					Jul/25/2012
;;
;; --------------------------------------------------------------------
(require 'tables)
(load-file "/var/www/data_base/common/rep_common/text_manipulate.jl")
;; --------------------------------------------------------------------
(defun tag_out_proc (tag value)
	(setq out_str_aa (concat 'string "\"" tag "\": "))
	(setq out_str_bb (concat 'string "\"" value "\""))
	(concat 'string out_str_aa out_str_bb)
	)
;
;; --------------------------------------------------------------------
(defun json_dict_write_proc_single (key unit_bb)
	(setq out_name (tag_out_proc "name" (table-ref unit_bb "name")))
	(setq out_population (tag_out_proc "population" (table-ref unit_bb "population")))
	(setq out_date (tag_out_proc "date_mod" (table-ref unit_bb "date_mod")))
	(setq out_str
		(concat 'string
		"\"" key "\": {" out_name "," out_population "," out_date "},\n")
		)
	)

;; --------------------------------------------------------------------
(defun json_write_proc (file_out dict_aa)
(let ((fp-file (open-file file_out 'write)))
	(setq out_str "{")
	(table-walk (lambda (key value)
		(setq out_str (concat 'string out_str
		(json_dict_write_proc_single key value))))
	dict_aa)
	(setq llx (length out_str))
	(setq out_str (substring out_str 0 (- llx 2)))
	(setq out_str (concat 'string out_str "}\n"))
	(write fp-file out_str)
	)
)

;; --------------------------------------------------------------------
(defun data_prepare_proc ()
	(setq dict_aa (make-table string-hash string=))
;
	(dict_append_proc dict_aa "t0921" "宇都宮" "32891" "1930-3-5")
	(dict_append_proc dict_aa "t0922" "小山" "12467" "1930-10-21")
	(dict_append_proc dict_aa "t0923" "佐野" "34593" "1930-5-16")
	(dict_append_proc dict_aa "t0924" "足利" "52492" "1930-9-23")
	(dict_append_proc dict_aa "t0925" "日光" "41851" "1930-8-9")
	(dict_append_proc dict_aa "t0926" "下野" "82753" "1930-5-19")
	(dict_append_proc dict_aa "t0927" "さくら" "51848" "1930-9-12")
	(dict_append_proc dict_aa "t0928" "矢板" "23752" "1930-1-18")
	(dict_append_proc dict_aa "t0929" "真岡" "51264" "1930-2-11")
	(dict_append_proc dict_aa "t0930" "栃木" "37189" "1930-9-24")
	(dict_append_proc dict_aa "t0931" "大田原" "18954" "1930-6-8")
	(dict_append_proc dict_aa "t0932" "鹿沼" "83954" "1930-5-7")
	(dict_append_proc dict_aa "t0933" "那須塩原" "95274" "1930-8-12")
	(dict_append_proc dict_aa "t0934" "那須烏山" "64235" "1930-9-21")
)

;; --------------------------------------------------------------------
(format t "*** 開始 ***")
(setq file_out (cadr command-line-args))
(format t file_out)

(setq dict_aa (data_prepare_proc))
(table-walk dict_display_proc_single dict_aa)

(json_write_proc file_out dict_aa)

(format t "*** 終了 ***")

;; ---------------------------------------------------------------------

max_stars_repo_licenses: MIT Unlicense

max_issues_repo_licenses: MIT Unlicense

max_forks_repo_licenses: MIT Unlicense


text:
# Define methods used in LSMR

##############################################################################
##
## FixedEffectCoefficients : vector x in A'Ax = A'b
##
## We define these methods used in lsmr! (duck typing):
## copyto!, fill!, rmul!, axpy!, norm
##
## Do not define iteration on each fixedeffect since it would conflict with eltype
##
##############################################################################

struct FixedEffectCoefficients{U <: AbstractVector}
	x::Vector{U}
end

Base.eltype(fecoef::FixedEffectCoefficients{U}) where {U} = eltype(U)
Base.length(fecoef::FixedEffectCoefficients) = sum(length(x) for x in fecoef.x)
LinearAlgebra.norm(fecoef::FixedEffectCoefficients) = sqrt(sum(sum(abs2, x) for x in fecoef.x))

function Base.fill!(fecoef::FixedEffectCoefficients, α::Number)
	for x in fecoef.x
		fill!(x, α)
	end
	return fecoef
end

function LinearAlgebra.rmul!(fecoef::FixedEffectCoefficients, α::Number)
	for x in fecoef.x
		rmul!(x, α)
	end
	return fecoef
end

function Base.copyto!(fecoef1::FixedEffectCoefficients, fecoef2::FixedEffectCoefficients)
	for (x1, x2) in zip(fecoef1.x, fecoef2.x)
		copyto!(x1, x2)
	end
	return fecoef1
end

function LinearAlgebra.axpy!(α::Number, fecoef1::FixedEffectCoefficients, fecoef2::FixedEffectCoefficients)
	for (x1, x2) in zip(fecoef1.x, fecoef2.x)
		axpy!(α, x1, x2)
	end
	return fecoef2
end

max_issues_repo_name: matthieugomez/FixedEffects

max_forks_repo_name: matthieugomez/FixedEffects


text:
using FourierGPE

#--- parameters
L = (30.,30.,8.)
N = (128,128,32)
sim = Sim(L,N)
@unpack_Sim sim;

#--- Initialize simulation
γ = 0.05
μ = 25.0
tf = 2.5/γ
Nt = 200
t = LinRange(0.,tf,Nt)

# potential
import FourierGPE.V
V(x,y,z,t) = 4*z^2

#--- random initial state
x,y,z = X
ψi = randn(N)+im*randn(N)
ϕi = kspace(ψi,sim)

@pack_Sim! sim;

#--- Evolve in k space
@time sol = runsim(sim); # will take a few minutes to run.

#--- Density isosurface
using Makie, AbstractPlotting

function dense(phi)
    ψm = xspace(phi,sim)
    density = abs2.(ψm)
    pmax = maximum(density)
    return density/pmax
end

function densityfilm(sol,Nt;file="3dquenchtrap.gif")
    saveto=joinpath("media",file)
    scene = Scene()
    tindex = Node(1)
    scene = volume(lift(i -> dense(sol[i]), tindex),
    algorithm = :iso,
    color = (:mediumseagreen,0.25),
    show_axis=false,
    isovalue=3f0(.15))

    R = 210
    eyeat = Vec3f0(R,R,0)
    lookat = Vec3f0(0,0,-50)

    record(scene, saveto, 1:Nt) do i
        update_cam!(scene, eyeat, lookat)
        rotate_cam!(scene, 0., -0.4, 0.)
        tindex[] = i
    end
    p = scene[end];
    return
end

p = densityfilm(sol,Nt,file="3dquenchslab3.gif")


text:
# Copyright (C) 2021 Ilya Baran.  This program is distributed under the terms of the MIT license.

using OrdinaryDiffEq
using MAT

abstract type AbstractBlock end

struct BlockOutputDefinition
    outputs::Variables
    computeOutputs::Function # Takes variables, parameters, time range, solution (in Variables form), and outputs, and returns outputs
    includeRawSolution::Bool # If true, computeOutputs takes a raw solution before solution-as-Variables
end

mutable struct BlockExtraData
    timeRange::Union{AbstractRange, Nothing}
    discontinuities::Union{Function, Nothing} # Takes parameters, timeRange, returns vector of discontinuities
    outputDefinition::Union{BlockOutputDefinition, Nothing}
    BlockExtraData() = new(nothing, nothing, nothing)
end

struct Block <: AbstractBlock
    variables::Variables
    parameters::Variables
    reactions::Array{AbstractReaction}
    extraData::BlockExtraData
    Block(v, p, r) = new(deepcopy(v), deepcopy(p), deepcopy(r), BlockExtraData());
end

function getExtraData(block::AbstractBlock)::BlockExtraData block.extraData end

function getTimeRange(block::AbstractBlock)::AbstractRange
    range = getExtraData(block).timeRange;
    if range === nothing
        error("Expected time range but it was missing");
    end
    range;
end

function setTimeRange!(block::AbstractBlock, timeRange::AbstractRange) getExtraData(block).timeRange = timeRange; end

function getOutputDefinition(block::AbstractBlock)::BlockOutputDefinition
    outputs = getExtraData(block).outputDefinition;
    if outputs === nothing
        error("Expected output definition but it was missing");
    end
    outputs;
end

function setOutputDefinition!(block::AbstractBlock, outputs::Variables, computeOutputs::Function)
    getExtraData(block).outputDefinition = BlockOutputDefinition(outputs, computeOutputs, false);
end

function setOutputDefinition!(block::AbstractBlock, outputs::Variables, computeOutputs::Function, includeRawSolution::Bool)
    getExtraData(block).outputDefinition = BlockOutputDefinition(outputs, computeOutputs, includeRawSolution);
end

function renameOutputs!(block::AbstractBlock, renameFunction::Function)
    oldDef = getOutputDefinition(block);
    newOutputs = renameVariables(oldDef.outputs, renameFunction);
    if oldDef.includeRawSolution
        newComputeFunction = (v, p, t, r, s, o) -> begin
            oldOutputs = oldDef.computeOutputs(v, p, t, r, s, deepcopy(oldDef.outputs));
            o.values = oldOutputs.values;
            return o;
        end;
    else
        newComputeFunction = (v, p, t, s, o) -> begin
            oldOutputs = oldDef.computeOutputs(v, p, t, s, deepcopy(oldDef.outputs));
            o.values = oldOutputs.values;
            return o;
        end;
    end
    setOutputDefinition!(block, newOutputs, newComputeFunction, oldDef.includeRawSolution);
end

function getDiscontinuities(block::AbstractBlock)::Vector
    d = getExtraData(block).discontinuities;
    if d === nothing
        return []
    end
    return d(getParameters(block), getTimeRange(block));
end

function setDiscontinuities!(block::AbstractBlock, discontinuities::T) where T <: Function
    getExtraData(block).discontinuities = discontinuities;
end

function runBlock(block::AbstractBlock)
    timeRange = getTimeRange(block);
    floatInterval::Tuple{Float64, Float64} = convert(Tuple{Float64, Float64}, (minimum(timeRange), maximum(timeRange)));
    problem = ODEProblem((x, p, t) -> computeDerivatives(block, t, x), Vector{Float64}(getVariables(block).values), floatInterval);
    solve(problem, Rodas4P(), tstops = getDiscontinuities(block));
end

function setParameters!(block::AbstractBlock, nameToValue::AbstractDict{String, T}) where T
    for (name, value) in nameToValue
        setParameter!(block, name, value);
    end
end

function solutionToVariables(solution, block::AbstractBlock, timeRange::AbstractRange)
    resultArray = Array{Array{Float64, 1}, 1}(undef, length(getVariables(block).variables));
    for index = 1:length(resultArray)
        resultArray[index] = solution(timeRange, idxs=index).u;
    end
    return Variables(getVariables(block), resultArray);
end

function solutionToMatrix(solution, timeRange::AbstractRange)::Matrix
    return Matrix(Array(solution(timeRange)));
end

function solutionToMatlab(solution, timeRange::AbstractRange, filename::String)
    t = Vector(timeRange);
    y = solutionToMatrix(solution, timeRange);
    status = string(solution.retcode);
    file = matopen(filename, "w");
    write(file, "t", t);
    write(file, "y", y);
    write(file, "status", status);
    close(file);
end

function computeDerivatives(block::Block, t::Number, x::Vector)::Vector
    derivatives = Variables(block.variables, zero(t * x));
    variables = Variables(block.variables, x);
    extraVariables = Dict{String, Any}();
    for reaction = block.reactions
        apply!(reaction, derivatives, t, variables, block.parameters, extraVariables);
    end
    #println("Time = $t");#, x = $x, result = $(derivatives.values)");
    vType = typeof(t * x);
    return vType(derivatives.values);
end

function getVariables(block::Block)::Variables block.variables; end
function getParameters(block::Block)::Variables block.parameters; end
function setParameter!(block::Block, name::String, value)
    block.parameters[name] = value;
end
function setParameters!(block::Block, values::Vector)
    block.parameters.values = values;
end

function computeOutputs(blockWithOutputs::AbstractBlock)
    timeRange = getTimeRange(blockWithOutputs);
    solution = runBlock(blockWithOutputs);
    return computeOutputsFromSolution(blockWithOutputs, timeRange, solution);
end

# ============================================ BlockWithBindings =======================================
struct BlockWithBindings <: AbstractBlock
    subblock::AbstractBlock
    parameterToBinding::Dict{String, Function} # Function takes t, variables, and existing parameters and returns a value
    parameters::Variables
    extraData::BlockExtraData
end

function BlockWithBindings(subblock::AbstractBlock, parameterToBinding::AbstractDict{String, T}) where T
    subblock = deepcopy(subblock);
    subblockParameters::Variables = getParameters(subblock);
    blockParameterToBinding = Dict{String, Function}();
    for (parameter, binding) in parameterToBinding
        if !haskey(subblockParameters.nameToIndex, parameter)
            error("Cannot bind nonexistent parameter $(parameter)");
        end
        if isa(binding, Function)
            blockParameterToBinding[parameter] = binding;
        else
            setParameter!(subblock, parameter, binding);
        end
    end
    newParameters = variablesSubtract(subblockParameters, Set(keys(parameterToBinding)));
    extraData = BlockExtraData();
    extraData.timeRange = getExtraData(subblock).timeRange;
    return BlockWithBindings(subblock, blockParameterToBinding, newParameters, extraData);
end

# Keep the parameters that are provided, and bind the rest to their current values
function BlockWithBindings(subblock::AbstractBlock, parametersToKeep::AbstractArray{String})
    subblock = deepcopy(subblock);
    subblockParameters::Variables = getParameters(subblock);
    newParameters::Array{Variable} = [];
    newValues::Array = [];
    for parameter in parametersToKeep
        if !haskey(subblockParameters.nameToIndex, parameter)
            error("Cannot keep nonexistent parameter $(parameter)");
        end
        index = subblockParameters.nameToIndex[parameter];
        variable = subblockParameters.variables[index];
        push!(newParameters, variable);
        push!(newValues, subblockParameters.values[index]);
    end
    newParametersV = Variables(newParameters);
    newParametersV.values = newValues;
    extraData = BlockExtraData();
    extraData.timeRange = getExtraData(subblock).timeRange;
    return BlockWithBindings(subblock, Dict{String, Function}(), newParametersV, extraData);
end

function getVariables(block::BlockWithBindings)::Variables getVariables(block.subblock); end
function getParameters(block::BlockWithBindings)::Variables block.parameters; end
function setParameter!(block::BlockWithBindings, name::String, value)
    block.parameters[name] = value;
    setParameter!(block.subblock, name, value);
end
function setParameters!(block::BlockWithBindings, values::Vector)
    block.parameters.values = values;
    for (name, index) in block.parameters.nameToIndex
        setParameter!(block.subblock, name, block.parameters.values[index]);
    end
end

function computeDerivatives(block::BlockWithBindings, t::Number, x::Vector)::Vector
    blockVariables = Variables(getVariables(block), x);
    for (parameter, binding) in block.parameterToBinding
        setParameter!(block.subblock, parameter, binding(t, blockVariables, block.parameters));
    end
    return computeDerivatives(block.subblock, t, x);
end

function computeOutputsFromSolution(block::AbstractBlock, timeRange, solution)
    outputDefinition = getOutputDefinition(block)
    if outputDefinition.includeRawSolution
        return outputDefinition.computeOutputs(getVariables(block), getParameters(block), timeRange,
                                            solution, solutionToVariables(solution, block, timeRange),
                                            deepcopy(outputDefinition.outputs));
    else
        return outputDefinition.computeOutputs(getVariables(block), getParameters(block), timeRange,
                                            solutionToVariables(solution, block, timeRange),
                                            deepcopy(outputDefinition.outputs));
    end
end

function recursiveComputeOutputs(block::AbstractBlock, timeRange, solution)
    computeOutputsFromSolution(block, timeRange, solution);
end

function recursiveComputeOutputs(block::BlockWithBindings, timeRange, solution)
    outputDefinition = getExtraData(block).outputDefinition;
    if (outputDefinition === nothing)
        return recursiveComputeOutputs(block.subblock, timeRange, solution);
    end
    computeOutputsFromSolution(block, timeRange, solution);
end

function computeOutputs(blockWithOutputs::BlockWithBindings)
    timeRange = getTimeRange(blockWithOutputs);
    solution = runBlock(blockWithOutputs);
    recursiveComputeOutputs(blockWithOutputs, timeRange, solution);
end

function getDiscontinuities(block::BlockWithBindings)::Vector
    d = getExtraData(block).discontinuities;
    if d === nothing
        return getDiscontinuities(block.subblock);
    end
    return d(getParameters(block), getTimeRange(block));
end

# ============================================ BlockCombo =======================================

const EXTRA_NAME = "_extra_";

struct BlockCombo <: AbstractBlock
    subblocks::Vector{Tuple{String, AbstractBlock}}
    parameters::Variables
    parameterNameToBlocksAndNames::Dict{String, Vector{Tuple{String, String}}} # For setting parameters
    glueFunctions::Vector{Tuple{String, String, Function}} # Block name, parameter name, function that computes it
    # Glue functions take time, variables, parameters (of the entire block)
    variables::Variables
    extraData::BlockExtraData
end

function BlockCombo(subblocks::Vector{Tuple{String, T}},
                    glueFunctions::Vector{Tuple{String, String, F}},
                    extraParameters::Variables) where {T <: AbstractBlock, F <: Function}
    if (length(subblocks) < 1)
        error("Must have at least one subblock");
    end
    extraData = deepcopy(getExtraData(subblocks[1][2]));
    subblocks = deepcopy(subblocks);
    gluedParameters = Set((f[1], f[2]) for f in glueFunctions);

    # Gather parameters
    seenNames = Set{String}();
    parameterArray = Vector{Variable}();
    parameterValueArray = [];
    parameterNameToBlocksAndNames = Dict{String, Vector{Tuple{String, String}}}();

    namesAndParameterSets = [(b[1], getParameters(b[2])) for b in subblocks];
    push!(namesAndParameterSets, (EXTRA_NAME, extraParameters));

    for namesAndParameters in namesAndParameterSets
        parameters = namesAndParameters[2];
        for parameter::Variable in parameters.variables
            if (in((namesAndParameters[1], parameter.name), gluedParameters))
                continue;
            end
            if !in(parameter.name, seenNames)
                push!(seenNames, parameter.name);
                push!(parameterArray, parameter);
                push!(parameterValueArray, parameters.values[parameters.nameToIndex[parameter.name]]);
            end
            if (namesAndParameters[1] != EXTRA_NAME)
                push!(get!(parameterNameToBlocksAndNames, parameter.name, []), (namesAndParameters[1], parameter.name));
            end
        end
    end

    parameters = Variables(parameterArray);

    variableArray = Vector{Variable}();
    variableValueArray = [];

    for nameAndSubblock in subblocks
        variables = getVariables(nameAndSubblock[2]);
        for variable in variables.variables
            push!(variableArray, variable);
            push!(variableValueArray, variables.values[variables.nameToIndex[variable.name]]);
        end
    end

    variables = Variables(variableArray);
    variables.values = variableValueArray;

    result = BlockCombo(subblocks, parameters, parameterNameToBlocksAndNames, glueFunctions, variables, extraData);
    setParameters!(result, parameterValueArray); # Propagate to subblocks
    result;
end

function getSubblock(block::BlockCombo, name::String)
    for (subblockName, subblock) in block.subblocks
        if name == subblockName
            return subblock;
        end
    end
    error("Subblock $name not found");
end

function getVariables(block::BlockCombo)::Variables block.variables; end
function getParameters(block::BlockCombo)::Variables block.parameters; end
function setParameter!(block::BlockCombo, name::String, value)
    block.parameters[name] = value;
    if haskey(block.parameterNameToBlocksAndNames, name)
        blocksAndNames = block.parameterNameToBlocksAndNames[name];
        for blockAndName in blocksAndNames
            setParameter!(getSubblock(block, blockAndName[1]), blockAndName[2], value);
        end
    end
end

function setParameters!(block::BlockCombo, values::Vector)
    block.parameters.values = values;

    for parameter in block.parameters.variables
        blocksAndNames = get(block.parameterNameToBlocksAndNames, parameter.name, nothing);
        if blocksAndNames === nothing
            continue;
        end
        for blockAndName in blocksAndNames
            setParameter!(getSubblock(block, blockAndName[1]), blockAndName[2], values[block.parameters.nameToIndex[parameter.name]]);
        end
    end
end

function computeDerivatives(block::BlockCombo, t::Number, x::Vector)::Vector
    blockVariables = Variables(block.variables, x);
    for nameParameterAndGlue in block.glueFunctions
        value = nameParameterAndGlue[3](t, blockVariables, block.parameters);
        setParameter!(getSubblock(block, nameParameterAndGlue[1]), nameParameterAndGlue[2], value);
    end

    derivatives = [];
    variableIndex = 1;
    for (_, subblock) in block.subblocks
        nVariables = length(getVariables(subblock).variables);
        push!(derivatives, computeDerivatives(subblock, t, x[variableIndex:(variableIndex + nVariables - 1)]))
        variableIndex += nVariables;
    end

    vcat(derivatives...);
end

max_stars_repo_name: ilya-baran-personal/modelBlocks

max_issues_repo_name: ilya-baran-personal/modelBlocks

max_forks_repo_name: ilya-baran-personal/modelBlocks


text:
# Autogenerated wrapper script for Icestorm_jll for aarch64-linux-gnu-cxx11
export icebox_asc2hlc, icebox_chipdb, icebox_colbuf, icebox_diff, icebox_explain, icebox_hlc2asc, icebox_html, icebox_maps, icebox_stat, icebox_vlog, icebram, icemulti, icepack, icepll, icetime, iceunpack

JLLWrappers.@generate_wrapper_header("Icestorm")
JLLWrappers.@declare_executable_product(icebox_asc2hlc)
JLLWrappers.@declare_executable_product(icebox_chipdb)
JLLWrappers.@declare_executable_product(icebox_colbuf)
JLLWrappers.@declare_executable_product(icebox_diff)
JLLWrappers.@declare_executable_product(icebox_explain)
JLLWrappers.@declare_executable_product(icebox_hlc2asc)
JLLWrappers.@declare_executable_product(icebox_html)
JLLWrappers.@declare_executable_product(icebox_maps)
JLLWrappers.@declare_executable_product(icebox_stat)
JLLWrappers.@declare_executable_product(icebox_vlog)
JLLWrappers.@declare_executable_product(icebram)
JLLWrappers.@declare_executable_product(icemulti)
JLLWrappers.@declare_executable_product(icepack)
JLLWrappers.@declare_executable_product(icepll)
JLLWrappers.@declare_executable_product(icetime)
JLLWrappers.@declare_executable_product(iceunpack)
function __init__()
    JLLWrappers.@generate_init_header()
    JLLWrappers.@init_executable_product(
        icebox_asc2hlc,
        "bin/icebox_asc2hlc",
    )

    JLLWrappers.@init_executable_product(
        icebox_chipdb,
        "bin/icebox_chipdb",
    )

    JLLWrappers.@init_executable_product(
        icebox_colbuf,
        "bin/icebox_colbuf",
    )

    JLLWrappers.@init_executable_product(
        icebox_diff,
        "bin/icebox_diff",
    )

    JLLWrappers.@init_executable_product(
        icebox_explain,
        "bin/icebox_explain",
    )

    JLLWrappers.@init_executable_product(
        icebox_hlc2asc,
        "bin/icebox_hlc2asc",
    )

    JLLWrappers.@init_executable_product(
        icebox_html,
        "bin/icebox_html",
    )

    JLLWrappers.@init_executable_product(
        icebox_maps,
        "bin/icebox_maps",
    )

    JLLWrappers.@init_executable_product(
        icebox_stat,
        "bin/icebox_stat",
    )

    JLLWrappers.@init_executable_product(
        icebox_vlog,
        "bin/icebox_vlog",
    )

    JLLWrappers.@init_executable_product(
        icebram,
        "bin/icebram",
    )

    JLLWrappers.@init_executable_product(
        icemulti,
        "bin/icemulti",
    )

    JLLWrappers.@init_executable_product(
        icepack,
        "bin/icepack",
    )

    JLLWrappers.@init_executable_product(
        icepll,
        "bin/icepll",
    )

    JLLWrappers.@init_executable_product(
        icetime,
        "bin/icetime",
    )

    JLLWrappers.@init_executable_product(
        iceunpack,
        "bin/iceunpack",
    )

    JLLWrappers.@generate_init_footer()
end  # __init__()


text:
using MapImages
import Sario: DemRsc
using Test
using Documenter

DocMeta.setdocmeta!(MapImages, :DocTestSetup, :(using MapImages; import Sario: DemRsc); recursive=true)

@testset "MapImages.jl" begin
    doctest(MapImages)

    test_data = reshape(collect(1:30), (6, 5))
    demrsc = DemRsc(x_first=1.0, y_first=2.0, x_step=0.1, y_step=-0.2, file_length=6, width=5)
    mapim = MapImage(test_data, demrsc)
    out = MapImages.crop_demrsc(demrsc, new_rows=2, new_cols=2)

    @test out.width == 2
    @test out.file_length == 2

    out = MapImages.crop_demrsc(demrsc, row_start=2, col_start=2, new_rows=2, new_cols=2)
    @test out.x_first == 1.1
    @test out.y_first == 1.8

    out = MapImages.crop_demrsc(demrsc, new_rows=2, new_cols=2, row_step=2, col_step=2)
    @test out.x_step == 0.2
    @test out.y_step == -0.4

end


text:
using LibGit2, GRUtils

const srcpath = joinpath(dirname(pathof(GRUtils)), "..")
const docpath = joinpath(DEPOT_PATH[1], "localdocs/GRUtils")

function deploylatest()
    srcrepo = LibGit2.GitRepo(srcpath)
    docrepo = LibGit2.GitRepo(docpath)
    # 1. Check that the repos are not dirty and in the appropriate branches
    if LibGit2.isdirty(srcrepo)
        throw(ErrorException("Dirty source repository!"))
    elseif LibGit2.isdirty(docrepo)
        throw(ErrorException("Dirty docs repository!"))
    end
    src_head = LibGit2.head(srcrepo)
    doc_head = LibGit2.head(docrepo)
    if match(r"/heads/master$", LibGit2.name(src_head)) isa Nothing
        throw(ErrorException("Source repository should be in master branch"))
    elseif match(r"/heads/gh-pages$", LibGit2.name(doc_head)) isa Nothing
        throw(ErrorException("Docs repository should be in the branch gh-pages"))
    end
    # 2. Build docs and chek if it's ok
    include("make.jl")
    ask(prompt="") = (println(prompt); readline())
    okstr = ask("Is the build ok? (y/n): ")
    (isempty(okstr) || okstr[1] ≠ 'y') && throw(ErrorException("Deployment aborted"))
    # 3. Create message for commit in doc repo
    head_commit = LibGit2.GitShortHash(LibGit2.GitHash(src_head), 7) |> string
    msg = "Build from $head_commit"
    # 4. Copy build in docs repo
    cp(joinpath(srcpath, "docs/build"), joinpath(docpath, "latest"), force=true)
    write(joinpath(docpath, "latest/siteinfo.js"), """var DOCUMENTER_CURRENT_VERSION = "latest";""")
    # 5. Add and commit to gh-pages
    LibGit2.add!(docrepo, "latest/*.*")
    commit = LibGit2.commit(docrepo, msg)
    LibGit2.branch!(docrepo, "gh-pages", string(commit))
end

function deploytag(tagname, addlink=true)
    tagname = "v" * string(VersionNumber(tagname))
    srcrepo = LibGit2.GitRepo(srcpath)
    docrepo = LibGit2.GitRepo(docpath)
    srctarget = LibGit2.GitTag(srcrepo, tagname) |> LibGit2.target
    targetmsg = "Build from $(string(LibGit2.GitShortHash(srctarget, 7)))"
    # Look for the commit with the target message
    targetcommits = String[]
    lookup_msg = (oid, repo) -> begin
        msg = LibGit2.message(LibGit2.GitCommit(repo, string(oid)))
        # push!(targetcommits, msg)
        if msg == targetmsg
            push!(targetcommits, string(oid))
        end
    end
    LibGit2.with(LibGit2.GitRevWalker(docrepo)) do walker
        LibGit2.map(lookup_msg, walker)
    end
    isempty(targetcommits) && return nothing
    # The target commit exists ...
    targethash = targetcommits[1]
    baseref = LibGit2.lookup_branch(docrepo, "gh-pages")
    LibGit2.checkout!(docrepo, string(targethash))
    cp(joinpath(docpath, "latest"), joinpath(docpath, tagname), force=true)
    write(joinpath(docpath, tagname, "siteinfo.js"), """var DOCUMENTER_CURRENT_VERSION = "$tagname";""")
    LibGit2.branch!(docrepo, "gh-pages", string(LibGit2.GitHash(baseref)))
    # Add links if requested
    if addlink
        # Create symlink to the minor version
        linkname = joinpath(docpath, tagname[1:end-2])
        islink(linkname) && rm(linkname)
        symlink(tagname, joinpath(docpath, tagname[1:end-2]))
        # Update list of versions
        versionlist = readlines(joinpath(docpath, "versions.js"))
        minortag = tagname[1:end-2]
        newstable = true
        for line = 1:length(versionlist)
            m = match(r"(v\d+\.\d+)", versionlist[line])
            m == nothing && continue # skip line
            version = m.captures[1]
            if version == minortag  # the version is already in the list
                if newstable
                    rm(joinpath(docpath, "stable"))
                    symlink(tagname, joinpath(docpath, "stable"))
                end
                break
            end
            if VersionNumber(version) < VersionNumber(minortag)
                # Add the version
                insert!(versionlist, line, "  \"$minortag\",")
                write(joinpath(docpath, "versions.js"), join(versionlist, "\n"))
                if newstable
                    rm(joinpath(docpath, "stable"))
                    symlink(tagname, joinpath(docpath, "stable"))
                end
                break
            end
            newstable = false # there were higher versions released
        end
    end
    # Add and commit to gh-pages
    LibGit2.add!(docrepo, "*")
    commit = LibGit2.commit(docrepo, "Deploy $tagname")
    LibGit2.branch!(docrepo, "gh-pages", string(commit))
end