text:
############################################################################################
"""
$(SIGNATURES)
Print summary of parameter chain and diagnostics to REPL.

# Examples
```julia
```

"""
function summary(
    trace::Trace,
    algorithmᵛ,
    model::ModelWrapper,
    sym=keys(model.val),
    burnin::Integer=trace.info.sampling.burnin,
    thinning::Integer = 1,
    printdefault::PrintDefault=PrintDefault(),
) where {S<:Union{Symbol,NTuple{k,Symbol} where k}}
    ## Print Diagnostics summary
    diagnosticssummary(trace, algorithmᵛ, nothing, burnin, thinning, printdefault)
    ## Print Chain summary
    chainsummary(trace, model, sym, Val(:text), burnin, thinning, printdefault)
    ## Return
    return nothing
end

############################################################################################
"""
$(SIGNATURES)
Sample `model` parameter given `data` with `args` algorithm. Default sampling arguments given in `default` keyword.

# Examples
```julia
```

"""
function sample(
    _rng::Random.AbstractRNG, model::M, data::D, args...; default=SampleDefault()
) where {M<:ModelWrapper,S<:SampleDefault,D}
    @unpack dataformat, chains, tempering, iterations, burnin, thinning, safeoutput,
    printoutput, printdefault, report = default
    ## Check if datatune can be created
    datatune = DataTune(data, dataformat)
    ## Check if iterations have to be adjusted if sequential data is used
    iterations = maxiterations(datatune, iterations)
    ArgCheck.@argcheck iterations > burnin "Burnin set higher than number of iterations."
    ## Check if we can capture previous samples
    updatesampler = update(datatune, tempering.adaption, args...)
    ## Construct SamplingInfo and ProgressLog
    printedparameter = PrintedParameter(showparam(model, datatune, args...)...)
    info = SamplingInfo(printedparameter, iterations, burnin, thinning, length(args), chains, updatesampler, tempering.adaption)
    progressmeter = progress(report, info)
    ## Initialize algorithms
    println("Constructing new sampler...")
    modelᵛ, algorithmᵛ, tempertune, datatuneᵛ = construct(
        _rng, model, data, default, tempering, datatune, chains, args...
    )
    ## Initialize trace
    trace = Trace(
        _rng, algorithmᵛ, model, BaytesCore.adjust(datatune, data),
        TraceInfo(tempertune, datatuneᵛ, default, info, progressmeter)
    )
    ## Loop through iterations
    println("Sampling starts...")
    propose!(_rng, trace, algorithmᵛ, modelᵛ, data)
    ## Print diagnostics
    println("Sampling finished, printing diagnostics and saving trace.")
    if printoutput
        ## Assign relevant parameter for printing and print summary to REPL.
        sym = trace.info.sampling.printedparam.printed
        summary(trace, algorithmᵛ, model, sym, trace.info.sampling.burnin, thinning, printdefault)
    end
    ## Save output
    if safeoutput
        println("Saving trace, initial model and algorithm.")
        safetrace(trace, model, algorithmᵛ, chains, iterations, burnin)
    end
    ## Return trace and algorithm
    return trace, algorithmᵛ
end

################################################################################
"""
$(SIGNATURES)
Continue sampling with all algorithms in `algorithm`.

# Examples
```julia
```

"""
function sample!(iterations::Integer,
    _rng::Random.AbstractRNG, model::M, data::D,
    trace::Trace, algorithmᵛ
) where {M<:ModelWrapper,D}
    @unpack tempertune, datatune, sampling, default = trace.info
    @unpack Nalgorithms, Nchains, burnin, thinning, captured, tempered = sampling
    @unpack safeoutput, printoutput, printdefault, report = default
    ## Create new DataTune struct, taking into account current Index and data dimension
    datatune_new = update(datatune, data)
    ## Check if iterations have to be adjusted if sequential data is used
    iterations = maxiterations(datatune_new, iterations)
    ArgCheck.@argcheck iterations > burnin "Burnin set higher than number of iterations."
    info = SamplingInfo(sampling.printedparam, iterations, burnin, thinning, Nalgorithms, Nchains, captured, tempered)
    progressmeter = progress(report, info)
    ## Construct new models for algorithms
    modelᵛ, datatuneᵛ = construct(model, datatune_new, Nchains, algorithmᵛ)
    ## Construct new trace to store new samples
    trace_new = Trace(
        _rng, algorithmᵛ, model, BaytesCore.adjust(datatune_new, data),
        TraceInfo(tempertune, datatuneᵛ, default, info, progressmeter)
    )
    ## Loop through iterations
    println("Sampling starts...")
    propose!(_rng, trace_new, algorithmᵛ, modelᵛ, data)
    ## Print diagnostics
    println("Sampling finished, printing diagnostics.")
    if printoutput
        ## Assign relevant parameter for printing and print summary to REPL.
        sym = trace_new.info.sampling.printedparam.printed
        summary(trace_new, algorithmᵛ, model, sym, trace_new.info.sampling.burnin, thinning, printdefault)
    end
    ## Save output
    if safeoutput
        println("Saving trace, initial model and algorithm.")
        safetrace(trace_new, model, algorithmᵛ, Nchains, iterations, burnin)
    end
    ## Return trace and algorithm
    return trace_new, algorithmᵛ
end

################################################################################
#export
export SampleDefault, summary, sample


text:
export 𝒯hcubature, generate_𝒯integrand

function generate_𝒯integrand(func, t; args=())
    function integrand(x)
        y = 1.0
        sv = similar(x)
        sv[1] = x[1]*t
        for i in 2:length(x)
            sv[i] = x[i]*sv[i-1]
            y = y*sv[i-1]
        end
        func(sv, args...)*y*t
    end
end

function 𝒯hcubature(f, t, ndim, args=(); rtol=1e-6, atol=1e-8)
    integrand = generate_𝒯integrand(f, t, args=args)
    a = zeros(ndim)
    b = ones(ndim)
    hcubature(integrand, a, b, rtol=rtol, atol=atol)
end

ext: jl


text:
module SmoothingKernels
    include("kernels.jl")

    kernels = [
                :uniform => uniform,
                :triangular => triangular,
                :epanechnikov => epanechnikov,
                :biweight => biweight,
                :triweight => triweight,
                :tricube => tricube,
                :gaussian => gaussian,
                :cosine => cosine,
                :logistic => logistic
              ]

    unnormalized_kernels = [
                             :uniform => uniform_unnormalized,
                             :triangular => triangular_unnormalized,
                             :epanechnikov => epanechnikov_unnormalized,
                             :biweight => biweight_unnormalized,
                             :triweight => triweight_unnormalized,
                             :tricube => tricube_unnormalized,
                             :gaussian => gaussian_unnormalized,
                             :cosine => cosine_unnormalized,
                             :logistic => logistic_unnormalized
                           ]
end


text:
function onevent(callback::Function, type::SString, canvas::GtkCanvas)
    mouse = canvas.mouse
    setfield!(mouse, Symbol(type), (args...) -> begin
        try
            if applicable(callback, args...)
                callback(args...)
            else
                callback()
            end
        catch e
            @error "Error in '$type' canvas event callback." exception=e
            Base.show_backtrace(stderr, catch_backtrace())
        end
    end)
end

function appclosed()
    app = current_app()
    destroy(app)
    destroy.(app.widgets)
    empty!(app.widgets)
    empty!(app.events)
    app.loop = false
end

function canvasfocus()
    app = current_app()
    GAccessor.focus(app.window, C_NULL)
end

struct Event{T}
    callback::Function
    Event(T, callback) = new{T}(callback)
end

function setevent(event::Event{T}) where {T}
    @warn "Event of type '$T' not recognized."
end

function setevent(event::Event{:appclosed})
    app = current_app()
    onevent(:destroy, app.window) do
        appclosed()
        event.callback()
    end
end

function setevent(event::Event{:keypress})
    app = current_app()
    onevent("key-press-event", app.window) do w, e
        try
            # If there is other element focused
            # this will throw an error
            GAccessor.focus(app.window)
        catch error
            # if not, the canvas is the current focused widget
            # and we can fire the keypress event
            event.callback(e)
        end
    end
end

for i in 1:3
    for event in [:press, :release, :motion]
        e = QuoteNode(Symbol("mouse$(i)$event"))
        ce = "button$(i)$event"
        @eval function setevent(event::Event{$e})
            onevent($ce, current_app(:canvas)) do w, e
                canvasfocus()
                event.callback((
                    pos = Point(e.x, e.y),
                    x = e.x,
                    y = e.y
                ))
            end
        end
    end
end

for (a, b) in [:mousepress => :mouse1press, :mousemotion => :mouse1motion, :mouserelease => :mouse1release]
    @eval function setevent(event::Event{$(QuoteNode(a))})
        setevent(Event($(QuoteNode(b)), event.callback))
    end
end

function setevent(event::Event{:mousemove})
    onevent(:motion, current_app(:canvas)) do w, e
        event.callback((
            pos = Point(e.x, e.y),
            x = e.x,
            y = e.y
        ))
    end
end

function setevent(event::Event{:setup})
    event.callback()
end

function setevent(event::Event{:update})
    app = current_app()
    canvas = app.canvas
    fps_interval = 1 / app.framerate
    then = time()
    app.framecount = 0

    Gtk.@guarded Gtk.draw(canvas) do c
        w, h = width(c), height(c)
        app.width = w
        app.height = h

        fps_interval = 1 / app.framerate
        now = time()
        Δt = now - then
        # @show Δt

        if Δt > fps_interval
            then = now# - (Δt % fps_interval)
            app.fps = floor(1 / Δt)

            app.drawing = Drawing(w, h, :image)
            app.drawing.cr = Gtk.getgc(c)
            app.framecount += 1
            try
                gsave()
                if applicable(event.callback, Δt)
                    event.callback(Δt)
                else
                    event.callback()
                end
                grestore()
                finish()
            catch e
                @error "Error in update function." exception=e
                Base.show_backtrace(stderr, catch_backtrace())
                app.loop = false
            end
        end
    end
end


text:
# TODO: move plot resonance to NMRSpectraSimulator

import NMRDataSetup
import NMRSpectraSimulator
import NMRSpecifyRegions

include("../src/NMRCalibrate.jl")
import .NMRCalibrate

using LinearAlgebra
using FFTW
import PyPlot
import BSON
import JLD

#import Clustering
import Statistics
import Random

include("./helpers/final_helpers.jl")
include("./helpers/solute_calibrate.jl")
include("./helpers/loop_entries1.jl")

PyPlot.close("all")
fig_num = 1

Random.seed!(25)
PyPlot.matplotlib["rcParams"][:update](["font.size" => 22, "font.family" => "serif"])

max_iters = 50000
#max_iters = 5

# dummy_SSFID = NMRSpectraSimulator.SpinSysFIDType1(0.0)
# project_name = "test_serine1"
# molecule_names = ["L-Serine";]
# w = [1.0; ]

dummy_SSFID = NMRSpectraSimulator.SpinSysFIDType1(0.0)
project_name = "test_glucose1"
molecule_names = ["D-(+)-Glucose";]
w = [1.0; ]

# dummy_SSFID = NMRSpectraSimulator.SpinSysFIDType1(0.0)
# project_name = "D-(+)-Glucose-700-r3"
# molecule_names = ["D-(+)-Glucose";]
# w = [1.0; ]

# dummy_SSFID = NMRSpectraSimulator.SpinSysFIDType2(0.0)
# project_name = "test_glucose2"
# molecule_names = ["D-(+)-Glucose";]
# w = [1.0; ]

# project_name = "D-(+)-Glucose-NRC-600"
# molecule_names = ["D-(+)-Glucose";]
# w = [1.0; ]
#
# project_name = "L-Serine-700"
# molecule_names = ["L-Serine"; "DSS"]
# w = [20.0/46; 1.0] # BMRB: DSS is 1 % => 46 mM
#
# project_name = "D-(+)-Glucose-700"
# molecule_names = ["D-(+)-Glucose"; "DSS"]
# w = [20.0/4.6; 1.0] # BMRB: DSS is 0.1 % => 4.6 mM
#
# project_name = "L-Isoleucine-700"
# molecule_names = ["L-Isoleucine"; "DSS"]
# w = [20.0/46; 1.0] # BMRB: DSS is 1 % => 46 mM
#
# project_name = "L-Leucine-500-2mM"
# molecule_names = ["L-Leucine";]
# w = [1.0]
#
# project_name = "L-Glutamine-700"
# molecule_names = ["L-Glutamine"; "DSS"]
# w = [20.0/0.5; 1.0] # BMRB: DSS is 500 uM => 0.5 mM

projects_dir = "/home/roy/MEGAsync/outputs/NMR/calibrate/final"
base_path_JLD = "/home/roy/Documents/repo/NMRData//src/input/molecules"
cs_config_path = "/home/roy/Documents/repo/NMRData/src/input/reduced_cs_config.txt"

println("Now on $(project_name)")

PyPlot.close("all")
fig_num = 1

Random.seed!(25)
PyPlot.matplotlib["rcParams"][:update](["font.size" => 22, "font.family" => "serif"])

### user inputs.
# 0.1% DSS is 0.0046 M = 4.6 mM.
save_folder_path = joinpath(projects_dir, project_name)
#@assert 1==2

w = w ./ norm(w) # since the fit data, y, is normalized.

