text:
X_COORD=0
Y_COORD=0
SIZE=0
function glavnaia(r::Robot, k::Int)
    global SIZE, Y_COORD, X_COORD
    SIZE=k
    num_ver=moves!(r,Sud)
    num_hor=moves!(r,West)
    marking(r)
    move_back(r,Sud,Y_COORD-num_ver)
    move_back(r,West,X_COORD-num_hor)
end

function marking(r::Robot)
    mark_row(r,Ost)
    side=West
    while isborder(r,Nord)==false
        move_to_side(r,Nord)
        mark_row(r,side)
        side=inverse(side)
    end
end

function mark_row(r::Robot,side::HorizonSide)
    mark_or_no(r,X_COORD,Y_COORD,SIZE)
    while isborder(r,side)==false
        move_to_side(r,side)
        mark_or_no(r,X_COORD,Y_COORD,SIZE)
    end
end

function mark_or_no(r::Robot, x_coord::Int, y_coord::Int, size::Int)
    if ((mod(x_coord,2*size) in 0:size-1)&&(mod(y_coord,2*size) in 0:size-1))
        putmarker!(r)
    end
    if ((mod(x_coord,2*size) in size:2*size-1)&&(mod(y_coord,2*size) in size:2*size-1))
        putmarker!(r)
    end
end

function move_to_side(r::Robot, side::HorizonSide) #двигает робота и отсчитывает координаты
    global X_COORD, Y_COORD
    if side==Nord
        Y_COORD+=1
    elseif side==Sud
        Y_COORD-=1
    elseif side==Ost
        X_COORD+=1
    elseif side==West
        X_COORD-=1
    end
    move!(r,side)
end

function moves!(r::Robot,side::HorizonSide) #двигает робота и возвращает количество шагов
    num_steps=0
    while isborder(r,side)==false
        move!(r,side)
        num_steps+=1
    end
    return num_steps
end

function move_back(r::Robot,side::HorizonSide,num_steps::Int)
    if (num_steps<0)==true
        side=inverse(side)
        num_steps*=-1
    end
    for _ in 1:num_steps
        move!(r,side)
    end
end

inverse(side::HorizonSide)=HorizonSide(mod(Int(side)+2,4))

max_stars_repo_name: Aminulik/mirea-progs

max_issues_repo_name: Aminulik/mirea-progs

max_forks_repo_name: Aminulik/mirea-progs


text:
using MLJBase
if !MLJBase.TESTING
    error(
        "To test MLJBase, the environment variable "*
        "`TEST_MLJBASE` must be set to `\"true\"`\n"*
        "You can do this in the REPL with `ENV[\"TEST_MLJBASE\"]=\"true\"`"
    )
end

using Distributed
# Thanks to https://stackoverflow.com/a/70895939/5056635 for the exeflags tip.
addprocs(; exeflags="--project=$(Base.active_project())")

@info "nprocs() = $(nprocs())"
@static if VERSION >= v"1.3.0-DEV.573"
    import .Threads
    @info "nthreads() = $(Threads.nthreads())"
else
    @info "Running julia $(VERSION). Multithreading tests excluded. "
end

@everywhere begin
    using MLJModelInterface
    using MLJBase
    using Test
    using CategoricalArrays
    using Logging
    using ComputationalResources
    using StableRNGs
end

import TypedTables
using Tables

function include_everywhere(filepath)
    include(filepath) # Load on Node 1 first, triggering any precompile
    if nprocs() > 1
        fullpath = joinpath(@__DIR__, filepath)
        @sync for p in workers()
            @async remotecall_wait(include, p, fullpath)
        end
    end
end

include("test_utilities.jl")

# load Models module containing model implementations for testing:
print("Loading some models for testing...")
include_everywhere("_models/models.jl")
print("\r                                           \r")

# enable conditional testing of modules by providing test_args
# e.g. `Pkg.test("MLJBase", test_args=["misc"])`
RUN_ALL_TESTS = isempty(ARGS)
macro conditional_testset(name, expr)
    name = string(name)
    esc(quote
        if RUN_ALL_TESTS || $name in ARGS
            @testset $name $expr
        end
    end)
end

# To avoid printing `@conditional_testset (macro with 1 method)`
# when loading this file via `include("test/preliminaries.jl")`.
nothing

ext: jl


text:
# syntax: proto3
using ProtoBuf
import ProtoBuf.meta

const WorkerHealth = (;[
    Symbol("OK") => Int32(0),
    Symbol("RECEIVED_SHUTDOWN_SIGNAL") => Int32(1),
    Symbol("INTERNAL_ERROR") => Int32(2),
    Symbol("SHUTTING_DOWN") => Int32(3),
]...)

const WorkerShutdownMode = (;[
    Symbol("DEFAULT") => Int32(0),
    Symbol("NOT_CONFIGURED") => Int32(1),
    Symbol("WAIT_FOR_COORDINATOR") => Int32(2),
    Symbol("SHUTDOWN_AFTER_TIMEOUT") => Int32(3),
]...)

const LogMessage_Level = (;[
    Symbol("UNKNOWN") => Int32(0),
    Symbol("DEBUGGING") => Int32(10),
    Symbol("INFO") => Int32(20),
    Symbol("WARN") => Int32(30),
    Symbol("ERROR") => Int32(40),
    Symbol("FATAL") => Int32(50),
]...)

mutable struct LogMessage <: ProtoType
    __protobuf_jl_internal_meta::ProtoMeta
    __protobuf_jl_internal_values::Dict{Symbol,Any}
    __protobuf_jl_internal_defaultset::Set{Symbol}

    function LogMessage(; kwargs...)
        obj = new(meta(LogMessage), Dict{Symbol,Any}(), Set{Symbol}())
        values = obj.__protobuf_jl_internal_values
        symdict = obj.__protobuf_jl_internal_meta.symdict
        for nv in kwargs
            fldname, fldval = nv
            fldtype = symdict[fldname].jtyp
            (fldname in keys(symdict)) || error(string(typeof(obj), " has no field with name ", fldname))
            values[fldname] = isa(fldval, fldtype) ? fldval : convert(fldtype, fldval)
        end
        obj
    end
end # mutable struct LogMessage
const __meta_LogMessage = Ref{ProtoMeta}()
function meta(::Type{LogMessage})
    ProtoBuf.metalock() do
        if !isassigned(__meta_LogMessage)
            __meta_LogMessage[] = target = ProtoMeta(LogMessage)
            allflds = Pair{Symbol,Union{Type,String}}[:level => Int32, :message => AbstractString]
            meta(target, LogMessage, allflds, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, ProtoBuf.DEF_VAL, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES, ProtoBuf.DEF_ONEOFS, ProtoBuf.DEF_ONEOF_NAMES)
        end
        __meta_LogMessage[]
    end
end
function Base.getproperty(obj::LogMessage, name::Symbol)
    if name === :level
        return (obj.__protobuf_jl_internal_values[name])::Int32
    elseif name === :message
        return (obj.__protobuf_jl_internal_values[name])::AbstractString
    else
        getfield(obj, name)
    end
end

const SessionLog_SessionStatus = (;[
    Symbol("STATUS_UNSPECIFIED") => Int32(0),
    Symbol("START") => Int32(1),
    Symbol("STOP") => Int32(2),
    Symbol("CHECKPOINT") => Int32(3),
]...)

mutable struct SessionLog <: ProtoType
    __protobuf_jl_internal_meta::ProtoMeta
    __protobuf_jl_internal_values::Dict{Symbol,Any}
    __protobuf_jl_internal_defaultset::Set{Symbol}

    function SessionLog(; kwargs...)
        obj = new(meta(SessionLog), Dict{Symbol,Any}(), Set{Symbol}())
        values = obj.__protobuf_jl_internal_values
        symdict = obj.__protobuf_jl_internal_meta.symdict
        for nv in kwargs
            fldname, fldval = nv
            fldtype = symdict[fldname].jtyp
            (fldname in keys(symdict)) || error(string(typeof(obj), " has no field with name ", fldname))
            values[fldname] = isa(fldval, fldtype) ? fldval : convert(fldtype, fldval)
        end
        obj
    end
end # mutable struct SessionLog
const __meta_SessionLog = Ref{ProtoMeta}()
function meta(::Type{SessionLog})
    ProtoBuf.metalock() do
        if !isassigned(__meta_SessionLog)
            __meta_SessionLog[] = target = ProtoMeta(SessionLog)
            allflds = Pair{Symbol,Union{Type,String}}[:status => Int32, :checkpoint_path => AbstractString, :msg => AbstractString]
            meta(target, SessionLog, allflds, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, ProtoBuf.DEF_VAL, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES, ProtoBuf.DEF_ONEOFS, ProtoBuf.DEF_ONEOF_NAMES)
        end
        __meta_SessionLog[]
    end
end
function Base.getproperty(obj::SessionLog, name::Symbol)
    if name === :status
        return (obj.__protobuf_jl_internal_values[name])::Int32
    elseif name === :checkpoint_path
        return (obj.__protobuf_jl_internal_values[name])::AbstractString
    elseif name === :msg
        return (obj.__protobuf_jl_internal_values[name])::AbstractString
    else
        getfield(obj, name)
    end
end

mutable struct TaggedRunMetadata <: ProtoType
    __protobuf_jl_internal_meta::ProtoMeta
    __protobuf_jl_internal_values::Dict{Symbol,Any}
    __protobuf_jl_internal_defaultset::Set{Symbol}

    function TaggedRunMetadata(; kwargs...)
        obj = new(meta(TaggedRunMetadata), Dict{Symbol,Any}(), Set{Symbol}())
        values = obj.__protobuf_jl_internal_values
        symdict = obj.__protobuf_jl_internal_meta.symdict
        for nv in kwargs
            fldname, fldval = nv
            fldtype = symdict[fldname].jtyp
            (fldname in keys(symdict)) || error(string(typeof(obj), " has no field with name ", fldname))
            values[fldname] = isa(fldval, fldtype) ? fldval : convert(fldtype, fldval)
        end
        obj
    end
end # mutable struct TaggedRunMetadata
const __meta_TaggedRunMetadata = Ref{ProtoMeta}()
function meta(::Type{TaggedRunMetadata})
    ProtoBuf.metalock() do
        if !isassigned(__meta_TaggedRunMetadata)
            __meta_TaggedRunMetadata[] = target = ProtoMeta(TaggedRunMetadata)
            allflds = Pair{Symbol,Union{Type,String}}[:tag => AbstractString, :run_metadata => Vector{UInt8}]
            meta(target, TaggedRunMetadata, allflds, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, ProtoBuf.DEF_VAL, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES, ProtoBuf.DEF_ONEOFS, ProtoBuf.DEF_ONEOF_NAMES)
        end
        __meta_TaggedRunMetadata[]
    end
end
function Base.getproperty(obj::TaggedRunMetadata, name::Symbol)
    if name === :tag
        return (obj.__protobuf_jl_internal_values[name])::AbstractString
    elseif name === :run_metadata
        return (obj.__protobuf_jl_internal_values[name])::Vector{UInt8}
    else
        getfield(obj, name)
    end
end

mutable struct Event <: ProtoType
    __protobuf_jl_internal_meta::ProtoMeta
    __protobuf_jl_internal_values::Dict{Symbol,Any}
    __protobuf_jl_internal_defaultset::Set{Symbol}

    function Event(; kwargs...)
        obj = new(meta(Event), Dict{Symbol,Any}(), Set{Symbol}())
        values = obj.__protobuf_jl_internal_values
        symdict = obj.__protobuf_jl_internal_meta.symdict
        for nv in kwargs
            fldname, fldval = nv
            fldtype = symdict[fldname].jtyp
            (fldname in keys(symdict)) || error(string(typeof(obj), " has no field with name ", fldname))
            values[fldname] = isa(fldval, fldtype) ? fldval : convert(fldtype, fldval)
        end
        obj
    end
end # mutable struct Event
const __meta_Event = Ref{ProtoMeta}()
function meta(::Type{Event})
    ProtoBuf.metalock() do
        if !isassigned(__meta_Event)
            __meta_Event[] = target = ProtoMeta(Event)
            allflds = Pair{Symbol,Union{Type,String}}[:wall_time => Float64, :step => Int64, :file_version => AbstractString, :graph_def => Vector{UInt8}, :summary => Summary, :log_message => LogMessage, :session_log => SessionLog, :tagged_run_metadata => TaggedRunMetadata, :meta_graph_def => Vector{UInt8}]
            oneofs = Int[0,0,1,1,1,1,1,1,1]
            oneof_names = Symbol[Symbol("what")]
            meta(target, Event, allflds, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, ProtoBuf.DEF_VAL, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES, oneofs, oneof_names)
        end
        __meta_Event[]
    end
end
function Base.getproperty(obj::Event, name::Symbol)
    if name === :wall_time
        return (obj.__protobuf_jl_internal_values[name])::Float64
    elseif name === :step
        return (obj.__protobuf_jl_internal_values[name])::Int64
    elseif name === :file_version
        return (obj.__protobuf_jl_internal_values[name])::AbstractString
    elseif name === :graph_def
        return (obj.__protobuf_jl_internal_values[name])::Vector{UInt8}
    elseif name === :summary
        return (obj.__protobuf_jl_internal_values[name])::Summary
    elseif name === :log_message
        return (obj.__protobuf_jl_internal_values[name])::LogMessage
    elseif name === :session_log
        return (obj.__protobuf_jl_internal_values[name])::SessionLog
    elseif name === :tagged_run_metadata
        return (obj.__protobuf_jl_internal_values[name])::TaggedRunMetadata
    elseif name === :meta_graph_def
        return (obj.__protobuf_jl_internal_values[name])::Vector{UInt8}
    else
        getfield(obj, name)
    end
end

mutable struct WatchdogConfig <: ProtoType
    __protobuf_jl_internal_meta::ProtoMeta
    __protobuf_jl_internal_values::Dict{Symbol,Any}
    __protobuf_jl_internal_defaultset::Set{Symbol}

    function WatchdogConfig(; kwargs...)
        obj = new(meta(WatchdogConfig), Dict{Symbol,Any}(), Set{Symbol}())
        values = obj.__protobuf_jl_internal_values
        symdict = obj.__protobuf_jl_internal_meta.symdict
        for nv in kwargs
            fldname, fldval = nv
            fldtype = symdict[fldname].jtyp
            (fldname in keys(symdict)) || error(string(typeof(obj), " has no field with name ", fldname))
            values[fldname] = isa(fldval, fldtype) ? fldval : convert(fldtype, fldval)
        end
        obj
    end
