text:
function affine_expand_del(dx::Vector{Float64}, fx0::Float64, ∇fx0::SVector{N,Float64}, s::Vector{Int}) where N
    v = fx0
    for i=1:N
        v += ∇fx0[i]*dx[s[i]]
    end
    return v
end
function affine_expand_del(dx::Vector{Interval{Float64}}, fx0::Float64, ∇fx0::SVector{N,Float64}, s::Vector{Int}) where N
    v = fx0
    for i = 1:N
        t = ∇fx0[i]
        tdx = dx[s[i]]
        if !iszero(t)
            v += t > 0.0 ? t*tdx.hi : t*tdx.lo
        end
    end
    return v
end

function affine_expand(x::Vector{Float64}, x0::Vector{Float64}, fx0::Float64, ∇fx0::SVector{N,Float64}) where N
    v = fx0
    for i=1:N
        v += ∇fx0[i]*(x[i] - x0[i])
    end
    return v
end
function affine_expand(x::Vector{Interval{Float64}}, x0::Vector{Float64}, fx0::Float64, ∇fx0::SVector{N,Float64}) where N
    v = Interval{Float64}(fx0)
    for i=1:N
        v += ∇fx0[i]*(x[i] - x0[i])
    end
    return v
end

function expand_set(::Type{MC{N2,T}}, x::MC{N1,T}, fsparse::Vector{Int}, subsparse::Vector{Int}, cv_buffer::Vector{Float64}, cc_buffer::Vector{Float64}) where {N1, N2, T<:RelaxTag}
    cvg = x.cv_grad
    ccg = x.cc_grad
    xcount = 1
    xcurrent = subsparse[1]
    for i = 1:N2
        if fsparse[i] === xcurrent
            cv_buffer[i] = cvg[xcount]
            cc_buffer[i] = ccg[xcount]
            xcount += 1
            if xcount <= N1
            xcurrent = subsparse[xcount]
            else
                break
            end
        else
            cv_buffer[i] = zero(Float64)
        end
    end
    cv_grad = SVector{N2,Float64}(cv_buffer)
    cc_grad = SVector{N2,Float64}(cc_buffer)
    return MC{N2,T}(x.cv, x.cc, x.Intv, cv_grad, cc_grad, x.cnst)
end

function set_value_post(z::MC{N,T}, v::VariableValues{Float64}, s::Vector{Int}, ϵ::Float64) where {V,N,T<:RelaxTag}
    l = z.cv
    u = z.cc
    lower_refinement = true
    upper_refinement = true
    @inbounds for i = 1:N
        cv_val = z.cv_grad[i]
        cc_val = z.cc_grad[i]
        i_sol = s[i]
        x_z = v.x[i_sol]
        lower_bound = _lbd(v, i_sol)
        upper_bound = _ubd(v, i_sol)
        if lower_refinement
            if cv_val > zero(Float64)
                if isinf(lower_bound)
                    !upper_refinement && break
                    lower_refinement = false
                else
                    l += cv_val*(lower_bound - x_z)
                end
            else
                if isinf(upper_bound)
                    !upper_refinement && break
                    lower_refinement = false
                else
                    l += cv_val*(upper_bound - x_z)
                end
            end
        end
        if upper_refinement
            if cc_val > zero(Float64)
                if isinf(lower_bound)
                    !lower_refinement && break
                    upper_refinement = false
                else
                    u += cc_val*(upper_bound - x_z)
                end
            else
                if isinf(upper_bound)
                    !lower_refinement && break
                    upper_refinement = false
                else
                    u += cc_val*(lower_bound - x_z)
                end
            end
        end
    end

    if lower_refinement && (z.Intv.lo + ϵ > l)
        l = z.Intv.lo
    elseif !lower_refinement
        l = z.Intv.lo
    else
        l -= ϵ
    end

    if upper_refinement && (z.Intv.hi - ϵ < u)
        u = z.Intv.hi
    elseif !upper_refinement
        u = z.Intv.hi
    else
        u += ϵ
    end

    return MC{N,T}(z.cv, z.cc, Interval{Float64}(l, u), z.cv_grad, z.cc_grad, z.cnst)
end

"""
$(FUNCTIONNAME)

Intersects the new set valued operator with the prior and performs affine bound tightening

- First forward pass: `post` should be set by user option, `is_intersect` should be false
  so that the tape overwrites existing values, and the `interval_intersect` flag could be set
  to either value.
- Forward CP pass (assumes same reference point): `post` should be set by user option,
  `is_intersect` should be true so that the tape intersects with  existing values, and the
  `interval_intersect` flag should be false.
- Forward CP pass (assumes same reference point): `post` should be set by user option,
  `is_intersect` should be true so that the tape intersects with existing values, and the
  `interval_intersect` flag should be false.
- Subsequent forward passes at new points: post` should be set by user option,
  `is_intersect` should be true so that the tape intersects with existing values, and the
  `interval_intersect` flag should be `true` as predetermined interval bounds are valid but
   the prior values may correspond to different points of evaluation.
"""
function cut(x::MC{N,T}, z::MC{N,T}, v::VariableValues, ϵ::Float64, s::Vector{Int}, cflag::Bool, pflag::Bool) where {N,T<:RelaxTag}
    (pflag & cflag)  && (return set_value_post(x ∩ z.Intv, v, s, ϵ))
    (pflag & !cflag) && (return set_value_post(x, v, s, ϵ))
    (pflag & cflag)  && (return x ∩ z.Intv)
    return x
end

max_stars_repo_name: MatthewStuber/EAGO

max_issues_repo_name: MatthewStuber/EAGO

max_forks_repo_name: MatthewStuber/EAGO


text:
fontpath(fontname) = joinpath(@__DIR__, "..", "..", "assets", "fonts", fontname)

const _cached_fonts = Dict{String, FTFont}()

"""
    load_font(str)

Load a font. `str` must be either the full path to the font file, or a font in
the package font folder.

A font at a given location is cached for further use.
"""
function load_font(str)
    if isfile(str)
        path = str
    elseif isfile(fontpath(str))
        path = fontpath(str)
    end

    get!(_cached_fonts, path) do
        FTFont(path)
    end
end

# Loading the font directly here lead to FreeTypeAbstraction to fail with error code 35
const _default_fonts = Dict(
    :regular => "NewCM10-Regular.otf",
    :italic => "NewCM10-Italic.otf",
    :math => "NewCMMath-Regular.otf"
)

const _default_font_mapping = Dict(
    :text => :regular,
    :digit => :regular,
    :function => :regular,
    :punctuation => :regular,
    :symbol => :math,
    :variable => :italic
)

"""
    Fontset([font_mapping, fonts])

A set of font for LaTeX rendering.

# Fields
  - `font_mapping` a dict mapping the different character types (`:digit`,
    `:function`, `:punctuation`, `:symbol`, `:variable`) to a font identifier.
    Default to `MathTeXEngine._default_font_mapping`
  - `fonts` a dict mapping font identifier to a font path. Default to
    `MathTeXEngine._default_fonts` which represents the NewComputerModern font.
"""
struct FontSet
    fonts::Dict{Symbol, String}
    font_mapping::Dict{Symbol, Symbol}
end

FontSet(fonts) = FontSet(fonts, _default_font_mapping)
FontSet() = FontSet(_default_fonts, _default_font_mapping)

function get_font(fontset, char_type)
    font_id = fontset.font_mapping[char_type]
    return fontset[font_id]
end

Base.getindex(fonset::FontSet, font_id) = load_font(fonset.fonts[font_id])

# Few helper functions
"""
    thickness(font::FTFont)

The thickness of the underline for the given font.
"""
thickness(font::FTFont) = font.underline_thickness / font.units_per_EM

"""
    thickness(font::FontSet)

The thickness of the underline for the given font set.
"""
thickness(fontset::FontSet) = thickness(fontset[:math])

"""
    xheight(font::FTFont)

The height of the letter x in the given font, i.e. the height of the letters
without neither ascender nor descender.
"""
xheight(font::FTFont) = inkheight(TeXChar('x', font))

"""
    xheight(font::FontSet)

The height of the letter x in the given fontset, i.e. the height of the letters
without neither ascender nor descender.
"""
xheight(fontset) = xheight(fontset[:regular])

ext: jl


text:
## xdiff.jl - forward and reverse passes of symbolic differentiation

## utils

function extend_deriv!(dg::ExGraph, dzdx_v::Symbol, dzdx::Any)
    subderivs = find_related(dg, dzdx_v)
    pos = indexof(dg, dzdx_v)
    if isempty(subderivs)
        # first split
        dzdx_ex_1 = getexpr(dg[dzdx_v])
        dzdx_ex_2 = dzdx
        dzdx_v_1 = Symbol("$(dzdx_v)__1")
        dzdx_v_2 = Symbol("$(dzdx_v)__2")
        sub_dg = ExGraph(;ctx=copy(dg.ctx))
        parse!(sub_dg, :($dzdx_v_1 = $dzdx_ex_1))
        parse!(sub_dg, :($dzdx_v_2 = $dzdx_ex_2))
        parse!(sub_dg, :($dzdx_v = $dzdx_v_1 .+ $dzdx_v_2))
        sub_dg = fuse_assigned(sub_dg)
        new_nodes = sub_dg.tape
    else
        # dg already contains subderivatives for dzdx_v
        last_idx = parse(Int, split(subderivs[end] |> String, "__")[end])
        dzdx_v_last = Symbol("$(dzdx_v)__$(last_idx + 1)")
        prev_dzdx_ex = getexpr(dg[dzdx_v])
        sub_dg = ExGraph()
        parse!(sub_dg, :($dzdx_v_last = $dzdx))
        parse!(sub_dg, :($dzdx_v = $prev_dzdx_ex .+ $dzdx_v_last))
        sub_dg = fuse_assigned(sub_dg)
        new_nodes = sub_dg.tape
    end
    delete!(dg, pos)
    insert!(dg, pos, new_nodes)
    return dg
end

## forward pass

function forward_pass!(g::ExGraph)
    evaluate!(g) # to get types of all variables and find correct functions to inline
    known_funcs = Set(sanitize(rule[1].args[1]) for rule in DIFF_RULES)
    done = false
    while !done
        graph_funcs = Set(getexpr(nd).args[1] for nd in g
                          if isa(nd, ExNode{:call}) || isa(nd, ExNode{:bcast}))
        unknown_funcs = setdiff(graph_funcs, known_funcs)
        unknown_func_vars = Set(varname(nd) for nd in g
                                if (isa(nd, ExNode{:call}) || isa(nd, ExNode{:bcast}))
                                && getexpr(nd).args[1] in unknown_funcs)
        g = inline_nodes(g, unknown_func_vars)
        evaluate!(g; force=true)
        done = isempty(unknown_func_vars)
    end
    return g
end

## reverse pass

"""
Perform one step of reverse pass. Add derivatives of output variable w.r.t.
node's dependenices to derivative graph.
"""
function rev_step!(g::ExGraph, dg::ExGraph, nd::ExNode{:(=)})
    y = varname(nd)
    x = dependencies(nd)[1]
    dzdx_vname = deriv_name(z, x)
    # parse!(dg, :($))
    error("Not implemented yet")

end

function rev_step!(g::ExGraph, dg::ExGraph, nd::ExNode{:constant})
    # do nothing
end

function rev_step!(g::ExGraph, dg::ExGraph, nd::ExNode{:input})
    # do nothing
end

function rev_step!(g::ExGraph, dg::ExGraph, nd::ExNode{:field})
    ex = getexpr(nd)
    m_name = ex.args[1]
    fld_name = ex.args[2].value
    dzdx_v = deriv_name(g.ctx[:loss], m_name)
    if haskey(dg, dzdx_v)
        m_nd = dg[dzdx_v]
        kw = @get_or_create(m_nd.meta, :kw, Dict())
        dzdy_v = deriv_name(g.ctx[:loss], varname(nd))
        kw[fld_name] = dzdy_v
    else
        m_nd = ExNode{:ctor}(dzdx_v, :(__construct($m_name)))
        kw = @get_or_create(m_nd.meta, :kw, Dict())
        dzdy_v = deriv_name(g.ctx[:loss], varname(nd))
        kw[fld_name] = dzdy_v
        push!(dg, m_nd)
    end
end

function rev_step!(g::ExGraph, dg::ExGraph, nd::ExNode{:ref})
    ex = getexpr(nd)
    base_name = ex.args[1]
    base_val = getvalue(g[base_name])
    idx = ex.args[2]
    dzdx_v = deriv_name(g.ctx[:loss], base_name)
    if !haskey(dg, dzdx_v)
        if isa(base_val, Tuple)
            tuple_ex = Expr(:tuple, (:_ for i=1:length(base_val))...)
            push!(dg, :tuple, dzdx_v, tuple_ex)
        elseif isa(base_val, AbstractArray)
            # experimental: trying to support array literals by converting to tuples
            tuple_ex = Expr(:tuple, (:_ for i=1:length(base_val))...)
            push!(dg, :tuple, dzdx_v, tuple_ex)
        else
            error("Currently only indexing of tuples is supported, " *
                  "but got $(typeof(base_val))")
        end
    end
    dzdx_nd = dg[dzdx_v]
    dzdx_ex = getexpr(dzdx_nd)
    dzdy_v = deriv_name(g.ctx[:loss], varname(nd))
    dzdx_ex.args[idx] = dzdy_v
end

function rev_step!(g::ExGraph, dg::ExGraph, nd::ExNode{:tuple})
    z = g.ctx[:loss]
    dzdy_v = deriv_name(z, varname(nd))
    dzdy_nd = dg[dzdy_v]
    dzdy_ex = getexpr(dzdy_nd)
    for (i, x) in enumerate(dependencies(nd))
        # map x derivative directly to the component in a tuple
        # remove_unused() should then remove tuple altogether
        # alternative way would be to generate expression like :(dz!dx = dz!dt[i])
        dzdx_v = deriv_name(z, x)
        dzdx_ex = dzdy_ex.args[i]
        parse!(dg, :($dzdx_v = $dzdx_ex))
    end
end

function rev_step!(g::ExGraph, dg::ExGraph, nd::ExNode{:call})
    y = varname(nd)
    z = g.ctx[:loss]
    dzdy_v = deriv_name(z, y)
    cg = cat(g, dg)
    ex = getexpr_kw(nd)
    dep_vals = [x in g ? getvalue(g[x]) : Core.eval(g.ctx[:mod], x)
                for x in dependencies(nd)]
    for (i, x) in enumerate(dependencies(nd))
        if !in(x, g)
            # global constant
            continue
        end
        xnd = g[x]
        if isa(xnd, ExNode{:constant})
            # don't clog dg with unnesessary derivs
            continue
        end
        dydx = deriv(ex, dep_vals, i)
        dzdx = subs(dydx, Dict(:ds => dzdy_v))
        # dzdx = expand_const(cg, dzdx) |> simplify
        dzdx_v = deriv_name(z, x)
        if haskey(dg, dzdx_v)
            extend_deriv!(dg, dzdx_v, dzdx)
        else
            parse!(dg, :($dzdx_v = $dzdx))
        end
    end
