text:
using LazyStack, BenchmarkTools
using JuliennedArrays, LazyArrays, SliceMap

# Array of Arrays

v1 = [rand(10) for i=1:10];
v4 = [rand(10^4) for i=1:10^4];

@code_warntype stack(v1)

@btime reduce(hcat, $v1);   # 155.417 ns (1 allocation: 896 bytes)
@btime collect(stack($v1)); # 290.430 ns (2 allocations: 912 bytes)
@btime collect(Align($v1,True(),False())); # 332.769 ns (2 allocations: 912 bytes)

@btime reduce(hcat, $v4);   # 429.879 ms (2 allocations: 762.94 MiB)
@btime collect(stack($v4)); # 445.761 ms (3 allocations: 762.94 MiB)
@btime collect(Align($v4,True(),False())); # 452.899 ms (3 allocations: 762.94 MiB)

# Tuple of Arrays

t1 = Tuple(v1);

@code_warntype stack(t1)

@btime hcat($t1...);          #   178.379 ns (4 allocations: 1.03 KiB)

@btime collect(stack($t1));   # 1.032 μs (2 allocations: 912 bytes) -- was quicker I swear
@btime collect($(stack(t1))); # 1.065 μs (1 allocation: 896 bytes)

@btime collect(Hcat($t1...)); # 149.001 ns (3 allocations: 1008 bytes) -- match this

@btime $(hcat(t1...))[9,9]    # 1.424 ns (0 allocations: 0 bytes)
@btime $(stack(t1))[9,9]      # 1.700 ns (0 allocations: 0 bytes)

# Generators

@code_warntype stack(rand(10) for i=1:10)
@code_warntype stack(i<5 ? ones(Int,10) : rand(10) for i=1:10)
@code_warntype collect(i<5 ? ones(Int,10) : rand(10) for i=1:10)

@btime reduce(hcat, [rand(10^3) for i=1:10^3]); # 1.900 ms (1003 allocations: 15.39 MiB)
@btime reduce(hcat, (rand(10^3) for i=1:10^3)); # 2.866 s (3999 allocations: 3.74 GiB)
@btime stack(rand(10^3) for i=1:10^3);          # 1.631 ms (1009 allocations: 15.38 MiB)
@btime stack([rand(10^3) for i=1:10^3]);        # 832.543 μs (1002 allocations: 7.76 MiB)
@btime [rand(10^3) for i=1:10^3];               # 858.156 μs (1001 allocations: 7.76 MiB)

@btime reduce(hcat, [i < 10 ? (i+1:i+10^2) : rand(10^2) for i=1:10^2]); # 65.562 μs (405 allocations: 170.16 KiB)
@btime reduce(hcat, i < 10 ? (i+1:i+10^2) : rand(10^2) for i=1:10^2); # 1.492 ms (396 allocations: 3.94 MiB)
@btime stack(i < 10 ? (i+1:i+10^2) : rand(10^2) for i=1:10^2); # 89.508 μs (9209 allocations: 378.72 KiB)

@btime reduce(hcat, [ones(10^2) for i=1:10^2]); #    12.755 μs (103 allocations: 166.58 KiB)
@btime reduce(hcat, (ones(10^2) for i=1:10^2)); # 1.419 ms (280 allocations: 3.95 MiB)
@btime stack(ones(10^2) for i=1:10^2);          #    12.286 μs (109 allocations: 166.03 KiB)
@btime stack([ones(10^2) for i=1:10^2]);        #     7.141 μs (102 allocations: 88.39 KiB)
@btime collect(stack([ones(10^2) for i=1:10^2])); #  13.037 μs (104 allocations: 166.59 KiB)

# Mapslices

M1 = rand(10,1000);
f1(x) = begin length(x)==10 || error(); identity.(x ./ length(x)) end
f1(x) = sqrt.(x ./ length(x))

@btime mapslices(f1, $M1; dims=1)       # 496.050 μs (7502 allocations: 399.89 KiB)
@btime stack(f1, eachcol($M1))          #  76.542 μs (2012 allocations: 281.72 KiB)
@btime stack(f1, eachslice($M1, dims=2))
@btime mapcols(f1, $M1)                 #  82.132 μs (2006 allocations: 289.33 KiB)


text:
###############################################################################
#
# Pre-defined parameter sets
#
###############################################################################
"""
    create_soil_VC(
                vc::AbstractSoilVC{FT},
                name::String,
                α::Number,
                n::Number,
                Θs::Number,
                Θr::Number
    ) where {FT<:AbstractFloat}
    create_soil_VC(
                vc::AbstractSoilVC{FT},
                name::String
    ) where {FT<:AbstractFloat}

Create a [`AbstractSoilVC`](@ref) type of soil VC, given
- `vc` [`AbstractSoilVC`](@ref) type identifier
- `name` Soil type name. Supported names include
  - "Sand"
  - "Loamy Sand"
  - "Sandy Loam"
  - "Loam"
  - "Sandy Clay Loam"
  - "Silt Loam"
  - "Silt"
  - "Clay Loam"
  - "Silty Clay Loam"
  - "Sandy Clay"
  - "Silty Clay"
  - "Clay"
- `α` Soil α
- `n` Soil n
- `Θs` SWC at saturation
- `Θr` Residual SWC
"""
function create_soil_VC(
            vc::BrooksCorey{FT},
            name::String,
            α::Number,
            n::Number,
            Θs::Number,
            Θr::Number
) where {FT<:AbstractFloat}
    _vc_vG = VanGenuchten{FT}(stype = name,
                              α     = α   ,
                              n     = n   ,
                              Θs    = Θs  ,
                              Θr    = Θr  )
    _vc_BC = BrooksCorey{FT}(stype = name        ,
                             b     = (2n-1)/(n-1),
                             ϕs    = 1/α         ,
                             Θs    = Θs          ,
                             Θr    = Θr          )
    fit_soil_VC!(_vc_vG, _vc_BC);

    return _vc_BC
end

function create_soil_VC(
            vc::VanGenuchten{FT},
            name::String,
            α::Number,
            n::Number,
            Θs::Number,
            Θr::Number
) where {FT<:AbstractFloat}
    return VanGenuchten{FT}(stype = name,
                            α     = α   ,
                            n     = n   ,
                            Θs    = Θs  ,
                            Θr    = Θr  )
end

function create_soil_VC(
            vc::AbstractSoilVC{FT},
            name::String
) where {FT<:AbstractFloat}
    # Parameters from Silt soil
    paras = [ 163.2656, 1.37, 0.46, 0.034];

    if name=="Sand"
        paras = [1479.5945, 2.68, 0.43, 0.045];
    elseif name=="Loamy Sand"
        paras = [1265.3084, 2.28, 0.41, 0.057];
    elseif name=="Sandy Loam"
        paras = [ 765.3075, 1.89, 0.41, 0.065];
    elseif name=="Loam"
        paras = [ 367.3476, 1.56, 0.43, 0.078];
    elseif name=="Sandy Clay Loam"
        paras = [ 602.0419, 1.48, 0.39, 0.100];
    elseif name=="Silt Loam"
        paras = [ 204.0820, 1.41, 0.45, 0.067];
    elseif name=="Silt"
        paras = [ 163.2656, 1.37, 0.46, 0.034];
    elseif name=="Clay Loam"
        paras = [ 193.8779, 1.31, 0.41, 0.095];
    elseif name=="Silty Clay Loam"
        paras = [ 102.0410, 1.23, 0.43, 0.089];
    elseif name== "Sandy Clay"
        paras = [ 275.5107, 1.23, 0.38, 0.100];
    elseif name=="Silty Clay"
        paras = [  51.0205, 1.09, 0.36, 0.070];
    elseif name=="Clay"
        paras = [  81.6328, 1.09, 0.38, 0.068];
    else
        @warn "Soil type $(name) not recognized, use Silt soil instead.";
    end

    return create_soil_VC(vc, name, paras...)
end

ext: jl

max_stars_repo_name: bbuman/Land

max_issues_repo_name: bbuman/Land

max_forks_repo_name: bbuman/Land


text:
module Types

export AbstractIntegrator, move!, current_positions, current_velocities, set_state!

using MosimoBase: Vector3s, Vector3

# M: Mass function; F: Force function
# Accept mass as a function (index -> value) instead of an array in order to perform better in special
# cases like that all the masses are the same.
abstract type AbstractIntegrator{M <: Function, F <: Function} end

move!(::AbstractIntegrator) = error("Unimplemented")

current_positions(::AbstractIntegrator) = error("Unimplemented")
current_velocities(::AbstractIntegrator) = error("Unimplemented")
set_state!(::AbstractIntegrator, rs::Vector3s, vs::Vector3s) = error("Unimplemented")

end


text:
using ImageMagick
using FileIO, Colors, FixedPointNumbers, ZipFile
using Test

workdir = joinpath(tempdir(), "Images")
writedir = joinpath(workdir, "write")
if !isdir(workdir)
    mkdir(workdir)
end
if !isdir(writedir)
    mkdir(writedir)
end

