text:
type XMLBuilder
    stk::Vector{Int}
    last::Int
    len::Int
    parent_offs::Vector{Int}
    parent_vals::Vector{Int}
    name_vals::Vector{String}
    attr_offs::Vector{Int}
    attr_vals::Vector{Pair{String,String}}
    itext_vals::Vector{String}
    otext_vals::Vector{String}
end

XMLBuilder() =
    XMLBuilder(Int[], 0, 0, Int[1], Int[], String[], Int[1], Pair{String,String}[], String[], String[])

function addelementstart!(b::XMLBuilder, name::String)
    b.len += 1
    if !isempty(b.stk)
        parent = b.stk[end]
        push!(b.parent_vals, parent)
    end
    push!(b.parent_offs, length(b.parent_vals)+1)
    push!(b.stk, b.len)
    b.last = 0
    push!(b.name_vals, name)
    push!(b.attr_offs, length(b.attr_vals)+1)
    push!(b.itext_vals, "")
    push!(b.otext_vals, "")
    nothing
end

function addelementend!(b::XMLBuilder)
    b.last = pop!(b.stk)
    nothing
end

function addattribute!(b::XMLBuilder, key::String, val::String)
    b.attr_offs[end] += 1
    push!(b.attr_vals, key => val)
    nothing
end

function addtext!(b::XMLBuilder, text::String)
    if b.last == 0
        if isempty(b.itext_vals[end])
            b.itext_vals[end] = text
        else
            b.itext_vals[end] *= text
        end
    else
        if isempty(b.otext_vals[b.last])
            b.otext_vals[b.last] = text
        else
            b.otext_vals[b.last] *= text
        end
    end
end

immutable XMLDocument
    len::Int
    parent::OptionalColumn{Vector{Int},Vector{Int}}
    child::PluralColumn{Vector{Int},Vector{Int}}
    name::PlainColumn{Base.OneTo{Int},Vector{String}}
    attr::PluralColumn{Vector{Int},Vector{Pair{String,String}}}
    itext::PlainColumn{Base.OneTo{Int},Vector{String}}
    otext::PlainColumn{Base.OneTo{Int},Vector{String}}
end

show(io::IO, doc::XMLDocument) =
    show(io, getroot(doc))

immutable XMLElement
    val::Int
    doc::XMLDocument
end

show(io::IO, el::XMLElement) =
    print(io, "<", getname(el), ">")

immutable XMLElementVector{V<:AbstractVector{Int}} <: AbstractVector{XMLElement}
    vals::V
    doc::XMLDocument
end

@inline size(ev::XMLElementVector) = size(ev.vals)
@inline length(ev::XMLElementVector) = length(ev.vals)
@inline getindex(ev::XMLElementVector, i::Int) = XMLElement(ev.vals[i], ev.doc)
@inline getindex(ev::XMLElementVector, idxs::AbstractVector{Int}) =
    XMLElementVector(ev.vals[idxs], ev.doc)
Base.array_eltype_show_how(::XMLElementVector) = (true, "")

getroot(doc::XMLDocument) =
    XMLElement(1, doc)

getname(el::XMLElement) =
    let cr = el.doc.name[el.val]
        cr[1]
    end

function getparent(el::XMLElement)
    cr = el.doc.parent[el.val]
    val =
        if isempty(cr)
            Nullable{XMLElement}()
        else
            Nullable(XMLElement(cr[1], el.doc))
        end
    return val
end

function getchildren(el::XMLElement, self::Bool=false)
    cr = el.doc.child[el.val]
    vals =
        if self
            vals = Int[el.val]
            append!(vals, cr)
            vals
        else
            collect(Int, cr)
        end
    return XMLElementVector(vals, el.doc)
end

function getdescendants(el::XMLElement, self::Bool=false)
    vals = Int[]
    if self
        push!(vals, el.val)
    end
    cr = el.doc.child[el.val]
    stk = Tuple{Int,Int}[]
    push!(stk, (el.val, 1))
    while !isempty(stk)
        val, pos = pop!(stk)
        move!(el.doc.child, cr, val)
        if pos <= length(cr)
            ch = cr[pos]
            push!(vals, ch)
            push!(stk, (val, pos+1))
            push!(stk, (ch, 1))
        end
    end
    return XMLElementVector(vals, el.doc)
end

function gettext(el::XMLElement)
    cr = el.doc.child[el.val]
    itextcr = el.doc.itext[el.val]
    if isempty(cr)
        return itextcr[1]
    end
    otextcr = el.doc.otext[el.val]
    text = String[itextcr[1]]
    stk = Tuple{Int,Int}[]
    push!(stk, (el.val, 1))
    while !isempty(stk)
        val, pos = pop!(stk)
        move!(el.doc.child, cr, val)
        if pos <= length(cr)
            ch = cr[pos]
            push!(stk, (val, pos+1))
            push!(stk, (ch, 1))
            move!(el.doc.itext, itextcr, ch)
            push!(text, itextcr[1])
        elseif !isempty(stk)
            move!(el.doc.otext, otextcr, val)
            push!(text, otextcr[1])
        end
    end
    return join(text)
end

function getattribute(el::XMLElement, attr::String)
    cr = el.doc.attr[el.val]
    for (aname, aval) in cr
        if aname == attr
            return Nullable{String}(aval)
        end
    end
    return Nullable{String}()
end

function build(b::XMLBuilder)
    len = b.len
    parent = OptionalColumn(b.parent_offs, b.parent_vals)
    size = fill(0, len)
    for parent_val in b.parent_vals
        size[parent_val] += 1
    end
    child_offs = Vector{Int}(len+1)
    child_vals = Vector{Int}(sum(size))
    child_offs[1] = 1
    for k = 1:len
        child_offs[k+1] = child_offs[k] + size[k]
    end
    child_idxs = child_offs[1:len]
    for k = 1:endof(b.parent_vals)
        parent_val = b.parent_vals[k]
        child_val = k+1
        child_vals[child_idxs[parent_val]] = child_val
        child_idxs[parent_val] += 1
    end
    child = PluralColumn(child_offs, child_vals)
    name = PlainColumn(b.name_vals)
    attr = PluralColumn(b.attr_offs, b.attr_vals)
    itext = PlainColumn(b.itext_vals)
    otext = PlainColumn(b.otext_vals)
    return XMLDocument(len, parent, child, name, attr, itext, otext)
end

function xmlcharacters(data::Ptr{Void}, str::Ptr{UInt8}, len::Int32)
    b = unsafe_pointer_to_objref(data)
    text = unsafe_string(str, len)
    addtext!(b, text)
    nothing
end

function xmlelementstart(data::Ptr{Void}, name::Ptr{UInt8}, atts::Ptr{Ptr{UInt8}})
    b = unsafe_pointer_to_objref(data)
    name = unsafe_string(name)
    addelementstart!(b, name)
    j = 1
    while unsafe_load(atts, j) != C_NULL && unsafe_load(atts, j+1) != C_NULL
        k = unsafe_string(unsafe_load(atts, j))
        v = unsafe_string(unsafe_load(atts, j+1))
        j += 2
        addattribute!(b, k, v)
    end
end

function xmlelementend(data::Ptr{Void}, name::Ptr{UInt8})
    b = unsafe_pointer_to_objref(data)
    addelementend!(b)
    nothing
end

function parsexml(text::String)
    p = XML_ParserCreateNS(C_NULL, 0x20)
    if p == C_NULL
        throw(OutOfMemoryError())
    end
    bld = XMLBuilder()
    xmlcharacters_c = cfunction(xmlcharacters, Void, (Ptr{Void}, Ptr{UInt8}, Int32))
    xmlelementstart_c = cfunction(xmlelementstart, Void, (Ptr{Void}, Ptr{UInt8}, Ptr{Ptr{UInt8}}))
    xmlelementend_c = cfunction(xmlelementend, Void, (Ptr{Void}, Ptr{UInt8}))
    XML_SetUserData(p, pointer_from_objref(bld))
    XML_SetElementHandler(p, xmlelementstart_c, xmlelementend_c)
    XML_SetCharacterDataHandler(p, xmlcharacters_c)
    status = XML_Parse(p, text, Int32(length(text)), Int32(1))
    if status != 1
        msg = unsafe_string(XML_ErrorString(UInt32(XML_GetErrorCode(p))))
        l = XML_GetCurrentLineNumber(p)
        c = XML_GetCurrentColumnNumber(p)+1
        XML_ParserFree(p)
        error("XML parsing failed at line $l, column $c: $msg")
    end
    XML_ParserFree(p)
    return build(bld)
end

max_stars_repo_name: rbt-lang/rbt-proto

max_issues_repo_name: rbt-lang/rbt-proto

max_forks_repo_name: rbt-lang/rbt-proto


text:
using Ipopt, JuMP
import JuMP.MathOptInterface
const MOI = MathOptInterface

my_square(x) = x^2
my_square_prime(x) = 2x
my_square_prime_prime(x) = 2

function wrapper(g, x)
    ∇my_g(g, x...)
end

#∇my_g(g, [2, 3]...)
function ∇my_g(g, x...)
    args = x
    @info "args", args
    g[1] = args[2]^2
    g[2] = 2*args[1]args[2]
end;
#∇h(g, [1, 2]...)

my_f(x, y) = (x - 1)^2 + (y - 2)^2
function ∇f(g, x, y)
    g[1] = 2 * (x - 1)
    g[2] = 2 * (y - 2)
end;

model = Model(Ipopt.Optimizer)
#m = Model(with_optimizer(Ipopt.Optimizer, print_level=0))
#register(model, :my_f, 2, my_f, ∇f)
register(model, :my_f, 2, my_f, autodiff=true)
register(model, :my_square, 1, my_square, my_square_prime,
         my_square_prime_prime)
@variable(model, 1.2 >= varname[1:2] >= 0.501)
@NLobjective(model, Min, my_f(varname[1], my_square(varname[2])))
@NLconstraint(model, log(varname[1] + varname[2]) == log(2))

#@NLconstraint(model, x[1]*my_square(x[2]) == 0)
JuMP.optimize!(model)
println(value.(varname))

ext: jl

max_stars_repo_name: dbindel/btg

max_issues_repo_name: dbindel/btg

max_forks_repo_name: dbindel/btg


text:
"""
    @io2str ex

Search and replace the placeholder `::IO` with a newly allocated
`Base.IOBuffer` which is afterwards converted to a string and
returned.

This macro allows a user to conveniently test text-based printing
functionality that require an `IO` argument. A particularly
common example of such would be a custom `Base.show` method.

# Examples

```jldoctest
julia> using ReferenceTests

julia> @io2str print(::IO, "Hello World")
"Hello World"

julia> @io2str show(IOContext(::IO, :limit=>true, :displaysize=>(10,10)), "text/plain", ones(10))
"10-element Array{Float64,1}:\\n 1.0\\n 1.0\\n 1.0\\n ⋮  \\n 1.0\\n 1.0"
```
"""
macro io2str(expr)
    io2str_impl(expr)
end

io2str_impl(arg) = :(throw(ArgumentError("Invalid use of `@io2str` macro: The given argument `$($(string(arg)))` is not an expression.")))

function io2str_impl(expr::Expr)
    nvar = gensym("io")
    if replace_expr!(expr, :(::IO), nvar)
        esc(quote
            $nvar = Base.IOBuffer()
            $expr
            Base.String(Base.resize!($nvar.data, $nvar.size))
        end)
    else
        :(throw(ArgumentError("Invalid use of `@io2str` macro: The given expression `$($(string(expr)))` does not contain `::IO` placeholder in a supported manner")))
    end
end

function replace_expr!(expr, pat, r)
    expr == pat && throw(ArgumentError("can't replace root expression"))
    found = false
    if expr isa Expr
        for i = 1:length(expr.args)
            arg = expr.args[i]
            if arg == pat
                expr.args[i] = r
                found = true
            else
                found = found || replace_expr!(arg, pat, r)
            end
        end
    end
    found
end

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

"""
    @withcolor ex

Make sure that `ex` is evaluated while `Base.have_color` is set
to `true`. The original value of `Base.have_color` will be
restored afterwards.

This macro is particularily useful for CI, where it is not
unusual that  `julia` is executed without the `--color=yes`
argument by default.

```julia
@withcolor print_with_color(:green, "foo")
```
"""
macro withcolor(expr)
    :(withcolor(()->$(esc(expr))))
end

function withcolor(fun)
    old_color = Base.have_color
    try
        Core.eval(Base, :(have_color = true))
        fun()
    finally
        Core.eval(Base, :(have_color = $old_color))
    end
end

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

function input_bool(prompt)
    while true
        println(prompt, " [y/n]")
        response = readline()
        length(response) == 0 && continue
        reply = lowercase(first(strip(response)))
        if reply == 'y'
            return true
        elseif reply =='n'
            return false
        end
        # Otherwise loop and repeat the prompt
    end
end


