text:
# initialize/deinitialize
function mlinitialize()
    env = ccall((:MLInitialize,MATHLINK_LIB), MLEnv, (Ptr{Uint8},), C_NULL)
    if env.ptr == C_NULL
        error("MathLink: could not initialize library")
    end
    env
end

function mldeinitialize(env::MLEnv)
    if env.ptr != C_NULL
        ccall((:MLDeinitialize,MATHLINK_LIB), None, (MLEnv,), env)
        env.ptr = C_NULL
    end
end

# open/close application
function mlopen{T<:String}(env::MLEnv, argv::Vector{T})
    erra = Array(Cint,1)
    ml = ccall((:MLOpenArgcArgv,MATHLINK_LIB), MLink, (MLEnv, Cint, Ptr{Ptr{Uint8}}, Ptr{Cint}), env, length(argv), argv, erra)
    if erra[1] != MLEOK
        error("MathLink: Could not open link ",erra[1])
    end
    return ml
end
function mlopen(env::MLEnv, str::String)
    erra = Array(Cint,1)
    ml = ccall((:MLOpenString,MATHLINK_LIB), MLink, (MLEnv, Ptr{Uint8}, Ptr{Cint}), env, str, erra)
    if erra[1] != MLEOK
        error("MathLink: Could not open link ",erra[1])
    end
    return ml
end

# by default, open local kernel
mlopen(env::MLEnv) = mlopen(env,["-linkname",MATHEMATICA_EXEC*" -mathlink","-linkmode","launch"])

function mlclose(ml::MLink)
    if ml.ptr != C_NULL
        ccall((:MLClose,MATHLINK_LIB), None, (MLink,), ml)
        ml.ptr = C_NULL
    end
end

mlactivate(ml::MLink) = ccall((:MLActivate,MATHLINK_LIB), MLRTN, (MLink,), ml) != MLRTN_ERR

# error handling
macro mlerr(expr)
    quote
        if $(esc(expr)) == MLRTN_ERR
            error("MathLink: ", mlerrormessage(ml))
        end
    end
end

mlerror(ml::MLink) = ccall((:MLError,MATHLINK_LIB), MLERR, (MLink,), ml)
mlerrormessage(ml::MLink) = bytestring(ccall((:MLErrorMessage,MATHLINK_LIB), Ptr{Uint8}, (MLink,), ml))
mlclearerror(ml::MLink) = ccall((:MLReady,MATHLINK_LIB), MLRTN, (MLink,), ml) != MLRTN_ERR

# packet handling
function mlflush(ml::MLink)
    @mlerr ccall((:MLFlush,MATHLINK_LIB), MLRTN, (MLink,), ml)
end

mlready(ml::MLink) = ccall((:MLReady,MATHLINK_LIB), MLRTN, (MLink,), ml) != MLRTN_ERR # true if data ready to be read

mlendpacket(ml::MLink) = ccall((:MLEndPacket,MATHLINK_LIB), None, (MLink,), ml)

mlnewpacket(ml::MLink) = ccall((:MLNewPacket,MATHLINK_LIB), None, (MLink,), ml)

function mlnextpacket(ml::MLink)
    pkt = ccall((:MLNextPacket,MATHLINK_LIB), MLPKT, (MLink,), ml)
    if pkt == ILLEGALPKT
        error("MathLink: Illegal packet")
    end
    pkt
end

# token handling
function mlputnext(ml::MLink,t)
    @mlerr ccall((:MLPutNext,MATHLINK_LIB), MLRTN, (MLink, MLTKN), ml, t)
end

mlgetnext(ml::MLink) = ccall((:MLGetNext,MATHLINK_LIB), MLTKN, (MLink,), ml)
mlgettype(ml::MLink) = ccall((:MLGetType,MATHLINK_LIB), MLTKN, (MLink,), ml)

# undocumented: similar to mlgetnext/mlgettype, except returns appropriate binary type
# if available (e.g. Int32, Int64, Float64). Otherwise returns standard
# tokens.
function mlgetnextraw(ml::MLink)
    ccall((:MLGetNextRaw,MATHLINK_LIB), MLTKN, (MLink,), ml)
end
function mlgetrawtype(ml::MLink)
    ccall((:MLGetRawType,MATHLINK_LIB), MLTKN, (MLink,), ml)
end

# put and get for different types

# strings and symbols refs
for S in (:String,:Symbol)
    L = symbol(string(:ML,S,:Ref))

    @eval begin
        # MLGet*String calls are all different
        function mlget(ml::MLink,::Type{$L{ASCIIString}})
            stra = Array(Ptr{Uint8},1)
            lba = Array(Cint,1)
            @mlerr ccall(($(string(:MLGetByte,S)),MATHLINK_LIB), MLRTN,
                         (MLink, Ptr{Ptr{Uint8}}, Ptr{Cint}, Clong),
                         ml, stra, lba, zero(Clong))
            return $L{ASCIIString,Uint8}(stra[1],lba[1])
        end
        function mlget(ml::MLink,::Type{$L{UTF8String}})
            stra = Array(Ptr{Uint8},1)
            lba = Array(Cint,1)
            @mlerr ccall(($(string(:MLGetUTF8,S)),MATHLINK_LIB), MLRTN,
                         (MLink, Ptr{Ptr{Uint8}}, Ptr{Cint}, Ptr{Cint}),
                         ml, stra, lba, &zero(Cint))
            return $L{UTF8String,Uint8}(stra[1],lba[1])
        end
        function mlget(ml::MLink,::Type{$L{UTF16String}})
            stra = Array(Ptr{Uint16},1)
            lba = Array(Cint,1)
            @mlerr ccall(($(string(:MLGetUTF16,S)),MATHLINK_LIB), MLRTN,
                         (MLink, Ptr{Ptr{Uint16}}, Ptr{Cint}, Ptr{Cint}),
                         ml, stra, lba, &zero(Cint))
            return $L{UTF16String,Uint16}(stra[1],lba[1])
        end
        function mlget(ml::MLink,::Type{$L{UTF32String}})
            stra = Array(Ptr{Char},1)
            lba = Array(Cint,1)
            @mlerr ccall(($(string(:MLGetUTF32,S)),MATHLINK_LIB), MLRTN,
                         (MLink, Ptr{Ptr{Char}}, Ptr{Cint}),
                         ml, stra, lba)
            return $L{UTF32String,Char}(stra[1],lba[1])
        end
    end

    for (M,T,U) in ((:Byte,:ASCIIString,:Uint8),(:UTF8,:UTF8String,:Uint8),
                    (:UTF16,:UTF16String,:Uint16),(:UTF32,:UTF32String,:Char))
        @eval begin
            function mlput(ml::MLink,s::$(symbol(string(:ML,S,:Ref))){$T,$U})
                @mlerr ccall(($(string(:MLPut,M,S)),MATHLINK_LIB), MLRTN,
                             (MLink, Ptr{$U}, Cint), ml, s.strptr, s.len)
            end

            function mlrelease(ml::MLink, s::$(symbol(string(:ML,S,:Ref))){$T,$U})
                ccall(($(string(:MLRelease,M,S)),MATHLINK_LIB), None,
                      (MLink, Ptr{$U}, Cint), ml, s.strptr, s.len)
            end
        end
    end