@testset "Read remote" begin
    urlbase = "http://www.imagemagick.org/Usage/images/"

    function getfile(name)
        file = joinpath(workdir, name)
        if !isfile(file)
            file = download(urlbase*name, file)
        end
        file
    end

    @testset "Gray" begin
        file = getfile("jigsaw_tmpl.png")
        img = ImageMagick.load(file)
        @test ImageMagick.metadata(file) == (reverse(size(img)), Gray{N0f8})
        @test eltype(img) == Gray{N0f8}
        @test ndims(img) == 2
        outname = joinpath(writedir, "jigsaw_tmpl.png")
        ImageMagick.save(outname, img)
        imgc = ImageMagick.load(outname)
        @test img == imgc
        @test reinterpret(UInt32, map(RGB24, img)) ==
            map(x->x&0x00ffffff, reinterpret(UInt32, map(ARGB32, img)))
        @test convert(Array{Gray{Float32}}, img) == float32.(img)
        imgrgb8 = map(RGB{N0f8}, img)
        @test imgrgb8[1,1].r == img[1].val
        open(outname, "w") do file
            show(file, MIME("image/png"), img)
        end
    end

    @testset "Gray with alpha channel" begin
        file = getfile("wmark_image.png")
        img = ImageMagick.load(file)
        m = ImageMagick.metadata(file)
        @test m[1] == reverse(size(img))
        @test ndims(img) == 2
        @test eltype(img) in (GrayA{N0f8}, RGBA{N0f8})
        if Sys.islinux()
            outname = joinpath(writedir, "wmark_image.png")
            ImageMagick.save(outname, img)
            sleep(0.2)
            imgc = ImageMagick.load(outname)
            @test img == imgc
            open(outname, "w") do file
                show(file, MIME("image/png"), img)
            end
        end
        @test reinterpret(UInt32, map(RGB24, img)) ==
            map(x->x&0x00ffffff, reinterpret(UInt32, map(ARGB32, img)))
    end

    @testset "RGB" begin
        file = getfile("rose.png")
        img = ImageMagick.load(file)
        @test ImageMagick.metadata(file) == (reverse(size(img)), RGB{N0f8})
        # Mac reader reports RGB4, imagemagick reports RGB
        @test ndims(img) == 2
        @test eltype(img) == RGB{N0f8}
        outname = joinpath(writedir, "rose.tiff")
        ImageMagick.save(outname, img)
        imgc = ImageMagick.load(outname)
        T = eltype(imgc)
        # Why does this one fail on OSX??
        Sys.isapple() || @test img == imgc
        @test reinterpret(UInt32, map(RGB24, img)) ==
            map(x->x&0x00ffffff, reinterpret(UInt32, map(ARGB32, img)))
        imgrgb8 = map(RGB{N0f8}, img)
        @test imgrgb8 == img
        outname = joinpath(writedir, "rose.png")
        open(outname, "w") do file
            show(file, MIME("image/png"), img)
        end
    end

    @testset "RGBA with 16 bit depth" begin
        file = getfile("autumn_leaves.png")
        img = ImageMagick.load(file)
        @test ImageMagick.metadata(file) == (reverse(size(img)), RGBA{N0f16})
        @test ndims(img) == 2
        @test eltype(img) == RGBA{N0f16}
        outname = joinpath(writedir, "autumn_leaves.png")
        Sys.isapple() || begin
            ImageMagick.save(outname, img)
            sleep(0.2)
            imgc = ImageMagick.load(outname)
            @test img == imgc
            @test reinterpret(UInt32, map(RGB24, img)) ==
                map(x->x&0x00ffffff, reinterpret(UInt32, map(ARGB32, img)))
        end
        open(outname, "w") do file
            show(file, MIME("image/png"), img)
        end
    end

    @testset "Indexed" begin
        file = getfile("present.gif")
        img = ImageMagick.load(file)
        @test ImageMagick.metadata(file) == (reverse(size(img)), RGB{N0f8})
        @test nimages(img) == 1
        @test reinterpret(UInt32, map(RGB24, img)) ==
            map(x->x&0x00ffffff, reinterpret(UInt32, map(ARGB32, img)))
        outname = joinpath(writedir, "present.png")
        open(outname, "w") do file
            show(file, MIME("image/png"), img)
        end
    end

    @testset "Images with a temporal dimension" begin
        fname = "swirl_video.gif"
        #fname = "bunny_anim.gif"  # this one has transparency but LibMagick gets confused about its size
        file = getfile(fname)  # this also has transparency
        img = ImageMagick.load(file)
        @test ImageMagick.metadata(file) == (size(img)[[2,1,3]], RGB{N0f8})
        @test size(img, 3) == 26
        outname = joinpath(writedir, fname)
        ImageMagick.save(outname, img)
        imgc = ImageMagick.load(outname)
        @test size(img) == size(imgc)
        @test img == imgc
    end

    @testset "Extra properties" begin
        Sys.isapple() || begin
            file = getfile("autumn_leaves.png")
            # List properties
            extraProps = magickinfo(file)
            @show extraProps

            img = ImageMagick.load(file)
            @test ImageMagick.metadata(file) == (reverse(size(img)), RGBA{N0f16})
            props = magickinfo(file, extraProps)
            for key in extraProps
                @test haskey(props, key) == true
                @test props[key] != nothing
            end

            props = magickinfo(file, extraProps[1])
            @test haskey(props, extraProps[1]) == true
            @test props[extraProps[1]] != nothing

            warnfile, io = mktemp()
            props = redirect_stderr(io) do
                magickinfo(file, "Nonexistent property")
            end
            close(io)
            @test haskey(props, "Nonexistent property") == true
            @test props["Nonexistent property"] == nothing
        end
    end
end

@testset "EXIF orientation" begin
    url = "http://magnushoff.com/assets/test-exiforientation.zip"
    fn = joinpath(workdir, "test-exiforientation.zip")
    download(url, fn)
    first_img = true
    r = ZipFile.Reader(fn)
    local img0
    for f in r.files
        data = read!(f, Array{UInt8}(undef, f.uncompressedsize...))
        if first_img
            img0 = readblob(data)
            first_img = false
        else
            img = readblob(data)
            @test all(img0.==img) == true
        end
    end
    ZipFile.close(r)
end

nothing


text:
import Dates

"""
    NullCalendar() <: Calendar

A holiday calendar for which there is never a holiday. *sigh*
"""
struct NullCalendar <: Calendar end

is_holiday(::NullCalendar, ::Dates.Date) = false

"""
    WeekendCalendar() <: Calendar

A calendar which will mark every Saturday and Sunday as a holiday
"""
struct WeekendCalendar <: Calendar end

function is_holiday(::WeekendCalendar, date::Dates.Date)
    signbit(5 - Dates.dayofweek(date))
end

"""
    JointCalendar(calendars::Vector{Calendar}) <: Calendar

A grouping of calendars, for which it is a holiday if it's marked as a holiday
for any of the underlying calendars.

By default addition of calendars will generate a joint calendar for you.
"""
struct JointCalendar{T} <: Calendar
    calendars::Vector{Calendar}
end

function is_holiday(cal::JointCalendar, date::Dates.Date)
    foldl(
        (acc, val) -> acc && is_holiday(val, date),
        cal.calendars;
        init = false,
    )
end

Base.:+(cal1::Calendar, cal2::Calendar) = JointCalendar([cal1, cal2])
Base.:+(cal1::Calendar, cal2::JointCalendar) =
    JointCalendar(vcat(cal1, cal2.calendars))
Base.:+(cal1::JointCalendar, cal2::Calendar) =
    JointCalendar(vcat(cal1.calendars, cal2))
Base.:+(cal1::JointCalendar, cal2::JointCalendar) =
    JointCalendar(vcat(cal1.calendars, cal2.calendars))

"""
    SimpleCalendarManager(calendars::Dict{String, Calendar}) <: Calendar

A basic calendar manager which just holds a reference to each underlying calendar, by name,
and will generate a joint calendar if multiple names are requested.
"""
struct SimpleCalendarManager <: CalendarManager
    calendars::Dict{String,Calendar}
end

function calendar(cal_mgr::SimpleCalendarManager, names)::Calendar
    if length(names) == 0
        NullCalendar()
    elseif length(names) == 1
        cal_mgr.calendars[names[1]]
    else
        foldl(
            (acc, val) -> acc + cal_mgr.calendars[val],
            names[2:end],
            cal_mgr.calendars[names[1]],
        )
    end
end

"""
    get_calendar_names(rdate::RDate)::Vector{String}

A helper method to get all of the calendar names that could potentially be requested by this rdate. This
mechanism can be used to mark the minimal set of calendars on which adjustments depend.
"""
get_calendar_names(::RDate) = Vector{String}()
get_calendar_names(rdate::Union{BizDays,CalendarAdj}) = rdate.calendar_names
get_calendar_names(rdate::RDateCompound) = Base.foldl((val, acc) -> vcat(
        acc,
        get_calendar_names(val),
        rdate.parts,
        init = Vector{String}(),
    ))
get_calendar_names(rdate::RDateRepeat) = get_calendar_names(rdate.part)


text:
module MangalTestNetwork
    using Mangal
    using Test
    include("setup.jl")

    # Nodes of a network
    N1 = network(19)
    nN1 = nodes(N1)
    @test typeof(nN1) <: Vector{MangalNode}

    # Datasets with no arguments
    @test typeof(networks()) <: Vector{MangalNetwork}

    # Networks by id
    @test typeof(network(19)) <: MangalNetwork

    # Network by name
    @test typeof(network("howking_1968_19680601_12")) <: MangalNetwork

    # Networks by dataset
    ds = dataset(62) # Olesen
    n_ds = networks(ds)
    @test typeof(n_ds) <: Vector{MangalNetwork}
    n_ds_q = networks(ds, Pair("count", 2))
    @test typeof(n_ds_q) <: Vector{MangalNetwork}
    @test length(n_ds_q) == 2

    # Wrap multiple calls
    this_page = 1
    networks_ponisio = networks(dataset("ponisio_2017"), Pair("count", 10), Pair("page", this_page))
    keep_querying = true
    while keep_querying
        global this_page += 1
        response = networks(dataset("ponisio_2017"), Pair("count", 10), Pair("page", this_page))
        append!(networks_ponisio, response)
        global keep_querying = length(response) > 0
    end
    @test length(networks_ponisio) == 131

end


text:
selected_css = """
.$SELECTED_CLASS {
  background: rgb(0, 209, 178);
  color: white;
  padding: 2px;
}

.selectable {
  cursor: pointer;
}
"""

single_select_js = js"""
function() {
  if (!event.ctrlKey) {
    // first make sure all selectable elements are not selected
    let selected = document.getElementsByClassName("selectable");
    for(let elem of selected) {
      elem.classList.remove($SELECTED_CLASS);
    }
  }
  if (this.classList.contains($SELECTED_CLASS)) {
    this.classList.remove($SELECTED_CLASS);
  } else {
    this.classList.add($SELECTED_CLASS);
  }
  let selected_list = []
  let selected = document.getElementsByClassName($SELECTED_CLASS);
  for (let elem of selected) {
    selected_list.push({'label':elem.innerText, 'id':elem.id})
  }
  _webIOScope.setObservableValue('selected_node', selected_list);
}
"""
selected_style = node(:style, selected_css)

nested_css = """
/* Create the caret/arrow with a unicode, and style it */
.caret-start {
  display: inline-block;
  margin-right: 6px;
  cursor: pointer;
}

.caret-down {
  transform: rotate(90deg);
}

/* Hide the nested list */
.nested {
  list-style-type:none;
  display: none;
}

/* Show the nested list when the user clicks on the caret/arrow (with JavaScript) */
.active {
  display: block;
}
"""
nested_style = node(:style, nested_css)

toggle_nested_js = js"""
function() {
    this.parentElement.querySelector(".nested").classList.toggle("active");
    this.classList.toggle("caret-down");
}
"""

function visible_list(text_array::Vector{<:AbstractString})
    list_elements = [node(:li, text) for text in text_array]
    return a_list(list_elements)