text:
# This file was generated, do not modify it. # hide
best_ensemble = fitted_params(tuned_ensemble).best_model
@show best_ensemble.model.K
@show best_ensemble.bagging_fraction

max_issues_repo_name: alan-turing-institute/MLJTutorials

max_forks_repo_name: alan-turing-institute/MLJTutorials


text:
using Test
using VoronoiGraph

using BenchmarkTools
using LinearAlgebra
using Random
using RecipesBase
using Statistics
using VoronoiCells
using VoronoiCells.GeometryBasics

# nice to have if tests dont pass
seed = rand(UInt)
@show seed
Random.seed!(seed)

# necessary condition for the voronoi diagram to be correct
function test_equidistance(verts, xs)
    allsame(x) = all(y -> y ≈ first(x), x)
    for (sig, v) in verts
        dists = [norm(xs[s] - v) for s in sig]
        relerr = maximum(abs, 1 .- dists ./ dists[1])
        if relerr > 1e-5
            @show dists
            return false
        end
    end
    return true
end

bigdata = [rand(2, 10000), rand(4,1000), rand(6,200), rand(8,100)]
smalldata = [rand(2, 1000), rand(3, 1000), rand(4,100)]

@testset "VoronoiGraph.jl" begin

    @testset "Equidistance" begin
        for data in bigdata
            @time verts, xs = voronoi(data)
            @test test_equidistance(verts, xs)

            vrand, xs = voronoi_random(data, 1000)
            @test test_equidistance(vrand, xs)

            @test issubset(keys(vrand), keys(verts))
        end
    end

    @testset "Raycast" begin
        for data in smalldata
            data = VoronoiGraph.vecvec(data)
            search = VoronoiGraph.RaycastCompare(data)
            @test_nowarn voronoi(data, search)
        end
    end

    @testset "Area computation" begin
        data = rand(2,1000)

        rect = Rectangle(Point2(-1000., -1000.), Point2(1000., 1000))
        tess = voronoicells(data[1,:], data[2,:], rect)
        area = voronoiarea(tess) |> sort

        v, P = VoronoiGraph.voronoi(data)
        _, A = VoronoiGraph.volumes(v, P)

        A = sort(A)

        # the larger areas lie at the boundary and are computed differently
        small = A .< 1
        #println("comparing areas on $(sum(small)) out of $(length(small)) cells")

        @test ≈(A[small], area[small], rtol=1e-8)
    end

    @testset "Monte Carlo Volumes" begin
        x = rand(2,20)
        v, xs = voronoi(x)
        A, V = volumes(v, xs)  # exact reference volumes

        Am, Vm = mc_volumes(xs, 10_000)
        err = std(filter(isfinite, (Am-A)./A))
        @test err < 0.2

        Am, Vm = mc_volumes(v, xs, 10_000)
        err = std(filter(isfinite, (Am-A)./A))
        @test err < 0.2
    end

    @testset "Plot recipe" begin
        verts, xs = voronoi(rand(2,20))
        p = VoronoiGraph.VoronoiPlot((verts, xs))

        # this is broken without `using Plots`, but works with iter
        # however we dont want to require Plots for testing, so accepting this as broken
        @test_broken RecipesBase.apply_recipe(Dict{Symbol, Any}(), p)
    end
end

function benchmark(n=1000)
    data = rand(2,n)

    rect = Rectangle(Point2(-1000., -1000.), Point2(1000., 1000))
    tess = voronoicells(data[1,:], data[2,:], rect)
    @show @benchmark VoronoiCells.voronoicells($data[1,:], $data[2,:], $rect)
    @show @benchmark VoronoiCells.voronoiarea($tess)

    v, P = VoronoiGraph.voronoi(data)
    @show @benchmark VoronoiGraph.voronoi($data)
    @show @benchmark VoronoiGraph.volumes($v, $P)
    return nothing
end


text:
function _precompile_()
    ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
    for n = 1:3, T in (Float32, Float64, ComplexF32, ComplexF64), D in (UnitRange{Int}, Vector{Int}, Int)
        @assert precompile(Tuple{typeof(fft),Array{T,n},D})
        @assert precompile(Tuple{typeof(rfft),Array{T,n},D})
        @assert precompile(Tuple{typeof(ifft),Array{T,n},D})
        @assert precompile(Tuple{typeof(irfft),Array{T,n},Int,D})
        @assert precompile(Tuple{typeof(rfft_output_size),Tuple{Int, Int, Int},D})
        @assert precompile(Tuple{typeof(rfft_output_size),Tuple{Int, Int},D})
        @assert precompile(Tuple{typeof(rfft_output_size),Tuple{Int},D})
    end
end


text:
mutable struct Integrator
    func::Function
    runningsum::Float64
    dt::Float64
    running::Bool
    function Integrator(f::Function, dt::Float64)
        this = new()
        this.func = f
        this.runningsum = 0.0
        this.dt = dt
        this.running = false
        return this
    end
end

function run(integ::Integrator, lastval::Float64 = 0.0)
    lasttime = time()
    while integ.running
        sleep(integ.dt)
        newtime = time()
        measuredinterval = newtime - lasttime
        newval = integ.func(measuredinterval)
        integ.runningsum += (lastval + newval) * measuredinterval / 2.0
        lasttime = newtime
        lastval = newval
    end
end

start!(integ::Integrator) = (integ.running = true; @async run(integ))
stop!(integ) = (integ.running = false)
f1(t) = sin(2π * t)
f2(t) = 0.0

it = Integrator(f1, 0.00001)
start!(it)
sleep(2.0)
it.func = f2
sleep(0.5)
v2 = it.runningsum
println("After 2.5 seconds, integrator value was $v2")

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
module Intervals

using Dates
using Printf
using RecipesBase
using Serialization: Serialization, AbstractSerializer, deserialize
using TimeZones

using Dates: AbstractDateTime, value, coarserperiod

import Base: ⊆, ⊇, ⊈, ⊉, union, union!, merge

abstract type Bound end
abstract type Bounded <: Bound end
struct Closed <: Bounded end
struct Open <: Bounded end
struct Unbounded <: Bound end

bound_type(x::Bool) = x ? Closed : Open

abstract type AbstractInterval{T, L <: Bound, R <: Bound} end

Base.eltype(::AbstractInterval{T}) where {T} = T
Base.broadcastable(x::AbstractInterval) = Ref(x)
bounds_types(x::AbstractInterval{T,L,R}) where {T,L,R} = (L, R)

include("isfinite.jl")
include("endpoint.jl")
include("interval.jl")
include("anchoredinterval.jl")
include("parse.jl")
include("description.jl")
include("plotting.jl")
include("docstrings.jl")
include("deprecated.jl")
include("compat.jl")

export Bound,
       Closed,
       Open,
       Unbounded,
       AbstractInterval,
       Interval,
       AnchoredInterval,
       HourEnding,
       HourBeginning,
       HE,
       HB,
       first,
       last,
       span,
       bounds_types,
       isclosed,
       anchor,
       merge,
       union,
       union!,
       less_than_disjoint,
       greater_than_disjoint,
       isdisjoint,
       superset,
       .., ≪, ≫, ⊆, ⊇, ⊈, ⊉
end


text:
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENCE in the project root.
# ------------------------------------------------------------------

"""
    𝓒(x)

Return closure of `x`.
"""
𝓒(x) = x ./ sum(x)

"""
    Composition(partscomps)
    Composition(parts, comps)
    Composition(part₁=comp₁, part₂=part₂, ...)
    Composition(comps)
    Composition(comp₁, comp₂, ...)

A D-part composition as defined by Aitchison 1986.

## Examples

A 2-part composition with parts `a=0.1` and `b=0.8`:

```julia
julia> Composition(a=0.2, b=0.8)
julia> Composition((a=0.2, b=0.8))
julia> Composition((:a, :b), (0.2, 0.8))
```

When the names of the parts are not specified, the
constructor uses default names `part1`, `part2`,
..., `partD`:

```
julia> Composition(0.1, 0.8)
julia> Composition((0.1, 0.8))
```
"""
struct Composition{D,PARTS}
  data::SVector{D,Union{Float64,Missing}}
end

Composition(data::NamedTuple) =
  Composition{length(data),keys(data)}(values(data))

Composition(; data...) = Composition((; data...))

Composition(parts::NTuple, comps) =
  Composition((; zip(parts, Tuple(comps))...))

Composition(comps) =
  Composition(ntuple(i->Symbol("part$i"), length(comps)), comps)

Composition(comp::Real, comps...) = Composition((comp, comps...))

"""
    parts(c)

Parts in the composition `c`.
"""
parts(::Composition{D,PARTS}) where {D,PARTS} = PARTS

"""
    components(c)

Components in the composition `c`.
"""
components(c::Composition) = getfield(c, :data)

"""
    getproperty(c, part)

Return the value of `part` in the composition `c`.
"""
function getproperty(c::Composition{D,PARTS}, PART::Symbol) where {D,PARTS}
  i = findfirst(isequal(PART), PARTS)
  if isnothing(i)
    throw(ArgumentError("invalid part"))
  else
    getfield(c, :data)[i]
  end
end

+(c₁::Composition{D,PARTS}, c₂::Composition{D,PARTS}) where {D,PARTS} =
  Composition(PARTS, 𝓒(components(c₁) .* components(c₂)))

-(c::Composition) = Composition(parts(c), 𝓒(1 ./ components(c)))

-(c₁::Composition, c₂::Composition) = c₁ + -c₂

*(λ::Real, c::Composition) = Composition(parts(c), 𝓒(components(c).^λ))

==(c₁::Composition, c₂::Composition) =
  parts(c₁) == parts(c₂) && 𝓒(components(c₁)) ≈ 𝓒(components(c₂))

"""
    dot(c₁, c₂)

Inner product between compositions `c₁` and `c₂`.
"""
function dot(c₁::Composition{D}, c₂::Composition{D}) where {D}
  x, y = components(c₁), components(c₂)
  sum(log(x[i]/x[j])*log(y[i]/y[j]) for j=1:D for i=j+1:D) / D
end

"""
    norm(c)

Aitchison norm of composition `c`.
"""
norm(c::Composition) = √dot(c,c)

"""
    distance(c₁, c₂)

Aitchison distance between compositions `c₁` and `c₂`.
"""
distance(c₁::Composition, c₂::Composition) = norm(c₁ - c₂)

# ------------
# IO methods
# ------------
function Base.show(io::IO, c::Composition)
  show(io, join(components(c), ":"))
end

function Base.show(io::IO, mime::MIME"text/plain",
                   c::Composition{D,PARTS}) where {D,PARTS}
  comps = components(c)
  x = Vector{Float64}()
  p = Vector{Symbol}()
  m = Vector{Symbol}()
  for i in 1:D
    if ismissing(comps[i])
      push!(m, PARTS[i])
    else
      push!(p, PARTS[i])
      push!(x, comps[i])
    end
  end
  plt = barplot(p, x, title="$D-part composition")
  isempty(m) || annotate!(plt, :t, "missing: $(join(m,", "))")
  show(io, mime, plt)
end


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

using JSON, Base.Threads, MultiThreadingTools

export DLEnv
type DLEnv # immutable
  dir::AbstractString
  name::AbstractString
  config::Dict
  _gpus::Vector{Int}
  _ext_event_libs::Dict{String,DLData}
  _verbosity::Integer # 0: nothing, 1: only errors, 2: default, 3: all
#constructors
  DLEnv() = DLEnv("config")
  DLEnv(name::AbstractString) = DLEnv(abspath(""), name)

  function DLEnv(dir::AbstractString, name::AbstractString)
    env = new(dir, name, Dict(), Int[], Dict(), 0)
    reload(env; replace=true)
    setup(env)
    return env
  end
end

type ConfigurationException <: Exception end

function Base.reload(env::DLEnv; replace=false)
  f = open(joinpath(env.dir,"$(env.name).json"), "r")
  dicttxt = readstring(f)  # file information to string
  env.config = JSON.parse(dicttxt)  # parse and transform data
  if replace
    env._verbosity = get(env.config, "verbosity", 2)
  end
end

#get keys of subdict
export get_properties
function get_properties(env::DLEnv, name::AbstractString)
  return get(env.config, name, Dict())
end

export set_properties!
function set_properties!(env::DLEnv, name::AbstractString, props::Dict)
  env.config[name] = props
end

export new_properties!
function new_properties!(modifier, env::DLEnv, template_name::AbstractString, new_name::AbstractString)
  d = copy(get_properties(env, template_name))

  modifier(d)
  set_properties!(env, new_name, d)
  return d
end

export resolvepath #concats the path
function resolvepath(env::DLEnv, path::AbstractString...)
  if isabspath(path[1])
    return joinpath(path...)
  else
    return joinpath(env.dir, path...)
  end
end

function Base.joinpath(env::DLEnv, elements::String...)
  return joinpath(env.dir, elements...)
end

#verbosity settings
function Base.info(env::DLEnv, level::Integer, msg::AbstractString)
  if env._verbosity >= level
    threadsafe_info(msg)
    flush(STDOUT)
  end
