text:
abstract type AbstractDiscreteMarkovChain <: AbstractMarkovChain end

"""
    DiscreteMarkovChain(transition_matrix)
    DiscreteMarkovChain(state_space, transition_matrix)
    DiscreteMarkovChain(continuous_markov_chain)

Creates a new discrete Markov chain object.

# Arguments
- `state_space`: The names of the states that make up the Markov chain.
- `transition_matrix`: The single step transition probability matrix.
- `continuous_markov_chain`: An instance of `ContinuousMarkovChain`.

# Examples
The following shows a basic Sunny-Cloudy-Rainy weather model.
```jldoctest DiscreteMarkovChain
using DiscreteMarkovChains
T = [
    0.9 0.1 0;
    0.5 0.2 0.3;
    0.1 0.4 0.5
]
X = DiscreteMarkovChain(["Sunny", "Cloudy", "Rainy"], T)
println(state_space(X))

# output

["Sunny", "Cloudy", "Rainy"]
```

```jldoctest DiscreteMarkovChain
println(transition_matrix(X))

# output

[0.9 0.1 0.0; 0.5 0.2 0.3; 0.1 0.4 0.5]
```

# References
1. [Wikipedia](https://en.wikipedia.org/wiki/Markov_chain#Discrete-time_Markov_chain)
2. [Dartmouth College](https://www.dartmouth.edu/~chance/teaching_aids/books_articles/probability_book/Chapter11.pdf)
"""
struct DiscreteMarkovChain <: AbstractDiscreteMarkovChain
    state_space
    transition_matrix
    function DiscreteMarkovChain(state_space, transition_matrix)
        check(state_space, transition_matrix, DiscreteMarkovChain)
        new(state_space, transition_matrix)
    end
end
"""
    required_row_sum(type)

# Arguments
- `type`: The type of Markov chain. It can be
`DiscreteMarkovChain` or `ContinuousMarkovChain`.

# Returns
The number that each row in the transition matrix should sum up to.
"""
required_row_sum(::Type{<:AbstractDiscreteMarkovChain}) = 1
required_row_sum(::Type{<:AbstractDiscreteMarkovChain}, ::Type{T}) where T = one(T)
function DiscreteMarkovChain(transition_matrix)
    return DiscreteMarkovChain(1:(size(transition_matrix)[1]), transition_matrix)
end

"""
    characteristic_matrix(::AbstractDiscreteMarkovChain)
    characteristic_matrix(::AbstractContinuousMarkovChain)

# Definitions
Many derivations and interesting ideas about Markov chains
involve the identity matrix or zero matrix somewhere along
the line. Most of the time, the identity matrix appears
more often in discrete Markov chains. Instead of the identity
matrix, the zero matrix appears in its place for continuous
Markov chains.

# Returns
The identity matrix if its argument is an instance of
`AbstractDiscreteMarkovChain`. The zero matrix if its
argument is an instance of `AbstractContinuousMarkovChain`
"""
characteristic_matrix(::AbstractDiscreteMarkovChain) = LinearAlgebra.I

"""
    periodicities(x::AbstractDiscreteMarkovChain)

A more advanced version of `communication_classes`
designed for discrete Markov chains. It is the same as
`communication_classes` but it returns periodicities as well.

# Definitions
The period, ``d_i`` of a state ``i`` is the greatest common denominator
of all integers ``n ∈ \\mathbb{N}`` for which ``p^{(n)}_{i,i} > 0``.
Written more succinctly,
```math
d_i = \\text{gcd}\\{ n ∈ \\mathbb{N} | p^{(n)}_{i,i} > 0 \\}
```
If ``d_i=1`` then state ``i`` is said to be aperiodic.

# Arguments
- `x`: some kind of discrete Markov chain.

# Returns
A tuple containing 3 arrays.
- This first array contains C arrays which store the states that communicate.
- The second array is an array of Bool where the ith value is true if the
  ith communication class is recurrent.
- The third array is the periodicity of each communication class.

# Examples
```jldoctest periodicities
using DiscreteMarkovChains
T = [
    1 0;
    0 1;
]
X = DiscreteMarkovChain(T)

periodicities(X)

# output

([[1], [2]], Any[true, true], Any[1, 1])
```

So the Markov chain has two communication classes and both are recurrent and aperiodic.

```jldoctest periodicities
T = [
    0.0 1.0 0.0;
    1.0 0.0 0.0;
    0.1 0.2 0.7;
]
X = DiscreteMarkovChain(["Sunny", "Cloudy", "Rainy"], T)

periodicities(X)

# output

([["Sunny", "Cloudy"], ["Rainy"]], Any[true, false], Any[2, 1])
```

So the Sunny and Cloudy states communicate and are recurrent with period 2.
The Rainy state is transient and aperiodic.
Note that this is not a very good weather model since once it
stops raining, it will never rain again. Also, each day after that,
the process will oscillate between Sunny and Cloudy.
"""
function periodicities(x::AbstractDiscreteMarkovChain)
    SI = state_index(x)
    T = transition_matrix(x)

    classes, recurrence = communication_classes(x)
    index_classes = [[SI[state] for state in class] for class in classes]

    periods = []
    for class in index_classes
        submatrix = T[class, class]
        push!(periods, breadth_first_search(submatrix))
    end
    return classes, recurrence, periods
end

"""
    is_regular(x)

# Definitions
A Markov chain is called a regular chain if some power of the
transition matrix has only positive elements. This is equivalent
to being ergodic and aperiodic.

# Arguments
- `x`: some kind of discrete Markov chain.

# Returns
`true` if the Markov chain, `x`, is regular.

# Examples
We will set up a matrix with 2 communication classes and
show that it is not regular.

```jldoctest is_regular
using DiscreteMarkovChains
T = [
    0 1 0;
    1 0 0;
    0 0 1;
]
X = DiscreteMarkovChain(T)

is_regular(X)

# output

false
```

Repeat the above but now all states communicate.

```jldoctest is_regular
T = [
    0.0 0.5 0.5;
    0.0 0.0 1.0;
    1.0 0.0 0.0;
]
X = DiscreteMarkovChain(T)

is_regular(X)

# output

true
```

Notice how a periodic chain is not regular even though
there is only one communication class.

```jldoctest is_regular
T = [
    0 1 0;
    0 0 1;
    1 0 0;
]
X = DiscreteMarkovChain(T)

is_regular(X)

# output

false
```
"""
function is_regular(x::AbstractDiscreteMarkovChain)
    classes, _, periods = periodicities(x)
    if length(classes) == 0
        return false
    end
    return (length(classes) == 1) & (periods[1] == 1)
end

"""
    first_passage_probabilities(x, t, i=missing, j=missing)

# Definitions
This is the probability that the process enters state ``j``
for the first time at time ``t`` given that the process started
in state ``i`` at time 0. That is, ``f^{(t)}_{i,j}``. If no `i`
or `j` is given, then it will return a matrix instead with
entries ``f^{(t)}_{i,j}`` for `i` and `j` in the state space of `x`.

# Why Do We Use A Slow Algorithm?
So that `t` can be symbolic if nessesary. That is, if symbolic math
libraries want to use this library, it will pose no hassle.

# Arguments
- `x`: some kind of Markov chain.
- `t`: the time to calculate the first passage probability.
- `i`: the state that the prcess starts in.
- `j`: the state that the process must reach for the first time.

# Returns
A scalar value or a matrix depending on whether `i` and `j` are given.

# Examples
```jldoctest first_passage_probabilities
using DiscreteMarkovChains
T = [
    0.1 0.9;
    0.3 0.7;
]
X = DiscreteMarkovChain(T)

first_passage_probabilities(X, 2)

# output

2×2 Array{Float64,2}:
 0.27  0.09
 0.21  0.27
```

If `X` has a custom state space,
then `i` and `j` must be in that state space.

```jldoctest first_passage_probabilities
T = [
    0.1 0.9;
    0.3 0.7;
]
X = DiscreteMarkovChain(["Sunny", "Rainy"], T)

first_passage_probabilities(X, 2, "Sunny", "Rainy")

# output

0.09000000000000001
```

Notice how this is the (1, 2) entry in the first example.

# References
1. [University of Windsor](https://scholar.uwindsor.ca/cgi/viewcontent.cgi?article=1125&context=major-papers)
2. [Durham University](http://maths.dur.ac.uk/stats/courses/ProbMC2H/_files/handouts/1516MarkovChains2H.pdf)
"""
function first_passage_probabilities(
    x::AbstractDiscreteMarkovChain, t, i=missing, j=missing
)
    S = state_space(x)
    T = transition_matrix(x)
    n = length(S)

    if n == 0
        return transition_matrix(x)
    end

    js = 1:n  # The columns to loop through
    calc_i_ne_j = true  # Calculate the off-diagonals
    calc_i_eq_j = true  # Calculate the diagonals
    if (i !== missing) && (j !== missing)
        i = state_index(x)[i]
        j = state_index(x)[j]
        js = [j]
        if i == j
            calc_i_ne_j = false
        else
            calc_i_eq_j = false
        end
    end

    Ft = zeros(eltype(T), n, n)  # Empty matrix

    # If i != j
    if calc_i_ne_j
        for j in js

            P0 = copy(T)
            P0[1:n, j] = zeros(eltype(T), n, 1)
            F = P0^(t-1) * T
            Ft[1:n, j] = F[1:n, j]
        end
    end

    # If i == j
    if calc_i_eq_j
        for j in js

            P_ = copy(T)
            P_[j, 1:n] = zeros(eltype(T), 1, n)

            Pnew = zeros(eltype(T), 2*n, 2*n)
            Pnew[1:n, 1:n] = T
            Pnew[(n+1):(2*n), (n+1):(2*n)] = P_
            Pnew[n+j, 1:n] = T[j, 1:n]

            P0 = copy(Pnew)
            P0[1:(2*n), j] = zeros(eltype(T), 2*n, 1)

            F = P0^(t - 1) * Pnew

            Ft[j, j] = F[n+j, j]
        end
    end

    if (i !== missing) && (j !== missing)
        return Ft[i, j]
    end
    return Ft
end


text:
#=
execute
] add OhMyREPL
] add Cryaons
in your REPL, then copy the code below into your startup.jl file at ~\.julia\config\
=#

try
    @eval using OhMyREPL
catch e
    @warn "error while importing OhMyREPL" e
end

import OhMyREPL: Passes.SyntaxHighlighter
using Crayons

