text:
function updateDictionary(optr_t::Type{AG}, opt_params, cb::DT, x::AbstractArray, hy::AbstractArray, params; shape::Shapes.AbstractShape=Shapes.Vec(size(x)), vlevel::Integer=0, kwargs...) where {DT<:LearningTarget,TT<:AbstractArray,TM<:AbstractArray,AG<:Optimizers.AbstractGradientDescent}
    vecpm, pminfo = decompose_params(DT, params)
    f(t) = lossfcn(cb, x, hy, compose_params(DT, t, pminfo); shape=shape)
    ∇f(t) = ForwardDiff.gradient(f, t)
    optr = optr_t(∇f; opt_params...)

    vecpm_opt = optr(vecpm; kwargs...)

    params_opt = compose_params(DT, vecpm_opt, pminfo)
    loss_opt = f(vecpm_opt)
    return (params_opt, loss_opt,)
end

function updateDictionary(nlopt_t::Type{Optimizers.CRS}, opt_params, cb::DT, x::AbstractArray, hy::AbstractArray, params; shape=Shapes.Vec(size(x)), vlevel::Integer=0, kwargs...) where {DT<:LearningTarget}
    vecpm, pminfo = decompose_params(DT, params)
    f(t, grad=nothing) = lossfcn(cb, x, hy, compose_params(DT, t, pminfo); shape=shape)

    nlopt = nlopt_t(f; opt_params...)

    vecpm_opt = nlopt(vecpm)
    minf = f(vecpm_opt)
    params_opt = compose_params(DT, vecpm_opt, pminfo)
    # vlevel >= 0 && println("fopt(θ) = $minf, f(θ) = $(f(minx))")
    return (params_opt, minf)
end

function updateDictionary(nlopt_t::Type{Optimizers.GlobalOpt}, opt_params, cb::DT, x::AbstractArray, hy::AbstractArray, params; shape=Shapes.Vec(size(x)), vlevel::Integer=0, kwargs...) where {DT<:LearningTarget}
    vecpm, pminfo = decompose_params(DT, params)
    f(t, grad=nothing) = lossfcn(cb, x, hy, compose_params(DT, t, pminfo); shape=shape)

    nlopt = nlopt_t(f; opt_params...)

    vecpm_opt = nlopt(vecpm)
    minf = f(vecpm_opt)
    params_opt = compose_params(DT, vecpm_opt, pminfo)
    # vlevel >= 0 && println("fopt(θ) = $minf, f(θ) = $(f(minx))")
    return (params_opt, minf)
end

function lossfcn(cb::LearningTarget, x::AbstractArray, y::AbstractArray, params; shape=Shapes.Vec(size(x))) where {TT<:AbstractArray,TM<:AbstractArray}
    cpcb = similar_dictionary(cb, params)
    syn = createTransform(cpcb, shape)
    norm(x - synthesize(syn, y))^2/2
end

function similar_dictionary(nsolt::AbstractNsolt, (θ, μ)::Tuple{TT,TM}) where {TT<:AbstractArray,TM<:AbstractArray}
    setrotations!(similar(nsolt, eltype(θ)), θ, μ)
end

similar_dictionary(target::Multiscale, params::NTuple{N}) where {N} = Multiscale(similar_dictionary.(target.filterbanks, params)...)

getParamsDictionary(nsolt::AbstractNsolt) = getrotations(nsolt)
setParamsDictionary!(nsolt::AbstractNsolt, pm::NTuple{2}) = setrotations!(nsolt, pm...)

function getParamsDictionary(nsolt::Cnsolt)
    θ, μ = getrotations(nsolt)
    ([angle.(diag(nsolt.Φ)); θ], μ)
end

function setParamsDictionary(nsolt::Cnsolt, (ϕnθ, μ))
    P = nchannels(nsolt)
    nsolt.Φ .= Diagonal(cis.(ϕnθ[1:P]))
    return setrotations!(nsolt, ϕnθ[(P+1):end], μ)
end

getParamsDictionary(targets::Multiscale) = map(t->getParamsDictionary(t), targets.filterbanks)
setParamsDictionary!(targets::Multiscale, pm::NTuple) = map((t,p)->setParamsDictionary!(t, p), targets.filterbanks, pm)

getOperatorType_scs(::Type, ::Val) = AbstractOperator
getOperatorType_scs(::Type{NS}, ::Val{:convolution}) where {NS<:AbstractNsolt} = ConvolutionalOperator

decompose_params(::Type, params) = (params, ())
compose_params(::Type, params, ()) = params

