text:
"""
    AbstractResponse
"""
abstract type AbstractResponse end

"""
`Response <: AbstractResponse`

# Description

An immutable which holds the information about response, such as the identifier of the examinee who gave the response,
`examinee_id::String`, the identifier of the answered item `item_idx::String`, and the answer starting and ending times `start_time::Dates.DateTime` and `end_time::Dates.DateTime`.

"""
struct Response <: AbstractResponse
    item_idx::Int64
    examinee_idx::Int64
    item_id::String
    examinee_id::String
    val::Union{Missing,Float64}
    start_time::Dates.DateTime
    end_time::Dates.DateTime
    Response(
        item_idx,
        examinee_idx,
        item_id,
        examinee_id,
        val,
        start_time,
        end_time,
    ) = new(item_idx, examinee_idx, item_id, examinee_id, val, start_time, end_time)
    Response(
        item_idx,
        examinee_idx,
        item_id,
        examinee_id,
        val,
        time::Dates.DateTime,
    ) = new(item_idx, examinee_idx, item_id, examinee_id, val, time, time)
end

# Outer Constructor Methods

"""
```julia
get_examinees_by_item_id(
    item_id::String,
    responses::Vector{<:AbstractResponse},
    examinees::Vector{<:AbstractExaminee},
)
```

# Description

It returns the examinees who answered to the item with id `item_id`.
"""
function get_examinees_by_item_id(
    item_id::String,
    responses::Vector{<:AbstractResponse},
    examinees::Vector{<:AbstractExaminee},
)
    examinees[filter(r -> r.item_id == item_id, responses).examinee_id]
end

"""
```julia
get_items_by_examinee_id(
    examinee_id::String,
    responses::Vector{<:AbstractResponse},
    items::Vector{<:AbstractItem},
)
```

# Description

It returns the items answered by the examinee with id `examinee_id`.
"""
function get_items_by_examinee_id(
    examinee_id::String,
    responses::Vector{<:AbstractResponse},
    items::Vector{<:AbstractItem},
)
    items[filter(r -> r.examinee_id == examinee_id, responses).item_id]
end

"""
```julia
add_response!(response::AbstractResponse, responses::Vector{<:AbstractResponse})
```

# Description

Push the response in the response vector `responses`.
"""
function add_response!(response::AbstractResponse, responses::Vector{<:AbstractResponse})
    push!(response, responses)
end

"""
```julia
get_responses_by_examinee_id(examinee_id::String, responses::Vector{<:AbstractResponse})
```

# Description

It returns the vector of responses given by examinee with id = `id`.
"""
function get_responses_by_examinee_id(
    examinee_id::String,
    responses::Vector{<:AbstractResponse},
)
    filter(r -> r.examinee_id == examinee_id, responses)
end

"""
```julia
get_responses_by_item_id(item_id::String, responses::Vector{<:AbstractResponse})
```

# Description

It returns the vector of responses to item with id equal to `item_id`.
"""
function get_responses_by_item_id(item_id::String, responses::Vector{<:AbstractResponse})
    filter(r -> r.item_id == item_id, responses)
end

"""
```julia
get_responses_by_item_idx(item_idx::Int64, responses::Vector{<:AbstractResponse}; sorted = true)
```

# Description

It returns the vector of responses to item with idx equal to `item_idx`.
The vector of responses is sorted by `examinee_idx` if `sorted = true`.
"""
function get_responses_by_item_idx(
    item_idx::Int64,
    responses::Vector{<:AbstractResponse};
    sorted = true,
)
    resp_item = filter(r -> r.item_idx == item_idx, responses)
    if sorted
        sort!(resp_item, by = r -> r.examinee_idx)
    end
end

"""
```julia
_generate_response(latent::Latent1D, parameters::AbstractParametersBinary)
```

# Description

Randomly generate a response for a 1-dimensional latent variable and custom item parameters.
"""
function _generate_response(latent::Latent1D, parameters::AbstractParametersBinary)
    Float64(rand(Distributions.Bernoulli(_probability(
        latent,
        parameters,
        Array{Float64,1}(undef, 0),
        Array{Float64,1}(undef, 0),
    ))))::Float64
end

"""
```julia
answer(examinee::AbstractExaminee, item::AbstractItem)
```

# Description

Randomly generate a response by `examinee` to a dichotomous (binary) `item`.
"""
function answer(examinee::AbstractExaminee, item::AbstractItem)
    Response(
        item.idx,
        examinee.idx,
        item.id,
        examinee.id,
        _generate_response(examinee.latent, item.parameters),
        Dates.now(),
        Dates.now(),
    )
end

"""
```julia
answer(examinee::AbstractExaminee, items::Vector{<:AbstractItem})
```

# Description

Randomly generate a response by `examinee` to dichotomous (binary) `items`.
"""
function answer(examinee::AbstractExaminee, items::Vector{<:AbstractItem})
    map(
        i -> Response(
            i.idx,
            examinee.idx,
            i.id,
            examinee.id,
            _generate_response(examinee.latent, i.parameters),
            Dates.now(),
        ),
        items,
    )
end

"""
```julia
answer(examinee_id::String, item_id::String, examinees::Vector{<:AbstractExaminee}, items::Vector{<:AbstractItem})
```

# Description

Randomly generate a response by `Examinee` with index `examinee_id` to a dichotomous (binary) `item` with index `item_id`.
"""
function answer(
    examinee_id::String,
    item_id::String,
    examinees::Vector{<:AbstractExaminee},
    items::Vector{<:AbstractItem},
)
    answer(
        get_examinee_by_id(examinee_id, examinees),
        get_item_by_id(item_id, items),
    )
end

"""
```julia
answer(examinees::Vector{<:AbstractExaminee}, items::Vector{<:AbstractItem})
```

# Description

Randomly generate responses by all the examinees in `examinees` to items in `items`.
"""
function answer(examinees::Vector{<:AbstractExaminee}, items::Vector{<:AbstractItem})
    mapreduce(e -> map(i -> answer(e, i), items), vcat, examinees)
end

"""
```julia
get_design_matrix(responses::Vector{Response}, I::Int64, N::Int64)
```

# Description

Returns the ``I \times N `` design matrix.
"""
function get_design_matrix(responses::Vector{Response}, I::Int64, N::Int64)
    has_answered = map(r -> CartesianIndex(r.item_idx, r.examinee_idx), responses)
    design = zeros(Float64, I, N)
    design[has_answered] .= one(Float64)
    return design::Matrix{Float64}
end

"""
```julia
get_response_matrix(responses::Vector{Response}, I::Int64, N::Int64)
```

# Description

Transform vector of `Response`s in a ``I \times N`` response matrix.
A non given answer has value `0.0`.
"""
function get_response_matrix(responses::Vector{Response}, I::Int64, N::Int64)
    response_matrix = Matrix{Union{Missing, Float64}}(missing .* ones(Float64, I, N))

    map(r -> response_matrix[CartesianIndex(r.item_idx, r.examinee_idx)] = r.val, responses)
    return response_matrix::Matrix{Union{Missing, Float64}}
end

"""
```julia
get_responses(response_matrix::Matrix{Float64}, design_matrix::Matrix{Float64}, items::Vector{<:AbstractItem}, examinees::Vector{<:AbstractExaminee})
```

Transforms a ``I \times N`` response matrix in a vector of `Response`s given a valid `design_matrix`, a vector of `Item`s and a vector of `Examinee`s.
"""
function get_responses(
    response_matrix::Matrix{Float64},
    design_matrix::Matrix{Float64},
    items::Vector{<:AbstractItem},
    examinees::Vector{<:AbstractExaminee},
)
    mapreduce(
        e -> map(
            i -> Response(
                i.idx,
                e.idx,
                i.id,
                e.id,
                response_matrix[i.idx, e.idx],
                Dates.now(),
            ),
            items[findall(design_matrix[:, e.idx] .> 0.0)],
        ),
        vcat,
        examinees,
    )
end

"""
```julia
get_items_idx_answered_by_examinee(
    examinee::AbstractExaminee,
    responses::Vector{<:AbstractResponse},
)
```

# Description

Returns the idx of the items answered by examinee.
"""
function get_items_idx_answered_by_examinee(
    examinee::AbstractExaminee,
    responses::Vector{<:AbstractResponse},
)
    resp_e = get_responses_by_examinee_id(examinee.id, responses)
    return map(r -> r.item_idx, resp_e)
end

"""
```julia
get_examinees_idx_who_answered_item(
    item::AbstractItem,
    responses::Vector{<:AbstractResponse},
)
```

# Description

Returns the idx of the examinees who answered to item.
"""
function get_examinees_idx_who_answered_item(
    item::AbstractItem,
    responses::Vector{<:AbstractResponse},
)
    resp_e = get_responses_by_item_id(item.id, responses)
    return map(r -> r.examinee_idx, resp_e)
end


text: include("truncatedconjugategradient.jl")

ext: jl


text:
function find_joltage_differences(adapters)
    pushfirst!(adapters, 0)
    device = maximum(adapters) + 3
    push!(adapters, device)
    sort!(adapters)
    one = 0
    three = 0
    for (x, y) in zip(adapters, adapters[2:end])
        diff = y - x
        if diff == 1
            one += 1
        elseif diff == 3
            three += 1
        end
    end
    one * three
end

function main()
    adapters = map(x -> parse(Int64, x), readlines("../resources/input_10.txt"))
    result = find_joltage_differences(adapters)
    println(result)
end

main()


text:
#######################################
#
# evt file
#
#######################################

"""
Read *.evt file and convert to form for EEG.jl
"""
function read_evt(fname::AbstractString, fs::Number; kwargs...)
    @info("Reading evt file: $fname")

    fs = ustrip(fs)

    d = readdlm(fname)

    @assert (size(d, 2) <= 3) "EVT file has too many columns"

    d = Dict(d[1,1] => d[2:end, 1], d[1,2] => d[2:end, 2], d[1,3] => d[2:end, 3])

    if haskey(d, "Tmu")
        d["Index"] = [1 + round.(i * (1 / 1000000) * float(fs)) for i in d["Tmu"]]
    elseif haskey(d, "Tsec")
        d["Index"] = [1 + round.(i  * float(fs)) for i in d["Tsec"]]
    else
        @warn("Unknown time scale in evt file")
    end

    d["Duration"] = ones(length(d["Code"]))

    @info("Imported $(length(d["Code"])) events")

    return Dict("Code" => d["Code"] + 252, "Index" => d["Index"], "Duration" => d["Duration"])
end


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

using Markdown
using InteractiveUtils

# ╔═╡ 8af90e7a-0e5e-4a9d-b39e-f7603fc1e25e
begin
	using Pkg
	Pkg.activate(mktempdir())

	ENV["LC_NUMERIC"]="C" # necessary for ensuring
	                      # parallel proper use of PyPlot and Triangulate
	Pkg.develop("Triangulate")
	Pkg.add("PyPlot")
	Pkg.add("PlutoUI")

	using Triangulate, PyPlot, PlutoUI, Printf
	PyPlot.svg(true)
end;

# ╔═╡ 7b468c8a-ce16-11eb-3009-f9e4c79e6c17
md"""
# Triangulate examples
$(TableOfContents(title=""))
## Triangulations of point sets
"""

# ╔═╡ 83f07f94-7e35-42de-9214-715c92b6dddd
md"""
### Delaunay triangulation (DT)

The Delaunay triangulation of a set of points is a triangulation
where for each triangle, the interior of its circumcircle  does not contain any points
of the triangulation.

The Delaunay triangulation of a set of points in general position
(no 4 of them on a circle) is unique. At the same time, it is a
triangulation of the convex hull of these points.

Given an input list of points, without any further flags, Triangle creates
just this triangulation (the "Q" flag suppresses the text output of Triangle).
For this and the next examples, the input list of points is created randomly,
but on a raster, preventing the appearance of too close points.

"""

# ╔═╡ c56434f3-e0a4-462e-86d5-fcfbb6778c11
md"""
### DT with boundary
Same as the previous example, but in addition specify the "c" flag.

In this case, Triangle outputs an additional list of segments
describing the boundary of the convex hull. In fact this is a constrained
Delaunay triangulation (CDT) where the boundary segments
are the seen constraining edges which must appear in the output.

"""

# ╔═╡ f13ad00e-a3d3-462f-93d4-ebe1fe4a7b4d
md"""
### DT with Voronoi diagram

Same as the previous example, but instead of "c" specify the "v" flag.
In this case, Triangle outputs information about the Voronoi diagram
of the point set which is a structure dual to  the Delaunay triangulation.

The Voronoi cell around a point $p$ a point set $S$ is defined
as the set of points $x$ such that $|x-p|<|x-q|$ for all $q\in S$
such that $p\neq q$.
The Voronoi cells of boundary points
of the convex hull are of infinite size. The corners of the Voronoi
cells are the circumcenters of the triangles. They can be far
outside of the triangulated domain.

"""

# ╔═╡ 852e1ebf-3f29-472a-945e-53920d7653be
md"""
### Boundary conforming DT (BCDT)
Specify "c" flag for convex hull segments, "v" flag for Voronoi
and "D" flag for creating a boundary conforming Delaunay triangulation of
the point set.

In this case additional points ("Steiner points") are created which split
the boundary segments and ensure that all triangle circumcenters
lie within the convex hull.
Due to random input, there may be situations where Triangle fails with this task,
so we check for the corresponding exception.

"""

# ╔═╡ b95859af-ab21-4c41-b095-82b3ea39724a
md"""
### Constrained DT (CDT)
Constrained Delaunay triangulation (CDT) of a point set with
additional constraints given a priori. This is obtained when
specifying the "p" flag and an additional list of segments each described
by two points
which should become edges of the triangulation. Note that
the resulting triangulation is not Delaunay in the sense
given above.

"""

# ╔═╡ c5a87fb5-c264-498d-aa7c-91d9e6d7bfe2
md"""
## Triangulations of domains
"""

# ╔═╡ 7f2b4252-a827-4de4-abb6-42d1bbde44b9
md"""
### CDT of a domain

Specification is similar to that of the CDT of a point set.

The domain is given by a segment list specifying its boundary.

This is obtained by specifying the "p" flag.

"""

# ╔═╡ 6793711b-d476-45c6-8b2b-b1d0693d8e6e
md"""
### CDT with maximum area constraint

This constraint is specfied as a floating
point number given after the -a flag.
Be careful to not give it in the exponential format as Triangle would be unable to analyse it.
Therefore it is dangerous to provide it via string interpolation and it is better to convert it to a string before using `@sprintf`.

Specifying only the maximum area constraint does not prevent very thin
triangles from occuring at the boundary.

"""

