text:
############################################################################################
# Models to be used in construction
objectives = [
    Objective(ModelWrapper(MyBaseModel(), myparameter, FlattenDefault()), data_uv),
    Objective(ModelWrapper(MyBaseModel(), myparameter, FlattenDefault(; output = Float32)), data_uv)
]
#=
iter = 2
backend = backends[1]
matrixadaption = massmatrices[1]
kernel = kernels[1]
generated = generating[2]
=#
############################################################################################
## Make model
for iter in eachindex(objectives)
    _obj = objectives[iter]
    _flattentype = _obj.model.info.reconstruct.default.output
    @testset "Kernel construction and propagation, all models" begin
        ## MCMC AD backends
        for backend in backends
            ## Mass matrix adaption settings
            for matrixadaption in massmatrices
                for generated in generating
                    ## Define MCMC default tuning parameter
                    mcmcdefault = MCMCDefault(;
                        stepsize = ConfigStepsize(; stepsizeadaption=UpdateFalse()),
                        proposal = ConfigProposal(; metric = matrixadaption),
                        GradientBackend = backend,
                        generated = generated)
                    ## MCMC kernels
                    for kernel in kernels
                        ## Check if Default options work
                        MCMC(_rng, kernel, _obj, mcmcdefault)
                        MCMC(_rng, kernel, _obj, mcmcdefault, SampleDefault())
                        MCMC(kernel, keys(_obj.tagged.parameter))
                        ## Check if constructors work
                        MCMCConstructor(kernel, keys(_obj.tagged.parameter)[begin], mcmcdefault)
                        constructor = MCMCConstructor(kernel, keys(_obj.tagged.parameter), mcmcdefault)
                        constructor(_rng, _obj.model, _obj.data, _flattentype(1.0), SampleDefault())
                        ## Initialize kernel and check if it can be run
                        mcmckernel = MCMC(
                            _rng,
                            kernel,
                            _obj,
                            mcmcdefault
                        )
                        _val1, _diag1 = propose(_rng, mcmckernel, _obj)
                        _val2, _diag2 = propose!(_rng, mcmckernel, _obj.model, _obj.data)

                        ## Test if Float types for proposal calculation all have same type after proposal step
                        # !NOTE kernel.result structs are already checked in ModelWrappers
                        @test mcmckernel.tune.stepsize.ϵ isa _flattentype
                        @test eltype(mcmckernel.tune.proposal.Σ) ==
                            eltype(mcmckernel.tune.proposal.Σ⁻¹ᶜʰᵒˡ) ==
                            eltype(mcmckernel.tune.proposal.chain) ==  _flattentype

                        ## Postprocessing
                        @test _diag1 isa infer(_rng, AbstractDiagnostics, mcmckernel, _obj.model, _obj.data)
                        @test _diag2 isa infer(_rng, AbstractDiagnostics, mcmckernel, _obj.model, _obj.data)
                        @test _diag1.base.prediction isa infer(_rng, mcmckernel, _obj.model, _obj.data)
                        @test _diag1.generated isa BaytesMCMC.infer_generated(_rng, mcmckernel, _obj.model, _obj.data)
                        results([_diag1, _diag2], mcmckernel, 2, [.1, .2, .5, .8, .9])
                        divs = BaytesMCMC.print_divergences([_diag1, _diag2], mcmckernel.tune.phase);
                        @test length(divs[1]) == length(divs[2])
                        BaytesMCMC.result!(mcmckernel, BaytesMCMC.get_result(mcmckernel))
                        generate_showvalues(_diag1)()

                        ## Tuning settings
                        #!NOTE: We do not need to check this for all backends as separate propose calls already evaluated and tune updates are independent of gradients
                        if backend == backends[1]
                            for iter in Base.OneTo(mcmckernel.tune.phase.slices[end])
                                propose(_rng, mcmckernel, _obj)
                            end
                        end

                        ## Check if MCMC also works with more/less data
                        propose!(_rng, mcmckernel, _obj.model, randn(_rng, length(data_uv)+10))
                        propose!(_rng, mcmckernel, _obj.model, randn(_rng, length(data_uv)-10))

                    end
                end
            end
        end
    end
end

############################################################################################
#!NOTE: MH often does not reach stepsize in default number of steps - keep out in settings
for iter in eachindex(objectives)
    _obj = objectives[iter]
    _flattentype = _obj.model.info.reconstruct.default.output
    @testset "Stepsize initial estimate, all models" begin
        for backend in backends
            mcmcdefault = MCMCDefault(;
                stepsize = ConfigStepsize(; initialstepsize = UpdateTrue(), stepsizeadaption=UpdateTrue()),
                GradientBackend = backend
            )
            ## MCMC kernels
            for kernel in gradientkernels #gradientkernels if error during test
                ## Initialize kernel and check if it can be run
                mcmckernel = MCMC(
                    _rng,
                    kernel,
                    _obj,
                    mcmcdefault
                )
                @test mcmckernel.tune.stepsize.ϵ isa _flattentype
                ## Stepsize Adaption
                _val1, _diag1 = propose(_rng, mcmckernel, _obj)
                @test mcmckernel.tune.stepsize.ϵ isa _flattentype
                @test eltype(mcmckernel.tune.proposal.Σ) ==
                    eltype(mcmckernel.tune.proposal.Σ⁻¹ᶜʰᵒˡ) ==
                    eltype(mcmckernel.tune.proposal.chain) ==  _flattentype
                ## Stepsize Adaption
                _val2, _diag2 = propose!(_rng, mcmckernel, _obj.model, _obj.data, _obj.temperature, UpdateTrue())
                @test mcmckernel.tune.stepsize.ϵ isa _flattentype
                @test eltype(mcmckernel.tune.proposal.Σ) ==
                    eltype(mcmckernel.tune.proposal.Σ⁻¹ᶜʰᵒˡ) ==
                    eltype(mcmckernel.tune.proposal.chain) ==  _flattentype
                ## Stepsize Adaption
                _val3, _diag3 = propose!(_rng, mcmckernel, _obj.model, _obj.data, _obj.temperature, UpdateFalse())
                @test mcmckernel.tune.stepsize.ϵ isa _flattentype
                @test eltype(mcmckernel.tune.proposal.Σ) ==
                    eltype(mcmckernel.tune.proposal.Σ⁻¹ᶜʰᵒˡ) ==
                    eltype(mcmckernel.tune.proposal.chain) ==  _flattentype

                ## Tuning settings
                #!NOTE: We do not need to check this for all backends as separate propose calls already evaluated and tune updates are independent of gradients
                if backend == backends[1] && kernel == kernels[1]
                    for iter in Base.OneTo(mcmckernel.tune.phase.slices[end]+10)
                        #!NOTE: This only checks for stepsize adaption, which is fixed above
                        propose(_rng, mcmckernel, _obj)
                    end
                end
            end
        end
    end
end


text:
using BenchmarkTools
using Random, Statistics
using Hungarian

const seed = Random.seed!(7)
const SUITE = BenchmarkGroup()

SUITE["square"] = BenchmarkGroup(["square-matrix"])
SUITE["square"]["Float64"] = BenchmarkGroup(["float"])
SUITE["square"]["UInt16"] = BenchmarkGroup(["integer"])

for n in [10, 50, 100, 200, 400, 800, 1000, 2000]
    A = rand(seed, n, n)
    SUITE["square"]["Float64"]["$n x $n"] = @benchmarkable hungarian($A)
end

for n in [100, 500, 1000, 2000, 4000, 8000, 16000]
    A = rand(seed, UInt16, n, n)
    SUITE["square"]["UInt16"]["$n x $n"] = @benchmarkable hungarian($A)
end

SUITE["non-square"] = BenchmarkGroup(["non-square-matrix"])
SUITE["non-square"]["Float64"] = BenchmarkGroup(["float"])

for (n, m) in zip([10, 50, 100, 200, 400, 800, 1000], [20, 100, 200, 400, 800, 1000, 2000])
    A = rand(seed, n, m)
    SUITE["non-square"]["Float64"]["$n x $m"] = @benchmarkable hungarian($A)
    A = rand(seed, m, n)
    SUITE["non-square"]["Float64"]["$m x $n"] = @benchmarkable hungarian($A)
end

ext: jl


text:
export
    AccSteerDriver,
    IDMDriver,
    UrbanDriver,
    MultiPtsDriver,
    MultiPtsTurningDriver,
    MOBILDriver,
    excute_action!

include("acc_steer_drivers.jl")
include("IDMDriver.jl")
include("MOBILDriver.jl")
include("urban_drivers.jl")
include("multiPts_driver.jl")
include("multiPts_functions_GD.jl")
include("waypoints_setting_functions.jl")
include("multiPtsTurning_driver.jl")


text:
function init_pseudo_observable_mappings!(m::AnSchorfheide) # do not edit inputs
    # The goal of this function is to populate the field m.pseudo_observable_mappings
    # with an OrderedDict mapping name (as Symbol) to a PseudoObservable instance

    # make sure you create `pseudo_names`, but it can be empty or have different names in it
    pseudo_names = [:y_t, :π_t, :z_t, :NominalFFR, :RealFFR]

    # Create PseudoObservable objects
    pseudo = OrderedDict{Symbol,PseudoObservable}() # do not edit
    for k in pseudo_names
        pseudo[k] = PseudoObservable(k)
    end

    # Starting here, you may want to edit the code

    # Fill in names and reverse transforms
    pseudo[:y_t].name = "Output Growth"
    pseudo[:y_t].longname = "Output Growth Per Capita"

    pseudo[:π_t].name = "Inflation"
    pseudo[:π_t].longname = "Inflation"
    pseudo[:π_t].rev_transform = quartertoannual

    pseudo[:z_t].name     = "z_t"
    pseudo[:z_t].longname = "z_t"

    pseudo[:NominalFFR].name     = "Nominal FFR"
    pseudo[:NominalFFR].longname = "Nominal FFR at an annual rate"

    pseudo[:RealFFR].name     = "Real FFR"
    pseudo[:RealFFR].longname = "Real FFR at an annual rate"

    # Add to model object
    m.pseudo_observable_mappings = pseudo # do not edit this line
end


text:
rcParams = PyPlot.PyDict(PyPlot.matplotlib."rcParams")
rcParams["lines.linewidth"] = 3 # Change linewidth

function fill_past(m, ylims)
    domain_idx = (t(m) .> m.present_year)
    fill_between(
        t(m)[.~domain_idx],
        ones(size(t(m)[.~domain_idx])) * ylims[1] * 2.,
        ones(size(t(m)[.~domain_idx])) * ylims[2] * 2.,
        facecolor="b", alpha=0.1
    )
    ylim(ylims)
    return
end

function plot_emissions(m::ClimateModel)
    title("effective emissions")
    #fill_past(m, ylims)
    plot(t(m), zeros(size(t(m))), "k-", alpha=0.9)
    plot(t(m), effective_emissions(m), linestyle = "--", color="grey", label=L"$rq$ (no-policy baseline)")
    plot(t(m), effective_emissions(m, M=true), color="C0", label=L"$rq(1-M)$ (controlled)")
    plot(t(m), effective_emissions(m, M=true, R=true), color="C1", label=L"$rq(1-M) - rq_{0}R$ (controlled)")
    ylimit = maximum(effective_emissions(m)) * 1.1
    ylims = [-ylimit, ylimit]
    ylabel(L"effective CO$_{2e}$ emissions [ppm / yr]")
    xlim(t(m)[1],2200.)
    ylim(minimum(effective_emissions(m, M=true, R=true))-5.,1.1*maximum(effective_emissions(m)))
    xticks(t(m)[1]:40.:2200.)
    xlabel("year")
    grid(true)
    return
end

function plot_concentrations(m::ClimateModel)
    title("concentrations")
    #fill_past(m, ylims)
    plot(t(m), c(m), "--", color="gray", label=L"$c$ (no-policy baseline)")
    plot(t(m), c(m, M=true), color="C0", label=L"$c_{M}$")
    plot(t(m), c(m, M=true, R=true), color="C1", label=L"$c_{M,R}$")
    ylims = [0., maximum(c(m))*1.05]
    ylabel(L"CO$_{2e}$ concentration [ppm]")
    xlabel("year")
    xlim(t(m)[1],2200.)
    ylim(100., 1.05*maximum(c(m)))
    xticks(t(m)[1]:40.:2200.)
    grid(true)
    return
end

function plot_temperatures(m::ClimateModel)
    title("temperature change since 1850")
    #fill_past(m, ylims)
    plot(t(m),2.0.*ones(size(t(m))),"k--", alpha=0.9)
    plot(t(m),T(m), "--", color="gray", label=L"$T$ (no-policy baseline)")
    plot(t(m),T(m, M=true), color="C0", label=L"$T_{M}$")
    plot(t(m),T(m, M=true, R=true), color="C1", label=L"$T_{M,R}$")
    plot(t(m),T(m, M=true, R=true, G=true), color="C3", label=L"$T_{M,R,G}$")
    plot(t(m),T(m, M=true, R=true, G=true, A=true), color="C2", label=L"$T_{M,R,G,A}$")
    ylims = [0., maximum(T(m)) * 1.05]
    ylabel(L"temperature anomaly [$^{\circ}$C]")
    xlabel("year")
    xlim(t(m)[1],2200.)
    xticks(t(m)[1]:40.:2200.)
    grid(true)
    legend()
    return
end

function plot_controls(m::ClimateModel)
    title("optimized control deployments")
    plot(t(m), m.controls.mitigate, color="C0", label=L"$M$ (emissions mitigation)")
    plot(t(m), m.controls.remove, color="C1", label=L"$R$ (carbon dioxide removal)")
    plot(t(m), m.controls.adapt, color="C2", label=L"$A$ (adaptation)")
    plot(t(m), m.controls.geoeng, color="C3", label=L"$G$ (solar geoengineering)")
    ylims = [0., 1.]
    ylim([0,1.0])
    ylabel("fractional control deployment")
    xlabel("year")
    xlim(t(m)[1],2200.)
    xticks(t(m)[1]:40.:2200.)
    grid(true)
    return