penumbra_dark = SyntaxHighlighter.ColorScheme()
SyntaxHighlighter.symbol!(penumbra_dark, crayon"#00A0BE")
SyntaxHighlighter.comment!(penumbra_dark, crayon"#636363")
SyntaxHighlighter.string!(penumbra_dark, crayon"#46A473")
SyntaxHighlighter.call!(penumbra_dark, crayon"#7E87D6")
SyntaxHighlighter.op!(penumbra_dark, crayon"#CB7459")
SyntaxHighlighter.keyword!(penumbra_dark, crayon"#BD72A8")
SyntaxHighlighter.function_def!(penumbra_dark, crayon"#7E87D6")
SyntaxHighlighter.error!(penumbra_dark, crayon"#CB7459")
SyntaxHighlighter.argdef!(penumbra_dark, crayon"#00A0BE")
SyntaxHighlighter.macro!(penumbra_dark, crayon"#7E87D6")
SyntaxHighlighter.number!(penumbra_dark, crayon"#A38F2D")
SyntaxHighlighter.text!(penumbra_dark, crayon"#8F8F8F")
SyntaxHighlighter.add!("Penumbra Dark", penumbra_dark)

colorscheme!("Penumbra Dark")

ext: jl

max_stars_repo_name: nealmckee/penumbra

max_issues_repo_name: nealmckee/penumbra

max_forks_repo_name: nealmckee/penumbra


text:
module FlexibleTypeMod

export FlexibleType, FlexType
using Cxx

typealias FlexType UInt8
typealias Cflexible_type Cxx.CppValue{Cxx.CxxQualType{Cxx.CppBaseType{symbol("graphlab::flexible_type")},(false,false,false)},16}

const INTEGER = FlexType(0)
const FLOAT = FlexType(1)
const STRING = FlexType(2)
const VECTOR = FlexType(3)
const LIST = FlexType(4)
const DICT = FlexType(5)
const DATETIME = FlexType(6)
const UNDEFINED = FlexType(7)
const IMAGE = FlexType(8)

import Cxx: CppValue
import Base: show, get, +, -, /, *, >, >=, ==, <, <=
import SFrames.Util: cstring, jlstring

immutable FlexibleType
    val::Cflexible_type
end

Base.hash(f::FlexibleType) = hash(f.val.data)

function FlexibleType(x::Int)
    FlexibleType(icxx"return flexible_type((long)$x);")
end

function FlexibleType(x::AbstractString)
    FlexibleType(icxx"return flexible_type($(cstring(x)));")
end

function FlexibleType(x::Float64)
    FlexibleType(icxx"return flexible_type((double)$x);")
end

function FlexibleType(x::Associative)
    d = icxx"vector<pair<flexible_type, flexible_type>>();"
    for (k, v) in x
        k_f = FlexibleType(k)
        v_f = FlexibleType(v)
        icxx"$d.push_back(make_pair($(k_f.val), $(v_f.val)));"
    end
    FlexibleType(icxx"return flexible_type($d);")
end

function FlexibleType(x::AbstractVector{Float64})
    # todo directly initiative stl vector from pointer
    v=icxx"vector<double>();"
    for _ in x
        icxx"$v.push_back((double)$_);"
    end
    FlexibleType(icxx"return flexible_type($v);")
end

function FlexibleType(x::AbstractVector)
    v=icxx"vector<flexible_type>();"
    for _ in x
        icxx"$v.push_back($(FlexibleType(_).val));"
    end
    FlexibleType(icxx"return flexible_type($v);")
end

# FlexibleType(::Void) = FlexibleType(icxx"return flexible_type(flex_type_enum::UNDEFINED);")
#
# const FLEX_NULL = FlexibleType(nothing)

function Base.convert(::Type{Int}, f::FlexibleType)
    icxx"auto x=$(f.val).get<flex_int>(); return x;"
end

function Base.convert(::Type{Float64}, f::FlexibleType)
    icxx"auto x=$(f.val).get<flex_float>(); return x;"
end

function Base.convert(::Type{UTF8String}, f::FlexibleType)
    icxx"auto x=$(f.val).get<flex_string>().c_str(); return x;" |> bytestring |> utf8
end

function Base.convert(::Type{Vector{Float64}}, f::FlexibleType)
    vec = icxx"return $(f.val).get<flex_vec>();"
    pointer_to_array(icxx"$vec.data();", icxx"$vec.size();", false)
end

function Base.convert(::Type{Vector{FlexibleType}}, f::FlexibleType)
    v = FlexibleType[]
    list = icxx"return $(f.val).get<flex_list>();"
    for n=1:icxx"$list.size();"
        push!(v, FlexibleType(icxx"auto x=$list[$n-1]; return x;"))
    end
    v
end

function Base.call{K,V}(::Type{Dict{K,V}}, f::FlexibleType)
    d = icxx"$(f.val).get<flex_dict>();"
    b = icxx"$d.begin();"
    e = icxx"$d.end();"
    d_jl = Dict{FlexibleType, FlexibleType}()
    while icxx"$b != $e;"
        val = icxx"*$b;"
        d_jl[FlexibleType(icxx"$val.first;")] = FlexibleType(icxx"$val.second;")
        b = icxx"$b+1;"
    end
    d_jl
end

flex_type(f::FlexibleType) = f.val.data[13]

Base.isnull(f::FlexibleType) = flex_type(f) == UNDEFINED  # Questionable

function Base.get(f::FlexibleType)
    typ = flex_type(f)
    if typ == INTEGER
        Int(f)
    elseif typ == FLOAT
        Float64(f)
    elseif typ == DICT
        Dict(f)
    elseif typ == STRING
        UTF8String(f)
    elseif typ == VECTOR
        convert(Vector{Float64}, f)
    elseif typ == LIST
        convert(Vector{FlexibleType}, f)
    end
end

function show(io::IO, t::FlexibleType)
    print(io, "Flex(")
    show(io, get(t))
    print(io, ")")
end

for (julia_op, c_op, ret_type) in [
    (:+, "+", FlexibleType),
    (:-, "-", FlexibleType),
    (:/, "/", FlexibleType),
    (:*, "*", FlexibleType),
    (:>, ">", Bool),
    (:>=, ">=", Bool),
    (:(==), "==", Bool),
    (:<, "<", Bool),
    (:<=, "<=", Bool)
    ]
    cstr = "\$(s1.val) $c_op \$(s2.val);"
    cstr_scalar = "\$(s1.val) $c_op \$s2;"
    @eval  function $julia_op(s1::FlexibleType, s2::FlexibleType)
        $ret_type($(Expr(:macrocall, Symbol("@icxx_str"), cstr)))
    end

    @eval function $julia_op(s1::FlexibleType, s2::Union{Int,Float64})
        $ret_type($(Expr(:macrocall, Symbol("@icxx_str"), cstr_scalar)))
    end

    @eval $julia_op(s1::Union{Int,Float64}, s2::FlexibleType) = $julia_op(s2, s1)
end

==(f1::FlexibleType, f2::FlexibleType) = f1.val.data == f2.val.data

macro dispatch_numeric(funcs...)
    block = Expr(:block)
    for func in funcs
        e = quote
            function $(esc(func))(f::FlexibleType)
                typ = f.val.data[13]
                if typ == INTEGER
                    $(esc(func))(Int(f)) |> FlexibleType
                elseif typ == FLOAT
                    $(esc(func))(Float64(f)) |> FlexibleType
                else
                    error("Invalid type for operation")
                end
            end
        end
        push!(block.args, e)
    end
    block
end
@dispatch_numeric Base.sin Base.cos Base.tan Base.log Base.exp Base.sqrt Base.mod Base.mod1 Base.expm1 (-)

end


text:
function check_axis_length(ks, inds)
    if length(ks) != length(inds)
        throw(DimensionMismatch(
            "keys and indices must have same length, got length(keys) = $(length(ks))" *
            " and length(indices) = $(length(inds)).")
        )
    end
    return nothing
end

function check_unique_keys(ks)
    if allunique(ks)
        return nothing
    else
        error("All keys must be unique")
    end
end
function check_offsets(ks, inds)
    if firstindex(inds) === firstindex(ks)
        return nothing
    else
        throw(ArgumentError("firstindex of $ks and $inds are not the same."))
    end
end


text:
# To fix all the output reference, run this file with
#
#    DOCUMENTER_FIXTESTS= julia doctests.jl
#
# If the inputs and outputs are giving you trouble, you can run the tests with
#
#    JULIA_DEBUG=DocTestsTests julia doctests.jl
#
# TODO: Combine the makedocs calls and stdout files. Also, allow running them one by one.
#
module DocTestsTests
using Test
using Documenter
using Documenter.Utilities.TextDiff: Diff, Words
import IOCapture

include("src/FooWorking.jl")
include("src/FooBroken.jl")
include("src/NoMeta.jl")

const builds_directory = joinpath(@__DIR__, "builds")
ispath(builds_directory) && rm(builds_directory, recursive=true)
mkpath(builds_directory)

function run_makedocs(f, mdfiles, modules=Module[]; kwargs...)
    dir = mktempdir(builds_directory)
    srcdir = joinpath(dir, "src"); mkpath(srcdir)

    for mdfile in mdfiles
        cp(joinpath(@__DIR__, "src", mdfile), joinpath(srcdir, mdfile))
    end

    c = IOCapture.iocapture(throwerrors=false) do
        makedocs(
            sitename = " ",
            root = dir,
            modules = modules;
            kwargs...
        )
    end

    @debug """run_makedocs($mdfiles, modules=$modules) -> $(c.error ? "fail" : "success")
    ------------------------------------ output ------------------------------------
    $(c.output)
    --------------------------------------------------------------------------------
    """ c.value stacktrace(c.backtrace) dir

    write(joinpath(dir, "output"), c.output)
    write(joinpath(dir, "output.onormalize"), onormalize(c.output))
    open(joinpath(dir, "result"), "w") do io
        show(io, "text/plain", c.value)
        println(io, "-"^80)
        show(io, "text/plain", stacktrace(c.backtrace))
    end

    f(c.value, !c.error, c.backtrace, c.output)
end

function printoutput(result, success, backtrace, output)
    printstyled("="^80, color=:cyan); println()
    println(output)
    printstyled("-"^80, color=:cyan); println()
    println(repr(result))
    printstyled("-"^80, color=:cyan); println()
end

function onormalize(s)
    # Runs a bunch of regexes on captured documenter output strings to remove any machine /
    # platform / environment / time dependent parts, so that it would actually be possible
    # to compare Documenter output to previously generated reference outputs.

    # Remove filesystem paths in doctests failures
    s = replace(s, r"(doctest failure in )(.*)$"m => s"\1{PATH}")
    s = replace(s, r"(@ Documenter.DocTests )(.*)$"m => s"\1{PATH}")

    # Remove stacktraces
    s = replace(s, r"(│\s+Stacktrace:)(\n(│\s+)\[[0-9]+\].*)(\n(│\s+)@.*)?+" => s"\1\\n\3{STACKTRACE}")

    return s
