text:
@testset "setup" begin
    @testset "simple random example" begin
        experiment = Dict{Symbol, Any}(
            :hash => 0,
            :data_file => TEST_DATA_FILE,
            :output_file => TEST_OUTPUT_FILE,
            :model => Dict(:type => :(SVDD.RandomOCClassifier),
                           :param => Dict{Symbol, Any}(),
                           :init_strategy => SVDD.FixedParameterInitialization(GaussianKernel(2), 0.5)),
            :split_strategy => OneClassActiveLearning.DataSplits(trues(TEST_DATA_NUM_OBSERVATIONS) , OneClassActiveLearning.FullSplitStrat()),
            :param => Dict(:num_al_iterations => 5,
                           :solver => TEST_SOLVER,
                           :initial_pools => fill(:U, TEST_DATA_NUM_OBSERVATIONS),
                           :adjust_K => true))

        @testset "pool based single query" begin
            exp =  deepcopy(experiment)
            exp[:query_strategy] = Dict(:type => :(OneClassActiveLearning.QueryStrategies.RandomPQs),
                                        :param => Dict{Symbol, Any}())
            exp[:oracle] = Dict(:type => :PoolOracle,
                                :param => Dict{Symbol, Any}())

            expected_experiment = deepcopy(exp)

            res = OneClassActiveLearning.active_learn(exp)

            @test res.status[:exit_code] == :success
            @test length(res.al_history, :query_history) == 5
            @test length(res.al_history, :angle_batch_diversity) == 5
            @test length(res.al_history, :euclidean_batch_diversity) == 5
            @test all(values(res.al_history, :angle_batch_diversity) .≈ 0.0)
            @test all(values(res.al_history, :euclidean_batch_diversity) .≈ 0.0)
            @test length(res.al_history, :time_qs) == 5
            @test all(values(res.al_history, :time_qs) .> 0.0)

            @test length(res.al_history, :time_fit) == 6
            @test all(values(res.al_history, :time_fit) .> 0.0)

            @test length(res.al_history, :query_history) == 5
            @test !isempty(res.worker_info)

            @test length(res.al_history, :cm) == 6
            @test OneClassActiveLearning.cohens_kappa(last(res.al_history[:cm])[2]) ≈ last(res.al_history[:cohens_kappa])[2]

            @test res.experiment[:param][:initial_pools] == expected_experiment[:param][:initial_pools]
        end

        @testset "pool based batch query" begin
            exp =  deepcopy(experiment)
            exp[:query_strategy] = Dict(:type => :(OneClassActiveLearning.QueryStrategies.RandomBatchQs),
                                        :param => Dict{Symbol, Any}(:k => 5))
            exp[:oracle] = Dict(:type => :PoolOracle,
                                :param => Dict{Symbol, Any}())

            expected_experiment = deepcopy(exp)

            res = OneClassActiveLearning.active_learn(exp)

            @test res.status[:exit_code] == :success
            @test length(res.al_history, :query_history) == 5
            @test all(length.(values(res.al_history, :query_history)) .== 5)
            @test length(res.al_history, :angle_batch_diversity) == 5
            @test length(res.al_history, :euclidean_batch_diversity) == 5
            @test all(.~(values(res.al_history, :euclidean_batch_diversity) .≈ 0.0))
            @test length(res.al_history, :time_qs) == 5
            @test all(values(res.al_history, :time_qs) .> 0.0)

            @test length(res.al_history, :time_fit) == 6
            @test all(values(res.al_history, :time_fit) .> 0.0)

            @test length(res.al_history, :query_history) == 5
            @test !isempty(res.worker_info)

            @test length(res.al_history, :cm) == 6
            @test OneClassActiveLearning.cohens_kappa(last(res.al_history[:cm])[2]) ≈ last(res.al_history[:cohens_kappa])[2]

            @test res.experiment[:param][:initial_pools] == expected_experiment[:param][:initial_pools]
        end

        @testset "query synthesis" begin
            exp =  deepcopy(experiment)
            exp[:query_strategy] = Dict(:type => :(OneClassActiveLearning.QueryStrategies.RandomQss),
                                        :param => Dict{Symbol, Any}())
            exp[:oracle] = OneClassActiveLearning.QuerySynthesisFunctionOracle(x -> fill(:inlier, size(x, 2)))

            expected_experiment = deepcopy(exp)

            res = OneClassActiveLearning.active_learn(exp)

            @test length(res.experiment[:split_strategy].train) == TEST_DATA_NUM_OBSERVATIONS + 5
            @test length(res.experiment[:split_strategy].test) == TEST_DATA_NUM_OBSERVATIONS + 5

            @test res.status[:exit_code] == :success
            @test length(res.al_history, :query_history) == 5
            @test length(res.al_history, :angle_batch_diversity) == 5
            @test length(res.al_history, :euclidean_batch_diversity) == 5
            @test all(values(res.al_history, :angle_batch_diversity) .≈ 0.0)
            @test all(values(res.al_history, :euclidean_batch_diversity) .≈ 0.0)
            @test length(res.al_history, :time_qs) == 5
            @test all(values(res.al_history, :time_qs) .> 0.0)

            @test length(res.al_history, :time_fit) == 6
            @test all(values(res.al_history, :time_fit) .> 0.0)

            @test length(values(res.al_history, :query_history)) == 5
            @test size(values(res.al_history, :query_history)[1]) == (TEST_DATA_NUM_DIMENSIONS, 1)
            @test !isempty(res.worker_info)

            @test length(res.al_history, :cm) == 6
            @test OneClassActiveLearning.cohens_kappa(last(res.al_history[:cm])[2]) ≈ last(res.al_history[:cohens_kappa])[2]

            @test res.experiment[:param][:initial_pools] == expected_experiment[:param][:initial_pools]
        end
    end

    @testset "process queries" begin
        @testset "pool based" begin
            qs = OneClassActiveLearning.QueryStrategies.RandomPQs()
            data = rand(2, 4)
            split_strategy = OneClassActiveLearning.DataSplits(trues(4))
            pools = [:U, :U, :Lin, :U]
            labels = [:inlier, :outlier, :inlier, :inlier]
            data, pools, labels = OneClassActiveLearning.process_queries!([1], labels[[1]], SVDD.RandomOCClassifier(data), split_strategy, data, pools, labels)
            @test pools == [:Lin, :U, :Lin, :U]
            data, pools, labels = OneClassActiveLearning.process_queries!([2,4], labels[[2,4]], SVDD.RandomOCClassifier(data), split_strategy, data, pools, labels)
            @test pools == [:Lin, :Lout, :Lin, :Lin]
            @test labels == [:inlier, :outlier, :inlier, :inlier]
        end

        @testset "query synthesis" begin
            qs = OneClassActiveLearning.QueryStrategies.RandomQss()
            data = rand(2, 3)
            split_strategy = OneClassActiveLearning.DataSplits(trues(3))
            pools = [:U, :U, :Lin]
            labels = [:inlier, :outlier, :inlier]
            data, pools, labels = OneClassActiveLearning.process_queries!(rand(2,1), [:inlier], SVDD.RandomOCClassifier(data), split_strategy, data, pools, labels)
            @test size(data) == (2, 4)
            @test labels == [:inlier, :outlier, :inlier, :inlier]
            @test pools == [:U, :U, :Lin, :Lin]
            @test split_strategy.train == [true, true, true, true]
            @test split_strategy.test == [true, true, true, false]
            data, pools, labels = OneClassActiveLearning.process_queries!(rand(2,3), fill(:outlier, 3), SVDD.RandomOCClassifier(data), split_strategy, data, pools, labels)
            @test size(data) == (2, 7)
            @test labels == [:inlier, :outlier, :inlier, :inlier, :outlier, :outlier, :outlier]
            @test pools == [:U, :U, :Lin, :Lin, :Lout, :Lout, :Lout]
            @test split_strategy.train == [true, true, true, true, true, true, true]
            @test split_strategy.test == [true, true, true, false, false, false, false]
        end
    end
