text:
module Contexts

export @context, @!, @defer, AbstractContext

abstract type AbstractContext end

struct Context <: AbstractContext
    resources
end

Context() = Context([])

cleanup!(x) = close(x)
cleanup!(f::Function) = f()

# This recursive arrangement does two things:
# 1. Avoids paying the try/catch setup cost more than once when no exceptions
#    occur.
# 2. Arranges all exceptions on the exception stack, reflecting the cleanup
#    ordering and avoiding the need for CompositeException
function _cleanup!(resources, i)
    try
        while i > 0
            cleanup!(resources[i])
            i -= 1
        end
    catch exc
        _cleanup!(resources, i-1)
        rethrow()
    end
end

function cleanup!(context::Context)
    # Clean up resources last to first.
    _cleanup!(context.resources, length(context.resources))
end

function defer(f::Function, ctx::Context)
    push!(ctx.resources, f)
    nothing
end

macro defer(ex)
    quote
        ctx = $(Expr(:islocal, esc(_context_name))) ?
            $(esc(_context_name)) : global_context($__module__, $(__source__.line), $(QuoteNode(__source__.file)))
        defer(()->$(esc(ex)), ctx)
    end
end

macro defer(ctx, ex)
    quote
        defer(()->$(esc(ex)), $(esc(ctx)))
    end
end

macro context(ex)
    if ex.head == :function
        ex.args[2] = quote
            try
                $(_context_name) = $Contexts.Context()
                $(ex.args[2])
            finally
                $Contexts.cleanup!($(_context_name))
            end
        end
        esc(ex)
    else
        quote
            let $(esc(_context_name)) = Context()
                try
                    $(esc(ex))
                finally
                    cleanup!($(esc(_context_name)))
                end
            end
        end
    end
end

macro !(ex)
    @assert ex.head == :call
    map!(a->esc(a), ex.args, ex.args)
    insert!(ex.args, 2, :ctx)
    quote
        ctx = $(Expr(:islocal, esc(_context_name))) ?
            $(esc(_context_name)) : global_context($__module__, $(__source__.line), $(QuoteNode(__source__.file)))
        $ex
    end
end

#-------------------------------------------------------------------------------
# Global context
function __init__()
    atexit() do
        global_cleanup!()
    end
end

_context_name = :var"#context"

_global_context = Context()

@noinline function global_context(_module, file, line)
    @warn """Using global `Context` — use a `@context` block to avoid this warning.
             Use `Contexts.global_cleanup!()` to clean up the resource.""" #=
        =# _module=_module _group="context" _file=file _line=line
    _global_context
end

function global_cleanup!()
    cleanup!(_global_context)
    empty!(_global_context.resources)
    nothing
end

include("base_interop.jl")

end


text:
#cd("C:\\Users\\dcirk\\Documents\\CSCE689\\Project")

##### Facebook Analysis ######

# Load in Facebook Data
using DelimitedFiles, MatrixMarket, JSON
include("MaxRecipFunctions.jl")
edgelist = readdlm("Datasets\\facebook-wall.txt")
edgelist = convert(Matrix{Int64}, edgelist[:, 1:2])

# Convert to adjacency matrix
n_edges = size(edgelist)[1]
n_nodes = maximum(edgelist)
A = sparse(edgelist[:, 1], edgelist[:, 2], repeat([1], n_edges), n_nodes, n_nodes)
# Rewire algorithm
time_facebook = @timed begin
A_rewire = greedyRewire(A)
end

# Reciprocity before and after
recip = compute_reciprocity(A)
recip_rewire = compute_reciprocity(A_rewire)

out_deg = transpose(sum(A, dims = 1))
in_deg = sum(A, dims = 2)
min_deg = zeros(n_nodes)
for i in 1:n_nodes
    min_deg[i] = min(in_deg[i], out_deg[i])
end
upperbound = sum(min_deg)

# reciproicty of original graph
facebook_recip = recip/n_edges
# reciprocity of rewired graph
facebook_rewire_recip = recip_rewire/n_edges
# upper bound
facebook_upper_bound = upperbound/n_edges
facebook_edges = n_edges
facebook_nodes = n_nodes

##### EU Email Analysis ######

A = MatrixMarket.mmread("Datasets\\email-Eu-core-temporal\\email-Eu-core-temporal.mtx")
# Convert to adjacency matrix
n_edges = sum(A)
n_nodes = size(A)[1]
# Rewire algorithm
time_email_EU = @timed begin
A_rewire = greedyRewire(A)
end

# Reciprocity before and after
recip = compute_reciprocity(A)
recip_rewire = compute_reciprocity(A_rewire)

out_deg = transpose(sum(A, dims = 1))
in_deg = sum(A, dims = 2)
min_deg = zeros(n_nodes)
for i in 1:n_nodes
    min_deg[i] = min(in_deg[i], out_deg[i])
end
upperbound = sum(min_deg)

# reciproicty of original graph
email_EU_recip = recip/n_edges
# reciprocity of rewired graph
email_EU_rewire_recip = recip_rewire/n_edges
# upper bound
email_EU_upper_bound = upperbound/n_edges
email_EU_edges = n_edges
email_EU_nodes = n_nodes

##### Slashdot Analysis ######

A = MatrixMarket.mmread("Datasets\\soc-Slashdot0811\\soc-Slashdot0811.mtx")
# Convert to adjacency matrix
n_edges = sum(A)
n_nodes = size(A)[1]
# Rewire algorithm
time_slashdot = @timed begin
    A_rewire = greedyRewire(A)
end

# Reciprocity before and after
recip = compute_reciprocity(A)
recip_rewire = compute_reciprocity(A_rewire)

out_deg = transpose(sum(A, dims = 1))
in_deg = sum(A, dims = 2)
min_deg = zeros(n_nodes)
for i in 1:n_nodes
    min_deg[i] = min(in_deg[i], out_deg[i])
end
upperbound = sum(min_deg)

# reciproicty of original graph
slashdot_recip = recip/n_edges
# reciprocity of rewired graph
slashdot_rewire_recip = recip_rewire/n_edges
# upper bound
slashdot_upper_bound = upperbound/n_edges
slashdot_edges = n_edges
slashdot_nodes = n_nodes

#### Mathoverflow Analysis ####

A = MatrixMarket.mmread("Datasets\\sx-mathoverflow\\sx-mathoverflow.mtx")
# Convert to adjacency matrix
n_edges = sum(A)
n_nodes = size(A)[1]
# Rewire algorithm
time_mathoverflow = @timed begin
    A_rewire = greedyRewire(A)
end

# Reciprocity before and after
recip = compute_reciprocity(A)
recip_rewire = compute_reciprocity(A_rewire)

out_deg = transpose(sum(A, dims = 1))
in_deg = sum(A, dims = 2)
min_deg = zeros(n_nodes)
for i in 1:n_nodes
    min_deg[i] = min(in_deg[i], out_deg[i])
end
upperbound = sum(min_deg)

# reciproicty of original graph
mathoverflow_recip = recip/n_edges
# reciprocity of rewired graph
mathoverflow_rewire_recip = recip_rewire/n_edges
# upper bound
mathoverflow_upper_bound = upperbound/n_edges
mathoverflow_edges = n_edges
mathoverflow_nodes = n_nodes

#### College Message Analysis ####

A = MatrixMarket.mmread("Datasets\\CollegeMsg\\CollegeMsg.mtx")
# Convert to adjacency matrix
n_edges = sum(A)
n_nodes = size(A)[1]
# Rewire algorithm
time_collegemsg = @timed begin
    A_rewire = greedyRewire(A)
end

# Reciprocity before and after
recip = compute_reciprocity(A)
recip_rewire = compute_reciprocity(A_rewire)

out_deg = transpose(sum(A, dims = 1))
in_deg = sum(A, dims = 2)
min_deg = zeros(n_nodes)
for i in 1:n_nodes
    min_deg[i] = min(in_deg[i], out_deg[i])
end
upperbound = sum(min_deg)

# reciproicty of original graph
collegemsg_recip = recip/n_edges
# reciprocity of rewired graph
collegemsg_rewire_recip = recip_rewire/n_edges
# upper bound
collegemsg_upper_bound = upperbound/n_edges
collegemsg_edges = n_edges
collegemsg_nodes = n_nodes

#### Epinions Analysis ####
A = MatrixMarket.mmread("Datasets\\soc-Epinions1\\soc-Epinions1.mtx")
# Convert to adjacency matrix
n_edges = sum(A)
n_nodes = size(A)[1]

# Rewire algorithm
time_epinions = @timed begin
    A_rewire = greedyRewire(A)
end

# Reciprocity before and after
recip = compute_reciprocity(A)
recip_rewire = compute_reciprocity(A_rewire)

out_deg = transpose(sum(A, dims = 1))
in_deg = sum(A, dims = 2)
min_deg = zeros(n_nodes)
for i in 1:n_nodes
    min_deg[i] = min(in_deg[i], out_deg[i])
end
upperbound = sum(min_deg)

# reciproicty of original graph
epinions_recip = recip/n_edges
# reciprocity of rewired graph
epinions_rewire_recip = recip_rewire/n_edges
# upper bound
epinions_upper_bound = upperbound/n_edges
epinions_edges = n_edges
epinions_nodes = n_nodes

#### Twitter Analysis ####
A = MatrixMarket.mmread("Datasets\\higgs-twitter\\higgs-twitter_mention.mtx")
# Convert to adjacency matrix
n_edges = sum(A)
n_nodes = size(A)[1]

# Rewire algorithm
time_twitter = @timed begin
    A_rewire = greedyRewire(A)
end

# Reciprocity before and after
recip = compute_reciprocity(A)
recip_rewire = compute_reciprocity(A_rewire)

out_deg = transpose(sum(A, dims = 1))
in_deg = sum(A, dims = 2)
min_deg = zeros(n_nodes)
for i in 1:n_nodes
    min_deg[i] = min(in_deg[i], out_deg[i])
end
upperbound = sum(min_deg)

# reciproicty of original graph
twitter_recip = recip/n_edges
# reciprocity of rewired graph
twitter_rewire_recip = recip_rewire/n_edges
# upper bound
twitter_upper_bound = upperbound/n_edges
twitter_edges = n_edges
twitter_nodes = n_nodes

#### PA Analysis ####
A = MatrixMarket.mmread("Datasets\\PAdata.mtx")
# Convert to adjacency matrix
n_edges = sum(A)
n_nodes = size(A)[1]

# Rewire algorithm
time_PA = @timed begin
    A_rewire = greedyRewire(A)
end

# Reciprocity before and after
recip = compute_reciprocity(A)
recip_rewire = compute_reciprocity(A_rewire)

out_deg = transpose(sum(A, dims = 1))
in_deg = sum(A, dims = 2)
min_deg = zeros(n_nodes)
for i in 1:n_nodes
    min_deg[i] = min(in_deg[i], out_deg[i])
end
upperbound = sum(min_deg)

# reciproicty of original graph
PA_recip = recip/n_edges
# reciprocity of rewired graph
PA_rewire_recip = recip_rewire/n_edges
# upper bound
PA_upper_bound = upperbound/n_edges

PA_edges = n_edges
PA_nodes = n_nodes

output_data =
["Facebook" facebook_recip facebook_rewire_recip facebook_upper_bound facebook_nodes facebook_edges time_facebook[2]
"EU Email" email_EU_recip email_EU_rewire_recip email_EU_upper_bound email_EU_nodes email_EU_edges time_email_EU[2]
"Slashdot" slashdot_recip slashdot_rewire_recip slashdot_upper_bound slashdot_nodes slashdot_edges time_slashdot[2]
"Mathoverflow" mathoverflow_recip mathoverflow_rewire_recip mathoverflow_upper_bound mathoverflow_nodes mathoverflow_edges time_mathoverflow[2]
"College Msg" collegemsg_recip collegemsg_rewire_recip collegemsg_upper_bound collegemsg_nodes collegemsg_edges time_collegemsg[2]
"Epinions" epinions_recip epinions_rewire_recip epinions_upper_bound epinions_nodes epinions_edges time_epinions[2]
"Twitter" twitter_recip twitter_rewire_recip twitter_upper_bound twitter_nodes twitter_edges time_twitter[2]
"Preferential" PA_recip PA_rewire_recip PA_upper_bound PA_nodes PA_edges time_PA[2]]

#writedlm("rewiringResults.txt", output_data)

ext: jl


text:
# This file was generated, do not modify it. # hide
@pipeline RegPipe(std = Standardizer(),
                  hot = OneHotEncoder(),
                  reg = LinearRegressor())

model = RegPipe()
pipe  = machine(model, Xc, y)
fit!(pipe, rows=train)
ŷ = predict(pipe, rows=test)
round(rms(ŷ, y[test])^2, sigdigits=4)


text:
@testset "plotting" begin
    x = rand(10)
    f = GP(SqExponentialKernel())
    gp = f(x, 0.1)

    z = rand(10)
    plt1 = sampleplot(z, gp)
    @test plt1.n == 1
    @test plt1.series_list[1].plotattributes[:x] == sort(z)

    plt2 = sampleplot(gp; samples=10)
    @test plt2.n == 10
    sort_x = sort(x)
    @test all(series.plotattributes[:x] == sort_x for series in plt2.series_list)

    z = rand(7)
    plt3 = sampleplot(z, f; samples=8)
    @test plt3.n == 8
    sort_z = sort(z)
    @test all(series.plotattributes[:x] == sort_z for series in plt3.series_list)

    # Check recipe dispatches for `FiniteGP`s
    rec = RecipesBase.apply_recipe(Dict{Symbol,Any}(), gp)
    @test length(rec) == 1 && length(rec[1].args) == 2 # one series with two arguments
    @test rec[1].args[1] == x
    @test rec[1].args[2] == gp
    @test isempty(rec[1].plotattributes) # no default attributes

    z = 1 .+ x
    for kwargs in (
        Dict{Symbol,Any}(),
        Dict{Symbol,Any}(:ribbon_scale => 3),
        Dict{Symbol,Any}(:ribbon_scale => rand()),
    )
        scale = get(kwargs, :ribbon_scale, 1.0)
        rec = RecipesBase.apply_recipe(kwargs, z, gp)
        @test length(rec) == 1 && length(rec[1].args) == 2 # one series with two arguments
        @test rec[1].args[1] == z
        @test rec[1].args[2] == zero(x)
        # 3 default attributes
        attributes = rec[1].plotattributes
        @test sort!(collect(keys(attributes))) == [:fillalpha, :linewidth, :ribbon]
        @test attributes[:fillalpha] == 0.3
        @test attributes[:linewidth] == 2
        @test attributes[:ribbon] == scale .* sqrt.(var(gp))
    end

    # Check recipe dispatches for `AbstractGP`s
    # with `AbstractVector` and `AbstractRange`:
    for x in (rand(10), 0:0.01:1)
        rec = RecipesBase.apply_recipe(Dict{Symbol,Any}(), x, f)
        @test length(rec) == 1 && length(rec[1].args) == 1 # one series with one argument
        @test rec[1].args[1] isa AbstractGPs.FiniteGP
        @test rec[1].args[1].x == x
        @test rec[1].args[1].f == f
        @test isempty(rec[1].plotattributes) # no default attributes
    end

    # Checks
    @test_throws DimensionMismatch plot(rand(5), gp)
    @test_throws ErrorException plot(rand(10), gp; ribbon_scale=-0.5)