end

function plot_benefits(m::ClimateModel; discounting=true)
    domain_idx = (t(m) .> m.domain.present_year)

    fill_between(
        t(m)[domain_idx],
        0 .*ones(size(t(m)))[domain_idx],
        net_benefit(m, discounting=discounting, M=true, R=true, G=true, A=true)[domain_idx],
        facecolor="grey", alpha=0.2
    )
    plot(t(m)[domain_idx], 0 .*ones(size(t(m)))[domain_idx], "--", color="gray", alpha=0.9, label="no-policy baseline")
    plot(t(m)[domain_idx], benefit(m, discounting=discounting, M=true, R=true, G=true, A=true)[domain_idx], color="C1", label="benefits (of avoided damages)")
    plot(t(m)[domain_idx], cost(m, discounting=discounting, M=true, R=true, G=true, A=true)[domain_idx], color="C3", label="costs (of climate controls)")
    plot(t(m)[domain_idx], net_benefit(m, discounting=discounting, M=true, R=true, G=true, A=true)[domain_idx], color="k", label="net benefits (benefits - costs)")
    ylabel(L"discounted costs and benefits [10$^{12}$ \$ / year]")
    xlabel("year")
    xlim(t(m)[1],2200.)
    xticks(t(m)[1]:40.:2200.)
    grid(true)
    title("cost-benefit analysis")
    return
end

function plot_damages(m::ClimateModel; discounting=true, percent_GWP=false)
    Enorm = deepcopy(E(m))/100.
    if ~percent_GWP; Enorm=1.; end;

    domain_idx = (t(m) .> m.domain.present_year)
    fill_between(
        t(m)[domain_idx],
        0 .*ones(size(t(m)))[domain_idx],
        (cost(m, discounting=discounting, M=true, R=true, G=true, A=true) ./ Enorm)[domain_idx],
        facecolor="C3", alpha=0.2
    )
    Tgoal = 2.
    plot(
        t(m)[domain_idx],
        (damage(m.economics.β, E(m), Tgoal, 0., discount=discount(m)) ./ Enorm)[domain_idx],
        "k--", alpha=0.5, label=L"damage threshold at 2$^{\circ}$ C with $A=0$"
    )
    damages = damage(m, discounting=discounting, M=true, R=true, G=true, A=true)
    costs = cost(m, discounting=discounting, M=true, R=true, G=true, A=true)
    plot(t(m)[domain_idx], (damage(m, discounting=discounting) ./ Enorm)[domain_idx], color="C0", label="uncontrolled damages")
    plot(t(m)[domain_idx], ((damages .+ costs)./ Enorm)[domain_idx], color="k", label="net costs (controlled damages + controls)")
    plot(t(m)[domain_idx], (damages ./ Enorm)[domain_idx], color="C1", label="controlled damages")
    plot(t(m)[domain_idx], (costs ./ Enorm)[domain_idx], color="C3", label="cost of controls")
    ylim([0, maximum((damage(m, discounting=discounting) ./ Enorm)[domain_idx]) * 0.75])

    if ~percent_GWP;
        if ~discounting;
            ylabel(L"costs [10$^{12}$ \$ / year]");
        else;
            ylabel(L"discounted costs [10$^{12}$ \$ / year]");
        end
    else
        if ~discounting
            ylabel("costs [% GWP]")
        else
            ylabel("discounted costs [% GWP]")
            print("NOT YET SUPPORTED")
        end
    end

    xlabel("year")
    xlim(t(m)[1],2200.)
    xticks(t(m)[1]:40.:2200.)
    grid(true)
    title("costs of avoiding a damage threshold")
    legend()
    return
end

function plot_state(m::ClimateModel; new_figure=true, plot_legends=true)
    if new_figure
        fig, axs = subplots(2, 3, figsize=(14,8))
        axs = vcat(axs...)
    end

    sca(axs[1])
    plot_emissions(m)
    title("a)", loc="left")
    sca(axs[2])
    plot_concentrations(m)
    title("b)", loc="left")
    sca(axs[3])
    plot_temperatures(m)
    title("c)", loc="left")

    sca(axs[4])
    plot_controls(m)
    title("d)", loc="left")
    sca(axs[5])
    plot_benefits(m)
    title("e)", loc="left")
    sca(axs[6])
    plot_damages(m)
    title("f)", loc="left")

    if plot_legends;
        for ii in 1:6
            sca(axs[ii])
            if ii <= 2;
                legend(loc="lower left");
            else
                legend(loc="upper left");
            end
        end
    end
    tight_layout()

    return fig, axs
end


text:
# mutable struct FileDFG
#     folderName::String
#     FileDFG(folderName::String)::FileDFG = new(foldername)
# end


text:
using PyCall
unshift!(PyVector(pyimport("sys")["path"]), "")
@pyimport merra2

# Here we process multiple variables at a time to avoid downloading the
# original data twice (all these variables are in the same files).
# These variables names are user choices, their merra-2 equivalent are
# specified below or in the default merra2_variables.py
var_names = ["tasmin"]#, "tasmax","prmax"]
delete_temp_dir = true
download_dir = "/path/to/output"
merra2_server = "https://goldsmr4.gesdisc.eosdis.nasa.gov/data/"

# The variables specification is in the same order as var_names above.
# esdt_dir, collection and merra_name can be found from
# https://gmao.gsfc.nasa.gov/pubs/docs/Bosilovich785.pdf
# https://goldsmr4.gesdisc.eosdis.nasa.gov/data/
# standard_name comes from
# http://cfconventions.org/standard-names.html
# Optionally, if all the variables are already in the default
# merra2_variables.py, this can be set to None.
global merra2_var_dicts
merra2_var_dicts = [Dict("esdt_dir"=> "M2SDNXSLV.5.12.4",
                     "collection"=> "statD_2d_slv_Nx",
                     "merra_name"=> "T2MMAX",
                     "standard_name"=> "air_temperature",
                     "cell_methods"=> "time: max"),
                    Dict("esdt_dir"=> "M2SDNXSLV.5.12.4",
                     "collection"=> "statD_2d_slv_Nx",
                     "merra_name"=> "T2MMIN",
                     "standard_name"=> "air_temperature",
                     "cell_methods"=> "time: min"),
                    Dict("esdt_dir"=> "M2SDNXSLV.5.12.4",
                     "collection"=> "statD_2d_slv_Nx",
                     "merra_name"=> "TPRECMAX",
                     "standard_name"=> "precipitation_flux",
                     "cell_methods"=> "time: max")]

# This loop will create annual files of daily MERRA2 data
for yyyy in range(2017,2017)
    merra2.daily_download_and_convert(
        merra2_server, var_names, merra2_var_dicts=nothing, initial_year=yyyy,
        final_year=yyyy, initial_month=1, final_month=12, initial_day=1,
        final_day=nothing, download_dir=download_dir,
        delete_temp_dir=delete_temp_dir)
end


text:
__precompile__()

module CNNVisualize

using Flux, Plots, Images, Augmentor
using GR
using Flux: Tracker
using Flux.Tracker: data

clibrary(:colorcet)

include("backpropagation.jl")
include("gradcam.jl")
include("deepdream.jl")
include("utils.jl")

export GradCAM, GuidedGradCAM, Backprop, VanillaBackprop, GuidedBackprop,
       Deconvolution, save_gradient_images, save_gradcam, save_grayscale_gradient,
       positive_negative_saliency, im2arr_rgb, image_to_arr, load_image, save_image,
       load_model, deepdream, make_step

# Some re exports

export save, load, cpu, gpu

end # module


text:
@everywhere using MCMC

@everywhere mymodel = model(v-> -dot(v,v), grad=v->-2v, init=ones(3))
@everywhere mytasks = mymodel * [HMC(0.75) for i in 1:10] * SerialMC(steps=50000, burnin=5000)

mychains = prun(mytasks)

[acceptance(chain) for chain in mychains]


text:
type SDF_FD <: SDFDynamicSystem
    omg::Float64
    zta::Float64
    phi::Float64
    u0::Float64
    v0::Float64

    SDF_FD(omg::Float64, zta::Float64, phi::Float64) = new(omg, zta, phi, 0.0, 0.0)
end

function systemEquation(s::SDF_FD, p::Function)
    function dydt(y::AbstractArray{Float64,1}, t::Float64)
        [y[2]; p(t)-2.0*s.zta*s.omg*y[2]-s.phi*g*sign(y[2])-s.omg*s.omg*y[1]]
    end
    dydt
end

function response(s::SDF_FD, p::Function, t::AbstractArray{Float64,1})
    y0 = [s.u0; s.v0]

    f = systemEquation(s, p)

    y = ode4(f, y0, t)

    u = y[:,1]
    v = y[:,2]
    fdz = 2.0*s.zta*s.omg*v
    fdp = s.phi*g*sign(v)
    a = map(p,t)-fdz-fdp-s.omg*s.omg*u

    [u v a fdz fdp]
end

max_stars_repo_name: Panchatantra/DSR

max_issues_repo_name: Panchatantra/DSR

max_forks_repo_name: Panchatantra/DSR


text:
using Flux

function make_onehot(::Type{T}, ::Val{alphabet_size}, labels::AbstractVector{S}) where {T, S, alphabet_size}
    bcast_labels = convert(XRTArray{T}, HloBroadcast((1,), (alphabet_size, length(labels)))(labels))
    iota = convert(XRTArray{T}, HloBroadcast((0,), (alphabet_size, length(labels)))(XRTArray(UInt32(1):UInt32(alphabet_size))))
    T.(iota .== bcast_labels)
end
@Zygote.nograd make_onehot
@Zygote.nograd Flux.OneHotMatrix

function Base.convert(::Type{<:XRTArray{T}}, m::Flux.OneHotMatrix) where {T}
    make_onehot(T, Val(size(m)[1]), m.data)
end


text:
"""
	OpticalFlowAlgo

An optical flow algorithm with given parameters.
"""

abstract type OpticalFlowAlgo end

"""
	optical_flow(prev_img, next_img, algo)

Returns the flow from `prev_img` to `next_image` for the points provided in the `algo`
type using specified algorithm `algo`.
"""

function optical_flow(prev_img::AbstractArray{T, 2}, next_image::AbstractArray{T,2}, algo::OpticalFlowAlgo) where T <: Gray
	# sanity checks
	@assert size.(indices(prev_img)) == size.(indices(next_image)) "Images must have the same size"

	# dispatch appropriate algorithm
	optflow(prev_img, next_image, algo)
end

#------------------
# IMPLEMENTATIONS
#------------------

include("lucas_kanade.jl")
include("farneback.jl")


text:
function gw_setup(;
        sys_root = _GW_SYSTEM_DFLT_ROOT,
        url::String,
        verbose = false,
    )

    # init
    gw = GitWorker(;sys_root, remote_url = url)
    gl = gitlink(gw)

    # init gitlink
    init_ok = GitLinks.instantiate(gl; verbose)
    if init_ok
        @info("Worker connected", url)
    else
        error("GitLink init fail (run 'git ls-remote <remote_url>' for testing connection)\nremote_url:$(url)")
    end

    gw_curr(gw)

    return gw
end

gw_setup(gw::GitWorker; verbose = false) =
    gw_setup(; verbose, sys_root = sys_root(gw), url = remote_url(gw))

max_stars_repo_name: josePereiro/GitWorkers

max_issues_repo_name: josePereiro/GitWorkers

max_forks_repo_name: josePereiro/GitWorkers


text:
#=
Test resnet operations:
=#

@testset "ResNet" begin
    # define vars
    # use for testing
    @testset "Basic example" begin

        # Coming from https://github.com/denizyuret/Knet.jl/blob/master/examples/resnet/ -- license in src/resnet.jl

        function test_resnet(image, model, target)
            # "resnet.jl (c) Ilker Kesen, 2017. Classifying images with Deep Residual Networks."

            o = Dict(:model => model, :atype => "KnetArray{Float32}", :top => 5, :image => image)

            @test [(k,v) for (k,v) in o][[1,2,4]] == [(:atype, "KnetArray{Float32}"), (:top, 5), (:model, model)]

            atype = eval(Meta.parse(o[:atype]))

            model = matconvnet(o[:model])

            avgimg = model["meta"]["normalization"]["averageImage"]
            avgimg = convert(Array{Float32}, avgimg)
            description = model["meta"]["classes"]["description"]

            w, ms = get_params(model["params"], atype)

            img = imgdata(o[:image], avgimg)
            img = convert(atype, img)
            # get model by length of parameters
            modeldict = Dict(
                162 => (resnet50, "resnet50"),
                314 => (resnet101, "resnet101"),
                467 => (resnet152, "resnet152"))

            !haskey(modeldict, length(w)) && error("wrong resnet MAT file")
            resnet, name = modeldict[length(w)]

            @info("Testing classification with $name")
            y1 = resnet(w,img,ms)
            z1 = vec(Array(y1))
            s1 = sortperm(z1,rev=true)
            p1 = exp.(logp(z1))
            @test description[s1[1:o[:top]]][1] == target

            println()
        end

        elephant = download("http://home.mweb.co.za/pa/pak04857/uniweb/animalimages/elephantthumb.jpg")
        cat = "https://github.com/BVLC/caffe/raw/master/examples/images/cat.jpg"

        test_resnet(elephant, "imagenet-resnet-101-dag", "African elephant, Loxodonta africana")
        test_resnet(cat, "imagenet-resnet-50-dag", "gibbon, Hylobates lar") # Should guess incorretly
        test_resnet(cat, "imagenet-resnet-152-dag", "gibbon, Hylobates lar") # Should guess incorrectly
    end

end


text:
using LinearAlgebra
using PyAMG
using SparseArrays
using Test