end


text:
using NLPModels
using CUTEst
using MiniLogging
include("Armijo.jl")
include("LinesearchCUTEst.jl")
include("CGlin.jl")
include("CRlin.jl")
basic_config(MiniLogging.INFO; date_format="%Y-%m-%d %H:%M:%S")

loggerCRlin = get_logger("CRlin")
loggerCGlin = get_logger("CGlin")
loggerArm = get_logger("Armijo")
loggerLinCUTEst = get_logger("LinesearchCUTEst")

num = parse(Int64,ARGS[1])
Problems = open(readlines, "Problems.txt")
# Problems = deleteat!(Problems,56) # pb "INDEF" not treated
Algos = [CGlin, CRlin]

D = ["model     algo nvar   f(x)    f(x0)   ‖g(x)‖  ‖g(x0)‖   #f  #g #Hv  #it optimal"]

problem = Problems[num]
println(problem)
model = CUTEstModel(problem)
for algo in Algos
    L = LinesearchCUTEst(model, algo)
    S = @sprintf("%5s %5s %4d %5s %5s %5s %5s %4d %4d %4d %4d %5s", L[1], L[2], L[3], L[4], L[5], L[6], L[7], L[8], L[9], L[10], L[11], L[12])
    D = vcat(D, S)
    reset!(model)
end
finalize(model)
writedlm(string(problem, "_CUTEst.txt"), D)

ext: jl


text:
using SafeTestsets
@time @safetestset "API Tests" begin include("api.jl") end
@time @safetestset "Nested Differentiation Tests" begin include("nested.jl") end
@time @safetestset "Dual Tests" begin include("dualtest.jl") end


text:
# # Solvers for ionosphere reflection coefficient
#
# LongwaveModePropagator uses the technique presented by
# [Budden (1955)](https://doi.org/10.1098/rspa.1955.0027) to compute the reflection
# coefficient of a horiontally stratified ionosphere consisting of an
# anisotropic, collisional plasma.
#
# Budden (1955) derives a differential equation for the reflection coefficient
# ``\bm{R}``:
# ```math
# \frac{2i}{k}\frac{\mathrm{d}\bm{R}}{\mathrm{d}z} = W_{21} + W_{22}\bm{R} - \bm{R}W_{11} - \bm{R}W_{12}\bm{R}
# ```
# where ``W`` is a ``4\times 4`` matrix divided into four ``2\times 2`` submatrices
# each containing components of the ``\bm{T}`` matrix.
#
# To obtain the reflection coefficient ``\bm{R}`` at any level, we integrate
# ``\mathrm{d}\bm{R}/\mathrm{d}z`` downward through the ionosphere from a great height
# and the height at which we stop is ``\bm{R}`` for a sharp boundary at that level with
# free space below.
#
# The reflection coefficient matrix ``\bm{R}`` consists of four complex reflection
# coefficients for the different combinations of incident and reflected wave
# polarization. These reflection coefficients are also functions of the wave
# angle of incidence, and therefore the integration of ``\mathrm{d}\bm{R}/\mathrm{d}z``
# occurs tens of thousands to hundreds of thousands of times for every
# `HomogeneousWaveguide` in the GRPF mode finder. It is therefore extremely important
# that the differential equations solver be as efficient as possible to minimize
# runtime.
# The mode finder is responsible for more than 90% of the runtime of
# LongwaveModePropagator, and most of the mode finder runtime is the ionosphere
# reflection coefficient integration.
#
# In this example, we will compare solvers and tolerances from
# DifferentialEquations to determine the most efficient combination with reasonable
# robustness and accuracy.
# We will begin by looking at the reflection coefficients as a function of height.
#
# First we load the packages we need.

using Statistics
using Plots
using OrdinaryDiffEq
using Interpolations

using LongwaveModePropagator
using LongwaveModePropagator: StaticArrays, QE, ME
const LMP = LongwaveModePropagator
nothing  #hide

# ## R(z)
#
# Let's examine the four reflection coefficients for the ionosphere as a function
# of height.
#
# We will use a typical daytime ionosphere with a Wait and Spies (1964) profile
# and a wave frequency of 24 kHz.
# The background magnetic field will be vertical with a magnitude of 50,000 nT.
# We also need to specify the wave angle of incidence on the ionosphere.
# We'll use a real ``\theta = 75°``.

species = Species(QE, ME, z->waitprofile(z, 75, 0.32), electroncollisionfrequency)
frequency = Frequency(24e3)
bfield = BField(50e-6, π/2, 0)
ea = EigenAngle(deg2rad(75));

# We start the integration at a "great height".
# For longwaves, anything above the D-region is fine.
# LongwaveModePropagator defaults to a `topheight` of 110 km.

topheight = 110e3
Mtop = LMP.susceptibility(topheight, frequency, bfield, species)
Rtop = LMP.bookerreflection(ea, Mtop)

# The starting solution `Rtop` comes from a solution of the Booker quartic for the
# wavefields at `topheight`.
#
# We use OrdinaryDiffEq.jl to integrate [`LongwaveModePropagator.dRdz`](@ref).
# Although it's not needed for computing the ionosphere reflection coefficient,
# `dRdz` takes a `ModeEquation` argument, specifying a complete waveguide,
# including `Ground`, for convenience.

ground = GROUND[1]
waveguide = HomogeneousWaveguide(bfield, species, ground)
me = PhysicalModeEquation(ea, frequency, waveguide);

# Then we simply define the `ODEProblem` and `solve`.

prob = ODEProblem{false}(LMP.dRdz, Rtop, (topheight, 0.0), (me, LMPParams()))

sol = solve(prob, RK4(); abstol=1e-9, reltol=1e-9);

# Let's plot the reflection coefficients next to the electron density and collision
# frequency curves.
# Because the reflection coefficients are complex-valued, we will plot their magnitude.

zs = topheight:-1000:0

ne = species.numberdensity.(zs)
nu = species.collisionfrequency.(zs)
Wr = LMP.waitsparameter.(zs, (frequency,), (bfield,), (species,))

altinterp = LinearInterpolation(reverse(Wr), reverse(zs))
eqz = altinterp(frequency.ω)  # altitude where ω = ωᵣ

ne[end] = NaN  # otherwise Plots errors
Wr[end] = NaN

p1 = plot([ne nu Wr], zs/1000;
          xlims=(10, 10^10), xaxis=(scale=:log10),
          ylabel="Altitude (km)",
          labels=["Nₑ (m⁻³)" "ν (s⁻¹)" "ωᵣ = ωₚ²/ν"], legend=:topleft,
          linewidth=1.5);

vline!(p1, [frequency.ω]; linestyle=:dash, color="gray", label="");
hline!(p1, [eqz/1000]; linestyle=:dash, color="gray", label="");
annotate!(p1, frequency.ω, 10, text(" ω", :left, 9));
annotate!(p1, 70, eqz/1000-3, text("ωᵣ = ω", :left, 9));

R11 = abs.(sol(zs; idxs=1))
R21 = abs.(sol(zs; idxs=2))
R12 = abs.(sol(zs; idxs=3))
R22 = abs.(sol(zs; idxs=4))

p2 = plot([R11 R21 R12 R22], zs/1000;
          xlims=(0, 1),
          yaxis=false, yformatter=_->"",
          legend=:right, labels=["R₁₁" "R₂₁" "R₁₂" "R₂₂"],
          linewidth=1.5);

hline!(p2, [eqz/1000]; linestyle=:dash, color="gray", label="");

plot(p1, p2; layout=(1,2), size=(800, 400))
#md savefig("integratedreflection_xyz.png"); nothing # hide
#md # ![](integratedreflection_xyz.png)

#