end


text:
function deckplayers(filename)

    lines = open(filename) do file
        readlines(file)
    end

    player1 = Int[]
    player2 = Int[]

    first_user = true

    for line = lines[2:end]
        if startswith(line, "Player")
            first_user = false
            continue
        end
        if line == ""
            continue
        end
        value = parse(Int, line)
        first_user ?  push!(player1, value) : push!(player2, value)
    end

    return (player1, player2)
end


text:
function (::Type{MT})(df::SignedDistanceField{3,ST,FT}, method::AbstractMeshingAlgorithm)::MT where {MT <: AbstractMesh, ST, FT}
    vertex_eltype = promote_type(FT, typeof(method.iso), typeof(method.eps))
    VertType, FaceType = _determine_types(MT, vertex_eltype, default_face_length(method))
    h = df.bounds
    vts, fcs = isosurface(df.data, method, VertType, FaceType, origin=VertType(origin(h)), widths=VertType(widths(h)))
    MT(vts, fcs)::MT
end

function (::Type{MT})(f::Function, h::HyperRectangle, samples::NTuple{3,T}, method::AbstractMeshingAlgorithm)::MT where {MT <: AbstractMesh, T <: Integer}
    vertex_eltype = promote_type(T, typeof(method.iso), typeof(method.eps))
    VertType, FaceType = _determine_types(MT,vertex_eltype, default_face_length(method))
    vts, fcs = isosurface(f, method, VertType, FaceType, samples=samples, origin=VertType(origin(h)), widths=VertType(widths(h)))
    MT(vts, fcs)::MT
end

function (::Type{MT})(f::Function, h::HyperRectangle, method::AbstractMeshingAlgorithm; samples::NTuple{3,T}=_DEFAULT_SAMPLES)::MT where {MT <: AbstractMesh, T <: Integer}
    vertex_eltype = promote_type(T, typeof(method.iso), typeof(method.eps))
    VertType, FaceType = _determine_types(MT,vertex_eltype, default_face_length(method))
    vts, fcs = isosurface(f, method, VertType, FaceType, samples=samples, origin=VertType(origin(h)), widths=VertType(widths(h)))
    MT(vts, fcs)::MT
end

function (::Type{MT})(volume::AbstractArray{T, 3}, method::AbstractMeshingAlgorithm; vargs...) where {MT <: AbstractMesh, T}
    vertex_eltype = promote_type(T, typeof(method.iso), typeof(method.eps))
    VertType, FaceType = _determine_types(MT,vertex_eltype, default_face_length(method))
    vts, fcs = isosurface(volume, method, VertType, FaceType, vargs...)
    MT(vts, fcs)::MT
end


text:
"""
Julia implementation of Daniel Lemire's Streaming Maximum-Minimum Filter:
'''Streaming Maximum-Minimum Filter Using No More than Three Comparisons per Element'''
http://arxiv.org/abs/cs.DS/0610046
"""
module MaxMinFilters

export movmaxmin, movmaxmin!,
    movmax, movmax!,
    movmin, movmin!,
    movrange, movrange!,
    movenvelope, movenvelope!,

    MaxMinFilter, RangeFilter, MaxFilter, MinFilter, EnvelopeFilter,
    run, run!, filt, filt!, reset!, delay

include("ring_buffer.jl")
include("movmaxmin.jl")
include("movmaxmin_stateful.jl")

end # module

# how should delays be fixed, add a kw to mov functinos?

# naming of streaming algorithms: should all moving functions start with `mov`?
# naming of stateful functions - should they be named as filters in DSP terminology?

# relation to other packages, such as DSP, rolling functions, indicators -
# how should we make a concise libraries?

# what notation is better for stateful filters: run! / filter! / movmaxmin! ?

# should I split filter into different objects (MinMaxFilter, MinFilter, MaxFilter, RangeFilter),
# or use just one object with several filtering methods/modes?


text:
# ...................................................... FITS objects .........................................................

"""
    FITS_name

FITS object to decompose the names of .fits files.

The fields are:
* `.name::String`: for filename 'p#.fits' this is 'p#.fits'
* `.prefix::String`: for filename 'p#.fits' this is 'p'
* `.numerator::String`: for filename 'p#.fits' this is '#', a serial number (e.g., '3') or a range (e.g., '3-7')
* `.extension::String`:  for filename 'p#.fits' this is '.fits'.
"""
struct FITS_name

    name::String
    prefix::String
    numerator::String
    extension::String

end

"""
    FITS_HDU

Object to hold a single "Header-Data Unit" (HDU).

The fields are
* `.filename::String`: name of the corresponding FITS file
* `.hduindex::Int`: identifier (a file may contain more than one HDU)
* `.header::T, where T=FITS_header`: the header object
* `.dataobject::V, where V=FITS_data`: the data object
"""
struct FITS_HDU{T,V}

    filename::String
    hduindex::Int
    header::T
    dataobject::V

end

"""
    FITS_header

Object to hold the header information of a `FITS_HDU`.

The fields are:
* `.hduindex::Int`: identifier (a file may contain more than one HDU)
* `.records::Array{String,1}`:  the header formated as an array of strings of 80 ASCII characters
* `.keys::Array{String,1}`: `keys[i]` - key corresponding to `records[i]` (record of index `i`)
* `.values::Array{Any,1}`: `value[i]` - corresponding to `records[i]`
* `.comments`: `comments[i]` - comment corresponding to `records[i]`
* `.dict::Dict{String,Any}`: Dictionary `key[i] => value[i]`
* `.maps::Dict{String,Int}`: Dictionary `key[i] => i`
"""
struct FITS_header

    hduindex::Int
    records::Array{String,1}
    keys::Array{String,1}
    values::Array{Any,1}
    comments::Array{String,1}
    dict::Dict{String,Any}
    maps::Dict{String,Int}

end

# ........................................... FITS_data object ..........................................................

"""
    FITS_data

Object to hold the data of the `FITS_HDU` of given `hduindex` and `hdutype`.

The fields are:
* `.hduindex::Int`: identifier (a file may contain more than one HDU)
* `.hdutype::String`: accepted types are 'PRIMARY', 'IMAGE' and 'TABLE'
* `.data::Any`: in the from appropriate for the `hdutype`
"""
struct FITS_data

    hduindex::Int
    hdutype::String
    data

end

"""
    FITS_table

Object to hold the data of a `TABLE HDU` (a `FITS_HDU` for ASCII tables). It contains the data in the form of records (rows) of ASCII strings.

The fields are:
* `.hduindex::Int`: identifier (a file may contain more than one HDU)
* `.rows::Array{String,1}`: the table formated as an array of rows of ASCII strings
"""
struct FITS_table

    hduindex::Int
    rows::Array{String,1}

end


text:
export
		at,
		get_size,
		set_at,
		set_size

function at( dblarr::DoubleArray, index::Integer )

	error_buffer = Array(UInt8, 128)
	retval = ccall( (:doublearray_At, LIB_SMILE), Int32, (Ptr{Void},Int32,Ptr{UInt8}), dblarr.ptr, index, error_buffer )
	error_str = bytestring(error_buffer)
	if startswith(error_str, "error")
		error( "doublearray_At: ", error_str )
	end
	return retval
end

function get_size( dblarr::DoubleArray )

	retval = ccall( (:doublearray_GetSize, LIB_SMILE), Int32, (Ptr{Void},), dblarr.ptr )
	return retval
end

function set_at( dblarr::DoubleArray, index::Integer, value::AbstractFloat )
	# indexing starts at 0

	error_buffer = Array(UInt8, 128)
	ccall( (:doublearray_SetAt, LIB_SMILE), Int32, (Ptr{Void},Int32,Float64,Ptr{UInt8}), dblarr.ptr, index, value, error_buffer )
	error_str = bytestring(error_buffer)
	if startswith(error_str, "error")
		error( "doublearray_At: ", error_str )
	end
end

function set_size( dblarr::DoubleArray, thisSize::Integer )

	err_int = ccall( (:doublearray_SetSize, LIB_SMILE), Int32, (Ptr{Void},Int32), dblarr.ptr, thisSize )
	if err_int < 0
		error( "doublearray_SetSize: ", err_int )
	end
end


text:
"""
	grdfilter(cmd0::String="", arg1=nothing, kwargs...)

Filter a grid file in the time domain using one of the selected convolution or non-convolution
isotropic or rectangular filters and compute distances using Cartesian or Spherical geometries.

Full option list at [`grdfilter`]($(GMTdoc)grdfilter.html)

Parameters
----------

- **F** | **filter** :: [Type => Str]

    Sets the filter type.
    ($(GMTdoc)grdfilter.html#f)
- **D** | **distflag** | **distance** :: [Type => Number]

    Distance flag tells how grid (x,y) relates to filter width.
    ($(GMTdoc)grdfilter.html#d)
- **G** | **outgrid** :: [Type => Str]

    Output grid file name. Note that this is optional and to be used only when saving
    the result directly on disk. Otherwise, just use the G = grdfilter(....) form.
    ($(GMTdoc)grdfilter.html#g)
- **I** | **inc** :: [Type => Str or Number]

    *x_inc* [and optionally *y_inc*] is the grid spacing.
    ($(GMTdoc)grdfilter.html#i)
- **N** | **nans** :: [Type => Str]

    Determine how NaN-values in the input grid affects the filtered output. Values are i|p|r
    ($(GMTdoc)grdfilter.html#n)
- $(GMT.opt_R)
- **T** | **toggle** :: [Type => Bool]

    Toggle the node registration for the output grid so as to become the opposite of the input grid
    ($(GMTdoc)grdfilter.html#t)
- $(GMT.opt_V)
- $(GMT.opt_f)
"""
function grdfilter(cmd0::String="", arg1=nothing; kwargs...)

	length(kwargs) == 0 && return monolitic("grdfilter", cmd0, arg1)

	d = init_module(false, kwargs...)[1]		# Also checks if the user wants ONLY the HELP mode
	cmd, = parse_common_opts(d, "", [:R :I :V_params :f])
	cmd  = parse_these_opts(cmd, d, [[:D :distflag :distance], [:F :filter], [:G :outgrid], [:N :nans], [:T :toggle]])

	common_grd(d, cmd0, cmd, "grdfilter ", arg1)		# Finish build cmd and run it
end

# ---------------------------------------------------------------------------------------------------
grdfilter(arg1, cmd0::String=""; kw...) = grdfilter(cmd0, arg1; kw...)


text:
using AstroImages, FITSIO, Images
using Test

import AstroImages: _float, render

@testset "Conversion to float and fixed-point" begin
    @testset "Float" begin
        for T in (Float16, Float32, Float64)
            @test _float(T(-9.8)) === T(-9.8)
            @test _float(T(12.3)) === T(12.3)
        end
    end
    @testset "Integers" begin
        for (UIT, SIT) in ((UInt8,  Int8),
                           (UInt16, Int16),
                           (UInt32, Int32),
                           (UInt64, Int64))
            N = sizeof(UIT) * 8
            NT = Normed{UIT, N}
            maxint = UIT(big(2) ^ (N - 1))
            @test _float(typemin(UIT)) === _float(typemin(SIT)) === NT(0)
            @test _float(UIT(85)) === reinterpret(NT, UIT(85))
            @test _float(SIT(-85)) === _float(UIT(-85 + big(maxint)))
            @test _float(SIT(115)) === _float(UIT(115) + maxint)
            @test _float(typemax(UIT)) === _float(typemax(SIT)) === NT(1)
        end
    end
end

@testset "FITS and images" begin
    fname = tempname() * ".fits"
    for T in [UInt8, Int8, UInt16, Int16, UInt32, Int32, Int64,
              Float32, Float64]
        data = reshape(T[1:100;], 5, 20)
        FITS(fname, "w") do f
            write(f, data)
        end
        @test load(fname) == data
        @test load(fname, (1, 1)) == (data, data)
        img = AstroImage(fname)
        rendered_img = render(img)
        @test iszero(minimum(rendered_img))
        @test convert(Matrix{Gray}, img) == rendered_img
    end
    rm(fname, force=true)
end

include("plots.jl")


text:
using Luxor

begin
    Drawing(142, 56, "logo.svg")
    origin()
    fontsize(50)
    fontface("Arial Rounded MT Bold")
    translate(-71, 12)
    text("\"{:", halign=:left, valign=:baseline)
    @show textpath("\"{:")
    translate(79, -10)
    juliacircles(11)
    translate(18, 10)
    text("}\"", halign=:left, valign=:baseline)
    @show textextents("}\"")
    # textextents
    finish()
end


text:
# coding:utf-8

include("switch_depwarn.jl")
switch_depwarn!(false)

using DataFrames
using DataStructures
using PyCall
using JSON

unshift!(PyVector(pyimport("sys")["path"]), "")
@pyimport treetagger

doc=
"""
アノテーション済みセッションの抽出
json形式出力
前処理：
  urlをreplaced-url
  数字を@card@
形態素解析：
  見出し語化
  品詞タグセットはtree-taggerタグセット
"""