end

export set_verbosity!
function set_verbosity!(env::DLEnv, verbosity::Integer)
  env._verbosity = verbosity
end

export get_verbosity
function get_verbosity(env::DLEnv)
  return env._verbosity
end

export with_verbosity
function with_verbosity(action, env::DLEnv, verb)
    default_verbosity = env._verbosity
    env._verbosity = verb
    result = action()
    env._verbosity = default_verbosity
    return result
end

export verbose
verbose(action, env::DLEnv) = with_verbosity(action, env, 1000)

export silent
silent(action, env::DLEnv) = with_verbosity(action, env, 0)

#dict indexing with functions
function Base.getindex(env::DLEnv, key::String)
  return env.config[key]
end

function Base.setindex!(env::DLEnv, key::String, value)
  env.config[key] = value
end

function Base.haskey(env::DLEnv, key::String)
  return haskey(env.config, key)
end

#detector types, returns subset of dets
export detectors
function detectors(env::DLEnv, dettype::AbstractString)
  if dettype == "BEGe"
    return BEGes_GERDA_II()
  elseif dettype == "coax" || dettype == "semi-coaxial"
    return Coax_GERDA_II()
  elseif dettype == "natural"
    return Natural_GERDA_II()
  elseif dettype == "used"
    return Used_GERDA_II()
  else
    throw(ArgumentError("Unknown detector type: $dettype"))
  end
end

function detectors(env::DLEnv, keywords::AbstractString...)
    sets = [detectors(env, keyword) for keyword in keywords]
    return intersect(sets...)
end

export detectors
function detectors(env::DLEnv) #extended to select detectors
  dettype = env.config["raw"]["detectors"]
  info(length(dettype))
  if length(dettype) == 0
    	return phase2_detectors
  else
	detectors = string.(filter!(sde->sde ≠ false, [sd in phase2_detectors && sd for sd in dettype]))
  	"BEGe" in dettype&&push!(detectors,BEGes_GERDA_II()...)
  	"coax" in dettype&&push!(detectors,Coax_GERDA_II()...)
  	"natural" in dettype&&push!(detectors,Natural_GERDA_II()...)
  	"used" in dettype&&push!(detectors,Used_GERDA_II()...)
  	if iszero(length(detectors)) throw(ArgumentError("Unknown detector type: $dettype")) else return unique(detectors) end
  end
end

export _create_h5data
function _create_h5data(env::DLEnv, output_dir)
    info(env, 2, "Reading original data from $(env.config["path"])")
    isdir(output_dir) || mkdir(output_dir) #error if target dir nonnexistent and cannot be created
    if haskey(env.config, "rawformat")
        _seg_to_hdf5(env, output_dir)
    else
        _mgdo_to_hdf5(env, output_dir)
    end
    info(env, 3, "Converted raw data to HDF5 and stored in $output_dir.")
end

function _mgdo_to_hdf5(env::DLEnv, output_dir)
  keylists = KeyList[]
  for (i,keylist_path) in enumerate(env.config["keylists"]) #txt that contains data file names
    if !endswith(keylist_path, ".txt") #if you left the txt from the end, it adds it
      keylist_path = keylist_path*".txt"
    end
#extracts whats in the txt (the datafile names) and append content of each .txt to a KeyList object
    push!(keylists, parse_keylist(resolvepath(env, keylist_path), keylist_path))
  end
    load_root(; verbosity=get_verbosity(env))
#gets keylists (name of .txt, actual datafiles in this txt) as input and reads files
    dets = detectors(env)
    mgdo_to_hdf5(env.config["path"], dets, output_dir, keylists; verbosity=get_verbosity(env)) # added raw to select detectors
end

function _seg_to_hdf5(env::DLEnv, output_dir)
    src_dirs = env.config["path"]
    keylists = Vector{AbstractString}[]
    if env.config["rawformat"] == "SegHDF5"
      for src_dir in src_dirs
          content = readdir(src_dir)
          content = joinpath.(src_dir, content[find(f->endswith(f, ".hdf5"), content)])
          push!(keylists, content)
      end
      segh5_to_hdf5(env.config["rawformat"], keylists, output_dir, get_verbosity(env))
    else
      for src_dir in src_dirs
          content = readdir(src_dir)
          content = src_dir .* "/" .* content[find(f->endswith(f, ".root"), content)]
          push!(keylists, content)
      end
      load_root(; verbosity=get_verbosity(env))
      seg_to_hdf5(env.config["rawformat"], keylists, output_dir, get_verbosity(env))
    end
end

#get data
export getdata
"""
data: DLData object that will be preprocessed if given. Useful when raw data is loaded and manipulated and you want it to preprocess without saving the intermediate state.
raw_dir_name: Defines the folder of the raw data. If exists, data can be loaded from it directly. If doesn't exist, it will be created and raw data from the root files will be loaded in there.
"""
function getdata(env::DLEnv; data::Union{DLData,Void}=nothing, raw_dir_name::String="raw", preprocessing::Union{AbstractString,Void}=nothing, targets::Array{String}=String[])
  if preprocessing==nothing
    return get(env, raw_dir_name; targets=targets) do
      _get_raw_data(env, raw_dir_name; targets=targets)
    end
  elseif preprocessing=="processed"
    return get(env, raw_dir_name; h5_libname="_preprocessed_all")
  else
#gets raw data and puts them to folder defined by preprocessing
    if data == nothing
        data = _get_raw_data(env, raw_dir_name; targets=[preprocessing]) #preprocesses data in *raw_dir_name*
    else
        data = deepcopy(data)
    end
    preprocessed = get(env, preprocessing; targets=targets) do #this is the return value
      preprocess(env, data, preprocessing)
    end

    if preprocessed == nothing
      return nothing
    end
    # Else check whether cache is up to date
    steps = get_properties(env, preprocessing)["preprocessing"]
    cache_up_to_date = true
    for lib in preprocessed
      cached_steps = lib[:preprocessing]
      if cached_steps != steps
        cache_up_to_date = false
      end
    end
    if cache_up_to_date
      return preprocessed
    else
      info(env, 2, "Refreshing cache of 'preprocessed'.")
      delete!(env, preprocessing)
      return getdata(env; preprocessing=preprocessing, targets=targets)
    end
  end
end

function _get_raw_data(env::DLEnv, raw_dir_name="raw"; targets::Array{String}=String[])
#dir to put data
  raw_dir = resolvepath(env, "data", raw_dir_name)
  if !isdir(raw_dir)
#get the data from the txts
    _create_h5data(env, raw_dir)
  end
  return lazy_read_all(raw_dir)
end

export preprocess
function preprocess(env::DLEnv, data::DLData, config_name) #config name is "pulses" or "noise"
"""
Selects detectors from the detectors key of the dict. Also selects preprocessing configuration settings.
Applies builtin filters: test pulses, baseline events and nonphysical events.
Then makes an empty data object for the events, fills in and saves it to a file.
Then gets the preprocessing steps from the config file.
Then loops over the selected detectors and applies the preprocessing steps.
After the preprocessing it filters the events that are failed during pp.
Then splits the data of the detector into the datasets specified in the config file and writes them into the result file.
"""
  config = env.config[config_name]
  @assert isa(config, Dict)
  select_channels = parse_detectors(config["detectors"])
  #if select_channels[1] in ["BEGe", "coax"]
#	select_channels = detectors(env, select_channels[1])
 # end
  info(env, 3, "Selected channels: $select_channels")
  if isa(select_channels,Vector) && length(select_channels) > 0
    filter!(data, :detector_name, select_channels)
  end

  N_dset = length(parse_datasets(env, config["sets"]))  # sets are train val and test with ratios
  result = DLData(fill(EventLibrary(zeros(0,0)), length(data)*N_dset))
  result.dir = _cachedir(env, "tmp-preprocessed-$config_name")
  steps = convert(Array{String}, config["preprocessing"])

  for i in 1:length(data)
    lib = data.entries[i]
    _builtin_filter(env, config, "test-pulses", lib, :isTP, isTP -> isTP == 0)
    info(env,3,"filtering non-test pulses: $(eventcount(lib)) events left")
    _builtin_filter(env, config, "baseline-events", lib, :isBL, isBL -> isBL == 0)
    info(env,3,"filtering non-baseline events: $(eventcount(lib)) events left")
    _builtin_filter(env, config, "unphysical-events", lib, :E, E -> (E > 0) && (E < 9999))
    info(env,3,"filtering unphysical events: $(eventcount(lib)) events left")
    _builtin_filter(env, config, "high-multiplicity-events", lib, :multiplicity, multiplicity -> multiplicity == 1)
    info(env,3,"filtering single multiplicity events: $(eventcount(lib)) events left")

    _builtin_filter(env, config, "muon-vetoed-events", lib, :isMuVetoed, isMuVetoed -> isMuVetoed == 0)
    info(env,3,"filtering muon vetoed events: $(eventcount(lib)) events left")

    _builtin_filter(env, config, "pileup-events", lib, :isPileup, isPileup -> isPileup == 0)
    info(env,3,"filtering non-pileup events: $(eventcount(lib)) events left")
    lib_t = preprocess_transform(env, lib, steps; copyf=identity)  # do actual prerpocessing
    #_builtin_filter(env, config, "failed-preprocessing", lib_t, :FailedPreprocessing, fail -> fail == 0)
    #info(env,3,"filtering failed-pp events: $(eventcount(data)) events left")
    part_data = DLData(collect(values(split(env, lib_t, config["sets"]))))
    write_all_sequentially(part_data, result.dir, true)  # write it into file
    info(env,3, "Wrote datasets of $(lib[:name]) and released allocated memory.")
    dispose(lib)
    dispose(lib_t)
    for j in 1:length(part_data)  # usually train, xval, test, so 3
      result.entries[N_dset*(i-1) + j] = part_data.entries[j]  # 1,2,3; 4,5,6: ... puts everything in results dict like train,val,test,train,val,test,...
    end
    @assert length(lib.waveforms) == 0 && length(lib_t.waveforms) == 0
  end

  return result # dict of split datasets
end

function _builtin_filter(env::DLEnv, config::Dict, ftype_key::String, data::EventCollection, label, exclude_prededicate)
  # TODO per detector to save memory
  ftype = config[ftype_key]
  if ftype == "exclude"
    result = filter!(data, label, exclude_prededicate)
    return result
  elseif ftype == "include"
    return data
  elseif ftype == "only"
    return filter!(data, label, x -> !exclude_prededicate(x))
  else
    throw(ArgumentError("Unknown filter keyword in configuration $ftype_key: $ftype"))
  end
end

#reads datasets and ratios from env (config)
export parse_datasets
function parse_datasets(env::DLEnv, strdict::Dict)
#empty dict
  result = Dict{AbstractString,Vector{AbstractFloat}}()
  if haskey(env.config, "keylists") # keylists contains data file txt names
    requiredlength = length(env["keylists"]) # as many files I read
  else
    requiredlength = 1
  end
  for (key,value) in strdict #loop over train, val and test

    if isa(value, Vector) # I can specify datasets per file, different for each but then the length of the array must match as many files I have
      @assert length(value) == requiredlength
      result[key] = value
    elseif isa(value, Real) #same ratios for all files
      result[key] = fill(value, requiredlength) # creates requiredlength long array with all values being value
    else
      throw(ConfigurationException())
    end
  end
#result contains datasets train, xval and test with their ratios
  return result
end

Base.split(env::DLEnv, data::EventCollection, strdict::Dict) = split(data, parse_datasets(env, strdict))

function setup(env::DLEnv)
  e_mkdir(env.dir)
  e_mkdir(joinpath(env.dir, "data"))
  e_mkdir(joinpath(env.dir, "models"))
  e_mkdir(joinpath(env.dir, "plots"))
end
e_mkdir(dir) = !isdir(dir) && mkdir(dir)

function Base.get(compute, env::DLEnv, lib_name::String; targets::Array{String}=String[], uninitialize=true, h5_libname::String="")
  if !isempty(targets) && containsall(env, targets)
    info(env, 2, "Skipping retrieval of '$lib_name'.")
    return nothing
  end
  if contains(env, lib_name)
    info(env, 2, "Retrieving '$lib_name' from cache.")
    return get(env, lib_name; h5_libname=h5_libname)
  else
    info(env, 2, "Computing '$lib_name'...")
    data = compute()
    info(env, 3, "Computation of '$lib_name' finished.")

    # check type
    if !isa(data, DLData)
      throw(TypeError(Symbol(compute), "get_or_compute", DLData, data))
    end

    if data.dir == nothing
      info(env, 3, "Writing computed data to env as '$lib_name' (uninitialize=$uninitialize).")
      push!(env, lib_name, data; uninitialize=uninitialize)
      return data
    else
      # Rename directory
      targetdir = _cachedir(env, lib_name)
          if targetdir != data.dir
              info(env, 3, "Moving data from $(data.dir) to $targetdir and reloading.")
              mv(data.dir, targetdir)
              return get(env, lib_name)
            else
                info(env, 3, "Data is already in the right location ($(data.dir)).")
                return data
            end
    end
  end