end

function rev_step!(g::ExGraph, dg::ExGraph, nd::ExNode{:bcast})
    y = varname(nd)
    z = g.ctx[:loss]
    dzdy_v = deriv_name(z, y)
    cg = cat(g, dg)
    ex = bcast_to_call(getexpr_kw(nd))
    # assuming only array-like dependencies
    dep_vals = [x in g ? getvalue(g[x])[1] : Core.eval(g.ctx[:mod], x)[1]
                for x in dependencies(nd)]
    for (i, x) in enumerate(dependencies(nd))
        if !in(x, g)
            # global constant
            continue
        end
        xnd = g[x]
        if isa(xnd, ExNode{:constant})
            # don't clog dg with unnesessary derivs
            continue
        end
        dydx = deriv(ex, dep_vals, i)
        dzdx = subs(dydx, Dict(:ds => dzdy_v))
        dzdx = calls_to_bcast(dzdx)
        # dzdx = expand_const(cg, dzdx) |> simplify
        dzdx_v = deriv_name(z, x)
        if haskey(dg, dzdx_v)
            extend_deriv!(dg, dzdx_v, dzdx)
        else
            parse!(dg, :($dzdx_v = $dzdx))
        end
    end
end

function reverse_pass!(g::ExGraph)
    z = @get_or_create(g.ctx, :loss, varname(g.tape[end]))
    dzdz_var = deriv_name(z, z)
    seed = @get_or_create(g.ctx, :seed, 1.0)
    g.ctx[:method] = :parse  # forcing :parse method for derivative graph
    dg = ExGraph(:($dzdz_var = $seed); ctx=g.ctx)
    for nd in reverse(g.tape)
        rev_step!(g, dg, nd)
    end
    outvars = [deriv_name(z, varname(nd)) for nd in g.tape if isa(nd, ExNode{:input})]
    return fuse_assigned(dg; outvars=outvars)
end

function _xdiff(g::AbstractExGraph)
    g = forward_pass!(g)
    dg = reverse_pass!(g)
    return g, dg
end

"""
    xdiff(ex; ctx=Dict(), inputs...)

Differentiate scalar-valued expression w.r.t. its inputs,
return expression for the derivatives.

    ex = :(sum(w * x .- y))
    dex = xdiff(ex; w=rand(2,3), x=rand(3,4), y=rand(2))

`xdiff()` also accepts a context `ctx::Dict{Any,Any}` which can be used to pass options
and extract additional information. Some options include:

 * `codegen::Espresso.CodeGen` - code generator used for derivative expression;
                                 valid values include `VecotorCodeGen()`, `BufCodeGen()`
                                 and `CuCodeGen()`
"""
function xdiff(ex; ctx=Dict(), inputs...)
    ctx = to_context(ctx)
    codegen = @get_or_create(ctx, :codegen, autoselect_codegen(inputs))
    ctx[:bitness] = sizeof(codegen.eltyp) * 8
    # inputs = unconvert_cuarrays(inputs)
    g = ExGraph(ex; ctx=ctx, inputs...)
    g, dg = _xdiff(g)
    rg = cat(g, dg)
    outvars = [deriv_name(g.ctx[:loss], var) for (var, _) in inputs]
    outvars = pushfirst!(outvars, varname(g[end]))
    push!(rg, :tuple, Espresso.genname(), Expr(:tuple, outvars...))
    rg = topsort(rg)
    infer_deriv_size!(rg) # do we still need this? can we replace rsizes with actual sizes?
    evaluate!(rg)
    return generate_code(codegen, rg)
end

function xdiff_track(f::Function; ctx=Dict(), inputs...)
    ctx = to_context(ctx)
    codegen = @get_or_create(ctx, :codegen, autoselect_codegen(inputs))
    ctx[:bitness] = sizeof(codegen.eltyp) * 8
    g = ExGraph(; ctx=ctx, inputs...)
    og = swap_default_graph!(g)
    tracked_vals = [tracked_val(g, var, val) for (var, val) in inputs]
    f(tracked_vals...)
    swap_default_graph!(og)
    g, dg = _xdiff(g)
    rg = cat(g, dg)
    outvars = pushfirst!([deriv_name(g.ctx[:loss], var) for (var, _) in inputs], varname(g[end]))
    push!(rg, :tuple, Espresso.genname(), Expr(:tuple, outvars...))
    rg = topsort(rg)
    infer_deriv_size!(rg) # do we still need this? can we replace rsizes with actual sizes?
    evaluate!(rg)
    return generate_code(codegen, rg)
end

function xdiff_parse(f::Function; ctx=Dict(), inputs...)
    ctx = to_context(ctx)
    types = ([typeof(val) for (name, val) in inputs]...,)
    args, ex = funexpr(f, types)
    ex = sanitize(ex)
    ctx[:mod] = Espresso.func_mod(f)
    return xdiff(ex; ctx=ctx, inputs...)
end

"""
    df = xdiff(f; ctx=Dict(), inputs...)

Differentiate scalar-valued function w.r.t. its inputs, return derivative function.

    loss(w, x, y) = sum(w * x .- y)
    w = rand(2,3); x = rand(3,4); y = rand(2)
    dloss = xdiff(loss; w=w, x=x, y=y)
    val, dw, dx, dy = dloss(w, x, y)

See also `xgrad()` for a more dynamic API.
"""
function xdiff(f::Function; ctx=Dict(), inputs...)
    ctx = to_context(ctx)
    method = get(ctx, :method, :parse)
    if method == :track
        dex = xdiff_track(f; ctx=ctx, inputs...)
    elseif method == :parse
        dex = xdiff_parse(f; ctx=ctx, inputs...)
    else
        error("Method $method is not supported")
    end
    ctx[:dex] = dex
    mod = get(ctx, :mod, @__MODULE__)
    name = Espresso.genname("$(func_name(f))_deriv_")
    types = ([typeof(val) for (_, val) in inputs]...,)
    args = get_or_generate_argnames(f, types)
    typed_args = [:($a::$t) for (a, t) in zip(args, map(top_type, types))]
    # function with additional argument `mem`
    fn_ex_mem = make_func_expr(name, [typed_args; :mem], [], dex)
    fn = Core.eval(mod, fn_ex_mem)
    # function with kw argument `mem=Dict()`
    fn_ex_mem_kw = make_func_expr(name, typed_args, [:mem => :(Dict{Any,Any}())], dex)
    Core.eval(mod, fn_ex_mem_kw)
    return fn
end


text:
function return_cache(::typeof(*),a::AbstractArray{<:Number},b::AbstractArray{<:Number})
  c = a*b
  CachedArray(c)
end

@inline function evaluate!(cache,::typeof(*),a::AbstractMatrix{<:Number},b::AbstractVector{<:Number})
  m = axes(a,1)
  setaxes!(cache,(m,))
  c = cache.array
  mul!(c,a,b)
  c
end

@inline function evaluate!(cache,::typeof(*),a::AbstractMatrix{<:Number},b::AbstractMatrix{<:Number})
  m = axes(a,1)
  n = axes(b,2)
  setaxes!(cache,(m,n))
  c = cache.array
  mul!(c,a,b)
  c
end

struct MulAddMap{T} <: Map
  α::T
  β::T
end

function return_cache(k::MulAddMap,a,b,c)
  d = a*b+c
  CachedArray(d)
end

@inline function evaluate!(cache,k::MulAddMap,a,b,c)
  setaxes!(cache,axes(c))
  d = cache.array
  copyto!(d,c)
  mul!(d,a,b,k.α,k.β)
  d
end


text:
module TestExamplesUpgradeToIXF
using LiterateTest.AssertAsTest: @assert
if Base.JLOptions().depwarn != 2
    include("../examples/upgrade-to-ixf.jl")
end
end  # module


text:
# Language interop in Julia
using PyCall
using Conda

py"""
import numpy as np

def myfunc(x):
    return np.sin(np.pi * x)
"""

myfunc = py"myfunc"

myfunc(10.0)

# Add python packages to the Python env
Conda.add("scipy")

# Call Python functions from inside Julia,
# even with Julia functions as arguments
optim = pyimport("scipy.optimize")
optim.newton(x -> cos(x) - x, 1)

max_stars_repo_name: JamieMair/intro-to-julia-talk

max_issues_repo_name: JamieMair/intro-to-julia-talk

max_forks_repo_name: JamieMair/intro-to-julia-talk


text:
# This file was generated by the Julia Swagger Code Generator
# Do not modify this file directly. Modify the swagger specification instead.

mutable struct VMScaleSetConvertToSinglePlacementGroupInput <: SwaggerModel
    activePlacementGroupId::Any # spec type: Union{ Nothing, String } # spec name: activePlacementGroupId

    function VMScaleSetConvertToSinglePlacementGroupInput(;activePlacementGroupId=nothing)
        o = new()
        validate_property(VMScaleSetConvertToSinglePlacementGroupInput, Symbol("activePlacementGroupId"), activePlacementGroupId)
        setfield!(o, Symbol("activePlacementGroupId"), activePlacementGroupId)
        o
    end
end # type VMScaleSetConvertToSinglePlacementGroupInput

const _property_map_VMScaleSetConvertToSinglePlacementGroupInput = Dict{Symbol,Symbol}(Symbol("activePlacementGroupId")=>Symbol("activePlacementGroupId"))
const _property_types_VMScaleSetConvertToSinglePlacementGroupInput = Dict{Symbol,String}(Symbol("activePlacementGroupId")=>"String")
Base.propertynames(::Type{ VMScaleSetConvertToSinglePlacementGroupInput }) = collect(keys(_property_map_VMScaleSetConvertToSinglePlacementGroupInput))
Swagger.property_type(::Type{ VMScaleSetConvertToSinglePlacementGroupInput }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_VMScaleSetConvertToSinglePlacementGroupInput[name]))}
Swagger.field_name(::Type{ VMScaleSetConvertToSinglePlacementGroupInput }, property_name::Symbol) =  _property_map_VMScaleSetConvertToSinglePlacementGroupInput[property_name]

function check_required(o::VMScaleSetConvertToSinglePlacementGroupInput)
    true
end

function validate_property(::Type{ VMScaleSetConvertToSinglePlacementGroupInput }, name::Symbol, val)
end


text:
#=
An example implementaion of an Autoencoder inspired by
https://github.com/alrojo/tensorflow-tutorial/blob/master/lab5_AE/lab5_AE.ipynb
This example trains an undercomplete autoencoder with a bottleneck of three hidden neurons, the activation of which can be used for a low-dimensional visualization of the 768-dimensional MNIST data.

The plotting functionality require Plots.jl and a backend, e.g., PyPlot.jl or GR.jl
`Pkg.add("Plots")`
`Pkg.add("PyPlot")` or `Pkg.add("GR")`
=#
using MNIST, Plots, TensorFlow, ValueHistories

function showdigit(x; kwargs...)
    d = reshape(x,28,28)
    heatmap(d; yflip=true, color=:grays, kwargs...)
end

function printtime(t0)
    dt = time()-t0
    "$(Int(dt÷60)):$(round(Int,dt % 60))"
end

num_features = 28^2

include(Pkg.dir("TensorFlow","examples","mnist_loader.jl"))
include(Pkg.dir("TensorFlow","src","layers","fully_connected.jl"))
loader = DataLoader()
session = Session(Graph())

x = placeholder(Float32, shape=[-1, num_features], name="x_pl")#/255
variable_scope("ae", initializer=Distributions.Normal(0, .01)) do
    global W1 = get_variable("weights1", [num_features, 512], Float32)
    global W2 = get_variable("weights2", [512, 3], Float32)
    global W1o = get_variable("weights1o", [512, num_features], Float32)
    global W2o = get_variable("weights2o", [3, 512], Float32)
end

B1 = Variable(0.01ones(Float32,512), name="bias1")
B2 = Variable(0.01ones(Float32,3), name="bias2")
B1o = Variable(0.01ones(Float32,num_features), name="bias1o")
B2o = Variable(0.01ones(Float32,512), name="bias2o")

# These lines define the network structure
l_enc = x*W1 + B1 |> nn.relu
l_z = l_enc*W2 + B2
l_dec = l_z*W2o + B2o |> nn.relu
l_out = l_dec*W1o + B1o |> nn.sigmoid

# loss_CE = -reduce_sum(x.*log(l_out+1e-8) + (1-x).*log(1-l_out+1e-8))
loss_MSE = reduce_mean((x-l_out).^2)
train_step = train.minimize(train.AdamOptimizer(5e-4), loss_MSE)

run(session, global_variables_initializer())
saver = train.Saver()
checkpoint_path = mktempdir()

summary_writer = train.SummaryWriter(checkpoint_path)