#parser = "nltk"
parser = "tagger"

# dataframes
da = readtable("./data/dialogueact.dat", separator = '\t')
dialogue = readtable("./data/message.dat", separator = '\t')

# tokenizer
@pyimport nltk

# ngram
function ngram(string, n)
  words = []
  !contains(summary(string),"Array") && (string = utf32(string))
  for i in collect(1:(length(string)-n+1))
    push!(words, string[i:i+n-1])
  end
  return words
end

# unigram & bigram
@show "creating ngram feature ..."
unigram, bigram, pos = [], [], []
for (i, message) in enumerate(dialogue[:content])
  i % 1000 == 0 && println(i)
  message =join(map(x->replace(x,r"^http.+","replaced-url"), vcat(split(message))), " ")
  token = []
  if parser == "tagger"
    seg = [(x[3], x[2]) for x in map(w->split(w, "\t"), treetagger.parse(message))]
    pos_tag = [x[2] for x  in seg]
    token = vcat("<s>", vcat([x[1] for x  in seg]), "</s>")
    push!(pos, pos_tag)
  else
    token = vcat("<s>", vcat(nltk.word_tokenize(message), "</s>"))
    map!(x -> replace(x, r"^\d+$", "9999"), token)
  end
  push!(unigram, token)
  push!(bigram, map(x -> join(x, "@"), ngram(token, 2)))
end
parser == "tagger" && (dialogue[:pos] = pos)
dialogue[:unigram] = map(x->map(uppercase, x), unigram)
dialogue[:bigram] = map(x->map(uppercase, x), bigram)

@show "extracting annotated session ..."
d, c = Dict(), 0
for sid in collect(1:800)
  # 8発話以上のセッション抽出
  df_message = dialogue[dialogue[:conversationId] .== string(sid), :]
  length(df_message[1]) < 8 && continue
  function_da, domain_da, utterance, speaker, sequence, unigram, bigram, pos, i = [], [], [], [], [], [], [], [], 0
  for (i, mid) in enumerate(df_message[:messageId])
    df_da = da[da[:MessageId] .== mid, :]
    # NAを置き換え
    df_da[isna(df_da[:functionDALabelId]), :functionDALabelId] = 1000
    df_da[isna(df_da[:domainDALabelId]), :domainDALabelId] = 2000
    length(df_da[1]) == 0 && break
    # push
    push!(function_da, sort(collect(counter(df_da[:functionDALabelId])), by = x->x[2])[end][1])
    push!(domain_da, sort(collect(counter(df_da[:domainDALabelId])), by = x->x[2])[end][1])
    push!(speaker, parse(df_message[:speaker][i]))
    push!(sequence, parse(df_message[:sequence][i]))
    push!(utterance, df_message[:content][i])
    push!(unigram, df_message[:unigram][i])
    push!(bigram, df_message[:bigram][i])
    parser == "tagger" &&  push!(pos, df_message[:pos][i])
  end
  # すべてアノテーション済みの場合
  if length(df_message[1]) == i
    if parser == "tagger"
      d[sid] = Dict(:function_da => [], :domain_da => [], :utterance => [], :speaker => [],
        :sequence => [], :unigram => [], :bigram => [], :pos => [])
    else
      d[sid] = Dict(:function_da => [], :domain_da => [], :utterance => [], :speaker => [],
        :sequence => [], :unigram => [], :bigram => [])
    end
    tmp = collect(1:length(sequence))
    # sort
    for (i, seq) in enumerate(sort(sequence, rev=true))
      tgt = tmp[i]
      push!(d[sid][:function_da], function_da[tgt])
      push!(d[sid][:domain_da], domain_da[tgt])
      push!(d[sid][:utterance], utterance[tgt])
      push!(d[sid][:speaker], speaker[tgt])
      push!(d[sid][:sequence], i)
      push!(d[sid][:unigram], unigram[tgt])
      push!(d[sid][:bigram], bigram[tgt])
      parser == "tagger" &&  push!(d[sid][:pos], pos[tgt])
    end
    c += 1
    c % 10 == 0 && println(c)
  end
end

# シリアライズ
open(io -> serialize(io, d), "./data/corpus.dic", "w")
JSON.print(open("./data/corpus.json", "w"), JSON.json(d))

# デシリアライズ
# d = open(deserialize, "corpus.dic")
# d = JSON.parse(JSON.parsefile("annotated_session.json", dicttype=Dict))


text:
@testset "b-matching $G" begin

for seed=1:20
    Random.seed!(seed)
    n = 30
    g = random_regular_graph(n, 10)
    w = EdgeMap(g, e -> rand())
    for b =2:4
        status, W, mates = minimum_weight_perfect_bmatching(g, b, w)
        @test Int(status) == 1
        @test  0 < W < b*n/2
        @test length.(mates) == fill(b, n)
        mts = vcat(mates...)
        for i=1:nv(g)
            count(x->x==i, mts) == b
        end

        status2, W2, mates2 = minimum_weight_perfect_bmatching(g, b, w, cutoff=0.9)
        @test Int(status2) == 1
        @test  0 < W2 < n
        @test length.(mates2) == fill(b, nv(g))
        mts = vcat(mates2...)
        for i=1:nv(g)
            count(x->x==i, mts) == b
        end
        #@test mates == mates2
    end
end

end # testset


text:
using PhaseRetrieval
using FFTW
using Distances
using Test

@testset "PhaseRetrieval.jl" begin
    @test begin # see Gerchberg's original paper
        c1 = [Int(abs(2t)<=1)*exp(im*15*pi*t^2) for t in -1:(1/256):(255/256)]
        c2 = fft(c1)
        r1, r2 = gerchbergsaxton(abs.(c1), abs.(c2), maxiter=130, init=c1)
        msd(c1, r1) < 0.01 && msd(c2, r2) < 0.01
    end
end


text:
"""
	struct ExtractDict{S} <: AbstractExtractor
		dict::S
	end

extracts all items in `dict` and return them as a `Mill.ProductNode`.
If a key is missing in extracted dict, `nothing` is passed to the child extractors.

# Examples

```jldoctest
julia> e = ExtractDict(Dict(:a=>ExtractScalar(Float32, 2, 3), :b=>ExtractCategorical(1:5)))
Dict
  ├── a: Float32
  └── b: Categorical d = 6

julia> res1 = e(Dict("a"=>1, "b"=>1))
ProductNode \t# 1 obs, 24 bytes
  ├── a: ArrayNode(1×1 Array with Union{Missing, Float32} elements) \t# 1 obs, 53 bytes
  └── b: ArrayNode(6×1 MaybeHotMatrix with Union{Missing, Bool} elements) \t# 1 obs, 77 bytes

julia> res1[:a].data
1×1 Matrix{Union{Missing, Float32}}:
 -3.0f0

julia> res1[:b].data
6×1 MaybeHotMatrix with eltype Union{Missing, Bool}:
  true
   ⋅
   ⋅
   ⋅
   ⋅
   ⋅

julia> res2 = e(Dict("a"=>0))
ProductNode \t# 1 obs, 24 bytes
  ├── a: ArrayNode(1×1 Array with Union{Missing, Float32} elements) \t# 1 obs, 53 bytes
  └── b: ArrayNode(6×1 MaybeHotMatrix with Union{Missing, Bool} elements) \t# 1 obs, 77 bytes

julia> res2[:a].data
1×1 Matrix{Union{Missing, Float32}}:
 -6.0f0

julia> res2[:b].data
6×1 MaybeHotMatrix with eltype Union{Missing, Bool}:
 missing
 missing
 missing
 missing
 missing
 missing
```
"""
struct ExtractDict{S} <: AbstractExtractor
	dict::S
	function ExtractDict(d::S) where {S<:Dict}
		new{typeof(d)}(d)
	end
end

function Base.getindex(m::ExtractDict, s::Symbol)
	!isnothing(m.dict) && haskey(m.dict, s) && return m.dict[s]
	nothing
end

Base.keys(e::ExtractDict) = keys(e.dict)

replacebyspaces(pad) = map(s -> (s[1], " "^length(s[2])), pad)
(s::ExtractDict)(::MissingOrNothing; store_input=false) = s(Dict{String, Any}())
(s::ExtractDict)(v; store_input=false)  = s(nothing)

function (s::ExtractDict{S})(v::AbstractDict; store_input=false) where {S<:Dict}
	o = [Symbol(k) => f(get(v,String(k),nothing); store_input) for (k,f) in s.dict]
	ProductNode((; o...))
end

function (s::ExtractDict{S})(ee::ExtractEmpty; store_input=false) where {S<:Dict}
	o = [Symbol(k) => f(ee; store_input) for (k,f) in s.dict]
	ProductNode((; o...))
end

extractbatch(extractor, samples) = mapreduce(extractor, catobs, samples)

Base.hash(e::ExtractDict, h::UInt) = hash(e.dict, h)
Base.:(==)(e1::ExtractDict, e2::ExtractDict) = e1.dict == e2.dict


text:
using TSML
using TSMLextra
using DataFrames
using Base.Threads
nthreads()

function predict(learner,data,train_ind,test_ind)
    features = convert(Matrix,data[:, 1:(end-1)])
    labels = convert(Array,data[:, end])
    # Create pipeline
    pipeline = Pipeline(
       Dict(
         :transformers => [
           OneHotEncoder(), # Encodes nominal features into numeric
           Imputer(), # Imputes NA values
           StandardScaler(),
           learner # Predicts labels on instances
         ]
       )
    )
    # Train
    fit!(pipeline, features[train_ind, :], labels[train_ind]);
    # Predict
    predictions = transform!(pipeline, features[test_ind, :]);
    # Assess predictions
    result = score(:accuracy, labels[test_ind], predictions)
    return result,pipeline
end

function threadedmodel(learners::Dict,data::DataFrame;trials=5)
    models=collect(keys(learners))
    global ctable = DataFrame()
    mtx = ReentrantLock()
    @threads for i=1:trials
        # Split into training and test sets
        Random.seed!(3i)
        (train_ind, test_ind) = holdout(size(data, 1), 0.20)
        @threads for model in models
            res,_=predict(learners[model],data,train_ind,test_ind)
            println("thread ",threadid(),", ",model," => ",round(res))
            lock(mtx)
            global ctable = vcat(ctable,DataFrame(model=model,acc=res))
            unlock(mtx)
        end
    end
    df = ctable |> DataFrame
    gp=by(df,:model) do x
       DataFrame(mean=mean(x.acc),std=std(x.acc),n=nrow(x))
    end
    sort!(gp,:mean,rev=true)
    return gp
end

# Caret ML
caret_svmlinear = CaretLearner(Dict(:learner=>"svmLinear"))
caret_treebag = CaretLearner(Dict(:learner=>"treebag"))
caret_rpart = CaretLearner(Dict(:learner=>"rpart"))
caret_rf = CaretLearner(Dict(:learner=>"rf"))

# ScikitLearn ML
sk_ridge = SKLearner(Dict(:learner=>"RidgeClassifier"))
sk_sgd = SKLearner(Dict(:learner=>"SGDClassifier"))
sk_knn = SKLearner(Dict(:learner=>"KNeighborsClassifier"))
sk_gb = SKLearner(Dict(:learner=>"GradientBoostingClassifier",:impl_args=>Dict(:n_estimators=>10)))
sk_extratree = SKLearner(Dict(:learner=>"ExtraTreesClassifier",:impl_args=>Dict(:n_estimators=>10)))
sk_rf = SKLearner(Dict(:learner=>"RandomForestClassifier",:impl_args=>Dict(:n_estimators=>10)))

# Julia ML
jrf = RandomForest(Dict(:impl_args=>Dict(:num_trees=>300)))
jpt = PrunedTree()
jada = Adaboost()

# Julia Ensembles
jvote_ens=VoteEnsemble(Dict(:learners=>[jrf,jpt,sk_gb,sk_extratree,sk_rf]))
jstack_ada=StackEnsemble(Dict(:stacker=>Adaboost(),:learners=>[jrf,jpt,sk_gb,sk_extratree,sk_rf]))
jstack_rf=StackEnsemble(Dict(:learners=>[jrf,jpt,sk_gb,sk_extratree,sk_rf]))
jbest_ens=BestLearner(Dict(:learners=>[jrf,sk_gb,sk_rf]))
jsuper_ens=VoteEnsemble(Dict(:learners=>[jvote_ens,jstack_ada,jstack_rf,jbest_ens,sk_rf,sk_gb]))

jstack_rf=StackEnsemble(Dict(:learners=>[jrf,jpt,jada]))
jbest_ens=BestLearner(Dict(:learners=>[jrf,jpt,jada]))
jstack_ada=StackEnsemble(Dict(:stacker=>Adaboost(),:learners=>[jrf,jpt,jada]))
jvote_ens=VoteEnsemble(Dict(:learners=>[jrf,jpt,jada]))
jsuper_ens=VoteEnsemble(Dict(:learners=>[jvote_ens,jstack_ada,jstack_rf,jbest_ens]))

#learners=Dict(
#      :jvote_ens=>jvote_ens,:jstack_rf=>jstack_rf,:jbest_ens=>jbest_ens, :jstack_ada=>jstack_ada,
#      :jrf => jrf,:jada=>jada,:jsuper_ens=>jsuper_ens,:crt_rpart=>caret_rpart,
#      :skl_knn=>sk_knn,:skl_gb=>sk_gb,:skl_extratree=>sk_extratree,
#      :sk_rf => sk_rf
#);

learners=Dict(
      :jrf => jrf,:jada=>jada,:jpt=>jpt,
      :jvote_ens=>jvote_ens,:jstack_rf=>jstack_rf,:jbest_ens=>jbest_ens,:jstack_ada=>jstack_ada,
      :jsuper_ens=>jsuper_ens
);

# use iris dataset
using RCall
iris = R"iris"|> rcopy
first(iris,5)

df = threadedmodel(learners,iris;trials=10)

function extract_features_from_timeseries(datadir)
  println("*** Extracting features ***")
  mdata = getstats(datadir)
  mdata[!,:dtype] = mdata[!,:dtype] |> Array{String}
  return mdata[!,3:(end-1)]
end

datadir = "data/realdatatsclassification/training"
tsdata = extract_features_from_timeseries(datadir)
first(tsdata,5)

