text:
import Base

"""
    Base.:/(df1::DataFrame, df2::DataFrame)
Extends / to operate on 2 DataFrames, each with one column of AbstractFloat type.
The operation will join the DataFrames on their descriptive columns to ensure the operation
is performed for related values. Values that are "missing" after joining are set to 0.
"""
function Base.:/(df1::DataFrame, df2::DataFrame)
    df = indexjoin(copy.([df1,df2]))
    out = findvalue(df)

    df[!,:value] .= df[:, out[1]] ./ df[:, out[2]]

    return df[:, unique([setdiff(propertynames(df),out);:value])]
end

"""
    Base.:+(df::Vararg{DataFrame})
Extends + to operate on 2+ DataFrames, each with one column of AbstractFloat type.
The operation will join the DataFrames on their descriptive columns to ensure the operation
is performed for related values. Values that are "missing" after joining are set to 0.
"""
function Base.:+(df::Vararg{DataFrame})
    N = length(df)

    if length(findvalue.(df)) > N
        @error("Can only add DataFrames with one value column EACH.")
    end

    df = indexjoin(ensurearray(df))
    out = findvalue(df)

    df[!,:value] .= df[:, out[1]];
    [df[!,:value] += df[:, out[ii]] for ii in 2:N]

    return df[:, unique([setdiff(propertynames(df),out);:value])]
end

"""
    Base.:-(df::Vararg{DataFrame})
Extends - to operate on 2+ DataFrames, each with one column of AbstractFloat type.
The operation will join the DataFrames on their descriptive columns to ensure the operation
is performed for related values. Values that are "missing" after joining are set to 0.
"""
function Base.:-(df::Vararg{DataFrame})
    N = length(df)

    if length(findvalue.(df)) > N
        @error("Can only subtract DataFrames with one value column EACH.")
    end

    df = indexjoin(ensurearray(df))
    out = findvalue(df)

    df[!,:value] .= df[:, out[1]];
    [df[!,:value] -= df[:, out[ii]] for ii in 2:N]

    return df[:, unique([setdiff(propertynames(df),out);:value])]
end

"""
    Base.:*(df::Vararg{DataFrame})
Extends * to operate on 2+ DataFrames, each with one column of AbstractFloat type.
The operation will join the DataFrames on their descriptive columns to ensure the operation
is performed for related values. Values that are "missing" after joining are set to 0.
"""
function Base.:*(df::Vararg{DataFrame})
    N = length(df)

    if length(findvalue.(df)) > N
        @error("Can only multiply DataFrames with one value column EACH.")
    end

    df = indexjoin(ensurearray(df))
    out = findvalue(df)

    df[!,:value] .= df[:, out[1]];
    [df[!,:value] .*= df[:, out[ii]] for ii in 2:N]

    return df[:, unique([setdiff(propertynames(df),out);:value])]
end

"""
    combine_over(df::DataFrame, col::Array{Symbol,1}; operation::Function = sum)
    combine_over(df::DataFrame, col::Symbol; operation::Function = sum)
This function applies [`combine`](https://juliadata.github.io/DataFrames.jl/stable/lib/functions/#DataFrames.combine)
to the input DataFrame `df` over the input column(s) `col`.

# Arguments
- `df::DataFrame`: DataFrame on which to operate.
- `col::Symbol` or `col::Array{Symbol,1}`: column(s) over which to operate.

# Keywords
- `operation::Function = sum`: Operation to perform over the DataFrame columns. By default,
    the function will return a summation. Other standard summary functions include: `sum`,
    `prod`, `minimum`, `maximum`, `mean`, `var`, `std`, `first`, `last` and `length`.

# Returns
- `df::DataFrame` WITHOUT the specified column(s) argument. The resulting DataFrame will be
    'shorter' than the input DataFrame.
"""
function combine_over(df::DataFrame, col::Array{Symbol,1}; fun::Function = sum)
    # !!!! add kwarg to findvalue to indicate whether to include integers as values
    val = findvalue(df)
    idx_by = setdiff(propertynames(df), [col; val])

    df = combine(groupby(df, idx_by), val .=> fun .=> val)

    [df[!,ii] .= round.(df[:,ii]; digits = DEFAULT_ROUND_DIGITS)
        for ii in find_oftype(df[:,val], AbstractFloat)]

    # !!!! See where we actually want to convert boolean sums to integers. I think it's just
    # in some labor sharing. We can probably keep summed booleans as integers. This seems less confusing.
    [df[!,ii] .= convert_type.(Float64, df[:,ii]) for ii in find_oftype(df[:,val], Int)]
    return df
end

function combine_over(df::DataFrame, col::Symbol; fun::Function = sum)
    return combine_over(df, ensurearray(col); fun = fun)
end

"""
    transform_over(df::DataFrame, col::Array{Symbol,1}; operation::Function = sum)
    transform_over(df::DataFrame, col::Symbol; operation::Function = sum)
This function applies [`transform`](https://juliadata.github.io/DataFrames.jl/stable/lib/functions/#DataFrames.transform)
to the input DataFrame `df` over the input column(s) `col`.

# Arguments
- `df::DataFrame`: DataFrame on which to operate.
- `col::Symbol` or `col::Array{Symbol,1}`: column(s) over which to operate.

# Keywords
- `operation::Function = sum`: Operation to perform over the DataFrame columns. By default,
    the function will return a summation. Other standard summary functions include: `sum`,
    `prod`, `minimum`, `maximum`, `mean`, `var`, `std`, `first`, `last` and `length`.

# Returns
- `df::DataFrame` WITH the specified column(s) argument. The resulting DataFrame will be
    the same length as the input DataFrame.
"""
function transform_over(df::DataFrame, col::Array{Symbol,1}; fun::Function = sum)
    val = findvalue(df)
    idx_by = setdiff(propertynames(df), [col; val])

    df = transform(groupby(df, idx_by), val .=> fun .=> val)

    [df[!,ii] .= round.(df[:,ii]; digits = DEFAULT_ROUND_DIGITS)
        for ii in find_oftype(df[:,val], AbstractFloat)]
    [df[!,ii] .= convert_type.(Float64, df[:,ii]) for ii in find_oftype(df[:,val], Int)]
    return df
end

function transform_over(df::DataFrame, col::Symbol; fun::Function = sum)
    return transform_over(df, ensurearray(col); fun = fun)
end

"""
Round either the specified columns or all columns of type `AbstractFloat` to the specified
number of digits.

# Arguments
- `df::DataFrame` of values in need of rounding
- `col::Symbol` or `col::Array{Symbol,1}` to round. If no columns are specified,
    all columns of type `AbstractFloat` will be rounded.

# Keywords
- `digits::Int = 10`: Number of decimal places to keep when rounding
"""
function round!(df::DataFrame; digits::Int = DEFAULT_ROUND_DIGITS)
    return round!(df, find_oftype(df, AbstractFloat); digits = digits)
end

function round!(df::DataFrame, col::Union{Symbol,Array{Symbol,1}}; digits::Int = DEFAULT_ROUND_DIGITS)
    df[!,col] .= round.(df[:,col]; digits = digits)
    return df
end

max_stars_repo_name: NREL/SLiDE

max_issues_repo_name: NREL/SLiDE

max_forks_repo_name: NREL/SLiDE


text:
function solve()
    r1, c1 = [parse(Int, x) for x in split(readline())]
    r2, c2 = [parse(Int, x) for x in split(readline())]

    x = abs(r1-r2)
    y = abs(c1-c2)

    if (x,y)==(0,0)
        0
    elseif x==y || x+y≤3
        1
    elseif (x+y)%2==0 || x+y≤6 || abs(x-y)≤3
        2
    else
        3
    end
end

println(solve())

ext: jl

max_stars_repo_name: aishikawa/atcoder-julia

max_issues_repo_name: aishikawa/atcoder-julia

max_forks_repo_name: aishikawa/atcoder-julia


text:
function domify(ctx, navnode, bib::BibliographyNode)
    @tags p li ul

    entries = read_bib_file(bib.bibfile)

    lis = []
    for el in bib.elements
        if !haskey(entries, el)
        	Utilities.warn("could not find bib entry for $el.")
        	continue
        end

        citation = format_bib_entry(entries[el])
        push!(lis, li(p[:id=>el](citation)))
    end

    ul(lis)
end


text:
using OrderedCollections

# OrderedDict is used in place of a Python Neo4j's Record Object

@testset "RecordTestCase" begin

    @testset "test_record_equality" begin
        record1 = OrderedDict(zip(["name", "empire"], ["Nigel", "The British Empire"]))
        record2 = OrderedDict(zip(["name", "empire"], ["Nigel", "The British Empire"]))
        record3 = OrderedDict(zip(["name", "empire"], ["Stefan", "Das Deutschland"]))
        @test record1 == record2
        @test record1 != record3
        @test record2 != record3
    end

    @testset "test_record_hashing" begin
        record1 = OrderedDict(zip(["name", "empire"], ["Nigel", "The British Empire"]))
        record2 = OrderedDict(zip(["name", "empire"], ["Nigel", "The British Empire"]))
        record3 = OrderedDict(zip(["name", "empire"], ["Stefan", "Das Deutschland"]))
        @test hash(record1) == hash(record2)
        @test hash(record1) != hash(record3)
        @test hash(record2) != hash(record3)
    end

    @testset "test_record_iter" begin
        record1 = OrderedDict(zip(["name", "empire"], ["Nigel", "The British Empire"]))
        @test collect(values(record1)) == ["Nigel", "The British Empire"]
    end

    @testset "test_record_as_dict" begin
        record1 = OrderedDict(zip(["name", "empire"], ["Nigel", "The British Empire"]))
        @test record1 == Dict("name"=>"Nigel", "empire"=>"The British Empire")
    end

    # test_record_as_list is equivelent to test_record_iter

    @testset "test_record_len" begin
        a_record = OrderedDict(zip(["name", "empire"], ["Nigel", "The British Empire"]))
        @test length(a_record) == 2
    end

    # test_record_repr

    @testset "test_record_data" begin
        r = OrderedDict(zip(["name", "age", "married"], ["Alice", 33, true]))
        @test record_data(r) == Dict("name"=>"Alice", "age"=>33, "married"=>true)
        @test record_data(r, ["name"]) == Dict("name"=>"Alice")
        @test record_data(r, ["age", "name"]) == Dict("age"=>33, "name"=>"Alice")
        @test record_data(r, ["age", "name", "shoe size"]) == Dict("age"=>33, "name"=>"Alice", "shoe size"=>nothing)
        @test record_data(r, [1,"name"]) == Dict("name"=>"Alice")
        @test record_data(r, [1]) == Dict("name"=>"Alice")
        @test record_data(r, [1,2]) == Dict("age"=>33, "name"=>"Alice")
        @test_throws ErrorException record_data(r, [2,1,1000])
    end

    @testset "test_record_keys" begin
        r = OrderedDict(zip(["name", "age", "married"], ["Alice", 33, true]))
        @test collect(keys(r)) == ["name", "age", "married"]
    end

    @testset "test_record_values" begin
        r = OrderedDict(zip(["name", "age", "married"], ["Alice", 33, true]))
        @test record_values(r) == ["Alice", 33, true]
        @test record_values(r, ["name"]) == ["Alice"]
        @test record_values(r, ["age", "name"]) == [33, "Alice"]
        @test record_values(r, ["age", "name", "shoe size"]) == [33, "Alice", nothing]
        @test record_values(r, [1, "name"]) == ["Alice", "Alice"]
        @test record_values(r, [1]) == ["Alice"]
        @test record_values(r, [2, 1]) == [33, "Alice"]
        @test_throws ErrorException record_values(r, [2, 1, 1000])
    end

    @testset "test_record_items" begin
        r = OrderedDict(zip(["name", "age", "married"], ["Alice", 33, true]))
        @test record_items(r) == [("name","Alice"), ("age", 33), ("married", true)]
        @test record_items(r, ["name"]) == [("name","Alice")]
        @test record_items(r, ["age", "name"]) == [("age", 33), ("name","Alice")]
        @test record_items(r, ["age", "name", "shoe size"]) == [("age", 33), ("name","Alice"), ("shoe size", nothing)]
        @test record_items(r, [1, "name"]) == [("name", "Alice"), ("name", "Alice")]
        @test record_items(r, [1]) == [("name","Alice")]
        @test record_items(r, [2, 1]) == [("age", 33), ("name","Alice")]
        @test_throws ErrorException record_items(r, [2, 1, 1000])
    end

    @testset "test_record_index" begin
        r = OrderedDict(zip(["name", "age", "married"], ["Alice", 33, true]))
        @test index(r, "name") == 1
        @test index(r, "age") == 2
        @test index(r, "married") == 3
        @test_throws ErrorException index(r, "shoe size")
        @test index(r, 1) == 1
        @test index(r, 2) == 2
        @test index(r, 3) == 3
        @test_throws ErrorException index(r, 4)
        @test_throws MethodError index(r, nothing)
    end

    @testset "test_record_value" begin
        r = OrderedDict(zip(["name", "age", "married"], ["Alice", 33, true]))
        @test value(r) == "Alice"
        @test value(r, "name") == "Alice"
        @test value(r, "age") == 33
        @test value(r, "shoe size") == nothing
        @test value(r, "shoe size", 6) == 6
        @test value(r, 1) == "Alice"
        @test value(r, 2) == 33
        @test value(r, 3) == true
        @test value(r, 4) == nothing
        @test value(r, 4, 6) == 6
        @test_throws MethodError index(r, nothing)
    end

    @testset "test_record_value" begin
        r = OrderedDict(zip(["name", "age", "married"], ["Alice", 33, true]))
        @test in("Alice", values(r))
        @test in(33, values(r))
        @test in(true, values(r))
        @test !in(7.5, values(r))
    end