# ╔═╡ 3f2f0b19-3c13-482d-abd5-e6325802c6e8
md"""
### BCDT with maximum area constraint

In addition to the area constraint specify the -D flag
in order to keep the triangle circumcenters  within the domain.

"""

# ╔═╡ 14145886-2a5f-4ae8-bf00-a5eaaa7e0568
md"""
### CDT with minimum angle condition

The "q" flag  allows to specify a minimum angle
constraint preventing skinny triangles.

This combination of flags, possibly with an additional "D" flag is recommended
when creating triangulations for finite element or finite volume methods.
It the mimimum angle is larger then 28.6 degrees, Triangle's algorithm may
run into an infinite loop.

"""

# ╔═╡ 61dc30ce-9256-4f20-9163-e8d473ac9e53
md"""
### Triangulation with refinement callback

A maximum area constraint is specified in the `unsuitable` callback
which is activated via the "u" flag if it has been passed before calling triangulate.

In addition, the "q" flag  allows to specify a minimum angle
constraint preventing skinny triangles.

"""

# ╔═╡ 30990760-92e1-4a26-98a5-b43be658dee7
md"""
### Triangulation of a heterogeneous domain

The segment list specifies its boundary and the inner boundary between subdomains.
An additional region list is specified which provides "region points" in `regionlist[1,:]`
and `regionlist[2,:]`.  These kind of mark the subdomains. `regionlist[3,:]` contains an attribute
which labels the subdomains. `regionlist[4,:]` contains a maximum area value. `size(regionlist,2)`
is the number of regions.

With the "A" flag, the subdomain labels are spread to all triangles in the corresponding
subdomains, becoming available in `triangleattributelist[1,:]`.
With the "a" flag, the area constraints are applied in the corresponding subdomains.

"""

# ╔═╡ 783975f5-cbab-4b23-9155-7a6276cd25df
md"""
### Triangulation of a domain with holes
The segment list specifies its boundary and the boundaries of the holes.
An additional hole list is specified which provides "hole points" in `holelist[1,:]`
and `holelist[2,:]`.

"""

# ╔═╡ 9a090bba-093b-4ca4-a186-1c43b52cd4ff
html"""<hr>"""

# ╔═╡ 9447e874-22ce-4b99-9037-e0d202430ee2
# Wrap "Pyplotting" into this function in order to shield calling code
# from all these peculiarities.
function pyplot(f;w=650,h=300)
	PyPlot.close()
	PyPlot.clf()
	fig=PyPlot.figure(1,dpi=100)
	fig.set_size_inches(w/100,h/100,forward=true)
	f()
	PyPlot.gcf()
end;

# ╔═╡ a5f7aca5-9e40-471a-bece-34498a804bd8
function example_convex_hull(;n=10,raster=10)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=hcat(unique([ Cdouble[rand(1:raster)/raster, rand(1:raster)/raster] for i in 1:n])...)
    display(triin)
    (triout, vorout)=triangulate("Q", triin)
	pyplot() do
		plot_in_out(PyPlot,triin,triout,title="Convex hull",circumcircles=true)
	end
end;

# ╔═╡ d0e63ebd-9288-42d5-9735-3c94a2baa8e3
example_convex_hull(;n=10,raster=10)

# ╔═╡ 6f3f0014-42bc-4565-a03e-208fed8b8f48
function example_convex_hull_with_boundary(;n=10,raster=10)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=hcat(unique([ Cdouble[rand(1:raster)/raster, rand(1:raster)/raster] for i in 1:n])...)
    display(triin)
    (triout, vorout)=triangulate("cQ", triin)
	pyplot() do
    	plot_in_out(PyPlot,triin,triout,title="Convex hull with boundary")
	end
end;

# ╔═╡ 6ec811ca-e6d3-43ea-8da9-02bb05060d8d
example_convex_hull_with_boundary(;n=10,raster=10)

# ╔═╡ 6e2d70c4-dc0f-4672-823a-930a50114811
function example_convex_hull_voronoi(;n=10,raster=10)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=hcat(unique([ Cdouble[rand(1:raster)/raster, rand(1:raster)/raster] for i in 1:n])...)
    (triout, vorout)=triangulate("vQ", triin)
	pyplot() do
    plot_in_out(PyPlot,triin,triout,voronoi=vorout,title="Convex hull with Voronoi diagram")
	end
end;

# ╔═╡ 0b54833f-0458-4417-aa03-27c4fe2a873c
example_convex_hull_voronoi(;n=10,raster=10)

# ╔═╡ 1ee0af11-96e4-4929-926e-d7143ed1f791
function example_convex_hull_voronoi_delaunay(;n=10,raster=10)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=hcat(unique([ Cdouble[rand(1:raster)/raster, rand(1:raster)/raster] for i in 1:n])...)
    try
        (triout, vorout)=triangulate("vcDQ", triin)
		pyplot() do
        plot_in_out(PyPlot,triin,triout,voronoi=vorout,title="Convex hull with Voronoi diagram")
		end
    catch err
        if typeof(err)==TriangulateError
            println("Triangle had some problem.")
            return true
        end
    end
end;

# ╔═╡ 4da9a598-bf59-4b3b-aea7-ee1c2721c206
 example_convex_hull_voronoi_delaunay(;n=10,raster=10)