end

function is_same_as_file(output, filename)
    # Compares output to the contents of a reference file. Runs onormalize on both strings
    # before doing a character-by-character comparison.
    fixtests = haskey(ENV, "DOCUMENTER_FIXTESTS")
    success = if isfile(filename)
        reference = read(filename, String)
        if onormalize(reference) != onormalize(output)
            diff = Diff{Words}(onormalize(reference), onormalize(output))
            @error """Output does not agree with reference file
            ref: $(filename)
            ------------------------------------ output ------------------------------------
            $(output)
            ---------------------------------- reference  ----------------------------------
            $(reference)
            ------------------------------ onormalize(output) ------------------------------
            $(onormalize(output))
            ---------------------------- onormalize(reference)  ----------------------------
            $(onormalize(reference))
            """ diff
            false
        else
            true
        end
    else
        fixtests || error("Missing reference file: $(filename)")
        false
    end
    if fixtests && !success
        @info "Updating $(filename)"
        write(filename, output)
        success = true
    end
    return success
end

rfile(filename) = joinpath(@__DIR__, "stdouts", filename)

@testset "doctesting" begin
    # So, we have 4 doctests: 2 in a docstring, 2 in an .md file. One of either pair is
    # OK, other is broken. Here we first test all possible combinations of these doctest
    # with strict = true to make sure that the doctests are indeed failing.
    #
    # Some tests are broken due to https://github.com/JuliaDocs/Documenter.jl/issues/974
    run_makedocs(["working.md"]; strict=true) do result, success, backtrace, output
        @test success
        @test is_same_as_file(output, rfile("1.stdout"))
    end

    run_makedocs(["broken.md"]; strict=true) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("2.stdout"))
    end

    run_makedocs(["working.md", "fooworking.md"]; modules=[FooWorking], strict=true) do result, success, backtrace, output
        @test success
        @test is_same_as_file(output, rfile("3.stdout"))
    end

    run_makedocs(["working.md", "foobroken.md"]; modules=[FooBroken], strict=true) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("4.stdout"))
    end

    run_makedocs(["broken.md", "fooworking.md"]; modules=[FooWorking], strict=true) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("5.stdout"))
    end

    run_makedocs(["broken.md", "foobroken.md"]; modules=[FooBroken], strict=true) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("6.stdout"))
    end

    run_makedocs(["fooworking.md"]; modules=[FooWorking], strict=true) do result, success, backtrace, output
        @test success
        @test is_same_as_file(output, rfile("7.stdout"))
    end

    run_makedocs(["foobroken.md"]; modules=[FooBroken], strict=true) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("8.stdout"))
    end

    # Here we try the default (strict = false) -- output should say that doctest failed, but
    # success should still be true.
    run_makedocs(["working.md"]) do result, success, backtrace, output
        @test success
        @test is_same_as_file(output, rfile("11.stdout"))
    end

    run_makedocs(["broken.md"]) do result, success, backtrace, output
        @test success
        @test is_same_as_file(output, rfile("12.stdout"))
    end

    # Tests for doctest = :only. The outout should reflect that the docs themselves do not
    # get built.
    run_makedocs(["working.md"]; modules=[FooWorking], doctest = :only) do result, success, backtrace, output
        @test success
        @test is_same_as_file(output, rfile("21.stdout"))
    end

    run_makedocs(["working.md"]; modules=[FooBroken], doctest = :only) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("22.stdout"))
    end

    run_makedocs(["broken.md"]; modules=[FooWorking], doctest = :only) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("23.stdout"))
    end

    run_makedocs(["broken.md"]; modules=[FooBroken], doctest = :only) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("24.stdout"))
    end
    # strict gets ignored with doctest = :only
    run_makedocs(["broken.md"]; modules=[FooBroken], doctest = :only, strict=false) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("25.stdout"))
    end

    # DocTestSetup in modules
    run_makedocs([]; modules=[NoMeta], doctest = :only) do result, success, backtrace, output
        @test !success
        @test is_same_as_file(output, rfile("31.stdout"))
    end
    # Now, let's use Documenter's APIs to add the necessary meta information
    DocMeta.setdocmeta!(NoMeta, :DocTestSetup, :(baz(x) = 2x))
    run_makedocs([]; modules=[NoMeta], doctest = :only) do result, success, backtrace, output
        @test success
        @test is_same_as_file(output, rfile("32.stdout"))
    end

    if VERSION >= v"1.5.0-DEV.178"
        # Julia 1.5 REPL softscope,
        # see https://github.com/JuliaLang/julia/pull/33864
        run_makedocs(["softscope.md"]) do result, success, backtrace, output
            @test success
            @test is_same_as_file(output, rfile("41.stdout"))
        end
    else
        # Old REPL scoping behaviour on older Julia version
        run_makedocs(["hardscope.md"]) do result, success, backtrace, output
            @test success
            @test is_same_as_file(output, rfile("42.stdout"))
        end
    end
end

using Documenter.DocTests: remove_common_backtrace
@testset "DocTest.remove_common_backtrace" begin
    @test remove_common_backtrace([], []) == []
    @test remove_common_backtrace([1], []) == [1]
    @test remove_common_backtrace([1,2], []) == [1,2]
    @test remove_common_backtrace([1,2,3], [1]) == [1,2,3]
    @test remove_common_backtrace([1,2,3], [2]) == [1,2,3]
    @test remove_common_backtrace([1,2,3], [3]) == [1,2]
    @test remove_common_backtrace([1,2,3], [2,3]) == [1]
    @test remove_common_backtrace([1,2,3], [1,3]) == [1,2]
    @test remove_common_backtrace([1,2,3], [1,2,3]) == []
    @test remove_common_backtrace([1,2,3], [0,1,2,3]) == []
end

end # module


text:
module Bifurcation

using PyPlot

include("model/model.jl")
using .Model
if !isfile("./differential_equation.jl")
    run(`sh create_diffeq.sh`)
end
include("./differential_equation.jl")
include("../../continuation.jl")

function analysis()
    global fp, br

    # BP: name(index) of bifurcation parameter
    global BP = C.CycElevel

    fp = []
    ev = []
    br = []

    for i in 1:6
        global p = param_values()

        if i == 1
            p[C.Emi1T] = 0.0
        elseif i == 2
            p[C.Emi1T] = 0.5
        elseif i == 3
            p[C.Emi1T] = 0.75
        elseif i == 4
            p[C.Emi1T] = 1.0
        elseif i == 5
            p[C.Emi1T] = 1.25
        elseif i == 6
            p[C.Emi1T] = 2.0
        end

        new_curve!(p, direction=false)
        push!(fp,readdlm("./Data/fp.dat",'\t',Float64,'\n'))
        push!(ev,readdlm("./Data/ev.dat",'\t',Float64,'\n'))
        push!(br,get_bistable_regime(ev[i]))
    end
end

function diagram()
    rc("figure",figsize = (9,6))
    rc("font",family = "Arial")
    rc("font",size = 20)
    rc("axes",linewidth = 1.2)
    rc("xtick.major",width = 1.2)
    rc("ytick.major",width = 1.2)
    rc("lines",linewidth = 2)

    for (i,(fixed_point,unstable_ss)) in enumerate(zip(fp,br))
        if i == 1
            color = "red"
        else
            color = "silver"
        end
        plot(
            fixed_point[1:unstable_ss[1]-1,VN+1],
            fixed_point[1:unstable_ss[1]-1,V.p27T+1],
            "-",color=color
        )
        plot(
            fixed_point[unstable_ss,VN+1],
            fixed_point[unstable_ss,V.p27T+1],
            "--",color=color
        )
        plot(
            fixed_point[unstable_ss[end]+1:end,VN+1],
            fixed_point[unstable_ss[end]+1:end,V.p27T+1],
            "-",color=color
        )
    end
    xlabel("CycE level")
    ylabel("p27 level")

    xlim(0.0,1.0)
    xticks([0,0.5,1])
    ylim(0.0,2.05)
    yticks([0,1,2])

    show()
end
end  # module

max_stars_repo_name: himoto/Bifurcation

max_issues_repo_name: himoto/Bifurcation

max_forks_repo_name: himoto/Bifurcation


text:
function expconv(A::AbstractVector, B::Number, t::AbstractVector)
    # Returns f = A ConvolvedWith exp(-B t)
    # Based on Flouri et al. (2016) MRM 76(3), doi: 10.1002/mrm.25991
    @assert length(A) == length(t)
    f = zeros(length(t))
    for i = 2:length(t)
        x = B * (t[i] - t[i-1])
        dA = (A[i] - A[i-1]) / x
        E = exp(-x)
        E0 = 1 - E
        E1 = x - E0
        f[i] = E * f[i-1] + A[i-1] * E0 + dA * E1
    end
    return f ./ B
end

macro extract(varnames, namedtuple)
    ex = Expr(:block)
    ex.args = [
        :($(esc(var)) = getindex($(esc(namedtuple)), $(esc(QuoteNode(var)))))
        for var in varnames.args
    ]
    ex
end

function interquartile_mean(x::AbstractVector)
    if std(x) > 1e-3
        quartiles = quantile(x, [0.25, 0.75])
        # `<=` is safer than `<` because latter can creat empty array if x is short
        interquartile_x = @. x[quartiles[1]<=x<=quartiles[2]]
    else
        interquartile_x = x
    end
    return mean(interquartile_x)
end

function percent_error(estimate::Number, truth::Number)
    return 100 * (estimate - truth) / truth
end

function positive_only_mask(x::NamedTuple)
    valid_keys = keys(x)
    mask = trues(size(x[valid_keys[1]]))
    for array in x
        @. mask = mask & (array > 0)
    end
    return mask
end

function resolve_fitting_inputs(; t, ca, ct, mask)
    @assert length(t) == length(ca) == size(ct)[end]
    num_timepoints = length(t)
    if typeof(ct) <: AbstractVector
        @assert length(ct) == num_timepoints
        ct = reshape(ct, 1, num_timepoints)
    end
    volume_size = size(ct)[1:end-1]
    resolved_mask = resolve_mask_size(mask, volume_size)
    return (t, ct, resolved_mask, num_timepoints, volume_size)
end

function resolve_relaxation_inputs(; signal, angles, mask)
    @assert length(angles) == size(signal)[end]
    num_angles = length(angles)
    if typeof(signal) <: AbstractVector
        @assert length(signal) == num_angles
        signal = reshape(signal, 1, num_angles)
    end
    volume_size = size(signal)[1:end-1]
    resolved_mask = resolve_mask_size(mask, volume_size)
    return (signal, angles, resolved_mask, num_angles, volume_size)
