text:
function vsynced_renderloop(screen)
    while isopen(screen) && !WINDOW_CONFIG.exit_renderloop[]
        pollevents(screen) # GLFW poll
        screen.render_tick[] = nothing
        if WINDOW_CONFIG.pause_rendering[]
            sleep(0.1)
        else
            make_context_current(screen)
            render_frame(screen)
            GLFW.SwapBuffers(to_native(screen))
            yield()
        end
    end
end

function fps_renderloop(screen::Screen, framerate=WINDOW_CONFIG.framerate[])
    time_per_frame = 1.0 / framerate
    while isopen(screen) && !WINDOW_CONFIG.exit_renderloop[]
        t = time_ns()
        pollevents(screen) # GLFW poll
        screen.render_tick[] = nothing
        if WINDOW_CONFIG.pause_rendering[]
            sleep(0.1)
        else
            make_context_current(screen)
            render_frame(screen)
            GLFW.SwapBuffers(to_native(screen))
            t_elapsed = (time_ns() - t) / 1e9
            diff = time_per_frame - t_elapsed
            if diff > 0.001 # can't sleep less than 0.001
                sleep(diff)
            else # if we don't sleep, we still need to yield explicitely to other tasks
                yield()
            end
        end
    end
end

function renderloop(screen; framerate=WINDOW_CONFIG.framerate[])
    isopen(screen) || error("Screen most be open to run renderloop!")
    try
        if WINDOW_CONFIG.vsync[]
            GLFW.SwapInterval(1)
            vsynced_renderloop(screen)
        else
            GLFW.SwapInterval(0)
            fps_renderloop(screen, framerate)
        end
    catch e
        showerror(stderr, e, catch_backtrace())
        println(stderr)
        rethrow(e)
    finally
        destroy!(screen)
    end
end

const WINDOW_CONFIG = (renderloop = Ref{Function}(renderloop),
    vsync = Ref(false),
    framerate = Ref(30.0),
    float = Ref(false),
    pause_rendering = Ref(false),
    focus_on_show = Ref(false),
    decorated = Ref(true),
    title = Ref("Makie"),
    exit_renderloop = Ref(false),)

"""
    set_window_config!(;
        renderloop = renderloop,
        vsync = false,
        framerate = 30.0,
        float = false,
        pause_rendering = false,
        focus_on_show = false,
        decorated = true,
        title = "Makie"
    )
Updates the screen configuration, will only go into effect after closing the current
window and opening a new one!
"""
function set_window_config!(; kw...)
    for (key, value) in kw
        getfield(WINDOW_CONFIG, key)[] = value
    end
end

function setup!(screen)
    glEnable(GL_SCISSOR_TEST)
    if isopen(screen)
        glScissor(0, 0, widths(screen)...)
        glClearColor(1, 1, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT)
        for (id, scene) in screen.screens
            if scene.visible[]
                a = pixelarea(scene)[]
                rt = (minimum(a)..., widths(a)...)
                glViewport(rt...)
                bits = GL_STENCIL_BUFFER_BIT
                glClearStencil(id)
                if scene.clear
                    c = to_color(scene.backgroundcolor[])
                    glScissor(rt...)
                    glClearColor(red(c), green(c), blue(c), alpha(c))
                    bits |= GL_COLOR_BUFFER_BIT
                    glClear(bits)
                end
            end
        end
    end
    glDisable(GL_SCISSOR_TEST)
    return
end

const selection_queries = Function[]

"""
Renders a single frame of a `window`
"""
function render_frame(screen::Screen; resize_buffers=true)
    nw = to_native(screen)
    ShaderAbstractions.is_context_active(nw) || return
    fb = screen.framebuffer
    if resize_buffers
        wh = Int.(framebuffer_size(nw))
        resize!(fb, wh)
    end
    w, h = size(fb)

    # prepare stencil (for sub-scenes)
    glEnable(GL_STENCIL_TEST)
    glBindFramebuffer(GL_FRAMEBUFFER, fb.id[1]) # color framebuffer
    glDrawBuffers(length(fb.render_buffer_ids), fb.render_buffer_ids)
    glEnable(GL_STENCIL_TEST)
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE)
    glStencilMask(0xff)
    glClearStencil(0)
    glClearColor(0, 0, 0, 0)
    glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
    glDrawBuffer(fb.render_buffer_ids[1])
    setup!(screen)
    glDrawBuffers(length(fb.render_buffer_ids), fb.render_buffer_ids)

    # render with FXAA & SSAO
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE)
    glStencilMask(0x00)
    GLAbstraction.render(screen, true, true)

    # SSAO
    screen.postprocessors[1].render(screen)

    # render with FXAA but no SSAO
    glDrawBuffers(2, [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1])
    glEnable(GL_STENCIL_TEST)
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE)
    glStencilMask(0x00)
    GLAbstraction.render(screen, true, false)
    glDisable(GL_STENCIL_TEST)

    # FXAA
    screen.postprocessors[2].render(screen)

    # no FXAA primary render
    glDrawBuffers(2, [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1])
    glEnable(GL_STENCIL_TEST)
    glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE)
    glStencilMask(0x00)
    GLAbstraction.render(screen, false)
    glDisable(GL_STENCIL_TEST)

    # transfer everything to the screen
    screen.postprocessors[3].render(screen)

    return
end

function id2scene(screen, id1)
    # TODO maybe we should use a different data structure
    for (id2, scene) in screen.screens
        id1 == id2 && return true, scene
    end
    return false, nothing
end

function GLAbstraction.render(screen::GLScreen, fxaa::Bool, ssao::Bool=false)
    # Somehow errors in here get ignored silently!?
    try
        # sort by overdraw, so that overdrawing objects get drawn last!
        # sort!(screen.renderlist, by = ((zi, id, robj),)-> robj.prerenderfunction.overdraw[])
        for (zindex, screenid, elem) in screen.renderlist
            found, scene = id2scene(screen, screenid)
            found || continue
            a = pixelarea(scene)[]
            glViewport(minimum(a)..., widths(a)...)
            if scene.clear
                glStencilFunc(GL_EQUAL, screenid, 0xff)
            else
                # if we don't clear, that means we have a screen that is overlaid
                # on top of another, which means it doesn't have a stencil value
                # so we can't do the stencil test
                glStencilFunc(GL_ALWAYS, screenid, 0xff)
            end
            if (fxaa && elem[:fxaa][]) && ssao && elem[:ssao][]
                render(elem)
            end
            if (fxaa && elem[:fxaa][]) && !ssao && !elem[:ssao][]
                render(elem)
            end
            if !fxaa && !elem[:fxaa][]
                render(elem)
            end
        end
    catch e
        @error "Error while rendering!" exception = e
        rethrow(e)
    end
    return
end


text:
using ShipNoiseGenerators
using DSP, FFTW, Statistics, Random, Test

function bandpassfilter(x, cutoff, fs; order=2)
    hp = digitalfilter(Bandpass(cutoff[1], cutoff[2]; fs=fs), Butterworth(order))
    filt(hp, x)
end

@testset "ShipNoiseGenerators.jl" begin

n = 96000
fs = 96000
g1 = PinkNoiseGenerator(n)
x = rand(g1)
p = welch_pgram(x, 1024; fs=fs)
xpts = log10.(freq(p))[2:end]
ypts = 10*log10.(power(p))[2:end]
β = sum((xpts.-mean(xpts)).*(ypts.-mean(ypts)))/sum((xpts.-mean(xpts)).^2)

@test β ≈ (10*log10(1/xpts[end])-10*log10(1/xpts[1]))/(log10(xpts[end])-log10(xpts[1])) atol=0.1

n = 96000
fs = 96000
As = [0.8]
frequencies = [200.0]
ϕs = [0.1]
g2 = ShipNoiseGenerator(n=n, fs=fs, As=As, frequencies=frequencies, ϕs=ϕs)
x = rand(g2)

cutoff = [10.0, 1000.0]
y = bandpassfilter(x, cutoff, fs; order=7)
env = abs.(hilbert(y))
rate = 1//80
fsnew = fs*rate
spec = abs.(rfft(resample(env, rate)))
spec[1] = 0.0
freqindices = FFTW.rfftfreq(n, fs)
@test freqindices[argmax(spec)] == frequencies[1]

end

ext: jl


text:
using LibPQ

abstract type DBTable end

psql_field_type(::Type{String}) = "VARCHAR"
psql_field_type(::Type{Dict}) = "JSONB"
psql_field_type(::Type{Integer}) = "INTEGER"
psql_field_type(::Type{Union{T, Integer}}) where T = "INTEGER"

psql_value(a::String) = "'$a'"
psql_value(a::T) where T<:Integer = string(a)
psql_value(a::DBTable) = a.id
psql_value(a::Dict) = "'$(JSON.json(a))'"

julia_value(a::String, b::Type{Dict}) = JSON.parse(a)
julia_value(a, b) = a

function table_name(d::Type)
    return lowercase(string(d))
end
function table_name(d)
    return table_name(typeof(d))