testx, testy = load_test_set()
testx ./= 255
testy = sum(testy.*(0:9)',2)[:]

f = plot(legend=false)
history = MVHistory()
@time begin
    t0 = time()
    for epoch = 1:10_000
        batch,_ = next_batch(loader, 64)
        batch ./= 255 # Normalize input to [0,1]
        if epoch%500 == 0
            train_loss = run(session, loss_MSE, Dict(x=>batch))
            val_loss, center = run(session, [loss_MSE, l_z], Dict(x=>testx))
            push!(history,:loss_train, epoch, train_loss)
            push!(history,:loss_val, epoch, val_loss)
            plot(history, reuse=true)
            scatter3d(center[:,1],center[:,2],center[:,3], zcolor = testy, legend=false, title="Latent space", reuse=true)
            info("step $epoch, training loss $train_loss, time taken: $(printtime(t0))")
            train.save(saver, session, joinpath(checkpoint_path, "ae_mnist"), global_step=epoch)
        end
        run(session, train_step, Dict(x=>batch))
    end
end

test_loss, center, reconstruction = run(session, [loss_MSE, l_z, l_out], Dict(x=>testx))
info("test accuracy $test_loss")

# Plot som example reconstructions
offset = 0
plot(layout=(2,2*2))
for i = 1:4
    d = reshape(testx[i+offset,:],28,28)
    heatmap!(d, subplot=i,yflip=true, color=:grays, title="Input", colorbar = false)
    d = reshape(reconstruction[i+offset,:],28,28)
    heatmap!(d, subplot=i+4,yflip=true, color=:grays, title="Reconstruction", colorbar = false)
end

max_stars_repo_name: octonion/tensorflow

max_issues_repo_name: octonion/tensorflow

max_forks_repo_name: octonion/tensorflow


text:
__precompile__()
module Remove_response_obspy

using PyCall, SeisIO, Suppressor

export remove_response_obspy, remove_response_obspy!

"""
remove_response_obspy(S::SeisData, xf::String; pre_filt::NTuple{4,Float64}=(0.001, 0.002, 10.0, 20.0), zeropadlen::Float64 = 30*60, output::String="VEL")
remove Instrumental response with obspy.
Currently available only one channel.
This function will be replaced by julia original function

#Input
 - zeropadlen: [min] zeropad at the side of dataset to avoid edge effect

"""
function remove_response_obspy!(S::SeisData, xf::String; pre_filt::NTuple{4,Float64}=(0.001, 0.002, 10.0, 20.0), zeropadlen::Float64 = 30*60.0, output::String="VEL")

    @suppress begin
    Stream          = pyimport_conda("obspy.core.stream", "Stream")
    Trace           = pyimport_conda("obspy.core.trace", "Trace")
    read_inventory  = pyimport_conda("obspy", "read_inventory")
    UTCDateTime     = pyimport_conda("obspy.core.utcdatetime", "UTCDateTime")

    for i = 1:length(S.x)
        #add zero at the edge of data to avoid edge effect
        numofzeropad = round(Int64, zeropadlen * S.fs[i])
        #z1 = zeros(numofzeropad)
        #---modify 2019/06/11: add fliped signal to avoid edge effect
        #SeisIO.taper!(S)

        if length(S.x[i]) < numofzeropad
            numofzeropad = round(Int64, length(S.x[i])/2)
        end

        zl = S.x[i][numofzeropad:-1:1]
        zr = S.x[i][end:-1:end-numofzeropad+1]
        #z1 = zeros(numofzeropad)
        #x_withpad = vcat(z1,S.x[i],z1)
        x_withpad = vcat(zl,S.x[i],zr)
        trace=Trace.Trace()
        #trace.data = S.x[i]
        trace.data = x_withpad
        trace.stats.sampling_rate = S.fs[i]
        trace.stats.delta = 1.0./ S.fs[i]
        trace.stats.starttime = UTCDateTime.UTCDateTime(string(u2d(S.t[i][1,2]*1e-6)))
        str1 = split(S.id[i], ".")
        trace.stats.network = str1[1]
        trace.stats.station=  str1[2]
        trace.stats.location= str1[3]
        trace.stats.channel=  str1[4]

        stream = Stream.Stream(traces=trace)
        #read stationXML
        inv = read_inventory.read_inventory(xf)
        #stresm.remove_sensitivity(inventory=inv)
        stream.remove_response(inventory=inv, output=output, pre_filt=pre_filt, plot=false)
        x_withpad_removed =  stream.traces[1].data
        S.x[i] = x_withpad_removed[numofzeropad+1:end-numofzeropad]
        #add note
        SeisIO.note!(S[i], "remove_response_obspy!, pre_filt=$pre_filt")
    end

    end
    return nothing
end

function remove_response_obspy(S::SeisData, xf::String; pre_filt::NTuple{4,Float64}=(0.001, 0.002, 10.0, 20.0), zeropadlen::Float64 = 30*60.0, output::String="VEL")
  U = deepcopy(S)
  remove_response_obspy!(U, pre_filt=pre_filt,  zeropadlen, output=output)
  return U
end

end


text:
# ---
# title: 51. N-Queens
# id: problem51
# author: Tian Jun
# date: 2020-10-31
# difficulty: Hard
# categories: Backtracking
# link: <https://leetcode.com/problems/n-queens/description/>
# hidden: true
# ---
#
# The **n-queens** puzzle is the problem of placing `n` queens on an `n x n`
# chessboard such that no two queens attack each other.
#
# Given an integer `n`, return _all distinct solutions to the **n-queens
# puzzle**_.
#
# Each solution contains a distinct board configuration of the n-queens'
# placement, where `'Q'` and `'.'` both indicate a queen and an empty space,
# respectively.
#
#
#
# **Example 1:**
#
# ![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
#
#
#
#     Input: n = 4
#     Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
#     Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above
#
#
# **Example 2:**
#
#
#
#     Input: n = 1
#     Output: [["Q"]]
#
#
#
#
# **Constraints:**
#
#   * `1 <= n <= 9`
#
#
## @lc code=start
using LeetCode

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


text:
module NumpyArrayInterface

import ArrayInterface

export NpyDescrField, NpyDescr, npy_descr, npy_pointer, npy_readonly, npy_shape, npy_strides, npy_info, npy_mask, npy_array

struct NpyDescrField
    name::String
    descr::Union{String,Vector{NpyDescrField}}
    basic_name::String
    shape::Union{Nothing,Vector{Int}}
end
NpyDescrField(name, descr; basic_name=name, shape=nothing) = NpyDescrField(name, descr, basic_name, shape)

function Base.show(io::IO, d::NpyDescrField)
    show(io, typeof(d))
    print(io, "(")
    show(io, d.name)
    print(io, ", ")
    show(io, d.descr)
    if d.basic_name != d.name
        print(io, ", basic_name=")
        show(io, d.basic_name)
    end
    if d.shape !== nothing
        print(io, ", shape=")
        show(io, d.shape)
    end
    print(io, ")")
end

struct NpyDescr
    typestr::String
    descr::Union{Nothing,Vector{NpyDescrField}}
end
NpyDescr(typestr) = NpyDescr(typestr, nothing)

function Base.show(io::IO, d::NpyDescr)
    show(io, typeof(d))
    print(io, "(")
    show(io, d.typestr)
    if d.descr !== nothing
        print(io, ", ")
        show(io, d.descr)
    end
    print(io, ")")
end

const ENDIAN_CHAR = ENDIAN_BOM == 0x04030201 ? '<' : '>'

const TYPE_TO_DESCR = Dict(
    Bool => NpyDescr("$(ENDIAN_CHAR)b$(sizeof(Bool))"),
    Int8 => NpyDescr("$(ENDIAN_CHAR)i1"),
    Int16 => NpyDescr("$(ENDIAN_CHAR)i2"),
    Int32 => NpyDescr("$(ENDIAN_CHAR)i4"),
    Int64 => NpyDescr("$(ENDIAN_CHAR)i8"),
    UInt8 => NpyDescr("$(ENDIAN_CHAR)u1"),
    UInt16 => NpyDescr("$(ENDIAN_CHAR)u2"),
    UInt32 => NpyDescr("$(ENDIAN_CHAR)u4"),
    UInt64 => NpyDescr("$(ENDIAN_CHAR)u8"),
    Float16 => NpyDescr("$(ENDIAN_CHAR)f2"),
    Float32 => NpyDescr("$(ENDIAN_CHAR)f4"),
    Float64 => NpyDescr("$(ENDIAN_CHAR)f8"),
    Complex{Float16} => NpyDescr("$(ENDIAN_CHAR)c4"),
    Complex{Float32} => NpyDescr("$(ENDIAN_CHAR)c8"),
    Complex{Float64} => NpyDescr("$(ENDIAN_CHAR)c16"),
)

for (T,d) in TYPE_TO_DESCR
    @eval npy_descr(::Type{$T}) = $d
end
npy_descr(T::DataType) = get!(TYPE_TO_DESCR, T) do
    if isstructtype(T)
        n = fieldcount(T)
        if n == 0
            NpyDescr("|V$(sizeof(T))")
        else
            flds = NpyDescrField[]
            for i = 1:n
                nm = fieldname(T, i)
                tp = fieldtype(T, i)
                d = npy_descr(tp)
                name = nm isa Integer ? "f$(nm-1)" : string(nm)
                descr = d.descr === nothing ? d.typestr : d.descr
                push!(flds, NpyDescrField(name, descr))
                off0 = fieldoffset(T, i) + sizeof(tp)
                off1 = i == n ? sizeof(T) : fieldoffset(T, i + 1)
                d = off1 - off0
                @assert d ≥ 0
                d > 0 && push!(flds, NpyDescrField("", "|V$(d)"))
            end
            NpyDescr("|V$(sizeof(T))", flds)
        end
    elseif isbitstype(T)
        NpyDescr("|V$(sizeof(T))")
    else
        @assert false
    end
end

npy_array(x) = nothing
npy_array(x::StridedArray) = x  # TODO

npy_descr(x) = npy_descr(eltype(x))

npy_shape(x) = Tuple{Vararg{Int}}(size(x))

npy_strides(x) = Tuple{Vararg{Int}}(strides(x) .* Base.aligned_sizeof(eltype(x)))

npy_pointer(x) = Ptr{Cvoid}(Base.unsafe_convert(Ptr{eltype(x)}, x))

npy_readonly(x) = !ArrayInterface.can_setindex(typeof(x))

npy_mask(x) = nothing

npy_info(x) = if (a = npy_array(x)) !== nothing
    (
        array = a,
        pointer = npy_pointer(a),
        readonly = npy_readonly(a),
        shape = npy_shape(a),
        descr = npy_descr(a),
        strides = npy_strides(a),
        mask = npy_mask(a),
    )
end

end # module


text:
# The informatio contained in `org-entities'

struct EntityData
    latex::String
    math::Bool
    html::String
    ascii::String
    latin1::String
    utf8::String
end

function Base.show(io::IO, ent::EntityData)
    function rep(field, name, color=:cyan, last=false)
        printstyled(io, getproperty(ent, field); color)
        print(io, ": $name")
        if !last print(io, ", ") end
    end
    if ent.ascii == ent.latin1 == ent.utf8
        rep(:utf8, "Unicode/Latin1/ASCII")
        rep(:latex, "LaTeX", :magenta)
        rep(:html, "HTML", :green, true)
    elseif ent.ascii == ent.latin1 == ent.utf8
        rep(:utf8, "Unicode/Latin1")
        rep(:ascii, "ASCII")
        rep(:latex, "LaTeX")
        rep(:html, "HTML", :green, true)
    else
        rep(:utf8, "Unicode")
        rep(:latin1, "Latin1")
        rep(:ascii, "ASCII")
        rep(:latex, "LaTeX", :magenta)
        rep(:html, "HTML", :green, true)
    end
    if ent.math
        printstyled(io, " (math)", color=:yellow)
    end
end

# (mapconcat
#  (lambda (entity)
#    (if (stringp entity)
#        (concat "# " entity)
#      (setf (caddr entity) (if (caddr entity) "true" "false"))
#      (replace-regexp-in-string "\\$" "\\\\$"
#        (apply #'format "%S => EntityData(%S, %s, %S, %S, %S, %S)" entity))))
#  org-entities "\n")

const Entities = Dict(
    # * Letters
    # ** Latin
    "Agrave" => EntityData("\\`{A}", true, "À", "A", "À", "À"),
    "agrave" => EntityData("\\`{a}", true, "à", "a", "à", "à"),
    "Aacute" => EntityData("\\'{A}", true, "Á", "A", "Á", "Á"),
    "aacute" => EntityData("\\'{a}", true, "á", "a", "á", "á"),
    "Acirc" => EntityData("\\^{A}", true, "Â", "A", "Â", "Â"),
    "acirc" => EntityData("\\^{a}", true, "â", "a", "â", "â"),
    "Amacr" => EntityData("\\bar{A}", true, "Ā", "A", "Ã", "Ã"),
    "amacr" => EntityData("\\bar{a}", true, "ā", "a", "ã", "ã"),
    "Atilde" => EntityData("\\~{A}", true, "Ã", "A", "Ã", "Ã"),
    "atilde" => EntityData("\\~{a}", true, "ã", "a", "ã", "ã"),
    "Auml" => EntityData("\\\"{A}", true, "Ä", "Ae", "Ä", "Ä"),
    "auml" => EntityData("\\\"{a}", true, "ä", "ae", "ä", "ä"),
    "Aring" => EntityData("\\AA{}", true, "Å", "A", "Å", "Å"),
    "AA" => EntityData("\\AA{}", true, "Å", "A", "Å", "Å"),
    "aring" => EntityData("\\aa{}", true, "å", "a", "å", "å"),
    "AElig" => EntityData("\\AE{}", true, "Æ", "AE", "Æ", "Æ"),
    "aelig" => EntityData("\\ae{}", true, "æ", "ae", "æ", "æ"),
    "Ccedil" => EntityData("\\c{C}", true, "Ç", "C", "Ç", "Ç"),
    "ccedil" => EntityData("\\c{c}", true, "ç", "c", "ç", "ç"),
    "Egrave" => EntityData("\\`{E}", true, "È", "E", "È", "È"),
    "egrave" => EntityData("\\`{e}", true, "è", "e", "è", "è"),
    "Eacute" => EntityData("\\'{E}", true, "É", "E", "É", "É"),
    "eacute" => EntityData("\\'{e}", true, "é", "e", "é", "é"),
    "Ecirc" => EntityData("\\^{E}", true, "Ê", "E", "Ê", "Ê"),
    "ecirc" => EntityData("\\^{e}", true, "ê", "e", "ê", "ê"),
    "Euml" => EntityData("\\\"{E}", true, "Ë", "E", "Ë", "Ë"),
    "euml" => EntityData("\\\"{e}", true, "ë", "e", "ë", "ë"),
    "Igrave" => EntityData("\\`{I}", true, "Ì", "I", "Ì", "Ì"),
    "igrave" => EntityData("\\`{i}", true, "ì", "i", "ì", "ì"),
    "Iacute" => EntityData("\\'{I}", true, "Í", "I", "Í", "Í"),
    "iacute" => EntityData("\\'{i}", true, "í", "i", "í", "í"),
    "Idot" => EntityData("\\.{I}", true, "&idot;", "I", "İ", "İ"),
    "inodot" => EntityData("\\i", true, "ı", "i", "ı", "ı"),
    "Icirc" => EntityData("\\^{I}", true, "Î", "I", "Î", "Î"),
    "icirc" => EntityData("\\^{i}", true, "î", "i", "î", "î"),
    "Iuml" => EntityData("\\\"{I}", true, "Ï", "I", "Ï", "Ï"),
    "iuml" => EntityData("\\\"{i}", true, "ï", "i", "ï", "ï"),
    "Ntilde" => EntityData("\\~{N}", true, "Ñ", "N", "Ñ", "Ñ"),
    "ntilde" => EntityData("\\~{n}", true, "ñ", "n", "ñ", "ñ"),
    "Ograve" => EntityData("\\`{O}", true, "Ò", "O", "Ò", "Ò"),
    "ograve" => EntityData("\\`{o}", true, "ò", "o", "ò", "ò"),
    "Oacute" => EntityData("\\'{O}", true, "Ó", "O", "Ó", "Ó"),
    "oacute" => EntityData("\\'{o}", true, "ó", "o", "ó", "ó"),
    "Ocirc" => EntityData("\\^{O}", true, "Ô", "O", "Ô", "Ô"),
    "ocirc" => EntityData("\\^{o}", true, "ô", "o", "ô", "ô"),
    "Otilde" => EntityData("\\~{O}", true, "Õ", "O", "Õ", "Õ"),
    "otilde" => EntityData("\\~{o}", true, "õ", "o", "õ", "õ"),
    "Ouml" => EntityData("\\\"{O}", true, "Ö", "Oe", "Ö", "Ö"),
    "ouml" => EntityData("\\\"{o}", true, "ö", "oe", "ö", "ö"),
    "Oslash" => EntityData("\\O", true, "Ø", "O", "Ø", "Ø"),
    "oslash" => EntityData("\\o{}", true, "ø", "o", "ø", "ø"),
    "OElig" => EntityData("\\OE{}", true, "Œ", "OE", "OE", "Œ"),
    "oelig" => EntityData("\\oe{}", true, "œ", "oe", "oe", "œ"),
    "Scaron" => EntityData("\\v{S}", true, "Š", "S", "S", "Š"),
    "scaron" => EntityData("\\v{s}", true, "š", "s", "s", "š"),
    "szlig" => EntityData("\\ss{}", true, "ß", "ss", "ß", "ß"),
    "Ugrave" => EntityData("\\`{U}", true, "Ù", "U", "Ù", "Ù"),
    "ugrave" => EntityData("\\`{u}", true, "ù", "u", "ù", "ù"),
    "Uacute" => EntityData("\\'{U}", true, "Ú", "U", "Ú", "Ú"),
    "uacute" => EntityData("\\'{u}", true, "ú", "u", "ú", "ú"),
    "Ucirc" => EntityData("\\^{U}", true, "Û", "U", "Û", "Û"),
    "ucirc" => EntityData("\\^{u}", true, "û", "u", "û", "û"),
    "Uuml" => EntityData("\\\"{U}", true, "Ü", "Ue", "Ü", "Ü"),
    "uuml" => EntityData("\\\"{u}", true, "ü", "ue", "ü", "ü"),
    "Yacute" => EntityData("\\'{Y}", true, "Ý", "Y", "Ý", "Ý"),
    "yacute" => EntityData("\\'{y}", true, "ý", "y", "ý", "ý"),
    "Yuml" => EntityData("\\\"{Y}", true, "Ÿ", "Y", "Y", "Ÿ"),
    "yuml" => EntityData("\\\"{y}", true, "ÿ", "y", "ÿ", "ÿ"),
    # ** Latin (special face)
    "fnof" => EntityData("\\textit{f}", true, "ƒ", "f", "f", "ƒ"),
    "real" => EntityData("\\Re", true, "ℜ", "R", "R", "ℜ"),
    "image" => EntityData("\\Im", true, "ℑ", "I", "I", "ℑ"),
    "weierp" => EntityData("\\wp", true, "℘", "P", "P", "℘"),
    "ell" => EntityData("\\ell", true, "ℓ", "ell", "ell", "ℓ"),
    "imath" => EntityData("\\imath", true, "ı", "[dotless i]", "dotless i", "ı"),
    "jmath" => EntityData("\\jmath", true, "ȷ", "[dotless j]", "dotless j", "ȷ"),
    # ** Greek
    "Alpha" => EntityData("A", true, "Α", "Alpha", "Alpha", "Α"),
    "alpha" => EntityData("\\alpha", true, "α", "alpha", "alpha", "α"),
    "Beta" => EntityData("B", true, "Β", "Beta", "Beta", "Β"),
    "beta" => EntityData("\\beta", true, "β", "beta", "beta", "β"),
    "Gamma" => EntityData("\\Gamma", true, "Γ", "Gamma", "Gamma", "Γ"),
    "gamma" => EntityData("\\gamma", true, "γ", "gamma", "gamma", "γ"),
    "Delta" => EntityData("\\Delta", true, "Δ", "Delta", "Delta", "Δ"),
    "delta" => EntityData("\\delta", true, "δ", "delta", "delta", "δ"),
    "Epsilon" => EntityData("E", true, "Ε", "Epsilon", "Epsilon", "Ε"),
    "epsilon" => EntityData("\\epsilon", true, "ε", "epsilon", "epsilon", "ε"),
    "varepsilon" => EntityData("\\varepsilon", true, "ε", "varepsilon", "varepsilon", "ε"),
    "Zeta" => EntityData("Z", true, "Ζ", "Zeta", "Zeta", "Ζ"),
    "zeta" => EntityData("\\zeta", true, "ζ", "zeta", "zeta", "ζ"),
    "Eta" => EntityData("H", true, "Η", "Eta", "Eta", "Η"),
    "eta" => EntityData("\\eta", true, "η", "eta", "eta", "η"),
    "Theta" => EntityData("\\Theta", true, "Θ", "Theta", "Theta", "Θ"),
    "theta" => EntityData("\\theta", true, "θ", "theta", "theta", "θ"),
    "thetasym" => EntityData("\\vartheta", true, "ϑ", "theta", "theta", "ϑ"),
    "vartheta" => EntityData("\\vartheta", true, "ϑ", "theta", "theta", "ϑ"),
    "Iota" => EntityData("I", true, "Ι", "Iota", "Iota", "Ι"),
    "iota" => EntityData("\\iota", true, "ι", "iota", "iota", "ι"),
    "Kappa" => EntityData("K", true, "Κ", "Kappa", "Kappa", "Κ"),
    "kappa" => EntityData("\\kappa", true, "κ", "kappa", "kappa", "κ"),
    "Lambda" => EntityData("\\Lambda", true, "Λ", "Lambda", "Lambda", "Λ"),
    "lambda" => EntityData("\\lambda", true, "λ", "lambda", "lambda", "λ"),
    "Mu" => EntityData("M", true, "Μ", "Mu", "Mu", "Μ"),
    "mu" => EntityData("\\mu", true, "μ", "mu", "mu", "μ"),
    "nu" => EntityData("\\nu", true, "ν", "nu", "nu", "ν"),
    "Nu" => EntityData("N", true, "Ν", "Nu", "Nu", "Ν"),
    "Xi" => EntityData("\\Xi", true, "Ξ", "Xi", "Xi", "Ξ"),
    "xi" => EntityData("\\xi", true, "ξ", "xi", "xi", "ξ"),
    "Omicron" => EntityData("O", true, "Ο", "Omicron", "Omicron", "Ο"),
    "omicron" => EntityData("\\textit{o}", true, "ο", "omicron", "omicron", "ο"),
    "Pi" => EntityData("\\Pi", true, "Π", "Pi", "Pi", "Π"),
    "pi" => EntityData("\\pi", true, "π", "pi", "pi", "π"),
    "Rho" => EntityData("P", true, "Ρ", "Rho", "Rho", "Ρ"),
    "rho" => EntityData("\\rho", true, "ρ", "rho", "rho", "ρ"),
    "Sigma" => EntityData("\\Sigma", true, "Σ", "Sigma", "Sigma", "Σ"),
    "sigma" => EntityData("\\sigma", true, "σ", "sigma", "sigma", "σ"),
    "sigmaf" => EntityData("\\varsigma", true, "ς", "sigmaf", "sigmaf", "ς"),
    "varsigma" => EntityData("\\varsigma", true, "ς", "varsigma", "varsigma", "ς"),
    "Tau" => EntityData("T", true, "Τ", "Tau", "Tau", "Τ"),
    "Upsilon" => EntityData("\\Upsilon", true, "Υ", "Upsilon", "Upsilon", "Υ"),
    "upsih" => EntityData("\\Upsilon", true, "ϒ", "upsilon", "upsilon", "ϒ"),
    "upsilon" => EntityData("\\upsilon", true, "υ", "upsilon", "upsilon", "υ"),
    "Phi" => EntityData("\\Phi", true, "Φ", "Phi", "Phi", "Φ"),
    "phi" => EntityData("\\phi", true, "φ", "phi", "phi", "ɸ"),
    "varphi" => EntityData("\\varphi", true, "ϕ", "varphi", "varphi", "φ"),
    "Chi" => EntityData("X", true, "Χ", "Chi", "Chi", "Χ"),
    "chi" => EntityData("\\chi", true, "χ", "chi", "chi", "χ"),
    "acutex" => EntityData("\\acute x", true, "´x", "'x", "'x", "𝑥́"),
    "Psi" => EntityData("\\Psi", true, "Ψ", "Psi", "Psi", "Ψ"),
    "psi" => EntityData("\\psi", true, "ψ", "psi", "psi", "ψ"),
    "tau" => EntityData("\\tau", true, "τ", "tau", "tau", "τ"),
    "Omega" => EntityData("\\Omega", true, "Ω", "Omega", "Omega", "Ω"),
    "omega" => EntityData("\\omega", true, "ω", "omega", "omega", "ω"),
    "piv" => EntityData("\\varpi", true, "ϖ", "omega-pi", "omega-pi", "ϖ"),
    "varpi" => EntityData("\\varpi", true, "ϖ", "omega-pi", "omega-pi", "ϖ"),
    "partial" => EntityData("\\partial", true, "∂", "[partial differential]", "[partial differential]", "∂"),
    # ** Hebrew
    "alefsym" => EntityData("\\aleph", true, "ℵ", "aleph", "aleph", "ℵ"),
    "aleph" => EntityData("\\aleph", true, "ℵ", "aleph", "aleph", "ℵ"),
    "gimel" => EntityData("\\gimel", true, "ℷ", "gimel", "gimel", "ℷ"),
    "beth" => EntityData("\\beth", true, "ℶ", "beth", "beth", "ב"),
    "dalet" => EntityData("\\daleth", true, "ℸ", "dalet", "dalet", "ד"),
    # ** Icelandic
    "ETH" => EntityData("\\DH{}", true, "Ð", "D", "Ð", "Ð"),
    "eth" => EntityData("\\dh{}", true, "ð", "dh", "ð", "ð"),
    "THORN" => EntityData("\\TH{}", true, "Þ", "TH", "Þ", "Þ"),
    "thorn" => EntityData("\\th{}", true, "þ", "th", "þ", "þ"),
    # * Punctuation
    # ** Dots and Marks
    "dots" => EntityData("\\dots{}", true, "…", "...", "...", "…"),
    "cdots" => EntityData("\\cdots{}", true, "⋯", "...", "...", "⋯"),
    "hellip" => EntityData("\\dots{}", true, "…", "...", "...", "…"),
    "middot" => EntityData("\\textperiodcentered{}", true, "·", ".", "·", "·"),
    "iexcl" => EntityData("!`", true, "¡", "!", "¡", "¡"),
    "iquest" => EntityData("?`", true, "¿", "?", "¿", "¿"),
    # ** Dash-like
    "shy" => EntityData("\\-", true, "", "", "", ""),
    "ndash" => EntityData("--", true, "–", "-", "-", "–"),
    "mdash" => EntityData("---", true, "—", "--", "--", "—"),
    # ** Quotations
    "quot" => EntityData("\\textquotedbl{}", true, """, "\"", "\"", "\""),
    "acute" => EntityData("\\textasciiacute{}", true, "´", "'", "´", "´"),
    "ldquo" => EntityData("\\textquotedblleft{}", true, "“", "\"", "\"", "“"),
    "rdquo" => EntityData("\\textquotedblright{}", true, "”", "\"", "\"", "”"),
    "bdquo" => EntityData("\\quotedblbase{}", true, "„", "\"", "\"", "„"),
    "lsquo" => EntityData("\\textquoteleft{}", true, "‘", "`", "`", "‘"),
    "rsquo" => EntityData("\\textquoteright{}", true, "’", "'", "'", "’"),
    "sbquo" => EntityData("\\quotesinglbase{}", true, "‚", ",", ",", "‚"),
    "laquo" => EntityData("\\guillemotleft{}", true, "«", "<<", "«", "«"),
    "raquo" => EntityData("\\guillemotright{}", true, "»", ">>", "»", "»"),
    "lsaquo" => EntityData("\\guilsinglleft{}", true, "‹", "<", "<", "‹"),
    "rsaquo" => EntityData("\\guilsinglright{}", true, "›", ">", ">", "›"),
    # * Other
    # ** Misc. (often used)
    "circ" => EntityData("\\^{}", true, "ˆ", "^", "^", "∘"),
    "vert" => EntityData("\\vert{}", true, "|", "|", "|", "|"),
    "vbar" => EntityData("|", true, "|", "|", "|", "|"),
    "brvbar" => EntityData("\\textbrokenbar{}", true, "¦", "|", "¦", "¦"),
    "S" => EntityData("\\S", true, "§", "paragraph", "§", "§"),
    "sect" => EntityData("\\S", true, "§", "paragraph", "§", "§"),
    "amp" => EntityData("\\&", true, "&", "&", "&", "&"),
    "lt" => EntityData("\\textless{}", true, "<", "<", "<", "<"),
    "gt" => EntityData("\\textgreater{}", true, ">", ">", ">", ">"),
    "tilde" => EntityData("\\textasciitilde{}", true, "~", "~", "~", "~"),
    "slash" => EntityData("/", true, "/", "/", "/", "/"),
    "plus" => EntityData("+", true, "+", "+", "+", "+"),
    "under" => EntityData("\\_", true, "_", "_", "_", "_"),
    "equal" => EntityData("=", true, "=", "=", "=", "="),
    "asciicirc" => EntityData("\\textasciicircum{}", true, "^", "^", "^", "^"),
    "dagger" => EntityData("\\textdagger{}", true, "†", "[dagger]", "[dagger]", "†"),
    "dag" => EntityData("\\dag{}", true, "†", "[dagger]", "[dagger]", "†"),
    "Dagger" => EntityData("\\textdaggerdbl{}", true, "‡", "[doubledagger]", "[doubledagger]", "‡"),
    "ddag" => EntityData("\\ddag{}", true, "‡", "[doubledagger]", "[doubledagger]", "‡"),
    # ** Whitespace
    "nbsp" => EntityData("~", true, " ", " ", " ", " "),
    "ensp" => EntityData("\\hspace*{.5em}", true, " ", " ", " ", " "),
    "emsp" => EntityData("\\hspace*{1em}", true, " ", " ", " ", " "),
    "thinsp" => EntityData("\\hspace*{.2em}", true, " ", " ", " ", " "),
    # ** Currency
    "curren" => EntityData("\\textcurrency{}", true, "¤", "curr.", "¤", "¤"),
    "cent" => EntityData("\\textcent{}", true, "¢", "cent", "¢", "¢"),
    "pound" => EntityData("\\pounds{}", true, "£", "pound", "£", "£"),
    "yen" => EntityData("\\textyen{}", true, "¥", "yen", "¥", "¥"),
    "euro" => EntityData("\\texteuro{}", true, "€", "EUR", "EUR", "€"),
    "EUR" => EntityData("\\texteuro{}", true, "€", "EUR", "EUR", "€"),
    "dollar" => EntityData("\\\$", true, "\$", "\$", "\$", "\$"),
    "USD" => EntityData("\\\$", true, "\$", "\$", "\$", "\$"),
    # ** Property Marks
    "copy" => EntityData("\\textcopyright{}", true, "©", "(c)", "©", "©"),
    "reg" => EntityData("\\textregistered{}", true, "®", "(r)", "®", "®"),
    "trade" => EntityData("\\texttrademark{}", true, "™", "TM", "TM", "™"),
    # ** Science et al.
    "minus" => EntityData("\\minus", true, "−", "-", "-", "−"),
    "pm" => EntityData("\\textpm{}", true, "±", "+-", "±", "±"),
    "plusmn" => EntityData("\\textpm{}", true, "±", "+-", "±", "±"),
    "times" => EntityData("\\texttimes{}", true, "×", "*", "×", "×"),
    "frasl" => EntityData("/", true, "⁄", "/", "/", "⁄"),
    "colon" => EntityData("\\colon", true, ":", ":", ":", ":"),
    "div" => EntityData("\\textdiv{}", true, "÷", "/", "÷", "÷"),
    "frac12" => EntityData("\\textonehalf{}", true, "½", "1/2", "½", "½"),
    "frac14" => EntityData("\\textonequarter{}", true, "¼", "1/4", "¼", "¼"),
    "frac34" => EntityData("\\textthreequarters{}", true, "¾", "3/4", "¾", "¾"),
    "permil" => EntityData("\\textperthousand{}", true, "‰", "per thousand", "per thousand", "‰"),
    "sup1" => EntityData("\\textonesuperior{}", true, "¹", "^1", "¹", "¹"),
    "sup2" => EntityData("\\texttwosuperior{}", true, "²", "^2", "²", "²"),
    "sup3" => EntityData("\\textthreesuperior{}", true, "³", "^3", "³", "³"),
    "radic" => EntityData("\\sqrt{\\,}", true, "√", "[square root]", "[square root]", "√"),
    "sum" => EntityData("\\sum", true, "∑", "[sum]", "[sum]", "∑"),
    "prod" => EntityData("\\prod", true, "∏", "[product]", "[n-ary product]", "∏"),
    "micro" => EntityData("\\textmu{}", true, "µ", "micro", "µ", "µ"),
    "macr" => EntityData("\\textasciimacron{}", true, "¯", "[macron]", "¯", "¯"),
    "deg" => EntityData("\\textdegree{}", true, "°", "degree", "°", "°"),
    "prime" => EntityData("\\prime", true, "′", "'", "'", "′"),
    "Prime" => EntityData("\\prime{}\\prime", true, "″", "''", "''", "″"),
    "infin" => EntityData("\\infty", true, "∞", "[infinity]", "[infinity]", "∞"),
    "infty" => EntityData("\\infty", true, "∞", "[infinity]", "[infinity]", "∞"),
    "prop" => EntityData("\\propto", true, "∝", "[proportional to]", "[proportional to]", "∝"),
    "propto" => EntityData("\\propto", true, "∝", "[proportional to]", "[proportional to]", "∝"),
    "not" => EntityData("\\textlnot{}", true, "¬", "[angled dash]", "¬", "¬"),
    "neg" => EntityData("\\neg{}", true, "¬", "[angled dash]", "¬", "¬"),
    "land" => EntityData("\\land", true, "∧", "[logical and]", "[logical and]", "∧"),
    "wedge" => EntityData("\\wedge", true, "∧", "[logical and]", "[logical and]", "∧"),
    "lor" => EntityData("\\lor", true, "∨", "[logical or]", "[logical or]", "∨"),
    "vee" => EntityData("\\vee", true, "∨", "[logical or]", "[logical or]", "∨"),
    "cap" => EntityData("\\cap", true, "∩", "[intersection]", "[intersection]", "∩"),
    "cup" => EntityData("\\cup", true, "∪", "[union]", "[union]", "∪"),
    "smile" => EntityData("\\smile", true, "⌣", "[cup product]", "[cup product]", "⌣"),
    "frown" => EntityData("\\frown", true, "⌢", "[Cap product]", "[cap product]", "⌢"),
    "int" => EntityData("\\int", true, "∫", "[integral]", "[integral]", "∫"),
    "therefore" => EntityData("\\therefore", true, "∴", "[therefore]", "[therefore]", "∴"),
    "there4" => EntityData("\\therefore", true, "∴", "[therefore]", "[therefore]", "∴"),
    "because" => EntityData("\\because", true, "∵", "[because]", "[because]", "∵"),
    "sim" => EntityData("\\sim", true, "∼", "~", "~", "∼"),
    "cong" => EntityData("\\cong", true, "≅", "[approx. equal to]", "[approx. equal to]", "≅"),
    "simeq" => EntityData("\\simeq", true, "≅", "[approx. equal to]", "[approx. equal to]", "≅"),
    "asymp" => EntityData("\\asymp", true, "≈", "[almost equal to]", "[almost equal to]", "≈"),
    "approx" => EntityData("\\approx", true, "≈", "[almost equal to]", "[almost equal to]", "≈"),
    "ne" => EntityData("\\ne", true, "≠", "[not equal to]", "[not equal to]", "≠"),
    "neq" => EntityData("\\neq", true, "≠", "[not equal to]", "[not equal to]", "≠"),
    "equiv" => EntityData("\\equiv", true, "≡", "[identical to]", "[identical to]", "≡"),
    "triangleq" => EntityData("\\triangleq", true, "≜", "[defined to]", "[defined to]", "≜"),
    "le" => EntityData("\\le", true, "≤", "<=", "<=", "≤"),
    "leq" => EntityData("\\le", true, "≤", "<=", "<=", "≤"),
    "ge" => EntityData("\\ge", true, "≥", ">=", ">=", "≥"),
    "geq" => EntityData("\\ge", true, "≥", ">=", ">=", "≥"),
    "lessgtr" => EntityData("\\lessgtr", true, "≶", "[less than or greater than]", "[less than or greater than]", "≶"),
    "lesseqgtr" => EntityData("\\lesseqgtr", true, "⋚", "[less than or equal or greater than or equal]", "[less than or equal or greater than or equal]", "⋚"),
    "ll" => EntityData("\\ll", true, "≪", "<<", "<<", "≪"),
    "Ll" => EntityData("\\lll", true, "⋘", "<<<", "<<<", "⋘"),
    "lll" => EntityData("\\lll", true, "⋘", "<<<", "<<<", "⋘"),
    "gg" => EntityData("\\gg", true, "≫", ">>", ">>", "≫"),
    "Gg" => EntityData("\\ggg", true, "⋙", ">>>", ">>>", "⋙"),
    "ggg" => EntityData("\\ggg", true, "⋙", ">>>", ">>>", "⋙"),
    "prec" => EntityData("\\prec", true, "≺", "[precedes]", "[precedes]", "≺"),
    "preceq" => EntityData("\\preceq", true, "≼", "[precedes or equal]", "[precedes or equal]", "≼"),
    "preccurlyeq" => EntityData("\\preccurlyeq", true, "≼", "[precedes or equal]", "[precedes or equal]", "≼"),
    "succ" => EntityData("\\succ", true, "≻", "[succeeds]", "[succeeds]", "≻"),
    "succeq" => EntityData("\\succeq", true, "≽", "[succeeds or equal]", "[succeeds or equal]", "≽"),
    "succcurlyeq" => EntityData("\\succcurlyeq", true, "≽", "[succeeds or equal]", "[succeeds or equal]", "≽"),
    "sub" => EntityData("\\subset", true, "⊂", "[subset of]", "[subset of]", "⊂"),
    "subset" => EntityData("\\subset", true, "⊂", "[subset of]", "[subset of]", "⊂"),
    "sup" => EntityData("\\supset", true, "⊃", "[superset of]", "[superset of]", "⊃"),
    "supset" => EntityData("\\supset", true, "⊃", "[superset of]", "[superset of]", "⊃"),
    "nsub" => EntityData("\\not\\subset", true, "⊄", "[not a subset of]", "[not a subset of", "⊄"),
    "sube" => EntityData("\\subseteq", true, "⊆", "[subset of or equal to]", "[subset of or equal to]", "⊆"),
    "nsup" => EntityData("\\not\\supset", true, "⊅", "[not a superset of]", "[not a superset of]", "⊅"),
    "supe" => EntityData("\\supseteq", true, "⊇", "[superset of or equal to]", "[superset of or equal to]", "⊇"),
    "setminus" => EntityData("\\setminus", true, "∖", "\\", " \\", "⧵"),
    "forall" => EntityData("\\forall", true, "∀", "[for all]", "[for all]", "∀"),
    "exist" => EntityData("\\exists", true, "∃", "[there exists]", "[there exists]", "∃"),
    "exists" => EntityData("\\exists", true, "∃", "[there exists]", "[there exists]", "∃"),
    "nexist" => EntityData("\\nexists", true, "∃", "[there does not exists]", "[there does not  exists]", "∄"),
    "nexists" => EntityData("\\nexists", true, "∃", "[there does not exists]", "[there does not  exists]", "∄"),
    "empty" => EntityData("\\emptyset", true, "∅", "[empty set]", "[empty set]", "∅"),
    "emptyset" => EntityData("\\emptyset", true, "∅", "[empty set]", "[empty set]", "∅"),
    "isin" => EntityData("\\in", true, "∈", "[element of]", "[element of]", "∈"),
    "in" => EntityData("\\in", true, "∈", "[element of]", "[element of]", "∈"),
    "notin" => EntityData("\\notin", true, "∉", "[not an element of]", "[not an element of]", "∉"),
    "ni" => EntityData("\\ni", true, "∋", "[contains as member]", "[contains as member]", "∋"),
    "nabla" => EntityData("\\nabla", true, "∇", "[nabla]", "[nabla]", "∇"),
    "ang" => EntityData("\\angle", true, "∠", "[angle]", "[angle]", "∠"),
    "angle" => EntityData("\\angle", true, "∠", "[angle]", "[angle]", "∠"),
    "perp" => EntityData("\\perp", true, "⊥", "[up tack]", "[up tack]", "⊥"),
    "parallel" => EntityData("\\parallel", true, "∥", "||", "||", "∥"),
    "sdot" => EntityData("\\cdot", true, "⋅", "[dot]", "[dot]", "⋅"),
    "cdot" => EntityData("\\cdot", true, "⋅", "[dot]", "[dot]", "⋅"),
    "lceil" => EntityData("\\lceil", true, "⌈", "[left ceiling]", "[left ceiling]", "⌈"),
    "rceil" => EntityData("\\rceil", true, "⌉", "[right ceiling]", "[right ceiling]", "⌉"),
    "lfloor" => EntityData("\\lfloor", true, "⌊", "[left floor]", "[left floor]", "⌊"),
    "rfloor" => EntityData("\\rfloor", true, "⌋", "[right floor]", "[right floor]", "⌋"),
    "lang" => EntityData("\\langle", true, "⟨", "<", "<", "⟨"),
    "rang" => EntityData("\\rangle", true, "⟩", ">", ">", "⟩"),
    "langle" => EntityData("\\langle", true, "⟨", "<", "<", "⟨"),
    "rangle" => EntityData("\\rangle", true, "⟩", ">", ">", "⟩"),
    "hbar" => EntityData("\\hbar", true, "ℏ", "hbar", "hbar", "ℏ"),
    "mho" => EntityData("\\mho", true, "℧", "mho", "mho", "℧"),
    # ** Arrows
    "larr" => EntityData("\\leftarrow", true, "←", "<-", "<-", "←"),
    "leftarrow" => EntityData("\\leftarrow", true, "←", "<-", "<-", "←"),
    "gets" => EntityData("\\gets", true, "←", "<-", "<-", "←"),
    "lArr" => EntityData("\\Leftarrow", true, "⇐", "<=", "<=", "⇐"),
    "Leftarrow" => EntityData("\\Leftarrow", true, "⇐", "<=", "<=", "⇐"),
    "uarr" => EntityData("\\uparrow", true, "↑", "[uparrow]", "[uparrow]", "↑"),
    "uparrow" => EntityData("\\uparrow", true, "↑", "[uparrow]", "[uparrow]", "↑"),
    "uArr" => EntityData("\\Uparrow", true, "⇑", "[dbluparrow]", "[dbluparrow]", "⇑"),
    "Uparrow" => EntityData("\\Uparrow", true, "⇑", "[dbluparrow]", "[dbluparrow]", "⇑"),
    "rarr" => EntityData("\\rightarrow", true, "→", "->", "->", "→"),
    "to" => EntityData("\\to", true, "→", "->", "->", "→"),
    "rightarrow" => EntityData("\\rightarrow", true, "→", "->", "->", "→"),
    "rArr" => EntityData("\\Rightarrow", true, "⇒", "=>", "=>", "⇒"),
    "Rightarrow" => EntityData("\\Rightarrow", true, "⇒", "=>", "=>", "⇒"),
    "darr" => EntityData("\\downarrow", true, "↓", "[downarrow]", "[downarrow]", "↓"),
    "downarrow" => EntityData("\\downarrow", true, "↓", "[downarrow]", "[downarrow]", "↓"),
    "dArr" => EntityData("\\Downarrow", true, "⇓", "[dbldownarrow]", "[dbldownarrow]", "⇓"),
    "Downarrow" => EntityData("\\Downarrow", true, "⇓", "[dbldownarrow]", "[dbldownarrow]", "⇓"),
    "harr" => EntityData("\\leftrightarrow", true, "↔", "<->", "<->", "↔"),
    "leftrightarrow" => EntityData("\\leftrightarrow", true, "↔", "<->", "<->", "↔"),
    "hArr" => EntityData("\\Leftrightarrow", true, "⇔", "<=>", "<=>", "⇔"),
    "Leftrightarrow" => EntityData("\\Leftrightarrow", true, "⇔", "<=>", "<=>", "⇔"),
    "crarr" => EntityData("\\hookleftarrow", true, "↵", "<-'", "<-'", "↵"),
    "hookleftarrow" => EntityData("\\hookleftarrow", true, "↵", "<-'", "<-'", "↵"),
    # ** Function names
    "arccos" => EntityData("\\arccos", true, "arccos", "arccos", "arccos", "arccos"),
    "arcsin" => EntityData("\\arcsin", true, "arcsin", "arcsin", "arcsin", "arcsin"),
    "arctan" => EntityData("\\arctan", true, "arctan", "arctan", "arctan", "arctan"),
    "arg" => EntityData("\\arg", true, "arg", "arg", "arg", "arg"),
    "cos" => EntityData("\\cos", true, "cos", "cos", "cos", "cos"),
    "cosh" => EntityData("\\cosh", true, "cosh", "cosh", "cosh", "cosh"),
    "cot" => EntityData("\\cot", true, "cot", "cot", "cot", "cot"),
    "coth" => EntityData("\\coth", true, "coth", "coth", "coth", "coth"),
    "csc" => EntityData("\\csc", true, "csc", "csc", "csc", "csc"),
    "deg" => EntityData("\\deg", true, "°", "deg", "deg", "deg"),
    "det" => EntityData("\\det", true, "det", "det", "det", "det"),
    "dim" => EntityData("\\dim", true, "dim", "dim", "dim", "dim"),
    "exp" => EntityData("\\exp", true, "exp", "exp", "exp", "exp"),
    "gcd" => EntityData("\\gcd", true, "gcd", "gcd", "gcd", "gcd"),
    "hom" => EntityData("\\hom", true, "hom", "hom", "hom", "hom"),
    "inf" => EntityData("\\inf", true, "inf", "inf", "inf", "inf"),
    "ker" => EntityData("\\ker", true, "ker", "ker", "ker", "ker"),
    "lg" => EntityData("\\lg", true, "lg", "lg", "lg", "lg"),
    "lim" => EntityData("\\lim", true, "lim", "lim", "lim", "lim"),
    "liminf" => EntityData("\\liminf", true, "liminf", "liminf", "liminf", "liminf"),
    "limsup" => EntityData("\\limsup", true, "limsup", "limsup", "limsup", "limsup"),
    "ln" => EntityData("\\ln", true, "ln", "ln", "ln", "ln"),
    "log" => EntityData("\\log", true, "log", "log", "log", "log"),
    "max" => EntityData("\\max", true, "max", "max", "max", "max"),
    "min" => EntityData("\\min", true, "min", "min", "min", "min"),
    "Pr" => EntityData("\\Pr", true, "Pr", "Pr", "Pr", "Pr"),
    "sec" => EntityData("\\sec", true, "sec", "sec", "sec", "sec"),
    "sin" => EntityData("\\sin", true, "sin", "sin", "sin", "sin"),
    "sinh" => EntityData("\\sinh", true, "sinh", "sinh", "sinh", "sinh"),
    "sup" => EntityData("\\sup", true, "⊃", "sup", "sup", "sup"),
    "tan" => EntityData("\\tan", true, "tan", "tan", "tan", "tan"),
    "tanh" => EntityData("\\tanh", true, "tanh", "tanh", "tanh", "tanh"),
    # ** Signs & Symbols
    "bull" => EntityData("\\textbullet{}", true, "•", "*", "*", "•"),
    "bullet" => EntityData("\\textbullet{}", true, "•", "*", "*", "•"),
    "star" => EntityData("\\star", true, "*", "*", "*", "⋆"),
    "lowast" => EntityData("\\ast", true, "∗", "*", "*", "∗"),
    "ast" => EntityData("\\ast", true, "∗", "*", "*", "*"),
    "odot" => EntityData("\\odot", true, "o", "[circled dot]", "[circled dot]", "ʘ"),
    "oplus" => EntityData("\\oplus", true, "⊕", "[circled plus]", "[circled plus]", "⊕"),
    "otimes" => EntityData("\\otimes", true, "⊗", "[circled times]", "[circled times]", "⊗"),
    "check" => EntityData("\\checkmark", true, "✓", "[checkmark]", "[checkmark]", "✓"),
    "checkmark" => EntityData("\\checkmark", true, "✓", "[checkmark]", "[checkmark]", "✓"),
    # ** Miscellaneous (seldom used)
    "para" => EntityData("\\P{}", true, "¶", "[pilcrow]", "¶", "¶"),
    "ordf" => EntityData("\\textordfeminine{}", true, "ª", "_a_", "ª", "ª"),
    "ordm" => EntityData("\\textordmasculine{}", true, "º", "_o_", "º", "º"),
    "cedil" => EntityData("\\c{}", true, "¸", "[cedilla]", "¸", "¸"),
    "oline" => EntityData("\\overline{~}", true, "‾", "[overline]", "¯", "‾"),
    "uml" => EntityData("\\textasciidieresis{}", true, "¨", "[diaeresis]", "¨", "¨"),
    "zwnj" => EntityData("\\/{}", true, "", "", "", ""),
    "zwj" => EntityData("", true, "", "", "", ""),
    "lrm" => EntityData("", true, "", "", "", ""),
    "rlm" => EntityData("", true, "", "", "", ""),
    # ** Smilies
    "smiley" => EntityData("\\ddot\\smile", true, "☺", ":-)", ":-)", "☺"),
    "blacksmile" => EntityData("\\ddot\\smile", true, "☻", ":-)", ":-)", "☻"),
    "sad" => EntityData("\\ddot\\frown", true, "☹", ":-(", ":-(", "☹"),
    "frowny" => EntityData("\\ddot\\frown", true, "☹", ":-(", ":-(", "☹"),
    # ** Suits
    "clubs" => EntityData("\\clubsuit", true, "♣", "[clubs]", "[clubs]", "♣"),
    "clubsuit" => EntityData("\\clubsuit", true, "♣", "[clubs]", "[clubs]", "♣"),
    "spades" => EntityData("\\spadesuit", true, "♠", "[spades]", "[spades]", "♠"),
    "spadesuit" => EntityData("\\spadesuit", true, "♠", "[spades]", "[spades]", "♠"),
    "hearts" => EntityData("\\heartsuit", true, "♥", "[hearts]", "[hearts]", "♥"),
    "heartsuit" => EntityData("\\heartsuit", true, "♥", "[hearts]", "[hearts]", "♥"),
    "diams" => EntityData("\\diamondsuit", true, "♦", "[diamonds]", "[diamonds]", "◆"),
    "diamondsuit" => EntityData("\\diamondsuit", true, "♦", "[diamonds]", "[diamonds]", "◆"),
    "diamond" => EntityData("\\diamondsuit", true, "⋄", "[diamond]", "[diamond]", "◆"),
    "Diamond" => EntityData("\\diamondsuit", true, "⋄", "[diamond]", "[diamond]", "◆"),
    "loz" => EntityData("\\lozenge", true, "◊", "[lozenge]", "[lozenge]", "⧫"),
    "_ " => EntityData("\\hspace*{0.5em}", true, " ", " ", " ", " "),
    "_  " => EntityData("\\hspace*{1.0em}", true, "  ", "  ", "  ", "  "),
    "_   " => EntityData("\\hspace*{1.5em}", true, "   ", "   ", "   ", "   "),
    "_    " => EntityData("\\hspace*{2.0em}", true, "    ", "    ", "    ", "    "),
    "_     " => EntityData("\\hspace*{2.5em}", true, "     ", "     ", "     ", "     "),
    "_      " => EntityData("\\hspace*{3.0em}", true, "      ", "      ", "      ", "      "),
    "_       " => EntityData("\\hspace*{3.5em}", true, "       ", "       ", "       ", "       "),
    "_        " => EntityData("\\hspace*{4.0em}", true, "        ", "        ", "        ", "        "),
    "_         " => EntityData("\\hspace*{4.5em}", true, "         ", "         ", "         ", "         "),
    "_          " => EntityData("\\hspace*{5.0em}", true, "          ", "          ", "          ", "          "),
    "_           " => EntityData("\\hspace*{5.5em}", true, "           ", "           ", "           ", "           "),
    "_            " => EntityData("\\hspace*{6.0em}", true, "            ", "            ", "            ", "            "),
    "_             " => EntityData("\\hspace*{6.5em}", true, "             ", "             ", "             ", "             "),
    "_              " => EntityData("\\hspace*{7.0em}", true, "              ", "              ", "              ", "              "),
    "_               " => EntityData("\\hspace*{7.5em}", true, "               ", "               ", "               ", "               "),
    "_                " => EntityData("\\hspace*{8.0em}", true, "                ", "                ", "                ", "                "),
    "_                 " => EntityData("\\hspace*{8.5em}", true, "                 ", "                 ", "                 ", "                 "),
    "_                  " => EntityData("\\hspace*{9.0em}", true, "                  ", "                  ", "                  ", "                  "),
    "_                   " => EntityData("\\hspace*{9.5em}", true, "                   ", "                   ", "                   ", "                   "),
    "_                    " => EntityData("\\hspace*{10.0em}", true, "                    ", "                    ", "                    ", "                    "),
)


text:
#Setup:
#Connect the BNCH that usually conntects to piezo MOD to the laser AOTF TTL.
#Connect the BNC that usually connects to piezo MON to the laser intensity meter.

#We will deliver 10 pulses each of these durations:
#   0.1s
#   0.01s
#   0.001s
#   0.0001s
#   0.00001s

#Re-run this script 10 times with different laser intensities: 10%, 20%...100%

using Unitful, ImagineInterface, Imagine
import Unitful:s, V

#Note: when the meter reads 17.8mW the voltage output reads 0.378 V
#This implies 47 W / V
#This was with scaling set to level 5 (see intensity meter manual, model 1830-C)

pct_intensity = 90 #percent laser intensity this round
srate = 300000*Unitful.s^-1
npulses_per_condition = 5

ocpi2 = rigtemplate("ocpi-2"; sample_rate = srate)
ao = getpositioners(ocpi2)[1]
ai = getname(ocpi2, monitor_name(ao))

pulse_durations = [1.0; 0.1; 0.01; 0.001; 0.0001; 0.00001] * Unitful.s
pulse_separations = pulse_durations
append!(ao, "nothing", fill(0.0V, 100000))

for (i,dur) in enumerate(pulse_durations)
    nsamps_on = ImagineInterface.calc_num_samps(dur, srate)
    nsamps_off = ImagineInterface.calc_num_samps(pulse_separations[i], srate)
    pt = typeof(0.0V)[]
    for p = 1:npulses_per_condition
        append!(pt, fill(3.3V, nsamps_on))
        append!(pt, fill(0.0V, nsamps_off))
    end
    append!(ao, "$(dur)_duration", pt)
end

outbasename = "laser_pct_$pct_intensity"
write_commands(outbasename*".json", ocpi2, 0, 0, 0.0s; isbidi = false, skip_validation=true)
sigs = run_imagine(outbasename, [ao;ai]; ai_trig_dest = "PFI2", ao_trig_dest = "PFI1", trigger_source = "Port2/Line0", skip_validation=true)

ai_sig = parse_ai(outbasename*".ai", ["AI0"], "ocpi-2", srate)[1]
smps = get_samples(ai_sig; sampmap=:volts)

#using Plots
#plotlyjs()
#plot(ustrip.(smps.data))

#c = cor(ustrip.(get_samples(sigs[1]; sampmap=:volts)), ustrip.(get_samples(pos, sampmap=:volts)))
#ao2 = getoutputs(getanalog(ocpi2))[2]
#ai = getinputs(getanalog(ocpi2))[3] #This is a generic input.  pos_mon currently has a problem with negative voltages
#ai2 = getinputs(getanalog(ocpi2))[4]

#nsamps_on = 10001
#nsamps_off = 10001
#nsamps_back_on = 10001
#nsamps = nsamps_on+nsamps_off+nsamps_back_on
#append!(pos, "on", fill(3.0*Unitful.V, nsamps_on))
#append!(pos, "off", fill(0.0*Unitful.V, nsamps_off))
#append!(pos, "back_on", fill(3.0*Unitful.V, nsamps_back_on))
#append!(ao2, "on2", fill(3.0*Unitful.V, nsamps_on))
#append!(ao2, "off2", fill(0.0*Unitful.V, nsamps_off))
#append!(ao2, "back_on2", fill(3.0*Unitful.V, nsamps_back_on))
#base_name = "" #"test_imaginectrl5"

#using Plots
#plot(ustrip(get_samples(sigs[1])))

#for testing
#tsk = prepare_ao(aos, 1000, "PFI1")
#tsk2 = prepare_ai(ais, 1000, 3003, "disable")
#After much pain, realized that can only synchronize start of AO and AI acq on usb 6xxx devices by using two different pfi channels
#NIDAQ.catch_error(NIDAQ.ExportSignal(tsk.th, NIDAQ.Val_StartTrigger, b"/Dev1/ai/StartTrigger"))
#rr2 = record_signals(base_name, ais, nsamps; trigger_terminal = "PFI1", run_locally = false)
#rr = output_signals(aos; trigger_terminal = "PFI0", run_locally = false)
#sleep(5.0)
#print("delivering pulses\n")
#ttl_pulse() #P0.0 is wired to PFI0
#(proc_id2, ai_sigs) = fetch(rr2)
#proc_id1 = fetch(rr)
#print("Freeing workers...\n")
#free_workers(proc_id1)
#free_workers(proc_id2)
##print("Loading ai file...\n")
##aidata = ImagineInterface.parse_ai(base_name*".ai", map(daq_channel, ais), "dummy-6002", srate)
#using Plots
#plot(ustrip(get_samples(sigs[1])))


text:
using BinaryBuilder

# These are the platforms built inside the wizard
platforms = [
    BinaryProvider.Linux(:i686, :glibc),
  BinaryProvider.Linux(:x86_64, :glibc),
  BinaryProvider.Linux(:aarch64, :glibc),
  BinaryProvider.Linux(:armv7l, :glibc),
  BinaryProvider.Linux(:powerpc64le, :glibc),
  BinaryProvider.MacOS(),
  BinaryProvider.Windows(:i686),
  BinaryProvider.Windows(:x86_64)
]

# If the user passed in a platform (or a few, comma-separated) on the
# command-line, use that instead of our default platforms
if length(ARGS) > 0
    platforms = platform_key.(split(ARGS[1], ","))
end
info("Building for $(join(triplet.(platforms), ", "))")

# Collection of sources required to build readstat
sources = [
    "https://github.com/WizardMac/ReadStat.git" =>
    "f6bfe845689abc6f251acedd1c873cc86e1fdd5c",
]

script = raw"""
cd $WORKSPACE/srcdir
cd ReadStat/
./autogen.sh
./configure --prefix=/ --host=$target

if [ $target = "x86_64-w64-mingw32" ] || [ $target = "i686-w64-mingw32" ]; then
./configure --prefix=/ --host=$target CFLAGS="-I$DESTDIR/include" LDFLAGS="-L$DESTDIR/lib"
else
./configure --prefix=/ --host=$target
fi

make -j4
make install

"""

products = prefix -> [
    ExecutableProduct(prefix,"readstat"),
    LibraryProduct(prefix,"libreadstat")
]

dependencies = [
"https://raw.githubusercontent.com/Keno/IConvBuilder7/ca7e6a82f61cdb1c5ae53bdc94702488384435db/build.jl"]

# Build the given platforms using the given sources
autobuild(pwd(), "readstat", platforms, sources, script, products, dependencies=dependencies)


text:
module SoftComputingFinal

include("decision_trees.jl")
include("evaluation.jl")
include("data.jl")
include("tree_samplers.jl")
include("splitting.jl")
include("gp.jl")

# For comparison, see: https://github.com/bensadeghi/DecisionTree.jl

# EXPORTS
export
    # Basic tree representation etc.
    DecisionTree, Decision, Classification, Variable,
    treedepth, treesize, decide, normalize_conditions,
    randsplit, randchild,
    # Sampling
    SplitSampler, RampedSplitSampler, BoltzmannSampler,
    # Data stuff
    load_glass, load_ionosphere, load_segmentation, load_testdata, create_fitness,
    # Genetic programming
    rungp, runssgp,
    # Other
    kfolds

end # module

max_stars_repo_name: phipsgabler/soft-computing-final

max_issues_repo_name: phipsgabler/soft-computing-final

max_forks_repo_name: phipsgabler/soft-computing-final


text:
using Images, ImageView

# load and preview image in a separate window
img = load("sample-images/cats-3061372_640.jpg");
imshow(img);


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

using Test, LegendTextIO, Tables

Test.@testset "Package LegendTextIO" begin

# include("test_dario_hits.jl")

end # LegendTextIO.jl


text:
# AUTO GENERATED FILE - DO NOT EDIT

export ''_forceplot

"""
    ''_forceplot(;kwargs...)

A ForcePlot component.
The ForcePlot component is used to visualize the shapley contributions
to a single prediction made by a tree-based ML model. This is a simple
wrapper on top of React implementation published in shapjs package.
Read more about the component here: https://github.com/slundberg/shap
Keyword arguments:
- `id` (String; optional): The ID of this component, used to identify dash components
in callbacks. The ID needs to be unique to the component.
- `style` (Dict; optional): Inline css of each element
- `title` (String; optional): Plot title
- `className` (String; optional): html class associated with the component, used for styling purposes
- `features` (Dict; optional): Values corresponding to each feature, should have same set of keys as "featureNames" prop
- `baseValue` (Real; optional): same as explainer.expected_value
- `plot_cmap` (a value equal to: 'RdBu', 'GnPR', 'CyPU', 'PkYg', 'DrDb', 'LpLb', 'YlDp', 'OrId' | Array of Strings; optional): The colors used for shap contributions that increase/decrease the prediction value.
Should be one of:
-- default colour combinations RdBu, GnPR, CyPU, PkYg, DrDb, LpLb, YlDp, OrId
-- list of two hex codes, e.g., ["#AAAA11", "#6633CC"]
-- list of two rgb values, e.g., ["rgb(255, 13, 87)", "rgb(30, 136, 229)"]
- `link` (a value equal to: 'identity', 'logit'; optional): either 'identity' or 'logit'
- `featureNames` (Dict with Strings as keys and values of type String; optional): Label corrresponding to each feature, should have same set of keys as "features" prop
- `outNames` (Array of Strings; optional): Single element list of prediction variable name.
- `hideBaseValueLabel` (Bool; optional): Show/hide the label above the base value
- `hideBars` (Bool; optional): Show/hide the color bars
- `labelMargin` (Real; optional): Margin (in px) for labels on top of the plot
"""
function ''_forceplot(; kwargs...)
        available_props = Symbol[:id, :style, :title, :className, :features, :baseValue, :plot_cmap, :link, :featureNames, :outNames, :hideBaseValueLabel, :hideBars, :labelMargin]
        wild_props = Symbol[]
        return Component("''_forceplot", "ForcePlot", "dash_shap_components", available_props, wild_props; kwargs...)
end


text:
include("algorithms/matching.jl")

@enum State begin
    used = 1
    unused = 2
    removed = 3
    vital = 4
end

"""
    AllDifferent(x::Array{<:AbstractIntVar}, trailer)

AllDifferent constraint, enforcing ∀ i ≠ j ∈ ⟦1, length(x)⟧, x[i] ≠ x[j].

The implementation of this contraint is inspired by:
 https://www.researchgate.net/publication/200034395_A_Filtering_Algorithm_for_Constraints_of_Difference_in_CSPs
Many of the functions below relate to algorithms depicted in the paper, and their
documentation refer to parts of the overall algorithm.
"""
struct AllDifferent <: Constraint
    x::Array{<:AbstractIntVar}
    active::StateObject{Bool}
    initialized::StateObject{Bool}
    matching::Vector{StateObject{Tuple{Int, Int, Bool}}}
    remainingEdges::RSparseBitSet{UInt64}
    edgeToIndex::Dict{Edge{Int}, Int}
    indexToEdge::Vector{Edge{Int}}
    nodesMin::Int
    numberOfVars::Int
    numberOfVals::Int
    numberOfEdges::Int

    function AllDifferent(x::Array{<:AbstractIntVar}, trailer)::AllDifferent
        max = Base.maximum(var -> maximum(var.domain), x)
        min = Base.minimum(var -> minimum(var.domain), x)
        range = max - min + 1
        active = StateObject{Bool}(true, trailer)
        initialized = StateObject{Bool}(false, trailer)
        numberOfVars = length(x)
        numberOfEdges = sum(var -> length(var.domain), x)
        matching = Vector{StateObject{Tuple{Int, Int, Bool}}}(undef, numberOfVars)
        for i = 1:numberOfVars
            matching[i] = StateObject{Tuple{Int, Int, Bool}}((0, 0, false), trailer)
        end
        remainingEdges = RSparseBitSet{UInt64}(numberOfEdges, trailer)
        edgeToIndex = Dict{Edge{Int}, Int}()
        indexToEdge = Vector{Edge{Int}}(undef, numberOfEdges)
        constraint = new(x,
            active,
            initialized,
            matching,
            remainingEdges,
            edgeToIndex,
            indexToEdge,
            min,
            numberOfVars,
            range,
            numberOfEdges
        )
        counter = 1
        for (idx, var) in enumerate(x)
            addOnDomainChange!(var, constraint)
            for val in var.domain
                dst = numberOfVars + val - min + 1
                constraint.edgeToIndex[Edge(idx, dst)] = counter
                constraint.indexToEdge[counter] = Edge(idx, dst)
                counter += 1
            end
        end
        return constraint
    end
end

"""
    valToNode(constraint, value)::Int

Return the node index of a value.
"""
function valToNode(con::AllDifferent, val::Int)
    return con.numberOfVars + val - con.nodesMin + 1
end

"""
    nodeToVal(constraint, node)::Int

Return the underlying value of a node.
"""
function nodeToVal(con::AllDifferent, node::Int)
    return node - con.numberOfVars + con.nodesMin - 1
end

"""
    orderEdge(edge)::Edge

Return the ordered version of an edge, i.e. with e.src ≤ e.dst.
"""
function orderEdge(e::Edge{Int})::Edge{Int}
    src, dst = e.src < e.dst ? (e.src, e.dst) : (e.dst, e.src)
    return Edge(src, dst)
end

function updateremaining!(constraint::AllDifferent, removed::BitVector)
    clearMask!(constraint.remainingEdges)
    addToMask!(constraint.remainingEdges, bitVectorToUInt64Vector(removed))
    reverseMask!(constraint.remainingEdges)
    intersectWithMask!(constraint.remainingEdges)
end

function updatevital!(constraint::AllDifferent, vital::BitVector)
    for match in constraint.matching
        e = Edge(match.value[1], match.value[2])
        idx = constraint.edgeToIndex[e]
        setValue!(match, (e.src, e.dst, vital[idx]))
    end
end

function getvital(constraint)
    vital = BitVector(undef, constraint.numberOfEdges) .= false
    for match in constraint.matching
        if match.value[3]
            vital[constraint.edgeToIndex[Edge(match.value[1], match.value[2])]] = true
        end
    end
    return vital
end

"""
    initializeGraphs!(constraint)

Return the graph and the empty directed graph of a variable-value problem.
"""
function initializeGraphs!(con::AllDifferent)::Pair{Graph{Int}, DiGraph{Int}}
    numberOfNodes = con.numberOfVars + con.numberOfVals
    edgeFilter = BitVector(con.remainingEdges)[1:con.numberOfEdges]
    allEdges = con.indexToEdge[edgeFilter]
    graph = Graph(allEdges)
    digraph = DiGraph(nv(graph))
    if nv(graph) < numberOfNodes
        add_vertices!(graph, numberOfNodes - nv(graph))
    end
    return Pair(graph, digraph)
end

"""
    getAllEdges(digraph, parents)::Set{Edge}

Return all the edges visited by a BFS on `digraph` encoded in `parents`.
"""
function getAllEdges(digraph::DiGraph{Int}, parents::Vector{Int})::Set{Edge{Int}}
    edgeSet = Set{Edge{Int}}()
    for i = 1:nv(digraph)
        if parents[i] > 0 && parents[i] != i
            validneighbors = filter(v -> parents[v] > 0, inneighbors(digraph, i))
            validedges = map(v -> orderEdge(Edge(v, i)), validneighbors)
            union!(edgeSet, validedges)
        end
    end
    return edgeSet
end

remapedge(edge::Edge{Int}, component::Vector{Int}) = Edge(component[edge.src], component[edge.dst])

"""
    removeEdges!(constraint, prunedValue, graph, digraph)

Remove all the unnecessary edges in graph and digraph as in the original paper.

Update `constraint.edgesState` with the new status of each edge, remove some
edges from `graph` and `digraph` and push the removed values in `prunedValue`.
Following exactly the procedure in the function with the same name in the original
paper.
"""
function removeEdges!(constraint::AllDifferent, prunedValues::Vector{Vector{Int}}, graph::Graph{Int}, digraph::DiGraph{Int})

    unused = BitVector(constraint.remainingEdges)[1:constraint.numberOfEdges]
    vital = BitVector(undef, constraint.numberOfEdges) .= false
    removed = BitVector(undef, constraint.numberOfEdges) .= .~ unused
    used = BitVector(undef, constraint.numberOfEdges) .= false

    allValues = constraint.numberOfVars+1:nv(digraph)
    freeValues = filter(v -> indegree(digraph,v) == 0, allValues)

    seen = fill(false, constraint.numberOfVals)
    components = filter(comp -> length(comp)>1, strongly_connected_components(digraph))
    for component in components
        edgeSet = orderEdge.(remapedge.(edges(digraph[component]), [component]))
        edgeIndices = getindex.([constraint.edgeToIndex], edgeSet)
        used[edgeIndices] .= true
        unused[edgeIndices] .= false
    end

    for node in freeValues
        if seen[node - constraint.numberOfVars]
            continue
        end
        parents = bfs_parents(digraph, node; dir=:out)
        edgeSet = getAllEdges(digraph, parents)
        edgeIndices = getindex.([constraint.edgeToIndex], edgeSet)
        used[edgeIndices] .= true
        unused[edgeIndices] .= false
        reached = filter(v -> parents[v] > 0, allValues)
        seen[reached .- constraint.numberOfVars] .= true
    end

    edgeIndices = map(constraint.matching) do pair
        var, val = pair.value
        e = Edge(var, val)
        return constraint.edgeToIndex[e]
    end
    vital[edgeIndices] .= true .& unused[edgeIndices]
    unused[edgeIndices] .= false

    rest = constraint.indexToEdge[unused]
    reversedRest = map(e -> Edge(e.dst, e.src), rest)
    rem_edge!.([graph], rest)
    rem_edge!.([digraph], reversedRest)
    removed[unused] .= true
    foreach(rest) do e
        var, val = e.src, e.dst
        push!(prunedValues[var], nodeToVal(constraint, val))
    end

    updateremaining!(constraint, removed)
    updatevital!(constraint, vital)
end

"""
    updateEdgesState!(constraint)::Set{Edge}

Return all the pruned values not already encoded in the constraint state.
"""
function updateEdgesState!(constraint::AllDifferent, prunedDomains::CPModification)
    modif = Set{Edge}()
    for (idx, var) in enumerate(constraint.x)
        if haskey(prunedDomains, var.id)
            union!(modif, Edge.([idx], valToNode.([constraint], prunedDomains[var.id])))
        end
    end
    return modif
end

"""
    propagate!(constraint::AllDifferent, toPropagate::Set{Constraint}, prunedDomains::CPModification)

`AllDifferent` propagation function. Implement the full procedure of the paper.
"""
function propagate!(constraint::AllDifferent, toPropagate::Set{Constraint}, prunedDomains::CPModification)
    if !constraint.active.value
        return true
    end

    # Variables Initialization
    graph, digraph = initializeGraphs!(constraint)
    #    Run only once, when constraint is first propagated
    if !constraint.initialized.value
        matching = maximumMatching!(graph, digraph, constraint.numberOfVars)
        if matching.size < constraint.numberOfVars
            return false
        end
        for (idx, match) in enumerate(matching.matches)
            setValue!(constraint.matching[idx], (match..., false))
        end
        setValue!(constraint.initialized, true)
    #    Otherwise just read the stored values
    else
        matching = Matching{Int}(length(constraint.matching), map(match -> (match.value[1] => match.value[2]), constraint.matching))
        buildDigraph!(digraph, graph, matching)
    end

    # TODO change this with the CPModification
    modifications = updateEdgesState!(constraint, prunedDomains)
    prunedValues = Vector{Vector{Int}}(undef, constraint.numberOfVars)
    for i = 1:constraint.numberOfVars
        prunedValues[i] = Int[]
    end

    removed = .~ BitVector(constraint.remainingEdges)[1:constraint.numberOfEdges]
    vital = getvital(constraint)
    needrematching = false
    for e in modifications
        rev_e = Edge(e.dst, e.src)
        idx = constraint.edgeToIndex[e]
        if e in edges(graph)
            if vital[idx]
                return false
            elseif e in edges(digraph)
                needrematching = true
                rem_edge!(digraph, e)
            else
                rem_edge!(digraph, rev_e)
            end
            rem_edge!(graph, e)
            # TODO get rid of edgesState
            removed[idx] = true
        end
    end

    updateremaining!(constraint, removed)

    if needrematching
        matching = maximizeMatching!(digraph, constraint.numberOfVars)
        if matching.size < constraint.numberOfVars
            return false
        end
        for (idx, match) in enumerate(matching.matches)
            setValue!(constraint.matching[idx], (match..., false))
        end
    end
    removeEdges!(constraint, prunedValues, graph, digraph)

    for (prunedVar, var) in zip(prunedValues, constraint.x)
        if !isempty(prunedVar)
            for val in prunedVar
                remove!(var.domain, val)
            end
            triggerDomainChange!(toPropagate, var)
            addToPrunedDomains!(prunedDomains, var, prunedVar)
        end
    end

    if constraint in toPropagate
        pop!(toPropagate, constraint)
    end

    if all(var -> length(var.domain) <= 1, constraint.x)
        setValue!(constraint.active, false)
    end

    for var in constraint.x
        if isempty(var.domain)
            return false
        end
    end
    return true
end

variablesArray(constraint::AllDifferent) = constraint.x

function Base.show(io::IO, ::MIME"text/plain", con::AllDifferent)
    print(io, string(typeof(con)), ": ", join([var.id for var in con.x], " != "), ", active = ", con.active)
    for var in con.x
        print(io, "\n   ", var)
    end
end

function Base.show(io::IO, con::AllDifferent)
    print(io, string(typeof(con)), ": ", join([var.id for var in con.x], " != "))
end


text:
function _precompile_()
    ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
    Base.precompile(Tuple{typeof(get_fitted),Matrix{Float64},Matrix{Float64},Matrix{Float64},Matrix{Float64},Int64})   # time: 17.953636
    Base.precompile(Tuple{typeof(get_fitted_residuals),Matrix{Float64},Matrix{Float64},Matrix{Float64},Matrix{Float64},Int64})   # time: 0.6654432
    Base.precompile(Tuple{typeof(QRfit),Matrix{Float64},Hermitian{Float64, Matrix{Float64}}})   # time: 0.0766731
    Base.precompile(Tuple{typeof(term_fit),Matrix{Float64},Hermitian{Float64, Matrix{Float64}}})   # time: 0.0535826
end


text:
using StanBase
using Test

stan_prog = "
data {
  int<lower=1> N;
  int<lower=0,upper=1> y[N];
  real empty[0];
}
parameters {
  real<lower=0,upper=1> theta;
}
model {
  theta ~ beta(1,1);
  y ~ bernoulli(theta);
}
";

@testset "Basic HelpModel" begin

  stanmodel = HelpModel( "help", stan_prog)

  stan_sample(stanmodel; n_chains=1)

  @test stanmodel.method == StanBase.Help(:help)
  @test StanBase.get_n_chains(stanmodel) == 1

end


text:
# Code derived from metabolicEP (https://github.com/anna-pa-m/Metabolic-EP)

function parseexpval!(expval, siteflagave::BitArray{1}, siteflagvar::BitArray{1})

    return expave, expvar = _parseexpval!(expval, siteflagave, siteflagvar)
end

function _parseexpval!(expval::Tuple, siteflagave::BitArray{1},siteflagvar::BitArray{1})

    N = length(siteflagave)
    length(expval) == 3 || error("We expect a 3-Tuple here")

    mean, var, expsite = expval
    1 <= expsite <= N || error("expsite = $expsite not ∈ 1,...,$N")
    expave = Dict{Int,Float64}()
    expvar = Dict{Int,Float64}()

    # mean
    if mean !== nothing
        siteflagave[expsite] = false
        expave[expsite] = mean
    end

    # var
    if var !== nothing
        siteflagvar[expsite] = false
        expvar[expsite] = var
    end

    expave, expvar
end

function _parseexpval!(expval::Vector,siteflagave::BitArray{1},siteflagvar::BitArray{1})

    N = length(siteflagave)
    expave = Dict{Int,Float64}()
    expvar = Dict{Int,Float64}()
    for (mean, var, expsite) in expval

        1 <= expsite <= N || error("expsite = $expsite not ∈ 1,...,$N")

        # mean
        if mean !== nothing
            siteflagave[expsite] = false
            expave[expsite] = mean
        end

        # var
        if var !== nothing
            siteflagvar[expsite] = false
            expvar[expsite] =  var
        end
    end

    expave,expvar
end
_parseexpval!(nothing, siteflagave::BitArray{1}, siteflagvar::BitArray{1})=(Dict{Int,Float64}(),Dict{Int,Float64}())


text:
#
# element-wise check
#
function issamematrix(A::AbstractArray{T,2},B::AbstractArray{T,2}) where T
   if size(A)!=size(B)
      throw(DimensionMismatch())
   end
   (m,n) = size(A)
   for j=1:n
      for i=1:m
         if A[i,j]!=B[i,j]
            println(i," ",j," ",A[i,j]," ",B[i,j])
            return false
         end
      end
   end
   return true
end
function issamematrix(A::AbstractArray{T,2},B::AbstractArray{T,1}) where T
   if length(A)!=length(B)
      throw(DimensionMismatch())
   end
   (m,n) = size(A)
   k = 0
   for j=1:n
      for i=1:m
         k = k + 1
         if A[i,j]!=B[k]
            return false
         end
      end
   end
   return true
end
issamematrix(a::Number,b::Number) = (a==b)

#
# test matrix generation
#
function make_test_matrix(Tx, Tm, m::Int, n::Int, r::Real)
   if Tx<:SparseMatrixCSC
      return sprand(Tm,m,n,r)
   else
      return Matrix{Tm}(sprand(Tm,m,n,r))
   end
end

# convert the test matrix to dictionary
function convert_test_to_dict(M, Ti)
   Tv = eltype(M)
   if Ti <: Integer
      A = SparseMatrixDict{Tv,Ti}(M)
   else
      A = SparseMatrixDict{Tv}(M)
   end
end

# different numbers
function generate_range(IDXt, lb::Integer, ub::Integer)
   n = ub-lb+1
   if IDXt<:Number
      return rand(lb:ub),1
   elseif IDXt<:AbstractRange
      lower = max(1,rand(lb:div(n,2)))
      upper = max(1,rand(div(n,2):ub))
      steps = max(1,rand(1:div(n,4)))
      return lower:steps:upper,length(lower:steps:upper)
   elseif IDXt<:Array
      m = rand(1:n)
      return rand(lb:ub,m),m
   elseif IDXt<:Bool
      return rand(Bool,n),n
   else
      return Colon(),n
   end
end

# my findnz
function myfindnz(A)
   return (begin; I=findall(!iszero,A); (getindex.(I, 1), getindex.(I, 2), A[I]); end)
end


text:
"""
    braycurtis(se::SummarizedExperiment, assay_name::String)

Evaluates the braycurtis dissimilarity index for each sample and rturns it as a matrix.

# Arguments
- `se::SummarizedExperiment`: the experiment object of interest.
- `assay_name::String`: the name of the assay to base the evaluation on.
"""
function braycurtis(se::SummarizedExperiment, assay_name::String)

    pairwise(BrayCurtis(), assay(se, assay_name), dims = 2)

end

"""
    jaccard(se::SummarizedExperiment, assay_name::String)

Evaluates the jaccard dissimilarity index for each sample and rturns it as a matrix.

# Arguments
- `se::SummarizedExperiment`: the experiment object of interest.
- `assay_name::String`: the name of the assay to base the evaluation on.
"""
function jaccard(se::SummarizedExperiment, assay_name::String)

    pairwise(Jaccard(), assay(se, assay_name), dims = 2)

end

"""
    hellinger(se::SummarizedExperiment, assay_name::String)

Evaluates the hellinger dissimilarity index for each sample and rturns it as a matrix.

# Arguments
- `se::SummarizedExperiment`: the experiment object of interest.
- `assay_name::String`: the name of the assay to base the evaluation on.
"""
function hellinger(se::SummarizedExperiment, assay_name::String)

    pairwise(HellingerDist(), assay(se, assay_name), dims = 2)

end

"""
    pcoa(se::SummarizedExperiment, assay_name::String)

Runs a Multidimensional Scaling (MDS) for the selected assay of se and returns a
model that can be executed with `predict(model)`.

# Arguments
- `se::SummarizedExperiment`: the experiment object of interest.
- `assay_name::String`: the name of the assay to base the evaluation on.
"""
function pcoa(se::SummarizedExperiment, assay_name::String; dist = braycurtis, dim_number = 3)

    return fit(MDS, dist(se, assay_name), maxoutdim = dim_number, distances = true)

end


text:
@testset "Conjugation action" begin
    G = PermutationGroups.AbstractAlgebra.SymmetricGroup(4)
    S = [perm"(1,2)(4)", perm"(1,2,3,4)"]
    @test gens(G) == S

    pt = perm"(4)"
    @test Orbit(S, pt) isa Orbit
    @test length(Orbit(S, pt)) == 1

    pt = perm"(1,2)(4)"
    @test Orbit(S, pt) isa Orbit
    @test length(Orbit(S, pt)) == 6
    @test all(==(permtype(pt)), permtype.(Orbit(S, pt)))

    pt = perm"(1,2)(4)"
    @test Schreier(S, pt) isa Schreier
    tr = Schreier(S, pt)
    @test all(pt^tr[p] == p for p in tr)

    pt = perm"(2,3,4)"
    @test Schreier(S, pt) isa Schreier
    tr = Schreier(S, pt)
    @test all(pt^tr[p] == p for p in tr)
end


text:
# QP example
#
#    minimize 2 x^2 + y^2 + xy + x + y
#
#       s.t.  x, y >= 0
#             x + y = 1
#
#    solution: (0.25, 0.75), objv = 1.875
#

using Gurobi, Compat.Test

@testset "QP1" begin

    env = Gurobi.Env()
    setparam!(env, "OutputFlag", 0)

    model = Gurobi.Model(env, "qp_01")

    add_cvars!(model, [1., 1.], 0., Inf)
    update_model!(model)

    add_qpterms!(model, [1, 1, 2], [1, 2, 2], [2., 1., 1.])
    add_constr!(model, [1., 1.], '=', 1.)
    update_model!(model)

    optimize(model)

    @test isapprox(get_solution(model), [0.25, 0.75], atol=1e-4)
    @test get_objval(model) ≈ 1.875
end


text:
macro dryer(expr)

    isa(expr, Expr) || error("argument is not an expression")
    expr.head == :do && (expr = rewrite_do(expr))
    expr.head == :call || error("expression is not a function call")

    target = expr.args[1]
    args = filter(!Mocking.iskwarg, expr.args[2:end])
    kwargs = Mocking.extract_kwargs(expr)

    args_var = gensym("args")
    alternate_var = gensym("alt")

    result = quote
        if DryRun.activated()
            # println($target, "(", $(args...), ";", $(kwargs...), ")")
            # println($target, "(", (join(repr.(($(args...),)), ", ")), ";", $(kwargs...), ")")
            println($target, "(", (join(repr.(($(args...),)), ", ")), ")")
        else
            $target($(args...); $(kwargs...))
        end
    end

    return esc(result)
end


text:
##
macro comment(args...) end
export @comment
##
macro copycode(name, definition)
    # @def insertme some_code...
    # @insertme => would insert some_code... here
    # from http://www.stochasticlifestyle.com/type-dispatch-design-post-object-oriented-programming-julia/
  return quote
      macro $(esc(name))()
          esc($(Expr(:quote, definition)))
      end
  end
end
export @copycode
##
# From: good macros here https://gist.github.com/MikeInnes/8299575

# Equivalent to @time @dotimes ... Consider using @benchmark.

# Repeat an operation n times, e.g.
# @dotimes 100 println("hi")

# macro dotimes(n, body)
#     quote
#         for i = 1:$(esc(n))
#             $(esc(body))
#         end
#     end
# end
# export @dotimes

# macro dotimed(n, body)
#   :(@time @dotimes $(esc(n)) $(esc(body)))
# end
# export @dotimed

# Stop Julia from complaining about redifined consts/types -
# @defonce type MyType
#   ...
# end
# or
# @defonce const pi = 3.14

# macro defonce(typedef::Expr)
#   if typedef.head == :type
#     name = typedef.args[2]
#   elseif typedef.head == :typealias || typedef.head == :abstract
#     name = typedef.args[1]
#   elseif typedef.head == :const
#     name = typedef.args[1].args[1]
#   else
#     error("@defonce called with $(typedef.head) expression")
#   end
# export @defonce

#   typeof(name) == Expr && (name = name.args[1]) # Type hints

#   :(if !isdefined(@__MODULE__, $(Expr(:quote, name)))
#       $(esc(typedef))
#     end)
# end

# Julia's do-while loop, e.g.
# @once_then while x < 0.5
#   x = rand()
# end

macro once_then(expr::Expr)
    @assert expr.head == :while
    esc(quote
            $(expr.args[2]) # body of loop
            $expr # loop
        end)
end
export @once_then

function memoize(f)
    cache = Dict()
    (args...) -> haskey(cache, args) ? cache[args] : (cache[args] = f(args...))
end
export @memoize

# Evaluate expressions at compile time, e.g.
# hard_calculation(n) = (sleep(1); n)
# f(x) = x * @preval hard_calculation(2) # takes 1 second
# f(2) => 4 # instant

"Force the input expression to be evaluated in compile-time"
macro preval(expr)
    eval(expr)
end
export @preval

# Speed up anonymous functions 10x
# @dotimed 10^8 (x -> x^2)(rand()) # 3.9 s
# @dotimed 10^8 (@fn x -> x^2)(rand()) # 0.36 s

macro fn(expr::Expr)
    @assert expr.head in (:function, :->)
    name = gensym()
    args = expr.args[1]
    args = typeof(args) == Symbol ? [args] : args.args
    body = expr.args[2]
    @eval $name($(args...)) = $body
    name
end
export @fn
##


text:
zxd = ZXDiagram(4)
push_gate!(zxd, Val{:Z}(), 1, 3//2)
push_gate!(zxd, Val{:H}(), 1)
push_gate!(zxd, Val{:Z}(), 1, 1//2)
push_gate!(zxd, Val{:Z}(), 2, 1//2)
push_gate!(zxd, Val{:H}(), 4)
push_gate!(zxd, Val{:CNOT}(), 3, 2)
push_gate!(zxd, Val{:CZ}(), 4, 1)
push_gate!(zxd, Val{:H}(), 2)
push_gate!(zxd, Val{:CNOT}(), 3, 2)
push_gate!(zxd, Val{:CNOT}(), 1, 4)
push_gate!(zxd, Val{:H}(), 1)
push_gate!(zxd, Val{:Z}(), 2, 1//4)
push_gate!(zxd, Val{:Z}(), 3, 1//2)
push_gate!(zxd, Val{:H}(), 4)
push_gate!(zxd, Val{:Z}(), 1, 1//4)
push_gate!(zxd, Val{:H}(), 2)
push_gate!(zxd, Val{:H}(), 3)
push_gate!(zxd, Val{:Z}(), 4, 3//2)
push_gate!(zxd, Val{:Z}(), 3, 1//2)
push_gate!(zxd, Val{:X}(), 4, 1//1)
push_gate!(zxd, Val{:CNOT}(), 3, 2)
push_gate!(zxd, Val{:H}(), 1)
push_gate!(zxd, Val{:Z}(), 4, 1//2)
push_gate!(zxd, Val{:X}(), 4, 1//1)

zxg = ZXGraph(zxd)
replace!(Rule{:lc}(), zxg)
replace!(Rule{:pab}(), zxg)

cir = circuit_extraction(zxg)

zxg2 = clifford_simplification(zxd)
@test nv(zxg2) <= nv(zxg)
@test ne(zxg2) <= ne(zxg)

zxg3 = full_reduction(zxd)
cir = circuit_extraction(zxg3)


text:
# Autogenerated wrapper script for STYRENE_jll for i686-linux-gnu-cxx03
export surrogate, truth

JLLWrappers.@generate_wrapper_header("STYRENE")
JLLWrappers.@declare_executable_product(surrogate)
JLLWrappers.@declare_executable_product(truth)
function __init__()
    JLLWrappers.@generate_init_header()
    JLLWrappers.@init_executable_product(
        surrogate,
        "bin/surrogate",
    )

    JLLWrappers.@init_executable_product(
        truth,
        "bin/truth",
    )

    JLLWrappers.@generate_init_footer()
end  # __init__()