end

function resolve_mask_size(mask, desired_size)
    if size(mask) == desired_size
        return mask .> 0
    elseif size(mask) == ()
        return repeat([mask .> 0], desired_size...)
    else
        error("Mask size: $(size(mask)) does not match input size $(desired_size)")
    end
end

function unify_size(element, desired_size)
    if size(element) == desired_size
        return element
    elseif size(element) == ()
        return repeat([element], desired_size...)
    else
        error("Element size: $(size(element)) does not match input size $(desired_size)")
    end
end

function apply_mask(; data, mask)
    @assert length(size(data)) >= length(size(mask))
    mask_indices = findall(mask)
    return data[mask_indices, :]
end

function crop(data::AbstractArray; mask = nothing)
    if isnothing(mask)
        mask = @. !isnan(data) & (data > 0)
    end
    xlim = findall(vec(sum(mask, dims = [2, 3])) .> 0)
    ylim = findall(vec(sum(mask, dims = [1, 3])) .> 0)
    return data[xlim, ylim, :]
end

function crop(data::NamedTuple; mask = nothing)
    for key in keys(data)
        data[key] = crop(data[key]; mask = mask)
    end
    return data
end


text:
using SpecialFunctions, Nemo, StatsFuns, Memoization
import Nemo.binom, Nemo.gamma

"Session-wide precision"
const precision = 5000 ## Increase for better precision

"Converter from real to arbitrary precision real (Arb)"
const RR = RealField(precision)
const CC = ComplexField(precision)

## common functions
gamma(x::Int64) = Nemo.gamma(RR(x))

#  Factorial function
fac(x::Int64) = Nemo.fac(RR(x))

@memoize function unsigned_Stirling2(n::Integer,k::Integer)
  # special cases
  if k<0 || n<0
    throw(DomainError())
  end
  if k>n
    return big(0)
  end
  if n==0  # and, by logic, k==0
    return big(1)
  end
  if k==0  # and, by logic, n>0
    return big(0)
  end
  # end of special cases, invoke recursion
  return  (k)*unsigned_Stirling2(n-1,k) + unsigned_Stirling2(n-1,k-1)
end

import Nemo.risingfac

function risingfac(r, n)
  if r == 0
    return 0
  else
    return prod(r + i for i in 0:(n-1))
  end
end
function risingfac(r::arb, n)
  if r == 0
    return arb_0
  else
    return prod(r + i for i in 0:(n-1))
  end
end

##functions to compute M function for Fergusson&Klass approximation
using Distributions, Roots

Γ(x) = SpecialFunctions.gamma(x)
using RCall
R"library(expint)"

function gamma_inc_r(a::Float64, x::Float64)
    res::Float64 = R"expint::gammainc($a,$x)"
    return res
end
# Γ(s,x) = SpecialFunctions.gamma_inc_r(s, x, 0)[2] * Γ(s)
Γ(s,x) = gamma_inc_r(s, x)

function log_βnk(β, n, k, σ)
    return log(β) + log(n) - 1/σ*log(k)
end
βnk(β, n, k, σ) = exp(log_βnk(β, n, k, σ))

function logxk(n, k, β, σ)
    if n==1
        return log(k*σ ) + log(GibbsTypePriors.Cnk(n, k+1, σ)) - log(σ) - log(GibbsTypePriors.Cnk(n, k, σ))
    else
       return log(k*σ + βnk(β, n-1, k, σ)) + log(GibbsTypePriors.Cnk(n, k+1, σ)) - log(σ) - log(GibbsTypePriors.Cnk(n, k, σ))
    end
end

function logxk_py(n, k, β, σ)
       return log(k*σ + β) + log(GibbsTypePriors.Cnk(n, k+1, σ)) - log(σ) - log(GibbsTypePriors.Cnk(n, k, σ))
end

function Pkn_NGG_approx_full(n, β, σ, f)
        Axnk = Array{arb}(undef, n-1)
        Axnk[1:n-1] = f.(n, 1:n-1, β, σ)
        Sum_xn= exp(f(n, 1, β, σ))
        for i in (3:n)
             Sum_xn= Sum_xn + exp(sum(Axnk[1:i-1]))
        end
        P1n = Array{arb}(undef, n)
        P1n[1] = exp(- log(1 +Sum_xn))
        for k in 2:(n)
              P1n[k] = exp(log(P1n[k-1]) + Axnk[k-1])
        end
        return P1n
end

Pkn_NGG_pred_approx(n, β, σ) = convert(Array{Float64,1}, Pkn_NGG_approx_full(n,  β, σ, logxk))