L1d(N) = spdiagm( -1 => -ones(N-1), 0 => 2*ones(N), 1 => -ones(N-1) ) * N^2
function L2d(N)
  L1 = L1d(N)
  ID = sparse(1.0I, N, N)
  A = kron(ID, L1) + kron(L1, ID)
  b = ones(size(A,1))
  return A, b
end
function L3d(N)
  L1 = L1d(N)
  ID = sparse(1.0I, N, N)
  A = kron(L1, ID, ID) + kron(ID, L1, ID) + kron(ID, ID, L1)
  b = ones(size(A,1))
  return A, b
end

println("=================================================")
println("Test 1: Julia `\\` vs PyAMG Blackbox `solve` (2D) Laplacian")
A, b = L2d(100)
print(" \\ : ")
@time x1 = A \ b
print("PyAMG-Warmup: ")
@time x2 = PyAMG.solve(A, b; tol=1e-12, verb=false);
print("PyAMG: ")
@time x3 = PyAMG.solve(A, b; tol=1e-12, verb=false)
println("|x_\\ - x_amg|_∞ = ", norm(x3 - x1, Inf))
println("|A x_amg - b|_∞ = ", norm(A * x2 - b, Inf))
@test norm(x3 - x1, Inf) < 1e-10

println("=================================================")
println("Test 2: Julia `\\` vs PyAMG Blackbox `solve` (3D)")
A, b = L3d(30)
print(" \\ : ")
@time x1 = A \ b
print("PyAMG-Warmup: ")
@time x2 = PyAMG.solve(A, b; tol=1e-12, verb=false)
print("PyAMG: ")
@time x3 = PyAMG.solve(A, b; tol=1e-12, verb=false)
println("|x_\\ - x_amg|_∞ = ", norm(x3-x1, Inf))
println("|A x_amg - b|_∞ = ", norm(A*x2 - b, Inf))
@test norm(x3-x1, Inf) < 1e-9

println("=================================================")
println("Test 3: RugeStubenSolver (3D)")
println("        50 x 50 x 50 grid = 125k dofs ")
A, b = L3d(50)
print("Create solver: ")
@time amg = RugeStubenSolver(A)
print("First solve: ")
@time x1 = solve(amg, b)
print("Second solve: (tol 1e-9)")
@time x2 = solve(amg, b; tol=1e-9)
print("Third solve: (tol 1e-6)")
@time x3 = solve(amg, b; tol=1e-6)
println("|A x_amg - b|_∞ = ", norm(A * x3 - b, Inf))
@test norm(A * x2 - b, Inf) < 1e-8

println("=================================================")
println("Test 4: \\, ldiv, and kwargs")
A, b = L2d(50)
amg = RugeStubenSolver(A)
x_solve = solve(amg, b, tol=1e-6, cycle="V", accel="cg")
set_kwargs!(amg, tol=1e-6, cycle="V", accel="cg")
x_bs = amg \ b
x_ldiv = LinearAlgebra.ldiv!(similar(b), amg, b)
@test x_solve == x_bs
@test x_solve == x_ldiv

println("=================================================")
println("Test 5: AMG as a preconditioner")
println("        100 x 100 Dirichlet problem, TOL = 1e-4")
println("        PyAMG vs CG vs PCG  (using IterativeSolvers)")
A, b = L2d(100)
amg = RugeStubenSolver(A)
M = aspreconditioner(amg)
using IterativeSolvers
TOL = 1e-4
println("Plain CG:")
@time x_cg = IterativeSolvers.cg(A, b; tol=TOL)
@time x_cg = IterativeSolvers.cg(A, b; tol=TOL)
println("PyAMG-preconditionerd CG:  (see `aspreconditioner`)")
@time x_pcg = IterativeSolvers.cg(A, b; Pl=M, tol=TOL)
@time x_pcg = IterativeSolvers.cg(A, b; Pl=M, tol=TOL)
println("PyAMG solver")
@time x_pyamg = PyAMG.solve(amg, b; tol=TOL*1e-2, accel="cg")
@time x_pyamg = PyAMG.solve(amg, b; tol=TOL*1e-2, accel="cg")
x = A \ b
println("|x_cg-x| = ", norm(x_cg - x), " \n",
         "|x_pcg-x| = ", norm(x_pcg - x), "\n",
         "|x_pyamg-x| = ", norm(x_pyamg - x) )
@test norm(x_cg - x) < 1e-5
@test norm(x_pcg - x) < 1e-5
@test norm(x_pyamg - x) < 1e-5


text:
mutable struct SearchTraceLogger{S,V}
    st::SearchTrace{S,V}
    it::Int
end

SearchTraceLogger(v) = SearchTraceLogger(SearchTrace(v), 1)

function init!(stl::SearchTraceLogger, s, v)
    stl.it = 1
    init!(stl.st, s, v)
    return stl
end

function init!(stl::SearchTraceLogger{Nothing, V}, s, v) where {S, V}
    stl.it = 1
    init!(stl.st, s, v)
    return stl
end

function init!(stl::SearchTraceLogger, v)
    stl.it = 1
    init!(stl.st, v)
    return stl
end

function log!(stl::SearchTraceLogger, data...)
    push!(stl, data)
    return stl
end

max_stars_repo_name: Michiel-VL/PolyaViz

max_issues_repo_name: Michiel-VL/PolyaViz

max_forks_repo_name: Michiel-VL/PolyaViz


text:
# # Symmetry reduction

#md # [![](https://mybinder.org/badge_logo.svg)](@__BINDER_ROOT_URL__/generated/Symmetry reduction.ipynb)
#md # [![](https://img.shields.io/badge/show-nbviewer-579ACA.svg)](@__NBVIEWER_ROOT_URL__/generated/Symmetry reduction.ipynb)
# **Adapted from**: [SymbolicWedderburn example](https://github.com/kalmarek/SymbolicWedderburn.jl/blob/tw/ex_sos/examples/ex_C4.jl)

import MutableArithmetics
const MA = MutableArithmetics
using MultivariatePolynomials
const MP = MultivariatePolynomials
using MultivariateBases
const MB = MultivariateBases

using Test #src
using DynamicPolynomials
@polyvar x[1:4]

# We would like to find the minimum value of the polynomial

poly = sum(x) + sum(x.^2)

# As we can decouple the problem for each `x[i]` for which `x[i] + x[i]^2` has
# minimum value 0.25, we would expect to get `-1` as answer.
# Can this decoupling be exploited by SumOfSquares as well ?
# For this, we need to use a certificate that can exploit the permutation symmetry of the polynomial.
# Symmetry reduction is still a work in progress in SumOfSquares, so we include the following files that will be incorporated into SumOfSquares.jl once SymbolicWedderburn.jl is released:
using SumOfSquares
include(joinpath(dirname(dirname(pathof(SumOfSquares))), "examples", "symmetry.jl"))

# We define the symmetry group as a permutation group in the variables.
# In order to do that, we define the action of a permutation on a monomial
# as the monomial obtained after permuting the variables.

using PermutationGroups
function action(mono::AbstractMonomial, p::Permutation)
    v = variables(mono)
    MP.substitute(MP.Eval(), mono, v => [v[i^p] for i in eachindex(v)])
end
function action(term::MP.AbstractTerm, el::Permutation)
    return MP.coefficient(term) * action(MP.monomial(term), el)
end
function action(poly::MP.AbstractPolynomial, el::Permutation)
    return MP.polynomial([action(term, el) for term in MP.terms(poly)])
end

G = PermGroup([perm"(1,2,3,4)"])

# We can use this certificate as follows:

import CSDP
solver = CSDP.Optimizer
model = Model(solver)
@variable(model, t)
@objective(model, Max, t)
certificate = SymmetricIdeal(Certificate.MaxDegree(SOSCone(), MonomialBasis, maxdegree(poly)), G, action)
con_ref = @constraint(model, poly - t in SOSCone(), ideal_certificate = certificate)
optimize!(model)
@test value(t) ≈ -1 #src
value(t)

# We indeed find `-1`, let's verify that symmetry was exploited:

g = gram_matrix(con_ref).sub_gram_matrices #src
@test length(g) == 4                       #src
@test g[1].basis.polynomials == [sum(x), 1.0] #src
@test size(g[1].Q) == (2, 2)                  #src
@test g[1].Q[1, 1] ≈ 1/4 atol=1e-6            #src
@test g[1].Q[1, 2] ≈ 1/2 atol=1e-6            #src
@test g[1].Q[2, 2] ≈ 1.0 atol=1e-6            #src
@test g[2].basis.polynomials == [x[1] - x[3]] #src
@test size(g[2].Q) == (1, 1)                  #src
@test g[2].Q[1, 1] ≈ 1/2 atol=1e-6            #src
@test g[3].basis.polynomials == [x[2] - x[4]] #src
@test size(g[3].Q) == (1, 1)                  #src
@test g[3].Q[1, 1] ≈ 1/2 atol=1e-6            #src
@test g[4].basis.polynomials == [x[1] - x[2] + x[3] - x[4]] #src
@test size(g[4].Q) == (1, 1)                  #src
@test g[4].Q[1, 1] ≈ 1/4 atol=1e-6            #src
for g in gram_matrix(con_ref).sub_gram_matrices
    println(g.basis.polynomials)
end

max_stars_repo_path: examples/Symmetry reduction.jl

max_issues_repo_path: examples/Symmetry reduction.jl

max_forks_repo_path: examples/Symmetry reduction.jl


text:
# # Power Network Flexibility Design
# Here we consider a stochastic flexibility design problem that is inspired
# from an example problem in Pulsipher JL, Zavala VM. *A scalable stochastic
# programming approach for the design of flexible systems*. Computers & Chemical
# Engineering. 2019 Sep 2;128:69-76. See the paper
# [here](https://doi.org/10.1016/j.compchemeng.2019.05.033).

# ## Modeling and Solution

using InfiniteOpt, Clp, Distributions

## Set the covariance matrix for the uncertain parameters
θ_nom = [0.; 60.; 10.]
covar = [80. 0 0; 0 80. 0; 0 0 120.]

## Set the dimensions
n_z = 3; n_θ = 3; n_d = 3

## Set the problem parameters
c = ones(n_d) / sqrt(n_d)
c_max = 5
U = 10000
num_samples = 100 # more should be used (kept small for doc generation)

## Initialize the model
m = InfiniteModel(Clp.Optimizer)
set_silent(m)

## Set the uncertainty parameters
@infinite_parameter(m, θ[i = 1:n_θ] ~ MvNormal(θ_nom, covar),
                    num_supports = num_samples)

## Initialize the variables
@variable(m, 0 <= y <= 1, Infinite(θ))
@variable(m, z[1:n_z], Infinite(θ))
@variable(m, d[1:n_d] >= 0)

## Set objective function
@objective(m, Max, expect(1 - y, θ))

## Set the line capacity constraints
@constraint(m, f1, -z[1] - 35 - d[1] <= y * U)
@constraint(m, f2, z[1] - 35 - d[1] <= y * U)
@constraint(m, f3, -z[2] - 50 - d[2] <= y * U)
@constraint(m, f4, z[1] - 50 - d[2] <= y * U)

## Set the generator capacity constraints
@constraint(m, f5, -z[3] <= y * U)
@constraint(m, f6, z[3] - 100 - d[3] <= y * U)

## Set the node balance constraints
@constraint(m, h1, z[1] - θ[1] == 0)
@constraint(m, h2, -z[1] -z[2] + z[3] - θ[2] == 0)
@constraint(m, h3, z[2] - θ[3] == 0)

## Enforce the minimum SF
@constraint(m, max_cost, sum(c[i] * d[i] for i = 1:n_d) <= c_max)

## Solve and and obtain results
optimize!(m)
if has_values(m)
    opt_y = value(y)
    opt_d = value.(d)
    opt_obj = objective_value(m)
end

## Estimate the value of SF
SF = 1 - sum(opt_y .>= 1e-8) / num_samples

## Print the results
print("------------------RESULTS------------------\n")
print("Optimal Objective:     ", opt_obj, "\n")
print("Optimal Cost:          ", sum(c[i] * opt_d[i] for i = 1:n_d), "\n")
print("Maximum Cost:          ", c_max, "\n")
print("Predicted SF:          ", 100 * SF, "%\n")
print("Optimal Design Values: ", opt_d, "\n\n")

# ### Maintenance Tests
# These are here to ensure this example stays up to date.
using Test
@test termination_status(m) == MOI.OPTIMAL
@test opt_y isa Vector{<:Real}


text:
export get_file_ver, set_file_ver

function set_file_ver(f::String, ver::Float32=vSeisIO)
  isfile(f) || error("File not found!")
  io = open(f, "a+")
  seekstart(io)
  String(read(io, 6)) == "SEISIO" || error("Not a SeisIO file!")
  write(io, ver)
  close(io)
  return nothing
end
set_file_ver(f::String, ver::Float64) = set_file_ver(f, Float32(ver))

function get_file_ver(f::String)
  isfile(f) || error("File not found!")
  io = open(f, "r")
  seekstart(io)
  String(read(io, 6)) == "SEISIO" || error("Not a SeisIO file!")
  ver = read(io, Float32)
  close(io)
  return ver
end