end


text:
"This function multiplies x by 2 and adds y."
f(x, y) = 2x + y


text:
@testset "SimplicialComplexes" begin
    @testset "maximalsimplices" begin
        sxcx = SimplicialComplex()
        push!(sxcx, (1, 2, 3, 4))
        push!(sxcx, (3, 4, 5))
        maxsxs = collect(values(SimplicialComplexes.maximalsimplices(sxcx)))
        @test [3, 4, 5] in maxsxs
        @test [1, 2, 3, 4] in maxsxs
        @test !([1, 2, 3] in maxsxs)
    end

    @testset "maximalcofaces, maximalfaces" begin
        maximalcofaces = SimplicialComplexes.maximalcofaces
        maximalfaces = SimplicialComplexes.maximalfaces

        sxcx = SimplicialComplex()
        push!(sxcx, (1, 2, 3, 4)) # lab = 1
        push!(sxcx, (2, 3, 4, 5)) # lab = 2
        push!(sxcx, (3, 4, 6))    # lab = 3

        @test maximalcofaces(sxcx, (1, 2)) == Set(1)
        @test maximalcofaces(sxcx, (4, 5)) == Set(2)
        @test maximalcofaces(sxcx, (3, 4)) == Set([1, 2, 3])

        @test maximalfaces(sxcx, (3, 4, 5, 6)) == Set(3)
        @test maximalfaces(sxcx, 1:10) == Set([1, 2, 3])
        @test maximalfaces(sxcx, 1:5) == Set([1, 2])

        sxcx = SimplicialComplex()
        push!(sxcx, (1, 3, 4, 5))
        push!(sxcx, (2, 3, 4, 5))
        push!(sxcx, (1, 3, 6))

        @test maximalcofaces(sxcx, (3, 4, 5)) == Set([1, 2])
        @test maximalcofaces(sxcx, (1, 3)) == Set([1, 3])
        @test maximalcofaces(sxcx, (1, 2, 3)) == Set()

        @test maximalfaces(sxcx, (1, 3, 4, 5, 6)) == Set([1, 3])
        @test maximalfaces(sxcx, (1, 2, 3, 4, 5)) == Set([1, 2])
        @test maximalfaces(sxcx, (1, 3, 5, 6)) == Set([3])
        @test maximalfaces(sxcx, (1, 2, 3)) == Set()
    end

    @testset "Insertion" begin
        sxcx = SimplicialComplex()
        push!(sxcx, (1, 3, 4, 5))
        push!(sxcx, (2, 3, 4, 5))
        push!(sxcx, (1, 3, 6))

        @test length(alllabels(sxcx)) == 3

        push!(sxcx, (1, 3, 4, 5))
        @test length(alllabels(sxcx)) == 3

        push!(sxcx, (1, 3, 4, 5, 7))
        @test length(alllabels(sxcx)) == 3

        push!(sxcx, (1, 3, 4, 5, 6, 7))
        @test length(alllabels(sxcx)) == 2

        push!(sxcx, (1, 2, 3, 4, 5, 6, 7))
        @test length(alllabels(sxcx)) == 1
    end

    @testset "in" begin
        sxcx = SimplicialComplex()
        push!(sxcx, (1, 3, 4, 5))
        push!(sxcx, (2, 3, 4, 5))
        push!(sxcx, (1, 3, 6))

        @test (1, 3, 4, 5) in sxcx
        @test (2, 3, 4, 5) in sxcx
        @test (1, 3, 6) in sxcx

        @test (1, 3) in sxcx
        @test (1, 6) in sxcx
        @test (2, 3, 5) in sxcx

        @test !((2, 3, 6) in sxcx)
        @test !((1, 7) in sxcx)

        sxcx = SimplicialComplex()
        push!(sxcx, (1, 2))
        push!(sxcx, (2, 3))
        push!(sxcx, (1, 3))
        @test !((1, 2, 3) in sxcx)

        @test_throws ArgumentError (2, 1) in sxcx
        @test_throws ArgumentError (7, 2) in sxcx
    end

    @testset "Removal" begin
        @testset "deletemaximals!" begin
            sxcx = SimplicialComplex()
            deletemaximals! = SimplicialComplexes.deletemaximals!
            push!(sxcx, (1, 2, 3))
            push!(sxcx, (2, 3, 4))
            push!(sxcx, (1, 2, 5))
            deletemaximals!(sxcx, Set([1, 2, 3]))

            @test isempty(sxcx)

            push!(sxcx, (1, 2, 3))
            push!(sxcx, (2, 3, 4))
            push!(sxcx, (1, 2, 5))

            deletemaximals!(sxcx, Set([1, 3]))
            @test length(SimplicialComplexes.maximalsimplices(sxcx)) == 1
        end
    end
end

@testset "Structure integrity" begin
    @testset "Labels" begin
        # Do stuff to a complex and make sure lbl_vals ∩ lbl_holes == {}
    end
end


text:
module CreateTablePermissions

import SearchLight.Migrations: create_table, column, primary_key, add_index, drop_table

function up()
  create_table(:permissions) do
    [
      primary_key()
      column(:name, :string, limit = 100)
    ]
  end

  add_index(:permissions, :name)
end

function down()
  drop_table(:permissions)
end

end


text:
module ComplementaryDistributions

using Distributions
using Random
using SpecialFunctions
using StatsFuns: twoπ, halfπ, inv2π, fourinvπ

export GeneralizedInverseGaussian, PolyaGamma, LaplaceTransformDistribution
include("generalizedinversegaussian.jl")
include("polyagamma.jl")
include("lap_transf_dist.jl")

end


text:
#=
A nieve metric for values of any type: simple subtraction of their values.

Nieve for characters,
since it doesn't represent the knowledge that humans have re classes of characters.
=#

function pointDifference(
        targetValue::T,
        corpusValue::T
        ) where{T}
    return corpusValue - targetValue
end

max_stars_repo_name: bootchk/resynthesizerJulia

max_issues_repo_name: bootchk/resynthesizerJulia

max_forks_repo_name: bootchk/resynthesizerJulia


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

"""
    FFTGS(var₁=>param₁, var₂=>param₂, ...)

FFT Gaussian simulation.

## Parameters

* `variogram` - theoretical variogram (default to `GaussianVariogram()`)
* `mean`      - mean of Gaussian field (default to `0`)

## Global parameters

* `threads` - number of threads in FFT (default to all physical cores)

### References

Gutjahr 1997. *General joint conditional simulations using a fast
Fourier transform method.*
"""
@simsolver FFTGS begin
  @param variogram = GaussianVariogram()
  @param mean = 0.0
  @global threads = cpucores()
end

function preprocess(problem::SimulationProblem, solver::FFTGS)
  hasdata(problem) && @warn "Conditional spectral Gaussian simulation is not currently supported"

  # retrieve problem info
  pdomain = domain(problem)
  npts = nelms(pdomain)
  dims = size(pdomain)
  center = CartesianIndex(dims .÷ 2)
  c = LinearIndices(dims)[center]

  # number of threads in FFTW
  FFTW.set_num_threads(solver.threads)

  mactypeof = Dict(name(v) => mactype(v) for v in variables(problem))

  # result of preprocessing
  preproc = Dict()

  for covars in covariables(problem, solver)
    for var in covars.names
      # get user parameters
      varparams = covars.params[(var,)]

      # determine value type
      V = mactypeof[var]

      # determine variogram model and mean
      γ = varparams.variogram
      μ = varparams.mean

      # check stationarity
      @assert isstationary(γ) "variogram model must be stationary"

      # compute covariances between centroid and all locations
      covs = sill(γ) .- pairwise(γ, pdomain, [c], 1:npts)
      C = reshape(covs, dims)

      # move to frequency domain
      F = sqrt.(abs.(fft(fftshift(C))))
      F[1] = zero(V) # set reference level

      # save preprocessed inputs for variable
      preproc[var] = (γ=γ, μ=μ, F=F)
    end
  end

  preproc
end

function solvesingle(problem::SimulationProblem, covars::NamedTuple, ::FFTGS, preproc)
  # retrieve problem info
  pdomain = domain(problem)
  dims = size(pdomain)

  mactypeof = Dict(name(v) => mactype(v) for v in variables(problem))

  varreal = map(covars.names) do var
    # unpack preprocessed parameters
    γ, μ, F = preproc[var]

    # determine value type
    V = mactypeof[var]

    # perturbation in frequency domain
    P = F .* exp.(im .* angle.(fft(rand(V, dims))))

    # move back to time domain
    Z = real(ifft(P))

    # adjust mean and variance
    σ² = Statistics.var(Z, mean=zero(V))
    Z .= √(sill(γ) / σ²) .* Z .+ μ

    # flatten result
    var => vec(Z)
  end

  Dict(varreal)
end


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

name = "FFMPEG"
version = v"4.1.0"

# Collection of sources required to build FFMPEG
sources = [
    ArchiveSource("https://ffmpeg.org/releases/ffmpeg-$(version.major).$(version.minor).tar.bz2",
                  "b684fb43244a5c4caae652af9022ed5d85ce15210835bce054a33fb26033a1a5"),
]

# Bash recipe for building across all platforms
# TODO: Theora once it's available
script = raw"""
cd $WORKSPACE/srcdir
cd ffmpeg-4.1/
sed -i 's/-lflite"/-lflite -lasound"/' configure
apk add coreutils yasm

if [[ "${target}" == *-linux-* ]]; then
    export ccOS="linux"
elif [[ "${target}" == *-apple-* ]]; then
    export ccOS="darwin"
elif [[ "${target}" == *-w32-* ]]; then
    export ccOS="mingw32"
elif [[ "${target}" == *-w64-* ]]; then
    export ccOS="mingw64"
elif [[ "${target}" == *-unknown-freebsd* ]]; then
    export ccOS="freebsd"
else
    export ccOS="linux"
fi

if [[ "${target}" == x86_64-* ]]; then
    export ccARCH="x86_64"
elif [[ "${target}" == i686-* ]]; then
    export ccARCH="i686"
elif [[ "${target}" == arm-* ]]; then
    export ccARCH="arm"
elif [[ "${target}" == aarch64-* ]]; then
    export ccARCH="aarch64"
elif [[ "${target}" == powerpc64le-* ]]; then
    export ccARCH="powerpc64le"
else
    export ccARCH="x86_64"
fi

pkg-config --list-all

./configure            \
  --enable-cross-compile \
  --cross-prefix=/opt/${target}/bin/${target}- \
  --arch=${ccARCH}     \
  --target-os=${ccOS}  \
  --cc="${CC}"         \
  --cxx="${CXX}"       \
  --dep-cc="${CC}"     \
  --ar=ar              \
  --nm=nm              \
  --objcc="${OBJC}"    \
  --sysinclude=${prefix}/include \
  --pkg-config=$(which pkg-config) \
  --pkg-config-flags=--static \
  --prefix=$prefix     \
  --sysroot=/opt/${target}/${target}/sys-root \
  --extra-libs=-lpthread \
  --enable-gpl         \
  --enable-version3    \
  --enable-nonfree     \
  --disable-static     \
  --enable-shared      \
  --enable-pic         \
  --disable-debug      \
  --disable-doc        \
  --enable-avresample  \
  --enable-libass      \
  --enable-libfdk-aac  \
  --enable-libfreetype \
  --enable-libmp3lame  \
  --enable-libopus     \
  --enable-libvorbis   \
  --enable-libx264     \
  --enable-libx265     \
  --enable-libvpx      \
  --enable-encoders    \
  --enable-decoders    \
  --enable-muxers      \
  --enable-demuxers    \
  --enable-parsers     \
  --enable-openssl     \
  --disable-schannel   \
  --extra-cflags="-I${prefix}/include" \
  --extra-ldflags="-L${libdir}"
make -j${nproc}
make install
install_license LICENSE.md COPYING.*
"""

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

# The products that we will ensure are always built
products = [
    ExecutableProduct("ffmpeg", :ffmpeg),
    ExecutableProduct("ffprobe", :ffprobe),
    LibraryProduct(["libavcodec", "avcodec"], :libavcodec),
    LibraryProduct(["libavdevice", "avdevice"], :libavdevice),
    LibraryProduct(["libavfilter", "avfilter"], :libavfilter),
    LibraryProduct(["libavformat", "avformat"], :libavformat),
    LibraryProduct(["libavresample", "avresample"], :libavresample),
    LibraryProduct(["libavutil", "avutil"], :libavutil),
    LibraryProduct(["libpostproc", "postproc"], :libpostproc),
    LibraryProduct(["libswresample", "swresample"], :libswresample),
    LibraryProduct(["libswscale", "swscale"], :libswscale),
]

# Dependencies that must be installed before this package can be built
# TODO: Theora once it's available
dependencies = [
    Dependency("libass_jll"),
    Dependency("libfdk_aac_jll"),
    Dependency("FriBidi_jll"),
    Dependency("FreeType2_jll"),
    Dependency("LAME_jll"),
    Dependency("libvorbis_jll"),
    Dependency("Ogg_jll"),
    Dependency("LibVPX_jll"),
    Dependency("x264_jll"),
    Dependency("x265_jll"),
    Dependency("Bzip2_jll"),
    Dependency("Zlib_jll"),
    Dependency("OpenSSL_jll"),
    Dependency("Opus_jll"),
]

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


text:
"""
# script bench_dim

- Julia version: 1.4
- Author: niallcullinane
- Date: 2020-08-20

Script to benchmark model with different input sizes for CPU and GPU.
"""

