text:
cfr(k) = print("Kelvin: $k, ",
  "Celsius: $(round(k-273.15,2)), ",
  "Fahrenheit: $(round(k*1.8-459.67,2)), ",
  "Rankine: $(round(k*1.8,2))")

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
using AutoHashEquals

@static if !isdefined(Base, :Nothing)
    const Nothing = Void
end

@static if VERSION >= v"0.7"
    using Test
    using Serialization
    using Markdown: plain
else
    using Base.Test
    using Base.Markdown: plain
end

function sausage(x)
    buf = IOBuffer()
    serialize(buf, x)
    seekstart(buf)
    deserialize(buf)
end

@auto_hash_equals struct A
    a::Int
    b
end
@test typeof(A(1,2)) == A
@test hash(A(1,2)) == hash(2,hash(1,hash(:A)))
@test A(1,2) == A(1,2)
@test A(1,2) == sausage(A(1,2))
@test A(1,2) != A(1,3)
@test A(1,2) != A(3,2)

abstract type B end
@auto_hash_equals struct C<:B x::Int end
@auto_hash_equals struct D<:B x::Int end
@test isa(C(1), B)
@test isa(D(1), B)
@test C(1) != D(1)
@test hash(C(1)) != hash(D(1))
@test C(1) == C(1)
@test C(1) == sausage(C(1))
@test hash(C(1)) == hash(C(1))

abstract type E{N<:Union{Nothing,Int}} end
@auto_hash_equals mutable struct F{N}<:E{N} e::N end
@auto_hash_equals mutable struct G{N}<:E{N}
    e::N
end
G() = G{Nothing}(nothing)
@test hash(F(1)) == hash(1, hash(:F))
@test hash(F(1)) != hash(F(2))
@test F(1) == F(1)
@test F(1) == sausage(F(1))
@test F(1) != F(2)
@test hash(G()) == hash(nothing, hash(:G))
@test G() == G()

macro dummy(x)
    esc(x)
end
@test @dummy(1) == 1

@auto_hash_equals mutable struct H
    @dummy h
    i
    @dummy j::Int
end
@test H(1,2,3) != H(2,1,3)
@test H(1,2,3) == H(1,2,3)
@test H(1,2,3) == sausage(H(1,2,3))
@test hash(H(1,2,3)) == hash(H(1,2,3))
@test hash(H(1,2,3)) != hash(H(2,1,3))

@auto_hash_equals struct I{A,B}
    a::A
    b::B
end
@test I{AbstractString,Int}("a", 1) == I{AbstractString,Int}("a", 1)
@test I{AbstractString,Int}("a", 1) == sausage(I{AbstractString,Int}("a", 1))

macro cond(test, block)
    if eval(test)
        block
    end
end

"""this is my data type"""
@auto_hash_equals mutable struct MyType
    field::Int
end
@test plain(@doc MyType) == "this is my data type\n"

println("ok")

ext: jl


text:
"""
# module PWscf

# Examples

```julia
julia>
```
"""
module PWscf

using AbInitioSoftwareBase.Inputs: InputEntry, Namelist, Card, Setter
using Compat: isnothing
using Setfield: @set!
using StructHelpers: @batteries
using Unitful: AbstractQuantity, Length, Temperature, ustrip, @u_str
using UnitfulAtomic

using ..Inputs: QuantumESPRESSOInput, VerbositySetter

import AbInitioSoftwareBase.Inputs: groupname, getpseudodir, getpotentials
import Crystallography: Bravais, Lattice, cellvolume
# import Pseudopotentials: pseudoformat
import ..Inputs: optionof

export optionof, groupname

include("namelists.jl")
include("cards.jl")
include("input.jl")
include("crystallography.jl")
include("set.jl")
include("validation.jl")

end


text:
using BinaryProvider # requires BinaryProvider 0.3.0 or later