end

function Base.get(env::DLEnv, lib_name::String; h5_libname="")
  _ensure_ext_loaded(env, lib_name; h5_libname=h5_libname)
  return env._ext_event_libs[lib_name]
end

Base.push!(env::DLEnv, lib_name::String, lib::EventLibrary; uninitialize::Bool=false) = push!(env, lib_name, DLData([lib]); uninitialize=uninitialize)

function Base.push!(env::DLEnv, lib_name::String, data::DLData; uninitialize::Bool=false)
  _ensure_ext_loaded(env, lib_name)
  env._ext_event_libs[lib_name] = data
  if env.config["cache"]
    write_all_multithreaded(data, joinpath(env.dir, "data", lib_name), uninitialize)
  end
end

function Base.contains(env::DLEnv, lib_name::String)
  if haskey(env._ext_event_libs, lib_name)
    return true
  end
  return env.config["cache"] && isdir(_cachedir(env, lib_name))
end

export containsall
function containsall(env::DLEnv, lib_names::Array{String})
  for lib_name in lib_names
    if !contains(env, lib_name) return false end
  end
  return true
end

function _ensure_ext_loaded(env::DLEnv, lib_name::String; h5_libname::String="")
  if !haskey(env._ext_event_libs, lib_name)
    c_dir = _cachedir(env, lib_name)
    if isdir(c_dir)
      env._ext_event_libs[lib_name] = lazy_read_all(c_dir; h5_libname=h5_libname)

    end
  end
end

function Base.delete!(env::DLEnv, lib_name::String)
  if haskey(env._ext_event_libs, lib_name)
    delete!(env._ext_event_libs, lib_name)
  end
  # Delete cached files (even if cache is set to false)
  c_dir = _cachedir(env, lib_name)
  if isdir(c_dir)
    rm(c_dir; recursive=true)
    info(env, 2, "Deleted cached $lib_name")
  end
end

#define directory of env, if nonexistent, then creates the dir
export _cachedir
function _cachedir(env::DLEnv, lib_name::String)
  return joinpath(env.dir, "data", lib_name)
end

export network
function network(env::DLEnv, name::String)
    dir = joinpath(env.dir, "models", name)
    isdir(dir) || mkdir(dir)
    return NetworkInfo(name, dir, get_properties(env, name), to_context(env._gpus))
end

export use_gpus
function use_gpus(env, gpus::Int...)
  env._gpus = [gpus...]
end


text:
using Test
using DFTK: PlaneWaveBasis, G_to_r!, r_to_G!, G_to_r, r_to_G

include("testcases.jl")

@testset "FFT and IFFT are an identity" begin
    Ecut = 4.0  # Hartree
    fft_size = [8, 8, 8]
    model = Model(silicon.lattice, n_electrons=silicon.n_electrons)
    pw = PlaneWaveBasis(model, Ecut, silicon.kcoords, silicon.ksymops; fft_size=fft_size)

    @testset "Transformation on the cubic basis set" begin
        f_G = Array{ComplexF64}(randn(Float64, pw.fft_size...))

        f_R = Array{ComplexF64}(undef, pw.fft_size...)
        G_to_r!(f_R, pw, f_G)

        f2_G = r_to_G(pw, f_R)
        f2_R = G_to_r(pw, f2_G; assume_real=false)
        f3_G = r_to_G!(similar(f_R), pw, f_R)

        @test maximum(abs.(f2_G - f_G)) < 1e-12
        @test maximum(abs.(f2_R - f_R)) < 1e-12
        @test maximum(abs.(f3_G - f_G)) < 1e-12

        G_to_r_mat = DFTK.G_to_r_matrix(pw)
        r_to_G_mat = DFTK.r_to_G_matrix(pw)
        @test maximum(abs.(G_to_r_mat * r_to_G_mat - I)) < 1e-12
        @test maximum(abs.(G_to_r_mat * vec(f_G) - vec(f_R))) < 1e-12
        @test maximum(abs.(r_to_G_mat * vec(f_R) - vec(f_G))) < 1e-12
    end

    @testset "Transformation on the spherical basis set" begin
        kpt = pw.kpoints[2]
        f_G = Array{ComplexF64}(randn(Float64, length(G_vectors(kpt))))

        f_R = Array{ComplexF64}(undef, pw.fft_size...)
        G_to_r!(f_R, pw, kpt, f_G)

        f2_G = similar(f_G)
        r_to_G!(f2_G, pw, kpt, copy(f_R))  # copy needed, because r_to_G! destructive

        f2_R = similar(f_R)
        G_to_r!(f2_R, pw, kpt, f2_G)

        @test maximum(abs.(f2_G - f_G)) < 1e-12
        @test maximum(abs.(f2_R - f_R)) < 1e-12
    end
end


text:
function fix_lang()
  return "This file is here only to tag the repository language. Do not delete, please!"
end


text:
using Test, UdpTest

@testset "Test 1" begin
#    @test "IPV4 127.0.0.1 : 1883 ==> uno" == show_info("uno", ["127.0.0.1","1883"],"IPV4")
    @test  0 == show_info("uno", ["127.0.0.1","1883"],"IPV4")
    @test_throws DomainError (-1)^0.5
end

@testset "Test 2" begin
#    @test "IPV4 127.0.0.1 : 1883 ==> uno" == show_info("uno", ["127.0.0.1","1883"],"IPV4")
    #@test  0 == server(5050)
    @test_throws MethodError server()
end

@testset "Test 3" begin
    @test_throws MethodError client()
end


text:
abstract type AbstractPotential end

"""
    AbstractClusterPotential <: AbstractPotential

Potentials between groups of atoms
"""
abstract type AbstractClusterPotential <: AbstractPotential end

"""
    AbstractPairPotential  <: AbstractPotential

Potential that depends only on distance between two atoms
"""
abstract type AbstractPairPotential  <: AbstractPotential end

abstract type AbstractPotentialTopology end

abstract type AbstractPairPotentialTopology{T<:AbstractPairPotential} <: AbstractPotentialTopology end

abstract type AbstractClusterPotentialTopology{T<:AbstractClusterPotential} <: AbstractPotentialTopology end

"""
PairPotentialTopology{T} <: AbstractPairPotentialTopology{T}

Container that maps a pair potential to respective atoms.

# Fields
- `indices::Vector{Tuple{Int, Int}}`: Stores information of atom pair(s) between which the potentilal exist
- `potential::T`: Potential structure
"""
struct PairPotentialTopology{T} <: AbstractPairPotentialTopology{T}
    potential::T
    indices::Vector{Tuple{Int, Int}}
    PairPotentialTopology(p::T, i, j) where T<:AbstractPairPotential = new{T}(p, [(i,j)])
    PairPotentialTopology(p::T, indices::AbstractVector{Tuple{Int, Int}} ) where T<:AbstractPairPotential = new{T}(p, indices )
end

"""
(p::PairPotentialTopology)(r)

Returns potential for given distance.
"""
(p::PairPotentialTopology)(r) = p.potential(r)

"""
(p::PairPotentialTopology)(c::AbstractCluster)

Returns potential for given cluster.
"""
function (p::PairPotentialTopology)(c::AbstractCluster)
    sum( [ p(distances(c, i, j)) for (i,j) in p.indices ] )
end

"""
(p::PairPotentialTopology)(c1::AbstractCluster, c2::AbstractCluster)

Returns potential for given cluster pair. Index for atom 2 is expected to be in cluster `c2`.
"""
function (p::PairPotentialTopology)(c1::AbstractCluster, c2::AbstractCluster)
    sum([p(distances(c1, j, c2, j)) for (i,j) in p.indeces ])
end

"""
ClusterPotentialTopology{T} <: AbstractClusterPotentialTopology{T}

Container that maps a cluster potential to respective atoms.

# Fields
- `potential::T`:
- `indices::Vector{Vector{Int}}`: Stores information of atoms between which the potentilal exist
"""
struct ClusterPotentialTopology{T} <: AbstractClusterPotentialTopology{T}
    potential::T
    indices::Vector{Vector{Int}}
    function ClusterPotentialTopology(p::T,i...) where T<:AbstractClusterPotential
        new{T}(p, [collect(i)] )
    end
    function ClusterPotentialTopology(p::T,i::AbstractMatrix) where T<:AbstractClusterPotential
        new{T}(p,[x for x in eachcol(i)])
    end
end

function (p::ClusterPotentialTopology)(c::AbstractCluster)
    sum(calculate_potential(p.potential, c, i) for i in p.indices)
end

"""
calculate_potential(potential::AbstractPotential, cluster::AbstractCluster, indices)

Calculates energy from given potential.
**Needs to be implemented for each potential.**

# Arguments
- `potential::AbstractPairPotential`  : potential
- `cluster::AbstractCluster`          : molecule cluster
- `indices`                           : indices for atoms that represent potential
"""
function calculate_potential(potential::AbstractPairPotential, cluster::AbstractCluster, indices)
    r = distances(cluster, indices[1], indices[2])
    return potential(r)
end

"""
    get_potential!(potential, terms)

Function that sets potential term after potential has been fitted.
That is this function transforms fitted term to potential terms.
**Needs to be implemented for each potential.**

# Arguments
- `potential` : used potential
- `terms`     : terms from fitting method
"""
function get_potential!(potential::AbstractPotential, terms)
    error("get_potential! not implemented for $(typof(potential))")
end

"""
    MoleculePairPotential{T<:AbstractPairPotential} <: AbstractClusterPotential

Structure to hold potential information between two molecules
"""
mutable struct MoleculePairPotential
    "Molecule 1"
    mol1::MoleculeIdenticalInformation{AtomOnlySymbol}
    "Molecule 2"
    mol2::MoleculeIdenticalInformation{AtomOnlySymbol}
    "Holds potential (index 1) and indexes for atoms (index 2)"
    topology::Vector{AbstractPotentialTopology}
    function MoleculePairPotential(mol1::AbstractMolecule,mol2::AbstractMolecule, potential::AbstractPotential)
        topology = Vector{PairPotentialTopology}()
        l = length(mol1)
        for i1 in mol1.identical.identical
            for i2 in mol2.identical.identical
                push!(topology, PairPotentialTopology(deepcopy(potential),[(i,l+j) for i in i1 for j in i2 ]) )
            end
        end
        @debug "mol1" mol1.identical.identical
        new(mol1, mol2, topology )
    end
    function MoleculePairPotential(mol1::AbstractMolecule,mol2::AbstractMolecule)
        new(mol1, mol2, Vector{AbstractPotentialTopology}())
    end
end

(mpp::MoleculePairPotential)(c::AbstractCluster) = sum( [t(c) for t in mpp.topology] )
(mpp::MoleculePairPotential)(c1::AbstractCluster, c2::AbstractCluster) = mpp(c1+c2)

function Base.show(io::IO, mpp::MoleculePairPotential; energy_unit="cm^-1")
    println(io, "Molecule Pair Potential")
    println(io, "Molecule 1 has $(length(mpp.mol1)) atoms ($(length(mpp.mol1.identical.identical)) unique) ")
    println(io, "Molecule 2 has $(length(mpp.mol2)) atoms ($(length(mpp.mol2.identical.identical)) unique) \n")
    println(io, "Potential topology:\n")
    m=MoleculeIdenticalInformation{AtomOnlySymbol}(vcat(mpp.mol1.atoms,mpp.mol2.atoms))
    for x in mpp.topology
        for y in x.indices
            show(io, x.potential, energy_unit=energy_unit)
            for z in y
                print(io, " :  ", z, m.atoms[z].id)
            end
            print(io,"\n")
        end
    end
end

"""
potential_variables(mpp::MoleculePairPotential, points)

Used to transform cluster data to fittable form.

# Arguments
- `mpp::MoleculePairPotential` : potential to be fitted
- `points` : array for clusters

# Returns
Array holding an array for each potential term in `mpp.topology` that can the be used for fitting.
"""
function potential_variables(mpp::MoleculePairPotential, points)
    out =[]
    for x in mpp.topology
        tmp=[]
        for y in x.indices
            push!(tmp,vcat(map(z->  potential_variables(x.potential, z, y), points)...) )
        end
        push!(out,+(tmp...))
    end
    return out
end


text:
# The returned result from a affine discrepancy