end

function nested_list(text_array::Vector{<:AbstractString})
    list_elements = [selectable_list_element(text) for text in text_array]
    return nested_list(list_elements)
end

function nested_list(list_elements::Array{<:Node})
    return dom"""ul.nested[style=$("list-style-type:none")]"""(list_elements...)
end

expand_button() = dom"""span.caret-start"""(dom"b"(">"), events=Dict("click" => toggle_nested_js))

function selectable_text_node(text::String, id::String = string(UUIDs.uuid4()))
    return node(
            :span,
            text,
            className="selectable",
            id=id,
            events=Dict("click" => single_select_js)
            )
end

function selectable_text_node(node::AbstractTreeViewNode)
  return selectable_text_node(label(node), tree_id(node))
end

function selectable_list_element(text::String, id::String = string(UUIDs.uuid4()))
    return dom"""li[style=$("cursor:auto")]"""(selectable_text_node(text, id))
end

function selectable_list_element(node::AbstractTreeViewNode)
    return selectable_list_element(label(node), tree_id(node))
end

function nested_selectable_list(an_array::Array)
    list_elements = [selectable_list_element(elt) for elt in an_array]
    return nested_list(list_elements)
end

function selectable_list(text_array::Array{<:AbstractString})
    list_elements = [selectable_list_element(text) for text in text_array]
    return a_list(list_elements)
end

function a_list(list_elements::Array{<:Node})
    return dom"""ul[style=$("list-style-type:none")]"""(list_elements...)
end

function selectable_list_element_with_list(text::String, list)
    return dom"""li[style=$("cursor:auto")]"""(expand_button(), selectable_text_node(text), list)
end

function selectable_list_element_with_list(node::AbstractTreeViewNode, list)
  return dom"""li[style=$("cursor:auto")]"""(
    expand_button(),
    selectable_text_node(node),
    list,
    )
end

function create_scope(treeview::Node)
    return Scope(dom = Node(:div, nested_style, selected_style, treeview))
end

function tohtml(node::AbstractTreeViewNode)
    if isempty(children(node))
        return selectable_list_element(node)
    else
        return selectable_list_element_with_list(node, nested_list(tohtml.(children(node))))
    end
end

function tohtml(root::TreeViewRoot)
  if isempty(root.nodes)
      error("Empty TreeView")
  end
  return a_list(tohtml.(root.nodes))
end


text:
import JUDI.get_data

export judiVector

function judiVector(con::H5SeisCon)
  nSamp = con.seis.getNSamp()
  nTrc = 0
  for val in con.pkeyvals
    nTrc += con.seis.getPKeyTraceSize(con.pkey, val, val)
  end
  m = nSamp*nTrc
  n = 1
  nsrc = length(con.pkeyvals)

  geometry = Geometry(con)
  dataCell = Array{H5SeisCon,1}(undef, nsrc)
  for i = 1:nsrc
    pkeyvals = Array{Float64,1}(undef,1)
    pkeyvals[1] = con.pkeyvals[i]
    dataCell[i] = H5SeisCon(seis=con.seis,
                            xkey=con.xkey, ykey=con.ykey, zkey=con.zkey,
                            pkey=con.pkey, pkeyvals=pkeyvals)
  end

  return judiVector{Float32, H5SeisCon}("Julia seismic data container",m,n,nsrc,geometry,dataCell)
end

function judiVector(geometry::Geometry, con::H5SeisCon)
  nSamp = con.seis.getNSamp()
  nTrc = 0
  for val in con.pkeyvals
    nTrc += con.seis.getPKeyTraceSize(con.pkey, val, val)
  end
  m = nSamp*nTrc
  n = 1
  nsrc = length(con.pkeyvals)

  # fill data vector with pointers to data location
  dataCell = Array{H5SeisCon,1}(undef, nsrc)
  for j=1:nsrc
      dataCell[j] = split(con,j)
  end

  return judiVector{Float32, H5SeisCon}("Julia seismic data container",m,n,nsrc,geometry,dataCell)
end

function judiVector(geometry::Geometry, conCell::Array{H5SeisCon})
  nSamp = conCell[1].seis.getNSamp()
  nTrc = 0
  nsrc = 0
  for con in conCell
    for val in con.pkeyvals
      nsrc += length(con.pkeyvals)
      nTrc += con.seis.getPKeyTraceSize(con.pkey, val, val)
    end
  end
  m = nSamp*nTrc
  n = 1

  # fill data vector with pointers to data location
  dataCell = Array{H5SeisCon,1}(undef, nsrc)
  for j=1:nsrc
      dataCell[j] = conCell[j]
  end

  return judiVector{Float32, H5SeisCon}("Julia seismic data container",m,n,nsrc,geometry,dataCell)
end

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

vec(x::H5SeisCon) = vec(x[1].data)

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

for opo=[:+, :-, :*, :/]
  @eval begin
      $opo(a::judiVector{avDT, H5SeisCon}, ::T) where {avDT, T<:Real} = throw(DomainError(a, "Addition for OOC judiVectors not supported."))
      $opo(::T, b::judiVector{bvDT, H5SeisCon}) where {bvDT, T<:Real} = throw(DomainError(b, "Addition for OOC judiVectors not supported."))
      $opo(::judiVector{avDT, H5SeisCon}, b::judiVector{bvDT, H5SeisCon}) where {avDT, bvDT} = throw(DomainError(b, "Addition for OOC judiVectors not supported."))
  end
end

for ipop=[:lmul!, :rmul!, :rdiv!, :ldiv!]
  @eval begin
      $ipop(a::judiVector{avDT, H5SeisCon}, ::T) where {avDT, T<:Real} = throw(DomainError(a, "Addition for OOC judiVectors not supported."))
      $ipop(::T, b::judiVector{bvDT, H5SeisCon}) where {bvDT, T<:Real} = throw(DomainError(b, "Addition for OOC judiVectors not supported."))
      $ipop(::judiVector{avDT, H5SeisCon}, b::judiVector{bvDT, H5SeisCon}) where {avDT, bvDT} = throw(DomainError(b, "Addition for OOC judiVectors not supported."))
  end
end

function get_data(x::judiVector{T, H5SeisCon}) where T
  shots = Array{Array{Float32, 2}, 1}(undef, x.nsrc)
  rec_geometry = Geometry(x.geometry)
  for j=1:x.nsrc
      shots[j] = convert(Array{Float32, 2}, x.data[j][1].data)
  end
  return judiVector(rec_geometry, shots)
end


text:
module Types
    import Base.+
    import Base.-
    import Base.*
    import Base.size
    export scaledArray, regImage, + , - , *, t2a

    struct scaledArray
               data::Array{Float64, 1}
               dimensions::Tuple{Vararg{Int64}}
               voxelsize::Array{Float64, 1}
               shift::Array{Float64, 1}
    end

    struct regImage
               data::Array{Float64,2}
               voxelsize::Array{Float64, 1}
               shift::Array{Float64, 1}
    end

    function +(a::scaledArray, b::scaledArray)
        @assert a.voxelsize == b.voxelsize "Array's world matrices must match"
        @assert a.shift == b.shift "Array's world matrices must match"
        return(scaledArray(a.data+b.data, a.dimensions, a.voxelsize, a.shift))
    end

    function +(a::scaledArray, b::Array{Float64,1}) # for derivative check
        return(scaledArray(a.data+b, a.dimensions, a.voxelsize, a.shift))
    end

    function +(b::Number, a::scaledArray ) # for derivative check
        return(scaledArray(a.data+b, a.dimensions, a.voxelsize, a.shift))
    end

    function -(a::scaledArray, b::scaledArray)
        return(a + (-1 * b))
    end

    function *(s::Number, a::scaledArray)
        b = deepcopy(a)
        b.data[:] = s * b.data[:]
        return b
    end

    function size(a::scaledArray)
        return size(a.data)
    end

    function size(a::regImage)
        return size(a.data)
    end

    # helpers to make addition/multiplication of distance and regularizer possible
    function +(a::Function, b::SparseMatrixCSC)
    	f(x) = a(x) + b*x
    	return f
    end
    function +(b::Function, a::Function)
    	f(x) = a(x) + b(x)
    	return f
    end
    function *(a::Number, b::Function)
    	f(x) = a * b(x)
    	return f
    end

    function t2a(t::Tuple)
    	return [a for a in t]
    end

end


text:
# This file contains function for automatically load event data.

"""
URLs
"""
abstract type URLs end

"""
    PublicWyscoutURLs
"""
Base.@kwdef struct PublicWyscoutURLs <: URLs
    competitions_url::String = "https://ndownloader.figshare.com/files/15073685"
    events_url::String = "https://ndownloader.figshare.com/files/14464685"
    matches_url::String = "https://ndownloader.figshare.com/files/14464622"
    players_url::String = "https://ndownloader.figshare.com/files/15073721"
    teams_url::String = "https://ndownloader.figshare.com/files/15073697"
end

"""
    function download_repo()
"""
function download_repo(data_loader::PublicWyscoutURLs, path::String)

    HTTP.download(data_loader.competitions_url, path)
    HTTP.download(data_loader.events_url, path)
    HTTP.download(data_loader.matches_url, path)
    HTTP.download(data_loader.players_url, path)
    HTTP.download(data_loader.teams_url, path)
end


text:
__precompile__(true)
module RobustPCA

# Robust Principal Component Analysis (RPCA) in Julia

# Reference:
# Lin, Zhouchen, Minming Chen, and Yi Ma. "The augmented lagrange multiplier
# method for exact recovery of corrupted low-rank matrices." arXiv preprint
# arXiv:1009.5055 (2010)
# http://arxiv.org/pdf/1009.5055.pdf)

export inexact_alm_rpca

include("inexact_alm.jl")

end # module


