text:
# MIT License
#
# Copyright (c) 2018 Martin Biel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

@with_kw mutable struct NesterovData{T}
    t::T = 1.0
end

@with_kw mutable struct NesterovParameters
    stabilizing_projection::Bool = false
end

"""
    NesterovProximal

Functor object for using nesterov accleration, with FISTA updates, in the prox step of a quasigradient algorithm. Create by supplying a [`Nesterov`](@ref) object through `prox ` to `QuasiGradient.Optimizer` or by setting the [`Prox`](@ref) attribute.

...
# Parameters
- `prox::AbstractProx = Polyhedron`: Inner prox step
- `stabilizing_projection::Bool = false`: Specify if an extra guarding projection should be performed to ensure first-stage feasibility
...
"""
struct NesterovProximal{T <: AbstractFloat, P <: AbstractProximal} <: AbstractProximal
    data::NesterovData{T}
    parameters::NesterovParameters

    prox::P
    xprev::Vector{T}

    function NesterovProximal(proximal::AbstractProximal, ::Type{T}; kw...) where T <: AbstractFloat
        P = typeof(proximal)
        return new{T,P}(NesterovData{T}(),
                        NesterovParameters(; kw...),
                        proximal,
                        Vector{T}())
    end
end

function initialize_prox!(quasigradient::AbstractQuasiGradient, nesterov::NesterovProximal)
    # Initialize inner
    initialize_prox!(quasigradient, nesterov.prox)
    return nothing
end

function restore_proximal_master!(quasigradient::AbstractQuasiGradient, nesterov::NesterovProximal)
    # Restore inner
    restore_proximal_master!(quasigradient, nesterov.prox)
    return nothing
end

function prox!(quasigradient::AbstractQuasiGradient, nesterov::NesterovProximal, x::AbstractVector, ∇f::AbstractVector, h::AbstractFloat)
    if length(nesterov.xprev) == 0
        resize!(nesterov.xprev, length(x))
        nesterov.xprev .= x
        return nothing
    end
    @unpack t = nesterov.data
    # Prox step
    prox!(quasigradient, nesterov.prox, x, ∇f, h)
    # Update incumbent xₖ
    quasigradient.ξ .= x
    # Update t
    tnext = (1 + sqrt(1 + 4*t^2)) / 2
    # Look-ahead
    x .= x + ((t - 1) / tnext) * (x - nesterov.xprev)
    # Possibly stabilize the iterate
    if nesterov.parameters.stabilizing_projection
        prox!(quasigradient, nesterov.prox, x, ∇f, 0.0)
    end
    # Update memory
    nesterov.xprev .= quasigradient.ξ
    nesterov.data.t = tnext
    return nothing
end

# API
# ------------------------------------------------------------
"""
    Nesterov

Factory object for [`NesterovProximal`](@ref). Pass to `prox` in `Quasigradient.Optimizer` or set the [`Prox`](@ref) attribute. See ?NesterovProximal for parameter descriptions.

"""
mutable struct Nesterov <: AbstractProx
    prox::AbstractProx
    parameters::NesterovParameters
end
Nesterov(; prox::AbstractProx = Polyhedron(), kw...) = Nesterov(prox, NesterovParameters(; kw...))

function (nesterov::Nesterov)(structure::StageDecompositionStructure, x₀::AbstractVector, ::Type{T}) where T <: AbstractFloat
    proximal = nesterov.prox(structure, x₀, T)
    return NesterovProximal(proximal, T; type2dict(nesterov.parameters)...)
end

function str(::Nesterov)
    return ""
end


text:
module EmojifyText
include("EmojiDict.jl")

# package code goes here

# conversion_dict = conversion_dict

function convert_line(line::String)
  # converts a line to emoji
  newline = "";
  for char in line
    newchar = get(conversion_dict, string(char), "🚓");
    newline = newline * newchar;
  end
  return newline;
end

function unconvert_line(line::String)
  # unconverts a line form emoji
  # TODO: make this work (emoji can be more than one char)
  newline = "";
  for char in line
    newchar = get(dict_conversion, string(char), "🚓");
    newline = newline * newchar;
  end
  return newline;
end

function convert_file(input_file_name::String, output_file_name::String)
  # Converts a file from ascii to emoji
  f_in = open(input_file_name,"r")
  f_out = open(output_file_name, "w")
  #Deleting the file if it exists
  print(f_out,"")
  close(f_out)
  f_out = open(output_file_name, "a")
  for line in eachline(f_in)
    newline = convert_line(line);
    print(f_out,newline);
  end
  close(f_in)
  close(f_out)
  # print_file(output_file_name)
end

function unconvert_file(input_file_name::String, output_file_name::String)
  # Converts a file from ascii to emoji
  # TODO: make this work (emoji can be more than one char)
  f_in = open(input_file_name,"r")
  f_out = open(output_file_name, "w")
  #Deleting the file if it exists
  print(f_out,"")
  close(f_out)
  f_out = open(output_file_name, "a")
  for line in eachline(f_in)
    newline = unconvert_line(line);
    print(f_out,newline);
  end
  close(f_in)
  close(f_out)
  # print_file(output_file_name)
end

function print_file(file_name::String)
  # Prints out a file to console
  f_n = open(file_name,"r")
  println("Printing file "*file_name)
  println("-----------------------------------------")
  for line in eachline(f_n)
    print(line)
  end
  close(f_n)
end

end # module

ext: jl


text:
# This file is auto-generated by AWSMetadata.jl
using AWS
using AWS.AWSServices: finspace_data
using AWS.Compat
using AWS.UUIDs