df = threadedmodel(learners,tsdata;trials=20)

(train_ind, test_ind) = holdout(size(tsdata, 1), 0.20)
res,workflow=predict(learners[:jbest_ens],tsdata,train_ind,test_ind)
res

showtree(workflow)

max_stars_repo_name: ppalmes/TSMLextra

max_issues_repo_name: ppalmes/TSMLextra

max_forks_repo_name: ppalmes/TSMLextra


text:
# initialize incoming spikes queue for each synapse

using ProgressMeter
using HDF5, JLD

function SpikingLayersNet()
    g = simple_graph(0)
    # I don't think Julia can create empty sparse matrices of arbitrary type
    # that can be flexibly resized...
    SpikingLayersNet(g,AbstractSpikingLayer[], Vector{SpikingConnection}[], Vector{SpikingConnection}[])
end

## Loading

function save(net::SpikingLayersNet, filename::ASCIIString)
    # saves the weights and state of the network
    save(filename,"net",net)
end

function loadNet(filename::ASCIIString)
    d = load(filename)
    d["net"]
end

# CONNECTING

function addSpikingConnection!(net::SpikingLayersNet, c::SpikingConnection)
    # TODO - this currently adds another synapse. need flag to
    # specify whether to overwrite an existing connection
    if c.s == 0
        error("Presynaptic layer not specified")
    end
    if c.t == 0
        error("Postsynaptic layer not specified")
    end
    add_edge!(net.g,c.s,c.t)
    push!(net.post[c.s],c)
    push!(net.pre[c.t],c)
end

function connect!(net::SpikingLayersNet, pre::ASCIIString, post::ASCIIString, c::SpikingConnection)
    pred = (n) -> l.name == n # comparator function
    s = findfirst(l->l.name==pre,net.l)
    t = findfirst(l->l.name==post,net.l)
    if s == 0
        error("Layer '$pre' not found")
    end
    if t == 0
        error("Layer '$post' not found")
    end
    c.s = s
    c.t = t
    addSpikingConnection!(net,c)
end

function add_layer!(net::SpikingLayersNet, layer::AbstractSpikingLayer)
    nv = num_vertices(net.g)
    push!(net.l, layer)
    v = add_vertex!(net.g)
    # extend matrix
    push!(net.pre, Vector{SpikingConnection}[])
    push!(net.post, Vector{SpikingConnection}[])
    layer
end

function normalizeWeights!(net::SpikingLayersNet)
    for conn_arr in net.pre
        for conn in conn_arr
            normalizeWeights!(conn)
        end
    end
end

function resetLayers!(net::SpikingLayersNet)
    for layer in net.l
        reset!(layer)
    end
end

### SIMULATION

function runSpikingNet!(net::SpikingLayersNet, settings::RunSettings)
    results = RunResults(net,settings)
    T=settings.T
    #stim_generator=settings.stim
    stim_spikes = settings.stim_spikes
    time_trace=0:dt:T
    nsteps=length(time_trace)

    if SHOW_PROGRESS
        update_interval = 2 # seconds
        progress = Progress(nsteps,update_interval) # min update interval = 2 sec
    end
    # simulate

    ts = zeros(length(net.l))
    for i=2:nsteps
        t = time_trace[i]
        ts[:] = t
        # parallelized version - update all the layers, then update all the synapses (needs layers)
        # to finish updating first

        # inputs to each layer at this given timestep
        stims = [(s == nothing) ? nothing : s[:,i] for s in stim_spikes]

        for (li, layer) in enumerate(net.l)
            # update layer
            update!(layer, net.pre[li], stims[li], t)
            # update synapses
            for sc in net.post[li]
                update!(sc,layer)
                if sc.stdp_enabled
                    updateSTDP!(sc, layer, net.l[sc.t])
                end
            end
        end
        recordSpikes!(results, net, i)
        if SHOW_PROGRESS
            next!(progress)
        end
    end
    results
end


text:
using Test, BasicTextRender, ImageCore

@testset "BasicTextRender.jl tests" begin
    img = rendertext("Hello world")
    @test size(img) == (89, 561)

    img = rendertext("Hello world", height=30)
    @test size(img) == (30, 190)
    img = rendertext("Hello world", height=30, font="Courier New")
    @test size(img) == (31, 202)

    img = rendertext("Hello world", height=30, font="Monaco")
    @test size(img) == (30, 167)

    img = rendertext("Hello world", height=30,
                    color=RGBA(1.0,0.0,0.0,1.0))
    @test size(img) == (30, 190)

    img = rendertext("Hello world", height=30,
                    color=RGBA(1.0,0.0,0.0,1.0),
                    backgroundColor=RGBA(1.0,1.0,1.0,1.0))
    @test size(img) == (30, 190)

    img = rand(RGBA{Float64},80,700)
    overlaytext!(img, "BasicTextRender.jl", 60, (10,40))

    @test size(img) == (80, 700)

end


text:
"""
An abstract mesh in one space dimension.
"""
abstract type AbstractFVMesh1D <: AbstractFVMesh end

# AbstractFVMesh1D API
@inline numedges(mesh::AbstractFVMesh1D) = numcells(mesh)+1
@inline edge_indices(mesh::AbstractFVMesh1D) = 1:numedges(mesh)
# REMARK: Dirichlet condition values are given by initial conditions.
@inline isleftperiodic(mesh::AbstractFVMesh1D) = (mesh.left_boundary == :PERIODIC)
@inline isrightperiodic(mesh::AbstractFVMesh1D) = (mesh.right_boundary == :PERIODIC)
@inline isleftzeroflux(mesh::AbstractFVMesh1D) = (mesh.left_boundary == :ZERO_FLUX)
@inline isrightzeroflux(mesh::AbstractFVMesh1D) = (mesh.right_boundary == :ZERO_FLUX)
@inline isleftdirichlet(mesh::AbstractFVMesh1D) = (mesh.left_boundary == :DIRICHLET)
@inline isrightdirichlet(mesh::AbstractFVMesh1D) = (mesh.right_boundary == :DIRICHLET)
@inline numcells(mesh::AbstractFVMesh1D) = mesh.N
@inline cell_indices(mesh::AbstractFVMesh1D) = 1:numcells(mesh)
@inline cell_centers(mesh::AbstractFVMesh1D) = mesh.cell_centers
@inline cell_faces(mesh::AbstractFVMesh1D) = mesh.cell_faces

"""
A uniform mesh in one space dimension of `N` cells
"""
struct Uniform1DFVMesh{T} <: AbstractFVMesh1D
  N ::Int
  Δx :: T
  cell_centers :: Vector{T}
  cell_faces :: Vector{T}
  left_boundary::Symbol
  right_boundary::Symbol
end

function Uniform1DFVMesh(N::Int,xinit::Real,xend::Real,leftbdtype=:ZERO_FLUX,rightbdtype=:ZERO_FLUX)
    L = xend - xinit
    dx = L/N
    xx = [i*dx+dx/2+xinit for i in 0:(N-1)]
    faces = [xinit + dx*i for i in 0:N]
    Uniform1DFVMesh(N,dx,xx,faces,leftbdtype,rightbdtype)
end

@inline cell_volume(cell::Int, mesh::Uniform1DFVMesh) = mesh.Δx
@inline cell_volumes(mesh::Uniform1DFVMesh) = mesh.Δx * ones(mesh.N)

### Boundary helper functions
function getPeriodicIndex(A::AbstractArray{T,2}, I...) where {T}
    checkbounds(Bool, A, I...) && return A[I...]
    if typeof(I[1]) <: Int
      return A[mod1(I[1], size(A,1)),I[2]]
    else
      return A[[mod1(i, size(A,1)) for i in I[1]],I[2]]
    end
end

# function getDirichletIndex(A::AbstractArray{T,2}, I...) where {T}
#     checkbounds(Bool, A, I...) && return A[I...]
#     I... < 1 ? A.oob_val[1] : A.oob_val[2]
# end

# Reflective boundaries
# function getZFindex(A::AbstractArray{T,2}, I...) where {T}
#     checkbounds(Bool, A, I...) && return A[I...]
#     k::Int64 = indices(A,1)[end]
#     if typeof(I[1]) <: Int
#       a = I[1] < 1 ? 1:-1
#       return A[k+1+a-mod1(I[1],k),I[2]]
#     else
#       return A[[checkindex(Bool, indices(A)[1], i) ? i : k + 1 + (i < 1 ? 1:-1) - mod1(i,k) for i in I[1]],I[2]]
#     end
# end

function getZFindex(A::AbstractArray{T,2}, I...) where {T}
    checkbounds(Bool, A, I...) && return A[I...]
    if typeof(I[1]) <: Int
      return A[min(size(A,1),max(1,I[1])),I[2]]
    else
      return A[[min(size(A,1),max(1,i)) for i in I[1]],I[2]]
    end
end

# function getZFrow(A, idx, idc)
#     if checkindex(Bool, indices(A)[1], idx)
#         return A[idx,idc]
#     else
#         a = interpolate!(A, BSpline(Linear()), OnGrid())
#         a = extrapolate(a, Linear())
#         return a[idx,idc]
#     end
# end

#Zero flux with recommendation from
#C.-W. Shu, Essentially non-oscillatory and weighted essentially non-oscillatory
#schemes for hyperbolic conservation laws.
#In: B. Cockburn, C. Johnson, C.-W. Shu and E. Tadmor
# function getZFrow(A, idx, idc)
#     if checkindex(Bool, indices(A)[1], idx)
#         return A[idx,idc]
#     else
#         k::typeof(idx) = min(abs(idx-indices(A)[1][1]), abs(idx - indices(A)[1][end]))
#         if typeof(idc) <: Int
#           return (10*k)^10*one(eltype(A))
#         else
#           return (10*k)^10*ones(A[indices(A)[1][1],idc])
#         end
#     end
# end
#
# function getZFindex(A::AbstractArray{T,2}, I...) where {T}
#     checkbounds(Bool, A, I...) && return A[I...]
#     if typeof(I[1]) <: Int
#       return getZFrow(A,I[1],I[2])
#     else
#         idend::Int64 = (I[2] == Colon() ? size(A,2) : size(I[2],1))
#         if idend > 1
#             B = zeros(eltype(A), size(I[1],1), idend)
#             for (k,j) in enumerate(I[1])
#                 B[k,:] = getZFrow(A,j,I[2])
#             end
#             return B
#         else
#             B = zeros(eltype(A), size(I[1],1))
#             for (k,j) in enumerate(I[1])
#                 B[k] = getZFrow(A,j,I[2])
#             end
#             return B
#         end
#     end
# end

# Reference for cell and edge numbers
#   1   2   3          N-1  N
# |---|---|---|......|---|---|
# 1   2   3   4 ... N-1  N  N+1
"""
    get_cellvals(A::AbstractArray{T,2}, idx..., mesh::AbstractFVMesh1D) where {T}
   cell values of variable `A` on cells `idx` of `mesh`.
"""
@inline function get_cellvals(A::AbstractArray{T,2}, mesh::AbstractFVMesh1D, idx...) where {T}
    checkbounds(Bool, A, idx...) && return A[idx...]
    if (minimum(idx[1]) < 1) && isleftperiodic(mesh)
        getPeriodicIndex(A, idx...)
    elseif (minimum(idx[1]) < 1) && (isleftzeroflux(mesh) || isleftdirichlet(mesh))
        getZFindex(A,idx...)
    elseif (maximum(idx[1]) > numcells(mesh)) && isrightperiodic(mesh)
        getPeriodicIndex(A, idx...)
    elseif (maximum(idx[1]) > numcells(mesh)) && (isrightzeroflux(mesh) || isrightdirichlet(mesh))
        getZFindex(A,idx...)
    else
        error("To be implemented.")
    end
end

"""
    cellval_at_left(edge::Int, A::AbstractArray{T,2}, mesh::AbstractFVMesh1D) where {T}

   cell values of variable `A` to the left of `edge` in `mesh`.
"""
@inline function cellval_at_left(edge::Int, A::AbstractArray{T,2}, mesh::AbstractFVMesh1D) where {T}
    idx = (edge-1,:)
    checkbounds(Bool, A, idx...) && return A[idx...]
    if isleftperiodic(mesh)
        getPeriodicIndex(A, idx...)
    elseif isleftzeroflux(mesh) || isleftdirichlet(mesh)
        getZFindex(A,idx...)
    else
        error("To be implemented.")
    end
end

"""
    cellval_at_right(edge::Int, A::AbstractArray{T,2}, mesh::AbstractFVMesh1D) where {T}

cell values of variable `A` to the right of `edge` in `mesh`.
"""
@inline function cellval_at_right(edge::Int, A::AbstractArray{T,2}, mesh::AbstractFVMesh1D) where {T}
    idx = (edge,:)
    checkbounds(Bool, A, idx...) && return A[idx...]
    if isrightperiodic(mesh)
        getPeriodicIndex(A, idx...)
    elseif isrightzeroflux(mesh) || isrightdirichlet(mesh)
        getZFindex(A,idx...)
    else
        error("To be implemented.")
    end
end

"""
    left_edge(cell::Int, mesh::AbstractFVMesh1D)

The index of the edge to the left of `cell` in `mesh`.
"""
@inline function left_edge(cell::Int, mesh::AbstractFVMesh1D)
    @boundscheck begin
        assert(1 <= cell <= numcells(mesh))
    end
    cell
end

"""
    right_edge(cell::Int, mesh::AbstractFVMesh1D)

The index of the edge to the right of `cell` in `mesh`.
"""
@inline function right_edge(cell::Int, mesh::AbstractFVMesh1D)
    @boundscheck begin
        assert(1 <= cell <= numcells(mesh))
    end
    cell+1
end

max_stars_repo_name: Paulms/DiscontinuousGalerkin

max_issues_repo_name: Paulms/DiscontinuousGalerkin

max_forks_repo_name: Paulms/DiscontinuousGalerkin