decompose_params(::Type{NS}, (θ, μ)) where {NS<:AbstractNsolt} = (θ, μ)
compose_params(::Type{NS}, θ, μ) where {NS<:AbstractNsolt} = (θ, μ)

function decompose_params(::Type{Multiscale}, params::NTuple{N}) where {N} # T<:AbstractNsolt
    θs, μs = map(t->t[1], params), map(t->t[2], params)
    vcat(θs...), (length.(θs), μs)
end

function compose_params(::Type{Multiscale}, vp::AbstractArray, pminfo::Tuple) where {N}
    arrpms = map(rng->vp[rng],intervals(pminfo[1]))
    map((lhs,rhs)->(lhs,rhs,), arrpms, pminfo[2])
end

max_stars_repo_name: nixir/MDCDL

max_issues_repo_name: nixir/MDCDL

max_forks_repo_name: nixir/MDCDL


text:
function convex_to_cbf(problem, name, filename)
    # work-around for optional dependency on Convex
    c, A, b, cones, var_to_ranges, vartypes, conic_constraints = eval(Main,:(Convex.conic_problem($problem)))
    var_cones = fill((:Free, 1:size(A, 2)),1)

    dat = mpbtocbf(name, vec(full(c)), A, vec(full(b)), cones, var_cones, vartypes, :Min)

    writecbfdata(filename, dat)
end

ext: jl

max_stars_repo_name: mlubin/ConicBenchmarkUtilities

max_issues_repo_name: mlubin/ConicBenchmarkUtilities

max_forks_repo_name: mlubin/ConicBenchmarkUtilities


text:
@testset "Recombinations" begin

    rng = StableRNG(42)

    compare(iter1, iter2) = length(setdiff(Set(iter1), Set(iter2))) == 0

    pop = [
        [1.0, 0.0],
        [0.0, 1.0],
        [0.0, 0.0]
    ]

    @testset "ES" begin
        @test average(pop) ≈ [1/3.,1/3.]
        @test compare( (marriage(pop, rng=rng) for i in 1:100), ([p1,p2] for p1 in [0,1.], p2 in [0,1.]))
    end

    @testset "GA" begin

        Random.seed!(rng, 4)
        @test SPX([0:4;], [5:9;], rng=rng) == ([0, 1, 7, 8, 9], [5, 6, 2, 3, 4])
        Random.seed!(rng, 1)
        @test TPX([0:4;], [5:9;], rng=rng) == ([0, 1, 7, 8, 4], [5, 6, 2, 3, 9])
        Random.seed!(rng, 3)
        @test UX([0:4;], [5:9;], rng=rng)  == ([0, 6, 7, 8, 9], [5, 1, 2, 3, 4])
        Random.seed!(rng ,2)
        @test SHFX([0:4;], [5:9;], rng=rng) == ([5, 6, 2, 8, 9], [0, 1, 7, 3, 4])

        Random.seed!(rng, 3)
        @test DC([0:4;], [5:9;], rng=rng)  == ([5, 1, 2, 3, 9], [5, 6, 7, 3, 9])
        @test AX(pop[1],pop[2]) == ([0.5, 0.5], [0.5, 0.5])
        @test WAX([1.0,0.5])(pop[1], pop[2]) == ([1.0, 2.0], [1.0, 2.0])

        Random.seed!(rng, 1)
        @test map(sum, HX(pop[1], pop[2])) |> collect ≈ [1.0, 1.0]
        Random.seed!(rng, 1)
        @test map(abs∘first∘diff, LX()(pop[1], pop[2])) |> collect ≈ [1.0, 1.0]
        Random.seed!(rng, 1)
        @test mapslices(diff, hcat(MILX(1., 0., 1.)(Real[0, 0.0], Real[1, 0.0], rng=rng)...), dims=2) |> vec ≈ [1.0, 0.0]
        Random.seed!(rng, 1)
        @test sum(sum.(SBX(0.5)(pop[1], pop[2], rng=rng) .- ([0.5, 0.7684], [0.5, 0.2316]))) ≈ 0 atol=1e-10
        @test SBX(0.0)(pop[1], pop[2], rng=rng) == ([0.5, 0.5], [0.5, 0.5])

        Random.seed!(rng, 2)
        @test PMX([0:4;], [3,2,1,0,4], rng=rng) == ([3, 2, 1, 0, 4], [0, 1, 2, 3, 4])
        @test CX([1:8;],[2,4,6,8,7,5,3,1]) == ([1,2,6,4,7,5,3,8],[2,4,3,8,5,6,7,1])
        @test CX([8,4,7,3,6,2,5,1,9,0],collect(0:9)) == ([8,1,2,3,4,5,6,7,9,0],[0,4,7,3,6,2,5,1,8,9])
        Random.seed!(rng, 18)
        @test OX1([1:8;], [2,4,6,8,7,5,3,1], rng=rng) == ([3,4,6,8,7,5,1,2], [8,7,3,4,5,6,1,2])
        Random.seed!(rng, 18)
        @test OX2([1:8;], [2,4,6,8,7,5,3,1], rng=rng) == ([1,2,3,4,6,5,7,8], [2,4,3,8,7,5,6,1])
        Random.seed!(rng, 18)
        @test POS([1:8;], [2,4,6,8,7,5,3,1], rng=rng) == ([1,4,6,2,3,5,7,8], [4,2,3,8,7,6,5,1])

        xo = BSX(3)
        @testset "BSX" for i in 1:100
            off1, off2 = xo(Bool[1,0,1,0,1,0], Bool[1,1,1,1,0,0])
            @test off1[end] == 0 && off2[end] == 0
            @test sum(off1) == 3 && sum(off2) == 3
        end

        Random.seed!(rng, 1)
        off1, off2 = SSX([0:4;], [5:9;], rng=rng)
        @test all(i -> i in 0:9, off1)
        @test all(i -> i in 0:9, off2)

    end

    @testset "DE" begin
        v1 = collect(1:10)
        v2 = fill(0,10)

        xvr = BINX(0.0)
        @test first( xvr(v1, v2) )== v1
        xvr = BINX(1.0)
        @test last( xvr(v1, v2) )== v1
        xvr = BINX(0.5)
        m1, m2 = xvr(v1, v2)
        @test m1.+m2 == v1

        xvr = EXPX(0.0)
        @test sum(first( xvr(v1, v2) )) <= 10
        xvr = EXPX(1.0)
        @test first( xvr(v1, v2) )== v1
        xvr = EXPX(0.05)
        @test sum(first( xvr(v1, v2) )) <= 19
    end