# ## Generate random scenarios
#
# Now that we've seen what the reflection coefficient functions look like,
# we'll focus on finding an accurate but efficient solver.
#
# We will evaluate the solutions across a range of different random ionospheres,
# frequencies, and angles of incidence.
# Each scenario is described by a [`PhysicalModeEquation`](@ref).

function generatescenarios(N)
    eas = EigenAngle.(complex.(rand(N)*(π/2-π/6) .+ π/6, rand(N)*deg2rad(-10)))
    frequencies = Frequency.(rand(N)*50e3 .+ 10e3)

    B = rand(30e-6:5e-7:60e-6, N)
    ## avoiding within 1° from 0° dip angle
    bfields = BField.(B, rand(N)*(π/2-0.018) .+ 0.018, rand(N)*2π)

    hps = rand(N)*20 .+ 69
    betas = rand(N)*0.8 .+ 0.2

    scenarios = Vector{PhysicalModeEquation}(undef, N)
    for i = 1:N
        species = Species(QE, ME, z->waitprofile(z, hps[i], betas[i]),
                          electroncollisionfrequency)
        ground = GROUND[5]  ## not used in integration of R
        waveguide = HomogeneousWaveguide(bfields[i], species, ground)

        me = PhysicalModeEquation(eas[i], frequencies[i], waveguide)
        scenarios[i] = me
    end

    return scenarios
end

scenarios = generatescenarios(30);

# ## Reference solutions
#
# To evaluate the accuracy of the reflection coefficients, we compare to a very
# low tolerance Runge-Kutta Order 4 method. The DifferentialEquations implementation
# of `RK4` uses adaptive stepping.
#
# The [`LongwaveModePropagator.integratedreflection`](@ref) function does the
# integration process above for us
# and returns the reflection coefficient at the ground.

ip = IntegrationParams(tolerance=1e-14, solver=RK4(), maxiters=1_000_000)
params = LMPParams(integrationparams=ip)

Rrefs = [LMP.integratedreflection(scenario; params=params) for scenario in scenarios];

# ## Evaluate solvers
#
# Now let's compute and time the results a set of different methods for a range
# of tolerances.
# We repeat the integration `N = 25` times for each combination of parameters
# to get a more accurate average time.

function compute(scenarios, tolerances, solvers)
    dims = length(scenarios), length(tolerances), length(solvers)
    Rs = Array{StaticArrays.SMatrix{2,2,ComplexF64,4}}(undef, dims...)
    times = Array{Float64}(undef, dims...)

    for k in eachindex(solvers)
        for j in eachindex(tolerances)
            ip = IntegrationParams(tolerance=tolerances[j], solver=solvers[k])
            params = LMPParams(integrationparams=ip)

            for i in eachindex(scenarios)
                ## warmup
                R = LMP.integratedreflection(scenarios[i]; params=params)

                ## loop for average time
                N = 25
                t0 = time_ns()
                for n = 1:N
                    R = LMP.integratedreflection(scenarios[i]; params=params)
                end
                ttotal = time_ns() - t0

                Rs[i,j,k] = R
                times[i,j,k] = ttotal/N
            end
        end
    end
    return Rs, times
end

tolerances = [1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9, 1e-10]
tolerancestrings = string.(tolerances)

solvers = [RK4(), Tsit5(), BS5(), OwrenZen5(), Vern6(), Vern7(), Vern8()]
solverstrings = replace.(string.(solvers), "OrdinaryDiffEq."=>"")

Rs, times = compute(scenarios, tolerances, solvers);

# We'll measure the error in the reflection coefficient matrices
# as the maximum absolute difference of the four elements of the matrix
# compared to the reference reflection coefficient matrix.

function differr(a, ref)
    differences = a .- ref
    aerror = similar(a, Float64)
    for i in eachindex(differences)
        absdiff = abs.(differences[i])
        aerror[i] = maximum(absdiff)
    end
    return aerror
end

Rerrs = differr(Rs, Rrefs)
mean_Rerrs = dropdims(mean(Rerrs; dims=1); dims=1)

heatmap(tolerancestrings, solverstrings, permutedims(log10.(mean_Rerrs));
        clims=(-9, -2),
        xlabel="tolerance", ylabel="solver",
        colorbar_title="log₁₀ max abs difference", colorbar=true)
#md savefig("integratedreflection_difference.png"); nothing # hide
#md # ![](integratedreflection_difference.png)

# And the average runtimes are

mean_times = dropdims(mean(times; dims=1); dims=1)

heatmap(tolerancestrings, solverstrings, permutedims(mean_times)/1e6;
        clims=(0, 5),
        xlabel="tolerance", ylabel="solver",
        colorbar_title="time (μs)", colorbar=true)
#md savefig("integratedreflection_time.png"); nothing # hide
#md # ![](integratedreflection_time.png)

# The best accuracy occurs with `Vern7` and experiment's we've done looking at the
# sensitivity of the mode solutions to integration tolerance have shown we can get away
# with a tolerance of `1e-4`. To play it safe, the default used by LMP is `1e-5`.
# The integration accuracy improves considerably for relatively little additional
# computation time if the tolerance is changed to `1e-9`, but that accuracy is not
# required.


text:
using Distributed
addprocs(30; exeflags="--project")

@everywhere begin

using TicketToRide
using Flux
using Flux.Optimise
using TicketToRide: train!
using LuxurySparse
using LinearAlgebra

function task(n, nlayers; verbose=false, nprune=10, nepochs=10, niteration=100, relative_error=true, least_prune=10)
    opt = Optimise.ADAMW()
    circuit = variational_circuit(n, nlayers);
    ham = heisenberg1D(n);
    dispatch!(circuit, :random);
    circuit, history = prune_train(opt, circuit, ham;
        nprune=nprune, nepochs=nepochs,
        niteration=niteration,
        relative_error=relative_error,
        least_prune=least_prune, cnot_prune=false);

    return circuit, history
end

function run_task(total, n, nlayers; verbose=false, nprune=10, nepochs=10, niteration=100, relative_error=true, least_prune=10)
    results = pmap(1:total) do k
        task(n, nlayers;
        nprune=nprune, nepochs=nepochs,
        niteration=niteration,
        relative_error=relative_error,
        least_prune=least_prune)
    end

    min_energy = 0
    circuit, history = nothing, nothing
    for each in results
        if each[2][end] < min_energy
            circuit = each[1]
            history = each[2]
        end
    end
    return circuit, history
end

end

using JLD2
c, h = run_task(30, 10, 100; nepochs=20, nprune=50, niteration=1000, least_prune=5)

jldopen("data-10-30.jld", "w+") do f
    f["circuit"] = c
    f["history"] = h
end


text:
# nCLD.jl
#
#   Functions to parse errors, status values, and warnings from the nCLD
#
# Jeremy Rüffer
# Thünen Institut
# Institut für Agrarklimaschutz
# Junior Research Group NITROSPHERE
# Julia 1.5.0
# 03.02.2020
# Last Edit: 03.08.2020

#include("/home/jeremy/BF-Eddy/Code/Julia/Jeremy/nCLD.jl")