end
function get_id(d::T, conn; exclude::Array{Symbol}=Symbol[])  where T<:DBTable
    fields = [name for name in fieldnames(typeof(d))
              if (name != :id) && !(name in exclude)]
    values = [psql_value(getfield(d,field)) for field in fields]
    conditions = [join(field_value, " = ") for field_value in
                 zip(fields, values)]
    sql = """
        SELECT
            id
        FROM
            $(table_name(d))
        WHERE
            $(join(conditions, " AND "))
    """
    @info sql
    data = fetch!(NamedTuple, execute(conn, sql)).id
    if length(data) == 0
        return nothing
    else
        return first(data)
    end
end
function exists(d::T, conn; exclude::Array{Symbol}=Symbol[]) where T <:DBTable
    return get_id(d, conn, exclude=exclude) != nothing
end

function create_table_sql(d)
    fields = []
    for name in fieldnames(d)
        ftype = fieldtype(d, name)
        if name == :id
            push!(fields, "id SERIAL PRIMARY KEY")
        else
            push!(fields, "$name $(psql_field_type(ftype))")
        end
    end
    return """
           CREATE TABLE IF NOT EXISTS $(table_name(d)) (
           $(join(fields, " ,"))

    )
    """
end
function insert_sql(d)
    fields = [name for name in fieldnames(typeof(d))
              if name != :id]
    values = [psql_value(getfield(d,field)) for field in fields]
    sql = """
        INSERT INTO $(table_name(d))
           ($(join(fields, ", ")))
        VALUES
              ($(join(values, ", ")))
        RETURNING id;
    """
end
function update(d::DBTable, conn)
    fields = [name for name in fieldnames(typeof(d))
              if name != :id]
    values = [psql_value(getfield(d,field)) for field in fields]
    kv_list = join([string(field, "=", value) for (field, value) in zip(fields, values)], ", ")
    sql = """
        UPDATE $(table_name(d))
        SET $(kv_list)
        WHERE
            id = $(d.id)
    """
    @info(sql)
    execute(conn, sql)
end
function insert(d::DBTable, conn)
    sql = insert_sql(d)
    @info sql
    result = execute(conn, sql)
    data = fetch!(NamedTuple, result)
    d.id = data.id[1]
    return d
end

function get(d::T, conn; exclude::Array{Symbol}=Symbol[])  where T<:DBTable
    fields = [name for name in fieldnames(typeof(d))
              if (name != :id) && !(name in exclude)]
    values = [psql_value(getfield(d,field)) for field in fields]
    conditions = [join(field_value, " = ") for field_value in
                 zip(fields, values)]
    sql = """
        SELECT
            *
        FROM
            $(table_name(d))
        WHERE
            $(join(conditions, " AND "))
    """
    @info sql
    data = fetch!(NamedTuple, execute(conn, sql))
    if isempty(data[:id])
        throw("Element not found")
    end
    params = Dict(k=>julia_value(first(data[k]), fieldtype(T, k)) for k in keys(data))
    d = T(;params...)
    return d
end
function get_or_insert(d::DBTable, conn)
    id = get_id(d, conn)
    if id == nothing
        insert(d, conn)
        return d
    else
        return get(d, conn)
    end
end


text:
# This file is a part of Julia. License is MIT: https://julialang.org/license

"""
Determine whether a statement is side-effect-free, i.e. may be removed if it has no uses.
"""
function stmt_effect_free(@nospecialize(stmt), @nospecialize(rt), src, spvals::SimpleVector)
    isa(stmt, Union{PiNode, PhiNode}) && return true
    isa(stmt, Union{ReturnNode, GotoNode, GotoIfNot}) && return false
    isa(stmt, GlobalRef) && return isdefined(stmt.mod, stmt.name)
    isa(stmt, Slot) && return false # Slots shouldn't occur in the IR at this point, but let's be defensive here
    if isa(stmt, Expr)
        e = stmt::Expr
        head = e.head
        if head === :static_parameter
            etyp = sparam_type(spvals[e.args[1]])
            # if we aren't certain enough about the type, it might be an UndefVarError at runtime
            return isa(etyp, Const) || issingletontype(widenconst(etyp))
        end
        ea = e.args
        if head === :call
            f = argextype(ea[1], src, spvals)
            f = singleton_type(f)
            f === nothing && return false
            is_return_type(f) && return true
            contains_is(_PURE_BUILTINS, f) && return true
            contains_is(_PURE_OR_ERROR_BUILTINS, f) || return false
            rt === Bottom && return false
            return _builtin_nothrow(f, Any[argextype(ea[i], src, spvals) for i = 2:length(ea)], rt)
        elseif head === :new
            a = ea[1]
            typ = argextype(a, src, spvals)
            # `Expr(:new)` of unknown type could raise arbitrary TypeError.
            typ, isexact = instanceof_tfunc(typ)
            isexact || return false
            isconcretedispatch(typ) || return false
            typ = typ::DataType
            fieldcount(typ) >= length(ea) - 1 || return false
            for fld_idx in 1:(length(ea) - 1)
                eT = argextype(ea[fld_idx + 1], src, spvals)
                fT = fieldtype(typ, fld_idx)
                eT ⊑ fT || return false
            end
            return true
        elseif head === :isdefined || head === :the_exception || head === :copyast || head === :inbounds || head === :boundscheck
            return true
        else
            # e.g. :simdloop
            return false
        end
    end
    return true
end

function abstract_eval_ssavalue(s::SSAValue, src::IRCode)
    return types(src)[s]
end

function abstract_eval_ssavalue(s::SSAValue, src::IncrementalCompact)
    return types(src)[s]
end

function compact_exprtype(compact::IncrementalCompact, @nospecialize(value))
    if isa(value, AnySSAValue)
        return types(compact)[value]
    elseif isa(value, Argument)
        return compact.ir.argtypes[value.n]
    end
    return argextype(value, compact.ir, compact.ir.spvals)
end

is_tuple_call(ir::IRCode, @nospecialize(def)) = isa(def, Expr) && is_known_call(def, tuple, ir, ir.spvals)
is_tuple_call(compact::IncrementalCompact, @nospecialize(def)) = isa(def, Expr) && is_known_call(def, tuple, compact)
function is_known_call(e::Expr, @nospecialize(func), src::IncrementalCompact)
    if e.head !== :call
        return false
    end
    f = compact_exprtype(src, e.args[1])
    return singleton_type(f) === func
end

max_stars_repo_name: jpsamaroo/julia

max_issues_repo_name: eli-schwartz/julia

max_forks_repo_name: eli-schwartz/julia


text:
import .Gadfly
import Cairo, Fontconfig

Image(height, width, a::Gadfly.Plot) =
    Image(height, width, ImageFileData(height, width, a))

function ImageFileData(height, width, a::Gadfly.Plot)
    filename = tempname()*".pdf"
    Gadfly.draw(Gadfly.PDF(filename, width*Gadfly.cm, height*Gadfly.cm), a)
    r = read(filename)
    rm(filename)
    ImageFileData(r, :pdf)
end


text:
function vm_execute!(io, ws)
    return_to = Vector{Int}()
    pc = 1
    while pc < length(ws.insns) + 1
        insn = ws.insns[pc][1]

        if insn == :push
            pc += 1
            arg = parse(Int, ws.insns[pc][2])
            push!(ws.stack, arg)
        elseif insn == :dup
            push!(ws.stack, ws.stack[end])
        elseif insn == :copy
            pc += 1
            arg = parse(Int, ws.insns[pc][2])
            push!(ws.stack, ws.stack[end - arg])
        elseif insn == :swap
            ws.stack[end], ws.stack[end-1] = ws.stack[end-1], ws.stack[end]
        elseif insn == :discard
            pop!(ws.stack)
        elseif insn == :slide
            pc += 1
            arg = parse(Int, ws.insns[pc][2])
            x = pop!(ws.stack)
            for i in 1:arg
                pop!(ws.stack)
            end
            push!(ws.stack, x)
        elseif insn == :add
            y, x = pop!(ws.stack), pop!(ws.stack)
            push!(ws.stack, x + y)
        elseif insn == :sub
            y, x = pop!(ws.stack), pop!(ws.stack)
            push!(ws.stack, x - y)
        elseif insn == :mul
            y, x = pop!(ws.stack), pop!(ws.stack)
            push!(ws.stack, x * y)
        elseif insn == :div
            y, x = pop!(ws.stack), pop!(ws.stack)
            push!(ws.stack, div(x, y))
        elseif insn == :mod
            y, x = pop!(ws.stack), pop!(ws.stack)
            push!(ws.stack, mod(x, y))
        elseif insn == :heap_write
            value, address = pop!(ws.stack), pop!(ws.stack)
            ws.heap[address] = value
        elseif insn == :heap_read
            address = pop!(ws.stack)
            value = ws.heap[address]
            push!(ws.stack, value)
        elseif insn == :label
            # do nothing
        elseif insn == :jump
            pc += 1
            arg = ws.insns[pc][2]
            pc = jump_to(ws, arg)
        elseif insn == :jump_zero
            pc += 1
            arg = ws.insns[pc][2]
            if iszero(pop!(ws.stack))
                pc = jump_to(ws, arg)
            end
        elseif insn == :jump_nega
            pc += 1
            arg = ws.insns[pc][2]
            if pop!(ws.stack) < 0
                pc = jump_to(ws, arg)
            end
        elseif insn == :call
            pc += 1
            arg = ws.insns[pc][2]
            push!(return_to, pc)
            pc = jump_to(ws, arg)
        elseif insn == :return
            pc = pop!(return_to)
        elseif insn == :exit
            return

        elseif insn == :char_out
            print(io, Char(pop!(ws.stack)))
        elseif insn == :num_out
            print(io, pop!(ws.stack))
        elseif insn == :char_in
            address = pop!(ws.stack)
            ws.heap[address] = read(stdin, Char)
        elseif insn == :num_in
            address = pop!(ws.stack)
            ws.heap[address] = parse(Int, readline())
        end

        pc += 1
    end
    error()
