text:
function GradientSpoiling1()

    GradientSpoiling(0, 1, 0, 1)
    g = GradientSpoiling(0.0, 1, 0f0, 2//3)

    show(devnull, g.gradient)
    show(devnull, "text/plain", g.gradient)
    show(devnull, g)
    show(devnull, "text/plain", g)
    return rfspoiling_increment(g) == 0

end

function GradientSpoiling2()

    grad = Gradient(0, 0, 0)
    spoil = GradientSpoiling(grad, 3.0)
    return spoiler_gradient(spoil) === grad

end

function RFSpoiling1()

    s = RFSpoiling(deg2rad(117))

    show(devnull, s)
    show(devnull, "text/plain", s)
    return true

end

function RFSpoiling2()

    Δθ = 1
    spoil = RFSpoiling(Δθ)
    return rfspoiling_increment(spoil) == Δθ

end

function RFandGradientSpoiling1()

    RFandGradientSpoiling(GradientSpoiling(0, 0, 0, 3), RFSpoiling(deg2rad(117)))
    RFandGradientSpoiling(Gradient(0, 0, 0), 3, RFSpoiling(deg2rad(117)))
    RFandGradientSpoiling((0, 0, 0), 3, RFSpoiling(deg2rad(117)))
    RFandGradientSpoiling(0, 0, 0, 3, RFSpoiling(deg2rad(117)))
    RFandGradientSpoiling(GradientSpoiling(0, 0, 0, 3), deg2rad(117))
    RFandGradientSpoiling(Gradient(0, 0, 0), 3, deg2rad(117))
    RFandGradientSpoiling((0, 0, 0), 3, deg2rad(117))
    RFandGradientSpoiling(GradientSpoiling(0, 0, 0, 3))
    RFandGradientSpoiling(RFSpoiling(deg2rad(117)), GradientSpoiling(0, 0, 0, 3))
    RFandGradientSpoiling(RFSpoiling(deg2rad(117)), Gradient(0, 0, 0), 3)
    RFandGradientSpoiling(RFSpoiling(deg2rad(117)), (0, 0, 0), 3)
    RFandGradientSpoiling(RFSpoiling(deg2rad(117)), 0, 0, 0, 3)
    RFandGradientSpoiling(deg2rad(117), GradientSpoiling(0, 0, 0, 3))
    RFandGradientSpoiling(deg2rad(117), Gradient(0, 0, 0), 3)
    s = RFandGradientSpoiling(deg2rad(117), (0, 0, 0), 3)

    show(devnull, s)
    show(devnull, "text/plain", s)
    return true

end

function RFandGradientSpoiling2()

    grad = Gradient(1.0, 2.0, 1//2)
    Δθ = 2f0
    spoil = RFandGradientSpoiling(grad, 1.0, Δθ)
    @test spoiler_gradient(spoil) === grad
    return rfspoiling_increment(spoil) == Δθ

end

function spoil1()

    s = Spin(Magnetization(1, 0.4, 5), 1, 1000, 100, 0)
    (S,) = spoil(s)
    applydynamics!(s, S)
    M_correct = Magnetization(0, 0, 5)
    @test s.M ≈ M_correct
    return S === BlochSim.IdealSpoilingMatrix()

end

function spoil2()

    s = Spin(Magnetization(1, 0.4, 5), 1, 1000, 100, 0)
    spoil!(s)
    M_correct = Magnetization(0, 0, 5)
    return s.M ≈ M_correct

end

function spoil3()

    s = Spin(1, 1000, 100, 0)
    spoil!(nothing, nothing, s, RFSpoiling())
    (A,) = spoil(s, RFSpoiling())

    return A === nothing

end

function spoil4()

    s = Spin(1, 1000, 100, 0)
    (A1, B1) = spoil(s, GradientSpoiling(1, 1, 1, 10))
    (A2, B2) = freeprecess(s, 10)

    @test A1 == A2
    return B1 == B2

end

function spoilmc1()

    s = SpinMC(MagnetizationMC((1, 0.4, 5), (0.2, 10, 0.2)), 1, [0.2, 0.8], [400, 1000], [20, 100], [15, 0], [20, 40])
    (S,) = spoil(s)
    applydynamics!(s, S)
    M_correct = MagnetizationMC((0, 0, 5), (0, 0, 0.2))
    @test s.M ≈ M_correct
    return S === BlochSim.IdealSpoilingMatrix()

end

function spoilmc2()

    s = SpinMC(MagnetizationMC((1, 0.4, 5), (0.2, 10, 0.2)), 1, [0.2, 0.8], [400, 1000], [20, 100], [15, 0], [20, 40])
    spoil!(s)
    M_correct = MagnetizationMC((0, 0, 5), (0, 0, 0.2))
    return s.M ≈ M_correct

end

@testset "AbstractSpoiling" begin

    @testset "GradientSpoiling" begin

        @test GradientSpoiling1()
        @test GradientSpoiling2()

    end

    @testset "RFSpoiling" begin

        @test RFSpoiling1()
        @test RFSpoiling2()

    end

    @testset "RFandGradientSpoiling" begin

        @test RFandGradientSpoiling1()
        @test RFandGradientSpoiling2()

    end

end

@testset "Spoiling" begin

    @test spoil1()
    @test spoil2()
    @test spoil3()
    @test spoil4()
    @test spoilmc1()
    @test spoilmc2()

end


text:
using ITensors, ITensorNetworkAD, AutoHOOT
using ITensorNetworkAD.ITensorNetworks:
  PEPS,
  Models,
  inner_network,
  broadcast_add,
  broadcast_minus,
  broadcast_mul,
  broadcast_inner,
  insert_projectors,
  split_network,
  inner_networks,
  tree_w_projectors,
  get_leaves
using ITensorNetworkAD.ITensorAutoHOOT:
  generate_optimal_tree, batch_tensor_contraction, Executor

@testset "test peps" begin
  Nx = 4
  Ny = 5
  sites = siteinds("S=1/2", Ny, Nx)
  peps = PEPS(sites)

  for ii in 1:(Ny - 1)
    for jj in 1:(Nx - 1)
      inds1 = inds(peps.data[ii, jj])
      inds2 = inds(peps.data[ii, jj + 1])
      inds3 = inds(peps.data[ii + 1, jj])
      inds4 = inds(peps.data[ii + 1, jj + 1])
      @test length(intersect(inds1, inds2)) == 1
      @test length(intersect(inds1, inds3)) == 1
      @test length(intersect(inds1, inds4)) == 0
    end
  end
end

@testset "test inner product" begin
  Nx = 2
  Ny = 3
  sites = siteinds("S=1/2", Ny, Nx)
  peps = PEPS(sites)
  randn!(peps)
  peps_prime = prime(linkinds, peps)
  inner = inner_network(peps, peps_prime)

  opt_inner = generate_optimal_tree(inner)
  out = contract(opt_inner)
  # output is a scalar
  @test size(out) == ()
end

@testset "test inner product with hamiltonian" begin
  Nx = 3
  Ny = 4
  sites = siteinds("S=1/2", Ny, Nx)
  peps = PEPS(sites)
  randn!(peps)

  opsum = OpSum()
  opsum += 0.5, "S+", 1, "S-", 2
  opsum += 0.5, "S-", 1, "S+", 2
  opsum += "Sz", 1, "Sz", 2
  mpo = MPO(opsum, [sites[2, 2], sites[2, 3]])

  peps_prime = prime(linkinds, peps)
  peps_prime_ham = prime(peps)
  inner = inner_network(peps, peps_prime, peps_prime_ham, mpo, [2 => 2, 2 => 3])
  opt_inner = generate_optimal_tree(inner)
  out = contract(opt_inner)
  # output is a scalar
  @test size(out) == ()
end

@testset "test plus, minus, multiplication" begin
  Nx = 2
  Ny = 3
  sites = siteinds("S=1/2", Ny, Nx)
  peps1 = PEPS(sites)
  randn!(peps1)
  peps2 = broadcast_mul(1.5, peps1)
  peps3 = broadcast_add(peps1, peps2)
  peps4 = broadcast_minus(peps1, peps2)
  for i in 1:Nx
    for j in 1:Ny
      @test isapprox(peps2.data[j, i], 1.5 * peps1.data[j, i])
      @test isapprox(peps3.data[j, i], peps1.data[j, i] + peps2.data[j, i])
      @test isapprox(peps4.data[j, i], peps1.data[j, i] - peps2.data[j, i])
    end
  end
end

@testset "test insert projectors" begin
  Nx, Ny = 3, 3
  sites = siteinds("S=1/2", Ny, Nx)
  peps = PEPS(sites; linkdims=2)
  randn!(peps)
  tn_split_row, tn_split_column, projectors_row, projectors_column = insert_projectors(peps)
  for i in 2:length(tn_split_row)
    for (t1, t2) in zip(tn_split_row[1], tn_split_row[i])
      @test t1 == t2
    end
  end
  for i in 2:length(tn_split_column)
    for (t1, t2) in zip(tn_split_column[1], tn_split_column[i])
      @test t1 == t2
    end
  end
end

@testset "test split network with hamiltonian" begin
  Nx, Ny = 3, 3
  sites = siteinds("S=1/2", Ny, Nx)
  peps = PEPS(sites; linkdims=2)
  randn!(peps)

  H_line = Models.lineham(Models.Model("tfim"), sites; h=1.0)
  H_row = [H for H in H_line if H.coord[2] isa Colon]
  H_column = [H for H in H_line if H.coord[1] isa Colon]

  _, _, projectors_row, projectors_column = insert_projectors(peps)
  peps_bra = addtags(linkinds, peps, "bra")
  peps_ket = addtags(linkinds, peps, "ket")
  peps_bra_rot = addtags(linkinds, peps, "brarot")
  peps_ket_rot = addtags(linkinds, peps, "ketrot")
  sites = commoninds(peps_bra, peps_ket)
  peps_bra_split = split_network(peps_bra)
  peps_ket_split = split_network(peps_ket)
  peps_ket_split_ham = prime(sites, peps_ket_split)
  peps_bra_split_rot = split_network(peps_bra_rot, true)
  peps_ket_split_rot = split_network(peps_ket_rot, true)
  peps_ket_split_rot_ham = prime(sites, peps_ket_split_rot)

  for i in 1:length(projectors_row)
    network_list = inner_networks(
      peps_bra_split, peps_ket_split, peps_ket_split_ham, projectors_row[i], [H_row[i]]
    )
    inners = batch_tensor_contraction(network_list)
    @test size(inners[1]) == ()
  end
  for i in 1:length(projectors_column)
    network_list = inner_networks(
      peps_bra_split_rot,
      peps_ket_split_rot,
      peps_ket_split_rot_ham,
      projectors_column[i],
      [H_column[i]],
    )
    inners = batch_tensor_contraction(network_list)
    @test size(inners[1]) == ()
  end
end

@testset "test converting peps based network into a tree" begin
  # ITensors.set_warn_order(9)
  Nx, Ny = 6, 5
  sites = siteinds("S=1/2", Ny, Nx)
  peps = PEPS(sites; linkdims=2)
  randn!(peps)
  H_line = Models.lineham(Models.Model("tfim"), sites; h=1.0)
  H_row = [H for H in H_line if H.coord[2] isa Colon]
  H_column = [H for H in H_line if H.coord[1] isa Colon]
  _, _, projectors_row, projectors_column = insert_projectors(peps)

  peps_bra = addtags(linkinds, peps, "bra")
  peps_ket = addtags(linkinds, peps, "ket")
  peps_bra_rot = addtags(linkinds, peps, "brarot")
  peps_ket_rot = addtags(linkinds, peps, "ketrot")
  sites = commoninds(peps_bra, peps_ket)
  peps_bra_split = split_network(peps_bra)
  peps_ket_split = split_network(peps_ket)
  peps_ket_split_ham = prime(sites, peps_ket_split)
  peps_bra_split_rot = split_network(peps_bra_rot, true)
  peps_ket_split_rot = split_network(peps_ket_rot, true)
  peps_ket_split_rot_ham = prime(sites, peps_ket_split_rot)

  projectors = projectors_row[1]
  peps_tree = inner_network(peps_bra_split, peps_ket_split, projectors, tree_w_projectors)
  leaves = get_leaves(peps_tree)
  @test length(leaves) == 2 * Nx * Ny + length(projectors)
  executor = Executor([peps_tree])
  out = batch_tensor_contraction(executor)
  @test size(out[1]) == ()
  for i in 1:length(projectors_row)
    peps_tree = inner_networks(
      peps_bra_split,
      peps_ket_split,
      peps_ket_split_ham,
      [projectors_row[i]],
      [H_row[i]],
      tree_w_projectors,
    )[1]
    leaves = get_leaves(peps_tree)
    @test length(leaves) == 2 * Nx * Ny + length(projectors_row[i]) + Nx
    executor = Executor([peps_tree])
    out = batch_tensor_contraction(executor)
    @test size(out[1]) == ()
  end
  for i in 1:length(projectors_column)
    peps_tree = inner_networks(
      peps_bra_split_rot,
      peps_ket_split_rot,
      peps_ket_split_rot_ham,
      [projectors_column[i]],
      [H_column[i]],
      tree_w_projectors,
    )[1]
    leaves = get_leaves(peps_tree)
    @test length(leaves) == 2 * Nx * Ny + length(projectors_column[i]) + Ny
    executor = Executor([peps_tree])
    out = batch_tensor_contraction(executor)
    @test size(out[1]) == ()
  end
end

ext: jl


text:
using Pio3d
using Test

@testset "Pio3d.jl" begin

    @test "nrn4" in Pio3d.get_labels()
    Pio3d.datasets_download("nrn4")
    pth = Pio3d.datasets_join_path("medical/orig/sample_data/nrn4.pklz")
    @test length(pth) > 0
    datap = Pio3d.read3d(pth)
    data3d = datap["data3d"]
    @test eltype(data3d) == UInt16

#     labels = Pio3d.get_labels()
    new_fn = "test_output_nrn4.dcm"

    # delete file if output file exists
    rm(new_fn, force=true)
    Pio3d.write3d(data3d, new_fn)
    @test isfile(new_fn)
    # Write your own tests here.
end

@testset "test read" begin
    pth = Pio3d.datasets_join_path("medical/orig/sample_data/nrn4.pklz")
    datap = Pio3d.read3d(pth)

    data3d = datap["data3d"]
    # eltype(data3d)
    @test size(data3d) == (7,7,6)

end


text:
# Script to run a test case for understanding how M scales with β #

# Add paths
loc_gm_package = "../src/"
if !(loc_gm_package in LOAD_PATH)
    push!(LOAD_PATH, "../src/")
end

# Required packages
using LinearAlgebra

using GraphicalModelLearning
using LightGraphs

# Function definitions
using Printf, SparseArrays
include("beta_scaling.jl")

using DelimitedFiles

# parameters
FLAG_create_struct_gm = false
FLAG_regular_random_gm = true
FLAG_lattice_gm = false
sampling_regime = M_regime()
FLAG_weak_impurity = false
learning_algo = NLP()

if FLAG_lattice_gm
    # text file to save adjacency matrix in
    file_adj_matrix_gm = "adj_matrix_ferro_lattice_gm_A_T.txt"
    # name of picture to save graphical model in
    file_plot_gm = "ferro_lattice_gm_A_T.eps"
    # File to save final results
    file_M_opt_gm = "M_opt_T_Regime_Ferro_Lattice_A.txt"
elseif FLAG_regular_random_gm
    # text file to save adjacency matrix in
    #file_adj_matrix_gm = "adj_matrix_ferro_random_regular_gm_B_M.txt"
    file_adj_matrix_gm = "spin_glass_random_regular_adj_matrix.txt"
    # name of picture to save graphical model in
    file_plot_gm = "ferro_random_regular_gm_B_M.eps"
    # File to save final results
    file_M_opt_gm = "M_opt_RR_FigD_M_RISE_reg_sensitivity_beta_1_5.txt"
end

# Simple test
N = 16
d = 3
α = 0.4
β = 1.5

# Create and plot the initial graphical graphical model
adj_matrix = readdlm(file_adj_matrix_gm)
struct_adj_matrix = sign.(abs.(adj_matrix))

## Start the complexity studies
τ = α/2
L = 45
M_factor = 0.05
M_guess = 1000

# Different values of regularization coefficient to be tested
c_array = vcat([0.02,0.04],[0.05 + 0.05*i for i=0:9],[0.5 + 0.1*i for i=1:15])
M_opt = Array{Int64,1}(undef,length(c_array))

# To prevent rewriting
adj_matrix_orig = deepcopy(adj_matrix)

println(β)
for i = 1:length(c_array)
    # define β
    c = copy(c_array[i])
    println(c)
    println(M_guess)

    # Change the
    adj_matrix = replace_beta_gm(adj_matrix_orig, α, β)

    # Get the optimum number of samples
    M_opt[i] = get_M_opt_glauber_dynamics_regularization(adj_matrix, RISE(c, true), learning_algo, sampling_regime, τ, L, M_guess, M_factor)

    # Update the guess of M_opt
    @printf("beta=%f, M_opt=%d \n", β, M_opt[i])
end

writedlm(file_M_opt_gm,M_opt)


text:
using LinearAlgebra

function projectMatrix(sig, xVal)
	I = 1.0*Matrix(LinearAlgebra.I,size(sig,1),size(sig,1))
	xx = (I - xVal*xVal')
	A3 = xx*sig*xx
    return A3

end

function multiOptimalSPCA(prob, k, ncomp)
	myprob = problem(copy(prob.data),copy(prob.Sigma))
	st = time()
	all_x = zeros(ncomp, size(myprob.Sigma,1))

	for rnd = 1:ncomp
		~, xVal, ~, ~, ~, ~, ~ = branchAndBound(myprob, k, outputFlag=0, timeCap = 60) ;
		all_x[rnd,:] = xVal

		myprob.Sigma = projectMatrix(myprob.Sigma, xVal)
	end

	return all_x, time()-st
end

function adjVarExplained(Sigma, xVal)

	depth = size(xVal,1)
	varexplained = zeros(depth)
	sig = copy(Sigma)

	for component = 1:depth
		mypc = xVal[component,:]
		mypc = mypc/norm(mypc)
		varexplained[component] = mypc'*sig*mypc
		sig = projectMatrix(sig, mypc)
	end

	return varexplained
end

max_stars_repo_name: ryancorywright/Optimal-SPCA

max_issues_repo_name: ryancorywright/Optimal-SPCA

max_forks_repo_name: ryancorywright/Optimal-SPCA


text:
# polygons
const POLYGONS_MIN = 50
const POLYGONS_MAX = 50000

# points
const POINTS_PER_POLYGON_MIN = 3
const POINTS_PER_POLYGON_MAX = 10
const POINTS_MIN = POINTS_PER_POLYGON_MIN*POLYGONS_MIN
const POINTS_MAX = POINTS_PER_POLYGON_MAX*POLYGONS_MAX

# mutation rates
const ADD_POLYGON_MUTATION_RATE = 500
const REMOVE_POLYGON_MUTATION_RATE = 1000
const MOVE_POLYGON_MUTATION_RATE = 500
const ADD_POINT_MUTATION_RATE = 1000
const REMOVE_POINT_MUTATION_RATE = 1000
const MOVE_POINT_MAX_MUTATION_RATE = 700
const MOVE_POINT_MID_MUTATION_RATE = 700
const MOVE_POINT_MIN_MUTATION_RATE = 700
const RED_MUTATION_RATE = 500
const GREEN_MUTATION_RATE = 500
const BLUE_MUTATION_RATE = 500
const ALPHA_MUTATION_RATE = 500

# ranges
const MOVE_POINT_RANGE_MID = 20
const MOVE_POINT_RANGE_MIN = 3
const RED_RANGE_MAX = 255
const RED_RANGE_MIN = 0
const GREEN_RANGE_MAX = 255
const GREEN_RANGE_MIN = 0
const BLUE_RANGE_MAX = 255
const BLUE_RANGE_MIN = 0
const ALPHA_RANGE_MAX = 60
const ALPHA_RANGE_MIN = 30

max_stars_repo_name: llrt/evolisa-jl

max_issues_repo_name: llrt/evolisa-jl

max_forks_repo_name: llrt/evolisa-jl


text:
Base.lock(h::Hist1D) = lock(h.hlock)
Base.unlock(h::Hist1D) = unlock(h.hlock)

"""
    sample(h::Hist1D, n::Int=1)

Sample a histogram's with weights equal to bin count, `n` times.
The sampled values are the bins' lower edges.
"""
function sample(h::Hist1D; n::Int=1)
    StatsBase.sample(binedges(h)[1:end-1], Weights(bincounts(h)), n)
end

@inline function _edge_binindex(r::AbstractRange, x::Real)
    return floor(Int, (x - first(r)) * inv(step(r))) + 1
    # # 20% faster and assigns -Inf, Inf, NaN to typemin(Int64)
    # return Base.unsafe_trunc(Int, round((x - first(r)) * inv(step(r)), RoundDown)) + 1
end

@inline function _edge_binindex(r::AbstractRange{<:Integer}, x::Integer)
    return (x - first(r))÷step(r) + 1
end

@inline function _edge_binindex(v::AbstractVector, x::Real)
    return searchsortedlast(v, x)
end

"""
    bincounts(h::Hist1D)

Get the bin counts of a histogram.
"""
@inline bincounts(h::Hist1D) = h.hist.weights

"""
    binedges(h::Hist1D)

Get the bin edges of a histogram.
"""
@inline binedges(h::Hist1D) = h.hist.edges[1]

"""
    bincenters(h::Hist1D)

Get the bin centers of a histogram.
"""
function bincenters(h::Hist1D)
    StatsBase.midpoints(binedges(h))
end

"""
    binerrors(f::T, h::Hist1D) where T<:Function = f.(h.sumw2)
    binerrors(h::Hist1D) = binerrors(sqrt, h)

Calculate the bin errors from `sumw2` with a Gaussian default.
"""
binerrors(f::T, h::Hist1D) where T<:Function = f.(h.sumw2)
binerrors(h::Hist1D) = binerrors(sqrt, h)

"""
    nbins(h::Hist1D)

Get the number of bins of a histogram.
"""
function nbins(h::Hist1D)
    length(bincounts(h))
end

"""
    integral(h::Hist1D)

Get the integral a histogram.
"""
function integral(h::Hist1D)
    sum(bincounts(h))
end

"""
    empty!(h::Hist1D)

Resets a histogram's bin counts and `sumw2`.
"""
function Base.empty!(h::Hist1D{T,E}) where {T,E}
    h.hist.weights .= zero(T)
    h.sumw2 .= 0.0
    return h
end

"""
    push!(h::Hist1D, val::Real, wgt::Real=1)
    atomic_push!(h::Hist1D, val::Real, wgt::Real=1)

Adding one value at a time into histogram.
`sumw2` (sum of weights^2) accumulates `wgt^2` with a default weight of 1.
`atomic_push!` is a slower version of `push!` that is thread-safe.

N.B. To append multiple values at once, use broadcasting via
`push!.(h, [-3.0, -2.9, -2.8])` or `push!.(h, [-3.0, -2.9, -2.8], 2.0)`
"""
@inline function atomic_push!(h::Hist1D{T,E}, val::Real, wgt::Real=1) where {T,E}
    lock(h)
    push!(h, val, wgt)
    unlock(h)
    return nothing
end

@inline function Base.push!(h::Hist1D{T,E}, val::Real, wgt::Real=1) where {T,E}
    r = binedges(h)
    L = nbins(h)
    binidx = _edge_binindex(r, val)
    if h.overflow
        binidx = clamp(binidx, 1, L)
        @inbounds h.hist.weights[binidx] += wgt
        @inbounds h.sumw2[binidx] += wgt^2
    else
        if unsigned(binidx - 1) < L
            @inbounds h.hist.weights[binidx] += wgt
            @inbounds h.sumw2[binidx] += wgt^2
        end
    end
    return nothing
end

Base.broadcastable(h::Hist1D) = Ref(h)

"""
    Hist1D(elT::Type{T}=Float64; binedges, overflow) where {T}

Initialize an empty histogram with bin content typed as `T` and bin edges.
To be used with [`push!`](@ref). Default overflow behavior (`false`)
will exclude values that are outside of `binedges`.
"""
function Hist1D(elT::Type{T}=Float64; bins, overflow=_default_overflow) where {T}
    counts = zeros(elT, length(bins) - 1)
    return Hist1D(Histogram(bins, counts); overflow=overflow)
end

"""
    Hist1D(array, edges::AbstractRange; overflow)
    Hist1D(array, edges::AbstractVector; overflow)

Create a `Hist1D` with given bin `edges` and vlaues from
array. Weight for each value is assumed to be 1.
"""
function Hist1D(A::AbstractVector, r::AbstractRange; overflow=_default_overflow)
    h = Hist1D(Int; bins=r, overflow=overflow)
    push!.(h, A)
    return h
end
function Hist1D(A::AbstractVector, edges::AbstractVector; overflow=_default_overflow)
    if _is_uniform_bins(edges)
        r = range(first(edges), last(edges), length=length(edges))
        return Hist1D(A, r; overflow=overflow)
    else
        h = Hist1D(Int; bins=edges, overflow=overflow)
        push!.(h, A)
        return h
    end
end

"""
    Hist1D(array, wgts::AbstractWeights, edges::AbstractRange; overflow)
    Hist1D(array, wgts::AbstractWeights, edges::AbstractVector; overflow)

Create a `Hist1D` with given bin `edges` and vlaues from
array. `wgts` should have the same `size` as `array`.
"""
function Hist1D(A, wgts::AbstractWeights, r::AbstractRange; overflow=_default_overflow)
    @boundscheck @assert size(A) == size(wgts)
    h = Hist1D(eltype(wgts); bins=r, overflow=overflow)
    push!.(h, A, wgts)
    return h
end
function Hist1D(A, wgts::AbstractWeights, edges::AbstractVector, overflow=_default_overflow)
    @inbounds if _is_uniform_bins(edges)
        r = range(first(edges), last(edges), length=length(edges))
        return Hist1D(A, wgts, r; overflow=overflow)
    else
        h = Hist1D(eltype(wgts); bins=edges, overflow=overflow)
        push!.(h, A, wgts)
        return h
    end
end

"""
    Hist1D(A::AbstractVector{T}; nbins::Integer=_sturges(A), overflow) where T
    Hist1D(A::AbstractVector{T}, wgts::AbstractWeights; nbins::Integer=_sturges(A), overflow) where T

Automatically determine number of bins based on `Sturges` algo.
"""
function Hist1D(A::AbstractVector{T}; nbins::Integer=_sturges(A), overflow=_default_overflow) where {T}
    F = float(T)
    lo, hi = minimum(A), maximum(A)
    r = StatsBase.histrange(F(lo), F(hi), nbins)
    return Hist1D(A, r; overflow=overflow)
end

function Hist1D(
    A::AbstractVector{T},
    wgts::AbstractWeights;
    nbins::Integer=_sturges(A),
    overflow=_default_overflow,
) where {T}
    F = float(T)
    lo, hi = minimum(A), maximum(A)
    r = StatsBase.histrange(F(lo), F(hi), nbins)
    return Hist1D(A, wgts, r; overflow=overflow)
end

"""
    Statistics.mean(h::Hist1D)
    Statistics.std(h::Hist1D)
    Statistics.median(h::Hist1D)
    Statistics.quantile(h::Hist1D, p)

Compute statistical quantities based on the bin centers weighted
by the bin counts.
"""
Statistics.mean(h::Hist1D) = Statistics.mean(bincenters(h), Weights(bincounts(h)))
Statistics.std(h::Hist1D) = sqrt(Statistics.var(bincenters(h), Weights(bincounts(h))))
Statistics.median(h::Hist1D) = Statistics.median(bincenters(h), Weights(bincounts(h)))
Statistics.quantile(h::Hist1D, p) = Statistics.quantile(bincenters(h), Weights(bincounts(h)), p)

"""
    function lookup(h::Hist1D, x)

For given x-axis value `x`, find the corresponding bin and return the bin content.
If a value is out of the histogram range, return `missing`.
"""
function lookup(h::Hist1D, x)
    r = binedges(h)
    !(first(r) <= x <= last(r)) && return missing
    return bincounts(h)[_edge_binindex(r, x)]
end

"""
    normalize(h::Hist1D)

Create a normalized histogram via division by `integral(h)`.
"""
function normalize(h::Hist1D)
    return h*(1/integral(h))
end

"""
    cumulative(h::Hist1D; forward=true)

Create a cumulative histogram. If `forward`, start
summing from the left.
"""
function cumulative(h::Hist1D; forward=true)
    # https://root.cern.ch/doc/master/TH1_8cxx_source.html#l02608
    f = forward ? identity : reverse
    h = deepcopy(h)
    h.hist.weights .= f(cumsum(f(h.hist.weights)))
    h.sumw2 .= f(cumsum(f(h.sumw2)))
    return h
end

"""
    rebin(h::Hist1D, n::Int=1)
    rebin(n::Int) = h::Hist1D -> rebin(h, n)

Merges `n` consecutive bins into one.
The returned histogram will have `nbins(h)/n` bins.
"""
function rebin(h::Hist1D, n::Int=1)
    @assert nbins(h) % n == 0
    p = x->Iterators.partition(x, n)
    counts = sum.(p(bincounts(h)))
    sumw2 = sum.(p(h.sumw2))
    edges = first.(p(binedges(h)))
    if _is_uniform_bins(edges)
        edges = range(first(edges), last(edges), length=length(edges))
    end
    return Hist1D(Histogram(edges, counts), sumw2; overflow=h.overflow)
end
rebin(n::Int) = h::Hist1D -> rebin(h, n)

"""
    restrict(h::Hist1D, low=-Inf, high=Inf)
    restrict(low=-Inf, high=Inf) = h::Hist1D -> restrict(h, low, high)

Returns a new histogram with a restricted x-axis.
`restrict(h, 0, 3)` (or `h |> restrict(0, 3)`)
will return a slice of `h` where the bin centers are in `[0, 3]` (inclusive).
"""
function restrict(h::Hist1D, low=-Inf, high=Inf)
    sel = low .<= bincenters(h) .<= high
    @assert sum(sel) > 0 "No bin centers contained in [$(low), $(high)]"
    edgesel = push!(copy(sel), false)

    # include the right edge of the rightmost selected bin
    lastidx = findlast(edgesel)
    if lastidx != nothing
        edgesel[lastidx+1] = 1
    end

    c = bincounts(h)[sel]
    edges = binedges(h)[edgesel]
    sumw2 = h.sumw2[sel]
    if _is_uniform_bins(edges)
        edges = range(first(edges), last(edges), length=length(edges))
    end
    Hist1D(Histogram(edges, c), sumw2; overflow=h.overflow)
end
restrict(low=-Inf, high=Inf) = h::Hist1D->restrict(h, low, high)


text:
#######################
# Community detection #
#######################

"""
louvain_clust(edges::String)

Calculate communities in graph using [Louvain algoritm](https://sites.google.com/site/findcommunities/)

**Arguments**
* `edges::String` name of file with edges definition
"""
function louvain_clust(edges::String)
    BASEPATH = dirname(dirname(pathof(CGE)))
    TMP = Sys.iswindows() ? ENV["TEMP"] : "/tmp"
    convertlouvain() do exe
        run(`$exe -i $edges -o $TMP/louvain.bin`)
    end
    louvain() do exe
        run(pipeline(`$exe $TMP/louvain.bin -l -1 -q id_qual`, stdout="$TMP/louvain.txt"))
    end
    hierarchy() do exe
        run(pipeline(`$exe $TMP/louvain.txt -l 1`,"$edges.ecg"))
    end
end

"""
louvain_clust(filename::String, edges::Array{Int,2}, weights::Array{Float64,1}))

Calculate communities in weighted graph using [Louvain algoritm](https://sites.google.com/site/findcommunities/)

**Arguments**
* `filename::String` name of file with edges definition
* `edges::Array{Int,2}` list of edges
* `weights::Array{Float64,1}` array of edges' weights
"""
function louvain_clust(filename::String, edges::Array{Int,2}, weights::Array{Float64,1})
    BASEPATH = dirname(dirname(pathof(CGE)))
    TMP = Sys.iswindows() ? ENV["TEMP"] : "/tmp"
    tmp_edges = joinpath(TMP,filename)
    open(tmp_edges, "w") do f
        for i in edges println(f, i) end
    end
    tmp_weights = joinpath(TMP,filename*".weights")
    open(tmp_weights, "w") do f
        for i in weights println(f, i) end
    end
    convertlouvain() do exe
        run(`$exe -i $tmp_edges -o $TMP/louvain.bin -w $tmp_weights`)
    end
    louvain() do exe
        run(pipeline(`$exe $TMP/louvain.bin -w $tmp_weights -l -1 -q id_qual`, stdout="$TMP/louvain.txt"))
    end
    hierarchy() do exe
        run(pipeline(`$exe $TMP/louvain.txt -l 1`,"$filename.ecg"))
    end
end


text:
module ThreePlayerTest

using  Test

import Cribbage: CribbageGame, GameState, UnexpectedPlayerException
import Cribbage.RandomPlay: RandomPlayer
using  Cribbage.ThreePlayer

@testset "Test ThreePlayerGame Constructor" begin
    p₁ = RandomPlayer("player one")
    @test_throws AssertionError ThreePlayerGame(p₁, p₁, p₁)
    p₂ = RandomPlayer("player two")
    @test_throws AssertionError ThreePlayerGame(p₁, p₂, p₁)
    p₃ = RandomPlayer("player three")
    @test_throws AssertionError ThreePlayerGame(p₁, p₂, p₃, 42)
    @test isa(ThreePlayerGame(p₁, p₂, p₃), CribbageGame)
    @test isa(ThreePlayerGame(p₁, p₂, p₃, 61), CribbageGame)
    @test isa(ThreePlayerGame(p₁, p₂, p₃, 121), CribbageGame)

end

examplegame = ThreePlayerGame(
    RandomPlayer("1"),
    RandomPlayer("2"),
    RandomPlayer("3"),
    121
)
badplayer = RandomPlayer("-1")

@testset "Test ThreePlayerGameState Constructor" begin
    @test_throws AssertionError ThreePlayerGameState(examplegame, badplayer)
    @test_throws AssertionError ThreePlayerGameState(examplegame, examplegame.p₁, -42)
    @test_throws AssertionError ThreePlayerGameState(examplegame, examplegame.p₁, 0, -53)
    @test isa(ThreePlayerGameState(examplegame, examplegame.p₁, 0, 0), GameState)
end

@testset "Test players" begin
    @test length(players(examplegame)) == 3
    for player in players(examplegame)
        @test player ≡ examplegame.p₁ || player ≡ examplegame.p₂ || player ≡ examplegame.p₃
    end
end

@testset "Test incrementscore" begin
    initialstate = ThreePlayerGameState(
        examplegame, examplegame.p₁, 0, 0, 0
    )
    @test_throws UnexpectedPlayerException incrementscore(initialstate, badplayer, 0)
    @test_throws AssertionError incrementscore(initialstate, examplegame.p₁, -5)

    @test incrementscore(initialstate, examplegame.p₁, 0) == initialstate

    newstate = incrementscore(initialstate, examplegame.p₁, 6)
    @test newstate.dealer ≡ initialstate.dealer
    @test newstate.s₁ == 6
    @test newstate.s₂ == 0
    @test newstate.s₃ == 0

    initialstate = ThreePlayerGameState(
        examplegame, examplegame.p₁, 100, 115, 98
    )
    newstate = incrementscore(initialstate, examplegame.p₂, 8)
    @test newstate.dealer ≡ initialstate.dealer
    @test newstate.s₁ == 100
    @test newstate.s₂ == 121
    @test newstate.s₃ == 98

    newstate = incrementscore(initialstate, examplegame.p₃, 25)
    @test newstate.dealer ≡ initialstate.dealer
    @test newstate.s₁ == 100
    @test newstate.s₂ == 115
    @test newstate.s₃ == 121

end

@testset "Test isgameover" begin

    @test !isgameover(ThreePlayerGameState(examplegame, examplegame.p₁, 112, 118, 102))
    @test isgameover(ThreePlayerGameState(examplegame, examplegame.p₁, 121, 118, 95))
    @test isgameover(ThreePlayerGameState(examplegame, examplegame.p₁, 100, 121, 118))
    @test isgameover(ThreePlayerGameState(examplegame, examplegame.p₁, 57, 118, 121))

end

@testset "Test reportscore" begin

    gs = ThreePlayerGameState(examplegame, examplegame.p₁, 121, 118, 115)
    @test isa(reportscore(gs), String)

end

@testset "Test rotatedealer" begin
    initialstate = ThreePlayerGameState(examplegame, examplegame.p₁, 1, 2, 3)
    newstate = rotatedealer(initialstate)
    @test newstate.dealer ≡ examplegame.p₂
    @test newstate.s₁ == initialstate.s₁
    @test newstate.s₂ == initialstate.s₂
    @test newstate.s₃ == initialstate.s₃

    newstate = rotatedealer(newstate)
    @test newstate.dealer ≡ examplegame.p₃
    @test newstate.s₁ == initialstate.s₁
    @test newstate.s₂ == initialstate.s₂
    @test newstate.s₃ == initialstate.s₃

    newstate = rotatedealer(newstate)
    @test newstate == initialstate
end

end  # module ThreePlayerTest


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

using Test, Random
import Base64:
    Base64EncodePipe,
    base64encode,
    Base64DecodePipe,
    base64decode,
    stringmime

const inputText = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."
const encodedMaxLine76 = """
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="""

const longEncodedText = "bmFtZSA9ICJHZW5pZSIKdXVpZCA9ICJjNDNjNzM2ZS1hMmQxLTExZTgtMTYx\nZi1hZjk1MTE3ZmJkMWUiCmF1dGhvcnMgPSBbIkFkcmlhbiBTYWxjZWFudSA8\nZUBlc3NlbmNpYXJ5LmNvbT4iXQp2ZXJzaW9uID0gIjAuOS40IgoKW2RlcHNd\nCkFyZ1BhcnNlID0gImM3ZTQ2MGM2LTJmYjktNTNhOS04YzViLTE2ZjUzNTg1\nMWM2MyIKRGF0ZXMgPSAiYWRlMmNhNzAtMzg5MS01OTQ1LTk4ZmItZGMwOTk0\nMzJlMDZhIgpEaXN0cmlidXRlZCA9ICI4YmE4OWUyMC0yODVjLTViNmYtOTM1\nNy05NDcwMDUyMGVlMWIiCkd1bWJvID0gIjcwOGVjMzc1LWIzZDYtNWE1Ny1h\nN2NlLTgyNTdiZjk4NjU3YSIKSFRUUCA9ICJjZDNlYjAxNi0zNWZiLTUwOTQt\nOTI5Yi01NThhOTZmYWQ2ZjMiCkh0dHBDb21tb24gPSAiNzcxNzJjMWItMjAz\nZi01NGFjLWFhNTQtM2YxMTk4ZmU5ZjkwIgpKU09OID0gIjY4MmMwNmEwLWRl\nNmEtNTRhYi1hMTQyLWM4YjFjZjc5Y2RlNiIKTWFya2Rvd24gPSAiZDZmNDM3\nNmUtYWVmNS01MDVhLTk2YzEtOWMwMjczOTQ2MDdhIgpNYmVkVExTID0gIjcz\nOWJlNDI5LWJlYTgtNTE0MS05OTEzLWNjNzBlN2YzNzM2ZCIKTWlsbGJvYXJk\nID0gIjM5ZWMxNDQ3LWRmNDQtNWY0Yy1iZWFhLTg2NmYzMGI0ZDNiMiIKTWlu\naUxvZ2dpbmcgPSAiZjU5NDAyZWMtMDI2Mi01NzA3LWE1NjEtNzcwYWY5NGJj\nNWE2IgpOZXR0bGUgPSAiNDlkZWExZWUtZjZmYS01YWE2LTlhMTEtODgxNmNl\nZTdkNGI5IgpOdWxsYWJsZXMgPSAiNGQxZTFkNzctNjI1ZS01YjQwLTkxMTMt\nYTU2MGVjN2E4ZWNkIgpPcmRlcmVkQ29sbGVjdGlvbnMgPSAiYmFjNTU4ZTEt\nNWU3Mi01ZWJjLThmZWUtYWJlOGE0NjlmNTVkIgpQa2cgPSAiNDRjZmU5NWEt\nMWViMi01MmVhLWI2NzItZTJhZmRmNjliNzhmIgpSZWV4cG9ydCA9ICIxODlh\nMzg2Ny0zMDUwLTUyZGEtYTgzNi1lNjMwYmE5MGFiNjkiClJldmlzZSA9ICIy\nOTVhZjMwZi1lNGFkLTUzN2ItODk4My0wMDEyNmMyYTNhYmUiClNIQSA9ICJl\nYThlOTE5Yy0yNDNjLTUxYWYtODgyNS1hYWE2M2NkNzIxY2UiClNlcmlhbGl6\nYXRpb24gPSAiOWU4OGI0MmEtZjgyOS01YjBjLWJiZTktOWU5MjMxOTgxNjZi\nIgpTb2NrZXRzID0gIjY0NjJmZTBiLTI0ZGUtNTYzMS04Njk3LWRkOTQxZjkw\nZGVjYyIKVVJJUGFyc2VyID0gIjMwNTc4YjQ1LTlhZGMtNTk0Ni1iMjgzLTY0\nNWVjNDIwYWY2NyIKVW5pY29kZSA9ICI0ZWMwYTgzZS00OTNlLTUwZTItYjlh\nYy04ZjcyYWNmNWE4ZjUiCllBTUwgPSAiZGRiNmQ5MjgtMjg2OC01NzBmLWJk\nZGYtYWIzZjljZjk5ZWI2IgoKW2NvbXBhdF0KanVsaWEgPSAiMSIK"
const longDecodedText = "name = \"Genie\"\nuuid = \"c43c736e-a2d1-11e8-161f-af95117fbd1e\"\nauthors = [\"Adrian Salceanu <e@essenciary.com>\"]\nversion = \"0.9.4\"\n\n[deps]\nArgParse = \"c7e460c6-2fb9-53a9-8c5b-16f535851c63\"\nDates = \"ade2ca70-3891-5945-98fb-dc099432e06a\"\nDistributed = \"8ba89e20-285c-5b6f-9357-94700520ee1b\"\nGumbo = \"708ec375-b3d6-5a57-a7ce-8257bf98657a\"\nHTTP = \"cd3eb016-35fb-5094-929b-558a96fad6f3\"\nHttpCommon = \"77172c1b-203f-54ac-aa54-3f1198fe9f90\"\nJSON = \"682c06a0-de6a-54ab-a142-c8b1cf79cde6\"\nMarkdown = \"d6f4376e-aef5-505a-96c1-9c027394607a\"\nMbedTLS = \"739be429-bea8-5141-9913-cc70e7f3736d\"\nMillboard = \"39ec1447-df44-5f4c-beaa-866f30b4d3b2\"\nMiniLogging = \"f59402ec-0262-5707-a561-770af94bc5a6\"\nNettle = \"49dea1ee-f6fa-5aa6-9a11-8816cee7d4b9\"\nNullables = \"4d1e1d77-625e-5b40-9113-a560ec7a8ecd\"\nOrderedCollections = \"bac558e1-5e72-5ebc-8fee-abe8a469f55d\"\nPkg = \"44cfe95a-1eb2-52ea-b672-e2afdf69b78f\"\nReexport = \"189a3867-3050-52da-a836-e630ba90ab69\"\nRevise = \"295af30f-e4ad-537b-8983-00126c2a3abe\"\nSHA = \"ea8e919c-243c-51af-8825-aaa63cd721ce\"\nSerialization = \"9e88b42a-f829-5b0c-bbe9-9e923198166b\"\nSockets = \"6462fe0b-24de-5631-8697-dd941f90decc\"\nURIParser = \"30578b45-9adc-5946-b283-645ec420af67\"\nUnicode = \"4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5\"\nYAML = \"ddb6d928-2868-570f-bddf-ab3f9cf99eb6\"\n\n[compat]\njulia = \"1\"\n"

@testset "Examples" begin
    # Encode and decode
    fname = tempname()
    open(fname, "w") do f
        opipe = Base64EncodePipe(f)
        write(opipe, inputText)
        @test close(opipe) === nothing
    end

    open(fname, "r") do f
        ipipe = Base64DecodePipe(f)
        @test read(ipipe, String) == inputText
        @test close(ipipe) === nothing
    end
    rm(fname)

    # Byte-by-byte encode and decode.
    buf = IOBuffer()
    pipe = Base64EncodePipe(buf)
    for char in inputText
        write(pipe, UInt8(char))
    end
    close(pipe)
    pipe = Base64DecodePipe(IOBuffer(take!(buf)))
    decoded = UInt8[]
    while !eof(pipe)
        push!(decoded, read(pipe, UInt8))
    end
    @test String(decoded) == inputText

    # Encode to string and decode
    @test String(base64decode(base64encode(inputText))) == inputText

    # Decode with max line chars = 76 and padding
    ipipe = Base64DecodePipe(IOBuffer(encodedMaxLine76))
    @test read(ipipe, String) == inputText

    # Decode with max line chars = 76 and no padding
    #ipipe = Base64DecodePipe(IOBuffer(encodedMaxLine76[1:end-1]))
    #@test read(ipipe, String) == inputText

    # Decode with two padding characters ("==")
    ipipe = Base64DecodePipe(IOBuffer(string(encodedMaxLine76[1:end - 2], "==")))
    @test read(ipipe, String) == inputText[1:end - 1]

    # Test incorrect format
    ipipe = Base64DecodePipe(IOBuffer(encodedMaxLine76[1:end - 3]))
    @test_throws ArgumentError read(ipipe, String)

    # issue #21314
    @test base64decode(chomp("test")) == base64decode("test")

    # issue #32397
    @test String(base64decode(longEncodedText)) == longDecodedText;
end

@testset "Random data" begin
    mt = MersenneTwister(1234)
    for _ in 1:1000
        data = rand(mt, UInt8, rand(0:300))
        @test hash(base64decode(base64encode(data))) == hash(data)
    end
end

struct PNG end
Base.show(io::IO, ::MIME"image/png", ::PNG) = print(io, "PNG")

@testset "stringmime" begin
    @test stringmime("text/plain", [1 2;3 4]) == repr("text/plain", [1 2;3 4])
    @test stringmime("text/html", "raw html data") == "raw html data"
    @test stringmime("text/plain", "string") == "\"string\""
    @test stringmime("image/png", UInt8[2,3,4,7]) == "AgMEBw=="
    @test stringmime("text/plain", 3.141592653589793, context = :compact => true) == "3.14159"
    @test stringmime("image/png", PNG()) == stringmime(MIME("image/png"), PNG()) == "UE5H"
end

function splace(in::String, p = 0.3)
    spaces = ["\n"," "]
    len = length(in)
    len == 0 && return in
    rc::String = ""
    i = 1
    for (x, v) in enumerate(sort(randsubseq(collect(1:len), p)))
        rc = rc * in[i:v] * rand(spaces)^rand(Int.(1:10))
        i = v + 1
    end
    return rc * in[i:len] * rand(spaces)^rand(Int.(1:10))
end

@testset "lstrsplaced" begin
    for _ in 1:1000
        @test String(base64decode(splace(longEncodedText))) == longDecodedText
    end
end

max_stars_repo_name: TimoLarson/julia

max_issues_repo_name: AsForMe/julia

max_forks_repo_name: AsForMe/julia


text:
using JuMP, EAGO

                     m = Model()

                     EAGO.register_eago_operators!(m)

                     @variable(m, -1 <= x[i=1:5] <= 1)

                     @variable(m, -14.06559781669544 <= q <= 13.37751741699121)

                     add_NL_constraint(m, :(1/(1 + exp(-(-0.7556338259975872 + -0.2850076752045205*1/(1 + exp(-(0.7432962759699406 + 0.616365769922441*1/(1 + exp(-(-0.4636854780986108 + 0.31515700957585446*$(x[1]) + 0.3880835610738931*$(x[2]) + 0.5564947842471106*$(x[3]) + -0.6707203467350809*$(x[4]) + 0.9006732364765586*$(x[5])))) + -0.9563519707270385*1/(1 + exp(-(0.08793534010659565 + 0.5972587053731475*$(x[1]) + 0.5980877634648243*$(x[2]) + 0.5697895882823776*$(x[3]) + -0.6631393507177727*$(x[4]) + -0.46226121533035425*$(x[5])))) + 0.21036883565748976*1/(1 + exp(-(-0.49150226791798435 + -0.92821223742826*$(x[1]) + -0.3666783267897977*$(x[2]) + -0.7302712314714839*$(x[3]) + 0.924313656245149*$(x[4]) + 0.3686570879245057*$(x[5]))))))) + -0.5819748667402531*1/(1 + exp(-(0.6367931081099356 + 0.08862555157308405*1/(1 + exp(-(-0.4636854780986108 + 0.31515700957585446*$(x[1]) + 0.3880835610738931*$(x[2]) + 0.5564947842471106*$(x[3]) + -0.6707203467350809*$(x[4]) + 0.9006732364765586*$(x[5])))) + 0.5030828808953012*1/(1 + exp(-(0.08793534010659565 + 0.5972587053731475*$(x[1]) + 0.5980877634648243*$(x[2]) + 0.5697895882823776*$(x[3]) + -0.6631393507177727*$(x[4]) + -0.46226121533035425*$(x[5])))) + -0.4316150506981855*1/(1 + exp(-(-0.49150226791798435 + -0.92821223742826*$(x[1]) + -0.3666783267897977*$(x[2]) + -0.7302712314714839*$(x[3]) + 0.924313656245149*$(x[4]) + 0.3686570879245057*$(x[5]))))))) + 0.4871037750371179*1/(1 + exp(-(0.12597161846431115 + 0.10770543162761159*1/(1 + exp(-(-0.4636854780986108 + 0.31515700957585446*$(x[1]) + 0.3880835610738931*$(x[2]) + 0.5564947842471106*$(x[3]) + -0.6707203467350809*$(x[4]) + 0.9006732364765586*$(x[5])))) + 0.46799585735795457*1/(1 + exp(-(0.08793534010659565 + 0.5972587053731475*$(x[1]) + 0.5980877634648243*$(x[2]) + 0.5697895882823776*$(x[3]) + -0.6631393507177727*$(x[4]) + -0.46226121533035425*$(x[5])))) + 0.45029698767672954*1/(1 + exp(-(-0.49150226791798435 + -0.92821223742826*$(x[1]) + -0.3666783267897977*$(x[2]) + -0.7302712314714839*$(x[3]) + 0.924313656245149*$(x[4]) + 0.3686570879245057*$(x[5])))))))))) + 1/(1 + exp(-(0.9186311390475046 + -0.8559712370251158*1/(1 + exp(-(0.7432962759699406 + 0.616365769922441*1/(1 + exp(-(-0.4636854780986108 + 0.31515700957585446*$(x[1]) + 0.3880835610738931*$(x[2]) + 0.5564947842471106*$(x[3]) + -0.6707203467350809*$(x[4]) + 0.9006732364765586*$(x[5])))) + -0.9563519707270385*1/(1 + exp(-(0.08793534010659565 + 0.5972587053731475*$(x[1]) + 0.5980877634648243*$(x[2]) + 0.5697895882823776*$(x[3]) + -0.6631393507177727*$(x[4]) + -0.46226121533035425*$(x[5])))) + 0.21036883565748976*1/(1 + exp(-(-0.49150226791798435 + -0.92821223742826*$(x[1]) + -0.3666783267897977*$(x[2]) + -0.7302712314714839*$(x[3]) + 0.924313656245149*$(x[4]) + 0.3686570879245057*$(x[5]))))))) + -0.2512416329037399*1/(1 + exp(-(0.6367931081099356 + 0.08862555157308405*1/(1 + exp(-(-0.4636854780986108 + 0.31515700957585446*$(x[1]) + 0.3880835610738931*$(x[2]) + 0.5564947842471106*$(x[3]) + -0.6707203467350809*$(x[4]) + 0.9006732364765586*$(x[5])))) + 0.5030828808953012*1/(1 + exp(-(0.08793534010659565 + 0.5972587053731475*$(x[1]) + 0.5980877634648243*$(x[2]) + 0.5697895882823776*$(x[3]) + -0.6631393507177727*$(x[4]) + -0.46226121533035425*$(x[5])))) + -0.4316150506981855*1/(1 + exp(-(-0.49150226791798435 + -0.92821223742826*$(x[1]) + -0.3666783267897977*$(x[2]) + -0.7302712314714839*$(x[3]) + 0.924313656245149*$(x[4]) + 0.3686570879245057*$(x[5]))))))) + 0.7592758779705528*1/(1 + exp(-(0.12597161846431115 + 0.10770543162761159*1/(1 + exp(-(-0.4636854780986108 + 0.31515700957585446*$(x[1]) + 0.3880835610738931*$(x[2]) + 0.5564947842471106*$(x[3]) + -0.6707203467350809*$(x[4]) + 0.9006732364765586*$(x[5])))) + 0.46799585735795457*1/(1 + exp(-(0.08793534010659565 + 0.5972587053731475*$(x[1]) + 0.5980877634648243*$(x[2]) + 0.5697895882823776*$(x[3]) + -0.6631393507177727*$(x[4]) + -0.46226121533035425*$(x[5])))) + 0.45029698767672954*1/(1 + exp(-(-0.49150226791798435 + -0.92821223742826*$(x[1]) + -0.3666783267897977*$(x[2]) + -0.7302712314714839*$(x[3]) + 0.924313656245149*$(x[4]) + 0.3686570879245057*$(x[5])))))))))) + 1/(1 + exp(-(-0.7151630404444922 + -0.15417874070690862*1/(1 + exp(-(0.7432962759699406 + 0.616365769922441*1/(1 + exp(-(-0.4636854780986108 + 0.31515700957585446*$(x[1]) + 0.3880835610738931*$(x[2]) + 0.5564947842471106*$(x[3]) + -0.6707203467350809*$(x[4]) + 0.9006732364765586*$(x[5])))) + -0.9563519707270385*1/(1 + exp(-(0.08793534010659565 + 0.5972587053731475*$(x[1]) + 0.5980877634648243*$(x[2]) + 0.5697895882823776*$(x[3]) + -0.6631393507177727*$(x[4]) + -0.46226121533035425*$(x[5])))) + 0.21036883565748976*1/(1 + exp(-(-0.49150226791798435 + -0.92821223742826*$(x[1]) + -0.3666783267897977*$(x[2]) + -0.7302712314714839*$(x[3]) + 0.924313656245149*$(x[4]) + 0.3686570879245057*$(x[5]))))))) + -0.016895941959414795*1/(1 + exp(-(0.6367931081099356 + 0.08862555157308405*1/(1 + exp(-(-0.4636854780986108 + 0.31515700957585446*$(x[1]) + 0.3880835610738931*$(x[2]) + 0.5564947842471106*$(x[3]) + -0.6707203467350809*$(x[4]) + 0.9006732364765586*$(x[5])))) + 0.5030828808953012*1/(1 + exp(-(0.08793534010659565 + 0.5972587053731475*$(x[1]) + 0.5980877634648243*$(x[2]) + 0.5697895882823776*$(x[3]) + -0.6631393507177727*$(x[4]) + -0.46226121533035425*$(x[5])))) + -0.4316150506981855*1/(1 + exp(-(-0.49150226791798435 + -0.92821223742826*$(x[1]) + -0.3666783267897977*$(x[2]) + -0.7302712314714839*$(x[3]) + 0.924313656245149*$(x[4]) + 0.3686570879245057*$(x[5]))))))) + -0.17047547651198514*1/(1 + exp(-(0.12597161846431115 + 0.10770543162761159*1/(1 + exp(-(-0.4636854780986108 + 0.31515700957585446*$(x[1]) + 0.3880835610738931*$(x[2]) + 0.5564947842471106*$(x[3]) + -0.6707203467350809*$(x[4]) + 0.9006732364765586*$(x[5])))) + 0.46799585735795457*1/(1 + exp(-(0.08793534010659565 + 0.5972587053731475*$(x[1]) + 0.5980877634648243*$(x[2]) + 0.5697895882823776*$(x[3]) + -0.6631393507177727*$(x[4]) + -0.46226121533035425*$(x[5])))) + 0.45029698767672954*1/(1 + exp(-(-0.49150226791798435 + -0.92821223742826*$(x[1]) + -0.3666783267897977*$(x[2]) + -0.7302712314714839*$(x[3]) + 0.924313656245149*$(x[4]) + 0.3686570879245057*$(x[5])))))))))) - $q <= 0.0))

                     @objective(m, Min, q)

                     return m

max_stars_repo_name: PSORLab/RSActivationFunctions

max_issues_repo_name: PSORLab/RSActivationFunctions

max_forks_repo_name: PSORLab/RSActivationFunctions


text:
export rule

@rule GCV(:x, Marginalisation) (m_y::Any, q_z::Any, q_κ::Any, q_ω::Any) = begin

    ksi = mean(q_κ) ^ 2 * var(q_z) + mean(q_z) ^ 2 * var(q_κ) + var(q_z) * var(q_κ)
    A = exp(-mean(q_ω) + var(q_ω) / 2)
    B = exp(-mean(q_κ) * mean(q_z) + ksi / 2)

    return NormalMeanVariance(mean(m_y), var(m_y) + inv(A * B))
end

@rule GCV(:x, Marginalisation) (q_y::Any, q_z::Any, q_κ::Any, q_ω::Any) = begin

    ksi = mean(q_κ) ^ 2 * var(q_z) + mean(q_z) ^ 2 * var(q_κ) + var(q_z) * var(q_κ)
    A = exp(-mean(q_ω) + var(q_ω) / 2)
    B = exp(-mean(q_κ) * mean(q_z) + ksi / 2)

    return NormalMeanVariance(mean(q_y),  inv(A * B))
end


text:
# latin stemmer from https://snowballstem.org/otherapps/schinke/

const QUE = r"que$"
const SPECIAL_LETTERS = r"j|v"
const QUE_EXCEPTIONS = r"((at)|(quo)|(ne)|(ita)|(abs)|(aps)|(abus)|(adae)|(adus)|(deni)|(de)|(sus)|(obli)|(perae)|(plenis)|(quando)|(quis)|(quae)|(cuius)|(cui)|(m)|(quam)|(qua)|(qui)|(quorum)|(quarum)|(quibus)|(quos)|(quas)|(quotusquis)|(quous)|(ubi)|(undi)|(us)|(uter)|(uti)|(utro)|(utribi)|(tor)|(co)|(conco)|(contor)|(detor)|(deco)|(exco)|(extor)|(obtor)|(optor)|(retor)|(reco)|(attor)|(inco)|(intor)|(praetor))que$"
const NOUN_SUFFIXES = r"((ibus)|(ius)|(ae)|(am)|(as)|(em)|(es)|(ia)|(is)|(nt)|(os)|(ud)|(um)|(us)|(a)|(e)|(i)|(o)|(u))$"
const VERB_SUFFIX_REPLACEMENTS = [r"iuntur$" => "i",
                                  r"beris$" => "bi",
                                  r"erunt$" => "i",
                                  r"untur$" => "i",
                                  r"iunt$" => "i",
                                  r"mini$" => "",
                                  r"ntur$" => "",
                                  r"stis$" => "",
                                  r"bor$" => "bi",
                                  r"ero$" => "eri",
                                  r"mur$" => "",
                                  r"mus$" => "",
                                  r"ris$" => "",
                                  r"sti$" => "",
                                  r"tis$" => "",
                                  r"tur$" => "",
                                  r"unt$" => "i",
                                  r"bo$" => "bi",
                                  r"ns$" => "",
                                  r"nt$" => "",
                                  r"ri$" => "",
                                  r"m$" => "",
                                  r"r$" => "",
                                  r"s$" => "",
                                  r"t$"  => ""]

@inline function map_letter(c)
    if c == "j"
        return "i"
    elseif c == "v"
        return "u"
    else
        return c
    end
end

@inline map_letters(word) = replace(word, SPECIAL_LETTERS => map_letter)

@inline function replace_verb_suffix(word)
    for (suffix, replacement) in VERB_SUFFIX_REPLACEMENTS
        if endswith(word, suffix)
            return replace(word, suffix => replacement)
        end
    end

    return word
end

function schinke_stemmer(word)
    word = lowercase(word)
    word = map_letters(word)

    if endswith(word, QUE)
        if !isnothing(match(QUE_EXCEPTIONS, word))
            return word
        else
            word = chop(word, tail = 3)
        end
    end

    word = replace(word, NOUN_SUFFIXES => "")
    length(word) ≥ 2 && return word

    word = replace_verb_suffix(word)
    return word
end

max_stars_repo_name: phipsgabler/turing-lda

max_issues_repo_name: phipsgabler/turing-lda

max_forks_repo_name: phipsgabler/turing-lda


text:
# Object-based wrapper for level-1 BLIS vector routines.
#

# Some methods here might share the same type lists.
# Still, they're made separate to give help message a clear meaning.
#
macro blis_object_api_level1v_form1(funcname)
    return quote
        @blis_ccall_object($funcname,
                           Cvoid,
                           x, xObj,
                           y, xObj)
    end
end

@blis_object_api_level1v_form1 addv
@blis_object_api_level1v_form1 copyv
@blis_object_api_level1v_form1 subv
@blis_object_api_level1v_form1 swapv

macro blis_object_api_level1v_form2(funcname)
    return quote
        @blis_ccall_object($funcname,
                           Cvoid,
                           α, xObj,
                           x, xObj,
                           y, xObj)
    end
end

@blis_object_api_level1v_form2 axpyv
@blis_object_api_level1v_form2 scal2v

macro blis_object_api_level1v_form3(funcname)
    return quote
        @blis_ccall_object($funcname,
                           Cvoid,
                           α, xObj,
                           x, xObj)
    end
end

@blis_object_api_level1v_form3 scalv
@blis_object_api_level1v_form3 setv
@blis_object_api_level1v_form3 setrv
@blis_object_api_level1v_form3 setiv

@blis_ccall_object(amaxv,
                   Cvoid,
                   x,     xObj,
                   index, xObj)
@blis_ccall_object(axpbyv,
                   Cvoid,
                   α, xObj,
                   x, xObj,
                   β, xObj,
                   y, xObj)
@blis_ccall_object(dotv,
                   Cvoid,
                   x, xObj,
                   y, xObj,
                   ρ, xObj)
@blis_ccall_object(dotxv,
                   Cvoid,
                   α, xObj,
                   x, xObj,
                   y, xObj,
                   β, xObj,
                   ρ, xObj)
@blis_ccall_object(invertv,
                   Cvoid,
                   x, xObj)
@blis_ccall_object(xpbyv,
                   Cvoid,
                   x, xObj,
                   β, xObj,
                   y, xObj)


text:
# canonical factor graph examples useful for development and learning.

export generateCanonicalFG_ZeroPose2, generateCanonicalFG_Hexagonal, generateCanonicalFG_TwoPoseOdo, generateCanonicalFG_Circle
export warmUpSolverJIT

"""
    $SIGNATURES

Generate a canonical factor graph with a Pose2 `:x0` and MvNormal with covariance `P0`
"""
function generateCanonicalFG_ZeroPose2(;fg::AbstractDFG=initfg(),
                                       graphinit::Bool=true,
                                       Σ0::AbstractMatrix{<:Real}= 0.01*Matrix{Float64}(LinearAlgebra.I,3,3),
                                       μ0::AbstractVector{<:Real}=zeros(3),
                                       label::Symbol=:x0 )
  #
  if !exists(fg, label)
    addVariable!(fg, label, Pose2)
    prpo = PriorPose2(MvNormal(μ0, Σ0))
    addFactor!(fg, [label], prpo, graphinit=graphinit)
  end
  return fg
end

"""
    $SIGNATURES

Generate a canonical factor graph: driving in a circular pattern with one landmark.

Notes
- Poses, :x0, :x1,... Pose2,
- Odometry, :x0x1f1, etc., Pose2Pose2 (Gaussian)
- OPTIONAL: 1 Landmark, :l1, Point2,
- 2 Sightings, :x0l1f1, :x6l1f1, RangeBearing (Gaussian)

Example
```julia
using RoME

fg = generateCanonicalFG_Hexagonal()
drawGraph(fg, show=true)
```

Related

generateCanonicalFG_Circle, generateCanonicalFG_Kaess, generateCanonicalFG_TwoPoseOdo
"""
function generateCanonicalFG_Circle(poses::Int=6;
                                    fg::AbstractDFG=initfg(),
                                    offsetPoses::Int=maximum([length(ls(fg, r"x\d"))-1;0]),
                                    autoinit::Union{Bool, Nothing}=nothing,
                                    graphinit::Bool=true,
                                    landmark::Bool=true,
                                    loopClosure::Bool=true,
                                    stopEarly::Int=9999999,
                                    biasTurn::Real=0.0,
                                    kappaOdo::Real=1.0,
                                    cyclePoses::Int=poses )
  # assume empty factor graph object fg
  @assert offsetPoses < poses "`offsetPoses` must be smaller than total number of `poses`"
  # IIF.getSolverParams(fg).drawtree = true

  graphinit = if autoinit == nothing
    graphinit
  else
    @warn "autoinit is deprecated, use graphinit instead"
    autoinit
  end

  ## 1. Drive around in a hexagon
  # Add the first pose :x0
  if !exists(fg, :x0)
    addVariable!(fg, :x0, Pose2)
    prpo = PriorPose2(MvNormal(zeros(3), 0.01*Matrix{Float64}(LinearAlgebra.I,3,3)))
    addFactor!(fg, [:x0], prpo, graphinit=graphinit)
  end

  println("STEP 1: Driving around a bit")
  # Add at a fixed location PriorPose2 to pin :x0 to a starting location
  for i in offsetPoses:poses-1
    if stopEarly <= i
      break
    end
    psym = Symbol("x$i")
    nsym = Symbol("x$(i+1)")
    addVariable!(fg, nsym, Pose2)
    pp = Pose2Pose2(MvNormal([10.0;0;2pi/(cyclePoses)+biasTurn], Matrix(Diagonal((kappaOdo*[0.1;0.1;0.1]).^2))))
    addFactor!(fg, [psym;nsym], pp , graphinit=graphinit)
  end

  if !landmark
    return fg
  end
  if !exists(fg, :l1)
    # Add node linking initial pose with a bearing range measurement landmark
    addVariable!(fg, :l1, Point2, tags=[:LANDMARK;])
    p2br = Pose2Point2BearingRange(Normal(0,0.1),Normal(20.0,1.0))
    addFactor!(fg, [:x0; :l1], p2br, graphinit=graphinit)
  end

  if !loopClosure || !exists(fg, Symbol("x$poses"))
    return fg
  end
  # add loop closure sighting
  p2br = Pose2Point2BearingRange(Normal(0,0.1),Normal(20.0,1.0))
  addFactor!(fg, [Symbol("x$poses"); :l1], p2br, graphinit=graphinit)

  # return the new factor graph object
  return fg
end

function driveHex(fgl, posecount::Int; steps::Int=5)
    # Drive around in a hexagon
    for i in (posecount-1):(posecount-1+steps)
        psym = Symbol("x$i")
        posecount += 1
        nsym = Symbol("x$(i+1)")
        addVariable!(fgl, nsym, Pose2)
        pp = Pose2Pose2(MvNormal([10.0;0;pi/3], Matrix(Diagonal([0.1;0.1;0.1].^2))))
        addFactor!(fgl, [psym;nsym], pp, graphinit=false )
    end

    return posecount
end

function offsetHexLeg(dfg::G, posecount::Int; direction=:right) where G <: AbstractDFG
    psym = Symbol("x$(posecount-1)")
    nsym = Symbol("x$(posecount)")
    posecount += 1
    addVariable!(dfg, nsym, Pose2)
    pp = nothing
    if direction == :right
        pp = Pose2Pose2(MvNormal([10.0;0;-pi/3], Matrix(Diagonal([0.1;0.1;0.1].^2))))
    elseif direction == :left
        pp = Pose2Pose2(MvNormal([10.0;0;pi/3], Matrix(Diagonal([0.1;0.1;0.1].^2))))
    end
    addFactor!(dfg, [psym; nsym], pp, graphinit=false )
    return posecount
end

"""
    $SIGNATURES

Generate a canonical factor graph: driving in a hexagonal circular pattern with one landmark.

Notes
- 7 Poses, :x0-:x6, Pose2,
- 1 Landmark, :l1, Point2,
- 6 Odometry, :x0x1f1, etc., Pose2Pose2 (Gaussian)
- 2 Sightings, :x0l1f1, :x6l1f1, RangeBearing (Gaussian)

Example
```julia
using RoME

fg = generateCanonicalFG_Hexagonal()
drawGraph(fg, show=true)
```

Related

generateCanonicalFG_Circle, generateCanonicalFG_Kaess, generateCanonicalFG_TwoPoseOdo
"""
function generateCanonicalFG_Hexagonal(;fg::AbstractDFG=initfg(),
                                        N::Int=100,
                                        autoinit::Union{Bool, Nothing}=nothing,
                                        graphinit::Bool=true  )
  #
  getSolverParams(fg).N = N
  graphinit = if autoinit === nothing
    graphinit
  else
    @warn "autoinit is deprecated, use graphinit instead"
    autoinit
  end
  return generateCanonicalFG_Circle(6, graphinit=graphinit, landmark=true, loopClosure=true; fg=fg)
end

"""
    $SIGNATURES

Build a basic factor graph in Pose2 with two `Pose2` and one landmark `Point2` variables,
along with `PriorPose2` on `:x0` and `Pose2Pose2` to `:x1`.  Also a `Pose2Point2BearingRange`
to landmark `:l1`.
"""
function generateCanonicalFG_TwoPoseOdo(;fg::AbstractDFG=initfg(),
                                        type::Type{Pose2}=Pose2,
                                        addlandmark::Bool=true,
                                        autoinit::Union{Bool,Nothing}=nothing,
                                        graphinit::Bool=true )
  #

  graphinit = if autoinit == nothing
    graphinit
  else
    @warn "autoinit is deprecated, use graphinit instead"
    autoinit
  end

  addVariable!(fg, :x0, Pose2)
  addVariable!(fg, :x1, Pose2)
  !addlandmark ? nothing : addVariable!(fg, :l1, Point2)

  addFactor!(fg, [:x0], PriorPose2(MvNormal([0;0;0.0],Matrix(Diagonal([1.0;1.0;0.01])))), graphinit=graphinit)
  addFactor!(fg, [:x0;:x1], Pose2Pose2(MvNormal([10.0;0;0.0],Matrix(Diagonal([1.0;1.0;0.01])))), graphinit=graphinit)
  !addlandmark ? nothing : addFactor!(fg, [:x1;:l1], Pose2Point2BearingRange(Normal(0.0,0.01), Normal(20.0, 1.0)), graphinit=graphinit)

  return fg
end

"""
    $SIGNATURES

Load and solve a canonical or user factor graph to warm up---precompile---several RoME/Caesar related functions.
"""
function warmUpSolverJIT(;fg::AbstractDFG=generateCanonicalFG_Hexagonal(),
                          drawtree::Bool=true )::Nothing
  #

  fcts = ls(fg, :x0)
  fcts = ls(fg)
  fcts = lsf(fg, :x0f1)
  fcts = lsf(fg)
  getSolverParams(fg).drawtree = drawtree
  tree, smt, hist = solveTree!(fg)
  nothing
end

#


text:
#####################
## Transformations ##
#####################

import Base.collect
import Base.filter

# merge the dictionaries with append as the key conflict behavior
function append_merge(source::Dict, dest::Dict)
    for key in keys(source)
        if key in keys(dest)
            for val in source[key]
                push!(dest[key], val)
            end
        else
            dest[key] = source[key]
        end
    end
end

# merge a list of (k,{v}) tuples into a dict with append
function append_merge(source::Array, dest::Dict)
    for kv in source
        if kv[1] in keys(dest)
            for val in kv[2]
                push!(dest[kv[1]], val)
            end
        else
            dest[kv[1]] = kv[2]
        end
    end
end

#### Map ####

# require that map function is of the form func(key::Any, value::Array{Any})
function map(master::Master, rdd::RDD, map_func::ASCIIString)
    op = Transformation("map", {"function" => map_func})
    doop(master, {rdd}, op, NoPartitioner())
end

# assumes co-partitioning between new and old
function map(worker::Worker, newRDD::WorkerRDD, part_id::Int64, args::Dict)
    map_func = args["function"]
    old_rdd_id = collect(keys(newRDD.rdd.dependencies))[1]
    partition = worker.rdds[old_rdd_id].partitions[part_id].data
    for key in keys(partition)
        kv_pairs = eval(Expr(:call, symbol(map_func), key, partition[key]))
        append_merge(kv_pairs, newRDD.partitions[part_id].data)
    end

    return true
end

#### Filter ####

function filter(master::Master, rdd::RDD, filter_func::ASCIIString)
    op = Transformation("filter", {"function" => filter_func})
    doop(master, {rdd}, op, NoPartitioner())
end

# assumes co-partitioning between new and old
function filter(worker::Worker, newRDD::WorkerRDD, part_id::Int64, args::Dict)
    func = args["function"]
    old_rdd_id = collect(keys(newRDD.rdd.dependencies))[1]
    partition = worker.rdds[old_rdd_id].partitions[part_id].data
    newRDD.partitions[part_id].data = eval(Expr(:call, filter, symbol(func), partition))
    return true
end

#### Group by key ####

function group_by_key(master::Master, rdd::RDD)
    newRDD = partition_by(master, rdd, HashPartitioner())
    collection = collect(master, newRDD)
    if newRDD == false
        return false
    end
    op = Transformation("group_by_key", Dict())
    doop(master, {newRDD}, op, HashPartitioner())
end

function group_by_key(worker::Worker, newRDD::WorkerRDD, part_id::Int64, args::Dict)
    old_rdd_id = collect(keys(newRDD.rdd.dependencies))[1]
    partition = worker.rdds[old_rdd_id].partitions[part_id].data
    append_merge(partition, newRDD.partitions[part_id].data)
    return true
end

#TODO reduce_by_key
function reduce_by_key(worker::Worker, newRDD::WorkerRDD, part_id::Int64, args::Dict)
    return true
end

#### Join ####

function join(master::Master, rddA::RDD, rddB::RDD)
    newA = partition_by(master, rddA, HashPartitioner())
    newB = partition_by(master, rddB, HashPartitioner())
    if newA == false || newB == false
        return false
    end
    op = Transformation("join", Dict())
    doop(master, {newA, newB}, op, HashPartitioner())
end

# makes the assumption that RDDs are co-partitioned, if the partition exists
function join(worker::Worker, newRDD::WorkerRDD, part_id::Int64, args::Dict)
    id_a = collect(keys(newRDD.rdd.dependencies))[1]
    id_b = collect(keys(newRDD.rdd.dependencies))[2]
    if id_a in keys(worker.rdds)
        worker_rdd = worker.rdds[id_a]
        append_merge(worker_rdd.partitions[part_id].data, newRDD.partitions[part_id].data)
    end
    if id_b in keys(worker.rdds)
        worker_rdd = worker.rdds[id_b]
        append_merge(worker_rdd.partitions[part_id].data, newRDD.partitions[part_id].data)
    end
    return true
end

#TODO sort
function sort(worker::Worker, newRDD::WorkerRDD, part_id::Int64, args::Dict)
    return true
end

#### Partition by ####

function partition_by(master::Master, rdd::RDD, partitioner::Partitioner)
    op = Transformation("partition_by", {"partitioner" => partitioner})
    doop(master, {rdd}, op, partitioner)
end

function partition_by(worker::Worker, newRDD::WorkerRDD, part_id::Int64, args::Dict)
    partitioner = mkPartitioner(args["partitioner"])
    old_rdd_id = collect(keys(newRDD.rdd.dependencies))[1]
    local_rdd_copy = worker.rdds[old_rdd_id]
    partition = local_rdd_copy.partitions[part_id]
    for key in keys(partition.data)
        new_partitions = assign(partitioner, newRDD.rdd, key)
        for new_partition in new_partitions
            if !("dest_partition" in collect(keys(args))) || new_partition == args["dest_partition"]
                new_worker = newRDD.rdd.partitions[new_partition]
                bool_val = "dest_partition" in collect(keys(args))
                send_key(worker, new_worker, newRDD.rdd.ID, new_partition, key, partition.data[key])
            end
        end
    end
    return true
end

#### Input ####

function input(master::Master, filename::ASCIIString, reader::ASCIIString)
    op = Transformation("input", {"filename" => filename, "reader" => reader})
    doop(master, {}, op, NoPartitioner())
end

function input(worker::Worker, newRDD::WorkerRDD, part_id::Int64, args::Dict)
    reader = args["reader"]
    file_name = args["filename"]

    stream = open(file_name)
    total_lines = countlines(stream)
    seekstart(stream)

    lines_partition = floor(total_lines / length(newRDD.rdd.partitions))
    begin_line = lines_partition * part_id
    end_line = lines_partition * (part_id + 1) - 1
    if part_id == (length(newRDD.rdd.partitions) - 1)
        end_line = total_lines - 1 # last partition always goes to the end
    end
    for l = 0:begin_line-1
        line::String = readline(stream)
    end
    partition = WorkerPartition(Dict{Any, Array{Any}}())

    for l = begin_line:end_line
        line::String = readline(stream)
        kv_pairs = eval(Expr(:call, symbol(reader), line))
        append_merge(kv_pairs, partition.data)
    end

    #Adds partition to partition map
    newRDD.partitions[part_id] = partition
end


text:
using DrWatson
@quickactivate "AccuracyAtTop_DeepTopPush"

include(srcdir("utilities.jl"))

# ------------------------------------------------------------------------------------------
# Settings
# ------------------------------------------------------------------------------------------
Dataset_Settings = Dict(
    :dataset => [FashionMNIST, CIFAR100, SVHN2Full, Molecules],
    :posclass => [0, 1, 1, 1],
)

Train_Settings = Dict(
    :batchsize => [32, 64, 128],
    :iters => 50,
    :optimiser => Descent,
    :steplength => 0.0001,
)

Model_Settings = Dict(
    :type => [BaseLine, DeepTopPush, PatMatNP, APPerf],
    :arg => [missing, missing, 0.01, 0.01],
    :surrogate => [missing, quadratic, quadratic, missing],
    :reg => 1e-3,
    :buffer => [missing, true, missing, missing],
)

run_benchmark(Dataset_Settings, Train_Settings, Model_Settings)


text:
# Parsers
"""
    parse_repos!(license::AbstractString,
                 created_at::AbstractString = "2007-10-29T14:37:16+00:00",
                 until::ZonedDateTime = until)
    parse_repos!(license::AbstractString,
                 data,
                 as_of,
                 created_at::AbstractString = "2007-10-29T14:37:16+00:00",
                 until::ZonedDateTime = until)
For one a given license, it collects the information for each repository.

# Arguments

- `license` is a SPDX identifier for some OSI-approved license.
- `created_at` is a datetime interval that GitHub supports ([docs](https://help.github.com/en/articles/understanding-the-search-syntax#query-for-dates))
- `until`: this value is the end-date for the queries (`GitHubAPI.until`)

# Examples

Start querying for all the repositories with license `zlib`.

```
parse_repos!("zlib")
```

After checking the [`github_repos_tracker`](http://sdad.policy-analytics.net:8080/?pgsql=postgis_1&db=oss&ns=universe&select=github_repos_tracker),

If the license scrapper already has completed some work, you can resume the job by passing the `created_at` argument.

```
parse_repos!("zlib",
             "2016-04-20T15:59:13+00:00..2017-12-17T09:59:36+00:00")
```
"""
function parse_repos!(license::AbstractString,
                      created_at::AbstractString = "2007-10-29T14:37:16+00:00",
                      until::ZonedDateTime = until)
    (@isdefined(db_user) && @isdefined(db_pwd) &&
        @isdefined(github_login) && @isdefined(github_token)) ||
        throw(ArgumentError("Run sdad_setup! before making a connection."))
    data, as_of, created_at = binary_search_dt_interval(license, created_at)
    conn = dbconnect()
    execute(conn,
            """insert into universe.github_repos_tracker values(
               '$license', '$created_at', $(data.repositoryCount)
               )
               on conflict (spdx, query) do update set
               total = excluded.total
            """)
    if !isempty(data.nodes)
        foreach(node -> insert_record_repos_by_license!(conn,
                                                        license, created_at, as_of,
                                                        node),
                data.nodes)
    end
    close(conn)
    while data.pageInfo.hasNextPage
        result = client.Query(github_api_query,
                              operationName = "SearchCursor",
                              vars = Dict("license_created" =>
                                          "license:$license created:$created_at",
                                          "cursor" => data.pageInfo.endCursor))
        as_of = get_as_of(result.Info)
        json = JSON3.read(result.Data)
        @assert(haskey(json, :data))
        data = json.data
        github_wait_out(data.rateLimit)
        data = data.search
        conn = dbconnect()
        if !isempty(data.nodes)
            foreach(node -> insert_record_repos_by_license!(conn,
                                                            license, created_at, as_of,
                                                            node),
                    data.nodes)
        end
        close(conn)
    end
    dt_end = match(r"(?<=\.\.).*", created_at).match |>
        (dt -> ZonedDateTime(dt, github_dtf))
    created_at = "$dt_end..$until"
    data, as_of, created_at = binary_search_dt_interval(license, created_at)
    iszero(data.repositoryCount) ||
        parse_repos!(license, data, as_of, created_at, until)
end
function parse_repos!(license::AbstractString,
                      data,
                      as_of,
                      created_at::AbstractString = "2007-10-29T14:37:16+00:00",
                      until::ZonedDateTime = until)
    (@isdefined(db_user) && @isdefined(db_pwd) &&
        @isdefined(github_login) && @isdefined(github_token)) ||
        throw(ArgumentError("Run sdad_setup! before making a connection."))
    conn = dbconnect()
    execute(conn,
            """insert into universe.github_repos_tracker values(
               '$license', '$created_at', $(data.repositoryCount)
               )
               on conflict (spdx, query) do update set
               total = excluded.total
            """)
    if !isempty(data.nodes)
        foreach(node -> insert_record_repos_by_license!(conn,
                                                        license, created_at, as_of,
                                                        node),
                data.nodes)
    end
    close(conn)
    while data.pageInfo.hasNextPage
        result = client.Query(github_api_query,
                              operationName = "SearchCursor",
                              vars = Dict("license_created" =>
                                          "license:$license created:$created_at",
                                          "cursor" => data.pageInfo.endCursor))
        as_of = get_as_of(result.Info)
        json = JSON3.read(result.Data)
        @assert(haskey(json, :data))
        data = json.data
        github_wait_out(data.rateLimit)
        data = data.search
        conn = dbconnect()
        if !isempty(data.nodes)
            foreach(node -> insert_record_repos_by_license!(conn,
                                                            license, created_at, as_of,
                                                            node),
                    data.nodes)
        end
        close(conn)
    end
    dt_end = match(r"(?<=\.\.).*", created_at).match |>
        (dt -> ZonedDateTime(dt, github_dtf))
    created_at = "$dt_end..$until"
    data, as_of, created_at = binary_search_dt_interval(license, created_at)
    iszero(data.repositoryCount) ||
        parse_repos!(license, data, as_of, created_at, until)
end
"""
    parse_commits!(slug::AbstractString, until::AbstractString)
    parse_commits!(slug::AbstractString, until::ZonedDateTime = until)
Writes to the database all the commit history for that repository.
# Examples
## Query one repository.
```
parse_commits!("uva-bi-sdad/GitHubAPI.jl")
```
## For resuming work, find the date of the oldest commit in the [database](http://sdad.policy-analytics.net:8080/?pgsql=postgis_1&db=oss&ns=universe).
```
parse_commits!("uva-bi-sdad/GitHubAPI.jl", "2019-01-01 15:30:00+00")
```
"""
parse_commits!(slug::AbstractString, until::AbstractString) =
    parse_commits!(slug, ZonedDateTime(until, postgis_dtf))
function parse_commits!(slug::AbstractString,
                        until::ZonedDateTime = until)
    (@isdefined(db_user) && @isdefined(db_pwd) &&
        @isdefined(github_login) && @isdefined(github_token)) ||
        throw(ArgumentError("Run sdad_setup! before making a connection."))
    owner, name = split(slug, '/')
    result = client.Query(github_api_query,
                          operationName = "Repository",
                          vars = Dict("owner" => owner,
                                      "name" => name,
                                      "until" => until))
    json = JSON3.read(result.Data)
    @assert(haskey(json, :data))
    github_wait_out(json.data.rateLimit)
    if haskey(json, :errors)
        if json.errors[1].type ∈ ["NOT_FOUND", "SERVICE_UNAVAILABLE"]
            oid = join("0" for x ∈ 1:40)
            login = "null"
            additions = "null"
            deletions = "null"
            committedDate = "null"
            conn = dbconnect()
            execute(conn,
                    """insert into universe.github_commits values(
                       '$slug', $login, '$oid',
                       $additions, $deletions, $committedDate
                       )
                       on conflict (slug, sha) do nothing
                    """)
            close(conn)
            return true
        end
    end
    data = json.data.repository.defaultBranchRef.target.history
    conn = dbconnect()
    if !isempty(data.nodes)
        foreach(node -> insert_commit!(conn, slug, node), data.nodes)
    end
    close(conn)
    while data.pageInfo.hasNextPage
        try
            result = client.Query(github_api_query,
                                  operationName = "RepositoryCursor",
                                  vars = Dict("owner" => owner,
                                              "name" => name,
                                              "until" => until,
                                              "cursor" => data.pageInfo.endCursor))
            json = JSON3.read(result.Data)
            @assert(haskey(json, :data))
            github_wait_out(json.data.rateLimit)
            data = json.data.repository.defaultBranchRef.target.history
            conn = dbconnect()
            if !isempty(data.nodes)
                foreach(node -> insert_commit!(conn, slug, node), data.nodes)
            end
            close(conn)
        catch
            sleep(1)
        end
    end
    true
end


text:
using Test
using MIPVerify
using MIPVerify: UnrestrictedPerturbationFamily, BlurringPerturbationFamily, LInfNormBoundedPerturbationFamily
@isdefined(TestHelpers) || include("../../../TestHelpers.jl")

@testset "Conv + FC + Softmax" begin
    ### Parameters for neural net
    batch = 1
    c1_in_height = 8
    c1_in_width = 8
    p1_stride_height = 2
    p1_stride_width = 2
    p1_strides = (1, p1_stride_height, p1_stride_width, 1)
    p1_height = round(Int, c1_in_height/p1_stride_height, RoundUp)
    p1_width = round(Int, c1_in_width/p1_stride_width, RoundUp)
    c1_in_channels = 1
    c1_filter_height = 2
    c1_filter_width = 2
    c1_out_channels = 2

    l1_height = 5
    l1_width = p1_height*p1_width*c1_out_channels

    l2_height = 3
    l2_width = l1_height

    ### Choosing data to be used
    input = gen_array(
        (batch, c1_in_height, c1_in_width, c1_in_channels),
        0, 1
    )

    c1_kernel = gen_array(
        (c1_filter_height, c1_filter_width, c1_in_channels, c1_out_channels),
        -1, 1
    )
    c1_bias = gen_array((c1_out_channels,), -1, 1)

    l1_kernel = gen_array((l1_width, l1_height), -1, 1)
    l1_bias = gen_array((l1_height,), -1, 1)

    l2_kernel = gen_array((l2_width, l2_height), -1, 1)
    l2_bias = gen_array((l2_height,), -1, 1)

    nn = Sequential(
        [
            Conv2d(c1_kernel, c1_bias), MaxPool(p1_strides), ReLU(),
            Flatten(4),
            Linear(l1_kernel, l1_bias), ReLU(),
            Linear(l2_kernel, l2_bias)
        ],
        "tests.integration.generated_weights.conv+fc+softmax"
    )

    @testset "BlurringPerturbationFamily" begin
        pp_blur = BlurringPerturbationFamily((5, 5))
        # TODO (vtjeng): Add example where blurring perturbation generates non-NaN results
        test_cases = [
            ((2, pp_blur, 1, 0), NaN),
            ((3, pp_blur, 1, 0), NaN),
        ]

        TestHelpers.batch_test_adversarial_example(nn, input, test_cases)
    end

    @testset "UnrestrictedPerturbationFamily" begin
        pp_unrestricted = UnrestrictedPerturbationFamily()
        @testset "Minimizing l1 norm" begin
            test_cases = [
                ((1, pp_unrestricted, 1, 0), 0),
                ((2, pp_unrestricted, 1, 0), 9.2825418),
            ]

            TestHelpers.batch_test_adversarial_example(nn, input, test_cases)
        end

        @testset "Minimizing lInf norm" begin
            test_cases = [
                ((1, pp_unrestricted, Inf, 0), 0),
                ((2, pp_unrestricted, Inf, 0), 0.4347241),
                ((3, pp_unrestricted, Inf, 0), 0.5045819),
            ]

            TestHelpers.batch_test_adversarial_example(nn, input, test_cases)
        end

        @testset "Increasing margin increases required distance" begin
            test_cases = [
                ((2, pp_unrestricted, 1, 0.1), 9.5642628),
            ]

            TestHelpers.batch_test_adversarial_example(nn, input, test_cases)
        end

        @testset "With multiple target labels specified, minimum target label found" begin
            test_cases = [
                (([2, 3], pp_unrestricted, Inf, 0), 0.4347241),
            ]

            TestHelpers.batch_test_adversarial_example(nn, input, test_cases)
        end
    end

    @testset "LInfNormBoundedPerturbationFamily" begin
        test_cases = [
            ((2, LInfNormBoundedPerturbationFamily(0.43), Inf, 0), NaN),  # restricting maximum perturbation to below minimum distance causes optimization problem to be infeasible
            ((2, LInfNormBoundedPerturbationFamily(0.435), Inf, 0), 0.4347241),  # restricting maximum perturbation to above minimum distance does not affect optimal value of problem
        ]

        TestHelpers.batch_test_adversarial_example(nn, input, test_cases)
    end
end


text:
using XGPaint
using Healpix

## Load halos from HDF5 files, establish a CIB model and cosmology
halo_pos, halo_mass = read_halo_catalog_hdf5(
    "/tigress/zequnl/xgpaint/websky_halos-light.hdf5")
# @load "/home/zequnl/testsamp.jld2" halo_mass halo_pos

cosmo = get_cosmology(h=0.7f0, OmegaM=0.25f0)
model = CIB_Planck2013{Float32}()

## Allocate some arrays and file them up for centrals and satellites
@time sources = generate_sources(model, cosmo, halo_pos, halo_mass);

## Deposit the sources into maps
fluxes_cen = Array{Float32, 1}(undef, sources.N_cen)
fluxes_sat = Array{Float32, 1}(undef, sources.N_sat)
m = Map{Float64,RingOrder}(model.nside)

for freq in ["030", "090", "148", "219", "277", "350",
        "143", "217", "353", "545", "857"]
    @time begin
        XGPaint.paint!(m, parse(Float32, freq) * 1.0f9, model, sources,
            fluxes_cen, fluxes_sat)
        Healpix.saveToFITS(m, "!/tigress/zequnl/xgpaint/jl/cib/cib$(freq).fits")
    end
end


text:
# Utility functions for testing

function ignore_output(body::Function; stdout::Bool=true, stderr::Bool=true)
    out_old = Base.stdout
    err_old = Base.stderr

    if stdout
        (out_rd, out_wr) = redirect_stdout()
    end
    if stderr
        (err_rd, err_wr) = redirect_stderr()
    end

    result = try
        body()
    finally
        if stdout
            redirect_stdout(out_old)
            close(out_wr)
            close(out_rd)
        end
        if stderr
            redirect_stderr(err_old)
            close(err_wr)
            close(err_rd)
        end
    end

    return result
end

# Used in tests as a shorter form of: `sprint(show, ..., context=:compact => true)`
show_compact = (io, args...) -> show(IOContext(io, :compact => true), args...)

# Takes the tuple from `compile` and adds the result into TimeZones cache. Typically should
# not be used and only should be required if the test tzdata version and built tzdata
# version do not match.
function cache_tz((tz, class)::Tuple{TimeZone, TimeZones.Class})
    TimeZones._tz_cache()[TimeZones.name(tz)] = (tz, class)
    return tz
end


text:
using BinaryProvider

# Parse some basic command-line arguments
const verbose = "--verbose" in ARGS
const prefix = Prefix(get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr")))

libpath = joinpath(@__DIR__, "usr/darknet-master")

products = Product[
    LibraryProduct(libpath,"libdarknet", :libdarknet)
    ]

# Download binaries from hosted location
bin_prefix = "https://github.com/ianshmean/bins/raw/master/3rdparty/Darknet"

download_info = Dict(
    #Linux(:x86_64)  => ("$bin_prefix/Darknet-AlexeyAB-YOLOv3-cuda10.1-cudnn7.5.0-Ubuntu18.04.tar.gz", "5ddad410540fac019e1996a4705075b259b4bc2dd13d220dc9bfd90496e64ed1"),
    MacOS(:x86_64)  => ("$bin_prefix/darknet-AlexeyAB-YOLOv3-MacOS.10.14.3-CPU-only.tar.gz", "c9d79e1918c785149d39920608b4efb22fc910895ab6baf9aa5f7f43169a37fe"),
    #MacOS(:x86_64)  => ("$bin_prefix/arcbasic.tar.gz", "06803eab8c89ec7c1bf39796ea448217614362b8a51a6c82eaf286be1574ba4d")
)
# First, check to see if we're all satisfied
@show satisfied(products[1]; verbose=true)
if any(!satisfied(p; verbose=false) for p in products)
    try
        # Download and install binaries
        url, tarball_hash = choose_download(download_info)
        install(url, tarball_hash; prefix=prefix, force=true, verbose=true)
    catch e
        if typeof(e) <: ArgumentError || typeof(e) <: MethodError
            error("Your platform $(Sys.MACHINE) is not supported by this package!")
        else
            rethrow(e)
        end
    end

    # Finally, write out a deps.jl file
    write_deps_file(joinpath(@__DIR__, "deps.jl"), products)
end


text:
include("spritz.jl")

# w pliku keys_N3_len3.txt sa wszystkie klucze dlugosci 3 z N=3
# wczytujemy jako tablice i konwertujemy do Intow

Klen=2
N=256

keys=zeros(N^Klen,Klen)

for v in 1:N^Klen
     tmp=v-1
    for j in 1:Klen
     #print("v = ", v, "j=",j,"  ")
     keys[v,j]= tmp % N
     tmp = convert(Int64,(tmp - (tmp % N) )  / N )
     #convert(Int64,floor(tmp / N))
    end
 #print(v," ")
end
keys=convert(Array{Int64,2},keys)

 println("keys = ",keys)
#println(keys)

#  A=convert( Array{Int64,2},readcsv("keys_N3_len3.txt"))

#M=[0 0 0 0 0 0]
M=[1 0]

M_len=size(M)[2]

### rows=size(A)[1]
### columns=size(A)[2]

rows=N^Klen
columns=Klen

C=ones(rows,M_len)
S=ones(rows,N)

#print("typeof(S)= ",typeof(S)," typeof(keys[3,1:3])=",typeof(keys[3,1:3]))

#println("asdf");

for v in 1:rows
   # C[v,:]=Spritz_Encrypt(N, A[v,1:3], M)
  # C[v,:]=       Spritz_Encrypt(N, keys[v,1:Klen], M,0)
   S[v,:]=       Spritz_Encrypt(N, keys[v,1:Klen], M,1)

   #println("Spritz_Encrypt(",N,", ",keys[v,1:Klen],", ",M,",0)");
end
# println("S=",S);

C=convert(Array{Int64,2},C)
S=convert(Array{Int64,2},S)

### writecsv("encrypted_all_keys_N3_len3_m.txt",C)

### writecsv("permuations_all_keys_N3_len3_m.txt",S)

writecsv("encrypted_all_tmp.txt",C)
writecsv("permutations_all_tmp.txt",S)

#writecsv("permuations_all_tmp.txt",S)

# return C

# sp=Spritz(8)

# Spritz_Show(sp)

# println("Update...1")
# Spritz_Update(sp)

# Spritz_Show(sp)

# println("Update...2")
# Spritz_Update(sp)

# Spritz_Show(sp)

# println("Update...3")
# Spritz_Update(sp)

# Spritz_Show(sp)

max_stars_repo_name: PawelPamula/MarkovTools

max_issues_repo_name: PawelPamula/MarkovTools

max_forks_repo_name: PawelPamula/MarkovTools


text:
import Dates
import GitHub
import HTTP
import TimeZones

function _most_recent(registry::GitHub.Repo;
                      api::GitHub.GitHubAPI,
                      auth::GitHub.Authorization,
                      event::AbstractString,
                      workflow_name::AbstractString)
    endpoint = "/repos/$(registry.full_name)/actions/runs"
    params = Dict(
        "event" => event,
    )
    json = GitHub.gh_get_json(
        api,
        endpoint;
        auth = auth,
        params = params,
    )
    workflow_runs = json["workflow_runs"]
    for workflow_run in workflow_runs
        if workflow_run["name"] == workflow_name
            if workflow_run["event"] == event
                created_at = TimeZones.ZonedDateTime(
                    workflow_runs[1]["created_at"],
                    "yyyy-mm-ddTHH:MM:SSzzzz"
                )
                return created_at
            end
        end
    end
    throw(ErrorException("I could not figure out when the most recent AutoMerge cron job was"))
end

function most_recent_automerge(registry::GitHub.Repo;
                               api::GitHub.GitHubAPI,
                               auth::GitHub.Authorization)
    schedule = _most_recent(
        registry;
        api = api,
        auth = auth,
        event = "schedule",
        workflow_name = "AutoMerge"
    )
    workflow_dispatch = _most_recent(
        registry;
        api = api,
        auth = auth,
        event = "workflow_dispatch",
        workflow_name = "AutoMerge"
    )
    return max(schedule, workflow_dispatch)
end

function time_since_last_automerge(registry::GitHub.Repo;
                                   api::GitHub.GitHubAPI,
                                   auth::GitHub.Authorization)
    last_automerge = most_recent_automerge(registry; api = api, auth = auth)
    now = TimeZones.now(TimeZones.localzone())
    return now - last_automerge
end

function trigger_new_workflow_dispatch(registry::GitHub.Repo;
                                       api::GitHub.GitHubAPI,
                                       auth::GitHub.Authorization,
                                       workflow_file_name::AbstractString)
    endpoint = "/repos/$(registry.full_name)/actions/workflows/$(workflow_file_name)/dispatches"
    params = Dict(
        "ref" => "master",
    )
    GitHub.gh_post(
        api,
        endpoint;
        auth = auth,
        params = params,
    )
    return nothing
end

function _canonicalize(p::Dates.CompoundPeriod)
    return Dates.canonicalize(p)
end

function _canonicalize(p::Dates.Period)
    return _canonicalize(Dates.CompoundPeriod(p))
end

function trigger_new_automerge_if_necessary()
    api = GitHub.DEFAULT_API
    auth = GitHub.authenticate(ENV["AUTOMERGE_TAGBOT_TOKEN"])
    registry = GitHub.repo(
        api,
        "JuliaRegistries/General";
        auth = auth,
    )
    t = time_since_last_automerge(
        registry;
        api,
        auth,
    )
    @info "Time since last AutoMerge" t _canonicalize(t)
    if t >= Dates.Minute(8)
        @info "Attempting to trigger a new AutoMerge workflow dispatch job..."
        trigger_new_workflow_dispatch(
            registry;
            api,
            auth,
            workflow_file_name = "automerge.yml",
        )
        @info "Triggered a new AutoMerge workflow dispatch job"
    end
    return nothing
end

trigger_new_automerge_if_necessary()

max_stars_repo_name: anandijain/General

max_issues_repo_name: anandijain/General

max_forks_repo_name: anandijain/General


text:
include("laff_dot.jl")

laff_norm2(x::T) where T <: Matrix{Number} = sqrt(laff_dot(x,x))

## Testar ##
x = Matrix([5,-2], 1, 2)
laff_norm2(x)

max_stars_repo_path: LAFF/Semana 1/laff_norm2.jl

max_issues_repo_path: LAFF/Semana 1/laff_norm2.jl

max_forks_repo_path: LAFF/Semana 1/laff_norm2.jl


text:
using StaticArrays: SVector, setindex
using AbstractTrees
using MacroTools

struct Nil end

const nil = Nil()

Base.isequal(::Nil, ::Nil) = true
Base.isequal(::Nil, ::Integer) = false
Base.isequal(::Integer, ::Nil) = false
Base.isless(::Nil, ::Integer) = true
Base.isless(::Integer, ::Nil) = false
Base.isless(::Nil, ::Nil) = false

"""
    masktuple(f, g, I)
Return `R::Tuple` such that `R[j] == f(j) when `I[j] isa
Nil` and `R[j] == g(I[j])` otherwise.
# Examples
```jldoctest
julia> A = [-1; -2; -3; -4; -5];
julia> B = [11; 12; 13; 14; 15];
julia> I = (2; 4; nil; 3; 1);
julia> masktuple(i->A[i], j->B[i], I)
  (12, 14, -3, 13, 11)
```
"""
@inline function masktuple(f, g, I::Tuple{Vararg{Union{Int, Nil}}})
    ntuple(j -> I[j] isa Nil ? f(j) : g(I[j]), length(I))
end
@generated function masktuple(f, g, ::Val{I}) where {I}
    return quote
        Base.@_inline_meta
        ($(masktuple(j->:(f($j)), j->:(g($j)),I)...),)
    end
end

"""
    imasktuple(f, g, I, n)
Return `R::Tuple` of length `n` such that `R[I[j]] == g(j)` whenever
`!(I[j] isa Nil)`, and `R[i] = f[i]` otherwise.
# Examples
```jldoctest
julia> A = (2, 4, 0, 3, 1);
julia> imasktuple(A, (-1, -2), (nil, 3))
  (2, 4, -2, 3, 1)
```
"""
@inline function imasktuple(f, g, I::Tuple{Vararg{Union{Int, Nil}}}, N)
    ntuple(j-> (k = findfirst(isequal(j), I)) === nothing ? f(j) : g(k), N)
end
@generated function imasktuple(f, g, ::Val{I}, ::Val{N}) where {I, N}
    return quote
        Base.@_inline_meta
        ($(imasktuple(i->:(f($i)), i->:(g($i)), I, N)...),)
    end
end

@inline jointuple(x) = x
@inline jointuple(x, y) = (x..., y...)
@inline jointuple(x, y, z...) = (x..., jointuple(y, z...)...)

@inline maptuple(f::F) where {F} = ()
@inline maptuple(f::F, x) where {F} = (f(x),)
@inline maptuple(f::F, x, y, z...) where {F} = (f(x), maptuple(f, y, z...)...)

@inline ziptuple(args::Tuple{}...) = ()
@inline ziptuple(args::Tuple...) = (map(first, args), ziptuple(map(Base.tail, args)...)...)

@inline nziptuple(N, args::Tuple{}...) = ntuple(n->(), N)
@inline nziptuple(N, args::Tuple...) = (map(first, args), nziptuple(N, map(Base.tail, args)...)...)

@inline expandtuple(N, arg::Tuple{}) = ntuple(n->(), N)
@inline expandtuple(N, arg::Tuple) = arg

@inline combinetuple(f, arg) = arg
@inline combinetuple(f::F, arg, tail...) where {F} = _combinetuple(f, arg, combinetuple(f, tail...))
@inline _combinetuple(f, ::Tuple{}, ::Tuple{}) = ()
@inline _combinetuple(f, ::Tuple{}, b::Tuple) = b
@inline _combinetuple(f, a::Tuple, ::Tuple{}) = a
@inline _combinetuple(f::F, a::Tuple, b::Tuple) where {F} = (f(first(a), first(b)), _combinetuple(f, Base.tail(a), Base.tail(b))...)

using Base.Broadcast: broadcasted, BroadcastStyle, Broadcasted

struct Delay
  value
end

Delay() = Delay(nothing)

Base.Broadcast.broadcasted(::BroadcastStyle, ::Delay, bc) = Delay(bc)

Base.Broadcast.materialize(d::Delay) = d.value

macro _(ex)
    return :(Delay().($(esc(ex))))
end

struct Insert{F}
  f::F
end

@inline Base.Broadcast.broadcasted(::BroadcastStyle, i::Insert, bc) = i.f(bc)

macro :($$)(ex)
    @assert @capture(ex, f_(args__))
    return :(Insert($(esc(f))).($(map(esc, args)...)))
end

abstract type Intercept end

@inline Base.Broadcast.broadcasted(style::BroadcastStyle, intr::Intercept, args...) = intr(args...)

export dump_type_tree

struct _TypeTreeDumper
    t
end

_isleaf(d::_TypeTreeDumper) = length("$(d.t)") < 70

AbstractTrees.children(d::_TypeTreeDumper) = _isleaf(d) ? [] : map(_TypeTreeDumper, d.t.parameters)

AbstractTrees.printnode(io::IO, d::_TypeTreeDumper) = _isleaf(d) ? print(io, d.t) : print(io,d.t.name)

function dump_type_tree(io::IO, t::DataType)
    print_tree(io, _TypeTreeDumper(t))
end

dump_type_tree(t::DataType) = dump_type_tree(stdout, t)


text:
#this file contains miscallaneous functions mainly related to the solve.jl file.

#   https://fenicsproject.org/olddocs/dolfin/2016.2.0/python/programmers-reference/fem/solving/solve.html

"""
lists available lu solver methods
"""
list_lu_solver_methods()=fenics.list_lu_solver_methods()
"""
lists available krylov solver methods
"""
list_krylov_solver_methods()=fenics.list_krylov_solver_methods()
"""
lists available krylov solver preconditioners
"""
list_krylov_solver_preconditioners()=fenics.list_krylov_solver_preconditioners()
"""
lists available linear solver methods
"""
list_linear_solver_methods()=fenics.list_linear_solver_methods()

"""
Lists information for NonlinearVariationalSolver
"""
info_NonLinearVariationalSolver() = fenics.info(fenics.NonlinearVariationalSolver.default_parameters(), true)
"""
Lists information for LinearVariationalSolver
"""
info_LinearVariatonalSolver() = fenics.info(fenics.LinearVariationalSolver.default_parameters(), true)

export list_lu_solver_methods, list_krylov_solver_methods, list_krylov_solver_preconditioners, list_linear_solver_methods, info_NonLinearVariationalSolver, info_LinearVariatonalSolver

"""
Provide values for some of the constants
"""
DOLFIN_PI()=fenics.DOLFIN_PI
DOLFIN_EPS()=fenics.DOLFIN_EPS
DOLFIN_SQRT_EPS()=fenics.DOLFIN_SQRT_EPS

export DOLFIN_PI, DOLFIN_EPS, DOLFIN_SQRT_EPS

struct MPI_Comm <: fenicsobject
  pyobject::PyObject
end

mpi_comm_world() = MPI_Comm(fenics.mpi_comm_world())
mpi_comm_self() = MPI_Comm(fenics.mpi_comm_world())

export MPI_Comm, mpi_comm_world, mpi_comm_self


text:
module MatDeforNonlinearModule

using FinEtools.FTypesModule: FInt, FFlt, FCplxFlt, FFltVec, FIntVec, FFltMat, FIntMat, FMat, FVec, FDataDict
import FinEtoolsDeforLinear.DeforModelRedModule: AbstractDeforModelRed
import FinEtoolsDeforLinear.MatDeforModule: AbstractMatDefor
import LinearAlgebra:  det

"""
    AbstractMatDeforNonlinear <: AbstractMatDefor

Abstract nonlinear material.

"""
abstract type AbstractMatDeforNonlinear <: AbstractMatDefor; end

"""
    tangentmoduli!(self::M, D::FFltMat, statev::FFltVec, Fn1::FFltMat,
        Fn::FFltMat, tn::FFlt, dtn::FFlt, loc::FFltMat, label::FInt)
        where {M<:AbstractMatDeforNonlinear}

Calculate the material stiffness matrix.

# Arguments
- `self` = material
- `D` = matrix of tangent moduli, supplied as a buffer and overwritten.
  Returned as output.
- `statev` = material state vector, the content of this vector must not change
    inside this function.
- `Fn1` = deformation gradient at time `tn1 = tn + dtn`,
- `Fn` = deformation gradient at time `tn`,
- `tn` = time in step `n`
- `dtn` = increment of time
- `loc` = location of the integration point in the reference coordinates (time `t0`),
- `label` = label of the element containing the integration point

!!! note
The deformation gradients and the matrix of the tangent moduli are expressed
with respect to the local material coordinate system.
"""
function tangentmoduli!(self::M, D::FFltMat, statev::FFltVec, Fn1::FFltMat, Fn::FFltMat, tn::FFlt, dtn::FFlt, loc::FFltMat, label::FInt) where {M<:AbstractMatDeforNonlinear}
    return self.tangentmoduli!(self, D, statev, Fn1, Fn, tn, dtn, loc, label)
end

"""
    update!(self::M, statev::FFltVec, stress::FFltVec, output::FFltVec,
        Fn1::FFltMat, Fn::FFltMat, tn::FFlt, dtn::FFlt,
        loc::FFltMat=zeros(3,1), label::FInt=0, quantity=:nothing)
        where {M<:AbstractMatDeforNonlinear}

Update material state.

# Arguments
- `self` = material
- `statev` = state variables: array which is (if necessary) allocated  in an appropriate
     size, filled with the state variables, and returned. The contents of this
     vector may change as the state of the material is updated by the logic inside
     this function. If this change is to be saved, it must happen outside of this
     function.
- `cauchy` = Cauchy stress vector, allocated by the caller with a size of the
  number of stress and strain components, `nstressstrain`. The components of
  the stress vector are calculated and stored in the `stress` vector.
- `output` =  array which is (if necessary) allocated  in an appropriate size,
     filled with the output quantity, and returned.
- `Fn1` = deformation gradient at time `tn1 = tn + dtn`,
- `Fn` = deformation gradient at time `tn`,
- `tn` = time in step `n`
- `dtn` = increment of time
- `loc` = location of the integration point in the reference coordinates (time `t0`),
- `label` = label of the element containing the integration point

# Output
- `cauchy` = Cauchy stress vector
- `output` = output array

!!! note
The deformation gradients and the stress vector are expressed
with respect to the local material coordinate system.
"""
function update!(self::M, statev::FFltVec, cauchy::FFltVec, output::FFltVec,  Fn1::FFltMat, Fn::FFltMat, tn::FFlt, dtn::FFlt, loc::FFltMat=zeros(3,1), label::FInt=0, quantity=:nothing) where {M<:AbstractMatDeforNonlinear}
    return self.update!(self, statev, cauchy, output, Fn1, Fn, tn, dtn, loc, label, quantity)
end

"""
    newstate(self::M) where {M<:AbstractMatDeforNonlinear}

Create an initial material state at an integration point.
"""
function newstate(self::M) where {M<:AbstractMatDeforNonlinear}
    return FFlt[] # the default material state is an empty array: no state to store
end

"""
    estimatesoundspeed(self::M)  where {M<:AbstractMatDeforNonlinear}

Estimate sound speed in the undeformed state.
"""
function estimatesoundspeed(self::M)  where {M<:AbstractMatDeforNonlinear}
    return 0.0
end

"""
    totlag2curr!(c, C, F)

Convert a total Lagrangean constitutive matrix to a current Lagrangean one
(sometimes known as "Eulerian").

- `C`    = Lagrangean constitutive matrix, 6x6, symmetric
- `F`    = current deformation gradient, F_iJ = partial x_i / partial X_J

The transformation is c_ijkl = 1/J C_IJKL F_iI F_jJ F_kK F_lL. In the present
case the fourth-order tensor is represented with a 6 x 6 matrix.
"""
function totlag2curr!(c, C, F)
	@assert size(F) == (3, 3)
	@assert size(C) == (6, 6)
    t4 = ((1, 1), (2, 2), (3, 3), (1, 2), (1, 3), (2, 3));
    t2 = ((1, 4, 5), (4, 2, 6), (5, 6, 3))
    for mj in 1:size(c, 2)
    	k, l = t4[mj]
    	for mi in 1:size(c, 1)
    		i, j = t4[mi]
    		s = 0.0
    		@inbounds for I in 1:3
    			@inbounds for J in 1:3
    				mI = t2[I][J]
    				FiIFjJ = F[i, I] * F[j, J]
    				@inbounds for K in 1:3
    					@inbounds for L in 1:3
    						mJ = t2[K][L]
    						s += FiIFjJ * F[k, K] * F[l, L] * C[mI, mJ]
    					end
    				end
    			end
    		end
    		c[mi, mj] = s
    	end
    end
    c .*= (1.0 / det(F))
    return c
end

"""
    totlag2currsymm!(c, C, F)

Convert a total Lagrangean constitutive matrix to a current Lagrangean one
(sometimes known as "Eulerian").

- `C`    = Lagrangean constitutive matrix, 6x6, symmetric
- `F`    = current deformation gradient, F_iJ = partial x_i / partial X_J

The transformation is c_ijkl = 1/J C_IJKL F_iI F_jJ F_kK F_lL. In the present
case the fourth-order tensor is represented with a 6 x 6 matrix.

!!! note
The Lagrangean material stiffness matrices, both input and output, are
presumed symmetric.
"""
function totlag2currsymm!(c, C, F)
	@assert size(F) == (3, 3)
	t4 = ((1, 1), (2, 2), (3, 3), (1, 2), (1, 3), (2, 3));
	t2 = ((1, 4, 5), (4, 2, 6), (5, 6, 3))
    for mj in 1:size(c, 2)
    	k, l = t4[mj]
    	for mi in 1:mj
    		i, j = t4[mi]
    		s = 0.0
    		@inbounds for I in 1:3
    			@inbounds for J in 1:3
    				mI = t2[I][J]
    				fiIjJ = F[i, I] * F[j, J]
    				@inbounds for K in 1:3
    					@inbounds for L in 1:3
    						mJ = t2[K][L]
    						s += fiIjJ * F[k, K] * F[l, L] * C[mI, mJ]
    					end
    				end
    			end
    		end
    		c[mj, mi] = c[mi, mj] = s
    	end
    end
    c .*= (1.0 / det(F))
    return c
end

"""
    totlag2curr4th!(c, C, F)

Convert a total Lagrangean constitutive matrix to a current Lagrangean one
(sometimes known as "Eulerian").

- `C`    = Lagrangean constitutive matrix, fourth-order tensor
- `F`    = current deformation gradient, F_iJ = partial x_i / partial X_J

The transformation is c_ijkl = 1/J C_IJKL F_iI F_jJ F_kK F_lL. Both the input
and the output are fourth-order tensors.
"""
function totlag2curr4th!(c, C, F)
	@assert size(F) == (3, 3)
	@assert size(c) == (3, 3, 3, 3)
	@assert size(C) == (3, 3, 3, 3)
    n = size(F, 1)
    c .= 0.0
    @inbounds for i in 1:n
    	@inbounds for j in 1:n
    		@inbounds for k in 1:n
    			@inbounds for l in 1:n
    				@inbounds for I in 1:n
    					@inbounds for J in 1:n
    						@inbounds for K in 1:n
    							@inbounds for L in 1:n
    								c[i, j, k, l] += F[i, I] * F[j, J] * F[k, K] * F[l, L] * C[I, J, K, L]
    							end
    						end
    					end
    				end
    			end
    		end
    	end
    end
    c ./= det(F)
    return c
end

end


text:
# multiple dispatch:
f(n, m) = "base case"
f(n::Number, m::Number) = "n and m are both numbers"
f(n::Number, m) = "n is a number"
f(n, m::Number) = "m is a number"
f(n::Integer, m::Integer) = "n and m are both integers"
#> f (generic function with 5 methods)
f(1.5, 2) #> "n and m are both numbers"
f(1, "bar") #> "n is a number"
f(1, 2) #> "n and m are both integers"
f("foo", [1,2]) #> "base case"
f(n::Float64, m::Integer) = "n is a float and m is an integer"
#> f (generic function with 6 methods)
f(1.5, 2) #> "n is a float and m is an integer"

# methods:
methods(+)
methods(sort)

using InteractiveUtils
InteractiveUtils.methodswith(String)

# measuring execution:
fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
@time fib(35)
@elapsed fib(35) #> elapsed time: 0.115853481 seconds (45144 bytes allocated)

# broadcasting:
arr = [1.0, 2.0, 3.0]
sin.(arr)
#> 3-element Array{Float64,1}:
#  0.8414709848078965
#  0.9092974268256817
#  0.1411200080598672

f(x,y) = x + 7y
f.(pi, arr)
#> 3-element Array{Float64,1}:
# 10.141592653589793
# 17.141592653589793
# 24.141592653589793