mutable struct nCLD_Status
	# Byte 1
	ScrubberHeating::Bool
	ReactorChamberHeating::Bool
	AdditionalConvHeating::Bool
	HotTubingHeating::Bool
	PeltierCooling::Bool
	#not_used::Bool
	#set::Bool
	#reserved::Bool

	# Byte 2
	OzoneGenerator::Bool
	CalValve::Bool
	VacuumPump::Bool
	BypassPressureRegulator::Bool
	Recorder::Bool
	AUXDevice::Bool
	#set::Bool
	#reserved::Bool

	# Byte 3
	RemoteMode::Bool
	#reserved::Bool
	PowerUpPhase::Bool
	CalibrationActive::Bool
	StandbyOperation::Bool
	#not_used::Bool
	#set::Bool
	#reserved::Bool

	# Constructor for nCLD_Status type
	function nCLD_Status(status::Float64)
		nCLD_Status(UInt(status))
	end # End nCLD_Status(status::Float64)
	function nCLD_Status(status::Int64)
		nCLD_Status(UInt(status))
	end # End nCLD_Status(status::Int64)

	# Constructor for nCLD_Status type
	function nCLD_Status(status::UInt)
		# Byte 1
		ScrubberHeating = status & 2^(1-1) > 0
		ReactorChamberHeating = status & 2^(2-1) > 0
		AdditionalConvHeating = status & 2^(3-1) > 0
		HotTubingHeating = status & 2^(4-1) > 0
		PeltierCooling = status & 2^(5-1) > 0
		#not_used = status & 2^(6-1) > 0
		#set = status & 2^(7-1) > 0
		#reserved = status & 2^(8-1) > 0

		# Byte 1
		OzoneGenerator = status & 2^(9-1) > 0
		CalValve = status & 2^(10-1) > 0
		VacuumPump = status & 2^(11-1) > 0
		BypassPressureRegulator = status & 2^(12-1) > 0
		Recorder = status & 2^(13-1) > 0
		AUXDevice = status & 2^(14-1) > 0
		#set = status & 2^(15-1) > 0
		#reserved = status & 2^(16-1) > 0

		# Byte 2
		RemoteMode = status & 2^(17-1) > 0
		#reserved = status & 2^(18-1) > 0
		PowerUpPhase = status & 2^(19-1) > 0
		CalibrationActive = status & 2^(20-1) > 0
		StandbyOperation = status & 2^(21-1) > 0
		#not_used = status & 2^(22-1) > 0
		#set = status & 2^(23-1) > 0
		#reserved = status & 2^(24-1) > 0

		new(ScrubberHeating,ReactorChamberHeating,AdditionalConvHeating,HotTubingHeating,PeltierCooling,OzoneGenerator,CalValve,VacuumPump,BypassPressureRegulator,Recorder,AUXDevice,RemoteMode,PowerUpPhase,CalibrationActive,StandbyOperation)
	end # End of constructor
end # End of type

function Base.show(io::IO, x::nCLD_Status)
	# Byte 1
	x.ScrubberHeating ? println("\tScrubber Heating ON") : println("\tScrubber Heating OFF")
	x.ReactorChamberHeating ? println("\tReactor Chamber Heating ON") : println("\tReactor Chamber Heating OFF")
	x.AdditionalConvHeating ? println("\tAdditional Conv Heating ON") : println("\tAdditional Conv Heating OFF")
	x.HotTubingHeating ? println("\tHot Tubing Heating ON") : println("\tHot Tubing Heating OFF")
	x.PeltierCooling ? println("\tPeltier Cooling ON") : println("\tPeltier Cooling OFF")
	#not_used
	#set
	#reserved

	# Byte 2
	x.OzoneGenerator ? println("\tOzone Generator ON") : println("\tOzone Generator OFF")
	x.CalValve ? println("\tCal Valve ON") : println("\tCal Valve OFF")
	x.VacuumPump ? println("\tVacuum Pump ON") : println("\tVacuum Pump OFF")
	x.BypassPressureRegulator ? println("\tBypass Pressure Regulator ON") : println("\tBypass Pressure Regulator OFF")
	x.Recorder ? println("\tRecorder ON") : println("\tRecorder OFF")
	x.AUXDevice ? println("\tAUX Device ON") : println("\tAUX Device OFF")
	#set
	#reserved

	# Byte 3
	x.RemoteMode ? println("\tRemote Mode") : print("")
	#reserved
	x.PowerUpPhase ? println("\tPower Up Phase") : print("")
	x.CalibrationActive ? println("\tCalibration Active") : print("")
	x.StandbyOperation ? println("\tStandby Operation") : print("")
	#not_used
	#set
	#reserved
end

# --------------------------------------------------------------------------------

mutable struct nCLD_ErrorsWarnings
	# Byte 1: Errors
	SetupCalDataLost::Bool
	VacuumFailure::Bool
	SensorMalfunction::Bool
	ScrubberHeatingFailure::Bool
	OzonatorHighVoltageFailure::Bool
	BypassPressureOutOfRange::Bool
	FlowSensorNotCalibrated::Bool
	PeltierCoolerFailure::Bool

	# Byte 2: Errors
	ConverterHeatingFailure::Bool
	ReactorHeatingFailure::Bool
	TubingHeatingFailure::Bool
	SampleCalFlowOutOfRange::Bool
	HardwareTypeChanged::Bool
	CalibrationError::Bool
	InletPressureO3OutOfRange::Bool
	PMTError::Bool

	# Byte 3: Warnings
	ConverterLifetime::Bool
	PumpMaintenance::Bool
	InstrumentTemperatureTooLow::Bool
	InstrumentTemperatureTooHigh::Bool
	BypassOutOfRange::Bool
	InletPressureO3TooLow::Bool
	#Unused::Bool
	#Unused::Bool

	# Bytle 4: Warnings
	RangeAOverflow::Bool
	OzoneNotConstant::Bool
	#Unused::Bool
	#Unused::Bool
	RangeBOverflow::Bool
	#Unused::Bool
	#Unused::Bool
	#Unused::Bool

	# Constructor for nCLD_ErrorsWarnings type
	function nCLD_ErrorsWarnings(status::Float64)
		nCLD_ErrorsWarnings(UInt(status))
	end # End nCLD_ErrorsWarnings(status::Float64)

	# Constructor for nCLD_ErrorsWarnings type
	function nCLD_ErrorsWarnings(status::String)
		nCLD_ErrorsWarnings(parse(UInt,"0x" * status))
	end # End nCLD_ErrorsWarnings(status::String)

	# Constructor for nCLD_ErrorsWarnings type
	function nCLD_ErrorsWarnings(status::UInt)
		# Byte 1: Errors
		SetupCalDataLost = status & 2^(1-1) > 0
		VacuumFailure = status & 2^(2-1) > 0
		SensorMalfunction = status & 2^(3-1) > 0
		ScrubberHeatingFailure = status & 2^(4-1) > 0
		OzonatorHighVoltageFailure = status & 2^(5-1) > 0
		BypassPressureOutOfRange = status & 2^(6-1) > 0
		FlowSensorNotCalibrated = status & 2^(7-1) > 0
		PeltierCoolerFailure = status & 2^(8-1) > 0

		# Byte 2: Errors
		ConverterHeatingFailure = status & 2^(9-1) > 0
		ReactorHeatingFailure = status & 2^(10-1) > 0
		TubingHeatingFailure = status & 2^(11-1) > 0
		SampleCalFlowOutOfRange = status & 2^(12-1) > 0
		HardwareTypeChanged = status & 2^(13-1) > 0
		CalibrationError = status & 2^(14-1) > 0
		InletPressureO3OutOfRange = status & 2^(15-1) > 0
		PMTError = status & 2^(16-1) > 0

		# Byte 3: Warnings
		ConverterLifetime = status & 2^(17-1) > 0
		PumpMaintenance = status & 2^(18-1) > 0
		InstrumentTemperatureTooLow = status & 2^(19-1) > 0
		InstrumentTemperatureTooHigh = status & 2^(20-1) > 0
		BypassOutOfRange = status & 2^(21-1) > 0
		InletPressureO3TooLow = status & 2^(22-1) > 0
		#Unused = status & 2^(23-1) > 0
		#Unused = status & 2^(24-1) > 0

		# Byte 4: Warnings
		RangeAOverflow = status & 2^(25-1) > 0
		OzoneNotConstant = status & 2^(26-1) > 0
		#Unused = status & 2^(27-1) > 0
		#Unused = status & 2^(28-1) > 0
		RangeBOverflow = status & 2^(29-1) > 0
		#Unused = status & 2^(30-1) > 0
		#Unused = status & 2^(31-1) > 0
		#Unused = status & 2^(32-1) > 0

		new(SetupCalDataLost,VacuumFailure,SensorMalfunction,ScrubberHeatingFailure,OzonatorHighVoltageFailure,BypassPressureOutOfRange,FlowSensorNotCalibrated,PeltierCoolerFailure,ConverterHeatingFailure,ReactorHeatingFailure,TubingHeatingFailure,SampleCalFlowOutOfRange,HardwareTypeChanged,CalibrationError,InletPressureO3OutOfRange,PMTError,ConverterLifetime,PumpMaintenance,InstrumentTemperatureTooLow,InstrumentTemperatureTooHigh,BypassOutOfRange,InletPressureO3TooLow,RangeAOverflow,OzoneNotConstant,RangeBOverflow)
	end # End of constructor