end

vm_execute!(ws) = vm_execute!(stdout, ws)

function find_labels(insns)
    labels = Dict()
    afterlabel = false
    for (i, (key, val)) in enumerate(insns)
        if key == :label
            afterlabel = true
        elseif afterlabel
            labels[val] = i
            afterlabel = false
        else
            afterlabel = false
        end
    end
    labels
end

function jump_to(ws, name)
    ws.labels[name]
end


text:
module WiSARD

using Random

include("model/model.jl")

include("encoding/encoding.jl")

include("images/images.jl")

end # module


text:
using ImageTerm

function display(X::Matrix)
    imageterm(X)
    return
end


text:
"""
    apply_textstyle!(g, s)

Internal function to apply the textstyle `s` in a GLE context.
"""
@inline function apply_textstyle!(g::GLE, s::TextStyle, parent_font::String=""; addset=false)
    if !isdef(s.font) && !isempty(parent_font)
        s.font = parent_font
    end
    isanydef(s) || return nothing
    addset         && "\nset"                     |> g
    isdef(s.font)  && "font $(s.font)"            |> g
    isdef(s.hei)   && "hei $(s.hei)"              |> g
    isdef(s.color) && "color $(col2str(s.color))" |> g
    return nothing
end

"""
    apply_linestyle!(g, s)

Internal function to apply the linestyle `s` in a GLE context.
"""
@inline function apply_linestyle!(g::GLE, s::LineStyle; nosmooth=false, addset=false)
    isanydef(s) || return nothing
    addset && "\nset" |> g
    isdef(s.lstyle) && "lstyle $(s.lstyle)"           |> g
    isdef(s.lwidth) && "lwidth $(s.lwidth)"           |> g
    isdef(s.color)  && "color $(col2str(s.color))"    |> g
    nosmooth || isdef(s.smooth) && s.smooth && "smooth" |> g
    return nothing
end

"""
    apply_markerstyle!(g, s)

Internal function to apply the markerstyle `s` in a GLE context.
"""
@inline function apply_markerstyle!(g::GLE, s::MarkerStyle; mcol=false, mscale=1)
    isanydef(s) || return nothing
    isdef(s.marker) && s.marker == "none" && return nothing
    if !mcol
        isdef(s.marker) && "marker $(s.marker)" |> g
        isdef(s.msize)  && "msize $(s.msize)"   |> g
        isdef(s.color)  && "color $(col2str(s.color))" |> g
    else
        "marker $(str(s))" |> g
        "msize" |> g
        if isdef(s.msize)
            "$(s.msize/mscale)" |> g
        else
            "$(0.4/mscale)"     |> g
        end
    end
    return nothing
end

"""
    apply_barstyle!(g, s)

Internal function to apply the barstyle `s` in a GLE context.
"""
@inline function apply_barstyle!(g::GLE, s::BarStyle)
    isanydef(s) || return nothing
    isdef(s.color) && "color $(col2str(s.color))" |> g
    "fill $(col2str(s.fill))" |> g
    return nothing
end

"""
    apply_barstyles_nostack!(g, v)

Internal function to apply the Vector of barstyles `v` in a GLE context when
the bars are not stacked.
"""
@inline function apply_barstyles_nostack!(g::GLE, v::Vector{BarStyle})
    # assumption that if one is defined, all are defined (this is checked
    # with the set_properties!)
    isanydef(v[1]) || return nothing
    isdef(v[1].color) && "color $(svec2str((col2str(s.color) for s ∈ v)))" |> g
    isdef(v[1].fill)  && "fill $(svec2str((col2str(s.fill) for s ∈ v)))"   |> g
    return nothing
end


text:
module Drawing

using GMT, Printf

const global paper_sizes = Dict(
	"A0"     => (2384, 3370),
	"A1"     => (1684, 2384),
	"A2"     => (1191, 1684),
	"A3"     => (842, 1191),
	"A4"     => (595, 842),
	"A5"     => (420, 595),
	"A6"     => (298, 420),
	"A"      => (612, 792),
	"Letter" => (612, 792),
	"Legal"  => (612, 1008),
	"Ledger" => (792, 1224),
	"B"      => (612, 1008),
	"C"      => (1584, 1224),
	"D"      => (2448, 1584),
	"E"      => (3168, 2448))

import ..CTRLshapes, ..KW, ..find_in_dict, ..def_fig_axes

export
	box, circle, cross, custom, diamond, ellipse, ellipseAz, hexagon, itriangle, letter, minus, pentagon,
	plus, square, star, triangle, rect, rotrect, rotrectAz, roundrect, ydash

circle(x, y, diam;    Vd=0, kw...) = shapes_1par(x, y, diam, "c"; Vd=Vd, kw...)
cross(x, y, diam;     Vd=0, kw...) = shapes_1par(x, y, diam, "x"; Vd=Vd, kw...)
diamond(x, y, diam;   Vd=0, kw...) = shapes_1par(x, y, diam, "d"; Vd=Vd, kw...)
hexagon(x, y, diam;   Vd=0, kw...) = shapes_1par(x, y, diam, "h"; Vd=Vd, kw...)
itriangle(x, y, diam; Vd=0, kw...) = shapes_1par(x, y, diam, "i"; Vd=Vd, kw...)
minus(x, y, diam;     Vd=0, kw...) = shapes_1par(x, y, diam, "-"; Vd=Vd, kw...)
pentagon(x, y, diam;  Vd=0, kw...) = shapes_1par(x, y, diam, "n"; Vd=Vd, kw...)
plus(x, y, diam;      Vd=0, kw...) = shapes_1par(x, y, diam, "+"; Vd=Vd, kw...)
square(x, y, diam;    Vd=0, kw...) = shapes_1par(x, y, diam, "s"; Vd=Vd, kw...)
star(x, y, diam;      Vd=0, kw...) = shapes_1par(x, y, diam, "a"; Vd=Vd, kw...)
triangle(x, y, diam;  Vd=0, kw...) = shapes_1par(x, y, diam, "t"; Vd=Vd, kw...)
ydash(x, y, diam;     Vd=0, kw...) = shapes_1par(x, y, diam, "y"; Vd=Vd, kw...)
box(x, y, a, b;       Vd=0, kw...) = shapes_2par(x, y, a, b, "r", 'c'; Vd=Vd, kw...)
rect(x, y, a, b;      Vd=0, kw...) = shapes_2par(x, y, a, b, "r", 'm'; Vd=Vd, kw...)

ellipse(x, y, dir, a, b;   Vd=0, kw...) = shapes_3par(x, y, dir, a, b, "e"; Vd=Vd, kw...)
ellipseAz(x, y, dir, a, b; Vd=0, kw...) = shapes_3par(x, y, dir, a, b, "E"; Vd=Vd, kw...)
rotrect(x, y, dir, a, b;   Vd=0, kw...) = shapes_3par(x, y, dir, a, b, "j"; Vd=Vd, kw...)
rotrectAz(x, y, dir, a, b; Vd=0, kw...) = shapes_3par(x, y, dir, a, b, "J"; Vd=Vd, kw...)
roundrect(x, y, w, h, r;   Vd=0, kw...) = shapes_3par(x, y, w, h, r, "R";   Vd=Vd, kw...)

function custom(x, y, name::String, par1; Vd=0, kw...)	# Custom symbol
	opt_S = string(" -Sk", name, "/", par1)			# The GMT -S option
	helper_shapes(x, y, opt_S; Vd=Vd, kw...)
end

function letter(x, y, size, str, font::String="", just::String=""; Vd=0, kw...)
	opts = (font != "") ? "+t" * font : ""
	(just != "") && (opts *= "+j" * just)
	opt_S = @sprintf(" -Sl%g+t%s%s", size, str, opts)
	helper_shapes(x, y, opt_S; Vd=Vd, kw...)
end

function shapes_1par(x, y, par1, scode; Vd=0, kw...)
	opt_S = string(" -S", scode, par1)
	helper_shapes(x, y, opt_S; Vd=Vd, kw...)
end

function shapes_2par(x, y, par1, par2, scode, orig; Vd=0, kw...)
	(orig == 'c') && (x += par1/2;	y += par2/2)		# Origin in lower left corner
	opt_S = string(" -S", scode, par1, "/", par2)
	helper_shapes(x, y, opt_S; Vd=Vd, kw...)
end

function shapes_3par(x, y, par1, par2, par3, scode; Vd=0, kw...)
	opt_S = string(" -S", scode, par1, "/", par2, "/", par3)
	helper_shapes(x, y, opt_S; Vd=Vd, kw...)
end