end

# string and symbols: these clean up after themselves
function mlget{T<:MLStrings}(ml::MLink,::Type{T})
    msr = mlget(ml,MLStringRef{T})
    str = T(copy(convert(Array,msr)))
    mlrelease(ml,msr)
    str
end
function mlput(ml::MLink,str::MLStrings)
    mlput(ml,convert(MLStringRef,str))
end

function mlget(ml::MLink,::Type{Symbol})
    msr = mlget(ml,MLSymbolRef{UTF8String})
    sym = symbol(UTF8String(copy(convert(Array,msr))))
    mlrelease(ml,msr)
    sym
end
function mlput(ml::MLink,s::Symbol)
    mlput(ml,convert(MLSymbolRef,string(s)))
end

# numeric types
for (M,T) in ((:Integer16,:Int16),(:Integer32,:Int32),(:Integer64,:Int64),
              (:Real32,:Float32),(:Real64,:Float64))
    @eval begin
        function mlput(ml::MLink,n::$T)
            @mlerr ccall(($(string(:MLPut,M)),MATHLINK_LIB), MLRTN, (MLink, $T), ml, n)
        end
        function mlget(ml::MLink,::Type{$T})
            na = Array($T,1)
            @mlerr ccall(($(string(:MLGet,M)),MATHLINK_LIB), MLRTN, (MLink, Ptr{$T}), ml, na)
            return na[1]
        end
    end
end

# numeric arrays refs (including bytes)
for (M,T) in ((:Integer16,:Int16),(:Integer32,:Int32),(:Integer64,:Int64),
              (:Real32,:Float32),(:Real64,:Float64),(:Byte,:Uint8))
    @eval begin
        # Methods for moving arrays
        function mlput{N}(ml::MLink, a::MLArrayRef{$T,N})
            @mlerr ccall(($(string(:MLPut,M,:Array)),MATHLINK_LIB), MLRTN,
                         (MLink, Ptr{$T}, Ptr{Cint}, Ptr{Ptr{Uint8}}, Cint),
                         ml, a.arrptr, a.dimptr, a.headptr, N)
        end
        function mlget(ml::MLink, ::Type{MLArrayRef{$T}})
            arra = Array(Ptr{$T},1)
            dima = Array(Ptr{Cint},1)
            heada = Array(Ptr{Ptr{Uint8}},1)
            na = Array(Cint,1)
            @mlerr ccall(($(string(:MLGet,M,:Array)),MATHLINK_LIB), MLRTN,
                     (MLink, Ptr{Ptr{$T}}, Ptr{Ptr{Cint}}, Ptr{Ptr{Ptr{Uint8}}}, Ptr{Cint}),
                     ml, arra, dima, heada, na)
            MLArrayRef{$T,int(na[1])}(arra[1],dima[1],heada[1])
        end
        function mlrelease{N}(ml::MLink, a::MLArrayRef{$T,N})
            ccall(($(string(:MLRelease,M,:Array)),MATHLINK_LIB), None,
                  (MLink, Ptr{$T}, Ptr{Cint}, Ptr{Ptr{Uint8}}, Cint),
                  ml, a.arrptr, a.dimptr, a.headptr, N)
        end
    end
end

# numeric arrays: copy and automatic cleanup
function mlput{T<:MLArrays,N}(ml::MLink, a::Array{T,N})
    mlput(ml,convert(MLArrayRef{T},Array{T,N}))
end
function mlget{T<:MLArrays}(ml::MLink, ::Type{Array{T}})
    ma = mlget(ml,MLArrayRef{T})
    a = copy(convert(Array{T},ma))
    mlrelease(ml,ma)
    a
end

# functions
function mlput(ml::MLink,f::MLFunction)
    @mlerr ccall((:MLPutFunction,MATHLINK_LIB), MLRTN, (MLink, Ptr{Uint8}, Cint), ml, f.name, f.nargs)
end

function mlget(ml::MLink,::Type{MLFunction})
    stra = Array(Ptr{Uint8},1)
    na = Array(Cint, 1)
    @mlerr ccall((:MLGetFunction,MATHLINK_LIB), MLRTN, (MLink, Ptr{Ptr{Uint8}}, Ptr{Cint}), ml, stra, na)

    str = bytestring(stra[1])
    ccall((:MLReleaseSymbol,MATHLINK_LIB), None, (MLink, Ptr{Uint8}), ml, stra[1])
    MLFunction(symbol(str), na[1])
end