end # End of type

function Base.show(io::IO, x::nCLD_ErrorsWarnings)
	# No Errors or Warnings at all?
	if x.SetupCalDataLost | x.VacuumFailure | x.SensorMalfunction | x.ScrubberHeatingFailure | x.OzonatorHighVoltageFailure | x.BypassPressureOutOfRange | x.FlowSensorNotCalibrated | x.PeltierCoolerFailure | x.ConverterHeatingFailure | x.ReactorHeatingFailure | x.TubingHeatingFailure | x.SampleCalFlowOutOfRange | x.HardwareTypeChanged | x.CalibrationError | x.InletPressureO3OutOfRange | x.PMTError | x.ConverterLifetime | x.PumpMaintenance | x.InstrumentTemperatureTooLow | x.InstrumentTemperatureTooHigh | x.BypassOutOfRange | x.InletPressureO3TooLow | x.RangeAOverflow | x.OzoneNotConstant | x.RangeBOverflow == false
		println("No errors or warnings.")
	else
		# Byte 1: Errors
		x.SetupCalDataLost ? println("\tSetup and Calibration Data Lost") : print("")
		x.VacuumFailure ? println("\tVacuum Failure") : print("")
		x.SensorMalfunction ? println("\tMalfunction of a sensor or regulation loop") : print("")
		x.ScrubberHeatingFailure ? println("\tScrubber heating failure") : print("")
		x.OzonatorHighVoltageFailure ? println("\tOzonator high voltage failure") : print("")
		x.BypassPressureOutOfRange ? println("\tBypass pressure out of range") : print("")
		x.FlowSensorNotCalibrated ? println("\tFlow sensor not calibrated") : print("")
		x.PeltierCoolerFailure ? println("\tPeltier cooling failure") : print("")

		# Byte 2: Errors
		x.ConverterHeatingFailure ? println("\tConverter heating failure") : print("")
		x.ReactorHeatingFailure ? println("\tReactor heating failure") : print("")
		x.TubingHeatingFailure ? println("\tTubing heating failure") : print("")
		x.SampleCalFlowOutOfRange ? println("\tSample / Cal flow out of range") : print("")
		x.HardwareTypeChanged ? println("\tHardware def.! I-Type changed!") : print("")
		x.CalibrationError ? println("\tCalibration Error") : print("")
		x.InletPressureO3OutOfRange ? println("\tInlet pressure O3 out of range") : print("")
		x.PMTError ? println("\tPMT Error") : print("")

		# Byte 3: Warnings
		x.ConverterLifetime ? println("\tConverter lifetime exceeded") : print("")
		x.PumpMaintenance ? println("\tPump maintenance requried") : print("")
		x.InstrumentTemperatureTooLow ? println("\tInstrument temperature too low") : print("")
		x.InstrumentTemperatureTooHigh ? println("\tInstrument temperature too high") : print("")
		x.BypassOutOfRange ? println("\tBypass out of allowed range") : print("")
		x.InletPressureO3TooLow ? println("\tInlet pressure O3 too low") : print("")
		#x.Unused ? println("\t") : print("")
		#x.Unused ? println("\t") : print("")

		# Byte 4: Warnings
		x.RangeAOverflow ? println("\tRange A overflow! Change Range") : print("")
		x.OzoneNotConstant ? println("\tO3-up. Ozone not constant!") : print("")
		#x.Unused ? println("\t") : print("")
		#x.Unused ? println("\t") : print("")
		x.RangeBOverflow ? println("\tRange B overflow! Change Range") : print("")
		#x.Unused ? println("\t") : print("")
		#x.Unused ? println("\t") : print("")
		#x.Unused ? println("\t") : print("")
	end
end


text:
import Gnuplot
import Gnuplot.recipe

export residuals

function Gnuplot.recipe(res::QSFitResults)
    out = [Gnuplot.recipe(res.pspec.data), Gnuplot.recipe(res.model)...]
    return reverse(out)
end

function residuals(res::QSFitResults)
    out1 = Gnuplot.PlotElement(
        cmds=["set grid"],
        data = Gnuplot.DatasetBin(res.pspec.data.domain[:],
                                  (res.pspec.data.val .- res.model()) ./ res.pspec.data.val),
        plot="with p t 'Residuals' lc rgb 'red'")
    out2 = Gnuplot.PlotElement(
        data = Gnuplot.DatasetText([extrema(res.pspec.data.domain[:])...], [0., 0.]),
        plot="with l notit dt 2 lc rgb 'black'")
    return [out1, out2]
end


text:
using ArchGDAL
using Test

# Here's our stub setup.
# Coarse grid from ul=(-10, 16) to lr=(10, 0) in xy
# Give it w=20, h=16.
# Fine grid from ul=(-5, 12) to lr=(5, 4) in xy
# Give it w=10*5=50, h=8*5=40, so 5x the resolution of the coarse.
# CCCCCCCCCC|CCCCCCCCCC
# CCCCCCCCCC|CCCCCCCCCC
# CCCCCCCCCC|CCCCCCCCCC
# CCCCCCCCCC|CCCCCCCCCC
# CCCCCFFFFF|FFFFFCCCCC
# CCCCCFFFFF|FFFFFCCCCC
# CCCCCFFFFF|FFFFFCCCCC
# CCCCCFFFFF|FFFFFCCCCC
# CCCCCFFFFF|FFFFFCCCCC
# ----------|----------
# CCCCCFFFFF|FFFFFCCCCC
# CCCCCFFFFF|FFFFFCCCCC
# CCCCCFFFFF|FFFFFCCCCC
# CCCCCFFFFF|FFFFFCCCCC
# CCCCCFFFFF|FFFFFCCCCC
# CCCCCCCCCC|CCCCCCCCCC
# CCCCCCCCCC|CCCCCCCCCC
# CCCCCCCCCC|CCCCCCCCCC
# CCCCCCCCCC|CCCCCCCCCC
coarse_geo = Geo(
      Float64[-10, 16] , # origin
      Float64[1 0.0; 0.0 -1],  # pixel size
   )
fine_geo = Geo(
      Float64[-5, 12] , # origin
      Float64[.2 0.0; 0.0 -.2],  # pixel size
   )

rr = pixel_rect(coarse_geo, [1, 1])
@test rr ≈ [-10 -9; 16 15]

ci1 = cover_rect([-10 -9; 16 15], coarse_geo, (26, 10))
@test ci1[1, 1] == CartesianIndex(1, 1)

ci2 = cover_rect([-10 -8; 16 15], coarse_geo, (26, 10))
cci2 = collect(ci2)
@test length(cci2) == 2

## A live data test.
hrsl_path = "~/data/inputs/HRSL/uganda2018/hrsl_uga_pop.tif"
landscan_path = "~/data/inputs/landscan/LandScan Global 2018/lspop2018/w001001.adf"
hrsl_exists = isfile(expanduser(hrsl_path))
landscan_exists = isfile(expanduser(landscan_path))