text:
@testset "Site counting" begin

    dna_alphabets = (DNAAlphabet{4}, DNAAlphabet{2})
    rna_alphabets = (RNAAlphabet{4}, RNAAlphabet{2})

    @testset "Specific count methods" begin

        function generate_possibilities_tester(::Type{A}) where A <: NucAlphs
            symbols = alphabet(A)
            arra = Vector{eltype(A)}()
            arrb = Vector{eltype(A)}()
            for i in 1:length(symbols), j in i:length(symbols)
                push!(arra, symbols[i])
                push!(arrb, symbols[j])
            end
            return BioSequence{A}(arra), BioSequence{A}(arrb)
        end

        @testset "2 bit" begin
            dnaA, dnaB = generate_possibilities_tester(DNAAlphabet{2})
            rnaA, rnaB = generate_possibilities_tester(RNAAlphabet{2})

            @test count(Conserved, dnaA, dnaB) == count(Conserved, dnaB, dnaA) == 4
            @test count(Mutated, dnaA, dnaB) == count(Mutated, dnaB, dnaA) == 6

            @test count(Conserved, rnaA, rnaB) == count(Conserved, rnaB, rnaA) == 4
            @test count(Mutated, rnaA, rnaB) == count(Mutated, rnaB, rnaA) == 6
        end

        @testset "4 bit" begin
            dnaA, dnaB = generate_possibilities_tester(DNAAlphabet{4})
            rnaA, rnaB = generate_possibilities_tester(RNAAlphabet{4})

            @test count(Conserved, dnaA, dnaB) == count(Conserved, dnaB, dnaA) == (4, 10)
            @test count(Mutated, dnaA, dnaB) == count(Mutated, dnaB, dnaA) == (6, 10)

            @test count(Conserved, rnaA, rnaB) == count(Conserved, rnaB, rnaA) == (4, 10)
            @test count(Mutated, rnaA, rnaB) == count(Mutated, rnaB, rnaA) == (6, 10)
        end
    end

    @testset "Randomized tests" begin
        # A test counting function which is naive.
        @inline function issite(::Type{Conserved}, a::BioSequence, b::BioSequence, idx)
            return a[idx] == b[idx]
        end
        @inline function issite(::Type{Mutated}, a::BioSequence, b::BioSequence, idx)
            return a[idx] != b[idx]
        end
        @inline function testcount2(::Type{P}, a::BioSequence, b::BioSequence) where P <: BioSequences.Position
            k = 0
            @inbounds for idx in 1:min(lastindex(a), lastindex(b))
                k += issite(P, a, b, idx)
            end
            return k
        end
        @inline function testcount(::Type{P}, a::BioSequence, b::BioSequence) where P <: BioSequences.Position
            k, c = 0, 0
            @inbounds for idx in 1:min(lastindex(a), lastindex(b))
                isvalid = !(isgap(a[idx]) || isgap(b[idx])) && !(isambiguous(a[idx]) || isambiguous(b[idx]))
                k += issite(P, a, b, idx) && isvalid
                c += isvalid
            end
            return k, c
        end
        function testcounting(::Type{S}, a::BioSequence, b::BioSequence) where S <: Site
            @test count(S, a, b) == count(S, b, a) == testcount(S, a, b)
        end
        function testcounting2(::Type{S}, a::BioSequence, b::BioSequence) where S <: Site
            @test count(S, a, b) == count(S, b, a) == testcount2(S, a, b)
        end
        function testforencs(a::Int, b::Int, subset::Bool)
            for alphabet in (DNAAlphabet, RNAAlphabet)
                for _ in  1:10
                    seqA = random_seq(alphabet{a}, rand(10:100))
                    seqB = random_seq(alphabet{b}, rand(10:100))
                    sa = seqA
                    sb = seqB
                    if subset
                        intA = random_interval(1, length(seqA))
                        intB = random_interval(1, length(seqB))
                        subA = seqA[intA]
                        subB = seqB[intB]
                        sa = subA
                        sb = subB
                    end
                    if a == 2 && b == 2
                        testcounting2(Conserved, sa, sb)
                        testcounting2(Mutated, sa, sb)
                    else
                        testcounting(Conserved, sa, sb)
                        testcounting(Mutated, sa, sb)
                    end
                end
            end
        end

        @testset "Testing 4-bit seq pairs" begin
            @testset "Full random sequences" begin
                testforencs(4, 4, false)
            end
            @testset "Subset random sequences" begin
                testforencs(4, 4, true)
            end
        end

        @testset "Testing 2-bit seq pairs" begin
            @testset "Full random sequences" begin
                testforencs(2, 2, false)
            end
            @testset "Subset random sequences" begin
                testforencs(2, 2, true)
            end
        end

        @testset "Testing mixed bit seq pairs" begin
            @testset "Full random sequences" begin
                testforencs(4, 2, false)
                testforencs(2, 4, false)
            end
            @testset "Subset random sequences" begin
                testforencs(4, 2, true)
                testforencs(2, 4, true)
            end
        end
    end

    @testset "Pairwise methods" begin
        @testset "4-bit encoded sequences" begin
            dnas = [dna"ATCGCCA-", dna"ATCGCCTA", dna"ATCGCCT-", dna"GTCGCCTA"]
            rnas = [rna"AUCGCCA-", rna"AUCGCCUA", rna"AUCGCCU-", rna"GUCGCCUA"]

            answer_mutated = [(0,0) (1,7) (1,7) (2,7);
                              (1,7) (0,0) (0,7) (1,8);
                              (1,7) (0,7) (0,0) (1,7);
                              (2,7) (1,8) (1,7) (0,0)]

            answer_conserved = [(0,0) (6,7) (6,7) (5,7);
                                (6,7) (0,0) (7,7) (7,8);
                                (6,7) (7,7) (0,0) (6,7);
                                (5,7) (7,8) (6,7) (0,0)]
            for i in (dnas, rnas)
                @test count_pairwise(Mutated, i...) == answer_mutated
                @test count_pairwise(Conserved, i...) == answer_conserved
            end
        end
        @testset "2-bit encoded sequences" begin
            dnas = [BioSequence{DNAAlphabet{2}}("ATCGCCAC"),
                    BioSequence{DNAAlphabet{2}}("ATCGCCTA"),
                    BioSequence{DNAAlphabet{2}}("ATCGCCTT"),
                    BioSequence{DNAAlphabet{2}}("GTCGCCTA")]
            rnas = [BioSequence{RNAAlphabet{2}}("AUCGCCAC"),
                    BioSequence{RNAAlphabet{2}}("AUCGCCUA"),
                    BioSequence{RNAAlphabet{2}}("AUCGCCUU"),
                    BioSequence{RNAAlphabet{2}}("GUCGCCUA")]
            answer_mutated = [0 2 2 3;
                              2 0 1 1;
                              2 1 0 2;
                              3 1 2 0]
            answer_conserved = [0 6 6 5;
                                6 0 7 7;
                                6 7 0 6;
                                5 7 6 0]
            for i in (dnas, rnas)
                @test count_pairwise(Mutated, i...) == answer_mutated
                @test count_pairwise(Conserved, i...) == answer_conserved
            end
        end
    end

    @testset "Windowed methods" begin
        @testset "4-bit encoded sequences" begin
            dnaA = dna"ATCGCCA-M"
            dnaB = dna"ATCGCCTAA"
            rnaA = rna"AUCGCCA-M"
            rnaB = rna"AUCGCCUAA"

            for seqs in ((dnaA, dnaB), (rnaA, rnaB))
                @test count(Conserved, seqs[1], seqs[2], 3, 1) == [IntervalValue(1, 3, (3,3)),
                                                                   IntervalValue(2, 4, (3,3)),
                                                                   IntervalValue(3, 5, (3,3)),
                                                                   IntervalValue(4, 6, (3,3)),
                                                                   IntervalValue(5, 7, (2,3)),
                                                                   IntervalValue(6, 8, (1,2)),
                                                                   IntervalValue(7, 9, (0,1))]
                @test count(Mutated, seqs[1], seqs[2], 3, 1) == [IntervalValue(1, 3, (0,3)),
                                                                 IntervalValue(2, 4, (0,3)),
                                                                 IntervalValue(3, 5, (0,3)),
                                                                 IntervalValue(4, 6, (0,3)),
                                                                 IntervalValue(5, 7, (1,3)),
                                                                 IntervalValue(6, 8, (1,2)),
                                                                 IntervalValue(7, 9, (1,1))]
            end
        end

        @testset "2-bit encoded sequences" begin
            dnaA = BioSequence{DNAAlphabet{2}}("ATCGCCATT")
            dnaB = BioSequence{DNAAlphabet{2}}("ATCGCCTAA")
            rnaA = BioSequence{RNAAlphabet{2}}("AUCGCCAUU")
            rnaB = BioSequence{RNAAlphabet{2}}("AUCGCCUAA")

            for seqs in ((dnaA, dnaB), (rnaA, rnaB))
                @test count(Conserved, seqs[1], seqs[2], 3, 1) == [IntervalValue(1, 3, 3),
                                                                   IntervalValue(2, 4, 3),
                                                                   IntervalValue(3, 5, 3),
                                                                   IntervalValue(4, 6, 3),
                                                                   IntervalValue(5, 7, 2),
                                                                   IntervalValue(6, 8, 1),
                                                                   IntervalValue(7, 9, 0)]
                @test count(Mutated, seqs[1], seqs[2], 3, 1) == [IntervalValue(1, 3, 0),
                                                                 IntervalValue(2, 4, 0),
                                                                 IntervalValue(3, 5, 0),
                                                                 IntervalValue(4, 6, 0),
                                                                 IntervalValue(5, 7, 1),
                                                                 IntervalValue(6, 8, 2),
                                                                 IntervalValue(7, 9, 3)]
            end
        end

        @testset "Mixed encodings" begin
            dnaA = dna"ATCGCCA-M"
            dnaB = BioSequence{DNAAlphabet{2}}("ATCGCCTAA")
            rnaA = rna"AUCGCCA-M"
            rnaB = BioSequence{RNAAlphabet{2}}("AUCGCCUAA")

            for seqs in ((dnaA, dnaB), (rnaA, rnaB))
                @test count(Conserved, seqs[1], seqs[2], 3, 1) == [IntervalValue(1, 3, (3,3)),
                                                                   IntervalValue(2, 4, (3,3)),
                                                                   IntervalValue(3, 5, (3,3)),
                                                                   IntervalValue(4, 6, (3,3)),
                                                                   IntervalValue(5, 7, (2,3)),
                                                                   IntervalValue(6, 8, (1,2)),
                                                                   IntervalValue(7, 9, (0,1))]
                @test count(Mutated, seqs[1], seqs[2], 3, 1) == [IntervalValue(1, 3, (0,3)),
                                                                 IntervalValue(2, 4, (0,3)),
                                                                 IntervalValue(3, 5, (0,3)),
                                                                 IntervalValue(4, 6, (0,3)),
                                                                 IntervalValue(5, 7, (1,3)),
                                                                 IntervalValue(6, 8, (1,2)),
                                                                 IntervalValue(7, 9, (1,1))]
            end
        end
    end
end


text:
module Renderer

export respond, html, json, redirect

import Revise
import HTTP, Reexport, Markdown, Logging, FilePaths
import Genie, Genie.Util, Genie.Configuration, Genie.Exceptions