type AffineDiscrepancyResult
    # n x p matrix of distinct sample points
    points::Array{Float64}
    # n x 1 vector of weights associated with each sample
    weights::Array{Float64}
    # Defines Lp norm in Lipschitz constraints:
    # |g(x)-g(y)| <= c_1 ||x-y||_{xnorm}
    xnorm::Float64
    # Pairs of sample points over which smoothness constraints enforced
    edges::Array{Int64,2}
    # 1 x p vector of optimal objective values for each optimization problem
    objectivevalue::Array{Float64}
    # n x p matrix of optimal g function values from each optimization problem
    # at each sample point
    g::Array{Float64}
    # n x p x p tensor of optimal grad g values from each optimization problem
    # at each sample point
    gradg::Array{Float64}
    # n x p matrix of Langevin operator applied to optimal g,
    # (T_Pg)(x) = <grad, g(x)> + <g(x),grad log p(x)>, evaluated at each
    # sample point
    operatorg::Array{Float64}
    # Time taken computing spanner edges
    edgetime::Float64
    # Time taken to set up and solve each optimization program
    solvetime::Array{Float64}
    # The flavor of operator used to construct the objective
    operator::AbstractString
    # Internal constructor assigns default value to xnorm
    AffineDiscrepancyResult(points,weights,edges,objectivevalue,g,gradg,
                              operatorg,edgetime,solvetime,operator) = (
        # Assign default value to xnorm
        xnorm = 1;
        new(points,weights,xnorm,edges,objectivevalue,g,gradg,
            operatorg,edgetime,solvetime,operator)
    )
end


text:
arnestr = "arne" ^ 1000
shortarnestr = "arne" ^ 100
bsofstr = "bsof" ^ 1000

@test ncd(shortarnestr, arnestr) < ncd(shortarnestr, bsofstr)


text:
    function eigensystem(A,x,radius,origin,Z2randomize!,randomize!;L0=10,Nq=64,M=10,κ=2,δ=1e-14,MaxCGstep=3000,epsCG=1e-16,α=1)
        println("Estimating number of eigenvalues...")
        println("------------------------------------------------------------")
        xtype = typeof(x)
        V = Vector{xtype}(undef,L0)
        for i=1:L0
            V[i] = zero(x)
            Z2randomize!(V[i])
        end

        Sk = Vector{xtype}(undef,L0*M)
        for i=1:L0*M
            Sk[i] = zero(x)
        end

        Y = Vector{xtype}(undef,Nq)
        for j=1:Nq
            Y[j] = zero(x)
        end

        evaluate_Eq22!(Sk,Y,V,A,x,radius,origin,α,L0,Nq,M,MaxCGstep,epsCG)

        S0 = Vector{xtype}(undef,L0)
        for j=1:L0
            S0[j] = zero(x)
            axpy!(1,Sk[j],S0[j])
        end

        mst = evaluate_Eq26(S0,V,L0)

        println("done.")
        println("------------------------------------------------------------")
        println("Estimated number of eigenvalues: ",mst)
        L = Int64(round(κ*mst/M,RoundNearestTiesAway))
        println("L is ",L)

        println("Calculating eigenvalues and eigenvectors...")
        println("------------------------------------------------------------")

        V = Vector{xtype}(undef,L)
        for i=1:L
            V[i] = zero(x)
            randomize!(V[i])
        end

    end

    function evaluate_Eq22!(Sk,Y,V,A,x,radius,origin,α,L,Nq,M,MaxCGstep,epsCG)
        vec_z = zeros(ComplexF64,Nq)
        vec_w = zeros(ComplexF64,Nq)
        for j in 1:Nq
            θj = (2π/Nq)*(j-1/2)
            vec_z[j] = origin + radius*(cos(θj)+im*α*sin(θj))
            vec_w[j] = α*cos(θj)+im*sin(θj)
        end

        for i in 1:L
            println("i = ",i,"/",L)
            #(zj I - H) Yj = V
            #(-zj I + H) (-Yj) = V
            Y = shiftedbicg_2003(-vec_z,A,V[i],maxsteps = MaxCGstep,eps=epsCG,verboselevel = 2)

            for k in 0:M-1
                ik = k*L+i
                for j in 1:Nq
                    coeff = radius*vec_w[j]*vec_z[j]^k/Nq
                    axpy!(-coeff,Y[j],Sk[ik])
                    #vec_Sk[:,ik] += ρ*vec_w[j]*vec_z[j]^k*vec_y[:,j]/Nq
                end
            end

        end
    end

    function evaluate_Eq26(S0,V,L0)
        m = 0.0im
        for i in 1:L0
            m += dot(V[i],S0[i])
            #m += hatV[:,i]'*vec_s0[:,i]
        end
        ms = Int64(round(real(m)/L0))
        if ms < 0
            ms = 1
        end
        return ms
    end

function eigensystem_old(A,x,N,ρ,γ;α=0.1,L0=10,Nq=64,M=10,κ=2,δ=1e-14,numr = 0,cg=true,MaxCGstep=3000,epsCG=1e-16)
        println("Estimating number of eigenvalues...")
        println("------------------------------------------------------------")

        V = Array{typeof(x),1}(undef,L0)
        for i=1:L0
            temp = rand([-1,1],N)
            V[i] = similar(x)
            for j=1:N
                V[i][j] = temp[j]
            end
            #=
            if typeof(x) <: Abstractfermion
                V[i] = similar(x)
                Z2_distribution_fermion!(V[i])
            else
                V[i] = rand([-1,1],N)
            end
            =#
        end

        Sk = Array{typeof(x),1}(undef,L0*M)
        for i=1:L0*M
            Sk[i] = similar(x)
        end

        Y = Array{typeof(x),1}(undef,Nq)
        for j=1:Nq
            Y[j] = similar(x)
        end

        #V = rand([-1,1],N,L0)
        calculate_Eq22!(Sk,Y,V,A,x,N,ρ,γ,α,L0,Nq,M,cg,MaxCGstep,epsCG)

        S0 = Array{typeof(x),1}(undef,L0)
        for i=1:L0
            S0[i] = similar(x)
            if typeof(x) <: Abstractfermion
                add!(0,S0[i],1,Sk[i]) #S0[i] = S[i]
            else
                S0[i] .= Sk[i]
            end
        end

        mst = calc_msinEq26(S0,V,L0)

        println("done.")
        println("------------------------------------------------------------")
        println("Estimated number of eigenvalues: ",mst)
        L = Int64(round(κ*mst/M,RoundNearestTiesAway))
        println("L is ",L)

        println("Calculating eigenvalues and eigenvectors...")
        println("------------------------------------------------------------")

        V = Array{typeof(x),1}(undef,L)
        for i=1:L
            temp = rand(N).*2 .- 1
            V[i] = similar(x)
            for j=1:N
                V[i][j] = temp[j]
            end

            #=
            if typeof(x) <: Abstractfermion
                V[i] = similar(x)
                uniform_distribution_fermion!(V[i])
            else
                V[i] = rand(N).*2 .- 1
            end
            =#

        end

        Sk = Array{typeof(x),1}(undef,L*M)
        for i=1:L*M
            Sk[i] = similar(x)
        end

        ε,vec_x,ms = calc_SS(Sk,Y,V,A,x,N,ρ,γ,α,L,Nq,M,δ,cg,MaxCGstep,epsCG)
        println(ε)
        Ax = similar(x)
        r = similar(x)

        is = 0
        eigenvalues = ComplexF64[]
        residuals = ComplexF64[]
        eigenvectors = []

        for i in 1:ms
            mul!(Ax,A,vec_x[i])
            add!(0,r,-ε[i],vec_x[i])
            add!(r,1,Ax)

            resiup = sqrt(real(dot(r,r)))
            println(resiup)

            #if resiup < 0.1 # && γ-ρ <= ε[i] <= ρ+γ
                is += 1
                push!(eigenvalues,ε[i])
            #    push!(residuals,resi)
                push!(eigenvectors,vec_x[i])
            #end

            #=
            residown1 = sqrt(dot(Ax,Ax))
            residown2 = sqrt(dot(vec_x[i],vec_x[i]))
            resi = real(resiup/(residown1+abs(ε[i])*residown2))
            if resi < 0.1 # && γ-ρ <= ε[i] <= ρ+γ
                is += 1
                push!(eigenvalues,ε[i])
                push!(residuals,resi)
                push!(eigenvectors,vec_x[i])
            end
            =#

        end

        #println("-----------------------------------------")
        println("done.")
        println("------------------------------------------------------------")
        return eigenvalues,residuals,eigenvectors,is

    end

    function calc_msinEq26(S0,V,L0)
        m = 0.0im
        for i in 1:L0
            m += dot(V[i],S0[i])
        end
        #m = real(m)/L0
        #println(m)
        ms = Int64(round(real(m)/L0))
        if ms < 0
            ms = 1
        end

        return ms
    end

    function calc_SS(Sk,Y,mV,A,x,N,ρ,γ,α,L,Nq,M,δ,cg,MaxCGstep,epsCG)
        calculate_Eq22!(Sk,Y,mV,A,x,N,ρ,γ,α,L,Nq,M,cg,MaxCGstep,epsCG)

        Skmatrix = zeros(ComplexF64,N,L*M)
        if typeof(x) <: Abstractfermion
            fermions2vectors!(Skmatrix,Sk)
        else
            for i=1:L*M
                for j=1:N
                    Skmatrix[j,i] = Sk[i][j]
                end
            end
        end

        (U,Σ,W ) = svd(Skmatrix)
        j = 0
        ratio = 1
        while ratio > δ
            j += 1
            ratio = Σ[j]/Σ[1]
            if j == length(Σ)
                ratio = 0.0
            end
        end

        ms = j
        Q = Array{typeof(x),1}(undef,ms)
        for i=1:ms
            Q[i] = similar(x)
            for j=1:N
                Q[i][j]=U[j,i]
            end
            if typeof(x) <: Abstractfermion
                set_wing_fermion!(Q[i])
            end
        end

        temp = Array{typeof(x),1}(undef,ms)
        for i=1:ms
            temp[i] = similar(x)
            mul!(temp[i],A,Q[i])
        end

        mat_Ht =zeros(ComplexF64,ms,ms)
        for i=1:ms
            for j=1:ms
                mat_Ht[j,i] = dot(Q[j],temp[i])
            end
        end

        #mat_Ht = (mat_Ht'+mat_Ht)/2

        ε,vec_w = eigen(mat_Ht)
        vec_x = Array{typeof(x),1}(undef,ms)
        for i=1:ms
            vec_x[i] = similar(x)
            if typeof(x) <: Abstractfermion
            else
                vec_x[i] .= 0
            end

            for j=1:N
                for k=1:ms
                    vec_x[i][j] += Q[k][j]*vec_w[k,i]
                end
            end

            if typeof(x) <: Abstractfermion
                set_wing_fermion!(vec_x[i])
            end
        end
        #vec_x = mat_Q*vec_w

        return ε,vec_x,ms
    end

    function calculate_Eq22!(Sk,Y,mV,A,x,N,ρ,γ,α,L,Nq,M,cg,MaxCGstep,epsCG) #mV = -V
        if typeof(x) <: Abstractfermion
            for i in 1:L
                for k = 0:M-1
                    ik = k*L+i
                    clear_fermion!(Sk[ik])
                end
            end
        else
            for i in 1:L
                for k = 0:M-1
                    ik = k*L+i
                    Sk[ik] .= 0
                end
            end
        end

        z = zeros(ComplexF64,Nq)
        w = zero(z)

        for j = 1:Nq
            θ = (2π/Nq)*(j-1/2)
            z[j] = γ + ρ*(cos(θ)+im*α*sin(θ))
            w[j] = α*cos(θ) + im*sin(θ)
        end

        #(zI - A) Y = V
        #(A - z I) (-Y) = V
        for i in 1:L
            #shiftedcg(Y,-z,x,A,mV[i])
            Y = shiftedbicg_2003(-z,A,mV[i],maxsteps = MaxCGstep,eps=epsCG,verboselevel = 1)
            #shiftedbicg(Y,-z,x,A,mV[i],maxsteps = MaxCGstep,eps=epsCG)
            for k = 0:M-1
                ik = k*L+i
                for j = 1:Nq
                    coef = ρ*w[j]*z[j]^k/Nq
                    add!(Sk[ik],-coef,Y[j])
                end
            end
        end

    end


text:
# Add factory methods
exceeds_limit(fex::nGraph.FluxExecutable, I::ILPHolder, local_args) =
    exceeds_limit(fex.ex.ngraph_function, I)

function exceeds_limit(f::nGraph.NFunction, I::ILPHolder, local_args)
    I.defrag || return false

    # Get the allocated bytes, including local arguments
    io_bytes = isempty(local_args) ? 0 : sum(sizeof, local_args)
    @info "IO Bytes: " io_bytes
    @info "Lenght Local Args: " length(local_args)
    @info "Temporary Pool Size: " convert(Int, nGraph.get_temporary_pool_size(f))

    alloc_bytes = io_bytes + nGraph.get_temporary_pool_size(f)

    # Convert to MB and compare to the maximum limit
    return (alloc_bytes / 1E6) > maxlimit(I)
end