function helper_shapes(x, y, cmd; Vd=0, kw...)
	# Helper function that takes care to all symbols plotting
	d = KW(kw)
	# Se if we are asked to force the start a new drawing (first= true) or bypass the CTRLshapes.first control
	first = find_in_dict(d, [:first])[1]
	((isa(first, Bool) && first)  || (isa(first, Int) && first != 0)) && (CTRLshapes.first[1] = true)
	((isa(first, Bool) && !first) || (isa(first, Int) && first == 0)) && (CTRLshapes.first[1] = false)

	if (CTRLshapes.first[1])
		CTRLshapes.fname[1] = joinpath(tempdir(), "GMTjl_tmp.ps")
		if ((val = find_in_dict(d, [:units])[1]) !== nothing)
			_cmd, opt_B = GMT.parse_B(d, "", def_fig_axes[1])
			cmd *= " --PROJ_LENGTH_UNIT=p"
			opt_J = " -Jx1"
			if ((val = find_in_dict(d, [:paper])[1]) !== nothing)
				ps = get(paper_sizes, string(val), (595, 842))
				opt_R = string(" -R0/",ps[1],"/0/",ps[2])
				ps_media = string(" --PS_MEDIA=", ps[1], "x", ps[2])
			else
				opt_R, ps_media = " -R0/594/0/841", " --PS_MEDIA=594x841"
			end
			CTRLshapes.points[1] = true
		else
			_cmd, opt_B, opt_J, opt_R = GMT.parse_BJR(d, "", "", false, " ")
			(opt_R == "")  && (opt_R = " -R0/21/0/29")
			(opt_J == " ") && (opt_J = " -Jx1")
		end
		(opt_B == def_fig_axes[1]) && (opt_B = "")			# We don't want a default -B here
		opt_O, opt_P = "", " -P"
		out, opt_T, EXT, = GMT.fname_out(d)
	else
		opt_R, opt_J, opt_B, opt_O, opt_P = " -R", " -J", "", " -O", ""
		(CTRLshapes.points[1]) && (cmd *= " --PROJ_LENGTH_UNIT=p")
	end

	redirect = (CTRLshapes.first[1]) ? " > " : " >> "
	if (length(kw) == 0)
		cmd = "psxy" * cmd * opt_R * opt_J * opt_B * opt_P * opt_O * " -K" * redirect * CTRLshapes.fname[1]
	else
		d[:Vd] = 2
		(opt_J != " -J" && length(opt_J) > 3) && (d[:J] = opt_J[4:end])	# Dirty trick to force assign the J we want
		(opt_R != " -R" && length(opt_R) > 3) && (d[:R] = opt_R[4:end])
		d[:B] = "none"
		cmd = GMT.common_plot_xyz("", nothing, cmd, CTRLshapes.first[1], false, d...)
		(CTRLshapes.first[1] && opt_B != "") && (cmd = replace(cmd, "psxy" => "psxy" * opt_B))
	end
	(0 < Vd < 2) && println(cmd);	(Vd > 1) && return cmd

	if (CTRLshapes.first[1])
		if (CTRLshapes.points[1])  cmd *= ps_media		# When units are points we always set a specific paper size
		elseif (EXT != "ps")       cmd *= " --PS_MEDIA=16840x16840"		# Exptend to a larger paper size (5 x A0)
		end
	end

	in_data = (length(x) == 1) ? [x y] : [x[:] y[:]]
	gmt(cmd, in_data)
	CTRLshapes.first[1] = false				# To signal next calls that they have to append
	fim = ((find_in_dict(d, [:show :fmt :savefig :figname :name], false)[1]) !== nothing) ? true : false
	(!fim) && return nothing

	CTRLshapes.first[1] = true				# Better reset it right now in case next command errors
	CTRLshapes.points[1] = false
	out, opt_T, EXT, fname, = GMT.fname_out(d, true)
	#show_non_consumed(d, cmd)
	GMT.close_PS_file(CTRLshapes.fname[1])
	GMT.showfig(d, CTRLshapes.fname[1], EXT, opt_T, false, fname)
end

"""
Submodule where the symbols of `plot` were elevated to the category of functions.
The functions that are currently available are:

- circle(x, y, diameter,    kw...)
- cross(x, y, diameter;     kw...)
- custom(x, y, name, size;  kw...)
- diamond(x, y, diameter;   kw...)
- hexagon(x, y, diameter;   kw...)
- itriangle(x, y, diameter; kw...)
- letter(x, y, size, str, font, just; kw...)
- minus(x, y, length;       kw...)
- pentagon(x, y, diameter;  kw...)
- plus(x, y, diameter;      kw...)
- square(x, y, diameter;    kw...)
- star(x, y, diameter;      kw...)
- triangle(x, y, diameter;  kw...)
- ydash(x, y, length;       kw...)
- box(x, y, width, height;  kw...)
- rect(x, y, width, height; kw...)
- ellipse(x, y, angle, majoraxis, minoraxis;     kw...)
- ellipseAz(x, y, azimuth, majoraxis, minoraxis; kw...)
- rotrect(x, y, angle, width, height;     kw...)
- rotrectAz(x, y, azimuth, width, height; kw...)
- roundrect(x, y, width, height, radius;  kw...)

For all symbols, except `box`, `diameter` is the diametr of the circumscribing circle and `x,y`
the coordinates of its center. `box` is the exception in that those are the coordinates of the
lower left corner.

This submodule uses global variables to track the first, middle and last layers in the stack plot but
in case of errors that track may be left in error state too. So, when one want to force the begining
of a new figure use the kwarg `first=true`. Likewise, if we want to add to a previously, still open fig,
use `first=false`.

By default the plot unites is ``cm`` but we can select ``points`` by doing `units=:points`. This takes care
of setting the appropriate `region` (A4 by default) and fig scale. If other then A4 size is wished, use
`paper=:A3` (or any of the common paper sizes). When units is left to ``cm`` the normal paper size ruple
applies (i.e. A4 for PS format or ~unlimitted for others.)

The `kw...` in the functions arguments are the normal kwargs that one can use in the `plot` module.

### Example, to draw a cute litte car:

	ellipse(300,201,0, 200, 50, units=:points, fill=:purple, pen=1)
	ellipse(340,206, 0,130, 66, fill=:purple, pen=1)
    ellipse(318,222,0, 60, 26, fill=:blue)
    box(200, 173, 205, 26, fill=:purple, pen=1)
    circle(305,185,56, fill=:black)
    circle(305,185,36, fill=:gray50)
    circle(400,185,56, fill=:black)
    circle(400,185,36, fill=:gray50, show=true)
"""
Drawing

end			# End module


text:
__precompile__()

module DFT

using Reexport

if VERSION < v"0.7.0-DEV.3187"
    @reexport using AbstractFFTs
else
    using AbstractFFTs
end

import AbstractFFTs: Plan, ScaledPlan,
                     fft, ifft, bfft, fft!, ifft!, bfft!,
                     plan_fft, plan_ifft, plan_bfft, plan_fft!, plan_ifft!, plan_bfft!,
                     rfft, irfft, brfft, plan_rfft, plan_irfft, plan_brfft,
                     fftshift, ifftshift,
                     rfft_output_size, brfft_output_size,
                     plan_inv, normalization

import Base: show, summary, size, ndims, length, eltype,
             *, A_mul_B!, inv, \, A_ldiv_B!

##############################################################################
# Native Julia FFTs:

include("ctfft.jl")
include("fftn.jl")

end


text:
# This file is a part of TextSearch.jl

export TextModel, VectorModel, trainsize, vocsize,
    TfWeighting, IdfWeighting, TpWeighting,
    FreqWeighting, BinaryLocalWeighting, BinaryGlobalWeighting,
    LocalWeighting, GlobalWeighting,
    fit, prune, prune_select_top

#####
##
## LocalWeighting
##
#####
"""
    LocalWeighting

Abstract type for local weighting
"""
abstract type LocalWeighting end

"""
    TfWeighting()

Term frequency weighting
"""
struct TfWeighting <: LocalWeighting end

"""
    TpWeighting()

Term probability weighting
"""
struct TpWeighting <: LocalWeighting end

"""
    FreqWeighting()

Frequency weighting
"""
struct FreqWeighting <: LocalWeighting end

"""
    BinaryLocalWeighting()

The weight is 1 for known tokens, 0 for out of vocabulary tokens
"""
struct BinaryLocalWeighting <: LocalWeighting end

#####
##
## GlobalWeighting
##
#####
"""
    GlobalWeighting

Abstract type for global weighting
"""
abstract type GlobalWeighting end

"""
    IdfWeighting()

Inverse document frequency weighting
"""
struct IdfWeighting <: GlobalWeighting end

"""
    BinaryGlobalWeighting()

The weight is 1 for known tokens, 0 for out of vocabulary tokens
"""
struct BinaryGlobalWeighting <: GlobalWeighting end

#####
##
## TextModels
##
#####
"""
    Model

An abstract type that represents a weighting model
"""
abstract type TextModel end

mutable struct VectorModel{_G<:GlobalWeighting, _L<:LocalWeighting} <: TextModel
    global_weighting::_G
    local_weighting::_L
    voc::Vocabulary
    maxoccs::Int32
    mindocs::Int32
end

function Base.copy(
        e::VectorModel;
        local_weighting=e.local_weighting,
        global_weighting=e.global_weighting,
        voc=e.voc,
        maxoccs=e.maxoccs,
        mindocs=e.mindocs,
    )
    VectorModel(global_weighting, local_weighting, voc, maxoccs, mindocs)
end