if hrsl_exists && landscan_exists
   out_path = "hrsl_ls_adjusted.tif"
   if ispath(out_path)
      rm(out_path)
   end
   assignweights(hrsl_path, landscan_path, out_path)
   @test ispath(out_path)
   @test stat(out_path).size > 1210320848
else
   println("Could not find HRSL or landscan")
   println("$(hrsl_path)=$(hrsl_exists)")
   println("$(landscan_path)=$(landscan_exists)")
end


text:
# optional arguments:
f(a, b = 5) = a + b
f(1) #> 6
f(2, 5) #> 7
f(3) #> 8
# f() #> ERROR: `f` has no method matching f()
# f(1, 2, 3)
#> ERROR: `f` has no method matching f(::Int64, ::Int64, # ::Int64)
# f(2, b = 5) #> ERROR: function f does not accept keyword arguments

# keyword arguments:
k(x; a1 = 1, a2 = 2) = x * (a1 + a2)
k(3, a2 = 3) #> 12
k(3, a2 = 3, a1 = 0) #> 9
k(3) #> 9

# combine all kinds of arguments in the same function
function allargs(normal_arg, optional_positional_arg=2; keyword_arg="ABC")
    print("normal arg: $normal_arg -")
    print("optional arg: $optional_positional_arg -")
    print("keyword arg: $keyword_arg")
end

allargs(1, 3, keyword_arg=4)
# prints: normal arg: 1 - optional arg: 3 - keyword arg: 4

function varargs2(;args...)
    args
end
varargs2(k1="name1", k2="name2", k3=7)
#> 3-element Array{Any,1} with elements:
# (:k1,"name1") (:k2,"name2") (:k3,7)

max_stars_repo_path: Module 1/Chapter03/arguments.jl

max_issues_repo_path: Module 1/Chapter03/arguments.jl

max_forks_repo_path: Module 1/Chapter03/arguments.jl


text:
module TestPadding
    using Base.Test
    using DataArrays

    dv = @data ones(3)
    @assert isequal(dv, padNA(dv, 0, 0))
    @assert length(padNA(dv, 2, 0)) == length(dv) + 2
    @assert length(padNA(dv, 0, 2)) == length(dv) + 2
    @assert length(padNA(dv, 2, 2)) == length(dv) + 4
end


text:
# Users of this module should first read the document
# https://alan-turing-institute.github.io/MLJ.jl/dev/adding_models_for_general_use/
module MLJBase

export MLJType, Model, Supervised, Unsupervised
export Deterministic, Probabilistic, Interval
export DeterministicNetwork, ProbabilisticNetwork, UnsupervisedNetwork
export fit, update, update_data, clean!
export predict, predict_mean, predict_mode, fitted_params
export transform, inverse_transform, se, evaluate, best
export info, info_dict
export is_same_except

export load_path, package_url, package_name, package_uuid  # model_traits.jl
export input_scitype, supports_weights                     # model_traits.jl
export target_scitype, output_scitype                      # model_traits.jl
export is_pure_julia, is_wrapper, prediction_type          # model_traits.jl
export params                                        # parameters.jl
export reconstruct, int, decoder, classes            # data.jl
export selectrows, selectcols, select, nrows         # data.jl
export table, levels_seen, matrix, container_type    # data.jl
export partition, unpack                             # data.jl
export @set_defaults                                 # utilities.jl
export @mlj_model                                    # mlj_model_macro.jl
export metadata_model, metadata_pkg                  # metadata_utilities
export HANDLE_GIVEN_ID, @more, @constant             # show.jl
export color_on, color_off                           # show.jl
export UnivariateFinite, average                     # distributions.jl
export SupervisedTask, UnsupervisedTask, MLJTask     # tasks.jl
export X_and_y, X_, y_, nrows, nfeatures             # tasks.jl
export info                                          # info.jl
export load_boston, load_ames, load_iris,
       load_reduced_ames, load_crabs,
       @load_boston, @load_ames, @load_iris,
       @load_reduced_ames, @load_crabs               # datasets.jl
export @load

# MEASURES
export measures # measures/registry.jl
export orientation, reports_each_observation
export is_feature_dependent, aggregation
export aggregate
export default_measure, value
# -- continuous
export mav, mae, rms, rmsl, rmslp1, rmsp, l1, l2
# -- confmat (measures/confusion_matrix)
export confusion_matrix, confmat
# -- finite (measures/finite)
export cross_entropy, BrierScore,
       misclassification_rate, mcr, accuracy,
       balanced_accuracy, bacc, bac,
       matthews_correlation, mcc
# -- -- binary // order independent
export auc, roc_curve, roc
# -- -- binary // order dependent
export TruePositive, TrueNegative, FalsePositive, FalseNegative,
       TruePositiveRate, TrueNegativeRate, FalsePositiveRate, FalseNegativeRate,
       FalseDiscoveryRate, Precision, NPV, FScore,
       # standard synonyms
       TPR, TNR, FPR, FNR,
       FDR, PPV,
       Recall, Specificity, BACC,
       # defaults and their synonyms
       truepositive, truenegative, falsepositive, falsenegative,
       truepositive_rate, truenegative_rate, falsepositive_rate,
       falsenegative_rate, negativepredicitive_value,
       positivepredictive_value,
       tp, tn, fp, fn, tpr, tnr, fpr, fnr,
       falsediscovery_rate, fdr, npv, ppv,
       recall, sensitivity, hit_rate, miss_rate,
       specificity, selectivity, f1score, f1, fallout

# methods from other packages to be rexported:
export pdf, mean, mode

# re-export of ScientificTypes (`Table` not exported):
export trait
export Scientific, Found, Unknown, Finite, Infinite
export OrderedFactor, Multiclass, Count, Continuous
export Binary, ColorImage, GrayImage, Image
export scitype, scitype_union, coerce, schema

# rexport from Random, Statistics, Distributions, CategoricalArrays:
export pdf, mode, median, mean, shuffle!, categorical, shuffle, levels, levels!
export std

import Base.==, Base.precision, Base.getindex
import Base: @__doc__

using Tables, DelimitedFiles
using OrderedCollections # already a dependency of StatsBase
using CategoricalArrays

# to be extended:
import StatsBase: fit, predict, fit!
import Missings.levels

import Distributions
import Distributions: pdf, mode

using ScientificTypes
using LossFunctions

# from Standard Library:

using Statistics, LinearAlgebra, Random, InteractiveUtils

## CONSTANTS

# the directory containing this file:
const srcdir = dirname(@__FILE__)
# horizontal space for field names in `MLJType` object display:
const COLUMN_WIDTH = 24
# how deep to display fields of `MLJType` objects:
const DEFAULT_SHOW_DEPTH = 0

include("utilities.jl")

## BASE TYPES

abstract type MLJType end
include("equality.jl") # equality for MLJType objects

## ABSTRACT MODEL TYPES

# for storing hyperparameters:
abstract type Model <: MLJType end

abstract type Supervised <: Model end
abstract type Unsupervised <: Model end

# supervised models that `predict` probability distributions are of:
abstract type Probabilistic <: Supervised end

# supervised models that `predict` point-values are of:
abstract type Deterministic <: Supervised end

# supervised models that `predict` intervals:
abstract type Interval <: Supervised end

# for models that are "exported" learning networks (return a Node as
# their fit-result; see MLJ docs:
abstract type ProbabilisticNetwork <: Probabilistic end
abstract type DeterministicNetwork <: Deterministic end
abstract type UnsupervisedNetwork <: Unsupervised end

## THE MODEL INTERFACE

# every model interface must implement a `fit` method of the form
# `fit(model, verbosity::Integer, training_args...) -> fitresult, cache, report`
# or, one the simplified versions
# `fit(model, training_args...) -> fitresult`
fit(model::Model, verbosity::Integer, args...) =
    fit(model, args...), nothing, nothing