# The general idea is that heap fragmentation causes the actual allocated amount to
# exceed the limit.
#
# To deal with this, we take the ALL instances where the memory limit is exceeded due
# to fragmentation and reduce the DRAM limit for the nodes around this instance.
#
# This should cause the ngraph allocator to free up some space so we don't go over the
# limit.
offenders(frame::Frame) = offenders(frame.modeltype, frame.profile_data)
function offenders(I::ILPHolder, data::FunctionData, io_bytes)
    dram_limits = I.dram_limits
    ml = maxlimit(I)
    @show ml

    # Go through all of the live tensors - find the first that exceeds the limit
    offending_tensors = XTensor{XNode}[]
    offending_nodes = XNode[]
    worst = 0
    for (index, live) in enumerate(live_tensors(data))
        # Find the DRAM tensors
        dram_tensors = filter(!is_persistent, live)
        isempty(dram_tensors) && continue

        # Find all out of bounds tensors
        for tensor in dram_tensors
            sz = (io_bytes + nGraph.get_pool_offset(unx(tensor)) + sizeof(tensor)) / 1E6
            if sz > ml
                push!(offending_tensors, tensor)
                worst = max(worst, sz)
            end
        end

        # Check if we have a workspace and get the offset of the workspace
        node = nodes(data)[index]
        offset = nGraph.Lib.get_workspace_tensor_offset(nGraph.getpointer(unx(node)))
        tensor_size = nGraph.Lib.get_workspace_tensor_size(nGraph.getpointer(unx(node)))
        sz = (io_bytes + offset + tensor_size) / 1E6
        if sz > ml
            push!(offending_nodes, node)
            worst = max(worst, sz)
        end
    end

    return offending_tensors, offending_nodes, worst
end

function update(I::T, local_args, data::FunctionData) where {T <: ILPHolder}
    dram_limits = I.dram_limits
    io_bytes = isempty(local_args) ? 0 : sum(sizeof, local_args)
    ml = maxlimit(I)

    offending_tensors, offending_nodes, worst = offenders(I, data, io_bytes)

    @info "Allocation size: " worst

    decrease_amount = max(
        # Decrease by 10%
        0.90,
        # If the overuse is small, just decrease by a tiny amount
        1 - ((worst / ml) - 1) / 2,
    )

    # Keep track of the indices that need their limits lowered
    indices = Int[]
    for node in offending_nodes
        push!(indices, I.node_to_limit_index[nGraph.name(node)])
    end

    for tensor in offending_tensors
        for node in users(tensor)
            (ismove(unx(node)) || !hasprofile(node)) && continue
            push!(indices, I.node_to_limit_index[nGraph.name(node)])
        end
    end

    # Expand indices around the radius
    radius = 10
    indices = Iterators.flatten([(idx - radius):(idx + radius) for idx in unique(indices)]) |>
        collect |>
        unique
    for idx in indices
        if checkbounds(Bool, dram_limits, idx)
            dram_limits[idx] = round(Int, decrease_amount * dram_limits[idx])
        end
    end

    # Return a new ILHolder
    return T(
        dram_limits,
        Dict{XTensor{XNode}, TensorMeta}(),
        Dict{XNode, Vector{JuMP.VariableRef}}(),
        Dict{String, Int}(),
        rb(I),
        wb(I),
        rba(I),
        wba(I),
        I.defrag,
    )
end

max_stars_repo_name: darchr/AutoTM

max_issues_repo_name: darchr/AutoTM

max_forks_repo_name: darchr/AutoTM


text:
export LordCressie

module LordCressie

using CSV
using ..Empirikos: MultinomialSummary, BinomialSample

const DATA = joinpath(@__DIR__, "lord_cressie_1975.csv")

function load_table()
    CSV.File(DATA)
end

function ebayes_samples()
    tbl = load_table()
    Zs = MultinomialSummary(BinomialSample.(tbl.x, 20), tbl.N1)
end

end


text:
for N in 2:8
    facts("Infeasible in $N dimensions") do
        m = Model()
        @variable(m, 0 ≤ x[1:N] ≤ 1)
        @constraint(m, x[1] ≥ 2)

        r, v = get_extrema(m)

        @fact isempty(r) --> true
        @fact isempty(v) --> true
    end
end


text:
# Julia wrapper for header: gdalwarper.h
# Automatically generated using Clang.jl

"""
    GDALWarpNoDataMasker(void * pMaskFuncArg,
                         int nBandCount,
                         GDALDataType eType,
                         int,
                         int,
                         int nXSize,
                         int nYSize,
                         GByte ** ppImageData,
                         int bMaskIsFloat,
                         void * pValidityMask,
                         int * pbOutAllValid) -> CPLErr
"""
function gdalwarpnodatamasker(pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, papabyImageData, bMaskIsFloat, pValidityMask, pbOutAllValid)
    aftercare(ccall((:GDALWarpNoDataMasker, libgdal), CPLErr, (Ptr{Cvoid}, Cint, GDALDataType, Cint, Cint, Cint, Cint, Ptr{Ptr{GByte}}, Cint, Ptr{Cvoid}, Ptr{Cint}), pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, papabyImageData, bMaskIsFloat, pValidityMask, pbOutAllValid))
end

"""
    GDALWarpDstAlphaMasker(void * pMaskFuncArg,
                           int nBandCount,
                           GDALDataType,
                           int nXOff,
                           int nYOff,
                           int nXSize,
                           int nYSize,
                           GByte **,
                           int bMaskIsFloat,
                           void * pValidityMask) -> CPLErr
"""
function gdalwarpdstalphamasker(pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, arg1, bMaskIsFloat, pValidityMask)
    aftercare(ccall((:GDALWarpDstAlphaMasker, libgdal), CPLErr, (Ptr{Cvoid}, Cint, GDALDataType, Cint, Cint, Cint, Cint, Ptr{Ptr{GByte}}, Cint, Ptr{Cvoid}), pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, arg1, bMaskIsFloat, pValidityMask))
end

"""
    GDALWarpSrcAlphaMasker(void * pMaskFuncArg,
                           int,
                           GDALDataType,
                           int nXOff,
                           int nYOff,
                           int nXSize,
                           int nYSize,
                           GByte **,
                           int bMaskIsFloat,
                           void * pValidityMask,
                           int * pbOutAllOpaque) -> CPLErr
"""
function gdalwarpsrcalphamasker(pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, arg1, bMaskIsFloat, pValidityMask, pbOutAllOpaque)
    aftercare(ccall((:GDALWarpSrcAlphaMasker, libgdal), CPLErr, (Ptr{Cvoid}, Cint, GDALDataType, Cint, Cint, Cint, Cint, Ptr{Ptr{GByte}}, Cint, Ptr{Cvoid}, Ptr{Cint}), pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, arg1, bMaskIsFloat, pValidityMask, pbOutAllOpaque))
end

"""
    GDALWarpSrcMaskMasker(void * pMaskFuncArg,
                          int,
                          GDALDataType,
                          int nXOff,
                          int nYOff,
                          int nXSize,
                          int nYSize,
                          GByte **,
                          int bMaskIsFloat,
                          void * pValidityMask) -> CPLErr
"""
function gdalwarpsrcmaskmasker(pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, arg1, bMaskIsFloat, pValidityMask)
    aftercare(ccall((:GDALWarpSrcMaskMasker, libgdal), CPLErr, (Ptr{Cvoid}, Cint, GDALDataType, Cint, Cint, Cint, Cint, Ptr{Ptr{GByte}}, Cint, Ptr{Cvoid}), pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, arg1, bMaskIsFloat, pValidityMask))
end

"""
    GDALWarpCutlineMasker(void * pMaskFuncArg,
                          int,
                          GDALDataType,
                          int nXOff,
                          int nYOff,
                          int nXSize,
                          int nYSize,
                          GByte **,
                          int bMaskIsFloat,
                          void * pValidityMask) -> CPLErr
"""
function gdalwarpcutlinemasker(pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, arg1, bMaskIsFloat, pValidityMask)
    aftercare(ccall((:GDALWarpCutlineMasker, libgdal), CPLErr, (Ptr{Cvoid}, Cint, GDALDataType, Cint, Cint, Cint, Cint, Ptr{Ptr{GByte}}, Cint, Ptr{Cvoid}), pMaskFuncArg, nBandCount, eType, nXOff, nYOff, nXSize, nYSize, arg1, bMaskIsFloat, pValidityMask))
end

"""
    GDALCreateWarpOptions() -> GDALWarpOptions *

Create a warp options structure.
"""
function gdalcreatewarpoptions()
    aftercare(ccall((:GDALCreateWarpOptions, libgdal), Ptr{GDALWarpOptions}, ()))
end

"""
    GDALDestroyWarpOptions(GDALWarpOptions * psOptions) -> void

Destroy a warp options structure.
"""
function gdaldestroywarpoptions(arg1)
    aftercare(ccall((:GDALDestroyWarpOptions, libgdal), Cvoid, (Ptr{GDALWarpOptions},), arg1))
end

"""
    GDALCloneWarpOptions(const GDALWarpOptions * psSrcOptions) -> GDALWarpOptions *

Clone a warp options structure.
"""
function gdalclonewarpoptions(arg1)
    aftercare(ccall((:GDALCloneWarpOptions, libgdal), Ptr{GDALWarpOptions}, (Ptr{GDALWarpOptions},), arg1))
end

"""
    GDALWarpInitDstNoDataReal(GDALWarpOptions * psOptionsIn,
                              double dNoDataReal) -> void

Initialize padfDstNoDataReal with specified value.

### Parameters
* **psOptionsIn**: options to initialize.
* **dNoDataReal**: value to initialize to.
"""
function gdalwarpinitdstnodatareal(arg1, dNoDataReal)
    aftercare(ccall((:GDALWarpInitDstNoDataReal, libgdal), Cvoid, (Ptr{GDALWarpOptions}, Cdouble), arg1, dNoDataReal))
end

"""
    GDALWarpInitSrcNoDataReal(GDALWarpOptions * psOptionsIn,
                              double dNoDataReal) -> void

Initialize padfSrcNoDataReal with specified value.

### Parameters
* **psOptionsIn**: options to initialize.
* **dNoDataReal**: value to initialize to.
"""
function gdalwarpinitsrcnodatareal(arg1, dNoDataReal)
    aftercare(ccall((:GDALWarpInitSrcNoDataReal, libgdal), Cvoid, (Ptr{GDALWarpOptions}, Cdouble), arg1, dNoDataReal))
end

"""
    GDALWarpInitNoDataReal(GDALWarpOptions * psOptionsIn,
                           double dNoDataReal) -> void

Initialize padfSrcNoDataReal and padfDstNoDataReal with specified value.

### Parameters
* **psOptionsIn**: options to initialize.
* **dNoDataReal**: value to initialize to.
"""
function gdalwarpinitnodatareal(arg1, dNoDataReal)
    aftercare(ccall((:GDALWarpInitNoDataReal, libgdal), Cvoid, (Ptr{GDALWarpOptions}, Cdouble), arg1, dNoDataReal))
end

"""
    GDALWarpInitDstNoDataImag(GDALWarpOptions * psOptionsIn,
                              double dNoDataImag) -> void

Initialize padfDstNoDataImag with specified value.

### Parameters
* **psOptionsIn**: options to initialize.
* **dNoDataImag**: value to initialize to.
"""
function gdalwarpinitdstnodataimag(arg1, dNoDataImag)
    aftercare(ccall((:GDALWarpInitDstNoDataImag, libgdal), Cvoid, (Ptr{GDALWarpOptions}, Cdouble), arg1, dNoDataImag))
end

"""
    GDALWarpInitSrcNoDataImag(GDALWarpOptions * psOptionsIn,
                              double dNoDataImag) -> void

Initialize padfSrcNoDataImag with specified value.

### Parameters
* **psOptionsIn**: options to initialize.
* **dNoDataImag**: value to initialize to.
"""
function gdalwarpinitsrcnodataimag(arg1, dNoDataImag)
    aftercare(ccall((:GDALWarpInitSrcNoDataImag, libgdal), Cvoid, (Ptr{GDALWarpOptions}, Cdouble), arg1, dNoDataImag))
end

"""
    GDALWarpResolveWorkingDataType(GDALWarpOptions * psOptions) -> void

If the working data type is unknown, this method will determine a valid working data type to support the data in the src and dest data sets and any noData values.

### Parameters
* **psOptions**: options to initialize.
"""
function gdalwarpresolveworkingdatatype(arg1)
    aftercare(ccall((:GDALWarpResolveWorkingDataType, libgdal), Cvoid, (Ptr{GDALWarpOptions},), arg1))
end

"""
    GDALWarpInitDefaultBandMapping(GDALWarpOptions * psOptionsIn,
                                   int nBandCount) -> void

Init src and dst band mappings such that Bands[i] = i+1 for nBandCount Does nothing if psOptionsIn->nBandCount is non-zero.

### Parameters
* **psOptionsIn**: options to initialize.
* **nBandCount**: bands to initialize for.
"""
function gdalwarpinitdefaultbandmapping(arg1, nBandCount)
    aftercare(ccall((:GDALWarpInitDefaultBandMapping, libgdal), Cvoid, (Ptr{GDALWarpOptions}, Cint), arg1, nBandCount))
end