# Parse some basic command-line arguments
const verbose = "--verbose" in ARGS
const prefix = Prefix(get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr")))
products = [
    LibraryProduct(prefix, ["libipopt"], :libipopt),
    ExecutableProduct(prefix, "ipopt", :amplexe),
]

# Download binaries from hosted location
bin_prefix = "https://github.com/JuliaOpt/IpoptBuilder/releases/download/v3.12.10-1-static-1"

# Listing of files generated by BinaryBuilder:
download_info = Dict(
    Linux(:aarch64, libc=:glibc, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/IpoptBuilder.v3.12.10.aarch64-linux-gnu-gcc4.tar.gz", "5e0beb008d3682ca133cf02be1ff7c8701a16a23db5e0afddf2fbcc94080a12e"),
    Linux(:aarch64, libc=:glibc, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/IpoptBuilder.v3.12.10.aarch64-linux-gnu-gcc7.tar.gz", "6cc74ad9af820bf64f86ec5307b69fc0d44c93ead25b99e1c5e5af917c2685f1"),
    Linux(:aarch64, libc=:glibc, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/IpoptBuilder.v3.12.10.aarch64-linux-gnu-gcc8.tar.gz", "ec250935455991c27b4750ede1fc272b1c1666b594af6cde9e31b1808fa70fdf"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/IpoptBuilder.v3.12.10.arm-linux-gnueabihf-gcc4.tar.gz", "dab0ace4f78df30b75cad987123ca34b067692cf9a17dcb745deeb96f15e6d70"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/IpoptBuilder.v3.12.10.arm-linux-gnueabihf-gcc7.tar.gz", "701bccb71ca24b343ce3bc0c731803d01c6df37839ba6b71859e9d884031d253"),
    Linux(:armv7l, libc=:glibc, call_abi=:eabihf, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/IpoptBuilder.v3.12.10.arm-linux-gnueabihf-gcc8.tar.gz", "2347b1cbec4c741a6a9df167bc280ba395140790b264a30becffdfc54912c214"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/IpoptBuilder.v3.12.10.i686-linux-gnu-gcc4.tar.gz", "3b265136a867977b3b90971d9abe491f847181f9a0bf34d4e2fe193f07f4ae67"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/IpoptBuilder.v3.12.10.i686-linux-gnu-gcc7.tar.gz", "d1adc40c17367897b876859a05d789e1806835f1b3665afaa769cf5a5ce1293d"),
    Linux(:i686, libc=:glibc, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/IpoptBuilder.v3.12.10.i686-linux-gnu-gcc8.tar.gz", "46857073b015fc2bc280fa0320a7e0614500e5e6669649ac34942af7c0d85d4c"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/IpoptBuilder.v3.12.10.i686-w64-mingw32-gcc4.tar.gz", "5f3b18194b66545bd9c7571920a6991d7285216cce360c7ab04cf1adbd73a7ff"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc6)) => ("$bin_prefix/IpoptBuilder.v3.12.10.i686-w64-mingw32-gcc6.tar.gz", "ebe32920c200002699ae0d3efb3122b9837a8c70f907516142cd54d711ac6c22"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/IpoptBuilder.v3.12.10.i686-w64-mingw32-gcc7.tar.gz", "322e5100b9ed4722aea3d6c31803234babce7c9cc28ce49465cd65772c93d994"),
    Windows(:i686, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/IpoptBuilder.v3.12.10.i686-w64-mingw32-gcc8.tar.gz", "d9d6327f2e4b97d436fceaa32d0314f4b8824bd971f39fec01a87e9edd83180c"),
    MacOS(:x86_64, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-apple-darwin14-gcc4.tar.gz", "1b3420794b883cc971d732afb0bac2608cf2a7e6bd80b5861b25cdc87f820a73"),
    MacOS(:x86_64, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-apple-darwin14-gcc7.tar.gz", "ab8439358117294fa7c17e55af86893902041f99b25e1052cdb15b54d03f7fe7"),
    MacOS(:x86_64, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-apple-darwin14-gcc8.tar.gz", "f76fef6a24fa44be02a7e70ee5dd49bfc06f84ee58e548ac07e1686e5198a0eb"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-linux-gnu-gcc4.tar.gz", "5d67c14f31a224d756ee8d27a440541f86a0ecb1596af3f913b7748e1b213ccb"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-linux-gnu-gcc7.tar.gz", "e6e64b0c15445bc1f910117c4aacad91d7e3f3dd70a66da5e607dd0960afde18"),
    Linux(:x86_64, libc=:glibc, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-linux-gnu-gcc8.tar.gz", "0968689197d4a4d85032ac0c8ce7cbfb7ebd6a1eddc740aaa0ee2dae806c0e96"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc4)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-w64-mingw32-gcc4.tar.gz", "6d9bf56ee88647a203f178cb08333a2e7824a88099d4e376c6357c0eb52e9dd8"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc6)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-w64-mingw32-gcc6.tar.gz", "72e5465e5b54ea4fac4234e1c680a96bc1c04723de4fbeb484323105ef088dcc"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc7)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-w64-mingw32-gcc7.tar.gz", "ab365e3c6ba5fc28cbc9a02720a6e40ff4b88f947895e5339a49eb7d81840fc5"),
    Windows(:x86_64, compiler_abi=CompilerABI(:gcc8)) => ("$bin_prefix/IpoptBuilder.v3.12.10.x86_64-w64-mingw32-gcc8.tar.gz", "e76f5862acb5d60435f13a1a66b7f189a8116a7f1b857fa01756629d7fb08f2a"),
)

# To fix gcc4 bug in Windows
# https://sourceforge.net/p/mingw-w64/bugs/727/
this_platform = platform_key_abi()
if typeof(this_platform)==Windows && this_platform.compiler_abi.gcc_version == :gcc4
   this_platform = Windows(arch(this_platform), libc=libc(this_platform), compiler_abi=CompilerABI(:gcc6))
end

function update_product(product::LibraryProduct, library_path, binary_path)
    LibraryProduct(library_path, product.libnames, product.variable_name)
end

function update_product(product::ExecutableProduct, library_path, binary_path)
    ExecutableProduct(joinpath(binary_path, basename(product.path)), product.variable_name)
end

custom_library = false
if haskey(ENV,"JULIA_IPOPT_LIBRARY_PATH") && haskey(ENV,"JULIA_IPOPT_EXECUTABLE_PATH")
    custom_products = [update_product(product, ENV["JULIA_IPOPT_LIBRARY_PATH"], ENV["JULIA_IPOPT_EXECUTABLE_PATH"]) for product in products]
    if all(satisfied(p; verbose=verbose) for p in custom_products)
        products = custom_products
        custom_library = true
    else
        error("Could not install custom libraries from $(ENV["JULIA_IPOPT_LIBRARY_PATH"]) and $(ENV["JULIA_IPOPT_EXECUTABLE_PATH"]).\nTo fall back to BinaryProvider call delete!(ENV,\"JULIA_IPOPT_LIBRARY_PATH\");delete!(ENV,\"JULIA_IPOPT_EXECUTABLE_PATH\") and run build again.")
    end
end

if !custom_library

    # Install unsatisfied or updated dependencies:
    unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)

    dl_info = choose_download(download_info, platform_key_abi())
    if dl_info === nothing && unsatisfied
        # If we don't have a compatible .tar.gz to download, complain.
        # Alternatively, you could attempt to install from a separate provider,
        # build from source or something even more ambitious here.
        error("Your platform (\"$(Sys.MACHINE)\", parsed as \"$(triplet(platform_key_abi()))\") is not supported by this package!")
    end

    # If we have a download, and we are unsatisfied (or the version we're
    # trying to install is not itself installed) then load it up!
    if unsatisfied || !isinstalled(dl_info...; prefix=prefix)
        # Download and install binaries
        install(dl_info...; prefix=prefix, force=true, verbose=verbose)
    end
end

# Write out a deps.jl file that will contain mappings for our products
write_deps_file(joinpath(@__DIR__, "deps.jl"), products, verbose=verbose)


text:
### A Pluto.jl notebook ###
# v0.11.2

using Markdown
using InteractiveUtils

# ╔═╡ 6630175e-d5aa-11ea-3171-a1dd3b56dfda
using DrWatson

# ╔═╡ aa1da76a-d5aa-11ea-366d-efb23acf302a
begin
	@quickactivate "GCN_HM_GRN-Integration"
	using Plots, DataFrames
end

# ╔═╡ 1a72945c-d5ac-11ea-0c5d-53dd35d75bc3
begin
	theme(:default)
	gr(fmt = :png, dpi = 200)
end

# ╔═╡ b21e917c-d5aa-11ea-35e0-ff99bdaa8e2e
df = collect_results(datadir("inner_batch"));

# ╔═╡ dc048b40-d5aa-11ea-1ec2-75f9ac6c2592
begin
	plts = []
	for i ∈ 1:size(df, 1)
		plt = plot([df[i, :losses_train], df[i, :losses_test], df[i, :auc_list]], label=["train" "test" "auc"], legend = :outerright)
		title!(plt, "$(df[i, :layer]), $(df[i, :batch]), $(DrWatson.roundval(df[i, :auc]; digits = 4, scientific = 4))")
		pushfirst!(plts, plt)
	end
	p = plot(plts..., layout = (Int32(length(plts) / 2),2))
end

# ╔═╡ Cell order:
# ╠═6630175e-d5aa-11ea-3171-a1dd3b56dfda
# ╠═aa1da76a-d5aa-11ea-366d-efb23acf302a
# ╠═1a72945c-d5ac-11ea-0c5d-53dd35d75bc3
# ╠═b21e917c-d5aa-11ea-35e0-ff99bdaa8e2e
# ╠═dc048b40-d5aa-11ea-1ec2-75f9ac6c2592


text:
"""
    DatasetRow{<:AbstractDataset, <:AbstractIndex}

A view of one row of an `AbstractDataset`.

A `DatasetRow` is returned by `getindex` or `view` functions when one row and a
selection of columns are requested, or when iterating the result
of the call to the [`eachrow`](@ref) function.

The `DatasetRow` constructor can also be called directly:

```
DatasetRow(parent::AbstractDataset, row::Integer, cols=:)
```

A `DatasetRow` supports the iteration interface and can therefore be passed to
functions that expect a collection as an argument. Its element type is always `Any`.

Indexing is one-dimensional like specifying a column of a `Dataset`.
You can also access the data in a `DatasetRow` using the `getproperty` and
`setproperty!` functions and convert it to a `Tuple`, `NamedTuple`, or `Vector`
using the corresponding functions.

If the selection of columns in a parent data frame is passed as `:` (a colon)
then `DatasetRow` will always have all columns from the parent,
even if they are added or removed after its creation.

# Examples
```jldoctest
julia> ds = Dataset(a = repeat([1, 2], outer=[2]),
                      b = repeat(["a", "b"], inner=[2]),
                      c = 1:4)
4×3 Dataset
 Row │ a      b       c
     │ Int64  String  Int64
─────┼──────────────────────
   1 │     1  a           1
   2 │     2  a           2
   3 │     1  b           3
   4 │     2  b           4

julia> ds[1, :]
DatasetRow
 Row │ a      b       c
     │ Int64  String  Int64
─────┼──────────────────────
   1 │     1  a           1

julia> @view ds[end, [:a]]
DatasetRow
 Row │ a
     │ Int64
─────┼───────
   4 │     2

julia> eachrow(ds)[1]
DatasetRow
 Row │ a      b       c
     │ Int64  String  Int64
─────┼──────────────────────
   1 │     1  a           1

julia> Tuple(ds[1, :])
(1, "a", 1)

julia> NamedTuple(ds[1, :])
(a = 1, b = "a", c = 1)

julia> Vector(ds[1, :])
3-element Vector{Any}:
 1
  "a"
 1
```
"""
struct DatasetRow{D<:AbstractDataset, S<:AbstractIndex}
    # although we allow D to be AbstractDataset to support extensions
    # in Dataset.jl it will always be a Dataset unless an inner constructor
    # is used. In this way we have a fast access to the data frame that
    # actually stores the data that DatasetRow refers to
    ds::D
    colindex::S
    dsrow::Int # row number in ds
    rownumber::Int # row number in the direct source AbstractDataset from which DatasetRow was created

    @inline DatasetRow(ds::D, colindex::S, row::Union{Signed, Unsigned},
                         rownumber::Union{Signed, Unsigned}) where
        {D<:AbstractDataset, S<:AbstractIndex} = new{D, S}(ds, colindex, row, rownumber)
end

_getnames(x::DatasetRow) = _names(x)

Base.@propagate_inbounds function DatasetRow(ds::Dataset, row::Integer, cols)
    @boundscheck if !checkindex(Bool, axes(ds, 1), row)
        throw(BoundsError(ds, (row, cols)))
    end
    DatasetRow(ds, SubIndex(index(ds), cols), row, row)
end

Base.@propagate_inbounds DatasetRow(ds::Dataset, row::Bool, cols) =
    throw(ArgumentError("invalid row index of type Bool"))

Base.@propagate_inbounds function DatasetRow(sds::SubDataset, row::Integer, cols)
    @boundscheck if !checkindex(Bool, axes(sds, 1), row)
        throw(BoundsError(sds, (row, cols)))
    end
    if index(sds) isa Index # sds was created using : as row selector
        colindex = SubIndex(index(sds), cols)
    else
        colindex = SubIndex(index(parent(sds)), parentcols(index(sds), cols))
    end
    @inbounds DatasetRow(parent(sds), colindex, rows(sds)[row], row)
end

Base.@propagate_inbounds DatasetRow(ds::SubDataset, row::Bool, cols) =
    throw(ArgumentError("invalid row index of type Bool"))

Base.@propagate_inbounds DatasetRow(ds::AbstractDataset, row::Integer) =
    DatasetRow(ds, row, :)

row(r::DatasetRow) = getfield(r, :dsrow)

"""
    rownumber(dsr::DatasetRow)

Return a row number in the `AbstractDataset` that `dsr` was created from.

Note that this differs from the first element in the tuple returned by
`parentindices`. The latter gives the row number in the `parent(dsr)`, which is
the source `Dataset` where data that `dsr` gives access to is stored.

# Examples
```jldoctest
julia> ds = Dataset(reshape(1:12, 3, 4), :auto)
3×4 Dataset
 Row │ x1     x2     x3     x4
     │ Int64  Int64  Int64  Int64
─────┼────────────────────────────
   1 │     1      4      7     10
   2 │     2      5      8     11
   3 │     3      6      9     12

julia> dsr = ds[2, :]
DatasetRow
 Row │ x1     x2     x3     x4
     │ Int64  Int64  Int64  Int64
─────┼────────────────────────────
   2 │     2      5      8     11

julia> rownumber(dsr)
2

julia> parentindices(dsr)
(2, Base.OneTo(4))

julia> parent(dsr)
3×4 Dataset
 Row │ x1     x2     x3     x4
     │ Int64  Int64  Int64  Int64
─────┼────────────────────────────
   1 │     1      4      7     10
   2 │     2      5      8     11
   3 │     3      6      9     12

julia> dsv = @view ds[2:3, 1:3]
2×3 SubDataset
 Row │ x1     x2     x3
     │ Int64  Int64  Int64
─────┼─────────────────────
   1 │     2      5      8
   2 │     3      6      9

julia> dsrv = dsv[2, :]
DatasetRow
 Row │ x1     x2     x3
     │ Int64  Int64  Int64
─────┼─────────────────────
   3 │     3      6      9

julia> rownumber(dsrv)
2

julia> parentindices(dsrv)
(3, 1:3)

julia> parent(dsrv)
3×4 Dataset
 Row │ x1     x2     x3     x4
     │ Int64  Int64  Int64  Int64
─────┼────────────────────────────
   1 │     1      4      7     10
   2 │     2      5      8     11
   3 │     3      6      9     12
```
"""
rownumber(r::DatasetRow) = getfield(r, :rownumber)

Base.parent(r::DatasetRow) = getfield(r, :ds)
Base.parentindices(r::DatasetRow) = (row(r), parentcols(index(r)))

Base.summary(dsr::DatasetRow) = # -> String
    @sprintf("%d-element %s", length(dsr), nameof(typeof(dsr)))
Base.summary(io::IO, dsr::DatasetRow) = print(io, summary(dsr))

Base.@propagate_inbounds Base.view(ads::AbstractDataset, rowind::Integer,
                                   colinds::MultiColumnIndex) =
    DatasetRow(ads, rowind, colinds)

Base.@propagate_inbounds Base.getindex(ds::AbstractDataset, rowind::Integer,
                                       colinds::MultiColumnIndex) =
    DatasetRow(ds, rowind, colinds)
Base.@propagate_inbounds Base.getindex(ds::AbstractDataset, rowind::Integer, ::Colon) =
    DatasetRow(ds, rowind, :)
Base.@propagate_inbounds Base.getindex(r::DatasetRow, idx::ColumnIndex) =
    parent(r)[row(r), parentcols(index(r), idx)]

Base.@propagate_inbounds function Base.getindex(r::DatasetRow, idxs::MultiColumnIndex)
    # we create a temporary DatasetRow object to compute the SubIndex
    # in the parent(r), but this object has an incorrect rownumber
    # so we later copy rownumber from r
    # the Julia compiler should be able to optimize out this indirection
    # and in this way we avoid duplicating the code that computes the correct SubIndex
    dsr_tmp = DatasetRow(parent(r), row(r), parentcols(index(r), idxs))
    return DatasetRow(parent(dsr_tmp), index(dsr_tmp), row(r), rownumber(r))
end

Base.@propagate_inbounds Base.getindex(r::DatasetRow, ::Colon) = r

# Modify Dataset
for T in MULTICOLUMNINDEX_TUPLE
    @eval function Base.setindex!(ds::Dataset,
                                  v::Union{DatasetRow, NamedTuple, AbstractDict},
                                  row_ind::Integer,
                                  col_inds::$(T))
        idxs = index(ds)[col_inds]
        if length(v) != length(idxs)
            throw(DimensionMismatch("$(length(idxs)) columns were selected but the assigned " *
                                    "collection contains $(length(v)) elements"))
        end

        if v isa AbstractDict
            if keytype(v) !== Symbol &&
                (keytype(v) <: AbstractString || all(x -> x isa AbstractString, keys(v)))
                v = (;(Symbol.(keys(v)) .=> values(v))...)
            end
            for n in view(_names(ds), idxs)
                if !haskey(v, n)
                    throw(ArgumentError("Column :$n not found in source dictionary"))
                end
            end
        elseif !all(((a, b),) -> a == b, zip(view(_names(ds), idxs), keys(v)))
            mismatched = findall(view(_names(ds), idxs) .!= collect(keys(v)))
            throw(ArgumentError("Selected column names do not match the names in assigned " *
                                "value in positions $(join(mismatched, ", ", " and "))"))
        end
        flag = false
        for (col, val) in pairs(v)
            ds[row_ind, col] = val
            if !flag && index(ds)[col] ∈ index(ds).sortedcols
                _reset_grouping_info!(ds)
                flag = true
            end
        end
        _modified(_attributes(ds))
        return ds
    end
end

Base.@propagate_inbounds Base.setindex!(r::DatasetRow, value, idx) =
    setindex!(parent(r), value, row(r), parentcols(index(r), idx))

index(r::DatasetRow) = getfield(r, :colindex)

Base.names(r::DatasetRow, cols::Colon=:) = names(index(r))

function Base.names(r::DatasetRow, cols)
    nms = _names(index(r))
    idx = index(r)[cols]
    idxs = idx isa Int ? (idx:idx) : idx
    return [string(nms[i]) for i in idxs]
end

Base.names(r::DatasetRow, T::Type) =
    [String(n) for n in _names(r) if eltype(parent(r)[!, n]) <: T]
Base.names(r::DatasetRow, fun::Function) = filter!(fun, names(r))

_names(r::DatasetRow) = view(_names(parent(r)), parentcols(index(r), :))

Base.haskey(r::DatasetRow, key::Bool) =
    throw(ArgumentError("invalid key: $key of type Bool"))
Base.haskey(r::DatasetRow, key::Integer) = 1 ≤ key ≤ size(r, 1)

function Base.haskey(r::DatasetRow, key::Symbol)
    hasproperty(parent(r), key) || return false
    index(r) isa Index && return true
    # here index(r) is a SubIndex
    pos = index(parent(r))[key]
    remap = index(r).remap
    length(remap) == 0 && lazyremap!(index(r))
    checkbounds(Bool, remap, pos) || return false
    return remap[pos] > 0
end

Base.haskey(r::DatasetRow, key::AbstractString) = haskey(r, Symbol(key))

# separate methods are needed due to dispatch ambiguity
Base.getproperty(r::DatasetRow, idx::Symbol) = r[idx]
Base.getproperty(r::DatasetRow, idx::AbstractString) = r[idx]
Base.setproperty!(r::DatasetRow, idx::Symbol, x::Any) = (r[idx] = x)
Base.setproperty!(r::DatasetRow, idx::AbstractString, x::Any) = (r[idx] = x)
Compat.hasproperty(r::DatasetRow, s::Symbol) = haskey(index(r), s)
Compat.hasproperty(r::DatasetRow, s::AbstractString) = haskey(index(r), s)

# Private fields are never exposed since they can conflict with column names
Base.propertynames(r::DatasetRow, private::Bool=false) = copy(_names(r))

Base.view(r::DatasetRow, col::ColumnIndex) =
    view(parent(r)[!, parentcols(index(r), col)], row(r))

function Base.view(r::DatasetRow, cols::MultiColumnIndex)
    # we create a temporary DatasetRow object to compute the SubIndex
    # in the parent(r), but this object has an incorrect rownumber
    # so we later copy rownumber from r
    # the Julia compiler should be able to optimize out this indirection
    # and in this way we avoid duplicating the code that computes the correct SubIndex
    dsr_tmp = DatasetRow(parent(r), row(r), parentcols(index(r), cols))
    return DatasetRow(parent(dsr_tmp), index(dsr_tmp), row(r), rownumber(r))
end

Base.view(r::DatasetRow, ::Colon) = r

"""
    size(dsr::DatasetRow[, dim])

Return a 1-tuple containing the number of elements of `dsr`.
If an optional dimension `dim` is specified, it must be `1`, and the number of
elements is returned directly as a number.

See also: [`length`](@ref)

# Examples
```jldoctest
julia> dsr = Dataset(a=1:3, b='a':'c')[1, :]
DatasetRow
 Row │ a      b
     │ Int64  Char
─────┼─────────────
   1 │     1  a

julia> size(dsr)
(2,)

julia> size(dsr, 1)
2
```
"""
Base.size(r::DatasetRow) = (length(index(r)),)
Base.size(r::DatasetRow, i) = size(r)[i]

"""
    length(dsr::DatasetRow)

Return the number of elements of `dsr`.

See also: [`size`](@ref)

# Examples
```jldoctest
julia> dsr = Dataset(a=1:3, b='a':'c')[1, :]
DatasetRow
 Row │ a      b
     │ Int64  Char
─────┼─────────────
   1 │     1  a

julia> length(dsr)
2
```
"""
Base.length(r::DatasetRow) = size(r, 1)

"""
    ndims(::DatasetRow)
    ndims(::Type{<:DatasetRow})

Return the number of dimensions of a data frame row, which is always `1`.
"""
Base.ndims(::DatasetRow) = 1
Base.ndims(::Type{<:DatasetRow}) = 1

Base.firstindex(r::DatasetRow) = 1
Base.lastindex(r::DatasetRow) = length(r)

if VERSION < v"1.6"
    Base.firstindex(r::DatasetRow, i::Integer) = first(axes(r, i))
    Base.lastindex(r::DatasetRow, i::Integer) = last(axes(r, i))
end
Base.axes(r::DatasetRow, i::Integer) = Base.OneTo(size(r, i))

Base.iterate(r::DatasetRow) = iterate(r, 1)

function Base.iterate(r::DatasetRow, st)
    st > length(r) && return nothing
    return (r[st], st + 1)
end

# Computing the element type requires going over all columns,
# so better let collect() do it only if necessary (widening)
Base.IteratorEltype(::Type{<:DatasetRow}) = Base.EltypeUnknown()

function Base.Vector(dsr::DatasetRow)
    ds = parent(dsr)
    T = reduce(promote_type, (eltype(ds[!, i]) for i in parentcols(index(dsr))))
    return Vector{T}(dsr)
end
Base.Vector{T}(dsr::DatasetRow) where T =
    T[dsr[i] for i in 1:length(dsr)]

Base.Array(dsr::DatasetRow) = Vector(dsr)
Base.Array{T}(dsr::DatasetRow) where {T} = Vector{T}(dsr)

Base.keys(r::DatasetRow) = propertynames(r)
Base.values(r::DatasetRow) =
    ntuple(col -> parent(r)[row(r), parentcols(index(r), col)], length(r))
Base.map(f, r::DatasetRow, rs::DatasetRow...) = map(f, copy(r), copy.(rs)...)
Base.get(dsr::DatasetRow, key::ColumnIndex, default) =
    haskey(dsr, key) ? dsr[key] : default
Base.get(f::Base.Callable, dsr::DatasetRow, key::ColumnIndex) =
    haskey(dsr, key) ? dsr[key] : f()
Base.broadcastable(::DatasetRow) =
    throw(ArgumentError("broadcasting over `DatasetRow`s is reserved"))

function Base.NamedTuple(dsr::DatasetRow)
    k = Tuple(_names(dsr))
    v = ntuple(i -> dsr[i], length(dsr))
    pc = parentcols(index(dsr))
    cols = _columns(parent(dsr))
    s = ntuple(i -> eltype(cols[pc[i]]), length(dsr))
    NamedTuple{k, Tuple{s...}}(v)
end

"""
    copy(dsr::DatasetRow)

Construct a `NamedTuple` with the same contents as the [`DatasetRow`](@ref).
This method returns a `NamedTuple` so that the returned object
is not affected by changes to the parent data frame of which `dsr` is a view.

"""
Base.copy(dsr::DatasetRow) = NamedTuple(dsr)

Base.convert(::Type{NamedTuple}, dsr::DatasetRow) = NamedTuple(dsr)

Base.merge(a::DatasetRow) = NamedTuple(a)
Base.merge(a::DatasetRow, b::NamedTuple) = merge(NamedTuple(a), b)
Base.merge(a::NamedTuple, b::DatasetRow) = merge(a, NamedTuple(b))
Base.merge(a::DatasetRow, b::DatasetRow) = merge(NamedTuple(a), NamedTuple(b))
Base.merge(a::DatasetRow, b::Base.Iterators.Pairs) = merge(NamedTuple(a), b)
Base.merge(a::DatasetRow, itr) = merge(NamedTuple(a), itr)

Base.hash(r::DatasetRow, h::UInt) = _nt_like_hash(r, h)

for eqfun in (:isequal, :(==)),
    (leftarg, rightarg) in ((:DatasetRow, :DatasetRow),
                            (:DatasetRow, :NamedTuple),
                            (:NamedTuple, :DatasetRow))
    @eval function Base.$eqfun(r1::$leftarg, r2::$rightarg)
        _equal_names(r1, r2) || return false
        return all(((a, b),) -> $eqfun(a, b), zip(r1, r2))
    end
end

for (eqfun, cmpfun) in ((:isequal, :isless), (:(==), :(<))),
    (leftarg, rightarg) in ((:DatasetRow, :DatasetRow),
                            (:DatasetRow, :NamedTuple),
                            (:NamedTuple, :DatasetRow))
    @eval function Base.$cmpfun(r1::$leftarg, r2::$rightarg)
        if !_equal_names(r1, r2)
            length(r1) == length(r2) ||
                throw(ArgumentError("compared objects must have the same number " *
                                    "of columns (got $(length(r1)) and $(length(r2)))"))
            mismatch = findfirst(i -> _getnames(r1)[i] != _getnames(r2)[i], 1:length(r1))
            throw(ArgumentError("compared objects must have the same property " *
                                "names but they differ in column number $mismatch " *
                                "where the names are :$(_getnames(r1)[mismatch]) and " *
                                ":$(_getnames(r2)[mismatch]) respectively"))
        end
        for (a, b) in zip(r1, r2)
            eq = $eqfun(a, b)
            if ismissing(eq)
                return missing
            elseif !eq
                return $cmpfun(a, b)
            end
        end
        return false # here we know that r1 and r2 have equal lengths and all values were equal
    end
end

function Dataset(dsr::DatasetRow)
    row, cols = parentindices(dsr)
    parent(dsr)[row:row, cols]
end

Base.push!(ds::Dataset, dsr::DatasetRow, cols::Symbol=:setequal, promote::Bool=(cols in [:union, :subset])) =
    push!(ds, NamedTuple(dsr); cols = cols, promote = promote)
Base.pushfirst!(ds::Dataset, dsr::DatasetRow, cols::Symbol=:setequal, promote::Bool=(cols in [:union, :subset])) =
    pushfirst!(ds, NamedTuple(dsr); cols = cols, promote = promote)
# @noinline pushhelper!(x, r) = push!(x, x[r])
#
# function Base.push!(ds::Dataset, dsr::DatasetRow; cols::Symbol=:setequal,
#                     promote::Bool=(cols in [:union, :subset]))
#     possible_cols = (:orderequal, :setequal, :intersect, :subset, :union)
#     if !(cols in possible_cols)
#         throw(ArgumentError("`cols` keyword argument must be any of :" *
#                             join(possible_cols, ", :")))
#     end
#
#     nrows, ncols = size(ds)
#     targetrows = nrows + 1
#
#     if parent(dsr) === ds && index(dsr) isa Index
#         # in this case we are sure that all we do is safe
#         r = row(dsr)
#         for col in _columns(ds)
#             # use a barrier function to improve performance
#             pushhelper!(col, r)
#         end
#         for (colname, col) in zip(_names(ds), _columns(ds))
#             if length(col) != targetrows
#                 for col2 in _columns(ds)
#                     resize!(col2, nrows)
#                 end
#                 throw(AssertionError("Error adding value to column :$colname"))
#             end
#         end
#         return ds
#     end
#
#     if ncols == 0
#         for (n, v) in pairs(dsr)
#             setproperty!(ds, n, fill!(Tables.allocatecolumn(typeof(v), 1), v))
#         end
#         return ds
#     end
#
#     if cols == :union
#         for (i, colname) in enumerate(_names(ds))
#             col = _columns(ds)[i]
#             if hasproperty(dsr, colname)
#                 val = dsr[colname]
#             else
#                 val = missing
#             end
#             S = typeof(val)
#             T = eltype(col)
#             if S <: T || promote_type(S, T) <: T
#                 push!(col, val)
#             elseif !promote
#                 try
#                     push!(col, val)
#                 catch err
#                     for col in _columns(ds)
#                         resize!(col, nrows)
#                     end
#                     @error "Error adding value to column :$colname."
#                     rethrow(err)
#                 end
#             else
#                 newcol = Tables.allocatecolumn(promote_type(S, T), targetrows)
#                 copyto!(newcol, 1, col, 1, nrows)
#                 newcol[end] = val
#                 firstindex(newcol) != 1 && _onebased_check_error()
#                 _columns(ds)[i] = newcol
#             end
#         end
#         for (colname, col) in zip(_names(ds), _columns(ds))
#             if length(col) != targetrows
#                 for col2 in _columns(ds)
#                     resize!(col2, nrows)
#                 end
#                 throw(AssertionError("Error adding value to column :$colname"))
#             end
#         end
#         for colname in setdiff(_names(dsr), _names(ds))
#             val = dsr[colname]
#             S = typeof(val)
#             if nrows == 0
#                 newcol = [val]
#             else
#                 newcol = Tables.allocatecolumn(Union{Missing, S}, targetrows)
#                 fill!(newcol, missing)
#                 newcol[end] = val
#             end
#             ds[!, colname] = newcol
#         end
#         return ds
#     end
#
#     current_col = 0
#     try
#         if cols === :orderequal
#             if _names(ds) != _names(dsr)
#                 msg = "when `cols == :orderequal` pushed row must have the same " *
#                       "column names and in the same order as the target data frame"
#                 throw(ArgumentError(msg))
#             end
#         elseif cols === :setequal
#             msg = "Number of columns of `DatasetRow` does not match that of " *
#                   "target data frame (got $(length(dsr)) and $ncols)."
#             ncols == length(dsr) || throw(ArgumentError(msg))
#         end
#         for (col, nm) in zip(_columns(ds), _names(ds))
#             current_col += 1
#             if cols === :subset
#                 val = get(dsr, nm, missing)
#             else
#                 val = dsr[nm]
#             end
#             S = typeof(val)
#             T = eltype(col)
#             if S <: T || !promote || promote_type(S, T) <: T
#                 push!(col, val)
#             else
#                 newcol = similar(col, promote_type(S, T), targetrows)
#                 copyto!(newcol, 1, col, 1, nrows)
#                 newcol[end] = val
#                 firstindex(newcol) != 1 && _onebased_check_error()
#                 _columns(ds)[columnindex(ds, nm)] = newcol
#             end
#         end
#         for col in _columns(ds)
#             @assert length(col) == targetrows
#         end
#     catch err
#         for col in _columns(ds)
#             resize!(col, nrows)
#         end
#         if current_col > 0
#             @error "Error adding value to column :$(_names(ds)[current_col])."
#         end
#         rethrow(err)
#     end
#     return ds
# end


text:
## Test util methods ##
module Test_utils

using MultipleTesting
using Base.Test

## isin ##
println(" ** ", "isin")
@test isin(0.1)
@test isin(0.1, 0.0, 1.0)
@test !isin(2.1)
@test !isin(2.1, 0.0, 1.0)
@test isin(rand(10))
@test isin(rand(10), 0.0, 1.0)
@test !isin(-rand(10))
@test !isin(-rand(10), 0.0, 1.0)
@test !isin([rand(10); -0.1])
@test !isin([rand(10); -0.1], 0.0, 1.0)

## validPValues ##
println(" ** ", "validPValues")
@test_throws DomainError MultipleTesting.validPValues([-1.])
@test_throws DomainError MultipleTesting.validPValues([2.])

## reorder ##
println(" ** ", "reorder")
x = [1, 5, 4, 2, 4, 3]
no, oo = MultipleTesting.reorder(x)
@test x[no] == sort(x)
@test x[no][oo] == x

## Grenander ##
println(" ** ", "grenander")
pv = [0.2, 0.4, 0.6, 0.8, 1.0]
p, f, F = MultipleTesting.grenander(pv)
@test_approx_eq p pv
@test_approx_eq f ones(p)
@test_approx_eq F pv

pv = [0.1, 0.3, 0.5, 0.7, 0.9]
p, f, F = MultipleTesting.grenander(pv)
@test_approx_eq p pv
@test_approx_eq f ones(p)
@test_approx_eq F pv+0.1

pv = [0.1, 0.1, 0.3, 0.3, 0.5, 0.5, 0.7, 0.7, 0.9, 0.9]
p, f, F = MultipleTesting.grenander(pv)
@test_approx_eq p unique(pv)
@test_approx_eq f ones(p)
@test_approx_eq F p+0.1

pv = [0.1, 0.2, 0.5, 0.6, 0.9]
p, f, F = MultipleTesting.grenander(pv)
@test_approx_eq p pv
@test_approx_eq f [2.0, 1.0, 1.0, 2/3, 2/3]
@test_approx_eq F [0.2, 0.4, 0.7, 0.8, 1.0]

## isotonic regression ##
println(" ** ", "isotonic_regression")
r = rand(10)
@test MultipleTesting.isotonic_regression(r, ones(r)) == MultipleTesting.isotonic_regression(r)
@test_throws DimensionMismatch MultipleTesting.isotonic_regression(rand(10), ones(5))

end


text:
using open62541
using Test

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


text:
# Generally, these methods may not have optimal speed for all Tabular types, but they should
# be robust fallbacks, with reference implementation behavior that other types can
# specialize for speed

function (==)(s1::AbstractSeries, s2::AbstractSeries)
    inds1 = indices(s1)[1]
    inds2 = indices(s2)[1]
    if inds1 === inds2
        return true
    end

    i1 = collect(inds1)
    i2 = collect(inds2)

    # They are not equal if they have different indices
    if i1 != i2
        if length(i1) == length(i2)
            for i ∈ i1
                if i ∉ i2
                    return false
                end
            end
        else
            return false
        end
    end

    # They are not equal if they have different values
    for i ∈ i1
        @inbounds if s1[i] != s2[i]
            return false
        end
    end

    return true
end

function (==)(t1::AbstractTable, t2::AbstractTable)
    inds_1 = indices(t1)
    inds_2 = indices(t2)

    row_inds_1 = collect(inds_1[1])
    row_inds_2 = collect(inds_2[1])

    # They are not equal if they have different row indices
    if row_inds_1 != row_inds_2
        if length(row_inds_1) != length(row_inds_2)
            for r ∈ row_inds_1
                if r ∉ row_inds_2
                    return false
                end
            end
        end
    end

    col_inds_1 = collect(inds_1[2])
    col_inds_2 = collect(inds_2[2])

    # They are not equal if they have different column indices
    if col_inds_1 != col_inds_2
        if length(col_inds_1) != length(col_inds_2)
            for c ∈ col_inds_1
                if c ∉ col_inds_2
                    return false
                end
            end
        end
    end

    # They are not equal if they have different values
    for r ∈ row_inds_1
        for c ∈ col_inds_1
            @inbounds if t1[r, c] != t2[r, c]
                return false
            end
        end
    end

    return true
end


text:
import Base: getindex, setindex!, get, view, selectdim, dotview

# Here we will define indexing (getindex,setindex!,firstindex,lastindex)
# interface for generic Value types.

# Julia handles access to AbstractArray, AbstractDict, and Tuple
# subtypes using getindex:

# getindex(obj, key...) => value
# setindex!(obj, val, key...) => obj

function setindex!(x::Value,v,I...)
    if !isempty(_tapes)
        error("Array overwriting during gradient calculation not supported.")
    else
        setindex!(value(x),v,I...)
    end
end

# x[...] .= f.(y...) ---> broadcast!(f, dotview(x, ...), y...)

function dotview(x::Value, I...)
    if !isempty(_tapes)
        error("Array overwriting during gradient calculation not supported.")
    else
        view(x, I...)
    end
end

# We handle the containers by overloading getindex:

@primitive  getindex(x,i...),dxi,xi  ungetindex(x,dxi,i)
back(::typeof(getindex),::Type{Arg{N}},o...) where {N} = nothing # Only the first arg has gradient

# use ungetindex machinery also for view and selectdim
@primitive  view(x,i...),dxi,xi  ungetindex(x,dxi,i)
back(::typeof(view),::Type{Arg{N}},o...) where {N} = nothing # Only the first arg has gradient
@inline selectdim(A::Value{<:AbstractArray}, d::Integer, i) = Base._selectdim(A, d, i, Base.setindex(map(Base.Slice, axes(A)), i, d))

# Gradient of getindex: If xi=getindex(x,i...) and we receive dxi,
# ungetindex creates dx representing zeros similar to x, with only
# dx[i...] set to dxi.  We use the sparse container Sparse for
# efficiency when x is an array.
# x -> getindex -> xi -> grad -> dxi -> ungetindex -> dx -> grad -> ddx -> getindex -> ddxi

# ungetindex(x,dxi,i)=Sparse(x,Any[dxi],Any[i])

# For Object arrays, Dict, Tuple, Number no need to use Sparse:

ungetindex(x::Number,dxi,i)=dxi

ungetindex(x::AbstractDict,dxi,i)=setindex!(empty(x), dxi, i...)

function ungetindex(x::Tuple,dxi,i) # use array code in case there are repeated indices
    dx = Array{Any}(nothing, length(x))
    dxi = (i[1] isa Real ? dxi : collect(Any,dxi))
    addtoindex!(dx, dxi, Base.to_indices(dx,i)...)
    tuple(dx...)
end

function ungetindex(x::AbstractArray{T},dxi,i) where T
    if isbitstype(T)
        # Issue Knet#439: hessians for neural networks In higher order derivatives, Sparse
        # structs may participate in operations like matmul etc.  We do not want to
        # implement every possible operation with Sparse, so in these cases we generate the
        # full array instead.  The first two conditions should only trigger for higher order
        # gradients, not during regular training.
        if dxi isa Value
            forw(addto!, zero(x), forw(ungetindex, x, dxi, i))
        elseif recording()
            addtoindex!(zero(x), dxi, i...)
        else
            Sparse(x,Any[dxi],Any[i])
        end
    else
        # Using addtoindex! instead of setindex! to handle repeated indices
        addtoindex!(Array{Union{T,Nothing}}(nothing, size(x)), dxi, i...)
    end
end

# For higher order derivatives, the operation of ungetindex might be
# recorded and differentiated, so it must be a primitive.  It is only
# differentiable wrt its value arg.  It should unbox its arguments,
# but it only needs to record if the value argument is boxed.  We'll
# have to define this manually.  To unbox the container arg and
# resolve ambiguity the ungetindex methods cover all combinations of
# first two args:
# (a,a), (a,r), (r,r), (r,a), (g2,a), (g2,r), (g,a), (g,r)

# Ignore Value in the first and third position
ungetindex(x::Value,dxi::Value,i)=ungetindex(value(x),dxi,value(i))
ungetindex(x::Value,dxi,i)=ungetindex(value(x),dxi,value(i))

back(::typeof(ungetindex),::Type{Arg{2}},ddx,dx,x,dxi,i)=getindex(ddx,value(i)...)
back(::typeof(ungetindex),::Type{Arg{N}},o...) where {N} = nothing

# gradcheck works with the first arg, we need to check ungetindex grad for its second arg
# ungetindex2(value, container, index)=ungetindex(container, value, index)
# addtest(:ungetindex2, rand(),  rand(2),   (2,))
# addtest(:ungetindex2, rand(2), rand(3),   (2:3,))
# addtest(:ungetindex2, rand(),  rand(2,2), (1,2))
# addtest(:ungetindex2, rand(2), rand(3,3), (1:2,3))

# get (getindex with a default value)
# This can be left as a composite function, it will get its gradient from getindex if necessary.
get(A::Value{T}, i::Integer, default) where {T<:AbstractArray} = (if checkbounds(Bool, length(A), i); A[i]; else; default; end)
get(A::Value{T}, I::Tuple{}, default) where {T<:AbstractArray} = similar(A, typeof(default), 0)
get(A::Value{T}, I::Dims, default) where {T<:AbstractArray}    = (if checkbounds(Bool, size(A), I...); A[I...]; else; default; end)


text:
module ZSHCompletions

using ..AST

const tab = " "

function emit(cmd::Entry)
    name = cmd.root.name
    "#compdef _$name $name \n" * emit("_", cmd.root, true)
end

function emit(prefix::String, cmd::NodeCommand, entry::Bool)
    lines = [
        "# These are set by _arguments",
        "local context state state_descr line",
        "typeset -A opt_args",
        "",
    ]

    args = basic_arguments(entry)
    hints = map(values(cmd.subcmds)) do x
        str = x.name * "\\:"
        str *= "'" * x.description.brief * "'"
    end

    hints = "((" * join(hints, " ") * "))"

    push!(args, "\"1: :$hints\"")
    push!(args, "\"*:: :->args\"")

    push!(lines, "_arguments -C \\")
    append!(lines, map(x -> tab * x * " \\", args))

    push!(lines, "")
    push!(lines, raw"case $state in")
    push!(lines, tab * "(args)")
    push!(lines, tab * tab * raw"case ${words[1]} in")

    commands = []
    for (_, each) in cmd.subcmds
        name = each.name
        push!(commands, name * ")")
        push!(commands, tab * prefix * cmd.name * "_" * name)
        push!(commands, ";;")
    end
    append!(lines, map(x -> tab^3 * x, commands))
    push!(lines, tab^2 * "esac")
    push!(lines, "esac")

    body = join(map(x -> tab * x, lines), "\n")

    script = []
    push!(
        script,
        """
        function $prefix$(cmd.name)() {
        $body
        }
        """,
    )

    for (_, each) in cmd.subcmds
        push!(script, emit(prefix * cmd.name * "_", each, false))
    end

    return join(script, "\n\n")
end

function emit(prefix::String, cmd::LeafCommand, entry::Bool)
    lines = ["_arguments \\"]
    args = basic_arguments(entry)

    for (_, option) in cmd.options
        name = option.name
        doc = option.description.brief
        token = "--$name"
        if option.short
            token = "{-$(name[1]),--$name}"
        end
        push!(args, "$token'[$doc]'")
    end

    append!(lines, map(x -> tab * x * " \\", args))
    body = join(map(x -> tab * x, lines), "\n")

    return """
    function $prefix$(cmd.name)() {
    $body
    }
    """
end

function basic_arguments(entry)
    args = ["'(- 1 *)'{-h,--help}'[show help information]'"]

    if entry
        push!(args, "'(- 1 *)'{-V,--version}'[show version information]'")
    end
    return args
end

function actions(args)
    hints = map(args) do x
        str = cmd_name(x) * "\\:"
        str *= "'" * cmd_doc(x).first * "'"
    end

    return "((" * join(hints, " ") * "))"
end

end


text:
"""

二重クロス集計表の各マス目の度数を円の面積で表現するのがバルーンプロットである。

以下の関数に freqtable() による集計表を与えれば描画される。注意点は，円の面積が度数に対応するということ。
"""

using Plots
function balloonplot(table; xlabel="", ylabel="",
                     color=:red, unit=100)
    θ = collect(0:360);
    x = cosd.(θ);
    y = sind.(θ);
    gr(linecolor=:black, label="")
    row, col = size(table)
    row_names, col_names = names(table)
    mx = maximum(table)
    p2 = vline(0.5:col+0.5, xlimit=(0.5, col+0.5),
        ylimit=(0.5, row+0.5), showaxis=false,
        xlabel=xlabel, ylabel=ylabel,
        aspect_ratio=1, size=(col*unit, row*unit))
    hline!(0.5:row+0.5)
    for i in 1:row
        for j in 1:col
            r = 0.45sqrt(table[i, j]/mx)
            plot!(r .* x .+ j, r .* y .+ (row - i + 1),
                  seriestype=:shape, fillcolor=color)
            annotate!(j, row - i + 1, text(string(table[i, j]), 8))
        end
    end
    xticks!(1:col, col_names)
    yticks!(1:row, reverse(row_names))
    p2
end;

"""
使用法

julia> using RDatasets

julia> haireyecolor = RDatasets.dataset("datasets", "HairEyeColor");

julia> using DataFrames, FreqTables, Plots

julia> haireyecolor[!, 1:3] = string.(haireyecolor[!, 1:3]); # String7 が使えないパッケージに対する過渡的な対処法

julia> HEC = DataFrame();

julia> for row in eachrow(haireyecolor), j in 1:row.Freq
           push!(HEC, row)
       end

julia> select!(HEC, Not(:Freq)); # Freq 列を除く

julia> EyeHair = freqtable(HEC.Eye, HEC.Hair)
4×4 Named Matrix{Int64}
Dim1 ╲ Dim2 │ Black  Blond  Brown    Red
────────────┼───────────────────────────
Blue        │    20     94     84     17
Brown       │    68      7    119     26
Green       │     5     16     29     14
Hazel       │    15     10     54     14

julia> balloonplot(EyeHair,
           xlabel="Hair color", ylabel="Eye color", color=:blue)
"""

max_stars_repo_name: r-de-r/stats

max_issues_repo_name: r-de-r/stats

max_forks_repo_name: r-de-r/stats


text:
function lateral_attenuation(settings::PyObject, beta, x_obs)
    # Depression angle: phi_d = beta (elevation angle) + epsilon (aircraft bank angle = 0)
    # Note: beta is in degrees
    T = eltype(beta)
    phi_d = beta

    # Lateral side distance
    l = x_obs[2]

    # Engine installation term [dB]
    if settings.engine_mounting == "underwing"
        E_eng = 10 * log10.((0.0039 * cos.(phi_d*pi/180).^2 .+ sin.(phi_d*pi/180).^2).^0.062./(0.8786 * sin.(2 * phi_d*pi/180).^2 .+ cos.(2*phi_d*pi/180).^2))
    elseif settings.engine_mounting == "fuselage"
        E_eng = 10 * log10.((0.1225 * cos.(phi_d*pi/180).^2 .+ sin.(phi_d*pi/180).^2).^0.329)
    elseif settings.engine_mounting == "propeller"
        E_eng = zeros(size(beta))
    elseif settings.engine_mounting == "none"
        E_eng = zeros(size(beta))
    else
        throw(DomainError("Invalid engine_mounting specified. Specify: underwing/fuselage/propeller/none."))
    end

    # Attenuation caused by ground and refracting-scattering effects [dB]
    # Note: beta is in degrees
    A_grs = zeros(T, size(beta))
    A_grs[findall(beta.<=50.)] = (1.137 .- 0.0229*beta .+ 9.72 * exp.(-0.142 * beta))[findall(beta.<=50.)]

    # Over-ground attenuation [dB]
    if 0. <= l <= 914
        g = 11.83 * (1 - exp(-0.00274 * l))
    elseif l > 914
        g = 10.86  # 11.83*(1-exp(-0.00274*914))
    else
        throw(DomainError("Lateral sideline distance negative."))
    end

    # Overall lateral attenuation
    Lambda = 10 .^ ((E_eng .- g * A_grs ./ 10.86) ./ 10.)

    return Lambda
end

max_stars_repo_name: MIT-LAE/pyNA

max_issues_repo_name: MIT-LAE/pyNA

max_forks_repo_name: MIT-LAE/pyNA


text:
"The perfect sidekick to your scientific inquiries"
module DrWatson
import Pkg, LibGit2

# Pure Julia implementation
include("project_setup.jl")
include("naming.jl")
include("saving_tools.jl")

# Functionality that saves/loads
using FileIO: save, load
export save, load
wsave = save
wload = load

include("saving_files.jl")

# Functionality that requires Dataframes and other heavy dependencies:
using Requires
function __init__()
    @require DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
        include("result_collection.jl")
    end
end

end


text:
using RecipesBase

@recipe function f(a::ConvexPolygon)
    grid --> false
    axis --> false
    ticks --> false
    legend --> false
    aspect_ratio --> :equal
    linewidth --> 1.0
    linecolor --> :black
    fillcolor --> :black
    fillalpha --> 0.5
    fillrange := 0.0
    leg := false
    @series begin
        vertices = vcat(a.pts, a.pts[1:1])
        Tuple.(vertices)
    end
end


text:
export
    cartesian_product,
    normalize,
    normalized_product,
    make_intervals,
    linear_value,
    bounded_linear_value,
    normal_density,
    memo,
    doop,
    mult_through,
    add_through,
    ancestors,
    topsort

###############################################
#                                             #
# Cartesian product of arrays                 #
# Result contains every combination of inputs #
#                                             #
###############################################

"""
    cartesian_product(xs::Tuple)
    cartesian_product(xs::Array)

Given an array of arrays, returns the cartesian product of those arrays.

# Examples
```julia-repl
julia> cartesian_product([[1,2],[3,4]])
4-element Array{Array{Int64,1},1}:
 [1, 3]
 [1, 4]
 [2, 3]
 [2, 4]

julia> cartesian_product([[1,2],[3,4],[5,6]])
8-element Array{Array{Int64,1},1}:
 [1, 3, 5]
 [1, 3, 6]
 [1, 4, 5]
 [1, 4, 6]
 [2, 3, 5]
 [2, 3, 6]
 [2, 4, 5]
 [2, 4, 6]
```
"""
cartesian_product(xs::Tuple) = cartesian_product([x for x in xs])

function cartesian_product(xs :: Array)
        if isempty(xs)
        result = Array{Any, 1}[[]]
    else
        yss = cartesian_product(xs[2:end])
        result = Array{Any, 1}[]
        for x in xs[1]
            for ys in yss
                zs = copy(ys)
                pushfirst!(zs, x)
                push!(result, zs)
            end
        end
    end
    return result
end

########################################################
#                                                      #
# Normalize an array of non-negative reals to xum to 1 #
#                                                      #
########################################################

"""
    normalized_product(xss, n)

Compute the product of the given arrays of length n and normalize the result.
Uses log computations to avoid underflow.
"""
function normalized_product(xss, n)
    rs = zeros(Float64, n)
    for xs in xss
        rs .+= [log(x) for x in xs]
    end
    z = max(rs...)
    rs .-= z
    ps = [exp(x) for x in rs]
    return normalize(ps)
end

#################################
#                               #
# Continuous variable utilities #
#                               #
#################################

"""
    make_intervals(range)

Given a range of values of a continuous variable, create interval bins
surrounding the values
"""
function make_intervals(range)
    srng = copy(range)
    sort!(srng)
    last = -Inf
    result = Tuple{Float64, Float64}[]
    for i = 2:length(srng)
        next = (srng[i-1] + srng[i]) / 2
        push!(result, (last, next))
        last = next
    end
    push!(result, (last, Inf))
    return result
end

"""
    linear_value(weights, bias, continuous_combo)

Weight and sum the `continuous_combo` with the given bias
"""
function linear_value(weights, bias, continuous_combo)
    result = bias
    for (weight, parent_value) in zip(weights, continuous_combo)
        result += weight * parent_value
    end
    return result
end

"""
    bounded_linear_value(weights, bias, interval_combo)

Weight and sum the upper and lower bounds in `interval_combo` with the
given bias
"""
function bounded_linear_value(weights, bias, interval_combo)
    lower = bias
    upper = bias
    # This code assumes that the intervals are correctly ordered
    for (weight, (lower_parent, upper_parent)) in zip(weights, interval_combo)
        if weight < 0
            lower += weight * upper_parent
            upper += weight * lower_parent
        else
            lower += weight * lower_parent
            upper += weight * upper_parent
        end
    end
    return (lower, upper)
end

"""
    normal_density(x, mean, variance)

Get the normal density of `x`
"""
function normal_density(x, mean, variance)
    d = sqrt(2 * pi * variance)
    e = -0.5 * (x-mean) * (x-mean) / variance
    return exp(e) / d
end

"""
    memo(f::Function)

returns a memoized one argument function
"""
function memo(f::Function)
    cache = Dict()
    function apply(arg)
        if arg in keys(cache)
            return cache[arg]
        else
            result = f(arg)
            cache[arg] = result
            return result
        end
    end
    return apply
end

####################################################################################
#                                                                                #
# Functions for performing an arithmetic operation recursively on a data structure #
#                                                                                #
####################################################################################

# This avoids having to write both forms of the function.
# Writing both forms could cause ambiguity.
# Will cause stack overflow if neither form is defined.
# Assumes op is commutative.
doop(x::Any, y::Any, op) = doop(y,x,op)

function doop(x::Float64, y::Float64, op::Function)
    return op(x,y)
end

function doop(x::Dict, y::Any, op) where {K,V}
    result = Dict()
    xf = floatize(x)
    for (k,v) in xf
        result[k] = doop(v, floatize(y), op)
    end
    return result
end

function doop(x::Dict, y::Dict, op) where {K,V1,V2}
    result = Dict()
    xf = floatize(x)
    yf = floatize(y)
    for k in keys(xf)
        result[k] = doop(xf[k], yf[k], op)
    end
    return result
end

function doop(xs::Array, y::Any, op)
    result = floatize(xs)
    for i = 1:length(xs)
        result[i] = doop(xs[i], floatize(y), op)
    end
    return result
end

function doop(xs::Array, ys::Array, op)
    xf = floatize(xs)
    yf = floatize(ys)
    return [doop(xf[i], yf[i], op) for i in 1:length(xs)]
end

function doop(xs::Tuple, y::Any, op)
    xf = floatize(xs)
    return ntuple(i -> doop(xf[i], floatize(y), op), length(xf))
end

function doop(xs::Tuple, ys::Tuple, op)
    xf = floatize(xs)
    yf = floatize(ys)
    return ntuple(i -> doop(xf[i], yf[i], op), length(xs))
end

mult_through(x,y) = doop(x, y, (x,y) -> x*y)

add_through(x,y) = doop(x, y, (x,y) -> x+y)

floatize(x) = _transform(y -> Float64(y), x)
#=
 Topological sort
=#

"""
    ancestors(graph :: Dict{U, Vector{U}}, var :: U, found:: Set{U}) :: Vector{U} where U

Find the ancestors of the given value x in the graph.  Found is a set of previously
found ancestors, to handle cyclic graphs and avoid infinite loops
"""
# Need to handle cyclic graphs
function ancestors(graph :: Dict{U, Vector{U}}, var :: U, found:: Set{U}) :: Vector{U} where U
    result = Vector{U}()
    for par in get(graph, var, [])
        if isa(par, U) && !(par in found)
            push!(found, par) # Prevent infinite recursion
            append!(result, ancestors(graph, par, found))
            push!(result, par) # Guarantee that the ancestors appear before par
        end
    end
    return result
end

"""
    topsort(graph::Dict{T, Vector{T}}) :: Vector{T} where T

Performs a topological sort on the given graph. In a cyclic graph, the order of variables in the cycle
is arbitrary, but they will be correctly sorted relative to other variables.
"""
function topsort(graph::Dict{U, Vector{U}}) :: Vector{U} where U
    result = Vector{U}()
    found = Set{U}()
    for var in keys(graph)
        if !(var in found)
            push!(found, var)
            s = Set{U}([var])
            ancs = ancestors(graph, var, s) # guaranteed to be in topological order
            for anc in ancs
                if !(anc in found)
                    push!(found, anc)
                    push!(result, anc)
                end
            end
            push!(result, var) # after the ancestors
        end
    end
    return result
end


text:
function CurrentSensor(; name)
    @named p = Pin()
    @named n = Pin()
    @variables i(t)
    eqs = [
        p.v ~ n.v
        i ~ p.i
        i ~ -n.i
    ]
    ODESystem(eqs, t, [i], [], systems=[p, n], defaults=Dict(i => 1.0), name=name)
end

function PotentialSensor(; name)
    @named p = Pin()
    @variables phi(t)
    eqs = [
        p.i ~ 0
        phi ~ p.v
    ]
    ODESystem(eqs, t, [phi], [], systems=[p], defaults=Dict(phi => 1.0), name=name)
end

function VoltageSensor(; name)
    @named p = Pin()
    @named n = Pin()
    @variables v(t)
    eqs = [
        p.i ~ 0
        n.i ~ 0
        v ~ p.v - n.v
    ]
    ODESystem(eqs, t, [v], [], systems=[p, n], defaults=Dict(v => 1.0), name=name)
end

function PowerSensor(; name)
    @named pc = Pin()
    @named nc = Pin()
    @named pv = Pin()
    @named nv = Pin()
    @named voltage_sensor = VoltageSensor()
    @named current_sensor = CurrentSensor()
    @variables power(t)
    eqs = [
        connect(voltage_sensor.p, pv)
        connect(voltage_sensor.n, nv)
        connect(current_sensor.p, pc)
        connect(current_sensor.n, nc)
        power ~ current_sensor.i * voltage_sensor.v
    ]
    ODESystem(eqs, t, [power], [], systems=[pc, nc, pv, nv, voltage_sensor, current_sensor], defaults=Dict(power => 1.0), name=name)
end

function MultiSensor(; name)
    @named pc = Pin()
    @named nc = Pin()
    @named pv = Pin()
    @named nv = Pin()
    @named voltage_sensor = VoltageSensor()
    @named current_sensor = CurrentSensor()
    @variables i(t) v(t)
    eqs = [
        connect(voltage_sensor.p, pv)
        connect(voltage_sensor.n, nv)
        connect(current_sensor.p, pc)
        connect(current_sensor.n, nc)
        i ~ current_sensor.i
        v ~ voltage_sensor.v
    ]
    ODESystem(eqs, t, [i, v], [], systems=[pc, nc, pv, nv, voltage_sensor, current_sensor], defaults=Dict(i => 1.0, v => 1.0), name=name)
end


text:
module chapters_whats_dash
using Dash, DashCoreComponents, DashHtmlComponents

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

app = dash()

app.layout = html_div() do
    html_h2("Introduction to Dash"),
    dcc_markdown("
Dash is a productive framework for building web applications in Julia, Python, and R.

Written on top of HTTP.jl, Plotly.js, and React.js,
Dash is ideal for building data visualization apps
with highly custom user interfaces in pure Julia, Python, or R.
It's particularly suited for anyone who works with data.

Through a couple of simple patterns, Dash abstracts away all of the
technologies and protocols that are required to build an
interactive web-based application.
Dash is simple enough that you can bind a user interface
around your Julia, Python, or R code in an afternoon.

Dash apps are rendered in the web browser. You can deploy your apps
to servers and then share them through URLs.
Since Dash apps are viewed in the web browser, Dash is inherently
cross-platform and mobile ready.

There is a lot behind the framework. To learn more about how the original Dash was built
and what motivated Dash, watch our talk from
Plotcon below
or read our [announcement letter](https://medium.com/@plotlygraphs/introducing-dash-5ecf7191b503).

Dash is an open source library, released under the permissive MIT license.
[Plotly](https://plotly.com) develops Dash for Julia and offers a
[platform for managing Dash for Julia, Python, and R apps in an enterprise environment](https://plotly.com/dash).
If you're interested, [please get in touch](https://plot.ly/get-demo).

***

  "),

    html_iframe(
        width = "100%",
        height = "480",
        style = Dict("border" => "none"),
        src = "https://www.youtube-nocookie.com/embed/5BAthiN0htc?rel=0"
    ),

    html_hr(),
    dcc_markdown("[Back to the table of contents](/)")
end

end

max_stars_repo_name: joelostblom/dash-docs

max_issues_repo_name: joelostblom/dash-docs

max_forks_repo_name: joelostblom/dash-docs


text:
# ---
# title: 732. My Calendar III
# id: problem732
# author: Tian Jun
# date: 2020-10-31
# difficulty: Hard
# categories: Segment Tree, Ordered Map
# link: <https://leetcode.com/problems/my-calendar-iii/description/>
# hidden: true
# ---
#
# Implement a `MyCalendarThree` class to store your events. A new event can
# **always** be added.
#
# Your class will have one method, `book(int start, int end)`. Formally, this
# represents a booking on the half open interval `[start, end)`, the range of
# real numbers `x` such that `start <= x < end`.
#
# A _K-booking_ happens when **K** events have some non-empty intersection (ie.,
# there is some time that is common to all K events.)
#
# For each call to the method `MyCalendar.book`, return an integer `K`
# representing the largest integer such that there exists a `K`-booking in the
# calendar.
#
# Your class will be called like this: `MyCalendarThree cal = new
# MyCalendarThree();` `MyCalendarThree.book(start, end)`
#
# **Example 1:**
#
#
#
#     MyCalendarThree();
#     MyCalendarThree.book(10, 20); // returns 1
#     MyCalendarThree.book(50, 60); // returns 1
#     MyCalendarThree.book(10, 40); // returns 2
#     MyCalendarThree.book(5, 15); // returns 3
#     MyCalendarThree.book(5, 10); // returns 3
#     MyCalendarThree.book(25, 55); // returns 3
#     Explanation:
#     The first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking.
#     The third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking.
#     The remaining events cause the maximum K-booking to be only a 3-booking.
#     Note that the last event locally causes a 2-booking, but the answer is still 3 because
#     eg. [10, 20), [10, 40), and [5, 15) are still triple booked.
#
#
#
#
# **Note:**
#
#   * The number of calls to `MyCalendarThree.book` per test case will be at most `400`.
#   * In calls to `MyCalendarThree.book(start, end)`, `start` and `end` are integers in the range `[0, 10^9]`.
#
#
#
#
## @lc code=start
using LeetCode

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


text:
using Images, MXNet, MLDatasets, StatsBase

DATASET_DIR = "data/101_ObjectCategories"
CLASSES = readdir(DATASET_DIR)

images = Tuple{String,Int}[]

for dir_id = 1:length(CLASSES)

    dir = CLASSES[dir_id]
    all_files = readdir(joinpath(DATASET_DIR, dir))
    keep_files = filter(x -> endswith(x, ".jpg"), all_files)

    append!(images, map(x -> (joinpath(DATASET_DIR, dir, x), dir_id - 1), keep_files))
end

### SHUFFLE DATASET
images = images[shuffle(1:length(images))];

### LOAD THE NEURAL NETWORK
nnet = mx.load_checkpoint("weights/inception-v3/InceptionV3-FE", 0, mx.FeedForward; context = mx.gpu());

### DEFINE SIZE OF TRAIN AND TEST DATASETS
features = zeros(2048, length(images))
batch_size_fe = 250

for idx = 1:batch_size_fe:length(images)

    println(idx)

    row_count = min(length(images) + 1 - idx, batch_size_fe)
    mx_data_features = mx.zeros((299, 299, 3, row_count));

    for idx_ = 1:row_count
        img = channelview(RGB.(imresize(load(images[idx + idx_ - 1][1]), (299, 299))))
        mx_data_features[idx_:idx_] = reshape(Float16.(permuteddimsview(img, (3, 2, 1))), (299, 299, 3, 1));
    end

    mx_data_features *= 256.0;
    mx_data_features -= 128.;
    mx_data_features /= 128.;

    train_data_provider = mx.ArrayDataProvider(:data => mx_data_features);
    features[:, idx:idx + row_count - 1] = mx.predict(nnet, train_data_provider)
end

arch = @mx.chain mx.Variable(:data) =>
  mx.FullyConnected(num_hidden=length(CLASSES)) =>
  mx.SoftmaxOutput(mx.Variable(:label))

nnet2 = mx.FeedForward(arch, context = mx.gpu())
optimizer = mx.ADAM()

train_indices = 1:8000;
valid_indices = 8000:length(images);
batch_size = length(valid_indices);

for i = 1:30

    println(i, " - ", i+batch_size-1);

    train_indices_batch = sample(train_indices, batch_size, replace = false);
    valid_indices_batch = sample(valid_indices, batch_size, replace = false);

    train_data_provider = mx.ArrayDataProvider(:data => features[:, train_indices_batch], :label => map(x -> x[2], images[train_indices_batch]), batch_size = batch_size);
    validation_data_provider = mx.ArrayDataProvider(:data => features[:, valid_indices_batch], :label => map(x -> x[2], images[valid_indices_batch]), batch_size = batch_size);

    mx.fit(nnet2, optimizer, train_data_provider, eval_data = validation_data_provider, n_epoch = 1, callbacks = [mx.speedometer()]);

end

### PREPARING THE INPUT
batch_size = 100
train_indices = 1:40000
validation_indices = 40001:50000
optimizer = mx.ADAM();

for i = 1:10

    println(i, " - ", i+batch_size-1)

    train_indices_batch = sample(train_indices, batch_size, replace = false)
    validation_indices_batch = sample(validation_indices, batch_size, replace = false)

    mx_data_train = mx.zeros((299, 299, 3, batch_size));
    mx_data_validation = mx.zeros((299, 299, 3, batch_size));

    map(x -> mx_data_train[x:x] = reshape(Float16.(channelview(imresize(train_x[:, :, :, train_indices_batch[x]], (299, 299)))), (299, 299, 3, 1)), 1:batch_size);
    map(x -> mx_data_validation[x:x] = reshape(Float16.(channelview(imresize(train_x[:, :, :, validation_indices_batch[x]], (299, 299)))), (299, 299, 3, 1)), 1:batch_size);

    mx_data_train *= 256.0;
    mx_data_train -= 128.;
    mx_data_train /= 128.;

    mx_data_validation *= 256.0;
    mx_data_validation -= 128.;
    mx_data_validation /= 128.;

    train_data_provider = mx.ArrayDataProvider(:data => mx_data_train, :label => train_y[train_indices_batch], batch_size = 50);
    validation_data_provider = mx.ArrayDataProvider(:data => mx_data_validation, :label => train_y[validation_indices_batch], batch_size = 100);

    mx.fit(nnet, optimizer, train_data_provider, eval_data = validation_data_provider, n_epoch = 5, callbacks = [mx.speedometer()]);
end

features_train = features[:, 1:4000]
features_validate = features[:, 4001:end];
features_train_y = outputs[1:4000]
features_validate_y = outputs[4001:end]

train_length = 4000
validation_length = 1000

train_data_array  = mx.zeros((size(features_train, 1)..., train_length...));
train_label_array = mx.zeros(train_length);

validation_data_array  = mx.zeros((size(features_train, 1)..., validation_length...));
validation_label_array = mx.zeros(validation_length);

# The number of records we send to the training should be at least number of outcome
for idx = 1:train_length
    train_data_array[idx:idx] = reshape(features_train[:, idx], (size(features_train, 1)..., 1...))
    train_label_array[idx:idx] = features_train_y[idx]
end

for idx = 1:validation_length
    validation_data_array[idx:idx] = reshape(features_validate[:, idx], (size(features_validate, 1)..., 1...))
    validation_label_array[idx:idx] = features_validate_y[idx]
end

train_data_provider = mx.ArrayDataProvider(:data => features_train, :label => train_label_array, batch_size = 500);
validation_data_provider = mx.ArrayDataProvider(:data => features_validate, :label => validation_label_array, batch_size = 500);

arch = @mx.chain mx.Variable(:data) =>
  mx.FullyConnected(num_hidden=128) =>
  mx.Activation(act_type=:relu) =>
  mx.FullyConnected(num_hidden=64) =>
  mx.Activation(act_type=:relu) =>
  mx.FullyConnected(num_hidden=10) =>
  mx.SoftmaxOutput(mx.Variable(:label))

arch = @mx.chain mx.Variable(:data) =>
  mx.FullyConnected(num_hidden=128) =>
  mx.Activation(act_type=:relu) =>
  mx.FullyConnected(num_hidden=64) =>
  mx.Activation(act_type=:relu) =>
  mx.FullyConnected(num_hidden=10) =>
  mx.Activation(act_type=:relu) =>
  mx.SoftmaxOutput(mx.Variable(:label))

nnet = mx.FeedForward(arch, context = mx.gpu())

mx.fit(nnet, mx.ADAM(), train_data_provider, eval_data = validation_data_provider, n_epoch = 250, callbacks = [mx.speedometer()]);
mx.fit(nnet, mx.ADAM(), train_data_provider, eval_data = train_data_provider, n_epoch = 250, callbacks = [mx.speedometer()]);


text:
using LinearAlgebra
using PyPlot
using PolynomialBasis
using ImplicitDomainQuadrature
using Revise
using CutCellDG
include("../../../../test/useful_routines.jl")
include("transformation-elasticity-solver.jl")

function angular_position(points)
    cpoints = points[1, :] + im * points[2, :]
    return rad2deg.(angle.(cpoints))
end

function interface_maxnorm_stress_error(polyorder, nelmts, penaltyfactor)
    width = 1.0
    K1, K2 = 247.0, 192.0    # Pa
    mu1, mu2 = 126.0, 87.0   # Pa
    lambda1 = lame_lambda(K1, mu1)
    lambda2 = lame_lambda(K2, mu2)
    stiffness = CutCellDG.HookeStiffness(lambda1, mu1, lambda2, mu2)

    theta0 = -0.067
    transfstress =
        CutCellDG.plane_strain_transformation_stress(lambda1, mu1, theta0)

    meshwidth = [width, width]
    numqp = required_quadrature_order(polyorder) + 2

    dx = width / nelmts
    penalty = penaltyfactor / dx * 0.5 * (lambda1 + mu1 + lambda2 + mu2)

    basis = TensorProductBasis(2, polyorder)
    interfacecenter = [0.0, 0.0]
    interfaceradius = 0.5
    outerradius = 2.0
    analyticalsolution = AnalyticalSolution(
        interfaceradius,
        outerradius,
        interfacecenter,
        lambda1,
        mu1,
        lambda2,
        mu2,
        theta0,
    )

    mesh, cellquads, facequads, interfacequads = construct_mesh_and_quadratures(
        meshwidth,
        nelmts,
        basis,
        interfacecenter,
        interfaceradius,
        numqp,
    )

    nodaldisplacement = nodal_displacement(
        mesh,
        basis,
        cellquads,
        facequads,
        interfacequads,
        stiffness,
        theta0,
        analyticalsolution,
        penalty,
    )

    refseedpoints, spatialseedpoints, seedcellids =
        CutCellDG.seed_zero_levelset_with_interfacequads(interfacequads, mesh)
    normals = CutCellDG.collect_interface_normals(interfacequads, mesh)
    tangents = CutCellDG.rotate_90(normals)

    spatialpoints = spatialseedpoints[1, :, :]
    referencepoints = refseedpoints
    referencecellids = seedcellids

    productstress = product_stress_at_reference_points(
        nodaldisplacement,
        basis,
        stiffness,
        transfstress,
        theta0,
        referencepoints,
        referencecellids,
        mesh,
    )
    parentstress = parent_stress_at_reference_points(
        nodaldisplacement,
        basis,
        stiffness,
        referencepoints,
        referencecellids,
        mesh,
    )

    productradialtraction =
        CutCellDG.traction_force_at_points(productstress, normals)
    parentradialtraction =
        CutCellDG.traction_force_at_points(parentstress, normals)

    productradialstress = vec(sum(productradialtraction .* normals, dims = 1))
    parentradialstress = vec(sum(parentradialtraction .* normals, dims = 1))

    exactproductradialstress =
        shell_radial_stress(analyticalsolution, interfaceradius)
    exactparentradialstress = core_in_plane_stress(analyticalsolution)

    productradialstressmaxnormerror =
        maximum(abs.(productradialstress .- exactproductradialstress)) /
        abs(exactproductradialstress)
    parentradialstressmaxnormerror =
        maximum(abs.(parentradialstress .- exactparentradialstress)) /
        abs(exactparentradialstress)

    productcircumferentialtraction =
        CutCellDG.traction_force_at_points(productstress, tangents)
    productcircumferentialstress =
        vec(sum(productcircumferentialtraction .* tangents, dims = 1))

    exactproductcircumferentialstress =
        shell_circumferential_stress(analyticalsolution, interfaceradius)

    productcircumferentialstressmaxnormerror =
        maximum(
            abs.(
                productcircumferentialstress .-
                exactproductcircumferentialstress,
            ),
        ) / abs(exactproductcircumferentialstress)

    return [
        parentradialstressmaxnormerror,
        productradialstressmaxnormerror,
        productcircumferentialstressmaxnormerror,
    ]
end

polyorder = 3
penaltyfactor = 1e2
powers = [3, 4, 5]
nelmts = [2^p+1 for p in powers]
dx = 1.0 ./ nelmts

stresserror = [
    interface_maxnorm_stress_error(polyorder, ne, penaltyfactor) for
    ne in nelmts
]

stresserror = vcat([s' for s in stresserror]...)

using CSV, DataFrames

grid = [string(ne)*" x "*string(ne) for ne in nelmts]

df = DataFrame(
    "Grid" => grid,
    "Parent Radial Stress Error" => stresserror[:, 1],
    "Product Radial Stress Error" => stresserror[:, 2],
    "Product Circumferential Stress Error" => stresserror[:,3]
)

folderpath = "examples/traction-continuity-with-transformation/cylindrical-particle/cylindrical-bc/"
filename = "polyorder-"*string(polyorder)*".csv"
CSV.write(folderpath*filename,df)


text:
"""
    Ash(h::Union{KHist, Hist, ExpandingHist})
    Ash(h::Union{KHist, Hist, ExpandingHist}, m::Int, kernel::Function)

Create an Average Shifted Histogram using `h` as the base histogram with smoothing parameter `m`
and kernel function `kernel`.  Built-in kernels are available in the `OnlineStats.Kernels` module.

# Example

    using OnlineStats, Plots

    o = fit!(Ash(ExpandingHist(1000)), randn(10^6))

    plot(o)
    plot(o, 20)
    plot(o, OnlineStats.Kernels.epanechnikov, 4)
"""
mutable struct Ash{H, T} <: OnlineStat{Number}
    hist::H
    density::T
    m::Int
    kernel::Function
end

_default_m(h) = ceil(Int, length(edges(h)) / 100)
_default_m(h::KHist) = ceil(Int, h.k / 100)

_default_density(h) = zeros(length(counts(h)))
_default_density(h::KHist) = zeros(h.k)

Ash(h, kernel::Function, m::Int = _default_m(h)) = Ash(h, m, kernel)

function Ash(h, m::Int = _default_m(h), kernel::Function = Kernels.biweight)
    Ash(h, _default_density(h), m, kernel)
end

_fit!(o::Ash, data) = _fit!(o.hist, data)

nobs(o::Ash) = nobs(o.hist)

value(o::Ash, kernel::Function, m::Int = o.m) = value(o, m, kernel)

function value(o::Ash, m::Int = o.m, kernel::Function = o.kernel)
    if nobs(o) > 0
        o.m = m
        o.kernel = kernel
        y = o.density
        y .= 0.0
        counts = OnlineStats.counts(o.hist)
        b = length(counts)
        mids = midpoints(o.hist)
        for k in eachindex(mids)
            if counts[k] > 0
                for i in max(1, k - m + 1):min(b, k + m - 1)
                    y[i] += counts[k] * kernel((i - k) / m)
                end
            end
        end
        density_sum_to_1!(o)
        inds = findfirst(x -> x > 0, y):findlast(x -> x > 0, y)
        return (x = mids[inds], y = y[inds])
    else
        return (x=midpoints(o.hist), y=o.density)
    end
end

function density_sum_to_1!(o::Ash{<:HistogramStat})
    rmul!(o.density, 1 / (sum(o.density) * step(edges(o.hist))))
end
function density_sum_to_1!(o::Ash{<:KHist})
    o.density ./= area(midpoints(o.hist), o.density)
end

#-----------------------------------------------------------------------------# Plots
histdensity(o::Ash) = counts(o.hist) ./ nobs(o) ./ step(edges(o.hist))
histdensity(o::Ash{<:KHist}) = counts(o.hist) ./ area(o.hist)

@recipe f(o::Ash, kernel::Function, m::Int = o.m) = o, m, kernel

@recipe function f(o::Ash, m::Int = o.m, kernel::Function = o.kernel; hist=true)
    if hist
        @series begin
            normalize --> true
            seriesalpha --> .4
            seriestype --> :sticks
            label --> "Histogram Density"
            midpoints(o.hist), histdensity(o)
        end
    end
    @series begin
        x, y = value(o, m, kernel)
        label --> "Ash(m=$(m), kernel=$(o.kernel))"
        xlims --> extrema(x)
        linewidth --> 3
        x, y
    end
end

#-----------------------------------------------------------------------------# kernels
module Kernels
in_range(u) = abs(u) ≤ 1

biweight(u)         = in_range(u) ? (1.0 - u ^ 2) ^ 2 : 0.0
cosine(u)           = in_range(u) ? cos(0.5 * π * u) : 0.0
epanechnikov(u)     = in_range(u) ? 1.0 - u ^ 2 : 0.0
triangular(u)       = in_range(u) ? 1.0 - abs(u) : 0.0
tricube(u)          = in_range(u) ? (1.0 - abs(u) ^ 3) ^ 3 : 0.0
triweight(u)        = in_range(u) ? (1.0 - u ^ 2) ^ 3 : 0.0
uniform(u)          = in_range(u) ? 0.5 : 0.0

gaussian(u) = exp(-0.5 * u ^ 2)
logistic(u) = 1.0 / (exp(u) + 2.0 + exp(-u))
end