# formerly MLCheckFunction
function mltesthead(ml::MLink,fname)
    na = Array(Cint, 1)
    @mlerr ccall((:MLTestHead,MATHLINK_LIB), MLRTN, (MLink, Ptr{Uint8}, Ptr{Cint}), ml, string(fname), na)
    na[1]
end

# loopbacks
function mlloopbackopen(e::MLEnv)
    erra = Array(Cint,1)
    ml = ccall((:MLLoopbackOpen,MATHLINK_LIB), MLink, (MLEnv, Ptr{Cint}), env, erra)
    if erra[1] != MLEOK
        error("MathLink: Could not open loopback link ",erra[1])
    end
    return ml
end

function mltransferexpression(dst::MLink,src::MLink)
    if ccall((:MLTransferExpression,MATHLINK_LIB), MLRTN, (MLink, MLink), dst, src) == MLRTN_ERR
        error("MathLink: Could not transfer expression")
    end
end
function mltransfertoendofloopbacklink(dst::MLink,src::MLink)
    if ccall((:MLTransferToEndOfLoopbackLink,MATHLINK_LIB), MLRTN, (MLink, MLink), dst, src) == MLRTN_ERR
        error("MathLink: Could not transfer expression")
    end
end

# marks
function mlcreatemark(ml::MLink)
    mk = ccall((:MLCreateMark,MATHLINK_LIB), MLMark, (MLink,), ml)
    if mk.ptr == C_NULL
        error("MathLink: Could not create mark")
    end
    mk
end

function mlseektomark(ml::MLink,mk::MLMark,n::Integer)
    sk = ccall((:MLSeekToMark,MATHLINK_LIB), MLMark, (MLink,MLMark,Cint), ml, mk, n)
    if sk.ptr == C_NULL
        error("MathLink: Could not seek mark")
    end
    sk
end
mlseektomark(ml::MLink,mk::MLMark) = mlseektomark(ml,mk,zero(Cint))

mldestroymark(ml::MLink,mk::MLMark) = ccall((:MLDestroyMark,MATHLINK_LIB), None, (MLink, MLMark), ml, mk)

# misc
function mlgetlinkedenvidstring(ml::MLink)
    stra = Array(Ptr{Uint8},1)
    @mlerr ccall((:MLGetLinkedEnvIDString,MATHLINK_LIB), MLRTN, (MLink, Ptr{Ptr{Uint8}}), ml, stra)
    str = bytestring(stra[1])
    ccall((:MLReleaseEnvIDString,MATHLINK_LIB), None, (MLink, Ptr{Uint8}), ml, stra[1])
    return str
end
function mlsetenvidstring(e::MLEnv,s::ASCIIString)
    if ccall((:MLSetEnvIDString,MATHLINK_LIB), MLRTN, (MLEnv, Ptr{Uint8}), e, bytestring(s)) == MLRTN_ERR
        error("MathLink: Could not set EnvID string")
    end
end

mllinkname(ml::MLink) = bytestring(ccall((:MLLinkName,MATHLINK_LIB), Ptr{Uint8}, (MLink,), ml))
function mltolinkid(ml::MLink)
    id = ccall((:MLToLinkID,MATHLINK_LIB), Clong, (MLink,), ml)
    if id == 0
        error("MathLink: Could not find link id")
    end
    id
end
function mlfromlinkid(e::MLEnv,id)
    ml = ccall((:MLFromLinkID,MATHLINK_LIB), MLink, (MLEnv,Clong), e, id)
    if ml.ptr == C_NULL
        error("MathLink: No MLink found")
    end
    ml
end


text:
@testset "gettag.jl" begin
    strvec = [
        "stn[CS]dt[20140325]type[full].mat",
        "stn[CS]dt[20140328]type[full].mat",
        "stn[CS]dt[20140327]type[full].mat",
        "stn[CS]dt[20140329]type[full].mat",
        "stn[CS]dt[20140330]type[full].mat",
        "stn[CS]dt[20140326]type[full].mat",
        "stn[TT]dt[20200716]type[full].mat",
        "stn[TT]dt[20200717]type[full].mat",
        "stn[TT]dt[20200718]type[full].mat",
        "stn[TT]dt[20200719]type[full].mat",
        "stn[TT]dt[20200720]type[full].mat"]
    dtstr = broadcast(x -> x.match, match.(r"(?<=dt\[)\d+?(?=\])", strvec))
    dtstr2 = gettag.("dt", strvec)
    @test isequal(dtstr, dtstr2)
    @test "TSUG" == gettag("stn","[Hello]stn[TSUG]dt[20220105].mat")
    @test "TS_U465-G" == gettag("stn","[Hello]stn[TS_U465-G]dt[20220105].mat")
end

ext: jl


text:
times(ls::Union{LinkStream,DirectedLinkStream}, name::AbstractString)=name ∈ ls ? ls.T : []

times(s::Union{StreamGraph,DirectedStreamGraph}, name::AbstractString)=name ∈ s ? s.W[name].presence : Intervals()

times(ls::AbstractDirectedStream, from::AbstractString, to::AbstractString)=haskey(ls.E,from)&haskey(ls.E[from],to) ? ls.E[from][to].presence : Intervals()

function times(ls::AbstractUndirectedStream, from::AbstractString, to::AbstractString)
    if from==to
        return Intervals()
    elseif from>to
        from,to=to,from
    end
    if haskey(ls.E,from)
        if haskey(ls.E[from],to)
            return ls.E[from][to].presence
        else
            return Intervals()
        end
    else
        return Intervals()
    end
end

function times(ls::Union{LinkStream,DirectedLinkStream})
    evt = Set{Float64}([ls.T.list[1][1],ls.T.list[1][2]])
    for l in links(ls)
        for interv in l.presence.list
            push!(evt,interv[1])
            push!(evt,interv[2])
        end
    end
    sort(collect(evt))
end