"""
    GDALSerializeWarpOptions(const GDALWarpOptions * psWO) -> CPLXMLNode *
"""
function gdalserializewarpoptions(arg1)
    aftercare(ccall((:GDALSerializeWarpOptions, libgdal), Ptr{CPLXMLNode}, (Ptr{GDALWarpOptions},), arg1))
end

"""
    GDALDeserializeWarpOptions(CPLXMLNode * psTree) -> GDALWarpOptions *
"""
function gdaldeserializewarpoptions(arg1)
    aftercare(ccall((:GDALDeserializeWarpOptions, libgdal), Ptr{GDALWarpOptions}, (Ptr{CPLXMLNode},), arg1))
end

"""
    GDALReprojectImage(GDALDatasetH hSrcDS,
                       const char * pszSrcWKT,
                       GDALDatasetH hDstDS,
                       const char * pszDstWKT,
                       GDALResampleAlg eResampleAlg,
                       double dfWarpMemoryLimit,
                       double dfMaxError,
                       GDALProgressFunc pfnProgress,
                       void * pProgressArg,
                       GDALWarpOptions * psOptions) -> CPLErr

Reproject image.

### Parameters
* **hSrcDS**: the source image file.
* **pszSrcWKT**: the source projection. If NULL the source projection is read from from hSrcDS.
* **hDstDS**: the destination image file.
* **pszDstWKT**: the destination projection. If NULL the destination projection will be read from hDstDS.
* **eResampleAlg**: the type of resampling to use.
* **dfWarpMemoryLimit**: the amount of memory (in bytes) that the warp API is allowed to use for caching. This is in addition to the memory already allocated to the GDAL caching (as per GDALSetCacheMax()). May be 0.0 to use default memory settings.
* **dfMaxError**: maximum error measured in input pixels that is allowed in approximating the transformation (0.0 for exact calculations).
* **pfnProgress**: a GDALProgressFunc() compatible callback function for reporting progress or NULL.
* **pProgressArg**: argument to be passed to pfnProgress. May be NULL.
* **psOptions**: warp options, normally NULL.

### Returns
CE_None on success or CE_Failure if something goes wrong.
"""
function gdalreprojectimage(hSrcDS, pszSrcWKT, hDstDS, pszDstWKT, eResampleAlg, dfWarpMemoryLimit, dfMaxError, pfnProgress, pProgressArg, psOptions)
    aftercare(ccall((:GDALReprojectImage, libgdal), CPLErr, (GDALDatasetH, Cstring, GDALDatasetH, Cstring, GDALResampleAlg, Cdouble, Cdouble, GDALProgressFunc, Ptr{Cvoid}, Ptr{GDALWarpOptions}), hSrcDS, pszSrcWKT, hDstDS, pszDstWKT, eResampleAlg, dfWarpMemoryLimit, dfMaxError, pfnProgress, pProgressArg, psOptions))
end

"""
    GDALCreateAndReprojectImage(GDALDatasetH hSrcDS,
                                const char * pszSrcWKT,
                                const char * pszDstFilename,
                                const char * pszDstWKT,
                                GDALDriverH hDstDriver,
                                char ** papszCreateOptions,
                                GDALResampleAlg eResampleAlg,
                                double dfWarpMemoryLimit,
                                double dfMaxError,
                                GDALProgressFunc pfnProgress,
                                void * pProgressArg,
                                GDALWarpOptions * psOptions) -> CPLErr

Reproject an image and create the target reprojected image.
"""
function gdalcreateandreprojectimage(hSrcDS, pszSrcWKT, pszDstFilename, pszDstWKT, hDstDriver, papszCreateOptions, eResampleAlg, dfWarpMemoryLimit, dfMaxError, pfnProgress, pProgressArg, psOptions)
    aftercare(ccall((:GDALCreateAndReprojectImage, libgdal), CPLErr, (GDALDatasetH, Cstring, Cstring, Cstring, GDALDriverH, Ptr{Cstring}, GDALResampleAlg, Cdouble, Cdouble, GDALProgressFunc, Ptr{Cvoid}, Ptr{GDALWarpOptions}), hSrcDS, pszSrcWKT, pszDstFilename, pszDstWKT, hDstDriver, papszCreateOptions, eResampleAlg, dfWarpMemoryLimit, dfMaxError, pfnProgress, pProgressArg, psOptions))
end

"""
    GDALAutoCreateWarpedVRT(GDALDatasetH hSrcDS,
                            const char * pszSrcWKT,
                            const char * pszDstWKT,
                            GDALResampleAlg eResampleAlg,
                            double dfMaxError,
                            const GDALWarpOptions * psOptionsIn) -> GDALDatasetH

Create virtual warped dataset automatically.

### Parameters
* **hSrcDS**: The source dataset.
* **pszSrcWKT**: The coordinate system of the source image. If NULL, it will be read from the source image.
* **pszDstWKT**: The coordinate system to convert to. If NULL no change of coordinate system will take place.
* **eResampleAlg**: One of GRA_NearestNeighbour, GRA_Bilinear, GRA_Cubic, GRA_CubicSpline, GRA_Lanczos, GRA_Average or GRA_Mode. Controls the sampling method used.
* **dfMaxError**: Maximum error measured in input pixels that is allowed in approximating the transformation (0.0 for exact calculations).
* **psOptionsIn**: Additional warp options, normally NULL.

### Returns
NULL on failure, or a new virtual dataset handle on success.
"""
function gdalautocreatewarpedvrt(hSrcDS, pszSrcWKT, pszDstWKT, eResampleAlg, dfMaxError, psOptions)
    aftercare(ccall((:GDALAutoCreateWarpedVRT, libgdal), GDALDatasetH, (GDALDatasetH, Cstring, Cstring, GDALResampleAlg, Cdouble, Ptr{GDALWarpOptions}), hSrcDS, pszSrcWKT, pszDstWKT, eResampleAlg, dfMaxError, psOptions))
end

"""
    GDALCreateWarpedVRT(GDALDatasetH hSrcDS,
                        int nPixels,
                        int nLines,
                        double * padfGeoTransform,
                        GDALWarpOptions * psOptions) -> GDALDatasetH

Create virtual warped dataset.

### Parameters
* **hSrcDS**: The source dataset.
* **nPixels**: Width of the virtual warped dataset to create
* **nLines**: Height of the virtual warped dataset to create
* **padfGeoTransform**: Geotransform matrix of the virtual warped dataset to create
* **psOptions**: Warp options. Must be different from NULL.

### Returns
NULL on failure, or a new virtual dataset handle on success.
"""
function gdalcreatewarpedvrt(hSrcDS, nPixels, nLines, padfGeoTransform, psOptions)
    aftercare(ccall((:GDALCreateWarpedVRT, libgdal), GDALDatasetH, (GDALDatasetH, Cint, Cint, Ptr{Cdouble}, Ptr{GDALWarpOptions}), hSrcDS, nPixels, nLines, padfGeoTransform, psOptions))
end

"""
    GDALInitializeWarpedVRT(GDALDatasetH hDS,
                            GDALWarpOptions * psWO) -> CPLErr

Set warp info on virtual warped dataset.

### Parameters
* **hDS**: dataset previously created with the VRT driver, and a SUBCLASS of "VRTWarpedDataset".
* **psWO**: the warp options to apply. Note that ownership of the transformation information is taken over by the function though everything else remains the property of the caller.

### Returns
CE_None on success or CE_Failure if an error occurs.
"""
function gdalinitializewarpedvrt(hDS, psWO)
    aftercare(ccall((:GDALInitializeWarpedVRT, libgdal), CPLErr, (GDALDatasetH, Ptr{GDALWarpOptions}), hDS, psWO))
end

"""
    GDALCreateWarpOperation(const GDALWarpOptions * psNewOptions) -> GDALWarpOperationH
"""
function gdalcreatewarpoperation(arg1)
    aftercare(ccall((:GDALCreateWarpOperation, libgdal), GDALWarpOperationH, (Ptr{GDALWarpOptions},), arg1))
end

"""
    GDALDestroyWarpOperation(GDALWarpOperationH hOperation) -> void
"""
function gdaldestroywarpoperation(arg1)
    aftercare(ccall((:GDALDestroyWarpOperation, libgdal), Cvoid, (GDALWarpOperationH,), arg1))
end

"""
    GDALChunkAndWarpImage(GDALWarpOperationH hOperation,
                          int nDstXOff,
                          int nDstYOff,
                          int nDstXSize,
                          int nDstYSize) -> CPLErr
"""
function gdalchunkandwarpimage(arg1, arg2, arg3, arg4, arg5)
    aftercare(ccall((:GDALChunkAndWarpImage, libgdal), CPLErr, (GDALWarpOperationH, Cint, Cint, Cint, Cint), arg1, arg2, arg3, arg4, arg5))
end

"""
    GDALChunkAndWarpMulti(GDALWarpOperationH hOperation,
                          int nDstXOff,
                          int nDstYOff,
                          int nDstXSize,
                          int nDstYSize) -> CPLErr
"""
function gdalchunkandwarpmulti(arg1, arg2, arg3, arg4, arg5)
    aftercare(ccall((:GDALChunkAndWarpMulti, libgdal), CPLErr, (GDALWarpOperationH, Cint, Cint, Cint, Cint), arg1, arg2, arg3, arg4, arg5))
end