@inline trainsize(model::VectorModel) = trainsize(model.voc)
@inline vocsize(model::VectorModel) = vocsize(model.voc)
@inline Base.length(model::VectorModel) = length(model.voc)
@inline occs(model::VectorModel, tokenID::Integer) = occs(model.voc, tokenID)
@inline ndocs(model::VectorModel, tokenID::Integer) = ndocs(model.voc, tokenID)
@inline weight(model::VectorModel, tokenID::Integer) = weight(model.voc, tokenID)
@inline token(model::VectorModel, tokenID::Integer) = token(model.voc, tokenID)
@inline Base.eachindex(model::VectorModel) = eachindex(model.voc)

"""
    VectorModel(global_weighting::GlobalWeighting, local_weighting::LocalWeighting, voc::Vocabulary; mindocs=1)

Creates a vector model using the input corpus.
"""
function VectorModel(global_weighting::GlobalWeighting, local_weighting::LocalWeighting, voc::Vocabulary; mindocs=1)
    maxocc = convert(Int32, maximum(voc.occs))
    mindocs = convert(Int32, mindocs)

    VectorModel(global_weighting, local_weighting, voc, maxocc, mindocs)
end

function VectorModel(global_weighting::GlobalWeighting, local_weighting::LocalWeighting, textconfig::TextConfig, corpus::AbstractVector; mindocs=1, minbatch=0)
    voc = Vocabulary(textconfig, corpus; minbatch)
    maxocc = convert(Int32, maximum(voc.occs))
    mindocs = convert(Int32, mindocs)
    VectorModel(global_weighting, local_weighting, voc, maxocc, mindocs)
end

Base.show(io::IO, model::VectorModel) = print(io, "{VectorModel global_weighting=$(model.global_weighting), local_weighting=$(model.local_weighting), train-voc=$(vocsize(model)), train-n=$(trainsize(model)), maxoccs=$(model.maxoccs)}")

"""
    prune(model::VectorModel, lowerweight::AbstractFloat)

Creates a new vector model without terms with smaller global weights than `lowerweight`.
"""
function prune(model::VectorModel, lowerweight::AbstractFloat)
    voc = Vocabulary(trainsize(model))
    old = model.voc
    for tokenID in eachindex(old)
        if prune_global_weighting(model, tokenID) >= lowerweight
            push!(voc, old.token[tokenID], old.occs[tokenID], old.ndocs[tokenID], old.weight[tokenID])
        end
    end

    VectorModel(model.global_weighting, model.local_weighting, voc, maximum(voc.occs), model.mindocs)
end

"""
    prune_select_top(model::VectorModel, k::Integer)
    prune_select_top(model::VectorModel, ratio::AbstractFloat)

Creates a new vector model with the best `k` tokens from `model` based on global weighting.
`ratio` is a floating point between 0 and 1 indicating the ratio of the vocabulary to be kept
"""
function prune_select_top(model::VectorModel, k::Integer)
    voc = model.voc
    w = [prune_global_weighting(model, tokenID) for tokenID in eachindex(voc)]
    sort!(w, rev=true)
    prune(model, float(w[k]))
end

prune_select_top(model::VectorModel, ratio::AbstractFloat) =
    prune_select_top(model, floor(Int, length(model.voc) * ratio))

"""
    vectorize(model::VectorModel, bow::BOW; normalize=true, mindocs=model.mindocs, minweight=1e-9) where Tv<:Real

Computes a weighted vector using the given bag of words and the specified weighting scheme.
"""
function vectorize(model::VectorModel{_G,_L}, bow::BOW; normalize=true, mindocs=model.mindocs, minweight=1e-9) where {_G,_L}
    numtokens::Int = 0
    if _L === TpWeighting
        for freq in values(bow)
            numtokens += freq
        end
    end

    maxoccs::Int = 0
    if _L === TfWeighting
        maxoccs = length(bow) == 0 ? 0 : maximum(bow)
    end

    vec = SVEC()
    voc = model.voc
    for (tokenID, freq) in bow
        voc.ndocs[tokenID] < mindocs && continue
        w = local_weighting(model.local_weighting, freq, maxoccs, numtokens) * global_weighting(model, tokenID)
        w >= minweight && (vec[tokenID] = w)
    end

    if length(vec) == 0
        vec[0] = 1f0
    else
        normalize && normalize!(vec)
    end

    vec
end

function vectorize(model::VectorModel, textconfig::TextConfig, text, buff::TextSearchBuffer; normalize=true, mindocs=model.mindocs, minweight=1e-9)
    empty!(buff)
    bow = vectorize(model.voc, textconfig, text, buff)
    vectorize(model, bow; normalize, mindocs, minweight)
end

function vectorize(model::VectorModel, textconfig::TextConfig, text; normalize=true, mindocs=model.mindocs, minweight=1e-9)
    buff = take!(CACHES)
    try
        copy(vectorize(model, textconfig, text, buff; normalize, mindocs, minweight))
    finally
        put!(CACHES, buff)
    end
end

function vectorize_corpus(model::VectorModel, textconfig::TextConfig, corpus::AbstractVector; normalize=true, minbatch=0)
    n = length(corpus)
    V = Vector{SVEC}(undef, n)
    minbatch = getminbatch(minbatch, n)

    #@batch minbatch=minbatch per=thread
    for i in 1:n
        text = corpus[i]
        buff = take!(CACHES)
        try
            V[i] = copy(vectorize(model, textconfig, text, buff; normalize))
        finally
            put!(CACHES, buff)
        end
    end

    V
end

function broadcastable(model::VectorModel)
    (model,)
end

# local weightings: TfWeighting, TpWeighting, FreqWeighting, BinaryLocalWeighting
# global weightings: IdfWeighting, BinaryGlobalWeighting

@inline local_weighting(::TfWeighting, occs, maxoccs, numtokens) = occs / maxoccs
@inline local_weighting(::FreqWeighting, occs, maxoccs, numtokens) = occs
@inline local_weighting(::TpWeighting, occs, maxoccs, numtokens) = occs / numtokens
@inline local_weighting(::BinaryLocalWeighting, occs, maxoccs, numtokens) = 1.0
@inline global_weighting(model::VectorModel{IdfWeighting}, tokenID) = @inbounds log2(trainsize(model) / (1 + ndocs(model, tokenID)))
@inline global_weighting(model::VectorModel{BinaryGlobalWeighting}, tokenID) = 1.0
@inline prune_global_weighting(model::VectorModel, tokenID) = global_weighting(model, tokenID)
@inline prune_global_weighting(model::VectorModel{BinaryGlobalWeighting}, tokenID) = @inbounds 1 / (1 + ndocs(model, tokenID))

max_stars_repo_name: sadit/TextModel

max_issues_repo_name: sadit/TextModel

max_forks_repo_name: sadit/TextModel


text:
module YaoExtensions

using LuxurySparse
using Yao, YaoBlocks.ConstGate, BitBasis

include("Miscellaneous.jl")

# new block types
include("block_extension/blocks.jl")

# easy build
include("easybuild/easybuild.jl")

# AD
include("diff_extension/diffs.jl")

include("gatecount.jl")

include("deprecate.jl")

end # module


text:
export ComediStream, init_devices!

const comedipath = joinpath(@__DIR__,"comedi","comedi_bridge.so")
const comediname = "/dev/comedi0"
const SendTuple = Tuple{Int32,Int32,Int32,Float64}
const ReadTuple = Tuple{Int32,Int32,Int32}

struct ComediStream <: LabStream
    devices::Array{AbstractDevice,1}
    sendbuffer::Array{SendTuple,1}
    readbuffer::Array{ReadTuple,1}
end

function ComediStream()
    ccall((:comedi_start, comedipath),Int32,(Ptr{UInt8},), comediname)
    ComediStream(AbstractDevice[], SendTuple[], ReadTuple[])
end

function init_devices!(comedistream::ComediStream, devs::AbstractDevice...)
    for dev in devs
        if dev ∉ comedistream.devices
            setstream!(dev, comedistream)
            push!(comedistream.devices, dev)
            initialize(dev)
        else
            warn("Device $dev already added to the stream")
        end
    end
    return
end

function send(comedistream::ComediStream, cmd::SendTuple)
    ccall((:comedi_write, comedipath), Int32, (Int32,Int32,Int32,Float64), cmd[1], cmd[2], cmd[3], cmd[4])
    return
end
function read(comedistream::ComediStream, cmd::ReadTuple)
    val = ccall((:comedi_read, comedipath), Float64, (Int32,Int32,Int32), cmd[1], cmd[2], cmd[3])
    dummy_timestamp = UInt64(0)
    return val, dummy_timestamp
end

function send(comedistream::ComediStream)
    map(cmd -> send(comedistream, cmd), comedistream.sendbuffer)
    empty!(comedistream.readbuffer)
    return
end
function read(comedistream::ComediStream)
    vals = map(cmd -> read(comedistream, cmd), comedistream.sendbuffer)
    empty!(comedistream.readbuffer)
    return vals
end

function close(stream::ComediStream)
    foreach(close, stream.devices)
    ccall((:comedi_stop, comedipath), Int32, (Int32,), 0)
    return
end


text:
## SUPERVISED

# This implementation of scitype for models and measures is highly experimental

const ST = ScientificTypes # only used in this file

struct SupervisedScitype{input_scitype, target_scitype, prediction_type} end