# proxy-related.
tol_coherence = 1e-2
α_relative_threshold = 0.05
#Δc_partition_radius = 1e-1 # 17 element partition for glucose spin group 2.
Δc_partition_radius_candidates = [1e-1; 0.3; 0.5; 0.7; 0.8; 0.9]
#Δc_partition_radius_candidates = [0.9;]
λ0 = 3.4
Δcs_max = 0.2 # for proxy.
κ_λ_lb = 0.5
κ_λ_ub = 2.5

# if a Δc_partition_radius candidate gives max partition sizes less than of
#   equal to this value, then use that candidate.
early_exit_part_size = 7

### end inputs.

## load data.
load_folder_path = joinpath(projects_dir, project_name)
load_path = joinpath(load_folder_path, "$(project_name).bson")
dic = BSON.load(load_path)
s_t = dic[:s_t]
fs = dic[:fs]
SW = dic[:SW]
ν_0ppm = dic[:ν_0ppm]

hz2ppmfunc = uu->(uu - ν_0ppm)*SW/fs
ppm2hzfunc = pp->(ν_0ppm + pp*fs/SW)

offset_Hz = ν_0ppm - (ppm2hzfunc(0.3)-ppm2hzfunc(0.0))

N = length(s_t)
DFT_s = fft(s_t)
U_DFT, U_y, U_inds = NMRDataSetup.getwraparoundDFTfreqs(N, fs, offset_Hz)

Z = maximum(abs.(DFT_s))
y = DFT_s[U_inds] ./ Z

#
S_U = DFT_s[U_inds]
P_y = hz2ppmfunc.(U_y)

PyPlot.figure(fig_num)
fig_num += 1

PyPlot.plot(P_y, real.(S_U), label = "data spectrum")

PyPlot.legend()
PyPlot.xlabel("ppm")
PyPlot.ylabel("real")
PyPlot.title("data spectra")

####### mixture proxy.
Δcs_max_mixture = collect( Δcs_max for i = 1:length(molecule_names))

println("Timing: trydiffΔcradius()")
@time mixture_params, Δc_partition_radius = NMRCalibrate.trydiffΔcradius(Δc_partition_radius_candidates,
    molecule_names, base_path_JLD, Δcs_max_mixture, hz2ppmfunc, ppm2hzfunc,
    fs, SW, λ0, ν_0ppm, early_exit_part_size, Δcs_max, tol_coherence,
    α_relative_threshold, dummy_SSFID)
As = mixture_params

ΩS_ppm = NMRSpectraSimulator.getPsnospininfo(As, hz2ppmfunc)
ΩS_ppm_sorted = sort(NMRSpectraSimulator.combinevectors(ΩS_ppm))

println("$(project_name): Partition sizes:")
display(NMRCalibrate.displaypartitionsizes(As[1]))
println("Δc_partition_radius = ", Δc_partition_radius)
println()

#@assert 1==2

# u_min = ppm2hzfunc(-0.5)
# u_max = ppm2hzfunc(3.0)

u_offset = 0.5
u_min = ppm2hzfunc(ΩS_ppm_sorted[1] - u_offset)
u_max = ppm2hzfunc(ΩS_ppm_sorted[end] + u_offset)

println("Timing: fitproxies!()")
@time NMRSpectraSimulator.fitproxies!(As;
κ_λ_lb = κ_λ_lb,
κ_λ_ub = κ_λ_ub,
u_min = u_min,
u_max = u_max,
Δr = 1.0,
Δκ_λ = 0.05)

######## end mixture proxy.

N_d = sum( NMRCalibrate.getNd(As[n]) for n = 1:length(As) )
#N_β = sum( NMRCalibrate.getNβ(As[n]) for n = 1:length(As) )
N_λ = sum( NMRCalibrate.getNλ(As[n]) for n = 1:length(As) )

####### cost func.
combinevectors = NMRSpectraSimulator.combinevectors

Δsys_cs, y_cost_all, U_cost_all, P_cost_all, exp_info, cost_inds,
cost_inds_set = NMRCalibrate.prepareoptim(cs_config_path, molecule_names, hz2ppmfunc,
U_y, y, As; region_min_dist = 0.1)

# visualize cost regions.
# sort(P_cost_set[1]) # etc..
P_cost_set = collect( P_y[cost_inds_set[r]] for r = 1:length(cost_inds_set) )

### optim all regions. # 900 secs.
y_cost = y_cost_all
U_cost = U_cost_all
P_cost = P_cost_all

PyPlot.figure(fig_num)
fig_num += 1

PyPlot.plot(P_y, real.(y), label = "data spectrum")
PyPlot.plot(P_cost, real.(y_cost), "^", label = "positions")

PyPlot.legend()
PyPlot.xlabel("ppm")
PyPlot.ylabel("real")
PyPlot.title("positions against data spectrum, real part")

#@assert 1==2

Δ_shifts = NMRSpectraSimulator.combinevectors(Δsys_cs)

##### set up updates.

P = LinRange(hz2ppmfunc(u_min), hz2ppmfunc(u_max), 50000)
U = ppm2hzfunc.(P)
U_rad = U .* (2*π)
#ΩS_ppm = collect( hz2ppmfunc.( NMRSpectraSimulator.combinevectors(A.Ωs) ./ (2*π) ) for A in mixture_params )

## parameters that affect qs.
# A.ss_params.d, A.ss_params.κs_λ, A.ss_params.κs_β
# A.d_singlets, A.αs_singlets, A.Ωs_singlets, A.β_singlets, A.λ0, A.κs_λ_singlets
# purposely perturb κ.

Es = collect( NMRSpectraSimulator.κCompoundFIDType(As[i]) for i = 1:length(As) )

κ_lb_default = 0.2
κ_ub_default = 50.0

# TODO here, change shift delta cs to each group.

# default, type2.
γ = 0.001 # in ppm.
shift_constants = (Δsys_cs, γ)

# type1.
if typeof(As) == Vector{NMRSpectraSimulator.CompoundFIDType{Float64, NMRSpectraSimulator.SpinSysFIDType1{Float64}}}
    shift_constants = Δ_shifts
end

r = 3 # for glucose nrc.

if project_name == "test_serine1"
    r = 1 # for serine 700.
end
y_cost = y[cost_inds_set[r]]
U_cost = U_y[cost_inds_set[r]]
P_cost = P_y[cost_inds_set[r]]

U_rad_cost = U_cost .* (2*π)

###
LS_inds = 1:length(U_cost)

println("Timing: runalignment(), r = $(r)")
@time p_star, q, κ_BLS, getshiftfunc, getβfunc, getλfunc,
obj_func, N_vars_set, p_initial = NMRCalibrate.runalignment(shift_constants,
U_rad_cost, y_cost, LS_inds, Es, As, fs, SW;
max_iters = max_iters,
xtol_rel = 1e-5,
ftol_rel = 1e-5,
w = w,
κ_lb_default = κ_lb_default,
κ_ub_default = κ_ub_default,
λ_each_lb = 0.9,
λ_each_ub = 1.2)

p_star
κ_BLS
q

d_star = getshiftfunc(p_star)
β_star = getβfunc(p_star)
λ_star = getλfunc(p_star)

###### visualize.
T = Float64

import PlotlyJS
import Plots
Plots.plotly()

projects_dir = "/home/roy/MEGAsync/outputs/NMR/calibrate/final/$(project_name)"
plots_save_folder = joinpath(projects_dir, "plots")
isdir(plots_save_folder) || mkdir(plots_save_folder)
project_title = "test" # TODO change later.

# prep initial.
initial_cost = obj_func(p_initial)
NMRCalibrate.parseκ!(Es, ones(T, length(κ_BLS)))
fill!(w, 1.0)
q_initial_U = q.(U_rad)
#println("norm(q_initial_U) = ", norm(q_initial_U))

final_cost = obj_func(p_star)
q_final_U = q.(U_rad)

PyPlot.figure(fig_num)
fig_num += 1

PyPlot.plot(P_y, real.(y), label = "y")
PyPlot.plot(P_cost, real.(y_cost), "x")

PyPlot.legend()
PyPlot.xlabel("ppm")
PyPlot.ylabel("real")
PyPlot.title("r = $(r). cost vs. data (y)")

PyPlot.figure(fig_num)
fig_num += 1

PyPlot.plot(P, real.(q_initial_U), label = "q initial")
PyPlot.plot(P_y, real.(y), label = "y")
PyPlot.plot(P, real.(q_final_U), "--", label = "q final")
#PyPlot.plot(P_cost, real.(y_cost), "x")

PyPlot.legend()
PyPlot.xlabel("ppm")
PyPlot.ylabel("real")
PyPlot.title("r = $(r). data (y) vs. fit")

plots_save_path = joinpath(plots_save_folder, "region_$(r)_real.html")
title_string = "$(project_title): region $(r), real"
savefigfitresult(plots_save_path, title_string,
 real.(q_final_U), P, P_cost, real.(y_cost);
initial_fit = real.(q_initial_U))

plots_save_path = joinpath(plots_save_folder, "region_$(r)_imaginary.html")
title_string = "$(project_title): region $(r), imaginary"
savefigfitresult(plots_save_path, title_string,
imag.(q_final_U), P, P_cost, imag.(y_cost);
initial_fit = imag.(q_initial_U))

plots_save_path = joinpath(plots_save_folder, "region_$(r)_modulus.html")
title_string = "$(project_title): region $(r), modulus"
savefigfitresult(plots_save_path, title_string,
abs.(q_final_U), P, P_cost, abs.(y_cost);
initial_fit = abs.(q_initial_U))

### save.
save_path = joinpath(save_folder_path, "results_$(r).bson")
BSON.bson(save_path,
p_star = p_star,
κ_lb_default = κ_lb_default,
κ_ub_default = κ_ub_default,
κ_star = κ_BLS,
d_star = d_star,
β_star = β_star,
λ_star = λ_star,
w = w,
# proxy setup-related below.
Δc_partition_radius = Δc_partition_radius,
tol_coherence = tol_coherence,
α_relative_threshold = α_relative_threshold,
λ0 = λ0,
Δcs_max = Δcs_max,
κ_λ_lb = κ_λ_lb,
κ_λ_ub = κ_λ_ub,
# experiement/cost-related below.
cost_inds_set = cost_inds_set,
Δsys_cs = Δsys_cs,
y_cost = y_cost,
U_cost = U_cost,
P_cost = P_cost,
As = As,
Es = Es,
y = y,
U_y = U_y,
fs = fs,
SW = SW,
ν_0ppm = ν_0ppm)


text:
"""
Johnson Lindenstrauss effective resistance upperbounding. If retXhat is set to true,
returns the vectors necessary for computing the effective resistance bounds instead
of the actual bounds.

~~~julia
	johnlind(a::SparseMatrixCSC{Tv,Ti}; eps::Tv = 0.5, solver=(la -> augTreeSolver(la,tol=1e-1,maxits=1000,maxtime=10)), retXhat::Bool = false)
~~~
"""

function johnlind{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti};
						eps::Tv = 0.5,
						solver=(la -> augTreeSolver(la,tol=1e-1,maxits=1000,maxtime=10)),
						retXhat::Bool = false)

	n = a.n
	m = length(a.nzval)
	dhat = ceil(Int64, 4 * log(m) / eps^2)

	P = ones(dhat, m)
	for i in 1:dhat
		for j in 1:m
			if rand() < 1/2
				P[i,j] = -P[i,j]
			end
		end
	end
	# we want ||P * x|| = ||x||
	P = P / sqrt(dhat)

	# compute B
	U = Ti[]
	V = Ti[]
	W = Tv[]

	pos = 1
	for i in 1:length(a.nzval)
		while a.colptr[pos + 1] <= i
			pos = pos + 1
		end

		p = a.rowval[i]
		q = pos
		w = sqrt(a.nzval[i])

		if p > q
			aux = p
			p = q
			q = aux
		end

		# multiply B by W
		push!(U, i)
		push!(V, p)
		push!(W, w)

		push!(U, i)
		push!(V, q)
		push!(W, -w)
	end

	B = sparse(U, V, W)

	# Get bs = P * W^(1/2) * B. We already multiplied W by B. Solve for each line. dims are dhat x n
	bs = P * B / sqrt(2)

	f = solver(lap(a) + speye(a.n) * 1e-10)

	# xhat = P * W^(1/2) * B * L ^-1 * ei
	xhat = zeros(n, dhat)
	for i in 1:dhat
		b = reshape(bs[i,:], n)
		b = b - mean(b)
		xhat[:,i] = f(b)
	end

	if retXhat
		return xhat
	end

	# compute the effective resistance
	reff = copy(a)
	pos = 1
	for i in 1:m
		while a.colptr[pos + 1] <= i
			pos = pos + 1
		end

		p = a.rowval[i]
		q = pos

		reff.nzval[i] = norm(xhat[p,:] - xhat[q,:])^2
	end

	return reff

end


text:
using Test

using MathOptInterface
const MOI = MathOptInterface
const MOIT = MathOptInterface.Test
const MOIU = MathOptInterface.Utilities
const MOIB = MathOptInterface.Bridges

include("../utilities.jl")