Reexport.@reexport using Genie.Flax

const Html = Genie.Flax
export Html

const DEFAULT_CHARSET = "charset=utf-8"
const DEFAULT_CONTENT_TYPE = :html
const DEFAULT_LAYOUT_FILE = "app"

const VIEWS_FOLDER = "views"
const LAYOUTS_FOLDER = "layouts"

"""
    const CONTENT_TYPES = Dict{Symbol,String}

Collection of content-types mappings between user friendly short names and
MIME types plus charset.
"""
const CONTENT_TYPES = Dict{Symbol,String}(
  :html       => "text/html; $DEFAULT_CHARSET",
  :plain      => "text/plain; $DEFAULT_CHARSET",
  :text       => "text/plain; $DEFAULT_CHARSET",
  :json       => "application/json; $DEFAULT_CHARSET",
  :js         => "application/javascript; $DEFAULT_CHARSET",
  :javascript => "application/javascript; $DEFAULT_CHARSET",
  :xml        => "text/xml; $DEFAULT_CHARSET",
  :markdown   => "text/markdown; $DEFAULT_CHARSET"
)

"""
    const RENDERERS = Dict

Collection of renderers associated to each supported mime time. Other mime-renderer pairs can be added -
or current ones can be replaced by custom ones, to be used by Genie.
"""
const RENDERERS = Dict(
  MIME"text/html"         => Flax.HTMLRenderer,
  MIME"application/json"  => Flax.JSONRenderer
)

const ResourcePath = Union{String,Symbol}
const HTTPHeaders = Dict{String,String}

const FilePath = FilePaths.PosixPath
const filepath = FilePaths.Path
export FilePath, filepath

"""
    mutable struct WebRenderable

Represents an object that can be rendered on the web as a HTTP Response
"""
mutable struct WebRenderable
  body::String
  content_type::Symbol
  status::Int
  headers::HTTPHeaders
end

"""
    WebRenderable(body::String)

Creates a new instance of `WebRenderable` with `body` as the body of the response and
default content type, no headers, and 200 status code.

#Examples
```jldoctest
julia> Genie.Renderer.WebRenderable("hello")
Genie.Renderer.WebRenderable("hello", :html, 200, Dict{String,String}())
```
"""
WebRenderable(body::String) = WebRenderable(body, DEFAULT_CONTENT_TYPE, 200, HTTPHeaders())

"""
    WebRenderable(body::String, content_type::Symbol)

Creates a new instance of `WebRenderable` with `body` as the body of the response and
`content_type` as the content type, no headers, and 200 status code.

#Examples
```jldoctest
julia> Genie.Renderer.WebRenderable("hello", :json)
Genie.Renderer.WebRenderable("hello", :json, 200, Dict{String,String}())
```
"""
WebRenderable(body::String, content_type::Symbol) = WebRenderable(body, content_type, 200, HTTPHeaders())

"""
    WebRenderable(; body::String = "", content_type::Symbol = DEFAULT_CONTENT_TYPE,
                    status::Int = 200, headers::HTTPHeaders = HTTPHeaders())

Creates a new instance of `WebRenderable` using the values passed as keyword arguments.

#Examples
```jldoctest
julia> Genie.Renderer.WebRenderable()
Genie.Renderer.WebRenderable("", :html, 200, Dict{String,String}())

julia> Genie.Renderer.WebRenderable(body = "bye", content_type = :js, status = 301, headers = Dict("Location" => "/bye"))
Genie.Renderer.WebRenderable("bye", :js, 301, Dict("Location" => "/bye"))
```
"""
WebRenderable(; body::String = "", content_type::Symbol = DEFAULT_CONTENT_TYPE,
                status::Int = 200, headers::HTTPHeaders = HTTPHeaders()) = WebRenderable(body, content_type, status, headers)

"""
    WebRenderable(wr::WebRenderable, status::Int, headers::HTTPHeaders)

Returns `wr` overwriting its `status` and `headers` fields with the passed arguments.

#Examples
```jldoctest
julia> Genie.Renderer.WebRenderable(Genie.Renderer.WebRenderable(body = "good morning", content_type = :js), 302, Dict("Location" => "/morning"))
Genie.Renderer.WebRenderable("good morning", :js, 302, Dict("Location" => "/morning"))
```
"""
function WebRenderable(wr::WebRenderable, status::Int, headers::HTTPHeaders)
  wr.status = status
  wr.headers = headers

  wr
end

function render(::Type{MIME"text/html"}, data::String;
                context::Module = @__MODULE__, layout::Union{String,Nothing} = nothing, vars...) :: WebRenderable
  try
    WebRenderable(RENDERERS[MIME"text/html"].render(data; context = context, layout = layout, vars...) |> Base.invokelatest)
  catch ex
    isa(ex, KeyError) && Flax.changebuilds() # it's a view error so don't reuse them
    rethrow(ex)
  end
end
function render(::Type{MIME"text/html"}, viewfile::FilePath; layout::Union{Nothing,FilePath} = nothing,
                  context::Module = @__MODULE__, vars...) :: WebRenderable
  try
    WebRenderable(RENDERERS[MIME"text/html"].render(viewfile; layout = layout, context = context, vars...) |> Base.invokelatest)
  catch ex
    isa(ex, KeyError) && Flax.changebuilds() # it's a view error so don't reuse them

    rethrow(ex)
  end
end

"""
"""
function html(resource::ResourcePath, action::ResourcePath; layout::ResourcePath = DEFAULT_LAYOUT_FILE,
                context::Module = @__MODULE__, status::Int = 200, headers::HTTPHeaders = HTTPHeaders(), vars...) :: HTTP.Response
  html(FilePaths.Path(joinpath(Genie.config.path_resources, string(resource), VIEWS_FOLDER, string(action)));
        layout = FilePaths.Path(joinpath(Genie.config.path_app, LAYOUTS_FOLDER, string(layout))),
        context = context, status = status, headers = headers, vars...)
end

"""
    html(data::String; context::Module = @__MODULE__, status::Int = 200, headers::HTTPHeaders = HTTPHeaders(), layout::Union{String,Nothing} = nothing, vars...) :: HTTP.Response

Parses the `data` input as HTML, returning a HTML HTTP Response.

# Arguments
- `data::String`: the HTML string to be rendered
- `context::Module`: the module in which the variables are evaluated (in order to provide the scope for vars). Usually the controller.
- `status::Int`: status code of the response
- `headers::HTTPHeaders`: HTTP response headers
- `layout::Union{String,Nothing}`: layout file for rendering `data`

# Example
```jldoctest
julia> html("<h1>Welcome \$(@vars(:name))</h1>", layout = "<div><% @yield %></div>", name = "Adrian")
HTTP.Messages.Response:
"
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8

<html><head></head><body><div><h1>Welcome Adrian</h1>
</div></body></html>"
```
"""
function html(data::String; context::Module = @__MODULE__, status::Int = 200, headers::HTTPHeaders = HTTPHeaders(), layout::Union{String,Nothing} = nothing, vars...) :: HTTP.Response
  WebRenderable(render(MIME"text/html", data; context = context, layout = layout, vars...), status, headers) |> respond
end

"""
    html(viewfile::FilePath; layout::Union{Nothing,FilePath} = nothing,
          context::Module = @__MODULE__, status::Int = 200, headers::HTTPHeaders = HTTPHeaders(), vars...) :: HTTP.Response

Parses and renders the HTML `viewfile`, optionally rendering it within the `layout` file. Valid file formats are `.html.jl` and `.flax.jl`.

# Arguments
- `viewfile::FilePath`: filesystem path to the view file as a `Renderer.FilePath`, ie `Renderer.FilePath("/path/to/file.html.jl")`
- `layout::FilePath`: filesystem path to the layout file as a `Renderer.FilePath`, ie `Renderer.FilePath("/path/to/file.html.jl")`
- `context::Module`: the module in which the variables are evaluated (in order to provide the scope for vars). Usually the controller.
- `status::Int`: status code of the response
- `headers::HTTPHeaders`: HTTP response headers
"""
function html(viewfile::FilePath; layout::Union{Nothing,FilePath} = nothing,
                context::Module = @__MODULE__, status::Int = 200, headers::HTTPHeaders = HTTPHeaders(), vars...) :: HTTP.Response
  WebRenderable(render(MIME"text/html", viewfile; layout = layout, context = context, vars...), status, headers) |> respond
end

### JSON RENDERING ###

function render(::Type{MIME"application/json"}, datafile::FilePath; context::Module = @__MODULE__, vars...) :: WebRenderable
  WebRenderable(RENDERERS[MIME"application/json"].render(datafile; context = context, vars...) |> Base.invokelatest, :json)
end
function render(::Type{MIME"application/json"}, data::String; context::Module = @__MODULE__, vars...) :: WebRenderable
  WebRenderable(RENDERERS[MIME"application/json"].render(data; context = context, vars...) |> Base.invokelatest, :json)
end
function render(::Type{MIME"application/json"}, data::Any) :: WebRenderable
  WebRenderable(RENDERERS[MIME"application/json"].render(data) |> Base.invokelatest, :json)
end

"""
"""
function json(resource::ResourcePath, action::ResourcePath; context::Module = @__MODULE__,
              status::Int = 200, headers::HTTPHeaders = HTTPHeaders(), vars...) :: HTTP.Response
  json(FilePaths.Path(joinpath(Genie.config.path_resources, string(resource), VIEWS_FOLDER, string(action) * RENDERERS[MIME"application/json"].JSON_FILE_EXT));
        context = context, status = status, headers = headers, vars...)
end

"""
"""
function json(datafile::FilePath; context::Module = @__MODULE__,
              status::Int = 200, headers::HTTPHeaders = HTTPHeaders(), vars...) :: HTTP.Response
  WebRenderable(render(MIME"application/json", datafile; context = context, vars...), status, headers) |> respond
end

"""
"""
function json(data::String; context::Module = @__MODULE__,
              status::Int = 200, headers::HTTPHeaders = HTTPHeaders(), vars...) :: HTTP.Response
  WebRenderable(render(MIME"application/json", data; context = context, vars...), status, headers) |> respond
end

"""
"""
function json(data; status::Int = 200, headers::HTTPHeaders = HTTPHeaders()) :: HTTP.Response
  WebRenderable(render(MIME"application/json", data), status, headers) |> respond
end

### REDIRECT RESPONSES ###

"""
Sets redirect headers and prepares the `Response`.
"""
function redirect(location::String, code::Int = 302, headers::HTTPHeaders = HTTPHeaders()) :: HTTP.Response
  headers["Location"] = location
  WebRenderable("Redirecting you to $location", :text, code, headers) |> respond