text:
"""
    Triangle{T} <: Surface{T}

Triangular surface, not a valid CSG object.
Primarily used as a component part of [`TriangleMesh`](@ref) or to enable intersection of [`AcceleratedParametricSurface`](@ref)s.
Can never be used directly as an optical surface as it doesn't have an [`OpticalInterface`](@ref).

```julia
Triangle(v1::SVector{3,T}, v2::SVector{3,T}, v3::SVector{3,T}, [uv1::SVector{2,T}, uv2::SVector{2,T}, uv3::SVector{2,T}])
```
"""
struct Triangle{T} <: Surface{T}
    A::SVector{3,T}
    B::SVector{3,T}
    C::SVector{3,T}
    uv1::SVector{2,T}
    uv2::SVector{2,T}
    uv3::SVector{2,T}
    BA::SVector{3,T}
    CA::SVector{3,T}
    normal::SVector{3,T}

    function Triangle(A::SVector{3,T}, B::SVector{3,T}, C::SVector{3,T}, uv1::SVector{2,T}, uv2::SVector{2,T}, uv3::SVector{2,T}) where {T<:Real}
        BA = B - A
        CA = C - A
        crs = cross(BA, CA)
        lcrs = norm(crs)
        # make sure the triangle is valid
        @assert !samepoint(lcrs, zero(T))
        n̂ = crs / lcrs
        return new{T}(A, B, C, uv1, uv2, uv3, BA, CA, n̂)
    end

    function Triangle(v1::AbstractArray{T,1}, v2::AbstractArray{T,1}, v3::AbstractArray{T,1}, uv1::AbstractArray{T,1}, uv2::AbstractArray{T,1}, uv3::AbstractArray{T,1}) where {T<:Real}
        @assert length(v1) == length(v2) == length(v3) == 3
        @assert length(uv1) == length(uv2) == length(uv3) == 2
        return Triangle(SVector{3}(v1), SVector{3}(v2), SVector{3}(v3), SVector{2}(uv1), SVector{2}(uv2), SVector{2}(uv3))
    end

end
export Triangle

Base.show(io::IO, a::Triangle{T}) where {T<:Real} = print(io, "Triangle{$T}($(a.A), $(a.B), $(a.C))")

function Triangle(v1::AbstractArray{T,1}, v2::AbstractArray{T,1}, v3::AbstractArray{T,1}) where {T<:Real}
    return Triangle(v1, v2, v3, zeros(T, 2), zeros(T, 2), zeros(T, 2))
end

function Triangle(v1::SVector{3,T}, v2::SVector{3,T}, v3::SVector{3,T}) where {T<:Real}
    return Triangle(v1, v2, v3, zeros(SVector{2,T}), zeros(SVector{2,T}), zeros(SVector{2,T}))
end

centroid(tri::Triangle{T}) where {T<:Real} = (vertex(tri, 1) + vertex(tri, 2) + vertex(tri, 3)) / 3
validtri(v1::SVector{3,T}, v2::SVector{3,T}, v3::SVector{3,T}) where {T<:Real} = !any(isnan.(v1)) && !any(isnan.(v2)) && !any(isnan.(v3)) && !samepoint(norm(cross(v2 - v1, v3 - v1)), zero(T))
vertex(tri::Triangle{T}, i::Int) where {T<:Real} = i == 1 ? tri.A : i == 2 ? tri.B : i == 3 ? tri.C : throw(ErrorException("Invalid index: $i"))
vertices(tri::Triangle{T}) where {T<:Real} = (tri.A, tri.B, tri.C)
uvvertex(tri::Triangle{T}, i::Int) where {T<:Real} = i == 1 ? tri.uv1 : i == 2 ? tri.uv2 : i == 3 ? tri.uv3 : throw(ErrorException("Invalid index: $i"))
point(tri::Triangle{T}, a1::T, a2::T, a3::T) where {T<:Real} = vertex(tri, 1) * a1 + vertex(tri, 2) * a2 + vertex(tri, 3) * a3
uv(tri::Triangle{T}, a1::T, a2::T, a3::T) where {T<:Real} = uvvertex(tri, 1) * a1 + uvvertex(tri, 2) * a2 + uvvertex(tri, 3) * a3
normal(tri::Triangle{T}) where {T<:Real} = tri.normal

# returns an Interval representing a half-space. If the ray and normal are pointing in opposite directions then the ray is entering the surface and the half-space will be Interval(surface intersection, +inf). If the ray and normal are pointing in the same directions then the ray is leaving the surface and the half-space will be Interval(-inf,surface intersection)
function surfaceintersection(tri::Triangle{T}, r::AbstractRay{T,3}) where {T<:Real}
    # Möller–Trumbore ray-triangle intersection algorithm
    D = direction(r)
    BA = tri.BA
    CA = tri.CA
    h = cross(D, CA)
    a = dot(BA, h)
    if samepoint(a, zero(T))
        return EmptyInterval(T) # This ray is parallel to this triangle
    end
    f = one(T) / a
    s = origin(r) - tri.A
    δ = f * dot(s, h)
    if (δ < zero(T) || δ > one(T))
        return EmptyInterval(T) # outside tri
    end
    q = cross(s, BA)
    β = f * dot(D, q)
    if (β < zero(T) || δ + β > one(T))
        return EmptyInterval(T) # outside tri
    end
    # At this stage we can compute t to find out where the intersection point is on the line.
    t = f * dot(CA, q)
    if (t > zero(T))
        u, v = δ * uvvertex(tri, 2) + β * uvvertex(tri, 3) + (1 - δ - β) * uvvertex(tri, 1)
        # these are the uvcoords want to use as starting point for newton iteration. Don't want the u,v which has already computed for the basis vecors BA,CA.
        n̂ = normal(tri)
        int = Intersection(t, point(r, t), n̂, u, v, NullInterface(T))
        if dot(n̂, D) < zero(T)
            return positivehalfspace(int)
        else
            return rayorigininterval(int)
        end
    else
        return EmptyInterval(T) # wrong side of ray origin
    end
end

function makemesh(t::Triangle{T}, ::Int = 0) where {T<:Real}
    return TriangleMesh([t])
end

##########################################################################################################

"""
    TriangleMesh{T} <: Surface{T}

An array of [`Triangle`](@ref)s forming a mesh.
Used for visualization purposes only.

```julia
TriangleMesh(tris::Vector{Triangle{T}})
```
"""
struct TriangleMesh{T} <: Surface{T}
    # TODO - make this more efficient, e.g. store unique vertices and indices
    triangles::Vector{Triangle{T}}

    function TriangleMesh(tris::Vector{Triangle{T}}) where {T<:Real}
        return new{T}(tris)
    end
end
export TriangleMesh
Base.eltype(::TriangleMesh{T}) where {T<:Real} = Triangle{T}

function makiemesh(tmesh::TriangleMesh{T}) where {T<:Real}
    # TODO probably should unify shared verts at this point or already have this stored in the trimesh
    points = Vector{SVector{3,T}}(undef, length(tmesh.triangles) * 3)
    indices = Array{UInt32,2}(undef, length(tmesh.triangles), 3)
    @inbounds @simd for i in 0:(length(tmesh.triangles) - 1)
        t = tmesh.triangles[i + 1]
        points[i * 3 + 1] = vertex(t, 1)
        points[i * 3 + 2] = vertex(t, 2)
        points[i * 3 + 3] = vertex(t, 3)
        indices[i + 1, :] = [i * 3 + 1, i * 3 + 2, i * 3 + 3]
    end
    return (points, indices)
end


text:
using ReduxImGui
using ReduxImGui.Redux
using ReduxImGui.CImGui

function naive_drags(store::AbstractStore, get_state=Redux.get_state)
    if ReduxImGui.DragInt(store, s->get_state(s).drag_ints["basic_drag_int"])
        @info "This triggers $(@__FILE__):$(@__LINE__)."
        v = ReduxImGui.get_value(get_state(store).drag_ints["basic_drag_int"])
        @info "Current value: $v"
    end
    CImGui.SameLine()
    ShowHelpMarker("Click and drag to edit value.\nHold SHIFT/ALT for faster/slower edit.\nDouble-click or CTRL+click to input value.")

    if ReduxImGui.DragInt(store, s->get_state(s).drag_ints["basic_drag_int2"])
        @info "This triggers $(@__FILE__):$(@__LINE__)."
        v = ReduxImGui.get_value(get_state(store).drag_ints["basic_drag_int2"])
        @info "Current value: $v"
    end

    if ReduxImGui.DragFloat(store, s->get_state(s).drag_floats["basic_drag_float"])
        @info "This triggers $(@__FILE__):$(@__LINE__)."
        v = ReduxImGui.get_value(get_state(store).drag_floats["basic_drag_float"])
        @info "Current value: $v"
    end

    if ReduxImGui.DragFloat(store, s->get_state(s).drag_floats["basic_drag_small_float"])
        @info "This triggers $(@__FILE__):$(@__LINE__)."
        v = ReduxImGui.get_value(get_state(store).drag_floats["basic_drag_small_float"])
        @info "Current value: $v"
    end
end


text:
module TasksController

using Genie.Renderer
using Genie.Router
using SearchLight
using Tasks
using Dates

using ViewHelper

# index page
#   - new_form      ... ajax_create form. The normal submit will be canceled by event.preventDefault().
#   - show_done     ... show/hidden done task.
#   - tasks_tr      ... Tasks List in table > tbody. included Edit and Destroy button.
#   - today         ... Just today.
function index()
    tasks = sort(SearchLight.all(Tasks.Task), by = t -> (t.done, t.deadline))
    tasks_tr = map(task_tr, tasks) |> join
    html!(:tasks, :index, show_done = show_done(), new_form = task_form(""), tasks_tr = tasks_tr, today = Date(now()))
end

function new() # not used
    html!(:tasks, :new)
end

function create() # not used
    Tasks.Task(content = @params(:task_content), done = false, deadline = Date(@params(:task_deadline))) |> save && redirect_to(:get_tasks)
end

# edit page
#   - edit_form     ... Normal edit form.
function edit()
    task = SearchLight.find_one!!(Tasks.Task, @params(:task_id))
    html!(:tasks, :edit, edit_form = task_form(link_to(:update_task), task = task))
end

# update action
function update()
    task = SearchLight.find_one!!(Tasks.Task, @params(:task_id))
    task.content  = @params(:task_content)
    task.deadline = @params(:task_deadline)
    save(task) && redirect_to(:get_tasks)
end

function ajax_create()
    task = Tasks.Task(content = @params(:task_content), done = false, deadline = Date(@params(:task_deadline))) |> save!
    respond(Dict(:html => task_tr(task)))
end

function ajax_update_status()
    task = SearchLight.find_one!!(Tasks.Task, @params(:task_id))
    task.done = @params(:task_done) == "true"
    save(task)
end

function ajax_destroy()
    SearchLight.find_one!!(Tasks.Task, @params(:task_id)) |> delete
    true
end

# ajax show_done changing.
#   just write .show_done file.
#   In julia, true is 1, false is 0.
function ajax_change_show_done()
    open(fp -> write(fp, string(+(@params(:show_done) == "true"))), ".show_done", "w")
end

function show_done()
    isfile(".show_done") || touch(".show_done")
     open(fp -> read(fp, String), ".show_done", "r") == "1"
end

end

max_stars_repo_name: ky-s/MyGenieApp

max_issues_repo_name: ky-s/MyGenieApp

max_forks_repo_name: ky-s/MyGenieApp


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

using Markdown
using InteractiveUtils

# ╔═╡ 539bb62b-e4e3-4d72-837c-302ac9335e2e
begin
	using PlutoUI
	TableOfContents()
end

# ╔═╡ 27130015-4f45-4ebd-9094-07ec00261e5f
md"""
# Useful Packages

Unlike R, Julia does not immediately expose a huge number of functions, but instead requires loading packages (whether from the standard library or from the broader package ecosystem) for a lot of relevant functionality for statistical analysis. There are technical reasons for this, but one further motivation is that Julia is at a broader "technical computing" audience (like MATLAB or perhaps Python) and less at a "statistical analysis" audience.

This has two important implications:
1. Even relatively simple programs will often load several packages.
2. Packages are often focused on adding a relatively narrow set of functionality, which means that "basic" functionality (e.g. reading a CSV file and manipuating it as a DataFrame) is often split across multiple packages. In other words, **see the the first point!**

This notebook is not intended to be an exhaustive list of packages, but rather to highlight a few packages that I suspect will be particularly useful.
Before getting onto the packages, I have one final hint: take advantage of how easy and first-class package management in Julia is. Having good package management makes reproducible analyses much easier and avoids breaking old analyses when you start a new one. Pluto helpfully installs and manages for you, but the package-manager REPL mode (activated by typing `]` at the `julia>` prompt) is very useful.
"""

# ╔═╡ cd73a70f-4ce0-4ef8-87be-d3b319066602
md"""

## Data wrangling

### Reading data

- [Arrow.jl](https://arrow.juliadata.org/dev/manual/) a high performance format for data storage, accessible in R via the [`arrow` package](https://arrow.apache.org/docs/r/) and in Python via `pyarrow`. (Confusingly, the function for reading and writing Arrow format files in R is called `read_feather` and `write_feather`, but the modern Arrow format is distinct from the older Feather format provided by the `feather` package.) This is the format that we store the example and test datasets in for MixedModels.jl.

- [CSV.jl](https://csv.juliadata.org/stable/index.html) useful for reading comma-separated values, tab-separated values and basically everything handled by the `read.csv` and `read.table` family of functions in R.

Note that by default both Arrow.jl and CSV.jl do not return a DataFrame, but rather "column tables" -- named tuples of column vectors.

### DataFrames

Unlike in R, DataFrames are not part of the base language, nor the standard library.

[DataFrames.jl](https://dataframes.juliadata.org/stable/) provides the basic infrastructure around DataFrames, as well as its own [mini language](https://bkamins.github.io/julialang/2020/12/24/minilanguage.html) for doing the split-apply-combine approach that underlies R's `dplyr` and much of the tidyverse.  The DataFrames.jl documentation is the place to for looking at how to e.g. read in a [CSV or Arrow file as a DataFrame](https://dataframes.juliadata.org/stable/man/importing_and_exporting/). Note that DataFrames.jl by default depends on [CategoricalArrays.jl](https://categoricalarrays.juliadata.org/stable/) to handle the equivalent of `factor` in the R world, but there is an alternative package for `factor`-like array type in Julia, [PooledArrays.jl](https://github.com/JuliaData/PooledArrays.jl/). PooledArrays are simpler, but more limited than CategoricalArrays and we (Phillip and Doug) sometimes use them in our examples and simulations.

DataFrame.jl's mini language can be a bit daunting, if you're used to manipulations in the style of base R or the tidyverse. For that, there are several options; recently, we'e had particularly nice experiences with [DataFrameMacros.jl](https://github.com/jkrumbiegel/DataFrameMacros.jl) and [Chain.jl](https://github.com/jkrumbiegel/Chain.jl) for a convenient syntax to connect or "pipe" together successive operations. It's your choice whether and which of these add-ons you want to use! Phillip tends to write his code using raw DataFrames.jl, but Doug really enjoys DataFrameMacros.jl.
"""