# each model interface may optionally overload the following refitting
# method:
update(model::Model, verbosity, fitresult, cache, args...) =
    fit(model, verbosity, args...)

# fallbacks for supervised models that don't support sample weights:
fit(model::Supervised, verbosity::Integer, X, y, w) =
    fit(model, verbosity, X, y)
update(model::Supervised, verbosity, fitresult, cache, X, y, w) =
    update(model, verbosity, fitresult, cache, X, y)

# stub for online learning method update method
function update_data end

# methods dispatched on a model and fit-result are called
# *operations*.  Supervised models must implement a `predict`
# operation (extending the `predict` method of StatsBase).

# unsupervised methods must implement this operation:
function transform end

# unsupervised methods may implement this operation:
function inverse_transform end

# this operation can be optionally overloaded to provide access to
# fitted parameters (eg, coeficients of linear model):
fitted_params(::Model, fitresult) = (fitresult=fitresult,)

# probabilistic supervised models may also overload one or more of
# `predict_mode`, `predict_median` and `predict_mean` defined below.

# mode:
predict_mode(model::Probabilistic, fitresult, Xnew) =
    mode.(predict(model, fitresult, Xnew))

# mean:
predict_mean(model::Probabilistic, fitresult, Xnew) =
    mean.(predict(model, fitresult, Xnew))

# median:
predict_median(model::Probabilistic, fitresult, Xnew) =
    median.(predict(model, fitresult, Xnew))

# operations implemented by some meta-models:
function se end
function evaluate end
function best end

# a model wishing invalid hyperparameters to be corrected with a
# warning should overload this method (return value is the warning
# message):
clean!(model::Model) = ""

## STUB FOR @load (extended by MLJModels)

macro load end

## TRAITS

"""

    info(object)

List the traits of an object, such as a model or a performance measure.

"""
info(object) = info(object, Val(ScientificTypes.trait(object)))

include("model_traits.jl")

# for unpacking the fields of MLJ objects:
include("parameters.jl")

# for displaying objects of `MLJType`:
include("show.jl")

# convenience methods for manipulating categorical and tabular data
include("data.jl")

# metadata utils
include("metadata_utilities.jl")

# probability distributions and methods not provided by
# Distributions.jl package:
include("distributions.jl")

include("info.jl")
include("datasets.jl")
include("tasks.jl")
include("measures/measures.jl")
include("measures/registry.jl")

# mlj model macro to help define models
include("mlj_model_macro.jl")

function __init__()
    ScientificTypes.TRAIT_FUNCTION_GIVEN_NAME[:supervised_model] =
        x-> x isa Supervised
    ScientificTypes.TRAIT_FUNCTION_GIVEN_NAME[:unsupervised_model] =
        x-> x isa Unsupervised
    ScientificTypes.TRAIT_FUNCTION_GIVEN_NAME[:measure] = is_measure
    ScientificTypes.TRAIT_FUNCTION_GIVEN_NAME[:measure_type] = is_measure_type
end

end # module


text:
@testset "Optimizer" begin
    @testset "Global" begin
        f(x) =
            (x[2] - 5.1 / (4 * π^2) * x[1]^2 + 5 / π * x[1] - 6)^2 +
            10 * (1 - 1 / (8π)) * cos(x[1]) +
            10 +
            5 * x[1]
        optix = [-3.689285272296118, 13.629987729088747]
        optif = -16.64402157084319
        lb = [-5.0, 0.0]
        ub = [10.0, 15.0]
        for o in (CRSOptimizer, ISRESOptimizer)
            opt = o(maxeval = 10000, population = 100)
            fx, x, ret = minimize(opt, f, :Auto, lb, ub, (lb + ub) / 2)
            @test isapprox(fx, optif, rtol = 1e-3)
            @test isapprox(x, optix, rtol = 1e-3)
        end

    end
    @testset "Local" begin
        f(x) = sum(i -> i * x[i]^2, length(x))
        lb = fill(-10.0, 5)
        ub = fill(10.0, 5)
        optif = 0.0
        optix = zeros(5)
        for o in (LBFGSOptimizer, BOBYQAOptimizer)
            opt = o(maxeval = 1000)
            fx, x, ret = minimize(opt, f, :Auto, lb, ub, (lb + ub) / 2)
            @test isapprox(fx, optif, rtol = 1e-3)
            @test isapprox(x, optix, rtol = 1e-3)
        end
    end
end


text:
push!(LOAD_PATH, ".")
using ProgressBars
using NPZ
using neural_network

@doc """
Test the neural network on the test set.
"""
function run_test_set()
    @info "start reading test data"
    testimages::Matrix{Float64} = npzread("dataset/test_images.npy")
    testlabels::Vector{Int} = parse.(Int, readlines(open("dataset/test_labels.txt")))
    @info "stop reading"

    network::NeuralNetwork = NeuralNetwork([784, 200, 75, 10], "src/params")

    total_correct = 0
    iter = ProgressBar(1:10000)
    for (i, image, label) ∈ zip(iter, eachcol(testimages), testlabels)
        feed_forward!(network, image)
        prediction = argmax(network.layers[end]) - 1
        if prediction == label
            total_correct += 1
        end
        set_description(iter, "Total Correct: $(total_correct) / $(i), $(round(total_correct * 100 / i, digits=2))%")
    end
end

run_test_set()

max_stars_repo_name: ShakyaMajumdar/neural-network

max_issues_repo_name: ShakyaMajumdar/neural-network

max_forks_repo_name: ShakyaMajumdar/neural-network


text:
# ---
# title: 1191. K-Concatenation Maximum Sum
# id: problem1191
# author: Tian Jun
# date: 2020-10-31
# difficulty: Medium
# categories: Dynamic Programming
# link: <https://leetcode.com/problems/k-concatenation-maximum-sum/description/>
# hidden: true
# ---
#
# Given an integer array `arr` and an integer `k`, modify the array by repeating
# it `k` times.
#
# For example, if `arr = [1, 2]` and `k = 3 `then the modified array will be
# `[1, 2, 1, 2, 1, 2]`.
#
# Return the maximum sub-array sum in the modified array. Note that the length
# of the sub-array can be `0` and its sum in that case is `0`.
#
# As the answer can be very large, return the answer  **modulo**  `10^9 + 7`.
#
#
#
# **Example 1:**
#
#
#
#     Input: arr = [1,2], k = 3
#     Output: 9
#
#
# **Example 2:**
#
#
#
#     Input: arr = [1,-2,1], k = 5
#     Output: 2
#
#
# **Example 3:**
#
#
#
#     Input: arr = [-1,-2], k = 7
#     Output: 0
#
#
#
#
# **Constraints:**
#
#   * `1 <= arr.length <= 10^5`
#   * `1 <= k <= 10^5`
#   * `-10^4 <= arr[i] <= 10^4`
#
#
## @lc code=start
using LeetCode

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


text:
 using Test
 using LogicCircuits
 using LogicCircuits.IO: VtreeCommentLine, VtreeHeaderLine, VtreeLeafLine, VtreeInnerLine