end
function redirect(named_route::Symbol, code::Int = 302, headers::HTTPHeaders = HTTPHeaders(); route_args...) :: HTTP.Response
  redirect(Genie.Router.linkto(named_route; route_args...), code, headers)
end

"""
    hasrequested(content_type::Symbol) :: Bool

Checks wheter or not the requested content type matches `content_type`.
"""
function hasrequested(content_type::Symbol) :: Bool
  task_local_storage(:__params)[:response_type] == content_type
end

### RESPONSES ###

"""
Constructs a `Response` corresponding to the content-type sof the request.
"""
function respond(r::WebRenderable) :: HTTP.Response
  haskey(r.headers, "Content-Type") || (r.headers["Content-Type"] = CONTENT_TYPES[r.content_type])

  HTTP.Response(r.status, [h for h in r.headers], body = r.body)
end
function respond(response::HTTP.Response) :: HTTP.Response
  response
end
function respond(body::String, params::Dict{Symbol,T})::HTTP.Response where {T}
  r = params[:RESPONSE]
  r.data = body

  r |> respond
end

function respond(err::T, content_type::Union{Symbol,String} = Genie.Router.responsetype(), code::Int = 500) :: T where {T<:Exception}
  T
end

function respond(body::String, content_type::Union{Symbol,String} = Genie.Router.responsetype(), code::Int = 200) :: HTTP.Response
  HTTP.Response(code,
                (isa(content_type, Symbol) ? ["Content-Type" => CONTENT_TYPES[content_type]] : ["Content-Type" => content_type]),
                body = body)
end
function respond(body, code::Int = 200, headers::HTTPHeaders = HTTPHeaders())
  HTTP.Response(code, [h for h in headers], body = string(body))
end
function respond(f::Function, code::Int = 200, headers::HTTPHeaders = HTTPHeaders())
  respond(f(), code, headers)
end

end


text:
# Randomized robust PCA

function rrpca(X,                   # (m,n) matrix
               k::Int=2,            # initial target rank
               λ::Int,              # sparse error term weight
               max_iter::Int=50,    # maximum number of iterations
               tol::Float64=1e-5,   # convergence tolerance
               p::Int=10,           # oversampling parameter
               q::Int=2)            # power scheme parameter

    m, n = size(X)
    μ = 1.25 * norm(X, 2)
    Z = X ./ dualnorm(X)
    S = spzeros(m, n)

    iter = 0
    while !converged() && iter < max_iter
        U, Σ, V = rsvd(X - S + Z ./ μ, k, p, q)
        k, l = predict_rank(Σ, 1 / μ)
        Σ_l = soft_thres(Diagonal(Σ)[1:l], 1 / μ)
        L = U[:, 1:l] * Σ_l * V[:, 1:l]'
        S = soft_thres(X - L + Z ./ μ, λ / μ)
        Z += (X - L - S) .* μ
        iter += 1
    end

    return L, S, X - L - S
end

function predict_rank()
end

function soft_thres()
end

converged(X, L, S, tol) = norm(X - L - S) < tol

function update_mu!()

end


text:
#!/home/arthurb/src/julia/julia

# julia -p NWORKERS ax1.jl params_file FILENAME SUFFIX
# julia -p NWORKERS ax1.jl params_file FILENAME SUFFIX START STOP
# julia -p NWORKERS ax1.jl FS NFFT NW K PVAL FILENAME SUFFIX
# julia -p NWORKERS ax1.jl FS NFFT NW K PVAL FILENAME SUFFIX START STOP
#
# analyze a set of time series with multi-taper spectral analysis and
# create a sparse matrix of just the time-frequency pixels whose F-test
# passes PVAL.
#
# typical usage consists of one or more input files being analyzed by one
# or more parameter sets.  for example, four microphone recordings of the
# same vocalizing mouse analyzed with three different NFFTs and the same
# NW, K, and PVAL.  <filename>.wav yield <filename>-[1-3].ax
#
# FS: sampling rate in Hertz
# NFFT: FFT window size in seconds, rounds up to the next power of 2 tics
# NW: multi-taper time-bandwidth product
# K: number of tapers
# PVAL: F-test p-val threshold
# FILENAME: the full path to a single .wav file containing all channels,
#     or of .ch[0-9] files each with a single channel of float32s
# SUFFIX: a string to append to FILENAME to differentiate parameter sets used
# START,STOP: optional time range, in seconds
#
# output is a binary file with a time x frequency x amplitude x channel
#     array of hot pixels
#
# julia -p 12 ax1.jl 'ultrasonic_params' 'urine' '1'
# julia -p 12 ax1.jl 200e3 32 15 29 0.01 'urine' '1'
# julia -p 12 ax1.jl 450450 32 15 29 0.01 0 30 'groundtruth' '1'

#using MAT
using DSP
using WAV
using HDF5
#using Debug
#using Profile
using Distributions

#using ClusterManagers
#addprocs(10, cman=SGEManager())
#require("/home/arthurb/projects/egnor/ax/ax1b.jl")  # full path on cmd line too

require("ax1b.jl")

#@debug function ax1(ARGS)
function ax1(ARGS)
  tstart=time()

  REMAP={}
  global FS, NFFT, NW, K, PVAL

  local FILELEN_TIC
  local NCHANNELS

  if (length(ARGS)<6)
    require(ARGS[1])
    FILENAME=ARGS[2]
    SUFFIX=ARGS[3]
  else
    FS=ARGS[1]
    NFFT=ARGS[2]
    NW=ARGS[3]
    K=ARGS[4]
    PVAL=ARGS[5]
    FILENAME=ARGS[6]
    SUFFIX=ARGS[7]
  end
  if ((length(ARGS)==5) || (length(ARGS)==9))
    global START
    global STOP
    START=ARGS[end-1]
    STOP=ARGS[end]
  end

  if (isa(FS,String))
    FS = float(FS)
  end
  if (isa(NFFT,String))
    NFFT = int(NFFT)
  end
  if (isa(NW,String))
    NW = int(NW)
  end
  if (isa(K,String))
    K = int(K)
  end
  if (isa(PVAL,String))
    PVAL = float(PVAL)
  end
  if ((length(ARGS)==5) || (length(ARGS)==9))
    if (isa(START,String))
      START = float(START)
    end
    if (isa(STOP,String))
      STOP = float(STOP)
    end
  end

  tmp=splitdir(functionlocs(do_it)[1][1])
  VERSIONAX=readall(`git --git-dir $(tmp[1])/.git log -1 --pretty=format:"%ci %H"`)
  TIMESTAMP=strftime("%Y%m%dT%H%M%S",time())

  SUBSAMPLE=1
  NWORKERS=nworkers()

  FS=int(FS/SUBSAMPLE)

  tmp=log2(NFFT)
  if(abs(tmp-round(tmp))>eps(tmp))
    warn("ax1 will be faster if NFFT is a power of 2")
  end
  tmp=NFFT/2
  if(abs(tmp-round(tmp))>eps(tmp))
    error("NFFT must be even")
  end
  NWINDOWS_PER_WORKER=int(6*256*1000/NFFT)  # NFFT/2 ticks

  tapers = float32(dpss(NFFT,NW))

  f=[0:(NFFT>>1)]*FS/NFFT
  df=f[2]-f[1]

  sig=invlogcdf(FDist(2,2*K-2),log(1-PVAL/NFFT))

  FILEPATH,tmp = splitdir(FILENAME)
  BASEIN,FILETYPE = splitext(tmp)
  FILENAME = joinpath(FILEPATH,BASEIN)
  if isfile(FILENAME*FILETYPE)
    FILEPATH=""
    FILENAMES=[]
    try
      FILELEN_TIC, NCHANNELS=wavread(FILENAME*FILETYPE; format="size")
    catch
      print("can't open file '$FILENAME$FILETYPE'")
      return
    end
    tmp=wavread(FILENAME*FILETYPE; subrange=1)
    if tmp[2]!=FS
      warn("sampling rates in argument list ($FS) and file ($tmp[2]) do not match;  continuing with $tmp[2]")
      FS=tmp[2]
    end
    REMAP=1.0:float(NCHANNELS)
  else
    FILETYPE=".ch"
    tmp = split(readall(`ls $FILEPATH`),"\n")
    tmp2 = map((x) -> ismatch(Regex("$BASEIN.ch[0-9]"),x), tmp)
    FILENAMES=tmp[tmp2]
    NCHANNELS=length(FILENAMES)
    if length(NCHANNELS)==0
      print(["can't find any .ch files with basename '$FILENAME'"])
      return
    end
    for i = 1:NCHANNELS
      filei = string(FILEPATH,"/",FILENAMES[i])
      local fid
      try
        fid = open(filei,"r")
      catch
        print("can't open file '$filei'")
        return
      end
      seekend(fid)
      FILELEN_TIC=int(position(fid)/4)
      close(fid)
      push!(REMAP,float(string(FILENAMES[i][end])))
    end
  end
  FILE_LEN=FILELEN_TIC/FS

  if !isdefined(Main,:START)
    START_TIC=0;
    STOP_TIC=FILELEN_TIC;
  else
    START_TIC=int(START*FS/(NFFT>>1))*(NFFT>>1);
    STOP_TIC=int(STOP*FS);
  end
  tmp=int((STOP_TIC-START_TIC)/(NFFT>>1)-1)
  @printf("Processing %d channels x %.1f min = %d windows = %.1f chunks of data in %s%s\n",
      NCHANNELS, (STOP_TIC-START_TIC)/FS/60, tmp, tmp/NWINDOWS_PER_WORKER, FILENAME, FILETYPE)

  fid = h5open("$FILENAME-$SUFFIX.ax", "w")
  did=d_create(fid,"/hotPixels",datatype(Float32),((1,4),(-1,4)),"chunk",(1024,4))
  attrs(did)["VERSION"] = VERSIONAX
  attrs(did)["TIMESTAMP"] = TIMESTAMP
  attrs(did)["FS"] = FS
  attrs(did)["NFFT"] = NFFT
  attrs(did)["NW"] = NW
  attrs(did)["K"] = K
  attrs(did)["PVAL"] = PVAL
  attrs(did)["FILENAME"] = FILENAME
  attrs(did)["SUFFIX"] = SUFFIX
  if isdefined(Main,:START);  attrs(did)["START"] = START;  end
  if isdefined(Main,:STOP) ;  attrs(did)["STOP"] = STOP  ;  end
  attrs(did)["NWORKERS"] = NWORKERS
  attrs(did)["NWINDOWS_PER_WORKER"] = NWINDOWS_PER_WORKER

  T = [START_TIC : (NFFT>>1)*NWINDOWS_PER_WORKER : STOP_TIC];
  idx = pmap(do_it, [(FILEPATH, FILENAMES, FILENAME, FILETYPE, FILELEN_TIC, NCHANNELS, t,
     NWINDOWS_PER_WORKER, NFFT, PVAL, FS, tapers, sig) for t in T])

  t=1
  k=0
  tloop=time()
  for i in idx
    if (time()-tloop)>10
      @printf("%d sec processed;  %d%% done\n",
          int((T[t]-START_TIC)/FS), int(100*(T[t]-START_TIC)/(STOP_TIC-START_TIC)))
      tloop=time()
    end