using DrWatson
@quickactivate "MEngProject"
using MEngProject,
    CUDA,
    DifferentialEquations,
    PyPlot,
    NNlib,
    ImageFiltering,
    Images,
    MEngProject.LaminartKernels,
    MEngProject.LaminartInitFunc,
    MEngProject.Utils,
    BenchmarkTools,
    Test

using OrdinaryDiffEq,
    ParameterizedFunctions, LSODA, Sundials, DiffEqDevTools, Noise

benchm_co = []
tspan = (0.0f0, 10f0)

batch_ = string(batch, "_", rand(1000:9999))
mkdir(plotsdir(string("bench_conv", batch_)))
file = "kan_sq_cont_l.png"

test_name_plt = [
    "GPU conv",
    "CPU conv",
    "CPU imfilter",
    "GPU imfilter FFT",
    "GPU imfilter IIR",
    "GPU imfilter FIR",
]

# CPU conv

global p = LaminartInitFunc.parameterInit_conv_gpu(
    datadir("img", file),
    Parameters.parameters_f32,
);

u0 = reshape(
    zeros(Float32, p.dim_i, p.dim_j * (5 * p.K + 2)),
    p.dim_i,
    p.dim_j,
    5 * p.K + 2,
    1,
)

arr1 = similar(@view u0[:, :, 1:p.K, :])
arr2 = similar(@view u0[:, :, 1:1, :])

f = LaminartFunc.LamFunction(
    arr1, #x
    similar(arr1), #m
    similar(arr1), #s
    arr2, #x_lgn,
    similar(arr1), #C,
    similar(arr1), #H_z,
    similar(arr1), # dy_temp,
    similar(arr1), # dm_temp,
    similar(arr1), # dz_temp,
    similar(arr1), # ds_temp,
    similar(arr2), # dv_temp,
    similar(arr1), # H_z_temp,
    similar(arr2), #  V_temp_1,
    similar(arr2), #  V_temp_2,
    similar(arr1), #  Q_temp,
    similar(arr1), #   P_temp
)
global prob_ke = ODEProblem(f, u0, tspan, p)
#         push!(benchm_ke, @benchmark solve(prob_ke))
global du = similar(u0)
global u_ = u0
push!(benchm_co, @benchmark f(du, u_, p, 1.0f0))

# CPU conv

global p = LaminartInitFunc.parameterInit_conv_cpu(
    datadir("img", file),
    Parameters.parameters_f32,
);

u0 = reshape(
    zeros(Float32, p.dim_i, p.dim_j * (5 * p.K + 2)),
    p.dim_i,
    p.dim_j,
    5 * p.K + 2,
    1,
)

arr1 = similar(@view u0[:, :, 1:p.K, :])
arr2 = similar(@view u0[:, :, 1:1, :])

f = LaminartFunc.LamFunction(
    arr1, #x
    similar(arr1), #m
    similar(arr1), #s
    arr2, #x_lgn,
    similar(arr1), #C,
    similar(arr1), #H_z,
    similar(arr1), # dy_temp,
    similar(arr1), # dm_temp,
    similar(arr1), # dz_temp,
    similar(arr1), # ds_temp,
    similar(arr2), # dv_temp,
    similar(arr1), # H_z_temp,
    similar(arr2), #  V_temp_1,
    similar(arr2), #  V_temp_2,
    similar(arr1), #  Q_temp,
    similar(arr1), #   P_temp
)
global prob_ke = ODEProblem(f, u0, tspan, p)
#         push!(benchm_ke, @benchmark solve(prob_ke))
global du = similar(u0)
global u_ = u0
push!(benchm_co, @benchmark f(du, u_, p, 1.0f0))

# CPU imfilter

global p = LaminartInitFunc.parameterInit_imfil_cpu(
    datadir("img", file),
    Parameters.parameters_f32,
);

u0 = reshape(
    zeros(Float32, p.dim_i, p.dim_j * (5 * p.K + 2)),
    p.dim_i,
    p.dim_j,
    5 * p.K + 2,
);

arr1 = u0[:, :, 1:p.K]
arr2 = u0[:, :, 1:1];

f = LaminartFunc.LamFunction_imfil_cpu(
    similar(arr2[:, :, 1]), #x_lgn,
    arr1, #C,
    similar(arr1), #H_z,
    similar(arr1), # H_z_temp,
    similar(arr2[:, :, 1]), # v_C_temp1,
    similar(arr2[:, :, 1]), # v_C_temp2,
    similar(arr1), # v_C_tempA,
    similar(arr1[:, :, 1]), #W_temp
);

global prob_ke = ODEProblem(f, u0, tspan, p)
#         push!(benchm_ke, @benchmark solve(prob_ke))
global du = similar(u0)
global u_ = u0
push!(benchm_co, @benchmark f(du, u_, p, 1.0f0))

# GPU imfilter FFT

global p = LaminartInitFunc.parameterInit_imfil_cpu(
    datadir("img", file),
    Parameters.parameters_f32,
);

u0 = reshape(
    zeros(Float32, p.dim_i, p.dim_j * (5 * p.K + 2)),
    p.dim_i,
    p.dim_j,
    5 * p.K + 2,
);

arr1 = u0[:, :, 1:p.K]
arr2 = u0[:, :, 1:1];

f = LaminartFunc.LamFunction_imfil_gpu_fft(
    similar(arr2[:, :, 1]), #x_lgn,
    arr1, #C,
    similar(arr1), #H_z,
    similar(arr1), # H_z_temp,
    similar(arr2[:, :, 1]), # v_C_temp1,
    similar(arr2[:, :, 1]), # v_C_temp2,
    similar(arr1), # v_C_tempA,
    similar(arr1[:, :, 1]), #W_temp
);

global prob_ke = ODEProblem(f, u0, tspan, p)
#         push!(benchm_ke, @benchmark solve(prob_ke))
global du = similar(u0)
global u_ = u0
push!(benchm_co, @benchmark f(du, u_, p, 1.0f0))

# GPU imfilter IIR

global p = LaminartInitFunc.parameterInit_imfil_cpu(
    datadir("img", file),
    Parameters.parameters_f32,
);

u0 = reshape(
    zeros(Float32, p.dim_i, p.dim_j * (5 * p.K + 2)),
    p.dim_i,
    p.dim_j,
    5 * p.K + 2,
);

arr1 = u0[:, :, 1:p.K]
arr2 = u0[:, :, 1:1];

f = LaminartFunc.LamFunction_imfil_gpu_iir(
    similar(arr2[:, :, 1]), #x_lgn,
    arr1, #C,
    similar(arr1), #H_z,
    similar(arr1), # H_z_temp,
    similar(arr2[:, :, 1]), # v_C_temp1,
    similar(arr2[:, :, 1]), # v_C_temp2,
    similar(arr1), # v_C_tempA,
    similar(arr1[:, :, 1]), #W_temp
);

global prob_ke = ODEProblem(f, u0, tspan, p)
#         push!(benchm_ke, @benchmark solve(prob_ke))
global du = similar(u0)
global u_ = u0
push!(benchm_co, @benchmark f(du, u_, p, 1.0f0))

# GPU imfilter FIR

global p = LaminartInitFunc.parameterInit_imfil_cpu(
    datadir("img", file),
    Parameters.parameters_f32,
);

u0 = reshape(
    zeros(Float32, p.dim_i, p.dim_j * (5 * p.K + 2)),
    p.dim_i,
    p.dim_j,
    5 * p.K + 2,
);

arr1 = u0[:, :, 1:p.K]
arr2 = u0[:, :, 1:1];

f = LaminartFunc.LamFunction_imfil_gpu_fir(
    similar(arr2[:, :, 1]), #x_lgn,
    arr1, #C,
    similar(arr1), #H_z,
    similar(arr1), # H_z_temp,
    similar(arr2[:, :, 1]), # v_C_temp1,
    similar(arr2[:, :, 1]), # v_C_temp2,
    similar(arr1), # v_C_tempA,
    similar(arr1[:, :, 1]), #W_temp
);

global prob_ke = ODEProblem(f, u0, tspan, p)
#         push!(benchm_ke, @benchmark solve(prob_ke))
global du = similar(u0)
global u_ = u0
push!(benchm_co, @benchmark f(du, u_, p, 1.0f0))

# benchmark plot

# time
fig, ax = plt.subplots()
for ben in enumerate(test_name_plt)
    ax.scatter(
        ben[2],
        median(benchm_co[ben[1]].times) * 1e-9,
        color = Utils.colours[ben[1]],
        edgecolors = "none",
    )
end

ax.set_ylabel("Time (\$s\$)")
ax.set_ylim(ymin = 0)
ax.grid(true)
fig.tight_layout()
plt.savefig(plotsdir(
    string("bench_conv", batch_),
    string("bench_conv_time.png"),
))
close("all")

# # memory

# fig, ax = plt.subplots()
# for ben in enumerate(test_name_plt)
#     ax.scatter(
#         ben[2],
#         benchm_i[ben[1]].memory * 1e-6,
#         color = Utils.colours[ben[1]],
#         edgecolors = "none",
#     )
# end

# ax.set_ylabel("Memory (\$MB\$)")
# ax.set_ylim(ymin=0)
# ax.grid(true)
# fig.tight_layout()
# plt.savefig(plotsdir(string("bench_conv", batch_), string("bench_convmem.png")))
# close("all")

# # alloc

# fig, ax = plt.subplots()
# for ben in enumerate(test_name_plt)
#     ax.scatter(
#         ben[2],
#         benchm_i[ben[1]].allocs,
#         color = Utils.colours[ben[1]],
#         edgecolors = "none",
#     )
# end

# ax.set_ylabel("Allocations")
# ax.set_ylim(ymin=0)
# ax.grid(true)
# fig.tight_layout()
# plt.savefig(plotsdir(
#     string("bench_conv", batch_),
#     string("bench_conv_alloc.png"),
# ))
# close("all")
benchm_gpu = nothing
benchm_cpu = nothing
y1Res_gpu = nothing
y1Res_cpu = nothing
prob_d = nothing
CUDA.reclaim()


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

"""
    VarlenDiffArrayCodec <: AbstractArrayCodec
"""
struct VarlenDiffArrayCodec <: AbstractArrayCodec
end
export VarlenDiffArrayCodec

# function _encode(io::IO, codec::VarlenDiffArrayCodec, x::T, last_x::T) where {T <: Integer}
#     write_autozz_varlen(io, x - last_x)
# end
#
#
# function _decode(io::IO, codec::VarlenDiffArrayCodec, last_x::T) where {T <: Integer}
#     read_autozz_varlen(io, T) + last_x
# end

# function _length(io::IO, codec::VarlenDiffArrayCodec, T::Type{<:Integer})
#     n::Int = 0
#     last_x::T = 0
#     while !eof(io)
#         x = _decode(io, T, last_x)
#         last_x = x
#         n += 1
#     end
#     n
# end

function encode_data!(encoded::AbstractVector{UInt8}, codec::VarlenDiffArrayCodec, data::AbstractVector{T}) where {T}
    output = IOBuffer()
    last_x::T = zero(T)
    @inbounds for x in data
        dx = signed(x) - signed(last_x)
        write_autozz_varlen(output, dx)
        last_x = x
    end

    tmp = take!(output)
    resize!(encoded, length(eachindex(tmp)))
    copyto!(encoded, tmp)

    encoded
end

function decode_data!(data::AbstractVector{T}, codec::VarlenDiffArrayCodec, encoded::AbstractVector{UInt8}) where {T}
    input = IOBuffer(encoded)
    last_x::T = zero(T)
    i = firstindex(data)
    while !eof(input)
        if lastindex(data) < i
            if isempty(data)
                resize!(data, length(eachindex(encoded)))
            else
                resize!(data, 2 * (i - firstindex(data)))
            end
        end
        dx = read_autozz_varlen(input, typeof(signed(zero(T))))
        x = last_x + dx
        last_x = x
        data[i] = x
        i += 1
    end
    if i <= lastindex(data)
        resize!(data, i - firstindex(data))
    end

    data
end


text:
using PopPKPlots
using Test

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


text:
function termplot(
    xs::AbstractVector{<:AbstractVector},
    ys::AbstractVector{<:AbstractVector};
    title::AbstractString = "",
    width::Integer = 100,
    height::Integer = 15,
    normalize::Bool = false,
    labels = nothing,
)

    for (x, y) in zip(xs, ys)
        all(isfinite, x) && all(isfinite, y) || throw(DomainError("NaN or Inf detected"))
        size(x) == size(y) || throw(DimensionMismatch("x and y must have the same size"))
    end

    _labels = labels === nothing ? Tuple("y$i" for i = 1:length(xs)) : labels
    length(_labels) == length(xs) || argerror("length of labels does not match number of lines")

    lines = zip(xs, ys, _labels)

    # shift/scale lines to fall into [-1, 1]
    if normalize
        lines = map(lines) do (x, y, name)
            _min, _max = extrema(y)
            name = @sprintf "%s [%.3g, %.3g]" name _min _max
            y = scaleandcenter!(convert(AbstractVector{Float64}, copy(y)), center = 0, range = 2)
            x, y, name
        end
    end

    xmin = round(minimum(l -> minimum(l[1]), lines), digits = 2)
    xmax = round(maximum(l -> maximum(l[1]), lines), digits = 2)
    ymin = round(minimum(l -> minimum(l[2]), lines), digits = 2)
    ymax = round(maximum(l -> maximum(l[2]), lines), digits = 2)

    # account for legend in plot width
    legend_len = maximum(l -> length(l[3]), lines)
    width -= legend_len

    (x, y, name), rest = firstrest(lines)
    plt = UnicodePlots.lineplot(
        x,
        y,
        name = name,
        xlim = [xmin, xmax],
        ylim = [ymin, ymax],
        title = title,
        width = width,
        height = height,
    )
    for (x, y, name) in rest
        UnicodePlots.lineplot!(plt, x, y, name = name)
    end

    return plt