function times(s::Union{StreamGraph,DirectedStreamGraph})
    evt = Set{Float64}([s.T.list[1][1],s.T.list[1][2]])
    for n in nodes(s)
        for interv in n.presence.list
            push!(evt,interv[1])
            push!(evt,interv[2])
        end
    end
    for l in links(s)
        for interv in l.presence.list
            push!(evt,interv[1])
            push!(evt,interv[2])
        end
    end
    sort(collect(evt))
end


text:
int_rules_7_3_1 = @theory begin

    #= ::Subsection::Closed:: =#
    #= 7.3.1*(a+b*arctanh(c*x^n))^p =#
    @apply_utils Antiderivative((~(a') + ~(b') * atanh(~(c') * (~x) ^ ~(n'))) ^ ~(p'), ~x) => ~x * (~a + ~b * atanh(~c * (~x) ^ ~n)) ^ ~p - ~b * ~c * ~n * ~p * Antiderivative(((~x) ^ ~n * (~a + ~b * atanh(~c * (~x) ^ ~n)) ^ (~p - 1)) / (1 - (~c) ^ 2 * (~x) ^ (2 * ~n)), ~x) <-- FreeQ([~a, ~b, ~c, ~n], ~x) && (IGtQ(~p, 0) && (EqQ(~n, 1) || EqQ(~p, 1)))
    @apply_utils Antiderivative((~(a') + ~(b') * acoth(~(c') * (~x) ^ ~(n'))) ^ ~(p'), ~x) => ~x * (~a + ~b * acoth(~c * (~x) ^ ~n)) ^ ~p - ~b * ~c * ~n * ~p * Antiderivative(((~x) ^ ~n * (~a + ~b * acoth(~c * (~x) ^ ~n)) ^ (~p - 1)) / (1 - (~c) ^ 2 * (~x) ^ (2 * ~n)), ~x) <-- FreeQ([~a, ~b, ~c, ~n], ~x) && (IGtQ(~p, 0) && (EqQ(~n, 1) || EqQ(~p, 1)))
    @apply_utils Antiderivative((~(a') + ~(b') * atanh(~(c') * (~x) ^ ~n)) ^ ~p, ~x) => Antiderivative(ExpandIntegrand(((~a + (~b * log(1 + ~c * (~x) ^ ~n)) / 2) - (~b * log(1 - ~c * (~x) ^ ~n)) / 2) ^ ~p, ~x), ~x) <-- FreeQ([~a, ~b, ~c], ~x) && (IGtQ(~p, 1) && IGtQ(~n, 0))
    @apply_utils Antiderivative((~(a') + ~(b') * acoth(~(c') * (~x) ^ ~n)) ^ ~p, ~x) => Antiderivative(ExpandIntegrand(((~a + (~b * log(1 + (~x) ^ -(~n) / ~c)) / 2) - (~b * log(1 - (~x) ^ -(~n) / ~c)) / 2) ^ ~p, ~x), ~x) <-- FreeQ([~a, ~b, ~c], ~x) && (IGtQ(~p, 1) && IGtQ(~n, 0))
    @apply_utils Antiderivative((~(a') + ~(b') * atanh(~(c') * (~x) ^ ~n)) ^ ~p, ~x) => Antiderivative((~a + ~b * acoth((~x) ^ -(~n) / ~c)) ^ ~p, ~x) <-- FreeQ([~a, ~b, ~c], ~x) && (IGtQ(~p, 1) && ILtQ(~n, 0))
    @apply_utils Antiderivative((~(a') + ~(b') * acoth(~(c') * (~x) ^ ~n)) ^ ~p, ~x) => Antiderivative((~a + ~b * atanh((~x) ^ -(~n) / ~c)) ^ ~p, ~x) <-- FreeQ([~a, ~b, ~c], ~x) && (IGtQ(~p, 1) && ILtQ(~n, 0))
    @apply_utils Antiderivative((~(a') + ~(b') * atanh(~(c') * (~x) ^ ~n)) ^ ~p, ~x) => With([k = Denominator(~n)], k * Subst(Antiderivative((~x) ^ (k - 1) * (~a + ~b * atanh(~c * (~x) ^ (k * ~n))) ^ ~p, ~x), ~x, (~x) ^ (1 / k))) <-- FreeQ([~a, ~b, ~c], ~x) && (IGtQ(~p, 1) && FractionQ(~n))
    @apply_utils Antiderivative((~(a') + ~(b') * acoth(~(c') * (~x) ^ ~n)) ^ ~p, ~x) => With([k = Denominator(~n)], k * Subst(Antiderivative((~x) ^ (k - 1) * (~a + ~b * acoth(~c * (~x) ^ (k * ~n))) ^ ~p, ~x), ~x, (~x) ^ (1 / k))) <-- FreeQ([~a, ~b, ~c], ~x) && (IGtQ(~p, 1) && FractionQ(~n))
    @apply_utils Antiderivative((~(a') + ~(b') * atanh(~(c') * (~x) ^ ~(n'))) ^ ~p, ~x) => Unintegrable((~a + ~b * atanh(~c * (~x) ^ ~n)) ^ ~p, ~x) <-- FreeQ([~a, ~b, ~c, ~n, ~p], ~x)
    @apply_utils Antiderivative((~(a') + ~(b') * acoth(~(c') * (~x) ^ ~(n'))) ^ ~p, ~x) => Unintegrable((~a + ~b * acoth(~c * (~x) ^ ~n)) ^ ~p, ~x) <-- FreeQ([~a, ~b, ~c, ~n, ~p], ~x)
end

max_stars_repo_path: src/rules/7 Inverse hyperbolic functions/7.3 Inverse hyperbolic tangent/7.3.1 (a+b arctanh(c x^n))^p.jl


text:
# TODO: add proper RNG support via ApproxFun pull request....
function Random.rand(cef::ExponentialFamilyDistribution, n::Int)
    approxfun_interval = ApproxFun.Interval{:closed,:closed}(extrema(cef)...)
    approxfun_fun = ApproxFun.Fun(
        x -> pdf(cef, x; include_basemeasure=true), approxfun_interval
    )
    return ApproxFun.sample(approxfun_fun, n)
end

#function rand(cef::ContinuousExponentialFamily{Uniform{T}})  where T<:Real
#	rand(ce, 1)
#end


text:
using Dates: now, Period, Millisecond, Second

include("threading-utils.jl")

abstract type AbstractServiceBackend end

struct UDPBackend <: AbstractServiceBackend
    server_address::IPv4
    server_port::Int
    socket::UDPSocket
    UDPBackend(ip::IPv4, port::Int) = new(ip, port, UDPSocket())
end
UDPBackend(ip::String,port::Int) = UDPBackend(getaddrinfo(ip), port)

# Sockets.send may cause crash in multi-threaded environment if multiple threads are trying
# to use the same underlying socket.
# This method is not thread-safe and callers are responsible for ensuring proper
# synchronization.
send(be::UDPBackend, msg::String) = Sockets.send(be.socket, be.server_address, be.server_port, msg)

# By default, connect to local statsd backend
DefaultStatsdBackend() = UDPBackend(IPv4(127, 0, 0, 1), 8125)

# Statsd exporter will emit metrics about its own operation.
Base.@kwdef struct StatsdMetrics <: AbstractMetricCollection
    statsd_exporter_packets_sent_total = Counter()
    statsd_exporter_emission_lag_ms_total = Counter()
    statsd_exporter_emission_duration_ms_total = Counter()
end

const metrics = StatsdMetrics()

Base.@kwdef mutable struct StatsdExporter
    # How often we should be sending metric updates to statds backend.
    send_interval::Period = Second(60)

    # Emit metrics that have not seen update in the last send_older_than period.
    send_older_than::Period = Second(120)

    # Backend that should receive statds messages.
    statsd_backend::AbstractServiceBackend = DefaultStatsdBackend()

    # Set of registries to pull the metrics from.
    metric_registries::Set{MetricRegistry} = Set{MetricRegistry}([get_default_registry()])

    # Timestamp of the last emission
    last_emission_timestamp::DateTime = DateTime(0)

    # PeriodicTask in charge of exporting metrics to statsd every `send_interval`
    periodic_task::Union{Nothing,PeriodicTask} = nothing
end

# Stops the background thread.
"""
    stop_statsd_exporter!(data::StatsdExporter)

Stops the currently running exporter thread (if it exists) and clears `data.exporter_thread`
"""
function stop_statsd_exporter!(data::StatsdExporter)
    if data.periodic_task !== nothing
        t = stop_periodic_task!(data.periodic_task)
        data.periodic_task = nothing
        return t
    else
        return nothing
    end
end

"""
    start_statsd_exporter!(data::StatsdExporter)

Starts background statsd exporter thread if `data.send_interval` is positive. This
background thread will be stored in `data.exporter_thread`.
"""
function start_statsd_exporter!(data::StatsdExporter)
    publish_metrics_from(metrics)
    if Dates.value(data.send_interval) > 0
        data.periodic_task = @spawn_sticky_periodic_task(
            "StatsdExporter",
            data.send_interval,
            send_metric_updates(data)
        )
    else
        @warn "Metric emission interval set to 0. Emission thread not enabled."
    end
end

function make_label_string(m::NumericMetric)
    if isempty(m.labels)
        return ""
    else
        # TODO(janrous): How can we ensure that labels are exported in
        # a stable order (for the purpose of testing).
        tag_string = join(["$k:$v" for (k, v) in m.labels], ",")
        return "|#$tag_string"
    end
end

function value_change!(m::Counter, cell::NumericMetric)
    new_value = cell.value[]
    old_value = get(m.last_emitted_values, cell.labels, 0.0)
    m.last_emitted_values[cell.labels] = new_value
    return new_value - old_value
end

function make_statsd_message(m::Counter, cell::NumericMetric)
    return "$(name(m)):$(value_change!(m, cell))|c$(make_label_string(cell))"
end

function make_statsd_message(m::Gauge, cell::NumericMetric)
    return "$(name(m)):$(cell.value[])|g$(make_label_string(cell))"
end

"""
    send_metric_updates(data::StatsdExporter)

Calculates metric updates that should be sent to statsd backend and sends these to the
configured statsd backend via UDP. Counter will report value changes since the last export
and this "last value" will be updated to reflect the new exported value.
"""
function send_metric_updates(data::StatsdExporter)
    messages = Vector{String}()
    new_timestamp = now()
    emission_lag_ms = 0
    # TODO(janrous): Should we support disabling send_older_than feature?

    # Returns true if c.last_changed[] is not within the two timestamps
    # [older_than, newer_than].
    should_emit_cell(c) = !(
        Dates.datetime2unix(new_timestamp - data.send_older_than)
        < c.last_changed[]
        < Dates.datetime2unix(data.last_emission_timestamp)
    )
    emission_duration = ServerMetrics.@time_ms begin
        for reg in data.metric_registries
            for metric in values(reg.metrics)
                for cell in filter(should_emit_cell, get_cells(metric))
                    push!(messages, make_statsd_message(metric, cell))
                end
            end
        end
        if Dates.value(data.last_emission_timestamp) > 0
            emission_lag_ms = Dates.value(
                convert(Millisecond, new_timestamp - data.last_emission_timestamp)
                - convert(Millisecond, data.send_interval)
            )
            if emission_lag_ms > 0
                inc!(metrics.statsd_exporter_emission_lag_ms_total, emission_lag_ms)
            end
        end
        for msg in messages
            send(data.statsd_backend, msg)
        end
        data.last_emission_timestamp = new_timestamp
    end
    inc!(metrics.statsd_exporter_packets_sent_total, length(messages))
    inc!(metrics.statsd_exporter_emission_duration_ms_total, emission_duration)
    # TODO(janrous): export emission_duration in a latency-tracking metric once available.
end

"""
    export_metrics_from_registry!(data::StatsdExporter, r::MetricRegistry)

Adds the metric registry `r` to the statsd exporter. This ensures that the metrics contained
within the registry will be shipped to statsd backend.
"""
function export_metrics_from_registry!(data::StatsdExporter, r::MetricRegistry)
    push!(data.metric_registries, r)
    return nothing
end


text:
using Documenter

makedocs(
  doctest = false,
  format = :html,
  sitename = "JuliaSmoothOptimizers",
  assets = ["assets/style.css"],
  pages = Any["Home" => "index.md",
              "Status" => "status.md"]
)

deploydocs(deps = nothing, make = nothing,
  repo = "github.com/JuliaSmoothOptimizers/JuliaSmoothOptimizers.github.io.git",
  target = "build"
)


text:
#!/bin/bash
# -*- mode: julia -*-
#=
exec ${JULIA:-julia} --color=yes --startup-file=no "${BASH_SOURCE[0]}" "$@"
=#

using Documenter
using PyBase

makedocs(
    sitename = "PyBase.jl",
    repo = "https://github.com/tkf/PyBase.jl/blob/{commit}{path}#{line}",
    format = :html,
    doctest = true,
    strict = true,
)


text:
"""
    infovalue(g::Vector{Integer}, b::Vector{Integer}))

Information value calculation of `g`, `b` vector of binned frequency counts

- weight of evidence = log(density g / density b), 0 adjusted
- infovalue = sum (density g - density b) * weight of evidence

Industry rule of thumb:
- iv <= 0.1         no significant change
- 0.1 < iv <= 0.25  minor change
- 0.25 < iv         major change
"""
function infovalue(
    g::Vector{TG} where {TG<:Integer},
    b::Vector{TB} where {TB<:Integer};
    woeadjust = 0.5,
)
    length(g) >= 2 || error("frequency vectors should have at least 2 elements")
    length(g) == length(b) || error("frequency vectors should be the same length")

    # infovalue is class invariant - swapping g/b doesn't matter
    # infovalue is order invariant - re-ordering rows doesn't matter
    # implementation modelled after SAS PROC BINNING where
    # weight of evidence is adjusted to avoid divide by 0

    pb = b ./ sum(b)
    pg = g ./ sum(g)
    pba = (b + (b .== 0) * woeadjust) ./ sum(b)     # adjust only pure bins
    pga = (g + (g .== 0) * woeadjust) ./ sum(g)
    woe = log.(pga ./ pba)
    sum((pg .- pb) .* woe)
end


text:
"""
```
deriv!(
    dlnParams::DislocationParameters,
    matParams::MaterialParameters,
    mesh::AbstractMesh,
    forceDisplacement::ForceDisplacement,
    network::DislocationNetwork,
)
```
Computes the nodal velocities of a network.
"""
function deriv!(
    dlnParams::DislocationParameters,
    matParams::MaterialParameters,
    mesh::AbstractMesh,
    forceDisplacement::ForceDisplacement,
    network::DislocationNetwork,
)
    calcSegForce!(dlnParams, matParams, mesh, forceDisplacement, network)
    dlnMobility!(dlnParams, matParams, network)

    links = network.links
    coord = network.coord
    nodeVel = network.nodeVel
    label = network.label
    numNode = network.numNode[1]

    # Fixed nodes do not move, internal fixed, surface fixed, external.
    fixedNode = (intFixDln, srfFixDln, extDln)
    idxFixed = findall(x -> x ∈ fixedNode, label)
    !isnothing(idxFixed) ? nodeVel[:, idxFixed] .= 0 : nothing

    # Make surface velocity zero along the surface normal.
    idxSurf = findall(x -> x == srfMobDln, label)
    for node in idxSurf
        # Find the links where node appears.
        nodeLink1 = links[1, :] .== node
        nodeLink2 = links[2, :] .== node

        # Find the node opposite to node in all its links.
        nodeOppLink1 = links[2, nodeLink1]
        nodeOppLink2 = links[1, nodeLink2]

        # Labels of the nodes connected to node.
        conLabel = label[(nodeOppLink1, nodeOppLink2)]
        # Find only those that are external.
        extIdx = findall(x -> x == extDln, conLabel)

        # Correct only if node is connected to external nodes.
        if !isnothing(extIdx)
            numExt = length(extIdx)
            conNodes = coord[:, extIdx]
            # If there is more than one external connection, calculate the mean normal.
            if numExt > 1
                conNodes = mean(conNodes, dims = 2)
                normalize!(conNodes)
            end
            # Vector rejection to remove velocity in the direction of the surface normal.
            nodeVel[:, node] -= @views (nodeVel[:, node] ⋅ conNodes) * conNodes
        end
    end

    return nothing
end

"""
```
integrate!(
    intParams::IntegrationParameters{T1,T2,T3} where {T1 <: AdaptiveEulerTrapezoid,T2,T3},
    intVars::IntegrationTime,
    dlnParams::DislocationParameters,
    matParams::MaterialParameters,
    mesh::AbstractMesh,
    forceDisplacement::ForceDisplacement,
    network::DislocationNetwork,
)
```
Integrates nodal velocities using a time-adaptive Euler-Trapezoid method.
"""
function integrate!(
    intParams::IntegrationParameters{
        T1,
        T2,
        T3,
    } where {T1 <: AdaptiveEulerTrapezoid, T2, T3},
    intVars::IntegrationTime,
    dlnParams::DislocationParameters,
    matParams::MaterialParameters,
    mesh::AbstractMesh,
    forceDisplacement::ForceDisplacement,
    network::DislocationNetwork,
)
    numNode = network.numNode[1]
    numNode == 0 && return network

    # Calculate current velocity.
    deriv!(dlnParams, matParams, mesh, forceDisplacement, network)

    # Store current position and velocity.
    idx = 1:numNode
    initCoord = network.coord[:, idx]
    initVel = network.nodeVel[:, idx]

    dtmin = intParams.dtmin
    dtmax = intParams.dtmax
    abstol = intParams.abstol
    reltol = intParams.reltol
    maxchange = intParams.maxchange
    exponent = intParams.exponent
    maxiter = intParams.maxiter

    dt = max(intVars.dt, dtmin)
    time = intVars.time
    step = intVars.step

    dtOld = zero(typeof(dt))
    dtOldGood::Bool = false
    convergent::Bool = false

    counter = 1
    while !convergent
        # Advance coordinates with forward euler.
        network.coord[:, idx] = initCoord + initVel * dt

        # Calculate new velocity from new coords.
        deriv!(dlnParams, matParams, mesh, forceDisplacement, network)
        coord = network.coord
        nodeVel = network.nodeVel

        # Calculate the distance moved.
        distance = @views coord[:, idx] - initCoord
        maxDist = maximum(abs.(distance))

        # Calculate the error with Euler trapezoid method.
        err = @views distance - (nodeVel[:, idx] + initVel) / 2 * dt
        maxErr = maximum(abs.(err))

        if dt <= dtmin
            counter = maxiter + 1
            # Check if errors are under the tolerances.
        elseif maxDist < abstol && maxErr < reltol
            dtOld = dt
            factor =
                maxchange *
                (1 / (1 + (maxchange^exponent - 1) * (maxErr / reltol)))^(1 / exponent)
            # If the errors are under the tolerances, increase the time step.
            dtOldGood = true
            dt = min(dt * factor, dtmax)
            counter += 1
            # If the errors are over the tolerances, check if the previous time step is good.
        elseif dtOldGood
            dt = dtOld
            counter = maxiter
            # If it wasn't, make the timestep smaller.
        else
            dt = dt / 2
        end

        if counter > maxiter || dt == dtmax
            convergent = true
        end
    end

    intVars = IntegrationTime(dt, time + dt, step + 1)

    return intVars
end

max_stars_repo_name: dcelisgarza/DDD

max_forks_repo_name: dcelisgarza/DDD


text:
using Enzyme
using ForwardDiff
using Parameters
using ShallowWaters

abstract type AbstractDifferentiation end

struct Passive <: AbstractDifferentiation end
struct Forward <: AbstractDifferentiation end
struct Reverse <: AbstractDifferentiation end

function ShallowWaters.run_model(::Type{T}, ::Passive; kwargs...) where {T<:AbstractFloat}

    P = Parameter(T=T;kwargs...)
    @unpack Tprog = P

    G = ShallowWaters.Grid{T,Tprog}(P)
    C = ShallowWaters.Constants{T,Tprog}(P,G)
    F = ShallowWaters.Forcing{T}(P,G)
    S = ShallowWaters.ModelSetup{T,Tprog}(P,G,C,F)

    Prog = ShallowWaters.initial_conditions(Tprog,S)
    Diag = ShallowWaters.preallocate(T,Tprog,G)

    Prog = ShallowWaters.time_integration(Prog,Diag,S)
    return Prog
end

function ShallowWaters.run_model(::Type{T_}, ::Forward; kwargs...) where {T_<:AbstractFloat}
    T = ForwardDiff.Dual{Nothing, T_, 1}

    P = Parameter(T=T;kwargs...)
    @unpack Tprog = P

    G = ShallowWaters.Grid{T,Tprog}(P)
    C = ShallowWaters.Constants{T,Tprog}(P,G)
    F = ShallowWaters.Forcing{T}(P,G)
    S = ShallowWaters.ModelSetup{T,Tprog}(P,G,C,F)

    Prog = ShallowWaters.initial_conditions(Tprog,S)
    Diag = ShallowWaters.preallocate(T,Tprog,G)

    Prog = ShallowWaters.time_integration(Prog,Diag,S)
    return Prog
end

Prog = ShallowWaters.run_model(Float64, Passive(); Ndays=2)
Prog = ShallowWaters.run_model(Float64, Forward(); Ndays=2)


text:
function ScatterMatrix1(olddf::DataFrame, colorido=[], legenda::Bool=true)
    df = olddf[completecases(olddf),:]
    n = size(df, 2)
    nomes = names(df)
    if colorido!=[]
        n = n-1
        splice!(nomes,findfirst(x -> string(x) == colorido, nomes))
    end
    M = Array(Compose.Context, (n,n))

    for (i,indexi) in zip(nomes,1:length(nomes))
        nowcor = false
        for (j,indexj) in zip(nomes,1:length(nomes))
            gdplot = Geom.point
            xTickMarks=yTickMarks=false
            xName=yName=""
            kps=:none
            if j == nomes[1]
                yName=string(i)
                yTickMarks=true
            end
            if i == nomes[end]
                xName=string(j)
                xTickMarks=true
            end

            kps=:none
            if legenda
               kps=:right
            end

            if nowcor#Cor Info
                M[indexi,indexj] = render(plot(df, x=string(j), y=string(i), color=colorido, gdplot,
                Guide.xlabel(nothing), Guide.ylabel(nothing), Guide.xticks(label=xTickMarks), Guide.yticks(label=yTickMarks),
                Theme(panel_stroke=colorant"black", key_position=kps)))
            elseif i == j #Name
		text0=string(names(olddf)[indexi])
		M[indexi,indexj] = compose(context(), (context(), text(0.5, 0.5, text0, hcenter)),
               	(context(0.1w, 0.1h, 0.8w, 0.8h), rectangle(), fill("white"), stroke("black")))#))
                nowcor=true
            else #scatterplots
                M[indexi,indexj] = render(plot(df, x=string(j), y=string(i), color=colorido, gdplot,
                Guide.xlabel(nothing), Guide.ylabel(nothing), Guide.xticks(label=xTickMarks), Guide.yticks(label=yTickMarks),
                Theme(panel_stroke=colorant"black", key_position=kps)))
            end
        end
    end
    return gridstack(M)
end

max_stars_repo_name: wakakusa/ScatterMatrixPlots

max_issues_repo_name: wakakusa/ScatterMatrixPlots

max_forks_repo_name: wakakusa/ScatterMatrixPlots


text:
using Tokenize
using DataStructures

const builtin_types = Dict{Symbol,Symbol}(
    :time => :ROSTime,
    :duration => :ROSDuration,
    :Header=>:Header,
    :char=>:UInt8,
    :byte=>:UInt8
)

const types_to_map = DataType[
    Bool, String, Float32, Float64,
    UInt8, UInt16, UInt32, UInt64,
    Int8, Int16, Int32, Int64
]
for T in types_to_map
    builtin_types[Symbol(lowercase(String(nameof(T))))] = nameof(T)
end

function parse_msg(io::IO)
    defs = []
    for line in eachline(io)
        parts = Tokenize.Tokens.Token[]
        for token in tokenize(line)
            if token.kind in (Tokenize.Tokens.COMMENT, Tokenize.Tokens.ENDMARKER)
                break
            elseif token.kind == Tokenize.Tokens.WHITESPACE
                continue
            else
                push!(parts, token)
            end
        end
        if length(parts) > 0
            push!(defs, parts)
        end
    end
    defs
end

function julia_sym(token)
    val = Tokenize.untokenize(token)
    if Tokenize.Tokens.kind(token) == Tokenize.Tokens.KEYWORD
        # escape keywords:
        Symbol("$(val)_")
    else
        Symbol(val)
    end
end

function make_struct(parent_pkg::Symbol, name::Symbol, lines)

    fields = Expr[]
    deps = Set{Tuple{Symbol,Symbol}}()

    for tokens in lines
        i = 0
        next() = if i >= length(tokens) nothing
                 else tokens[(i+=1)]
                 end
        t = next()

        decl = julia_sym(t)
        t = next()

        if t.kind == Tokenize.Tokens.FWD_SLASH
            typ = julia_sym(next())
            push!(deps, (decl, typ))
            decl = Expr(:(.), decl, QuoteNode(typ))
            t = next()
        else
            decl = get(builtin_types, decl) do
                push!(deps, (parent_pkg, decl))
                decl
            end
        end

        if t.kind == Tokenize.Tokens.LSQUARE
            t = next()
            if t.kind == Tokenize.Tokens.INTEGER
                N = parse(Int,t.val)
                decl = Expr(:curly, :SVector, N, decl)
                t = next()
            elseif t.kind == Tokenize.Tokens.RSQUARE
                decl = Expr(:curly, :Vector, decl)
            end
            t.kind != Tokenize.Tokens.RSQUARE && error("mismatched brakets")
            t = next()
        end

        decl = Expr(:(::), julia_sym(t), decl)
        t = next()

        if t == nothing
            push!(fields, decl)
        elseif t.kind == Tokenize.Tokens.EQ
            # TODO constant
        end
    end

    Expr(:struct, false, Expr(:<:, name, :Readable), Expr(:block, fields...)), deps
end

function add_struct!(out::OrderedDict, pkg::Symbol, lib, name::Symbol)
    if haskey(out, name)
        return
    end
    expr, deps = lib[name]
    for (modname, sym) in deps
        if modname == pkg
            add_struct!(out, pkg, lib, sym)
        end
    end
    out[name] = expr
    nothing
end

function gen_package(package::Symbol, msg_dir::String, deps)
    structs = Dict()
    for msg_file in readdir(msg_dir)
        base, ext = splitext(msg_file)
        typename = Symbol(base)
        if typename in nameof.(types_to_map)
            println("Skipping built-in $typename")
            continue
        end
        tokens = open(parse_msg, joinpath(msg_dir, msg_file), "r")
        structs[typename] = make_struct(package, typename, tokens)
    end

    # depends = Set{Symbol}()
    decls = OrderedDict{Symbol,Expr}()
    for (sname, (expr, _)) in pairs(structs)
        add_struct!(decls, package, structs, sname)
        # for (pkg, item) in deps
        #     if pkg != package
        #         push!(depends, pkg)
        #     end
        # end
    end

    imports = [:(using $m) for m in deps]
    definitions = [expr for (_, expr) in decls]
    code = Expr(:block,
        :(using RobotOSData.Messages),
        imports...,
        definitions...)
    Expr(:module, true, package, code)
end

function gen_package(package::Symbol, msg_dir::String, io::IO, deps)
    code = gen_package(package, msg_dir, deps)
    println(io, "# !! auto-generated code, do not edit !!")
    println(io, code)
end

function gen_package_file(src::String, dst::String, deps...)
    fname = basename(src)
    msg_dir = joinpath(src, "msg")
    if !isdir(msg_dir) return end
    println("Processing $msg_dir")
    pkg = Symbol(fname)
    pkg_file = "$fname.jl"
    pkg_path = joinpath(dst, pkg_file)
    println("Writing to $pkg_path")
    open(pkg_path, "w") do io
        gen_package(pkg, msg_dir, io, deps)
    end
    pkg_file, pkg
end

function gen_module(mod_name::Symbol, ros_pkg_roots::Vector{String}, dst_dir::String, deps...)
    statements = Expr[]
    for src_dir in ros_pkg_roots
        filename, pkg_name = gen_package_file(src_dir, dst_dir, deps...)
        push!(statements, :(include($filename)))
        push!(statements, :(export $pkg_name))
    end

    mod_code = Expr(:module, true, mod_name, Expr(:block, statements...))

    mod_path = joinpath(dst_dir, "$mod_name.jl")
    open(mod_path, "w") do io
        print(io, mod_code)
    end
end