@testset "Indicator activated on 0" begin
    # linear problem with indicator constraint
    # similar to indicator1_test with reversed z1
    # max  2x1 + 3x2
    # s.t. x1 + x2 <= 10
    #      z1 == 0 ==> x2 <= 8
    #      z2 == 1 ==> x2 + x1/5 <= 9
    #      (1-z1) + z2 >= 1 <=> z2 - z1 >= 0
    model = MOIU.MockOptimizer(MOIU.Model{Float64}())
    config = MOIT.TestConfig()

    x1 = MOI.add_variable(model)
    x2 = MOI.add_variable(model)
    z1 = MOI.add_variable(model)
    z2 = MOI.add_variable(model)

    vc1 = MOI.add_constraint(model, z1, MOI.ZeroOne())
    @test vc1.value == z1.value
    vc2 = MOI.add_constraint(model, z2, MOI.ZeroOne())
    @test vc2.value == z2.value
    f1 = MOI.VectorAffineFunction(
        [
            MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, z1)),
            MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(1.0, x2)),
        ],
        [0.0, 0.0],
    )
    iset1 = MOI.IndicatorSet{MOI.ACTIVATE_ON_ZERO}(MOI.LessThan(8.0))

    BT = MOIB.Constraint.concrete_bridge_type(
        MOIB.Constraint.IndicatorActiveOnFalseBridge{Float64},
        typeof(f1),
        typeof(iset1),
    )
    BT2 = MOIB.Constraint.concrete_bridge_type(
        MOIB.Constraint.IndicatorActiveOnFalseBridge,
        typeof(f1),
        typeof(iset1),
    )
    bridge = MOIB.Constraint.bridge_constraint(BT, model, f1, iset1)

    @test BT === BT2
    @test bridge isa BT

    z1comp = bridge.variable_index
    @test MOI.get(model, MOI.ConstraintFunction(), bridge.zero_one_cons) ==
          MOI.SingleVariable(z1comp)
    @test MOI.get(model, MOI.ConstraintSet(), bridge.disjunction_cons) ==
          MOI.EqualTo(1.0)
    disjunction_cons =
        MOI.get(model, MOI.ConstraintFunction(), bridge.disjunction_cons)
    for t in disjunction_cons.terms
        @test t.variable_index == z1 || t.variable_index == z1comp
        @test t.coefficient ≈ 1.0
    end
end


text:
module JuSwarm

# Dependancies
using StaticArrays
using Format
using StatsBase
using LinearAlgebra
using Optim

# Includes
include("PSOOptions.jl")
include("Particle.jl")
include("Swarm.jl")
include("initswarmuniform.jl")
include("psoptimize.jl")
include("PSOSolution.jl")

export PSOOptions
export psoptimize

end

max_stars_repo_name: GrantHecht/JuSwarm

max_forks_repo_name: GrantHecht/JuSwarm


text:
"""
Load/setup common configurations, variables, helper functions and packages.
"""

using DataFrames, Query, Dates, CSV, YAML
using Statistics, Distributions
using Plots, StatsPlots
using Streamfall

using Distributed, BlackBoxOptim

Plots.gr(fmt=:png)

CPU_THREADS = Sys.CPU_THREADS
CPU_CORES = Int(CPU_THREADS / 2)

DATA_PATH = "../data/"
FIG_PATH = "../figures/"

TRAINING_TIME = 60*60*24
CALIB_TIME = 60*60*24  # calibration time in seconds
DATE_FORMAT = "YYYY-mm-dd"

#### common data set up
function setup_network(spec)
    network = YAML.load_file(joinpath(DATA_PATH, spec))
    sn = create_network("Gingera Catchment", network)

    # Override base run function as we're using temperature data
    n_id, node = sn["410730"]

    return sn, n_id
end

function include_everywhere(filepath)
    fullpath = joinpath(@__DIR__, filepath)
    @sync for p in procs()
        @async remotecall_wait(include, p, fullpath)
    end
end

# Load data
FULL_DATASET = DataFrame(CSV.File(joinpath(DATA_PATH, "CAMELS-AUS_410730.csv"), comment="#", dateformat=DATE_FORMAT))
const CALIB_LN = 11134

CALIB_OBS = FULL_DATASET[1:CALIB_LN, "410730_Q"]
VALID_OBS = FULL_DATASET[CALIB_LN+1:end, "410730_Q"]

CALIB_FLOW = Dict(
    "410730" => CALIB_OBS
)

VALID_FLOW = Dict(
    "410730" => VALID_OBS
)

CALIB_CLIMATE = Climate(FULL_DATASET[1:CALIB_LN, ["410730_P", "410730_PET"]], "_P", "_PET")
FULL_CLIMATE = Climate(FULL_DATASET, "_P", "_PET")

CALIB_DATES = FULL_DATASET[1:CALIB_LN, "Date"]
VALID_DATES = FULL_DATASET[CALIB_LN+1:end, "Date"]

APPROACHES = ["NNSE", "NmKGE", "mean_NmKGE", "NnpKGE",  # "RMSE",
              "split_NNSE", "split_NmKGE", "split_mean_NmKGE", "split_NnpKGE"]  # "split_RMSE",

split_NNSE = (obs, sim) -> Streamfall.naive_split_metric(obs, sim; metric=Streamfall.NNSE)
split_NmKGE = (obs, sim) -> Streamfall.naive_split_metric(obs, sim; metric=Streamfall.NmKGE)
# split_RMSE = (obs, sim) -> Streamfall.naive_split_metric(obs, sim; metric=Streamfall.RMSE)
split_mean_NmKGE = (obs, sim) -> Streamfall.naive_split_metric(obs, sim; metric=Streamfall.mean_NmKGE)
split_NnpKGE = (obs, sim) -> Streamfall.naive_split_metric(obs, sim; metric=Streamfall.NnpKGE)

OBJFUNCS = [
    Streamfall.NNSE, Streamfall.NmKGE, Streamfall.mean_NmKGE,  # , Streamfall.RMSE
    Streamfall.NnpKGE,
    split_NNSE, split_NmKGE, split_mean_NmKGE,  # split_RMSE
    split_NnpKGE
]

"""
For backwards-compatibility.

Earlier approach defined an additional bin for values which were greater than historic extremes.
Because the approach now uses an adaptive "online" approach, this is no longer necessary as the
extreme always fall into the furthest (right-most) bin.
"""
function find_state_vars(cmd, thresholds, params, num_params)
    n_states = length(thresholds) + 1

    return find_state_vars(cmd, thresholds, params, num_params, n_states)
end

function find_state_vars(val, thresholds, params, num_params, n_states)
    # index of parameters (e.g., if 3 partitions, then create 3 arrays of 8 parameters)
    param_idxs = collect(Iterators.partition(1:length(params), num_params))

    lower = thresholds[1]
    thresholds = thresholds[2:end]
    set_id = n_states
    for (idx, step) in enumerate(thresholds)
        if (lower <= val < step)
            set_id = idx
            break
        end

        lower = step
    end

    param_set = param_idxs[set_id]
    node_params = params[param_set]

    return set_id, node_params, param_idxs
end

function report_metrics(obs, sim)
    rmse = Streamfall.RMSE(obs, sim)
    nse = Streamfall.NSE(obs, sim)
    mKGE = Streamfall.mKGE(obs, sim)
    mean_NmKGE = Streamfall.mean_NmKGE(obs, sim)
    NnpKGE = Streamfall.NnpKGE(obs, sim)
    pbias = Streamfall.PBIAS(obs, sim)
    rsr = Streamfall.RSR(obs, sim)
    npKGE = Streamfall.npKGE(obs, sim)

    split_NSE = Streamfall.naive_split_metric(obs, sim; metric=Streamfall.NSE)
    split_mKGE = Streamfall.naive_split_metric(obs, sim; metric=Streamfall.mKGE)
    split_RMSE = Streamfall.naive_split_metric(obs, sim; metric=Streamfall.RMSE)
    split_NNSE = Streamfall.naive_split_metric(obs, sim; metric=Streamfall.NNSE)
    split_NmKGE = Streamfall.naive_split_metric(obs, sim; metric=Streamfall.NmKGE)
    split_mean_NmKGE = Streamfall.naive_split_metric(obs, sim; metric=Streamfall.mean_NmKGE)
    split_NnpKGE = Streamfall.naive_split_metric(obs, sim; metric=Streamfall.NnpKGE)

    nnse = Streamfall.NNSE(obs, sim)
    nmkge = Streamfall.NmKGE(obs, sim)

    @info "Metrics:" nnse nmkge mean_NmKGE NnpKGE rmse nse mKGE npKGE pbias rsr split_NNSE split_NmKGE split_RMSE split_mean_NmKGE split_NSE split_mKGE split_NnpKGE
end


text:
include("gs.jl")

using .GS

nos = [No(1, 1, -1; rx=true, ry=true),
       No(2, 3, -1),
       No(3, 5, -1),
       No(4, 7, -1; rx=true, ry=true),
       No(5, 3, 1; fx=10, fy=-5),
       No(6, 5, 1)]

# Material
material = Material(1, 100e6)

# Elementos
area = 0.0014
elementos = [Elemento(1, nos[1], nos[2], area, material),
             Elemento(2, nos[1], nos[5], area, material),
             Elemento(3, nos[2], nos[3], area, material),
             Elemento(4, nos[2], nos[6], area, material),
             Elemento(5, nos[4], nos[3], area, material),
             Elemento(6, nos[5], nos[2], area, material),
             Elemento(7, nos[6], nos[3], area, material),
             Elemento(8, nos[6], nos[4], area, material),
             Elemento(9, nos[6], nos[5], area, material)]

# Estrutura
estrutura = Estrutura(nos, elementos)
print(deslocamentos(estrutura))
plotar_estrutura_inicial(estrutura)

max_stars_repo_name: lorranfoliveira/ge

max_issues_repo_name: lorranfoliveira/ge

max_forks_repo_name: lorranfoliveira/ge


text:
using Documenter, Weber, Colors
makedocs(
  modules = [Weber],
  format = :html,
  sitename = "Weber.jl",
  html_prettyurls = true,
  pages = Any[
    "Introduction" => "index.md",
    "User guide" => Any[
      "Getting Started" => "start.md",
      "Trial Creation" => "trial_guide.md",
      "Stimulus Generation" => "stimulus.md",
      "Adaptive Tracks" => "adaptive.md",
      "Advanced Experiments" => "advanced.md",
      "Extending Weber" => "extend.md"
    ],
    "Reference" => Any[
      "Experiments" => "experiment.md",
      "Trials" => "trials.md",
      "Video" => "video.md",
      "Events" => "event.md",
      "Extensions" => "extend_ref.md"
    ]
  ]
)
deploydocs(
  repo = "github.com/haberdashPI/Weber.jl.git",
  julia = "0.6",
  osname = "osx",
  deps = nothing,
  make = nothing,
  target = "build"
)


text:
using Documenter
using TwoPointFunctions

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

makedocs(;
    modules=[TwoPointFunctions],
    authors="Jishnu Bhattacharya",
    repo="https://github.com/jishnub/TwoPointFunctions.jl/blob/{commit}{path}#L{line}",
    sitename="TwoPointFunctions.jl",
    format=Documenter.HTML(;
        prettyurls=get(ENV, "CI", "false") == "true",
        canonical="https://jishnub.github.io/TwoPointFunctions.jl",
        assets=String[],
    ),
    pages=[
        "Reference" => "index.md",
    ],
)

deploydocs(;
    repo="github.com/jishnub/TwoPointFunctions.jl",
)


text:
# Outputs the value of the objective function at x.
function mixobjective(L, x, eps = 0)
  return -sum(log.(L * x .+ eps))
end