text:
"""
solve_lambdas(S::Int64, L::Int64)
    S: number of species
    L: number of links
Returns the numerical approximation of the Lagrange multipliers for the joint degree distribution of maximum entropy
"""
function solve_lambdas(S::Int64, L::Int64)
    model = Model(Ipopt.Optimizer) # create a non-linear model
    set_silent(model)

    @variable(model, x[1:2] >= 0)  # lambdas (x) are positive real numbers
    @NLobjective(model, Max, x[1]) # could have been another objective function
    @NLobjective(model, Max, x[2]) # could have been another objective function
    @NLconstraint(model, sum(sum(k_in * exp(-x[1]*k_in - x[2]*k_out) for k_in = 0:S) for k_out = 0:S) / sum(sum(exp(-x[1]*k_in - x[2]*k_out) for k_in = 0:S) for k_out = 0:S) == L/S) # constraint for the average in-degree
    @NLconstraint(model, sum(sum(k_out * exp(-x[1]*k_in - x[2]*k_out) for k_in = 0:S) for k_out = 0:S) / sum(sum(exp(-x[1]*k_in - x[2]*k_out) for k_in = 0:S) for k_out = 0:S) == L/S) # constraint for the average out-degree
    optimize!(model) # find numerical solution
    return value.(x)
end

"""
joint_degree_dist_maxent(S::Int64, L::Int64)
    S: number of species
    L: number of links
Returns the joint degree distribution of maximum entropy given S and L
"""
function joint_degree_dist_maxent(S::Int64, L::Int64)
    x = solve_lambdas(S, L) # approximate the Lagrange multipliers

    joint_degree_dist = zeros(S+1, S+1) # create matrix for the joint degree distribution (S+1 rows and columns because in and out-degrees go from 0 to S)

    Z = sum(sum(exp(-x[1]*k_in - x[2]*k_out) for k_in = 0:S) for k_out = 0:S) # compute partition function (denominator of the MaxEnt distribution)

    # compute probability for every combination of in and out-degrees
    for k_in in 0:S
        for k_out in 0:S
            num = exp(-x[1]*k_in - x[2]*k_out)
            joint_degree_dist[k_in + 1, k_out + 1] = num/Z
        end
    end
    return joint_degree_dist
end


text:
@testset "base" begin

@test_throws LoadError @eval begin
    foo = :bar
    CUDAdrv.@apicall(foo, ())
end

@test_throws ErrorException CUDAdrv.@apicall(:cuNonexisting, ())

@test_throws ErrorException @eval CUDAdrv.@apicall(:cuDummyAvailable, ())
@test_throws CUDAdrv.CuVersionError @eval CUDAdrv.@apicall(:cuDummyUnavailable, ())

if CUDAdrv.configured
    @test_throws_cuerror CUDAdrv.ERROR_INVALID_DEVICE CUDAdrv.@apicall(:cuDeviceGet, (Ptr{CUDAdrv.CuDevice_t}, Cint), Ref{CUDAdrv.CuDevice_t}(), length(devices()))
end

CUDAdrv.vendor()

end


text:
z = 90

r1 = z % 2
r2 = z % 4
r3 = z % 8

# println(r1)
# println(r2)
# println(r3)

rems = map(x -> z % x, [2,4,8])
foreach(println, rems)

for r in rems
    println(rems)
end


text:
lst = Pair[Pair("gold", "shiny"),
           Pair("neon", "inert"),
           Pair("sulphur", "yellow"),
           Pair("iron", "magnetic"),
           Pair("zebra", "striped"),
           Pair("star", "brilliant"),
           Pair("apple", "tasty"),
           Pair("ruby", "red"),
           Pair("dice", "random"),
           Pair("coffee", "stimulating"),
           Pair("book", "interesting")]

println("The original list: \n - ", join(lst, "\n - "))
sort!(lst; by=first)
println("\nThe list, sorted by name: \n - ", join(lst, "\n - "))
sort!(lst; by=last)
println("\nThe list, sorted by value: \n - ", join(lst, "\n - "))

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
isacidic(atom::Atom) =
    (i = which_natural_aminoacid(atom)) == 0 ? false :
    (natural_aminoacids[i].type == "Acidic")

isaliphatic(atom::Atom) =
    (i = which_natural_aminoacid(atom)) == 0 ? false :
    (natural_aminoacids[i].type == "Aliphatic")

isaromatic(atom::Atom) =
    (i = which_natural_aminoacid(atom)) == 0 ? false :
    (natural_aminoacids[i].type == "Aromatic")

isbasic(atom::Atom) =
    (i = which_natural_aminoacid(atom)) == 0 ? false :
    (natural_aminoacids[i].type == "Basic")

ischarged(atom::Atom) =
    (i = which_natural_aminoacid(atom)) == 0 ? false : (natural_aminoacids[i].charge != 0)

ishydrophobic(atom::Atom) =
    (i = which_natural_aminoacid(atom)) == 0 ? false :
    (natural_aminoacids[i].hydrophobic == true)

isneutral(atom::Atom) =
    (i = which_natural_aminoacid(atom)) == 0 ? false : (natural_aminoacids[i].charge == 0)

isnonpolar(atom::Atom) =
    (i = which_natural_aminoacid(atom)) == 0 ? false :
    (natural_aminoacids[i].polar == false)

ispolar(atom::Atom) =
    (i = which_natural_aminoacid(atom)) == 0 ? false : (natural_aminoacids[i].polar == true)

isprotein(atom::Atom; newres = nothing) =
    (atom.resname == newres || which_natural_aminoacid(atom) != 0) ? true : false

isprotein(residue::Residue; newres = nothing) =
    isprotein(residue.atoms[residue.range[begin]]; newres = newres)

backbone_atoms = ["N", "CA", "C", "O"]
isbackbone(atom::Atom; backbone_atoms = backbone_atoms) =
    which_natural_aminoacid(atom) == 0 ? false : (atom.name in backbone_atoms)

not_side_chain_atoms = ["N", "CA", "C", "O", "HN", "H", "HA", "HT1", "HT2", "HT3"]
issidechain(atom::Atom; not_side_chain_atoms = not_side_chain_atoms) =
    which_natural_aminoacid(atom) == 0 ? false : (!(atom.name in not_side_chain_atoms))

water_residues = ["HOH", "OH2", "TIP3", "TIP3P", "TIP4P", "TIP5P", "TIP7P", "SPC", "SPCE"]
iswater(atom::Atom; water_residues = water_residues) =
    (findfirst(isequal(atom.resname), water_residues) != nothing)

max_stars_repo_name: mcubeg/PDBTools

max_issues_repo_name: mcubeg/PDBTools

max_forks_repo_name: mcubeg/PDBTools


text:
struct MaxUCB
    c::Float64
end

function select_best(crit::MaxUCB, h_node::POWTreeObsNode, rng)
    tree = h_node.tree
    h = h_node.node
    best_criterion_val = -Inf
    local best_node::Int
    istied = false
    local tied::Vector{Int}
    ltn = log(tree.total_n[h])
    for node in tree.tried[h]
        n = tree.n[node]
        if isinf(tree.v[node])
            criterion_value = tree.v[node]
        elseif n == 0
            criterion_value = Inf
        else
            criterion_value = tree.v[node] + crit.c*sqrt(ltn/n)
        end
        if criterion_value > best_criterion_val
            best_criterion_val = criterion_value
            best_node = node
            istied = false
        elseif criterion_value == best_criterion_val
            if istied
                push!(tied, node)
            else
                istied = true
                tied = [best_node, node]
            end
        end
    end
    if istied
        return rand(rng, tied)
    else
        return best_node
    end
end

struct MaxQ end

function select_best(crit::MaxQ, h_node::POWTreeObsNode, rng)
    tree = h_node.tree
    h = h_node.node
    best_node = first(tree.tried[h])
    best_v = tree.v[best_node]
    @assert !isnan(best_v)
    for node in tree.tried[h][2:end]
        if tree.v[node] >= best_v
            best_v = tree.v[node]
            best_node = node
        end
    end
    return best_node
end

struct MaxTries end

function select_best(crit::MaxTries, h_node::POWTreeObsNode, rng)
    tree = h_node.tree
    h = h_node.node
    best_node = first(tree.tried[h])
    best_n = tree.n[best_node]
    @assert !isnan(best_n)
    for node in tree.tried[h][2:end]
        if tree.n[node] >= best_n
            best_n = tree.n[node]
            best_node = node
        end
    end
    return best_node
end


text:
@doc raw"""
    logfmt"..."

Parse an [NGINX-style log format string](https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format)
and return a function mapping `(io::IO, http::HTTP.Stream) -> body` suitable for passing to
[`HTTP.listen`](@ref) using the `access_log` keyword argument.

The following variables are currently supported:

 - `$http_name`: arbitrary request header (with `-` replaced with `_`, e.g. `http_user_agent`)
 - `$sent_http_name`: arbitrary response header (with `-` replaced with `_`)
 - `$request`: the request line, e.g. `GET /index.html HTTP/1.1`
 - `$request_method`: the request method
 - `$request_uri`: the request URI
 - `$remote_addr`: client address
 - `$remote_port`: client port
 - `$remote_user`: user name supplied with the Basic authentication
 - `$server_protocol`: server protocol
 - `$time_iso8601`: local time in ISO8601 format
 - `$time_local`: local time in Common Log Format
 - `$status`: response status code
 - `$body_bytes_sent`: number of bytes in response body

## Examples
```julia
logfmt"[$time_iso8601] \\"$request\\" $status" # [2021-05-01T12:34:40+0100] "GET /index.html HTTP/1.1" 200

logfmt"$remote_addr \\"$http_user_agent\\"" # 127.0.0.1 "curl/7.47.0"
```
"""
macro logfmt_str(s)
    return logfmt_parser(s)
end

function logfmt_parser(s)
    s = String(s)
    vars = Symbol[]
    ex = Expr(:call, :print, :io)
    i = 1
    while i <= lastindex(s)
        j = findnext(==('\$'), s, i)
        if j === nothing
            j = lastindex(s)
            push!(ex.args, String(s[i:j]))
            break
        end
        if j > i
            push!(ex.args, String(s[i:prevind(s, j)]))
        end
        sym, j = Meta.parse(s, nextind(s, j); greedy=false)
        e = symbol_mapping(sym)
        isa(e, Tuple) ? push!(ex.args, e...) : push!(ex.args, e)
        i = j
    end
    f = Expr(:->, Expr(:tuple, :io, :http), ex)
    return f
end

function symbol_mapping(s::Symbol)
    str = string(s)
    if (m = match(r"^http_(.+)$", str); m !== nothing)
        hdr = replace(String(m[1]), '_' => '-')
        :(HTTP.header(http.message, $hdr, "-"))
    elseif (m = match(r"^sent_http_(.+)$", str); m !== nothing)
        hdr = replace(String(m[1]), '_' => '-')
        :(HTTP.header(http.message.response, $hdr, "-"))
    elseif s === :remote_addr
        :(http.stream.c.peerip)
    elseif s === :remote_port
        :(http.stream.c.peerport)
    elseif s === :remote_user
        :("-") # TODO: find from Basic auth...
    elseif s === :time_iso8601
        if !Sys.iswindows()
            :(Libc.strftime("%FT%T%z", time()))
        else
            # TODO: Libc.strftime doesn't seem to work properly on Windows
            # so format without timezone using Dates stdlib
            :(Dates.format(now(), dateformat"yyyy-mm-dd\THH:MM:SS"))
        end
    elseif s === :time_local
        if !Sys.iswindows()
            :(Libc.strftime("%d/%b/%Y:%H:%M:%S %z", time()))
        else
            # TODO: Libc.strftime doesn't seem to work properly on Windows
            # so format without timezone using Dates stdlib
            :(Dates.format(now(), dateformat"dd/u/yyyy:HH:MM:SS"))
        end
    elseif s === :request
        m = symbol_mapping(:request_method)
        t = symbol_mapping(:request_uri)
        p = symbol_mapping(:server_protocol)
        (m, " ", t, " ", p...)
    elseif s === :request_method
        :(http.message.method)
    elseif s === :request_uri
        :(http.message.target)
    elseif s === :server_protocol
        ("HTTP/", :(http.message.version.major), ".", :(http.message.version.minor))
    elseif s === :status
        :(http.message.response.status)
    elseif s === :body_bytes_sent
        return :(http.nwritten)
    else
        error("unknown variable in logfmt: $s")
    end
end

"""
    common_logfmt(io::IO, http::HTTP.Stream)

Format a log message in the Common Log Format and write to `io`.
"""
const common_logfmt = logfmt"$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent"

"""
    combined_logfmt(io::IO, http::HTTP.Stream)

Format a log message in the Combined Log Format and write to `io`.
"""
const combined_logfmt = logfmt"$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\""


text:
# ---
# title: 822. Card Flipping Game
# id: problem822
# author: Tian Jun
# date: 2020-10-31
# difficulty: Medium
# categories:
# link: <https://leetcode.com/problems/card-flipping-game/description/>
# hidden: true
# ---
#
# On a table are `N` cards, with a positive integer printed on the front and
# back of each card (possibly different).
#
# We flip any number of cards, and after we choose one card.
#
# If the number `X` on the back of the chosen card is not on the front of any
# card, then this number X is good.
#
# What is the smallest number that is good?  If no number is good, output `0`.
#
# Here, `fronts[i]` and `backs[i]` represent the number on the front and back of
# card `i`.
#
# A flip swaps the front and back numbers, so the value on the front is now on
# the back and vice versa.
#
# **Example:**
#
#
#
#     Input: fronts = [1,2,4,4,7], backs = [1,3,4,1,3]
#     Output: 2
#     Explanation: If we flip the second card, the fronts are [1,3,4,4,7] and the backs are [1,2,4,1,3].
#     We choose the second card, which has number 2 on the back, and it isn't on the front of any card, so 2 is good.
#
#
#
# **Note:**
#
#   1. `1 <= fronts.length == backs.length <= 1000`.
#   2. `1 <= fronts[i] <= 2000`.
#   3. `1 <= backs[i] <= 2000`.
#
#
## @lc code=start
using LeetCode

## add your code here:
## @lc code=end


text:
module TensorValuesTests

using TensorValues
using Test

@testset "TypesTests" begin
  include("TypesTests.jl")
end

@testset "OperationsTests" begin
  include("OperationsTests.jl")
end

@testset "IndexingTests" begin
  include("IndexingTests.jl")