end


text:
# MIT License

# Copyright (c) 2021 Fraunhofer-Chalmers Centre

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

########################################
# Converts the instances from http://soa.iti.es/files/RCmax.7z to csv format used by the LR algorithm
# Download and extract the instances, place this script in the same folder and the new files will be created side-by-side the old files.
# The instances are also available in the instance.7z, already on the csv format.
########################################
using DelimitedFiles

cd(@__DIR__ )
folders = [name  for  name in readdir() if isnothing(findfirst('.',name))]
for folder in folders
    for file in readdir(folder)
        A = readdlm(folder*"/"*file)
        B = A[3:end,2:2:end]
        B = collect(transpose(B))
        outfile = replace(file, "txt" => "csv")
        writedlm(folder*"/"*outfile, B, ",")
    end
end

max_stars_repo_name: Fraunhofer-Chalmers-Centre/RCmax

max_issues_repo_name: Fraunhofer-Chalmers-Centre/RCmax

max_forks_repo_name: Fraunhofer-Chalmers-Centre/RCmax


text:
module CalibrationErrorsDistributions

using Reexport

@reexport using CalibrationErrors
@reexport using Distributions
@reexport using KernelFunctions

using Distances: Distances, Euclidean, SqEuclidean
using ExactOptimalTransport: ExactOptimalTransport
using PDMats: PDMats
using Tulip: Tulip

using LinearAlgebra: LinearAlgebra

const OT = ExactOptimalTransport

include("distances/types.jl")
include("distances/wasserstein.jl")

include("normal.jl")
include("laplace.jl")
include("mvnormal.jl")
include("mixturemodel.jl")

include("deprecated.jl")

end


text:
module PathwayList

using CSV
using DataFrames

function getPathwayList(file)
    df = CSV.read(file,
                  delim="\t",
                  datarow=1,
                  quotechar="\\",
                  header=["pathway_id",
                          "pathway_name",
                          "short_name",
                          "colour"],
                  types=[String,
                         String,
                         String,
                         String])

    pathwayList = Dict()
    for row in eachrow(df)
        if !isnothing(row[Symbol("pathway_name")])
            pathwayName = row[Symbol("pathway_name")]
            pathwayList[pathwayName] = row
        end
    end

    return pathwayList
end

end

max_stars_repo_name: OICR/mp-biopath

max_issues_repo_name: OICR/mp-biopath

max_forks_repo_name: OICR/mp-biopath


text:
using DiffEqSensitivity, OrdinaryDiffEq, Zygote, Test