end

function termplot(ys::AbstractVector{<:AbstractVector}; kwargs...)
    termplot(map(eachindex, ys), ys; kwargs...)
end

termplot(x::AbstractVector, y::AbstractVector; kwargs...) = termplot([x], [y]; kwargs...)
termplot(y::AbstractVector; kwargs...) = termplot([y]; kwargs...)


text:
#Calculate the test error of prediction using the model ml on the test data set
function test_linear(test::Array{StatisticalMoments{Float64}}, testname::AbstractString, fm::FeatureMap,  ml::ML_model, options, output_type::AbstractString;
  title="default", strdir="default") #km_transform::Function,
  # Get all volfracs and radiuses
  volfracs= union([td.label[1] for td in test]);
  radiuses = union([td.label[2] for td in test]);
  concentrations = union([td.label[1]/(td.label[2]^2) for td in test]);
  testerror =0;  Rsqd =0;  meanerror = 0;  zerohypo =0;
  testerrors = zeros(length(volfracs)*length(radiuses));
  #volfracs = sort([vl for vl in volfracs]);
  #radiuses = sort([rd for rd in radiuses]);
  if(output_type =="radius")
    meanhypo = mean(radiuses);
  elseif(output_type =="volumefraction")
    meanhypo = mean(volfracs); #volume fraction
  else
    meanhypo = mean(concentrations); #concentration
  end

  if(output_type =="radius")
    getdt = mom ->  mom.label[2];  # radius
  elseif(output_type =="volumefraction")
    getdt = mom ->  mom.label[1]; #volume fraction
  else
    getdt = mom ->  mom.label[1]/(mom.label[2]^2);  # concentration
  end
  reptd = collect([getdt(mom) for mom in test]);
  meanerror = sum((meanhypo-reptd).^2);
  zerohypo = sum(reptd.^2);
  pred = collect([ predict_linear(mom, fm, ml, options)[1] for mom in test ]);
  squarederror = sum((pred.-reptd).^2);
  absoluterror = sqrt(squarederror) /sqrt(zerohypo);
  Rsqd = 1 - squarederror/meanerror;

  if(options.plotfigures)
    println(output_type," ",floor(1000.0*absoluterror)/10.0, "% absolute test error")
    ## Plotting the regression
    #println("Plotting...check folder ./figures for the plots scatvol-$(ml.name)")
    gap = maximum(reptd)-minimum(reptd);
    if title == "default" title = "Rsqd = $(round(100*Rsqd)/100)" end
    scatter(reptd,pred,  xlabel = "true $(output_type)",
    ylabel = "predicted $(output_type)", label = "",  title=title)
    line  =minimum(reptd):gap/100.01:maximum(reptd);
    plot!(line,line,   label = "")
    if strdir == "default"
      strdir = isdir("../figures/") ? "../figures/" : ""
    end
     open("./test_output.txt", "w") do f
        write(f, "$(output_type)\n")
            for td in reptd
               write(f, "$(td)\n")
            end
        write(f, "\n\n")
        write(f, "predicted\n")
            for td in pred
               write(f, "$(td)\n")
            end
        write(f, "\n\n")
        write(f, "line\n")
            for td in line
               write(f, "$(td)\n")
            end
     end
#     savefig("$(strdir)scat-$(ml.name)-$(testname).svg");
    savefig("$(strdir)scat-$(ml.name)-$(testname).pdf");
    println("R squared error $(output_type)= ",round(100*Rsqd)/100) #round(100*Rsqd)/100
    testerror_heatmap(test, fm, ml, options, output_type; strdir=strdir)
  end

  return round(100*Rsqd)/100
end

## OLD version

#    rep =[]; #The repeated test outputs
#     pred = []; #The predicted outputs
#     totcount =1;
#  #     label_f = mom -> [mom.label[1],mom.label[2]]
#   #labels = union([ label_f(td)  for td in moms])

#     for mom in test
#             vl = mom.label[1] ;
#             rd = mom.label[2] ;
#             if(output_type =="radius")
#                 dt = rd;
#             else
#                 dt = vl/(rd)^2;
#             end
#             rep = [rep ; dt];
#             ypred = predict_linear(mom, fm, ml, options);
#             ypred= ypred[1];
#             pred = [pred ; ypred];
#             testerrors[totcount] =  abs(ypred -dt)/dt;
#             Rsqd = Rsqd+ (ypred -dt)^2; # Accumulating R squared error
#             meanerror= meanerror+ (meanhypo-dt)^2;#Calculating the error of the  meanerrorthesis
#             zerohypo= zerohypo+ dt^2;#Calculating the error of the  meanerrorthesis
#             if(isnan(testerror))
#                 print("dt: ", dt, "ypred: ",ypred)
#             end
#             totcount = totcount+1;
# #        end
#     end
#     testerror  = mean(testerrors);
#     absoluterror = sqrt(Rsqd) /sqrt(zerohypo);
#     println("squared error: ", Rsqd)
#     Rsqd = 1- Rsqd/meanerror;


text:
using jInv.InverseSolve
using jInv.Utils

n = 200
x = rand(n) + im*rand(n)
A = sprand(n,2*n,0.1)

Hx = A[1:2:end,1:2:end]*real(x) +
     A[1:2:end,2:2:end]*imag(x) +
     im*A[2:2:end,1:2:end]*real(x) +
     im*A[2:2:end,2:2:end]*imag(x)

@test all(abs.(HessMatVec(A,x)-Hx).<1e-12)


text:
# Kernel Principal Component Analysis

import Arpack

"""Center a kernel matrix"""
struct KernelCenter{T<:Real}
    means::AbstractVector{T}
    total::T
end

"""Fit `KernelCenter` object"""
function fit(::Type{KernelCenter}, K::AbstractMatrix{T}) where {T<:Real}
    n = size(K, 1)
    means = vec(mean(K, dims=2))
    KernelCenter(means, sum(means) / n)
end

"""Center kernel matrix."""
function transform!(C::KernelCenter, K::AbstractMatrix{<:Real})
    r, c = size(K)
    tot = C.total
    means = mean(K, dims=1)
    @simd for i in 1:r
        for j in 1:c
            @inbounds K[i, j] -= C.means[i] + means[j] - tot
        end
    end
    return K
end

"""Kernel PCA type"""
struct KernelPCA{T<:Real}
    X::AbstractMatrix{T}           # fitted data or precomputed kernel
    ker::Union{Nothing, Function}  # kernel function
    center::KernelCenter           # kernel center
    λ::AbstractVector{T}           # eigenvalues  in feature space
    α::AbstractMatrix{T}           # eigenvectors in feature space
    inv::AbstractMatrix{T}         # inverse transform coefficients
end

## properties

indim(M::KernelPCA) = size(M.X, 1)
outdim(M::KernelPCA) = length(M.λ)