end # mutable struct WatchdogConfig
const __meta_WatchdogConfig = Ref{ProtoMeta}()
function meta(::Type{WatchdogConfig})
    ProtoBuf.metalock() do
        if !isassigned(__meta_WatchdogConfig)
            __meta_WatchdogConfig[] = target = ProtoMeta(WatchdogConfig)
            allflds = Pair{Symbol,Union{Type,String}}[:timeout_ms => Int64]
            meta(target, WatchdogConfig, allflds, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, ProtoBuf.DEF_VAL, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES, ProtoBuf.DEF_ONEOFS, ProtoBuf.DEF_ONEOF_NAMES)
        end
        __meta_WatchdogConfig[]
    end
end
function Base.getproperty(obj::WatchdogConfig, name::Symbol)
    if name === :timeout_ms
        return (obj.__protobuf_jl_internal_values[name])::Int64
    else
        getfield(obj, name)
    end
end

mutable struct RequestedExitCode <: ProtoType
    __protobuf_jl_internal_meta::ProtoMeta
    __protobuf_jl_internal_values::Dict{Symbol,Any}
    __protobuf_jl_internal_defaultset::Set{Symbol}

    function RequestedExitCode(; kwargs...)
        obj = new(meta(RequestedExitCode), Dict{Symbol,Any}(), Set{Symbol}())
        values = obj.__protobuf_jl_internal_values
        symdict = obj.__protobuf_jl_internal_meta.symdict
        for nv in kwargs
            fldname, fldval = nv
            fldtype = symdict[fldname].jtyp
            (fldname in keys(symdict)) || error(string(typeof(obj), " has no field with name ", fldname))
            values[fldname] = isa(fldval, fldtype) ? fldval : convert(fldtype, fldval)
        end
        obj
    end
end # mutable struct RequestedExitCode
const __meta_RequestedExitCode = Ref{ProtoMeta}()
function meta(::Type{RequestedExitCode})
    ProtoBuf.metalock() do
        if !isassigned(__meta_RequestedExitCode)
            __meta_RequestedExitCode[] = target = ProtoMeta(RequestedExitCode)
            allflds = Pair{Symbol,Union{Type,String}}[:exit_code => Int32]
            meta(target, RequestedExitCode, allflds, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, ProtoBuf.DEF_VAL, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES, ProtoBuf.DEF_ONEOFS, ProtoBuf.DEF_ONEOF_NAMES)
        end
        __meta_RequestedExitCode[]
    end
end
function Base.getproperty(obj::RequestedExitCode, name::Symbol)
    if name === :exit_code
        return (obj.__protobuf_jl_internal_values[name])::Int32
    else
        getfield(obj, name)
    end
end

mutable struct WorkerHeartbeatRequest <: ProtoType
    __protobuf_jl_internal_meta::ProtoMeta
    __protobuf_jl_internal_values::Dict{Symbol,Any}
    __protobuf_jl_internal_defaultset::Set{Symbol}

    function WorkerHeartbeatRequest(; kwargs...)
        obj = new(meta(WorkerHeartbeatRequest), Dict{Symbol,Any}(), Set{Symbol}())
        values = obj.__protobuf_jl_internal_values
        symdict = obj.__protobuf_jl_internal_meta.symdict
        for nv in kwargs
            fldname, fldval = nv
            fldtype = symdict[fldname].jtyp
            (fldname in keys(symdict)) || error(string(typeof(obj), " has no field with name ", fldname))
            values[fldname] = isa(fldval, fldtype) ? fldval : convert(fldtype, fldval)
        end
        obj
    end
end # mutable struct WorkerHeartbeatRequest
const __meta_WorkerHeartbeatRequest = Ref{ProtoMeta}()
function meta(::Type{WorkerHeartbeatRequest})
    ProtoBuf.metalock() do
        if !isassigned(__meta_WorkerHeartbeatRequest)
            __meta_WorkerHeartbeatRequest[] = target = ProtoMeta(WorkerHeartbeatRequest)
            allflds = Pair{Symbol,Union{Type,String}}[:shutdown_mode => Int32, :watchdog_config => WatchdogConfig, :exit_code => RequestedExitCode]
            meta(target, WorkerHeartbeatRequest, allflds, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, ProtoBuf.DEF_VAL, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES, ProtoBuf.DEF_ONEOFS, ProtoBuf.DEF_ONEOF_NAMES)
        end
        __meta_WorkerHeartbeatRequest[]
    end
end
function Base.getproperty(obj::WorkerHeartbeatRequest, name::Symbol)
    if name === :shutdown_mode
        return (obj.__protobuf_jl_internal_values[name])::Int32
    elseif name === :watchdog_config
        return (obj.__protobuf_jl_internal_values[name])::WatchdogConfig
    elseif name === :exit_code
        return (obj.__protobuf_jl_internal_values[name])::RequestedExitCode
    else
        getfield(obj, name)
    end
end

mutable struct WorkerHeartbeatResponse <: ProtoType
    __protobuf_jl_internal_meta::ProtoMeta
    __protobuf_jl_internal_values::Dict{Symbol,Any}
    __protobuf_jl_internal_defaultset::Set{Symbol}

    function WorkerHeartbeatResponse(; kwargs...)
        obj = new(meta(WorkerHeartbeatResponse), Dict{Symbol,Any}(), Set{Symbol}())
        values = obj.__protobuf_jl_internal_values
        symdict = obj.__protobuf_jl_internal_meta.symdict
        for nv in kwargs
            fldname, fldval = nv
            fldtype = symdict[fldname].jtyp
            (fldname in keys(symdict)) || error(string(typeof(obj), " has no field with name ", fldname))
            values[fldname] = isa(fldval, fldtype) ? fldval : convert(fldtype, fldval)
        end
        obj
    end
end # mutable struct WorkerHeartbeatResponse
const __meta_WorkerHeartbeatResponse = Ref{ProtoMeta}()
function meta(::Type{WorkerHeartbeatResponse})
    ProtoBuf.metalock() do
        if !isassigned(__meta_WorkerHeartbeatResponse)
            __meta_WorkerHeartbeatResponse[] = target = ProtoMeta(WorkerHeartbeatResponse)
            allflds = Pair{Symbol,Union{Type,String}}[:health_status => Int32, :worker_log => Base.Vector{Event}, :hostname => AbstractString]
            meta(target, WorkerHeartbeatResponse, allflds, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, ProtoBuf.DEF_VAL, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES, ProtoBuf.DEF_ONEOFS, ProtoBuf.DEF_ONEOF_NAMES)
        end
        __meta_WorkerHeartbeatResponse[]
    end
end
function Base.getproperty(obj::WorkerHeartbeatResponse, name::Symbol)
    if name === :health_status
        return (obj.__protobuf_jl_internal_values[name])::Int32
    elseif name === :worker_log
        return (obj.__protobuf_jl_internal_values[name])::Base.Vector{Event}
    elseif name === :hostname
        return (obj.__protobuf_jl_internal_values[name])::AbstractString
    else
        getfield(obj, name)
    end
end

export WorkerHealth, WorkerShutdownMode, Event, LogMessage_Level, LogMessage, SessionLog_SessionStatus, SessionLog, TaggedRunMetadata, WatchdogConfig, RequestedExitCode, WorkerHeartbeatRequest, WorkerHeartbeatResponse