"""
    GDALWarpRegion(GDALWarpOperationH hOperation,
                   int nDstXOff,
                   int nDstYOff,
                   int nDstXSize,
                   int nDstYSize,
                   int nSrcXOff,
                   int nSrcYOff,
                   int nSrcXSize,
                   int nSrcYSize) -> CPLErr
"""
function gdalwarpregion(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
    aftercare(ccall((:GDALWarpRegion, libgdal), CPLErr, (GDALWarpOperationH, Cint, Cint, Cint, Cint, Cint, Cint, Cint, Cint), arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
end

"""
    GDALWarpRegionToBuffer(GDALWarpOperationH hOperation,
                           int nDstXOff,
                           int nDstYOff,
                           int nDstXSize,
                           int nDstYSize,
                           void * pDataBuf,
                           GDALDataType eBufDataType,
                           int nSrcXOff,
                           int nSrcYOff,
                           int nSrcXSize,
                           int nSrcYSize) -> CPLErr
"""
function gdalwarpregiontobuffer(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11)
    aftercare(ccall((:GDALWarpRegionToBuffer, libgdal), CPLErr, (GDALWarpOperationH, Cint, Cint, Cint, Cint, Ptr{Cvoid}, GDALDataType, Cint, Cint, Cint, Cint), arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11))
end

"""
    GWKGetFilterRadius(GDALResampleAlg eResampleAlg) -> int
"""
function gwkgetfilterradius(eResampleAlg)
    aftercare(ccall((:GWKGetFilterRadius, libgdal), Cint, (GDALResampleAlg,), eResampleAlg))
end

"""
    GWKGetFilterFunc(GDALResampleAlg eResampleAlg) -> FilterFuncType
"""
function gwkgetfilterfunc(eResampleAlg)
    aftercare(ccall((:GWKGetFilterFunc, libgdal), FilterFuncType, (GDALResampleAlg,), eResampleAlg))
end

"""
    GWKGetFilterFunc4Values(GDALResampleAlg eResampleAlg) -> FilterFunc4ValuesType
"""
function gwkgetfilterfunc4values(eResampleAlg)
    aftercare(ccall((:GWKGetFilterFunc4Values, libgdal), FilterFunc4ValuesType, (GDALResampleAlg,), eResampleAlg))
end


text:
using JLBoostFriends
using Test

@testset "JLBoostFriends.jl" begin
    # Write your own tests here.
end


text:
# compare pressure profiles
using DataFrames
df1 = readtable("p_profile_local_refine")
df2 = readtable("p_profile_coarse_peaceman")
figure(figsize=(5,2.5))
plot(df1[:x_m], df1[:p_pa]./1e5, "--o", label="locally refined grid")
plot(df2[:x_m], df2[:p_pa]./1e5, label="coarse, Peaceman well model")
xlabel("x [m]")
ylabel("p [bar]")
legend()
savefig("dp_t.png", bbox_inches="tight")

max_stars_repo_name: simulkade/geothermal

max_issues_repo_name: simulkade/geothermal

max_forks_repo_name: simulkade/geothermal


text:
module ECDSA

##############################################################################
##
## TODO
##
##############################################################################

# - Add tests
# - Implement secp256k1

##############################################################################
##
## References
##
##############################################################################

# - https://github.com/bitcoin/secp256k1/blob/master/src/secp256k1.c
# - http://www.ijcaonline.org/allpdf/pxc387876.pdf
# - http://jeremykun.com/2014/02/08/introducing-elliptic-curves/
# - https://gist.github.com/anonymous/a3799a5a2b0354022eac
# - https://github.com/wwilson/Catacomb.jl

end # module ECDSA


text:
using Printf

function main()
    output_dir_name="post_result"
    input_result_dir="result"

    #vtk_頭の文字
    front="# vtk DataFile Version 2.0"*"\n"
    back="\n"*"ASCII"*"\n"*"DATASET UNSTRUCTURED_GRID"*"\n"
    write_file(output_dir_name,input_result_dir,front,back)
end

function write_file(outdir,inresult_dir,front,back)

    nodes_xmax, nodes_ymax = read_nodenum(1)
    nodes = read_nodes(1)

    elements = read_elements(1)

    inf=readdir(inresult_dir)
    T = zeros(nodes_xmax-3,nodes_ymax-3)

    for i in 1:length(inf)
        if occursin(".dat", inf[i]) == true
            fff=[]
            open(inresult_dir*"/"*inf[i], "r") do f
                fff=read(f,String)
            end
            fff=split(fff,"\n",keepempty=false)

            for i in 2:length(fff)
                fff[i]=replace(fff[i]," \r" => "")
                #temp = split(fff[2]," ")
                T[i-1] = parse(Float64,fff[i])
            end
            dname = replace(inf[i],".dat" => "")
            out_file = replace(inf[i],".dat" => ".vtk")

            write_points(nodes,out_file,outdir,dname,front,back)
            write_cells(elements,out_file,outdir)
            write_result(T,out_file,outdir)
        end
    end
end

function read_nodenum(skipnum)
    # xmax : 仮想セルも含めたnodeのxの数
    # ymax : 仮想セルも含めたnodeのyの数
    fff=[]
    open("grid/nodesnum", "r") do f
        fff=read(f,String)
    end
    fff=split(fff,"\n",keepempty=false)
    num_nodes=length(fff)-skipnum

    for i in 1+skipnum:length(fff)
        fff[i]=replace(fff[i]," \r" => "")
    end

    temp = split(fff[2]," ")
    xmax = parse(Int64,temp[1])
    ymax = parse(Int64,temp[2])

    return xmax, ymax
end

function read_nodes(skipnum)

    fff=[]
    open("grid/nodes_forvtk", "r") do f
        fff=read(f,String)
    end
    fff=split(fff,"\n",keepempty=false)
    num_nodes=length(fff)-skipnum

    for i in 1+skipnum:length(fff)
        fff[i]=replace(fff[i]," \r" => "")
    end

    nodes=zeros(num_nodes,3)
    for i in 1:num_nodes
        temp=split(fff[i+skipnum]," ")

        # x = parse(Float64,temp[1])
        # y = parse(Float64,temp[2])
        # z = parse(Float64,temp[3])
        x = parse(Float64,temp[2])
        y = parse(Float64,temp[3])
        z = 0.0
        nodes[i,1] = x
        nodes[i,2] = y
        nodes[i,3] = z
    end
    return nodes
end

function read_elements(skipnum)
    fff=[]
    open("grid/element_forvtk", "r") do f
        fff=read(f,String)
    end
    fff=split(fff,"\n",keepempty=false)
    num_elements=length(fff)-skipnum

    for i in 1+skipnum:length(fff)
        fff[i]=replace(fff[i]," \r" => "")
    end

    elements = zeros(num_elements,4)
    for i in 1:num_elements
        temp=split(fff[i+skipnum]," ")

        elements[i,1] = parse(Int64,temp[2])-1
        elements[i,2] = parse(Int64,temp[3])-1
        elements[i,3] = parse(Int64,temp[4])-1
        elements[i,4] = parse(Int64,temp[5])-1
    end
    return elements
end

function  write_points(nodes,out_file,outdir,dname,front,back)
    a = size(nodes)[1]
    a_st = @sprintf("%1.0f", a)

    fff = outdir*"/"*out_file
    open(fff,"w") do f
        write(f,front)
        write(f,dname)
        write(f,back)
        write(f,"POINTS "*a_st*" float\n")
        for i in 1:a
            x = @sprintf("%7.7f", nodes[i,1])
            y = @sprintf("%7.7f", nodes[i,2])
            z = @sprintf("%7.7f", nodes[i,3])
            write(f, x*" "*y*" "*z*"\n")
        end
    end
    println("fin writing points")
end

function write_cells(elements,out_file,outdir)
    a = size(elements)[1]
    b = a*5
    a_st = @sprintf("%1.0f", a)
    b_st = @sprintf("%1.0f", b)

    fff = outdir*"/"*out_file
    open(fff,"a") do f
        write(f, "CELLS "*a_st*" "*b_st*"\n")
        for i in 1:a
            d1 = @sprintf("%1.0f", elements[i,1])
            d2 = @sprintf("%1.0f", elements[i,2])
            d3 = @sprintf("%1.0f", elements[i,3])
            d4 = @sprintf("%1.0f", elements[i,4])
            write(f, "4 "*d1*" "*d2*" "*d3*" "*d4*"\n")
        end
        write(f, "CELL_TYPES "*a_st*"\n")
        for i in 1:a
            write(f, "9\n")          #四角のみ
        end
    end
    println("fin writing cells")
end

function write_result(T,out_file,outdir)
    a = length(T)
    a_st = @sprintf("%1.0f", a)

    dtype = "cell_scalars"

    temp1 = "CELL_DATA "*a_st*"\n"
    temp2 = "SCALARS "*dtype*" float\nLOOKUP_TABLE default\n"

    fff = outdir*"/"*out_file
    open(fff,"a") do f
        write(f, temp1)
        write(f, temp2)
        for i in 1:a
            data = @sprintf("%7.7f", T[i])
            write(f, data)
            write(f, "\n")
        end
    end
    print("fin writing "*out_file)
end
# main
main()


text:
function secret_handshake(code::Integer)

end


text:
module Mapper

using ..Model

const STORE = Dict{Int64, Paper}()
const COUNTER = Ref{Int64}(0)

function store!(paper)
    if haskey(STORE, paper.id)
        # updating
        STORE[paper.id] = paper
    else
        # creating new
        paper.id = COUNTER[] += 1
        STORE[paper.id] = paper
    end
    return
end

function get(id)
    return STORE[id]
end

function delete!(id)
    delete!(STORE, id)
    return
end

function getAllPapers()
    return collect(values(STORE))
end

end


text:
using UUIDs

project_filename = joinpath(dirname(@__DIR__), "Project.toml")

write("Project.toml", replace(read("Project.toml", String), r"uuid = .*?\n" =>"uuid = \"$(uuid4())\"\n"))


text:
using Compat
using Docile
using ZMQ
using IncDataStats

const COLLECTOR_PORT = int(get(ENV, "WOODY_PORT", 5543))

include("timetable.jl")
include("collector.jl")
include("reporter.jl")
include("controller.jl")
include("macros.jl")
include("analysis.jl")

@async begin
    c = Collector(COLLECTOR_PORT)
    run_collector_loop(c)
end


text:
using Random
import Base: length, size, iterate, eltype, IteratorSize, IteratorEltype, haslength, @propagate_inbounds, repeat, rand, tail
import .Iterators: cycle, Cycle, take

include("../utils/ImageReader.jl")
include("../../configs.jl")

mutable struct WIDER_Data
    dir::String
    bboxes
    files::Array{String}
    batch_size::Int64
    num_files::Int64
    num_faces::Int64
    shuffle::Bool
    augment::Bool
    curr_idx::Int64
    reader::Image_Reader
    img_size::Int64
    dtype

    function WIDER_Data(dir, label_dir; batch_size::Int64=32, train::Bool=true, img_size=640, shuffle::Bool=true, dtype=Array{Float32})
        files = []
        bbox_dict = Dict()
        num_faces = 0
        open(label_dir * "label.txt", "r") do io
            lines = readlines(io)
            filename = nothing
            for line in lines
                if line[1] == '#'
                    if filename !== nothing
                        bbox_dict[filename] = _clean_bboxes(bbox_dict[filename])
                    end
                    filename = line[3:end]
                    push!(files, filename)
                    bbox_dict[filename] = []
                else
                    bbox = [parse(Float64, x) for x in split(line, " ")]
                    bbox[3] += bbox[1]
                    bbox[4] += bbox[2]
                    num_faces += 1
                    push!(bbox_dict[filename], bbox)
                end
            end
            bbox_dict[filename] = _clean_bboxes(bbox_dict[filename])
        end

        return new(dir, bbox_dict, files, batch_size, length(files), num_faces, shuffle, train, 1, Image_Reader(train), img_size, dtype)
    end
end

function _clean_bboxes(bboxes)
    annotations = zeros(15, length(bboxes))
    # annotation processing
    for person in 1:length(bboxes)
        z_coords = 0
        iter_cnt = length(bboxes[person]) == 4 ? 4 : length(bboxes[person])-1 # differences in val data and train data format
        for iter in 1:iter_cnt
            if (iter > 4) && (mod(iter, 3) == 1)
                z_coords += 1
                continue
            end
            i = iter - z_coords
            annotations[i,person] =  bboxes[person][iter]
        end
        if all(>=(0), annotations[5:14,person])
            annotations[15,person] = 1.0
        else # no/partial landmark info
            annotations[15,person] = -1.0
        end
        if !all(>=(0), annotations[1:4,person])
            annotations[1,person] = -1.0
        end
    end
    return annotations
end

function iterate(data::WIDER_Data, state=ifelse(
            data.shuffle, randperm(data.num_files), collect(1:data.num_files)))

    if state === nothing || length(state) < data.batch_size
        return (nothing, nothing), nothing
    else
        imgs = data.files[state[1:data.batch_size]]
        imgs_arr = zeros(3, data.img_size, data.img_size, data.batch_size)
        labels = []
        idx = 1
        for img_path in imgs
            img_dir = data.dir * "images/" * img_path
            img, box = read_img(img_dir, len=data.img_size, r=data.reader, boxes=data.bboxes[img_path])
            img = reverse(img, dims=1) # needed for using pytorch model
            push!(labels, box)
            imgs_arr[:,:,:,idx] .= img
            idx += 1
        end
        imgs_arr = convert(data.dtype, permutedims(imgs_arr, (3,2,1,4)))

        if length(state) <= data.batch_size
            return (imgs_arr, labels), []
        else
            return (imgs_arr, labels), state[data.batch_size+1:end]
        end
    end
end

max_stars_repo_name: barisbatuhan/retinaface

max_issues_repo_name: barisbatuhan/retinaface

max_forks_repo_name: barisbatuhan/retinaface


text:
using OscillatoryIntegralsODE
using SpecialFunctions
using StaticArrays
using BenchmarkTools
using PyPlot
using OrdinaryDiffEq

nu = 100
r = 100
f(x) = exp(-x^2/16)

clf()
xs = 0:0.001:5.0
plot(xs, [f(x) * sphericalbesselj(nu, r * x) for x in xs], "-", label=raw"$e^{-x^2/16} j_{100}(100x)$")
legend()
gcf()

##
si = SphericalBesselJIntegral{Float64}(nu, r)
result = levintegrate(si, f, 1.0, 5.0, Vern9(); abstol=1e-6, reltol=1e-6)

ref = 0.0008322179291456167
print("relative error = ", (result - ref) / ref, "\n")

## timing
@btime levintegrate($si, $f, 1.0, 5.0, Vern9(); abstol=1e-6, reltol=1e-6)

max_stars_repo_name: xzackli/OscillatoryIntegralsODE

max_issues_repo_name: xzackli/OscillatoryIntegralsODE

max_forks_repo_name: xzackli/OscillatoryIntegralsODE


text:
# Note that this script can accept some limited command-line arguments, run
# `julia build_tarballs.jl --help` to see a usage message.
using BinaryBuilder, Pkg

name = "glmnet"
version = v"4.0.2"

# Collection of sources required to complete build
sources = [
    GitSource("https://github.com/cran/glmnet.git", "b1a4b50de01e0cd24343959d7cf86452bac17b26")
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/glmnet/src

# Add stub for `setpb`, which normally comes from `pb.c` to connect the
# progress meter to R, but we don't need that
echo "
      subroutine setpb(val)
      return
      end
" > pb.f

flags="-fdefault-real-8 -ffixed-form -shared -O3"
if [[ ${target} != *mingw* ]]; then
    flags="${flags} -fPIC";
fi
if [[ ${target} != aarch64* ]] && [[ ${target} != arm* ]]; then
    flags="${flags} -m${nbits}";
fi
mkdir -p ${libdir}
${FC} ${LDFLAGS} ${flags} glmnet5dpclean.f wls.f pb.f -o ${libdir}/libglmnet.${dlext}
install_license ../DESCRIPTION
"""

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

# The products that we will ensure are always built
products = [
    LibraryProduct("libglmnet", :libglmnet)
]

# Dependencies that must be installed before this package can be built
dependencies = Dependency[
    Dependency(PackageSpec(name="CompilerSupportLibraries_jll", uuid="e66e0078-7015-5450-92f7-15fbd957f2ae"))
]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies)

max_stars_repo_name: sharanry/Yggdrasil

max_issues_repo_name: sharanry/Yggdrasil

max_forks_repo_name: sharanry/Yggdrasil