# ╔═╡ 0d648d22-aa03-437b-b019-59bd693bc55e
function example_cdt(;n=10,raster=10)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=hcat(unique([ Cdouble[rand(1:raster)/raster, rand(1:raster)/raster] for i in 1:n])...)
    npt=size(triin.pointlist,2)
    triin.segmentlist=Matrix{Cint}([1 2; npt-1 npt-2;  1 npt;]')
    triin.segmentmarkerlist=Vector{Cint}([2,3,4])
    (triout, vorout)=triangulate("pcQ", triin)
	pyplot() do
    plot_in_out(PyPlot,triin,triout,title="CDT")
	end
end;

# ╔═╡ 910ce428-4989-4b08-8037-887dae5c847e
example_cdt(;n=10,raster=10)

# ╔═╡ ccd8ed8d-7991-4a19-8a05-65761adc2fee
function example_domain_cdt()
    triin=Triangulate.TriangulateIO()
    triin.pointlist=Matrix{Cdouble}([0.0 0.0 ; 1.0 0.0 ; 1.0  1.0 ; 0.6 0.6; 0.0 1.0]')
    triin.segmentlist=Matrix{Cint}([1 2 ; 2 3 ; 3 4 ; 4 5 ; 5 1 ]')
    triin.segmentmarkerlist=Vector{Int32}([1, 2, 3, 4, 5])
    (triout, vorout)=triangulate("pQ", triin)
	pyplot() do
    plot_in_out(PyPlot,triin,triout,title="Domain CDT")
	end
end;

# ╔═╡ fc5264cf-b1f7-43c5-bcff-09e6274ca215
example_domain_cdt()

# ╔═╡ b71b1faf-ff3f-4404-8a2c-9de84fa498f7
function example_domain_cdt_area(;maxarea=0.05)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=Matrix{Cdouble}([0.0 0.0 ; 1.0 0.0 ; 1.0  1.0 ; 0.6 0.6; 0.0 1.0]')
    triin.segmentlist=Matrix{Cint}([1 2 ; 2 3 ; 3 4 ; 4 5 ; 5 1 ]')
    triin.segmentmarkerlist=Vector{Int32}([1, 2, 3, 4, 5])
    area=@sprintf("%.15f",maxarea) # Don't use exponential format!
    (triout, vorout)=triangulate("pa$(area)Q", triin)
	pyplot() do
    plot_in_out(PyPlot,triin,triout,voronoi=vorout, title="Domain CDT with area constraint",circumcircles=true)
	end
end;

# ╔═╡ cd007961-7f0e-4c56-9c86-1a9827a71e3e
example_domain_cdt_area(;maxarea=0.05)

# ╔═╡ 8ac6cc0b-e4e7-4aa0-bc37-b22b15bf2a83
function example_domain_bcdt_area(;maxarea=0.05)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=Matrix{Cdouble}([0.0 0.0 ; 1.0 0.0 ; 1.0  1.0 ; 0.6 0.6; 0.0 1.0]')
    triin.segmentlist=Matrix{Cint}([1 2 ; 2 3 ; 3 4 ; 4 5 ; 5 1 ]')
    triin.segmentmarkerlist=Vector{Int32}([1, 2, 3, 4, 5])
    area=@sprintf("%.15f",maxarea)
    (triout, vorout)=triangulate("pa$(area)DQ", triin)
	pyplot() do
    plot_in_out(PyPlot,triin,triout,voronoi=vorout, title="Boundary conforming Delaunay triangulation", circumcircles=true)

	end
end;

# ╔═╡ a0cb6060-8278-444a-a4e5-46055d98616c
example_domain_bcdt_area(;maxarea=0.05)

# ╔═╡ a7e97f1c-e091-4555-abc0-a71abd22dd8a
function example_domain_qcdt_area(;minangle=20, maxarea=0.05)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=Matrix{Cdouble}([0.0 0.0 ; 1.0 0.0 ; 1.0  1.0 ; 0.6 0.6; 0.0 1.0]')
    triin.segmentlist=Matrix{Cint}([1 2 ; 2 3 ; 3 4 ; 4 5 ; 5 1 ]')
    triin.segmentmarkerlist=Vector{Int32}([1, 2, 3, 4, 5])
    area=@sprintf("%.15f",maxarea)
    angle=@sprintf("%.15f",minangle)
    (triout, vorout)=triangulate("pa$(area)q$(angle)", triin)
	pyplot() do
    plot_in_out(PyPlot,triin,triout,voronoi=vorout, title="Quality triangulation")
	end
end;

# ╔═╡ 4e7dad4d-7d3c-4201-a56f-28e3df51e885
example_domain_qcdt_area(;maxarea=0.05,minangle=20)

# ╔═╡ 083b8aef-67f1-489a-8eff-b70fb6dc9da4
function example_domain_localref(;minangle=20)
    center_x=0.6
    center_y=0.6
    localdist=0.1
    function unsuitable(x1,y1,x2,y2,x3,y3,area)
        bary_x=(x1+x2+x3)/3.0
        bary_y=(y1+y2+y3)/3.0
        dx=bary_x-center_x
        dy=bary_y-center_y
        qdist=dx^2+dy^2
        qdist>1.0e-5 && area>0.1*qdist
    end

    triunsuitable(unsuitable)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=Matrix{Cdouble}([0.0 0.0 ; 1.0 0.0 ; 1.0  1.0 ; 0.6 0.6; 0.0 1.0]')
    triin.segmentlist=Matrix{Cint}([1 2 ; 2 3 ; 3 4 ; 4 5 ; 5 1 ]')
    triin.segmentmarkerlist=Vector{Int32}([1, 2, 3, 4, 5])
    angle=@sprintf("%.15f",minangle)
    (triout, vorout)=triangulate("pauq$(angle)Q", triin)
	pyplot() do
    plot_in_out(PyPlot,triin,triout,voronoi=vorout, title="Quality triangulation with local refinement")
	end
end;

# ╔═╡ 3c88e5cb-5e85-4146-a6d7-3682f4b9a892
example_domain_localref(;minangle=20)

# ╔═╡ f0f9922b-70ce-4866-bc41-349a26095ade
function example_domain_regions(;minangle=20)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=Matrix{Cdouble}([0.0 0.0 ;0.5 0.0; 1.0 0.0 ; 1.0  1.0 ; 0.6 0.6; 0.0 1.0]')
    triin.segmentlist=Matrix{Cint}([1 2 ; 2 3 ;3 4 ; 4 5 ; 5 6 ; 6 1 ; 2 5]')
    triin.segmentmarkerlist=Vector{Int32}([1, 2, 3, 4, 5, 6, 7])
    triin.regionlist=Matrix{Cdouble}([0.2 0.8; 0.2 0.2; 1 2 ; 0.01 0.05])
    angle=@sprintf("%.15f",minangle)
    (triout, vorout)=triangulate("paAq$(angle)Q", triin)
	pyplot() do
    plot_in_out(PyPlot,triin,triout,voronoi=vorout, title="Hetero domain triangulation")
	end
end;

# ╔═╡ 58dbd0e3-b34f-44ec-905a-488c7bbd07ca
example_domain_regions(minangle=20)

# ╔═╡ 3dfe5acd-72f7-4510-b3a0-40364f679073
function example_domain_holes(;minangle=20,maxarea=0.001)
    triin=Triangulate.TriangulateIO()
    triin.pointlist=Matrix{Cdouble}([0.0 0.0;
                                     1.0 0.0;
                                     1.0 1.0;
                                     0.0 1.0;
                                     0.2 0.2;
                                     0.3 0.2;
                                     0.3 0.3;
                                     0.2 0.3;
                                     0.6 0.6;
                                     0.7 0.6;
                                     0.7 0.7;
                                     0.6 0.7;
                                     ]')
    triin.segmentlist=Matrix{Cint}([1 2; 2 3; 3 4; 4 1; 5 6; 6 7; 7 8; 8 5;  9 10; 10 11; 11 12; 12 9;]')
    triin.segmentmarkerlist=Vector{Int32}([1, 1,1,1, 2,2,2,2, 3,3,3,3])
    triin.holelist=[0.25 0.25; 0.65 0.65;]'
    area=@sprintf("%.15f",maxarea) # Don't use exponential format!
    angle=@sprintf("%.15f",minangle)
    (triout, vorout)=triangulate("pa$(area)q$(angle)Q", triin)

	pyplot() do
    plot_in_out(PyPlot,triin,triout,voronoi=vorout, title="Domain with holes")
	end
end;

# ╔═╡ 6545d149-d4dd-4f26-8f51-5db90f6b444d
example_domain_holes(minangle=20,maxarea=0.05)

# ╔═╡ Cell order:
# ╟─7b468c8a-ce16-11eb-3009-f9e4c79e6c17
# ╟─83f07f94-7e35-42de-9214-715c92b6dddd
# ╠═a5f7aca5-9e40-471a-bece-34498a804bd8
# ╠═d0e63ebd-9288-42d5-9735-3c94a2baa8e3
# ╟─c56434f3-e0a4-462e-86d5-fcfbb6778c11
# ╠═6f3f0014-42bc-4565-a03e-208fed8b8f48
# ╠═6ec811ca-e6d3-43ea-8da9-02bb05060d8d
# ╟─f13ad00e-a3d3-462f-93d4-ebe1fe4a7b4d
# ╠═6e2d70c4-dc0f-4672-823a-930a50114811
# ╠═0b54833f-0458-4417-aa03-27c4fe2a873c
# ╟─852e1ebf-3f29-472a-945e-53920d7653be
# ╠═1ee0af11-96e4-4929-926e-d7143ed1f791
# ╠═4da9a598-bf59-4b3b-aea7-ee1c2721c206
# ╟─b95859af-ab21-4c41-b095-82b3ea39724a
# ╠═0d648d22-aa03-437b-b019-59bd693bc55e
# ╠═910ce428-4989-4b08-8037-887dae5c847e
# ╟─c5a87fb5-c264-498d-aa7c-91d9e6d7bfe2
# ╟─7f2b4252-a827-4de4-abb6-42d1bbde44b9
# ╠═ccd8ed8d-7991-4a19-8a05-65761adc2fee
# ╠═fc5264cf-b1f7-43c5-bcff-09e6274ca215
# ╟─6793711b-d476-45c6-8b2b-b1d0693d8e6e
# ╠═b71b1faf-ff3f-4404-8a2c-9de84fa498f7
# ╠═cd007961-7f0e-4c56-9c86-1a9827a71e3e
# ╟─3f2f0b19-3c13-482d-abd5-e6325802c6e8
# ╠═8ac6cc0b-e4e7-4aa0-bc37-b22b15bf2a83
# ╠═a0cb6060-8278-444a-a4e5-46055d98616c
# ╟─14145886-2a5f-4ae8-bf00-a5eaaa7e0568
# ╠═a7e97f1c-e091-4555-abc0-a71abd22dd8a
# ╠═4e7dad4d-7d3c-4201-a56f-28e3df51e885
# ╟─61dc30ce-9256-4f20-9163-e8d473ac9e53
# ╠═083b8aef-67f1-489a-8eff-b70fb6dc9da4
# ╠═3c88e5cb-5e85-4146-a6d7-3682f4b9a892
# ╟─30990760-92e1-4a26-98a5-b43be658dee7
# ╠═f0f9922b-70ce-4866-bc41-349a26095ade
# ╠═58dbd0e3-b34f-44ec-905a-488c7bbd07ca
# ╟─783975f5-cbab-4b23-9155-7a6276cd25df
# ╠═3dfe5acd-72f7-4510-b3a0-40364f679073
# ╠═6545d149-d4dd-4f26-8f51-5db90f6b444d
# ╟─9a090bba-093b-4ca4-a186-1c43b52cd4ff
# ╠═8af90e7a-0e5e-4a9d-b39e-f7603fc1e25e
# ╠═9447e874-22ce-4b99-9037-e0d202430ee2


text:
using SplitApplyPlot
using Test

@testset "utils" begin
    v1 = [1, 2, 7, 11]
    v2 = [1, 3, 4, 5.1]
    @test SplitApplyPlot.mergesorted(v1, v2) == [1, 2, 3, 4, 5.1, 7, 11]
    @test SplitApplyPlot.mergesorted(v2, v1) == [1, 2, 3, 4, 5.1, 7, 11]
    @test_throws ArgumentError SplitApplyPlot.mergesorted([2, 1], [1, 3])
    @test_throws ArgumentError SplitApplyPlot.mergesorted([1, 2], [10, 3])

    e1 = (-3, 11)
    e2 = (-5, 10)
    @test SplitApplyPlot.extend_extrema(e1, e2) == (-5, 11)

    @test SplitApplyPlot.midpoints(1:10) == 1.5:9.5
end


text:
## ABSTRACT TYPE

abstract type StoppingCriterion end

## FALL BACK METHODS

# initialization call is either:
update(::StoppingCriterion, loss) = nothing # state

# ... or:
update_training(::StoppingCriterion, loss) = nothing # state

# subsequent updating:
update(::StoppingCriterion, loss, state) = state
update_training(::StoppingCriterion, loss, state) = state

# returns whether it's time to stop:
done(::StoppingCriterion, state) = false

message(criterion::StoppingCriterion, state) = "Stop triggered by "*
    "$criterion stopping criterion. "

needs_loss(::Type) = false
needs_training_losses(::Type) = false

for trait in [:needs_loss, :needs_training_losses]
    eval(:($trait(c) = $trait(typeof(c))))
end


text:
function op_V_loc( pw::PWGrid, V_loc, psi::Array{ComplexF64,2} )
    #
    Ns = pw.Ns
    Ω  = pw.Ω
    Npoints = prod(Ns)
    Ncols   = size(psi)[2]

    ctmp = zeros(ComplexF64, Npoints, Ncols)
    idx = pw.gvecw.idx_gw2r
    for ic = 1:Ncols
        ctmp[idx,ic] = psi[:,ic]
    end

    # get values of psi in real space grid via forward transform
    ctmp = G_to_R( Ns, ctmp )

    cVpsi = R_to_G( Ns, Diagprod(V_loc, ctmp) )
    return cVpsi[idx,:]
end

function op_V_loc( pw::PWGrid, V_loc, psi::Array{ComplexF64,1} )
    #
    Ns = pw.Ns
    Ω  = pw.Ω
    Npoints = prod(Ns)

    ctmp = zeros(ComplexF64, Npoints)
    idx = pw.gvecw.idx_gw2r
    ctmp[idx] = psi[:]

    # get values of psi in real space grid via forward transform
    ctmp = G_to_R( Ns, ctmp )

    cVpsi = R_to_G( Ns, V_loc .* ctmp )
    return cVpsi[idx]
end

# B is usually consists of more than one-column
function Diagprod( a, B::Array{ComplexF64,2} )
    Nstates = size(B)[2]
    Npoints = size(B)[1]
    out = zeros( ComplexF64, size(B) )
    for ist = 1:Nstates
        for ip = 1:Npoints
            out[ip,ist] = a[ip]*B[ip,ist]
        end
    end
    return out
end


text:
# This file is part of Kpax3. License is MIT.

# these tests must be run after "mcmc/posterior.jl"
function test_traceR()
  fileroot = "../build/mcmc_6"
  maxlag = 20

  (entropy, avgd) = Kpax3.traceR(fileroot, maxlag=maxlag)

  fpR = open(string(fileroot, "_row_partition.bin"), "r")

  tmp = zeros(Int, 1)

  read!(fpR, tmp)
  n = tmp[1]

  read!(fpR, tmp)
  read!(fpR, tmp)
  N = tmp[1]

  k = zeros(Int, N)
  R = zeros(Int, n, N)
  v = zeros(Int, n, N)

  e = 0.0

  k0 = zeros(Int, 1)
  R0 = zeros(Int, n)

  T = 1
  while !eof(fpR)
    read!(fpR, k0)
    read!(fpR, R0)

    k[T] = k0[1]
    copyto!(R, 1 + n * (T - 1), Kpax3.normalizepartition(R0, n), 1, n)

    for i in 1:n
      v[R[i, T], T] += 1
    end

    e = 0.0
    for g in 1:k[T]
      e -= v[g, T] * (log(v[g, T]) - log(n)) / n
    end

    @test isapprox(entropy[T], e, atol=ε)

    T += 1
  end

  close(fpR)

  # test lag 1
  l = 1
  z = zeros(Float64, N - l)
  for t in 1:(N - l)
    z[t] = Kpax3.jaccard(R[:, t], k[t], R[:, t+l], k[t+l], n)
  end
  @test isapprox(avgd[l], Statistics.mean(z), atol=ε)

  # test lag 5
  l = 5
  z = zeros(Float64, N - l)
  for t in 1:(N - l)
    z[t] = Kpax3.jaccard(R[:, t], k[t], R[:, t+l], k[t+l], n)
  end
  @test isapprox(avgd[l], Statistics.mean(z), atol=ε)

  # test lag 13
  l = 13
  z = zeros(Float64, N - l)
  for t in 1:(N - l)
    z[t] = Kpax3.jaccard(R[:, t], k[t], R[:, t+l], k[t+l], n)
  end
  @test isapprox(avgd[l], Statistics.mean(z), atol=ε)

  nothing
end

test_traceR()

function test_traceC()
  fileroot = "../build/mcmc_6"
  maxlag = 20

  (entropy, avgd) = Kpax3.traceC(fileroot, maxlag=maxlag)

  fpC = open(string(fileroot, "_col_partition.bin"), "r")

  tmp = zeros(Int, 1)

  read!(fpC, tmp)
  n = tmp[1]

  read!(fpC, tmp)
  m = tmp[1]

  read!(fpC, tmp)
  N = tmp[1]

  C = zeros(UInt8, m, N)
  e = 0.0

  C0 = zeros(UInt8, m)
  v0 = zeros(Float64, 3)

  T = 1
  while !eof(fpC)
    readbytes!(fpC, C0, m)
    copyto!(C, 1 + m * (T - 1), C0, 1, m)

    fill!(v0, 0.0)
    for b in 1:m
      v0[C[b, T]] += 1
    end
    v0 /= m

    e = 0.0
    if v0[1] > 0.0
      e -= v0[1] * log(v0[1])
    end

    if v0[2] > 0.0
      e -= v0[2] * log(v0[2])
    end

    if v0[3] > 0.0
      e -= v0[3] * log(v0[3])
    end

    @test isapprox(entropy[T], e, atol=ε)

    T += 1
  end

  close(fpC)

  # test lag 1
  l = 1
  z = zeros(Float64, N - l)
  for t in 1:(N - l)
    z[t] = Distances.hamming(C[:, t], C[:, t + l]) / m
  end
  @test isapprox(avgd[l], Statistics.mean(z), atol=ε)

  # test lag 5
  l = 5
  z = zeros(Float64, N - l)
  for t in 1:(N - l)
    z[t] = Distances.hamming(C[:, t], C[:, t + l]) / m
  end

  @test isapprox(avgd[l], Statistics.mean(z), atol=ε)

  # test lag 13
  l = 13
  z = zeros(Float64, N - l)
  for t in 1:(N - l)
    z[t] = Distances.hamming(C[:, t], C[:, t + l]) / m
  end

  @test isapprox(avgd[l], Statistics.mean(z), atol=ε)

  nothing
end

test_traceC()

function test_imsevar()
  fileroot = "../build/mcmc_6"
  maxlag = 20

  N = 100
  ac = [ 1.3916266386374644969947667050291784107685089111328125;
         0.58693069919258922251259491531527601182460784912109375;
         0.10875873500110790070838362453287118114531040191650390625;
         0.1399794990760882262836872769184992648661136627197265625;
        -0.06737442536689319805276454644626937806606292724609375;
        -0.282376530912865408851075699203647673130035400390625;
        -0.2376229639169518126351476894342340528964996337890625;
        -0.12117094585437278297934682314007659442722797393798828125;
        -0.3315812679619776215389492790563963353633880615234375;
        -0.2608754424296446661202253380906768143177032470703125;
         0.07139428050428296701479524699607281945645809173583984375;
        -0.056644446311564296270280038925193366594612598419189453125;
        -0.028541953982895558461496676727620069868862628936767578125;
         0.1310526295294208998232221574653522111475467681884765625;
         0.037620699765729091745836143445558263920247554779052734375;
        -0.2301652651239802782612287046504206955432891845703125;
        -0.063254641673751998975916421841247938573360443115234375;
         0.097048399599290402495199714394402690231800079345703125;
        -0.0192219724107511556743244085510013974271714687347412109375;
        -0.097383940885847675161812730948440730571746826171875;
        -0.0578078906440780693198888684491976164281368255615234375]

  z = 0.0306296450517703523763746176200584159232676029205322265625

  @test isapprox(Kpax3.imsevar(ac, N), z, atol=ε)

  nothing
end

test_imsevar()

function test_ess()
  @test Kpax3.ess(1.0, 1.0, 10) == 10
  @test Kpax3.ess(2.0, 2.0, 100) == 100

  @test Kpax3.ess(2.0, 4.0, 10) == 5
  @test Kpax3.ess(2.0, 4.0, 100) == 50

  @test Kpax3.ess(4.0, 2.0, 10) == 20
  @test Kpax3.ess(4.0, 2.0, 100) == 200

  @test Kpax3.ess(4.0, 12.0, 10) == 3
  @test Kpax3.ess(4.0, 12.0, 100) == 33

  @test Kpax3.ess(4.0, 16.0, 10) == 2
  @test Kpax3.ess(4.0, 16.0, 100) == 25

  nothing
end

test_ess()


text:
using Nuklear
using Nuklear.LibNuklear
using Nuklear.GLFWBackend
using GLFW, ModernGL

const WINDOW_WIDTH = 1200
const WINDOW_HEIGHT = 800

const MAX_VERTEX_BUFFER = 512 * 1024
const MAX_ELEMENT_BUFFER = 128 * 1024

@static if Sys.isapple()
    const VERSION_MAJOR = 3
    const VERSION_MINOR = 3
end

@static if Sys.isapple()
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, VERSION_MAJOR)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, VERSION_MINOR)
    GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE)
    GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE)
else
    GLFW.DefaultWindowHints()
end

# set up GLFW error callback
error_callback(err::GLFW.GLFWError) = @error "GLFW ERROR: code $(err.code) msg: $(err.description)"
GLFW.SetErrorCallback(error_callback)

# create window
win = GLFW.CreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo")
@assert win != C_NULL
GLFW.MakeContextCurrent(win)
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)

# init context
ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER)

# init font
nk_glfw3_font_stash_begin()
nk_glfw3_font_stash_end()

# window flags
show_menu = Ref{Cint}(true)
titlebar = Ref{Cint}(true)
border = Ref{Cint}(true)
resize = Ref{Cint}(true)
movable = Ref{Cint}(true)
no_scrollbar = Ref{Cint}(false)
scale_left = Ref{Cint}(false)
window_flags = nk_flags(0)
minimizable = Ref{Cint}(true)
# popups
show_app_about = Ref{Cint}(false)

# states
# menubar
mprog = Ref{nk_size}(60)
mslider = Ref{Cint}(10)
mcheck = Ref{Cint}(true)
prog = Ref{Csize_t}(40)
slider = Ref{Cint}(10)
check = Ref{Cint}(true)
@enum menu_state_enum MENU_NONE MENU_FILE MENU_EDIT MENU_VIEW MENU_CHART
menu_state = MENU_NONE
# widgets
checkbox = Ref{Cint}(0)
option = 0
int_slider = Ref{Cint}(5)
float_slider = Ref{Cfloat}(2.5)
prog_value = Ref{Csize_t}(40)
property_float = Ref{Cfloat}(2)
property_int = Ref{Cint}(10)
property_neg = Ref{Cint}(10)
range_float_min = Ref{Cfloat}(0)
range_float_max = Ref{Cfloat}(100)
range_float_value = Ref{Cfloat}(50)
range_int_min = Ref{Cint}(0)
range_int_value = Ref{Cint}(2048)
range_int_max = Ref{Cint}(4096)

# render loop
while !GLFW.WindowShouldClose(win)
    global show_menu; global titlebar; global border; global resize; global movable;
    global no_scrollbar; global scale_left; global window_flags; global minimizable
    global show_app_about; global int_slider; global float_slider; global prog_value;
    global property_float; global property_int; global property_neg;
    global range_float_min; global range_float_max; global range_float_value;
    global range_int_min; global range_int_value; global range_int_max;

    GLFW.PollEvents()
    nk_glfw3_new_frame()

    Bool(border[]) && (window_flags |= NK_WINDOW_BORDER;)
    Bool(resize[]) && (window_flags |= NK_WINDOW_SCALABLE;)
    Bool(movable[]) && (window_flags |= NK_WINDOW_MOVABLE;)
    Bool(no_scrollbar[]) && (window_flags |= NK_WINDOW_NO_SCROLLBAR;)
    Bool(scale_left[]) && (window_flags |= NK_WINDOW_SCALE_LEFT;)
    Bool(minimizable[]) && (window_flags |= NK_WINDOW_MINIMIZABLE;)

    if Bool(nk_begin(ctx, "Overview", nk_rect(10, 10, 400, 600), window_flags))
        if Bool(show_menu[])
            # menubar
            nk_menubar_begin(ctx)

            # menu #1
            nk_layout_row_begin(ctx, NK_STATIC, 25, 5)
            nk_layout_row_push(ctx, 45)
            if Bool(nk_menu_begin_label(ctx, "MENU", NK_TEXT_LEFT, nk_vec2(120, 200)))
                nk_layout_row_dynamic(ctx, 25, 1)
                Bool(nk_menu_item_label(ctx, "Hide", NK_TEXT_LEFT)) && (show_menu[] = false;)
                Bool(nk_menu_item_label(ctx, "About", NK_TEXT_LEFT)) && (show_app_about[] = true;)
                nk_progress(ctx, prog, 100, NK_MODIFIABLE)
                nk_slider_int(ctx, 0, slider, 16, 1)
                nk_checkbox_label(ctx, "check", check)
                nk_menu_end(ctx)
            end

            # menu #2
            global menu_state
            nk_layout_row_push(ctx, 60)
            if Bool(nk_menu_begin_label(ctx, "ADVANCED", NK_TEXT_LEFT, nk_vec2(200, 600)))
                menu_state = MENU_NONE
                state = Ref{nk_collapse_states}(NK_MINIMIZED)
                state[] = (menu_state == MENU_FILE) ? NK_MAXIMIZED : NK_MINIMIZED
                if Bool(nk_tree_state_push(ctx, NK_TREE_TAB, "FILE", state))
                    menu_state = MENU_FILE
                    nk_menu_item_label(ctx, "New", NK_TEXT_LEFT)
                    nk_menu_item_label(ctx, "Open", NK_TEXT_LEFT)
                    nk_menu_item_label(ctx, "Save", NK_TEXT_LEFT)
                    nk_menu_item_label(ctx, "Close", NK_TEXT_LEFT)
                    nk_menu_item_label(ctx, "Exit", NK_TEXT_LEFT)
                    nk_tree_pop(ctx)
                else
                    menu_state = (menu_state == MENU_FILE) ? MENU_NONE : menu_state
                end

                state[] = (menu_state == MENU_EDIT) ? NK_MAXIMIZED : NK_MINIMIZED
                if Bool(nk_tree_state_push(ctx, NK_TREE_TAB, "EDIT", state))
                    menu_state = MENU_EDIT
                    nk_menu_item_label(ctx, "Copy", NK_TEXT_LEFT)
                    nk_menu_item_label(ctx, "Delete", NK_TEXT_LEFT)
                    nk_menu_item_label(ctx, "Cut", NK_TEXT_LEFT)
                    nk_menu_item_label(ctx, "Paste", NK_TEXT_LEFT)
                    nk_tree_pop(ctx)
                else
                    menu_state = (menu_state == MENU_EDIT) ? MENU_NONE : menu_state
                end

                state[] = (menu_state == MENU_VIEW) ? NK_MAXIMIZED : NK_MINIMIZED
                if Bool(nk_tree_state_push(ctx, NK_TREE_TAB, "VIEW", state))
                    menu_state = MENU_VIEW
                    nk_menu_item_label(ctx, "About", NK_TEXT_LEFT)
                    nk_menu_item_label(ctx, "Options", NK_TEXT_LEFT)
                    nk_menu_item_label(ctx, "Customize", NK_TEXT_LEFT)
                    nk_tree_pop(ctx)
                else
                    menu_state = (menu_state == MENU_VIEW) ? MENU_NONE : menu_state
                end

                state[] = (menu_state == MENU_CHART) ? NK_MAXIMIZED : NK_MINIMIZED
                if Bool(nk_tree_state_push(ctx, NK_TREE_TAB, "CHART", state))
                    values = Cfloat[26,13,30,15,25,10,20,40,12,8,22,28]
                    menu_state = MENU_CHART
                    nk_layout_row_dynamic(ctx, 150, 1)
                    nk_chart_begin(ctx, NK_CHART_COLUMN, length(values), 0, 50)
                    for i = 1:length(values)
                        nk_chart_push(ctx, values[i])
                    end
                    nk_chart_end(ctx)
                    nk_tree_pop(ctx)
                else
                    menu_state = (menu_state == MENU_CHART) ? MENU_NONE : menu_state
                end
                nk_menu_end(ctx)
            end
            # menu widgets
            nk_layout_row_push(ctx, 70)
            nk_progress(ctx, mprog, 100, NK_MODIFIABLE)
            nk_slider_int(ctx, 0, mslider, 16, 1)
            nk_checkbox_label(ctx, "check", mcheck)
            nk_menubar_end(ctx)
        end

        # about popup
        if Bool(show_app_about[])
           s = nk_rect(20, 100, 300, 190)
           if Bool(nk_popup_begin(ctx, NK_POPUP_STATIC, "About", NK_WINDOW_CLOSABLE, s))
               nk_layout_row_dynamic(ctx, 20, 1)
               nk_label(ctx, "Nuklear", NK_TEXT_LEFT)
               nk_label(ctx, "By Micha Mettke", NK_TEXT_LEFT)
               nk_label(ctx, "nuklear is licensed under the public domain License.", NK_TEXT_LEFT)
               nk_popup_end(ctx)
           else
               show_app_about = false
           end
        end

        # window flags
        if Bool(nk_tree_push(ctx, NK_TREE_TAB, "Window", NK_MINIMIZED))
            nk_layout_row_dynamic(ctx, 30, 2)
            nk_checkbox_label(ctx, "Titlebar", titlebar)
            nk_checkbox_label(ctx, "Menu", show_menu)
            nk_checkbox_label(ctx, "Border", border)
            nk_checkbox_label(ctx, "Resizable", resize)
            nk_checkbox_label(ctx, "Movable", movable)
            nk_checkbox_label(ctx, "No Scrollbar", no_scrollbar)
            nk_checkbox_label(ctx, "Minimizable", minimizable)
            nk_checkbox_label(ctx, "Scale Left", scale_left)
            nk_tree_pop(ctx)
        end

        # if Bool(nk_tree_push(ctx, NK_TREE_TAB, "Widgets", NK_MINIMIZED))
        #     @enum options A B C
        #     global checkbox
        #     global option
        #     if Bool(nk_tree_push(ctx, NK_TREE_NODE, "Text", NK_MINIMIZED))
        #         # Text Widgets
        #         nk_layout_row_dynamic(ctx, 20, 1)
        #         nk_label(ctx, "Label aligned left", NK_TEXT_LEFT)
        #         nk_label(ctx, "Label aligned centered", NK_TEXT_CENTERED)
        #         nk_label(ctx, "Label aligned right", NK_TEXT_RIGHT)
        #         nk_label_colored(ctx, "Blue text", NK_TEXT_LEFT, nk_rgb(0,0,255))
        #         nk_label_colored(ctx, "Yellow text", NK_TEXT_LEFT, nk_rgb(255,255,0))
        #         nk_text(ctx, "Text without /0", 15, NK_TEXT_RIGHT)
        #
        #         nk_layout_row_static(ctx, 100, 200, 1)
        #         nk_label_wrap(ctx, "This is a very long line to hopefully get this text to be wrapped into multiple lines to show line wrapping")
        #         nk_layout_row_dynamic(ctx, 100, 1)
        #         nk_label_wrap(ctx, "This is another long text to show dynamic window changes on multiline text")
        #         nk_tree_pop(ctx)
        #     end
        #
        #     if Bool(nk_tree_push(ctx, NK_TREE_NODE, "Button", NK_MINIMIZED))
        #         # Buttons Widgets
        #         nk_layout_row_static(ctx, 30, 100, 3)
        #         Bool(nk_button_label(ctx, "Button")) && println("Button pressed!")
        #         nk_button_set_behavior(ctx, NK_BUTTON_REPEATER)
        #         Bool(nk_button_label(ctx, "Repeater")) && println("Repeater is being pressed!")
        #         nk_button_set_behavior(ctx, NK_BUTTON_DEFAULT)
        #         nk_button_color(ctx, nk_rgb(0,0,255))
        #
        #         nk_layout_row_static(ctx, 25, 25, 8)
        #         nk_button_symbol(ctx, NK_SYMBOL_CIRCLE_SOLID)
        #         nk_button_symbol(ctx, NK_SYMBOL_CIRCLE_OUTLINE)
        #         nk_button_symbol(ctx, NK_SYMBOL_RECT_SOLID)
        #         nk_button_symbol(ctx, NK_SYMBOL_RECT_OUTLINE)
        #         nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_UP)
        #         nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_DOWN)
        #         nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_LEFT)
        #         nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_RIGHT)
        #
        #         nk_layout_row_static(ctx, 30, 100, 2)
        #         nk_button_symbol_label(ctx, NK_SYMBOL_TRIANGLE_LEFT, "prev", NK_TEXT_RIGHT)
        #         nk_button_symbol_label(ctx, NK_SYMBOL_TRIANGLE_RIGHT, "next", NK_TEXT_LEFT)
        #         nk_tree_pop(ctx)
        #     end
        #
        #     if Bool(nk_tree_push(ctx, NK_TREE_NODE, "Basic", NK_MINIMIZED))
        #         # Basic widgets
        #         ratio = Cfloat[120, 150]
        #
        #         nk_layout_row_static(ctx, 30, 100, 1)
        #         nk_checkbox_label(ctx, "Checkbox", checkbox)
        #
        #         nk_layout_row_static(ctx, 30, 80, 3)
        #         option = Bool(nk_option_label(ctx, "optionA", option == A)) ? A : option
        #         option = Bool(nk_option_label(ctx, "optionB", option == B)) ? B : option
        #         option = Bool(nk_option_label(ctx, "optionC", option == C)) ? C : option
        #
        #         # nk_layout_row(ctx, NK_STATIC, 30, 2, ratio)
        #         # nk_labelf(ctx, NK_TEXT_LEFT, "Slider int")
        #         # nk_slider_int(ctx, 0, int_slider, 10, 1)
        #         #
        #         # nk_label(ctx, "Slider float", NK_TEXT_LEFT)
        #         # nk_slider_float(ctx, 0, float_slider, 5.0, 0.5)
        #         # nk_labelf(ctx, NK_TEXT_LEFT, "Progressbar: %zu" , prog_value[])
        #         # nk_progress(ctx, prog_value, 100, NK_MODIFIABLE)
        #
        #         nk_layout_row(ctx, NK_STATIC, 25, 2, ratio)
        #         nk_label(ctx, "Property float:", NK_TEXT_LEFT)
        #         nk_property_float(ctx, "Float:", 0, property_float, 64.0, 0.1, 0.2)
        #         nk_label(ctx, "Property int:", NK_TEXT_LEFT)
        #         nk_property_int(ctx, "Int:", 0, property_int, 100.0, 1, 1)
        #         nk_label(ctx, "Property neg:", NK_TEXT_LEFT)
        #         nk_property_int(ctx, "Neg:", -10, property_neg, 10, 1, 1)
        #
        #         nk_layout_row_dynamic(ctx, 25, 1)
        #         nk_label(ctx, "Range:", NK_TEXT_LEFT)
        #         nk_layout_row_dynamic(ctx, 25, 3)
        #         nk_property_float(ctx, "#min:", 0, range_float_min, range_float_max[], 1.0, 0.2)
        #         nk_property_float(ctx, "#float:", range_float_min[], range_float_value, range_float_max[], 1.0, 0.2)
        #         nk_property_float(ctx, "#max:", range_float_min[], range_float_max, 100, 1.0, 0.2)
        #
        #         nk_property_int(ctx, "#min:", INT_MIN, range_int_min, range_int_max[], 1, 10)
        #         nk_property_int(ctx, "#neg:", range_int_min[], range_int_value, range_int_max[], 1, 10)
        #         nk_property_int(ctx, "#max:", range_int_min[], range_int_max, typemax(Cint), 1, 10)
        #
        #         nk_tree_pop(ctx)
        #     end
        # end
    end
    nk_end(ctx)

    # draw
    width, height = GLFW.GetWindowSize(win)
    glViewport(0, 0, width, height)
    glClear(GL_COLOR_BUFFER_BIT)
    # glClearColor(bg.r, bg.g, bg.b, bg.a)
    nk_glfw3_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER)
    GLFW.SwapBuffers(win)
end

nk_glfw3_shutdown()
GLFW.DestroyWindow(win)


text:
# ------------ Identifier alias and APIs ------------ #

const Identifier = MLIR.API.MlirIdentifier

get_name(op::Operation) = MLIR.API.mlirOperationGetName(uwnrap(op))
create_identifier(ctx::Context, str::StringRef) = MLIR.API.mlirIdentifierGet(ctx, str)
Base.:(==)(id1::Identifier, id2::Identifier) = MLIR.API.mlirIdentifierEqual(id1, id2)
get_str(id::Identifier) = MLIR.API.mlirIdentifierStr(id)

# Constructor.
Identifier(ctx::Context, str::String) = create_identifier(ctx, StringRef(str))

@doc(
"""
const Identifier = MLIR.API.MlirIdentifier
""", Identifier)


text:
function mse( x, y, dim )
    return mean( ( x - y ).^ 2, dim )
end

function mse( x, y )
    return mean( ( x - y ).^ 2 )
end

max_stars_repo_name: tbepler/julia-regression

max_issues_repo_name: tbepler/julia-regression

max_forks_repo_name: tbepler/julia-regression


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

# definitions related to C interface

import Core.Intrinsics: cglobal, box

cfunction(f::Function, r, a) = ccall(:jl_function_ptr, Ptr{Void}, (Any, Any, Any), f, r, a)

if ccall(:jl_is_char_signed, Ref{Bool}, ())
    typealias Cchar Int8
else
    typealias Cchar UInt8
end
"""
    Cchar

Equivalent to the native `char` c-type.
"""
Cchar

if is_windows()
    typealias Clong Int32
    typealias Culong UInt32
    typealias Cwchar_t UInt16
else
    typealias Clong Int
    typealias Culong UInt
    typealias Cwchar_t Int32
end
"""
    Clong

Equivalent to the native `signed long` c-type.
"""
Clong
"""
    Culong

Equivalent to the native `unsigned long` c-type.
"""
Culong
"""
    Cwchar_t

Equivalent to the native `wchar_t` c-type (`Int32`).
"""
Cwchar_t

if !is_windows()
    const sizeof_mode_t = ccall(:jl_sizeof_mode_t, Cint, ())
    if sizeof_mode_t == 2
        typealias Cmode_t Int16
    elseif sizeof_mode_t == 4
        typealias Cmode_t Int32
    elseif sizeof_mode_t == 8
        typealias Cmode_t Int64
    end
end

# construction from typed pointers
convert{T<:Union{Int8,UInt8}}(::Type{Cstring}, p::Ptr{T}) = box(Cstring, p)
convert(::Type{Cwstring}, p::Ptr{Cwchar_t}) = box(Cwstring, p)
convert{T<:Union{Int8,UInt8}}(::Type{Ptr{T}}, p::Cstring) = box(Ptr{T}, p)
convert(::Type{Ptr{Cwchar_t}}, p::Cwstring) = box(Ptr{Cwchar_t}, p)

# construction from untyped pointers
convert{T<:Union{Cstring,Cwstring}}(::Type{T}, p::Ptr{Void}) = box(T, p)

pointer(p::Cstring) = convert(Ptr{UInt8}, p)
pointer(p::Cwstring) = convert(Ptr{Cwchar_t}, p)

# comparisons against pointers (mainly to support `cstr==C_NULL`)
==(x::Union{Cstring,Cwstring}, y::Ptr) = pointer(x) == y
==(x::Ptr, y::Union{Cstring,Cwstring}) = x == pointer(y)

# here, not in pointer.jl, to avoid bootstrapping problems in coreimg.jl
unsafe_wrap(::Type{String}, p::Cstring, own::Bool=false) = unsafe_wrap(String, convert(Ptr{UInt8}, p), own)
unsafe_wrap(::Type{String}, p::Cstring, len::Integer, own::Bool=false) =
    unsafe_wrap(String, convert(Ptr{UInt8}, p), len, own)
unsafe_string(s::Cstring) = unsafe_string(convert(Ptr{UInt8}, s))

# convert strings to String etc. to pass as pointers
cconvert(::Type{Cstring}, s::String) =
    ccall(:jl_array_cconvert_cstring, Ref{Vector{UInt8}},
          (Vector{UInt8},), s.data)
cconvert(::Type{Cstring}, s::AbstractString) =
    cconvert(Cstring, String(s)::String)

function cconvert(::Type{Cwstring}, s::AbstractString)
    v = transcode(Cwchar_t, String(s).data)
    !isempty(v) && v[end] == 0 || push!(v, 0)
    return v
end

eltype(::Type{Cstring}) = UInt8
eltype(::Type{Cwstring}) = Cwchar_t

containsnul(p::Ptr, len) =
    C_NULL != ccall(:memchr, Ptr{Cchar}, (Ptr{Cchar}, Cint, Csize_t), p, 0, len)
containsnul(s::String) = containsnul(unsafe_convert(Ptr{Cchar}, s), sizeof(s))
containsnul(s::AbstractString) = '\0' in s

function unsafe_convert(::Type{Cstring}, s::Vector{UInt8})
    p = unsafe_convert(Ptr{Cchar}, s)
    containsnul(p, sizeof(s)) &&
        throw(ArgumentError("embedded NULs are not allowed in C strings: $(repr(s))"))
    return Cstring(p)
end

function unsafe_convert(::Type{Cwstring}, v::Vector{Cwchar_t})
    for i = 1:length(v)-1
        v[i] == 0 &&
            throw(ArgumentError("embedded NULs are not allowed in C strings: $(repr(v))"))
    end
    v[end] == 0 ||
        throw(ArgumentError("C string data must be NUL terminated: $(repr(v))"))
    p = unsafe_convert(Ptr{Cwchar_t}, v)
    return Cwstring(p)
end

# symbols are guaranteed not to contain embedded NUL
convert(::Type{Cstring}, s::Symbol) = Cstring(unsafe_convert(Ptr{Cchar}, s))

if is_windows()
"""
    Base.cwstring(s)

Converts a string `s` to a NUL-terminated `Vector{Cwchar_t}`, suitable for passing to C
functions expecting a `Ptr{Cwchar_t}`. The main advantage of using this over the implicit
conversion provided by `Cwstring` is if the function is called multiple times with the
same argument.

This is only available on Windows.
"""
function cwstring(s::AbstractString)
    bytes = String(s).data
    0 in bytes && throw(ArgumentError("embedded NULs are not allowed in C strings: $(repr(s))"))
    return push!(transcode(UInt16, bytes), 0)
end
end

# transcoding between data in UTF-8 and UTF-16 for Windows APIs,
# and also UTF-32 for APIs using Cwchar_t on other platforms.

"""
    transcode(T, src)

Convert string data between Unicode encodings. `src` is either a
`String` or a `Vector{UIntXX}` of UTF-XX code units, where
`XX` is 8, 16, or 32. `T` indicates the encoding of the return value:
`String` to return a (UTF-8 encoded) `String` or `UIntXX`
to return a `Vector{UIntXX}` of UTF-`XX` data.   (The alias `Cwchar_t`
can also be used as the integer type, for converting `wchar_t*` strings
used by external C libraries.)

The `transcode` function succeeds as long as the input data can be
reasonably represented in the target encoding; it always succeeds for
conversions between UTF-XX encodings, even for invalid Unicode data.

Only conversion to/from UTF-8 is currently supported.
"""
function transcode end

transcode{T<:Union{UInt8,UInt16,UInt32,Int32}}(::Type{T}, src::Vector{T}) = src
transcode{T<:Union{Int32,UInt32}}(::Type{T}, src::String) = T[T(c) for c in src]
transcode{T<:Union{Int32,UInt32}}(::Type{T}, src::Vector{UInt8}) = transcode(T, String(src))
function transcode{S<:Union{Int32,UInt32}}(::Type{UInt8}, src::Vector{S})
    buf = IOBuffer()
    for c in src; print(buf, Char(c)); end
    take!(buf)
end
transcode(::Type{String}, src::String) = src
transcode(T, src::String) = transcode(T, src.data)
transcode(::Type{String}, src) = String(transcode(UInt8, src))

function transcode(::Type{UInt16}, src::Vector{UInt8})
    dst = UInt16[]
    i, n = 1, length(src)
    n > 0 || return dst
    sizehint!(dst, 2n)
    a = src[1]
    while true
        if i < n && -64 <= a % Int8 <= -12 # multi-byte character
            b = src[i += 1]
            if -64 <= (b % Int8) || a == 0xf4 && 0x8f < b
                # invalid UTF-8 (non-continuation or too-high code point)
                push!(dst, a)
                a = b; continue
            elseif a < 0xe0 # 2-byte UTF-8
                push!(dst, xor(0x3080, UInt16(a) << 6, b))
            elseif i < n # 3/4-byte character
                c = src[i += 1]
                if -64 <= (c % Int8) # invalid UTF-8 (non-continuation)
                    push!(dst, a, b)
                    a = c; continue
                elseif a < 0xf0 # 3-byte UTF-8
                    push!(dst, xor(0x2080, UInt16(a) << 12, UInt16(b) << 6, c))
                elseif i < n
                    d = src[i += 1]
                    if -64 <= (d % Int8) # invalid UTF-8 (non-continuation)
                        push!(dst, a, b, c)
                        a = d; continue
                    elseif a == 0xf0 && b < 0x90 # overlong encoding
                        push!(dst, xor(0x2080, UInt16(b) << 12, UInt16(c) << 6, d))
                    else # 4-byte UTF-8
                        push!(dst, 0xe5b8 + (UInt16(a) << 8) + (UInt16(b) << 2) + (c >> 4),
                                   xor(0xdc80, UInt16(c & 0xf) << 6, d))
                    end
                else # too short
                    push!(dst, a, b, c)
                    break
                end
            else # too short
                push!(dst, a, b)
                break
            end
        else # ASCII or invalid UTF-8 (continuation byte or too-high code point)
            push!(dst, a)
        end
        i < n || break
        a = src[i += 1]
    end
    return dst
end

function transcode(::Type{UInt8}, src::Vector{UInt16})
    n = length(src)
    n == 0 && return UInt8[]

    # Precompute m = sizeof(dst).   This involves annoying duplication
    # of the loop over the src array.   However, this is not just an
    # optimization: it is problematic for security reasons to grow
    # dst dynamically, because Base.winprompt uses this function to
    # convert passwords to UTF-8 and we don't want to make unintentional
    # copies of the password data.
    a = src[1]
    i, m = 1, 0
    while true
        if a < 0x80
            m += 1
        elseif a < 0x800 # 2-byte UTF-8
            m += 2
        elseif a & 0xfc00 == 0xd800 && i < length(src)
            b = src[i += 1]
            if (b & 0xfc00) == 0xdc00 # 2-unit UTF-16 sequence => 4-byte UTF-8
                m += 4
            else
                m += 3
                a = b; continue
            end
        else
            # 1-unit high UTF-16 or unpaired high surrogate
            # either way, encode as 3-byte UTF-8 code point
            m += 3
        end
        i < n || break
        a = src[i += 1]
    end

    dst = Array{UInt8}(m)
    a = src[1]
    i, j = 1, 0
    while true
        if a < 0x80 # ASCII
            dst[j += 1] = a % UInt8
        elseif a < 0x800 # 2-byte UTF-8
            dst[j += 1] = 0xc0 | ((a >> 6) % UInt8)
            dst[j += 1] = 0x80 | ((a % UInt8) & 0x3f)
        elseif a & 0xfc00 == 0xd800 && i < n
            b = src[i += 1]
            if (b & 0xfc00) == 0xdc00
                # 2-unit UTF-16 sequence => 4-byte UTF-8
                a += 0x2840
                dst[j += 1] = 0xf0 | ((a >> 8) % UInt8)
                dst[j += 1] = 0x80 | ((a % UInt8) >> 2)
                dst[j += 1] = xor(0xf0, ((a % UInt8) << 4) & 0x3f, (b >> 6) % UInt8)
                dst[j += 1] = 0x80 | ((b % UInt8) & 0x3f)
            else
                dst[j += 1] = 0xe0 | ((a >> 12) % UInt8)
                dst[j += 1] = 0x80 | (((a >> 6) % UInt8) & 0x3f)
                dst[j += 1] = 0x80 | ((a % UInt8) & 0x3f)
                a = b; continue
            end
        else
            # 1-unit high UTF-16 or unpaired high surrogate
            # either way, encode as 3-byte UTF-8 code point
            dst[j += 1] = 0xe0 | ((a >> 12) % UInt8)
            dst[j += 1] = 0x80 | (((a >> 6) % UInt8) & 0x3f)
            dst[j += 1] = 0x80 | ((a % UInt8) & 0x3f)
        end
        i < n || break
        a = src[i += 1]
    end
    return dst
end

# deferring (or un-deferring) ctrl-c handler for external C code that
# is not interrupt safe (see also issue #2622).  The sigatomic_begin/end
# functions should always be called in matched pairs, ideally via:
#            disable_sigint() do .. end
# reennable_sigint is provided so that immediate ctrl-c handling is
# re-enabled within a sigatomic region, e.g. inside a Julia callback function
# within a long-running C routine.
sigatomic_begin() = ccall(:jl_sigatomic_begin, Void, ())
sigatomic_end() = ccall(:jl_sigatomic_end, Void, ())

"""
    disable_sigint(f::Function)

Disable Ctrl-C handler during execution of a function on the current task,
for calling external code that may call julia code that is not interrupt safe.
Intended to be called using `do` block syntax as follows:

    disable_sigint() do
        # interrupt-unsafe code
        ...
    end

This is not needed on worker threads (`Threads.threadid() != 1`) since the
`InterruptException` will only be delivered to the master thread.
External functions that do not call julia code or julia runtime
automatically disable sigint during their execution.
"""
function disable_sigint(f::Function)
    sigatomic_begin()
    res = f()
    # Exception unwind sigatomic automatically
    sigatomic_end()
    res
end

"""
    reenable_sigint(f::Function)

Re-enable Ctrl-C handler during execution of a function.
Temporarily reverses the effect of `disable_sigint`.
"""
function reenable_sigint(f::Function)
    sigatomic_end()
    res = f()
    # Exception unwind sigatomic automatically
    sigatomic_begin()
    res
end

function ccallable(f::Function, rt::Type, argt::Type, name::Union{AbstractString,Symbol}=string(f))
    ccall(:jl_extern_c, Void, (Any, Any, Any, Cstring), f, rt, argt, name)
end

macro ccallable(rt, def)
    if isa(def,Expr) && (def.head === :(=) || def.head === :function)
        sig = def.args[1]
        if sig.head === :call
            name = sig.args[1]
            at = map(sig.args[2:end]) do a
                if isa(a,Expr) && a.head === :(::)
                    a.args[2]
                else
                    :Any
                end
            end
            return quote
                $(esc(def))
                ccallable($(esc(name)), $(esc(rt)), $(Expr(:curly, :Tuple, map(esc, at)...)), $(string(name)))
            end
        end
    end
    error("expected method definition in @ccallable")
end

max_stars_repo_name: habemus-papadum/julia

max_issues_repo_name: habemus-papadum/julia

max_forks_repo_name: habemus-papadum/julia


text:
# some implementations for convenience
# maintained by Zach Sunberg

actions{S}(mdp::MDP{S,Bool}) = (true, false)
actions{S}(mdp::MDP{S,Bool}, s::S, t::Tuple{Bool,Bool}=(true,false)) = (true,false)
actions{S,O}(mdp::POMDP{S,Bool,O}) = (true, false)
actions{S,O}(mdp::POMDP{S,Bool,O}, s::S, t::Tuple{Bool,Bool}=(true,false)) = (true,false)
actions{S,O}(mdp::RPOMDP{S,Bool,O}) = (true, false)
actions{S,O}(mdp::RPOMDP{S,Bool,O}, s::S, t::Tuple{Bool,Bool}=(true,false)) = (true,false)
n_actions{S}(mdp::MDP{S,Bool}) = 2
n_actions{S,O}(mdp::POMDP{S,Bool,O}) = 2
n_actions{S,O}(mdp::RPOMDP{S,Bool,O}) = 2

actions{S}(mdp::MDP{S,Int}, s::S, r::Range) = actions(mdp)

rand(rng::AbstractRNG, t::Tuple{Bool, Bool}) = rand(rng, Bool)
rand(t::Tuple{Bool, Bool}) = rand(Bool)

iterator(s::AbstractVector) = s
iterator(s::Tuple) = s
iterator(r::Range) = r
iterator(g::Base.Generator) = g

states(mdp::MDP{Bool}) = (true, false)
states(mdp::POMDP{Bool}) = (true, false)
states(mdp::RPOMDP{Bool}) = (true, false)
n_states(mdp::MDP{Bool}) = 2
n_states(mdp::POMDP{Bool}) = 2
n_states(mdp::RPOMDP{Bool}) = 2

observations{S,A}(::POMDP{S,A,Bool}) = (true,false)
observations{S,A}(::POMDP{S,A,Bool}, s::S) = (true,false)
observations{S,A}(::RPOMDP{S,A,Bool}) = (true,false)
observations{S,A}(::RPOMDP{S,A,Bool}, s::S) = (true,false)
n_observations{S,A}(::POMDP{S,A,Bool}) = 2
n_observations{S,A}(::RPOMDP{S,A,Bool}) = 2

state_index(mdp::Union{MDP, POMDP, RPOMDP}, s::Int) = s
action_index(mdp::Union{MDP, POMDP, RPOMDP}, a::Int) = a
obs_index(mdp::Union{MDP, POMDP, RPOMDP}, o::Int) = o

state_index(mdp::Union{MDP, POMDP, RPOMDP}, s::Bool) = s+1
action_index(mdp::Union{MDP, POMDP, RPOMDP}, a::Bool) = a+1
obs_index(mdp::Union{MDP, POMDP, RPOMDP}, o::Bool) = o+1


text:
using Lattices

@testset "Square Lattice" begin
    a = rand([1, -1], 2, 2)
    lat_a = SquareLattice(a)
    @testset "Constructor" begin
        @test (typeof(lat_a) === SquareLattice)
        # print_lat(lat_a)

        b = rand([0, 2], 2, 2)
        @test_throws AssertionError lat_b = SquareLattice(b)

        c = rand([1, -1], 2, 3)
        @test_throws AssertionError lat_c=SquareLattice(c)

    end

    @testset "Flipping one spin" begin
        # a = rand([1, -1], 2, 2)
        # lat_a = SquareLattice(a)
        lat_a2 = deepcopy(lat_a)
        # print_lat(lat_a)

        flip_pos = (1,1)
        flip!(lat_a2, (flip_pos))
        # print_lat(lat_a)
        @test (lat_a.point_list[flip_pos...]+lat_a2.point_list[flip_pos...]==0)
    end

    @testset "Get spins" begin
        position_list = [(1,1), (1,2), (2,2), (2,1)]
        spins_list = get_spins(lat_a, position_list)
        @test all(
            [spins_list[i]==lat_a.point_list[position_list[i]...] for i in length(position_list)]
            )
    end

end


text:
module Hydrographs

using Dates: Date
using DataStructures: OrderedDict
using DataFrames: DataFrame, outerjoin, sort!
using CSV: File
using JSON: parse
using VegaLite: VLSpec

export hydrograph, dataset

function read_template()
    path = joinpath(@__DIR__, "template.json")
    template = parse(open(path), dicttype=OrderedDict)
    return template
end

vl_template = read_template()

function set_options!(vlspec, kwargs)
    if :width in keys(kwargs)
        for panel in vlspec["vconcat"]
            panel["width"] = kwargs[:width]
        end
    end
    if :aggregate in keys(kwargs)
        if kwargs[:aggregate] == "monthly"
            vlspec["vconcat"][end]["encoding"]["x"]["timeUnit"] = "yearmonth"
            vlspec["vconcat"][end]["encoding"]["y"]["aggregate"] = "sum"
        elseif kwargs[:aggregate] == "weekly"
            vlspec["vconcat"][end]["encoding"]["x"]["timeUnit"] = "yearweek"
            vlspec["vconcat"][end]["encoding"]["y"]["aggregate"] = "sum"
        end
    end
    if get(kwargs, :logscale, false)
        vlspec["vconcat"][end-1]["encoding"]["y"]["scale"]["type"] = "log"
        vlspec["vconcat"][end]["encoding"]["y"]["scale"]["type"] = "log"
    end
end

"""
    hydrograph(data::DataFrame, T::AbstractString,
               Q::AbstractString; kwargs...)::VLSpec

Plot a hydrograph with two columns in a dataframe.

### Arguments
* `data` : A dataframe
* `T` : Column name for time X axis
* `Q` : Column name for streamlow Y axis

### Examples
```julia
julia> hydrograph(df, "Date", "Flow"; width=1000)
```
"""
function hydrograph(data::DataFrame, T::AbstractString,
                    Q::AbstractString; kwargs...)::VLSpec
    vlspec = deepcopy(vl_template)

    popfirst!(vlspec["vconcat"])

    for panel in vlspec["vconcat"]
        panel["encoding"]["x"]["field"] = T
        panel["encoding"]["y"]["field"] = Q
    end

    set_options!(vlspec, kwargs)

    plt = data |> VLSpec(vlspec)
    return plt
end

"""
    hydrograph(data::DataFrame, T::AbstractString,
               Q::AbstractString, P::AbstractString; kwargs...)::VLSpec

Plot a hydrograph with three columns in a dataframe.

### Arguments
* `data` : A dataframe
* `T` : Column name for time X axis
* `Q` : Column name for streamlow Y axis
* `P` : Column name for rainfall Y axis

### Examples
```julia
julia> hydrograph(df, "Date", "Flow", "Rainfall"; width=1000)
```
"""
function hydrograph(data::DataFrame, T::AbstractString,
                    Q::AbstractString, P::AbstractString; kwargs...)::VLSpec
    vlspec = deepcopy(vl_template)

    for panel in vlspec["vconcat"]
        panel["encoding"]["x"]["field"] = T
    end
    vlspec["vconcat"][1]["encoding"]["y"]["field"] = P
    vlspec["vconcat"][2]["encoding"]["y"]["field"] = Q
    vlspec["vconcat"][3]["encoding"]["y"]["field"] = Q

    set_options!(vlspec, kwargs)

    plt = data |> VLSpec(vlspec)
    return plt
end

"""
    hydrograph(data::DataFrame; kwargs...)::VLSpec

Plot a hydrograph with default columns in a dataframe.
The default name for time is "Dates" and the default name for streamflow data is "Flow".

### Arguments
* `data` : A dataframe

### Examples
```julia
julia> hydrograph(df; width=1000)
```
"""
function hydrograph(data::DataFrame; kwargs...)::VLSpec
    T, Q = names(data)[1:2]
    return hydrograph(data, T, Q; kwargs...)
end

"""
    hydrograph(data::DataFrame; kwargs...)::VLSpec

Plot a hydrograph with default columns in a dataframe.
The default name for time is "Dates" and the default name for streamflow data is "Flow".

### Arguments
* `data` : A dataframe

### Examples
```julia
julia> hydrograph(df; width=1000)
```
"""
function hydrograph(data::DataFrame, it::Int, iq::Int; kwargs...)::VLSpec
    T, Q = names(data)[[it, iq]]
    return hydrograph(data, T, Q; kwargs...)
end

"""
    hydrograph(data::DataFrame, it::Int, iq::Int, ip::Int; kwargs...)::VLSpec

Plot a hydrograph with a dataframe and column numbers.

### Arguments
* `data` : A dataframe
* `it` : Column number for time X axis
* `iq` : Column number for streamflow Y axis
* `ip` : Column number for rainfall Y axis

### Examples
```julia
julia> hydrograph(df, 1, 2, 4; width=1000)
```
"""
function hydrograph(data::DataFrame, it::Int, iq::Int, ip::Int; kwargs...)::VLSpec
    T, Q, P = names(data)[[it, iq, ip]]
    return hydrograph(data, T, Q, P; kwargs...)
end

"""
    hydrograph(T::Array{Date,1}, Q::Array{Float64,1},
               P::Array{Float64,1}; kwargs...)::VLSpec

Plot a hydrograph with three arrays. All three arrays should have the same length.

### Arguments
* `T` : Array for time X axis
* `Q` : Array for streamflow Y axis
* `P` : Array for rainfall Y axis

### Examples
```julia
julia> hydrograph(df, 1, 2, 4; width=1000)
```
"""
function hydrograph(T::Array{Date,1}, Q::Array{Float64,1},
                    P::Array{Float64,1}; kwargs...)::VLSpec
    length(T) == length(Q) || throw(DimensionMismatch("Date and streamflow arrays have different lengths."))
    length(T) == length(P) || throw(DimensionMismatch("Date and rainfall arrays have different lengths."))

    data = DataFrame("Date"=>T, "Flow"=>Q, "Rainfall"=>P)
    return hydrograph(data, "Date", "Flow", "Rainfall"; kwargs...)
end

"""
    hydrograph(Q::DataFrame, P::DataFrame; kwargs...)::VLSpec

Plot a hydrograph with two dataframes. The two dataframes can have different periods.

### Arguments
* `Q` : A dataframe with "Date" and "Flow" columns
* `P` : A dtaaframe with "Date" and "Rainfall" columns

### Examples
```julia
julia> hydrograph(df_q, df_p; width=1000)
```
"""
function hydrograph(Q::DataFrame, P::DataFrame; kwargs...)::VLSpec
    data = outerjoin(Q, P, on="Date")
    sort!(data, "Date")
    return hydrograph(data, "Date", names(Q)[2], names(P)[2]; kwargs...)
end

"""
    dataset(name::AbstractString)::DataFrame

Return a sample dataset.

### Arguments
* `name` : Dataset name e.g. "doherty"

### Examples
```julia
julia> data = dataset("doherty")
```
"""
function dataset(name::AbstractString)::DataFrame
    data_path = joinpath(@__DIR__, "..", "data", string(name, ".csv"))

    ispath(data_path) || error("Unable to locate dataset file $data_path")
    isfile(data_path) || error("Not a file, $data_path")

    return DataFrame(File(data_path, header=true, comment="#"))
end

end # Hydrographs


text:
export print_fields, eq_cmn, print_cmn, print_cmn_br

"""
	print_prod_resp(n, species)

	Prints the product of responses for
	n		Vector of integers (the n vector)
	species	Vector of strings with species names, e.g. ['A', 'B', 'C']

	No return value

"""
function print_prod_resp(n, species)

	prod_resp = ""
	for j in 1:length(n)
		if n[j] == 1
			prod_resp *= "·R"*species[j]
		elseif n[j] == 2
			prod_resp *= "·R"*species[j]*"²"
		elseif n[j] > 2
			prod_resp *= string(factorial(n[j]))
			prod_resp *= "·R"*species[j]*"^"*string(n[j])
		end
	end
	print(prod_resp)

end

"""
	eq_cmn(m, n, s_i::Matrix{Int64}, r_i::Matrix{Int64}, k, species; print=false)

	Calculates the binomial prefactor and product of means for the c_mn

	Input
	m		Vector of integers which are the 'm' values
	n		Vector of integers which are the 'n' values
	s_i,r_i	Stoichiometric matrices
	k		Vector of strings with reaction rates, e.g. ['k1', 'k2', 'k3']
	species	Vector of strings with species names, e.g. ['A', 'B', 'C']

	If print==false returns
	res		Vector of integers which is the prefactor of the c_mn
			and includes all binomial factors
	mus		Vector of strings with the products of means of the
			different species according to the binomial factors

	Otherwise
	Call the function `print_cmn(res, mus)`

"""
function eq_cmn(m, n, s_i::Matrix{Int64}, r_i::Matrix{Int64}, k, species; print=false)

	num_int = length(k)
	res = zeros(Int, num_int)
	mus = ["" for i in 1:num_int]

	for β in 1:num_int

		prod_mu = 1					# product with mu_i
		prod1, prod2 = 1, 1			# initialize products

		mus[β] *= k[β] #*"·"

		for i in 1:length(species)

			prod1 *= binomial(s_i[β,i], m[i])
			prod2 *= binomial(r_i[β,i], m[i])
			prod_mu *= binomial(r_i[β,i], n[i])

			if r_i[β,i] == n[i]+1
				mus[β] *= "·μ"*species[i]
			elseif r_i[β,i] == n[i]+2
				mus[β] *=  "·μ"*species[i]*"²"
			elseif r_i[β,i] > n[i]+2
				mus[β] *=  "·μ"*species[i]*"^"*string(r_i[β,i]-n[i])
			end

		end

		res[β] += (prod1 - prod2) * prod_mu

	end

	if print
		print_cmn(res, mus)
	else
		return (res, mus)
	end

end

"""
	print_cmn(res::Vector{Int}, mus::Vector{String})

	Prints the c_mn

	Input
	res		Vector of integers which is the prefactor of the c_mn
			and includes all binomial factors
	mus		Vector of strings with the products of means of the
			different species according to the binomial factors

	Usually takes as input the return values of `eq_cmn(...)`

	No return value

"""
function print_cmn(res::Vector{Int}, mus::Vector{String})

	if all(res .== 0)
		print("0")
	end

	firstprint = true
	for β in 1:length(res)
		if firstprint
			if res[β]==1
				print(mus[β])
				firstprint=false
			elseif res[β]==-1
				print("-", mus[β])
				firstprint=false
			elseif abs(res[β]) > 1
				print(res[β], mus[β])
				firstprint=false
			end
		elseif abs(res[β]) == 1
			if res[β]>0
				print("+ ", mus[β])
			elseif res[β]<0
				print("– ", mus[β])
			end
		elseif abs(res[β]) > 1
			if res[β]>0
				print("+ ", res[β], mus[β])
			elseif res[β]<0
				print("– ", -res[β], mus[β])
			end
		end
		if β != length(res) && res[β]*res[β+1] != 0
			print(" ")
		end
	end

end

"""
	print_cmn_br(res::Vector{Int}, mus::Vector{String})

	Same as print_cmn(res, mus) but with brackets before and after
	the expression

"""
function print_cmn_br(res::Vector{Int}, mus::Vector{String})
	print("(")
	print_cmn(res, mus)
	print(")")
end

"""
	print_fields(p::Parameters, plf::Plefka, k_param, species; disconnected_resp=true)

	Prints the fields according to the theoretical equations for linear
	and/or quadratic order parameters and expansion order alpha and
	alpha^2, depending on the definition of plf::Plefka

	Input:
	p		Parameters for the reaction network
	plf		Plefka definition
	k_param	Vector of strings with reaction rates, e.g. ['k1', 'k2', 'k3']
	species	Vector of strings with species names, e.g. ['A', 'B', 'C']

	diconnected_resp	Boolean specifying if we use disconnected or connected
						response functions for the calculations of the fields

	No output, only printing to the standard output  (stdout)

"""
function print_fields(p::Parameters, plf::Plefka, k_param, species; disconnected_resp=true)

	N = length(p.k[1])		# number of species
	z = zeros(Int, N)		# Vector of zeros
	c_mn2(m,n) = eq_cmn(m, n, p.s_i, p.r_i, k_param, species; print=false)

	if plf.orderParam == "linear"

		for i in 1:N

			println("––––––––––\nSpecies: ", species[i])

			# Theta1
			print("Theta1 = ")
			(r1,m1) = c_mn2(e_i(N,i), z)
			all(r1 .== 0) ? print("0") : print_cmn(-r1, m1)
			println()

			if plf.alphaOrder == 2

				firstprint = true
				print("Theta2 = ")
				for n in n_list(p.r_i)
					if sum(n) > 1
						(r3,m3) = c_mn2(e_i(N,i), n)
						(r4,m4) = c_mn2(n, z)
						if any(r3 .!= 0) && any(r4 .!= 0)
							firstprint ? print("-2Δt sum[ ") : print(" + ")
							print_cmn_br(r3, m3)
							print_cmn_br(r4, m4)
							print_prod_resp(n,species)
							firstprint = false
						end
					end
				end
				firstprint ? println("0") : println(" ]")
			end
			println()
		end

	elseif plf.orderParam == "quad"

		for i in 1:N

			println("––––––––––\nSpecies: ", species[i])

			# Theta1
			print("Theta1 = ")
			(r1,m1) = c_mn2(e_i(N,i), z)
			(r2,m2) = c_mn2(e_i(N,i), e_i(N,i))

			all(r1 .== 0) ? print("") : print_cmn_br(-r1, m1)
			if all(r1 .== 0) && all(r2 .== 0)
				print("0")
			end
			if disconnected_resp && any(r2 .!= 0)
				print(" + ")
				print_cmn_br(r2, m2)
				print("·μ", species[i])
			end
			println()

			# hatR1
			print("hatR1  = ")
			if all(r2 .== 0)
				println("0")
			else
				print_cmn_br(-r2, m2)
				println("/Δt")
			end

			if plf.alphaOrder == 2

				# Theta2
				firstprint = true
				print("Theta2 = ")

				for n in n_list(p.r_i)

					(r3,m3) = c_mn2(e_i(N,i), n)
					(r4,m4) = c_mn2(n, z)
					(r5,m5) = c_mn2(n, e_i(N,i))
					(r6,m6) = c_mn2(e_i(N,i), n .+ e_i(N,i))

					if sum(n) > 1

						if any(r3 .!= 0) && any(r4 .!= 0)
							firstprint ? print("-2Δt sum[ ") : print(" + ")
							print_cmn_br(r3, m3)
							print_cmn_br(r4, m4)
							print_prod_resp(n,species)
							firstprint = false
						end
						if any(r6 .!= 0) && any(r4 .!= 0) && disconnected_resp
							firstprint ? print("-2Δt sum[ ") : print(" + ")
							print_cmn_br(-r6, m6)
							print_cmn_br(r4, m4)
							print("·μ", species[i])
							print_prod_resp(n,species)
						end
						if any(r3 .!= 0) && any(r5 .!= 0) && disconnected_resp
							firstprint ? print("-2Δt sum[ ") : print(" + ")
							print_cmn_br(-r3, m3)
							print_cmn_br(r5, m5)
							print("·μ", species[i])
							print_prod_resp(n,species)
						end
						if any([r3; r4; r5; r6] .!= 0)
							firstprint = false
						end

					elseif sum(n) == 1 && n[i] != 1

						if any(r3 .!= 0) && any(r5 .!= 0) && disconnected_resp
							firstprint ? print("-2Δt sum[ ") : print(" + ")
							print_cmn_br(-r3, m3)
							print_cmn_br(r5, m5)
							print("·μ", species[i])
							print_prod_resp(n,species)
							firstprint = false
						end

					end

				end

				firstprint ? println("0") : println(" ]")

				# hatR2
				firstprint = true
				print("hatR2  = ")

				for n in n_list(p.r_i)

					(r3,m3) = c_mn2(e_i(N,i), n)
					(r4,m4) = c_mn2(n, z)
					(r5,m5) = c_mn2(n, e_i(N,i))
					(r6,m6) = c_mn2(e_i(N,i), n .+ e_i(N,i))

					if sum(n) > 1

						if any(r6 .!= 0) && any(r4 .!= 0)
							firstprint ? print("-2·[ ") : print(" + ")
							print("sum( ")
							print_cmn_br(r6, m6)
							print_cmn_br(r4, m4)
							print("·δ(t,t'-)")
							print_prod_resp(n,species)
							print(" )")
							firstprint = false
						end
						if any(r3 .!= 0) && any(r5 .!= 0)
							firstprint ? print("-2·[ ") : print(" + ")
							print_cmn_br(r3, m3)
							print_cmn_br(r5, m5)
							print_prod_resp(n,species)
							firstprint = false
						end

					elseif sum(n) == 1 && n[i] != 1

						if any(r3 .!= 0) && any(r5 .!= 0)
							firstprint ? print("-2·[ ") : print(" + ")
							print_cmn_br(r3, m3)
							print_cmn_br(r5, m5)
							print_prod_resp(n,species)
							firstprint = false
						end

					end

				end

				firstprint ? println("0") :	println(" ]")

			end

			println()

		end

	end

end

max_stars_repo_name: mkurjahn/CRN

max_issues_repo_name: mkurjahn/CRN

max_forks_repo_name: mkurjahn/CRN


text:
import Base:  ==, hash
"""
    Agent()
"""
struct Agent{T}
    step::Function
    state::T
    id::UUID
end
Agent(step::Function, data::T, rng::AbstractRNG=Random.GLOBAL_RNG) where T = Agent{T}(step, data, UUIDs.uuid4(rng))

function ==(agent1::Agent, agent2::Agent)
    agent1.id==agent2.id
end

function hash(agent::Agent, h::UInt)
    hash(agent.id, h)
end


text:
mutable struct Source{NT}
    size::Tuple{Int64, Int64}
    ctable::Metadata.CTable
    data::Vector{UInt8}
    columns::Vector{ArrowVector}
end

function Source(::Type{NT}, ctable::Metadata.CTable,
                data::Vector{UInt8}) where {NT <: NamedTuple}
    s = Source{NT}((ctable.num_rows, length(ctable.columns)),
                   ctable, data, Vector{ArrowVector}(undef, 0))
    s.columns = constructall(s)
    s
end
function Source(io::FilenameOrIO; use_mmap::Bool=SHOULD_USE_MMAP)
    data = loaddata(io, use_mmap=use_mmap)
    ctable = getctable(data)
    sch = schema(ctable)
    Source(sch, ctable, data)
end

function schema(ctable::Metadata.CTable)
    ncols = length(ctable.columns)
    header = Vector{Symbol}(undef, ncols)
    types = Vector{Type}(undef, ncols)
    for (i, col) ∈ enumerate(ctable.columns)
        header[i] = Symbol(col.name)
        types[i] = juliatype(col)
    end
    return NamedTuple{Tuple(header), Tuple{types...}}
end

Tables.istable(::Type{<:Source}) = true
Tables.columnaccess(::Type{<:Source}) = true
Tables.schema(s::Source{NT}) where {NT} = Tables.Schema(NT)

Tables.columns(s::Source{NamedTuple{names, T}}) where {names, T} =
    NamedTuple{names}(Tuple(s.columns[i] for i = 1:length(names)))

getcolumn(s::Source, col::Integer) = s.ctable.columns[col]

colnumber(s::Source, col::Integer) = col
colnumber(s::Source{NamedTuple{names, T}}, col::String) where {names, T} = Tables.columnindex(names, Symbol(col))
colnumber(s::Source{NamedTuple{names, T}}, col::Symbol) where {names, T} = Tables.columnindex(names, col)

colname(s::Source{NamedTuple{names, T}}, col::Integer) where {names, T} = names[col]
colname(s::Source{NamedTuple{names, T}}, col::Symbol) where {names, T} = names[col]
colname(s::Source, col::String) = Symbol(col)
colname(s::Source, col::Symbol) = col

Base.size(s::Source) = s.size
Base.size(s::Source, i::Integer) = ifelse(i == 1, s.size[1], s.size[2])

datapointer(s::Source) = pointer(s.data)

checkcolbounds(s::Source, col::Integer) = (1 ≤ col ≤ size(s, 2)) || throw(BoundsError(s, col))

"""
    Feather.read(file::AbstractString)

Create a `DataFrame` representing the Feather file `file`.  This data frame will use `ArrowVector`s to
refer to data within the feather file.  By default this is memory mapped and no data is actually read
from disk until a particular field of the dataframe is accessed.

To copy the entire file into memory, instead use `materialize`.
"""
function read(io::FilenameOrIO; use_mmap::Bool=SHOULD_USE_MMAP)
    DataFrame(Source(io, use_mmap=use_mmap), copycols=false)
end

# TODO update docs
"""
    Feather.materialize(s::Feather.Source[, rows, cols])
    Feather.materialize(file::AbstractString[, rows, cols])

Read a feather file into memory and return it as a `DataFrame`.  Optionally one may only read in
particular rows or columns (these should be specified with `AbstractVector`s, columns can be either
integers or `Symbol`s).

For most purposes, it is recommended that you use `read` instead so that data is read off
disk only as necessary.
"""
materialize(x) = x
materialize(A::ArrowVector{T}) where T = convert(Vector{T}, A)
materialize(A::DictEncoding{T}) where T = Arrow.categorical(A)
materialize(A::ArrowVector, idx::AbstractVector{<:Integer}) = A[idx]
materialize(A::DictEncoding, idx::AbstractVector{<:Integer}) = Arrow.categorical(A, idx)

materialize(s::Source, col::Union{Symbol,<:Integer}) = materialize(s.columns[colnumber(s,col)])
function materialize(s::Source, rows::AbstractVector{<:Integer}, col::Union{Symbol,<:Integer})
    materialize(s.columns[colnumber(s,col)], rows)
end

function materialize(s::Source, rows::AbstractVector{<:Integer}, cols::AbstractVector{T}
                    ) where {T<:Union{Symbol,<:Integer}}
    DataFrame((colname(s,col)=>materialize(s,rows,col) for col ∈ cols)...)
end
function materialize(io::FilenameOrIO, rows::AbstractVector{<:Integer}, cols::AbstractVector{T}
                    ) where {T<:Union{Symbol,<:Integer}}
    materialize(Source(io), rows, cols)
end
function materialize(s::Source, cols::AbstractVector{<:Union{Symbol,<:Integer}})
    materialize(s, 1:size(s,1), cols)
end

materialize(s::Source) = materialize(s, 1:size(s,1), 1:size(s,2))
materialize(io::FilenameOrIO) = materialize(Source(io))

materialize(df::DataFrame) = DataFrame((n=>materialize(df[n]) for n ∈ names(df))...)

#=====================================================================================================
    new column construction stuff
=====================================================================================================#
Base.length(p::Metadata.PrimitiveArray) = p.length

startloc(p::Metadata.PrimitiveArray) = p.offset+1

Arrow.nullcount(p::Metadata.PrimitiveArray) = p.null_count

function bitmasklength(p::Metadata.PrimitiveArray)
    nullcount(p) == 0 ? 0 : padding(bytesforbits(length(p)))
end

function offsetslength(p::Metadata.PrimitiveArray)
    isprimitivetype(p.dtype) ? 0 : padding((length(p)+1)*sizeof(Int32))
end

valueslength(p::Metadata.PrimitiveArray) = p.total_bytes - offsetslength(p) - bitmasklength(p)

function offsetsloc(p::Metadata.PrimitiveArray)
    if isprimitivetype(p.dtype)
        throw(ErrorException("Trying to obtain offset values for primitive array."))
    end
    startloc(p) + bitmasklength(p)
end

# override default offset type
Locate.Offsets(col::Metadata.PrimitiveArray) = Locate.Offsets{Int32}(offsetsloc(col))

Locate.length(col::Metadata.PrimitiveArray) = length(col)
Locate.values(col::Metadata.PrimitiveArray) = startloc(col) + bitmasklength(col) + offsetslength(col)
# this is only relevant for lists, values type must be UInt8
Locate.valueslength(col::Metadata.PrimitiveArray) = valueslength(col)
Locate.bitmask(col::Metadata.PrimitiveArray) = startloc(col)

function constructcolumn(::Type{T}, data::Vector{UInt8}, meta::Metadata.CategoryMetadata,
                         col::Metadata.Column) where T
    reftype = juliatype(col.values.dtype)
    DictEncoding{T}(locate(data, reftype, col.values), locate(data, T, col.metadata.levels))
end
function constructcolumn(::Type{Union{T,Missing}}, data::Vector{UInt8}, meta::Metadata.CategoryMetadata,
                         col::Metadata.Column) where T
    reftype = Union{juliatype(col.values.dtype),Missing}
    DictEncoding{Union{T,Missing}}(locate(data, reftype, col.values),
                                   locate(data, T, col.metadata.levels))
end

function constructcolumn(::Type{T}, data::Vector{UInt8}, meta, col::Metadata.Column) where T
    locate(data, T, col.values)
end

function constructcolumn(s::Source, ::Type{T}, col::Integer) where T
    @boundscheck checkcolbounds(s, col)
    col = getcolumn(s, col)
    constructcolumn(T, s.data, col.metadata, col)
end
constructcolumn(s::Source{NamedTuple{names, T}}, col::Integer) where {names, T} = constructcolumn(s, fieldtype(T, col), col)
constructcolumn(s::Source{NamedTuple{names, T}}, col::AbstractString) where {names, T} = constructcolumn(s, Tables.columnindex(names, Symbol(col)))

constructall(s::Source) = ArrowVector[constructcolumn(s, i) for i ∈ 1:size(s,2)]


text:
# Dynamically include setup code, import modules, and finally evaluate the function expression passed by the user.
# User settings are loaded from the input file `MATDaemon.JL_OPTIONS` located in the jlcall.m workspace folder.
# The workspace folder is passed using the environment variable `MATDAEMON_WORKSPACE`.

# MATDaemon must be available
import MATDaemon

let
    # Load jlcall.m input parser results from workspace
    local workspace = ENV["MATDAEMON_WORKSPACE"]
    local opts = MATDaemon.load_options(workspace)

    # Initialize user project environment etc.
    MATDaemon.init_environment(opts)

    # Print environment for debugging
    if opts.debug
        println("* Environment for evaluating Julia expression:")
        println("*   Working dir: $(pwd())")
        println("*   Module: $(@__MODULE__)")
        println("*   Load path: $(LOAD_PATH)")
        println("*   Active project: $(Base.active_project())", "\n")
    end

    # Include setup code
    if !isempty(opts.setup)
        include(opts.setup)
    end

    # Load modules from strings; will fail if not installed
    for mod in opts.modules
        @eval import $(Meta.parse(mod))
    end

    # Parse and evaluate `f` from string
    local f_expr = :(let; $(Meta.parse(opts.f)); end)

    # If not a function call, return a thunk
    if opts.nofun
        f_expr = :(let; $(f_expr); (args...; kwargs...) -> nothing; end)
    end

    if opts.debug
        println("* Generated Julia function expression: ")
        println(string(MATDaemon.MacroTools.prettify(f_expr)), "\n")
    end

    local f = @eval $(f_expr)

    # Call `f`, loading MATLAB input arguments from `opts.infile`
    # and saving Julia outputs to `opts.outfile`
    local output = MATDaemon.jlcall(f, opts)
end


text:
module X6ADC

type X6
	id::Int
end
X6() = X6(-1)

__init__() = push!(Libdl.DL_LOAD_PATH, joinpath(dirname(@__FILE__), "../../build/"))

const DSP_WB_OFFSET = [0x2000, 0x2100]
const DECIMATION_FACTOR = 4

immutable Channel
	a::Cint
	b::Cint
	c::Cint
end

function connect!(dev::X6, id)
	ret = ccall((:connect_x6, "libx6"), Int32, (Int32,), id)
	if ret == 0
		dev.id = id
	else
		error("Unable to open connection to X6 $id")
	end
end

function disconnect!(dev::X6)
	ret = ccall((:disconnect_x6, "libx6"), Int32, (Int32,), dev.id)
	dev.id = -1
end

function is_open(dev::X6)
	bool(ccall((:is_open, "libx6"), Int32, (Int32,), dev.id))
end

function set_logging_level(level)
	#Sets logging level library wide
	ccall((:set_logging_level, "libx6"), Int32, (Int32,), level)
end

function get_logic_temperature(dev::X6)
	ccall((:get_logic_temperature, "libx6"), Float32, (Int32, Int32), dev.id, 0)
end

function get_sampleRate(dev::X6)
	ccall((:get_sampleRate, "libx6"), Float64, (Int32,), dev.id)
end

function set_sampleRate(dev::X6, rate::Real)
	ccall((:set_sampleRate, "libx6"), Int32, (Int32, Float64), dev.id, rate)
end

function get_trigger_source(dev::X6)
	ccall((:get_trigger_source, "libx6"), Int32, (Int32,), dev.id)
end

function set_trigger_source(dev::X6, source::Int)
	ccall((:set_trigger_source, "libx6"), Int32, (Int32, Int32), dev.id, source)
end

function get_reference(dev::X6)
	strMap = {0 => "internal", 1=>"external"}
	ref = ccall((:get_reference, "libx6"), Int32, (Int32,), dev.id)
	strMap[ref]
end

function set_reference(dev::X6, ref::String)
	strMap = {"internal" => 0, "external"=> 1}
	ccall((:set_reference, "libx6"), Int32, (Int32, Int32), dev.id, strMap[ref])
end

function enable_stream(dev::X6, a, b, c)
	ccall((:enable_stream, "libx6"), Int32, (Int32, Int32, Int32, Int32), dev.id, a, b, c)
end

function disable_stream(dev::X6, a, b, c)
	ccall((:disable_stream, "libx6"), Int32, (Int32, Int32, Int32, Int32), dev.id, a, b, c)
end

function read_register(dev::X6, addr, offset)
	#Read an address in the wishbone register space
	ccall((:read_register, "libx6"), Int32, (Int32, Int32, Int32), dev.id, addr, offset)
end

function write_register(dev::X6, addr, offset, value)
	#Write an address in the wishbone register space
	ccall((:write_register, "libx6"), Int32, (Int32, Int32, Int32, Int32), dev.id, addr, offset, value)
end

function set_averager_settings(dev::X6, recordLength::Int, numSegments::Int, waveforms::Int, roundRobins::Int)
	ccall((:set_averager_settings, "libx6"), Int32, (Int32, Int32, Int32, Int32, Int32),
											dev.id, recordLength, numSegments, waveforms, roundRobins)
end

function acquire(dev::X6)
	ccall((:acquire, "libx6"), Int32, (Int32,), dev.id)
end

function stop(dev::X6)
	ccall((:stop, "libx6"), Int32, (Int32,), dev.id)
end

function wait_for_acquisition(dev::X6, timeOut::Int)
	ccall((:wait_for_acquisition, "libx6"), Int32, (Int32, Int32), dev.id, timeOut)
end

function transfer_stream(dev::X6, a, b, c)
	channels = [Channel(a,b,c)]
	bufSize = ccall((:get_buffer_size, "libx6"), Int32, (Int32, Ptr{Channel}, Uint32), dev.id, channels, length(channels))
	wfs = Array(Float64, bufSize)
	success = ccall((:transfer_stream, "libx6"), Int32, (Int32, Ptr{Channel}, Uint32, Ptr{Float64}, Int32),
													dev.id, channels, length(channels), wfs, bufSize)
	@assert success == 0 "Transferring waveforms failed!"
	if (b == 0) # physical channel
		return wfs
	else
		return wfs[1:2:end] + 1im*wfs[2:2:end]
	end
end

function unittest()
	set_logging_level(6)

	x6 = X6()

	connect!(x6, 0)

	@assert is_open(x6) "Could not open connection to X6 board."

	@printf("Current logic temperature is %.1f\n", get_logic_temperature(x6))

	@printf("Current PLL frequency is %.2f GHz\n", get_sampleRate(x6)/1e9)

	set_reference(x6, "external")

	println("Enabling streams...")
	for physChan = 1:2
		# enable raw stream
		enable_stream(x6, physChan, 0, 0)
		for demodChan = 1:2
			for resultChan = 0:1
				enable_stream(x6, physChan, demodChan, resultChan)
			end
		end
	end

	println("Setting NCO phase increments...")
	phaseInc = 4/100
	write_register(x6, DSP_WB_OFFSET[1], 0x0010, int(round(1 * phaseInc * 2^18)))
	write_register(x6, DSP_WB_OFFSET[1], 0x0011, int(round(1 * phaseInc * 2^18)))
	write_register(x6, DSP_WB_OFFSET[2], 0x0010, int(round(1 * phaseInc * 2^18)))
	write_register(x6, DSP_WB_OFFSET[2], 0x0011, int(round(1 * phaseInc * 2^18)))

	println("Writing kernel lengths")
	write_register(x6, DSP_WB_OFFSET[1], 24+1-1, 2)
	write_register(x6, DSP_WB_OFFSET[1], 24+2-1, 2)
	write_register(x6, DSP_WB_OFFSET[2], 24+1-1, 2)
	write_register(x6, DSP_WB_OFFSET[2], 24+2-1, 2)

	set_averager_settings(x6, 2048, 9, 1, 1)

	println("Acquiring...")
	acquire(x6)
	success = wait_for_acquisition(x6, 1)
	println("Wait for acquisition returned $success")
	stop(x6)

	rawwfs = [transfer_stream(x6, physChan, 0, 0) for physChan = 1:2]
	demodwfs = [transfer_stream(x6, physChan, demodChan, 0) for physChan = 1:2, demodChan = 1:2]
	resultwfs = [transfer_stream(x6, physChan, demodChan, 1) for physChan = 1:2, demodChan = 1:2]

	disconnect!(x6)

	return rawwfs, demodwfs, resultwfs
end

end #module


text:
module GriddingMachineOld

using Conda
using Dates
using LazyArtifacts

using DataFrames: DataFrame
using Distributed: @everywhere, addprocs, pmap, rmprocs, workers
using DocStringExtensions: FIELDS
using Glob: glob
using PkgUtility: month_days, nanmean, predownload_artifact!, read_csv, read_nc, save_csv!, save_nc!
using ProgressMeter: @showprogress
using PyCall: pyimport
using UnPack: @unpack

using ..Indexer: lat_ind, lon_ind

# global constants
ARTIFACTs_TOML = joinpath(@__DIR__, "../Artifacts.toml");
CDSAPI_PORTAL  = "https://cds.climate.copernicus.eu/api/v2";
CDSAPI_KEY     = "";
CDSAPI_CLIENT  = nothing;
MODIS_HOME     = "/net/fluo/data1/data/MODIS";
MODIS_PORTAL   = "https://e4ftl01.cr.usgs.gov";
MODIS_USER_ID  = "";
MODIS_USER_PWD = "";

# export public types for GriddedDataset
export GriddedDataset

# export public types for UngriddedDataset
export MOD09A1v006NIRv, MOD15A2Hv006LAI

# export public types for ERA5Dataset
export ERA5LandHourly, ERA5SingleLevelsHourly

#export public functions for GriddedDataset
export regrid_LUT, save_LUT!

#export public functions for UngriddedDataset
export process_RAW!

# export public functions for ERA5Dataset
export fetch_ERA5!

# include functions to load/read datasets
include("Datasets/DatasetType.jl")
include("Datasets/regrid.jl"     )
include("Datasets/save.jl"       )

# include function for ERA5 datasets
include("ERA5/DataType.jl")
include("ERA5/fetch.jl"   )

# include functions to grid datasets
include("Gridding/DataType.jl" )
include("Gridding/compile.jl"  )
include("Gridding/fetch.jl"    )
include("Gridding/grid.jl"     )
include("Gridding/load.jl"     )
include("Gridding/parse.jl"    )
include("Gridding/query.jl"    )
include("Gridding/shortcuts.jl")
include("Gridding/workers.jl"  )

# The Util functions
include("Utils/cdsapi.jl"  )
include("Utils/password.jl")

end # module


text:
let
# NOTE: Single quotes break remote execution. File needs to be shorter than 2000 chars.
if VERSION > v"0.7-"
  port = parse(Int, popfirst!(ARGS))
else
  port = parse(Int, shift!(ARGS))
end

junorc = haskey(ENV, "JUNORC_PATH") ? joinpath(ENV["JUNORC_PATH"], "juno_startup.jl") : joinpath(homedir(), ".julia", "config", "juno_startup.jl")
junorc = abspath(normpath(expanduser(junorc)))

if (VERSION > v"0.7-" ? Base.find_package("Atom") : Base.find_in_path("Atom")) == nothing
  p = VERSION > v"0.7-" ? x -> printstyled(x, color=:cyan, bold=true) : x -> print_with_color(:cyan, x, bold=true)
  p("\nHold on tight while we are installing some packages for you.\nThis should only take a few seconds...\n\n")

  if VERSION > v"0.7-"
    using Pkg
    Pkg.activate()
  end

  Pkg.add("Atom")
  Pkg.add("Juno")

  println()
end

# TODO: Update me when tagging a new relase:
MIN_ATOM_VER = v"0.11.3"
outdated = false

try
  if VERSION >= v"1.0-"
    using Pkg
    atompath = Base.find_package("Atom")

    if !occursin(Pkg.devdir(), atompath) # package is not `dev`ed
      tomlpath = joinpath(dirname(atompath), "..", "Project.toml")
      atomversion = VersionNumber(Pkg.TOML.parsefile(tomlpath)["version"])

      if atomversion < MIN_ATOM_VER
        outdated = """
          Please upgrade Atom.jl to at least version `$(MIN_ATOM_VER)` with e.g. `using Pkg; Pkg.update()`.

          If the integrated REPL is non-functional, try an external terminal opened with the `Julia Client: Open External REPL` command.
        """
      end
    end
  end
catch err
end

println("Starting Julia...")

try
  import Atom
  using Juno
  Atom.handle("junorc") do path
    cd(path)
    ispath(junorc) && include(junorc)
    nothing
  end
  Atom.connect(port)
  if outdated != false
    Atom.msg("versionwarning", outdated)
  end
catch
  if outdated != false
    printstyled("Outdated version of Atom.jl detected.\n", outdated, "\n", color = Base.error_color())
  end
  rethrow()
end
end

max_stars_repo_name: blegat/atom-julia-client

max_issues_repo_name: blegat/atom-julia-client

max_forks_repo_name: blegat/atom-julia-client


text:
# File: randport.jl
# Author:
# Date: 29-June-2020

# import XL data
[names,~,returns] = getXLData('hfma_matlab_data','10 Hedge Funds');

N = size(returns,2); # # data points
am1 = fMean(returns,1); # monthly mean
covar = cov(returns); # covariance matrix

# plot mean-variance efficient frontier
portopt(am1,covar,N)

# randomise asset weights (1,000)
w = exprnd(1,1000,N);
tot = sum(w,2);
tot = tot(:,ones(N,1));
w = w./tot;

# calculate exp. return & std. dev. for each portfolio
[pRisk , pRet] = portstats(am1,covar,w);

# plot random portfolios
hold on
plot(pRisk,pRet,'.r');
hold off


text:
struct DeepCorrectionPolicy{M<:Union{MDP,POMDP},P<:AbstractNNPolicy, A} <: AbstractNNPolicy
    problem::M
    correction_network::P
    lowfi_values::Any
    correction::Any
    correction_weight::Float64
    action_map::Vector{A}
end

function _actionvalues(policy::DeepCorrectionPolicy, o::AbstractArray{T,N}) where {T<:Real,N}
    q_low = lowfi_values(policy.lowfi_values, policy.correction_network.problem, o)
    q_corr = actionvalues(policy.correction_network, o)
    q_val = correction(policy.correction, policy.correction_network.problem, q_low, q_corr, policy.correction_weight)
    return q_val
end

function POMDPPolicies.actionvalues(policy::DeepCorrectionPolicy{M}, s) where M<:MDP
    return _actionvalues(policy, convert_s(Array{Float64}, s, policy.problem))
end

function POMDPPolicies.actionvalues(policy::DeepCorrectionPolicy{M}, o) where M<:POMDP
    return _actionvalues(policy, convert_o(Array{Float64}, o, policy.problem))
end

function POMDPs.action(policy::DeepCorrectionPolicy, o)
    q_val = actionvalues(policy, o)
    ai = argmax(q_val)
    return policy.action_map[ai]
end

function POMDPs.value(policy::DeepCorrectionPolicy, o)
    return maximum(actionvalues(policy, o))
end

function DeepQLearning.resetstate!(policy::DeepCorrectionPolicy)
    DeepQLearning.resetstate!(policy.correction_network)
end

function DeepQLearning.getnetwork(policy::DeepCorrectionPolicy)
    return getnetwork(policy.correction_network)
end