@testset "PlainVtree File Parser Test" begin
    vtree_lines = parse_vtree_file(zoo_vtree_file("little_4var.vtree"))
    for i = 1:9
        @test vtree_lines[i] isa VtreeCommentLine
    end
    @test vtree_lines[10] isa VtreeHeaderLine
    for i = 11:14
        @test vtree_lines[i] isa VtreeLeafLine
    end
    for i = 15:17
        @test vtree_lines[i] isa VtreeInnerLine
    end

    vtree = compile_vtree_format_lines(vtree_lines)

    function test_vtree(vtree)
        # TODO these tests need to be rewritten not to be sensitive to the bottom-up traversal order
        # for i = 1:4
        #     @test vtree[i] isa PlainVtreeLeafNode
        # end
        # for i = 5:7
        #     @test vtree[i] isa PlainVtreeInnerNode
        # end

        # @test sort(variables(vtree[1])) == [1]
        # @test sort(variables(vtree[2])) == [2]
        # @test sort(variables(vtree[3])) == [3]
        # @test sort(variables(vtree[4])) == [4]

        # @test sort(variables(vtree[5])) == [1,2]
        # @test sort(variables(vtree[6])) == [3,4]
        # @test sort(variables(vtree[7])) == [1,2,3,4]

        # for i = 1:4
        #     @test num_variables(vtree[i]) == 1
        # end
        # @test num_variables(vtree[5]) == 2
        # @test num_variables(vtree[6]) == 2
        # @test num_variables(vtree[7]) == 4

        @test num_variables(vtree[end]) == 4
        @test sort(variables(vtree[end])) == [1,2,3,4]
        @test vtree[end] isa PlainVtreeInnerNode
        @test vtree[1] isa PlainVtreeLeafNode
        @test vtree[2] isa PlainVtreeLeafNode

    end

    test_vtree(vtree)

    mktempdir() do tmp
        # Save the vtree
        temp_path = "$tmp/little_4var_temp.vtree"
        save(vtree, temp_path)

        # load it from file, and then run the same tests
        test_vtree(load_vtree(temp_path))

        # Save dot file
        dot_path = "$tmp/little_4var_temp.dot"
        save(vtree, dot_path)

        # Save unsupported format
        dot_path = "$tmp/little_4var_temp.bad_extension"
        @test_throws String save(vtree, dot_path);
    end
end


text:
module Tutorial

# 1 Getting started

using Modia
@usingModiaPlot

# Define model
SimpleModel = Model(
    T = 0.4,
    x = Var(init=0.2),
    equations = :[T * der(x) + x = 1],
)

# Transform to ODE form
simpleModel = @instantiateModel(SimpleModel)

# Simulate with a default integrator of DifferentialEquations
simulate!(simpleModel, stopTime = 1.2)

# Simulate with a specific integrator (Tsit5) and use a unit for stopTime
simulate!(simpleModel, Tsit5(), stopTime = 1.2u"s")

# Produce a line plot with GLMakie
plot(simpleModel, ("x", "der(x)"), figure=1)

# 2.1 - Equation oriented models

using Modia

LowPassFilter = Model(
    T = 0.2,
    u = input,
    y = output | Var(:x),
    x = Var(init=0),
    equations = :[T * der(x) + x = u],
)

# 2.2 Merging models

HighPassFilter = LowPassFilter | Model( y = :(-x + u) )

setLogMerge(true)

LowAndHighPassFilter = LowPassFilter | Model(
        y = nothing,
        low = output | Var(:x),
        high = output | Var(:(-x + u)),
    )

setLogMerge(false)

@showModel LowAndHighPassFilter

# 2.3 Functions and tables

TestLowAndHighPassFilter = LowAndHighPassFilter | Model(
        u = :(sin( (time+1u"s")*u"1/s/s" * time)*u"V"),
        x = Var(init=0.2u"V")
    )

using Interpolations

table = CubicSplineInterpolation(0:0.5:2.0, [0.0, 0.7, 2.0, 1.8, 1.2])
TestLowAndHighPassFilter2 = TestLowAndHighPassFilter | Map(u = :(table(time*u"1/s")*u"V"))

function ref(time)
    y1 = sin(time)
    y2 = cos(time)
    return (y1,y2)
end

using Modia
TestMultiReturningFunction1 = Model(
    equations = :[
        (y1,y2) = ref(time)
        y3 = y1+y2
    ]
)
testMultiReturningFunction1 = @instantiateModel(TestMultiReturningFunction1, unitless=true)
simulate!(testMultiReturningFunction1, stopTime=10.0)
plot(testMultiReturningFunction1, ["y1", "y2", "y3"], figure=2)

# 2.4 Hierarchical modeling

TwoFilters = (
    high = HighPassFilter,
    low = LowPassFilter,
)

BandPassFilter = (
    u = input,
    y = output,
    high = HighPassFilter | Map(T=0.5, x=Var(init=0.1u"V")),
    low = LowPassFilter | Map(x=Var(init=0.2u"V")),
    equations = :[
        high.u = u,
        low.u = high.y,
        y = low.y]
)

TestBandPassFilter = BandPassFilter | Map(
        u = :(sin( (time+1u"s")*u"1/s/s" * time)*u"V")
    )

#bandPassFilter = @instantiateModel(TestBandPassFilter, logStateSelection=true)
#simulate!(bandPassFilter, Tsit5(), stopTime = 50u"s")
#plot(bandPassFilter, ["u", "y"], figure=3)

StateSpace = Model(
    A = fill(0.0, 0, 0),
    B = fill(0.0, 0, 0),
    C = fill(0.0, 0, 0),
    D = fill(0.0, 0, 0),
    u = input,
    y = output,
    x = Var(init = zeros(0)),
    equations = :[
        der(x) = A*x + B*u
             y = C*x + D*u
    ]
)

SecondOrder = Model(
    w = 20.0,
    D =  0.1,
    k =  2.0,
    sys = StateSpace | Map(A = :([  0        1;
                                 -w^2  -2*D*w]),
                           B = :([0; w^2;;]),    # Julia 1.7: Trailing ";" defines a column matrix
                           C = :([k 0]),
                           D = :(zeros(1,1)),
                           x = Var(init = zeros(2)) ),
    equations = :[sys.u = [1.0]]
)
secondOrder = @instantiateModel(SecondOrder)
simulate!(secondOrder, stopTime=2.0)
plot(secondOrder, ("sys.u", "sys.x", "sys.y"), figure=3)

using ModiaLang.StaticArrays
TestArray1 = Model(
    v = Var(init=SVector{3,Float64}(1.0, 2.0, 3.0)),
    equations = :[der(v) = -v]
)
testArray1 = @instantiateModel(TestArray1, logCode=true)
simulate!(testArray1, stopTime=2.0)
plot(testArray1, "v", figure=4)

TestArray2 = Model(
    v = Var(init=[1.0, 2.0, 3.0]),
    equations = :[der(v) = -v]
)
testArray2 = @instantiateModel(TestArray2)
simulate!(testArray2, stopTime=2.0, merge=Map(v = [4.0, 3.0, 2.0, 1.0]))
plot(testArray2, "v", figure=5)

TwoInertiasAndIdealGearTooManyInits = Model(
    J1    = 50.0,
    J2    = 100.0,
    ratio = 2.0,
    f     = 3.0, # Hz

    phi1 = Var(init = 0.0), # Absolute angle of inertia1
    w1   = Var(init = 0.0), # Absolute angular velocity of inertia1
    phi2 = Var(init = 0.0), # Absolute angle of inertia2
    w2   = Var(init = 0.0), # Absolute angular velocity of inertia2

    equations = :[
        tau = 2.0*sin(2*3.14*f*time/u"s")

        # inertia1
        w1 = der(phi1)
        J1*der(w1) = tau - tau1

        # ideal gear
        phi1 = ratio*phi2
        ratio*tau1 = tau2

        # inertia2
        w2 = der(phi2)
        J2*der(w2) = tau2
    ]
)

drive1 = @instantiateModel(TwoInertiasAndIdealGearTooManyInits, logStateSelection=true)

end


text:
module BlockFactorizations

using LinearAlgebra
using Base.Threads
using Base.Threads: @spawn, @sync

const AbstractMatOrFac{T} = Union{AbstractMatrix{T}, Factorization{T}}
const AbstractMatOrFacOrUni{T} = Union{AbstractMatrix{T}, Factorization{T}, UniformScaling{T}}
const AbstractVecOfVec{T} = AbstractVector{<:AbstractVector{T}}
const AbstractVecOfVecOrMat{T} = AbstractVector{<:AbstractVecOrMat{T}}

export BlockFactorization, BlockDiagonalFactorization

include("block.jl")

end # module