# ╔═╡ 9143e623-5457-48b5-9304-ce2a757b18b5
md"""
## Regression

Unlike in R, neither formula processing nor basic regression are part of the base language or the standard library.

The formula syntax and basic contrast-coding schemes in Julia is provided by [StatsModels.jl](https://juliastats.org/StatsModels.jl/v0.6/). By default, MixedModels.jl re-exports the `@formula` macro and most commonly used contrast schemes from StatsModels.jl, so you often don't have to worry about loading StatsModels.jl directly. The same is true for [GLM.jl](https://juliastats.org/GLM.jl/dev/manual/), which provides basic linear and generalized linear models, such as ordinary least squares (OLS) regression and logistic regression, i.e. the classical, non mixed regression models.

The basic functionality looks quite similar to R, e.g.

```julia
julia> lm(@formula(y ~ 1 + x), data)
julia> glm(@formula(y ~ 1 + x), data, Binomial(), LogitLink())
```

but the more general modelling API (also used by MixedModels.jl) is also supported:

```julia
julia> fit(LinearModel, @formula(y ~ 1 + x), mydata)
julia> fit(GeneralizedLinearModel, @formula(y ~ 1 + x), data, Binomial(), LogitLink())
```

(You can also specify your model matrices directly and skip the formula interface, but we don't recommend this as it's easy to mess up in really subtle but very probelmatic ways.)

### `@formula`, macros and domain-specific languages

As a sidebar: why is `@formula` a macro and not a normal function? Well, that's because formulas are essentially their own domain-specific language (a variant of [Wilkinson-Roger notation](https://www.jstor.org/stable/2346786)) and macros are used for manipulating the language itself -- or in this case, handling an entirely new, embedded language! This is also why macros are used by packages like [Turing.jl](https://turing.ml/) and [Soss.jl](https://cscherrer.github.io/Soss.jl/stable/) that define a language for Bayesian probabilistic programming like [PyMC3](https://docs.pymc.io/) or [Stan](https://mc-stan.org/).

### Extensions to the formula syntax

There are several ongoing efforts to extend the formula syntax to include some of the "extras" available in R, e.g. [RegressionFormulae.jl](https://github.com/kleinschmidt/RegressionFormulae.jl) to use the caret (`^`) notation to limit interactions to a certain order (`(a+b+c)^2` generates `a + b + c + a&b + a&c + b&c`, but not `a&b&c`).
Note also that Julia uses `&` to express interactions, not `:` like in R.

### Standardizing Predictors

Although function calls such as `log` can be used within Julia formulae, they must act on a rowwise basis, i.e. on observations. Transformations such as z-scoring or centering (often done with `scale` in R) require knowledge of the entire column. [StandardizedPredictors.jl](https://beacon-biosignals.github.io/StandardizedPredictors.jl/stable/) provides functions for centering, scaling, and z-scoring within the formula. These are treated as pseudo-contrasts and computed on demand, meaning that `predict` and `effects` (see next) computations will handle these transformations on new data (e.g. centering new data *around the mean computed during fitting the original data*) correctly and automatically.

### Effects

John Fox's `effects` package in R (and the related `ggeffects` package for plotting these using `ggplot2`) provides a nice way to visualize a model's overall view of the data. This functionality is provided by [`Effects.jl`](https://beacon-biosignals.github.io/Effects.jl/stable/) and works out-of-the-box with most regression model packages in Julia (including MixedModels.jl). Support for formulae with embedded functions (such as `log`) is not yet complete, but we're working on it!

### Estimated Marginal / Least Square Means

To our knowledge, There is currently nothing like the R package `emmeans` package in Julia. However, it is often better to use sensible, hypothesis-driven contrast coding than to compute all pairwise comparisons after the fact. 😃
"""

# ╔═╡ 32c08c8a-68ec-4bcb-bd4b-3fd76ffca0f0
md"""
## Hypothesis Testing

Classical statistical tests such as the t-test can be found in the package [HypothesisTests.jl](https://github.com/JuliaStats/HypothesisTests.jl/).
"""

# ╔═╡ 4df36c00-be80-4fa5-83ca-c9648352fc42
md"""
## Plotting ecosystem

Throughtout this course, we have used the Makie ecosystem for plotting, but there are several alternatives in Julia.

### Makie

The [Makie ecosystem](https://makie.juliaplots.org/stable/) is a relatively new take on graphics that aims to be both powerful and easy to use. Makie.jl itself only provides abstract definitions for many components (and is used in e.g. MixedModelsMakie.jl to define plot types for MixedModels.jl). The actual plotting and rendering is handled by a backend package such as CairoMakie.jl (good for Pluto notebooks or rending static 2D images) and GLMakie.jl (good for dynamic, interactive visuals and 3D images). AlgebraOfGraphics.jl builds a grammar of graphics upon the Makie framework. It's a great way to get good plots very quickly, but extensive customization is still best achieved by using Makie directly.

### Plots.jl

[Plots.jl](https://docs.juliaplots.org/latest/) is the original plotting package in Julia, but we often find it difficult to work with compared to some of the other alternatives. [StatsPlots.jl](https://github.com/JuliaPlots/StatsPlots.jl) builds on this, adding common statistical plots, while [UnicodePlots.jl](https://github.com/Evizero/UnicodePlots.jl) renders plots as Unicode characters directly in the REPL.

[PGFPlotsX.jl](https://kristofferc.github.io/PGFPlotsX.jl/stable/) is a very new package that writes directly to PGF (the format used by LaTeX's tikz framework) and can stand alone or be used as a rendering backend for the Plots.jl ecosystem.

### Gadfly

[Gadfly.jl](https://gadflyjl.org/stable/) was the original attempt to create a plotting system in Julia based on the grammar of graphics (the "gg" in `ggplot2`). Development has largely stalled, but some functionality still exceeds AlgebraOfGraphics.jl, which has taken up the grammar of graphics mantle. Notably, the MixedModels.jl documentation still uses Gadfly as of this writing (early September 2021).

### Others

There are many [other graphics packages available in Julia](https://juliapackages.com/c/graphics), often wrapping well-established frameworks such as [VegaLite](https://www.queryverse.org/VegaLite.jl/stable/).
"""

# ╔═╡ 919a3d3c-1375-46ab-8507-ae884bb52cfb
md"""
## Connecting to Other Languages

Using Julia doesn't mean you have to leave all the packages you knew in other languages behind. In Julia, it's often possible to even easily and quickly invoke code from other languages *from within Julia*.

[RCall.jl](https://juliainterop.github.io/RCall.jl/stable/gettingstarted/) provides a very convenient interface for interacting with R. [JellyMe4.jl](https://github.com/palday/JellyMe4.jl/) add support for moving MixedModels.jl and `lme4` models back and forth between the languages (which means that you can use `emmeans`, `sjtools`, `DHARMa`, `car`, etc. to examine MixedModels.jl models!). [RData.jl](https://github.com/JuliaData/RData.jl) provides support for reading `.rds` and `.rda` files from Julia, while [RDatasets.jl](https://github.com/JuliaStats/RDatasets.jl) provides convenient access to many of the standard datasets provided by R and various R packages.

[PyCall.jl](https://github.com/JuliaPy/PyCall.jl/) provides a very convenient way for interacting with Python code and packages. [PyPlot.jl](https://github.com/JuliaPy/PyPlot.jl) builds upon this foundation to provide support for Python's `matplotlib`. Similarly, [PyMNE.jl](
https://github.com/beacon-biosignals/PyMNE.jl) and [PyFOOOF.jl](https://github.com/beacon-biosignals/pyfooof.jl) provide some additional functionality to make interacting with MNE-Python and FOOOF from within Julia even easier than with vanilla PyCall.

For MATLAB users, there is also [MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl)

[Cxx.jl](https://juliainterop.github.io/Cxx.jl/stable/) provides interoperability with C++. It also provides a C++ REPL mode, making it possible to treating C++ much more like a dynamic language than the traditional compiler toolchain would allow.

Support for calling C and Fortran is [part of the Julia standard library](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/).
"""

# ╔═╡ 35c0e59d-23ce-4b60-b80c-e619a50f990d
md"""

## Notebooks and Webpages

We haved [Pluto.jl](https://github.com/fonsp/Pluto.jl/) throughout this course because it provides a nice, easy way to interact with Julia, including automatic package management. [PlutoUI.jl](https://github.com/fonsp/PlutoUI.jl) provides some nice tools for interacting with Pluto notebooks and making them more dynamic (including the `TableOfContents()` in this one!). We also like that Pluto notebooks are essentially normal Julia code stored in plain text under the hood, which means that they play quite nice with version control systems such as Git. Sometimes, however, the reactive nature of Pluto (update any one cell and the changes propogate to all impacted cells) is not desireable.

An alternative notebook interface is provide by [Jupyter](https://jupyter.org/), formerly IPython. Indeed, the "Ju" actually stands for Julia. Jupyter support is provided by the package [IJulia.jl](https://julialang.github.io/IJulia.jl/stable/).

The [Weave.jl](https://weavejl.mpastell.com/stable/) package provides "Julia markdown", which is similar to knitr/RMarkdown or the the Sweave formats in R. Notably, Weave also provides support for converting between `jmd` files and Jupyter notebooks.

Similarly, [Literate.jl](https://fredrikekre.github.io/Literate.jl/v2/) is a simple package for literate programming (i.e. programming where documentation and code are "woven" together) and can generate Markdown, plain code and Jupyter notebook output. It is designed for simplicity and speed and is less extensive than Weave.jl and Documenter.jl.

[Documenter.jl](https://juliadocs.github.io/Documenter.jl/stable/) is the standard tool for building webpages from Julia documentation

[Books.jl](https://rikhuijzer.github.io/Books.jl/) is a package designed to offer somewhat similar functionality to the `bookdown` package in R.
"""

# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"

[compat]
PlutoUI = "~0.7.9"
"""

# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[JSON]]
deps = ["Dates", "Mmap", "Parsers", "Unicode"]
git-tree-sha1 = "8076680b162ada2a031f707ac7b4953e30667a37"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.2"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[Parsers]]
deps = ["Dates"]
git-tree-sha1 = "438d35d2d95ae2c5e8780b330592b6de8494e779"
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
version = "2.0.3"

[[PlutoUI]]
deps = ["Base64", "Dates", "InteractiveUtils", "JSON", "Logging", "Markdown", "Random", "Reexport", "Suppressor"]
git-tree-sha1 = "44e225d5837e2a2345e69a1d1e01ac2443ff9fcb"
uuid = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
version = "0.7.9"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[Reexport]]
git-tree-sha1 = "45e428421666073eab6f2da5c9d310d99bb12f9b"
uuid = "189a3867-3050-52da-a836-e630ba90ab69"
version = "1.2.2"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[Suppressor]]
git-tree-sha1 = "a819d77f31f83e5792a76081eee1ea6342ab8787"
uuid = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
version = "0.2.0"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
"""

# ╔═╡ Cell order:
# ╟─27130015-4f45-4ebd-9094-07ec00261e5f
# ╟─cd73a70f-4ce0-4ef8-87be-d3b319066602
# ╟─9143e623-5457-48b5-9304-ce2a757b18b5
# ╟─32c08c8a-68ec-4bcb-bd4b-3fd76ffca0f0
# ╟─4df36c00-be80-4fa5-83ca-c9648352fc42
# ╟─919a3d3c-1375-46ab-8507-ae884bb52cfb
# ╟─35c0e59d-23ce-4b60-b80c-e619a50f990d
# ╠═539bb62b-e4e3-4d72-837c-302ac9335e2e
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002


text:
using Documenter
using AtomicLevels

makedocs(
    modules = [AtomicLevels],
    sitename = "AtomicLevels",
    pages = [
        "Home" => "index.md",
        "Orbitals" => "orbitals.md",
        "Configurations" => "configurations.md",
        "CSFs" => "csfs.md",
        "Other utilities" => "utilities.md",
        "Internals" => "internals.md",
    ],
)

deploydocs(repo = "github.com/JuliaAtoms/AtomicLevels.jl.git")


text:
"""
	readggp(filein;what,offset,nanval)
Load ggp file and store it in DataFrame

**Input**
* filein: full name of the input file
* what: what should be loaded. By default "data", Other options="header" | "blocks"
* offset: Should the offset in block info by applied? Default = false. Use only in combination with `what="data"`
* nanval: flag used for NaNs (NaN will be returned)

**Output**
Depends on `what` switch
* "data": dataframe containing columns + timevector in DateTime format (of what="data")
* "header": string header
* "blocks": DataFrame with starting time and offset for each column

**Example**
```
data = readggp("../test/input/ggp_data.ggp",nanval=99999.999);
blockinfo = readggp("../test/input/ggp_data.tsf",what="blocks");
headerinfo = readggp("../test/input/ggp_data.tsf",what="header");
```
"""
function readggp(filein::String;what::String="data",offset::Bool=false,
					nanval::Float64=9999.999)
	# get block info
	blockinfo = readggpblocks(filein);
	# Return either header or block info if requested
	if what == "header"
		return readggphead(filein);
	elseif what == "blocks"
		return blockinfo;
	else # Otherwise read data
		return readggpdata(filein,blockinfo,nanval,offset=offset)
	end