# Fit a mixture model using the sequential quadratic programming
# algorithm ("mix-SQP"). Input argument L is the n x m conditional
# likelihood matrix, where n is the number of samples and m is the
# number of mixture components. Optional input argument x is the
# initial estimate of the mixture weights (when one or more entries of
# x are negative, the default initial estimate is used in which all
# the entries are set to 1/k, where k = size(L,2).
function mixSQP(L; x = -1, convtol = 1e-8, pqrtol = 1e-8, eps = 1e-8,
                sptol = 1e-6, maxiter = 100, maxqpiter = 100,
                lowrank = "svd", qpsubprob = "activeset",
                verbose = true)

  # Get the number of rows (n) and columns (k) of L.
  n = size(L,1);
  k = size(L,2);

  # If any entries of input x are negative, set the initial estimate
  # to the default setting. When the MOSEK solver is used, the initial
  # estimate is set to a sparse vector in which all but two of the
  # entries are zero.
  if any(x .< 0)
    x = ones(k)/k;
  end

  # If requested (i.e., if pqrtol > 0), compute the partial QR
  # decomposition with relative precision "tol", then retrieve the
  # permutation matrix, P. For details on the partial QR, see
  # https://github.com/klho/LowRankApprox.jl.
  if lowrank != "none" && qpsubprob == "mosek"
    error("lowrank must be \"none\" when qpsubprob = \"mosek\"");
  end
  if lowrank == "qr"
    F = pqrfact(L, rtol = pqrtol);
    P = sparse(F[:P]);
  elseif lowrank == "svd"
    F = psvdfact(L, rtol = pqrtol);
    S = Diagonal(F[:S]);
  end

  # Summarize the analysis here.
  if verbose
    @printf("Running SQP algorithm with the following settings:\n")
    @printf("- %d x %d data matrix\n",n,k)
    @printf("- convergence tolerance  = %0.2e\n",convtol)
    @printf("- zero threshold         = %0.2e\n",sptol)
    if lowrank == "qr"
      err = maximum(abs.(F[:Q]*F[:R]*P' - L));
      @printf("- partial QR tolerance   = %0.2e\n",pqrtol)
      @printf("- partial QR max. error  = %0.2e\n",err)
    elseif lowrank == "svd"
      err = maximum(abs.(F[:U]*S*F[:Vt] - L));
      @printf("- partial SVD tolerance  = %0.2e\n",pqrtol)
      @printf("- partial SVD max. error = %0.2e\n",err)
    else
      @printf("- Exact derivative computation (partial QR not used).\n")
    end
  end

  # Initialize storage for the outputs obj, gmin, nnz and nqp.
  obj      = zeros(maxiter);
  gmin     = zeros(maxiter);
  nnz      = zeros(Int,maxiter);
  nqp      = zeros(Int,maxiter);
  timing   = zeros(maxiter);
  qptiming = zeros(maxiter);

  # Print the column labels for reporting the algorithm's progress.
  if verbose
    @printf("iter      objective -min(g+1)  #nz #qp #ls\n")
  end

  # Repeat until we reach the maximum number of iterations, or until
  # convergence is reached.
  numiter = 0;
  numls   = 0;
  for i = 1:maxiter
    numiter = i;

    out, timing[i] = @timed begin

      # Compute the gradient and Hessian, optionally using the partial
      # QR decomposition to increase the speed of these computations.
      # gradient and Hessian computation -- Rank reduction method
      if lowrank == "qr"
        D = 1 ./ (F[:Q]*(F[:R]*(P'*x)) .+ eps);
        g = -P * (F[:R]' * (F[:Q]'*D)/n);
        H = P * (F[:R]' * (F[:Q]'*Diagonal(D.^2)*F[:Q])*F[:R])*P'/n +
            eps*Diagonal(ones(k));
      elseif lowrank == "svd"
        D = 1 ./ (F[:U]*(S*(F[:Vt]*x)) .+ eps);
        g = -F[:Vt]'*(S * (F[:U]'*D))/n;
        H = (F[:V]*S*(F[:U]'*Diagonal(D.^2)*F[:U])* S*F[:Vt])/n +
            eps*Diagonal(ones(k));
      else
        D = 1 ./ (L*x .+ eps);
        g = -L'*D/n;
        H = L'*Diagonal(D.^2)*L/n .+ eps * Diagonal(ones(k));
      end

      # Report on the algorithm's progress.
      obj[i]  = mixobjective(L,x,eps);
      gmin[i] = minimum(g) .+ 1;
      nnz[i]  = length(findall(x .> sptol));
      if verbose
        if i == 1
          @printf("%4d %0.8e %+0.2e %4d\n",i,obj[i],-gmin[i],nnz[i]);
        else
          @printf("%4d %0.8e %+0.2e %4d %3d %3d\n",i,obj[i],
                  -gmin[i],nnz[i],nqp[i - 1],numls);
        end
      end

      # Check convergence of outer loop.
      if gmin[i] >= -convtol
        break
      end

      # Solve the QP subproblem using either the active-set or
      # interior-point (MOSEK) method.
      out, qptiming[i] = @timed if qpsubprob == "activeset"
        y, nqp[i] = qpactiveset(x,g,H,convtol = convtol,sptol = sptol,
                                maxiter = maxqpiter);
      elseif qpsubprob == "mosek"
        y = qpmosek(x,g,H);
      end

      # Perform backtracking line search
      for t = 1:10
        numls = t;
        if lowrank == "qr"
          D_new = 1 ./ (F[:Q]*(F[:R]*(P'*y)) .+ eps);
        elseif lowrank == "svd"
          D_new = 1 ./ (F[:U]*(S*(F[:Vt]*y)) .+ eps);
        else
          D_new = 1 ./ (L*y .+ eps);
        end
        if all(D_new .> 0)
          if sum(log.(D)) - sum(log.(D_new)) > sum((x - y) .* g) / 2
            break
          end
        end
        y = (y-x)/2 + x;
      end

      numiter = i;
    end

    # Update the solution to the original optimization problem.
    x = copy(y);
  end

  # Return: (1) the solution (after zeroing out any values below the
  # tolerance); (2) the value of the objective at each iteration; (3)
  # the minimum gradient value of the modified objective at each
  # iteration; (4) the number of nonzero entries in the vector at each
  # iteration; (5) the number of inner iterations taken to solve the
  # QP subproblem at each outer iteration; (6) the total runtime for
  # each iteration; and (7) the runtime for solving the quadratic
  # subproblem at each iteration.
  x[x .< sptol] .= 0;
  x = x/sum(x);
  return Dict([("x",Vector(x)),
               ("obj",obj[1:numiter]),
               ("gmin",gmin[1:numiter]),
               ("nnz",nnz[1:numiter]),
               ("nqp",nqp[1:numiter]),
               ("timing",timing[1:numiter]),
               ("qptiming",qptiming[1:numiter])])
end

# Solve the QP subproblem for the mix-SQP algorithm using an active
# set method.
function qpactiveset(x, g, H; convtol = 1e-8, sptol = 1e-6, maxiter = 100)

  # Get the number of degrees of freedom in the optimization problem.
  k = length(x);

  # Initialize the solution to the QP subproblem.
  y       = zeros(k);
  ind     = findall(x .> sptol);
  y[ind] .= 1/length(ind);

  # Repeat until we reach the maximum number of iterations, or until
  # convergence is reached.
  numiter = 0;
  for i = 1:maxiter
    numiter = i;

    # Define the smaller QP subproblem.
    s   = length(ind);
    H_s = H[ind,ind];
    d   = H*y + 2*g .+ 1;
    d_s = d[ind];

    # Solve the smaller problem.
    p      = zeros(k);
    p_s    = -H_s \ d_s;
    p[ind] = p_s;

    # Check convergence using KKT
    if norm(p_s) < convtol

      # Compute the Lagrange multiplier.
      z = d;
      if all(z .>= -convtol)
        break
      elseif length(ind) < k
        notind  = setdiff(1:k,ind);
        ind_min = notind[findmin(z[notind])[2]];
        ind     = sort([ind; ind_min]);
      end
    else

      # Find a feasible step length.
      alpha     = 1;
      alpha0    = -y[ind] ./ p_s;
      ind_block = findall(p_s .< 0);
      alpha0    = alpha0[ind_block];
      if ~isempty(ind_block)
        v, t = findmin(alpha0);
        if v < 1

          # Blocking constraint.
          ind_block = ind[ind_block[t]];
          alpha     = v;

          # Update working set if there is a blocking constraint.
          deleteat!(ind,findall(ind .- ind_block .== 0));
        end
      end

      # Move to the new "inner loop" iterate (y) along the search
      # direction.
      y = y + alpha * p;
    end
  end

  # Return the solution to the quadratic program.
  return y, numiter
end

# Solve the QP subproblem for the mix-SQP algorithm using MOSEK.
function qpmosek(x, g, H)
  k      = length(g);
  y      = copy(x);
  bkx    = repeat([MSK_BK_LO],k,1)[:];
  blx    = zeros(k);
  bux    = Inf * ones(k);
  numvar = length(bkx);
  c      = 2*g .+ 1;

  maketask() do task
    appendvars(task,numvar)
    putclist(task,[1:numvar;],c)
    putvarboundslice(task,1,numvar+1,bkx,blx,bux)
    Q    = LowerTriangular(H);
    ind  = findall(Q .> 0);
    a    = (i->i[1]).(ind)
    b    = (i->i[2]).(ind)
    putqobj(task,a,b,Q[ind])
    putobjsense(task,MSK_OBJECTIVE_SENSE_MINIMIZE)
    optimize(task)
    y = getxx(task,MSK_SOL_ITR)
  end
  return y
end

max_stars_repo_name: stephenslab/mixsqp-paper

max_issues_repo_name: stephenslab/mixsqp-paper

max_forks_repo_name: stephenslab/mixsqp-paper


text:
function moments(entity::HomographyMatrix, 𝒟::Tuple{AbstractArray, Vararg{AbstractArray}})
    # ʹ : CTRL + SHIFT + 02b9
    ℳ, ℳʹ = collect(𝒟)
    N = length(ℳ)
    if (N != length(ℳʹ))
           throw(ArgumentError("There should be an equal number of points for each view."))
    end
    𝐀 =  @SMatrix zeros(9,9)
    for n = 1:N
        𝐦  = hom(ℳ[n])
        𝐦ʹ = hom(ℳʹ[n])
        𝐔 = -𝐦 ⊗ vec2antisym(𝐦ʹ)
        𝐀 = 𝐀 + 𝐔*𝐔'
    end
    𝐀/N
end


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

struct SchedulingV1Api <: SwaggerApi
    client::Swagger.Client
end

function _swaggerinternal_createSchedulingV1PriorityClass(_api::SchedulingV1Api, body; pretty=nothing, dryRun=nothing, fieldManager=nothing, _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "POST", IoK8sApiSchedulingV1PriorityClass, "/apis/scheduling.k8s.io/v1/priorityclasses", ["BearerToken"], body)
    Swagger.set_param(_ctx.query, "pretty", pretty)  # type String
    Swagger.set_param(_ctx.query, "dryRun", dryRun)  # type String
    Swagger.set_param(_ctx.query, "fieldManager", fieldManager)  # type String
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["*/*"] : [_mediaType])
    return _ctx
end

"""

create a PriorityClass
Param: body::IoK8sApiSchedulingV1PriorityClass (required)
Param: pretty::String
Param: dryRun::String
Param: fieldManager::String
Return: IoK8sApiSchedulingV1PriorityClass
"""
function createSchedulingV1PriorityClass(_api::SchedulingV1Api, body; pretty=nothing, dryRun=nothing, fieldManager=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_createSchedulingV1PriorityClass(_api, body; pretty=pretty, dryRun=dryRun, fieldManager=fieldManager, _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function createSchedulingV1PriorityClass(_api::SchedulingV1Api, response_stream::Channel, body; pretty=nothing, dryRun=nothing, fieldManager=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_createSchedulingV1PriorityClass(_api, body; pretty=pretty, dryRun=dryRun, fieldManager=fieldManager, _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

function _swaggerinternal_deleteSchedulingV1CollectionPriorityClass(_api::SchedulingV1Api; pretty=nothing, allowWatchBookmarks=nothing, body=nothing, __continue__=nothing, dryRun=nothing, fieldSelector=nothing, gracePeriodSeconds=nothing, labelSelector=nothing, limit=nothing, orphanDependents=nothing, propagationPolicy=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "DELETE", IoK8sApimachineryPkgApisMetaV1Status, "/apis/scheduling.k8s.io/v1/priorityclasses", ["BearerToken"], body)
    Swagger.set_param(_ctx.query, "pretty", pretty)  # type String
    Swagger.set_param(_ctx.query, "allowWatchBookmarks", allowWatchBookmarks)  # type Bool
    Swagger.set_param(_ctx.query, "continue", __continue__)  # type String
    Swagger.set_param(_ctx.query, "dryRun", dryRun)  # type String
    Swagger.set_param(_ctx.query, "fieldSelector", fieldSelector)  # type String
    Swagger.set_param(_ctx.query, "gracePeriodSeconds", gracePeriodSeconds)  # type Int32
    Swagger.set_param(_ctx.query, "labelSelector", labelSelector)  # type String
    Swagger.set_param(_ctx.query, "limit", limit)  # type Int32
    Swagger.set_param(_ctx.query, "orphanDependents", orphanDependents)  # type Bool
    Swagger.set_param(_ctx.query, "propagationPolicy", propagationPolicy)  # type String
    Swagger.set_param(_ctx.query, "resourceVersion", resourceVersion)  # type String
    Swagger.set_param(_ctx.query, "timeoutSeconds", timeoutSeconds)  # type Int32
    Swagger.set_param(_ctx.query, "watch", watch)  # type Bool
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["*/*"] : [_mediaType])
    return _ctx
end

"""

delete collection of PriorityClass
Param: pretty::String
Param: allowWatchBookmarks::Bool
Param: body::IoK8sApimachineryPkgApisMetaV1DeleteOptions
Param: __continue__::String
Param: dryRun::String
Param: fieldSelector::String
Param: gracePeriodSeconds::Int32
Param: labelSelector::String
Param: limit::Int32
Param: orphanDependents::Bool
Param: propagationPolicy::String
Param: resourceVersion::String
Param: timeoutSeconds::Int32
Param: watch::Bool
Return: IoK8sApimachineryPkgApisMetaV1Status
"""
function deleteSchedulingV1CollectionPriorityClass(_api::SchedulingV1Api; pretty=nothing, allowWatchBookmarks=nothing, body=nothing, __continue__=nothing, dryRun=nothing, fieldSelector=nothing, gracePeriodSeconds=nothing, labelSelector=nothing, limit=nothing, orphanDependents=nothing, propagationPolicy=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_deleteSchedulingV1CollectionPriorityClass(_api; pretty=pretty, allowWatchBookmarks=allowWatchBookmarks, body=body, __continue__=__continue__, dryRun=dryRun, fieldSelector=fieldSelector, gracePeriodSeconds=gracePeriodSeconds, labelSelector=labelSelector, limit=limit, orphanDependents=orphanDependents, propagationPolicy=propagationPolicy, resourceVersion=resourceVersion, timeoutSeconds=timeoutSeconds, watch=watch, _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function deleteSchedulingV1CollectionPriorityClass(_api::SchedulingV1Api, response_stream::Channel; pretty=nothing, allowWatchBookmarks=nothing, body=nothing, __continue__=nothing, dryRun=nothing, fieldSelector=nothing, gracePeriodSeconds=nothing, labelSelector=nothing, limit=nothing, orphanDependents=nothing, propagationPolicy=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_deleteSchedulingV1CollectionPriorityClass(_api; pretty=pretty, allowWatchBookmarks=allowWatchBookmarks, body=body, __continue__=__continue__, dryRun=dryRun, fieldSelector=fieldSelector, gracePeriodSeconds=gracePeriodSeconds, labelSelector=labelSelector, limit=limit, orphanDependents=orphanDependents, propagationPolicy=propagationPolicy, resourceVersion=resourceVersion, timeoutSeconds=timeoutSeconds, watch=watch, _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

function _swaggerinternal_deleteSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String; pretty=nothing, body=nothing, dryRun=nothing, gracePeriodSeconds=nothing, orphanDependents=nothing, propagationPolicy=nothing, _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "DELETE", IoK8sApimachineryPkgApisMetaV1Status, "/apis/scheduling.k8s.io/v1/priorityclasses/{name}", ["BearerToken"], body)
    Swagger.set_param(_ctx.path, "name", name)  # type String
    Swagger.set_param(_ctx.query, "pretty", pretty)  # type String
    Swagger.set_param(_ctx.query, "dryRun", dryRun)  # type String
    Swagger.set_param(_ctx.query, "gracePeriodSeconds", gracePeriodSeconds)  # type Int32
    Swagger.set_param(_ctx.query, "orphanDependents", orphanDependents)  # type Bool
    Swagger.set_param(_ctx.query, "propagationPolicy", propagationPolicy)  # type String
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["*/*"] : [_mediaType])
    return _ctx
end

"""

delete a PriorityClass
Param: name::String (required)
Param: pretty::String
Param: body::IoK8sApimachineryPkgApisMetaV1DeleteOptions
Param: dryRun::String
Param: gracePeriodSeconds::Int32
Param: orphanDependents::Bool
Param: propagationPolicy::String
Return: IoK8sApimachineryPkgApisMetaV1Status
"""
function deleteSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String; pretty=nothing, body=nothing, dryRun=nothing, gracePeriodSeconds=nothing, orphanDependents=nothing, propagationPolicy=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_deleteSchedulingV1PriorityClass(_api, name; pretty=pretty, body=body, dryRun=dryRun, gracePeriodSeconds=gracePeriodSeconds, orphanDependents=orphanDependents, propagationPolicy=propagationPolicy, _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function deleteSchedulingV1PriorityClass(_api::SchedulingV1Api, response_stream::Channel, name::String; pretty=nothing, body=nothing, dryRun=nothing, gracePeriodSeconds=nothing, orphanDependents=nothing, propagationPolicy=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_deleteSchedulingV1PriorityClass(_api, name; pretty=pretty, body=body, dryRun=dryRun, gracePeriodSeconds=gracePeriodSeconds, orphanDependents=orphanDependents, propagationPolicy=propagationPolicy, _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

function _swaggerinternal_getSchedulingV1APIResources(_api::SchedulingV1Api; _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "GET", IoK8sApimachineryPkgApisMetaV1APIResourceList, "/apis/scheduling.k8s.io/v1/", ["BearerToken"])
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf"] : [_mediaType])
    return _ctx
end

"""

get available resources
Return: IoK8sApimachineryPkgApisMetaV1APIResourceList
"""
function getSchedulingV1APIResources(_api::SchedulingV1Api; _mediaType=nothing)
    _ctx = _swaggerinternal_getSchedulingV1APIResources(_api; _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function getSchedulingV1APIResources(_api::SchedulingV1Api, response_stream::Channel; _mediaType=nothing)
    _ctx = _swaggerinternal_getSchedulingV1APIResources(_api; _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

function _swaggerinternal_listSchedulingV1PriorityClass(_api::SchedulingV1Api; pretty=nothing, allowWatchBookmarks=nothing, __continue__=nothing, fieldSelector=nothing, labelSelector=nothing, limit=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "GET", IoK8sApiSchedulingV1PriorityClassList, "/apis/scheduling.k8s.io/v1/priorityclasses", ["BearerToken"])
    Swagger.set_param(_ctx.query, "pretty", pretty)  # type String
    Swagger.set_param(_ctx.query, "allowWatchBookmarks", allowWatchBookmarks)  # type Bool
    Swagger.set_param(_ctx.query, "continue", __continue__)  # type String
    Swagger.set_param(_ctx.query, "fieldSelector", fieldSelector)  # type String
    Swagger.set_param(_ctx.query, "labelSelector", labelSelector)  # type String
    Swagger.set_param(_ctx.query, "limit", limit)  # type Int32
    Swagger.set_param(_ctx.query, "resourceVersion", resourceVersion)  # type String
    Swagger.set_param(_ctx.query, "timeoutSeconds", timeoutSeconds)  # type Int32
    Swagger.set_param(_ctx.query, "watch", watch)  # type Bool
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf", "application/json;stream=watch", "application/vnd.kubernetes.protobuf;stream=watch"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["*/*"] : [_mediaType])
    return _ctx
end

"""

list or watch objects of kind PriorityClass
Param: pretty::String
Param: allowWatchBookmarks::Bool
Param: __continue__::String
Param: fieldSelector::String
Param: labelSelector::String
Param: limit::Int32
Param: resourceVersion::String
Param: timeoutSeconds::Int32
Param: watch::Bool
Return: IoK8sApiSchedulingV1PriorityClassList
"""
function listSchedulingV1PriorityClass(_api::SchedulingV1Api; pretty=nothing, allowWatchBookmarks=nothing, __continue__=nothing, fieldSelector=nothing, labelSelector=nothing, limit=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_listSchedulingV1PriorityClass(_api; pretty=pretty, allowWatchBookmarks=allowWatchBookmarks, __continue__=__continue__, fieldSelector=fieldSelector, labelSelector=labelSelector, limit=limit, resourceVersion=resourceVersion, timeoutSeconds=timeoutSeconds, watch=watch, _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function listSchedulingV1PriorityClass(_api::SchedulingV1Api, response_stream::Channel; pretty=nothing, allowWatchBookmarks=nothing, __continue__=nothing, fieldSelector=nothing, labelSelector=nothing, limit=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_listSchedulingV1PriorityClass(_api; pretty=pretty, allowWatchBookmarks=allowWatchBookmarks, __continue__=__continue__, fieldSelector=fieldSelector, labelSelector=labelSelector, limit=limit, resourceVersion=resourceVersion, timeoutSeconds=timeoutSeconds, watch=watch, _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

function _swaggerinternal_patchSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String, body; pretty=nothing, dryRun=nothing, fieldManager=nothing, force=nothing, _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "PATCH", IoK8sApiSchedulingV1PriorityClass, "/apis/scheduling.k8s.io/v1/priorityclasses/{name}", ["BearerToken"], body)
    Swagger.set_param(_ctx.path, "name", name)  # type String
    Swagger.set_param(_ctx.query, "pretty", pretty)  # type String
    Swagger.set_param(_ctx.query, "dryRun", dryRun)  # type String
    Swagger.set_param(_ctx.query, "fieldManager", fieldManager)  # type String
    Swagger.set_param(_ctx.query, "force", force)  # type Bool
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["application/json-patch+json", "application/merge-patch+json", "application/strategic-merge-patch+json", "application/apply-patch+yaml"] : [_mediaType])
    return _ctx
end

"""

partially update the specified PriorityClass
Param: name::String (required)
Param: body::IoK8sApimachineryPkgApisMetaV1Patch (required)
Param: pretty::String
Param: dryRun::String
Param: fieldManager::String
Param: force::Bool
Return: IoK8sApiSchedulingV1PriorityClass
"""
function patchSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String, body; pretty=nothing, dryRun=nothing, fieldManager=nothing, force=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_patchSchedulingV1PriorityClass(_api, name, body; pretty=pretty, dryRun=dryRun, fieldManager=fieldManager, force=force, _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function patchSchedulingV1PriorityClass(_api::SchedulingV1Api, response_stream::Channel, name::String, body; pretty=nothing, dryRun=nothing, fieldManager=nothing, force=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_patchSchedulingV1PriorityClass(_api, name, body; pretty=pretty, dryRun=dryRun, fieldManager=fieldManager, force=force, _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

function _swaggerinternal_readSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String; pretty=nothing, exact=nothing, __export__=nothing, _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "GET", IoK8sApiSchedulingV1PriorityClass, "/apis/scheduling.k8s.io/v1/priorityclasses/{name}", ["BearerToken"])
    Swagger.set_param(_ctx.path, "name", name)  # type String
    Swagger.set_param(_ctx.query, "pretty", pretty)  # type String
    Swagger.set_param(_ctx.query, "exact", exact)  # type Bool
    Swagger.set_param(_ctx.query, "export", __export__)  # type Bool
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["*/*"] : [_mediaType])
    return _ctx
end

"""

read the specified PriorityClass
Param: name::String (required)
Param: pretty::String
Param: exact::Bool
Param: __export__::Bool
Return: IoK8sApiSchedulingV1PriorityClass
"""
function readSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String; pretty=nothing, exact=nothing, __export__=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_readSchedulingV1PriorityClass(_api, name; pretty=pretty, exact=exact, __export__=__export__, _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function readSchedulingV1PriorityClass(_api::SchedulingV1Api, response_stream::Channel, name::String; pretty=nothing, exact=nothing, __export__=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_readSchedulingV1PriorityClass(_api, name; pretty=pretty, exact=exact, __export__=__export__, _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

function _swaggerinternal_replaceSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String, body; pretty=nothing, dryRun=nothing, fieldManager=nothing, _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "PUT", IoK8sApiSchedulingV1PriorityClass, "/apis/scheduling.k8s.io/v1/priorityclasses/{name}", ["BearerToken"], body)
    Swagger.set_param(_ctx.path, "name", name)  # type String
    Swagger.set_param(_ctx.query, "pretty", pretty)  # type String
    Swagger.set_param(_ctx.query, "dryRun", dryRun)  # type String
    Swagger.set_param(_ctx.query, "fieldManager", fieldManager)  # type String
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["*/*"] : [_mediaType])
    return _ctx
end

"""

replace the specified PriorityClass
Param: name::String (required)
Param: body::IoK8sApiSchedulingV1PriorityClass (required)
Param: pretty::String
Param: dryRun::String
Param: fieldManager::String
Return: IoK8sApiSchedulingV1PriorityClass
"""
function replaceSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String, body; pretty=nothing, dryRun=nothing, fieldManager=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_replaceSchedulingV1PriorityClass(_api, name, body; pretty=pretty, dryRun=dryRun, fieldManager=fieldManager, _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function replaceSchedulingV1PriorityClass(_api::SchedulingV1Api, response_stream::Channel, name::String, body; pretty=nothing, dryRun=nothing, fieldManager=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_replaceSchedulingV1PriorityClass(_api, name, body; pretty=pretty, dryRun=dryRun, fieldManager=fieldManager, _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

function _swaggerinternal_watchSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String; allowWatchBookmarks=nothing, __continue__=nothing, fieldSelector=nothing, labelSelector=nothing, limit=nothing, pretty=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "GET", IoK8sApimachineryPkgApisMetaV1WatchEvent, "/apis/scheduling.k8s.io/v1/watch/priorityclasses/{name}", ["BearerToken"])
    Swagger.set_param(_ctx.path, "name", name)  # type String
    Swagger.set_param(_ctx.query, "allowWatchBookmarks", allowWatchBookmarks)  # type Bool
    Swagger.set_param(_ctx.query, "continue", __continue__)  # type String
    Swagger.set_param(_ctx.query, "fieldSelector", fieldSelector)  # type String
    Swagger.set_param(_ctx.query, "labelSelector", labelSelector)  # type String
    Swagger.set_param(_ctx.query, "limit", limit)  # type Int32
    Swagger.set_param(_ctx.query, "pretty", pretty)  # type String
    Swagger.set_param(_ctx.query, "resourceVersion", resourceVersion)  # type String
    Swagger.set_param(_ctx.query, "timeoutSeconds", timeoutSeconds)  # type Int32
    Swagger.set_param(_ctx.query, "watch", watch)  # type Bool
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf", "application/json;stream=watch", "application/vnd.kubernetes.protobuf;stream=watch"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["*/*"] : [_mediaType])
    return _ctx
end

"""

watch changes to an object of kind PriorityClass. deprecated: use the 'watch' parameter with a list operation instead, filtered to a single item with the 'fieldSelector' parameter.
Param: name::String (required)
Param: allowWatchBookmarks::Bool
Param: __continue__::String
Param: fieldSelector::String
Param: labelSelector::String
Param: limit::Int32
Param: pretty::String
Param: resourceVersion::String
Param: timeoutSeconds::Int32
Param: watch::Bool
Return: IoK8sApimachineryPkgApisMetaV1WatchEvent
"""
function watchSchedulingV1PriorityClass(_api::SchedulingV1Api, name::String; allowWatchBookmarks=nothing, __continue__=nothing, fieldSelector=nothing, labelSelector=nothing, limit=nothing, pretty=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_watchSchedulingV1PriorityClass(_api, name; allowWatchBookmarks=allowWatchBookmarks, __continue__=__continue__, fieldSelector=fieldSelector, labelSelector=labelSelector, limit=limit, pretty=pretty, resourceVersion=resourceVersion, timeoutSeconds=timeoutSeconds, watch=watch, _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function watchSchedulingV1PriorityClass(_api::SchedulingV1Api, response_stream::Channel, name::String; allowWatchBookmarks=nothing, __continue__=nothing, fieldSelector=nothing, labelSelector=nothing, limit=nothing, pretty=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_watchSchedulingV1PriorityClass(_api, name; allowWatchBookmarks=allowWatchBookmarks, __continue__=__continue__, fieldSelector=fieldSelector, labelSelector=labelSelector, limit=limit, pretty=pretty, resourceVersion=resourceVersion, timeoutSeconds=timeoutSeconds, watch=watch, _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

function _swaggerinternal_watchSchedulingV1PriorityClassList(_api::SchedulingV1Api; allowWatchBookmarks=nothing, __continue__=nothing, fieldSelector=nothing, labelSelector=nothing, limit=nothing, pretty=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = Swagger.Ctx(_api.client, "GET", IoK8sApimachineryPkgApisMetaV1WatchEvent, "/apis/scheduling.k8s.io/v1/watch/priorityclasses", ["BearerToken"])
    Swagger.set_param(_ctx.query, "allowWatchBookmarks", allowWatchBookmarks)  # type Bool
    Swagger.set_param(_ctx.query, "continue", __continue__)  # type String
    Swagger.set_param(_ctx.query, "fieldSelector", fieldSelector)  # type String
    Swagger.set_param(_ctx.query, "labelSelector", labelSelector)  # type String
    Swagger.set_param(_ctx.query, "limit", limit)  # type Int32
    Swagger.set_param(_ctx.query, "pretty", pretty)  # type String
    Swagger.set_param(_ctx.query, "resourceVersion", resourceVersion)  # type String
    Swagger.set_param(_ctx.query, "timeoutSeconds", timeoutSeconds)  # type Int32
    Swagger.set_param(_ctx.query, "watch", watch)  # type Bool
    Swagger.set_header_accept(_ctx, ["application/json", "application/yaml", "application/vnd.kubernetes.protobuf", "application/json;stream=watch", "application/vnd.kubernetes.protobuf;stream=watch"])
    Swagger.set_header_content_type(_ctx, (_mediaType === nothing) ? ["*/*"] : [_mediaType])
    return _ctx
end

"""

watch individual changes to a list of PriorityClass. deprecated: use the 'watch' parameter with a list operation instead.
Param: allowWatchBookmarks::Bool
Param: __continue__::String
Param: fieldSelector::String
Param: labelSelector::String
Param: limit::Int32
Param: pretty::String
Param: resourceVersion::String
Param: timeoutSeconds::Int32
Param: watch::Bool
Return: IoK8sApimachineryPkgApisMetaV1WatchEvent
"""
function watchSchedulingV1PriorityClassList(_api::SchedulingV1Api; allowWatchBookmarks=nothing, __continue__=nothing, fieldSelector=nothing, labelSelector=nothing, limit=nothing, pretty=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_watchSchedulingV1PriorityClassList(_api; allowWatchBookmarks=allowWatchBookmarks, __continue__=__continue__, fieldSelector=fieldSelector, labelSelector=labelSelector, limit=limit, pretty=pretty, resourceVersion=resourceVersion, timeoutSeconds=timeoutSeconds, watch=watch, _mediaType=_mediaType)
    Swagger.exec(_ctx)
end

function watchSchedulingV1PriorityClassList(_api::SchedulingV1Api, response_stream::Channel; allowWatchBookmarks=nothing, __continue__=nothing, fieldSelector=nothing, labelSelector=nothing, limit=nothing, pretty=nothing, resourceVersion=nothing, timeoutSeconds=nothing, watch=nothing, _mediaType=nothing)
    _ctx = _swaggerinternal_watchSchedulingV1PriorityClassList(_api; allowWatchBookmarks=allowWatchBookmarks, __continue__=__continue__, fieldSelector=fieldSelector, labelSelector=labelSelector, limit=limit, pretty=pretty, resourceVersion=resourceVersion, timeoutSeconds=timeoutSeconds, watch=watch, _mediaType=_mediaType)
    Swagger.exec(_ctx, response_stream)
end

export createSchedulingV1PriorityClass, deleteSchedulingV1CollectionPriorityClass, deleteSchedulingV1PriorityClass, getSchedulingV1APIResources, listSchedulingV1PriorityClass, patchSchedulingV1PriorityClass, readSchedulingV1PriorityClass, replaceSchedulingV1PriorityClass, watchSchedulingV1PriorityClass, watchSchedulingV1PriorityClassList


text:
using Test
using CompScienceMeshes
using LinearAlgebra
using StaticArrays

for T in [Float64, Float32]
    rectangle = meshrectangle(T(1.0), T(1.0), T(1.0));

    @test vertices(rectangle,1) == point(T, 0.0, 0.0, 0.0)
    @test vertices(rectangle,2) == point(T, 0.0, 1.0, 0.0)
    @test vertices(rectangle,3) == point(T, 1.0, 0.0, 0.0)

    @test numvertices(rectangle) == 4
    @test numcells(rectangle) == 2
    @test dimension(rectangle) == 2

    faces = skeleton(rectangle, 2)
    edges = skeleton(rectangle, 1)
    verts = skeleton(rectangle, 0)

    @test numcells(verts) == 4
    @test numcells(edges) == 5
    @test numcells(faces) == 2

    Λ  = connectivity(verts, edges, sign)
    Σᵀ = connectivity(edges, faces, sign)

    @test size(Λ)  == (5,4)
    @test size(Σᵀ) == (2,5)

    @test norm(Σᵀ*Λ, Inf) ≈ 0

    bnd = boundary(rectangle)

    @test dimension(bnd) == 1
    @test numvertices(bnd) == 4
    @test numcells(bnd) == 4

    vtoe, num_vtoe = vertextocellmap(edges)
    vtof, num_vtof = vertextocellmap(faces)

    @test size(vtoe) == (4,3)
    @test size(vtof) == (4,2)

    @test size(num_vtoe) == (4,)
    @test size(num_vtof) == (4,)

    file = joinpath(dirname(@__FILE__), "sphere.in")
    sphere = readmesh(file)
    @test numvertices(sphere) == 335
    @test numcells(sphere) == 666

    m = meshrectangle(T(1.0),T(1.0),T(1.0))
    CompScienceMeshes.rotate!(m, [1,0,0]*T(π/2))

    @test CompScienceMeshes.isoriented(boundary(m))

    tol = sqrt(eps(T))
    @test norm(vertices(m,1) - point(T,0.0,0.0,0.0)) < tol
    @test norm(vertices(m,2) - point(T,0.0,0.0,1.0)) < tol
    @test norm(vertices(m,3) - point(T,1.0,0.0,0.0)) < tol
    @test norm(vertices(m,4) - point(T,1.0,0.0,1.0)) < tol

    ## test fliporientation
    V = [
        point(T,0,0,0),
        point(T,1,0,0),
        point(T,0,1,0),
    ]

    F = [
        index(1,2,3)
    ]

    m = Mesh(V,F)
    n = fliporientation(m)

    @test m.faces[1] == index(1,2,3)
    @test n.faces[1] == index(2,1,3)

    # test the construction of transposed connectivity matricees
    Σᵀ = connectivity(edges, faces, identity)
    Σ = connectivity(faces, edges, identity)
    @test norm(Σᵀ - Σ', Inf) == 0
end

max_stars_repo_name: krcools/CompScienceMeshes

max_issues_repo_name: krcools/CompScienceMeshes

max_forks_repo_name: krcools/CompScienceMeshes


text:
struct GdkRectangle <: GBoxed
    x::Int32
    y::Int32
    width::Int32
    height::Int32
    GdkRectangle(x, y, w, h) = new(x, y, w, h)
end
@make_gvalue(GdkRectangle, Ptr{GdkRectangle}, :boxed, (:gdk_rectangle, :libgdk))
convert(::Type{GdkRectangle}, rect::Ptr{GdkRectangle}) = unsafe_load(rect)

struct GdkPoint
    x::Int32
    y::Int32
    GdkPoint(x, y) = new(x, y)
end
# GdkPoint is not a GBoxed type

struct GdkRGBA
	r::Cdouble
	g::Cdouble
	b::Cdouble
    a::Cdouble
    GdkRGBA(r, g, b, a) = new(r, g, b, a)
end
@make_gvalue(GdkRGBA, Ptr{GdkRGBA}, :boxed, (:gdk_rgba,:libgdk))
convert(::Type{GdkRGBA}, rgba::Ptr{GdkRGBA}) = unsafe_load(rgba)

baremodule GdkKeySyms
    const VoidSymbol = 0xffffff
    const BackSpace = 0xff08
    const Tab = 0xff09
    const Linefeed = 0xff0a
    const Clear = 0xff0b
    const Return = 0xff0d
    const Pause = 0xff13
    const Scroll_Lock = 0xff14
    const Sys_Req = 0xff15
    const Escape = 0xff1b
    const Delete = 0xffff
    const Home = 0xff50
    const Left = 0xff51
    const Up = 0xff52
    const Right = 0xff53
    const Down = 0xff54
    const Page_Up = 0xff55
    const Next = 0xff56
    const Page_Down = 0xff56
    const End = 0xff57
    const Insert = 0xff63
    const Num_Lock = 0xff7f
    const F1 = 0xffbe
    const F2 = 0xffbf
    const F3 = 0xffc0
    const F4 = 0xffc1
    const F5 = 0xffc2
    const F6 = 0xffc3
    const F7 = 0xffc4
    const F8 = 0xffc5
    const F9 = 0xffc6
    const F10 = 0xffc7
    const F11 = 0xffc8
    const F12 = 0xffc9
    const Shift_L = 0xffe1
    const Shift_R = 0xffe2
    const Control_L = 0xffe3
    const Control_R = 0xffe4
    const Caps_Lock = 0xffe5
    const Shift_Lock = 0xffe6
    const Meta_L = 0xffe7
    const Meta_R = 0xffe8
    const Alt_L = 0xffe9
    const Alt_R = 0xffea
    const Super_L = 0xffeb
    const Super_R = 0xffec
    const Hyper_L = 0xffed
    const Hyper_R = 0xffee
    const KP_Enter = 0xff8d
end

abstract type GdkEvent <: GBoxed end
@make_gvalue(GdkEvent, Ptr{GdkEvent}, :boxed, (:gdk_event, :libgdk))
function convert(::Type{GdkEvent}, evt::Ptr{GdkEvent})
    e = unsafe_load(convert(Ptr{GdkEventAny}, evt))
    if     e.event_type == GdkEventType.KEY_PRESS ||
           e.event_type == GdkEventType.KEY_RELEASE
        return unsafe_load(convert(Ptr{GdkEventKey}, evt))
    elseif e.event_type == GdkEventType.BUTTON_PRESS ||
           e.event_type == GdkEventType.DOUBLE_BUTTON_PRESS ||
           e.event_type == GdkEventType.TRIPLE_BUTTON_PRESS ||
           e.event_type == GdkEventType.BUTTON_RELEASE
        return unsafe_load(convert(Ptr{GdkEventButton}, evt))
    elseif e.event_type == GdkEventType.SCROLL
        return unsafe_load(convert(Ptr{GdkEventScroll}, evt))
    elseif e.event_type == GdkEventType.MOTION_NOTIFY
        return unsafe_load(convert(Ptr{GdkEventMotion}, evt))
    elseif e.event_type == GdkEventType.ENTER_NOTIFY ||
           e.event_type == GdkEventType.LEAVE_NOTIFY
        return unsafe_load(convert(Ptr{GdkEventCrossing}, evt))
    else
        return unsafe_load(convert(Ptr{GdkEventAny}, evt))
    end
end

struct GdkEventAny <: GdkEvent
    event_type::GEnum
    gdk_window::Ptr{Nothing}
    send_event::Int8
end

struct GdkEventButton <: GdkEvent
    event_type::GEnum
    gdk_window::Ptr{Nothing}
    send_event::Int8
    time::UInt32
    x::Float64
    y::Float64
    axes::Ptr{Float64}
    state::UInt32
    button::UInt32
    gdk_device::Ptr{Nothing}
    x_root::Float64
    y_root::Float64
end

struct GdkEventScroll <: GdkEvent
    event_type::GEnum
    gdk_window::Ptr{Nothing}
    send_event::Int8
    time::UInt32
    x::Float64
    y::Float64
    state::UInt32
    direction::GEnum
    gdk_device::Ptr{Nothing}
    x_root::Float64
    y_root::Float64
    delta_x::Float64
    delta_y::Float64
end

struct GdkEventKey <: GdkEvent
    event_type::GEnum
    gdk_window::Ptr{Nothing}
    send_event::Int8
    time::UInt32
    state::UInt32
    keyval::UInt32
    length::Int32
    string::Ptr{UInt8}
    hardware_keycode::UInt16
    group::UInt8
    flags::UInt32
end
GdkEventKey() = (uz32 = UInt32(0); GdkEventKey(GEnum(0), C_NULL, Int8(0), uz32, uz32, uz32, Int32(0), C_NULL, UInt16(0), 0x00, uz32))

is_modifier(evt::GdkEventKey) = (evt.flags & 0x0001) > 0

struct GdkEventMotion <: GdkEvent
  event_type::GEnum
  gdk_window::Ptr{Nothing}
  send_event::Int8
  time::UInt32
  x::Float64
  y::Float64
  axes::Ptr{Float64}
  state::UInt32
  is_hint::Int16
  gdk_device::Ptr{Nothing}
  x_root::Float64
  y_root::Float64
end

struct GdkEventCrossing <: GdkEvent
  event_type::GEnum
  gdk_window::Ptr{Nothing}
  send_event::Int8
  gdk_subwindow::Ptr{Nothing}
  time::UInt32
  x::Float64
  y::Float64
  x_root::Float64
  y_root::Float64
  mode::GEnum
  detail::GEnum
  focus::Cint
  state::UInt32
end

keyval(name::AbstractString) =
  ccall((:gdk_keyval_from_name, libgdk), Cuint, (Ptr{UInt8},), bytestring(name))

screen_size() = screen_size(ccall((:gdk_screen_get_default, libgdk),
                                          Ptr{Nothing}, ()))

function screen_size(screen::Ptr{Nothing})
    return (ccall((:gdk_screen_get_width, libgdk), Cint, (Ptr{Nothing},), screen),
            ccall((:gdk_screen_get_height, libgdk), Cint, (Ptr{Nothing},), screen))
end

function get_origin(window)
    window_x, window_y = mutable(Cint), mutable(Cint)
	ccall(
        (:gdk_window_get_origin, libgdk), Cint,
        (Ptr{GObject}, Ptr{Cint}, Ptr{Cint}),
        window, window_x, window_y
    )
	return (window_x[], window_y[])
end

max_stars_repo_name: adamwillisMastery/MiniGtk

max_issues_repo_name: adamwillisMastery/MiniGtk

max_forks_repo_name: adamwillisMastery/MiniGtk


text:
using Discretizers
import LPDM: default_action, isterminal
import POMDPs: rand, actions

mutable struct LightDark1DDespot <: AbstractLD1
# @with_kw mutable struct LightDark1DDespot <: AbstractLD1
    min_noise::Float64
    min_noise_loc::Float64
    Q::Float64
    R::Float64
    term_radius::Float64
    n_bins::Int         # per linear dimension
    max_x::Float64     # assume symmetry in x and y for simplicity
    bin_edges::Vector{Float64}
    bin_centers::Vector{Float64}
    lindisc::LinearDiscretizer
    init_dist::Any
    discount::Float64
    count::Int
    n_rand::Int
    resample_std::Float64
    action_space_type::Symbol
    nominal_action_space::Vector{LD1Action}
    extended_action_space::Vector{LD1Action}
    reward_func::Symbol

    function LightDark1DDespot(action_space_type::Symbol; reward_func = :quadratic)
        this = new()
        this.min_noise               = 0.0
        this.min_noise_loc           = 5.0
        this.Q                       = 0.5
        this.R                       = 0.5
        this.term_radius             = 0.05
        this.n_bins                  = 100 # per linear dimension
        this.max_x                   = 10     # assume symmetry in x and y for simplicity
        this.bin_edges               = collect(-this.max_x:(2*this.max_x)/this.n_bins:this.max_x)
        this.bin_centers             = [(this.bin_edges[i]+this.bin_edges[i+1])/2 for i=1:this.n_bins]
        this.lindisc                 = LinearDiscretizer(this.bin_edges)
        this.discount                = 1.0
        this.count                   = 0
        this.n_rand                  = 0
        this.resample_std            = 0.5 # st. deviation for particle resampling
        this.nominal_action_space    = [1.0, 0.1, 0.01]
        this.extended_action_space   = vcat(1*this.nominal_action_space,
                                            2*this.nominal_action_space,
                                            3*this.nominal_action_space,
                                            4*this.nominal_action_space,
                                            5*this.nominal_action_space)
        this.action_space_type       = action_space_type
        this.reward_func                  = reward_func
        return this
    end
end

# POMDPs.actions(p::LightDark1DDespot, ::Bool) = [1.0, 0.5, 0.1, 0.01];
# POMDPs.actions(p::LightDark1DDespot, ::Bool) = [5.0, 1.0, 0.1, 0.01]
# POMDPs.actions(p::LightDark1DDespot) = vcat(-POMDPs.actions(p, true), POMDPs.actions(p,true))
# POMDPs.actions(p::LightDark1DDespot) = vcat(-POMDPs.actions(p, true), [0.0], POMDPs.actions(p,true))

function POMDPs.actions(p::LightDark1DDespot)
    if p.action_space_type == :small
        # return vcat(-p.nominal_action_space, [0.0], p.nominal_action_space)
        return vcat(-p.nominal_action_space, p.nominal_action_space)
    elseif p.action_space_type == :large
        # return vcat(-p.extended_action_space, [0.0], p.extended_action_space)
        return vcat(-p.extended_action_space, p.extended_action_space)
    else
        error("Action space $(p.action_space_type) is not valid for POMDP of type $(typeof(p))")
    end
end

LPDM.default_action(p::LightDark1DDespot) = 0.00
LPDM.default_action(p::LightDark1DDespot, ::Vector{LPDMParticle{LD1State}}) = LPDM.default_action(p)
POMDPs.rand(p::LightDark1DDespot, s::LD1State, rng::LPDM.RNGVector) = norminvcdf(s, p.resample_std, rand(rng)) # for resampling

# POMDPs.actions(p::LightDark1DDespot, ::Bool) = [0.1, 0.01]
#POMDPs.actions(p::LightDark1DDespot) = Float64Iter(collect(permutations(vcat(POMDPs.actions(p, true), -POMDPs.actions(p,true)), 2)))

# reward(p::LightDark1DDespot, s::Float64, a::Float64, sp::Float64) = -(p.Q*(s^2) + p.R*(a^2))
# reward(p::LightDark1DDespot, s::Float64, a::Float64)              = -(p.Q*(s^2) + p.R*(a^2))

# function reward(p::LightDark1DDespot, s::Float64, a::Float64)
#     # println("REWARD: s=$s, a=$a, Q=$(p.Q), R=$(p.R), r=$(-(p.Q*(s^2) + p.R*(a^2)))")
#     return -(p.Q*(s^2) + p.R*(a^2))
# end

# ORIGINAL
# reward(p::LightDark1DDespot, s::Float64) = -1.0
# reward(p::LightDark1DDespot, s::Float64, a::Float64) = reward(p, s)
# reward(p::LightDark1DDespot, s::Float64, a::Float64, sp::Float64) = reward(p, s)

# This is somewhat resembling Zach's version
# function reward(p::LightDark1DDespot, s::Float64, a::Float64)
#     # r = -1.0 # default
#     if isterminal(p,s) && a == 0.0
#         r = 100.0
#     elseif a == 0.0 # don't take a=0.0 elsewhere
#         # error("giving a negative reward for a==0")
#         r = -100.0
#     else
#         r = -(p.Q*s^2 + p.R*a^2)
#     end
#     return r
# end

# Simple reward (same as "Belief space planning assuming maximum likelihood observations")
# reward(p::LightDark1DDespot, s::Float64, a::Float64) = -(p.Q*s^2 + p.R*a^2)
# reward(p::LightDark1DDespot, s::Float64, a::Float64) = -1
#
# reward(p::LightDark1DDespot, s::Float64, a::Float64, sp::Float64) = reward(p,s,a)

# #DEBUG VERSION
# #generate_o(p::LightDark1DDespot, sp::Float64, rng::AbstractRNG) = sp
# function generate_o(p::LightDark1DDespot, sp::Float64, rng::AbstractRNG)
#     o = sp
#     o_disc = p.bin_centers[encode(p.lindisc,o)]
#     # println("$o -> $o_disc")
#     return o_disc
#     # return obs_index(p,o_disc) # return a single combined obs index
# end

max_stars_repo_name: ebalaban/LightDarkLPDM

max_issues_repo_name: tomerarnon/LightDarkLPDM

max_forks_repo_name: tomerarnon/LightDarkLPDM


text:
Network = NetLib.SimpleNet

netparams = NetLib.SimpleNetHP(
    width = 200,
    depth_common = 6,
    use_batch_norm = true,
    batch_norm_momentum = 1.0,
)

self_play = SelfPlayParams(
    sim = SimParams(
        num_games = 500,
        num_workers = 128,
        batch_size = 128,
        use_gpu = false,
        reset_every = 4,
        flip_probability = 0.0,
        alternate_colors = false,
    ),
    mcts = MctsParams(
        num_iters_per_turn = 100,
        cpuct = 1.0,
        temperature = ConstSchedule(1.0),
        dirichlet_noise_ϵ = 0.2,
        dirichlet_noise_α = 1.0,
    ),
)

arena = ArenaParams(
    sim = SimParams(
        num_games = 120,
        num_workers = 100,
        batch_size = 100,
        use_gpu = false,
        reset_every = 1,
        flip_probability = 0.5,
        alternate_colors = true,
    ),
    mcts = MctsParams(
        self_play.mcts,
        temperature = ConstSchedule(0.3),
        dirichlet_noise_ϵ = 0.1,
    ),
    update_threshold = 0.00,
)

learning = LearningParams(
    use_gpu = false,
    samples_weighing_policy = LOG_WEIGHT,
    l2_regularization = 1e-4,
    optimiser = CyclicNesterov(
        lr_base = 1e-3,
        lr_high = 1e-2,
        lr_low = 1e-3,
        momentum_high = 0.9,
        momentum_low = 0.8,
    ),
    batch_size = 32,
    loss_computation_batch_size = 2048,
    nonvalidity_penalty = 1.0,
    min_checkpoints_per_epoch = 0,
    max_batches_per_checkpoint = 5_000,
    num_checkpoints = 1,
)

params = Params(
    arena = arena,
    self_play = self_play,
    learning = learning,
    num_iters = 15,
    memory_analysis = MemAnalysisParams(num_game_stages = 4),
    ternary_rewards = true,
    use_symmetries = true,
    mem_buffer_size = PLSchedule(80_000),
)

benchmark_sim = SimParams(arena.sim; num_games = 60, num_workers = 100, batch_size = 100)

benchmark = [
    Benchmark.Duel(
        Benchmark.Full(self_play.mcts),
        Benchmark.MctsRollouts(self_play.mcts),
        benchmark_sim,
    ),
    Benchmark.Duel(
        Benchmark.NetworkOnly(),
        Benchmark.MctsRollouts(self_play.mcts),
        benchmark_sim,
    ),
    Benchmark.Duel(
        Benchmark.NetworkOnly(),
        Benchmark.MinMaxTS(depth = 1, amplify_rewards = true, τ = 1.0),
        benchmark_sim,
    ),
]

experiment = Experiment("gomoku", GameSpec(), params, Network, netparams, benchmark)


text:
"""
    count_word_occurrence_in_file(file_name::String, word::String)

Counts how often word appears in file file_name.
Example: if file contains "one two one two three four"
         And word is "one", then this function returns 2
"""
function count_word_occurrence_in_file(file_name::String, word::String)
    open(file_name, "r") do file
        lines = readlines(file)
        return count(word, join(lines))
    end
end


text:
module MuseInference

using AbstractDifferentiation
using Base.Iterators: peel, repeated
using ComponentArrays
using CovarianceEstimation
using Dates
using Distributed
using Distributions
using FileIO
using FiniteDifferences
using ForwardDiff
using LinearAlgebra
using Markdown
using NamedTupleTools
using Optim
using Printf
using ProgressMeter
using Random
using Requires
using Setfield
using Statistics
using Test
using UnPack

export SimpleMuseProblem, MuseResult, muse, muse!, get_J!, get_H!

include("util.jl")
include("ad.jl")
include("progress.jl")
include("interface.jl")
include("muse.jl")
include("simple.jl")
@init @require Turing="fce5fe82-541a-59a6-adf8-730c64b5f9a0" include("turing.jl")
@init @require Soss="8ce77f84-9b61-11e8-39ff-d17a774bf41c" include("soss.jl")

end


text:
#############################################################
#Data Import and Preprocesing

using Knet
include(Knet.dir("data", "cifar.jl"))
include(Knet.dir("data", "mnist.jl"))
function import_data(dataset::String)
	if dataset == "CIFAR10"
		smallimgs, labels, smallimgstest, labelstest = cifar10()
	    smallimgs = reshape(smallimgs, 32^2 * 3, 50000)
	    smallimgstest = reshape(smallimgstest, 32^2 * 3, 10000)
	elseif dataset == "MNIST"
		smallimgs, labels, smallimgstest, labelstest = mnist()
	    smallimgs = reshape(smallimgs, 28^2, 60000)
	    smallimgstest = reshape(smallimgstest, 28^2, 10000)
	end
	smallimgs, labels .- 1, smallimgstest, labelstest .- 1
end

function subtractmean!(data)
        m = mean(data, dims = 2)
        d, n = size(data)
        for i in 0:n-1
                BLAS.axpy!(-1., m, 1:d, data, i*d + 1: (i+1) * d)
        end
end
function subtractmean(data)
        m = deepcopy(data)
        subtractmean!(m)
        m
end

function normalizelinewise!(data)
    max = maximum(data, dims = 2)
    d, n = size(data)
    for i in 1:d
        (max[i] != 0.) && (data[i,:] ./= max[i])
    end
    subtractmean!(data)
end

function standardizelinewise!(data)
    means = mean(data, dims = 2)
    stds = std(data, dims = 2)
    d, n = size(data)
    for i in 1:d
        data[i,:] .-= means[i]
        (stds[i] != 0.) && (data[i,:] ./= stds[i])
    end
end

#scale data between [-1,1]
function rescaledata!(data)
        absmax = maximum(abs(data))
        scale!(data, 1. / absmax)
end

# data is dxn array of n d-dimensional random vectors with mean 0
function whiten(data; method = :f_ZCA)
        f = svdfact(data)
        eval(method)(f) * sqrt(size(data, 2) - 1)
end

f_ZCA(f::LinearAlgebra.SVD) = f[:U] * f[:Vt]
f_PCA(f::LinearAlgebra.SVD) = f[:Vt]


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

using Markdown
using InteractiveUtils

# ╔═╡ 8b92a6cf-cd6a-4364-9f59-598bcf206ef1
html"<button onclick=present()>Presentation mode</button>"

# ╔═╡ 5c402564-e0e1-11eb-105b-951cfa19cacd
md"""
# Julia for Statistics and Data Science

- *Cecile Ane*, *Douglas Bates* & *Claudia Solis-Lemus* all with U. of Wisconsin - Madison

1. What is **Julia**?
    - How does it differ from other languages, particularly **R** and **Python**, used for data science?
    - How do I get started?
2. A deep dive into a shallow function - how does compilation work?
3. Motivating example -- `MixedModels.jl` compared to R's `lme4`
4. Some recommended packages for Statistics and Data Science
5. `RCall.jl` and `PyCall.jl`: to allow you to build on your current skills
"""

# ╔═╡ 7e8e3903-a76d-4310-b5df-8508fdd7dba3
md"""
## What is **Julia**?

- an Open Source language designed for "technical computing".  The motivation is best explained in [this blog post](https://julialang.org/blog/2012/02/why-we-created-julia/) by its creators.
- development of the [language itself](https://github.com/JuliaLang/julia) and most packages, e.g. [DataFrames](https://github.com/JuliaData/DataFrames.jl), takes place on github.
- a few things to notice about [github.com/JuliaLang/julia](https://github.com/JuliaLang/julia)
    - nearly 1100 contributors
    - over 40,000 issues or pull requests
    - over 50,000 commits
    - the most common coding language used in the `julia` repository is Julia
- installation is easiest using binary downloads from [julialang.org](https://julialang.org/downloads)
"""

# ╔═╡ dc65b3ed-2acf-4bf5-be2e-77dde963a7f1
md"""
## Package Ecosystem
- Julia packages are public git repositories (usually on `github.com` or `gitlab.com`) listed in the [General Registry](https://github.com/JuliaRegistries/General)
- see the [Packages tab at julialang.org](https://julialang.org/packages) for links to package exploration tools.  I prefer the one at [JuliaHub](https://juliahub.com/ui/packages)
    + when browsing Julia packages visit the repository and look at the Languages bar on the right hand side.  Most packages are 100% Julia code.  As [Erik Engheim wrote](https://medium.com/codex/is-julia-really-fast-12cd7caef96b), "it's turtles all the way down".
    + [this blog posting](https://julialang.org/blog/2021/08/general-survey/) provides some analysis of the package registry contents
- setting version numbers according to [semantic versioning](https://semver.org/) is strongly encouraged
- often packages are maintained by organizations on github.com, e.g. DataFrames.jl belongs to the JuliaData organization.
"""

# ╔═╡ bb892d27-63f6-4267-bfcc-758e743c31d2
md"""
## Ways to use Julia
1. Start the App and use the Read-Eval-Print-Loop (REPL) **[demo this]**
    - the REPL has name completion and other conveniences
    - the REPL has "modes"
        + standard Julia input
        + help mode
        + package manager mode
        + shell mode
        + some packages, e.g. `RCall`, enable their own modes
2. An Integrated Development Environment (IDE)
    - [Visual Studio Code](https://code.visualstudio.com/) (with the Julia extension) is the most widely used
3. [Jupyter notebooks](https://jupyter.org) using the [IJulia package](https://github.com/JuliaLang/IJulia.jl)
4. [Pluto](https://github.com/fonsp/Pluto.jl) notebooks, which we are using here.
    - (+) notebooks are self-contained with regard to packages needed and versions thereof
    - (+) notebooks are reactive - changes in one place are propagated to others
    - (-) reactivity sometimes requires awkward code phrasing
    - (-) output displayed is the result of `show` on the value of a block, which sometimes misses things that would normally be displayed
"""

# ╔═╡ 864522f6-a214-4397-91cb-8d4d670e7638
md"""
## Why Julia?
- allows you to bypass the **two language problem**
    + high-level, *dynamic* languages (R, Python, Matlab/Octave, etc.) allow for ease of use and high productivity
    + often code in these languages is unable to give the performance required for high-throughput, production uses
    + the usual solution is to re-write the performance-critical parts in a low-level, *static* language (C, C++, Fortran) and integrate that with the higher-level code
- language and support system are carefully crafted to provide **both** high level tools and high performance
- recently developed language with the benefit of hindsight and modern tools.
"""

# ╔═╡ d78b2581-31cd-4a08-9c8f-59aedc5c3086
md"""
## Why not Julia?
- recently developed language
    + ecosystem is not as mature as R, Python, etc.
    + don't have time to learn a new language and support system because this proposal is due Friday
"""

# ╔═╡ 0aab07a1-893e-4c8b-ae8d-198146058d2e
md"""
## In what ways is **Julia** similar to **R**?

- Algorithms are expressed as **functions**
- Functions are *generic* - different *methods* can be defined for argument *signatures*
    * in **R** a function must be explicitly designated as a generic function
    * **R** has different systems of generic functions and methods (S3 and S4)
        - S3 method dispatch is on the first argument's class only (single dispatch)
        - S4 allows for **multiple dispatch**
- In Julia all functions are generic and all functions use multiple dispatch
"""

# ╔═╡ 85fcacb1-7ad2-4fb8-9c87-28cfaa0f1404
md"""
## What makes Julia fast?
- The first time a particular method is invoked, it is compiled using a *Just In Time* compiler based on [**LLVM**](https://llvm.org) - the low-level virtual machine
    * good news - the actual execution is fast and subsequent calls are fast
    * bad news - the first call can be slow because of the compilation overhead
        + known informally as the "Time To First Plot" problem
- Ease of programming and ease of compilation are often antithetical goals
    * ease of programming emphasizes generality provided by languages with dynamic types
    * ease of compilation requires very specific, usually static, types b/c that's what the processor works with
- In Julia methods are often defined for general, abstract argument types but compilation takes place for specific, concrete types.

Time for a deep dive (notebook named `2compilation.jl`)
"""

# ╔═╡ Cell order:
# ╟─8b92a6cf-cd6a-4364-9f59-598bcf206ef1
# ╟─5c402564-e0e1-11eb-105b-951cfa19cacd
# ╟─7e8e3903-a76d-4310-b5df-8508fdd7dba3
# ╟─dc65b3ed-2acf-4bf5-be2e-77dde963a7f1
# ╟─bb892d27-63f6-4267-bfcc-758e743c31d2
# ╟─864522f6-a214-4397-91cb-8d4d670e7638
# ╟─d78b2581-31cd-4a08-9c8f-59aedc5c3086
# ╟─0aab07a1-893e-4c8b-ae8d-198146058d2e
# ╟─85fcacb1-7ad2-4fb8-9c87-28cfaa0f1404


text:
# MIT License
#
# Copyright (c) 2020 Joshua E Gentry

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
using Suppressor

datadir = mktempdir(;cleanup = false)

@test download_languages("eng"; target = datadir) == true
@test download_pdf_font(;target = datadir) == true

include("tess_data.jl")
include("common.jl")
include("tess_inst.jl")
include("tess_inst_config.jl")
include("tess_inst_get_var.jl")
include("tess_inst_set_var.jl")
include("tess_inst_set_debug_var.jl")
include("tess_inst_image.jl")
include("tess_inst_output.jl")
include("tess_output.jl")
include("tess_run_pipeline_callback.jl")
include("tess_run_pipeline_filename.jl")
include("tess_pipeline_alto.jl")
include("tess_pipeline_hocr.jl")
include("tess_pipeline_lstm_box.jl")
include("tess_pipeline_pdf.jl")
include("tess_pipeline_text.jl")
include("tess_pipeline_tsv.jl")
include("tess_pipeline_unlv.jl")
include("tess_pipeline_word_box.jl")
include("tess_pipeline_all.jl")

rm(datadir;recursive = true)


text:
using Random
using PyCall
using ArviZ: attributes

try
    ArviZ.initialize_bokeh()
catch
    @info "bokeh backend not available. bokeh tests will be skipped."
end

py"""
class PyNullObject(object):
   def __init__(self):
       pass
"""

function create_model(seed = 10)
    rng = MersenneTwister(seed)
    J = 8
    nchains = 4
    ndraws = 500
    data = Dict(
        "J" => J,
        "y" => [28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0],
        "sigma" => [15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0],
    )
    posterior = Dict(
        "mu" => randn(rng, nchains, ndraws),
        "tau" => abs.(randn(rng, nchains, ndraws)),
        "eta" => randn(rng, nchains, ndraws, J),
        "theta" => randn(rng, nchains, ndraws, J),
    )
    posterior_predictive = Dict("y" => randn(rng, nchains, ndraws, J))
    sample_stats = Dict(
        "energy" => randn(rng, nchains, ndraws),
        "diverging" => (randn(rng, nchains, ndraws) .> 0.90),
        "max_depth" => (randn(rng, nchains, ndraws) .> 0.90),
        "log_likelihood" => randn(rng, nchains, ndraws, J),
    )
    prior = Dict(
        "mu" => randn(rng, nchains, ndraws) / 2,
        "tau" => abs.(randn(rng, nchains, ndraws)) / 2,
        "eta" => randn(rng, nchains, ndraws, data["J"]) / 2,
        "theta" => randn(rng, nchains, ndraws, data["J"]) / 2,
    )
    prior_predictive = Dict("y" => randn(rng, nchains, ndraws, J) / 2)
    sample_stats_prior = Dict(
        "energy" => randn(rng, nchains, ndraws),
        "diverging" => Int.(randn(rng, nchains, ndraws) .> 0.95),
    )
    model = from_dict(
        posterior = posterior,
        posterior_predictive = posterior_predictive,
        sample_stats = sample_stats,
        prior = prior,
        prior_predictive = prior_predictive,
        sample_stats_prior = sample_stats_prior,
        observed_data = Dict("y" => data["y"]),
        dims = Dict("y" => ["obs_dim"], "log_likelihood" => ["obs_dim"]),
        coords = Dict("obs_dim" => 1:J),
    )
    return model
end

models() = (model_1 = create_model(10), model_2 = create_model(11))

function noncentered_schools_data()
    return Dict(
        "J" => 8,
        "y" => [28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0],
        "sigma" => [15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0],
    )
end

function check_multiple_attrs(test_dict, parent)
    failed_attrs = []
    for (dataset_name, attributes) in test_dict
        if Symbol(dataset_name) ∈ propertynames(parent)
            dataset = getproperty(parent, Symbol(dataset_name))
            for attribute in attributes
                if Symbol(attribute) ∉ propertynames(dataset)
                    push!(failed_attrs, (dataset_name, attribute))
                end
            end
        else
            push!(failed_attrs, dataset_name)
        end
    end
    return failed_attrs
end

dimsizes(ds) = ds._dims
convertindex(x::AbstractArray) = x
convertindex(o::PyObject) = o.array.values
vardict(ds) = Dict(k => convertindex(v._data) for (k, v) in ds._variables)
dimdict(ds) = Dict(k => v._dims for (k, v) in ds._variables)


text:
#------------------------------------------------------------------------------
"""
    FindNoNaNPs(Keepdim,x...)

Find rows (if Keepdim==1) which have no NaNs missing in other dimensions (eg. in no columns).

# Input
- `Keepdim::Int`: 1 if check rows, 2 if check columns, etc
- `z::Array`: one or several numerical arrays

# Output
- `vvb::BitArray(T)`: vector, element t is true if row (if Keepdim==1) t has no NaN or missing

# Notice
- Set Keepdim=2 if we should instead look for NaNs/missings along rows (and other dimensions).

Paul.Soderlind@unisg.ch

"""
function FindNoNaNPs(Keepdim,x...)

  xNum  = length(x)
  T     = size(x[1],Keepdim)                 #length of output

  xDims = maximum([ndims(x[i]) for i=1:xNum])
  dims  = setdiff(1:xDims,Keepdim)           #dimensions to check

  vvM = falses(T,xNum)
  for i = 1:xNum                             #loop over inputs
    vvM[:,i] = any(isnan.(x[i]) .| ismissing.(x[i]),dims=dims)
  end

  vvb = vec(.!any(vvM,dims=2))      #rows witout NaN/missing in any of the x matrices

  return vvb

end
#------------------------------------------------------------------------------

max_stars_repo_name: PaulSoderlind/PaulSoderlindCode

max_issues_repo_name: PaulSoderlind/PaulSoderlindCode

max_forks_repo_name: PaulSoderlind/PaulSoderlindCode