"""
    create_changeset(change_type, dataset_id, format_params, source_params)
    create_changeset(change_type, dataset_id, format_params, source_params, params::Dict{String,<:Any})

Creates a new Changeset in a FinSpace Dataset.

# Arguments
- `change_type`: Option to indicate how a Changeset will be applied to a Dataset.
  REPLACE - Changeset will be considered as a replacement to all prior loaded Changesets.
  APPEND - Changeset will be considered as an addition to the end of all prior loaded
  Changesets.    MODIFY - Changeset is considered as a replacement to a specific prior
  ingested Changeset.
- `dataset_id`: The unique identifier for the FinSpace Dataset where the Changeset will be
  created.
- `format_params`: Options that define the structure of the source file(s) including the
  format type (formatType), header row (withHeader), data separation character (separator)
  and the type of compression (compression).   formatType is a required attribute and can
  have the following values:     PARQUET - Parquet source file format.    CSV - CSV source
  file format.    JSON - JSON source file format.    XML - XML source file format.    For
  example, you could specify the following for formatParams:  \"formatParams\": {
  \"formatType\": \"CSV\", \"withHeader\": \"true\", \"separator\": \",\",
  \"compression\":\"None\" }
- `source_params`: Options that define the location of the data being ingested.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A token used to ensure idempotency.
"""
function create_changeset(
    changeType,
    datasetId,
    formatParams,
    sourceParams;
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "POST",
        "/datasets/$(datasetId)/changesetsv2",
        Dict{String,Any}(
            "changeType" => changeType,
            "formatParams" => formatParams,
            "sourceParams" => sourceParams,
            "clientToken" => string(uuid4()),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function create_changeset(
    changeType,
    datasetId,
    formatParams,
    sourceParams,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "POST",
        "/datasets/$(datasetId)/changesetsv2",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "changeType" => changeType,
                    "formatParams" => formatParams,
                    "sourceParams" => sourceParams,
                    "clientToken" => string(uuid4()),
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    create_data_view(dataset_id, destination_type_params)
    create_data_view(dataset_id, destination_type_params, params::Dict{String,<:Any})

Creates a Dataview for a Dataset.

# Arguments
- `dataset_id`: The unique Dataset identifier that is used to create a Dataview.
- `destination_type_params`: Options that define the destination type for the Dataview.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"asOfTimestamp"`: Beginning time to use for the Dataview. The value is determined as
  Epoch time in milliseconds. For example, the value for Monday, November 1, 2021 12:00:00 PM
  UTC is specified as 1635768000000.
- `"autoUpdate"`: Flag to indicate Dataview should be updated automatically.
- `"clientToken"`: A token used to ensure idempotency.
- `"partitionColumns"`: Ordered set of column names used to partition data.
- `"sortColumns"`: Columns to be used for sorting the data.
"""
function create_data_view(
    datasetId, destinationTypeParams; aws_config::AbstractAWSConfig=global_aws_config()
)
    return finspace_data(
        "POST",
        "/datasets/$(datasetId)/dataviewsv2",
        Dict{String,Any}(
            "destinationTypeParams" => destinationTypeParams,
            "clientToken" => string(uuid4()),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function create_data_view(
    datasetId,
    destinationTypeParams,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "POST",
        "/datasets/$(datasetId)/dataviewsv2",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "destinationTypeParams" => destinationTypeParams,
                    "clientToken" => string(uuid4()),
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    create_dataset(alias, dataset_description, dataset_title, kind, permission_group_params)
    create_dataset(alias, dataset_description, dataset_title, kind, permission_group_params, params::Dict{String,<:Any})

Creates a new FinSpace Dataset.

# Arguments
- `alias`: The unique resource identifier for a Dataset.
- `dataset_description`: Description of a Dataset.
- `dataset_title`: Display title for a FinSpace Dataset.
- `kind`: The format in which Dataset data is structured.    TABULAR - Data is structured
  in a tabular format.    NON_TABULAR - Data is structured in a non-tabular format.
- `permission_group_params`: Permission group parameters for Dataset permissions.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A token used to ensure idempotency.
- `"ownerInfo"`: Contact information for a Dataset owner.
- `"schemaDefinition"`: Definition for a schema on a tabular Dataset.
"""
function create_dataset(
    alias,
    datasetDescription,
    datasetTitle,
    kind,
    permissionGroupParams;
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "POST",
        "/datasetsv2",
        Dict{String,Any}(
            "alias" => alias,
            "datasetDescription" => datasetDescription,
            "datasetTitle" => datasetTitle,
            "kind" => kind,
            "permissionGroupParams" => permissionGroupParams,
            "clientToken" => string(uuid4()),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function create_dataset(
    alias,
    datasetDescription,
    datasetTitle,
    kind,
    permissionGroupParams,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "POST",
        "/datasetsv2",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "alias" => alias,
                    "datasetDescription" => datasetDescription,
                    "datasetTitle" => datasetTitle,
                    "kind" => kind,
                    "permissionGroupParams" => permissionGroupParams,
                    "clientToken" => string(uuid4()),
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    delete_dataset(dataset_id)
    delete_dataset(dataset_id, params::Dict{String,<:Any})

Deletes a FinSpace Dataset.

# Arguments
- `dataset_id`: The unique identifier of the Dataset to be deleted.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A token used to ensure idempotency.
"""
function delete_dataset(datasetId; aws_config::AbstractAWSConfig=global_aws_config())
    return finspace_data(
        "DELETE",
        "/datasetsv2/$(datasetId)",
        Dict{String,Any}("clientToken" => string(uuid4()));
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function delete_dataset(
    datasetId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "DELETE",
        "/datasetsv2/$(datasetId)",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("clientToken" => string(uuid4())), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_changeset(changeset_id, dataset_id)
    get_changeset(changeset_id, dataset_id, params::Dict{String,<:Any})

Get information about a Changeset.

# Arguments
- `changeset_id`: The unique identifier of the Changeset for which to get data.
- `dataset_id`: The unique identifier for the FinSpace Dataset where the Changeset is
  created.

"""
function get_changeset(
    changesetId, datasetId; aws_config::AbstractAWSConfig=global_aws_config()
)
    return finspace_data(
        "GET",
        "/datasets/$(datasetId)/changesetsv2/$(changesetId)";
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_changeset(
    changesetId,
    datasetId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "GET",
        "/datasets/$(datasetId)/changesetsv2/$(changesetId)",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_data_view(dataset_id, dataview_id)
    get_data_view(dataset_id, dataview_id, params::Dict{String,<:Any})

Gets information about a Dataview.

# Arguments
- `dataset_id`: The unique identifier for the Dataset used in the Dataview.
- `dataview_id`: The unique identifier for the Dataview.

"""
function get_data_view(
    datasetId, dataviewId; aws_config::AbstractAWSConfig=global_aws_config()
)
    return finspace_data(
        "GET",
        "/datasets/$(datasetId)/dataviewsv2/$(dataviewId)";
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_data_view(
    datasetId,
    dataviewId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "GET",
        "/datasets/$(datasetId)/dataviewsv2/$(dataviewId)",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_dataset(dataset_id)
    get_dataset(dataset_id, params::Dict{String,<:Any})

Returns information about a Dataset.

# Arguments
- `dataset_id`: The unique identifier for a Dataset.

"""
function get_dataset(datasetId; aws_config::AbstractAWSConfig=global_aws_config())
    return finspace_data(
        "GET",
        "/datasetsv2/$(datasetId)";
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_dataset(
    datasetId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "GET",
        "/datasetsv2/$(datasetId)",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_programmatic_access_credentials(environment_id)
    get_programmatic_access_credentials(environment_id, params::Dict{String,<:Any})

Request programmatic credentials to use with FinSpace SDK.

# Arguments
- `environment_id`: The FinSpace environment identifier.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"durationInMinutes"`: The time duration in which the credentials remain valid.
"""
function get_programmatic_access_credentials(
    environmentId; aws_config::AbstractAWSConfig=global_aws_config()
)
    return finspace_data(
        "GET",
        "/credentials/programmatic",
        Dict{String,Any}("environmentId" => environmentId);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_programmatic_access_credentials(
    environmentId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "GET",
        "/credentials/programmatic",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("environmentId" => environmentId), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    get_working_location()
    get_working_location(params::Dict{String,<:Any})

A temporary Amazon S3 location, where you can copy your files from a source location to
stage or use as a scratch space in FinSpace notebook.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"locationType"`: Specify the type of the working location.    SAGEMAKER - Use the Amazon
  S3 location as a temporary location to store data content when working with FinSpace
  Notebooks that run on SageMaker studio.    INGESTION - Use the Amazon S3 location as a
  staging location to copy your data content and then use the location with the Changeset
  creation operation.
"""
function get_working_location(; aws_config::AbstractAWSConfig=global_aws_config())
    return finspace_data(
        "POST", "/workingLocationV1"; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end
function get_working_location(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return finspace_data(
        "POST",
        "/workingLocationV1",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_changesets(dataset_id)
    list_changesets(dataset_id, params::Dict{String,<:Any})

Lists the FinSpace Changesets for a Dataset.

# Arguments
- `dataset_id`: The unique identifier for the FinSpace Dataset to which the Changeset
  belongs.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results per page.
- `"nextToken"`: A token indicating where a results page should begin.
"""
function list_changesets(datasetId; aws_config::AbstractAWSConfig=global_aws_config())
    return finspace_data(
        "GET",
        "/datasets/$(datasetId)/changesetsv2";
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_changesets(
    datasetId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "GET",
        "/datasets/$(datasetId)/changesetsv2",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_data_views(dataset_id)
    list_data_views(dataset_id, params::Dict{String,<:Any})

Lists all available Dataviews for a Dataset.

# Arguments
- `dataset_id`: The unique identifier of the Dataset for which to retrieve Dataviews.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results per page.
- `"nextToken"`: A token indicating where a results page should begin.
"""
function list_data_views(datasetId; aws_config::AbstractAWSConfig=global_aws_config())
    return finspace_data(
        "GET",
        "/datasets/$(datasetId)/dataviewsv2";
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_data_views(
    datasetId,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "GET",
        "/datasets/$(datasetId)/dataviewsv2",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    list_datasets()
    list_datasets(params::Dict{String,<:Any})

Lists all of the active Datasets that a user has access to.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results per page.
- `"nextToken"`: A token indicating where a results page should begin.
"""
function list_datasets(; aws_config::AbstractAWSConfig=global_aws_config())
    return finspace_data(
        "GET", "/datasetsv2"; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end
function list_datasets(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return finspace_data(
        "GET", "/datasetsv2", params; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end

"""
    update_changeset(changeset_id, dataset_id, format_params, source_params)
    update_changeset(changeset_id, dataset_id, format_params, source_params, params::Dict{String,<:Any})

Updates a FinSpace Changeset.

# Arguments
- `changeset_id`: The unique identifier for the Changeset to update.
- `dataset_id`: The unique identifier for the FinSpace Dataset in which the Changeset is
  created.
- `format_params`: Options that define the structure of the source file(s).
- `source_params`: Options that define the location of the data being ingested.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A token used to ensure idempotency.
"""
function update_changeset(
    changesetId,
    datasetId,
    formatParams,
    sourceParams;
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "PUT",
        "/datasets/$(datasetId)/changesetsv2/$(changesetId)",
        Dict{String,Any}(
            "formatParams" => formatParams,
            "sourceParams" => sourceParams,
            "clientToken" => string(uuid4()),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function update_changeset(
    changesetId,
    datasetId,
    formatParams,
    sourceParams,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "PUT",
        "/datasets/$(datasetId)/changesetsv2/$(changesetId)",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "formatParams" => formatParams,
                    "sourceParams" => sourceParams,
                    "clientToken" => string(uuid4()),
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    update_dataset(alias, dataset_id, dataset_title, kind)
    update_dataset(alias, dataset_id, dataset_title, kind, params::Dict{String,<:Any})

Updates a FinSpace Dataset.

# Arguments
- `alias`: The unique resource identifier for a Dataset.
- `dataset_id`: The unique identifier for the Dataset to update.
- `dataset_title`: A display title for the Dataset.
- `kind`: The format in which the Dataset data is structured.    TABULAR - Data is
  structured in a tabular format.    NON_TABULAR - Data is structured in a non-tabular
  format.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A token used to ensure idempotency.
- `"datasetDescription"`: A description for the Dataset.
- `"schemaDefinition"`: Definition for a schema on a tabular Dataset.
"""
function update_dataset(
    alias, datasetId, datasetTitle, kind; aws_config::AbstractAWSConfig=global_aws_config()
)
    return finspace_data(
        "PUT",
        "/datasetsv2/$(datasetId)",
        Dict{String,Any}(
            "alias" => alias,
            "datasetTitle" => datasetTitle,
            "kind" => kind,
            "clientToken" => string(uuid4()),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function update_dataset(
    alias,
    datasetId,
    datasetTitle,
    kind,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return finspace_data(
        "PUT",
        "/datasetsv2/$(datasetId)",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "alias" => alias,
                    "datasetTitle" => datasetTitle,
                    "kind" => kind,
                    "clientToken" => string(uuid4()),
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end


text:
using Qaintessent

"""
    qft_circuit(N)

Construct the quantum Fourier transform circuit for `N` qubits.
"""
function qft_circuit(N)
    main = vcat([
        CircuitGate[j == i ?
             circuit_gate(i, HadamardGate()) :
             circuit_gate(j, PhaseShiftGate(2π/2^(j-i+1)), i) for j in N:-1:i]
        for i in N:-1:1]...)
    swap = [circuit_gate(i, N-i+1, SwapGate()) for i in 1:(N÷2)]
    return N > 1 ? CircuitGate[main; swap] : main
end

"""
    toffoli_circuit(trg, cntrl, N)

construct the circuit for decomposing the Toffoli gate in Figure 4.9 of Nielsen and Chuang (2000). the constructed toffoli gate acts in circuit of `N` qubits, has controls
    on wires in Tuple `cntrl` and has target on wire `trg`.
returns a `CircuitGateChain{N}` object.
"""
function toffoli_circuit(trg::Integer, cntrl::Tuple{<:Integer,<:Integer})
    [
        circuit_gate(trg, HadamardGate()),
        circuit_gate(trg, X, cntrl[2]),
        circuit_gate(trg, TdagGate()),
        circuit_gate(trg, X, cntrl[1]),
        circuit_gate(trg, TGate()),
        circuit_gate(trg, X, cntrl[2]),
        circuit_gate(trg, TdagGate()),
        circuit_gate(trg, X, cntrl[1]),
        circuit_gate(trg, TGate()),
        circuit_gate(cntrl[2], TGate()),
        circuit_gate(cntrl[2], X, cntrl[1]),
        circuit_gate(trg, HadamardGate()),
        circuit_gate(cntrl[1], TGate()),
        circuit_gate(cntrl[2], TdagGate()),
        circuit_gate(cntrl[2], X, cntrl[1]),
    ]
end

"""
    vbe_adder_circuit(N)

Construct an in-place adder for 2 integers represented by `N` qubits.
Based on ripple-carry adder circuit in Vedral et. al (Phys. Rev. A 54, 147 (1996), arXiv:quant-ph/9511018)
Returns a CircuitGateChain{3N+1} as there are N+1 ancillary wires

If the two added integers are represented as:

``A = a_{0}\\times 2^{0} + a_{1} \\times 2^{1} + a_{2} \\times 2^{2} + .. + a_{N} \\times 2^{N} \\\\ B = b_{0}\\times 2^{0} + b_{1} \\times 2^{1} + b_{2} \\times 2^{2} + .. + b_{N} \\times 2^{N}``

The input index should be ``a_{0}a_{1}a_{2}..a_{N}b_{0}b_{1}b_{2}..b_{N} + 1`` with ``a_{0}`` as the fastest running index

The output index will be in the form ``a_{0}a_{1}a_{2}..a_{N}c_{0}c_{1}c_{2}..c_{N} + 1`` where:

``C = (A+B) \\% (2^{N} + 1) = c_{0}\\times 2^{0} + c_{1} \\times 2^{1} + c_{2} \\times 2^{2} + ... + c_{N} \\times 2^{N}``
"""
function vbe_adder_circuit(N::Integer)
    M = 3N + 1
    carry(a,b,c,d) =
    [
        circuit_gate(d, X, (b, c)),
        circuit_gate(c, X, b),
        circuit_gate(d, X, (a, c)),
    ]
    rcarry(a,b,c,d) =
    [
        circuit_gate(d, X, (a, c)),
        circuit_gate(c, X, b),
        circuit_gate(d, X, (b, c)),
    ]
    sum(a,b,c) =
    [
        circuit_gate(c, X, b),
        circuit_gate(c, X, a),
    ]
    rsum(a,b,c) =
    [
        circuit_gate(c, X, a),
        circuit_gate(c, X, b),
    ]
    cgc = carry(2N+1, 1, N+1, 2N+2)
    for i in 2:N
        append!(cgc, carry(2N+i, i, N+i, 2N+i+1))
    end

    append!(cgc, [circuit_gate(2N, X, N)])
    append!(cgc, sum(3N, N, 2N))
    for i in N-1:-1:1
        append!(cgc, rcarry(2N+i, i, N+i, 2N+i+1))
        append!(cgc, sum(2N+i, i, N+i))
    end
    return cgc
end

"""
    p_round(P, M, N)
        execute a P round for qcla algorithms
"""
function p_round(P::Function, ::Integer, N::Integer)
    # Add gates for P rounds
    pchain = CircuitGate[]
    tmax = floor(Int, log(2, N)) - 1
    for t in 1:tmax
        mmax = floor(Int, N/2^t) - 1
        for m in 1:mmax
            push!(pchain, circuit_gate(P(t, m), X, (P(t-1, 2m), P(t-1, 2m+1))))
        end
    end
    return pchain
end

"""
    p_round(P, M, N)
        execute a P round for qcla algorithms
"""
function p_round(P::Function, ::Integer, ::Integer, Ñ::Integer)
    # Add gates for P rounds
    pchain = CircuitGate[]
    tmax = floor(Int, log(2, Ñ)) - 1
    for t in 1:tmax
        mmax = floor(Int, Ñ/2^t) - 1
        for m in 1:mmax
            push!(pchain, circuit_gate(P(t, m), X, (P(t-1, 2m), P(t-1, 2m+1))))
        end
    end
    return pchain
end

"""
    g_round(P, M, N)
        execute a P round for qcla algorithms
"""
function g_round(P::Function, G::Function, ::Integer, N::Integer)
    # Add gates for G rounds
    gchain = CircuitGate[]
    tmax = floor(Int, log(2, N))
    for t in 1:tmax
        mmax = floor(Int, N/2^t) - 1
        for m in 0:mmax
            push!(gchain, circuit_gate(G(2^t*m + 2^t), X, (G(2^t*m+2^(t-1)) , P(t-1, 2m+1))))
        end
    end
    return gchain
end

function g_round(P::Function, G::Function, M::Integer, ::Integer, Ñ::Integer)
    # Add gates for G rounds
    gchain = CircuitGate[]
    tmax = floor(Int, log(2, Ñ))
    for t in 1:tmax
        mmax = floor(Int, Ñ/2^t) - 1
        for m in 0:mmax
            push!(gchain, circuit_gate(G(2^t*m + 2^t), X, (G(2^t*m+2^(t-1)), P(t-1, 2m+1))))
        end
    end
    return gchain
end

"""
    c_round(P, G, M, N)
"""
function c_round(P::Function, G::Function, M::Integer, N::Integer)
    cchain = CircuitGate[]
    tmax = floor(Int, log(2, 2N/3))
    for t in tmax:-1:1
        mmax = floor(Int, (N-2^(t-1))/2^t)
        for m in 1:mmax
            push!(cchain, circuit_gate(G(2^t*m + 2^(t-1)), X, (G(2^t*m), P(t-1, 2m))))
        end
    end
    return cchain
end

function c_round(P::Function, G::Function, M::Integer, N::Integer, Ñ::Integer)
    cchain = CircuitGate[]
    tmax = floor(Int, log(2, 2Ñ/3))
    for t in tmax:-1:1
        mmax = floor(Int, (Ñ-2^(t-1))/2^t)
        for m in 1:mmax
            push!(cchain, circuit_gate(G(2^t*m + 2^(t-1)), X, (G(2^t*m), P(t-1, 2m))))
        end
    end
    return cchain
end

"""
    qcla_out_adder_circuit(N)
Construct an out-of-place adder for 2 integers represented by `N` qubits. returns a `CircuitGateChain{3N+1}` object.
Based on quantum carry-lookahead adder circuit by Draper et. al (Quant. Inf. Comp. 6, 351-369 (2006), arXiv:quant-ph/0406142)
Returns a CircuitGateChain{3N+1} as there are N+1 ancillary wires
If the two added integers are represented as:
``A = a_{0}\\times 2^{0} + a_{1} \\times 2^{1} + a_{2} \\times 2^{2} + .. + a_{N} \\times 2^{N} \\\\ B = b_{0}\\times 2^{0} + b_{1} \\times 2^{1} + b_{2} \\times 2^{2} + .. + b_{N} \\times 2^{N}``
The input index should be ``a_{0}a_{1}a_{2}..a_{N}b_{0}b_{1}b_{2}..b_{N} + 1`` as Julia starts indexing at `1` and ``a_{0}`` as the fastest running index
The output index will be in the form ``a_{0}a_{1}a_{2}..a_{N}b_{0}b_{1}b_{2}..b_{N}c_{0}c_{1}c_{2}..c_{N+1} + 1`` where:
``C = A+B = c_{0}\\times 2^{0} + c_{1} \\times 2^{1} + c_{2} \\times 2^{2} + ... + c_{N+1} \\times 2^{N+1}``
"""
function qcla_out_adder_circuit(N)

    anc = N - count_ones(N) - floor(Int, log(N))
    M = 3N + anc + 1

    function a(m::Integer)
        m + 1
    end

    function b(m::Integer)
        N + m + 1
    end

    function s(m::Integer)
        2N + m + 1
    end

    function G(m::Integer)
        s(m)
    end

    function P(l::Integer, m::Integer)
        if l == 0
            return b(m)
        end

        while l > 1
            m += floor(Int, N/2^(l-1)-1)
            l -= 1
        end
        3N + 1 + m
    end

    setup = CircuitGate[]

    push!(setup, circuit_gate(s(1), X, (a(0), b(0))))
    for i in 1:N-1
        push!(setup, circuit_gate(s(i+1), X, (a(i), b(i))))
        push!(setup, circuit_gate(b(i), X, a(i)))
    end
    cgc = setup

    # Add gates for P rounds
    pchain = p_round(P, M, N)
    append!(cgc, pchain)

    # Add gates for G rounds
    gchain = g_round(P, G, M, N)
    append!(cgc, gchain)

    #  Add gates for C rounds
    cchain = c_round(P, G, M, N)
    append!(cgc, cchain)

    # Remove effect of P rounds
    append!(cgc, reverse(pchain))

    # Teardown
    teardown = CircuitGate[]

    push!(teardown, circuit_gate(s(0), X, b(0)))
    push!(teardown, circuit_gate(s(0), X, a(0)))
    for i in 1:N-1
        push!(teardown, circuit_gate(s(i), X, b(i)))
        push!(teardown, circuit_gate(b(i), X, a(i)))
    end
    append!(cgc, teardown)
    return cgc
end

# TODO: Add comparator circuit for QCLA adder
"""
    qcla_inplace_adder_circuit(N)

Construct an in-place adder for 2 integers represented by `N` qubits.
Based on quantum carry-lookahead adder circuit by Draper et. al (Quant. Inf. Comp. 6, 351-369 (2006), arXiv:quant-ph/0406142)
Returns a CircuitGateChain{3N+1} as there are N+1 ancillary wires

If the two added integers are represented as:

``A = a_{0}\\times 2^{0} + a_{1} \\times 2^{1} + a_{2} \\times 2^{2} + .. + a_{N} \\times 2^{N} \\\\ B = b_{0}\\times 2^{0} + b_{1} \\times 2^{1} + b_{2} \\times 2^{2} + .. + b_{N} \\times 2^{N}``

The input index should be ``a_{0}a_{1}a_{2}..a_{N}b_{0}b_{1}b_{2}..b_{N} + 1`` as Julia starts indexing at `1` and ``a_{0}`` as the fastest running index

The output index will be in the form ``a_{0}a_{1}a_{2}..a_{N}c_{0}c_{1}c_{2}..c_{N+1} + 1`` where:

``C = A+B = c_{0}\\times 2^{0} + c_{1} \\times 2^{1} + c_{2} \\times 2^{2} + ... + c_{N+1} \\times 2^{N+1}``
"""
function qcla_inplace_adder_circuit(N)

    n = 1
    anc = 0
    while 2^n < N
        anc += N ÷ 2^n -1
        n += 1
    end
    M = 3N + anc

    function a(m::Integer)
        m < N || error("a only takes m < N")
        m + 1
    end

    function b(m::Integer)
        m < N || error("b only takes m < N")
        N + m + 1
    end

    function G(m::Integer)
        m > 0 || error("G only takes positive integers")
        M - anc - m + 1
        # 2N + m
    end

    function s(m::Integer)
        m <= N || error("a only takes m <= N")
        if m < N
            return b(m)
        end
        G(m)
    end

    function P(l::Integer, m::Integer)
        if l == 0
            return b(m)
        end

        while l > 1
            m += floor(Int, N/2^(l-1)-1)
            l -= 1
        end
        3N + m
    end

    # setup
    setup = CircuitGate[]
    for i in 0:N-1
        push!(setup, circuit_gate(G(i+1), X, (a(i), b(i))))
        push!(setup, circuit_gate(b(i), X, a(i)))
    end

    # Add gates for P rounds
    pchain = p_round(P, M, N)
    cgc = deepcopy(pchain)

    # Add gates for G rounds
    gchain = g_round(P, G, M, N)
    append!(cgc, gchain)

    #  Add gates for C rounds
    cchain = c_round(P, G, M, N)
    append!(cgc, cchain)

    # Remove effect of P rounds
    append!(cgc, reverse(pchain))

    # Run intermediate circuit
    intermediate = CircuitGate[]

    push!(intermediate, circuit_gate(b(0), X))
    for i in 1:N-2
        push!(intermediate, circuit_gate(b(i), X, G(i)))
        push!(intermediate, circuit_gate(b(i), X))
        push!(intermediate, circuit_gate(b(i), X, a(i)))
    end
    if N > 1
        push!(intermediate, circuit_gate(b(N-1), X, G(N-1)))
    end

    if N > 1
        pchain2 = p_round(P, M, N, N-1)
        gchain2 = g_round(P, G, M, N, N-1)
        cchain2 = c_round(P, G, M, N, N-1)
        rpchain2 = reverse(pchain2)
        r = vcat(pchain2, gchain2, cchain2, rpchain2)
    else
        r = CircuitGate[]
    end

    append!(cgc, intermediate)
    append!(cgc, reverse(r))

    # ## Teardown
    teardown = CircuitGate[]

    if N > 1
        push!(teardown, circuit_gate(G(1), X, (a(0), b(0))))
    end
    push!(teardown, circuit_gate(b(0), X))
    for i in 1:N-2
        push!(teardown, circuit_gate(b(i), X, a(i)))
        push!(teardown, circuit_gate(G(i+1), X, (a(i), b(i))))
        push!(teardown, circuit_gate(b(i), X))
    end

    prepend!(cgc, setup)
    append!(cgc, teardown)

    return cgc
end


text:
module Controlz

using Polynomials
using DifferentialEquations
using Interpolations
using Printf
using Roots
using DataFrames
using CairoMakie, ColorSchemes, Colors

include("plot_theme.jl")

include("tf.jl")
include("special_tfs.jl")
include("systems.jl")
include("sim.jl")
include("viz.jl")
include("controls.jl")
include("margins.jl")
include("closed_loops.jl")
include("show.jl")

export
    # tf.jl
    TransferFunction, zeros_poles_gain, zeros_poles_k, zero_frequency_gain, evaluate, s, proper, strictly_proper, pole_zero_cancellation, zpk_form, system_order,
    # special_tfs.jl
    first_order_system, second_order_system, time_constant, damping_coefficient,
    # systems.jl
    characteristic_polynomial,
    # show.jl
    # sim.jl
    simulate, interpolate,
    # viz.jl
    viz_response, viz_poles_and_zeros, nyquist_diagram, root_locus, bode_plot, mk_gif,
    # controlz.jl
    PController, PIController, PIDController,
    # margins.jl
    gain_phase_margins,
    # closedloops.jl
    ClosedLoopTransferFunction,
    # plot_theme.jl
    cool_theme
end

max_stars_repo_name: SimonEnsemble/Controlz

max_issues_repo_name: SimonEnsemble/Controlz

max_forks_repo_name: SimonEnsemble/Controlz


text:
export EucledianContourDiscretization,
       discretizeContour,
       getContour,
       getContourDistances,
       getContourDerivatives,
       spread_timepoints

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

abstract type AbstractContourDiscretization end
abstract type AbstractSK_ContourDiscretization <: AbstractContourDiscretization end

"""
    Get distance between points in contour
"""
function getContourDistances(CD::AbstractContourDiscretization,
                             C::AbstractContour)
    contour = getContour(CD,C)
    N = length(contour)
    return [contour[i+1] - contour[i]  for i in 1:N-1]
end

"""
    Get the discretized contour
"""
function getContour(CD::AbstractContourDiscretization,
                    C::AbstractContour)
    ts = spread_timepoints(CD,C)
    return map(t -> get_timepoint(t,C), ts)
end

"""
    Get the discretized contour derivatives
"""
function getContourDerivatives(CD::AbstractContourDiscretization,
                    C::AbstractContour)
    ts = spread_timepoints(CD,C)
    return map(t -> get_derivative_timepoint(t,C), ts)
end

"""
    Discretizing the contour
"""
function discretizeContour(CD::EucledianContour, N_points)
    return EucledianContourDiscretization(N_points)
end

function discretizeContour(CD::NonEquilSchwingerKeldyshContour, points)
    return NonEquilSchwingerKeldyshContourDiscretization(points)
end

function discretizeContour(CD::NonEquilTiltedSchwingerKeldyshContour, points)
    return NonEquilTiltedSchwingerKeldyshContourDiscretization(points)
end

function discretizeContour(CD::SchwingerKeldyshContour, N_points)
    return SchwingerKeldyshContourDiscretization(N_points)
end

function discretizeContour(CD::SchwingerKeldyshContour, NR_RT_points, NR_ET_Points)
    return SchwingerKeldyshContourDiscretization(NR_RT_points,NR_ET_Points)
end

function discretizeContour(CD::SchwingerKeldyshContour, NR_RT_points_1, NR_RT_points_2, NR_ET_Points)
    return SchwingerKeldyshContourDiscretization(NR_RT_points_1, NR_RT_points_2,NR_ET_Points)
end

function discretizeContour(CD::SchwingerKeldyshContourRounded1, N_points_1, N_points_rounded)
    return SchwingerKeldyshRounded1ContourDiscretization(N_points_1,N_points_rounded)
end

function discretizeContour(CD::SchwingerKeldyshContourRounded1, N_points)
    N_points_1 = 3*N_points/4
    N_points_rounded = 1*N_points/4
    return SchwingerKeldyshRounded1ContourDiscretization(N_points_1,N_points_rounded)
end

function discretizeContour(CD::SchwingerKeldyshContourRounded2, N_points_1, N_points_rounded1, N_points_rounded2)
    return SchwingerKeldyshRounded2ContourDiscretization(N_points_1,N_points_rounded1,N_points_rounded2)
end

function discretizeContour(CD::SchwingerKeldyshContourRounded2, N_points)
    N_points_1 = N_points/2
    N_points_rounded1 = N_points/4
    N_points_rounded2 = N_points/4
    return SchwingerKeldyshRounded2ContourDiscretization(N_points_1,N_points_rounded1,N_points_rounded2)
end

function discretizeContour(CD::SchwingerKeldyshContourRounded3, N_points_1, N_points_rounded1, N_points_rounded2, N_points_rounded3)
    return SchwingerKeldyshRounded3ContourDiscretization(N_points_1,N_points_rounded1,N_points_rounded2,N_points_rounded3)
end

function discretizeContour(CD::SchwingerKeldyshContourRounded3, N_points)
    N_points_1 = N_points/2
    N_points_rounded1 = 2*N_points/8
    N_points_rounded2 = 1*N_points/8
    N_points_rounded3 = 1*N_points/8
    return SchwingerKeldyshRounded3ContourDiscretization(N_points_1,N_points_rounded1,N_points_rounded2,N_points_rounded3)
end

################# Eucledian contour #################
struct EucledianContourDiscretization <: AbstractContourDiscretization
    NR_Points::Integer
end

Base.string(CD::EucledianContourDiscretization) = string("NR_Points: ", CD.NR_Points)

function spread_timepoints(CD::EucledianContourDiscretization,
                           C::EucledianContour)
    return collect(range(0,stop=1,length=CD.NR_Points+1))
end

################# Non-Equilibrium Schwinger-Keldysh Contour ####################

"""
    Discretization of the Non-Equilibrium Schwinger Keldysh Contour
"""
struct NonEquilSchwingerKeldyshContourDiscretization <: AbstractSK_ContourDiscretization
    points::Integer
    function NonEquilSchwingerKeldyshContourDiscretization(points)
        @assert (points-1)%2 == 0
        new(points)
    end
end

Base.string(CD::NonEquilSchwingerKeldyshContourDiscretization) = string("NR_Points: ", CD.points)

"""
    Create the parameter array, i.e. the points corresponding to
    t in Reals in the parameterize curve

    This will include the periodic boundary value, i.e. the t=1.0
"""
function spread_timepoints(CD::NonEquilSchwingerKeldyshContourDiscretization,
                          C::NonEquilSchwingerKeldyshContour)
    timePoints = []
    N = floor(Int64,CD.points/2)
    for i in 0:CD.points-1
        if i < CD.points/2
            push!(timePoints, i*t1(C)/N)
        else
            i_eff = i - N
            push!(timePoints, t1(C) + i_eff*t2(C)/N)
        end

    end
    return timePoints
end

################# Non-Equilibrium Schwinger-Keldysh Contour ####################

"""
    Discretization of the Non-Equilibrium Schwinger Keldysh Contour
"""
struct NonEquilTiltedSchwingerKeldyshContourDiscretization <: AbstractSK_ContourDiscretization
    points::Integer
    function NonEquilTiltedSchwingerKeldyshContourDiscretization(points)
        @assert (points-1)%2 == 0
        new(points)
    end
end

Base.string(CD::NonEquilTiltedSchwingerKeldyshContourDiscretization) = string("NR_Points: ", CD.points)

"""
    Create the parameter array, i.e. the points corresponding to
    t in Reals in the parameterize curve

    This will include the periodic boundary value, i.e. the t=1.0
"""
function spread_timepoints(CD::NonEquilTiltedSchwingerKeldyshContourDiscretization,
                          C::NonEquilTiltedSchwingerKeldyshContour)
    timePoints = []
    N = floor(Int64,CD.points/2)
    for i in 0:CD.points-1
        if i < CD.points/2
            push!(timePoints, i*t1(C)/N)
        else
            i_eff = i - N
            push!(timePoints, t1(C) + i_eff*t2(C)/N)
        end

    end
    return timePoints
end

################## Schwinger-Keldysh Contour ####################

"""
    Discretization of the Schwinger Keldysh Contour
"""
struct SchwingerKeldyshContourDiscretization <: AbstractSK_ContourDiscretization
    NR_RT_FW_Points::Integer
    NR_RT_BW_Points::Integer
    NR_RT_Points::Integer
    NR_ET_Points::Integer
    function SchwingerKeldyshContourDiscretization(RTP1,RTP2,ETP)
        RTP = RTP1 + RTP2
        new(RTP1,RTP2,RTP,ETP)
    end
end

function Base.string(CD::SchwingerKeldyshContourDiscretization)
        string("NR_Points: ", get_total_points(CD), "\n",
              "NR_RT_FW_Points: ", CD.NR_RT_FW_Points, "\n",
              "NR_RT_BW_Points: ", CD.NR_RT_BW_Points, "\n",
              "NR_ET_Points: ", CD.NR_ET_Points)
end

"""
Short initialization of the SchwingerKeldyshContourDiscretization struct

Separates the nr of points into 2/3 RT-points and 1/3 ET-points.
If not possible the RT-points will be rounded up and the nr of
ET points rounded down
"""
function SchwingerKeldyshContourDiscretization(NR_Points)
    @assert NR_Points>2

    # Special case for 4 points
    if NR_Points == 4
        return SchwingerKeldyshContourDiscretization(2,2)
    end

    RTP = 2*ceil(Int64,NR_Points/3)
    RTP1 = ceil(Int64,RTP/2)
    ETP = floor(Int64, NR_Points/3 - (NR_Points%2==0 ? (NR_Points/3)%2 : (NR_Points%3)%2))

    SchwingerKeldyshContourDiscretization(RTP1,RTP1,ETP)
end

"""
Short initialization of the SchwingerKeldyshContourDiscretization struct

Separates the nr of points into 2/3 RT-points and 1/3 ET-points.
If not possible the RT-points will be rounded up and the nr of
ET points rounded down
"""
function SchwingerKeldyshContourDiscretization(RTP,ETP)
    @assert RTP>0

    RTP1 = floor(Int64,RTP/2)
    RTP2 = ceil(Int64,RTP/2)

    SchwingerKeldyshContourDiscretization(RTP1,RTP2,ETP)
end

"""
    Create the parameter array, i.e. the points corresponding to
    t in Reals in the parameterize curve

    This will include the periodic boundary value, i.e. the t=1.0
"""
function spread_timepoints(CD::SchwingerKeldyshContourDiscretization,
                          C::SchwingerKeldyshContour)
    timePoints = []
    for i in 0:get_total_points(CD)
        if i <= CD.NR_RT_FW_Points
            N = CD.NR_RT_FW_Points
            push!(timePoints, i*t1(C)/N)
        elseif i < CD.NR_RT_Points
            i_eff = i - CD.NR_RT_FW_Points
            N = CD.NR_RT_BW_Points
            push!(timePoints, t1(C) + i_eff*t2(C)/N)
        else
            i_eff = i - CD.NR_RT_Points
            N = CD.NR_ET_Points

            if N == 0
                push!(timePoints, t1(C) + t2(C) + t3(C))
            else
                push!(timePoints, t1(C) + t2(C) + i_eff*t3(C)/N)
            end
        end

    end
    return timePoints
end

################## Schwinger-Keldysh Contour with one rounded corner ####################

"""
    Schwinger-Keldysh contour with one rounded corners
"""
struct SchwingerKeldyshRounded1ContourDiscretization <: AbstractSK_ContourDiscretization
    NR_RT_Points::Integer
    NR_ET_Points::Integer
    NR_Rounded1_Points::Integer
    function SchwingerKeldyshRounded1ContourDiscretization(RTP,ETP,Rounded1)
        @assert RTP%2 == 0
        @assert RTP > 0
        @assert ETP > 0
        @assert Rounded1 > 2
        new(RTP,ETP,Rounded1)
    end
end

function Base.string(CD::SchwingerKeldyshRounded1ContourDiscretization)
    string("NR_Points: ", get_total_points(CD), "\n",
          "NR_RT_Points: ", CD.NR_RT_FW_Points, "\n",
          "NR_ET_Points: ", CD.NR_ET_Points, "\n",
          "NR_Rounded1_Points: ", CD.NR_Rounded1_Points)
end

"""
Short initialization of the SchwingerKeldyshRounded1ContourDiscretization struct

Separates the nr of points into 2/3 RT-points and 1/3 ET-points.
If not possible the RT-points will be rounded up and the nr of
ET points rounded down
"""
function SchwingerKeldyshRounded1ContourDiscretization(NR_Points, NR_Rounded1)
    LinearPoints = SchwingerKeldyshContourDiscretization(NR_Points)
    RTP = LinearPoints.NR_RT_Points
    ETP = LinearPoints.NR_ET_Points
    SchwingerKeldyshRounded1ContourDiscretization(RTP,ETP,NR_Rounded1)
end

"""
    Create the parameter array, i.e. the points corresponding to
    t in Reals in the parameterize curve

    This will include the periodic boundary value, i.e. the t=1.0
"""
function spread_timepoints(CD::SchwingerKeldyshRounded1ContourDiscretization,
                          C::SchwingerKeldyshContourRounded1)
    timePoints = []
    for i in 0:get_total_points(CD)
        if i <= CD.NR_RT_Points / 2
            N = CD.NR_RT_Points / 2
            push!(timePoints, i*t1(C)/N)

        elseif i <= CD.NR_RT_Points / 2 + CD.NR_Rounded1_Points / 2
            i_eff = i - (CD.NR_RT_Points / 2)
            N = CD.NR_Rounded1_Points / 2
            push!(timePoints, t1(C) + i_eff*t2(C)/N)

        elseif i <= CD.NR_RT_Points / 2 + CD.NR_Rounded1_Points
            i_eff = i - (CD.NR_RT_Points / 2) - (CD.NR_Rounded1_Points / 2)
            N = CD.NR_Rounded1_Points / 2
            push!(timePoints, t1(C) + t2(C) + i_eff*t3(C)/N)

        elseif i <= CD.NR_RT_Points + CD.NR_Rounded1_Points
            i_eff = i - (CD.NR_RT_Points / 2) - CD.NR_Rounded1_Points
            N = CD.NR_RT_Points / 2
            push!(timePoints, t1(C) + t2(C) + t3(C) + i_eff*t4(C)/N)

        else
            i_eff = i - CD.NR_RT_Points - CD.NR_Rounded1_Points
            N = CD.NR_ET_Points + 1
            push!(timePoints, t1(C) + t2(C) + t3(C) + t4(C) + i_eff*t5(C)/N)
        end

    end
    return timePoints
end

################## Schwinger-Keldysh Contour with two rounded corner ####################

"""
    Schwinger-Keldysh contour with one rounded corners
"""
struct SchwingerKeldyshRounded2ContourDiscretization <: AbstractSK_ContourDiscretization
    NR_RT_Points
    NR_ET_Points
    NR_Rounded1_Points
    NR_Rounded2_Points
    function SchwingerKeldyshRounded2ContourDiscretization(RTP,ETP,Rounded1,Rounded2)
        @assert RTP%2 == 0
        @assert RTP > 0
        @assert ETP > 0
        @assert Rounded1 > 2
        new(RTP,ETP,Rounded1,Rounded2)
    end
end

function Base.show(io::IO, CD::SchwingerKeldyshRounded2ContourDiscretization)
    string("NR_Points: ", get_total_points(CD), "\n",
          "NR_RT_Points: ", CD.NR_RT_FW_Points, "\n",
          "NR_ET_Points: ", CD.NR_ET_Points, "\n",
          "NR_Rounded1_Points: ", CD.NR_Rounded1_Points, "\n",
          "NR_Rounded2_Points: ", CD.NR_Rounded2_Points)
end

"""
Short initialization of the SchwingerKeldyshRounded1ContourDiscretization struct

Separates the nr of points into 2/3 RT-points and 1/3 ET-points.
If not possible the RT-points will be rounded up and the nr of
ET points rounded down
"""
function SchwingerKeldyshRounded2ContourDiscretization(NR_Points, NR_Rounded1, NR_Rounded2)
    LinearPoints = SchwingerKeldyshContourDiscretization(NR_Points)
    RTP = LinearPoints.NR_RT_Points
    ETP = LinearPoints.NR_ET_Points
    SchwingerKeldyshRounded2ContourDiscretization(RTP,ETP,NR_Rounded1, NR_Rounded2)
end

"""
    Create the parameter array, i.e. the points corresponding to
    t in Reals in the parameterize curve

    This will include the periodic boundary value, i.e. the t=1.0
"""
function spread_timepoints(CD::SchwingerKeldyshRounded2ContourDiscretization,
                          C::SchwingerKeldyshContourRounded2)
    timePoints = []
    for i in 0:get_total_points(CD)
        if i <= CD.NR_RT_Points / 2
            N = CD.NR_RT_Points / 2
            push!(timePoints, i*t1(C)/N)

        elseif i <= CD.NR_RT_Points / 2 + CD.NR_Rounded1_Points / 2
            i_eff = i - (CD.NR_RT_Points / 2)
            N = CD.NR_Rounded1_Points / 2
            push!(timePoints, t1(C) + i_eff*t2(C)/N)

        elseif i <= CD.NR_RT_Points / 2 + CD.NR_Rounded1_Points
            i_eff = i - (CD.NR_RT_Points / 2) - (CD.NR_Rounded1_Points / 2)
            N = CD.NR_Rounded1_Points / 2
            push!(timePoints, t1(C) + t2(C) + i_eff*t3(C)/N)

        elseif i <= CD.NR_RT_Points + CD.NR_Rounded1_Points
            i_eff = i - (CD.NR_RT_Points / 2) - CD.NR_Rounded1_Points
            N = CD.NR_RT_Points / 2
            push!(timePoints, t1(C) + t2(C) + t3(C) + i_eff*t4(C)/N)

        elseif i <= CD.NR_RT_Points + CD.NR_Rounded1_Points + CD.NR_Rounded2_Points
            i_eff = i - CD.NR_RT_Points - CD.NR_Rounded1_Points
            N = CD.NR_Rounded2_Points
            push!(timePoints, t1(C) + t2(C) + t3(C) + t4(C) + i_eff*t5(C)/N)

        else
            i_eff = i - CD.NR_RT_Points - CD.NR_Rounded1_Points - CD.NR_Rounded2_Points
            N = CD.NR_ET_Points + 1
            push!(timePoints, t1(C) + t2(C) + t3(C) + t4(C) + t5(C) + t5(C) + i_eff*t6(C)/N)
        end

    end
    return timePoints
end

################## Schwinger-Keldysh Contour with three rounded corner ####################

"""
    Schwinger-Keldysh contour with one rounded corners
"""
struct SchwingerKeldyshRounded3ContourDiscretization <: AbstractSK_ContourDiscretization
    NR_RT_Points
    NR_ET_Points
    NR_Rounded1_Points
    NR_Rounded2_Points
    NR_Rounded3_Points
    function SchwingerKeldyshRounded3ContourDiscretization(RTP,ETP,Rounded1,Rounded2,Rounded3)
        @assert RTP%2 == 0
        @assert RTP > 0
        @assert ETP > 0
        @assert Rounded1 > 2
        new(RTP,ETP,Rounded1,Rounded2,Rounded3)
    end
end

function Base.string(CD::SchwingerKeldyshRounded3ContourDiscretization)
    string("NR_Points: ", get_total_points(CD), "\n",
          "NR_RT_Points: ", CD.NR_RT_FW_Points, "\n",
          "NR_ET_Points: ", CD.NR_ET_Points, "\n",
          "NR_Rounded1_Points: ", CD.NR_Rounded1_Points, "\n",
          "NR_Rounded2_Points: ", CD.NR_Rounded2_Points, "\n",
          "NR_Rounded3_Points: ", CD.NR_Rounded3_Points)
end

"""
Short initialization of the SchwingerKeldyshRounded1ContourDiscretization struct

Separates the nr of points into 2/3 RT-points and 1/3 ET-points.
If not possible the RT-points will be rounded up and the nr of
ET points rounded down
"""
function SchwingerKeldyshRounded3ContourDiscretization(NR_Points, NR_Rounded1, NR_Rounded2, NR_Rounded3)
    LinearPoints = SchwingerKeldyshContourDiscretization(NR_Points)
    RTP = LinearPoints.NR_RT_Points
    ETP = LinearPoints.NR_ET_Points
    SchwingerKeldyshRounded3ContourDiscretization(RTP,ETP,NR_Rounded1, NR_Rounded2, NR_Rounded3)
end

"""
    Create the parameter array, i.e. the points corresponding to
    t in Reals in the parameterize curve

    This will include the periodic boundary value, i.e. the t=1.0
"""
function spread_timepoints(CD::SchwingerKeldyshRounded3ContourDiscretization,
                          C::SchwingerKeldyshContourRounded3)
    timePoints = []
    for i in 0:get_total_points(CD)
        if i <= CD.NR_RT_Points / 2
            N = CD.NR_RT_Points / 2
            push!(timePoints, i*t1(C)/N)

        elseif i <= CD.NR_RT_Points / 2 + CD.NR_Rounded1_Points / 2
            i_eff = i - (CD.NR_RT_Points / 2)
            N = CD.NR_Rounded1_Points / 2
            push!(timePoints, t1(C) + i_eff*t2(C)/N)

        elseif i <= CD.NR_RT_Points / 2 + CD.NR_Rounded1_Points
            i_eff = i - (CD.NR_RT_Points / 2) - (CD.NR_Rounded1_Points / 2)
            N = CD.NR_Rounded1_Points / 2
            push!(timePoints, t1(C) + t2(C) + i_eff*t3(C)/N)

        elseif i <= CD.NR_RT_Points + CD.NR_Rounded1_Points
            i_eff = i - (CD.NR_RT_Points / 2) - CD.NR_Rounded1_Points
            N = CD.NR_RT_Points / 2
            push!(timePoints, t1(C) + t2(C) + t3(C) + i_eff*t4(C)/N)

        elseif i <= CD.NR_RT_Points + CD.NR_Rounded1_Points + CD.NR_Rounded2_Points
            i_eff = i - CD.NR_RT_Points - CD.NR_Rounded1_Points
            N = CD.NR_Rounded2_Points
            push!(timePoints, t1(C) + t2(C) + t3(C) + t4(C) + i_eff*t5(C)/N)

        elseif i < CD.NR_RT_Points + CD.NR_Rounded1_Points + CD.NR_Rounded2_Points + CD.NR_ET_Points
            i_eff = i - CD.NR_RT_Points - CD.NR_Rounded1_Points - CD.NR_Rounded2_Points
            N = CD.NR_ET_Points
            push!(timePoints, t1(C) + t2(C) + t3(C) + t4(C) + t5(C) + i_eff*t6(C)/N)

        else
            i_eff = i - CD.NR_RT_Points - CD.NR_Rounded1_Points - CD.NR_Rounded2_Points - CD.NR_ET_Points
            N = CD.NR_Rounded3_Points
            push!(timePoints, t1(C) + t2(C) + t3(C) + t4(C) + t5(C) + t6(C) + i_eff*t7(C)/N)
        end

    end
    return timePoints
end

################## Help functions to get nr of points ######################

function get_NR_FW_BW_points(CD::AbstractSK_ContourDiscretization)
    return CD.NR_RT_Points / 2
end

function get_NR_rounded1_points(CD::SchwingerKeldyshRounded1ContourDiscretization)
    return CD.NR_Rounded1_Points / 2
end

function get_total_points(CD::SchwingerKeldyshContourDiscretization)
    return CD.NR_RT_Points + CD.NR_ET_Points
end

function get_total_points(CD::SchwingerKeldyshRounded1ContourDiscretization)
    return CD.NR_RT_Points + CD.NR_Rounded1_Points + CD.NR_ET_Points
end

function get_total_points(CD::SchwingerKeldyshRounded2ContourDiscretization)
    return CD.NR_RT_Points + CD.NR_Rounded1_Points + CD.NR_Rounded2_Points + CD.NR_ET_Points
end

function get_total_points(CD::SchwingerKeldyshRounded3ContourDiscretization)
    return CD.NR_RT_Points + CD.NR_Rounded1_Points + CD.NR_Rounded2_Points + CD.NR_ET_Points + CD.NR_Rounded3_Points
end


text:
pyQt = pyimport("PyQt5.QtWidgets")
pyQtCore = pyimport("PyQt5.QtCore")

@pydef  mutable struct sd_QTTable <: pyQt.QTableWidget
    function  __init__(self,i1,i2,parent)
        pyQt.QTableWidget.__init__(self, 10,2 ,parent)
        self.setContextMenuPolicy(pyQtCore.Qt.ContextMenuPolicy.CustomContextMenu)
        self.customContextMenuRequested.connect(self.contextMenuShow)
        self.setColumnWidth(0,300)
    end
    function contextMenuShow( self,pos)
        self.action1 = pyQt.QAction("新加拷贝的 源 或者 目标 文件夹")
        self.action1.setData(1)
        self.action2 = pyQt.QAction("删除拷贝的 源 或者 目标  文件夹")
        self.action2.setData(2)

        self.Menu1= pyQt.QMenu(self)
        self.Menu1.addSeparator()
        self.Menu1.addAction(  self.action1 )
        self.Menu1.addAction(  self.action2 )

        self.action1.triggered.connect( self.menuSeleted_add )
        self.action2.triggered.connect( self.menuSeleted_Remove )

        a= self.Menu1.exec( self.mapToGlobal(pos)  )
    end
    function menuSeleted_add(self,idx)
        println( )
        dir1 = pyQt.QFileDialog.getExistingDirectory(self, "选择 源 文件夹",
                                                "/");
        dir2 = pyQt.QFileDialog.getExistingDirectory(self, "选择 目标 文件夹",
                "/")
        self.addFIlePair(dir1,dir2)

    end
    function menuSeleted_Remove(self,idx)
        row= self.currentRow()
        self.removeRow(row)
    end

    function addFIlePair(self, from,to)
        row= self.currentRow()
        println(row)
        a = pyQt.QTableWidgetItem(""*from)
        self.setItem(row+0,0,a)
        a = pyQt.QTableWidgetItem(""*to)
        self.setItem(row+0,1,a)
    end

    function setFilePairs(self,pairs)
            row =0
            for (a1,a2) in pairs
                a = pyQt.QTableWidgetItem(a1)
                self.setItem(row,0,a)
                a = pyQt.QTableWidgetItem(a2)
                self.setItem(row,1,a)
                row += 1
            end

    end

    function getFilePairs(self)
        a1=[]
        for row =0:self.rowCount()
            a= self.item( row,0)
            b= self.item( row,1)

            dir1=""
            dir2=""
            if !isnothing(a )
                dir1 =a.text()
            end
            if !isnothing( b )
                dir2 = b.text()
            end

            if  length(dir1) >0 || length( dir2 ) > 0
                append!(a1, [dir1=>dir2] )
            end
        end
        return a1

    end

end


text:
# Miscellaneous Utilities
# =======================
#
# Miscellaneous utilities for interactive tools.
#
# This file is a part of BioJulia.
# License is MIT: https://github.com/BioJulia/Bio.jl/blob/master/LICENSE.md

"""
Copy a whole NucleotideSequence into the user's clipboard.

Useful for pasting a query sequence in web services like BLAST.
"""
function Base.clipboard(seq::BioSequence, width::Integer=50)
    buf = IOBuffer()
    for i in 1:length(seq)
        if i % width == 1 && i > width
            write(buf, '\n')
        end
        write(buf, convert(Char, seq[i]))
    end
    clipboard(bytestring(buf))
end

"""
Copy a Kmer into the user's clipboard.

Useful for pasting a query sequence in web services like BLAST.
"""
Base.clipboard(x::Kmer) = clipboard(string(x))


text:
#name: JuliaDup
#description: Duplicates a string in Julia
#language: julia
#tags: test, selenium
#input: string s
#output: string res
res = s^2

max_stars_repo_name: datagrok-ai/packages

max_issues_repo_name: datagrok-ai/packages

max_forks_repo_name: datagrok-ai/packages


text:
using DiffEqProblemLibrary, StochasticDiffEq, DiffEqDevTools
using DiffEqProblemLibrary.SDEProblemLibrary: importsdeproblems; importsdeproblems()
import DiffEqProblemLibrary.SDEProblemLibrary: prob_sde_stiffquadito
using Plots; gr()
const N = 10

prob = remake(prob_sde_stiffquadito,p=(50.0,1.0))
sol = solve(prob,SRIW1())
plot(sol)

prob = remake(prob_sde_stiffquadito,p=(500.0,1.0))
sol = solve(prob,SRIW1())
plot(sol)

prob = remake(prob_sde_stiffquadito,p=(50.0,1.0))
@time sol = solve(prob,SRIW1())
@time sol = solve(prob,SRIW1(),adaptive=false,dt=0.01)
@time sol = solve(prob,ImplicitRKMil(),dt=0.005)
@time sol = solve(prob,EM(),dt=0.01);

prob = remake(prob_sde_stiffquadito,p=(500.0,1.0))
@time sol = solve(prob,SRIW1())
@time sol = solve(prob,SRIW1(),adaptive=false,dt=0.002)
@time sol = solve(prob,ImplicitRKMil(),dt=0.001)
@time sol = solve(prob,EM(),dt=0.002);

prob = remake(prob_sde_stiffquadito,p=(5000.0,70.0))
@time sol = solve(prob,SRIW1(),dt=0.0001)
@time sol = solve(prob,SRIW1(),adaptive=false,dt=0.00001)
@time sol = solve(prob,ImplicitRKMil(),dt=0.00001)
@time sol = solve(prob,EM(),dt=0.00001);

prob = remake(prob_sde_stiffquadito,p=(50.0,1.0))

reltols = 1.0 ./ 10.0 .^ (3:5)
abstols = reltols#[0.0 for i in eachindex(reltols)]
setups = [Dict(:alg=>SRIW1()),
          Dict(:alg=>EM(),:dts=>1.0./8.0.^((1:length(reltols)) .+ 1)),
          Dict(:alg=>SRIW1(),:dts=>1.0./8.0.^((1:length(reltols)) .+ 1),:adaptive=>false)
          #Dict(:alg=>RKMil(),:dts=>1.0./8.0.^((1:length(reltols)) .+ 1),:adaptive=>false),
          ]
names = ["SRIW1","EM","SRIW1 Fixed"] #"RKMil",
wp = WorkPrecisionSet(prob,abstols,reltols,setups;numruns=N,names=names,error_estimate=:l2)
plot(wp)

prob = remake(prob_sde_stiffquadito,p=(500.0,1.0))

reltols = 1.0 ./ 10.0 .^ (3:5)
abstols = reltols#[0.0 for i in eachindex(reltols)]
setups = [Dict(:alg=>SRIW1()),
          Dict(:alg=>EM(),:dts=>1.0./8.0.^((1:length(reltols)) .+ 2)),
          Dict(:alg=>SRIW1(),:dts=>1.0./8.0.^((1:length(reltols)) .+ 2),:adaptive=>false)
          #Dict(:alg=>RKMil(),:dts=>1.0./8.0.^((1:length(reltols)) .+ 2),:adaptive=>false),
          ]
names = ["SRIW1","EM","SRIW1 Fixed"] #"RKMil",
wp = WorkPrecisionSet(prob,abstols,reltols,setups;numruns=N,names=names,error_estimate=:l2,print_names=true)
plot(wp)

using DiffEqBenchmarks
DiffEqBenchmarks.bench_footer(WEAVE_ARGS[:folder],WEAVE_ARGS[:file])


text:
module CreateTsvForHeatmap

using CSV
using DataFrames

include("idMap.jl")
include("keyoutputs.jl")
include("pathwayList.jl")

function run(resultsFolder, pathwayListFile, keyOutputsFile, verbose)
    keyoutputs = KeyOutputs.getKeyoutputs(keyOutputsFile)
    pathwayList = PathwayList.getPathwayList(pathwayListFile)

    printHeader = false
    if !isdir(resultsFolder)
       println("$resultsFolder is not a directory")
    else
       mergedResultsFile = "$(resultsFolder)keyoutput-results.tsv"
       mergedResultsPathwayLevelFile = "$(resultsFolder)pathway-level-results.tsv"

       outfile = open(mergedResultsFile, "w")
       outPathwayFile = open(mergedResultsPathwayLevelFile, "w")

       if verbose
           println("creating: $mergedResultsFile and $mergedResultsPathwayLevelFile")
       end

       pathwaysFolder= joinpath(resultsFolder, "pathways")
       dirContents = readdir(pathwaysFolder)
       println("Run Directory:")
       println(dirContents)
       println("pathways:")
       println(pathwaysFolder)
       columns = ()
       for pathwayName in dirContents
           path = "$pathwaysFolder/$pathwayName"
           if verbose
               println("Processing: $path")
           end
           if isdir(path)
               pathwayId = get(pathwayList[pathwayName][:pathway_id])
               colour = get(pathwayList[pathwayName][:colour])
               shortName = get(pathwayList[pathwayName][:short_name])
               resultsFile = joinpath(path, "results.tsv")
               df = CSV.read(resultsFile,
                             delim="\t",
                             datarow=2,
			     types=Dict(1=>String),
                             quotechar="\\",
                             nullable=false)

               if printHeader == false
                  printHeader = true
                  columns = names(df)
                  write(outfile, "nodes\t")
                  for col in columns
                      if col != :node
                         write(outfile, "$col\t")
                      end
                  end
                  write(outfile, "pathway_label\n")

                  write(outPathwayFile, "nodes\t")
                  for col in columns
                      if col != :node
                         write(outPathwayFile, "$col\t")
                      end
                  end
                  write(outPathwayFile, "pathway_label\n")
               end

               fivePercent = 0.05 * size(columns,1)

               averageValues = []
               for row in eachrow(df)
                  nodeId = string(row[:node])
                  if haskey(keyoutputs, pathwayName) && haskey(keyoutputs[pathwayName], nodeId)
                      countChanged = 0
                      for col in columns
                          if col != :node
                             if row[col] != 1
                                 countChanged += 1
                             end
                          end
                      end
                      if countChanged >= fivePercent
                          for col in columns
                              if col == :node
                                  write(outfile, "$(pathwayId)-$(nodeId)\t")
                              else
                                  value = row[col]
                                  write(outfile, "$value\t")
                              end
                          end
                          write(outfile, "$shortName\n")
                          push!(averageValues, row)
                      end
                  end
               end

               numElements = length(averageValues)

               if numElements > 0
                   write(outPathwayFile, "$(pathwayId)\t")
                   for col in columns
                      if col != :node
                          sum = 0
                          for row in averageValues
                              sum = sum + log(10, row[Symbol(col)])
                          end
                          average = 10 ^ (sum / numElements)
                          write(outPathwayFile, "$average\t")
                      end
                   end
                   write(outPathwayFile, "$shortName\n")
               end
           end
        end
    end
end

end

max_stars_repo_name: OICR/mp-biopath

max_issues_repo_name: OICR/mp-biopath

max_forks_repo_name: OICR/mp-biopath


text:
# This file is a part of JuliaFEM.
# License is MIT: see https://github.com/JuliaFEM/Mortar2D.jl/blob/master/LICENSE

using Documenter

deploydocs(
    repo = "github.com/JuliaFEM/Mortar2D.jl.git",
    julia = "0.6",
    target = "build",
    deps = nothing,
    make = nothing)


text:
@testset "Distances" begin
  d = Aitchison()
  c = rand(Composition{5}, 100)

  # test with compositions
  @test d(c[1], c[2]) ≈ distance(c[1], c[2])
  @test pairwise(d, c) ≈ [distance(c[i], c[j]) for i in 1:100, j in 1:100]

  # test with raw vectors
  x, y = rand(3), rand(3)
  @test d(x, y) ≈ d(Composition(x), Composition(y))
end


text:
module ExamplesTest

  using Catlab
	using Catlab.Present
  using Catlab.Programs
	using CombinatorialSpaces
  using Catlab.WiringDiagrams
	using CombinatorialSpaces.ExteriorCalculus
  using Test

	using Decapodes.Examples

  rules = gen_dec_rules()
  for v in values(rules)
    @test v isa WiringDiagram
  end

end


text:
############################################################################################
function init(
    ::Type{HMC},
    config::ConfigHMC,
    objective::Objective,
    difftune::AbstractDifferentiableTune,
)
    return HMC(
        ModelWrappers.log_density_and_gradient(objective, difftune),
        difftune,
        config.energy,
        StepNumberTune(
            config.stepnumber.stepnumberadaption, config.stepnumber.steps, config.stepnumber.maxsteps, config.stepnumber.∫dt
        ),
    )
end

function infer(
    _rng::Random.AbstractRNG,
    diagnostics::Type{AbstractDiagnostics},
    kernel::HMC,
    model::ModelWrapper,
    data::D,
) where {D}
    return DiagnosticsHMC{model.info.reconstruct.default.output}
end
############################################################################################
#export
export init, infer


text:
# Script to replicate figure 7

using DataFrames, StatsBase, Statistics, LinearAlgebra, XLSX, StatsPlots

# -------------- Previous scripts -----------------------------------------------------------------------------------------------------------------------

include("src/data_wrangling.jl") # formats the raw input data obtained from WIOD

# -------------- Sectoral aggregation for country level I/O-Table ---------------------------------------------------------------------------------------

# the function "country_level_head_reis_index" uses the output from the function "obtain_matrices"
# it prepares the data to replicate figure 7 by aggregating the I/O table from a country-sector level to a country level I/O-Table
# and calculates the Head-Reis index (trade costs) for intermediate and final goods
# outputs:
#

function country_level_head_reis_index(df::DataFrame, year::Int64)

    # beginning of the function is the same as in "figure_2_3.jl"

    ID, FD, GO, VA, IV, other = obtain_matrices(df, year) # N*S×N*S, N*S×N, N*S×1, N*S×1, N*S×N, N*S×1

    # aggregate to country level
    ID_ctry = [sum(ID[i:i+n_sectors-1, j:j+n_sectors-1]) for i in 1:n_sectors:n_sectors*n_ctrys, j in 1:n_sectors:n_sectors*n_ctrys] # N×N
    FD_ctry = [sum(FD[i:i+n_sectors-1, j]) for i in 1:n_sectors:n_sectors*n_ctrys, j in 1:n_ctrys] # N×N, sum over origin sector
    IV_ctry = [sum(IV[i:i+n_sectors-1, j]) for i in 1:n_sectors:n_sectors*n_ctrys, j in 1:n_ctrys] # N×N, sum over origin sector
    GO_ctry = [sum(GO[i:i+n_sectors-1]) for i in 1:n_sectors:n_sectors*n_ctrys] # N×1, sum over destination countries
    VA_ctry = [sum(VA[i:i+n_sectors-1]) for i in 1:n_sectors:n_sectors*n_ctrys] # N×1

    # net inventory correction
    iv_correction = [GO_ctry[i]/(GO_ctry[i]-IV_ctry[i, j]) for i in 1:n_ctrys, j in 1:n_ctrys] # N×N, to match country wise inventory changes

    ID_ctry = ID_ctry .* iv_correction # N×N, element wise multiplication
    FD_ctry = FD_ctry .* iv_correction # N×N

    # new section of function starts

    # footnote 22 on page 21: substitute 1e-18 for any zero entries otherwise trade costs explode to infinity!
    replace!(x -> x == 0.0 ? 1e-18 : x, ID_ctry)
    replace!(x -> x == 0.0 ? 1e-18 : x, FD_ctry)

    # claculate Head-Reis index
    θ = 5 # assumption on trade elasticity
    τ_ID = [((ID_ctry[i, j] * ID_ctry[j, i]) / (ID_ctry[i, i] * ID_ctry[j, j]))^(-1/(2θ)) for i in 1:n_ctrys, j in 1:n_ctrys] # bilateral trade costs
    τ_FD = [((FD_ctry[i, j] * FD_ctry[j, i]) / (FD_ctry[i, i] * FD_ctry[j, j]))^(-1/(2θ)) for i in 1:n_ctrys, j in 1:n_ctrys]

    # simple average over all country pairs where i<j => upper triangular matrix without within country trade costs (i.e. diagonal = 1.0)
    τ_avg_id = filter(x -> x != 1.0 && x != 0.0, UpperTriangular(τ_ID)) # filter out 1.0 (within country trade costs) & 0.0 (lower triangular matrix)
    τ_avg_fd = filter(x -> x != 1.0 && x != 0.0, UpperTriangular(τ_FD))

    # footnote 23 on page 21: drop largest 1% of values
    sort!(τ_avg_id)
    sort!(τ_avg_fd)
    τ_avg_id = mean(τ_avg_id[1:end-10]) # use 10/820 instead of 1%
    τ_avg_fd = mean(τ_avg_fd[1:end-10])

    return τ_avg_id, τ_avg_fd
end

t_fig_7 = DataFrame(year=Int[], τ_ID=Float64[], τ_FD=Float64[])

for i in years
    τ_avg_id, τ_avg_fd = country_level_head_reis_index(df, i)
    push!(t_fig_7, [i τ_avg_id τ_avg_fd])
end

XLSX.writetable("clean/t_fig_7.xlsx", t_fig_7, overwrite=true) # export table to folder as input for plotting figure 7

# -------------- Figure 7 -------------------------------------------------------------------------------------------------------------------

t_fig_7 = DataFrame(XLSX.readtable("clean/t_fig_7.xlsx", "Sheet1")...) # load directly from file
transform!(t_fig_7, :year .=> ByRow(Int) .=> :year, renamecols=false) # change type to Int64
transform!(t_fig_7, [:τ_ID, :τ_FD] .=> ByRow(Float64) .=> [:τ_ID, :τ_FD], renamecols=false) # change type to Float64

p_fig7 = plot(t_fig_7.year, [t_fig_7.τ_ID t_fig_7.τ_FD], xlabel="Year", ylabel="Iceberg τ", title="Figure 7: Head-Reis τ's (country level) over time",
                label=["Input-use mean τ" "Final-use mean τ"], ylims=(2.45,5.05), leg=:bottomleft, lw=2)

savefig(p_fig7, "images/figure7.png") # export image to folder


text:
module Floquet
using ..Hop, LinearAlgebra, QuadGK

export FloquetHamiltonian, get_floquet_occupation, get_illuminated_hamiltonian, get_illuminated_band

struct FloquetHamiltonian
    Hs::Vector{Matrix{ComplexF64}}
    Hf::Matrix{ComplexF64}
    Ω::Float64
    nstates::Int64
    nfstates::Int64
    harmonics_cutoff::Int64
end

function Base.show(io::IO, f::FloquetHamiltonian)
    print(io, "FloquetHamiltonian: $(f.nstates) states with $(2*f.harmonics_cutoff+1) harmonics.")
end

"""
FloquetHamiltonian(Hs::Vector{Matrix{T}}, Ω::Real, harmonics_cutoff::Integer) where T<:Number
```
Floquet Hamiltonian.

Generally, Floquet theory deals with time periodic
Hamiltonians ``H(t+Ω)=H(t)``.

# Constructor Arguments

 - `Hs` is a list of Hamiltonian looking like ``[H_0, H_1, H_2, ...]``.
   ``H_n`` is defined as: ``H_n=\\frac{1}{T}\\int_0^T e^{inΩt} H(t) dt``.
 - `Ω` is the frequency (provided in eV) of the Floquet system.
 - `harmonics_cutoff` determines how many harmonics are used in Floquet Hamiltonian

# Fields
 - `Hs::Vector{Matrix{ComplexF64}}`, see above explanations.
 - `Hf::Matrix{ComplexF64}` is Floquet Hamiltonian.
   With basis functions defined as ``u_n=e^{-inΩt}``, ``H^F_{mn}=H_{m-n}-mΩδ_{mn}``.
   The basis of returned Floquet Hamiltonian is
   ``(e^{iNΩt}, e^{i(N-1)Ωt}, ..., e^{-iNΩt})``
   where internal states are omitted.
 - `Ω::Float64`, see above explanations.
 - `nstates::Int64` is number of internal states.
 - `nfstates::Int64` is number of Floquet states.
 - `harmonics_cutoff::Int64`, see above explanations.
"""
function FloquetHamiltonian(Hs::Vector{Matrix{T}}, Ω::Real, harmonics_cutoff::Integer) where T<:Number
    nstates = size(Hs[1], 1)
    nfstates = nstates*(2*harmonics_cutoff+1)
    nhams = length(Hs)
    Hf = zeros(ComplexF64, (nfstates, nfstates))
    for j = 0:(nhams-1)
        for i = 1:(2harmonics_cutoff+1-j)
            if j == 0
                Hf[nstates*(i-1)+1:nstates*i, nstates*(i-1)+1:nstates*i] = Hs[1]-(i-harmonics_cutoff-1)*Ω*I
            else
                Hf[nstates*(i-1)+1:nstates*i, nstates*(i-1+j)+1:nstates*(i+j)] = Hs[j+1]'
                Hf[nstates*(i-1+j)+1:nstates*(i+j), nstates*(i-1)+1:nstates*i] = Hs[j+1]
            end
        end
    end
    @assert ishermitian(Hf)
    return FloquetHamiltonian(Hs, Hf, Ω, nstates, nfstates, harmonics_cutoff)
end

function get_floquet_GR(f::FloquetHamiltonian, ω::Real; Γ::Real=1.0)
    return inv((ω+im*Γ/2)*I-f.Hf)
end

function get_floquet_Σless(f::FloquetHamiltonian, ω::Real; Γ::Real=1.0, μ::Real=1.0)
    Σ = zeros(ComplexF64, size(f.Hf))
    for i=1:(2*f.harmonics_cutoff+1)
        Σ[(i-1)*f.nstates+1:i*f.nstates, (i-1)*f.nstates+1:i*f.nstates] =
            im*Γ*fermi(ω+(i-f.harmonics_cutoff-1)*f.Ω, μ)*Matrix(1.0I, f.nstates, f.nstates)
    end
    return Σ
end

function get_floquet_Gless(f::FloquetHamiltonian, ω::Real; Γ::Real=1.0, μ::Real=1.0)
    GR = get_floquet_GR(f, ω, Γ=Γ)
    Σless = get_floquet_Σless(f, ω, Γ=Γ, μ=μ)
    return GR*Σless*(GR')
end

"""
```julia
get_floquet_occupation(f::FloquetHamiltonian; Γ::Real=1.0, μ::Real=0.0,
    atol::Real=1.0e-3) --> Vector{Float64}
```

Calculate occupation number of `f` assuming coupling to a heat bath with coupling
constant `Γ`. Chemical potential of the heat bath is `μ`. `atol` is the integration
absolute tolerance.
"""
function get_floquet_occupation(f::FloquetHamiltonian; Γ::Real=1.0, μ::Real=0.0,
    atol::Real=1.0e-3)
    _, egvecs = eigen(f.Hf)
    occs = zeros(f.nstates)
    for i=(f.harmonics_cutoff*f.nstates+1):(f.harmonics_cutoff+1)*f.nstates
        occ = -im*quadgk(
        ω->egvecs[:, i]'*get_floquet_Gless(f, ω, Γ=Γ, μ=μ)*egvecs[:, i],
        -Inf, Inf, atol=atol)[1]/(2π)
        @assert imag(occ) < 1.0e-6
        occs[i-f.harmonics_cutoff*f.nstates] = real(occ)
    end
    return occs
end

"""
```julia
get_illuminated_hamiltonian(t::TightBindingModel, k::Vector{<:Real};
    A::Vector{<:Number}, Ω::Real, harmonics_cutoff::Integer=2)
    --> FloquetHamiltonian
```

Calculate Floquet Hamiltonian of `t` at `k` point under the illumination of light with frequency Ω.
Peierls substitution is performed to the lowest order. Only electric field is taken
into account. The parameter `A` looks like ``[A_x, A_y, A_z]`` denoting vector potential
``A(t)=[A_x, A_y, A_z]e^{-iΩt}+c.c.``. Floquet Hamiltonian is truncated
up to `harmonics_cutoff` harmonics.
"""
function  get_illuminated_hamiltonian(t::TightBindingModel, k::Vector{<:Real};
    A::Vector{<:Number}, Ω::Real, harmonics_cutoff::Integer=2)
    @assert length(A) == 3 "Length of A is not correct."

    H0 = gethamiltonian(t, k)

    # Unfortunately we cannot calculate Hamiltonian by gethamiltonian since
    # H1 is not necessarily Hermitian.
    cpositions = t.lat*t.positions # Cartesian positions
    H1 = zeros(ComplexF64, size(H0))
    for (R, hopping) = t.hoppings
        cR = t.lat*R
        Peierlshopping = zeros(ComplexF64, size(H0))
        for n = 1:t.norbits
            for m = 1:t.norbits
                Peierlshopping[m, n] = im*hopping[m, n]*((cpositions[:, n]-cpositions[:, m]+cR)⋅A)
            end
        end
        H1 += exp(2π*im*(k⋅R))*Peierlshopping
    end

    return FloquetHamiltonian([H0, H1], Ω, harmonics_cutoff)
end

"""
```julia
function get_illuminated_band(t::TightBindingModel; A::Vector{<:Number}, Ω::Real,
    kp::KPath, harmonics_cutoff::Integer=2) --> Matrix{Float64}
```

Calculate bands of `t` on `kp` under the illumination of light with frequency Ω.
Peierls substitution is performed to the lowest order. Only electric field is taken
into account. The parameter `A` looks like ``[A_x, A_y, A_z]`` denoting vector potential
``A(t)=[A_x, A_y, A_z]e^{-iΩt}+c.c.``. Floquet Hamiltonian is truncated
up to `harmonics_cutoff` harmonics.

This function returns Floquet band energies at each k stored in column.
"""
function get_illuminated_band(t::TightBindingModel; A::Vector{<:Number}, Ω::Real,
    kp::KPath, harmonics_cutoff::Integer=2)
    bands = zeros(t.norbits*(2harmonics_cutoff+1), kp.nkpts)
    for ik=1:kp.nkpts
        bands[:, ik] = eigvals(
            get_illuminated_hamiltonian(t, kp.kpts[:, ik], A=A, Ω=Ω, harmonics_cutoff=harmonics_cutoff).Hf
        )
    end
    return bands
end

function get_illuminated_occupation(t::TightBindingModel, kp::KPath;
    Γ::Real, μ::Real, A::Vector{<:Number}, Ω::Real, harmonics_cutoff::Integer, atol::Real=1.0e-3)
    occs = zeros((t.norbits, kp.nkpts))
    for ik=1:kp.nkpts
        f = get_illuminated_hamiltonian(t, kp.kpts[:, ik], A=A, Ω=Ω, harmonics_cutoff=harmonics_cutoff)
        occs[:, ik] = get_floquet_occupation(f; μ=μ, Γ=Γ, atol=atol)
    end
    return occs
end

fermi(E, μ; T=0) = E<μ ? 1.0 : 0.0

end


text:
"""
    intersect_segment_circle(start, end, circle, fudge=0.5)

Test whether a line segment and circle intersect.

# Arguments
- `start::Entity`: The start of the line segment. (Needs x, y attributes)
- `end::Entity`: The end of the line segment. (Needs x, y attributes)
- `circle::Entity`: The circle to test against. (Needs x, y, r attributes)
- `fudge::Entity`: A fudge factor; additional distance to leave between the segment and circle. (Probably set this to the ship radius, 0.5.)
"""
function intersect_segment_circle(startl::Entity, endl::Entity, circle::Entity, fudge=0.5)
    # Derived with SymPy
    # Parameterize the segment as start + t * (end - start),
    # and substitute into the equation of a circle
    # Solve for t
    dx = endl.x - startl.x
    dy = endl.y - startl.y

    a = dx^2 + dy^2
    b = -2.0 * (startl.x^2 - startl.x*endl.x - startl.x*circle.x + endl.x*circle.x +
                startl.y^2 - startl.y*endl.y - startl.y*circle.y + endl.y*circle.y)
    c = (startl.x - circle.x)^2 + (startl.y - circle.y)^2

    if a < 1e-6
        # Start and end are the same point
        return calculate_distance_between(startl, circle) <= radius(circle) + fudge
    end

    # Time along segment when closest to the circle (vertex of the quadratic)
    t = min(-b / (2 * a), 1.0)
    if t < 0
        return false
    end

    closest_x = startl.x + dx * t
    closest_y = startl.y + dy * t
    closest_distance = calculate_distance_between(Position(closest_x, closest_y), circle)

    return closest_distance <= radius(circle) + fudge
end

max_stars_repo_name: xinnosuke/Halite-II

max_issues_repo_name: xinnosuke/Halite-II

max_forks_repo_name: xinnosuke/Halite-II


text:
"""
    bvp1.py solves the 1D Poisson equation using FEniCS
    Copyright (C) 2013  Greg von Winckel
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   Solve the 1D Poisson equation -u''=-1 with u(0)=0, u'(1)=1
   The exact solution is x^2/2
"""
module bvp1

using FenicsPy

# Number of elements
nel = 20

# Left end point
xmin = 0

# Right end point
xmax = 1

# Polynomial order of trial/test functions
p = 2

# Create mesh and function space
mesh = IntervalMesh(nel,xmin,xmax)

# Define function space for this mesh using Continuous Galerkin
# (Lagrange) functions of order p on each element
V = FunctionSpace(mesh,"CG",p)

# Define boundary boundary values
u0 = Expression("x[0]", degree=1)

# This imposes a Dirichlet condition at the point x=0
dirichlet_boundary = "on_boundary && abs(x[0])<1e-14"

# Enforce u=u0 at x=0
bc = DirichletBC(V,u0,dirichlet_boundary)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-1)
g = Constant(1)
a = inner(grad(u),grad(v))*dx
L = f*v*dx+g*v*ds

# Compute solution
u = FeFunction(V)
solve(a == L, u, bc)

# plot solution
#plot(u)

# Dump solution to file in VTK format
file = File("bvp1/bvp1.pvd")
file << u

end # module bvp1


text:
export patch, @patch, Patcher

type Patcher
  mod::Any
  name::Union(Expr,Symbol)
  new::Any

  Patcher(mod::Any, name::Symbol, new::Any) = new(mod, name, new)
end

function patch(fn::Function, patchers::Array{Patcher}; i=1)

  if i > length(patchers)
    fn()
  else
    patch(patchers[i].mod, patchers[i].name, patchers[i].new) do
      patch(fn, patchers, i=i+1)
    end
  end

end

function patch(fn::Function, mod::Module, name::Symbol, new::Function)
  const old = mod.eval(name)
  if isgeneric(old) && isconst(mod, name)
    if !isgeneric(new)
      patch(old, :env, nothing) do
        patch(old, :fptr, new.fptr) do
          old.code=new.code
          return fn()
        end
      end
    else
      patchimp(fn, mod, name, new)
    end
  else
    return patchimpl(fn, mod, name, new)
  end
end

function patchimp(fn::Function, mod::Module, name::Symbol, new::Function)
  old = mod.eval(name)
  oldenv = old.env
  oldfptr = old.fptr
  old.env = new.env
  old.fptr = new.fptr
  try
    fn()
  finally
    old.env = oldenv
    old.fptr = oldfptr
  end
end

patch(fn::Function, obj::Any, name::Symbol, new::Any) = patchimpl(fn, Core, :($obj.$name), new)

patch(fn::Function, mod::Module, name::Symbol, new::Any) = patchimpl(fn, mod, name, new)

function patchimpl(fn::Function, mod::Module, name::Union(Expr,Symbol), new::Any)
  const old = mod.eval(name)
  mod.eval(:($name = $new))
  try
    fn()
  finally
    mod.eval(:($name = $old))
  end
end

macro patch(expr::Expr, name::Symbol, new::Any)
  quote
    const old = $(esc(name))
    try
      $(esc(name))=$(esc(new))
      $(esc(expr))()
    finally
      $(esc(name))=old
    end
  end
end


text:
############################################################################
# Initial arrays
############################################################################

global UA=zeros(vleva); global UA1=zeros(vleva); global UAst=zeros(vleva);
global qA=zeros(vleva); global qA1=zeros(vleva); global qAst=zeros(vleva);
global ΘA=zeros(vleva); global ΘA1=zeros(vleva); global ΘAst=zeros(vleva);
global ρA=zeros(vleva);
global TA=zeros(vleva);
global PA=zeros(vleva);

global qS=0.01

############################################################################
# Initial conditions
############################################################################
# atmospheric temperature in DegC
TA=25 .-0.007.*ZAC;
PA[1]=101300;
PA[2:vleva]=PA[1].*exp.(-cumsum(ΔZA./(Rgas.*(TA[2:end].+d2k)./gravity_mks)));
ρA=PA./(Rgas.*(TA.+d2k));
# atmospheric potential temperature in kelvin
ΘA[:]=(TA.+d2k).*(PA[1]./PA).^(2/7);
ΘA0=ΘA[end].+1;
RH=0.8
qA=saltsat*cvapor_fac*exp.(-cvapor_exp./(TA.+d2k))./ρA.*RH;
#println("qA0")
#println(qA)

UA=(0.2/karman).*(log.(ZAC./1e-4));

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

global LH=zeros(1); #latent heat flux
global SH=zeros(1); #sensible heat flux
global TAU=zeros(1); # wind stress
global LW=zeros(1); # net longwave
global SW=zeros(1); # net shortwavwe
global Qnet=zeros(1); # net heat flux
global MOL=zeros(1); # monin obokuv length
global E=zeros(1); # Evaporation
global KAm=zeros(vleva); # verical mixing of momntum
global KAt=zeros(vleva); # verical mixing of scalar
global γct=zeros(vleva);
global γcq=zeros(vleva);
global γcm=zeros(vleva);

global ustar=0.01;
FWflux=zeros(size(Qnet));
ρO=1000
q0=1.01*qA[1]
ΘA0=300

############################################################################
# SOIL
############################################################################

#initial conditions
T_initial = 273+25;
h_initial = -1;

global head=h_initial*ones(soil_numerical_parameters.Ns,1)
global TS=T_initial*ones(soil_numerical_parameters.Ns,1)
global q_l=zeros(soil_numerical_parameters.Ns,1)
global q_v=zeros(soil_numerical_parameters.Ns,1)
global rho_vs=zeros(soil_numerical_parameters.Ns,1)

Cp_soil=zeros(soil_numerical_parameters.Ns,1)

struct shydraulic{R}
  C::R
  K::R
  K_vh::R
  K_h::R
  K_lT::R
  K_vT::R
  K_T::R
end

theta_l, theta_v= hydraulic_variables(head, TS, constants, soil_parameters);
Cp_soil = constants.Cs.*(soil_parameters.theta_s.-theta_l).+constants.Cw.*theta_l+constants.Cv.*theta_v;


text:
@testset "896.monotonic-array.jl" begin
    @test is_monotonic([1,2,2,3]) == true
    @test is_monotonic([6,5,4,4]) == true
    @test is_monotonic([1,3,2]) == false
    @test is_monotonic([1,2,4,5]) == true
    @test is_monotonic([1,1,1]) == true
end


text:
export CircularArraySARTSTrajectory,
    InitStage,
    PreTrainStage,
    INIT_STAGE,
    PRETRAIN_STAGE,
    record!

using ReinforcementLearning
using StatsBase
using CircularArrayBuffers
using Random

#A few extensions to JuliaReinforcementLearningCore that were useful

const CircularArraySARTSTrajectory = Trajectory{
    <:NamedTuple{
        SARTS,
        <:Tuple{
            <:CircularArrayBuffer,
            <:CircularArrayBuffer,
            <:CircularArrayBuffer,
            <:CircularArrayBuffer,
            <:CircularArrayBuffer,
        },
    },
}

CircularArraySARTSTrajectory(;
    capacity::Int,
    state = Int => (),
    action = Int => (),
    reward = Float32 => (),
    terminal = Bool => (),
    next_state = Int => (),
) = CircularArrayTrajectory(;
    capacity = capacity,
    state = state,
    action = action,
    reward = reward,
    terminal = terminal,
    next_state = next_state,
)

Base.length(t::CircularArraySARTSTrajectory) = length(t[:terminal])

function fetch!(
    s::BatchSampler{SARTS},
    t::CircularArraySARTSTrajectory,
    inds::Vector{Int}
)
    batch = NamedTuple{SARTS}(
        (
            (consecutive_view(t[x], inds) for x in SARTS)...,
        )
    )
    if isnothing(s.cache)
        s.cache = map(batch) do x
            convert(Array, x)
        end
    else
        map(s.cache, batch) do dest, src
            copyto!(dest, src)
        end
    end
end

function StatsBase.sample(rng::AbstractRNG, t::CircularArraySARTSTrajectory, s::BatchSampler)
    inds = rand(rng, 1:length(t), s.batch_size)
    fetch!(s, t, inds)
    inds, s.cache
end

struct InitStage <: AbstractStage end
const INIT_STAGE = InitStage()

struct PreTrainStage <: AbstractStage end
const PRETRAIN_STAGE = PreTrainStage()

function record!(t::AbstractTrajectory, experience::NamedTuple)
    for key in keys(t)
        if haskey(experience, key)
            push!(t[key], experience[key])
        else
            throw(ArgumentError("Missing key in experience: " * key))
        end
    end
end
nothing

max_stars_repo_name: polhager/FurutaRL

max_issues_repo_name: polhager/FurutaRL

max_forks_repo_name: polhager/FurutaRL


text:
abstract type AbstractEvent end

struct NoEvent <: AbstractEvent
end

struct Event{T <: EpidemicModel} <: AbstractEvent
  time::Float64
  individual::Int64
  new_state::DiseaseState

  function Event{T}(time::Float64, id::Int64, new_state::DiseaseState) where T <: EpidemicModel
    if new_state in _state_progressions[T]
      return new{T}(time, id, new_state)
    else
      @error "Invalid disease state provided for $(T) Epidemic Models."
    end
  end

  function Event(new_time::Float64, event::Event{T}) where T <: EpidemicModel
    return new{T}(new_time, event.individual, event.new_state)
  end
end

function _time(x::Event{T}) where T <: EpidemicModel
  return x.time
end

function _time(x::NoEvent)
  return Inf
end

# function Base.copy(x::Event{T}) where T <: EpidemicModel
#   return Event{T}(copy(x.time), copy(x.id), copy(x.new_state))
# end

function _new_transmission(e::Event{T}) where T <: Union{SEIR, SEI}
  return e.new_state == State_E
end

function _new_transmission(e::Event{T}) where T <: Union{SIR, SI}
  return e.new_state == State_I
end

function _individual(x::Event{T}) where T <: EpidemicModel
  return x.individual
end

function _new_state(x::Event{T}) where T <: EpidemicModel
  return x.new_state
end

function Base.show(io::IO, x::Event{T}) where T <: EpidemicModel
  return print(io, "Transition of individual $(x.individual) into $(x.new_state) state at t = $(x.time)")
end


text:
using CombinedUncertainDiffEq
using CombinedUncertainDiffEq: Uniform, to_distribution, to_interval
using Test

@testset "Conversions" begin
    i = 1..2
    d = Uniform(1, 2)

    # Individual
    @test to_distribution(i) == d
    @test to_distribution(d) == d
    @test to_interval(i) == i
    @test to_interval(d) == i

    # Round trip
    @test to_interval(to_distribution(i)) == i
    @test to_distribution(to_interval(d)) == d
end


text:
include(joinpath(dirname(@__FILE__), "../src/DBN.jl")
using HDF5, JLD

data_layer = HDF5DataLayer(name="train-data", source="train.txt", batch_size=1024)
source = HDF5DataSource(data_layer)
dbn = DBN([64*11, 2048, 2048, 2048])

fit(dbn, source)

file = jldopen("DBN3.jld", "w")
@write file dbn
close(file)