end

@testset "ReinterpretTests" begin
  include("ReinterpretTests.jl")
end

end # module TensorValuesTests


text:
"""
    ChebyshevImplementation

implementation of computation of second Poincare invariant by approximating surface with
Chebyshev polynomials
"""
module ChebyshevImplementation

using ...PoincareInvariants: @argcheck
import ...PoincareInvariants: compute!, getpoints, getpointnum

using Base: Callable
using LinearAlgebra

include("PaduaTransforms.jl")
using .PaduaTransforms

const AbstractArray3 = AbstractArray{<:Any, 3}

## Differentiation ##

function getdiffmat(::Type{T}, degree::Integer) where T
    D = zeros(T, degree+1, degree+1)

    D[1, 2:2:end] .= 1:2:degree

    for i in 3:2:degree+1
        D[2:2:i-1, i] .= 2 * (i-1)
    end

    for i in 4:2:degree+1
        D[3:2:i-1, i] .= 2 * (i-1)
    end

    D
end

struct DiffPlan{T}
    D::Matrix{T}
end

DiffPlan{T}(degree::Integer) where T = DiffPlan{T}(getdiffmat(T, degree))

# differentiate in the Chebyshev basis
function differentiate!(∂x::AbstractMatrix, ∂y::AbstractMatrix, P::DiffPlan, coeffs::AbstractMatrix)
    ∂x[:, :] = coeffs  # differentiate each row
    rmul!(∂x, LowerTriangular(P.D'))

    ∂y[:, :] = coeffs  # differentiate each column
    lmul!(UpperTriangular(P.D), ∂y)

    ∂x, ∂y
end

function differentiate!(∂x::AbstractArray3, ∂y::AbstractArray3, P::DiffPlan, coeffs::AbstractArray3)
    axes(∂x, 3) == axes(∂y, 3) == axes(coeffs, 3) || error()

    @views for i in axes(coeffs, 3)
        differentiate!(∂x[:, :, i], ∂y[:, :, i], P, coeffs[:, :, i])
    end

    ∂x, ∂y
end

## Integration ##

getintegrator(::Type{T}, n) where T = T[isodd(i) ? 0 : T(2) / T(1 - i^2) for i in 0:n]
getintegrator(n) = getintegrator(Float64, n)

integrate(coeffs, integrator) = dot(integrator, coeffs, integrator)

## getintegrand for Ω Callable and out-of-place ##

struct OOPIntPlan{T, IP, P}
    invpaduaplan::IP
    ∂xvals::Matrix{T}
    ∂yvals::Matrix{T}
    intvals::Vector{T}
    paduaplan::P
end

function OOPIntPlan{T}(D, degree) where T
    invpaduaplan = InvPaduaTransformPlan{T}(degree)
    ∂xvals = Matrix{T}(undef, getpaduanum(degree), D)
    ∂yvals = Matrix{T}(undef, getpaduanum(degree), D)
    intvals = Vector{T}(undef, getpaduanum(degree))
    paduaplan = PaduaTransformPlan{T}(degree)

    OOPIntPlan{T, typeof(invpaduaplan), typeof(paduaplan)}(
        invpaduaplan, ∂xvals, ∂yvals, intvals, paduaplan
    )
end

function getintegrand!(
    intcoeffs::AbstractMatrix, plan::OOPIntPlan, Ω::Callable, phasepoints::AbstractMatrix, t, p,
    ∂xcoeffs::AbstractArray3, ∂ycoeffs::AbstractArray3
)
    ∂xvals = invpaduatransform!(plan.∂xvals, plan.invpaduaplan, ∂xcoeffs)
    ∂yvals = invpaduatransform!(plan.∂yvals, plan.invpaduaplan, ∂ycoeffs)

    @views for i in axes(plan.intvals, 1)
        plan.intvals[i] = dot(∂yvals[i, :], Ω(phasepoints[i, :], t, p), ∂xvals[i, :])
    end

    paduatransform!(intcoeffs, plan.paduaplan, plan.intvals)

    intcoeffs
end

## ChebyshevPlan and _compute! ##

getintplan(::Type{T}, ::Callable, D, degree, ::Val{false}) where T = OOPIntPlan{T}(D, degree)
# getintplan(::Type{T}, ::Callable, D, degree, ::Val{true}) where T = IPIntPlan{T}(D, degree)
# getintplan(::Type{T}, Ω::AbstractMatrix, D, degree, ::Val{nothing}) where T = IPIntPlan{T}(Ω, D, degree)

struct ChebyshevPlan{T, IP, PP<:PaduaTransformPlan}
    degree::Int
    paduaplan::PP
    phasecoeffs::Array{T, 3}
    diffplan::DiffPlan{T}
    ∂x::Array{T, 3}
    ∂y::Array{T, 3}
    intplan::IP  # getting coefficients of integrand to integrate
    intcoeffs::Matrix{T}
    integrator::Vector{T}
end

function ChebyshevPlan{T}(Ω::Callable, D::Integer, N::Integer, ::Val{inplace}) where {T, inplace}
    degree = getdegree(nextpaduanum(N))

    paduaplan = PaduaTransformPlan{T}(degree)
    phasecoeffs = zeros(T, degree+1, degree+1, D)
    diffplan = DiffPlan{T}(degree)
    ∂x = Array{T, 3}(undef, degree+1, degree+1, D)
    ∂y = Array{T, 3}(undef, degree+1, degree+1, D)
    intplan = getintplan(T, Ω, D, degree, Val(inplace))
    intcoeffs = zeros(T, degree+1, degree+1)
    integrator = getintegrator(T, degree)

    ChebyshevPlan{T, typeof(intplan), typeof(paduaplan)}(
        degree, paduaplan, phasecoeffs, diffplan, ∂x, ∂y, intplan, intcoeffs, integrator
    )
end

function compute!(plan::ChebyshevPlan, Ω::Callable, phasepoints::AbstractMatrix, t, p)
    paduatransform!(plan.phasecoeffs, plan.paduaplan, phasepoints)
    differentiate!(plan.∂x, plan.∂y, plan.diffplan, plan.phasecoeffs)
    getintegrand!(plan.intcoeffs, plan.intplan, Ω, phasepoints, t, p, plan.∂x, plan.∂y)
    integrate(plan.intcoeffs, plan.integrator)
end

# function _compute!(plan::ChebyshevPlan, Ω::AbstractMatrix, D, phasepoints)
#     paduatransform!(plan.phasecoeffs, plan.paduaplan, phasepoints)
#     differentiate!(plan.∂x, plan.∂y, plan.diffplan, plan.phasecoeffs)
#     getintegrand!(plan.intcoeffs, plan.intplan, Ω, plan.∂x, plan.∂y)
#     integrate(plan.intcoeffs, plan.intplan)
# end

## getpoints and getpointnum ##

getpointnum(plan::ChebyshevPlan) = getpaduanum(plan.degree)
getpoints(plan::ChebyshevPlan) = getpaduapoints(plan.degree) do x, y
    (x + 1) / 2, (y + 1) / 2
end

getpoints(f::Function, plan::ChebyshevPlan) = getpaduapoints(plan.degree) do x, y
    f((x + 1) / 2, (y + 1) / 2)
end

end  # module ChebyshevImplementation


text:
module DatasetsAPI

using CatBoost
using DataFrames

train, test = load_dataset(:msrank_10k)

end # module


text:
module ExtraFrech

# Write your package code here.
include("codes.jl")
export ERFRF
export Importance
export Importance_Unique
export ExtraFrechetRF
export pred_rf
export OOB_unique
export Combine
export Combine_OOB

end


text:
# Use baremodule to shave off a few KB from the serialized `.ji` file
baremodule pugixml_jll
using Base
using Base: UUID
import JLLWrappers

JLLWrappers.@generate_main_file_header("pugixml")
JLLWrappers.@generate_main_file("pugixml", UUID("ef860afa-341e-548b-93de-15e6b165854a"))
end  # module pugixml_jll


text:
isdefined(Base, :__precompile__) && __precompile__(true)

module AppConf

using Compat

export @dev,
       @prod,
       conf,
       parseconf

function __init__()
  if !haskey(ENV, "JULIA_ENV")
    ENV["JULIA_ENV"] = "dev"
  end
end

macro dev(e)
  if ENV["JULIA_ENV"] == "dev"
    return esc(e)
  end
end

macro prod(e)
  if ENV["JULIA_ENV"] == "prod"
    return esc(e)
  end
end

function findeq(ln::AbstractString)
    for i = 1:length(ln)
        c = chr2ind(ln, i);
        if ln[c] == '='
            return c
        end
    end
    return -1
end

function stripcomments(ln::AbstractString)
  ret = ""
  for c in ln
    if c == '#'
      break
    end
    ret = string(ret, c)
  end
  ret
end

function evalEnv(ln::AbstractString)
  nln = ln
  for v in matchall(r"\$[A-Z|_|-]+", ln)
    vname = replace(v, "\$", "")
    nln = replace(nln, v, ENV[vname])
  end
  nln
end

function cleanstring(str::AbstractString)
  s = strip(str)
  strip(s, '"')
end

function isnumeric(str::AbstractString)
  is = true
  dots = 0
  for c in cleanstring(str)
    if c == '.'
      dots += 1
    end
    if c != '.' && !isdigit(c)
      is = false
    end
  end
  if dots > 1
    is = false
  end
  is
end

islist(str::AbstractString) = str[1] == '[' && str[length(str)] == ']'
istuple(str::AbstractString) = str[1] == '(' && str[length(str)] == ')'

parselist(str::AbstractString) = map((x) -> isnumeric(x) ? parse(x) : cleanstring(x),
    split(match(r"\[(.*)\]", str).captures[1], ","))

function parsetuple(str::AbstractString)
    res = map((x) -> isnumeric(x) ? parse(x) : cleanstring(x),
        split(match(r"\((.*)\)", str).captures[1], ","))
    return ntuple((i) -> res[i], length(res))
end

function parseconf(file::AbstractString)
  file = abspath(file)
  tpath = abspath("$file.template")
  if !isfile(file)
    if isfile(tpath)
      warn("No file found at $file, using template at $tpath")
      file = tpath
    else
      error("No file found at $file")
    end
  end

  f = open(file)
  inquotes = false
    if !isdefined(:conf)
        conf = Dict{AbstractString, Any}()
    end
  while !eof(f)
    ln = evalEnv(stripcomments(readline(f)))
    ix = findeq(ln)
    if ix == -1
      continue
    end
    key = strip(ln[1:ix - 1])
    val = strip(chomp(ln[ix + 1:end]))
    if isnumeric(val) || val == "true" || val == "false"
      conf[key] = parse(val)
    # Handle single-line lists
    elseif islist(val)
      conf[key] = parselist(val)
    # Handle single-line lists
    elseif istuple(val)
      conf[key] = parsetuple(val)
    # Parse multi-line lists
    elseif val[1] == '['
      # Base case: first line
      curLn = val
      s = IOBuffer()
      while length(curLn) == 0 || curLn[end] != ']'
        println(s, curLn)
        eof(f) && (error("Unexpected end of input: ", curLn))
        curLn = strip(readline(f))
      end
      println(s, curLn)
      lst = takebuf_string(s)
      # parse and eval may not be performance optimal.
      conf[key] = eval(parse(lst))
    else
      conf[key] = cleanstring(val)
    end
    if key == "env"
      ENV["JULIA_ENV"] = strip(val, '"')
    end
  end
  global conf = conf
  conf
end

end # module


text:
#!/home/mkschleg/opt/bin/julia
#SBATCH -o test_err.out # Standard output
#SBATCH -e test_out.err # Standard error
#SBATCH --mem-per-cpu=1000M # Memory request of 1 GB
#SBATCH --time=00:05:00 # Running time of 10 minutes
#SBATCH --ntasks=256
#SBATCH --cpus-per-task=1
#SBATCH --account=def-whitem
#SBATCH --no-kill

using Pkg
Pkg.activate(".")

using Reproduce

const save_loc = "test_exp"
const exp_file = "examples/experiment.jl"
const exp_module_name = :Main
const exp_func_name = :main_experiment

function make_arguments(args::Dict)
    new_args = ["--opt1", args["opt1"], "--opt2", args["opt2"]]
    return new_args
end

function test_experiment()
    arg_dict = Dict(
        ["opt1"=>collect(1:50), "opt2"=>[5,6,7,8]]
    )
    arg_list = ["opt1", "opt2"]

    static_args = ["--steps", "102902"]

    args_iterator = ArgIterator(arg_dict, static_args; arg_list=arg_list, make_args=make_arguments)

    experiment = Experiment(save_loc,
                            exp_file,
                            exp_module_name,
                            exp_func_name,
                            args_iterator)

    create_experiment_dir(experiment; tldr="hello tldr")
    add_experiment(experiment; settings_dir="settings")
    ret = job(experiment; num_workers=6, extra_args=[save_loc])
    post_experiment(experiment, ret)

end

test_experiment()


text:
module StaticLint

include("exception_types.jl")

using SymbolServer, CSTParser
using CSTParser: EXPR, isidentifier, Import, Using, Export, Quote, Quotenode, x_Str, FunctionDef, setparent!, kindof, valof, typof, parentof, is_assignment, isoperator, ispunctuation, iskw, defines_function, is_splat
using SymbolServer: VarRef

const noname = EXPR(CSTParser.NoHead, nothing, 0, 0, nothing, CSTParser.NoKind, false, nothing, nothing)
baremodule CoreTypes # Convenience
using ..SymbolServer
using Base: ==
const DataType = SymbolServer.stdlibs[:Core][:DataType]
const Function = SymbolServer.stdlibs[:Core][:Function]
const Module = SymbolServer.stdlibs[:Core][:Module]
const String = SymbolServer.stdlibs[:Core][:String]
const Symbol = SymbolServer.stdlibs[:Core][:Symbol]
const Int = SymbolServer.stdlibs[:Core][:Int]
const Float64 = SymbolServer.stdlibs[:Core][:Float64]
const Vararg = SymbolServer.FakeTypeName(Core.Vararg)
isva(x) = (x isa SymbolServer.FakeTypeName && x.name.name == :Vararg && x.name.parent isa SymbolServer.VarRef && x.name.parent.name == :Core) || (x isa SymbolServer.FakeUnionAll && isva(x.body))
end

include("bindings.jl")
include("scope.jl")

mutable struct Meta
    binding::Union{Nothing,Binding}
    scope::Union{Nothing,Scope}
    ref::Union{Nothing,Binding,SymbolServer.SymStore}
    error
end
Meta() = Meta(nothing, nothing, nothing, nothing)

function Base.show(io::IO, m::Meta)
    m.binding !== nothing && show(io, m.binding)
    m.ref !== nothing && printstyled(io, " * ", color=:red)
    m.scope !== nothing && printstyled(io, " new scope", color=:green)
    m.error !== nothing && printstyled(io, " lint ", color=:red)
end
hasmeta(x::EXPR) = x.meta isa Meta
hasbinding(m::Meta) = m.binding isa Binding
hasref(m::Meta) = m.ref !== nothing
hasscope(m::Meta) = m.scope isa Scope
scopeof(m::Meta) = m.scope
bindingof(m::Meta) = m.binding

abstract type State end
mutable struct Toplevel{T} <: State
    file::T
    targetfile::Union{Nothing,T}
    included_files::Vector{String}
    scope::Scope
    delayed::Vector{EXPR}
    server
end

function (state::Toplevel)(x::EXPR)
    resolve_import(x, state)
    mark_bindings!(x, state)
    add_binding(x, state)
    mark_globals(x, state)
    handle_macro(x, state)
    s0 = scopes(x, state)
    resolve_ref(x, state)
    followinclude(x, state)

    if CSTParser.defines_function(x) || CSTParser.defines_macro(x) || typof(x) === CSTParser.Export
        push!(state.delayed, x)
    else
        traverse(x, state)
    end

    state.scope != s0 && (state.scope = s0)
    return state.scope
end

mutable struct Delayed <: State
    scope::Scope
    server
end

function (state::Delayed)(x::EXPR)
    mark_bindings!(x, state)
    add_binding(x, state)
    mark_globals(x, state)
    handle_macro(x, state)
    s0 = scopes(x, state)
    resolve_ref(x, state)

    traverse(x, state)

    state.scope != s0 && (state.scope = s0)
    return state.scope
end

"""
    traverse(x, state)

Iterates across the child nodes of an EXPR in execution order (rather than
storage order) calling `state` on each node.
"""
function traverse(x::EXPR, state)
    if is_binary_call(x) && (CSTParser.is_assignment(x) && !CSTParser.is_func_call(x[1]) || typof(x[2]) === CSTParser.Tokens.DECLARATION) && !(CSTParser.is_assignment(x) && is_curly(x[1]))
        state(x[3])
        state(x[2])
        state(x[1])
    elseif is_where(x)
        @inbounds for i = 3:length(x)
            state(x[i])
        end
        state(x[1])
        state(x[2])
    elseif typof(x) === CSTParser.Generator
        @inbounds for i = 2:length(x)
            state(x[i])
        end
        state(x[1])
    elseif typof(x) === CSTParser.Flatten  && length(x) === 1 && length(x[1]) >= 3 && length(x[1][1]) >= 3
        for i = 3:length(x[1][1])
            state(x[1][1][i])
        end
        for i = 3:length(x[1])
            state(x[1][i])
        end
        state(x[1][1][1])
    elseif length(x) > 0
        @inbounds for i in 1:length(x)
            state(x[i])
        end
    end
end

"""
    followinclude(x, state)

Checks whether the arguments of a call to `include` can be resolved to a path.
If successful it checks whether a file with that path is loaded on the server
or a file exists on the disc that can be loaded.
If this is successful it traverses the code associated with the loaded file.

"""
function followinclude(x, state::State)
    if is_call(x) && length(x) > 0 && isidentifier(x[1]) && valofid(x[1]) == "include"
        init_path = path = get_path(x, state)
        if isempty(path)
        elseif isabspath(path)
            if hasfile(state.server, path)
            elseif canloadfile(state.server, path)
                loadfile(state.server, path)
            else
                path = ""
            end
        elseif !isempty(getpath(state.file)) && isabspath(joinpath(dirname(getpath(state.file)), path))
            # Relative path from current
            if hasfile(state.server, joinpath(dirname(getpath(state.file)), path))
                path = joinpath(dirname(getpath(state.file)), path)
            elseif canloadfile(state.server, joinpath(dirname(getpath(state.file)), path))
                path = joinpath(dirname(getpath(state.file)), path)
                loadfile(state.server, path)
            else
                path = ""
            end
        elseif !isempty((basepath = _is_in_basedir(getpath(state.file)); basepath))
            # Special handling for include method used within Base
            path = joinpath(basepath, path)
            if hasfile(state.server, path)
                # skip
            elseif canloadfile(state.server, path)
                loadfile(state.server, path)
            else
                path = ""
            end
        else
            path = ""
        end
        if hasfile(state.server, path)
            if path in state.included_files
                seterror!(x, IncludeLoop)
                return
            end
            oldfile = state.file
            state.file = getfile(state.server, path)
            push!(state.included_files, getpath(state.file))
            setroot(state.file, getroot(oldfile))
            setscope!(getcst(state.file), nothing)
            state(getcst(state.file))
            state.file = oldfile
            pop!(state.included_files)
        elseif !is_in_fexpr(x, CSTParser.defines_function) && !isempty(init_path)
            seterror!(x, MissingFile)
        end
    end
end

include("server.jl")
include("imports.jl")
include("references.jl")
include("macros.jl")
include("linting/checks.jl")
include("type_inf.jl")
include("utils.jl")
end


text:
################################################################################
### Construct table and write to disk

outdir = joinpath(pwd(), "output")
if !isdir(outdir)
    mkdir(outdir)
end

data_infile    = joinpath(outdir, "my_test_table_in1.csv")
data_outfile   = joinpath(outdir, "my_test_table_out.csv")
issues_infile  = joinpath(outdir, "my_test_table_input_issues.tsv")
issues_outfile = joinpath(outdir, "my_test_table_output_issues.tsv")

tbl = DataFrame(
    patientid = [1, 2, 3, 4],
    age       = [11, 22, 33, 444],
    dose      = ["small", "medium", "large", "medium"],
    fever     = [false, true, true, false]
)

CSV.write(data_infile, tbl; delim=',')
tbl = nothing

################################################################################
### Compare DataFrame to Schema

# Schema
patientid = ColumnSchema(:patientid, "Patient ID",  UInt,   !CATEGORICAL, REQUIRED,  UNIQUE, UInt)
age       = ColumnSchema(:age,       "Age (years)", Int,    !CATEGORICAL, REQUIRED, !UNIQUE, Int)
dose      = ColumnSchema(:dose,      "Dose size",   String,  CATEGORICAL, REQUIRED, !UNIQUE, ["small", "medium", "large"])
fever     = ColumnSchema(:fever,     "Had fever",   Bool,    CATEGORICAL, REQUIRED, !UNIQUE, Bool)
ts        = TableSchema(:mytable, "My table", [patientid, age, dose, fever], [:patientid])

# Compare data to schema
compare(ts, data_infile; output_data_file=data_outfile, input_issues_file=issues_infile, output_issues_file=issues_outfile)
issues_in = DataFrame(CSV.File(issues_infile; delim='\t'))
@test size(issues_in, 1) == 0

# Modify schema: Forbid the age column from having values of 120 or above
age.validvalues = 0:120

# Compare again
compare(ts, data_infile; output_data_file=data_outfile, input_issues_file=issues_infile, output_issues_file=issues_outfile)
outdata    = DataFrame(CSV.File(data_outfile))
issues_in  = DataFrame(CSV.File(issues_infile; delim='\t'))
issues_out = DataFrame(CSV.File(issues_outfile; delim='\t'))
@test size(issues_in, 1)  == 1
@test size(issues_out, 1) == 1
@test ismissing(outdata[4, :age]) == true

# Fix input data
indata = DataFrame(CSV.File(data_infile))
indata[4, :age] = 44
data_infile     = joinpath(outdir, "my_test_table_in2.csv")  # Hack: Having some issues opening an existing file on Windows
CSV.write(data_infile, indata; delim=',')
compare(ts, data_infile; output_data_file=data_outfile, input_issues_file=issues_infile, output_issues_file=issues_outfile)
issues_in = DataFrame(CSV.File(issues_infile; delim='\t'))
@test size(issues_in, 1) == 0

################################################################################
# Test intra-row constraints
function test_row_constraints()
    filename = joinpath(dirname(pathof(Schemata)), "..", "test/schemata/row_constraints.toml")
    schema   = readschema(filename)
    indata   = DataFrame(
                  patientid = UInt.([1,2,3]),
                  dob=Date.(["1992-10-01", "1988-03-23", "1983-11-18"]),
                  date_of_marriage=[Date("2015-09-13"), missing, Date("1981-11-01")])
    data_infile = joinpath(outdir, "my_test_table_in3.csv")
    CSV.write(data_infile, indata; delim=',')
    compare(schema.tables[:dates], data_infile; output_data_file=data_outfile, input_issues_file=issues_infile, output_issues_file=issues_outfile)
end
test_row_constraints()
issues_in = DataFrame(CSV.File(issues_infile))
@test size(issues_in, 1) == 1

################################################################################
# Clean up

rm(outdir; recursive=true)


text:
module LibSSH2

using LibSSH2_jll
using Sockets
using FilePathsBase

function __init__()
    rc=LibSSH2.libssh2_init(0)
	atexit(()->libssh2_exit())
end

struct libssh2_socket_t
    descriptor::Cint
end
Base.cconvert(::Type{libssh2_socket_t}, sock::TCPSocket) = libssh2_socket_t(reinterpret(Cint, Base._fd(sock)))

const ssize_t = Csize_t

include("ctypes.jl")
include("libssh2/libssh2_api.jl")
include("libssh2/libssh2_h.jl")
include("libssh2/libssh2_publickey_h.jl")
include("libssh2/libssh2_sftp_h.jl")

version() = unsafe_string(libssh2_version(0))

libssh2_session_init() = libssh2_session_init_ex(C_NULL, C_NULL, C_NULL, C_NULL)
libssh2_userauth_password(sess, uname, passwd) = libssh2_userauth_password_ex(sess, uname, length(uname), passwd, length(passwd), C_NULL)
libssh2_userauth_publickey_fromfile(sess, uname, publickey, privatekey, passphrase=C_NULL) = libssh2_userauth_publickey_fromfile_ex(sess, uname, length(uname), publickey, privatekey, passphrase)
libssh2_userauth_list(sess, uname) = unsafe_string(libssh2_userauth_list(sess, uname, length(uname)))

libssh2_sftp_opendir(sftp, path) = libssh2_sftp_open_ex(sftp, path, length(path), 0, 0, LIBSSH2_SFTP_OPENDIR)
libssh2_sftp_open(sftp, filename, flags, mode) = libssh2_sftp_open_ex(sftp, filename, length(filename), flags, mode, LIBSSH2_SFTP_OPENFILE)

function libssh2_sftp_fstat(handle)
	attr=Ref(LibSSH2.LIBSSH2_SFTP_ATTRIBUTES(0,0,0,0,0,0,0))
	GC.@preserve attr begin
	    ret = LibSSH2.libssh2_sftp_fstat_ex(handle, attr, 0)
	end
	@assert ret == 0
	return attr[]
end

Base.filesize(attr::LIBSSH2_SFTP_ATTRIBUTES) = convert(Int, attr.filesize)

function libssh2_sftp_fstat!(handle, attr)
	_attr=Ref(attr)
	GC.@preserve _attr begin
	    ret = LibSSH2.libssh2_sftp_fstat_ex(handle, _attr, 1)
	end
	@assert ret == 0
	return handle
end

function libssh2_sftp_readdir(dir, max_size=2^8)
	namebuf = Vector{UInt8}(undef, max_size)
	infobuf = Vector{UInt8}(undef, max_size)
	attr=Ref(LibSSH2.LIBSSH2_SFTP_ATTRIBUTES(0,0,0,0,0,0,0))

	GC.@preserve namebuf infobuf begin
	    ret = LibSSH2.libssh2_sftp_readdir_ex(dir, pointer(namebuf), length(namebuf), pointer(infobuf), length(infobuf), attr)
	end

	@assert ret >= 0
	is_eof = ret == 0

	name = unsafe_string(pointer(namebuf))
	info = unsafe_string(pointer(infobuf))
	return is_eof, name, info, attr[]
end

libssh2_session_last_error() = libssh2_session_last_error(current_ssh_session())
function libssh2_session_last_error(session)
	string_pt = Ref(Cstring(Ptr{UInt8}()))
	len_pt = Ref(Cint(1))
	err_code = libssh2_session_last_error(session, string_pt, len_pt, 0)
	return unsafe_string(string_pt[]), err_code
end

include("session.jl")
include("agent.jl")
include("interface.jl")

end


text:
#@synapsetype Synapse <: AbstractSynapse
@createtype(Synapse,AbstractSynapse,synapseclient.Synapse,PyObject,false)

login(args...;kwargs...) = Synapse(synapseclient.login(args...;kwargs...))
check_for_updates(args...;kwargs...) = synapseclient.check_for_updates(args...;kwargs...)
release_notes(args...;kwargs...) = synapseclient.release_notes(args...;kwargs...)

# NB: Public API
@synapsefunction Synapse chunkedQuery
@synapsefunction Synapse delete
@synapsefunction Synapse deleteProvenance
@synapsefunction Synapse downloadTableColumns
@synapsefunction Synapse downloadTableFile
@synapsefunction Synapse get
@synapsefunction Synapse getAnnotations
@synapsefunction Synapse getChildren
@synapsefunction Synapse getColumn
@synapsefunction Synapse getColumns
@synapsefunction Synapse getConfigFile
@synapsefunction Synapse getEvaluation
@synapsefunction Synapse getEvaluationByContentSource
@synapsefunction Synapse getEvaluationByName
@synapsefunction Synapse getPermissions
@synapsefunction Synapse getProvenance
@synapsefunction Synapse getSubmission
@synapsefunction Synapse getSubmissionBundles
@synapsefunction Synapse getSubmissionStatus
@synapsefunction Synapse getSubmissions
@synapsefunction Synapse getTableColumns
@synapsefunction Synapse getTeam
@synapsefunction Synapse getTeamMembers
@synapsefunction Synapse getUserProfile
@synapsefunction Synapse getWiki
@synapsefunction Synapse getWikiHeaders
@synapsefunction Synapse invalidateAPIKey
@synapsefunction Synapse login
@synapsefunction Synapse logout
@synapsefunction Synapse md5Query
@synapsefunction Synapse onweb
@synapsefunction Synapse printEntity
@synapsefunction Synapse restDELETE
@synapsefunction Synapse restGET
@synapsefunction Synapse restPOST
@synapsefunction Synapse restPUT
@synapsefunction Synapse sendMessage
@synapsefunction Synapse setAnnotations
@synapsefunction Synapse setEndpoints
@synapsefunction Synapse setPermissions
@synapsefunction Synapse setProvenance
@synapsefunction Synapse store
@synapsefunction Synapse submit
@synapsefunction Synapse tableQuery
@synapsefunction Synapse updateActivity
@synapsefunction Synapse uploadFile

# deprecated in python version, were never exported by SynapseClient.jl
@synapsefunction Synapse query
@synapsefunction Synapse downloadEntity
@synapsefunction Synapse createEntity
@synapsefunction Synapse getEntity
@synapsefunction Synapse updateEntity

# not exported
@synapsefunction Synapse _list
@synapsefunction Synapse _findteam


text:
#=
composite_test1:
- Julia version:
- Author: Dan
- Date: 2021-06-07
=#


text:
using InformationMeasures
using Test

arr1 = rand(1000)
arr2 = rand(1000)
arr3 = rand(1000)

# Variables are shared between these tests - don't re-order
include("testDiscretization.jl")
include("testEntropy.jl")
include("testConditionalEntropy.jl")
include("testMutualInformation.jl")
include("testConditionalMutualInformation.jl")
include("testInteractionInformation.jl")
include("testTotalCorrelation.jl")
include("testPartialInformationDecomposition.jl")
include("testCrossEntropy.jl")


text:
using Weiqi

import Weiqi: empty, black, white, cb

@testset "set and access coords and stone to board" begin
# Set coordinate (1,2) as black, white, empty on board
cb.array[1,2] = black;
cb.array[1,2] = white;
cb.array[1,2] = empty;
cb

# Access coordinate (1,2) on board, with associated color
cb.array[1,2]
cb.array[1,3]

# Empty all board
cb.array[1:19,1:19] = empty;
end


text:
function faiToGtf(pathFai, pathGtf; addLen = 0)
    mkpath(dirname(pathGtf))

    fw = open(pathGtf, "w")
    open(pathFai) do f
        for ln in eachline(f)
            arr = split(chomp(ln), "\t")
            oldStart = 0
            oldEnd = parse(Int64, arr[2])
            newStart = string(oldStart + 1 - addLen)
            newEnd =   string(oldEnd + addLen)
            write(fw, @sprintf "%s\tbedToGtf\texon\t%s\t%s\t%s\t%s\t.\tgene_id \"%s\"; transcript_id \"%s\"\n" arr[1] newStart newEnd "0" "." arr[1] arr[1])

        end
    end
    close(fw)

    # 1. seqname - The name of the sequence. Must be a chromosome or scaffold.
    # 2. source - The program that generated this feature.
    # 3. feature - The name of this type of feature. Some examples of standard feature types are "CDS", "start_codon", "stop_codon", and "exon".
    # 4. start - The starting position of the feature in the sequence. The first base is numbered 1.
    # 5. end - The ending position of the feature (inclusive).
    # 6. score - A score between 0 and 1000. If the track line useScore attribute is set to 1 for this annotation data set, the score value will determine the level of gray in which this feature is displayed (higher numbers = darker gray). If there is no score value, enter ".".
    # 7 s trand - Valid entries include '+', '-', or '.' (for don't know/don't care).
    # 8 frame - If the feature is a coding exon, frame should be a number between 0-2 that represents the reading frame of the first base. If the feature is not a coding exon, the value should be '.'.
    # group - All lines with the same group are linked together into a single item.
end

pathFai = ARGS[1]
pathGtf = ARGS[2]

faiToGtf(pathFai, pathGtf)

max_stars_repo_name: rikenbit/RamDAQ-prototype

max_issues_repo_name: rikenbit/RamDAQ-prototype

max_forks_repo_name: rikenbit/RamDAQ-prototype


text:
# Parts of this code were taken / derived from Graphs.jl. See LICENSE for
# licensing details.
"""
    connected_components!(label, g)

Fill `label` with the `id` of the connected component in the undirected graph
`g` to which it belongs. Return a vector representing the component assigned
to each vertex. The component value is the smallest vertex ID in the component.
"""
function connected_components! end
@traitfn function connected_components!(label::AbstractVector, g::::(!IsDirected))
    # this version of connected components uses Breadth First Traversal
    # with custom visitor type in order to improve performance.
    # one BFS is performed for each component.
    # This algorithm is linear in the number of edges of the graph
    # each edge is touched once. memory performance is a single allocation.
    # the return type is a vector of labels which can be used directly or
    # passed to components(a)
    T = eltype(g)
    nvg = nv(g)

    visitor = LightGraphs.ComponentVisitorVector(label, zero(T))
    colormap = fill(0, nvg)
    queue = Vector{T}()
    sizehint!(queue, nvg)
    for v in vertices(g)
        if label[v] == zero(T)
            visitor.labels[v] = v
            visitor.seed = v
            traverse_graph!(g, BreadthFirst(), v, visitor; vertexcolormap=colormap, queue=queue)
        end
    end
    return label
end

"""
    components_dict(labels)

Convert an array of labels to a map of component id to vertices, and return
a map with each key corresponding to a given component id
and each value containing the vertices associated with that component.
"""
function components_dict(labels::Vector{T}) where T<:Integer
    d = Dict{T,Vector{T}}()
    for (v,l) in enumerate(labels)
        vec = get(d, l, Vector{T}())
        push!(vec, v)
        d[l] = vec
    end
    return d
end

"""
    components(labels)

Given a vector of component labels, return a vector of vectors representing the vertices associated
with a given component id.
"""
function components(labels::Vector{T}) where T<:Integer
    d = Dict{T, T}()
    c = Vector{Vector{T}}()
    i = one(T)
    for (v,l) in enumerate(labels)
        index = get!(d, l, i)
        if length(c) >= index
            push!(c[index], v)
        else
            push!(c, [v])
            i += 1
        end
    end
    return c, d
end

"""
    connected_components(g)

Return the [connected components](https://en.wikipedia.org/wiki/Connectivity_(graph_theory))
of an undirected graph `g` as a vector of components, with each element a vector of vertices
belonging to the component.

For directed graphs, see [`strongly_connected_components`](@ref) and
[`weakly_connected_components`](@ref).
"""
function connected_components end
@traitfn function connected_components(g::::(!IsDirected))
    T = eltype(g)
    label = zeros(T, nv(g))
    connected_components!(label, g)
    c, d = components(label)
    return c
end

"""
    is_connected(g)

Return `true` if graph `g` is connected. For directed graphs, use
[`is_weakly_connected`](@ref) or [`is_strongly_connected`](@ref).
"""
function is_connected end
@traitfn is_connected(g::::(!IsDirected)) = ne(g)+1 >= nv(g) && length(connected_components(g)) == 1

"""
    weakly_connected_components(g)

Return the weakly connected components of the directed graph `g`. This
is equivalent to the connected components of the undirected equivalent of `g`.
"""
function weakly_connected_components end
@traitfn weakly_connected_components(g::::IsDirected) = connected_components(Graph(g))

"""
    is_weakly_connected(g)

Return `true` if the directed graph `g` is connected.
"""
function is_weakly_connected end
@traitfn is_weakly_connected(g::::IsDirected) = length(weakly_connected_components(g)) == 1

# Adapated from Graphs.jl
mutable struct TarjanVisitor{T<:Integer} <: AbstractGraphVisitor
    stack::Vector{T}
    onstack::BitVector
    lowlink::Vector{T}
    index::Vector{T}
    components::Vector{Vector{T}}
end

TarjanVisitor(n::T) where T<:Integer = TarjanVisitor(
    Vector{T}(),
    falses(n),
    Vector{T}(),
    zeros(T, n),
    Vector{Vector{T}}()
)

function discover_vertex!(vis::TarjanVisitor, v)
    vis.index[v] = length(vis.stack) + 1
    push!(vis.lowlink, length(vis.stack) + 1)
    push!(vis.stack, v)
    vis.onstack[v] = true
    return true
end

function examine_neighbor!(vis::TarjanVisitor, v, w, v_color::Int, w_color::Int, e_color::Int)
    if w_color != 0 && vis.onstack[w] # != 0 means seen
        while vis.index[w] > 0 && vis.index[w] < vis.lowlink[end]
            pop!(vis.lowlink)
        end
    end
    return true
end

function close_vertex!(vis::TarjanVisitor, v)
    if vis.index[v] == vis.lowlink[end]
        component = splice!(vis.stack, vis.index[v]:length(vis.stack))
        vis.onstack[component] = false
        pop!(vis.lowlink)
        push!(vis.components, component)
    end
    return true
end

"""
    strongly_connected_components(g)

Compute the strongly connected components of a directed graph `g`.
"""
function strongly_connected_components end
@traitfn function strongly_connected_components(g::::IsDirected)
    T = eltype(g)
    nvg = nv(g)
    cmap = zeros(Int, nvg)
    components = Vector{Vector{T}}()

    for v in vertices(g)
        if cmap[v] == 0 # 0 means not visited yet
            visitor = TarjanVisitor(nvg)
            traverse_graph!(g, DepthFirst(), v, visitor, vertexcolormap=cmap)
            for component in visitor.components
                push!(components, component)
            end
        end
    end
    return components
end

"""
    is_strongly_connected(g)

Return `true` if directed graph `g` is strongly connected.
"""
function is_strongly_connected end
@traitfn is_strongly_connected(g::::IsDirected) = length(strongly_connected_components(g)) == 1

"""
    period(g)

Return the (common) period for all vertices in a strongly connected directed graph.
Will throw an error if the graph is not strongly connected.
"""
function period end
@traitfn function period(g::::IsDirected)
    T = eltype(g)
    !is_strongly_connected(g) && error("Graph must be strongly connected")

    # First check if there's a self loop
    has_self_loops(g) && return 1

    g_bfs_tree  = bfs_tree(g,1)
    levels      = gdistances(g_bfs_tree,1)
    tree_diff   = difference(g,g_bfs_tree)
    edge_values = Vector{T}()

    divisor = 0
    for e in edges(tree_diff)
        @inbounds value = levels[src(e)] - levels[dst(e)] + 1
        divisor = gcd(divisor,value)
        isequal(divisor,1) && return 1
    end

    return divisor
end

"""
    condensation(g[, scc])

Return the condensation graph of the strongly connected components `scc`
in the directed graph `g`. If `scc` is missing, generate the strongly
connected components first.
"""
function condensation end
@traitfn function condensation{T<:Integer}(g::::IsDirected, scc::Vector{Vector{T}})
    h = DiGraph{T}(length(scc))

    component = Vector{T}(nv(g))

    for (i,s) in enumerate(scc)
        @inbounds component[s] = i
    end

    @inbounds for e in edges(g)
        s, d = component[src(e)], component[dst(e)]
        if (s != d)
            add_edge!(h,s,d)
        end
    end
    return h
end
@traitfn condensation(g::::IsDirected) = condensation(g,strongly_connected_components(g))

"""
    attracting_components(g)

Return a vector of vectors of integers representing lists of attracting
components in the directed graph `g`.

The attracting components are a subset of the strongly
connected components in which the components do not have any leaving edges.
"""
function attracting_components end
@traitfn function attracting_components(g::::IsDirected)
    T = eltype(g)
    scc  = strongly_connected_components(g)
    cond = condensation(g,scc)

    attracting = Vector{T}()

    for v in vertices(cond)
        if outdegree(cond,v) == 0
            push!(attracting,v)
        end
    end
    return scc[attracting]
end

mutable struct NeighborhoodVisitor{T<:Integer} <: AbstractGraphVisitor
    d::T
    neigs::Vector{T}
end

NeighborhoodVisitor(d::T) where T<:Integer = NeighborhoodVisitor(d, Vector{T}())

function examine_neighbor!(visitor::NeighborhoodVisitor, u::Integer, v::Integer, ucolor::Int, vcolor::Int, ecolor::Int)
    -ucolor > visitor.d && return false # color is negative for not-closed vertices
    if vcolor == 0
        push!(visitor.neigs, v)
    end
    return true
end

"""
    neighborhood(g, v, d)

Return a vector of the vertices in `g` at a geodesic distance less or equal to `d`
from `v`.

### Optional Arguments
- `dir=:out`: If `g` is directed, this argument specifies the edge direction
with respect to `v` of the edges to be considered. Possible values: `:in` or `:out`.
"""
function neighborhood(g::AbstractGraph, v::Integer, d::Integer; dir=:out)
    @assert d >= 0 "Distance has to be greater then zero."
    T = eltype(g)
    visitor = NeighborhoodVisitor(d)
    push!(visitor.neigs, T(v))
    traverse_graph!(g, BreadthFirst(), v, visitor,
        vertexcolormap=Dict{T,Int}(), dir=dir)
    return visitor.neigs
end

"""
    isgraphical(degs)

Return true if the degree sequence `degs` is graphical, according to
[Erdös-Gallai condition](http://mathworld.wolfram.com/GraphicSequence.html).

### Performance
    Time complexity: ``\\mathcal{O}(|degs|^2)``
"""
function isgraphical(degs::Vector{Int})
    iseven(sum(degs)) || return false
    n = length(degs)
    for r=1:n-1
        cond = sum(i->degs[i], 1:r) <= r*(r-1) + sum(i->min(r,degs[i]), r+1:n)
        cond || return false
    end
    return true
end

max_stars_repo_name: AsileBcd/JohnsonCycles

max_issues_repo_name: AsileBcd/JohnsonCycles

max_forks_repo_name: AsileBcd/JohnsonCycles