end

"""
Auxiliary function to read data in ggp file
"""
function readggpdata(filein::String,blockinfo::DataFrame,nanval;offset::Bool=false)
	# Declare output variable size
	columns = size(blockinfo,2)-1;
	lines = sum((blockinfo[:stopline].+1)-blockinfo[:startline]);
	outmat = Matrix{Float64}(undef,lines,columns);
	# Read file
	open(filein,"r") do fid
		readdummy(fid,blockinfo[:startline][1]-1); # read header
		c = 1; # count outmat/outtime lines
		# Read blocks
		for i in 1:size(blockinfo,1)
			for j in 1:1:(blockinfo[:stopline][i]-blockinfo[:startline][i])+1
				outmat[c,1:columns] = readdataline(fid);
				c += 1;
			end
			if i != size(blockinfo,1) # read lines between blocks (except for last block)
				readdummy(fid,blockinfo[:startline][i+1]-blockinfo[:stopline][i]-1);
			end
	    end
	end
	# replace flagged values
	outmat[outmat.==nanval] .= NaN;
	outframe = DataFrame(datetime=sumtime.(outmat[:,1],outmat[:,2]));
	# Correct offsets if needed
	if offset
		for i in 1:size(blockinfo,1),  j in 3:columns
			if blockinfo[j+1][i] != 0.0
				outmat[outframe[:datetime] .>= blockinfo[:datetime][i],j] .+= blockinfo[j+1][i];
			end
		end
	end
	return append2df(outframe,outmat[:,3:end],"column");
end

"""
Auxiliary function to read block headers
"""
function readggpblocks(filein::String)
	blockoffset,blockdate = Vector{String}(),Vector{String}();
	blockstart,blockstop = Vector{Int}(),Vector{Int}();
	open(filein,"r") do fid
		i = 0; # count lines to get block start and stop
		while !eof(fid)
			row = readline(fid); i += 1;
			if occursin("77777777",row)
				# check if the data is in correct format
				if length(row)> 8
					if row[9:15] != "       "
						row = row[1:8]*"       "*row[16:end];
					end
				end
				push!(blockoffset,row);
				row = readline(fid);i += 1; # read next line containing date
				push!(blockdate,row);
				push!(blockstart,i);
			elseif occursin("99999999",row) # end of block
				push!(blockstop,i-1);
			end
		end
	end
	return block2output(blockoffset,blockdate,blockstart,blockstop)
end

"""
Auxiliary function to read just the GGP file header
"""
function readggphead(filein::String)
	head = Vector{String}();
	open(filein,"r") do fid
		row = readline(fid);
		while !occursin("C**",row)
			push!(head,row);
			row = readline(fid);
		end
	end
	return head
end

"""
Auxiliary function to convert string vectors containing info on offset to dataframe
"""
function block2output(offset::Vector{String},timestr::Vector{String},
					blockstart::Vector{Int},blockstop::Vector{Int})
	# get number of columns
	numchan = splitline(timestr[1]) |> length |> x-> x-2;
	# Declare output variable (or rather variables used for output)
	outtime = Vector{DateTime}();
	outoffset = Matrix{Float64}(undef,0,numchan);
	for (i,v) in enumerate(timestr)
		push!(outtime,sumtime(splitline(v)[1],splitline(v)[2]));
		if length(split(offset[i])) > 1 # this means the line contains also offset info
			temp = splitline(offset[i]);
		else # line without offset => add 0.0 offset for consistancy
			temp = vcat(77777777,zeros(numchan)) # append zeros
		end
		outoffset = vcat(outoffset,transpose(temp[2:end]));
	end
	out = DataFrame(datetime=outtime,startline=blockstart,stopline=blockstop);
	return append2df(out,outoffset,"offset_column");
end

"""
Auxiliary function to convert time pattern (HHMMSS) to DateTime
"""
function append2df(indf::DataFrame,appmat::Matrix{Float64},appstring::String)
	for i in 1:size(appmat,2)
		indf[Symbol(appstring,i)] = appmat[:,i];
	end
	return indf
end

"""
Time in ggp files is splitted in to rows, one with YYYYMMDD second with HHMMSS
These need to be converted to DateTime
"""
function sumtime(indate::Float64,intime::Float64)
	function splittime(time)
		time1::Int = floor(time/10000.);
		time2::Int = floor((time - time1*10000.)/100.);
		time3::Int = floor(time - time1*10000. - time2*100.);
		return time1,time2,time3
	end
	year,month,day = splittime(indate);
	hour,minute,second = splittime(intime);
	return DateTime(year,month,day,hour,minute,second);
end

"""
Auxiliary function to read data lines in input file (stream)
"""
function readdataline(fid::IOStream)
	temp = readline(fid);
	i::Int = 15;
	out = split(temp[1:i]); # fix position
	while i < length(temp)-1
		push!(out,temp[i+1:i+10])# 10 strings per data-column
		i += 10;
	end
	return Base.parse.(Float64,out)
end
"""
Auxiliary function to prepare string for data extraction
"""
function splitline(str::String)
	return replace(str,r"\s\s+"=>" ") |> split |> x -> Base.parse.(Float64,x)
end
"""
Auxiliary function to read dummy lines in input file (stream)
"""
function readdummy(fid::IOStream,ntime::Int)
	for i in 1:ntime
		readline(fid);
	end
end

"""
	writeggp(datawrite,fileout,units,decimal,header)
Write DataFrame to ggp format

**Input**
* datawrite: dataframe containing columns + timevector in DateTime format
* fileout: full name of the output file
* units: optional output units for all columns
* decimal: optional output precision for either all or individual columns. Maximum precision are 8 decimal places!
* header: optional output containing header lines
* channels: DataFrame keys to be exported
* file_format: "preterna" (=ggp=default) or "eterna" file format
* blockinfo: info on data blocks (see example below)
* flagval: string used as flag for missing data

**Example**
```
datawrite = DataFrame(pres=collect(1000.:1:1011.),
					grav=collect(900.:-3:(900.-11*3)),
       				datetime=collect(DateTime(2010,1,1):Dates.Hour(1):DateTime(2010,1,1,11)));
# set units using the same keywords as in the dataframe
units = Dict(:grav=>"V",:pres=>"hPa")
# Set header. All entries with the excetpion of "freetext" will be formatted
header = Dict("Filename"=>"file.data",
              "Station"=>"Wettzell",
              "Instrument"=>"iGrav",
              "N. Latitude (deg)"=>49.14354,
              "E. Longitude (deg)"=>12.87866,
              "Elevation MSL (m)"=>613.7+1.05,
              "Author"=>"Name (name@gfz-potsdam.de)",
			  "freetext"=>"This text will be appended without formating\nMaximum 80 characters per line");
fileout = "f:\\mikolaj\\download\\test.ggp";
# Set block info:
#	"start" = starting time of the block
#	"offset" = offset for each block and channel
#   "header" = Insert text between blocks (e.g. for Eterna34-V60)
# 				Warning: the first entry will be inserted at the
#               beginning of the file and the second to the start of the
#               first block! => set length(blocks)+1 headers!
#               Or, setting cell with one row = same block header for
#               all block start. Do not set if needed.
#				Example for ET34-ANA-V60 (same for all blocks):
#               ["instrument",calibation,calibration SD, lag ,Number of Chebychev polynomial bias parameter];
block = Dict("start"=>[DateTime(2010,1,1,09,0,0)],
			"offset"=>[10 20], # one row per block. 2 values per row = number channels
            "header"=>["iGrav006" 1.0 1.0 0.0 3])
writeggp(datawrite,fileout,units=units,
			header=header,decimal=[2,1],
			blockinfo=block,channels=[:grav,:pres]);
```
"""
function writeggp(datawrite::DataFrame,fileout::String;
					units=[],decimal=[2],channels=[],header=[],
					file_format="preterna",blockinfo=[],
					flagval="9999.999")
	# Get channel names if not set
	if isempty(channels)
		channels,timei = findchannels(datawrite);
	else
		temp,timei = findchannels(datawrite);
	end
	# check decimal precision
	if length(decimal) != length(channels)
		decimal = zeros(Int,length(channels)) + decimal[1];
	end
	# find data blocks
	bstart,bstop,bvalue = writeggp_findblocks(datawrite,channels,blockinfo);
	# write to file
	open(fileout,"w") do fid
		writeggp_head(fid,header,channels,units);
		for i in 1:length(bstart)
			# write block header if needed
			if !isempty(blockinfo)
				if haskey(blockinfo,"header")
					# either identical header for each block (temp=1), or each block new header (temp=i)
					temp = size(blockinfo["header"],1) == 1 ? 1 : i
					@printf(fid,"%-10s%15.4f%10.4f%10.3f%10i\n",
	                    	blockinfo["header"][temp,1],blockinfo["header"][temp,2],
							blockinfo["header"][temp,3],blockinfo["header"][temp,4],
	                    	blockinfo["header"][temp,5]);
				end
			end
			# write data for each block
			writeggp_writeblock(fid,datawrite,channels,timei,bstart[i],bstop[i],
								bvalue[i,:],decimal,flagval);
		end
	    # Add 88888888 for eterna format only
	    if file_format=="eterna"
	         @printf(fid,"88888888\n");
	    end
	end
end

"""
auxiliary function to write the header
"""
function writeggp_head(fid,header,channels,units)
	if !isempty(header)
		for i in header
			if !occursin("freetext",i[1])
	   			@printf(fid,"%-21s: %s\n",i[1],i[2])
			end
		end
		if haskey(header,"freetext")
			@printf(fid,"%s\n",header["freetext"])
		end
	end
	@printf(fid,"yyyymmdd hhmmss");
	for i in 1:length(channels)
		if isempty(units)
			temp = "?"
		else
			temp = units[channels[i]];
		end
		@printf(fid," %s(%s)",channels[i],temp);
	end
	@printf(fid,"\nC*******************************************************\n");
end