ST.scitype(model::Deterministic, ::ST.DefaultConvention) =
    SupervisedScitype{input_scitype(model),
                    target_scitype(model),
                    :deterministic}

ST.scitype(model::Probabilistic, ::ST.DefaultConvention) =
    SupervisedScitype{input_scitype(model),
                    target_scitype(model),
                    :probabilistic}

ST.scitype(model::Interval, ::ST.DefaultConvention) =
    SupervisedScitype{input_scitype(model),
                    target_scitype(model),
                    :interval}

function Base.getproperty(::SupervisedScitype{input_scitype, target_scitype, prediction_type},
                          field::Symbol) where {input_scitype, target_scitype, prediction_type}
    if field === :input_scitype
        return input_scitype
    elseif field === :target_scitype
        return target_scitype
    elseif field === :prediction_type
        return prediction_type
    else
        throw(ArgumentError("Unsupported property. "))
    end
end

# crashes julia:
# MLJBase.getproperty(S::Type{<:SupervisedScitype}, p::Symbol) =
#     getproperty(S(), p)

Base.propertynames(::SupervisedScitype) = (:input_scitype, :target_scitype, :prediction_type)

_as_named_tuple(s::SupervisedScitype) =
    NamedTuple{(:input_scitype, :target_scitype, :prediction_type)}((s.input_scitype, s.target_scitype, s.prediction_type))

function Base.show(io::IO, ::MIME"text/plain", S::SupervisedScitype)
    show(io, MIME("text/plain"), _as_named_tuple(S))
end

## UNSUPERVISED

struct UnsupervisedScitype{input_scitype, output_scitype} end

ST.scitype(model::Unsupervised, ::ST.DefaultConvention) =
    UnsupervisedScitype{input_scitype(model),
                      MLJBase.output_scitype(model)}

function Base.getproperty(::UnsupervisedScitype{input_scitype, output_scitype},
                          field::Symbol) where {input_scitype, output_scitype}
    if field === :input_scitype
        return input_scitype
    elseif field === :output_scitype
        return output_scitype
    else
        throw(ArgumentError("Unsupported property. "))
    end
end

# crashes julia:
# MLJBase.getproperty(U::Type{<:UnsupervisedScitype}, p::Symbol) =
#     getproperty(U(), p)

Base.propertynames(::UnsupervisedScitype) = (:input_scitype, :output_scitype)

_as_named_tuple(s::UnsupervisedScitype) =
    NamedTuple{(:input_scitype, :output_scitype)}(
        (s.input_scitype, s.output_scitype))

function Base.show(io::IO, ::MIME"text/plain", S::UnsupervisedScitype)
    show(io, MIME("text/plain"), _as_named_tuple(S))
end

## MEASURES

struct MeasureScitype{target_scitype,
               prediction_type,
               orientation,
               reports_each_observation,
               is_feature_dependent,
               supports_weights} end

ST.scitype(measure, ::ST.DefaultConvention, ::Val{:measure}) =
    MeasureScitype{target_scitype(measure),
               prediction_type(measure),
               orientation(measure),
               reports_each_observation(measure),
               is_feature_dependent(measure),
               supports_weights(measure)}

function Base.getproperty(::MeasureScitype{target_scitype,
               prediction_type,
               orientation,
               reports_each_observation,
               is_feature_dependent,
               supports_weights},
                          field::Symbol) where {target_scitype,
                                                prediction_type,
                                                orientation,
                                                reports_each_observation,
                                                is_feature_dependent,
                                                supports_weights}
    if field === :target_scitype
        return target_scitype
    elseif field === :prediction_type
        return prediction_type
    elseif field === :orientation
        return orientation
    elseif field === :reports_each_observation
        return reports_each_observation
    elseif field === :is_feature_dependent
        return is_feature_dependent
    elseif field === :supports_weights
        return supports_weights
    else
        throw(ArgumentError("Unsupported property. "))
    end
end

# crashes julia:
# MLJBase.getproperty(M::Type{<:MeasureScitype}, p::Symbol) =
#     getproperty(M(), p)

Base.propertynames(::MeasureScitype) =
    (:target_scitype, :prediction_type, :orientation,
     :reports_each_observation, :is_feature_dependent, :supports_weights)

function _as_named_tuple(m::MeasureScitype)
    names = propertynames(m)
    return NamedTuple{names}(Tuple([getproperty(m, p) for p in names]))
end

function Base.show(io::IO, ::MIME"text/plain", M::MeasureScitype)
      show(io, MIME("text/plain"), _as_named_tuple(M))
end


text:
@testset "010" begin
    @test tdpe.e010(10) == 17
end

max_stars_repo_name: mithodin/ttpe

max_issues_repo_name: mithodin/ttpe

max_forks_repo_name: mithodin/ttpe


text:
struct RuntimeFn{Args,Kwargs,Body,Name} end
struct Unset end

Base.show(io::IO, rtfn::RuntimeFn{Args,Kwargs,Body,Name}) where {Args,Kwargs,Body,Name} =
    begin
        args = from_type(Args)
        kwargs = from_type(Kwargs)
        args = join(map(string, args), ", ")
        kwargs = join(map(string, kwargs), ", ")
        body = from_type(Body) |> rmlines
        repr = "$Name = ($args;$kwargs) -> $body"
        print(io, repr)
    end

Base.show(io::IO, ::Type{RuntimeFn{Args,Kwargs,Body,Name}}) where {Args,Kwargs,Body,Name} =
    print(io, "ggfunc-$Name")

struct Argument
    name::Symbol
    type::Union{Nothing,Any}
    default::Union{Unset,Any}
end

function compress_impl!(arg::Argument)
    meta, default = compress_impl!(arg.default)
    encoded = Call(
        Constructor{Argument}(),
        tuple(compress(arg.name), compress(arg.type), default),
    )
    may_cache_call(meta, encoded)
end

Base.show(io::IO, arg::Argument) = begin
    print(io, arg.name)
    if arg.type !== nothing
        print(io, "::", arg.type)
    end
    if arg.default !== Unset()
        print(io, "=", arg.default)
    end
end

function _ass_positional_args!(
    assign_block::Vector{Expr},
    args::List{Argument},
    ninput::Int,
    pargs::Symbol,
)
    i = 1
    for arg in args
        ass = arg.name
        if arg.type !== nothing
            ass = :($ass::$(arg.type))
        end
        if i > ninput
            arg.default === Unset() && error("Input arguments too few.")
            ass = :($ass = $(arg.default))
        else
            ass = :($ass = $pargs[$i])
        end
        push!(assign_block, ass)
        i += 1
    end
end

const _zero_arg = compress(list(Argument))
@generated function (::RuntimeFn{Args,_zero_arg,Body})(pargs...) where {Args,Body}
    args = from_type(Args)
    ninput = length(pargs)
    assign_block = Expr[]
    body = from_type(Body)
    _ass_positional_args!(assign_block, args, ninput, :pargs)
    quote
        let $(assign_block...)
            $body
        end
    end
end

_get_kwds(::Type{Base.Iterators.Pairs{A,B,C,NamedTuple{Kwds,D}}}) where {Kwds,A,B,C,D} =
    Kwds

@generated function (::RuntimeFn{Args,Kwargs,Body})(
    pargs...;
    pkwargs...,
) where {Args,Kwargs,Body}
    args = from_type(Args)
    kwargs = from_type(Kwargs)
    ninput = length(pargs)
    assign_block = Expr[]
    body = from_type(Body)
    if isempty(kwargs)
        _ass_positional_args!(assign_block, args, ninput, :pargs)
    else
        kwds = gensym("kwds")
        feed_in_kwds = _get_kwds(pkwargs)
        push!(assign_block, :($kwds = pkwargs))
        _ass_positional_args!(assign_block, args, ninput, :pargs)
        for kwarg in kwargs
            ass = k = kwarg.name
            if kwarg.type !== nothing
                ass = :($ass::$(kwarg.type))
            end
            if k in feed_in_kwds
                ass = :($ass = $kwds[$(QuoteNode(k))])
            else
                default = kwarg.default
                default === Unset() && error("no default value for keyword argument $(k)")
                ass = :($ass = $default)
            end
            push!(assign_block, ass)
        end
    end
    quote
        let $(assign_block...)
            $body
        end
    end
end


text:
using Latexify: Latexify

# Recipe to hook into Latexify.jl's `latexify` function for `Sym`s.
# We do not use SymPy's `latex` function here since it behaves a bit different
# from Latexify.jl. Thus, we let Latexify.jl handle everything for the sake of
# consistency.
# For example, SymPy doesn't print a multiplication symbol by default while
# Latexify uses a `"\\cdot"` (unless the keyword argument `cdot=false` is set).
Latexify.@latexrecipe function _(x::Sym)
  return string(x)
end


text:
"""
    filter_w(w::FT, w_min::FT) where {FT}

Return velocity such that
`abs(filter_w(w, w_min)) >= abs(w_min)`
while preserving the sign of `w`.
"""
filter_w(w::FT, w_min::FT) where {FT} =
    max(abs(w), abs(w_min)) * (w < 0 ? sign(w) : 1)