projection(M::KernelPCA) = M.α ./ sqrt.(M.λ')
principalvars(M::KernelPCA) = M.λ

## use

"""Calculate transformation to kernel space"""
function transform(M::KernelPCA, x::AbstractVecOrMat{<:Real})
    k = pairwise(M.ker, M.X, x)
    transform!(M.center, k)
    return projection(M)'*k
end

transform(M::KernelPCA) = sqrt.(M.λ) .* M.α'

"""Calculate inverse transformation to original space"""
function reconstruct(M::KernelPCA, y::AbstractVecOrMat{<:Real})
    if size(M.inv, 1) == 0
        throw(ArgumentError("Inverse transformation coefficients are not available, set `inverse` parameter when fitting data"))
    end
    Pᵗ = M.α' .* sqrt.(M.λ)
    k = pairwise(M.ker, Pᵗ, y)
    return M.inv*k
end

## show

function Base.show(io::IO, M::KernelPCA)
    print(io, "Kernel PCA(indim = $(indim(M)), outdim = $(outdim(M)))")
end

## interface functions

function fit(::Type{KernelPCA}, X::AbstractMatrix{T};
             kernel::Union{Nothing, Function} = (x,y)->x'y,
             maxoutdim::Int = min(size(X)...),
             remove_zero_eig::Bool = false, atol::Real = 1e-10,
             solver::Symbol = :eig,
             inverse::Bool = false,  β::Real = convert(T, 1.0),
             tol::Real = 0.0, maxiter::Real = 300) where {T<:Real}

    d, n = size(X)
    maxoutdim = min(min(d, n), maxoutdim)

    # set kernel function if available
    K = if isa(kernel, Function)
        pairwise(kernel, X)
    elseif kernel === nothing
        @assert issymmetric(X) "Precomputed kernel matrix must be symmetric."
        inverse = false
        X
    else
        throw(ArgumentError("Incorrect kernel type. Use a function or a precomputed kernel."))
    end

    # center kernel
    center = fit(KernelCenter, K)
    transform!(center, K)

    # perform eigenvalue decomposition
    evl, evc = if solver == :eigs || SparseArrays.issparse(K)
        evl, evc = Arpack.eigs(K, nev=maxoutdim, which=:LR, v0=2.0*rand(n) .- 1.0, tol=tol, maxiter=maxiter)
        real.(evl), real.(evc)
    else
        Eg = eigen(Hermitian(K))
        Eg.values, Eg.vectors
    end

    # sort eigenvalues in descending order
    ord = sortperm(evl; rev=true)
    ord = ord[1:min(length(ord), maxoutdim)]

    # remove zero eigenvalues
    λ, α = if remove_zero_eig
        ez = map(!, isapprox.(evl[ord], zero(T), atol=atol))
        evl[ord[ez]], evc[:, ord[ez]]
    else
        evl[ord], evc[:, ord]
    end

    # calculate inverse transform coefficients
    Q = zeros(T, 0, 0)
    if inverse
        Pᵗ = α' .* sqrt.(λ)
        KT = pairwise(kernel, Pᵗ)
        Q = (KT + diagm(0 => fill(β, size(KT,1)))) \ X'
    end

    KernelPCA(X, kernel, center, λ, α, Q')
end


text:
struct MockMLJ <: Convention end

@testset "convention" begin
    set_convention(ScientificTypesBase.NoConvention())
    c = ""
    @test_logs (:warn, "No convention specified. Did you forget to use the `set_convention` function?") (c = ST.convention())
    @test c isa ST.NoConvention

    set_convention(MockMLJ())
    c = ST.convention()
    @test c isa MockMLJ
end

@testset "trait" begin
    TRAIT_FUNCTION_GIVEN_NAME[:table] = Tables.istable

    isjunk(::Any)     = false
    isjunk(s::String) = s == "junk" ? true : false
    TRAIT_FUNCTION_GIVEN_NAME[:junk] = isjunk

    X = [1,2,3]
    @test ST.trait(X) == :other
    X = (x = [1,2,3],
         y = [5,5,7])
    @test ST.trait(X) == :table
    TRAIT_FUNCTION_GIVEN_NAME[:named_tuple] = X -> X isa NamedTuple
    @test_throws Exception ST.trait(X)
end

@testset "nonmissing" begin
    U = Union{Missing,Int}
    @test nonmissing(U) == Int
end

@testset "table" begin
    T0 = Table(Continuous)
    @test T0 == Table{K} where K<:AbstractVector{<:Continuous}
    T1 = Table(Continuous, Count)
    @test T1 == Table{K} where K<:Union{AbstractVector{<:Continuous}, AbstractVector{<:Count}}
    T2 = Table(Continuous, Union{Missing,Continuous})
    @test T2 == Table{K} where K<:Union{AbstractVector{<:Union{Missing,Continuous}}}
end


text:
## Various solver API's used in the past.  These functions are due to be standardized, and obsolete code / functions removed.

export solveTree!, solveGraph!
export fetchCliqHistoryAll!

## ==============================================================================================
## Launch the tasks/treads for cliques
## ==============================================================================================
"""
    $SIGNATURES

Start tasks (@async or Threads.@spawn threads if multithread=true) to solve the factor graph on the tree.
"""
function taskSolveTree!(dfg::AbstractDFG,
                          treel::AbstractBayesTree,
                          timeout::Union{Nothing, <:Real}=nothing;
                          oldtree::AbstractBayesTree=BayesTree(),
                          drawtree::Bool=false,
                          verbose::Bool=false,
                          verbosefid=stdout,
                          limititers::Int=-1,
                          limititercliqs::Vector{Pair{Symbol, Int}}=Pair{Symbol, Int}[],
                          downsolve::Bool=false,
                          incremental::Bool=false,
                          multithread::Bool=false,
                          skipcliqids::Vector{Symbol}=Symbol[],
                          recordcliqs::Vector{Symbol}=Symbol[],
                          delaycliqs::Vector{Symbol}=Symbol[],
                          smtasks=Task[],
                          algorithm::Symbol=:default)
  #
  # revert DOWNSOLVED status to INITIALIZED in preparation for new upsolve
  resetTreeCliquesForUpSolve!(treel)

  drawtree ? drawTree(treel, show=false, filepath=joinLogPath(dfg,"bt.dot")) : nothing

  cliqHistories = Dict{Int,Vector{CSMHistoryTuple}}()

  resize!(smtasks, getNumCliqs(treel))

  approx_iters = getNumCliqs(treel)*24
  solve_progressbar = verbose ? nothing : ProgressUnknown("Solve Progress: approx max $approx_iters, at iter")

  # queue all the tasks/threads
  if !isTreeSolved(treel, skipinitialized=true)
    @sync begin
      monitortask = monitorCSMs(treel, smtasks)
      # duplicate int i into async (important for concurrency)
      for i in 1:getNumCliqs(treel) # TODO, this might not always work?
        scsym = getCliqFrontalVarIds(getClique(treel, i))
        if length(intersect(scsym, skipcliqids)) == 0

          limthiscsm = filter(x -> (x[1] in scsym), limititercliqs)
          limiter = 0<length(limthiscsm) ? limthiscsm[1][2] : limititers

          if multithread
            smtasks[i] = Threads.@spawn tryCliqStateMachineSolve!(dfg, treel, i, timeout; algorithm=algorithm, oldtree=oldtree, verbose=verbose, verbosefid=verbosefid, drawtree=drawtree, limititers=limititers, downsolve=downsolve, incremental=incremental, delaycliqs=delaycliqs, recordcliqs=recordcliqs, solve_progressbar=solve_progressbar)
          else
            smtasks[i] = @async tryCliqStateMachineSolve!(dfg, treel, i, timeout; algorithm=algorithm, oldtree=oldtree, verbose=verbose, verbosefid=verbosefid, drawtree=drawtree, limititers=limiter, downsolve=downsolve, incremental=incremental, delaycliqs=delaycliqs, recordcliqs=recordcliqs, solve_progressbar=solve_progressbar)
          end
        end # if
      end # for
    end # sync
  end # if

  # if record cliques is in use, else skip computational delay
  0 == length(recordcliqs) ? nothing : fetchCliqHistoryAll!(smtasks, cliqHistories)

  !isnothing(solve_progressbar) && finish!(solve_progressbar)

  return smtasks, cliqHistories
end

function tryCliqStateMachineSolve!( dfg::G,
                                    treel::AbstractBayesTree,
                                    cliqKey::Int,
                                    timeout::Union{Nothing, <:Real}=nothing;
                                    oldtree::AbstractBayesTree=BayesTree(),
                                    verbose::Bool=false,
                                    verbosefid=stdout,
                                    drawtree::Bool=false,
                                    limititers::Int=-1,
                                    downsolve::Bool=false,
                                    incremental::Bool=false,
                                    delaycliqs::Vector{Symbol}=Symbol[],
                                    recordcliqs::Vector{Symbol}=Symbol[],
                                    solve_progressbar=nothing,
                                    algorithm::Symbol=:default) where G <: AbstractDFG
  #
  clst = :na
  cliq = getClique(treel, cliqKey) #treel.cliques[cliqKey]
  syms = getCliqFrontalVarIds(cliq) # ids =

  oldcliq = attemptTreeSimilarClique(oldtree, getCliqueData(cliq))
  oldcliqdata = getCliqueData(oldcliq)

  opts = getSolverParams(dfg)
  # Base.rm(joinpath(opts.logpath,"logs/cliq$i"), recursive=true, force=true)
  mkpath(joinpath(opts.logpath,"logs/cliq$(cliq.id)/"))
  logger = SimpleLogger(open(joinpath(opts.logpath,"logs/cliq$(cliq.id)/log.txt"), "w+")) # NullLogger()
  # global_logger(logger)
  history = Vector{CSMHistoryTuple}()
  recordthiscliq = length(intersect(recordcliqs,syms)) > 0
  delaythiscliq = length(intersect(delaycliqs,syms)) > 0
  try
    history = initStartCliqStateMachine!( dfg, treel, cliq, timeout;
                                          oldcliqdata=oldcliqdata,
                                          drawtree=drawtree, verbose=verbose, verbosefid=verbosefid,
                                          limititers=limititers, downsolve=downsolve,
                                          recordhistory=recordthiscliq, incremental=incremental,
                                          delay=delaythiscliq, logger=logger, solve_progressbar=solve_progressbar,
                                          algorithm=algorithm )
    #
    # cliqHistories[cliqKey] = history
    if length(history) >= limititers && limititers != -1
      # @warn "writing logs/cliq$(cliq.id)/csm.txt"
      # @save "/tmp/cliqHistories/cliq$(cliq.id).jld2" history
      fid = open(joinpath(opts.logpath,"logs/cliq$(cliq.id)/csm.txt"), "w")
      printCliqHistorySummary(fid, history)
      close(fid)
    end
    flush(logger.stream)
    close(logger.stream)
    # clst = getCliqueStatus(cliq)
    # clst = cliqInitSolveUp!(dfg, treel, cliq, drawtree=drawtree, limititers=limititers )
  catch err
    bt = catch_backtrace()
    println()
    showerror(stderr, err, bt)
    # @warn "writing /tmp/caesar/logs/cliq$(cliq.id)/*.txt"
    fid = open(joinpath(opts.logpath,"logs/cliq$(cliq.id)/stacktrace.txt"), "w")
    showerror(fid, err, bt)
    close(fid)
    fid = open(joinpath(opts.logpath,"logs/cliq$(cliq.id)_stacktrace.txt"), "w")
    showerror(fid, err, bt)
    close(fid)
    # @save "/tmp/cliqHistories/$(cliq.label).jld2" history
    fid = open(joinpath(opts.logpath,"logs/cliq$(cliq.id)/csm.txt"), "w")
    printCliqHistorySummary(fid, history)
    close(fid)
    fid = open(joinpath(opts.logpath,"logs/cliq$(cliq.id)_csm.txt"), "w")
    printCliqHistorySummary(fid, history)
    close(fid)
    flush(logger.stream)
    close(logger.stream)
    rethrow()
  end
  # if !(clst in [UPSOLVED; DOWNSOLVED; MARGINALIZED])
  #   error("Clique $(cliq.id), initInferTreeUp! -- cliqInitSolveUp! did not arrive at the desired solution statu: $clst")
  # end
  return history
end

"""
    $SIGNATURES

Standalone state machine solution for a single clique.

Related:

initInferTreeUp!
"""
function solveCliqWithStateMachine!(dfg::G,
                                    tree::AbstractBayesTree,
                                    frontal::Symbol;
                                    iters::Int=200,
                                    downsolve::Bool=true,
                                    recordhistory::Bool=false,
                                    verbose::Bool=false,
                                    nextfnc::Function=canCliqMargRecycle_StateMachine,
                                    prevcsmc::Union{Nothing,CliqStateMachineContainer}=nothing) where G <: AbstractDFG
  #
  cliq = getClique(tree, frontal)

  children = getChildren(tree, cliq)#Graphs.out_neighbors(cliq, tree.bt)

  prnt = getParent(tree, cliq)

  destType = (G <: InMemoryDFGTypes) ? G : InMemDFGType

  csmc = isa(prevcsmc, Nothing) ? CliqStateMachineContainer(dfg, initfg(destType, solverParams=getSolverParams(dfg)), tree, cliq, prnt, children, false, true, true, downsolve, false, getSolverParams(dfg)) : prevcsmc
  statemachine = StateMachine{CliqStateMachineContainer}(next=nextfnc, name="cliq$(cliq.id)")
  while statemachine(csmc, verbose=verbose, iterlimit=iters, recordhistory=recordhistory); end
  statemachine, csmc
end

## ==============================================================================================
# Prepare CSM (based on FSM) entry points
## ==============================================================================================

"""
    $SIGNATURES

Fetch solver history from clique state machines that have completed their async Tasks and store in the `hist::Dict{Int,Tuple}` dictionary.
"""
function fetchCliqHistoryAll!(smt::Vector{Task},
                              hist::Dict{Int,Vector{CSMHistoryTuple}}=Dict{Int,Vector{CSMHistoryTuple}}() )
  #
  for i in 1:length(smt)
    sm = smt[i]
    # only fetch states that have completed processing
    if sm.state == :done
      haskey(hist, i) ? @warn("overwriting existing history key $i") : nothing
      hist[i] = fetch(sm)
    elseif !isnothing(sm.storage) && haskey(sm.storage, :statemachine)
      hist[i] = CSMHistoryTuple.(sm.storage[:statemachine].history)
    end
  end
  hist
end

## ==============================================================================================
# Nominal user interface to the solver
## ==============================================================================================

"""
    $SIGNATURES

Perform inference over the Bayes tree according to `opt::SolverParams`.

Notes
- Variety of options, including fixed-lag solving -- see `getSolverParams(fg)` for details.
  - See online Documentation for more details: https://juliarobotics.org/Caesar.jl/latest/
- Latest result always stored in `solvekey=:default`.
- Experimental `storeOld::Bool=true` will duplicate the current result as supersolve `:default_k`.
  - Based on `solvable==1` assumption.
- `limititercliqs` allows user to limit the number of iterations a specific CSM does.
- keywords `verbose` and `verbosefid::IOStream` can be used together to to send output to file or default `stdout`.
- keyword `recordcliqs=[:x0; :x7...]` identifies by frontals which cliques to record CSM steps.
  - See [`repeatCSMStep!`](@ref), [`printCSMHistoryLogical`](@ref), [`printCSMHistorySequential`](@ref)

Example
```julia
# pass in old `tree` to enable compute recycling -- see online Documentation for more details
tree, smt, hist = solveTree!(fg [,tree])
```

Related

[`solveCliq!`](@ref), [`buildTreeReset!`](@ref), [`repeatCSMStep`](@ref), [`printCSMHistoryLogical`](@ref)
"""
function solveTree!(dfgl::AbstractDFG,
                    oldtree::AbstractBayesTree=BayesTree();
                    timeout::Union{Nothing, <:Real}=nothing,
                    storeOld::Bool=false,
                    verbose::Bool=false,
                    verbosefid=stdout,
                    delaycliqs::Vector{Symbol}=Symbol[],
                    recordcliqs::Vector{Symbol}=Symbol[],
                    limititercliqs::Vector{Pair{Symbol, Int}}=Pair{Symbol, Int}[],
                    injectDelayBefore::Union{Nothing,Vector{<:Pair{Int,<:Pair{<:Function, <:Real}}}}=nothing,
                    skipcliqids::Vector{Symbol}=Symbol[],
                    eliminationOrder::Union{Nothing, Vector{Symbol}}=nothing,
                    variableOrder::Union{Nothing, Vector{Symbol}}=nothing,
                    eliminationConstraints::Vector{Symbol}=Symbol[],
                    variableConstraints=nothing,
                    smtasks::Vector{Task}=Task[],
                    dotreedraw = Int[1;],
                    runtaskmonitor::Bool=true,
                    algorithm::Symbol=:default,
                    multithread::Bool=false  )
  #
  # workaround in case isolated variables occur
  ensureSolvable!(dfgl)
  opt = getSolverParams(dfgl)

  # depcrecation
  if variableOrder !== nothing
    @warn "`variableOrder` keyword is deprecated, use `eliminationOrder` instead."
    eliminationOrder = variableOrder
  end
  if variableConstraints !== nothing
    @warn "`variableConstraints` keyword is deprecated, use `eliminationConstraints` instead."
    eliminationConstraints = variableConstraints
  end

  # showtree should force drawtree
  opt.showtree && !opt.drawtree ? @info("Since .showtree=true, also bumping .drawtree=true") : nothing
  opt.drawtree |= opt.showtree

  # depcrecation
  # update worker pool incase there are more or less
  setWorkerPool!()
  if opt.multiproc && nprocs() == 1
    @info "Setting `.multiproc=false` since `Distributed.nprocs() == 1`"
    opt.multiproc = false
  end

  if opt.graphinit
    @info "Ensure variables are all initialized (graphinit)"
    ensureAllInitialized!(dfgl)
    if algorithm==:parametric
      @warn "Parametric is using default graphinit"
      initParametricFrom!(dfgl)
    end
  end
  # construct tree
  @info "Solving over the Bayes (Junction) tree."

  hist = Dict{Int, Vector{CSMHistoryTuple}}()

  if opt.isfixedlag
      @info "Quasi fixed-lag is enabled (a feature currently in testing)!"
      fifoFreeze!(dfgl)
  end

  # perhaps duplicate current value
  if storeOld || opt.dbg
    ss = listSupersolves(dfgl) .|> string
    ss_ = ss[occursin.(r"default_",ss)] .|> x->x[9:end]
    filter!(x->occursin(r"^\d+$",x), ss_)  # ss_ = ss_[occursin.(r"^\d$",ss_)]
    allk = parse.(Int,ss_)
    nextk = length(allk) == 0 ? 0 : maximum(allk)+1
    newKey = Symbol(:default_, nextk)
    deepcopySupersolve!(dfgl, newKey, :default, solvable=1)
    # foreach(x->updateVariableSolverData!(dfgl, x, getSolverData(getVariable(dfgl,x), :default), newKey, true, Symbol[]), ls(dfgl, solvable=1))
    @info "storeOld=true, previous :default deepcopied into $newKey for solvable==1 variables."
  end

  orderMethod = 0 < length(eliminationConstraints) ? :ccolamd : :qr

  # current incremental solver builds a new tree and matches against old tree for recycling.
  tree = buildTreeReset!(dfgl, eliminationOrder, drawpdf=false, show=opt.showtree,ensureSolvable=false,filepath=joinpath(opt.logpath,"bt.pdf"), eliminationConstraints=eliminationConstraints, ordering=orderMethod)

  # setAllSolveFlags!(tree, false)

  initTreeMessageChannels!(tree)

  # if desired, drawtree in a loop
  treetask, _dotreedraw = drawTreeAsyncLoop(tree, opt; dotreedraw = dotreedraw)

  @info "Do tree based init-inference on tree"

  # choose algorithm
  if algorithm == :parametric
    @error "Under development, do not use, see #539"
    storeOld && @error("parametric storeOld keyword not wired up yet.")
    # alltasks, hist = taskSolveTreeParametric!(dfgl, tree; smtasks=smtasks, oldtree=tree, verbose=verbose, drawtree=opt.drawtree, recordcliqs=recordcliqs, limititers=opt.limititers, incremental=opt.incremental, skipcliqids=skipcliqids, delaycliqs=delaycliqs, multithread=multithread )
    smtasks, hist = taskSolveTree!( dfgl, tree, timeout; algorithm=algorithm,
                                    multithread=multithread, smtasks=smtasks, oldtree=oldtree,
                                    verbose=verbose, verbosefid=verbosefid,
                                    drawtree=opt.drawtree, recordcliqs=recordcliqs,
                                    limititers=opt.limititers, downsolve=opt.downsolve,
                                    incremental=opt.incremental, skipcliqids=skipcliqids, delaycliqs=delaycliqs)
    @info "Finished tree based Parametric inference"
  else # fall back is :default with take CSM
    if opt.async
      @async smtasks, hist = taskSolveTree!(dfgl, tree, timeout;algorithm=algorithm,
                                            multithread=multithread, smtasks=smtasks, oldtree=oldtree,
                                            verbose=verbose, verbosefid=verbosefid,
                                            drawtree=opt.drawtree, recordcliqs=recordcliqs,
                                            limititers=opt.limititers, downsolve=opt.downsolve,
                                            incremental=opt.incremental, skipcliqids=skipcliqids, delaycliqs=delaycliqs)
    else
      smtasks, hist = taskSolveTree!( dfgl, tree, timeout;
                                      algorithm=algorithm, multithread=multithread, smtasks=smtasks, oldtree=oldtree,
                                      verbose=verbose, verbosefid=verbosefid, drawtree=opt.drawtree, recordcliqs=recordcliqs,
                                      limititers=opt.limititers, downsolve=opt.downsolve, incremental=opt.incremental,
                                      skipcliqids=skipcliqids, delaycliqs=delaycliqs, limititercliqs=limititercliqs)
      @info "Finished tree based init-inference"
    end
  end

  # NOTE copy of data from new tree in to replace outisde oldtree
  oldtree.bt = tree.bt
  oldtree.btid = tree.btid
  oldtree.cliques = tree.cliques
  oldtree.frontals = tree.frontals
  oldtree.eliminationOrder = tree.eliminationOrder
  oldtree.buildTime = tree.buildTime

  hist = !opt.async ? fetchCliqHistoryAll!(smtasks) : hist

  if opt.drawtree && opt.async
    @warn "due to async=true, only keeping task pointer, not stopping the drawtreerate task!  Consider not using .async together with .drawtreerate != 0"
    push!(smtasks, treetask)
  else
    dotreedraw[1] = 0
  end

  # if debugging and not async then also print the CSMHistory
  if opt.dbg && !opt.async
    printCSMHistorySequential(hist, joinLogPath(dfgl,"HistoryCSMAll.txt") )
  end

  return oldtree, smtasks, hist
end

"""
    solveGrapn!

See `solveTree!`.
"""
const solveGraph! = solveTree!

"""
    $SIGNATURES

Perform inference over one clique in the Bayes tree according to `opt::SolverParams`.

Example
```julia
tree = buildTreeReset!(fg)
smt, hist = solveCliq!(fg, tree, :x1 [,cliqHistories=hist] )
```

Related

[`solveTree!`](@ref), [`buildTreeReset!`](@ref)
"""
function solveCliq!(dfgl::AbstractDFG,
                    tree::AbstractBayesTree,
                    cliqid::Symbol;
                    verbose::Bool=false,
                    recordcliq::Bool=false,
                    # cliqHistories = Dict{Int,Vector{CSMHistoryTuple}}(),
                    async::Bool=false )
  #
  # hist = Vector{CSMHistoryTuple}()
  opt = DFG.getSolverParams(dfgl)

  if opt.isfixedlag
      @info "Quasi fixed-lag is enabled (a feature currently in testing)!"
      fifoFreeze!(dfgl)
  end

  # if !isTreeSolved(treel, skipinitialized=true)
  cliq = getClique(tree, cliqid)
  cliqtask = if async
    @async tryCliqStateMachineSolve!(dfgl, tree, cliq.id, verbose=verbose, drawtree=opt.drawtree, limititers=opt.limititers, downsolve=opt.downsolve,recordcliqs=(recordcliq ? [cliqid] : Symbol[]), incremental=opt.incremental)
  else
    tryCliqStateMachineSolve!(dfgl, tree, cliq.id, verbose=verbose, drawtree=opt.drawtree, limititers=opt.limititers, downsolve=opt.downsolve,recordcliqs=(recordcliq ? [cliqid] : Symbol[]), incremental=opt.incremental) # N=N
  end
  # end # if

  # post-hoc store possible state machine history in clique (without recursively saving earlier history inside state history)
  # assignTreeHistory!(tree, cliqHistories)

  # cliqHistories
  return cliqtask
end


text:
"""

    dffit(setting, i)

Calculate the effect of the ith observation on the linear regression fit.

# Arguments
- `setting::RegressionSetting`: A regression setting object.
- `i::Int`: Index of the observation.

# Examples
```julia-repl
julia> reg = createRegressionSetting(@formula(calls ~ year), phones);
julia> dffit(reg, 1)
2.3008326745719785

julia> dffit(reg, 15)
2.7880619386124295

julia> dffit(reg, 16)
3.1116532421969794

julia> dffit(reg, 17)
4.367981450347031

julia> dffit(reg, 21)
-5.81610150322166
```

# References
Belsley, David A., Edwin Kuh, and Roy E. Welsch. Regression diagnostics:
Identifying influential data and sources of collinearity. Vol. 571. John Wiley & Sons, 2005.
"""
function dffit(setting::RegressionSetting, i::Int)::Float64
    X = designMatrix(setting)
    y = responseVector(setting)
    return dffit(X, y, i)
end

function dffit(X::Array{Float64,2}, y::Array{Float64,1}, i::Int)::Float64
    n, _ = size(X)
    indices = [j for j in 1:n if i != j]
    olsfull = ols(X, y)
    Xsub = X[indices,:]
    ysub = y[indices]
    olsjacknife = ols(Xsub, ysub)
    return predict(olsfull, X)[i] - predict(olsjacknife, X)[i]
end

"""
    dffit(setting)

Calculate `dffit` for all observations.

# Arguments
- `setting::RegressionSetting`: A regression setting object.

# Examples
```julia-repl
julia> reg = createRegressionSetting(@formula(calls ~ year), phones);

julia> dffit(reg)
24-element Array{Float64,1}:
   2.3008326745719785
   1.2189579001467337
   0.35535667547543426
  -0.14458523141740898
  -0.5558346324846752
  -0.8441316814464983
  -1.0329184407957257
  -1.16600692151232
  -1.2005633711667656
  -1.2549187193476428
  -1.3195581500053777
  -1.42383876236147
  -1.5917690629803474
  -1.6582086833534504
   2.7880619386124295
   3.1116532421969794
   4.367981450347031
   5.927603041427858
   8.442860517217582
  12.370243663029527
  -5.81610150322166
 -10.089153963127842
 -12.10803256546825
 -14.67006851119936
```

# References
Belsley, David A., Edwin Kuh, and Roy E. Welsch. Regression diagnostics:
Identifying influential data and sources of collinearity. Vol. 571. John Wiley & Sons, 2005.
"""
function dffit(setting::RegressionSetting)::Array{Float64,1}
    n, _ = size(setting.data)
    result = [dffit(setting, i) for i in 1:n]
    return result
end

function dffit(X::Array{Float64,2}, y::Array{Float64,1})::Array{Float64,1}
    n, _ = size(X)
    result = [dffit(X, y, i) for i in 1:n]
    return result
end

"""
    hatmatrix(setting)

Calculate Hat matrix of dimensions n x n for a given regression setting with n observations.

# Arguments
- `setting::RegressionSetting`: A regression setting object.

# Examples
```julia-repl
julia> reg = createRegressionSetting(@formula(calls ~ year), phones);
julia> size(hatmatrix(reg))

(24, 24)
"""
function hatmatrix(setting::RegressionSetting)::Array{Float64,2}
    X = designMatrix(setting)
    return hatmatrix(X)
end

function hatmatrix(X::Array{Float64,2})::Array{Float64,2}
    return X * inv(X'X) * X'
end

"""
    studentizedResiduals(setting)

Calculate Studentized residuals for a given regression setting.

# Arguments:
- `setting::RegressionSetting`: A regression setting object.

# Examples
```julia-repl
julia> reg = createRegressionSetting(@formula(calls ~ year), phones);

julia> studentizedResiduals(reg)
24-element Array{Float64,1}:
  0.2398783264505892
  0.1463945666608097
  0.04934549995087145
 -0.023289236798461784
 -0.10408303320973748
 -0.18382934382804111
 -0.2609395640240455
 -0.33934473417314376
 -0.3973205657179429
 -0.46258080183149236
 -0.5261488085924144
 -0.5918396227060093
 -0.6616423337899147
 -0.6611792918262785
  1.0277190922689816
  1.0297863954540103
  1.2712201589839855
  1.4974523565936426
  1.8386296155264197
  2.316394853333409
 -0.9368354141338643
 -1.4009989983319822
 -1.4541520919831887
 -1.529459974327181
```
"""
function studentizedResiduals(setting::RegressionSetting)::Array{Float64,1}
    X = designMatrix(setting)
    y = responseVector(setting)
    return studentizedResiduals(X, y)
end

function studentizedResiduals(X::Array{Float64,2}, y::Array{Float64,1})::Array{Float64,1}
    olsreg = ols(X, y)
    n, p = size(X)
    e = residuals(olsreg)
    s = sqrt(sum(e.^2.0) / (n - p))
    hat = hatmatrix(X)
    stde = [e[i] / (s * sqrt(1.0 - hat[i, i])) for i in 1:n]
    return stde
end

"""
    adjustedResiduals(setting)

Calculate adjusted residuals for a given regression setting.

# Arguments:
- `setting::RegressionSetting`: A regression setting object.

# Examples
```julia-repl
julia> reg = createRegressionSetting(@formula(calls ~ year), phones);
julia> adjustedResiduals(reg)
24-element Array{Float64,1}:
  13.486773572526268
   8.2307993473897
   2.774371467851612
  -1.3093999279776498
  -5.851901346871404
 -10.335509559699863
 -14.670907823058053
 -19.07911256736661
 -22.338710565623828
 -26.00786250934617
 -29.58187157605512
 -33.27523207616458
 -37.19977737822219
 -37.173743587631165
  57.781855070799956
  57.898085871534626
  71.47231139524963
  84.19185329435882
 103.37399662263209
 130.23557965295348
 -52.6720662600165
 -78.76891816539992
 -81.75736547266746
 -85.9914301855088
```
"""
function adjustedResiduals(setting::RegressionSetting)::Array{Float64,1}
    X = designMatrix(setting)
    y = responseVector(setting)
    return adjustedResiduals(X, y)
end

function adjustedResiduals(X::Array{Float64,2}, y::Array{Float64,1})::Array{Float64,1}
    olsreg = ols(X, y)
    n, p = size(X)
    e = residuals(olsreg)
    hat = hatmatrix(X)
    stde = [e[i] / (sqrt(1 - hat[i, i])) for i in 1:n]
    return stde
end

"""

    jacknifedS(setting, k)

Estimate standard error of regression with the kth observation is dropped.

# Arguments
- `setting::RegressionSetting`: A regression setting object.
- `k::Int`: Index of the omitted observation.

# Examples
```julia-repl
julia> reg = createRegressionSetting(@formula(calls ~ year), phones);
julia> jacknifedS(reg, 2)
57.518441664761035

julia> jacknifedS(reg, 15)
56.14810222161477
```
"""
function jacknifedS(setting::RegressionSetting, k::Int)::Float64
    X = designMatrix(setting)
    y = responseVector(setting)
    return jacknifedS(X, y, k)
end

function jacknifedS(X::Array{Float64,2}, y::Array{Float64,1}, k::Int)::Float64
    n, p = size(X)
    indices = [i for i in 1:n if i != k]
    Xsub = X[indices,:]
    ysub = y[indices]
    olsreg = ols(Xsub, ysub)
    e = residuals(olsreg)
    s = sqrt(sum(e.^2.0) / (n - p - 1))
    return s
end

"""

    cooks(setting)

Calculate Cook distances for all observations in a regression setting.

# Arguments
- `setting::RegressionSetting`: A regression setting object.

# Examples
```julia-repl
julia> reg = createRegressionSetting(@formula(calls ~ year), phones);

julia> cooks(reg)
24-element Array{Float64,1}:
 0.005344774190779822
 0.0017088194691033689
 0.00016624914057962608
 3.1644452583114795e-5
 0.0005395058666404081
 0.0014375008774859539
 0.0024828140956511258
 0.0036279720445167277
 0.004357605989540906
 0.005288503758364767
 0.006313578057565415
 0.0076561205696857254
 0.009568574875389256
 0.009970039008782357
 0.02610396373381051
 0.029272523880917646
 0.05091236198400663
 0.08176555044049343
 0.14380266904640235
 0.26721539425047447
 0.051205153558783356
 0.13401084683481085
 0.16860324592350226
 0.2172819114905912
```

# References
Cook, R. Dennis. "Detection of influential observation in linear regression."
Technometrics 19.1 (1977): 15-18.
"""
function cooks(setting::RegressionSetting)::Array{Float64,1}
    X = designMatrix(setting)
    y = responseVector(setting)
    return cooks(X, y)
end

function cooks(X::Array{Float64,2}, y::Array{Float64,1})::Array{Float64,1}
    n, p = size(X)
    olsreg = ols(X, y)
    res = residuals(olsreg)
    hat = hatmatrix(X)
    s2 = sum(res .* res) / (n - p)
    d = zeros(Float64, n)
    for i in 1:n
        @inbounds d[i] = ((res[i]^2.0) / (p * s2)) * (hat[i, i] / (1 - hat[i, i])^2.0)
    end
    return d
end

"""
    mahalanobisSquaredMatrix(data::DataFrame; meanvector=nothing, covmatrix=nothing)

Calculate Mahalanobis distances.

# Arguments
- `data::DataFrame`: A DataFrame object of the multivariate data.
- `meanvector::Array{Float64, 1}`: Optional mean vector of variables.
- `covmatrix::Array{Float64, 2}`: Optional covariance matrix of data.

# References
Mahalanobis, Prasanta Chandra. "On the generalized distance in statistics."
National Institute of Science of India, 1936.
"""
function mahalanobisSquaredMatrix(data::DataFrame; meanvector=nothing, covmatrix=nothing)::Union{Nothing,Array{Float64,2}}
    datamat = convert(Matrix, data)
    return mahalanobisSquaredMatrix(datamat, meanvector=meanvector, covmatrix=covmatrix)
end

function mahalanobisSquaredMatrix(datamat::Matrix; meanvector=nothing, covmatrix=nothing)::Union{Nothing,Array{Float64,2}}
    if meanvector === nothing
        meanvector = applyColumns(mean, datamat)
    end
    if covmatrix === nothing
        covmatrix = cov(datamat)
    end
    try
        invm = inv(covmatrix)
        MD2 = (datamat .- meanvector') * invm * (datamat .- meanvector')'
        return MD2
    catch e
        if det(covmatrix) == 0
            @warn "singular covariance matrix, mahalanobis distances can not be calculated"
        end
        return nothing
    end
end

"""
    dfbeta(setting, omittedIndex)

Apply DFBETA diagnostic for a given regression setting and observation index.

# Arguments
- `setting::RegressionSetting`: A regression setting object.
- `omittedIndex::Int`: Index of the omitted observation.

# Example
```julia-repl
julia> setting = createRegressionSetting(@formula(calls ~ year), phones);
julia> dfbeta(setting, 1)
2-element Array{Float64,1}:
  9.643915678524024
 -0.14686166007904422
```
"""
function dfbeta(setting::RegressionSetting, omittedIndex::Int)::Array{Float64,1}
    X = designMatrix(setting)
    y = responseVector(setting)
    return dfbeta(X, y, omittedIndex)
end

function dfbeta(X::Array{Float64,2}, y::Array{Float64,1}, omittedIndex::Int)::Array{Float64,1}
    n = length(y)
    omittedindices = filter(x -> x != omittedIndex, 1:n)
    regfull = ols(X, y)
    regomitted = ols(X[omittedindices, :], y[omittedindices])
    return coef(regfull) .- coef(regomitted)
end

"""
    covratio(setting, omittedIndex)

Apply covariance ratio diagnostic for a given regression setting and observation index.

# Arguments
- `setting::RegressionSetting`: A regression setting object.
- `omittedIndex::Int`: Index of the omitted observation.

# Example
```julia-repl
julia> setting = createRegressionSetting(@formula(calls ~ year), phones);
julia> covratio(setting, 1)
1.2945913799871505
```
"""
function covratio(setting::RegressionSetting, omittedIndex::Int)
    X = designMatrix(setting)
    y = responseVector(setting)
    return covratio(X, y, omittedIndex)
end

function covratio(X::Array{Float64,2}, y::Array{Float64,1}, omittedIndex::Int)
    n, p = size(X)
    reg = ols(X, y)
    r = residuals(reg)
    s2 = sum(r.^2.0) / Float64(n - p)
    xxinv = inv(X'X)

    indices = filter(x -> x != omittedIndex, 1:n)

    Xomitted = X[indices,:]
    yomitted = y[indices]
    xxinvomitted = inv(Xomitted' * Xomitted)
    regomitted = ols(Xomitted, yomitted)
    resomitted = residuals(regomitted)
    s2omitted = sum(resomitted.^2.0) / Float64(n - p - 1)

    covrat = det(s2omitted * xxinvomitted) / det(s2 * xxinv)

    return covrat
end

"""
    hadimeasure(setting; c = 2.0)

Apply Hadi's regression diagnostic for a given regression setting

# Arguments
- `setting::RegressionSetting`: A regression setting object.
- `c::Float64`: Critical value selected between 2.0 - 3.0. The default is 2.0.

# Example
```julia-repl
julia> setting = createRegressionSetting(@formula(calls ~ year), phones);
julia> hadimeasure(setting)
```

# References
Chatterjee, Samprit and Hadi, Ali. Regression Analysis by Example.
     5th ed. N.p.: John Wiley & Sons, 2012.
"""
function hadimeasure(setting::RegressionSetting; c::Float64=2.0)
    X = designMatrix(setting)
    y = responseVector(setting)
    hadimeasure(X, y, c=c)
end

function hadimeasure(X::Array{Float64,2}, y::Array{Float64,1}; c::Float64=2.0)
    n, p = size(X)
    reg = ols(X, y)
    res = residuals(reg)
    res2 = res.^2.0
    sumres = sum(res2)
    hat = hatmatrix(X)
    H = zeros(Float64, n)
    for i in 1:n
        @inbounds H[i] = (p * res2[i]) / ((1 - hat[i, i]) * (sumres - res2[i])) + (hat[i, i] / (1 - hat[i, i]))
    end
    crit1 = mean(H) + c * std(H)
    potentials = filter(i -> abs(H[i]) > crit1, 1:n)
    return Dict("measure" => H, "crit1" => crit1, "potentials" => potentials)
end

max_stars_repo_name: akadal/LinRegOutliers

max_issues_repo_name: akadal/LinRegOutliers

max_forks_repo_name: akadal/LinRegOutliers


text:
# Model
include_model("simple_manipulator")

# Horizon
T = 11

# Time step
tf = 1.0
h = tf / (T - 1)

# Bounds
_uu = Inf * ones(model.m)
_ul = zeros(model.m)
_ul[model.idx_u] .= -Inf
ul, uu = control_bounds(model, T, _ul, _uu)

# Initial and final states
q1 = [pi / 2.5, -2.0 * pi / 2.5, 0.5, 0.0]
qT = [pi / 2.5, -2.0 * pi / 2.5, 1.5, 0.0]

x1 = [q1; q1]

xl, xu = state_bounds(model, T, x1 = x1)

# Objective
q_ref = linear_interpolation(q1, qT, T)
x_ref = configuration_to_state(q_ref)

Qq = Diagonal([1.0, 1.0, 1.0, 1.0])
Q = cat(0.5 * Qq, 0.5 * Qq, dims = (1, 2))
QT = cat(0.5 * Qq, Diagonal([10.0, 10.0, 100.0, 1.0]), dims = (1, 2))
R = Diagonal([1.0e-3, 1.0e-3, zeros(model.m - model.nu)...])

obj_tracking = quadratic_tracking_objective(
    [t < T ? Q : QT for t = 1:T],
    [R for t = 1:T-1],
    [x_ref[t] for t = 1:T],
    [zeros(model.m) for t = 1:T])

obj_penalty = PenaltyObjective(1.0e3, model.m)
obj = MultiObjective([obj_tracking, obj_penalty])

# Constraints
include_constraints("contact")
con_contact = contact_constraints(model, T)

# Problem
prob = trajectory_optimization_problem(model,
               obj,
               T,
               h = h,
               xl = xl,
               xu = xu,
               ul = ul,
               uu = uu,
               con = con_contact)

# Trajectory initialization
x0 = deepcopy(x_ref) # linear interpolation on state
u0 = [1.0e-5 * rand(model.m) for t = 1:T-1] # random controls

# Pack trajectories into vector
z0 = pack(x0, u0, prob)

#NOTE: may need to run examples multiple times to get good trajectories
# Solve nominal problem
@time z̄ , info = solve(prob, copy(z0))

check_slack(z̄, prob)
x̄, ū = unpack(z̄, prob)

include(joinpath(pwd(), "models/visualize.jl"))
vis = Visualizer()
render(vis)
visualize!(vis, model,
    state_to_configuration([[x̄[1] for i = 1:5]...,x̄..., [x̄[end] for i = 1:5]...]),
    Δt = h)
open(vis)


text:
# Base Hamiltonian
function bHamilton(bitnum::Int)
    H = sparse(0.5*(I-σ₁))
    Iden = spdiagm([1,1])
    for i=2:bitnum
        H = H⊗Iden
    end

    for i=2:bitnum
        H_BC = Iden
        for j=2:bitnum
            if i==j
                H_BC = H_BC⊗sparse(0.5*(I-σ₁))
            else
                H_BC = H_BC⊗Iden
            end
        end
        H+=H_BC
    end
    return H
end

# problem Hamiltonian
function pHamilton{M,N}(ins::Instance{M,N},n::Integer)
    sum = spzeros(2^n,2^n);
    for clause in ins.c
        sum += spdiagm(Int[clause(i) for i=0:2^n-1])
    end
    return sum
end

export bHamilton,pHamilton

max_stars_repo_name: Roger-luo/QuCmp

max_issues_repo_name: Roger-luo/QuCmp

max_forks_repo_name: Roger-luo/QuCmp


text:
# Ensemble learning methods.
module EnsembleMethods

importall Orchestra.Types
importall Orchestra.Util

import StatsBase
import Iterators: product
import MLBase

import Orchestra.Transformers.DecisionTreeWrapper: fit!, transform!
import Orchestra.Transformers.DecisionTreeWrapper: PrunedTree
import Orchestra.Transformers.DecisionTreeWrapper: RandomForest
import Orchestra.Transformers.DecisionTreeWrapper: DecisionStumpAdaboost

export VoteEnsemble,
       StackEnsemble,
       BestLearner,
       fit!,
       transform!

# Set of machine learners that majority vote to decide prediction.
type VoteEnsemble <: Learner
  model
  options

  function VoteEnsemble(options=Dict())
    default_options = {
      # Output to train against
      # (:class).
      :output => :class,
      # Learners in voting committee.
      :learners => [PrunedTree(), DecisionStumpAdaboost(), RandomForest()]
    }
    new(nothing, nested_dict_merge(default_options, options))
  end
end

function fit!(ve::VoteEnsemble, instances::Matrix, labels::Vector)
  # Train all learners
  learners = ve.options[:learners]
  for learner in learners
    fit!(learner, instances, labels)
  end
  ve.model = { :learners => learners }
end

function transform!(ve::VoteEnsemble, instances::Matrix)
  # Make learners vote
  learners = ve.options[:learners]
  predictions = map(learner -> transform!(learner, instances), learners)
  # Return majority vote prediction
  return StatsBase.mode(predictions)
end

# Ensemble where a 'stack' learner learns on a set of learners' predictions.
type StackEnsemble <: Learner
  model
  options

  function StackEnsemble(options=Dict())
    default_options = {
      # Output to train against
      # (:class).
      :output => :class,
      # Set of learners that produce feature space for stacker.
      :learners => [PrunedTree(), DecisionStumpAdaboost(), RandomForest()],
      # Machine learner that trains on set of learners' outputs.
      :stacker => RandomForest(),
      # Proportion of training set left to train stacker itself.
      :stacker_training_proportion => 0.3,
      # Provide original features on top of learner outputs to stacker.
      :keep_original_features => false
    }
    new(nothing, nested_dict_merge(default_options, options))
  end
end

function fit!(se::StackEnsemble, instances::Matrix, labels::Vector)
  learners = se.options[:learners]
  num_learners = size(learners, 1)
  num_instances = size(instances, 1)
  num_labels = size(labels, 1)

  # Perform holdout to obtain indices for
  # partitioning learner and stacker training sets
  shuffled_indices = randperm(num_instances)
  stack_proportion = se.options[:stacker_training_proportion]
  (learner_indices, stack_indices) = holdout(num_instances, stack_proportion)

  # Partition training set for learners and stacker
  learner_instances = instances[learner_indices, :]
  stack_instances = instances[stack_indices, :]
  learner_labels = labels[learner_indices]
  stack_labels = labels[stack_indices]

  # Train all learners
  for learner in learners
    fit!(learner, learner_instances, learner_labels)
  end

  # Train stacker on learners' outputs
  label_map = MLBase.labelmap(labels)
  stacker = se.options[:stacker]
  keep_original_features = se.options[:keep_original_features]
  stacker_instances = build_stacker_instances(
    learners, stack_instances, label_map, keep_original_features
  )
  fit!(stacker, stacker_instances, stack_labels)

  # Build model
  se.model = {
    :learners => learners,
    :stacker => stacker,
    :label_map => label_map,
    :keep_original_features => keep_original_features
  }
end

function transform!(se::StackEnsemble, instances::Matrix)
  # Build stacker instances
  learners = se.model[:learners]
  stacker = se.model[:stacker]
  label_map = se.model[:label_map]
  keep_original_features = se.model[:keep_original_features]
  stacker_instances = build_stacker_instances(
    learners, instances, label_map, keep_original_features
  )

  # Predict
  return transform!(stacker, stacker_instances)
end

# Build stacker instances.
function build_stacker_instances{T<:Learner}(
  learners::Vector{T}, instances::Matrix,
  label_map, keep_original_features=false)

  # Build empty stacker instance space
  num_labels = size(label_map.vs, 1)
  num_instances = size(instances, 1)
  num_learners = size(learners, 1)
  stacker_instances = zeros(num_instances, num_learners * num_labels)

  # Fill stack instances with predictions from learners
  for l_index = 1:num_learners
    predictions = transform!(learners[l_index], instances)
    for p_index in 1:size(predictions, 1)
      pred_encoding = MLBase.labelencode(label_map, predictions[p_index])
      pred_column = (l_index-1) * num_labels + pred_encoding
      stacker_instances[p_index, pred_column] =
        one(eltype(stacker_instances))
    end
  end

  # Add original features to stacker instance space if enabled
  if keep_original_features
    stacker_instances = [instances stacker_instances]
  end

  # Return stacker instances
  return stacker_instances
end

# Selects best learner out of set.
# Will perform a grid search on learners if options grid is provided.
type BestLearner <: Learner
  model
  options

  function BestLearner(options=Dict())
    default_options = {
      # Output to train against
      # (:class).
      :output => :class,
      # Function to return partitions of instance indices.
      :partition_generator => (instances, labels) -> kfold(size(instances, 1), 5),
      # Function that selects the best learner by index.
      # Arg learner_partition_scores is a (learner, partition) score matrix.
      :selection_function => (learner_partition_scores) -> findmax(mean(learner_partition_scores, 2))[2],
      # Score type returned by score() using respective output.
      :score_type => Real,
      # Candidate learners.
      :learners => [PrunedTree(), DecisionStumpAdaboost(), RandomForest()],
      # Options grid for learners, to search through by BestLearner.
      # Format is [learner_1_options, learner_2_options, ...]
      # where learner_options is same as a learner's options but
      # with a list of values instead of scalar.
      :learner_options_grid => nothing
    }
    new(nothing, nested_dict_merge(default_options, options))
  end
end

function fit!(bls::BestLearner, instances::Matrix, labels::Vector)
  # Obtain learners as is if no options grid present
  if bls.options[:learner_options_grid] == nothing
    learners = bls.options[:learners]
  # Generate learners if options grid present
  else
    # Foreach prototype learner, generate learners with specific options
    # found in grid.
    learners = Transformer[]
    for l_index in 1:length(bls.options[:learners])
      # Obtain options grid
      options_prototype = bls.options[:learner_options_grid][l_index]
      grid_list = nested_dict_to_tuples(options_prototype)
      grid_keys = map(x -> x[1], grid_list)
      grid_values = map(x -> x[2], grid_list)

      # Foreach combination of options
      # generate learner.
      for combination in product(grid_values...)
        # Assign values for each option
        learner_options = deepcopy(options_prototype)
        for g_index in 1:length(grid_list)
          keys = grid_keys[g_index]
          value = combination[g_index]
          nested_dict_set!(learner_options, keys, value)
        end

        # Generate learner
        learner_prototype = bls.options[:learners][l_index]
        learner = create_transformer(learner_prototype, learner_options)

        # Append to candidate learners
        push!(learners, learner)
      end
    end
  end

  # Generate partitions
  partition_generator = bls.options[:partition_generator]
  partitions = partition_generator(instances, labels)

  # Train each learner on each partition and obtain validation output
  num_partitions = size(partitions, 1)
  num_learners = size(learners, 1)
  num_instances = size(instances, 1)
  score_type = bls.options[:score_type]
  learner_partition_scores = Array(score_type, num_learners, num_partitions)
  for l_index = 1:num_learners, p_index = 1:num_partitions
    partition = partitions[p_index]
    rest = setdiff(1:num_instances, partition)
    learner = learners[l_index]

    training_instances = instances[partition,:]
    training_labels = labels[partition]
    validation_instances = instances[rest, :]
    validation_labels = labels[rest]

    fit!(learner, training_instances, training_labels)
    predictions = transform!(learner, validation_instances)
    result = score(:accuracy, validation_labels, predictions)
    learner_partition_scores[l_index, p_index] = result
  end

  # Find best learner based on selection function
  best_learner_index =
    bls.options[:selection_function](learner_partition_scores)
  best_learner = learners[best_learner_index]

  # Retrain best learner on all training instances
  fit!(best_learner, instances, labels)

  # Create model
  bls.model = {
    :best_learner => best_learner,
    :best_learner_index => best_learner_index,
    :learners => learners,
    :learner_partition_scores => learner_partition_scores
  }
end

function transform!(bls::BestLearner, instances::Matrix)
  transform!(bls.model[:best_learner], instances)
end

end # module


text:
function render(list::Tuple)
    #=
    this yield...
    Not sure if this is even safe to do it here, but it turned out to make
    animations a lot smoother. We need to get a grip on these yields at some
    point, right now it's pretty unpredictable how the flow of the different
    tasks and callbacks actually translates to drawcalls
    =#
    for elem in list
        render(elem)
    end
end
"""
When rendering a specialised list of Renderables, we can do some optimizations
"""
function render{Pre}(list::Vector{RenderObject{Pre}})
    isempty(list) && return nothing
    first(list).prerenderfunction()
    vertexarray = first(list).vertexarray
    program = vertexarray.program
    glUseProgram(program.id)
    glBindVertexArray(vertexarray.id)
    for renderobject in list
        Bool(value(renderobject.uniforms[:visible])) || continue # skip invisible
        # make sure we only bind new programs and vertexarray when it is actually
        # different from the previous one
        if renderobject.vertexarray != vertexarray
            vertexarray = renderobject.vertexarray
            if vertexarray.program != program
                program = renderobject.vertexarray.program
                glUseProgram(program.id)
            end
            glBindVertexArray(vertexarray.id)
        end
        for (key,value) in program.uniformloc
            if haskey(renderobject.uniforms, key)
                gluniform(value..., renderobject.uniforms[key])
            end
        end
        renderobject.postrenderfunction()
    end
    # we need to assume, that we're done here, which is why
    # we need to bind VertexArray to 0.
    # Otherwise, every glBind(::GLBuffer) operation will be recorded into the state
    # of the currently bound vertexarray
    glBindVertexArray(0)
    return nothing
end

"""
Renders a RenderObject
Note, that this function is not optimized at all!
It uses dictionaries and doesn't care about OpenGL call optimizations.
So rewriting this function could get us a lot of performance for scenes with
a lot of objects.
"""
function render(renderobject::RenderObject, vertexarray=renderobject.vertexarray)
    # same as the yield on line 9
    if Bool(value(renderobject.uniforms[:visible]))
        renderobject.prerenderfunction()
        program = vertexarray.program
        glUseProgram(program.id)
        for (key,value) in program.uniformloc
            if haskey(renderobject.uniforms, key)
                gluniform(value..., renderobject.uniforms[key])
            end
        end
        glBindVertexArray(vertexarray.id)
        renderobject.postrenderfunction()
        glBindVertexArray(0)
    end
     return nothing
end

"""
Renders a vertexarray, which consists of the usual buffers plus a vector of
unitranges which defines the segments of the buffers to be rendered
"""
function render{T <: VecOrSignal{UnitRange{Int}}}(vao::GLVertexArray{T}, mode::GLenum=GL_TRIANGLES)
    for elem in value(vao.indexes)
        glDrawArrays(mode, max(first(elem)-1, 0), length(elem)+1)
    end
     return nothing
end

"""
Renders a vertex array which supplies an indexbuffer
"""
function render{T<:Union{Integer, Face}}(vao::GLVertexArray{GLBuffer{T}}, mode::GLenum=GL_TRIANGLES)
    glDrawElements(mode, length(vao.indices)*cardinality(vao.indices), julia2glenum(T), C_NULL)
    return nothing
end
"""
Renders a normal vertex array only containing the usual buffers buffers.
"""
function render(vao::GLVertexArray, mode::GLenum=GL_TRIANGLES)
    glDrawArrays(mode, 0, length(vao))
    return nothing
end

"""
Render instanced geometry
"""
renderinstanced(vao::GLVertexArray, a, primitive=GL_TRIANGLES) = renderinstanced(vao, length(a), primitive)

"""
Renders `amount` instances of an indexed geometry
"""
function renderinstanced{T<:Union{Integer, Face}}(vao::GLVertexArray{GLBuffer{T}}, amount::Integer, primitive=GL_TRIANGLES)
    glDrawElementsInstanced(primitive, length(vao.indices)*cardinality(vao.indices), julia2glenum(T), C_NULL, amount)
    return nothing
end
"""
Renders `amount` instances of an not indexed geoemtry geometry
"""
function renderinstanced(vao::GLVertexArray, amount::Integer, primitive=GL_TRIANGLES)
    glDrawElementsInstanced(primitive, length(vao), GL_UNSIGNED_INT, C_NULL, amount)
    return nothing
end
#handle all uniform objects

##############################################################################################
#  Generic render functions
#####
function enabletransparency()
    glEnablei(GL_BLEND, 0)
    glDisablei(GL_BLEND, 1)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
end


text:
using AbInitioSoftwareBase
using Documenter

DocMeta.setdocmeta!(AbInitioSoftwareBase, :DocTestSetup, :(using AbInitioSoftwareBase); recursive=true)

makedocs(;
    modules=[AbInitioSoftwareBase],
    authors="Qi Zhang <singularitti@outlook.com>",
    repo="https://github.com/MineralsCloud/AbInitioSoftwareBase.jl/blob/{commit}{path}#{line}",
    sitename="AbInitioSoftwareBase.jl",
    format=Documenter.HTML(;
        prettyurls=get(ENV, "CI", "false") == "true",
        canonical="https://MineralsCloud.github.io/AbInitioSoftwareBase.jl",
        assets=String[],
    ),
    pages=[
        "Home" => "index.md",
        "Manual" => ["Installation" => "install.md", "Development" => "develop.md"],
        "Troubleshooting" => "troubleshooting.md",
        "API by module" => [
            "`AbInitioSoftwareBase` module" => "api/AbInitioSoftwareBase.md",
            "`Inputs` module" => "api/Inputs.md",
            "`Commands` module" => "api/Commands.md",
        ],
    ],
)

deploydocs(;
    repo="github.com/MineralsCloud/AbInitioSoftwareBase.jl",
)


text:
#= visualize_radar_points.jl
visualize the vel_comp of every point in radar =#

module visualize

using PyCall
@pyimport matplotlib.py

export visualizeVelCmp

function visualizeVelCmp(data)
    x = data[:,1];
    y = data[:,2];
    vx_comp = data[:,3];
    vy_comp = data[:,4];

    rotated_x, rotated_y = rotateRight90deg(x, y);
    plt = scatter(
        rotated_x,
        rotated_y,
        framestyle=:origin,
        xticks=-maximum(rotated_x):5:maximum(rotated_x),
        yticks=-maximum(rotated_y):5:maximum(rotated_y),
        xlabel="Y",
        ylabel="X",
        legend=false
        );

    for id_raw in 1:length(rotated_x)
        current_vx_comp = vx_comp[id_raw];
        current_vy_comp = vy_comp[id_raw];

        current_rotated_vx_comp, current_rotated_vy_comp = rotateRight90deg(
            current_vx_comp, current_vy_comp);

        plt_vel_x = [
            rotated_x[id_raw];
            rotated_x[id_raw]+current_rotated_vx_comp];
        plt_vel_y = [
            rotated_y[id_raw];
            rotated_y[id_raw]+current_rotated_vy_comp];
        plot!(
            plt,
            plt_vel_x,
            plt_vel_y
        )
    end
    gui()
end

function rotateRight90deg(original_x, original_y)
    return_x = -original_y;
    return_y = original_x;

    return return_x, return_y;
end

end


text:
module DayN

export get_inputs, get_solution1, get_solution2

## Input getting
function get_inputs()
    test_input1 = nothing
    test_output1 = nothing
    test_input2 = nothing
    test_output2 = nothing
    data = readlines(joinpath(@__DIR__, "input.txt"))
    return (; test_input1, test_input2, test_output1, test_output2, data)
end

## Solution functions
# Part 1
function get_solution1(data)
    return nothing
end

# Part 2
function get_solution2(data)
    return nothing
end

end


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

function mark_perimetr!(r::Robot)
  num_steps = fill(0, 3)

  for (i, side) in enumerate((Sud, West, Sud))
      num_steps[i] = moves!(r, side)
  end

  side = where_border!(r, Ost, Nord)

  mark_perimetr(r, side)

  moves!(r, Sud)
  moves!(r, West)

  for (i, side) in enumerate((Nord, Ost, Nord))
      moves!(r, side, num_steps[i])
  end

end

function where_border!(r::Robot, dir_of_border::HorizonSide, dir_of_movement::HorizonSide)
  while !isborder(r, dir_of_border)
      if !isborder(r, dir_of_movement)
          move!(r, direction_of_movement)
      else
          move!(r, dir_to_border)
          dir_of_movement = inverse(dir_of_movement)
      end
  end
  return dir_of_movement
end

function mark_perimetr(r::Robot, side::HorizonSide)
  move!(r, inverse(side))

  if side == Sud
      for (i, now_side) in enumerate((Sud, Ost, Nord, West))
          move!(r, now_side)
          putmarkers!(r, Int(now_side))
          putmarker!(r)
      end
  else
      for (i, now_side) in enumerate((Nord, Ost, Sud, West))
          move!(r, now_side)
          putmarkers!(r, Int(now_side))
      end
  end
end

function putmarkers!(r::Robot, side::Int)
  while isborder(r, HorizonSide(mod(side + 1, 4)))
      putmarker!(r)
      move!(r, HorizonSide(side))
  end
end

function moves!(r::Robot, side::HorizonSide)
  count = 0
  while !isborder(r, side)
      move!(r, side)
      count += 1
  end
  return count
end

function moves!(r::Robot, side::HorizonSide, num_steps::Int)
  for i = 1:num_steps
      move!(r, side)
  end
end

max_stars_repo_name: NikitaDestroyer/Basics

max_issues_repo_name: NikitaDestroyer/Basics

max_forks_repo_name: NikitaDestroyer/Basics