#@bp
    sz=size(i,1)
    set_dims!(did,(k+sz,4))
    did[k+(1:sz),1:3]=i[:,1:3]
    did[k+(1:sz),4]=REMAP[i[:,4]]
    k+=sz
    t+=1
  end

  close(fid)

  tstop = time() - tstart
  @printf("Run time was %f minutes.\n",tstop/60)
end

#@iprofile clear
ax1(ARGS)
#@iprofile report

max_stars_repo_name: JaneliaSciComp/Ax

max_issues_repo_name: JaneliaSciComp/Ax

max_forks_repo_name: JaneliaSciComp/Ax


text:
# module SQLite_Octo.SQL

function connect(; kw...)::SQLiteAdapter
    options = Dict(kw)
    SQLiteAdapter(SQLite.DB(options[:db]))
end


text:
using JuMP; using NCDatasets; using AxisArrays; using CalliopeJuMP.Util;

function load_export_constraints(model_dict)
    sets = model_dict["sets"]
    constraint_dict = Dict()
    backend_model = model_dict["backend_model"]

    if haskey(sets, "loc_carriers_update_system_balance_constraint")
        for loc_carrier=sets["loc_carriers_update_system_balance_constraint"], timestep=sets["timesteps"]
            update_system_balance_constraint(model_dict, loc_carrier, timestep)
        end
    end

    if haskey(sets, "loc_tech_carriers_export_balance_constraint")
        constraint_dict["export_balance_constraint"] = (
            build_constraint(["loc_tech_carriers_export_balance_constraint", "timesteps"],
            "export_balance", model_dict
            )
        )
    end

    if haskey(sets, "loc_techs_update_costs_var_constraint")
        for cost=sets["costs"], loc_tech=sets["loc_techs_update_costs_var_constraint"], timestep=sets["timesteps"]
            update_costs_var_constraint(model_dict, cost, loc_tech, timestep)
        end
    end

    if haskey(sets, "loc_tech_carriers_export_max_constraint")
        constraint_dict["export_max_constraint"] = (
            build_constraint(["loc_tech_carriers_export_max_constraint", "timesteps"],
            "export_max", model_dict
            )
        )
    end

    return constraint_dict
end

function update_system_balance_constraint(model_dict, loc_carrier, timestep)
    """
    Update system balance constraint (from energy_balance.py) to include export
    """
    system_balance = model_dict["expressions"]["system_balance"]
    sets = model_dict["sets"]
    prod, con, exporting = get_loc_tech_carriers(model_dict, loc_carrier)

    append!(system_balance[loc_carrier, timestep],
        -1 * sum(model_dict["variables"]["carrier_export"][loc_tech_carrier, timestep]
                 for loc_tech_carrier in exporting)
    )
    loc_carrier_index = findin(sets["loc_carriers"], [loc_carrier])
    timestep_index = findin(sets["loc_carriers"], [timestep])
    model_dict["constraints"]["system_balance_constraint"][loc_carrier_index, timestep_index] = (
        @constraint(model_dict["backend_model"],
            system_balance[loc_carrier, timestep] == 0)
    )
end

function export_balance_constraint_rule(backend_model, set_indices, model_dict)
    """
    Ensure no technology can "pass" its export capability to another technology
    with the same carrier_out, by limiting its export to the capacity of its production
    """
    loc_tech_carrier, timestep = set_indices
    return @constraint(backend_model,
        model_dict["variables"]["carrier_prod"][loc_tech_carrier, timestep] >=
        model_dict["variables"]["carrier_export"][loc_tech_carrier, timestep]
    )

end

function update_costs_var_constraint(model_dict, cost, loc_tech, timestep)
    """
    Update time varying cost constraint (from costs.py) to include export
    """
    parameters = model_dict["parameters"]
    sets = model_dict["sets"]
    cost_var_rhs = model_dict["expressions"]["cost_var_rhs"]
    loc_tech_carrier = parameters["lookup_loc_techs_export"][loc_tech]
    weight = parameters["timestep_weights"][timestep]

    cost_export = (
        get_param(model_dict, "cost_export", [loc_tech, cost], timestep)
        * model_dict["variables"]["carrier_prod"][loc_tech_carrier, timestep]
        * weight
    )

    loc_tech_index = findin(sets["loc_carriers"], [loc_tech])
    cost_index = findin(sets["loc_carriers"], [cost])
    timestep_index = findin(sets["loc_carriers"], [timestep])
    append!(model_dict["expressions"]["cost_var_rhs"][cost, loc_tech, timestep], cost_export)
    model_dict["constraints"]["cost_var_constraint"][cost_index, loc_tech_index, timestep_index] = (
        @constraint(model_dict["backend_model"],
            model_dict["variables"]["cost_var"][cost, loc_tech, timestep] ==
            model_dict["expressions"]["cost_var_rhs"][cost, loc_tech, timestep])
    )
end

function export_max_constraint_rule(backend_model, set_indices, model_dict)
    """
    Set maximum export. All exporting technologies.
    """
    loc_tech_carrier, timestep = set_indices
    loc_tech = get_loc_tech(loc_tech_carrier)
    sets = model_dict["sets"]
    variables = model_dict["variables"]

    if loc_tech_is_in(sets, loc_tech, "loc_techs_milp")
        operating_units = variables["operating_units"][loc_tech, timestep]
    else
        operating_units = 1
    end

    export_cap = get_param(model_dict, "export_cap", [loc_tech])

    return @constraint(backend_model,
        variables["carrier_export"][loc_tech_carrier, timestep] <=
        export_cap * operating_units
    )
end

max_stars_repo_name: calliope-project/CalliopeJuMP

max_issues_repo_name: calliope-project/CalliopeJuMP

max_forks_repo_name: calliope-project/CalliopeJuMP


text:
#=
The edit distance between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other. For example, the edit distance between “kitten” and “sitting” is three: substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”.

Given two strings, compute the edit distance between them.
=#

function editdistance(string1::String, string2::String)
    if length(string1) <= length(string2)
        string_to_change = string1
        final_string = string2
    else
        string_to_change = string2
        final_string = string1
    end
    edit_distance = 0
    char_index = 1
    while string_to_change != final_string
        if char_index > length(string_to_change)
            string_to_change = string_to_change * final_string[char_index:char_index]
            edit_distance = edit_distance + 1
        elseif string_to_change[char_index] != final_string[char_index]
            string_to_change = final_string[1:char_index] * string_to_change[char_index+1:end]
            edit_distance = edit_distance + 1
        end
        char_index = char_index + 1
    end
    return edit_distance
end

max_stars_repo_name: DominiqueCaron/daily-coding-problem

max_issues_repo_name: DominiqueCaron/daily-coding-problem

max_forks_repo_name: DominiqueCaron/daily-coding-problem


text:
using Test

for testscen in 1:2
    valdir, scenario, use_permafrost, use_seaice = get_scenario(testscen)
    println(scenario)

    m = test_page_model()
    include("../src/components/RCPSSPScenario.jl")
    include("../src/components/Population.jl")

    scenario = addrcpsspscenario(m, scenario)
    population = addpopulation(m)

    population[:y_year_0] = 2015.
    population[:y_year] = Mimi.dim_keys(m.md, :time)
    population[:popgrw_populationgrowth] = scenario[:popgrw_populationgrowth]

    p = load_parameters(m)

    set_leftover_params!(m, p)

    run(m)

    # Generated data
    pop = m[:Population, :pop_population]

    # Recorded data
    pop_compare = readpagedata(m, "test/validationdata/$valdir/pop_population.csv")

    @test pop ≈ pop_compare rtol = 1e-3
end


text:
struct FileFormatError <: Exception
    message::AbstractString
end

struct ConfigError <: Exception
    message::AbstractString
end

struct ReaderConfig
    filename::AbstractString
    encoding::AbstractString
    chunk_size::Int64
    convert_dates::Bool
    include_columns::Vector
    exclude_columns::Vector
    string_array_fn::Dict{Symbol, Function}
    number_array_fn::Dict{Symbol, Function}
    column_types::Dict{Symbol, Type}
    verbose_level::Int64
end

struct Column
    id::Int64
    name::AbstractString
    label::Vector{UInt8}  # really?
    format::AbstractString
    coltype::UInt8
    length::Int64
end

struct ColumnNamePointer
    index::Int
    offset::Int
    length::Int
end

# technically these fields may have lower precision (need casting?)
struct SubHeaderPointer
    offset::Int64
    length::Int64
    compression::Int64
    shtype::Int64
end

mutable struct Handler
    io::IOStream
    config::ReaderConfig

    compression::UInt8
    column_names_strings::Vector{Vector{UInt8}}
    column_names::Vector{AbstractString}
    column_symbols::Vector{Symbol}
    column_types::Vector{UInt8}
    column_formats::Vector{AbstractString}
    columns::Vector{Column}

    # column indices being read/returned
    # tuple of column index, column symbol, column type
    column_indices::Vector{Tuple{Int64, Symbol, UInt8}}
    column_name_pointers::Vector{ColumnNamePointer}

    current_page_data_subheader_pointers::Vector{SubHeaderPointer}
    cached_page::Vector{UInt8}
    column_data_lengths::Vector{Int64}
    column_data_offsets::Vector{Int64}
    current_row_in_file_index::Int64
    current_row_in_page_index::Int64

    file_endianness::Symbol
    sys_endianness::Symbol
    byte_swap::Bool

    U64::Bool
    int_length::Int8
    page_bit_offset::Int8
    subheader_pointer_length::UInt8

    file_encoding::AbstractString
    platform::AbstractString
    name::Union{AbstractString,Vector{UInt8}}
    file_type::Union{AbstractString,Vector{UInt8}}

    date_created::DateTime
    date_modified::DateTime

    header_length::Int64
    page_length::Int64
    page_count::Int64
    sas_release::Union{AbstractString,Vector{UInt8}}
    server_type::Union{AbstractString,Vector{UInt8}}
    os_version::Union{AbstractString,Vector{UInt8}}
    os_name::Union{AbstractString,Vector{UInt8}}

    row_length::Int64
    row_count::Int64
    col_count_p1::Int64
    col_count_p2::Int64
    mix_page_row_count::Int64
    lcs::Int64
    lcp::Int64

    current_page_type::Int64
    current_page_block_count::Int64       # number of records in current page
    current_page_subheaders_count::Int64
    column_count::Int64
    # creator_proc::Union{Void, Vector{UInt8}}

    byte_chunk::Dict{Symbol, Vector{UInt8}}
    string_chunk::Dict{Symbol, AbstractVector{String}}
    current_row_in_chunk_index::Int64

    current_page::Int64
    vendor::UInt8
    use_base_transcoder::Bool

    string_decoder_buffer::IOBuffer
    string_decoder::StringDecoder

    column_types_dict::CIDict{Symbol,Type}

    Handler(config::ReaderConfig) = new(
        Base.open(config.filename),
        config)
