text:
using PGFPlots
const PGF = PGFPlots

color = ["magenta", "orange", "cyan"];
p_energy = [PGF.Plots.Linear([j * timestep[i] for j = 1:length(e)], e .- e[1], legendentry="h=$(timestep[i])", mark="none",style="color="*color[i]*", line width=2pt, solid") for (i, e) in enumerate(me)]

a1 = Axis(vcat(p_energy...),
    xmin=0,
    xmax=10.0,
    hideAxis=false,
	ylabel="energy drift",
	xlabel="time (s)",
    legendPos="south east")
dir = joinpath("/home/taylor/Downloads")
PGF.save(joinpath(dir, "energy_drift.tikz"), a1)

##########
# Visualization
color = RGBA(255.0/255.0,0.0,255.0,1.0);
z = get_maximal_state(storage)
z = [[z[1] for t = 1:100]..., z..., [z[end] for t = 1:100]...]
build_robot(mech, vis=vis, color=color)
T = length(z)
anim = MeshCat.Animation(convert(Int, floor(1.0 / timestep0)))
for t = 1:T
    MeshCat.atframe(anim, t) do
        set_robot(vis, mech, z[t])
    end
end
MeshCat.setanimation!(vis, anim)
setvisible!(vis["/Axes"], false)
setvisible!(vis["/Grid"], false)

# Ghost
t = 400 # 225, 150, 1
set_robot(vis, mech, z[t])
##########


text:
# Gadfly.weave implements a system for executable documentation, report
# generation, literate programming, and such.
# It works as follows:
#   1. Input is parsed by pandoc into JSON.
#   2. Weave executes the code blocks found in the JSON data, and inserts the
#      results back into the JSON representation.
#   3. This processed JSON representation is fed back into pandoc, and output in
#      the desired format.
#
# Pandoc's extensions to markdown are used to, e.g., tag code blocks as
# producing image data, or those that should not be executed.

export weave
import Base.parse

# A special module in which a documents code is executed.
module WeaveSandbox
    # Replace OUTPUT_STREAM references so we can capture output.
    OUTPUT_STREAM = IOString()
    print(x) = Base.print(OUTPUT_STREAM, x)
    println(x) = Base.println(OUTPUT_STREAM, x)

    # Output
    MIME_OUTPUT = Array(Tuple, 0)
    emit(mime, data) = push!(MIME_OUTPUT, (mime, data))
end

# Super-simple pandoc interface.
function pandoc(infn, infmt::String, outfmt::String, args::String...)
    cmd = ByteString["pandoc",
                     "--from=$(infmt)",
                     "--to=$(outfmt)"]
    for arg in args
        push!(cmd, arg)
    end

    readall(infn |> Cmd(cmd))
end

# An iterator for the parse function: parsit(source) will iterate over the
# expressiosn in a string.
type ParseIt
    value::String
end

## iterate over (cmd, expr)
parseit(value::String) = ParseIt(value)
start(it::ParseIt) = 1
function next(it::ParseIt, pos)
    (ex,newpos) = Base.parse(it.value, pos)
    ((it.value[pos:(newpos-1)], ex), newpos)
end
done(it::ParseIt, pos) = pos > length(it.value)

# Means to format output for including in results
# export so user can write methods for other outputs
export gadfly_format
gadfly_format(x...) = string(x...)
gadfly_format(x::Vector) = string(reshape(x, (1, length(x))))

# comment output ala knitr
commentify(x::String) = "## " * replace(chomp(x), "\n", "\n## ")
commentify(x) = x

# Execute an executable document.
#
# Code blocks within their own paragraph are executed, and their output is
# inserted into the document. This behavior is controlled by attributes assigned
# to the block.
#
# Args:
#   infn: Input filename.
#   infmt: Pandoc-compatible input format. E.g., markdown, rst, html, json, latex.
#   outfmt: Pandoc-compatibly output format.
#   pandoc_args: Extra arguments passed to pandoc.
#
# Returns:
#   A string in the requested output format,
#
function weave(infn::String, infmt::String, outfmt::String,
               pandoc_args::String...; debug::Bool=false)
    selfcontained = any([pandoc_arg == "--self-contained"
                         for pandoc_arg in pandoc_args])

    # Substitute the default emitters for ones that simply print the image data.
    # Weave will detect the type of the data and embed it appropriately.
    for mime in ["image/svg+xml", "image/png", "image/gif", "image/jpeg",
                 "application/javascript"]
        Compose.emitters[mime] = data -> WeaveSandbox.emit(mime, data)
    end

    docname = match(r"^(.*)(\.[^\.]*)$", basename(infn)).captures[1]
    metadata, document = JSON.parse(pandoc(infn, infmt, "json"))

    # document is an array of singeton dictionaries. The one key gives the block
    # type, while the format of the value depends on the type.

    # Return a true/false for an attribute with the given default value.
    function attrib_bool(keyvals::Dict, key, default::Bool)
        haskey(keyvals, key) ? lowercase(strip(keyvals[key])) != "false" : default
    end

    processed_document = {}
    fignum = WeakRef(0)

    for block in document
        if debug
            println("Block: ", block)
            println("---")
        end

        if !isa(block, Dict) || !haskey(block, "CodeBlock")
            push!(processed_document, process_block(block))
            continue
        end

        # Process code blocks
        attribs, source = [v for v in values(block)][1]
        id, classes, keyvals = attribs
        classes = Set(classes...)
        keyvals = [k => v for (k,v) in keyvals]

        if !attrib_bool(keyvals, "execute", true)
            push!(processed_document, block)
            continue
        end

        # dispatch on the block type, defaulting to julia

        if contains(classes, "graphviz")
            mime, output = execblock_graphviz(source)
        elseif contains(classes, "latex")
            mime, output = execblock_latex(source)
        else
            mime, output, results = execblock_julia(source)
        end

## commands to pass to manage what is printed. Default is opposite.
#         hide=true  execute=false output=false   results=false commands=false asis=true
# cmd        no          yes (block)  yes          yes             no           yes
# results    no           no          yes           no            yes          asis
# output    yes           no           no          yes            yes           yes

        if !attrib_bool(keyvals, "hide", false)
            for (cmd, expr, result) in results
                if attrib_bool(keyvals, "commands", true)
                    push!(processed_document, ["CodeBlock"=>{{"", {"julia"}, {}}, cmd}])
                end

                ## print result if there
                res = gadfly_format(result)
                if  attrib_bool(keyvals, "results", true) &&
                    isa(expr, Expr) && expr.head != :toplevel &&
                        !isa(result, Nothing) &&
                    !isa(result, Function) &&
                    length(res) > 0

                    ## asis. Can be used to return raw output from julia
                    if attrib_bool(keyvals, "asis", false)
                        push!(processed_document,
                              ["RawBlock"=>["html",gadfly_format(result)]])
                    else
                        out = commentify(res)
                        push!(processed_document, ["CodeBlock"=>{{"", {"julia"}, {}}, out}])
                    end

                end
            end
        end

        if attrib_bool(keyvals, "output", true)
            push!(processed_document,
                  process_output(mime, output, id, classes, keyvals,
                                 outfmt, docname, fignum, selfcontained)...)
        end
    end

    jsonout_path, jsonout = mktemp()
    JSON.print(jsonout, {metadata, processed_document})
    flush(jsonout)
    output = pandoc(jsonout_path, "json", outfmt, pandoc_args...)
    close(jsonout)
    rm(jsonout_path)
    output
end

## loop over values to replace inline code
function process_block(p::Dict)
    for (k,v) in p
        if k == "Code"
            lang = v[1][2]
            if length(lang) > 0 && lang[1] == "julia"
                out = eval(WeaveSandbox, parse(v[2]))
                p[k][2] = string(out)
            end
        else
            p[k] = process_block(v)
        end
    end
    p
end

function process_block(p::Array)
    for i in 1:length(p)
        p[i] = process_block(p[i])
    end
    p
end
process_block(p::Any) = p

# Detect data type.
#
# This is a sparse and crude version of the unix file command which takes a
# chunk of data and tries to figure out what it is. The only classification we
# need to do is between text, svg, and images types. Everything else is just
# binary noise which we should avoid outputting.
#
# Args:
#   data: Data with a type to be detected.
#
# Returns:
#   A mime type, or "binary" for unknown binary output.
#
function datatype(data::Vector{Uint8})
    const magic_numbers =
        [(Uint8['G', 'I', 'F', '8'], "image/gif"),
         (Uint8[0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a], "image/png"),
         (Uint8[0xff, 0xd8], "image/jpeg")]

    for (magic, mime) in magic_numbers
        if length(data) >= length(magic) && data[1:length(magic)] == magic
            return mime
        end
    end

    # Note: this falsely detects utf16 data as binary. Also, "binary" is not a
    # real mime type.
    if has(data, uint8('\0'))
        return "binary"
    end

    xml_magic = "<?xml"
    html_magic = "<!DOCTYPE html"
    if length(data) >= length(xml_magic) &&
       bytestring(data[1:length(xml_magic)]) == xml_magic &&
       !is(match(r"<svg", bytestring(data)), nothing)
        "image/svg+xml"
    elseif !is(match(r"^\s*<!DOCTYPE\s+html", bytestring(data)), nothing)
        "text/html"
    else
        "text/plain"
    end
end