"""
    enforce_unit_bounds(a_up_i::FT, a_min::FT, a_max::FT) where {FT}

Enforce variable to be positive.

Ideally, this safety net will be removed once we
have robust positivity preserving methods. For now,
we need this to avoid domain error in certain
circumstances.
"""
enforce_unit_bounds(a_up_i::FT, a_min::FT, a_max::FT) where {FT} =
    clamp(a_up_i, a_min, a_max)

"""
    enforce_positivity(x::FT) where {FT}

Enforce variable to be positive.

Ideally, this safety net will be removed once we
have robust positivity preserving methods. For now,
we need this to avoid domain error in certain
circumstances.
"""
enforce_positivity(x::FT) where {FT} = max(x, FT(0))

"""
    fix_void_up(ρa_up_i::FT, val::FT, fallback = FT(0)) where {FT}

Substitute value by a consistent fallback in case of
negligible area fraction (void updraft).
"""
function fix_void_up(ρa_up_i::FT, val::FT, fallback = FT(0)) where {FT}
    tol = sqrt(eps(FT))
    return ρa_up_i > tol ? val : fallback
end


text:
# Tables.jl interface

DimTableSources = Union{AbstractDimStack,AbstractDimArray}

Tables.istable(::DimTableSources) = true
Tables.columnaccess(::Type{<:DimTableSources}) = true
Tables.columns(x::DimTableSources) = DimTable(x)

Tables.columnnames(A::AbstractDimArray) = _colnames(DimStack(A))
Tables.columnnames(s::AbstractDimStack) = _colnames(s)

Tables.schema(A::AbstractDimArray) = Tables.schema(DimStack(A))
function Tables.schema(s::AbstractDimStack)
    Tables.Schema(_colnames(s), (map(eltype, dims(s))..., map(eltype, data(s))...))
end

@inline Tables.getcolumn(x::DimTableSources, i::Int) = Tables.getcolumn(DimTable(x), i)
@inline Tables.getcolumn(x::DimTableSources, key::Symbol) =
    Tables.getcolumn(DimTable(x), key)
@inline Tables.getcolumn(x::DimTableSources, ::Type{T}, i::Int, key::Symbol) where T =
    Tables.getcolumn(DimTable(x), T, i, key)
@inline Tables.getcolumn(t::DimTableSources, dim::DimOrDimType) =
    Tables.getcolumn(t, dimnum(t, dim))

"""
    DimColumn{T,D<:Dimension} <: AbstractVector{T}

    DimColumn(dim::Dimension, dims::Tuple{Vararg{<:DimTuple}})
    DimColumn(dim::DimColumn, length::Int, dimstride::Int)

A table column based on a `Dimension` and it's relationship with other
`Dimension`s in `dims`.

`length` is the product of all dim lengths (usually the length of the corresponding
array data), while stride is the product of the preceding dimension lengths, which
may or may not be the real stride of the corresponding array depending on the data type.
For `A isa Array`, the `dimstride` will match the `stride`.

When the second argument is a `Tuple` of `Dimension`, the `length` and `dimstride`
fields are calculated from the dimensions, relative to the column dimension `dim`.

This object will be returned as a column of [`DimTable`](@ref).
"""
struct DimColumn{T,D<:Dimension} <: AbstractVector{T}
    dim::D
    dimstride::Int
    length::Int
end
function DimColumn(dim::D, alldims::DimTuple) where D<:Dimension
    # This is the apparent stride for indexing purposes,
    # it is not always the real array stride
    stride = dimstride(alldims, dim)
    len = prod(map(length, alldims))
    DimColumn{eltype(dim),D}(dim, stride, len)
end

dim(c::DimColumn) = getfield(c, :dim)
dimstride(c::DimColumn) = getfield(c, :dimstride)

# Simple Array interface

Base.length(c::DimColumn) = getfield(c, :length)
@inline function Base.getindex(c::DimColumn, i::Int)
    Base.@boundscheck checkbounds(c, i)
    dim(c)[mod((i - 1) ÷ dimstride(c), length(dim(c))) + 1]
end
Base.getindex(c::DimColumn, ::Colon) = vec(c)
Base.getindex(c::DimColumn, A::AbstractArray) = [c[i] for i in A]
Base.size(c::DimColumn) = (length(c),)
Base.axes(c::DimColumn) = (Base.OneTo(length(c)),)
Base.vec(c::DimColumn{T}) where T = [c[i] for i in eachindex(c)]
Base.Array(c::DimColumn) = vec(c)

struct DimArrayColumn{T,A<:AbstractDimArray{T},DS,DL,L} <: AbstractVector{T}
    data::A
    dimstrides::DS
    dimlengths::DL
    length::L
end
function DimArrayColumn(A::AbstractDimArray{T}, alldims::DimTuple) where T
    # This is the apparent stride for indexing purposes,
    # it is not always the real array stride
    dimstrides = map(d -> dimstride(alldims, d), dims(A))
    dimlengths = map(length, dims(A))
    len = prod(map(length, alldims))
    DimArrayColumn(A, dimstrides, dimlengths, len)
end

data(c::DimArrayColumn) = getfield(c, :data)
dimstrides(c::DimArrayColumn) = getfield(c, :dimstrides)
dimlengths(c::DimArrayColumn) = getfield(c, :dimlengths)

# Simple Array interface

Base.length(c::DimArrayColumn) = getfield(c, :length)
@inline function Base.getindex(c::DimArrayColumn, i::Int)
    Base.@boundscheck checkbounds(c, i)
    I = map((s, l) -> _strideind(s, l, i), dimstrides(c), dimlengths(c))
    data(c)[I...]
end

_strideind(stride, len, i) = mod((i - 1) ÷ stride, len) + 1

Base.getindex(c::DimArrayColumn, ::Colon) = vec(c)
Base.getindex(c::DimArrayColumn, A::AbstractArray) = [c[i] for i in A]
Base.size(c::DimArrayColumn) = (length(c),)
Base.axes(c::DimArrayColumn) = (Base.OneTo(length(c)),)
Base.vec(c::DimArrayColumn{T}) where T = [c[i] for i in eachindex(c)]
Base.Array(c::DimArrayColumn) = vec(c)

"""
    AbstractDimTable <: Tables.AbstractColumns

Abstract supertype for dim tables
"""
abstract type AbstractDimTable <: Tables.AbstractColumns end

"""
    DimTable <: AbstractDimTable

    DimTable(A::AbstractDimArray)

Construct a Tables.jl compatible object out of an `AbstractDimArray`.

This table will have a column for the array data and columns for each
`Dimension` index, as a [`DimColumn`]. These are lazy, and generated
as required.

Column names are converted from the dimension types using
[`DimensionalData.dim2key`](@ref). This means type `Ti` becomes the
column name `:Ti`, and `Dim{:custom}` becomes `:custom`.

To get dimension columns, you can index with `Dimension` (`X()`) or
`Dimension` type (`X`) as well as the regular `Int` or `Symbol`.
"""
struct DimTable{Keys,S,DC,DAC} <: AbstractDimTable
    stack::S
    dimcolumns::DC
    dimarraycolumns::DAC
end
DimTable{K}(stack::S, dimcolumns::DC, strides::SD) where {K,S,DC,SD} =
    DimTable{K,S,DC,SD}(stack, dimcolumns, strides)
DimTable(A::AbstractDimArray, As::AbstractDimArray...) = DimTable((A, As...))
function DimTable(As::Tuple{<:AbstractDimArray,Vararg{<:AbstractDimArray}}...)
    DimTable(DimStack(As...))
end
function DimTable(s::AbstractDimStack)
    dims_ = dims(s)
    dimcolumns = map(d -> DimColumn(d, dims_), dims_)
    dimarraycolumns = map(A -> DimArrayColumn(A, dims_), s)
    keys = _colnames(s)
    DimTable{keys}(s, dimcolumns, dimarraycolumns)
end

stack(t::DimTable) = getfield(t, :stack)
dimcolumns(t::DimTable) = getfield(t, :dimcolumns)
dimarraycolumns(t::DimTable) = getfield(t, :dimarraycolumns)
dims(t::DimTable) = dims(stack(t))

for func in (:dims, :val, :index, :mode, :metadata, :order, :sampling, :span, :bounds, :locus,
             :name, :label, :units, :arrayorder, :indexorder, :relation)
    @eval $func(t::DimTable, args...) = $func(dataset(t), args...)

end

# Tables interface

Tables.istable(::DimTable) = true
Tables.columnaccess(::Type{<:DimTable}) = true
Tables.columns(t::DimTable) = t
Tables.columnnames(c::DimTable{Keys}) where Keys = Keys
function Tables.schema(t::DimTable{Keys}) where Keys
    s = stack(t)
    Tables.Schema(Keys, (map(eltype, dims(s))..., map(eltype, data(s))...))
end

@inline function Tables.getcolumn(t::DimTable{Keys}, i::Int) where Keys
    nkeys = length(Keys)
    if i > length(dims(t))
        dimarraycolumns(t)[i - length(dims(t))]
    elseif i > 0 && i < nkeys
        dimcolumns(t)[i]
    else
        throw(ArgumentError("There is no table column $i"))
    end
end
@inline function Tables.getcolumn(t::DimTable, dim::DimOrDimType)
    dimcolumns(t)[dimnum(t, dim)]
end
# Retrieve a column by name
@inline function Tables.getcolumn(t::DimTable{Keys}, key::Symbol) where Keys
    if key in keys(dimarraycolumns(t))
        dimarraycolumns(t)[key]
    else
        dimcolumns(t)[dimnum(dims(t), key)]
    end