function Pkn_NGGM_arb_precomputed(k::N, n::N,H::N,  β::T, σ::T, Pk_NGG::A) where {T<:Number, N<:Integer,A<:Vector}
    H_arb = RR(H)
    return (fac(H) // (H_arb^k * fac(H-k))) * sum([ (1//(H_arb^i))*unsigned_Stirling2(i+k,k)* Pk_NGG[k+i] for i in 0:(n-k)])
end

Pkn_NGGM_precomp(k,n, H, β, σ, Pk_NGG) = convert(Float64, Pkn_NGGM_arb_precomputed(k, n, H, β, σ, Pk_NGG))

## functions to smooth the plot

function quantilepk(pk)
  n = length(pk)
  cdfk = cumsum(pk)
  quantile_grid = (1:n)/(n+1)
  return [argmin(cdfk .< q) for q in quantile_grid]
end

function gamma_qq(pk, α, β)
  n = length(pk)
  quantile_grid = (1:n)/(n+1)
  return sum((quantile.(Gamma(α, 1/β), quantile_grid) - quantilepk(pk)).^2)
end

 function smooth_pk(Pkn)
  lower = [1., 0.0]
  upper = [10000, 10000]
  initial_x = [2.0, 2.0]
  objfun_pkn(x) = gamma_qq(Pkn, x[1], x[2])
  res = optimize(objfun_pkn, lower, upper, initial_x)
  α_, β_ = res.minimizer
  return Pkn__smooth = pdf.(Gamma(α_, 1/β_), eachindex(Pkn))
end

###

max_stars_repo_name: konkam/approximatingBNPpriors

max_issues_repo_name: konkam/approximatingBNPpriors

max_forks_repo_name: konkam/approximatingBNPpriors


text:
using ConservationLawsParticles
using SpecialFunctions, RecursiveArrayTools, DifferentialEquations, Plots

using Random
Random.seed!(42)

# model
V(t, x) = -x^3
W(t, x) = 0.0 # x^2/2 - abs(x)
W′(t, x) = 0.0 # x - sign(x)
mob(ρ) = 1.0 # max((ρ-1)^2 * (2ρ+1), 0)
smodel = DiffusiveSampledModel((V,), ((W′,),), (mob,), (Diffusion(1),))
imodel = DiffusiveIntegratedModel((V,), ((W,),), (mob,), (Diffusion(1),))

# initial condition
n = 200
x0 = ArrayPartition(vcat(range(-1, 1; length=n+1)))
x0 = ArrayPartition(sort(randn(n+1)))

refsol(t, x) = sqrt(2)/gamma(1/4) * exp(-x^4/4)

# time span
tspan = (0., 2.0)

# ODE system for the particles
sprob = ODEProblem(velocities_diff!, x0, tspan, smodel)
# iprob = ODEProblem(velocities_diff!, x0, tspan, imodel)

abstol = reltol = 2e-8

@time ssol = solve(sprob, BS5(); abstol=abstol, reltol=reltol);

# plot the particle trajectories
plot(legend=false, title="Trajectories", xlabel="time", ylabel="position")
plot!(ssol, vars=eachindex(x0)[1:5:end]; color=:blue)
savefig("diffusion4.png")

# plot an animation of the density
anim = @animate for t in range(sqrt.(tspan)...; length=101).^2
    plot(title="Density", xlabel="position", ylabel="density",
        xlims=(-3,3), ylims=(0,1/sqrt(pi)))
    plot!(x -> refsol(t, x); color=:blue, label="asymptotic")
    densityplot!(ssol(t); color=:red, label="particles")
end
gif(anim, "diffusion4.gif")


text:
function gen_carrier!(
    carrier::StructArray{<:Complex{T}},
    frequency,
    sampling_frequency,
    start_phase,
    amplitude::T
) where T
    c_re = carrier.re; c_im = carrier.im
    phase_step = upreferred(frequency / sampling_frequency)
    @avx for i in 1:length(carrier)
        c_im_temp, c_re_temp =
            sincos(T(2π) * ((i - 1) * T(phase_step) + T(start_phase)))
        c_im[i] = c_im_temp * amplitude
        c_re[i] = c_re_temp * amplitude
    end
    carrier
end


text:
# Adapted from https://discourse.julialang.org/t/expression-parser/41880/7
# code by Alan R. Rogers, Professor of Anthropology, University of Utah

import Nemo: fmpq_mpoly

function myeval(e::Union{Expr,Symbol,Number}, map::Dict{Symbol,fmpq_mpoly})
    try
        return _myeval(e, map)
    catch ex
        println("Can't parse \"$e\"")
        rethrow(ex)
    end
end

function _myeval(s::Symbol, map::Dict{Symbol,fmpq_mpoly})
    if haskey(map, s)
        return map[s]
    else
        throw(UndefVarError(s))
    end
end

# Numbers are converted to fmpq.
function _myeval(x::Number, map::Dict{Symbol,fmpq_mpoly})
    return fmpq(x)
end

# To parse an expression, convert the head to a singleton
# type, so that Julia can dispatch on that type.
function _myeval(e::Expr, map::Dict{Symbol,fmpq_mpoly})
    return _myeval(Val(e.head), e.args, map)
end

# Call the function named in args[1]
function _myeval(::Val{:call}, args, map::Dict{Symbol,fmpq_mpoly})
    return _myeval(Val(args[1]), args[2:end], map)
end

# Addition
function _myeval(::Val{:+}, args, map::Dict{Symbol,fmpq_mpoly})
    x = 0
    for arg ∈ args
        x += _myeval(arg, map)
    end
    return x
end

# Subtraction and negation
function _myeval(::Val{:-}, args, map::Dict{Symbol,fmpq_mpoly})
    len = length(args)
    if len == 1
        return -_myeval(args[1], map)
    else
        return _myeval(args[1], map) - _myeval(args[2], map)
    end
end

# Multiplication
function _myeval(::Val{:*}, args, map::Dict{Symbol,fmpq_mpoly})
    x = 1
    for arg ∈ args
        x *= _myeval(arg, map)
    end
    return x
end

# Division
function _myeval(::Val{:/}, args, map::Dict{Symbol,fmpq_mpoly})
    return _myeval(args[1], map) / _myeval(args[2], map)
end


text:
function makefigure2(dist::DataFrame;
  Fret::Symbol = DIST_RET)

  local sdist::SubDataFrame

  generateagg(df::AbstractDataFrame) = aggregatebyday(x->mean(x)*100, df, selectcols=[:day, Fret])

  ydomain::Vector{Float64} = [-0.2,0.2]
  yaxistext::String = "Mean (daily cross- in %)"
  figurename="figure2"

  sdist = view(dist, (r->r.primary && (!ismissing(r[Fret]))).(eachrow(dist)), :)

  makedividendeventplots(generateagg, sdist,
    Fret=Fret,
    ydomain=ydomain,
    yaxistext=yaxistext,
    figurename=figurename
    )
end

max_stars_repo_name: clintonTE/CCA

max_issues_repo_name: clintonTE/CCA

max_forks_repo_name: clintonTE/CCA


text:
mutable struct ViewerWindow
    context::ZMQ.Context
    socket::ZMQ.Socket
    web_url::String
    zmq_url::String
    bridge::Union{ZMQWebSocketBridge, Void}

    function ViewerWindow(bridge::ZMQWebSocketBridge)
        context = ZMQ.Context()
        socket = ZMQ.Socket(context, ZMQ.REQ)
        ZMQ.connect(socket, zmq_url(bridge))
        new(context, socket, web_url(bridge), zmq_url(bridge), bridge)
    end

    function ViewerWindow(zmq_url::AbstractString)
        context = ZMQ.Context()
        socket = ZMQ.Socket(context, ZMQ.REQ)
        ZMQ.connect(socket, zmq_url)
        web_url = request_zmq_url(socket)
        # Connect again, to work around weird bug in the Python version of the
        # server. See https://github.com/rdeits/meshcat-python/pull/2
        socket = ZMQ.Socket(context, ZMQ.REQ)
        ZMQ.connect(socket, zmq_url)
        new(context, socket, web_url, zmq_url, nothing)
    end

    function ViewerWindow()
        bridge = ZMQWebSocketBridge()
        @async run(bridge)
        ViewerWindow(bridge)
    end
end

function request_zmq_url(socket::ZMQ.Socket)
    ZMQ.send(socket, "url")
    zmq_url = unsafe_string(ZMQ.recv(socket))
end

url(c::ViewerWindow) = c.web_url
Base.open(c::ViewerWindow) = open_url(url(c))
function Base.close(c::ViewerWindow)
    close(c.socket)
    close(c.context)
end

function Base.wait(c::ViewerWindow)
    ZMQ.send(c.socket, "wait")
    ZMQ.recv(c.socket)
    nothing
end

function Base.send(c::ViewerWindow, cmd::AbstractCommand)
    data = lower(cmd)
    ZMQ.send(c.socket, data["type"], true)
    ZMQ.send(c.socket, data["path"], true)
    ZMQ.send(c.socket, pack(data), false)
    ZMQ.recv(c.socket)
    nothing
end

function open_url(url)
    try
        @static if is_windows()
            run(`start $url`)
        elseif is_apple()
            run(`open $url`)
        elseif is_linux()
            run(`xdg-open $url`)
        end
    catch e
        println("Could not open browser automatically: $e")
        println("Please open the following URL in your browser:")
        println(url)
    end
end

"""
    vis = Visualizer()

Construct a new MeshCat visualizer instance. This also starts the MeshCat ZeroMQ
and file servers, choosing an appropriate port automatically.

Useful methods:

    open(vis) # open the visualizer in your browser
    vis[:group1] # get a new visualizer representing a sub-tree of the scene
    setobject!(vis[:group1], geometry) # set the object shown by this sub-tree of the visualizer

    vis = Visualizer(zmq_url::String)

Connect to an existing MeshCat server at the given ZeroMQ URL.
"""
struct Visualizer
    window::ViewerWindow
    path::Path
end

Visualizer() = Visualizer(ViewerWindow(), ["meshcat"])
Visualizer(zmq_url::AbstractString) = Visualizer(ViewerWindow(zmq_url), ["meshcat"])

"""
$(SIGNATURES)

Get the URL at which the MeshCat file server is running.
Open this URL in your browser to see the 3D scene.
"""
url(v::Visualizer) = url(v.window)

"""
Open the visualizer's web URL in your default browser
"""
Base.open(v::Visualizer) = (open(v.window); v)
Base.close(v::Visualizer) = close(v.window)
Base.show(io::IO, v::Visualizer) = print(io, "MeshCat Visualizer at $(url(v)) with path $(v.path)")

"""
$(SIGNATURES)

Wait until at least one browser has connected to the
visualizer's server. This is useful in scripts to delay
execution until the browser window has opened.
"""
Base.wait(v::Visualizer) = wait(v.window)

"""
$(SIGNATURES)

Set the object at this visualizer's path. This replaces whatever
geometry was presently at that path. To draw multiple geometries,
place them at different paths by using the slicing notation:

    setobject!(vis[:group1][:box1], geometry1)
    setobject!(vis[:group1][:box2], geometry2)
"""
function setobject!(vis::Visualizer, obj::AbstractObject)
    send(vis.window, SetObject(obj, vis.path))
    vis
end

setobject!(vis::Visualizer, geom::GeometryLike) = setobject!(vis, Mesh(geom))
setobject!(vis::Visualizer, cloud::PointCloud) = setobject!(vis, Points(cloud))
setobject!(vis::Visualizer, geom::GeometryLike, material::AbstractMaterial) = setobject!(vis, Mesh(geom, material))

"""
$(SIGNATURES)

Set the transform of this visualizer's path. This can be done
before or after adding an object at that path. The overall transform
of an object is the composition of the transforms of all of its parents,
so setting the transform of `vis[:group1]` affects the poses of the objects
at `vis[:group1][:box1]` and `vis[:group1][:box2]`.
"""
function settransform!(vis::Visualizer, tform::Transformation)
    send(vis.window, SetTransform(tform, vis.path))
    vis
end

"""
$(SIGNATURES)

Delete the geometry at this visualizer's path and all of its descendants.
"""
function delete!(vis::Visualizer)
    send(vis.window, Delete(vis.path))
    vis
end

Base.getindex(vis::Visualizer, path::Union{Symbol, AbstractString}...) = Visualizer(vis.window, vcat(vis.path, path...))


text:
Pkg.add("MetadataTools")
# Pkg.test("MetadataTools")
Pkg.add("MetaGraphs")
Pkg.add("GraphPlot")
using MetadataTools
using LightGraphs
using MetaGraphs
using GraphPlot

pkgmeta = get_all_pkg()
pg = make_dep_graph(pkgmeta)
pkg_graph = get_pkg_dep_graph("LightGraphs", pg)
pg
pg.pkgnames
pg.adjlist

g = DiGraph(length(pg.adjlist))
mg = MetaDiGraph(g)
for i in 1:length(pg.adjlist)
    for j in pg.adjlist[i]
        add_edge!(mg, j, i)
    end
end
for v in 1:nv(mg)
    val = pg.pkgnames[v]
    set_prop!(mg, v, :name, val)
end

lgv = find(x->x=="LightGraphs", pg.pkgnames)[1]
t = bfs_tree(mg, lgv)
tvertices = [v for v in vertices(t) if degree(t, v) > 0]
subg = induced_subgraph(mg, tvertices)[1]
tnames = pg.pkgnames[tvertices]
tnamesmeta = [prop[:name] for (v, prop) in subg.vprops]
sort(tnamesmeta) == sort(tnames)

fname = "graph.plot.svg"
gplot_kwargs = Dict(:nodelabel=>tnames,
                    # :nodesize=> Float64[sqrt(degree(subg, v))/10 for v in vertices(subg)])
                    :nodesize=>100)
draw(SVG(fname, 8inch, 8inch), gplot(subg; gplot_kwargs...))


text:
@info "Starting precompile exec."

using CairoMakie
CairoMakie.activate!(type = "png")

display(scatter(rand(100), rand(100)))

@info "Precompile exec finished."

max_stars_repo_name: greimel/julia-binder

max_issues_repo_name: greimel/julia-binder

max_forks_repo_name: greimel/julia-binder


text:
using Dash

app = dash()
app.layout = html_div(id="before-reload-content") do
    html_h3("Hot restart"),
    dcc_input(id="input", value="initial"),
    html_div(id="output")
end

callback!(app, Output("output","children"), Input("input","value")) do value
    return "before reload $value"

end

run_server(app,
        dev_tools_hot_reload=true,
        dev_tools_hot_reload_interval=0.1,
        dev_tools_hot_reload_watch_interval=0.1,
        dev_tools_hot_reload_max_retry=100
)


text:
m = HealpixMap{Float64,RingOrder}(model.nside)

using BenchmarkTools
using HDF5
using JLD2

@btime Healpix.saveToFITS(m, "!test.fits", typechar="E")

##

function testHDF5()
    h5open("test.h5", "w") do file
        write(file, "A", m.pixels)  # alternatively, say "@write file A"
    end
end

##
@btime testHDF5()

##
function testJLD2()
    A = m.pixels
    @save "test.jld2" A
end

##
@time testJLD2()
##
@time testHDF5()
##
@time Healpix.saveToFITS(m, "!test.fits", typechar="E")
##
@time Healpix.saveToFITS(m, "!test.fits", typechar="D")
##

max_stars_repo_name: xzackli/XGPaint

max_issues_repo_name: xzackli/XGPaint

max_forks_repo_name: xzackli/XGPaint


text:
using BlockArrays, ArrayLayouts, LinearAlgebra, Test
import BlockArrays: BlockLayout
import ArrayLayouts: DenseRowMajor, ColumnMajor, StridedLayout

@testset "Linear Algebra" begin
    @testset "BlockArray scalar * matrix" begin
        A = BlockArray{Float64}(randn(6,6), fill(2,3), 1:3)
        @test 2A == A*2 == 2Matrix(A)
        @test blockisequal(axes(2A),axes(A))
    end

    @testset "BlockArray matrix * vector" begin
        A = BlockArray{Float64}(randn(6,6), fill(2,3), 1:3)
        b = randn(6)
        @test MemoryLayout(A) isa BlockLayout{DenseColumnMajor,DenseColumnMajor}
        V = view(A,Block(2,3))
        @test MemoryLayout(V) isa DenseColumnMajor
        @test strides(V) == (1,2)

        @test V*view(b,4:6) ≈ V*b[4:6] ≈ Matrix(V) * b[4:6]
        @test all(muladd!(1.0,V,view(b,4:6),0.0,similar(b,2)) .=== BLAS.gemv!('N', 1.0, Matrix(V), b[4:6], 0.0, similar(b,2)))

        @test A*b isa PseudoBlockVector
        @test A*BlockVector(b,1:3) isa BlockVector
        @test blockisequal(axes(A*b,1), axes(A,1))
        @test A*b ≈ Matrix(A)*b ≈ A*BlockVector(b,1:3)
        @test all(A*b .=== A*PseudoBlockVector(b,1:3))

        V = view(A, Block.(1:2), Block(3))
        @test MemoryLayout(V) isa BlockLayout{DenseColumnMajor,DenseColumnMajor}
        @test all(V*view(b,4:6) .=== V*b[4:6])
        @test V*b[4:6] ≈ Matrix(V)*b[4:6]

        V = view(A, Block(3), Block.(1:2))
        @test MemoryLayout(V) isa BlockLayout{StridedLayout,DenseColumnMajor}
        @test all(V*view(b,4:6) .=== V*b[4:6])
        @test V*b[4:6] ≈ Matrix(V)*b[4:6]

        V = view(A, Block.(2:3), Block.(1:2))
        @test MemoryLayout(V) isa BlockLayout{ColumnMajor,DenseColumnMajor}
        @test all(V*view(b,4:6) .=== V*b[4:6])
        @test V*b[4:6] ≈ Matrix(V)*b[4:6]

        # checks incompatible blocks
        @test A^2 ≈ A*A ≈ Matrix(A)^2
    end

    @testset "PseudoBlockArray matrix * vector" begin
        A = PseudoBlockArray{Float64}(randn(6,6), fill(2,3), 1:3)
        b = randn(6)
        @test MemoryLayout(A) isa DenseColumnMajor
        V = view(A,Block(2,3))
        @test MemoryLayout(V) isa ColumnMajor
        @test strides(V) == (1,6)

        @test V*view(b,4:6) ≈ V*b[4:6] ≈ Matrix(V) * b[4:6]
        @test all(muladd!(1.0,V,view(b,4:6),0.0,similar(b,2)) .=== BLAS.gemv!('N', 1.0, Matrix(V), b[4:6], 0.0, similar(b,2)))

        @test A*b isa PseudoBlockVector
        @test blockisequal(axes(A*b,1), axes(A,1))
        @test A*b ≈ Matrix(A)*b ≈ A*BlockVector(b,1:3)
        @test all(A*b .=== A*PseudoBlockVector(b,1:3))

        V = view(A, Block.(1:2), Block(3))
        @test MemoryLayout(V) isa ColumnMajor
        @test all(V*view(b,4:6) .=== V*b[4:6])
        @test V*b[4:6] ≈ Matrix(V)*b[4:6]

        V = view(A, Block(3), Block.(1:2))
        @test MemoryLayout(V) isa ColumnMajor
        @test all(V*view(b,4:6) .=== V*b[4:6])
        @test V*b[4:6] ≈ Matrix(V)*b[4:6]

        V = view(A, Block.(2:3), Block.(1:2))
        @test MemoryLayout(V) isa ColumnMajor
        @test all(V*view(b,4:6) .=== V*b[4:6])
        @test V*b[4:6] ≈ Matrix(V)*b[4:6]

        # checks incompatible blocks
        @test A^2 ≈ A*A ≈ Matrix(A)^2
    end

    @testset "matrix * matrix" begin
        A = BlockArray(randn(6,6), fill(2,3), 1:3)
        B = BlockArray(randn(6,3), 1:3, 1:2)

        @test A*B isa BlockMatrix
        @test A*B ≈ Matrix(A)*B ≈ A*Matrix(B) ≈ Matrix(A)*Matrix(B) ≈
                PseudoBlockArray(A)*B ≈ A*PseudoBlockArray(B)

        @test all(PseudoBlockArray(A)*PseudoBlockArray(B) .=== PseudoBlockArray(A)*Matrix(B) .===
                    Matrix(A)*PseudoBlockArray(B) .=== Matrix(A)*Matrix(B))
    end

    @testset "adjoint" begin
        A = BlockArray(randn(6,6), fill(2,3), 1:3)
        B = BlockArray(randn(6,6), 1:3, 1:3)
        C = BlockArray(randn(6,6) + im*randn(6,6), fill(2,3), 1:3)
        b = randn(6)
        c = randn(6) .+ im*randn(6)
        @test MemoryLayout(A') isa BlockLayout{DenseRowMajor,DenseRowMajor}
        @test MemoryLayout(C') isa BlockLayout{DenseRowMajor,ConjLayout{DenseRowMajor}}

        @test view(A', Block(2, 3)) == view(A, Block(3,2))'
        @test view(transpose(A), Block(2, 3)) == transpose(view(A, Block(3,2)))
        @test view(C', Block(2, 3)) == view(C, Block(3,2))'
        @test view(transpose(C), Block(2, 3)) == transpose(view(C, Block(3,2)))

        V = view(A', Block(2,3))
        @test MemoryLayout(V) isa DenseRowMajor
        @test strides(V) == (2,1)
        @test V*b[5:6] ≈ A[Block(3,2)]'b[5:6]
        @test V'*b[3:4] ≈ A[Block(3,2)]*b[3:4]
        @test V*c[5:6] ≈ A[Block(3,2)]'c[5:6]
        @test V'*c[3:4] ≈ A[Block(3,2)]*c[3:4]
        @test all(muladd!(1.0,V,b[5:6],0.0,similar(b,2)) .=== BLAS.gemv!('T', 1.0, Matrix(V'), b[5:6], 0.0, similar(b,2)))
        @test all(muladd!(1.0,V',b[3:4],0.0,similar(b,2)) .=== BLAS.gemv!('N', 1.0, Matrix(V'), b[3:4], 0.0, similar(b,2)))
        @test A'*b ≈ Matrix(A)'*b
        @test A'*c ≈ Matrix(A)'*c
        @test all(transpose(A)*b .=== A'b)
        @test all(transpose(A)*c .=== A'c)

        V = view(C', Block(2,3))
        @test MemoryLayout(V) isa ConjLayout{DenseRowMajor}
        @test strides(V) == (2,1)
        @test V*b[5:6] ≈ C[Block(3,2)]'b[5:6]
        @test V'*b[3:4] ≈ C[Block(3,2)]*b[3:4]
        @test V*c[5:6] ≈ C[Block(3,2)]'c[5:6]
        @test V'*c[3:4] ≈ C[Block(3,2)]*c[3:4]
        @test all(muladd!(1.0+0im,V,c[5:6],0.0+0im,similar(c,2)) .=== BLAS.gemv!('C', 1.0+0im, Matrix(V'), c[5:6], 0.0+0im, similar(c,2)))
        @test all(muladd!(1.0,V',c[3:4],0.0+0im,similar(c,2)) .=== BLAS.gemv!('N', 1.0+0im, Matrix(V'), c[3:4], 0.0+0im, similar(c,2)))
        @test A'*b ≈ Matrix(A)'*b
        @test A'*c ≈ Matrix(A)'*c
        @test all(transpose(A)*b .=== A'b)
        @test all(transpose(A)*c .=== A'c)

        @test A'*B ≈ A'*Matrix(B) ≈ Matrix(A)'*B
        @test B'*A' ≈ Matrix(B)'*A' ≈ B'*Matrix(A)' ≈ Matrix(B')*Matrix(A')
        @test A'*A ≈ Matrix(A)'*Matrix(A)
    end

    @testset "triangular" begin
        A = BlockArray(randn(6,6), 1:3, 1:3)
        B = BlockArray(randn(6,6), fill(2,3), 1:3)
        b = randn(6)
        @test MemoryLayout(UpperTriangular(A)) isa TriangularLayout{'U','N',BlockLayout{DenseColumnMajor,DenseColumnMajor}}
        @test UpperTriangular(A) == UpperTriangular(Matrix(A))
        V = view(A, Block(2,2))
        @test MemoryLayout(UpperTriangular(V)) isa TriangularLayout{'U','N',DenseColumnMajor}
        @test mul(UpperTriangular(V),b[2:3]) ≈ UpperTriangular(V)*b[2:3] ≈ UpperTriangular(Matrix(V))*b[2:3]

        @testset "bug in view pointer" begin
            b2 = PseudoBlockArray(b,(axes(A,1),))
            @test Base.unsafe_convert(Ptr{Float64}, b) == Base.unsafe_convert(Ptr{Float64}, b2) ==
                    Base.unsafe_convert(Ptr{Float64}, view(b2,Block(1)))
        end

        @testset "matching blocks" begin
            @test UpperTriangular(A) * b ≈ UpperTriangular(Matrix(A)) * b
            @test UnitUpperTriangular(A) * b ≈ UnitUpperTriangular(Matrix(A)) * b
            @test LowerTriangular(A) * b ≈ LowerTriangular(Matrix(A)) * b
            @test UnitLowerTriangular(A) * b ≈ UnitLowerTriangular(Matrix(A)) * b

            @test UpperTriangular(A) \ b ≈ UpperTriangular(Matrix(A)) \ b
            @test UnitUpperTriangular(A) \ b ≈ UnitUpperTriangular(Matrix(A)) \ b
            @test LowerTriangular(A) \ b ≈ LowerTriangular(Matrix(A)) \ b
            @test UnitLowerTriangular(A) \ b ≈ UnitLowerTriangular(Matrix(A)) \ b
        end
        @testset "non-matching blocks" begin
            @test UpperTriangular(B) * b ≈ UpperTriangular(Matrix(B)) * b
            @test UnitUpperTriangular(B) * b ≈ UnitUpperTriangular(Matrix(B)) * b
            @test LowerTriangular(B) * b ≈ LowerTriangular(Matrix(B)) * b
            @test UnitLowerTriangular(B) * b ≈ UnitLowerTriangular(Matrix(B)) * b

            @test UpperTriangular(B) \ b ≈ UpperTriangular(Matrix(B)) \ b
            @test UnitUpperTriangular(B) \ b ≈ UnitUpperTriangular(Matrix(B)) \ b
            @test LowerTriangular(B) \ b ≈ LowerTriangular(Matrix(B)) \ b
            @test UnitLowerTriangular(B) \ b ≈ UnitLowerTriangular(Matrix(B)) \ b
        end
    end

    @testset "inv" begin
        A = PseudoBlockArray{Float64}(randn(6,6), fill(2,3), 1:3)
        F = factorize(A)
        # Defaults to QR for generality
        @test F isa LinearAlgebra.QRCompactWY
        B = randn(6,6)
        @test ldiv!(F, copy(B)) ≈ Matrix(A) \ B
        B̃ = PseudoBlockArray(copy(B),1:3,fill(2,3))
        @test ldiv!(F, B̃) ≈ A\B ≈ Matrix(A) \ B

        @test inv(A) isa PseudoBlockArray
        @test inv(A) ≈ inv(Matrix(A))
        @test inv(A)*A ≈ Matrix(I,6,6)

        A = BlockArray{Float64}(randn(6,6), fill(2,3), 1:3)
        @test inv(A) isa BlockArray
        @test inv(A)*A ≈ Matrix(I,6,6)
    end

    @testset "Block Diagonal" begin
        D = mortar(Diagonal([randn(2,2),randn(2,2)]))
        @test MemoryLayout(D) isa BlockLayout{DiagonalLayout{DenseColumnMajor},DenseColumnMajor}
    end

    @testset "adjtrans block view strides" begin
        A = BlockArray(randn(6,6), fill(2,3), 1:3)
        @test strides(view(A', Block(1,2)))  == strides(bview(A', Block(1,2))) == (2,1)
    end

    @testset "mul! with adj" begin
        N = 10000
        M = 10
        A = mortar((randn(N, M), randn(N, M), randn(N, M)))

        X,Y = A',A
        Z = zeros(eltype(X), size(X, 1), size(Y, 2))
        mul!(Z, X, Y)
        @test Z ≈ X*Y

        Z = zeros(eltype(X), size(X, 1), size(Y, 2))
        mul!(Z, copy(X), Y)
        @test Z ≈ X*Y
    end

    @testset "5-arg mul! (#174)" begin
        A = BlockArray(rand(4, 5), [1,3], [2,3])
        Ã = PseudoBlockArray(A)
        B = BlockArray(rand(5, 3), [2,3], [1,1,1])
        B̃ = PseudoBlockArray(B)
        C = randn(4,3)
        @test mul!(view(copy(C),:,1:3), A, B, 1, 2) ≈ A*B + 2C
        @test mul!(view(copy(C),:,1:3), A, B̃, 1, 2) ≈ A*B + 2C
        @test mul!(view(copy(C),:,1:3), Ã, B, 1, 2) ≈ A*B + 2C
        @test mul!(view(copy(C),:,1:3), Ã, B̃, 1, 2) ≈ A*B + 2C
    end
end


text:
using DataFrames, CSV

using CSV, DataFrames

function csv2html(fname; header::Bool=false)
    csv = CSV.read(fname)
    @assert(size(csv, 2) > 0)
    str = """

csv2html Example

"""
    tags = header ? ("", "	") : ("", "	")

    for i=1:size(csv, 2)
        str *= "            " * tags[1] * csv[1, i] * tags[2] * "\n"
    end

    str *= " "^8 * "
\n"

    for i=2:size(csv, 1)
        str *= "
\n"

        for j=1:size(csv, 2)
            str *= "            " * "" * csv[i, j] * "	\n"
        end

        str *= "
\n"
    end

    str * "
\n\n\n\n"
end

print(csv2html("input.csv", header=true))

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
using ReinforcementLearningExperiments
using CUDA

CUDA.allowscalar(false)

run(E`JuliaRL_BasicDQN_CartPole`)
# run(E`JuliaRL_BC_CartPole`)
# run(E`JuliaRL_DQN_CartPole`)
# run(E`JuliaRL_PrioritizedDQN_CartPole`)
# run(E`JuliaRL_Rainbow_CartPole`)
# run(E`JuliaRL_QRDQN_CartPole`)
# run(E`JuliaRL_REMDQN_CartPole`)
# run(E`JuliaRL_IQN_CartPole`)
# run(E`JuliaRL_VMPO_CartPole`)
# run(E`JuliaRL_VPG_CartPole`)
# run(E`JuliaRL_BasicDQN_MountainCar`)
# run(E`JuliaRL_DQN_MountainCar`)
# run(E`JuliaRL_A2C_CartPole`)
# run(E`JuliaRL_A2CGAE_CartPole`)
# run(E`JuliaRL_PPO_CartPole`)
# run(E`JuliaRL_MAC_CartPole`)
# run(E`JuliaRL_DDPG_Pendulum`)
# run(E`JuliaRL_SAC_Pendulum`)
# run(E`JuliaRL_TD3_Pendulum`)
# run(E`JuliaRL_PPO_Pendulum`)

# run(E`JuliaRL_BasicDQN_SingleRoomUndirected`)

max_stars_repo_name: jbrea/ReinforcementLearning

max_issues_repo_name: jbrea/ReinforcementLearning

max_forks_repo_name: jbrea/ReinforcementLearning


text:
Types = (Float32, Float64, BigFloat)
f(x, y, z) = 0.5x^2 - 0.1y^3 + z
f_wraparound2(x, y, z) = 5*sin(x) + 2*cos(y) + z
f_wraparound3(x, y, z) = 5*sin(x) + 2*cos(y) - 3*cos(z)
for T in Types
    grid = range(zero(T), stop=2π*one(T), length=40)
    f_uw = f.(grid, grid', reshape(grid, 1, 1, :))
    f_wr = f_uw .% (2π)
    uw_test = unwrap(f_wr)
    difference = first(f_uw) - first(uw_test)
    @test isapprox(uw_test .+ difference, f_uw, atol=1e-8)
    # test in-place version
    unwrap!(f_wr)
    difference = first(f_uw) - first(f_wr)
    @test isapprox(f_wr .+ difference, f_uw, atol=1e-8)

    f_uw = f_wraparound2.(grid, grid', reshape(grid, 1, 1, :))
    f_wr = f_uw .% (2π)
    uw_test = unwrap(f_wr, (true, true, false))
    difference = first(f_uw) - first(uw_test)
    @test isapprox(uw_test .+ difference, f_uw, atol=1e-8)
    # test in-place version
    unwrap!(f_wr, (true, true, false))
    difference = first(f_uw) - first(f_wr)
    @test isapprox(f_wr .+ difference, f_uw, atol=1e-8)

    f_uw = f_wraparound3.(grid, grid', reshape(grid, 1, 1, :))
    f_wr = f_uw .% (2π)
    uw_test = unwrap(f_wr, (true, true, true))
    difference = first(f_uw) - first(uw_test)
    @test isapprox(uw_test .+ difference, f_uw, atol=1e-8)
    # test in-place version
    unwrap!(f_wr, (true, true, true))
    difference = first(f_uw) - first(f_wr)
    @test isapprox(f_wr .+ difference, f_uw, atol=1e-8)
end

max_forks_repo_name: platawiec/Unwrap


text:
using SafeTestsets, Test

@safetestset "Differentiation Test" begin include("diff.jl") end
@safetestset "Overloading Test" begin include("overloads.jl") end


text:
module  TestHeterozygosity

using PopGen
using Test

cats = @nancycats;
testarray = cats.genodata.genotype[1:10]

@testset "Heterozygosity.jl" begin
    @testset "counthet" begin
    @test PopGen.counthet(testarray, 135) == 5
    @test PopGen.counthet(testarray, 143) == 4
    @test PopGen.counthet(testarray, [135,143]) == [5,4]
    end

    @testset "counthom" begin
        @test PopGen.counthom(testarray, 135) == 2
        @test PopGen.counthom(testarray, 143) == 0
        @test PopGen.counthom(testarray, [135,143]) == [2,0]
    end

    @testset "het obs/exp" begin
        @test PopGen._hetero_obs(testarray) == 0.6
        @test PopGen._hetero_exp(testarray) == 0.6015625
    end

    @testset "Nei het" begin
        @test ismissing(PopGen._genediversitynei87(missing,0.5,11))
        @test ismissing(PopGen._genediversitynei87(0.5,missing,11))
        @test ismissing(PopGen._genediversitynei87(missing,missing,11))
        @test PopGen._genediversitynei87(0.5,0.5,11) == 0.525
        @test PopGen._genediversitynei87(0.5,0.5,11, corr = false) == 0.4772727272727273
    end

    @testset "heterozygosity" begin
        tmp = heterozygosity(cats)
        @test tmp isa DataFrames.DataFrame
        @test size(tmp) == (9,4)
        tmp = heterozygosity(cats, by = "sample")
        @test tmp isa DataFrames.DataFrame
        @test size(tmp) == (237,3)
        tmp = heterozygosity(cats, by = "population")
        @test tmp isa DataFrames.DataFrame
        @test size(tmp) == (17,4)
        @test samplehet(cats, "N215") == 1/3
        @test_throws ArgumentError samplehet(cats, "M115")
    end
end

end #module


text:
# Submit coverage to Coveralls.io
export Coveralls
"""
Coverage.Coveralls Module

This module provides functionality to push coverage information to the Coveralls
web service. It exports the `submit` and `submit_token` methods.
"""
module Coveralls
    using Coverage
    using Requests
    using JSON
    using Compat

    export submit, submit_token

    #=
    JSON structure for Coveralls
    Accessed 2015/07/24:
    https://coveralls.zendesk.com/hc/en-us/articles/201774865-API-Introduction
    {
      "service_job_id": "1234567890",
      "service_name": "travis-ci",
      "source_files": [
        {
          "name": "example.rb",
          "source": "def four\n  4\nend",
          "coverage": [null, 1, null]
        },
        {
          "name": "lib/two.rb",
          "source": "def seven\n  eight\n  nine\nend",
          "coverage": [null, 1, 0, null]
        }
      ]
    }
    =#

    # to_json
    # Convert a FileCoverage instance to its Coveralls JSON representation
    to_json(fc::FileCoverage) = Dict("name"     => fc.filename,
                                     "source"   => fc.source,
                                     "coverage" => fc.coverage)

    """
        submit(fcs::Vector{FileCoverage})

    Take a vector of file coverage results (produced by `process_folder`),
    and submits them to Coveralls. Assumes that this code is being run
    on TravisCI. If running locally, use `submit_token`.
    """
    function submit(fcs::Vector{FileCoverage})
        data = Dict("service_job_id"    => ENV["TRAVIS_JOB_ID"],
                    "service_name"      => "travis-ci",
                    "source_files"      => map(to_json, fcs))
        println("Submitting data to Coveralls...")
        req = Requests.post(
                URI("https://coveralls.io/api/v1/jobs"),
                files = [FileParam(JSON.json(data),"application/json","json_file","coverage.json")])
        println("Result of submission:")
        println(Compat.UTF8String(req.data))
    end

    # query_git_info
    # Pulls information about the repository that isn't available if we
    # are running somewhere other than TravisCI
    import Git
    function query_git_info(dir="")
        commit_sha      = Git.readchomp(`rev-parse HEAD`, dir=dir)
        author_name     = Git.readchomp(`log -1 --pretty=format:"%aN"`, dir=dir)
        author_email    = Git.readchomp(`log -1 --pretty=format:"%aE"`, dir=dir)
        committer_name  = Git.readchomp(`log -1 --pretty=format:"%cN"`, dir=dir)
        committer_email = Git.readchomp(`log -1 --pretty=format:"%cE"`, dir=dir)
        message         = Git.readchomp(`log -1 --pretty=format:"%s"`, dir=dir)
        remote          = Git.readchomp(`config --get remote.origin.url`, dir=dir)
        branch          = Git.branch(dir=dir)

        # Normalize remote url to https
        remote = "https" * Git.normalize_url(remote)[4:end]

        return Dict(
            "branch"    => branch,
            "remotes"   => [
                Dict(
                    "name"  => "origin",
                    "url"   => remote
                )
            ],
            "head" => Dict(
                "id" => commit_sha,
                "author_name"       => author_name,
                "author_email"      => author_email,
                "committer_name"    => committer_email,
                "committer_email"   => committer_email,
                "message"           => message
            )
        )
    end

    """
        submit_token(fcs::Vector{FileCoverage}, git_info=query_git_info)

    Take a `Vector` of file coverage results (produced by `process_folder`),
    and submits them to Coveralls. For submissions not from TravisCI.

    git_info can be either a `Dict` or a function that returns a `Dict`.
    """
    function submit_token(fcs::Vector{FileCoverage}, git_info=query_git_info)
        data = Dict("repo_token" => ENV["REPO_TOKEN"],
                    "source_files" => map(to_json, fcs))

        # Attempt to parse git info via git_info, unless the user explicitly disables it by setting git_info to nothing
        try
            if isa(git_info, Function)
                data["git"] = git_info()
            elseif isa(git_info, Dict)
                data["git"] = git_info
            end
        end

        r = post(URI("https://coveralls.io/api/v1/jobs"), files =
            [FileParam(JSON.json(data),"application/json","json_file","coverage.json")])
        println("Result of submission:")
        println(Compat.UTF8String(r.data))
    end
end  # module Coveralls


text:
function cntvalue(c)
    val = 0
    if occursin(c, "AEIOULNRST")
        val = 1
    elseif occursin(c, "DG")
        val = 2
    elseif occursin(c, "BCMP")
        val = 3
    elseif occursin(c, "FHVWY")
        val = 4
    elseif c == "K"
        val = 5
    elseif occursin(c, "JX")
        val = 8
    elseif occursin(c, "QZ")
        val = 10
    end
    return val
end

function score(str)
    tmp = 0
    for c in str
        upc = uppercase(c)
        tmp = tmp + cntvalue(string(upc))
    end
    return tmp
end

max_stars_repo_name: stepinski/julia-in

max_issues_repo_name: stepinski/julia-init

max_forks_repo_name: stepinski/julia-init


text:
"""
The `Information` module of MIToS defines types and functions useful to calculate
information measures (e.g. *Mutual Information* (MI) and *Entropy*) over a Multiple Sequence Alignment (MSA).
This module was designed to count `Residue`s (defined in the `MSA` module)
in special contingency tables (as fast as possible) and to derive probabilities from this counts.
Also, includes methods for applying corrections to that tables, e.g. pseudocounts and pseudo frequencies.
Finally, `Information` allows to use this probabilities and counts to estimate information measures and other frequency based values.

**Features**

- Estimate multi dimensional frequencies and probabilities tables from sequences, MSAs, etc...
- Correction for small number of observations
- Correction for data redundancy on a MSA
- Estimate information measures
- Calculate corrected mutual information between residues

```julia

using MIToS.Information
```
"""
module Information

using MIToS.Utils
using MIToS.MSA
using PairwiseListMatrices

export BLOSUM62_Pi, BLOSUM62_Pij,

SequenceWeights, Pseudocount, AdditiveSmoothing,
ResidueContingencyTables, ResidueCount, ResidueProbability,
nresidues, update!, apply_pseudocount!, count!, normalize!,
blosum_pseudofrequencies!, apply_pseudofrequencies!, probabilities,
delete_dimensions!, delete_dimensions,

AbstractMeasure, SymmetricMeasure,
Entropy, KullbackLeibler,
MutualInformation, MutualInformationOverEntropy,
estimate, estimate_on_marginal,
GapUnionPercentage, GapIntersectionPercentage,

estimateincolumns, estimateinsequences, cumulative,

APC!,

buslje09, BLMI, pairwisegapfraction,

# Formats from MIToS.MSA:
Raw, Stockholm, FASTA,

# Externals
gaussdca

include("BLOSUM62.jl")
include("Probabilities.jl")
include("InformationMeasures.jl")
include("Iterations.jl")
include("Corrections.jl")
include("CorrectedMutualInformation.jl")
include("Externals.jl")

end


text:
function extensibleunion!(@nospecialize(u))
    global _registry_extensibleunion_to_genericfunctions
    global _registry_extensibleunion_to_members
    if !isconcretetype(u)
        throw(ArgumentError("The provided type must be a concrete type"))
    end
    if !isstructtype(u)
        throw(ArgumentError("The provided type must be a struct type"))
    end
    if length(fieldnames(u)) > 0
        throw(ArgumentError("The provided type must have no fields"))
    end
    if u.mutable
        throw(ArgumentError("The provided type must be an immutable type"))
    end
    if !(supertype(u) === Any)
        throw(ArgumentError(
            "The immediate supertype of the provided type must be Any"))
    end
    if !haskey(_registry_extensibleunion_to_members, u)
        _registry_extensibleunion_to_genericfunctions[u] = Set{Any}()
        _registry_extensibleunion_to_members[u] = Set{Any}([u])
    end
    _update_all_methods_for_extensibleunion!(u)
    return u
end

function isextensibleunion(@nospecialize(u))
    global _registry_extensibleunion_to_members
    return haskey(_registry_extensibleunion_to_members, u)
end

function addtounion!(@nospecialize(u), varargs...)
    return addtounion!(u, varargs)
end

function addtounion!(@nospecialize(u), @nospecialize(varargs::Tuple))
    global _registry_extensibleunion_to_members
    if isextensibleunion(u)
        old_members_set = deepcopy(_registry_extensibleunion_to_members[u])
        for i = 1:length(varargs)
            push!(_registry_extensibleunion_to_members[u], varargs[i])
        end
        new_members_set = deepcopy(_registry_extensibleunion_to_members[u])
        old_members_union = _set_to_union(old_members_set)
        new_members_union = _set_to_union(new_members_set)
        _update_all_methods_for_extensibleunion!(u, old_members_union =>
                                                    new_members_union)
    else
        throw(ArgumentError(
            "First argument must be a registered extensible union."))
    end
end

function unioncurrentlycontains(@nospecialize(u), @nospecialize(t))
    global _registry_extensibleunion_to_members
    if isextensibleunion(u)
        return t in _registry_extensibleunion_to_members[u]
    else
        throw(ArgumentError(
            "First argument must be a registered extensible union."))
    end
end


text:
signbit{S<:Sculpt,Q<:Qualia,C<:Clay}(a::Flex{S,Q,C}) = (signbit(a.lo),signbit(a.hi))
sign{S<:Sculpt,Q<:Qualia,C<:Clay}(a::Flex{S,Q,C}) = (sign(a.lo),sign(a.hi))

function copysign{S<:Sculpt,Q<:Qualia,C<:Clay}(a::Flex{S,Q,C}, b::Real)
    lo = copysign(a.lo,b)
    hi = copysign(a.hi,b)
    lo <= hi ? (S)(lo,hi) : (negate(S))(hi,lo)
end

function flipsign{S<:Sculpt,Q<:Qualia,C<:Clay}(a::Flex{S,Q,C}, b::Real)
    lo = flipsign(a.lo,b)
    hi = flipsign(a.hi,b)
    lo <= hi ? (S)(lo,hi) : (negate(S))(hi,lo)
end

frexp{S<:Sculpt,Q<:Qualia,C<:Clay}(a::Flex{S,Q,C}) = (frexp(a.lo),frexp(a.hi))
ldexp{S<:Sculpt,Q<:Qualia,C<:Clay,I<:Union{Int32,Int64}}(::Type{Flex{S,Q,C}},a::Tuple{Tuple{C,I},Tuple{C,I}}) =
   (Flex{S,Q,C})(ldexp(a[1][1],a[1][2]),ldexp(a[2][1],a[2][2]))
ldexp{C<:Clay,I<:Union{Int32,Int64}}(a::Tuple{Tuple{C,I},Tuple{C,I}}) =
   (ClCl)(ldexp(a[1][1],a[1][2]),ldexp(a[2][1],a[2][2]))

#= from Julia
`decompose(x)`: non-canonical decomposition of rational values as `num*2^pow/den`.
The decompose function is the point where rational-valued numeric types that support
hashing hook into the hashing protocol. `decompose(x)` should return three integer
values `num, pow, den`, such that the value of `x` is mathematically equal to
    num*2^pow/den
The decomposition need not be canonical in the sense that it just needs to be *some*
way to express `x` in this form, not any particular way – with the restriction that
`num` and `den` may not share any odd common factors. They may, however, have powers
of two in common – the generic hashing code will normalize those as necessary.
Special values:
 - `x` is zero: `num` should be zero and `den` should have the same sign as `x`
 - `x` is infinite: `den` should be zero and `num` should have the same sign as `x`
 - `x` is not a number: `num` and `den` should both be zero
=#
decompose{S<:Sculpt, Q<:Qualia, C<:Clay}(x::Flex{S,Q,C}) = decompose(mostRepresentativeValue(x))

isnan{S<:Sculpt, Q<:Qualia, C<:Clay}(x::Flex{S,Q,C}) = (isnan(x.lo) | isnan(x.hi))
isinf{S<:Sculpt, Q<:Qualia, C<:Clay}(x::Flex{S,Q,C}) = (isinf(x.lo) | isinf(x.hi))
isfinite{S<:Sculpt, Q<:Qualia, C<:Clay}(x::Flex{S,Q,C}) = (isfinite(x.lo) & isfinite(x.hi))
issubnormal{S<:Sculpt, Q<:Qualia, C<:Clay}(x::Flex{S,Q,C}) = (issubnormal(x.lo) | issubnormal(x.hi))
isinteger{S<:Sculpt, Q<:Qualia, C<:Clay}(x::Flex{S,Q,C}) = (x.lo==x.hi) && (isinteger(x.lo) & isinteger(x.hi))