end


text:
# --- Calling Embedded Julia from MATLAB --- #

const catching_interrupts = Ref(false)

# entry point for MATLAB calling Julia
function jl_mex(plhs::Vector{Ptr{Cvoid}}, prhs::Vector{Ptr{Cvoid}})
    if ENABLE_INTERRUPTS && !catching_interrupts[]
        try
            catching_interrupts[] = true
            jl_mex_outer(plhs, prhs)
        finally
            catching_interrupts[] = false
        end
    else
        jl_mex_inner(plhs, prhs)
    end
end

# enables interrupt processing
function jl_mex_outer(plhs::Vector{Ptr{Cvoid}}, prhs::Vector{Ptr{Cvoid}})
    # do the desired computation
    main_task = @async jl_mex_inner(plhs, prhs)

    # check for interrupt
    interrupt_handler = @async begin
        while !Base.istaskdone(main_task)
            if is_interrupt_pending()
                Base.throwto(main_task, InterruptException())
            end
            yield()
        end
    end

    # wait for main task to finish
    wait(main_task)
end

# runs julia function with mex function inputs, catching errors if they occur
function jl_mex_inner(plhs::Vector{Ptr{Cvoid}}, prhs::Vector{Ptr{Cvoid}})

    # number of outputs
    nlhs = length(plhs)

    for i = 1:nlhs
        # default output is boolean false
        none = MATLAB.mxarray(false);
        # transfer ownership to MATLAB
        none.own = false
        # give pointer to MATLAB
        plhs[i] = none.ptr
    end

    # try running julia code, while capturing exceptions
    try

        # extract function and arguments (function in first slot, arguments in remaining slots)
        fun = Core.eval(Main, Meta.parse(MATLAB.jstring(MATLAB.MxArray(prhs[1], false))))
        args = MATLAB.MxArray.(prhs[2:end], false)

        # call Julia function
        out = fun(args)

        # determine whether results should be iterated over
        iter = nlhs - 1 > 1

        # transfer each output to MATLAB
        if iter
            for i = 1:nlhs-1
                # create MATLAB array for output
                mx = MATLAB.mxarray(out[i])
                # transfer ownership to MATLAB
                mx.own = false
                # give pointer to MATLAB
                plhs[1+i] = mx.ptr
            end
        else
            # create MATLAB array for output
            mx = MATLAB.mxarray(out)
            # transfer ownership to MATLAB
            mx.own = false
            # give pointer to MATLAB
            plhs[2] = mx.ptr
        end

    catch exn

        # get backtrace
        bt = catch_backtrace()

        # create MATLABException from exception and backtrace
        mexn = MatlabException(exn, bt)

        # return MATLABexception in first output slot
        plhs[1] = mexn.ptr

    end

end

# --- Functions Used by MATLAB when calling embedded Julia --- #

# evaluates a Julia expressions
function jl_eval(exprs::Vector{MATLAB.MxArray})
    return Core.eval(Main, Meta.parse(MATLAB.jstring(only(exprs))))
end

# Call a julia function, possibly with keyword arguments.
#
# The values in the args array are interpreted as follows:
#      Index = Meaning
# -----------------------------------------------------------
#          1 = the function to call
#          2 = an integer, npos, the number of positional arguments
#   3:2+npos = positional arguments
# 3+npos:end = keyword arguments, in keyword/value pairs
#
# If npos < 0, all arguments are assumed positional.
function jl_call_kw(args::Vector{MATLAB.MxArray})

    # process arguments
    nargs = length(args)

    # first argument is the function
    func = Meta.parse(MATLAB.jstring(args[1]))

    # second argument is the number of positional arguments
    npos = Int(MATLAB.jscalar(args[2]))::Int

    # construct the call expression
    expr = Expr(:call, func)

    # add positional arguments
    npos = npos < 0 ? nargs - 2 : npos # if npos is negative, all arguments are positional
    for i = 1:npos
        push!(expr.args, MATLAB.jvalue(args[2+i]))
    end

    # add keyword arguments
    nkw = div(nargs - 2 - npos, 2)
    for i = 1:nkw
        # assemble the key-value pair
        kw = Symbol(MATLAB.jstring(args[2+npos+(2*i-1)]))
        val = MATLAB.jvalue(args[2+npos+(2*i)])
        push!(expr.args, Expr(:kw, kw, val))
    end

    # return the evaluated expression
    return Core.eval(Main, expr)
end

# used for mimicing a basic Julia repl from the MATLAB console
input(prompt="julia> ") = call_matlab(1, "input", prompt, "s")[1]

max_stars_repo_name: taylormcd/mexjulia

max_issues_repo_name: taylormcd/mexjulia

max_forks_repo_name: taylormcd/mexjulia


text:
module Rotations

Rx(θ::Real) = begin
    s = sin(θ)
    c = cos(θ)
    [1 0 0;
     0 c -s;
     0 s c]
end

Ry(θ::Real) = begin
    s = sin(θ)
    c = cos(θ)
    [c 0 s;
     0 1 0;
     -s 0 c]
end

Rz(θ::Real) = begin
    s = sin(θ)
    c = cos(θ)
    [c -s 0;
     s c 0;
     0 0 1]
end

end


text:
module Elements

import Genie
using Stipple

import Genie.Renderer.Html: HTMLString, normal_element

export root, elem, @iif, @elsiif, @els, @text, @bind, @data

#===#

function root(app::M)::String where {M<:ReactiveModel}
  Genie.Generator.validname(typeof(app) |> string)
end

function root(app::Type{M})::String where {M<:ReactiveModel}
  Genie.Generator.validname(app |> string)
end

function elem(app::M)::String where {M<:ReactiveModel}
  "#$(root(app))"
end

#===#

function vue_integration(model::M; vue_app_name::String, endpoint::String, channel::String)::String where {M<:ReactiveModel}
  vue_app = replace(Genie.Renderer.Json.JSONParser.json(model |> Stipple.render), "\"{" => " {")
  vue_app = replace(vue_app, "}\"" => "} ")

  output = "var $vue_app_name = new Vue($vue_app);\n\n"

  for field in fieldnames(typeof(model))
    output *= Stipple.watch(vue_app_name, getfield(model, field), field, channel, model)
  end

  output *= "\n\nwindow.parse_payload = function(payload){ window.$(vue_app_name)[payload.key] = payload.value; };"
end

#===#

macro iif(expr)
  "v-if='$(startswith(string(expr), ":") ? string(expr)[2:end] : expr)'"
end

macro elsiif(expr)
  "v-else-if='$(startswith(string(expr), ":") ? string(expr)[2:end] : expr)'"
end

macro els(expr)
  "v-else='$(startswith(string(expr), ":") ? string(expr)[2:end] : expr)'"
end

macro text(expr)
  directive = occursin(" | ", string(expr)) ? ":text-content.prop" : "v-text"
  "$(directive)='$(startswith(string(expr), ":") ? string(expr)[2:end] : expr)'"
end

macro bind(expr)
  "v-model='$(startswith(string(expr), ":") ? string(expr)[2:end] : expr)'"
end

macro data(expr)
  :(Symbol($expr))
end

#===#

include(joinpath("elements", "stylesheet.jl"))

end


text:
using Test, LinearAlgebra, SparseArrays, Random
using QDLDL
rng = Random.MersenneTwister(0706)

@testset "linear solves" begin

  m = 20
  n = 30

  A = random_psd(n)
  B = sprandn(m,n,0.2)
  C = -random_psd(m)
  M = [A B' ; B C]

  b = randn(m+n)
  F = qdldl(M)

  #basic solve
  @test norm(F\b -  M\b, Inf) <= 1e-10

  #solve function
  @test norm(solve(F,b) -  M\b, Inf) <= 1e-10

  #solve in place
  x = copy(b)
  solve!(F,x)
  @test norm(x -  M\b, Inf) <= 1e-10

  #scalar system
  A = sprandn(1,1,1.)
  b = randn(1)
  @test norm(qdldl(A)\b -  A\b, Inf) <= 1e-10

  #32 bit floats, 32 bit ints
  b = randn(m+n)
  M32 = SparseMatrixCSC{Float32,Int32}(M)
  b32 = Vector{Float32}(b)
  @test norm(qdldl(M32)\b32 -  M\b, Inf) <= 1e-6

  #32 bit floats, 64 bit ints
  Mx = SparseMatrixCSC{Float32,Int64}(M)
  @test norm(qdldl(Mx)\b32 -  M\b, Inf) <= 1e-6

  #64 bit floats, 32 bit ints
  Mx = SparseMatrixCSC{Float64,Int32}(M)
  @test norm(qdldl(Mx)\b -  M\b, Inf) <= 1e-6

end

nothing


text:
using Primes

function squarefree_sum(rows)
    numbers = Set(binomial(n, k) for n in 1:rows for k in 0:Int(floor(n/2)))

    limit = Int(floor(sqrt(maximum(numbers))))
    squares = primes(limit).^2

    println(sum(square_check(num, squares) for num in numbers))
end

function square_check(num, squares)
    for square in squares
        if num % square == 0
            return 0
        end

        if square > num
            return num
        end
    end

    return num
end

squarefree_sum(50)

max_stars_repo_name: HarrisonGreen/Project-Euler-Solutions

max_issues_repo_name: HarrisonGreen/Project-Euler-Solutions

max_forks_repo_name: HarrisonGreen/Project-Euler-Solutions