end
@inline function Tables.getcolumn(t::DimTable, ::Type{T}, i::Int, key::Symbol) where T
    Tables.getcolumn(t, key)
end

function _colnames(s::AbstractDimStack)
    dimkeys = map(dim2key, (dims(s)))
    # The data is always the last column/s
    (dimkeys..., keys(s)...)
end


text:
function load_text(path)
    words(text) = [m.match for m in eachmatch(r"\w+", lowercase(text))]
    dict = Dict{String, Int}()
    for w in words(read(open(path), String))
        dict[w] = get(dict, w, 0) + 1
    end
    sumwords =  sum(values(dict))
    P(word) = dict[word] / sumwords

    t = Trie{Tuple{String, Float64}}()
    for word in keys(dict)
        t[word] = (word, P(word))
    end

    t
end

function correction(t::Trie, word)
    if haskey(t, word) return word end
    res = correction(t, word, 1)
    if res == word
        return correction(t, word, 2)
    else
        return res
    end
end

function correction(t::Trie, word, tolerance)
    correct_recursive(t, word, 1, tolerance, (word, 0.0))[1]
end

function correct_recursive(t::Trie, word, idx, tolerance, found)
    if tolerance == 0
        res = get(t, word[idx:end], found)
        res[2] > found[2] ? (return res) : (return found)
    end

    res = found
    # no op
    if idx <= length(word) && (word[idx] in keys(t.children))
        res = correct_recursive(t.children[word[idx]], word, idx + 1, tolerance, res)
    end

    #deletion
    res = correct_recursive(t, word, idx + 1, tolerance - 1, res)

    # insertion
    for (k, v) in t.children
        res = correct_recursive(v, word, idx, tolerance - 1, res)
    end

    #replace
    for (k, v) in t.children
        res = correct_recursive(v, word, idx + 1, tolerance - 1, res)
    end

    #transpose
    if idx <= length(word) - 1
        if (word[idx + 1] in keys(t.children)) && (word[idx] in keys(t.children[word[idx + 1]].children))
            res = correct_recursive(t.children[word[idx + 1]].children[word[idx]], word, idx + 2, tolerance - 1, res)
        end
    end
    return res
end


text:
# This file is a part of JuliaFEM.
# License is MIT: see https://github.com/JuliaFEM/FEMBeam.jl/blob/master/LICENSE

using FEMBeam
using FEMBase.Test

X = Dict(
    1 => [0.0, 0.0, 0.0],
    2 => [3.0, 0.0, 0.0])
el1 = Element(Seg2, [1, 2])
beams = [el1]
update!(beams, "geometry", X)
update!(beams, "youngs modulus", 210.0e6)
update!(beams, "shear modulus", 84.0e6)
update!(beams, "cross-section area", 20.0e-2)
update!(beams, "torsional moment of inertia y", 10.0e-5)
update!(beams, "torsional moment of inertia z", 20.0e-5)
update!(beams, "polar moment of inertia", 5.0e-5)
update!([el1], "normal", [0.0, 1.0, 0.0])
problem = Problem(Beam, "example 0", 6)
add_elements!(problem, [el1])
time = 0.0
assemble!(problem, time)
K = full(problem.assembly.K)
K_expected = [    1.4e7      0.0       0.0       0.0       0.0      0.0  -1.4e7      0.0       0.0       0.0       0.0      0.0
                  0.0    18666.7       0.0       0.0       0.0  28000.0   0.0    18666.7       0.0       0.0       0.0  28000.0
                  0.0        0.0    9333.33      0.0  -14000.0      0.0   0.0        0.0    9333.33      0.0  -14000.0      0.0
                  0.0        0.0       0.0    1400.0       0.0      0.0   0.0        0.0       0.0   -1400.0       0.0      0.0
                  0.0        0.0  -14000.0       0.0   28000.0      0.0   0.0        0.0  -14000.0       0.0   14000.0      0.0
                  0.0    28000.0       0.0       0.0       0.0  56000.0   0.0    28000.0       0.0       0.0       0.0  28000.0
                 -1.4e7      0.0       0.0       0.0       0.0      0.0   1.4e7      0.0       0.0       0.0       0.0      0.0
                  0.0    18666.7       0.0       0.0       0.0  28000.0   0.0    18666.7       0.0       0.0       0.0  28000.0
                  0.0        0.0    9333.33      0.0  -14000.0      0.0   0.0        0.0    9333.33      0.0  -14000.0      0.0
                  0.0        0.0       0.0   -1400.0       0.0      0.0   0.0        0.0       0.0    1400.0       0.0      0.0
                  0.0        0.0  -14000.0       0.0   14000.0      0.0   0.0        0.0  -14000.0       0.0   28000.0      0.0
                  0.0    28000.0       0.0       0.0       0.0  28000.0   0.0    28000.0       0.0       0.0       0.0  56000.0
]
@test isapprox(K, K_expected)


text:
function add_to_cost!(m::JuMP.AbstractModel, cost_expression::Union{JuMP.JuMP.AffExpr, JuMP.JuMP.GenericQuadExpr})

    if haskey(m.obj_dict, :objective_function)

        if (isa(m.obj_dict[:objective_function],JuMP.AffExpr) && isa(cost_expression,JuMP.AffExpr))

            JuMP.add_to_expression!(m.obj_dict[:objective_function],cost_expression)

        elseif (isa( m.obj_dict[:objective_function],JuMP.GenericQuadExpr) && isa(cost_expression,JuMP.GenericQuadExpr))

            JuMP.add_to_expression!(m.obj_dict[:objective_function],cost_expression)

        else

            m.obj_dict[:objective_function] += cost_expression

        end

    else

        m.obj_dict[:objective_function] = cost_expression

    end

end


text:
#Demo 12:
#-------------------------------------------------------------------------------
using InspectDR
using Colors
using Random
import InspectDR: DTPPOINTS_PER_INCH

#==Input
===============================================================================#

#Constants
#-------------------------------------------------------------------------------
black = RGB24(0, 0, 0)
white = RGB24(1, 1, 1)
red = RGB24(1, 0, 0)
green = RGB24(0, 1, 0)
blue = RGB24(0, 0, 1)

ppi = 300
pt2px = ppi/DTPPOINTS_PER_INCH
line_default = line(color=blue, width=1*pt2px)
line_meas = line(style=:none, color=red, width=1.5*pt2px)
line_model = line(style=:solid, color=blue, width=1.5*pt2px)
glyph_meas = glyph(shape=:o, size=3*pt2px)

#Input data
#-------------------------------------------------------------------------------
x = collect(0:100)
xmax = maximum(x)
ymodel = 0.5*x .+ 25

#Generate "Measured" data:
Random.seed!(11) #Reseed
ymeas = randn(length(x))*(xmax/10) .+ ymodel

#==Generate plot
===============================================================================#
mplot = InspectDR.Multiplot(title="")

plot = add(mplot, InspectDR.Plot2D(:lin, :lin,
	title="",
	xlabel="X-Value", ylabels=["Y-Value"])
)
_strip = plot.strips[1]
_strip.yext_full = InspectDR.PExtents1D(min=0, max=100)

wfrm = add(plot, x, ymeas, id="Measured")
	wfrm.line = line_meas
	wfrm.glyph = glyph_meas
wfrm = add(plot, x, ymodel, id="Model")
	wfrm.line = line_model

#Use IEEE stylesheets:
#NOTE: could also set style of InspectDR.defaults *before* creating plot/mplot.
InspectDR.setstyle!(plot, :IEEE, ppi=ppi, enable_legend=true)
InspectDR.setstyle!(mplot, :IEEE, ppi=ppi)

#Add annotation:
afont = plot.layout[:font_annotation]
ypos = 0.92
fstr = "Plot for IEEE publication @ $ppi DPI"
	add(plot, atext(fstr, xoffset_rel=0.5, yoffset_rel=ypos, font=afont, align=:bc, strip=1))
fstr = "No title - maximizes data area"
	add(plot, atext(fstr, xoffset_rel=0.5, yoffset_rel=ypos, font=afont, align=:tc, strip=1))
fstr = "(caption expected below)"
	add(plot, atext(fstr, xoffset_rel=0.5, yoffset_rel=1-ypos, font=afont, align=:cc, strip=1))

gplot = display(InspectDR.GtkDisplay(), mplot)

#Write from Multiplot to ensure we control dimensions of whole plot
#instead of just data area:
InspectDR.write_png("export_IEEEPlot.png", mplot)

:DONE


text:
# irfvar1.m AAA
# Luca Brugnolini 2014
# Based on Kilian code
# This function gives impulse response estimates by VAR

function irfvar1(A, SIGMA, p, H)

# Input : Slope parameter estiamtes, variance of estimated residual,
#         lag order, impulse response horizons
# Output: IRF estimates

K = size(SIGMA,1);
A = [A;[eye(K*(p-1)) zeros(K*(p-1),K)]];   # translating to companion form
J = [eye(K,K) zeros(K,K*(p-1))];
P = chol(SIGMA)';                          # recursive structure assumption
IRF = reshape((J*A^0*J'*P)',K^2,1);

for i = 1:H
IRF = [IRF reshape((J*A^i*J'*P)',K^2,1)];

end

    return IRF

end