# Generate JSON for the output of an executed code block.
#
# Args:
#   output: The output data.
#   id: ID of the code block.
#   classes: The set of classes assigned to the code block.
#   keyval: Dictionary of key/value attributes assigned to the code block.
#   docname: Name of the document.
#   fignum: Reference to a number for the next figure.
#   selfcontained: True if output should be inserted without generating any
#                  external files.
#
# Return:
#   An array of JSON elements that will be inserted directly after the code
#   block that was executed.
#
function process_output(mime::String, output::Vector{Uint8},
                        id::String, classes::Set, keyvals::Dict,
                        outfmt::String, docname::String, fignum::WeakRef,
                        selfcontained::Bool)

    if isempty(output)
        []
    elseif mime == "binary"
        warn("Skipping unknown binary data produced by a code block.")
        []
    elseif mime == "text/plain"
        out = commentify(bytestring(output))
        [["CodeBlock" => {{"", {"julia_output"}, {}}, out}]]
    elseif mime == "application/javascript" || !is(match(r"^image", mime), nothing)

        fignum.value += 1

        figname = isempty(id) ? "fig_$(fignum.value)" : id
        if has(keyvals, "alt")
            caption = alttext = @sprintf("Figure %d: %s", fignum.value, keyvals["alt"])
        else
            alttext = "Figure $(fignum.value)"
            caption = ""
        end

        if selfcontained
            if mime == "image/svg+xml" && (outfmt == "html" || outfmt == "html5")
                # TODO: Thides works on firefox and safari, but is somewhat broken
                # on chrome. It seems to be a chrome bug, but I need to
                # investigate more.
                svgdata = bytestring(encode(Base64, output))
                [["RawBlock" =>
                    ["html",
                     "

                      <object alt=\"$(alttext)\" type=\"image/svg+xml\"
                              data=\"data:image/svg+xml;base64,$(svgdata)\">
                      </object>

"]]]
            elseif mime == "application/javascript"
                jsdata = bytestring(output)
                parent_id = string(figname, "_container")
                [["RawBlock" =>
                    ["html",
                    """

                    """
                      ]]]
            else
                # Let pandoc handle binary image formats.
                figfn, figio = mktemp()
                write(figio, output)
                close(figio)
                [["Para" => {["Image" => {{["Str" => alttext]}, {figfn, ""}}]}]]
            end
        else
            figext = ""
            if mime == "image/svg+xml"
                figext = "svg"
            elseif mime == "image/gif"
                figext = "gif"
            elseif mime == "image/png"
                figext = "png"
            elseif mime == "image/jpeg"
                figext = "jpg"
            elseif mime == "application/javascript"
                figext = "js"
            end

            figfn = "$(docname)_$(figname).$(figext)"
            figio = open(figfn, "w")
            write(figio, output)
            close(figio)
            figurl = figfn # TODO: support adding an absolute path

            if mime == "image/svg+xml" && (outfmt == "html" || outfmt == "html5")
                # SVG needs to be included using the object (or embed) tag for
                # embeded javascript to work.
                [["RawBlock" =>
                    ["html",
                     "
<object alt=\"$(alttext)\" \
                                      data=\"$(figurl)\" \
                                      type=\"image/svg+xml\"></object> \

$(caption)

"]]]
            elseif mime == "application/javascript"
                parent_id = string(figname, "_container")
                [["RawBlock" =>
                    ["html",
                        "

                         "]]]
            else
                [["Para" => {["Image" => {{["Str" => alttext]}, {figurl, ""}}]}]]
            end
        end
    else
        error("Unknown mime type: ", mime)
    end
end

# Functions to process code blocks.

# Render graphviz code blocks and return the resulting SVG data.
function execblock_graphviz(source)
    input_path, input = mktemp()
    print(input, source)
    flush(input)
    seek(input, 0)
    output = readall(input > `dot -Tsvg`)
    close(input)
    rm(input_path)
    "image/svg+xml", output
end

# Render latex, convert to SVG with dvisvgm, and return the SVG data.
function execblock_latex(source)
    # latex -> dvi
    input_path, input = mktemp()
    print(input, source)
    flush(input)
    seek(input, 0)
    latexout_dir = mktempdir()
    run(`latex -output-format=dvi -output-directory=$(latexout_dir) $(input_path)` > SpawnNullStream())
    rm(input_path)

    # dvi -> svg
    latexout_path = "$(latexout_dir)/$(basename(input_path)).dvi"
    output = readall(`dvisvgm --stdout --no-fonts $(latexout_path)` .> SpawnNullStream())
    run(`rm -rf $(latexout_dir)`)

    "image/svg+xml", output
end

# Execute a block of julia code, capturing its output.
function execblock_julia(source)
    out = Any[]

    for (cmd, expr) in parseit(strip(source))
        result = try
            eval(WeaveSandbox, expr)
        catch e
            io = IOBuffer()
            print(io, "ERROR: ")
            Base.error_show(io, e)
            tmp = bytestring(io)
            close(io)
            tmp
        end
        push!(out, (cmd, expr, result))
    end

    if length(WeaveSandbox.MIME_OUTPUT) > 0
        mime, output = pop!(WeaveSandbox.MIME_OUTPUT)
        mime, convert(Vector{Uint8}, output), out
    else
        seek(WeaveSandbox.OUTPUT_STREAM, 0)
        output = takebuf_array(WeaveSandbox.OUTPUT_STREAM)
        truncate(WeaveSandbox.OUTPUT_STREAM, 0)
        "text/plain", output, out
    end

end

ext: jl


text:
"""
# IOTraits.jl

[Trait types][White] for describing the capabilities and behaviour of
IO interfaces.

## Background

This collection of Trait types began as a tool for resolving method
selection issues in the UnixIO.jl package. Everything in Unix is a
file, but there are many types of file, many types of Unix, and
many ways a file handled can be configured.

Selection of correct (or most efficient) methods can often be
achieved with a simple type hierarchy. However traits seemed to be
useful way to deal with variations in behaviour that depend
file handle configuration and platform.
In general `libc`'s `write(2)` and `read(2)` can be used to transfer
data to or from any Unix file descriptor. However, the precise
behaviour of these functions depends on many factors.

Will `read` block?, if not, will it return less than the requested
number of bytes? Can I query the number of bytes available to read
first? How should I wait for more bytes? If more than one line is
buffered will `read` return all of them?  Will read ever return a
partial line? Will every call to `write(2)` result in a packet being
transmitted? Is it efficient to write one byte at a time?
Does `lseek` work with this file? Does `FIONREAD` work with this file?

The answers depend on combinations of file type, configuration and
platform.

This collection expands on the traits from UnixIO.jl in the hope of making
them more broadly useful.
According to the [law of the hammer][Hammer] some of this is probably
overkill. The intention is to consider the application of Trait types
to various aspects of IO and to see where it leads.

[White]: https://www.juliabloggers.com/the-emergent-features-of-julialang-part-ii-traits/

[Hammer]: https://en.wikipedia.org/wiki/Law_of_the_hammer

[Duck]: https://en.wikipedia.org/wiki/Duck_typing

## Overview

The IOTraits interface is built around the `transfer(io, buffer)` function.
This function transfers data between an IO interface and a buffer.

Traits are used to specify the behaviour of the IO and the buffer.

#### `IODirection`
Which way is the transfer?
(`In`, `Out` or `Exchange`).

#### `FromBufferInterface`
How to get data from the buffer?
(`FromIO`, `FromPop`, `FromTake`, `FromIndex`, `FromIteration` or `FromPtr`)

#### `ToBufferInterface`
How to put data into the buffer?
(`ToIO`, `ToPush`, `ToPut`, `ToIndex` or `ToPtr`)

#### `TotalSize`
How much data is available?
(`UnknownTotalSize`, `VariableTotalSize`, `FixedTotalSize`, or
 `InfiniteTotalSize`)

#### `TransferSize`
How much data can be moved in a single transfer?
(`UnknownTransferSize`, `KnownTransferSize`, `LimitedTransferSize` or
 `FixedTransferSize`)

#### `ReadFragmentation`
What guarantees are made about fragmentation?
(`ReadsBytes`, `ReadsLines`, `ReadsPackets` or `ReadsRequestedSize`)

#### `WaitingMechanism`
How to wait for activity?
(`WaitBySleeping`, `WaitUsingPosixPoll`, `WaitUsingEPoll`, `WaitUsingPidFD` or
 `WaitUsingKQueue`)

## Unfiled Notes

 - How could `Base.isready` apply to IO? It's definition is nice and concise:
   "Determine whether a Channel has a value stored to it. Returns immediately,
   does not block."
 - Build `ReadlineMux <: IOMux` and `ReadlineDemux <: IODemux` as an example
   of a traits aware IO mechanism. e.g. merge multiple streams onto a single
   IO with a mux/demux header.
 - Consider traits to select special purpose IO functions where appropriate:
   `pread`, `readv`, `writev`, `sendfile` ?
 - Consider using traits to identify inefficient access patterns.
     - Keep stats per file
        - No. tranfers, No. bytes, No. seeks.
        - Time between transfers (rolling avg?)
     - Warn if stats don't align with traits
        - e.g. large number of small reads for high per-call overhead IO.
"""
module IOTraits

using Preconditions
using Preferences

# Transfer Direction Trait.

abstract type IODirection end
struct In <: IODirection end
struct Out <: IODirection end
struct Exchange <: IODirection end

"""
### `IODirection` -- Transfer Direction Trait.

The `IODirection` trait describes the direction of data transfer
for an IO interface. `IODirection(typeof(io))` returns one of:

 * `In()` -- data is "read" from the `IO`.
 * `Out()` -- data is "written" to the `IO`.
 * `Exchange()` -- data is simultaneously exchanged
                   between the `io` and a buffer (e.g. like [SPI][SPI]).

[SPI]: https://en.wikipedia.org/wiki/Serial_Peripheral_Interface
"""
IODirection(x) = IODirection(typeof(x))

# Transfer Function.

"""
## `transfer()` - The Data Transfer Function

```julia
transfer(io, buffer [, n=1] [;start=(1 => 1)]) -> n_transfered
```

Transfer at most `n` items between `io` and `buffer`.

Return the number of items transferred.

The type of transfer depends on the `IODirection(io)`
 (`In`, `Out` or `Exchange`).

`start` specifies the starting indexes for the transfer
(if `hasioindex(io)` and/or `hasioindex(buffer)` are true).

The type of items transferred depends on `ioeltype(io)` and `ioeltype(buffer)`.
By default `ioeltype(x) = eltype(x)`.

The `buffer` can be an `AbstractArray`, an `AbstractChannel`, a `URI` or an `IO`.

Or, the `buffer` can be any collection that implements
the Iteration Interface, the Indexing Interface,
the AbstractChannel interface, or the `push!`/`pop!` interface.
In some cases it is necessary to define a method of `ToBufferInterface()`
or `FromBufferInterface()` to specify the interface to use for a particular
buffer type (e.g. if a buffer implements more than one of the supported
interfaces).
Defining these trait methods can also help to ensure that the most efficient
interface is used for a particular buffer type.

If either the `io` or the `buffer` is a `URI` then items are transferred
to (or from) the identified resource.
A transfer to a `URI` creates a new resource or replaces the resource
(i.e. HTTP PUT semantics).

```julia
transfer(io => buffer [, n=1] [;start=(1 => 1)]) -> n_transfered
transfer(buffer => io [, n=1] [;start=(1 => 1)]) -> n_transfered
```

`io` and `buffer` can be passed as a pair.
An `In()` io must be on the left.
An `Out()` io must be on the right.
"""
function transfer end

# From Buffer Interface Trait.

abstract type FromBufferInterface end
struct FromIO <: FromBufferInterface end
struct FromPop <: FromBufferInterface end
struct FromTake <: FromBufferInterface end
struct FromIndex <: FromBufferInterface end
struct FromIteration <: FromBufferInterface end
struct FromPtr <: FromBufferInterface end

"""
### `FromBufferInterface` -- Data Transfer Source Interface Trait.

The `FromBufferInterface` trait defines what interface is used to take
data from a particular buffer type
(or what interface is preferred for best performance).

`FromBufferInterface(typeof(buffer))` returns one of:

 * `FromIO()` -- Take data from the buffer using the `Base.IO` interface.
 * `FromPop()` -- Use `pop!(buffer)` to read from the buffer.
 * `FromTake()` -- Use `take!(buffer)`.
 * `FromIndex()` -- Use `buffer[i]` (the default).
 * `FromIteration()` -- Use `for x in buffer...`.
 * `FromPtr()` -- Use `unsafe_copyto!(buffer, x, n)`.

Default `FromBufferInterface` methods are built in for common buffer types:
"""
FromBufferInterface(x) = FromBufferInterface(typeof(x))
FromBufferInterface(::Type) = FromIteration()
FromBufferInterface(::Type{<:IO}) = FromIO()
FromBufferInterface(::Type{<:AbstractArray}) = FromIndex()
FromBufferInterface(::Type{<:AbstractChannel}) = FromTake()
FromBufferInterface(::Type{<:Ptr}) = FromPtr()

# To Buffer Interface Trait.

abstract type ToBufferInterface end
struct ToIO <: ToBufferInterface end
struct ToPush <: ToBufferInterface end
struct ToPut <: ToBufferInterface end
struct ToIndex <: ToBufferInterface end
struct ToPtr <: ToBufferInterface end

"""
### `ToBufferInterface` -- Data Transfer Destination Interface Trait.

The `ToBufferInterface` trait defines what interface is used to store data
in a particular type of buffer
(or what interface is preferred for best performance).

`ToBufferInterface(typeof(buffer))` one of:

 * `ToIO` -- Write data to the buffer using the `Base.IO` interface.
 * `ToPush` -- Use `push!(buffer, data)`
 * `ToPut` -- Use `put!(buffer, data)`
 * `ToIndex` -- Use `buffer[i] = data (the default)`
 * `ToPtr` -- Use `unsafe_copyto!(x, buffer, n)`.

Default `ToBufferInterface` methods are built in for common buffer types.
"""
ToBufferInterface(x) = ToBufferInterface(typeof(x))
ToBufferInterface(::Type) = ToPush()
ToBufferInterface(::Type{<:IO}) = ToIO()
ToBufferInterface(::Type{<:AbstractArray}) = ToIndex()
ToBufferInterface(::Type{<:AbstractChannel}) = ToPut()
ToBufferInterface(::Type{<:Ptr}) = ToPtr()

# Total Size Trait.

abstract type TotalSize end
struct UnknownTotalSize <: TotalSize end
struct InfiniteTotalSize <: TotalSize end
abstract type KnownTotalSize end
struct VariableTotalSize <: KnownTotalSize end
struct FixedTotalSize <: KnownTotalSize end

"""
### `TotalSize` -- Data Size Trait.

The `TotalSize` trait describes how much data is available from an
IO interface.

`TotalSize(typeof(io))` returns one of:

 * `VariableTotalSize()` -- The total amount of data available can be queried
   using the `length` function. Note: the total size can change. e.g. new
   lines might be appended to a log file.
 * `FixedTotalSize()` -- The amount of data is known and will not change.
   Applicable to block devices. Applicable to some network streams. e.g.
   a HTTP Message where Content-Length is known.
 * `InfiniteTotalSize()` -- End of file will never be reached. Applicable
   to some device files.
 * `UnknownTotalSize()` -- No known data size limit.
"""
TotalSize(x) = TotalSize(typeof(x))
TotalSize(::Type) = UnknownTotalSize()

# Transfer Size Trait.

abstract type TransferSize end
struct UnknownTransferSize <: TransferSize end
struct KnownTransferSize <: TransferSize end
struct LimitedTransferSize <: TransferSize end
struct FixedTransferSize <: TransferSize end

"""
### `TransferSize` -- Transfer Size Trait.

The `TransferSize` trait describes how much data can be moved in a single
transfer.

`TransferSize(typeof(io))` returns one of:

 * `UnknownTransferSize()` -- Transfer size is not known in advance.
   The only way to know how much data is available is to attempt a transfer.
 * `KnownTransferSize()` -- The amount of data immediately available for
   transfer can be queried using the `bytesavailable` function.
 * `LimitedTransferSize()` -- The amount of data that can be moved in a single
   transfer is limited. e.g. by a device block size or buffer size. The maximum
   transfer size can queried using the `max_transfer_size` function.
   The amount of data immediately available for transfer can be queried using
   the `bytesavailable` function.
 * `FixedTransferSize()` -- The amount of data that moved by a single trasfer
   is fixed. e.g. `/dev/input/event0` device always transfers
   `sizeof(input_event)` bytes.

"""
TransferSize(x) = TransferSize(typeof(x))
TransferSize(t::Type) = TransferSizeMechanism(t) == NoSizeMechanism() ?
                        UnknownTransferSize() :
                        KnownTransferSize()

abstract type TransferSizeMechanism end
struct NoSizeMechanism <: TransferSizeMechanism end
struct SupportsFIONREAD <: TransferSizeMechanism end
struct SuppoutsStatSize <: TransferSizeMechanism end

"""
`TransferSizeMechanism(typeof(io))` returns one of:

 * `SupportsFIONREAD()` -- The underlying device supports `ioctl(2), FIONREAD`.
 * `SuppoutsStatSize()` -- The underlying device supports  `fstat(2), st_size`.
"""
TransferSizeMechanism(x) = TransferSizeMechanism(typeof(x))
TransferSizeMechanism(::Type) = NoSizeMechanism()

# Data Fragmentation Trait.

abstract type ReadFragmentation end
struct ReadsBytes         <: ReadFragmentation end
struct ReadsLines         <: ReadFragmentation end
struct ReadsPackets       <: ReadFragmentation end
struct ReadsRequestedSize <: ReadFragmentation end

"""
### `ReadFragmentation` -- Data Fragmentation Trait.

The `ReadFragmentation` trait describes what guarantees an IO interface makes
about fragmentation of data returned by the underlying `read(2)` system call.

`ReadFragmentation(typeof(io))` returns one of:

 * `ReadsBytes` -- No special guarantees about what is returned by `read(2)`.
   This is the default.

 * `ReadsLines` -- `read(2)` returns exactly one line at a time.
   Does not return partially buffered lines unless an explicit `EOL` or `EOF`
   control character is received.
   Applicable to Character devices in canonical mode.
   See [termios(3)](https://man7.org/linux/man-pages/man3/termios.3.html).

 * `ReadsPackets` -- `read(2)` returns exactly one packet at a time.
   Does not return partially buffered packets.
   Applicable to some sockets and pipes depending on configuration.
   e.g. See the `O_DIRECT` flag in
   [pipe(2)](https://man7.org/linux/man-pages/man2/pipe.2.html).

 * `ReadsRequestedSize` -- `read(2)` returns exactly the number of elements
   requested (or throws an exception).
   Applicable to local files and some virtual files (e.g. `/dev/random`,
   `/dev/null`).
"""
ReadFragmentation(x) = ReadFragmentation(typeof(x))
ReadFragmentation(::Type) = ReadsBytes()

# Event Notification Mechanism Trait.

abstract type WaitingMechanism end
struct WaitBySleeping     <: WaitingMechanism end
struct WaitUsingPosixPoll <: WaitingMechanism end
struct WaitUsingEPoll     <: WaitingMechanism end
struct WaitUsingPidFD     <: WaitingMechanism end
struct WaitUsingKQueue    <: WaitingMechanism end

"""
### `WaitingMechanism` -- Event Notificaiton Mechanism Trait.

The `WaitingMechanism` trait describes ways of waiting for OS resources
that are not immediately available. e.g. when `read(2)` returns
`EAGAIN` (`EWOULDBLOCK`), or when `waitpid(2)` returns `0`.

Resource types, `R`, that have an applicable `WaitingMechanism`, `T`,
define a method of `Base.wait(::T, r::R)`.

If a `WaitingMechanism`, `T`, is not available on a particular OS
then `Base.isvalid(::T)` should be defined to return `false`.

`WaitingMechanism(typeof(io))` returns one of:

 * `WaitBySleeping` -- Wait using a dumb retry/sleep loop (the default).
   This may be more efficient for small systems with simple IO requirements,
   or for systems that simply do not spend a lot of time waiting for IO.

 * `WaitUsingPosixPoll` -- Wait using the POXSX `poll` mechanism.
   Wait for activity on a set of file descriptors.
   Applicable to FIFO pipes, sockets and character devices
   (but not local files).
   See [`poll(2)`](https://man7.org/linux/man-pages/man2/poll.2.html)

 * `WaitUsingEPoll` -- Wait using the Linux `epoll` mechanism.
   Like `poll` but scales better for workloads with a large number of
   waiting streams.
   See [`epoll(7)`](https://man7.org/linux/man-pages/man7/epoll.7.html)

 * `WaitUsingKQueue` -- Wait using the BSB `kqueue` mechanism.
   Like `epoll` but can also wait for files, processes, signals etc.
   See [`kqueue(2)`](https://www.freebsd.org/cgi/man.cgi?kqueue)

 * `WaitUsingPidFD()` -- Wait for process termination using
   the Linux `pidfd` mechanism. A `pidfd` is a special process monitoring
   file descriptor that can in turn be monitored by `poll` or `epoll`. See
   [`pidfd_open(2)`](http://man7.org/linux/man-pages/man2/pidfd_open.2.html).

TODO: Consider Linux AIO `io_getevents` for disk io?

"""
WaitingMechanism(x) = WaitingMechanism(typeof(x))
WaitingMechanism(::Type) = WaitBySleeping()

Base.isvalid(::WaitingMechanism) = true
Base.isvalid(::WaitUsingEPoll) = Sys.islinux()
Base.isvalid(::WaitUsingPidFD) = Sys.islinux()
Base.isvalid(::WaitUsingKQueue) = Sys.isbsd() && false # not yet implemented.

firstvalid(x, xs...) = isvalid(x) ? x : firstvalid(xs...)

const default_poll_mechanism = firstvalid(WaitUsingKQueue(),
                                          WaitUsingEPoll(),
                                          WaitUsingPosixPoll(),
                                          WaitBySleeping())

Base.wait(x, ::WaitBySleeping) = sleep(0.1)

"""
## Preferred Polling Mechanism

    set_poll_mechanism(name)

Configure the preferred event polling mechanism:
"kqueue", "epoll", "poll", or "sleep".

Note: this setting applies only to `poll(2)`-compatible file descriptors
(i.e. it does not apply to local disk files).

By default, IOTraits will try to choose the best available mechanism
(see `default_poll_mechanism`).

This setting is persistently stored through [Preferences.jl][Prefs].

To find out what mechanism is used for a particular `FD` call:
`WaitingMechanism(fd)`

[Prefs]: https://github.com/JuliaPackaging/Preferences.jl
"""
function set_poll_mechanism(x)
    @require poll_mechanism(x) != nothing
    @require isvalid(poll_mechanism(x))
    @set_preferences!("waiting_mechanism" => x)
    @warn "Preferred IOTraits.WaitingMechanism set to $(poll_mechanism(x))." *
          "UnixIO must be recompiled for this setting to take effect."
end

poll_mechanism(name) = name == "kqueue" ? WaitUsingKQueue() :
                       name == "epoll"  ? WaitUsingEPoll() :
                       name == "poll"   ? WaitUsingPosixPoll() :
                       name == "sleep"  ? WaitBySleeping() :
                                          default_poll_mechanism

const preferred_poll_mechanism =
    poll_mechanism(@load_preference("waiting_mechanism"))

# Exports.

export transfer

export TotalSize,
       UnknownTotalSize, InfiniteTotalSize, KnownTotalSize, VariableTotalSize,
       FixedTotalSize

export TransferSize,
       UnknownTransferSize, KnownTransferSize, LimitedTransferSize,
       FixedTransferSize

export TransferSizeMechanism,
       NoSizeMechanism, SupportsFIONREAD, SuppoutsStatSize

export ReadFragmentation,
       ReadsBytes, ReadsLines, ReadsPackets, ReadsRequestedSize

export WaitingMechanism,
       WaitBySleeping, WaitUsingPosixPoll, WaitUsingEPoll, WaitUsingPidFD,
       WaitUsingKQueue

using ReadmeDocs

end # module IOTraits


text:
vars_options = fieldnames(options_simulation)
vars_bounds = fieldnames(boundary_stop_conditions)[findall(fieldtypes(boundary_stop_conditions) .<: Number)]

arg_options = join(["$field = p.opts.$field" for field in vars_options], ",\n")
arg_bounds  = join(["$field = p.opts.$field" for field in vars_bounds], ",\n")

new_struct_options = join(vars_options, ",")
new_struct_bounds = join(vars_bounds, ",")

eval(quote
@inline function simulate(
    p::T1, # initial parameters file
    tf::T2 = nothing; # a single number (length of the experiment) or a vector (interpolated points)
    sol::R1 = solution(), # using a new sol or continuing from previous simulation?
    I   = nothing, # constant current C-rate. also accepts :rest
    V   = nothing, # constant voltage. also accepts :hold if continuing simulation
    P   = nothing, # constant power.   also accepts :hold if continuing simulation
    SOC             = p.opts.SOC, # initial SOC of the simulation. only valid if not continuing simulation
    outputs         = p.opts.outputs, # sol output states
    abstol          = p.opts.abstol, # absolute tolerance in DAE solve
    reltol          = p.opts.reltol, # relative tolerance in DAE solve
    abstol_init     = abstol, # absolute tolerance in initialization
    reltol_init     = reltol, # relative tolerance in initialization
    maxiters        = p.opts.maxiters, # maximum solver iterations
    check_bounds    = p.opts.check_bounds, # check if the boundaries (V_min, SOC_max, etc.) are satisfied
    reinit          = p.opts.reinit, # reinitialize the initial guess
    verbose         = p.opts.verbose, # print information about the run
    interp_final    = p.opts.interp_final, # interpolate the final points if a boundary is hit
    tstops          = p.opts.tstops, # times the solver explicitly solves for
    tdiscon         = p.opts.tdiscon, # times of known discontinuities in the current function
    interp_bc       = p.opts.interp_bc, # :interpolate or :extrapolate
    save_start      = p.opts.save_start, # warm-start for the initial guess
    stop_function   = p.opts.stop_function,
    calc_integrator = p.opts.calc_integrator,
    V_max         = p.bounds.V_max,
    V_min         = p.bounds.V_min,
    SOC_max       = p.bounds.SOC_max,
    SOC_min       = p.bounds.SOC_min,
    T_max         = p.bounds.T_max,
    c_s_n_max     = p.bounds.c_s_n_max,
    I_max         = p.bounds.I_max,
    I_min         = p.bounds.I_min,
    η_plating_min = p.bounds.η_plating_min,
    c_e_min       = p.bounds.c_e_min,
    dfilm_max     = p.bounds.dfilm_max,
    ) where {
        T1<:model,
        T2<:Union{Number,AbstractVector,Nothing},
        R1<:Union{solution,Vector{Float64}},
    }

    # Check if the outputs are the same as in the cache
    var_keep, outputs = model_states_logic(outputs)

    # `nextfloat` ensures that there is always a unique point for interpolation
    t0 = isempty(sol) || sol isa Array{Float64,1} ? 0.0 : (@inbounds nextfloat(sol.t[end]))

    # identifying the run type
    run = get_run(I,V,P,t0,tf,p,sol)

    # putting opts and bounds into a structure.
    opts = options_simulation_immutable($(Meta.parse(new_struct_options))...)
    bounds = boundary_stop_conditions_immutable($(Meta.parse(new_struct_bounds))..., boundary_stop_prev_values())

    # getting the initial conditions and run setup
    int, funcs, sol = initialize_simulation!(sol, p, run, bounds, opts)

    if !within_bounds(run)
        if verbose @warn "Instantly hit simulation stop conditions: $(run.info.exit_reason)" end
        exit_simulation!(p, sol, run, bounds, int, opts; cancel_interp=true)

        return sol
    end

    if verbose println("\n$(run)") end

    solve!(sol, int, run, p, bounds, opts, funcs)

    exit_simulation!(p, sol, run, bounds, int, opts)

    # if you want to interpolate the results
    if T2 <: AbstractVector sol = sol(tf, interp_bc=opts.interp_bc) end

    if verbose println("\n$(sol)\n") end

    return sol
end
end)


text:
# ---
# jupyter:
#   jupytext:
#     formats: ipynb,jl:hydrogen
#     text_representation:
#       extension: .jl
#       format_name: hydrogen
#       format_version: '1.3'
#       jupytext_version: 1.11.2
#   kernelspec:
#     display_name: Julia 1.6.1
#     language: julia
#     name: julia-1.6
# ---

# %%
VERSION

# %%
a = quote
    x = "hoge!"
    for i in 1:5
        println(x)
    end
    println("after for loop: ", x)
end |> Base.remove_linenums! |> string
include_string(Main, a)

# %%
b = quote
    x = "hoge!"
    for i in 1:5
        x = "moge!"
        println(x)
    end
    println("after for loop: ", x)
end |> Base.remove_linenums! |> string
include_string(Main, b)

# %%
c = quote
    x = "hoge!"
    for i in 1:5
        local x = "moge!"
        println(x)
    end
    println("after for loop: ", x)
end |> Base.remove_linenums! |> string
include_string(Main, c)

# %%
d = quote
    x = "hoge!"
    for i in 1:5
        global x = "moge!"
        println(x)
    end
    println("after for loop: ", x)
end |> Base.remove_linenums! |> string
include_string(Main, d)

# %%
e = quote
    x = "hoge!"
    for i in 1:5
        x *= "moge!"
        println(x)
    end
    println("after for loop: ", x)
end |> Base.remove_linenums! |> string
include_string(Main, e)

# %%
write("b.jl", b)
read("b.jl", String) |> println

# %%
; d:/Julia-1.6.1/bin/julia b.jl

# %%
x = "hoge!"
for i in 1:5
    println(x)
end
println("after for loop: ", x)

# %%
x = "hoge!"
for i in 1:5
    x = "moge!"
    println(x)
end
println("after for loop: ", x)

# %%
for i in 1:5
    y = "moge!"
    println(x)
end
println("after for loop: ", y)

# %%
x = "hoge!"
for i in 1:5
    x *= "moge!"
    println(x)
end
println("after for loop: ", x)

# %%

max_stars_repo_path: 0005/soft scope warning example.jl

max_stars_repo_name: genkuroki/public

max_issues_repo_path: 0005/soft scope warning example.jl

max_issues_repo_name: genkuroki/public

max_forks_repo_path: 0005/soft scope warning example.jl

max_forks_repo_name: genkuroki/public


text:
module GeometryObjects

using Random

using RecipesBase
using StaticArrays: FieldVector

# types
export Point2D, Point3D, Velocity2D, Circle2D, Circle3D, Particle2D
include("types.jl")
include("recipes.jl")

end # module


text:
function banner()
    FIGlet.render("XtalsPyTools", FIGlet.availablefonts()[286])
end

max_stars_repo_name: SimonEnsemble/XtalsPyTools

max_issues_repo_name: SimonEnsemble/XtalsPyTools


text:
@testset "Evaluation" begin
    # """Test the construction and accessors of Evaluation objects."""

    #Status can only be one of ["OPEN", "PLANNED", "CLOSED", "COMPLETED"]
    @test_pythrows ValueError Evaluation(name="foo", description="bar", status="BAH")
    @test_pythrows ValueError Evaluation(name="foo", description="bar", status="OPEN", contentSource="a")

    #Assert that the values are
    ev = Evaluation(name="foobar2", description="bar", status="OPEN", contentSource="syn1234")
    @test ev["name"] == "foobar2"
    @test ev["description"] == "bar"
    @test ev["status"] == "OPEN"
end

@testset "Submission" begin
    # """Test the construction and accessors of Evaluation objects."""

    @test_pythrows PyKeyError Submission(foo="bar")
end


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

using Markdown
using InteractiveUtils

# ╔═╡ 1cb400e6-68ca-11eb-287e-75c3fb94873f
begin
	using Pkg
	Pkg.activate(temp = true)
	Pkg.add(PackageSpec(name = "DataAPI", version = "1.6"))
	Pkg.add(PackageSpec(name = "AbstractPlotting", rev = "master"))
	Pkg.add(["Revise", "CairoMakie", "DataFrames", "CategoricalArrays", "PooledArrays", "UnPack", "Random"])
	Pkg.add("PlutoUI")

	Pkg.develop("TabularMakie")

	using Revise, CairoMakie, DataFrames, CategoricalArrays, PooledArrays
	using DataAPI: DataAPI, refarray
	using TabularMakie
	using UnPack
	using PlutoUI

	Base.show(io::IO, ::MIME"text/html", x::CategoricalArrays.CategoricalValue) = print(io, get(x))
end

# ╔═╡ 77df0d1a-278a-449c-ac55-30991044afc2
md"""
# Pipeline
"""

# ╔═╡ 37ebd83c-319c-462c-8eb1-859f81b9eae0
import TabularMakie:
	specification, draw_axis!, add_legend,
	has_legend_or_colorbar, outer_legend_position

# ╔═╡ 6588b3e6-6941-11eb-198a-15e00c20cb5a
md"""
# Some Examples
"""

# ╔═╡ 904fef48-76be-11eb-26b4-fb433306bc3f
md"""
## Categorical axes
"""

# ╔═╡ 9cfe05a6-76be-11eb-06ed-a92dc0e547c4
md"""
## Grouped bar
"""

# ╔═╡ a4051f48-6952-11eb-036e-37b3de9c37c2
md"""
## Rename and transform variables on the fly
"""

# ╔═╡ 6cf5fce8-6941-11eb-24f1-6d4281735519
rec_1(x) = recode!(x, "m 1" => "Nice name", "m 2" => "Other")

# ╔═╡ 5281fcf6-6946-11eb-34b9-79d05e3ec288
rec_2(x) = recode!(x, "lx 1" => "Panel 1")

# ╔═╡ 1a656cf2-694a-11eb-1c4f-edf4c99c3edc
minus1(x) = x .- 1

# ╔═╡ 2fec9ae6-76c4-11eb-35cd-c5c79181ed02
md"""
## Band
"""

# ╔═╡ 365c077a-76c4-11eb-14d1-47bcb751896c
let
	df = DataFrame(
		x = 1:10, lo = rand(10), up = 5 .+ rand(10)
	)
	lplot(Band, df, :x, :lo => "nice y label", :up,
		  title = "Some nice title",
		  axis_attr = (xticklabelrotation = 0.5, )
	)
end

# ╔═╡ 69854f4e-6952-11eb-28c5-6dcf989a98f4
md"""
# Appendix
"""

# ╔═╡ 8672add8-6952-11eb-195e-4133c66f1e07
md"""
## Package environment
"""

# ╔═╡ 9bee489c-694e-11eb-3d8f-735277e3db10
md"""
## Generate some data
"""

# ╔═╡ 346fba94-6950-11eb-0c18-43cb90d135b8
ts_df = let
	g_la = ["lay 1", "lay 2"]
	g_co = ["c 1", "c 2"]
	g_ls = ["ls 1", "ls 2", "ls 3"]

	ts_df = DataFrame(Iterators.product(g_la, g_co, g_ls))

	grps = [:g_la, :g_co, :g_ls]

	rename!(ts_df, grps)
	transform!(ts_df, grps .=> categorical, renamecols = false)

	transform!(ts_df, :g_co => (x -> refarray(x) .+ rand.())  => :s_co)
	transform!(ts_df, :s_co => ByRow(float) => :s_co)

	ts_df[:,:grp] = 1:size(ts_df, 1) |> categorical

	function rw_nt(T)
		function(i)
			rw = cumsum(randn(T))

			DataFrame([(t = t, v = v, grp = i) for (t, v) in enumerate(rw)])
		end
	end

	T = 100

	combine(groupby(ts_df, [grps; :s_co])) do sdf
		grp = sdf.grp[1]

		rw_nt(T)(grp)
	end
end

# ╔═╡ 45624846-6950-11eb-1d56-35b025fbbaa4
lplot(Lines, ts_df,
		:t,
		:v;
		color = :g_co => "Buahh!",
		layout_x = :g_la,
		linestyle = :g_ls,
		linewidth = 2,
		legend_attr = (position = :left,)
)

# ╔═╡ c9c43f1c-9917-4e31-a0a8-dd20791423b0
lplot(Lines, ts_df,
		:t,
		:v;
		color = :g_co => "Group",
		linestyle = :g_co => "Group",
		layout_x = :g_la,
		layout_y = :g_ls,
		linewidth = 2,
		legend_attr = (position = :left,)
)

# ╔═╡ 220b673c-9304-11eb-0393-55e82f833b60
fig = lplot(Lines, ts_df, :t, :v; color = :s_co, layout_y = :g_co, group = :grp )

# ╔═╡ 47184b9e-68ca-11eb-2595-3b8b241a2b9f
cs_df = let
	N = 100
	dummy_df = DataFrame(
		xxx = rand(N),
		yyy = rand(N),
		s_m  = rand(5:13, N),
		g_c  = rand(["c 1", "c 2", "c 3"], N) |> categorical,
		g_lx = rand(["lx 1", "lx 2", "lx 3"], N) |> categorical,
		g_m  = rand(["m 1", "m 2", "m 3"], N) |> categorical
		)

	dummy_df[:,:s_c] = 2 .* rand(N) .+ refarray(dummy_df.g_lx)
	dummy_df
end

# ╔═╡ 987bde45-6e35-44e9-8724-5cdbf195fe9b
let
	spec = specification(Scatter, cs_df,
		:xxx,
		:yyy;
		markersize = :s_m,
		layout_wrap = :g_lx
	)

	fig = Figure()

	legend_position = :top
	@unpack ax_pos, legs_pos = outer_legend_position(fig, has_legend_or_colorbar(spec), legend_position)

	draw_axis!(ax_pos, spec)

	add_legend(legs_pos, spec; legend_position)

	fig
end

# ╔═╡ e929fe20-775f-49f0-9a42-be0954771ea2
let
	spec = specification(Scatter, cs_df,
		:xxx,
		:yyy;
		color = :g_c,
		marker = :g_c,
		markersize = :s_m,
		layout_wrap = :g_lx
	)

	fig = Figure()

	ax_pos, legs_pos, orientation = fig[1,1], fig[1,2], :vertical

	draw_axis!(ax_pos, spec)

	add_legend(legs_pos, spec; orientation)

	fig
end

# ╔═╡ 831aa510-6947-11eb-3694-21c2defdbaef
lplot(Scatter, cs_df,
	:xxx,
	:yyy;
	color = :s_c,
	marker = :g_m,
	markersize = :s_m,
	layout_wrap = :g_lx,
	legend_attr = (position = :top, titleposition = :top)
)

# ╔═╡ 4e1fd39c-68cc-11eb-3c84-b3127a09c2a2
lplot(Scatter, cs_df,
	:xxx => minus1,
	:yyy => ByRow(x -> x + 1) => "the y plus one";
	color = :s_c => "hey there",
	marker = :g_m => rec_1 => "bla",
	markersize = :s_m => :tada,
	layout_wrap = :g_lx => rec_2,
	legend_attr = (titleposition = :top,)
  )

# ╔═╡ 69ff7b02-9302-11eb-38d7-6d020c3bc20d
lplot(Scatter, cs_df,
	:xxx => minus1,
	:yyy => ByRow(x -> x + 1) => "the y plus one";
	color = :s_c => "hey there",
	marker = :g_m => rec_1 => "bla",
	#markersize = :s_m => :tada,
	layout_wrap = :g_lx => rec_2,
	legend_attr = (titleposition = :top, position = :right)
  )

# ╔═╡ 07ce7300-76b5-11eb-3132-2bea7add28e1
cat_df = let
	N = 100
	cat0 = rand(["Low", "Mid", "High"], N)
	cat0[1:3] .= ["Mid", "High", "Low"]

	cat  = categorical(cat0, levels = ["Low", "Mid", "High"])
	y    = rand(N)

	y .= y .- (cat0 .== "Low")
	y .= y .+ (cat0 .== "High")

	DataFrame(; cat0, cat, y)
end

# ╔═╡ c5777782-8bb7-11eb-2f40-3f2ca4012da6
lplot(Scatter, cat_df, :cat0, :y)

# ╔═╡ 8030ddc6-76b5-11eb-030d-b79fd57b5a6f
lplot(Scatter, cat_df, :cat => "x in the right order", :y)

# ╔═╡ 96075d2a-76bd-11eb-2d9d-31333dd2f2fd
bar_tbl = (x = [1, 1, 1, 2, 2, 2, 3, 3, 3],
       height = 0.1:0.1:0.9,
       grp = "Group " .* string.([1, 2, 3, 1, 2, 3, 1, 2, 3]),
       grp1 = "grp " .* string.([1, 2, 2, 1, 1, 2, 1, 1, 2]),
       grp2 = "Grp " .* string.([1, 1, 2, 1, 2, 1, 1, 2, 1])
       )

# ╔═╡ 38e89a48-d9d8-4289-9fce-08a672915cdb
bar_df = DataFrame(bar_tbl)

# ╔═╡ 15490b34-23ec-4ae5-b473-c4c1b7f49e07
begin
	spec = specification(BarPlot, bar_df,
		:grp_x => "nice name for x",
		:y,
		stack = :grp_stack,
		dodge = :grp_dodge,
		color = :grp_stack => "nice name for color"
	)

	spec0 = specification(Scatter, cs_df,
		:xxx,
		:yyy;
		markersize = :s_m,
		layout_wrap = :g_lx
	)

	nothing
end

# ╔═╡ d7168aff-2835-447c-9e57-1609ed4ae71e
spec0.style_pairs

# ╔═╡ 1afe791f-eee2-4b03-a2b0-474ddd2044c1
spec0.style_dict

# ╔═╡ 69bf9657-96b0-4b76-860e-c2653c78c89e
lplot(BarPlot, bar_df, :x, :height, stack = :grp, color = :grp)

# ╔═╡ 1eff7f5a-76c0-11eb-247c-9bc3836564ed
lplot(BarPlot, bar_df, :x, :height => ByRow(log), dodge = :grp, color = :grp)

# ╔═╡ c92db4ce-76bd-11eb-0d6f-d55846f72e5b
lplot(BarPlot, bar_df,
	:x => "nice name for x",
	:height,
	stack = :grp1,
	dodge = :grp2,
	color = :grp => " "
)

# ╔═╡ 911d9356-6952-11eb-1712-bd37da0cba85
TableOfContents()

# ╔═╡ Cell order:
# ╟─77df0d1a-278a-449c-ac55-30991044afc2
# ╠═37ebd83c-319c-462c-8eb1-859f81b9eae0
# ╠═15490b34-23ec-4ae5-b473-c4c1b7f49e07
# ╠═d7168aff-2835-447c-9e57-1609ed4ae71e
# ╠═1afe791f-eee2-4b03-a2b0-474ddd2044c1
# ╠═987bde45-6e35-44e9-8724-5cdbf195fe9b
# ╠═e929fe20-775f-49f0-9a42-be0954771ea2
# ╟─6588b3e6-6941-11eb-198a-15e00c20cb5a
# ╠═831aa510-6947-11eb-3694-21c2defdbaef
# ╠═45624846-6950-11eb-1d56-35b025fbbaa4
# ╠═c9c43f1c-9917-4e31-a0a8-dd20791423b0
# ╠═220b673c-9304-11eb-0393-55e82f833b60
# ╟─904fef48-76be-11eb-26b4-fb433306bc3f
# ╠═c5777782-8bb7-11eb-2f40-3f2ca4012da6
# ╠═8030ddc6-76b5-11eb-030d-b79fd57b5a6f
# ╟─9cfe05a6-76be-11eb-06ed-a92dc0e547c4
# ╠═69bf9657-96b0-4b76-860e-c2653c78c89e
# ╠═1eff7f5a-76c0-11eb-247c-9bc3836564ed
# ╠═c92db4ce-76bd-11eb-0d6f-d55846f72e5b
# ╟─a4051f48-6952-11eb-036e-37b3de9c37c2
# ╠═6cf5fce8-6941-11eb-24f1-6d4281735519
# ╠═5281fcf6-6946-11eb-34b9-79d05e3ec288
# ╠═1a656cf2-694a-11eb-1c4f-edf4c99c3edc
# ╠═4e1fd39c-68cc-11eb-3c84-b3127a09c2a2
# ╠═69ff7b02-9302-11eb-38d7-6d020c3bc20d
# ╟─2fec9ae6-76c4-11eb-35cd-c5c79181ed02
# ╠═365c077a-76c4-11eb-14d1-47bcb751896c
# ╟─69854f4e-6952-11eb-28c5-6dcf989a98f4
# ╟─8672add8-6952-11eb-195e-4133c66f1e07
# ╠═1cb400e6-68ca-11eb-287e-75c3fb94873f
# ╟─9bee489c-694e-11eb-3d8f-735277e3db10
# ╠═346fba94-6950-11eb-0c18-43cb90d135b8
# ╠═47184b9e-68ca-11eb-2595-3b8b241a2b9f
# ╠═07ce7300-76b5-11eb-3132-2bea7add28e1
# ╠═96075d2a-76bd-11eb-2d9d-31333dd2f2fd
# ╠═38e89a48-d9d8-4289-9fce-08a672915cdb
# ╠═911d9356-6952-11eb-1712-bd37da0cba85

max_stars_repo_name: greimel/TabularMakie

max_issues_repo_name: greimel/TabularMakie

max_forks_repo_name: greimel/TabularMakie


text:
using ITensors, Test

@testset "diagITensor (DiagBlockSparse)" begin
  @testset "diagITensor get and set elements" begin
    i = Index(QN(0) => 2, QN(1) => 3; tags="i")

    D = diagITensor(QN(), i, dag(i'))

    for b in eachnzblock(D)
      @test flux(D, b) == QN()
    end

    D[i => 1, i' => 1] = 1
    D[i => 2, i' => 2] = 2
    D[i => 3, i' => 3] = 3
    D[i => 4, i' => 4] = 4
    D[i => 5, i' => 5] = 5

    @test_throws ErrorException D[i => 1, i' => 2] = 2.0

    @test D[i => 1, i' => 1] == 1
    @test D[i => 2, i' => 2] == 2
    @test D[i => 3, i' => 3] == 3
    @test D[i => 4, i' => 4] == 4
    @test D[i => 5, i' => 5] == 5
  end

  @testset "diagITensor Tuple constructor" begin
    i = Index(QN(0) => 2, QN(1) => 3; tags="i")

    D = diagITensor((i, dag(i')))

    for b in eachnzblock(D)
      @test flux(D, b) == QN()
    end
  end

  @testset "delta" begin
    i = Index(QN(0) => 2, QN(1) => 3; tags="i")
    ĩ = sim(i; tags="i_sim")
    j = Index(QN(0) => 2, QN(1) => 3, QN(2) => 4; tags="j")

    A = randomITensor(QN(), i, dag(j))

    δiĩ = δ(dag(i), ĩ)

    @test storage(δiĩ) isa NDTensors.DiagBlockSparse{ElT,ElT} where {ElT<:Number}

    B = A * δiĩ

    A = permute(A, i, j)
    B = permute(B, ĩ, j)

    @test norm(dense(NDTensors.tensor(A)) - dense(NDTensors.tensor(B))) ≈ 0
  end

  @testset "delta Tuple constructor" begin
    i = Index(QN(0) => 2, QN(1) => 3; tags="i")
    ĩ = sim(i; tags="i_sim")

    δiĩ = δ((dag(i), ĩ))

    for b in eachnzblock(δiĩ)
      @test flux(δiĩ, b) == QN()
    end
  end

  @testset "denseblocks: convert DiagBlockSparse to BlockSparse" begin
    i = Index([QN(0) => 2, QN(1) => 3])
    A = diagITensor(i', dag(i))
    randn!(ITensors.data(A))
    B = denseblocks(A)
    for n in 1:dim(i)
      @test A[n, n] == B[n, n]
    end
    @test dense(A) == dense(B)
  end

  @testset "Regression test for QN delta contraction bug" begin
    # http://itensor.org/support/2814/block-sparse-itensor-wrong-results-multiplying-delta-tensor
    s = Index([QN(("N", i, 1)) => 1 for i in 1:2])
    l = dag(addtags(s, "left"))
    r = addtags(s, "right")
    u = addtags(s, "up")
    d = dag(addtags(s, "down"))
    A = emptyITensor(l, r, u, d)
    A[1, 1, 1, 1] = 1.0
    A[1, 1, 2, 2] = 1.0
    A[2, 2, 1, 1] = 1.0
    A[2, 2, 2, 2] = 1.0
    δlr = δ(dag(l), dag(r))
    δud = δ(dag(u), dag(d))
    A1 = A * δlr
    denseA1 = dense(A) * dense(δlr)
    A2 = A1 * δud
    denseA2 = denseA1 * dense(δud)
    @test dense(A1) ≈ denseA1
    @test dense(A2) ≈ denseA2
    @test A2[] ≈ 4
  end

  @testset "Regression test for printing a QN Diag ITensor" begin
    # https://github.com/ITensor/NDTensors.jl/issues/61
    i = Index([QN() => 2])
    A = randomITensor(i', dag(i))
    U, S, V = svd(A, i')
    # Test printing S
    io = IOBuffer()
    show(io, S)
    sS = String(take!(io))
    @test sS isa String
    # Test printing U
    io = IOBuffer()
    show(io, U)
    sU = String(take!(io))
    @test sU isa String
  end
end

nothing


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

using Test, SparseArrays

const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :TestHelpers) || @eval Main include(joinpath($(BASE_TEST_PATH), "TestHelpers.jl"))
using .Main.TestHelpers.OAs

using Random
using Random.DSFMT

using Random: Sampler, SamplerRangeFast, SamplerRangeInt, MT_CACHE_F, MT_CACHE_I

@testset "Issue #6573" begin
    srand(0)
    rand()
    x = rand(384)
    @test findall(x .== rand()) == []
end

@test rand() != rand()
@test 0.0 <= rand() < 1.0
@test rand(UInt32) >= 0
@test -10 <= rand(-10:-5) <= -5
@test -10 <= rand(-10:5) <= 5
@test minimum([rand(Int32(1):Int32(7^7)) for i = 1:100000]) > 0
@test typeof(rand(false:true)) === Bool
@test typeof(rand(Char)) === Char
@test length(randn(4, 5)) == 20
@test length(randn(ComplexF64, 4, 5)) == 20
@test length(bitrand(4, 5)) == 20

@test rand(MersenneTwister(0)) == 0.8236475079774124
@test rand(MersenneTwister(42)) == 0.5331830160438613
# Try a seed larger than 2^32
@test rand(MersenneTwister(5294967296)) == 0.3498809918210497

# Test array filling, Issues #7643, #8360
@test rand(MersenneTwister(0), 1) == [0.8236475079774124]
let A = zeros(2, 2)
    rand!(MersenneTwister(0), A)
    @test A == [0.8236475079774124  0.16456579813368521;
                0.9103565379264364  0.17732884646626457]
end
let A = zeros(2, 2)
    @test_throws ArgumentError rand!(MersenneTwister(0), A, 5)
    @test rand(MersenneTwister(0), Int64, 1) == [2118291759721269919]
end
let A = zeros(Int64, 2, 2)
    rand!(MersenneTwister(0), A)
    @test A == [858542123778948672  5715075217119798169;
                8690327730555225005 8435109092665372532]
end

# rand from AbstractArray
let mt = MersenneTwister()
    @test rand(mt, 0:3:1000) in 0:3:1000
    @test issubset(rand!(mt, Vector{Int}(undef, 100), 0:3:1000), 0:3:1000)
    coll = Any[2, UInt128(128), big(619), "string"]
    @test rand(mt, coll) in coll
    @test issubset(rand(mt, coll, 2, 3), coll)

    # check API with default RNG:
    rand(0:3:1000)
    rand!(Vector{Int}(undef, 100), 0:3:1000)
    rand(coll)
    rand(coll, 2, 3)
end

# randn
@test randn(MersenneTwister(42)) == -0.5560268761463861
let A = zeros(2, 2)
    randn!(MersenneTwister(42), A)
    @test A == [-0.5560268761463861  0.027155338009193845;
                -0.444383357109696  -0.29948409035891055]
end

let B = zeros(ComplexF64, 2)
    randn!(MersenneTwister(42), B)
    @test B == [ComplexF64(-0.5560268761463861,-0.444383357109696),
                ComplexF64(0.027155338009193845,-0.29948409035891055)] * 0.7071067811865475244008
end

for T in (Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, BigInt,
          Float16, Float32, Float64, Rational{Int})
    r = rand(convert(T, 97):convert(T, 122))
    @test typeof(r) == T
    @test 97 <= r <= 122
    r = rand(convert(T, 97):convert(T,2):convert(T, 122),2)[1]
    @test typeof(r) == T
    @test 97 <= r <= 122
    @test mod(r,2)==1

    if T<:Integer && !(T===BigInt)
        x = rand(typemin(T):typemax(T))
        @test isa(x,T)
        @test typemin(T) <= x <= typemax(T)
    end
end

@test !any([(Random.maxmultiple(i)+i) > 0xFF for i in 0x00:0xFF])
@test all([(Random.maxmultiple(i)+1) % i for i in 0x01:0xFF] .== 0)
@test all([(Random.maxmultiple(i)+1+i) > 0xFF for i in 0x00:0xFF])
@test length(0x00:0xFF)== Random.maxmultiple(0x0)+1

if sizeof(Int32) < sizeof(Int)
    local r = rand(Int32(-1):typemax(Int32))
    @test typeof(r) == Int32
    @test -1 <= r <= typemax(Int32)
    for U = (Int64, UInt64)
        @test all(div(one(UInt128) << 52, k)*k - 1 == SamplerRangeInt(map(U, 1:k)).u
                  for k in 13 .+ Int64(2).^(32:51))
        @test all(div(one(UInt128) << 64, k)*k - 1 == SamplerRangeInt(map(U, 1:k)).u
                  for k in 13 .+ Int64(2).^(52:62))
    end
end

# BigInt specific
for T in [UInt32, UInt64, UInt128, Int128]
    local r, s
    s = big(typemax(T)-1000) : big(typemax(T)) + 10000
    # s is a 11001-length array
    @test rand(s) isa BigInt
    @test sum(rand(s, 1000) .== rand(s, 1000)) <= 20
    @test big(typemax(T)-1000) <= rand(s) <= big(typemax(T)) + 10000
    r = rand(s, 1, 2)
    @test size(r) == (1, 2)
    @test typeof(r) == Matrix{BigInt}
    guardsrand() do
        srand(0)
        r = rand(s)
        srand(0)
        @test rand(s) == r
    end
end

# Test ziggurat tables
ziggurat_table_size = 256
nmantissa           = Int64(2)^51 # one bit for the sign
ziggurat_nor_r      = parse(BigFloat,"3.65415288536100879635194725185604664812733315920964488827246397029393565706474")
erfc_zigg_root2     = parse(BigFloat,"2.580324876539008898343885504487203185398584536409033046076029509351995983934371e-04")
nor_section_area    = ziggurat_nor_r*exp(-ziggurat_nor_r^2/2) + erfc_zigg_root2*sqrt(big(π)/2)
emantissa           = Int64(2)^52
ziggurat_exp_r      = parse(BigFloat,"7.69711747013104971404462804811408952334296818528283253278834867283241051210533")
exp_section_area    = (ziggurat_exp_r + 1)*exp(-ziggurat_exp_r)

ki = Vector{UInt64}(undef, ziggurat_table_size)
wi = Vector{Float64}(undef, ziggurat_table_size)
fi = Vector{Float64}(undef, ziggurat_table_size)
# Tables for exponential variates
ke = Vector{UInt64}(undef, ziggurat_table_size)
we = Vector{Float64}(undef, ziggurat_table_size)
fe = Vector{Float64}(undef, ziggurat_table_size)
function randmtzig_fill_ziggurat_tables() # Operates on the global arrays
    wib = big.(wi)
    fib = big.(fi)
    web = big.(we)
    feb = big.(fe)
    # Ziggurat tables for the normal distribution
    x1 = ziggurat_nor_r
    wib[256] = x1/nmantissa
    fib[256] = exp(-0.5*x1*x1)
    # Index zero is special for tail strip, where Marsaglia and Tsang
    # defines this as
    # k_0 = 2^31 * r * f(r) / v, w_0 = 0.5^31 * v / f(r), f_0 = 1,
    # where v is the area of each strip of the ziggurat.
    ki[1] = trunc(UInt64,x1*fib[256]/nor_section_area*nmantissa)
    wib[1] = nor_section_area/fib[256]/nmantissa
    fib[1] = one(BigFloat)

    for i = 255:-1:2
        # New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus
        # need inverse operator of y = exp(-0.5*x*x) -> x = sqrt(-2*ln(y))
        x = sqrt(-2.0*log(nor_section_area/x1 + fib[i+1]))
        ki[i+1] = trunc(UInt64,x/x1*nmantissa)
        wib[i] = x/nmantissa
        fib[i] = exp(-0.5*x*x)
        x1 = x
    end

    ki[2] = UInt64(0)

    # Zigurrat tables for the exponential distribution
    x1 = ziggurat_exp_r
    web[256] = x1/emantissa
    feb[256] = exp(-x1)

    # Index zero is special for tail strip, where Marsaglia and Tsang
    # defines this as
    # k_0 = 2^32 * r * f(r) / v, w_0 = 0.5^32 * v / f(r), f_0 = 1,
    # where v is the area of each strip of the ziggurat.
    ke[1] = trunc(UInt64,x1*feb[256]/exp_section_area*emantissa)
    web[1] = exp_section_area/feb[256]/emantissa
    feb[1] = one(BigFloat)

    for i = 255:-1:2
        # New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus
        # need inverse operator of y = exp(-x) -> x = -ln(y)
        x = -log(exp_section_area/x1 + feb[i+1])
        ke[i+1] = trunc(UInt64,x/x1*emantissa)
        web[i] = x/emantissa
        feb[i] = exp(-x)
        x1 = x
    end
    ke[2] = zero(UInt64)

    wi[:] = wib
    fi[:] = fib
    we[:] = web
    fe[:] = feb
    return nothing
end
randmtzig_fill_ziggurat_tables()
@test all(ki == Random.ki)
@test all(wi == Random.wi)
@test all(fi == Random.fi)
@test all(ke == Random.ke)
@test all(we == Random.we)
@test all(fe == Random.fe)

#same random numbers on for small ranges on all systems
guardsrand() do
    seed = rand(UInt)
    srand(seed)
    r = map(Int64, rand(map(Int32, 97:122)))
    srand(seed)
    @test r == rand(map(Int64, 97:122))
    srand(seed)
    r = map(UInt64, rand(map(UInt32, 97:122)))
    srand(seed)
    @test r == rand(map(UInt64, 97:122))
end

for U in (Int64, UInt64)
    @test all(div(one(UInt64) << 52, k)*k - 1 == SamplerRangeInt(map(U, 1:k)).u
              for k in 13 .+ Int64(2).^(1:30))
end

#issue 8257
let i8257 = 1:1/3:100
    for i = 1:100
        @test rand(i8257) in i8257
    end
end

# test code paths of rand!

let mt = MersenneTwister(0)
    A128 = Vector{UInt128}()
    @test length(rand!(mt, A128)) == 0
    for (i,n) in enumerate([1, 3, 5, 6, 10, 11, 30])
        resize!(A128, n)
        rand!(mt, A128)
        @test length(A128) == n
        @test A128[end] == UInt128[0x15de6b23025813ad129841f537a04e40,
                                   0xcfa4db38a2c65bc4f18c07dc91125edf,
                                   0x33bec08136f19b54290982449b3900d5,
                                   0xde41af3463e74cb830dad4add353ca20,
                                   0x066d8695ebf85f833427c93416193e1f,
                                   0x48fab49cc9fcee1c920d6dae629af446,
                                   0x4b54632b4619f4eca22675166784d229][i]
    end

    srand(mt, 0)
    for (i,T) in enumerate([Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, Float16, Float32])
        A = Vector{T}(undef, 16)
        B = Vector{T}(undef, 31)
        rand!(mt, A)
        rand!(mt, B)
        @test A[end] == Any[21, 0x7b, 17385, 0x3086, -1574090021, 0xadcb4460, 6797283068698303107, 0xc8e6453e139271f3,
                            69855512850528774484795047199183096941, Float16(0.16895), 0.21086597f0][i]
        @test B[end] == Any[49, 0x65, -3725, 0x719d, 814246081, 0xdf61843a, 2120308604158549401, 0xcb28c236e9c0f608,
                            61881313582466480231846019869039259750, Float16(0.38672), 0.20027375f0][i]
    end

    srand(mt, 0)
    AF64 = Vector{Float64}(undef, Random.dsfmt_get_min_array_size()-1)
    @test rand!(mt, AF64)[end] == 0.957735065345398
    @test rand!(mt, AF64)[end] == 0.6492481059865669
    resize!(AF64, 2*length(mt.vals))
    @test invoke(rand!, Tuple{MersenneTwister,AbstractArray{Float64},Random.SamplerTrivial{Random.CloseOpen01_64}},
                 mt, AF64, Random.SamplerTrivial(Random.CloseOpen01()))[end]  == 0.1142787906708973
end

# Issue #9037
let mt = MersenneTwister(0)
    a = Vector{Float64}()
    resize!(a, 1000) # could be 8-byte aligned
    b = Vector{Float64}(undef, 1000) # should be 16-byte aligned
    c8 = Vector{UInt64}(undef, 1001)
    pc8 = pointer(c8)
    if Int(pc8) % 16 == 0
        # Make sure pc8 is not 16-byte aligned since that's what we want to test.
        # It still has to be 8-byte aligned since it is otherwise invalid on
        # certain architectures (e.g. ARM)
        pc8 += 8
    end
    c = unsafe_wrap(Array, Ptr{Float64}(pc8), 1000) # Int(pointer(c)) % 16 == 8

    for A in (a, b, c)
        local A
        srand(mt, 0)
        rand(mt) # this is to fill mt.vals, cf. #9040
        rand!(mt, A) # must not segfault even if Int(pointer(A)) % 16 != 0
        @test A[end-4:end] == [0.3371041633752143, 0.41147647589610803, 0.6063082992397912, 0.9103565379264364, 0.16456579813368521]
    end
end

# make sure reading 128-bit ints from RandomDevice works
let a = [rand(RandomDevice(), UInt128) for i=1:10]
    @test reduce(|, a)>>>64 != 0
end

# test all rand APIs
for rng in ([], [MersenneTwister(0)], [RandomDevice()])
    ftypes = [Float16, Float32, Float64]
    cftypes = [ComplexF16, ComplexF32, ComplexF64, ftypes...]
    types = [Bool, Char, BigFloat, Base.BitInteger_types..., ftypes...]
    randset = Set(rand(Int, 20))
    randdict = Dict(zip(rand(Int,10), rand(Int, 10)))
    collections = [BitSet(rand(1:100, 20))          => Int,
                   randset                          => Int,
                   GenericSet(randset)              => Int,
                   randdict                         => Pair{Int,Int},
                   GenericDict(randdict)            => Pair{Int,Int},
                   1:100                            => Int,
                   rand(Int, 100)                   => Int,
                   Int                              => Int,
                   Float64                          => Float64,
                   "qwèrtï"                         => Char,
                   GenericString("qwèrtï")          => Char,
                   OffsetArray(rand(2, 3), (4, -5)) => Float64]
    functypes = Dict(rand  => types, randn  => cftypes, randexp  => ftypes,
                     rand! => types, randn! => cftypes, randexp! => ftypes)

    b2 = big(2)
    u3 = UInt(3)
    for f in [rand, randn, randexp]
        f(rng...)                     ::Float64
        f(rng..., 5)                  ::Vector{Float64}
        f(rng..., 2, 3)               ::Array{Float64, 2}
        f(rng..., b2, u3)             ::Array{Float64, 2}
        for T in functypes[f]
            a0 = f(rng..., T)         ::T
            a1 = f(rng..., T, 5)      ::Vector{T}
            a2 = f(rng..., T, 2, 3)   ::Array{T, 2}
            a3 = f(rng..., T, b2, u3) ::Array{T, 2}
            a4 = f(rng..., T, (2, 3)) ::Array{T, 2}
            if T <: AbstractFloat && f === rand
                for a in [a0, a1..., a2..., a3..., a4...]
                    @test 0.0 <= a < 1.0
                end
            end
        end
    end
    for (C, T) in collections
        a0  = rand(rng..., C)                                                       ::T
        a1  = rand(rng..., C, 5)                                                    ::Vector{T}
        a2  = rand(rng..., C, 2, 3)                                                 ::Array{T, 2}
        a3  = rand(rng..., C, (2, 3))                                               ::Array{T, 2}
        a4  = rand(rng..., C, b2, u3)                                               ::Array{T, 2}
        a5  = rand!(rng..., Array{T}(undef, 5), C)                          ::Vector{T}
        a6  = rand!(rng..., Array{T}(undef, 2, 3), C)                       ::Array{T, 2}
        a7  = rand!(rng..., GenericArray{T}(undef, 5), C)                   ::GenericArray{T, 1}
        a8  = rand!(rng..., GenericArray{T}(undef, 2, 3), C)                ::GenericArray{T, 2}
        a9  = rand!(rng..., OffsetArray(Array{T}(undef, 5), 9), C)          ::OffsetArray{T, 1}
        a10 = rand!(rng..., OffsetArray(Array{T}(undef, 2, 3), (-2, 4)), C) ::OffsetArray{T, 2}
        @test size(a1) == (5,)
        @test size(a2) == size(a3) == (2, 3)
        for a in [a0, a1..., a2..., a3..., a4..., a5..., a6..., a7..., a8..., a9..., a10...]
            if C isa Type
                @test a isa C
            else
                @test a in C
            end
        end
    end
    for C in [1:0, Dict(), Set(), BitSet(), Int[],
              GenericDict(Dict()), GenericSet(Set()),
              "", Test.GenericString("")]
        @test_throws ArgumentError rand(rng..., C)
        @test_throws ArgumentError rand(rng..., C, 5)
    end
    for f! in [rand!, randn!, randexp!]
        for T in functypes[f!]
            X = T == Bool ? T[0,1] : T[0,1,2]
            for A in (Vector{T}(undef, 5),
                      Matrix{T}(undef, 2, 3),
                      GenericArray{T}(undef, 5),
                      GenericArray{T}(undef, 2, 3),
                      OffsetArray(Array{T}(undef, 5), -3),
                      OffsetArray(Array{T}(undef, 2, 3), (4, 5)))
                local A
                f!(rng..., A)                    ::typeof(A)
                if f! === rand!
                    f!(rng..., A, X)             ::typeof(A)
                    if A isa Array && T !== Char # Char/Integer comparison
                        f!(rng..., sparse(A))    ::typeof(sparse(A))
                        f!(rng..., sparse(A), X) ::typeof(sparse(A))
                    end
                end
            end
        end
    end

    bitrand(rng..., 5)             ::BitArray{1}
    bitrand(rng..., 2, 3)          ::BitArray{2}
    bitrand(rng..., b2, u3)        ::BitArray{2}
    rand!(rng..., BitVector(undef, 5))     ::BitArray{1}
    rand!(rng..., BitMatrix(undef, 2, 3))  ::BitArray{2}

    # Test that you cannot call randn or randexp with non-Float types.
    for r in [randn, randexp, randn!, randexp!]
        local r
        @test_throws MethodError r(Int)
        @test_throws MethodError r(Int32)
        @test_throws MethodError r(Bool)
        @test_throws MethodError r(String)
        @test_throws MethodError r(AbstractFloat)
        # TODO(#17627): Consider adding support for randn(BigFloat) and removing this test.
        @test_throws MethodError r(BigFloat)

        @test_throws MethodError r(Int64, (2,3))
        @test_throws MethodError r(String, 1)

        @test_throws MethodError r(rng..., Number, (2,3))
        @test_throws MethodError r(rng..., Any, 1)
    end
end

function hist(X, n)
    v = zeros(Int, n)
    for x in X
        v[floor(Int, x*n) + 1] += 1
    end
    v
end

# test uniform distribution of floats
for rng in [MersenneTwister(), RandomDevice()],
    T in [Float16, Float32, Float64, BigFloat],
        prec in (T == BigFloat ? [3, 53, 64, 100, 256, 1000] : [256])
    setprecision(BigFloat, prec) do
        # array version
        counts = hist(rand(rng, T, 2000), 4)
        @test minimum(counts) > 300 # should fail with proba < 1e-26
        # scalar version
        counts = hist([rand(rng, T) for i in 1:2000], 4)
        @test minimum(counts) > 300
    end
end

# test reproducility of methods
let mta = MersenneTwister(42), mtb = MersenneTwister(42)

    @test rand(mta) == rand(mtb)
    @test rand(mta,10) == rand(mtb,10)
    @test randn(mta) == randn(mtb)
    @test randn(mta,10) == randn(mtb,10)
    @test randexp(mta) == randexp(mtb)
    @test randexp(mta,10) == randexp(mtb,10)
    @test rand(mta,1:100) == rand(mtb,1:100)
    @test rand(mta,1:10,10) == rand(mtb,1:10,10)
    @test rand(mta,Bool) == rand(mtb,Bool)
    @test bitrand(mta,10) == bitrand(mtb,10)

    @test randstring(mta) == randstring(mtb)
    @test randstring(mta,10) == randstring(mtb,10)

    @test randsubseq(mta,1:10,0.4) == randsubseq(mtb,1:10,0.4)
    @test randsubseq!(mta,Int[],1:10,0.4) == randsubseq!(mtb,Int[],1:10,0.4)

    @test shuffle(mta,Vector(1:10)) == shuffle(mtb,Vector(1:10))
    @test shuffle!(mta,Vector(1:10)) == shuffle!(mtb,Vector(1:10))
    @test shuffle(mta,Vector(2:11)) == shuffle(mtb,2:11)
    @test shuffle!(mta, rand(mta, 2, 3)) == shuffle!(mtb, rand(mtb, 2, 3))
    @test shuffle(mta, rand(mta, 2, 3)) == shuffle(mtb, rand(mtb, 2, 3))

    @test randperm(mta,10) == randperm(mtb,10)
    @test sort!(randperm(10)) == sort!(shuffle(1:10)) == 1:10
    @test randperm(mta,big(10)) == randperm(mtb,big(10)) # cf. #16376
    @test randperm(0) == []
    @test eltype(randperm(UInt(1))) === Int
    @test_throws ErrorException randperm(-1)

    A, B = Vector{Int}(undef, 10), Vector{Int}(undef, 10)
    @test randperm!(mta, A) == randperm!(mtb, B)
    @test randperm!(A) === A

    @test randcycle(mta,10) == randcycle(mtb,10)
    @test eltype(randcycle(UInt(1))) === Int
    @test randcycle!(mta, A) == randcycle!(mtb, B)
    @test randcycle!(A) === A

    @test sprand(mta,1,1,0.9) == sprand(mtb,1,1,0.9)
    @test sprand(mta,10,10,0.3) == sprand(mtb,10,10,0.3)
end

# test MersenneTwister polynomial generation and jump
let seed = rand(UInt)
    mta = MersenneTwister(seed)
    mtb = MersenneTwister(seed)
    step = 25000*2
    size = 4
    jump25000 = "[encoded data, 5046 characters]";
    jump1e20  = "[encoded data, 5046 characters]"
    @test DSFMT.GF2X(jump25000) == DSFMT.calc_jump(25000)
    @test DSFMT.GF2X(jump1e20)  == DSFMT.calc_jump(big(10)^20)

    # check validity of the implementation of copy(::GF2X)
    let z = big(1); @assert z !== z+0 end

    # test PRNG jump

    mts = randjump(mta, 25000, size)
    @test length(mts) == 4

    for x in (rand(mts[k], Float64) for j=1:step, k=1:size)
        @test rand(mtb, Float64) == x
    end
end

# test that the following is not an error (#16925)
guardsrand() do
    srand(typemax(UInt))
    srand(typemax(UInt128))
end

# copy, == and hash
let seed = rand(UInt32, 10)
    r = MersenneTwister(seed)
    @test r == MersenneTwister(seed) # r.vals should be all zeros
    @test hash(r) == hash(MersenneTwister(seed))
    s = copy(r)
    @test s == r && s !== r
    @test hash(s) == hash(r)
    skip, len = rand(0:2000, 2)
    for j=1:skip
        rand(r)
        rand(s)
    end
    @test rand(r, len) == rand(s, len)
    @test s == r
    @test hash(s) == hash(r)
    h = rand(UInt)
    @test hash(s, h) == hash(r, h)
end

# MersenneTwister initialization with invalid values
@test_throws DomainError DSFMT.DSFMT_state(zeros(Int32, rand(0:DSFMT.JN32-1)))

@test_throws DomainError MersenneTwister(zeros(UInt32, 1), DSFMT.DSFMT_state(),
                                         zeros(Float64, 10), zeros(UInt128, MT_CACHE_I>>4), 0, 0)

@test_throws DomainError MersenneTwister(zeros(UInt32, 1), DSFMT.DSFMT_state(),
                                         zeros(Float64, MT_CACHE_F), zeros(UInt128, MT_CACHE_I>>4), -1, 0)

@test_throws DomainError MersenneTwister(zeros(UInt32, 1), DSFMT.DSFMT_state(),
                                         zeros(Float64, MT_CACHE_F), zeros(UInt128, MT_CACHE_I>>3), 0, 0)

@test_throws DomainError MersenneTwister(zeros(UInt32, 1), DSFMT.DSFMT_state(),
                                         zeros(Float64, MT_CACHE_F), zeros(UInt128, MT_CACHE_I>>4), 0, -1)

# seed is private to MersenneTwister
let seed = rand(UInt32, 10)
    r = MersenneTwister(seed)
    @test r.seed == seed && r.seed !== seed
    # RNGs do not share their seed in randjump
    let rs = randjump(r, big(10)^20, 2)
        @test  rs[1].seed !== rs[2].seed
        srand(rs[2])
        @test seed == rs[1].seed != rs[2].seed
    end
    resize!(seed, 4)
    @test r.seed != seed
end

# srand(rng, ...) returns rng (#21248)
guardsrand() do
    g = Random.GLOBAL_RNG
    m = MersenneTwister(0)
    @test srand() === g
    @test srand(rand(UInt)) === g
    @test srand(rand(UInt32, rand(1:10))) === g
    @test srand(m) === m
    @test srand(m, rand(UInt)) === m
    @test srand(m, rand(UInt32, rand(1:10))) === m
    @test srand(m, rand(1:10)) === m
end

# Issue 20062 - ensure internal functions reserve_1, reserve are type-stable
let r = MersenneTwister(0)
    @inferred Random.reserve_1(r)
    @inferred Random.reserve(r, 1)
end

# test randstring API
let b = ['0':'9';'A':'Z';'a':'z']
    for rng = [[], [MersenneTwister(0)]]
        @test length(randstring(rng...)) == 8
        @test length(randstring(rng..., 20)) == 20
        @test issubset(randstring(rng...), b)
        for c = ['a':'z', "qwèrtï", Set(codeunits("gcat"))],
                len = [8, 20]
            s = len == 8 ? randstring(rng..., c) : randstring(rng..., c, len)
            @test length(s) == len
            if eltype(c) == Char
                @test issubset(s, c)
            else # UInt8
                @test issubset(s, Set(Char(v) for v in c))
            end
        end
    end
    @test randstring(MersenneTwister(0)) == randstring(MersenneTwister(0), b)
end

# this shouldn't crash (#22403)
@test_throws ArgumentError rand!(Union{UInt,Int}[1, 2, 3])

@testset "$RNG() & srand(rng::$RNG) initializes randomly" for RNG in (MersenneTwister, RandomDevice)
    m = RNG()
    a = rand(m, Int)
    m = RNG()
    @test rand(m, Int) != a
    # passing `nothing` is equivalent to passing nothing
    m = RNG(nothing)
    b = rand(m, Int)
    @test b != a
    srand(m)
    c = rand(m, Int)
    @test c ∉ (a, b)
    srand(m)
    @test rand(m, Int) ∉ (a, b, c)
    srand(m, nothing)
    d = rand(m, Int)
    @test d ∉ (a, b, c)
    srand(m, nothing)
    @test rand(m, Int) ∉ (a, b, c, d)
end

@testset "MersenneTwister($seed) & srand(m::MersenneTwister, $seed) produce the same stream" for seed in [0:5; 10000:10005]
    m = MersenneTwister(seed)
    a = [rand(m) for _=1:100]
    srand(m, seed)
    @test a == [rand(m) for _=1:100]
end

struct RandomStruct23964 end
@testset "error message when rand not defined for a type" begin
    @test_throws ArgumentError rand(nothing)
    @test_throws ArgumentError rand(RandomStruct23964())
end

@testset "rand(::$RNG, ::UnitRange{$T}" for RNG ∈ (MersenneTwister(), RandomDevice()),
                                                 T ∈ (Int32, UInt32, Int64, Int128, UInt128)
    RNG isa MersenneTwister && srand(RNG, rand(UInt128)) # for reproducibility
    r = T(1):T(108)
    @test rand(RNG, SamplerRangeFast(r)) ∈ r
end

@testset "eltype for UniformBits" begin
    @test eltype(Random.UInt52()) == UInt64
    @test eltype(Random.UInt52(UInt128)) == UInt128
    @test eltype(Random.UInt104()) == UInt128
end


text:
# Autogenerated wrapper script for libgraphicsmagic_jll for x86_64-linux-gnu-cxx03
export gm, libGraphicsMagic, libGraphicsMagicPlusPlus, libGraphicsMagicWand

using CompilerSupportLibraries_jll
using libwebp_jll
using libpng_jll
using FreeType2_jll
using Ghostscript_jll
using Xorg_libXext_jll
using Xorg_libSM_jll
using Libtiff_jll
using LittleCMS_jll
using Bzip2_jll
using XZ_jll
using Libtool_jll
JLLWrappers.@generate_wrapper_header("libgraphicsmagic")
JLLWrappers.@declare_executable_product(gm)
JLLWrappers.@declare_library_product(libGraphicsMagic, "libGraphicsMagick.so.3")
JLLWrappers.@declare_library_product(libGraphicsMagicPlusPlus, "libGraphicsMagick++.so.12")
JLLWrappers.@declare_library_product(libGraphicsMagicWand, "libGraphicsMagickWand.so.2")
function __init__()
    JLLWrappers.@generate_init_header(CompilerSupportLibraries_jll, libwebp_jll, libpng_jll, FreeType2_jll, Ghostscript_jll, Xorg_libXext_jll, Xorg_libSM_jll, Libtiff_jll, LittleCMS_jll, Bzip2_jll, XZ_jll, Libtool_jll)
    JLLWrappers.@init_executable_product(
        gm,
        "bin/gm",
    )

    JLLWrappers.@init_library_product(
        libGraphicsMagic,
        "lib/libGraphicsMagick.so",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@init_library_product(
        libGraphicsMagicPlusPlus,
        "lib/libGraphicsMagick++.so",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@init_library_product(
        libGraphicsMagicWand,
        "lib/libGraphicsMagickWand.so",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

    JLLWrappers.@generate_init_footer()
end  # __init__()


text:
# Ridge Regression (Tikhonov regularization)

#### auxiliary

function lreg_chkdims(X::AbstractMatrix, Y::AbstractVecOrMat, trans::Bool)
    mX, nX = size(X)
    dX = ifelse(trans, mX, nX)
    dY = ifelse(trans, nX, mX)
    size(Y, 1) == dY || throw(DimensionMismatch("Dimensions of X and Y mismatch."))
    return dX
end

lrsoltype{T}(::DenseVector{T}) = Vector{T}
lrsoltype{T}(::DenseMatrix{T}) = Matrix{T}

_vaug{T}(X::DenseMatrix{T}) = vcat(X, ones(T, 1, size(X,2)))::Matrix{T}
_haug{T}(X::DenseMatrix{T}) = hcat(X, ones(T, size(X,1), 1))::Matrix{T}

## linear least square

function llsq{T<:FloatingPoint}(X::DenseMatrix{T}, Y::DenseVecOrMat{T};
                                trans::Bool=false, bias::Bool=true)
    if trans
        mX, nX = size(X)
        size(Y, 1) == nX || throw(DimensionMismatch("Dimensions of X and Y mismatch."))
        mX <= nX || error("mX <= nX is required when trans is false.")
    else
        mX, nX = size(X)
        size(Y, 1) == mX || throw(DimensionMismatch("Dimensions of X and Y mismatch."))
        mX >= nX || error("mX >= nX is required when trans is false.")
    end
    _ridge(X, Y, zero(T), trans, bias)
end

## ridge regression

function ridge{T<:FloatingPoint}(X::DenseMatrix{T}, Y::DenseVecOrMat{T}, r::Real;
                                trans::Bool=false, bias::Bool=true)
    lreg_chkdims(X, Y, trans)
    r >= zero(r) || error("r must be non-negative.")
    _ridge(X, Y, convert(T, r), trans, bias)
end

function ridge{T<:FloatingPoint}(X::DenseMatrix{T}, Y::DenseVecOrMat{T}, r::DenseVector{T};
                                trans::Bool=false, bias::Bool=true)
    d = lreg_chkdims(X, Y, trans)
    length(r) == d || throw(DimensionMismatch("Incorrect length of r."))
    _ridge(X, Y, r, trans, bias)
end

function ridge{T<:FloatingPoint}(X::DenseMatrix{T}, Y::DenseVecOrMat{T}, r::DenseMatrix{T};
                                trans::Bool=false, bias::Bool=true)
    d = lreg_chkdims(X, Y, trans)
    size(r) == (d, d) || throw(DimensionMismatch("Incorrect size of r."))
    _ridge(X, Y, r, trans, bias)
end

## implementation

function _ridge{T<:FloatingPoint}(X::DenseMatrix{T}, Y::DenseVecOrMat{T},
                                  r::Union(Real, DenseVecOrMat), trans::Bool, bias::Bool)
    if bias
        if trans
            X_ = _vaug(X)
            A = cholfact!(_ridge_reg!(A_mul_Bt(X_, X_), r, bias)) \ (X_ * Y)
        else
            X_ = _haug(X)
            A = cholfact!(_ridge_reg!(X_'X_, r, bias)) \ (X_'Y)
        end
    else
        if trans
            A = cholfact!(_ridge_reg!(A_mul_Bt(X, X), r, bias)) \ (X * Y)
        else
            A = cholfact!(_ridge_reg!(X'X, r, bias)) \ (X'Y)
        end
    end
    return A::lrsoltype(Y)
end

function _ridge_reg!(Q::Matrix, r::Real, bias::Bool)
    if r > zero(r)
        n = size(Q, 1) - int(bias)
        for i = 1:n
            @inbounds Q[i,i] += r
        end
    end
    return Q
end

function _ridge_reg!(Q::Matrix, r::DenseVector, bias::Bool)
    n = size(Q, 1) - int(bias)
    @assert length(r) == n
    for i = 1:n
        @inbounds Q[i,i] += r[i]
    end
    return Q
end

function _ridge_reg!(Q::Matrix, r::Matrix, bias::Bool)
    n = size(Q, 1) - int(bias)
    @assert size(r) == (n, n)
    for j = 1:n, i = 1:n
        @inbounds Q[i,j] += r[i,j]
    end
    return Q
end


text:
using NLOptControl
using VehicleModels
using MAVs

#s=Settings(;format=:png,MPC=false,save=true);
#TODO check settings -> some og thermare killing it
#=

julia> include("testPathFollowing.jl")
ERROR: LoadError: KNITRO: Error initializing problem
 in #init_problem#6(::Array{Float64,1}, ::Array{Float64,1}, ::Function, ::KNITRO.KnitroProblem, ::Int32, ::Int32, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Int32,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Int32,1}, ::Array{Int32,1}, ::Array{Int32,1}, ::Array{Int32,1}) at /home/febbo/.julia/v0.5/KNITRO/src/ktr_functions.jl:546
 in (::KNITRO.#kw##init_problem)(::Array{Any,1}, ::KNITRO.#init_problem, ::KNITRO.KnitroProblem, ::Int32, ::Int32, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Int32,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Int32,1}, ::Array{Int32,1}, ::Array{Int32,1}, ::Array{Int32,1}) at ./<missing>:0
 in #initializeProblem#2(::Array{Float64,1}, ::Ptr{Void}, ::Function, ::KNITRO.KnitroProblem, ::Int32, ::Int32, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Int32,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Int32,1}, ::Array{Int32,1}, ::Array{Int32,1}, ::Array{Int32,1}) at /home/febbo/.julia/v0.5/KNITRO/src/KNITRO.jl:97
 in (::KNITRO.#kw##initializeProblem)(::Array{Any,1}, ::KNITRO.#initializeProblem, ::KNITRO.KnitroProblem, ::Int32, ::Int32, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Int32,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Int32,1}, ::Array{Int32,1}, ::Array{Int32,1}, ::Array{Int32,1}) at ./<missing>:0
 in optimize!(::KNITRO.KnitroMathProgModel) at /home/febbo/.julia/v0.5/KNITRO/src/KnitroSolverInterface.jl:229
 in #solvenlp#162(::Bool, ::Function, ::JuMP.Model, ::JuMP.ProblemTraits) at /home/febbo/.julia/v0.5/JuMP/src/nlp.jl:1271
 in (::JuMP.#kw##solvenlp)(::Array{Any,1}, ::JuMP.#solvenlp, ::JuMP.Model, ::JuMP.ProblemTraits) at ./<missing>:0
 in #solve#109(::Bool, ::Bool, ::Bool, ::Array{Any,1}, ::Function, ::JuMP.Model) at /home/febbo/.julia/v0.5/JuMP/src/solvers.jl:170
 in #optimize#15(::Int64, ::Function, ::JuMP.Model, ::NLOptControl.NLOpt, ::NLOptControl.Result, ::NLOptControl.Settings) at /home/febbo/.julia/v0.5/NLOptControl/src/utils.jl:287
 in initializePathFollowing(::MAVs.CaseModule.Case) at /home/febbo/.julia/v0.5/MAVs/src/PathFollowing.jl:140
 in include_from_node1(::String) at ./loading.jl:488
while loading /home/febbo/.julia/v0.5/MAVs/examples/pathFollowing/testPathFollowing.jl, in expression starting on line 11

=#
# initialize problem\
#c=defineCase(;(:mode=>:path));
c=defineCase(;(:mode=>:caseStudy));

mdl,n,r,params=initializePathFollowing(c);

optimize(mdl,n,r,s); # first run

# postProcess
using PrettyPlots, Plots
pyplot()
r.results_dir = string(r.main_dir,"/results/","TMP/");
resultsDir(r.results_dir);
allPlots(n,r,s,1);
pSimPath(n,r,s,c,1)


text:
package java.lang;

public class RuntimeException extends Exception {
    public RuntimeException() { this.s = null; }
    public RuntimeException(String s) { this.s = s; }
    private final String s;
}

max_stars_repo_name: HarvardPL/cryptoerase

max_issues_repo_name: HarvardPL/cryptoerase

max_forks_repo_name: HarvardPL/cryptoerase


text:
#using PolyhedralOmega, LinearAlgebra, Test
using LinearAlgebra, Test, PolyhedralOmega

include("../src/SymbolicCone.jl")
@testset "macmahon" begin
    @test PolyhedralOmega.macmahon([1 -1; -2 1], [0, 0]) != nothing
end

max_stars_repo_name: kevseryolcu/PolyhedralOmega

max_issues_repo_name: kevseryolcu/PolyhedralOmega

max_forks_repo_name: kevseryolcu/PolyhedralOmega


text:
using ITensors
using PastaQ
using Random
using HDF5

Random.seed!(1234)

N = 4
depth = 4
nshots = 5
circuit = randomcircuit(N, depth)

# 1. Generation of measurement data on the quantum states
# at the output of a circuit. Each data-point is a projetive
# measurement in an arbitrary local basis. The default local basis
# is `["X","Y","Z"]`.
# a) Unitary circuit
# Returns output state as MPS
println("Generate samples from random projective measurements of the state U|0,0,…>:")
data, ψ = getsamples(circuit, nshots; local_basis=["X", "Y", "Z"])
# Example of writing and reading
writesamples(data, ψ, "data/qst_circuit.h5")
data, ψ = readsamples("data/qst_circuit.h5")
@show maxlinkdim(ψ)
display(data)
println()

# Note: the above is equivalent to:
#> bases = randombases(N,nshots)
#> ψ = runcircuit(N,gates)
#> data = getsamples(ψ,nshots,bases)

# b) Noisy circuit
# Returns the mixed density operator as MPO
println(
  "Generate samples from random projective measurements of the state ρ = ε(|0,0,…⟩⟨0,0,…|) generated from noisy gate evolution:",
)
data, ρ = getsamples(
  circuit, nshots; local_basis=["X", "Y", "Z"], noise=("amplitude_damping", (γ=0.01,))
)
# Example of writing and reading
writesamples(data, ρ, "data/qst_circuit_noisy.h5")
data, ρ = readsamples("data/qst_circuit_noisy.h5")
@show maxlinkdim(ρ)
display(data)
println()

# 2. Generation of measurerment data for quantum process
# tomography. Each measurement consist of a input product
# state and an output projective measurement in a arbitrary
# local basis. By default, the single-qubit input states are
# the 6 eigenstates of Pauli operators.
# Return the MPO for the unitary circuit
println(
  "Generate samples from random input states and random project measurements of the circuit U:",
)
data, U = getsamples(circuit, nshots; local_basis=["X", "Y", "Z"], process=true)
# Example of writing and reading
writesamples(data, U, "data/qpt_circuit.h5")
data, U = readsamples("data/qpt_circuit.h5")
if !isnothing(U)
  @show maxlinkdim(U)
end
display(data)
println()

println(
  "Generate samples from random input states and random project measurements of the Choi matrix Λ generated from noisy gate evolution:",
)
# Returns the Choi matrix `Λ` as MPO wiith `2N` sites
data, Λ = getsamples(
  circuit,
  nshots;
  local_basis=["X", "Y", "Z"],
  process=true,
  noise=("amplitude_damping", (γ=0.01,)),
)
# Example of writing and reading
writesamples(data, Λ, "data/qpt_circuit_noisy.h5")
data, Λ = readsamples("data/qpt_circuit_noisy.h5")
if !isnothing(Λ)
  @show maxlinkdim(Λ)
end
display(data)
println()


text:
using BoardGameGeek
using Documenter

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

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

deploydocs(; repo="github.com/adrhill/BoardGameGeek.jl", devbranch="main")


text:
using Colors
DEFAULTSYMBOLS = [:original, :auto, :default]
iter_expand(e) = Base.Iterators.repeated(e)
iter_expand(l::Vector) = Base.Iterators.cycle(l)
iter_expand(r::AbstractRange) = IterGen(st->rand(r))
iter_expand(t::Tuple) = IterGen(st->rand(t))
struct IterGen
    generator
end
Base.iterate(it::IterGen, state=0) = it.generator(state),state+1
Base.IteratorSize(it::IterGen) = Base.IsInfinite()

"""
load a img as mask, recolor, or resize, etc
## examples
* loadmask(open("res/heart.jpg"), 256, 256) #resize to 256*256
* loadmask("res/heart.jpg", ratio=0.3) #scale 0.3
* loadmask("res/heart.jpg", color="red", ratio=2) #set forecolor
* loadmask("res/heart.jpg", transparent=rgba->maximum(rgba[1:3])*(rgba[4]/255)>128) #set transparent with a Function
* loadmask("res/heart.jpg", color="red", transparent=(1,1,1)) #set forecolor and transparent
* loadmask("res/heart.svg") #other arguments are not supported
About orther keyword arguments like outline, linecolor, smoothness, see function `outline`.
"""
function loadmask(img::AbstractMatrix{<:TransparentRGB}, args...;
    color=:auto, backgroundcolor=:auto, transparent=:auto,
    outline=0,  linecolor="black", smoothness=0.5, padding=0, kargs...)
    copied = false
    if !(isempty(args) && isempty(kargs))
        img = imresize(img, args...; kargs...)
        copied = true
    end
    if color ∉ DEFAULTSYMBOLS || backgroundcolor ∉ DEFAULTSYMBOLS
        copied || (img = copy(img))
        mask = imagemask(img, transparent)
        if color ∉ DEFAULTSYMBOLS
            color = parsecolor(color)
            alpha(color) == 1 || @warn "the alpha channel is ignored"
            m = @view img[mask]
            Render.recolor!(m, color) #保持透明度
        end
        if backgroundcolor ∉ DEFAULTSYMBOLS
            backgroundcolor = parsecolor(backgroundcolor)
            m = @view img[.~mask]
            m .= convert.(eltype(img), backgroundcolor) #不保持透明度
        end
    end
    if outline > 0
        img = Render.outline(img, linewidth=outline, color=linecolor, smoothness=smoothness,
        transparent=transparent)
    end
    if padding != 0
        bc = backgroundcolor in DEFAULTSYMBOLS ? :auto : bc
        img = Render.padding(img, padding, backgroundcolor=bc)
    end
    img
end
function loadmask(img::AbstractMatrix{<:Colorant}, args...; kargs...)
    loadmask(ARGB.(img), args...; kargs...)
end
function loadmask(img::SVGImageType, args...; padding=0, transparent=:auto, outline=0, linecolor=:auto, kargs...)
    if !isempty(args) || !isempty(v for v in values(values(kargs)) if v ∉ DEFAULTSYMBOLS) || outline != 0
        @warn "editing svg file is not supported: $args $kargs"
    end
    if padding != 0
        bc = get(kargs, :backgroundcolor, (0,0,0,0))
        bc = bc in DEFAULTSYMBOLS ? (0,0,0,0) : bc
        img = Render.padding(img, padding, backgroundcolor=bc)
    end
    img
end
function loadmask(file, args...; kargs...)
    mask = Render.load(file)
    loadmask(mask, args...; kargs...)
end

"like `paint` but export svg"
function paintsvg(wc::WC; background=true)
    imgs = getsvgimages(wc)
    poss = getpositions(wc)
    if background == false || background === nothing
        sz = size(wc.mask)
        bgcolor = (1,1,1,0)
    else
        if background == true
            bgcolor = getbackgroundcolor(wc)
            bgcolor = bgcolor in DEFAULTSYMBOLS ? (1,1,1,0) : bgcolor
            background = getsvgmask(wc)
            if background === nothing
                @warn "embed bitmap into SVG. You can set `background=false` to remove background."
                background = getmask(wc)
            end
        else
            bgcolor = (1,1,1,0)
        end
        imgs = Iterators.flatten(((background,), imgs))
        poss = Iterators.flatten((((1, 1),), poss))
        sz = size(background)
    end
    Render.overlay(imgs, poss, backgroundcolor=bgcolor, size=reverse(sz))
end
function paintsvg(wc::WC, file, args...; kargs...)
    img = paintsvg(wc, args...; kargs...)
    Render.save(file, img)
    img
end

"""
# examples
* paint(wc::WC)
* paint(wc::WC, background=false) #no background
* paint(wc::WC, background=outline(wc.mask)) #use a new background
* paint(wc::WC, ratio=0.5) #resize the result
* paint(wc::WC, "result.png", ratio=0.5) #save as png file, other bitmap formats may also work
* paint(wc::WC, "result.svg") #save as svg file
"""
function paint(wc::WC, args...; background=true, kargs...)
    if background == true
        bgcolor = getbackgroundcolor(wc)
        bgcolor = bgcolor in DEFAULTSYMBOLS ? (1,1,1,0) : bgcolor
        background = fill(convert(eltype(wc.mask), parsecolor(bgcolor)), size(wc.mask))
        overlay!(background, wc.mask)
    elseif background == false || background === nothing
        background = fill(convert(eltype(wc.mask), parsecolor((1,1,1,0))), size(wc.mask))
    else
        background = copy(background)
    end
    overlay!(background, wc.imgs, getpositions(wc))
    if !(isempty(args) && isempty(kargs))
        background = ARGB.(background) #https://github.com/JuliaImages/ImageTransformations.jl/issues/97
        background = imresize(background, args...; kargs...)
    end
    background
end

function paint(wc::WC, file, args...; kargs...)
    if endswith(file, r".svg|.SVG")
        img = paintsvg(wc, args...; kargs...)
    else
        img = paint(wc, args...; kargs...)
    end
    Render.save(file, img)
    img
end

function record(wc::WC, label::AbstractString, gif_callback=x->x)
#     @show size(n1)
    resultpic = overlay!(paint(wc),
        rendertextoutlines(label, 32, color="black", linecolor="white", linewidth=1), 20, 20)
    gif_callback(resultpic)
end

runexample(example=:random) = @time evalfile(pkgdir(WordCloud)*"/examples/$(example).jl")
showexample(example=:random) = read(pkgdir(WordCloud)*"/examples/$(example).jl", String)|>print
examples = [e[1:prevind(e, end, 3)] for e in basename.(readdir(pkgdir(WordCloud)*"/examples")) if endswith(e, ".jl")]
@doc "Available values: [" * join(":".*examples, ", ") * "]" runexample
@doc "Available values: [" * join(":".*examples, ", ") * "]" showexample
function runexamples(examples=examples)
    println(length(examples), " examples: ", examples)
    for (i,e) in enumerate(examples)
        println("="^20, "\n# ",i,"/",length(examples), "\t", e, "\n", "="^20)
        runexample(e)
    end
end


text:
#Function that is passed two pointers, sets the value of y to x*x
function square(x,y)
    y[1]=x[1]*x[1]
end

max_stars_repo_name: CAB-LAB/DataFarm

max_issues_repo_name: esa-esdl/DataFarm

max_forks_repo_name: esa-esdl/DataFarm


text:
#!/usr/bin/env bash
    #=
    exec julia -tauto --project="$(realpath $(dirname $0))" --color=yes --startup-file=no -e "include(popfirst!(ARGS))" \
    "${BASH_SOURCE[0]}" "$@"
    =#

include("plot_random_core.jl")

if isempty(ARGS) || length(ARGS) < 4
	throw(error("""
	Please specify the number of datapoints you want to calculate from the command line, along with q, n, and d.  For example
		./examples/plot_random.jl 6 7 3 1000 10000 # q, n, d, num_data_points, selection_set_size
	That is respectively: the size of the alphabet; the block length of the words; the distance of the code; the number of datapoints in the code; and the selection set size of each "random" sample.
	"""))
end

global q = parse(Int, ARGS[1])
global n = parse(Int, ARGS[2])
global d = parse(Int, ARGS[3])
global stop_at = parse(Int, ARGS[4])
global m = ifelse(length(ARGS) > 4, parse(Int, ARGS[5]), 10_000)

global upper_bound_adjustment = 10
global 🐖 = length(get_codewords_greedy(q, n, d))
global 🍖 = hamming_bound(q, n, d)
global 🐺 = singleton_bound(q, n, d)
graphing(q, n, d, stop_at, m = m)


text:
# Use baremodule to shave off a few KB from the serialized `.ji` file
baremodule BlueZ_jll
using Base
using Base: UUID
import JLLWrappers

JLLWrappers.@generate_main_file_header("BlueZ")
JLLWrappers.@generate_main_file("BlueZ", UUID("471b5b61-da80-5748-8755-67d5084d21f2"))
end  # module BlueZ_jll