function fiip(du,u,p,t)
  du[1] = dx = p[1]*u[1] - p[2]*p[51]*p[75]*u[1]*u[2]
  du[2] = dy = -p[3]*p[81]*p[25]*u[2] + (sum(@view(p[4:end]))/100)*u[1]*u[2]
end
function foop(u,p,t)
  dx = p[1]*u[1] - p[2]*p[51]*p[75]*u[1]*u[2]
  dy = -p[3]*p[81]*p[25]*u[2] + (sum(@view(p[4:end]))/100)*p[4]*u[1]*u[2]
  [dx,dy]
end

p = [1.5,1.0,3.0,1.0]; u0 = [1.0;1.0]
p = reshape(vcat(p,ones(100)),4,26)
prob = ODEProblem(fiip,u0,(0.0,10.0),p)
proboop = ODEProblem(foop,u0,(0.0,10.0),p)

loss = (u0,p)->sum(solve(prob,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=ForwardDiffSensitivity()))
@time du01,dp1 = Zygote.gradient(loss,u0,p)

loss = (u0,p)->sum(solve(prob,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=InterpolatingAdjoint()))
@time du02,dp2 = Zygote.gradient(loss,u0,p)

loss = (u0,p)->sum(solve(prob,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=ForwardDiffSensitivity(chunk_size=104)))
@time du03,dp3 = Zygote.gradient(loss,u0,p)

@test du01 ≈ du02 rtol=1e-12
@test du01 ≈ du03 rtol=1e-12
@test dp1 ≈ dp2 rtol=1e-12
@test dp1 ≈ dp3 rtol=1e-12

loss = (u0,p)->sum(solve(proboop,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=ForwardDiffSensitivity()))
@time du01,dp1 = Zygote.gradient(loss,u0,p)

loss = (u0,p)->sum(solve(proboop,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=InterpolatingAdjoint()))
@time du02,dp2 = Zygote.gradient(loss,u0,p)

loss = (u0,p)->sum(solve(proboop,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=ForwardDiffSensitivity(chunk_size=104)))
@time du03,dp3 = Zygote.gradient(loss,u0,p)

@test du01 ≈ du02 rtol=1e-12
@test du01 ≈ du03 rtol=1e-12
@test dp1 ≈ dp2 rtol=1e-12
@test dp1 ≈ dp3 rtol=1e-12

function fiip(du,u,p,t)
  du[1] = dx = p[1]*u[1] - p[2]*u[1]*u[2]
  du[2] = dy = -p[3]*u[2] + p[4]*u[1]*u[2]
  du[3:end] .= p[4]
end
function foop(u,p,t)
  dx = p[1]*u[1] - p[2]*u[1]*u[2]
  dy = -p[3]*u[2] + p[4]*u[1]*u[2]
  reshape(vcat(dx,dy,repeat([p[4]],100)),2,51)
end

p = [1.5,1.0,3.0,1.0]; u0 = [1.0;1.0]
u0 = reshape(vcat(u0,ones(100)),2,51)
prob = ODEProblem(fiip,u0,(0.0,10.0),p)
proboop = ODEProblem(foop,u0,(0.0,10.0),p)

loss = (u0,p)->sum(solve(prob,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=ForwardDiffSensitivity()))
@time du01,dp1 = Zygote.gradient(loss,u0,p)

loss = (u0,p)->sum(solve(prob,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=InterpolatingAdjoint()))
@time du02,dp2 = Zygote.gradient(loss,u0,p)

loss = (u0,p)->sum(solve(prob,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=ForwardDiffSensitivity(chunk_size=102)))
@time du03,dp3 = Zygote.gradient(loss,u0,p)

@test du01 ≈ du02 rtol=1e-12
@test du01 ≈ du03 rtol=1e-12
@test dp1 ≈ dp2 rtol=1e-12
@test dp1 ≈ dp3 rtol=1e-12

loss = (u0,p)->sum(solve(proboop,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=ForwardDiffSensitivity()))
@time du01,dp1 = Zygote.gradient(loss,u0,p)

loss = (u0,p)->sum(solve(proboop,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=InterpolatingAdjoint()))
@time du02,dp2 = Zygote.gradient(loss,u0,p)

loss = (u0,p)->sum(solve(proboop,Tsit5(),u0=u0,p=p,abstol=1e-14,reltol=1e-14,saveat=0.1,sensealg=ForwardDiffSensitivity(chunk_size=102)))
@time du03,dp3 = Zygote.gradient(loss,u0,p)

@test du01 ≈ du02 rtol=1e-12
@test du01 ≈ du03 rtol=1e-12
@test dp1 ≈ dp2 rtol=1e-12
@test dp1 ≈ dp3 rtol=1e-12