text:
@testset "Float64" begin
    @mkdeepconvert(convfloat, Float64)
    @test convfloat(:(1//4)) == 0.25
    @test convfloat(:(1//4 + 0)) == 0.25
    @test convfloat("2^63") == 9.223372036854776e18
end

@testset "Int128" begin
    @mkdeepconvert(conv128, Int128)
    resa = conv128(:(1//4))
    @test  resa == 1//4
    @test typeof(resa) == Rational{Int128}

    @test conv128(:(2^64)) == Int128(2)^64
    @test conv128(:(2^64 + 2^17)) == Int128(2)^64 + 2^17
    @test conv128("2^64 + 2^17") == Int128(2)^64 + 2^17

    @test conv128(1) == 1
    @test typeof(conv128(1)) == Int128
    @test conv128("1") == 1
    @test typeof(conv128("1")) == Int128
    @test conv128("2^63") == Int128(2)^63

    @test typeof(@int128( 23432432 // 3)) == Rational{Int128}

end

@testset "BigInt" begin
    aa = BigInt[BigInt(2)^64, BigInt(2)^63]
    @test aa == bi"[2^64, 2^63]"
    @test bi"(10^53 - 1) // 9"  == (BigInt(10)^53-1)//9
    @test typeof( @bigint 111111111111111111111111111) == BigInt
    @test typeof( @bigint(111111111111111111111111111 + 8)) == BigInt
    @test typeof(1111111111111111111111111 // 3) == Rational{Int128}
    @test typeof(@bigint(1111111111111111111111111 // 3)) == Rational{BigInt}

    @bigint function gg(x)
           2^64 * x
    end
    y = gg(1.0)
    @test typeof(y) == BigFloat
    @test y > 0
end

@testset "BigFloat" begin
    @test bf"1e-50+1" == parse(BigFloat, "1e-50") + BigFloat(1)
    @test (BigFloat(1) * BigFloat(10)^-10 -  @bigfloat 1/10^10) == 0
    a = Meta.parse("1+1")
    @test deepbigfloat(a) == BigFloat(2)
    @test deepbigfloat("1+1") == BigFloat(2)
end

@testset "UInt64" begin
    @mkdeepconvert(convuint64, UInt64)
    res = convuint64("10^19 + 10^17")
    @test res == 0x8c2a687ce7720000
    @test typeof(res) == UInt64
end


text:
#AbstractHMM and HMM forked from HMMBase to maintain compatibility with MS_HMMBase

"""
    AbstractHMM{F<:VariateForm}
An HMM type must at-least implement the following interface:
```julia
struct CustomHMM{F,T} <: AbstractHMM{F}
    π0::AbstractVector{T}              # Initial state distribution
    π::AbstractMatrix{T}               # Transition matrix
    D::AbstractVector{Distribution{F}} # Observations distributions
    # Custom fields ....
end
```
"""
abstract type AbstractHMM{F<:VariateForm} end

"""
    HMM([π0::AbstractVector{T}, ]π::AbstractMatrix{T}, D::AbstractVector{<:Distribution{F}}) where F where T
Build an HMM with transition matrix `π` and observations distributions `D`.
If the initial state distribution `π0` is not specified, a uniform distribution is assumed.
Observations distributions can be of different types (for example `Normal` and `Exponential`).
However they must be of the same dimension (all scalars or all multivariates).
# Example
```julia
hmm = HMM([0.9 0.1; 0.1 0.9], [Normal(0,1), Normal(10,1)])
```
"""
struct HMM{F,T} <: AbstractHMM{F}
    π0::Vector{T}
    π::Matrix{T}
    D::Vector{Distribution{F}}
    HMM{F,T}(π0, π, D) where {F,T} = assert_hmm(π0, π, D) && new(π0, π, D)
end

HMM(π0::AbstractVector{T}, π::AbstractMatrix{T}, D::AbstractVector{<:Distribution{F}}) where {F,T} = HMM{F,T}(π0, π, D)
HMM(π::AbstractMatrix{T}, D::AbstractVector{<:Distribution{F}}) where {F,T} = HMM{F,T}(ones(size(π)[1])/size(π)[1], π, D)

"""
    assert_hmm(π0, π, D)
Throw an `ArgumentError` if the initial state distribution and the transition matrix rows does not sum to 1,
and if the observations distributions does not have the same dimensions.
"""
function assert_hmm(π0::AbstractVector,
                    π::AbstractMatrix,
                    D::AbstractVector{<:Distribution})
    !isprobvec(π0) && throw(ArgumentError("Initial state vector π0 is not a valid probability vector!"))
    !istransmat(π) && throw(ArgumentError("Transition matrix π is not valid!"))

    !all(length.(D) .== length(D[1])) && throw(ArgumentError("All distributions must have the same dimensions"))
    !(length(π0) == size(π,1) == length(D)) && throw("Length of initial state vector π0, dimension of transition matrix π, and number of distributions D are not the same!")
    return true
end

issquare(A::AbstractMatrix) = size(A,1) == size(A,2)
istransmat(A::AbstractMatrix) = issquare(A) && all([isprobvec(A[i,:]) for i in 1:size(A,1)])


text:
using MagneticReadHead
using MagneticReadHead: new_debug_ctx
using Cassette
using Test

using InteractiveUtils

# This test we are keeping around just to demonstrate how to
# check on when things go wrong
@testset "_totuple" begin
    ctx = new_debug_ctx()
    ir = @code_lowered Cassette.recurse(ctx, Base._totuple, Tuple, 20)
    #@show ir
    #@show ir.codelocs
    errors = Core.Compiler.validate_code(ir)
    @test errors == []
    res = Cassette.recurse(ctx, Base._totuple, Tuple, 20)
    @test res == (20,)
end

@testset "Normal things should not error" begin
    normal_codes = (
        (fn = sum, args=([1,2,3,4],)),
        (fn = identity, args=(302,)),
        (fn = fill!, args=([1.0, 2.0], 0.0)),
        (fn = Base._totuple, args=(Tuple, 41)),
    )
    for code in normal_codes
        ctx = new_debug_ctx()
        expected = code.fn(code.args...)
        direct_recurse = Cassette.recurse(ctx, code.fn, code.args...)
        @test expected == direct_recurse
        indirect_recurse = Cassette.recurse(ctx, () -> code.fn(code.args...))
        @test expected == indirect_recurse
    end
end

function boopa!(x,y)
    x[1]=y
    return x
end

@testset "basic mutating function" begin
    ctx = new_debug_ctx()
    res = Cassette.recurse(ctx, boopa!, [1,2], 3)
    @test res == [3,2]

    @testset "function calling a basic mutating function" begin
        ctx = new_debug_ctx()
        res = Cassette.recurse(ctx, ()->boopa!([1,2],4))
        @test res == [4,2]
    end
end

@testset "let blocks in branches" begin
    # this is the breaking case that means we need to check when variables come into scope

    function danger11(x)
        if x==1
            y=1
            return x
        elseif x==2
            let
                m=2
                x+=m
            end
            return 2x
        end
        return x
    end

    ctx = new_debug_ctx()
    res = Cassette.recurse(ctx, danger11, 1)
    @test res == 1
end

@testset "Closures that modify outer content" begin
    function danger19()
        y=2
        function inner()
            h=y
            y=12
            return h
        end
        inner()
    end

    ctx = new_debug_ctx()
    res = Cassette.recurse(ctx, danger19)
    @test res == 2
end

@testset "TimerOutputs based breaking case" begin
    # This is the case from https://github.com/oxinabox/MagneticReadHead.jl/issues/56
    # if that was not fixed then the `@run` line will cause errors
    iob = IOBuffer()
    @run TimerOutputs.print_header(iob, 0.5, 0.1, 3, 3, 7, false, true, 11, true, "oio")
    @test !isempty(String(take!(iob)))
end


text:
module PiecewiseLinearApprox

include("piecewise_linear.jl")

export piecewise_linear_nodes
export linear_nodes
export piecewise_linear_evaluate

end # module


text:
# Automatically generated using Clang.jl
# Some customization for opaque struct types by Jeff Harrison.

const TREE_SITTER_LANGUAGE_VERSION = 11
const TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION = 9
const TSSymbol = UInt16
const TSFieldId = UInt16
mutable struct TSLanguage end
mutable struct TSParser end
mutable struct TSTree end
mutable struct TSQuery end
mutable struct TSQueryCursor end

@cenum TSInputEncoding::UInt32 begin
    TSInputEncodingUTF8 = 0
    TSInputEncodingUTF16 = 1
end

@cenum TSSymbolType::UInt32 begin
    TSSymbolTypeRegular = 0
    TSSymbolTypeAnonymous = 1
    TSSymbolTypeAuxiliary = 2
end

struct TSPoint
    row::UInt32
    column::UInt32
end

struct TSRange
    start_point::TSPoint
    end_point::TSPoint
    start_byte::UInt32
    end_byte::UInt32
end

struct TSInput
    payload::Ptr{Cvoid}
    read::Ptr{Cvoid}
    encoding::TSInputEncoding
end

@cenum TSLogType::UInt32 begin
    TSLogTypeParse = 0
    TSLogTypeLex = 1
end

struct TSLogger
    payload::Ptr{Cvoid}
    log::Ptr{Cvoid}
end

struct TSInputEdit
    start_byte::UInt32
    old_end_byte::UInt32
    new_end_byte::UInt32
    start_point::TSPoint
    old_end_point::TSPoint
    new_end_point::TSPoint
end

struct TSNode
    context::NTuple{4,UInt32}
    id::Ptr{Cvoid}
    tree::Ptr{TSTree}
end

struct TSTreeCursor
    tree::Ptr{Cvoid}
    id::Ptr{Cvoid}
    context::NTuple{2,UInt32}
end

struct TSQueryCapture
    node::TSNode
    index::UInt32
end

struct TSQueryMatch
    id::UInt32
    pattern_index::UInt16
    capture_count::UInt16
    captures::Ptr{TSQueryCapture}
end

@cenum TSQueryPredicateStepType::UInt32 begin
    TSQueryPredicateStepTypeDone = 0
    TSQueryPredicateStepTypeCapture = 1
    TSQueryPredicateStepTypeString = 2
end

struct TSQueryPredicateStep
    type::TSQueryPredicateStepType
    value_id::UInt32
end

@cenum TSQueryError::UInt32 begin
    TSQueryErrorNone = 0
    TSQueryErrorSyntax = 1
    TSQueryErrorNodeType = 2
    TSQueryErrorField = 3
    TSQueryErrorCapture = 4
end


text:
# Note (1). See
# https://discourse.julialang.org/t/weight-regularisation-which-iterates-params-m-in-flux-mutating-arrays-is-not-supported/64314

""" Penalizer(λ, α)

Returns a callable object `penalizer` for evaluating regularization
penalties associated with some numerical array. Specifically,
`penalizer(A)` returns

   λ*(α*L1 + (1 - α)*L2),

where `L1` is the sum of absolute values of the elments of `A` and
`L2` is the sum of squares of those elements.
"""
struct Penalizer{T}
    lambda::T
    alpha::T
    function Penalizer(lambda, alpha)
        lambda == 0 && return new{Nothing}(nothing, nothing)
        T = promote_type(typeof.((lambda, alpha))...)
        return new{T}(lambda, alpha)
    end
end

(::Penalizer{Nothing})(::Any) = 0
function (p::Penalizer)(A)
    λ = p.lambda
    α = p.alpha
    # avoiding broadcasting; see Note (1) above
    L2 = sum(abs2, A)
    L1 = sum(abs,  A)
    return  λ*(α*L1 + (1 - α)*L2)
end

"""
    PenalizedLoss(model, chain)

Returns a callable object `p`, for returning the penalized loss on
some batch of data `(x, y)`. Specifically, `p(x, y)` returns

   loss(chain(x), y) + sum(Penalizer(λ, α).(params(chain)))

where `loss = model.loss`, `α = model.alpha`, `λ = model.lambda`.

See also [`Penalizer`](@ref)

"""
struct PenalizedLoss{P}
    loss
    penalizer::P
    chain
    params
    function PenalizedLoss(model, chain)
        loss = model.loss
        penalizer = Penalizer(model.lambda, model.alpha)
        params = Flux.params(chain)
        return new{typeof(penalizer)}(loss, penalizer, chain, params)
    end
end
(p::PenalizedLoss{Penalizer{Nothing}})(x, y) = p.loss(p.chain(x), y)
(p::PenalizedLoss)(x, y) = p.loss(p.chain(x), y) +
    sum(p.penalizer(θ) for θ in p.params)


text:
@testset "LUGS" begin
  𝒮 = georef((z=[0.,1.,0.,1.,0.],), [0. 25. 50. 75. 100.])
  𝒟 = RegularGrid(100)

  # ----------------------
  # conditional simulation
  # ----------------------
  problem = SimulationProblem(𝒮, 𝒟, :z, 2)
  solver = LUGS(:z => (variogram=SphericalVariogram(range=10.),))

  Random.seed!(2018)
  solution = solve(problem, solver)

  if visualtests
    @plottest plot(solution,size=(600,400),layout=(2,1)) joinpath(datadir,"LU-condsim.png") !isCI
  end

  # ------------------------
  # unconditional simulation
  # ------------------------
  problem = SimulationProblem(𝒟, :z=>Float64, 2)
  solver = LUGS(:z => (variogram=SphericalVariogram(range=10.),))

  Random.seed!(2018)
  solution = solve(problem, solver)

  if visualtests
    @plottest plot(solution,size=(600,400),layout=(2,1)) joinpath(datadir,"LU-uncondsim.png") !isCI
  end

  # -------------
  # co-simulation
  # -------------
  𝒟 = RegularGrid(500)
  problem = SimulationProblem(𝒟, (:z=>Float64,:y=>Float64), 1)
  solver = LUGS(:z => (variogram=SphericalVariogram(range=10.),),
                :y => (variogram=GaussianVariogram(range=10.),),
                (:z,:y) => (correlation=0.95,))

  Random.seed!(2020)
  solution = solve(problem, solver)

  if visualtests
    @plottest plot(solution,size=(600,400),layout=(2,1)) joinpath(datadir,"LU-cosim.png") !isCI
  end
end


text:
module GradientStorageTests

using Test
using VarianceReducedSG
using LinearAlgebra
using SparseArrays
using Random
Random.seed!(0)

using VarianceReducedSG:
	reset!,
	grad!,
	vrgrad!,
	innov!,
	grad_approx!,
	store_from_point!,
	store_from_grad!,
	store_from_innov!,
	vrgrad_store!

function allsame(g1, g2)
	length(g1) != length(g2) && return false
	length(g1[1]) != length(g2[1]) && return false

	dim = length(g1[1])
	n = length(g1)

	pass = true
	for i = 1:n
		test = isapprox(g1[i], g2[i])
		pass = pass && test
	end
	pass
end

function allsameexcept(g1, g2, ids)
	length(g1) != length(g2) && return false
	length(g1[1]) != length(g2[1]) && return false

	dim = length(g1[1])
	n = length(g1)

	pass = true
	for i in 1:n
		test = isapprox(g1[i], g2[i])
		if i in ids
			pass = pass && !test
		else
			pass = pass && test
		end
	end
	pass
end

function resetstorereset(g1, g2, n, dim)
	reset!(g1)
	reset!(g2)

	@test allsame(g1, g2)

	x = randn(dim)
	i = rand(1:n)
	store_from_point!(g1,x,i)
	@test allsameexcept(g1, g2, i)

	ids = rand(1:n, Int(ceil(n/10)))
	store_from_point!(g1,x,ids)
	@test allsameexcept(g1, g2, [i;ids])

	store_from_point!(g1,x)
	@test allsameexcept(g1, g2, 1:n)

	reset!(g1)
	@test allsame(g1, g2)

	grad = randn(dim)
	i1 = rand(1:n)
	state = grad!(grad,g1,x,i1)
	store_from_grad!(g1,grad,state)
	@test allsameexcept(g1, g2, i1)

	res = zeros(dim)
	i2 = rand(1:n)
	state = innov!(res,g1,x,i2)
	store_from_innov!(g1,res,state)
	@test allsameexcept(g1, g2, [i1, i2])

	reset!(g1)
	@test allsame(g1, g2)
end

function nonmodifying_calls(g1, g2, n, dim)
	reset!(g1)
	reset!(g2)

	res = randn(dim)
	x = randn(dim)
	x_ctrl = copy(x)

	grad!(res, g1, x, rand(1:n))
	@test allsame(g1, g2)
	@test x == x_ctrl

	grad_approx!(res, g1)
	@test allsame(g1, g2)

	vrgrad!(res, g1, x, rand(1:n), randn())
	@test allsame(g1, g2)
	@test x == x_ctrl

	length(g1)
	@test allsame(g1, g2)

	g1[rand(1:n)]
	@test allsame(g1, g2)
end

function modifying_calls(g1, g2, n, dim)
	reset!(g1)
	reset!(g2)

	x = randn(dim)

	res = copy(x)
	i = rand(1:n)
	mod_ids = [i]

	vrgrad_store!(res, g1, x, i, randn())
	@test allsameexcept(g1, g2, mod_ids)
	@test !isapprox(res, x)

	res = copy(x)
	i = rand(1:n)
	mod_ids = [mod_ids; i]

	vrgrad_store!(res, g1, x, i, randn())
	@test allsameexcept(g1, g2, mod_ids)
	@test !isapprox(res, x)

	reset!(g1)
	@test allsame(g1, g2)
end

function correct_grad(g, n, dim, f)
	# Scramble memories a little
	store_from_point!(g, randn(dim))
	store_from_point!(g, randn(dim), rand(1:n, Int(ceil(n/7))))

	x1 = randn(dim)
	i1 = rand(1:n)
	ctrl1 = similar(x1)
	f(ctrl1,x1,i1)

	x2 = randn(dim)
	i2 = rand(1:n)
	ctrl2 = similar(x2)
	f(ctrl2,x2,i2)

	res = randn(dim)
	grad!(res, g, x1, i1)
	@test isapprox(res, ctrl1)

	grad!(res, g, x2, i2)
	@test isapprox(res, ctrl2)

	res = randn(dim)
	grad!(res, g, x1, i1)
	@test isapprox(res, ctrl1)

	grad!(res, g, x2, i2)
	@test isapprox(res, ctrl2)

	x = randn(dim)
	i = rand(1:n)

	res1 = randn(dim)
	res2 = randn(dim)
	grad!(res1, g, x, i)
	grad!(res2, g, x, i)
	@test isapprox(res1, res2)

	res1 = randn(dim)
	res2 = randn(dim)
	grad!(res1, g, x, i)
	grad!(res2, g, x, i)
	@test isapprox(res1, res2)
end

function correct_approximation(g, n, dim, f)
	# Uniform storage
	x = randn(dim)

	ctrl = zeros(dim)
	buf = similar(x)
	for i = 1:n
		f(buf, x, i)
		ctrl .+= buf./n
	end

	store_from_point!(g, x)

	x_res = randn(dim)
	grad_approx!(x_res, g)
	@test isapprox(x_res, ctrl)

	vrgrad!(x_res, g, x, rand(1:n), randn())
	@test isapprox(x_res, ctrl)

	vrgrad_store!(x_res, g, x, rand(1:n), randn())
	@test isapprox(x_res, ctrl)

	grad_approx!(x_res, g)
	@test isapprox(x_res, ctrl)

	vrgrad!(x_res, g, x, rand(1:n), randn())
	@test isapprox(x_res, ctrl)

	# Non-uniform storage
	old_approx = randn(dim)
	grad_approx!(old_approx, g)

	x = randn(dim)
	i = rand(1:n)

	old_y = copy(g[i])
	new_y = zeros(dim)
	f(new_y, x, i)

	store_from_point!(g, x, i)

	new_approx = randn(dim)
	grad_approx!(new_approx, g)
	@test isapprox(new_approx, old_approx + (new_y - old_y)/n)

	old_approx = new_approx

	x = randn(dim)
	i = rand(1:n)
	old_y = copy(g[i])
	new_y = zeros(dim)
	f(new_y, x, i)

	vrgrad_store!(zeros(dim), g, x, i, randn())

	new_approx = randn(dim)
	vrgrad!(new_approx, g, randn(dim), rand(1:n), 0)
	@test isapprox(new_approx, old_approx + (new_y - old_y)/n)

	old_approx = new_approx

	x = randn(dim)
	i = rand(1:n)
	old_y = copy(g[i])
	new_y = zeros(dim)
	f(new_y, x, i)

	grad = randn(dim)
	state = grad!(grad, g, x, i)
	@test isapprox(grad, new_y)
	store_from_grad!(g, grad, state)

	new_approx = randn(dim)
	grad_approx!(new_approx, g)
	@test isapprox(new_approx, old_approx + (new_y - old_y)/n)

	old_approx = new_approx

	x = randn(dim)
	i = rand(1:n)
	old_y = copy(g[i])
	new_y = zeros(dim)
	f(new_y, x, i)

	innov = randn(dim)
	state = innov!(innov,g,x,i)
	@test isapprox(innov, new_y - old_y)
	store_from_innov!(g, innov, state)

	new_approx = randn(dim)
	vrgrad_store!(new_approx, g, randn(dim), rand(1:n), 0)
	@test isapprox(new_approx, old_approx + (new_y - old_y)/n)
end

function correct_innovation(g, n, dim, f)
	x = randn(dim)
	store_from_point!(g, x)

	res = randn(dim)
	res_app = randn(dim)
	grad_approx!(res_app, g)
	vrgrad!(res, g, x, rand(1:n), randn())
	@test isapprox(res, res_app)

	res = randn(dim)
	innov!(res,g,x,rand(1:n))
	@test isapprox(res, zeros(dim), atol=eps()^(2/3))

	res = randn(dim)
	res_app = randn(dim)
	grad_approx!(res_app, g)
	vrgrad_store!(res, g, x, rand(1:n), randn())
	@test isapprox(res, res_app)

	res = randn(dim)
	res_app = randn(dim)
	grad_approx!(res_app, g)
	vrgrad!(res, g, x, rand(1:n), randn())
	@test isapprox(res, res_app)

	x = randn(dim)
	i = rand(1:n)
	old_y = copy(g[i])
	new_y = zeros(dim)
	f(new_y, x, i)

	step = randn()
	res = randn(dim)
	res_app = randn(dim)
	grad_approx!(res_app, g)
	vrgrad!(res, g, x, i, step)
	@test isapprox(res, res_app + step*(new_y - old_y))

	res = randn(dim)
	innov!(res, g, x, i)
	@test isapprox(res, new_y - old_y)

	step = randn()
	res = zeros(dim)
	res_app = randn(dim)
	grad_approx!(res_app, g)
	vrgrad_store!(res, g, x, i, step)
	@test isapprox(res, res_app + step*(new_y - old_y))

	step = randn()
	res = zeros(dim)
	res_app = randn(dim)
	grad_approx!(res_app, g)
	vrgrad!(res, g, x, i, step)
	@test isapprox(res, res_app)

	x = randn(dim)
	i = rand(1:n)
	step_innv = randn()

	vr_ctrl = zeros(dim)
	tmp = zeros(dim)
	vr_ctrl .-= step_innv.*g[i]
	grad!(tmp, g, x, i)
	vr_ctrl .+= step_innv.*tmp
	grad_approx!(tmp, g)
	vr_ctrl .+= tmp

	vr = randn(dim)
	vrgrad!(vr, g, x, i, step_innv)
	@test isapprox(vr, vr_ctrl)

	res = copy(x)
	vrgrad_store!(res, g, x, i, step_innv)
	@test isapprox(res, vr_ctrl)

	x = randn(dim)
	i = rand(1:n)
	old_y = copy(g[i])
	new_y = zeros(dim)
	f(new_y, x, i)

	res = randn(dim)
	state = innov!(res, g, x, i)
	@test isapprox(res, new_y - old_y)
	store_from_innov!(g, res, state)
	state = innov!(res, g, x, i)
	@test isapprox(res, zeros(dim), atol=eps()^(2/3))
end

function equivalent(g1, g2, n, dim; uniform_store = false)
	x = randn(dim)
	store_from_point!(g1, x)
	store_from_point!(g2, x)
	@test allsame(g1,g2)

	if !uniform_store
		x = randn(dim)
		ids = 1:3:n
		store_from_point!(g1, x, ids)
		store_from_point!(g2, x, ids)
		@test allsame(g1,g2)

		x = randn(dim)
		i = rand(1:n)
		innv = randn()

		res1 = randn(dim)
		res2 = randn(dim)
		x_ctrl = copy(x)
		vrgrad_store!(res1, g1, x, i, innv)
		vrgrad_store!(res2, g2, x, i, innv)
		@test isapprox(res1, res2)
		@test isapprox(x, x_ctrl)

		x = randn(dim)
		i = rand(1:n)

		res1 = randn(dim)
		res2 = randn(dim)
		state1 = grad!(res1, g1, x, i)
		state2 = grad!(res2, g2, x, i)
		store_from_grad!(g1, res1, state1)
		store_from_grad!(g2, res2, state2)
		@test allsame(g1,g2)

		x = randn(dim)
		i = rand(1:n)

		res1 = randn(dim)
		res2 = randn(dim)
		state1 = innov!(res1, g1, x, i)
		state2 = innov!(res2, g2, x, i)
		store_from_innov!(g1, res1, state1)
		store_from_innov!(g2, res2, state2)
		@test allsame(g1,g2)
	end

	x = randn(dim)
	i = rand(1:n)

	res1 = randn(dim)
	res2 = randn(dim)
	grad!(res1, g1, x, i)
	grad!(res2, g2, x, i)
	@test isapprox(res1, res2)

	res1 = randn(dim)
	res2 = randn(dim)
	grad_approx!(res1, g1)
	grad_approx!(res2, g2)
	@test isapprox(res1, res2)

	x = randn(dim)
	i = rand(1:n)

	res1 = randn(dim)
	res2 = randn(dim)
	innov!(res1, g1, x, i)
	innov!(res2, g2, x, i)
	@test isapprox(res1, res2)

	x = randn(dim)
	i = rand(1:n)
	innv = randn()

	res1 = randn(dim)
	res2 = randn(dim)
	x_ctrl = copy(x)
	vrgrad!(res1, g1, x, i, innv)
	vrgrad!(res2, g2, x, i, innv)
	@test isapprox(res1, res2)
	@test isapprox(x, x_ctrl)
end

function allocation(g,n,dim; uniform_store=false)
	length(g) #run once to make sure it is compiled
	@test 0 == @allocated length(g)
	#g[1]
	#@test 0 == @allocated g[1]
	reset!(g)
	@test 0 == @allocated reset!(g)

	x = randn(dim)
	res = randn(dim)
	ids = 1:3:n
	i = rand(1:n)
	step_innv = randn()

	grad = randn(dim)
	gstate = grad!(grad, g, x, i)
	#@test 0 == @allocated gstate = grad!(grad, g, x, i)
	@test 0 == @allocated grad!(grad, g, x, i)

	vrgrad!(res, g, x, i, step_innv)
	@test 0 == @allocated vrgrad!(res, g, x, i, step_innv)

	innov = randn(dim)
	istate = innov!(innov, g, x, i)
	#@test 0 == @allocated istate = innov!(innov, g, x, i)
	@test 0 == @allocated innov!(innov, g, x, i)

	grad_approx!(res, g)
	@test 0 == @allocated grad_approx!(res, g)

	if uniform_store
		store_from_point!(g,x)
		@test 0 == @allocated store_from_point!(g,x)
	else
		store_from_point!(g,x)
		@test 0 == @allocated store_from_point!(g,x)

		store_from_point!(g,x,ids)
		@test 0 == @allocated store_from_point!(g,x,ids)

		store_from_point!(g, x, i)
		@test 0 == @allocated store_from_point!(g, x, i)

		vrgrad_store!(res, g, x, i ,step_innv)
		@test 0 == @allocated vrgrad_store!(res, g, x, i, step_innv)

		store_from_grad!(g, grad, gstate)
		@test 0 == @allocated store_from_grad!(g, grad, gstate)

		store_from_innov!(g, innov, istate)
		@test 0 == @allocated store_from_innov!(g, innov, istate)
	end
end

@testset "VRGradient" begin
	isq(y,x,i) = (y .= i.*x.^2)

	for dim in [1,101], n in [1,313]
		g1 = VRGradient(isq, zeros(dim), n)
		g2 = VRGradient(isq, zeros(dim), n)

		@test allsame(g1,g2)
		@test length(g1) == n
		@test length(g2) == n

		resetstorereset(g1, g2, n, dim)

		nonmodifying_calls(g1, g2, n, dim)
		modifying_calls(g1, g2, n, dim)

		correct_grad(g1, n, dim, isq)
		correct_approximation(g1, n, dim, isq)
		correct_innovation(g1, n, dim, isq)

		allocation(g1, n, dim)
	end
end

@testset "LinearVRG" begin
	sq(x, i) = i*x^2

	for dim in [1,101], n in [1,313]
		data = [randn(dim) for _ = 1:n]
		nzero = Int(ceil(dim/17))
		for d in data
			d[rand(1:dim, dim - nzero)] .= 0.0
		end

		isq(y,x,i) = (y .= data[i]*sq(dot(data[i],x), i))

		g1 = LinearVRG(sq, data, zeros(dim))
		g2 = LinearVRG(sq, data, zeros(dim))

		@test allsame(g1,g2)
		@test length(g1) == n
		@test length(g2) == n

		resetstorereset(g1, g2, n, dim)

		nonmodifying_calls(g1, g2, n, dim)
		modifying_calls(g1, g2, n, dim)

		correct_grad(g1, n, dim, isq)
		correct_approximation(g1, n, dim, isq)
		correct_innovation(g1, n, dim, isq)

		g_ctrl = VRGradient(isq, zeros(dim), n)
		equivalent(g1, g_ctrl, n, dim)

		allocation(g1, n, dim)
	end
end

@testset "LinearVRG - Sparse Data" begin
	sq(x, i) = i*x^2

	for dim in [1,101], n in [1,313]
		spdata = [(d = sprandn(dim, 0.5); d[1] = randn(); d) for _ = 1:n]
		data = [ Array(spd) for spd in spdata]

		isq(y,x,i) = (y .= data[i]*sq(dot(data[i],x), i))

		g1 = LinearVRG(sq, spdata, zeros(dim))
		g2 = LinearVRG(sq, spdata, zeros(dim))

		@test allsame(g1,g2)
		@test length(g1) == n
		@test length(g2) == n

		resetstorereset(g1, g2, n, dim)

		nonmodifying_calls(g1, g2, n, dim)
		modifying_calls(g1, g2, n, dim)

		correct_grad(g1, n, dim, isq)
		correct_approximation(g1, n, dim, isq)
		correct_innovation(g1, n, dim, isq)

		gd = LinearVRG(sq, data, zeros(dim))
		equivalent(g1, gd, n, dim)

		allocation(g1, n, dim)
	end
end

@testset "UniformVRG" begin
	isq(y,x,i) = (y .= i.*x.^2)

	for dim in [1,101], n in [1,313]
		g1 = UniformVRG(isq, zeros(dim), n)
		g2 = UniformVRG(isq, zeros(dim), n)

		@test allsame(g1,g2)
		@test length(g1) == n
		@test length(g2) == n

		x = randn(dim)
		store_from_point!(g1,x)
		store_from_point!(g2,x)
		@test allsame(g1,g2)

		reset!(g1)
		reset!(g2)
		@test allsame(g1,g2)

		nonmodifying_calls(g1, g2, n, dim)

		g_ctrl = VRGradient(isq, zeros(dim), n)
		equivalent(g1, g_ctrl, n, dim, uniform_store = true)

		allocation(g1, n, dim, uniform_store = true)

		x = randn(dim)
		i = rand(1:n)
		res = randn(dim)
		innv = randn()
		@test_throws ErrorException store_from_point!(g1, x, i)
		@test_throws ErrorException store_from_grad!(g1, x, i)
		@test_throws ErrorException store_from_innov!(g1, x, i)
		@test_throws ErrorException vrgrad_store!(res, g1, x, i, innv)
	end
end

end # module


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

#doc
"""
    calculate_localmode(hist)

*BAT-internal, not part of stable public API.*

Calculates the modes of a 1d statsbase histogram.
A vector containing the bin-center(s) of the heighest bin(s) is returned.
"""
function calculate_localmode(hist)
    maxima = maximum(hist.weights)
    maxima_idx = findall(x->x==maxima, hist.weights)
    edges = collect(hist.edges[1])

    modes = Vector{Real}(undef, length(maxima_idx))
    for (i, ind) in enumerate(maxima_idx)
        modes[i] = edges[ind] + 0.5 *abs(edges[ind+1] - edges[ind])
    end

    return modes
end

#todo
function calculate_localmode_2d(hist)
    maxima = maximum(hist.weights)
    maxima_idx = findall(x->x==maxima, hist.weights)
    edges = [collect(hist.edges[1]), collect(hist.edges[2])]

    modes = Vector{Vector{Real}}(undef, length(maxima_idx))
    for (i, ind) in enumerate(maxima_idx)

        x = edges[1][ind[1]] + 0.5 *abs(edges[1][ind[1]+1] - edges[1][ind[1]])
        y = edges[2][ind[2]] + 0.5 *abs(edges[2][ind[2]+1] - edges[2][ind[2]])
       modes[i] = [x , y]
    end

    return modes
end


text:
ROS.@include actionlib_tutorials: FibonacciAction, FibonacciFeedback, FibonacciResult, FibonacciGoal

function testActionClient()
    ROS.init("testActionlibClient")
    nh = ROS.NodeHandle()

    action_client = ROS.actionlib.SimpleActionClient("test_action_server", ROS.actionlib_tutorials_FibonacciAction, true)
    #  ROS.actionlib.waitForServer(action_client)
    ROS.actionlib.waitForServer(action_client, ROS.Duration(120))
    ROS.actionlib.sendGoal(action_client, ROS.actionlib_tutorials_FibonacciGoal())
    ROS.actionlib.waitForResult(action_client, ROS.Duration(3))
    ROS.actionlib.sendGoalAndWait(action_client, ROS.actionlib_tutorials_FibonacciGoal(), ROS.Duration(3), ROS.Duration(5))

    action_result_called = ROS.actionlib.getResult(action_client)
    ROS.actionlib.cancelAllGoals(action_client)
    ROS.actionlib.cancelGoal(action_client)
    state = ROS.actionlib.getState(action_client)
    ROS.actionlib.getText(state)
    ROS.actionlib.isDone(state)
    ROS.actionlib.toString(state)
    ROS.actionlib.isServerConnected(action_client)

    ROS.actionlib.stopTrackingGoal(action_client)
    ROS.shutdown(nh)
    println("All $(basename(@__FILE__)) tests passed.")
end


text:
using SimpleWeightedGraphs
using SparseArrays

function rand_network(nodeNumber::Int, density::Float64; weights = 1:10)
    adj_matrix = zeros(nodeNumber, nodeNumber)
    for row in 1:nodeNumber
        for element in 1:nodeNumber
            if rand() < density && (element != row)
                wert = rand(weights)
                adj_matrix[row, element] = wert
                adj_matrix[element, row] = wert
            end
        end
    end
    SimpleWeightedGraph(adj_matrix)
end

function rand_directed_network(nodeNumber::Int, density::Float64; weights = 1:10)
    adj_matrix = zeros(nodeNumber, nodeNumber)
    for row in 1:nodeNumber
        for element in 1:nodeNumber
            if rand() < density && (element != row)
                wert = rand(weights)
                adj_matrix[row, element] = wert
            end
        end
    end
    SimpleWeightedDiGraph(adj_matrix)
end

function rand_dag(N::Int, p::Float64; weights = 1:10)

    # schuffel vertiges
    dag = zeros(N, N)
    sink = Random.shuffle!(collect(1:N))

    # randomly connect i>j nodes:
    L = length(sink)
    for k = 1:L
        nArcs = trunc(Int, round(p * (L - k)))
        idx = shuffle!(findall(x -> x > k, (1:L)))
        for l = 1:nArcs
            dag[sink[k], sink[idx[l]]] = rand(weights)
        end
    end

    return SimpleWeightedDiGraph(dag) # stores transposed
end

"""
    mk_connected_layered_dag(n, m, p = 0.3; super = true, weights = 1:1)

Generator for a random, connected and weighted DAG. Per default a super-source and super-sink is added.

#### Arguments
- `n::Int64`: number of layers.
- `m:Int64`: number of maximum node per layer.
- `p::Float64`: probability for short-cuts
- (optional) `super = true`: If true, than add super-source and super-sink to network.
- (optional) `weights=1:1`: add random (Float64) weights to your edges.
"""
function mk_connected_layered_dag(n::Int64, m::Int64, p = 0.3::Float64; super = true, weights = 1:1)
    s = 0
    (super) ? super_sink_source = 1 : super_sink_source = 0

    # create layers
    L = []

    if super_sink_source == 1
        push!(L, [1])
        s = 1
    end
    for i = 1:n
        layer = collect(1+s:rand(1+s:m+s))
        push!(L, layer)
        s = maximum(layer)
    end
    n_nodes = maximum(maximum(L))
    (super_sink_source == 1) ? push!(L, [n_nodes + 1]) : nothing

    # @show L

    # create adjacency matrix:
    adj = SparseMatrixCSC(zeros(n_nodes + super_sink_source, n_nodes + super_sink_source))

    # connect super_sink to layer-1
    if super_sink_source == 1
        for i = 1+super_sink_source:maximum(L[2])
            adj[1, i] = rand(weights)
        end
    end

    # inter node completation:
    n = n + 2 * super_sink_source
    for k = (1+super_sink_source):n
        for i = minimum(L[k]):maximum(L[k])

            if (k < n)
                # inter layer !
                for j = 1:length(L[k])
                    ls = Random.shuffle!(L[k+1])
                    adj[L[k][j], ls[1]] = rand(weights)
                end

                # inter network:
                if (rand() < p)

                    kj = collect(minimum(L[k]):maximum(L[n]))
                    cs = Random.shuffle!(kj)
                    (cs[1] != i) ? adj[i, cs[1]] = rand(weights) : nothing
                end
            end

            # no empty parents !
            if isempty(WeightedNetwork.parents(adj, i)) && (i != 1)
                dd = Random.shuffle!(L[k-1])
                adj[dd[1], i] = rand(weights)
            end

        end
    end

    if super_sink_source == 1
        for k = (1+super_sink_source):n-1
            for i = minimum(L[k]):maximum(L[k])
                childs = WeightedNetwork.children(adj, i)
                if isempty(childs)
                    adj[i, n_nodes+1] = rand(weights)
                end
            end
        end
    end

    return SimpleWeightedDiGraph(adj')

end

max_stars_repo_name: tragisch/NetworkOptimization

max_issues_repo_name: tragisch/NetworkOptimization

max_forks_repo_name: tragisch/NetworkOptimization


text:
using Test
using QuantumLattices.Prerequisites

@testset "decimaltostr" begin
    @test decimaltostr(:a)==":a"
    @test decimaltostr(1)=="1"
    @test decimaltostr(10^6)=="1000000"
    @test decimaltostr(1//7)=="1//7"
    @test decimaltostr(1.0)=="1.0"
    @test decimaltostr(10.0^6)=="1.0e+06"
    @test decimaltostr(10^-5)=="1.0e-05"
    @test decimaltostr(1/7)=="0.14286"
    @test decimaltostr(1/7,8)=="0.14285714"
    @test decimaltostr(0im)=="0.0"
    @test decimaltostr(1//7+0im)=="1//7"
    @test decimaltostr(1.0im)=="1.0im"
    @test decimaltostr(-1.0im)=="-1.0im"
    @test decimaltostr(0.1+0.12im)=="0.1+0.12im"
    @test decimaltostr(0.1-0.12im)=="0.1-0.12im"
end

@testset "ordinal" begin
    @test ordinal(1)=="1st"
    @test ordinal(2)=="2nd"
    @test ordinal(3)=="3rd"
    @test ordinal(4)=="4th"
    @test ordinal(5)=="5th"
end

@testset "delta" begin
    @test delta(1,2)==0
    @test delta(1,1)==1
end

@testset "rawtype" begin
    @test Int|>rawtype==Int
    @test Vector{Int}|>rawtype==Array
    @test Vector|>rawtype==Array
    @test Vector{<:Real}|>rawtype==Array
end


text:
# subtypes should implement:
# transform_op(x, reg) -> LinearOperator
# projection_op(supp, reg) -> LinearOperator
# proximal!(x, thresh, reg) -> vector
# lipschitz_scale(reg) -> value
# support(x, reg) -> vector
# TODO: document above properly
abstract type Regularizer end

support(x, thresh, reg::Regularizer) = abs.(transform_op(reg) * x) .> thresh
proximal(x, thresh, reg::Regularizer) = proximal!(copy(x), thresh, reg) # fallback for out-of-place proximal

## L1 regularizer
struct L1 <: Regularizer
    size::Int
end

support(x, reg::L1) = support(x, 1e-3, reg)
lipschitz_scale(::L1) = 2.

# proximal

softthresh(val, thresh) = max(abs(val) - thresh, 0) .* sign(val)
function proximal!(x, thresh, reg::L1)
    x .= softthresh.(x, thresh)
    x
end

# transform

transform_op(reg::L1) = Identity(reg.size)

# projection

struct L1Project <: LinearMap{Float64}
    p::Int
    suppinv::AbstractVector{<:Int}
end

Base.size(P::L1Project) = (sum(P.suppinv .> 0), P.p)
L1ProjectTranspose = LinearMaps.TransposeMap{<:Any, <:L1Project}

function L1Project(p::Int, supp::AbstractVector{Bool})
    suppinv = zeros(Int, p)
    index = 1
    for i in 1:p
        if supp[i]
            suppinv[i] = index
            index += 1
        end
    end
    L1Project(p, suppinv)
end

Base.:(*)(P::L1Project, x::AbstractVector) = x[P.suppinv .> 0]
Base.:(*)(Pt::L1ProjectTranspose, y::AbstractVector) = [Pt.lmap.suppinv[i] > 0 ? y[Pt.lmap.suppinv[i]] : 0.0 for i in 1:Pt.lmap.p] # TODO: use zero element instead?
mul!(uflat::AbstractVecOrMat, P::L1Project, yflat::AbstractVector) = (uflat[:] = P * yflat)
mul!(uflat::AbstractVecOrMat, Pt::L1ProjectTranspose, yflat::AbstractVector) = (uflat[:] = Pt * yflat)

projection_op(supp, reg::L1) = L1Project(reg.size, supp)

## TV regularizer

# TODO: type-genericness?
struct TVProximalWork
    Δ::AbstractArray{Float64}
    diff::AbstractArray{Float64}
end

struct TV <: Regularizer
    size::Tuple
    wrap::Bool
    work::TVProximalWork
end

TV(size::Tuple) = TV(size, true)
function TV(size::Tuple, wrap::Bool)
    Δ = Array{Float64}(undef, size)
    diff = Array{Float64}(undef, size)
    TV(size, wrap, TVProximalWork(Δ, diff))
end

support(x, reg::TV) = support(x, 0.005, reg)
lipschitz_scale(::TV) = 50

# proximal

function proximal!(x, thresh, reg::TV)
    x = reshape(x, reg.size)
    D = length(reg.size)
    Δ = reg.work.Δ
    diff = reg.work.diff
    Δ .= 0
    for i in 1:D
        xroll = roll(x, -1, i)
        diff .= (d -> min(2 * D * thresh, abs(d) / 2) * sign(d)).(xroll .- x)
        Δ .+= diff ./ (2 * D)
        Δ .-= roll(diff, 1, i) ./  (2 * D)
    end
    x .+= Δ
    reshape(x, prod(reg.size))
end

function proximal2(x, thresh, reg::TV)
    y = x[:]
    proxTV!(y, thresh, shape=reg.size, iterations=20)
    y
end

# transform

roll(x, shift, dim) = ShiftedArrays.circshift(x, (zeros(dim-1)..., shift, zeros(ndims(x)-dim)...))

struct Gradient <: LinearMap{Float64}
    size::Tuple
    dim::Int
    wrap::Bool
end

Base.size(D::Gradient) = (prod(D.size), prod(D.size))
GradientTranspose = LinearMaps.TransposeMap{<:Any, <:Gradient}
function Base.:(*)(D::Gradient, xflat::AbstractVector)
    x = reshape(xflat, D.size)
    y = x .- roll(x, -1, D.dim)
    !D.wrap && (y[end] = 0)
    y[:]
end
function Base.:(*)(Dt::GradientTranspose, xflat::AbstractVector)
    D = Dt.lmap
    x = reshape(xflat, D.size)
    y = x .- roll(x, 1, D.dim)
    !D.wrap && (y[1] = 0)
    y[:]
end

mul!(uflat::AbstractVecOrMat, D::Gradient, yflat::AbstractVector) = (uflat[:] = D * yflat)
mul!(uflat::AbstractVecOrMat, Dt::GradientTranspose, yflat::AbstractVector) = (uflat[:] = Dt * yflat)

function transform_op(reg::TV)
    ndims = length(reg.size)
    vcat([Gradient(reg.size, dim, reg.wrap) for dim in 1:ndims]...) # TODO: (style) should I avoid these sort of splatted arrays?
end

# projection

# TODO (relevant everywhere): proper typing for things like 2D arrays
# TODO: type generic code (no Float64's)
struct TVProject{N} <: LinearMap{Float64}
    size::NTuple{N, Int}
    regions::AbstractVector{AbstractVector{CartesianIndex{N}}}
end

Base.size(P::TVProject) = (length(P.regions), prod(P.size))
TVProjectTranspose = LinearMaps.TransposeMap{<:Any, <:TVProject}

function TVProject(size::Tuple, regions::AbstractVector{AbstractVector{CartesianIndex}})
    TVProject{length(size)}(size, regions) # TODO: check if this what I'm supposed to do
end

# TODO: type support properly
function TVProject(size::Tuple, supp::AbstractVector{<:Bool}, wrap::Bool)
    ndims = length(size)
    explored = fill(false, size)
    regions = AbstractVector{CartesianIndex{ndims}}[]
    supp = reshape(supp, (size..., ndims))
    for start_pt in CartesianIndices(size)
        explored[start_pt] && continue
        region = [start_pt]
        explored[start_pt] = true;
        s = Stack{CartesianIndex{ndims}}();
        push!(s, start_pt)
        while !isempty(s)
            pt = pop!(s)
            pttup = Tuple(pt)
            for i in 1:ndims
                if wrap || pt[i] < size[i]
                    pt_right = CartesianIndex(pttup[1:i-1]..., 1 + mod(pt[i], size[i]), pttup[i+1:ndims]...)
                    if !supp[pt, i] && !explored[pt_right]
                        explored[pt_right] = true
                        push!(s, pt_right)
                        push!(region, pt_right)
                    end
                end
                if wrap || pt[i] > 1
                    pt_left = CartesianIndex(pttup[1:i-1]..., 1 + mod(pt[i]-2, size[i]), pttup[i+1:ndims]...)
                    if !supp[pt_left, i] && !explored[pt_left]
                        explored[pt_left] = true
                        push!(s, pt_left)
                        push!(region, pt_left)
                    end
                end
            end
        end
        push!(regions, region)
    end
    TVProject(size, regions)
end

function Base.:(*)(P::TVProject, x::AbstractVector)
    x = reshape(x, P.size)
    region_norm(region) = sum(x[pt] for pt in region) / sqrt(length(region))
    map(region_norm, P.regions)
end

function region_vals(P::TVProject, x::AbstractVector)
    x = reshape(x, P.size)
    function region_vals(region)
        vals = [x[pt] for pt in region]
        sum(vals) / length(region), maximum(vals) - minimum(vals), length(vals)
    end
    map(region_vals, P.regions)
end

function Base.:(*)(Pt::TVProjectTranspose, y::AbstractVector)
    P = Pt.lmap
    x = zeros(P.size)
    for (val, region) in zip(y, P.regions)
        x[region] .= val / sqrt(length(region))
    end
    x[:]
end

projection_op(supp, reg::TV) = TVProject(reg.size, supp, reg.wrap)
mul!(uflat::AbstractVecOrMat, P::TVProject, yflat::AbstractVector) = (uflat[:] = P * yflat)
mul!(uflat::AbstractVecOrMat, Pt::TVProjectTranspose, yflat::AbstractVector) = (uflat[:] = Pt * yflat)


text:
@doc raw"""
    RotationAction(
        M::Manifold,
        SOn::SpecialOrthogonal,
        AD::ActionDirection = LeftAction(),
    )

Space of actions of the [`SpecialOrthogonal`](@ref) group $\mathrm{SO}(n)$ on a
Euclidean-like manifold `M` of dimension `n`.
"""
struct RotationAction{TM<:Manifold,TSO<:SpecialOrthogonal,TAD<:ActionDirection} <:
       AbstractGroupAction{TAD}
    manifold::TM
    SOn::TSO
end

function RotationAction(
    M::Manifold,
    SOn::SpecialOrthogonal,
    ::TAD=LeftAction(),
) where {TAD<:ActionDirection}
    return RotationAction{typeof(M),typeof(SOn),TAD}(M, SOn)
end

function Base.show(io::IO, A::RotationAction)
    return print(io, "RotationAction($(A.manifold), $(A.SOn), $(direction(A)))")
end

const RotationActionOnVector{N,F,TAD} = RotationAction{
    <:Union{Euclidean{Tuple{N},F},TranslationGroup{Tuple{N},F}},
    SpecialOrthogonal{N},
    TAD,
}

base_group(A::RotationAction) = A.SOn

g_manifold(A::RotationAction) = A.manifold

function switch_direction(A::RotationAction{TM,TSO,TAD}) where {TM,TSO,TAD}
    return RotationAction(A.manifold, A.SOn, switch_direction(TAD()))
end

apply(A::RotationActionOnVector{N,F,LeftAction}, a, p) where {N,F} = a * p
function apply(A::RotationActionOnVector{N,F,RightAction}, a, p) where {N,F}
    return inv(base_group(A), a) * p
end

apply!(A::RotationActionOnVector{N,F,LeftAction}, q, a, p) where {N,F} = mul!(q, a, p)

function inverse_apply(A::RotationActionOnVector{N,F,LeftAction}, a, p) where {N,F}
    return inv(base_group(A), a) * p
end
inverse_apply(A::RotationActionOnVector{N,F,RightAction}, a, p) where {N,F} = a * p

apply_diff(A::RotationActionOnVector{N,F,LeftAction}, a, p, X) where {N,F} = a * X
function apply_diff(A::RotationActionOnVector{N,F,RightAction}, a, p, X) where {N,F}
    return inv(base_group(A), a) * X
end

function apply_diff!(A::RotationActionOnVector{N,F,LeftAction}, Y, a, p, X) where {N,F}
    return mul!(Y, a, X)
end
function apply_diff!(A::RotationActionOnVector{N,F,RightAction}, Y, a, p, X) where {N,F}
    return mul!(Y, inv(base_group(A), a), X)
end

function inverse_apply_diff(A::RotationActionOnVector{N,F,LeftAction}, a, p, X) where {N,F}
    return inv(base_group(A), a) * X
end
inverse_apply_diff(A::RotationActionOnVector{N,F,RightAction}, a, p, X) where {N,F} = a * X

function optimal_alignment(A::RotationActionOnVector{N,T,LeftAction}, p, q) where {N,T}
    is_manifold_point(A.manifold, p, true)
    is_manifold_point(A.manifold, q, true)

    Xmul = p * transpose(q)
    F = svd(Xmul)
    L = size(Xmul)[2]
    UVt = F.U * F.Vt
    Ostar = det(UVt) ≥ 0 ? UVt : F.U * Diagonal([i < L ? 1 : -1 for i in 1:L]) * F.Vt
    return convert(typeof(Xmul), Ostar)
end
function optimal_alignment(A::RotationActionOnVector{N,T,RightAction}, p, q) where {N,T}
    return optimal_alignment(switch_direction(A), q, p)
end


text:
####
#### tokenize
####

debug() = false

is_start_cond(nt, active_scope) =
    is_start_cond(grep_type(nt.fp), nt.flag, nt.ID, active_scope)

is_start_cond(gt::Type{GreedyType}, ::StartFlag, ID, active_scope) =
    isempty(active_scope) ? true : last(active_scope) ≠ ID
is_start_cond(gt, ::StopFlag, args...) = false
is_start_cond(gt, ::StartFlag, args...) = true

is_stop_cond(nt, active_scope) =
    is_stop_cond(grep_type(nt.fp), nt.flag, active_scope)
is_stop_cond(gt, ::StartFlag, active_scope) = false
is_stop_cond(gt, ::StopFlag, active_scope) =
    isempty(active_scope) ? false : true

function substr(s,i,j)
    try
      ss = SubString(s, i, j)
    catch
      # bad unicode ending, return
      # nothing for error handling.
      return nothing
    end
end

"""
    TokenStream(
        code::S,
        flag_set::FlagSet{FP}
    ) where {S<:AbstractString, FP<:FlagPair}

A dictionary whose keys are the flag IDs,
and whose values are vectors of `Int`s
indicating the nestedness of the flag.

The token stream is incremented
with a vector of Int's:

scope⁺          |  |      |
            |------------------------>
scope⁻                 |        |  |

                    ___    _____
                 __|   |__|     |__
            |   |                  |
scope       |------------------------>

"""
function TokenStream(
    code::S,
    flag_set::FlagSet{FP};
    rm_outer_bcs = false
  ) where {S<:AbstractString, FP<:FlagPair}

  @inbounds begin
    flag_pairs = flag_set.flag_pairs

    all_flags = NamedTuple[]
    for x in flag_pairs
      for (wb, trig) in x.start.trigger
        push!(all_flags, (fp=x, trig=trig, flag=x.start, ID=x.ID,
          len_b_left=length(wb[1]), len_b_right=length(wb[2])))
      end
      for (wb, trig) in x.stop.trigger
        push!(all_flags, (fp=x, trig=trig, flag=x.stop, ID=x.ID,
          len_b_left=length(wb[1]), len_b_right=length(wb[2])))
      end
    end
    greedy_config = any([grep_type(fp) isa GreedyType for fp in flag_pairs])

    # Convenience maps:
    ei = eachindex(code)
    i_max = max(ei...)
    ei_end = last(ei)
    token_stream = Dict{String,Vector{Int}}()
    token_stream⁺ = Dict{String,Vector{Int}}()
    token_stream⁻ = Dict{String,Vector{Int}}()
    @inbounds for fp in flag_pairs
      token_stream[fp.ID] = Int[0 for i in 1:i_max]
      token_stream⁺[fp.ID] = Int[0 for i in 1:i_max]
      token_stream⁻[fp.ID] = Int[0 for i in 1:i_max]
    end

    overlap_trigs = Dict{String,Bool}(nt.ID => false for nt in all_flags)
    for x in flag_pairs
      for trig in values(x.start.trigger)
        overlap_trigs[x.ID] = any(trig==x for x in values(x.stop.trigger))
      end
    end

    trig_lens = length.(Set([nt.trig for nt in all_flags]))
    active_scope = String[]
    i = first(ei)

    @inbounds for i_base in ei
      it = iterate(code, i)
      it == nothing && break
      debug() && println("------------------- i = $(i)")
      # Filter non-equal lengths:
      # may be non-unique, greedy over start trigs
      # Three options:
      #  - 1) Close last active scope
      #  - 2) Open new scope
      #  - 3) Do nothing (no string match)
      @inbounds for nt in all_flags
        ID = nt.ID
        trig = nt.trig
        L_trig = length(trig)
        @inbounds for trig_len in trig_lens
          safe_end = min(i + trig_len - 1, ei_end)
          if L_trig ≠ safe_end-i+1
            # No chance for match
            continue
          end
          code_substr = substr(code, i, safe_end)
          code_substr==nothing && break
          if trig==code_substr
            debug() && println("*********** match")
            # Update token stream (option 1 or 2)
            debug() && @show trig
            debug() && @show ID
            debug() && @show grep_type(nt.fp)
            debug() && @show nt.flag
            debug() && @show nt.fp
            debug() && @show active_scope
            debug() && @show is_stop_cond(nt, active_scope)
            debug() && @show is_start_cond(nt, active_scope)

            # Is true in all tests:
            stop_cond = is_stop_cond(nt, active_scope)
            start_cond = is_start_cond(nt, active_scope)
            if stop_cond
              if rm_outer_bcs
                i_mod = min(i+L_trig-nt.len_b_right, i_max)
              else
                i_mod = min(i+L_trig, i_max)
              end
              token_stream⁺[pop!(active_scope)][i_mod] = -1
            elseif start_cond
              if rm_outer_bcs
                i_mod = i+nt.len_b_left
              else
                i_mod = i
              end
              token_stream⁻[ID][i_mod] = 1
              push!(active_scope, ID)
            end
            if (stop_cond==start_cond==false) && overlap_trigs[ID]
              # Need to check if triggers match for complement flag
              continue
            end
            # increment i to avoid double-counting
            # for variable flag lengths:
            for k in 1:max(L_trig-1, 0)
              it = iterate(code, i)
              it == nothing && break
              i = it[2]
            end

            debug() && println("***********")
            break # leave for loop
          end # trig==code_substr
        end # for trig_len in trig_lens
      end # for nt in all_flags
      it = iterate(code, i)
      it == nothing && break
      i = it[2]
    end # for i in eachindex(code)

    for fp in flag_pairs
      ID = fp.ID
      token_stream[ID] = cumsum(token_stream⁺[ID] .+ token_stream⁻[ID])
    end

    if !greedy_config
      scope_sum = [0 for i in 1:i_max]
      @inbounds for (flag, scope) in token_stream
          scope_sum .+= scope
      end
      if last(scope_sum) != 0 && last(scope_sum) != 1
        @warn "Scope is not conserved"
      end
    end
  end

  return TokenStream(flag_set,code,token_stream)
end

"""
    (ts::TokenStream)(flag_id, cond=x->x>=1)

A `code` substring whose `TokenStream`, for
`flag_id`, satisfies the condition `cond`.
"""
function (ts::TokenStream)(flag_id, cond=x->x>=1)
  i_code = [i for i in eachindex(ts.code) if cond(ts.token_stream[flag_id][i])]
  return join([substr(ts.code, i, i) for i in i_code])
end


text:
using Distributions, CSV, Printf

# Load observed data
x_samples = CSV.read("hmm_observations.csv")[:,1]

# Gives P(Y_k = 1 | All other variables)
# Inputs
# - x: The vector of (observed) X variables
# - y: The current sample of Y variables
# - θ_k: The vector parameters in the form [q, σ²]
# - k: The index of the Y variable of interest
# Outputs
# The probability that Y_k = 1 given all other variables
function conditional_probability(x, y, θ_k, k)
    # Extract density parameters
    q, σ² = θ_k
    σ = sqrt(σ²)

    n = length(x)

    𝒩₁ = Normal(1, σ)
    𝒩₀ = Normal(0, σ)

    if k == 1
        joint = (q*(y[k+1] == 1) + (1-q)*(y[k+1] == 0))*pdf(𝒩₁, x[k])

        marginal = joint + (q*(y[k+1] == 0) + (1-q)*(y[k+1] == 1))*pdf(𝒩₀,x[k])
    elseif k == n
        joint = (q*(y[k-1] == 1) + (1-q)*(y[k-1] == 0))*pdf(𝒩₁, x[k])

        marginal = joint + (q*(y[k-1] == 0) + (1-q)*(y[k-1] == 1))*pdf(𝒩₀,x[k])

    else
        joint = (q*(y[k-1] == 1) + (1-q)*(y[k-1] == 0))*(q*(y[k+1] == 1) + (1-q)*(y[k+1] == 0))*pdf(𝒩₁,x[k])

        marginal = joint + (q*(y[k-1] == 0) + (1-q)*(y[k-1] == 1))*(q*(y[k+1] == 0) + (1-q)*(y[k+1] == 1))*pdf(𝒩₀, x[k])
    end

    return joint/marginal
end

# Performs one single Gibbs sampler iteration of Y
# Inputs
# - x: The vector of (observed) X variables
# - y: The current sample of Y variables
# - θ_k: The vector parameters in the form [q, σ²]
# - k: The index of the Y variable of interest
# Outputs
# A Y sample where each index of Y is sampled by conditioning on all other
# variables
function get_single_gibbs_sample(x, y, θ_k)
    n = length(x)

    for k = 1:n
        y[k] = rand() < conditional_probability(x, y, θ_k, k)
    end

    return y
end

# Returns a sample of Y
# Inputs
# - x: The vector of (observed) X variables
# - θ_k: The vector parameters in the form [q, σ²]
# Outputs
# A Y sample where Y ~ P(Y|X = x)
function gibbs_sampler(x, θ_k)
    n = length(x)

    y = rand(0:1, n)

    for i = 1:75 # Burn-in period
        y = get_single_gibbs_sample(x, y, θ_k)
    end

    return y
end

# Estimates the values of a,b,c (as defined in example) via Monte Carlo
# Inputs
# - x: The vector of (observed) X variables
# - θ_k: The vector parameters in the form [q, σ^2]
# Outputs
# A vector in the form [a,b,c] representing a MC estimate of a,b, and c
function estimate_a_b_c(x, θ_k)
    n = length(x)

    # Number of MC samples
    num_samples = 100

    # Estimate sum
    total_a = 0
    total_b = 0
    total_c = 0

    for k = 1:num_samples
        y_samples = gibbs_sampler(x, θ_k)
        estimate_a = sum(y_samples[1:(n-1)] .== y_samples[2:n])
        estimate_b = sum(y_samples[1:(n-1)] .!= y_samples[2:n])
        estimate_c = sum((x - y_samples).^2)

        total_a += estimate_a
        total_b += estimate_b
        total_c += estimate_c
    end

    return [total_a, total_b, total_c]./num_samples
end

# Performs EM algorithm to estimate θ
# Inputs
# - x: The vector of (observed) X variables
# Outputs
# An estimate of θ = [q, σ²]
function em_algorithm(x)
    # Initialize theta parameter [q, σ²]
    θ_k = [0.5, 1]
    @printf("k = 0: [%f, %f]\n", θ_k[1], θ_k[2])

    num_iterations = 500

    for i = 1:num_iterations
        a, b, c = estimate_a_b_c(x, θ_k)
        q_1 = a/(a+b)
        σ²_1 = c/(a+b+1)
        θ_k = [q_1, σ²_1]

        @printf("k = %d: [%f, %f]\n", i, θ_k[1], θ_k[2])
    end

    return θ_k
end

em_algorithm(x_samples)

max_stars_repo_name: seanrattana/courses

max_issues_repo_name: seanrattana/courses

max_forks_repo_name: seanrattana/courses


text:
"""
Hypatia examples and script utilities.
"""
module Examples

using LinearAlgebra
# delete later, affects qr. see https://github.com/JuliaLang/julia/pull/40623
if VERSION < v"1.7.0-DEV.1188"
    const ColumnNorm = Val{true}
end

using Test
import Random
using Printf
import DataFrames
import CSV
import DataStructures: OrderedDict

import Hypatia
import Hypatia.PolyUtils
import Hypatia.Cones
import Hypatia.Models
import Hypatia.Solvers

include("common.jl")
include("common_native.jl")
include("common_JuMP.jl")
include("spectral_functions_JuMP.jl")
include("setup.jl")

const model_types = [
    "native",
    "JuMP",
    ]

# list of names of native examples to run
const native_examples = [
    "densityest",
    "doptimaldesign",
    "linearopt",
    "matrixcompletion",
    "matrixregression",
    "maxvolume",
    "polyenvelope",
    "polymin",
    "portfolio",
    "sparsepca",
    ]

# list of names of JuMP examples to run
const JuMP_examples = [
    "CBLIB",
    "centralpolymat",
    "classicalquantum",
    "conditionnum",
    "contraction",
    "convexityparameter",
    "covarianceest",
    "densityest",
    "discretemaxlikelihood",
    "doptimaldesign",
    "entanglementassisted",
    "experimentdesign",
    "lotkavolterra",
    "lyapunovstability",
    "matrixcompletion",
    "matrixquadratic",
    "matrixregression",
    "maxvolume",
    "nearestcorrelation",
    "nearestpolymat",
    "nearestpsd",
    "nonparametricdistr",
    "normconepoly",
    "polyenvelope",
    "polymin",
    "polynorm",
    "portfolio",
    "regionofattr",
    "relentrentanglement",
    "robustgeomprog",
    "semidefinitepoly",
    "shapeconregr",
    "signomialmin",
    "sparselmi",
    "stabilitynumber",
    ]

load_example(mod::String, ex::String) =
    include(joinpath(@__DIR__, ex, mod * ".jl"))

get_test_instances(mod::String, ex::String) =
    include(joinpath(@__DIR__, ex, mod * "_test.jl"))

get_benchmark_instances(mod::String, ex::String) =
    include(joinpath(@__DIR__, ex, mod * "_benchmark.jl"))

model_type_examples(mod::String) = eval(Symbol(mod, "_examples"))

# load all examples
for mod in model_types, ex in model_type_examples(mod)
    load_example(mod, ex)
end

# build ordered dictionary of all test instances
function get_test_instances()
    test_insts = OrderedDict{String, OrderedDict}()
    for mod in model_types
        mod_insts = test_insts[mod] = OrderedDict()
        for ex in model_type_examples(mod)
            mod_insts[ex] = get_test_instances(mod, ex)
        end
    end
    return test_insts
end

end


text:
@testset "AbstractArray interface" begin
    @testset "size and length" begin
        m = @SMatrix [1 2 3; 4 5 6; 7 8 9; 10 11 12]

        @test length(m) == 12
        @test Base.linearindexing(m) == Base.LinearFast()
        @test Base.isassigned(m, 2, 2) == true
    end

    @testset "similar_type" begin
        @test similar_type(SVector{3,Int}) == SVector{3,Int}
        @test similar_type(@SVector [1,2,3]) == SVector{3,Int}

        @test similar_type(SVector{3,Int}, Float64) == SVector{3,Float64}
        @test similar_type(SMatrix{3,3,Int,9}, 2) == SVector{2, Int}
        @test similar_type(SMatrix{3,3,Int,9}, (2,)) == SVector{2, Int}
        @test similar_type(SMatrix{3,3,Int,9}, Float64, 2) == SVector{2, Float64}
        @test similar_type(SMatrix{3,3,Int,9}, Float64, 2) == SVector{2, Float64}

        @test similar_type(SMatrix{3,3,Int,9}, Float64) == SMatrix{3, 3, Float64, 9}
        @test similar_type(SVector{2,Int}, (3,3)) == SMatrix{3, 3, Int, 9}
        @test similar_type(SVector{2,Int}, Float64, (3,3)) == SMatrix{3, 3, Float64, 9}

        @test similar_type(SArray{(4,4,4),Int,3,64}, Float64) == SArray{(4,4,4), Float64, 3, 64}
        @test similar_type(SVector{2,Int}, (3,3,3)) == SArray{(3,3,3), Int, 3, 27}
        @test similar_type(SVector{2,Int}, Float64, (3,3,3)) == SArray{(3,3,3), Float64, 3, 27}

        # Some specializations for the mutable case
        @test similar_type(MVector{3,Int}, Float64) == MVector{3,Float64}
        @test similar_type(MMatrix{3,3,Int,9}, 2) == MVector{2, Int}
        @test similar_type(MMatrix{3,3,Int,9}, (2,)) == MVector{2, Int}
        @test similar_type(MMatrix{3,3,Int,9}, Float64, 2) == MVector{2, Float64}
        @test similar_type(MMatrix{3,3,Int,9}, Float64, 2) == MVector{2, Float64}

        @test similar_type(MMatrix{3,3,Int,9}, Float64) == MMatrix{3, 3, Float64, 9}
        @test similar_type(MVector{2,Int}, (3,3)) == MMatrix{3, 3, Int, 9}
        @test similar_type(MVector{2,Int}, Float64, (3,3)) == MMatrix{3, 3, Float64, 9}

        @test similar_type(MArray{(4,4,4),Int,3,64}, Float64) == MArray{(4,4,4), Float64, 3, 64}
        @test similar_type(MVector{2,Int}, (3,3,3)) == MArray{(3,3,3), Int, 3, 27}
        @test similar_type(MVector{2,Int}, Float64, (3,3,3)) == MArray{(3,3,3), Float64, 3, 27}
    end

    @testset "similar" begin
        sv = @SVector [1,2,3]
        sm = @SMatrix [1 2; 3 4]
        sa = SArray{(1,1,1),Int,3,1}((1,))

        @test isa(similar(sv), MVector{3,Int})
        @test isa(similar(sv, Float64), MVector{3,Float64})
        @test isa(similar(sv, 4), MVector{4,Int})
        @test isa(similar(sv, (4,)), MVector{4,Int})
        @test isa(similar(sv, Float64, 4), MVector{4,Float64})
        @test isa(similar(sv, Float64, (4,)), MVector{4,Float64})

        @test isa(similar(sm), MMatrix{2,2,Int,4})
        @test isa(similar(sm, Float64), MMatrix{2,2,Float64,4})
        @test isa(similar(sv, (3,3)), MMatrix{3,3,Int,9})
        @test isa(similar(sv, Float64, (3,3)), MMatrix{3,3,Float64,9})

        @test isa(similar(sa), MArray{(1,1,1),Int,3,1})
        @test isa(similar(sa, Float64), MArray{(1,1,1),Float64,3,1})
        @test isa(similar(sv, (3,3,3)), MArray{(3,3,3),Int,3,27})
        @test isa(similar(sv, Float64, (3,3,3)), MArray{(3,3,3),Float64,3,27})
    end
#=
    @testset "size and length" begin
        vec = [1,2,3,4]
        sv = SVector{(4,)}(vec)
        mv = MVector{(4,)}(vec)

        mat = eye(3)
        sm = MMatrix{(3,3)}(mat)
        mm = MMatrix{(3,3)}(mat)

        @test size(sv) == size(vec)
        @test size(mv) == size(vec)
        @test size(sm) == size(mat)
        @test size(mm) == size(mat)

        @test size(sv,1) == size(vec,1)
        @test size(mv,1) == size(vec,1)
        @test size(sm,1) == size(mat,1)
        @test size(mm,1) == size(mat,1)
        @test size(sv,2) == size(vec,2) # Test for
        @test size(mv,2) == size(vec,2) # trailing 1's
        @test size(sm,2) == size(mat,2)
        @test size(mm,2) == size(mat,2)

        @test length(sv) == length(vec)
        @test length(mv) == length(vec)
        @test length(sm) == length(mat)
        @test length(mm) == length(mat)

        @test size(typeof(sv)) == size(vec)
        @test size(typeof(mv)) == size(vec)
        @test size(typeof(sm)) == size(mat)
        @test size(typeof(mm)) == size(mat)

        @test size(typeof(sv),1) == size(vec,1)
        @test size(typeof(mv),1) == size(vec,1)
        @test size(typeof(sm),1) == size(mat,1)
        @test size(typeof(mm),1) == size(mat,1)
        @test size(typeof(sv),2) == size(vec,2) # Test for
        @test size(typeof(mv),2) == size(vec,2) # trailing 1's
        @test size(typeof(sm),2) == size(mat,2)
        @test size(typeof(mm),2) == size(mat,2)

        @test length(typeof(sv)) == length(vec)
        @test length(typeof(mv)) == length(vec)
        @test length(typeof(sm)) == length(mat)
        @test length(typeof(mm)) == length(mat)
    end

    @testset "Miscellanious introspection" begin
        vec = [1,2,3,4]
        sv = SVector{(4,)}(vec)
        mv = MVector{(4,)}(vec)

        @test Base.isassigned(sv, 4)    == Base.isassigned(vec, 4)
        @test Base.isassigned(sv, 5)    == Base.isassigned(vec, 5)
        @test Base.isassigned(sv, 2, 1) == Base.isassigned(vec, 2, 1) # test for trailing ones
        @test Base.isassigned(mv, 4)    == Base.isassigned(vec, 4)
        @test Base.isassigned(mv, 5)    == Base.isassigned(vec, 5)
        @test Base.isassigned(mv, 2, 1) == Base.isassigned(vec, 2, 1)

        @test Base.linearindexing(sv) == Base.LinearFast()
        @test Base.linearindexing(mv) == Base.LinearFast()
        @test Base.linearindexing(typeof(sv)) == Base.LinearFast()
        @test Base.linearindexing(typeof(mv)) == Base.LinearFast()
    end

    @testset "similar and similar_type" begin
        vec = [1,2,3,4]
        sv = SVector{(4,)}(vec)
        mv = MVector{(4,)}(vec)

        @test_throws Exception similar(sv)
        @test typeof(similar(mv)) == typeof(mv)
        @test_inferred similar(mv)
        @test_throws Exception similar(sv, Float64)
        @test typeof(similar(mv, Float64)) <: MVector{(4,),Float64}
        @test_inferred similar(mv, Float64)
        @test_throws Exception similar(sv, (2,2))
        @test_throws Exception similar(mv, (2,2))
        @test_throws Exception similar(sv, Float64, (2,2))
        @test_throws Exception similar(mv, Float64, (2,2))
        @test_throws Exception similar(sv, Val{(2,2)})
        @test typeof(similar(mv, Val{(2,2)})) <: MArray{(2,2),Int}
        @test_inferred similar(mv, Val{(2,2)})
        @test_throws Exception similar(sv, Float64, Val{(2,2)})
        @test typeof(similar(mv, Float64, Val{(2,2)})) <: MArray{(2,2),Float64}
        @test_inferred similar(mv, Float64, Val{(2,2)})

        @test similar_type(sv) == typeof(sv)
        @test_inferred similar_type(sv)
        @test similar_type(mv) == typeof(mv)
        @test_inferred similar_type(mv)
        @test similar_type(sv, Float64) == SArray{(4,),Float64,1,NTuple{4,Float64}}
        @test_inferred similar_type(sv, Float64)
        @test similar_type(mv, Float64) == MArray{(4,),Float64,1,NTuple{4,Float64}}
        @test_inferred similar_type(mv, Float64)
        @test_throws Exception similar_type(sv, (2,2))
        @test_throws Exception similar_type(mv, (2,2))
        @test_throws Exception similar_type(sv, Float64, (2,2))
        @test_throws Exception similar_type(mv, Float64, (2,2))
        @test similar_type(sv, Val{(2,2)}) == SArray{(2,2),Int,2,NTuple{4,Int}}
        @test_inferred similar_type(sv, Val{(2,2)})
        @test similar_type(mv, Val{(2,2)}) == MArray{(2,2),Int,2,NTuple{4,Int}}
        @test_inferred similar_type(mv, Val{(2,2)})
        @test similar_type(sv, Float64, Val{(2,2)}) == SArray{(2,2),Float64,2,NTuple{4,Float64}}
        @test_inferred similar_type(sv, Float64, Val{(2,2)})
        @test similar_type(mv, Float64, Val{(2,2)}) == MArray{(2,2),Float64,2,NTuple{4,Float64}}
        @test_inferred similar_type(mv, Float64, Val{(2,2)})
    end

    @testset "reshape and permutedims" begin
        vec = [4,5,6,7]
        sv = SVector{(4,)}(vec)
        mv = MVector{(4,)}(vec)

        mat = reshape(vec,(2,2))
        sm = SMatrix{(2,2)}(mat)
        mm = MMatrix{(2,2)}(mat)

        @test reshape(sv, Val{(2,2)}) === sm
        @test_inferred reshape(sv, Val{(2,2)})
        @test reshape(mv, Val{(2,2)}) == mm
        @test_inferred reshape(mv, Val{(2,2)})
        @test isa(reshape(mv, Val{(2,2)}), MMatrix)
        @test_throws Exception reshape(sv, (2,2)) == sm
        @test_throws Exception reshape(sv, (2,2)) == sm

    end

    =#
end


text:
const Filter_base = [4.118588707535708e-6,4.662307783048404e-6,5.2778064058937756e-6,5.97456061553286e-6,6.7632974390298035e-6,7.656160041269817e-6,8.666894677627122e-6,9.811062327352187e-6,1.1106278265924788e-5,1.2572483264760042e-5,1.42322505935799e-5,1.6111133551969887e-5,1.8238058880617237e-5,2.0645772109076137e-5,2.3371341696506312e-5,2.645672972698995e-5,2.994943794568826e-5,3.390323908202406e-5,3.8379004719130766e-5,4.3445642455208216e-5,4.918115678505129e-5,5.567385003477687e-5,6.302368183899532e-5,7.134380809054521e-5,8.076232305601659e-5,9.142423147817327e-5,0.00010349368102719458,0.00011715648947091054,0.00013262300547161834,0.00015013134705348249,0.0001699510675990275,0.00019238730581535294,0.00021778548356175115,0.0002465366238651324,0.00027908337099788406,0.00031592680530155527,0.0003576341576754271,0.00040484754250000483,0.0004582938434450188,0.0005187959043609725,0.0005872851975459913,0.0006648161644249478,0.0007525824494258378,0.000851935276985483,0.0009644042546116447,0.0010917209222795108,0.001235845410722264,0.0013989966190390965,0.0015836863762264436,0.001792758112573506,0.002029430636295734,0.0022973476893787268,0.002600634045580001,0.0029439590142573526,0.0033326083277104117,0.0037725655187922052,0.004270604041657015,0.0048343915539089625,0.005472607965649308,0.006195079072871529,0.007012927832585425,0.007938745608658544,0.008986786024826483,0.010173184409377162,0.011516206210016314,0.013036528203437736,0.014757556829019875,0.01670578854762277,0.018911217773465227,0.021407798659484324,0.024233967845691123,0.027433236218606032,0.03105485879233143,0.03515459302455746,0.039795557242315927,0.0450492023935578,0.050996412085361466,0.05772874784464333,0.06534985877304568,0.07397707729864236,0.08374322559219596,0.09479866045903013,0.10731358818908403,0.12148068500391276,0.13751806344428075,0.15567263036799733,0.1762238882567611,0.19948823835583873,0.22582385189647586,0.2556361843969779,0.28938421793905067,0.32758752752368947,0.37083428029819565,0.41979029080811425,0.47520927168614446,0.5379444375946745,0.6089616410728969,0.6893542425242224,0.7803599432780343,0.8833798408827509,1.0,1.1320158709991752,1.2814599321940212,1.4506329812931589,1.6421395578187052,1.858928041846342,2.104336046415478,2.382141802457978,2.6966223273530128,3.0526192726543444,3.4556134647626755,3.911809286149797,4.428230196243525,5.012826862585463,5.6745995670177445,6.423736771429134,7.271771976378781,8.231761287547819,9.318484423780736,10.548672261378394,11.941264417849103,13.517700840802913,15.302251891207787,17.32239200287436,19.609222670922968,22.197951281441636,25.12843315425841,28.445785143962375,32.20108024599797,36.45213390178773,41.26439410861079,46.71194903811227,52.878667676447755,59.85949104702991,67.76189389517093,76.70753933829559,86.83415195624421,98.29763815922249,111.27448647797397,125.96448473034971,142.5937958969891,161.41844006140866,182.72823602144376,206.85126325595743,234.15891294197226,265.0716057862269,300.06526470124555,339.67864197737873,384.5216137578392,435.2845695160886,492.7490410932563,557.7997349371907,631.4381527880333,714.7980105104556,809.1626924564707,915.9850100811499,1036.9095690092008,1173.7980889093292,1328.7580659938624,1504.175219423221,1702.7502211507524,1927.5402746900081,2182.006182939198,2470.0656297055034,2796.1534952362003,3165.290134357197,3583.1586684094545,4056.1924809477755,4591.674264260404,5197.848141601231,5884.046591336165,6660.8341270911405,7540.169945960111,8535.592048857829,9662.425667681435,10938.019208165191,12382.011340936813,14016.633352832258,15867.051413382505,17961.754025908867,20332.99062831218,23017.268096126892,26055.912791858576,29495.706813754354,33389.60823950846,37797.56645356835,42787.44511058541,48436.06694468877,54830.396510166145,62068.8790626859,70262.95619408888,79538.78155502846,90039.16308022855,101925.76161830177,115381.57981559623,130613.77957221285,147856.8714469329,167376.3251142129,189472.6564588066,214486.05423174356,242801.6174983236]

const Filter_J0 = [0.001502009920951996,-0.010381698214761684,0.036840860097595164,-0.0899033803922747,0.1708228653683386,-0.2711574965683628,0.37649328091859574,-0.47220778569122657,0.5477821108964709,-0.598235168530351,0.6234579161218533,-0.6265043664825772,0.6119722535117335,-0.5847017386614045,0.5491105578961686,-0.5087886168486084,0.466521063454302,-0.42426478870289325,0.3833953891193336,-0.34472344791723936,0.3087689146689051,-0.2757043936836481,0.24562331000974616,-0.21839207265400126,0.19393194321380827,-0.17198162962644575,0.15242270280410272,-0.13494945901825492,0.11946763189654602,-0.10565870880444576,0.09348235554803391,-0.08261252527983624,0.07307776352617497,-0.06453648128170122,0.057096310621587334,-0.0503848596052138,0.04459955739673305,-0.039316995063444334,0.03483854475429614,-0.030664946477420088,0.027221072240291456,-0.0239015861084346,0.021281566646364738,-0.018612484595521447,0.0166553863034614,-0.014472420076504743,0.013057747649606617,-0.0112262860010603,0.01026681855793083,-0.008673802242165359,0.008110336867084816,-0.006657362814208594,0.0064551189810889585,-0.005052401511783908,0.005198901427050626,-0.0037597430078747883,0.004264054516529264,-0.0026994965688974474,0.003592800056058723,-0.001806129128998902,0.0031436470898586703,-0.0010244200001620755,0.0028888297674091743,-0.00030605070741955765,0.0028125907445050334,0.00039337862066047565,0.0029102041487017727,0.0011170884769911178,0.003187676341595336,0.0019097806429850762,0.003662102032146541,0.002820378358784995,0.004362688559939043,0.00390500249156868,0.005332492348686608,0.005230339939427049,0.006630934845839698,0.006877554068703745,0.008337168998822609,0.00894685647454101,0.010554326601376745,0.011562765604509096,0.013414536265660639,0.014879837399735491,0.01708424133651957,0.01908809265444127,0.021768513715084332,0.024416120601223806,0.027711234465350204,0.031127164234404547,0.03518414022027745,0.03949791311363894,0.04444976215720499,0.04975843326998389,0.05566745711499243,0.06194979043415182,0.06868206490557184,0.07561674395011839,0.08258498356357226,0.08919363526298704,0.09487468694200528,0.09889168186909404,0.1000429465449573,0.09701684432980286,0.08778959614991438,0.07050976759285542,0.04277885348484935,0.0035584532926218175,-0.04721045326487935,-0.10489787743225988,-0.1602095040734828,-0.19459781573132096,-0.1849077459954238,-0.1075416502002519,0.03603772748747661,0.19759013047489976,0.26132313321851336,0.11713996822458939,-0.1875877928130144,-0.3023811499746215,0.04816313568456773,0.36399529664885466,-0.14910233461562913,-0.26373490348543854,0.403626618077187,-0.3140979465010458,0.1817936940513108,-0.09073871804263177,0.04294648754516024,-0.020586135807067835,0.010392667161913182,-0.005611784806872302,0.0032402025511569896,-0.0019858724388273777,0.0012807317326135252,-0.0008625379175606825,0.0006029659078214355,-0.00043548936996943465,0.00032375891570874245,-0.0002469821224005998,0.00019279062274925971,-0.00015357911509105972,0.0001245378784936744,-0.00010255126402954421,8.555848220947627e-5,-7.217092847633435e-5,6.143686328308002e-5,-5.2693349406473615e-5,4.547125514262373e-5,-3.943328415865359e-5,3.433297116479562e-5,-2.9987212220165472e-5,2.625765743561439e-5,-2.3037978256349448e-5,2.024507133101665e-5,-1.781292564438252e-5,1.5688305607849465e-5,-1.382767949211047e-5,1.2195005442258958e-5,-1.0760110818279243e-5,9.497485767095914e-6,-8.385371134393834e-6,7.405061254071319e-6,-6.54036828602285e-6,5.7772102413267325e-6,-5.103293321879673e-6,4.5078641320869195e-6,-3.981511195181711e-6,3.515999321029234e-6,-3.1041249680128287e-6,2.7395854235553632e-6,-2.4168587823583312e-6,2.1310948134441218e-6,-1.8780185742021288e-6,1.6538488252707735e-6,-1.4552318768651144e-6,1.2791890865306748e-6,-1.1230741959148155e-6,9.845363053859617e-7,-8.61485744931908e-7,7.520624269970173e-7,-6.546081033468263e-7,5.676445159602976e-7,-4.8985884555185e-7,4.200967410687925e-7,-3.5736212641225517e-7,3.008221596993967e-7,-2.498151048163967e-7,2.0385823466866512e-7,-1.6265189071584773e-7,1.260741670061131e-7,-9.415841791345086e-8,6.704391121706343e-8,-4.4891090827293947e-8,2.7761325666544702e-8,-1.5480404355710375e-8,7.532730014109875e-9,-3.0524770418657847e-9,9.587785609683078e-10,-2.0575286298055636e-10,2.2414416956474645e-11]

const Filter_J1 = [4.782787133250618e-10,-2.9784175503440788e-9,9.772383277089722e-9,-2.238234099608581e-8,4.044677432947085e-8,-6.173481585455392e-8,8.329391218560819e-8,-1.024945350228408e-7,1.1780779749909977e-7,-1.287006146083485e-7,1.35592434383499e-7,-1.3921010821521872e-7,1.406574572276967e-7,-1.407488190837528e-7,1.4051720878600928e-7,-1.404074668777783e-7,1.4127886061686993e-7,-1.4315595655055356e-7,1.4689283208027915e-7,-1.5210916706348747e-7,1.598980155013874e-7,-1.694091840791126e-7,1.8227089415749844e-7,-1.969585687860337e-7,2.160395242710676e-7,-2.3691320619292838e-7,2.6369843208466607e-7,-2.9202021404039016e-7,3.285244508632466e-7,-3.6589094553627693e-7,4.148750103686303e-7,-4.6327136995173986e-7,5.285269736975052e-7,-5.903498371995408e-7,6.77102116115609e-7,-7.551294280790103e-7,8.70624734664098e-7,-9.678853091813045e-7,1.1222658353725904e-6,-1.2417228058919743e-6,1.449346703689914e-6,-1.593245655920808e-6,1.8747045814274419e-6,-2.0433320340041385e-6,2.4285695351967146e-6,-2.617992645652053e-6,3.151172966139178e-6,-3.349241203288127e-6,4.096418661354996e-6,-4.275827575123915e-6,5.33711975522078e-6,-5.44354237736265e-6,6.97257669216721e-6,-6.9045562968161745e-6,9.139702543697744e-6,-8.714837303363596e-6,1.2029590806160379e-5,-1.0927976968519436e-5,1.59125267194553e-5,-1.3582559661331659e-5,2.1176226828087565e-5,-1.6678205993448338e-5,2.8384979250408712e-5,-2.0132088397797457e-5,3.837204531118811e-5,-2.3702184335455945e-5,5.238530885000794e-5,-2.685437394370126e-5,7.231858155790216e-5,-2.8535361884516687e-5,0.00010108123118106823,-2.6788477540644352e-5,0.00014319185407094621,-1.8108424211338017e-5,0.00020573561552327273,3.6361648565843316e-6,0.0002999126469285961,4.8993332079278846e-5,0.00044354733854670903,0.00013589101811995494,0.0006651582352127376,0.0002945199160862439,0.0010105553806271136,0.0005753396479225405,0.0015535077418254303,0.0010621193133794828,0.0024128970258747457,0.0018929698186109245,0.0037800177772191607,0.0032937343278959356,0.005961217980039154,0.005629593532055224,0.009442252680316808,0.009481022824713792,0.014979159139973408,0.015745093424331037,0.02370896637000014,0.02574059076213687,0.0372327828431175,0.04122500890061429,0.05750710321227736,0.06404464284623569,0.08609179655185725,0.09471713980445745,0.12172497389177185,0.128535970003989,0.15450777327408322,0.1475596409096932,0.15621399202016978,0.11147620703185755,0.07748983135608338,-0.02762826685014771,-0.1019873017831784,-0.2203988997111164,-0.21185762869925318,-0.1605241508315224,0.09164902579868109,0.23792823877700942,0.26075777853738125,-0.15662188259001042,-0.28932081756330175,0.01314851911624769,0.42691302759079564,-0.4000505000648904,0.11513789407450359,0.09374824435871762,-0.16037231301955096,0.15071857939129532,-0.12120369075996129,0.09411065607998234,-0.07374223843458433,0.059038567576124905,-0.04828811752847585,0.04019705429957688,-0.03391978772064108,0.02891824715676397,-0.024845271759013743,0.021470449751150148,-0.01863582802005709,0.01622957936236386,-0.014170085406786529,0.01239608412101189,-0.010860414401084047,0.009525944424535663,-0.008362857744723338,0.0073468029551253195,-0.006457604321096636,0.005678343995599488,-0.004994694916726544,0.004394425810860881,-0.003867026401966086,0.003403418035555667,-0.0029957260668529964,0.0026370977166248776,-0.002321554011737298,0.0020438677474690805,-0.0017994616759226389,0.0015843226896713463,-0.0013949288614414647,0.001228186970888625,-0.0010813786997088304,0.0009521140746075729,-0.0008382910302044814,0.0007380601822098776,-0.0006497940667124724,0.0005720602290123041,-0.0005035976454348357,0.000443296041223001,-0.00039017773206623073,0.0003433816697409889,-0.00030214941633163506,0.00026581280850704716,-0.0002337831047958839,0.00020554143583887405,-0.00018063040107732216,0.00015864667598176302,-0.0001392345123516876,0.00012208003098625932,-0.00010690622166177854,9.346858036256814e-5,-8.155132858123432e-5,7.096417463153107e-5,-6.153959246866666e-5,5.313060911614544e-5,-4.560910598312646e-5,3.886464858418121e-5,-3.2803856352344075e-5,2.735029677587626e-5,-2.24448161508053e-5,1.804607628158424e-5,-1.4130826937491561e-5,1.0693106849359383e-5,-7.741205331453028e-6,5.29105764436983e-6,-3.3552268362550323e-6,1.928295620636745e-6,-9.725371257205876e-7,4.110080763295935e-7,-1.3553176263207053e-7,3.0748587523233524e-8,-3.5668195345476294e-9]

max_stars_repo_name: GeoscienceAustralia/HiQGA

max_issues_repo_name: GeoscienceAustralia/HiQGA


text:
==(a::Node,b::Node)=(a.name==b.name)&(a.presence==b.presence)

nodes(ls::Union{LinkStream,DirectedLinkStream})=ls.V

nodes(ls::Union{LinkStream,DirectedLinkStream},t::Float64)=ls.V

nodes(ls::Union{LinkStream,DirectedLinkStream},t0::Float64,t1::Float64)=ls.V

nodes(s::Union{StreamGraph,DirectedStreamGraph},t::Float64)=[n for (n,interv) in s.W if t ∈ interv]

nodes(s::Union{StreamGraph,DirectedStreamGraph})=[b for (a,b) in s.W]

nodes(s::Union{StreamGraph,DirectedStreamGraph},t0::Float64,t1::Float64)=t0<=t1 ? [n for (n,interv) in s.W if Intervals([(t0,t1)]) ⊆ interv] : []

function nodes(tc::TimeCursor)
    length(tc.S.nodes) > 0 && return tc.S.nodes
    res=Set{AbstractString}()
    if length(tc.S.links) > 0
        for l in tc.S.links
            push!(res,l[1])
            push!(res,l[2])
        end
        return res
    else
        return Set{AbstractString}()
    end
end

function nodes(tc::TimeCursor,t::Float64)
    goto!(tc,t)
    if haskey(tc.T,t)
        s1=nodes(tc)
        previous!(tc)
        return s1 ∪ nodes(tc)
    else
        return nodes(tc)
    end
end

function nodes(tc::TimeCursor,t0::Float64,t1::Float64)
    N=Set{AbstractString}()
    goto!(tc,t0)
    haskey(tc.T,t0) && previous!(tc)
    N=N ∪ nodes(tc)
    while tc.S.t1 < t1
        next!(tc)
        N=N ∪ nodes(tc)
    end
    if haskey(tc.T,t1)
        next!(tc)
        N=N ∪ nodes(tc)
    end
    N
end


text:
using Pkg
Pkg.activate(@__DIR__)
Pkg.instantiate()

using JuliaFormatter

main_paths = ["./src", "./test"]
for main_path in main_paths
    for folder in readdir(main_path)
        @show folder_path = joinpath(main_path, folder)
        if isfile(folder_path)
            !occursin(".jl", folder_path) && continue
        end
        format(folder_path;
            whitespace_ops_in_indices = true,
            remove_extra_newlines = true,
            verbose = true,
            always_for_in = true,
            whitespace_typedefs = true,
            whitespace_in_kwargs = false,
            format_docstrings = true,

            # always_use_return = true # removed since it has false positives.
            )
    end
end


text:
using Compat.Test
using IJulia
using Compat: occursin

mktemp() do path, io
    redirect_stdout(IJulia.IJuliaStdio(io, "stdout")) do
        stdout = isdefined(Base, :devnull) ? Base.stdout : Base.STDOUT
        println(stdout, "stdout")
        println("print")
    end
    flush(io)
    seek(io, 0)
    @test read(io, String) == "stdout\nprint\n"
    @test_throws ArgumentError redirect_stdout(IJulia.IJuliaStdio(io, "stderr"))
    @test_throws ArgumentError redirect_stdout(IJulia.IJuliaStdio(io, "stdin"))
    @test_throws ArgumentError redirect_stderr(IJulia.IJuliaStdio(io, "stdout"))
    @test_throws ArgumentError redirect_stderr(IJulia.IJuliaStdio(io, "stdin"))
    @test_throws ArgumentError redirect_stdin(IJulia.IJuliaStdio(io, "stdout"))
    @test_throws ArgumentError redirect_stdin(IJulia.IJuliaStdio(io, "stderr"))
end

mktemp() do path, io
    redirect_stderr(IJulia.IJuliaStdio(io, "stderr")) do
        stderr = isdefined(Base, :devnull) ? Base.stderr : Base.STDERR
        println(stderr, "stderr")
    end
    flush(io)
    seek(io, 0)
    @test read(io, String) == "stderr\n"
end

mktemp() do path, io
    redirect_stdin(IJulia.IJuliaStdio(io, "stdin")) do
        # We can't actually do anything here because `IJuliaexecute_msg` has not
        # yet been initialized, so we just make sure that redirect_stdin does
        # not error.
    end
end


text:
using VimCompletion

jlcompserver = @async serverstart("localhost", 18888)

max_stars_repo_name: wangl-cc/juliatools-nvim

max_issues_repo_name: wangl-cc/juliatools-nvim

max_forks_repo_name: wangl-cc/juliatools-nvim