"""
auxiliary function to find data blocks
"""
function writeggp_findblocks(datain,channels,blockinfo)
	# Find blocks in time/data
	bstart = [1];
	bstop = Vector{Int64}();
	bvalue = zeros(1,length(channels));
	if !isempty(blockinfo)
		for i in 1:length(blockinfo["start"])
			temp = findall(x -> x >= blockinfo["start"][i],datain[:datetime]);
			if !isempty(temp)
				if temp[1] != 1
					bstart = vcat(bstart,temp[1]);
					bstop = vcat(bstop,temp[1]-1);
					bvalue = vcat(bvalue,blockinfo["offset"][i,:]');
				end
			end
		end
		bstop = vcat(bstop,size(datain,1));
	else
		bstop = size(datain,1);
	end
	return bstart,bstop,bvalue
end

"""
Auxiliary function to write data blocks
"""
function writeggp_writeblock(fid::IOStream,datawrite::DataFrame,
							channels::Vector{Symbol},timei::Symbol,
							s1::Int,s2::Int,bvalue,
							decimal::Vector{Int},
							flagval::String)
	# beginning of the block
	@printf(fid,"77777777       ");
	for (c,j) in enumerate(channels)
		writewithprecision(fid,bvalue[c],decimal[c]);
	end
	@printf(fid,"\n");
	for i in s1:s2
		# write date+time
		@printf(fid,"%04i%02i%02i %02i%02i%02i",
				Dates.year(datawrite[timei][i]),
				Dates.month(datawrite[timei][i]),
				Dates.day(datawrite[timei][i]),
				Dates.hour(datawrite[timei][i]),
				Dates.minute(datawrite[timei][i]),
				Dates.second(datawrite[timei][i]));
		# write data
		for (c,j) in enumerate(channels)
			if ismissing(datawrite[j][i]) || isnan(datawrite[j][i])
				@printf(fid,"%10s",flagval);
			else
				writewithprecision(fid,datawrite[j][i],decimal[c]);
			end
		end
		@printf(fid,"\n");
	end
    # End block
    @printf(fid,"99999999\n");
end

function writewithprecision(fid::IOStream,d::Float64,decimal::Int)
	if decimal == 0
		@printf(fid,"%10.0f",d);
	elseif decimal == 1
		@printf(fid,"%10.1f",d);
	elseif decimal == 2
		@printf(fid,"%10.2f",d);
	elseif decimal == 3
		@printf(fid,"%10.3f",d);
	elseif decimal == 4
		@printf(fid,"%10.4f",d);
	elseif decimal == 5
		@printf(fid,"%10.5f",d);
	elseif decimal == 6
		@printf(fid,"%10.6f",d);
	elseif decimal == 7
		@printf(fid,"%10.7f",d);
	elseif decimal == 8
		@printf(fid,"%10.8f",d);
	end
end

"""
	ggpdata2blocks(datawrite;channels)
Get block info removing all NaNs (split into blocks)

**Input**
* datawrite: dataframe containing columns + timevector in DateTime format
* channels: DataFrame keys to be exported

**Output**
corrected DataFrame and block info needed for writeggp function

**Example**
```
datawrite = DataFrame(pres=collect(1000.:1:1011.),
				grav=collect(900.:-3:(900.-11*3)),
   				datetime=collect(DateTime(2010,1,1):Dates.Hour(1):DateTime(2010,1,1,11)));
datawrite[:grav][[3,6]] = NaN;
datawrite[:pres][7] = NaN;
dataout,block = ggpdata2blocks(datawrite;channels=[])
```
"""
function ggpdata2blocks(datawrite::DataFrame;channels=[])
	# use all channels/columns if not set
	if isempty(channels)
		channels,timei = FileTools.findchannels(datawrite);
	else
		timei = :datetime;
	end
	remrows = ggpdata2blocks_unique(datawrite,channels)
	dataout = datawrite[vcat(timei,channels)]
	if !isempty(remrows)
		dataout = deleterows!(dataout,remrows);
		# get blocks
		if length(remrows)>1
			remrows = vcat(remrows[1],remrows[findall(x->x.>1,diff(remrows)).+1])
		end
		block = Dict("start"=>datawrite[timei][remrows],
					 "offset"=>zeros(length(remrows),length(channels)),
					 "header"=>hcat("INSTR",ones(length(channels))',zeros(2)'));
	else
		block = [];
	end
	return dataout,block
end

"""
Auxiliary funciton to find rows with NaNs
"""
function ggpdata2blocks_unique(datawrite,channels)
	# find channels containing NaN
	remrows = Vector{Int}();
	for i in channels
		remrows = vcat(remrows,findall(x->isnan(x),datawrite[i]))
	end
	return sort(unique(remrows))
end


text:
@testset "Relative ideals" begin
  @testset "Arithmetic" begin
     Qx, x = FlintQQ["x"]
    f = x^2 + 12*x - 92
     K, a = NumberField(f, "a")
    OK = maximal_order(K)
    Ky, y = K["y"]
    g = y^2 - 54*y - 73
    L, b = number_field(g, "b")
    OL = maximal_order(L)

    coeff_ideals = [ deepcopy(Hecke.pseudo_basis(OL, copy = false)[i][2]) for i = 1:2 ]
    PM = Hecke.PseudoMatrix(matrix(K, [1 0; 0 1]), coeff_ideals)
    PM1 = pseudo_hnf(Hecke.PseudoMatrix(matrix(K, [3 0; 0 3]), coeff_ideals), :lowerleft, true)
    PM2 = pseudo_hnf(Hecke.PseudoMatrix(matrix(K, [6 0; 0 6]), coeff_ideals), :lowerleft, true)
    PM3 = pseudo_hnf(Hecke.PseudoMatrix(matrix(K, [9 0; 0 9]), coeff_ideals), :lowerleft, true)
    I = ideal(OL, PM)
    I1 = frac_ideal(OL, PM)
    A = ideal(OL, PM1)
    A1 = frac_ideal(OL, PM1)
    B = ideal(OL, PM2)
    C = ideal(OL, PM3)
    C1 = frac_ideal(OL, PM3)

    @test A == B + C
    @test B == intersect(A, B)
    @test K(2)*C == A*B
    @test inv(C)*C1 == I1
    @test norm(A) == OK(9)*OK
    @test norm(I) == OK(1)*OK
    D = divexact(C, B)
    D = fmpz(2)*D
    @test D == A1
    @test isone(I)
    @test minimum(A) == numerator(PM1.coeffs[1])
  end

  @testset "Prime decomposition" begin
     Qx, x = FlintQQ["x"]
    f = x^2 + 12*x - 92
     K, a = NumberField(f, "a")
    OK = maximal_order(K)
    Ky, y = K["y"]
    g = y^2 - 54*y - 73
    L, b = number_field(g, "b")
    OL = maximal_order(L)

    p = prime_decomposition(OK, 11)[1][1]
    (p1, e1), (p2, e2) = prime_decomposition(OL, p)
    @test e1 == 1 && e2 == 1
    @test p1*p2 == p*OL

    p = prime_decomposition(OK, 2)[1][1]
    (p1, e1), (p2, e2) = prime_decomposition(OL, p)
    @test e1 == 1 && e2 == 1
    @test p1*p2 == p*OL

    Q, q = number_field(x, "q")
    Z = maximal_order(Q)
    Qy, y = Q["y"]
    g =  y^3 + 34*y^2 + 45*y + 98
    p = prime_decomposition(Z, 11)[1][1]
    L, b = number_field(g, "b")
    OL = maximal_order(L)
    (p1, e1), (p2, e2) = prime_decomposition(OL, p)
    @test e1 == 1 && e2 == 1
    @test p1*p2 == p*OL
    @test p1.splitting_type[2] == 2 || p2.splitting_type[2] == 2

    g = y^4 + 56*y^3 + 27*y^2 + -10*y + 56
    p = prime_decomposition(Z, 2)[1][1]
    L, b = number_field(g, "b")
    OL = maximal_order(L)
    (p1, e1), (p2, e2), (p3, e3) = prime_decomposition(OL, p)
    @test p1^e1*p2^e2*p3^e3 == p*OL
    @test p1.splitting_type[2] == 1 && p2.splitting_type[2] == 1 && p3.splitting_type[2] == 1
  end

  @testset "Residue fields" begin
     Qx, x = FlintQQ["x"]
    f = x^4 - 95x^3 - 91x^2 + 90x - 31
     K, a = NumberField(f, "a")
    OK = maximal_order(K)
    Ky, y = K["y"]
    g = y^3 - 70y^2 + 27y + 97
    L, b = NumberField(g, "b")
    OL = maximal_order(L)

    # p is not a index divisor
    p = prime_decomposition(OK, 13)[1][1]
    P = prime_decomposition(OL, p)[1][1]
    F, mF = ResidueField(OL, P)
    @test degree(F) == p.splitting_type[2]*P.splitting_type[2]

    pb = pseudo_basis(P, copy = false)
    for i = 1:degree(OL)
      b = OL(minimum(pb[i][2])*pb[i][1])
      @test iszero(mF(b))
    end

    for i = 1:1
      c = rand(OL, 100)
      d = rand(OL, 100)
      pinv = pseudo_inv(mF)
      @test mod(pinv(mF(c)), P) == mod(c, P)
      @test mod(pinv(mF(d)), P) == mod(d, P)
      @test mF(c + d) == mF(c) + mF(d)
      @test mF(c*d) == mF(c)*mF(d)
      e = mF(c)
      f = mF(d)
      @test mod(pinv(e + f), P) == mod(pinv(e) + pinv(f), P)
      @test mod(pinv(e*f), P) == mod(pinv(e)*pinv(f), P)
    end

    # p is a index divisor
    p = prime_decomposition(OK, 5)[1][1]
    PP = prime_decomposition(OL, p)
    P = PP[1][1]
    if P.splitting_type[2] == 1
      P = PP[2][1]
    end
    F, mF = ResidueField(OL, P)
    @test degree(F) == p.splitting_type[2]*P.splitting_type[2]

    pb = pseudo_basis(P, copy = false)
    for i = 1:degree(OL)
      b = OL(minimum(pb[i][2])*pb[i][1])
      @test iszero(mF(b))
    end

    for i = 1:1
      c = rand(OL, 100)
      d = rand(OL, 100)
      pinv = pseudo_inv(mF)
      @test mod(pinv(mF(c)), P) == mod(c, P)
      @test mod(pinv(mF(d)), P) == mod(d, P)
      @test mF(c + d) == mF(c) + mF(d)
      @test mF(c*d) == mF(c)*mF(d)
      e = mF(c)
      f = mF(d)
      @test mod(pinv(e + f), P) == mod(pinv(e) + pinv(f), P)
      @test mod(pinv(e*f), P) == mod(pinv(e)*pinv(f), P)
    end

  end

  @testset "Idempotents and uniformizers" begin
     Qx, x = FlintQQ["x"]
    f = x^2 + 12*x - 92
     K, a = NumberField(f, "a")
    OK = maximal_order(K)
    Ky, y = K["y"]
    g = y^2 - 54*y - 73
    L, b = number_field(g, "b")
    OL = maximal_order(L)

    I = OL(2)*OL
    J = OL(3)*OL
    u, v = idempotents(I, J)
    @test u in I
    @test v in J
    @test u + v == OL(1)

    p = prime_decomposition(OK, 11)[1][1]
    (p1, e1), (p2, e2) = prime_decomposition(OL, p)

    u, v = idempotents(p1, p2)
    @test u in p1
    @test v in p2
    @test u + v == OL(1)

    # p1.splitting_type[1] == 1
    u1 = uniformizer(p1)
    @test u1 in p1
    @test valuation(u1, p1) == 1

    u2 = anti_uniformizer(p1)
    @test valuation(u2, p1) == -1

    p = prime_decomposition(OK, 401)[1][1]
    P = prime_decomposition(OL, p)[1][1]

    # P.splitting_type[1] == 2
    u3 = uniformizer(P)
    @test u3 in P
    @test valuation(u3, P) == 1

    u4 = anti_uniformizer(P)
    @test valuation(u4, P) == -1

    Q, q = NumberField(x, "q")
    Z = maximal_order(Q)
    Qy, y = Q["y"]
    f = y^2 + 12*y - 92
     K, a = NumberField(f, "a")
    OK = maximal_order(K)

    p = prime_decomposition(Z, 2)[1][1]
    P = prime_decomposition(OK, p)[1][1]

    u5 = uniformizer(P)
    @test u5 in P
    @test valuation(u5, P) == 1

    u6 = anti_uniformizer(P)
    @test valuation(u6, P) == -1
  end
end


text:
module MenuBars

Base.Experimental.@optlevel 2

using Redux
using CImGui
using ..Menus
import ..Menus: AbstractMenuAction

## actions
"""
    AbstractGenericMenuBarAction <: AbstractSyncAction
Abstract supertype for all kinds of menu bar actions. Literally, its subtype could be anything.
"""
abstract type AbstractGenericMenuBarAction <: AbstractSyncAction end

"""
    AbstractMenuBarAction <: AbstractGenericMenuBarAction
Abstract supertype for all menu bar actions. Any subtypes of this type should has a
`label` field or at least `x.label` should work.
"""
abstract type AbstractMenuBarAction <: AbstractGenericMenuBarAction end

# for determining whether we should apply the reducer function to certain action types.
struct Valid end
struct Invalid end

is_valid(a::AbstractSyncAction) = Invalid
is_valid(a::AbstractGenericMenuBarAction) = Invalid
is_valid(a::AbstractMenuBarAction) = Valid

_get_label(a::AbstractSyncAction, ::Type{Invalid}) = "___REDUXIMGUI_RESERVED_DUMMY_ACTION_LABEL"
_get_label(a::AbstractSyncAction, ::Type{Valid}) = a.label
get_label(a::AbstractSyncAction) = _get_label(a, is_valid(a))

"""
    Rename(label, new_label)
Change widget's label to `new_label`.
Note that renaming may also change the identifier, please refer to `help?> CImGui.PushID`.
"""
struct Rename <: AbstractMenuBarAction
    label::String
    new_label::String
end

"""
    SetTriggeredTo(label, is_triggered)
Update widget's `is_triggered` state.
"""
struct SetTriggeredTo <: AbstractMenuBarAction
    label::String
    is_triggered::Bool
end

"""
    EditMenus(label, action::AbstractMenuAction)
Edit manus.
"""
struct EditMenus{T<:AbstractMenuAction} <: AbstractMenuBarAction
    label::String
    action::T
end

"""
    Show(label)
Show the menubar.
"""
struct Show <: AbstractMenuBarAction
    label::String
end

"""
    Hide(label)
Hide the menubar.
"""
struct Hide <: AbstractMenuBarAction
    label::String
end

## state
"""
    AbstractGenericMenuBar <: AbstractImmutableState
Abstract supertype for all kinds of menu bars. Literally, its subtype could be anything.
"""
abstract type AbstractGenericMenuBar <: AbstractImmutableState end

"""
    AbstractMenuBar <: AbstractGenericMenuBar
Abstract supertype for all menu bars. Any subtypes of this type should has a
`label` field or at least `x.label` should work.
"""
abstract type AbstractMenuBar <: AbstractGenericMenuBar end

is_valid(s::AbstractImmutableState) = Invalid
is_valid(s::AbstractGenericMenuBar) = Invalid
is_valid(s::AbstractMenuBar) = Valid

_get_label(s::AbstractImmutableState, ::Type{Invalid}) = "___REDUXIMGUI_RESERVED_DUMMY_STATE_LABEL"
_get_label(s::AbstractImmutableState, ::Type{Valid}) = s.label
get_label(s::AbstractImmutableState) = _get_label(s, is_valid(s))

"""
    MenuBar(label::AbstractString, menus = [], is_hidden = false)
A menu bar state which contains a label a.k.a the identifier, a list of menus,
a flag value `is_hidden` and a flag value `is_triggered` that records the state of
the latest poll events.
"""
struct MenuBar <: AbstractMenuBar
    label::String
    menus::Vector{Menus.Menu}
    is_hidden::Bool
    is_triggered::Bool
end
MenuBar(label::AbstractString, menus = [], is_hidden = false) =
    MenuBar(label, menus, is_hidden, false)

is_hidden(s::MenuBar) = s.is_hidden
is_triggered(s::MenuBar) = s.is_triggered

# reducers
reducer(state::AbstractState, action::AbstractAction) = state
reducer(state::Vector{<:AbstractState}, action::AbstractAction) = state
reducer(state::Dict{String,<:AbstractState}, action::AbstractAction) = state

reducer(s::MenuBar, a::Rename) = MenuBar(a.new_label, s.menus, s.is_hidden, s.is_triggered)
reducer(s::MenuBar, a::SetTriggeredTo) = MenuBar(s.label, s.menus, s.is_hidden, a.is_triggered)
reducer(s::MenuBar, a::Show) = MenuBar(s.label, s.menus, false, s.is_triggered)
reducer(s::MenuBar, a::Hide) = MenuBar(s.label, s.menus, true, s.is_triggered)

reducer(s::MenuBar, a::EditMenus) =
    MenuBar(s.label, Menus.reducer(s.menus, a.action), s.is_hidden, s.is_triggered)

## UI
"""
    (::MenuBar)(store::AbstractStore, get_state=Redux.get_state, chain_action=identity) -> Bool
Return `true` when `ImGuiWindowFlags_MenuBar` is setted on the parent window.
`get_state` is a router function that tells how to find the target state from `store`.
`chain_action` is for chaining upstream actions.
"""
function MenuBar(store::AbstractStore, get_state=Redux.get_state, chain_action=identity)
    s = get_state(store)
    is_activated = CImGui.BeginMenuBar()
    dispatch!(
        store,
        SetTriggeredTo(s.label, is_activated) |> chain_action
    )
    if is_activated && !s.is_hidden
        for (i, menu) in enumerate(s.menus)
            menu(
                store,
                x->get_state(x).menus[i],
                x->EditMenus(s.label, x) |> chain_action,
            )
        end
        CImGui.EndMenuBar()
    end
    return is_activated
end

end # module


text:
# A 3D point in Cartesian coordinates.

struct Point_3D{T <: AbstractFloat}
    x::SVector{3,T}
end

# Constructors
# -------------------------------------------------------------------------------------------------
# 3D single constructor
Point_3D(x::T, y::T, z::T) where {T <: AbstractFloat} = Point_3D(SVector(x,y,z))
# 2D single constructor
Point_3D(x::T, y::T) where {T <: AbstractFloat} = Point_3D(SVector(x, y, T(0)))
# 1D single constructor
Point_3D(x::T) where {T <: AbstractFloat} = Point_3D(SVector(x, T(0), T(0)))
# 3D tuple constructor
Point_3D(x::Tuple{T,T,T}) where {T <: AbstractFloat} = Point_3D(SVector(x))
# 2D tuple constructor
Point_3D((x, y)::Tuple{T,T}) where {T <: AbstractFloat} = Point_3D(SVector(x, y, T(0)))
# 3D single conversion constructor
Point_3D(type::Type{T}, x::X, y::Y, z::Z) where {T <: AbstractFloat,
                                                 X <: Real,
                                                 Y <: Real,
                                                 Z <: Real} = Point_3D(SVector(T(x),T(y),T(z)))
# 2D single conversion constructor
Point_3D(type::Type{T}, x::X, y::Y) where {T <: AbstractFloat,
                                           X <: Real,
                                           Y <: Real} = Point_3D(SVector(T(x),T(y),T(0)))
# 1D single conversion constructor
Point_3D(type::Type{T}, x::X) where {T <: AbstractFloat,
                                     X <: Real} = Point_3D(SVector(T(x),T(0),T(0)))

# Base
# -------------------------------------------------------------------------------------------------
Base.broadcastable(p⃗::Point_3D) = Ref(p⃗)
Base.zero(::Point_3D{T}) where {T <: AbstractFloat} = Point_3D((T(0), T(0), T(0)))
Base.firstindex(::Point_3D) = 1
Base.lastindex(::Point_3D) = 3
Base.getindex(p⃗::Point_3D, i::Int) = p⃗.x[i]
(::Type{T})(p⃗::Point_3D) where {T <: AbstractFloat} = Point_3D(T.(p⃗.x))

# Operators
# -------------------------------------------------------------------------------------------------
==(p⃗₁::Point_3D, p⃗₂::Point_3D) = (p⃗₁.x == p⃗₂.x)
function ≈(p⃗₁::Point_3D{T}, p⃗₂::Point_3D{T}) where {T <: AbstractFloat}
    return distance(p⃗₁, p⃗₂) < 5e-6
end
+(p⃗₁::Point_3D, p⃗₂::Point_3D) = Point_3D(p⃗₁.x[1] + p⃗₂.x[1],
                                         p⃗₁.x[2] + p⃗₂.x[2],
                                         p⃗₁.x[3] + p⃗₂.x[3]
                                        )
-(p⃗₁::Point_3D, p⃗₂::Point_3D) = Point_3D(p⃗₁.x[1] - p⃗₂.x[1],
                                         p⃗₁.x[2] - p⃗₂.x[2],
                                         p⃗₁.x[3] - p⃗₂.x[3]
                                        )
×(p⃗₁::Point_3D, p⃗₂::Point_3D) = Point_3D(p⃗₁.x[2]*p⃗₂.x[3] - p⃗₂.x[2]*p⃗₁.x[3],
                                         p⃗₁.x[3]*p⃗₂.x[1] - p⃗₂.x[3]*p⃗₁.x[1],
                                         p⃗₁.x[1]*p⃗₂.x[2] - p⃗₂.x[1]*p⃗₁.x[2],
                                )

⋅(p⃗₁::Point_3D, p⃗₂::Point_3D) = p⃗₁.x[1]*p⃗₂.x[1] +
                                p⃗₁.x[2]*p⃗₂.x[2] +
                                p⃗₁.x[3]*p⃗₂.x[3]

+(p⃗::Point_3D, n::Real) = Point_3D(p⃗.x[1] + n,
                                   p⃗.x[2] + n,
                                   p⃗.x[3] + n
                                  )
+(n::Real,  p⃗::Point_3D) = p⃗ + n
-(p⃗::Point_3D, n::Real) = Point_3D(p⃗.x[1] - n,
                                   p⃗.x[2] - n,
                                   p⃗.x[3] - n
                                  )
-(n::Real,  p⃗::Point_3D) = p⃗ - n
*(n::Real,  p⃗::Point_3D) = Point_3D(p⃗.x[1] * n,
                                    p⃗.x[2] * n,
                                    p⃗.x[3] * n
                                   )
*(p⃗::Point_3D, n::Real) = n*p⃗
/(p⃗::Point_3D, n::Real) = Point_3D(p⃗.x[1] / n,
                                   p⃗.x[2] / n,
                                   p⃗.x[3] / n
                                  )
-(p⃗::Point_3D) = -1*p⃗
# 3 x 3 matrix multiplication, returns a point
*(A::SMatrix{3, 3, T, 9}, p⃗::Point_3D{T}) where {T <: AbstractFloat} = Point_3D(A * p⃗.x)

# Methods
# -------------------------------------------------------------------------------------------------
norm(p⃗::Point_3D) = sqrt(p⃗ ⋅ p⃗)
distance(p⃗₁::Point_3D, p⃗₂::Point_3D) = norm(p⃗₁ - p⃗₂)

max_stars_repo_name: KyleVaughn/MOCNeutronTransport

max_issues_repo_name: KyleVaughn/MOCNeutronTransport

max_forks_repo_name: KyleVaughn/MOCNeutronTransport


text:
@test array_matches(binedges(DiscretizeUniformCount(2), [1.0,2.0,3.0,4.0,5.0,6.0]), [1.0, 3.5, 6.0], 1e-8)
@test array_matches(binedges(DiscretizeUniformCount(2), [6.0,2.0,30.0,4.0,1.0,1.0]), [1.0, 3.0, 30.0], 1e-8)
@test array_matches(binedges(DiscretizeUniformCount(2), [1.0,2.0,3.0,4.0,5.0]), [1.0, 3.5, 5.0], 1e-8)
@test array_matches(binedges(DiscretizeUniformCount(3), [1.0,2.0,3.0,4.0,5.0]), [1.0, 2.5, 4.5, 5.0], 1e-8)
@test_throws ErrorException binedges(DiscretizeUniformCount(3), [1.0, 1.0])

@test binedges(DiscretizeUniformCount(2), [1,2]) == [1, 2, 2]
@test binedges(DiscretizeUniformCount(3), [1,2,2,2,3]) == [1, 2, 3, 3]
@test binedges(DiscretizeUniformCount(3), [1,2,2,2,50]) == [1, 2, 26, 50]
@test binedges(DiscretizeUniformCount(3), [1,2,2,2,51]) == [1, 2, 27, 51]
@test binedges(DiscretizeUniformCount(3), [1,1,1,2,3,4,50,51,52]) == [1, 2, 27, 52]


text:
#=
This file is auto-generated. Do not edit.
=#
"""
    mutable struct PSSFixed <: PSS
        V_pss::Float64
        ext::Dict{String, Any}
        states::Vector{Symbol}
        n_states::Int64
        internal::InfrastructureSystemsInternal
    end

Parameters of a PSS that returns a fixed voltage to add to the reference for the AVR

# Arguments
- `V_pss::Float64`: Fixed voltage stabilization signal, validation range: (0, nothing)
- `ext::Dict{String, Any}`
- `states::Vector{Symbol}`
- `n_states::Int64`
- `internal::InfrastructureSystemsInternal`: power system internal reference, do not modify
"""
mutable struct PSSFixed <: PSS
    "Fixed voltage stabilization signal"
    V_pss::Float64
    ext::Dict{String, Any}
    states::Vector{Symbol}
    n_states::Int64
    "power system internal reference, do not modify"
    internal::InfrastructureSystemsInternal
end

function PSSFixed(V_pss, ext=Dict{String, Any}(), )
    PSSFixed(V_pss, ext, Vector{Symbol}(), 0, InfrastructureSystemsInternal(), )
end

function PSSFixed(; V_pss, ext=Dict{String, Any}(), )
    PSSFixed(V_pss, ext, )
end

# Constructor for demo purposes; non-functional.
function PSSFixed(::Nothing)
    PSSFixed(;
        V_pss=0,
        ext=Dict{String, Any}(),
    )
end

"""Get PSSFixed V_pss."""
get_V_pss(value::PSSFixed) = value.V_pss
"""Get PSSFixed ext."""
get_ext(value::PSSFixed) = value.ext
"""Get PSSFixed states."""
get_states(value::PSSFixed) = value.states
"""Get PSSFixed n_states."""
get_n_states(value::PSSFixed) = value.n_states
"""Get PSSFixed internal."""
get_internal(value::PSSFixed) = value.internal

"""Set PSSFixed V_pss."""
set_V_pss!(value::PSSFixed, val::Float64) = value.V_pss = val
"""Set PSSFixed ext."""
set_ext!(value::PSSFixed, val::Dict{String, Any}) = value.ext = val
"""Set PSSFixed states."""
set_states!(value::PSSFixed, val::Vector{Symbol}) = value.states = val
"""Set PSSFixed n_states."""
set_n_states!(value::PSSFixed, val::Int64) = value.n_states = val
"""Set PSSFixed internal."""
set_internal!(value::PSSFixed, val::InfrastructureSystemsInternal) = value.internal = val


text:
struct composite{T<:Array{Float64}, S<:Float64}
    a::T
    b::S
end

struct composite2{T<:Array{Float64}, S<:Int64}
    a::T
    b::S
end

obj = composite([1.0;2;3], 5.6)
obj2 = composite2([1.0;3], 5)
function gg(x, y::Union{Int64, Float64, Nothing}=nothing)
    if y==nothing
        return x^2
    else
        return x
end
end

max_stars_repo_name: dbindel/btg

max_issues_repo_name: dbindel/btg

max_forks_repo_name: dbindel/btg


text:
using Gtk, GtkUtilities

 function randbw(ctx, w, h)
    pic = zeros(Int64, w, h)
    for i in 1:length(pic)
        pic[i] = rand([1, 0])
    end
    copy!(ctx, pic)
end

const can = @GtkCanvas()
const win = GtkWindow(can, "Image Noise", 320, 240)

@guarded draw(can) do widget
    ctx = getgc(can)
    h = height(can)
    w = width(can)
    randbw(ctx, w, h)
end

show(can)
const cond = Condition()
endit(w) = notify(cond)
signal_connect(endit, win, :destroy)

while true
    frames = 0
    t = time()
    for _ in 1:100
        draw(can)
        show(can)
        sleep(0.0001)
        frames += 1
    end
    fps = round(frames / (time() - t), digits=1)
    set_gtk_property!(win, :title, "Image Noise: $fps fps")
end

wait(cond)

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
function cp_files_and_directories(
        src::AbstractString,
        dst::AbstractString;
        overwrite::Bool,
        )::Nothing
    if isdir(src)
        # @debug("src is dir", src, isdir(src))
        mkpath(dst)
        for item in readdir(src)
            # @debug("item: ", item)
            item_src_path = joinpath(src, item)
            item_dst_path = joinpath(dst, item)
            cp_files_and_directories(
                item_src_path,
                item_dst_path;
                overwrite = overwrite,
                )
        end
    elseif isfile(src)
        # @debug("src is file", src, isfile(src))
        # @debug("dst: ", dst, isfile(dst), ispath(dst))
        if overwrite || !ispath(dst)
            rm(dst; force = true, recursive = true,)
            cp(src, dst)
            # @debug("copied file", src, dst)
        end
    else
        # @error("weird src", src)
    end
    return nothing
end

function cache_to_path!(
        ;
        from::AbstractVector{<:AbstractString},
        to::AbstractVector{<:AbstractString},
        cache = joinpath(homedir(), "predictmd_cache_travis"),
        )::Nothing
    cache_path_src::String = joinpath(cache, from...)
    path_dst::String = joinpath(to...)
    mkpath(cache_path_src)
    mkpath(path_dst)
    @debug("cache_path_src: ", cache_path_src,)
    @debug("path_dst: ", path_dst,)
    cp_files_and_directories(
        cache_path_src,
        path_dst;
        overwrite = false,
        )
    return nothing
end

function path_to_cache!(
        ;
        from::AbstractVector{<:AbstractString},
        to::AbstractVector{<:AbstractString},
        cache = joinpath(homedir(), "predictmd_cache_travis"),
        )::Nothing
    path_src::String = joinpath(from...)
    cache_path_dst::String = joinpath(cache, to...)
    mkpath(path_src)
    mkpath(cache_path_dst)
    @debug("path_src: ", path_src,)
    @debug("cache_path_dst: ", cache_path_dst,)
    cp_files_and_directories(
        path_src,
        cache_path_dst;
        overwrite = true,
        )
    return nothing
end


text:
using Parameters

abstract type meteomatics_url end

"""
Data type for containing required & optional parts of the request URL for data queries
"""
@with_kw struct data_url<:meteomatics_url
    validdatetime::String
    parameters::String
    location::String
    format::String = "csv"
    opts::Dict
end

"""
Data type for containing required & optional parts of the request URL for meta queries
"""
@with_kw struct meta_url<:meteomatics_url
    metaquery::String
    model::String
    parameters::String
    validdatetime::Union{String, Nothing} = nothing
    # there are no 'opts' for a meta_url in the current implementations
end

max_stars_repo_name: meteomatics/julia-connector-api

max_issues_repo_name: meteomatics/julia-connector-api

max_forks_repo_name: meteomatics/julia-connector-api