text:
using ..common
using Compat
using ProtoBuf
import ProtoBuf.meta
import Base: hash, isequal, ==

type __enum_ContainerStateProto <: ProtoEnum
    C_NEW::Int32
    C_RUNNING::Int32
    C_COMPLETE::Int32
    __enum_ContainerStateProto() = new(1,2,3)
end #type __enum_ContainerStateProto
const ContainerStateProto = __enum_ContainerStateProto()

type __enum_YarnApplicationStateProto <: ProtoEnum
    NEW::Int32
    NEW_SAVING::Int32
    SUBMITTED::Int32
    ACCEPTED::Int32
    RUNNING::Int32
    FINISHED::Int32
    FAILED::Int32
    KILLED::Int32
    __enum_YarnApplicationStateProto() = new(1,2,3,4,5,6,7,8)
end #type __enum_YarnApplicationStateProto
const YarnApplicationStateProto = __enum_YarnApplicationStateProto()

type __enum_YarnApplicationAttemptStateProto <: ProtoEnum
    APP_ATTEMPT_NEW::Int32
    APP_ATTEMPT_SUBMITTED::Int32
    APP_ATTEMPT_SCHEDULED::Int32
    APP_ATTEMPT_ALLOCATED_SAVING::Int32
    APP_ATTEMPT_ALLOCATED::Int32
    APP_ATTEMPT_LAUNCHED::Int32
    APP_ATTEMPT_FAILED::Int32
    APP_ATTEMPT_RUNNING::Int32
    APP_ATTEMPT_FINISHING::Int32
    APP_ATTEMPT_FINISHED::Int32
    APP_ATTEMPT_KILLED::Int32
    __enum_YarnApplicationAttemptStateProto() = new(1,2,3,4,5,6,7,8,9,10,11)
end #type __enum_YarnApplicationAttemptStateProto
const YarnApplicationAttemptStateProto = __enum_YarnApplicationAttemptStateProto()

type __enum_FinalApplicationStatusProto <: ProtoEnum
    APP_UNDEFINED::Int32
    APP_SUCCEEDED::Int32
    APP_FAILED::Int32
    APP_KILLED::Int32
    __enum_FinalApplicationStatusProto() = new(0,1,2,3)
end #type __enum_FinalApplicationStatusProto
const FinalApplicationStatusProto = __enum_FinalApplicationStatusProto()

type __enum_LocalResourceVisibilityProto <: ProtoEnum
    PUBLIC::Int32
    PRIVATE::Int32
    APPLICATION::Int32
    __enum_LocalResourceVisibilityProto() = new(1,2,3)
end #type __enum_LocalResourceVisibilityProto
const LocalResourceVisibilityProto = __enum_LocalResourceVisibilityProto()

type __enum_LocalResourceTypeProto <: ProtoEnum
    ARCHIVE::Int32
    FILE::Int32
    PATTERN::Int32
    __enum_LocalResourceTypeProto() = new(1,2,3)
end #type __enum_LocalResourceTypeProto
const LocalResourceTypeProto = __enum_LocalResourceTypeProto()

type __enum_NodeStateProto <: ProtoEnum
    NS_NEW::Int32
    NS_RUNNING::Int32
    NS_UNHEALTHY::Int32
    NS_DECOMMISSIONED::Int32
    NS_LOST::Int32
    NS_REBOOTED::Int32
    __enum_NodeStateProto() = new(1,2,3,4,5,6)
end #type __enum_NodeStateProto
const NodeStateProto = __enum_NodeStateProto()

type __enum_AMCommandProto <: ProtoEnum
    AM_RESYNC::Int32
    AM_SHUTDOWN::Int32
    __enum_AMCommandProto() = new(1,2)
end #type __enum_AMCommandProto
const AMCommandProto = __enum_AMCommandProto()

type __enum_ApplicationAccessTypeProto <: ProtoEnum
    APPACCESS_VIEW_APP::Int32
    APPACCESS_MODIFY_APP::Int32
    __enum_ApplicationAccessTypeProto() = new(1,2)
end #type __enum_ApplicationAccessTypeProto
const ApplicationAccessTypeProto = __enum_ApplicationAccessTypeProto()

type __enum_QueueStateProto <: ProtoEnum
    Q_STOPPED::Int32
    Q_RUNNING::Int32
    __enum_QueueStateProto() = new(1,2)
end #type __enum_QueueStateProto
const QueueStateProto = __enum_QueueStateProto()

type __enum_QueueACLProto <: ProtoEnum
    QACL_SUBMIT_APPLICATIONS::Int32
    QACL_ADMINISTER_QUEUE::Int32
    __enum_QueueACLProto() = new(1,2)
end #type __enum_QueueACLProto
const QueueACLProto = __enum_QueueACLProto()

type __enum_ReservationRequestInterpreterProto <: ProtoEnum
    R_ANY::Int32
    R_ALL::Int32
    R_ORDER::Int32
    R_ORDER_NO_GAP::Int32
    __enum_ReservationRequestInterpreterProto() = new(0,1,2,3)
end #type __enum_ReservationRequestInterpreterProto
const ReservationRequestInterpreterProto = __enum_ReservationRequestInterpreterProto()

type __enum_ContainerExitStatusProto <: ProtoEnum
    SUCCESS::Int32
    INVALID::Int32
    ABORTED::Int32
    DISKS_FAILED::Int32
    __enum_ContainerExitStatusProto() = new(0,0,0,0)
end #type __enum_ContainerExitStatusProto
const ContainerExitStatusProto = __enum_ContainerExitStatusProto()

type SerializedExceptionProto
    message::AbstractString
    trace::AbstractString
    class_name::AbstractString
    cause::SerializedExceptionProto
    SerializedExceptionProto() = (o=new(); fillunset(o); o)
end #type SerializedExceptionProto
hash(v::SerializedExceptionProto) = ProtoBuf.protohash(v)
isequal(v1::SerializedExceptionProto, v2::SerializedExceptionProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::SerializedExceptionProto, v2::SerializedExceptionProto) = ProtoBuf.protoeq(v1, v2)

type ApplicationIdProto
    id::Int32
    cluster_timestamp::Int64
    ApplicationIdProto() = (o=new(); fillunset(o); o)
end #type ApplicationIdProto
hash(v::ApplicationIdProto) = ProtoBuf.protohash(v)
isequal(v1::ApplicationIdProto, v2::ApplicationIdProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ApplicationIdProto, v2::ApplicationIdProto) = ProtoBuf.protoeq(v1, v2)

type ApplicationAttemptIdProto
    application_id::ApplicationIdProto
    attemptId::Int32
    ApplicationAttemptIdProto() = (o=new(); fillunset(o); o)
end #type ApplicationAttemptIdProto
hash(v::ApplicationAttemptIdProto) = ProtoBuf.protohash(v)
isequal(v1::ApplicationAttemptIdProto, v2::ApplicationAttemptIdProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ApplicationAttemptIdProto, v2::ApplicationAttemptIdProto) = ProtoBuf.protoeq(v1, v2)

type ContainerIdProto
    app_id::ApplicationIdProto
    app_attempt_id::ApplicationAttemptIdProto
    id::Int64
    ContainerIdProto() = (o=new(); fillunset(o); o)
end #type ContainerIdProto
hash(v::ContainerIdProto) = ProtoBuf.protohash(v)
isequal(v1::ContainerIdProto, v2::ContainerIdProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ContainerIdProto, v2::ContainerIdProto) = ProtoBuf.protoeq(v1, v2)

type ResourceProto
    memory::Int32
    virtual_cores::Int32
    ResourceProto() = (o=new(); fillunset(o); o)
end #type ResourceProto
hash(v::ResourceProto) = ProtoBuf.protohash(v)
isequal(v1::ResourceProto, v2::ResourceProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ResourceProto, v2::ResourceProto) = ProtoBuf.protoeq(v1, v2)

type ResourceOptionProto
    resource::ResourceProto
    over_commit_timeout::Int32
    ResourceOptionProto() = (o=new(); fillunset(o); o)
end #type ResourceOptionProto
hash(v::ResourceOptionProto) = ProtoBuf.protohash(v)
isequal(v1::ResourceOptionProto, v2::ResourceOptionProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ResourceOptionProto, v2::ResourceOptionProto) = ProtoBuf.protoeq(v1, v2)

type PriorityProto
    priority::Int32
    PriorityProto() = (o=new(); fillunset(o); o)
end #type PriorityProto
hash(v::PriorityProto) = ProtoBuf.protohash(v)
isequal(v1::PriorityProto, v2::PriorityProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::PriorityProto, v2::PriorityProto) = ProtoBuf.protoeq(v1, v2)

type URLProto
    scheme::AbstractString
    host::AbstractString
    port::Int32
    file::AbstractString
    userInfo::AbstractString
    URLProto() = (o=new(); fillunset(o); o)
end #type URLProto
hash(v::URLProto) = ProtoBuf.protohash(v)
isequal(v1::URLProto, v2::URLProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::URLProto, v2::URLProto) = ProtoBuf.protoeq(v1, v2)

type LocalResourceProto
    resource::URLProto
    size::Int64
    timestamp::Int64
    _type::Int32
    visibility::Int32
    pattern::AbstractString
    should_be_uploaded_to_shared_cache::Bool
    LocalResourceProto() = (o=new(); fillunset(o); o)
end #type LocalResourceProto
hash(v::LocalResourceProto) = ProtoBuf.protohash(v)
isequal(v1::LocalResourceProto, v2::LocalResourceProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::LocalResourceProto, v2::LocalResourceProto) = ProtoBuf.protoeq(v1, v2)

type ApplicationResourceUsageReportProto
    num_used_containers::Int32
    num_reserved_containers::Int32
    used_resources::ResourceProto
    reserved_resources::ResourceProto
    needed_resources::ResourceProto
    memory_seconds::Int64
    vcore_seconds::Int64
    ApplicationResourceUsageReportProto() = (o=new(); fillunset(o); o)
end #type ApplicationResourceUsageReportProto
hash(v::ApplicationResourceUsageReportProto) = ProtoBuf.protohash(v)
isequal(v1::ApplicationResourceUsageReportProto, v2::ApplicationResourceUsageReportProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ApplicationResourceUsageReportProto, v2::ApplicationResourceUsageReportProto) = ProtoBuf.protoeq(v1, v2)

type ApplicationReportProto
    applicationId::ApplicationIdProto
    user::AbstractString
    queue::AbstractString
    name::AbstractString
    host::AbstractString
    rpc_port::Int32
    client_to_am_token::TokenProto
    yarn_application_state::Int32
    trackingUrl::AbstractString
    diagnostics::AbstractString
    startTime::Int64
    finishTime::Int64
    final_application_status::Int32
    app_resource_Usage::ApplicationResourceUsageReportProto
    originalTrackingUrl::AbstractString
    currentApplicationAttemptId::ApplicationAttemptIdProto
    progress::Float32
    applicationType::AbstractString
    am_rm_token::TokenProto
    applicationTags::Array{AbstractString,1}
    ApplicationReportProto() = (o=new(); fillunset(o); o)
end #type ApplicationReportProto
const __val_ApplicationReportProto = @compat Dict(:diagnostics => "N/A")
meta(t::Type{ApplicationReportProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_ApplicationReportProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::ApplicationReportProto) = ProtoBuf.protohash(v)
isequal(v1::ApplicationReportProto, v2::ApplicationReportProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ApplicationReportProto, v2::ApplicationReportProto) = ProtoBuf.protoeq(v1, v2)

type ApplicationAttemptReportProto
    application_attempt_id::ApplicationAttemptIdProto
    host::AbstractString
    rpc_port::Int32
    tracking_url::AbstractString
    diagnostics::AbstractString
    yarn_application_attempt_state::Int32
    am_container_id::ContainerIdProto
    original_tracking_url::AbstractString
    ApplicationAttemptReportProto() = (o=new(); fillunset(o); o)
end #type ApplicationAttemptReportProto
const __val_ApplicationAttemptReportProto = @compat Dict(:diagnostics => "N/A")
meta(t::Type{ApplicationAttemptReportProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_ApplicationAttemptReportProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::ApplicationAttemptReportProto) = ProtoBuf.protohash(v)
isequal(v1::ApplicationAttemptReportProto, v2::ApplicationAttemptReportProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ApplicationAttemptReportProto, v2::ApplicationAttemptReportProto) = ProtoBuf.protoeq(v1, v2)

type NodeIdProto
    host::AbstractString
    port::Int32
    NodeIdProto() = (o=new(); fillunset(o); o)
end #type NodeIdProto
hash(v::NodeIdProto) = ProtoBuf.protohash(v)
isequal(v1::NodeIdProto, v2::NodeIdProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::NodeIdProto, v2::NodeIdProto) = ProtoBuf.protoeq(v1, v2)

type NodeResourceMapProto
    node_id::NodeIdProto
    resource_option::ResourceOptionProto
    NodeResourceMapProto() = (o=new(); fillunset(o); o)
end #type NodeResourceMapProto
hash(v::NodeResourceMapProto) = ProtoBuf.protohash(v)
isequal(v1::NodeResourceMapProto, v2::NodeResourceMapProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::NodeResourceMapProto, v2::NodeResourceMapProto) = ProtoBuf.protoeq(v1, v2)

type ContainerReportProto
    container_id::ContainerIdProto
    resource::ResourceProto
    node_id::NodeIdProto
    priority::PriorityProto
    creation_time::Int64
    finish_time::Int64
    diagnostics_info::AbstractString
    log_url::AbstractString
    container_exit_status::Int32
    container_state::Int32
    node_http_address::AbstractString
    ContainerReportProto() = (o=new(); fillunset(o); o)
end #type ContainerReportProto
const __val_ContainerReportProto = @compat Dict(:diagnostics_info => "N/A")
meta(t::Type{ContainerReportProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_ContainerReportProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::ContainerReportProto) = ProtoBuf.protohash(v)
isequal(v1::ContainerReportProto, v2::ContainerReportProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ContainerReportProto, v2::ContainerReportProto) = ProtoBuf.protoeq(v1, v2)

type ContainerProto
    id::ContainerIdProto
    nodeId::NodeIdProto
    node_http_address::AbstractString
    resource::ResourceProto
    priority::PriorityProto
    container_token::TokenProto
    ContainerProto() = (o=new(); fillunset(o); o)
end #type ContainerProto
hash(v::ContainerProto) = ProtoBuf.protohash(v)
isequal(v1::ContainerProto, v2::ContainerProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ContainerProto, v2::ContainerProto) = ProtoBuf.protoeq(v1, v2)

type NodeReportProto
    nodeId::NodeIdProto
    httpAddress::AbstractString
    rackName::AbstractString
    used::ResourceProto
    capability::ResourceProto
    numContainers::Int32
    node_state::Int32
    health_report::AbstractString
    last_health_report_time::Int64
    node_labels::Array{AbstractString,1}
    NodeReportProto() = (o=new(); fillunset(o); o)
end #type NodeReportProto
hash(v::NodeReportProto) = ProtoBuf.protohash(v)
isequal(v1::NodeReportProto, v2::NodeReportProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::NodeReportProto, v2::NodeReportProto) = ProtoBuf.protoeq(v1, v2)

type NodeIdToLabelsProto
    nodeId::NodeIdProto
    nodeLabels::Array{AbstractString,1}
    NodeIdToLabelsProto() = (o=new(); fillunset(o); o)
end #type NodeIdToLabelsProto
hash(v::NodeIdToLabelsProto) = ProtoBuf.protohash(v)
isequal(v1::NodeIdToLabelsProto, v2::NodeIdToLabelsProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::NodeIdToLabelsProto, v2::NodeIdToLabelsProto) = ProtoBuf.protoeq(v1, v2)

type LabelsToNodeIdsProto
    nodeLabels::AbstractString
    nodeId::Array{NodeIdProto,1}
    LabelsToNodeIdsProto() = (o=new(); fillunset(o); o)
end #type LabelsToNodeIdsProto
hash(v::LabelsToNodeIdsProto) = ProtoBuf.protohash(v)
isequal(v1::LabelsToNodeIdsProto, v2::LabelsToNodeIdsProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::LabelsToNodeIdsProto, v2::LabelsToNodeIdsProto) = ProtoBuf.protoeq(v1, v2)

type ResourceRequestProto
    priority::PriorityProto
    resource_name::AbstractString
    capability::ResourceProto
    num_containers::Int32
    relax_locality::Bool
    node_label_expression::AbstractString
    ResourceRequestProto() = (o=new(); fillunset(o); o)
end #type ResourceRequestProto
const __val_ResourceRequestProto = @compat Dict(:relax_locality => true)
meta(t::Type{ResourceRequestProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_ResourceRequestProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::ResourceRequestProto) = ProtoBuf.protohash(v)
isequal(v1::ResourceRequestProto, v2::ResourceRequestProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ResourceRequestProto, v2::ResourceRequestProto) = ProtoBuf.protoeq(v1, v2)

type PreemptionContainerProto
    id::ContainerIdProto
    PreemptionContainerProto() = (o=new(); fillunset(o); o)
end #type PreemptionContainerProto
hash(v::PreemptionContainerProto) = ProtoBuf.protohash(v)
isequal(v1::PreemptionContainerProto, v2::PreemptionContainerProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::PreemptionContainerProto, v2::PreemptionContainerProto) = ProtoBuf.protoeq(v1, v2)

type StrictPreemptionContractProto
    container::Array{PreemptionContainerProto,1}
    StrictPreemptionContractProto() = (o=new(); fillunset(o); o)
end #type StrictPreemptionContractProto
hash(v::StrictPreemptionContractProto) = ProtoBuf.protohash(v)
isequal(v1::StrictPreemptionContractProto, v2::StrictPreemptionContractProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::StrictPreemptionContractProto, v2::StrictPreemptionContractProto) = ProtoBuf.protoeq(v1, v2)

type PreemptionResourceRequestProto
    resource::ResourceRequestProto
    PreemptionResourceRequestProto() = (o=new(); fillunset(o); o)
end #type PreemptionResourceRequestProto
hash(v::PreemptionResourceRequestProto) = ProtoBuf.protohash(v)
isequal(v1::PreemptionResourceRequestProto, v2::PreemptionResourceRequestProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::PreemptionResourceRequestProto, v2::PreemptionResourceRequestProto) = ProtoBuf.protoeq(v1, v2)

type PreemptionContractProto
    resource::Array{PreemptionResourceRequestProto,1}
    container::Array{PreemptionContainerProto,1}
    PreemptionContractProto() = (o=new(); fillunset(o); o)
end #type PreemptionContractProto
hash(v::PreemptionContractProto) = ProtoBuf.protohash(v)
isequal(v1::PreemptionContractProto, v2::PreemptionContractProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::PreemptionContractProto, v2::PreemptionContractProto) = ProtoBuf.protoeq(v1, v2)

type PreemptionMessageProto
    strictContract::StrictPreemptionContractProto
    contract::PreemptionContractProto
    PreemptionMessageProto() = (o=new(); fillunset(o); o)
end #type PreemptionMessageProto
hash(v::PreemptionMessageProto) = ProtoBuf.protohash(v)
isequal(v1::PreemptionMessageProto, v2::PreemptionMessageProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::PreemptionMessageProto, v2::PreemptionMessageProto) = ProtoBuf.protoeq(v1, v2)

type ResourceBlacklistRequestProto
    blacklist_additions::Array{AbstractString,1}
    blacklist_removals::Array{AbstractString,1}
    ResourceBlacklistRequestProto() = (o=new(); fillunset(o); o)
end #type ResourceBlacklistRequestProto
hash(v::ResourceBlacklistRequestProto) = ProtoBuf.protohash(v)
isequal(v1::ResourceBlacklistRequestProto, v2::ResourceBlacklistRequestProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ResourceBlacklistRequestProto, v2::ResourceBlacklistRequestProto) = ProtoBuf.protoeq(v1, v2)

type LogAggregationContextProto
    include_pattern::AbstractString
    exclude_pattern::AbstractString
    rolled_logs_include_pattern::AbstractString
    rolled_logs_exclude_pattern::AbstractString
    LogAggregationContextProto() = (o=new(); fillunset(o); o)
end #type LogAggregationContextProto
const __val_LogAggregationContextProto = @compat Dict(:include_pattern => ".*", :rolled_logs_exclude_pattern => ".*")
meta(t::Type{LogAggregationContextProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_LogAggregationContextProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::LogAggregationContextProto) = ProtoBuf.protohash(v)
isequal(v1::LogAggregationContextProto, v2::LogAggregationContextProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::LogAggregationContextProto, v2::LogAggregationContextProto) = ProtoBuf.protoeq(v1, v2)

type ApplicationACLMapProto
    accessType::Int32
    acl::AbstractString
    ApplicationACLMapProto() = (o=new(); fillunset(o); o)
end #type ApplicationACLMapProto
const __val_ApplicationACLMapProto = @compat Dict(:acl => " ")
meta(t::Type{ApplicationACLMapProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_ApplicationACLMapProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::ApplicationACLMapProto) = ProtoBuf.protohash(v)
isequal(v1::ApplicationACLMapProto, v2::ApplicationACLMapProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ApplicationACLMapProto, v2::ApplicationACLMapProto) = ProtoBuf.protoeq(v1, v2)

type YarnClusterMetricsProto
    num_node_managers::Int32
    YarnClusterMetricsProto() = (o=new(); fillunset(o); o)
end #type YarnClusterMetricsProto
hash(v::YarnClusterMetricsProto) = ProtoBuf.protohash(v)
isequal(v1::YarnClusterMetricsProto, v2::YarnClusterMetricsProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::YarnClusterMetricsProto, v2::YarnClusterMetricsProto) = ProtoBuf.protoeq(v1, v2)

type QueueInfoProto
    queueName::AbstractString
    capacity::Float32
    maximumCapacity::Float32
    currentCapacity::Float32
    state::Int32
    childQueues::Array{QueueInfoProto,1}
    applications::Array{ApplicationReportProto,1}
    accessibleNodeLabels::Array{AbstractString,1}
    defaultNodeLabelExpression::AbstractString
    QueueInfoProto() = (o=new(); fillunset(o); o)
end #type QueueInfoProto
hash(v::QueueInfoProto) = ProtoBuf.protohash(v)
isequal(v1::QueueInfoProto, v2::QueueInfoProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::QueueInfoProto, v2::QueueInfoProto) = ProtoBuf.protoeq(v1, v2)

type QueueUserACLInfoProto
    queueName::AbstractString
    userAcls::Array{Int32,1}
    QueueUserACLInfoProto() = (o=new(); fillunset(o); o)
end #type QueueUserACLInfoProto
hash(v::QueueUserACLInfoProto) = ProtoBuf.protohash(v)
isequal(v1::QueueUserACLInfoProto, v2::QueueUserACLInfoProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::QueueUserACLInfoProto, v2::QueueUserACLInfoProto) = ProtoBuf.protoeq(v1, v2)

type ReservationIdProto
    id::Int64
    cluster_timestamp::Int64
    ReservationIdProto() = (o=new(); fillunset(o); o)
end #type ReservationIdProto
hash(v::ReservationIdProto) = ProtoBuf.protohash(v)
isequal(v1::ReservationIdProto, v2::ReservationIdProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ReservationIdProto, v2::ReservationIdProto) = ProtoBuf.protoeq(v1, v2)

type ReservationRequestProto
    capability::ResourceProto
    num_containers::Int32
    concurrency::Int32
    duration::Int64
    ReservationRequestProto() = (o=new(); fillunset(o); o)
end #type ReservationRequestProto
const __val_ReservationRequestProto = @compat Dict(:num_containers => 1, :concurrency => 1, :duration => -1)
meta(t::Type{ReservationRequestProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_ReservationRequestProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::ReservationRequestProto) = ProtoBuf.protohash(v)
isequal(v1::ReservationRequestProto, v2::ReservationRequestProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ReservationRequestProto, v2::ReservationRequestProto) = ProtoBuf.protoeq(v1, v2)

type ReservationRequestsProto
    reservation_resources::Array{ReservationRequestProto,1}
    interpreter::Int32
    ReservationRequestsProto() = (o=new(); fillunset(o); o)
end #type ReservationRequestsProto
const __val_ReservationRequestsProto = @compat Dict(:interpreter => ReservationRequestInterpreterProto.R_ALL)
meta(t::Type{ReservationRequestsProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_ReservationRequestsProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::ReservationRequestsProto) = ProtoBuf.protohash(v)
isequal(v1::ReservationRequestsProto, v2::ReservationRequestsProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ReservationRequestsProto, v2::ReservationRequestsProto) = ProtoBuf.protoeq(v1, v2)

type ReservationDefinitionProto
    reservation_requests::ReservationRequestsProto
    arrival::Int64
    deadline::Int64
    reservation_name::AbstractString
    ReservationDefinitionProto() = (o=new(); fillunset(o); o)
end #type ReservationDefinitionProto
hash(v::ReservationDefinitionProto) = ProtoBuf.protohash(v)
isequal(v1::ReservationDefinitionProto, v2::ReservationDefinitionProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ReservationDefinitionProto, v2::ReservationDefinitionProto) = ProtoBuf.protoeq(v1, v2)

type ContainerStatusProto
    container_id::ContainerIdProto
    state::Int32
    diagnostics::AbstractString
    exit_status::Int32
    ContainerStatusProto() = (o=new(); fillunset(o); o)
end #type ContainerStatusProto
const __val_ContainerStatusProto = @compat Dict(:diagnostics => "N/A", :exit_status => -1000)
meta(t::Type{ContainerStatusProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_ContainerStatusProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::ContainerStatusProto) = ProtoBuf.protohash(v)
isequal(v1::ContainerStatusProto, v2::ContainerStatusProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ContainerStatusProto, v2::ContainerStatusProto) = ProtoBuf.protoeq(v1, v2)

type ContainerResourceIncreaseRequestProto
    container_id::ContainerIdProto
    capability::ResourceProto
    ContainerResourceIncreaseRequestProto() = (o=new(); fillunset(o); o)
end #type ContainerResourceIncreaseRequestProto
hash(v::ContainerResourceIncreaseRequestProto) = ProtoBuf.protohash(v)
isequal(v1::ContainerResourceIncreaseRequestProto, v2::ContainerResourceIncreaseRequestProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ContainerResourceIncreaseRequestProto, v2::ContainerResourceIncreaseRequestProto) = ProtoBuf.protoeq(v1, v2)

type ContainerResourceIncreaseProto
    container_id::ContainerIdProto
    capability::ResourceProto
    container_token::TokenProto
    ContainerResourceIncreaseProto() = (o=new(); fillunset(o); o)
end #type ContainerResourceIncreaseProto
hash(v::ContainerResourceIncreaseProto) = ProtoBuf.protohash(v)
isequal(v1::ContainerResourceIncreaseProto, v2::ContainerResourceIncreaseProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ContainerResourceIncreaseProto, v2::ContainerResourceIncreaseProto) = ProtoBuf.protoeq(v1, v2)

type ContainerResourceDecreaseProto
    container_id::ContainerIdProto
    capability::ResourceProto
    ContainerResourceDecreaseProto() = (o=new(); fillunset(o); o)
end #type ContainerResourceDecreaseProto
hash(v::ContainerResourceDecreaseProto) = ProtoBuf.protohash(v)
isequal(v1::ContainerResourceDecreaseProto, v2::ContainerResourceDecreaseProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ContainerResourceDecreaseProto, v2::ContainerResourceDecreaseProto) = ProtoBuf.protoeq(v1, v2)

type StringLocalResourceMapProto
    key::AbstractString
    value::LocalResourceProto
    StringLocalResourceMapProto() = (o=new(); fillunset(o); o)
end #type StringLocalResourceMapProto
hash(v::StringLocalResourceMapProto) = ProtoBuf.protohash(v)
isequal(v1::StringLocalResourceMapProto, v2::StringLocalResourceMapProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::StringLocalResourceMapProto, v2::StringLocalResourceMapProto) = ProtoBuf.protoeq(v1, v2)

type StringStringMapProto
    key::AbstractString
    value::AbstractString
    StringStringMapProto() = (o=new(); fillunset(o); o)
end #type StringStringMapProto
hash(v::StringStringMapProto) = ProtoBuf.protohash(v)
isequal(v1::StringStringMapProto, v2::StringStringMapProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::StringStringMapProto, v2::StringStringMapProto) = ProtoBuf.protoeq(v1, v2)

type StringBytesMapProto
    key::AbstractString
    value::Array{UInt8,1}
    StringBytesMapProto() = (o=new(); fillunset(o); o)
end #type StringBytesMapProto
hash(v::StringBytesMapProto) = ProtoBuf.protohash(v)
isequal(v1::StringBytesMapProto, v2::StringBytesMapProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::StringBytesMapProto, v2::StringBytesMapProto) = ProtoBuf.protoeq(v1, v2)

type ContainerLaunchContextProto
    localResources::Array{StringLocalResourceMapProto,1}
    tokens::Array{UInt8,1}
    service_data::Array{StringBytesMapProto,1}
    environment::Array{StringStringMapProto,1}
    command::Array{AbstractString,1}
    application_ACLs::Array{ApplicationACLMapProto,1}
    ContainerLaunchContextProto() = (o=new(); fillunset(o); o)
end #type ContainerLaunchContextProto
hash(v::ContainerLaunchContextProto) = ProtoBuf.protohash(v)
isequal(v1::ContainerLaunchContextProto, v2::ContainerLaunchContextProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ContainerLaunchContextProto, v2::ContainerLaunchContextProto) = ProtoBuf.protoeq(v1, v2)

type ApplicationSubmissionContextProto
    application_id::ApplicationIdProto
    application_name::AbstractString
    queue::AbstractString
    priority::PriorityProto
    am_container_spec::ContainerLaunchContextProto
    cancel_tokens_when_complete::Bool
    unmanaged_am::Bool
    maxAppAttempts::Int32
    resource::ResourceProto
    applicationType::AbstractString
    keep_containers_across_application_attempts::Bool
    applicationTags::Array{AbstractString,1}
    attempt_failures_validity_interval::Int64
    log_aggregation_context::LogAggregationContextProto
    reservation_id::ReservationIdProto
    node_label_expression::AbstractString
    am_container_resource_request::ResourceRequestProto
    ApplicationSubmissionContextProto() = (o=new(); fillunset(o); o)
end #type ApplicationSubmissionContextProto
const __val_ApplicationSubmissionContextProto = @compat Dict(:application_name => "N/A", :queue => "default", :cancel_tokens_when_complete => true, :unmanaged_am => false, :maxAppAttempts => 0, :applicationType => "YARN", :keep_containers_across_application_attempts => false, :attempt_failures_validity_interval => -1)
meta(t::Type{ApplicationSubmissionContextProto}) = meta(t, ProtoBuf.DEF_REQ, ProtoBuf.DEF_FNUM, __val_ApplicationSubmissionContextProto, true, ProtoBuf.DEF_PACK, ProtoBuf.DEF_WTYPES)
hash(v::ApplicationSubmissionContextProto) = ProtoBuf.protohash(v)
isequal(v1::ApplicationSubmissionContextProto, v2::ApplicationSubmissionContextProto) = ProtoBuf.protoisequal(v1, v2)
==(v1::ApplicationSubmissionContextProto, v2::ApplicationSubmissionContextProto) = ProtoBuf.protoeq(v1, v2)

export ContainerStateProto, YarnApplicationStateProto, YarnApplicationAttemptStateProto, FinalApplicationStatusProto, LocalResourceVisibilityProto, LocalResourceTypeProto, NodeStateProto, AMCommandProto, ApplicationAccessTypeProto, QueueStateProto, QueueACLProto, ReservationRequestInterpreterProto, ContainerExitStatusProto, SerializedExceptionProto, ApplicationIdProto, ApplicationAttemptIdProto, ContainerIdProto, ResourceProto, ResourceOptionProto, NodeResourceMapProto, PriorityProto, ContainerProto, ContainerReportProto, URLProto, LocalResourceProto, ApplicationResourceUsageReportProto, ApplicationReportProto, ApplicationAttemptReportProto, NodeIdProto, NodeReportProto, NodeIdToLabelsProto, LabelsToNodeIdsProto, ResourceRequestProto, PreemptionMessageProto, StrictPreemptionContractProto, PreemptionContractProto, PreemptionContainerProto, PreemptionResourceRequestProto, ResourceBlacklistRequestProto, ApplicationSubmissionContextProto, LogAggregationContextProto, ApplicationACLMapProto, YarnClusterMetricsProto, QueueInfoProto, QueueUserACLInfoProto, ReservationIdProto, ReservationRequestProto, ReservationRequestsProto, ReservationDefinitionProto, ContainerLaunchContextProto, ContainerStatusProto, ContainerResourceIncreaseRequestProto, ContainerResourceIncreaseProto, ContainerResourceDecreaseProto, StringLocalResourceMapProto, StringStringMapProto, StringBytesMapProto


text:
using MatrixCompletion
import Distributions

@testset "$(format("Estimator: MOM[NegativeBinomial]]"))"  begin
  let
    for i in 1:10
      p_test = rand()
      r_test = rand() * 20
      data = rand(Distributions.NegativeBinomial(r_test, p_test), 1000 * 200)
      tc = estimator(MOM{NegativeBinomial}(), data)
      @test abs(tc[:p] - p_test) / p_test < 0.05
      @test abs(tc[:r] - r_test) / r_test < 0.05
    end
  end
end

ext: jl


text:
using Documenter

deploydocs(
    target="_build/dirhtml",
    repo="github.com/dfm/sphinx-documenter-deploy.git",
    devbranch="main",
    devurl="latest"
)

max_stars_repo_name: dfm/sphinx-documenter-deploy

max_issues_repo_name: dfm/sphinx-documenter-deploy

max_forks_repo_name: dfm/sphinx-documenter-deploy


text:
function is_correct_model(connectivity, pmap, expected_final_state, tspan, init_cond)
  #=
    Function to test whether a generated model has the right number of species,
    parameters, reactions. This function also tests if the model reaches a
    desired final state when simulated from the provided initial conditions and
    time span.
  =#
  N = size(connectivity, 1) # Number of nodes
  E = count(!iszero, connectivity)# Number of edges

  model = create_model(connectivity)

  @test length(species(model)) == N
  @test length(parameters(model)) == 2*N + 3*E
  @test length(reactions(model)) == 2*N + E

  @nonamespace u₀map = [model.a[1] => init_cond[1], model.a[2] => init_cond[2],
                        model.a[3] => init_cond[3]]
  prob = ODEProblem(model, u₀map, tspan, pmap)
  sol = solve(prob, lsoda())
  error = broadcast(abs, sol.u[end,:][1] - expected_final_state)
  @test all(error .< 1e-2)
end

# Test Goodwin's oscillatory network
connectivity_g = [0 0 1; 1 0 0; 0 -1 0]
tspan_g = (0.0, 1.0)
init_cond_g = [0.9, 0.9, 0.9]
expected_final_state_g = [0.21, 0.23, 0.039]
model = create_model(connectivity_g)
@nonamespace pmap_g = (model.α[1] => 0.00004564, model.α[2] => 0.16342066,
                       model.α[3] => 0.70351496, model.β[1] => 56.67356362,
                       model.β[2] => 50.87885942, model.β[3] => 30.31904319,
                       model.γ[1] => 1657.39796806, model.γ[2] => 6988.78292809,
                       model.γ[3] => 7461.31128709, model.κ[1] => 0.11196140,
                       model.κ[2] => 0.50912113, model.κ[3] => 0.33210760,
                       model.η[1] => 3.67709117, model.η[2] => 7.17307667,
                       model.η[3] => 6.80130072)
is_correct_model(connectivity_g, pmap_g, expected_final_state_g, tspan_g, init_cond_g)

# Test Goodwin + PF
connect_gpf = [0 0 1; 1 0 0; 0 -1 1]
tspan_gpf = (0.0, 1.0)
init_cond_gpf = [0.9, 0.9, 0.9]
expec_final_state_gpf = [0.047, 0.43, 0.13]
model = create_model(connect_gpf)
@nonamespace pmap_gpf = (model.α[1] => 0.00012570, model.α[2] => 0.24512445,
                         model.α[3] => 0.74518621, model.β[1] => 70.08309071,
                         model.β[2] => 30.10686277, model.β[3] => 37.80616599,
                         model.γ[1] => 6451.62448135, model.γ[2] => 104.67978494,
                         model.γ[3] => 7529.60087920, model.γ[4] => 5429.62993215,
                         model.κ[1] => 0.66066269, model.κ[2] => 0.59212769,
                         model.κ[3] => 0.69523907, model.κ[4] => 0.58345202,
                         model.η[1] => 9.22476250, model.η[2] => 4.34775355,
                         model.η[3] => 8.88820688, model.η[4] => 5.73136565)
is_correct_model(connect_gpf, pmap_gpf, expec_final_state_gpf, tspan_gpf, init_cond_gpf)


text:
export fit, fit!, results

const DEFAULT_INITIAL_PARAM = NaN.*ones(1, 1)
const DEFAULT_NUM_SEEDS = 3
const DEFAULT_VERBOSE = 1
const VARIANCE_ZERO = 1e-10

struct Fitted{D <: Distribution, T <: AbstractFloat}
    num_obs::Integer
    unknowns::Unknowns
    aic::T
    bic::T
    llk::T
    coefs::Vector{T}
    numerical_hessian::Matrix{T}
    quantile_residuals::Vector{T}
end

struct CoefsStats{T <: AbstractFloat}
    unknowns::Unknowns
    coefs::Vector{T}
    std_errors::Vector{T}
    t_stat::Vector{T}
    p_values::Vector{T}
end

struct EstimationStats{D <: Distribution, T <: AbstractFloat}
    num_obs::Integer
    loglikelihood::T
    aic::T
    bic::T
    np::T
    jarquebera_p_value::T
    coefs_stats::CoefsStats{T}
end

function results(f::Fitted{D, T}) where {D, T}
    estim_results = eval_coefs_stats(f)
    np = length(f.unknowns)
    jarquebera_p_value = pvalue(JarqueBeraTest(f.quantile_residuals))
    return EstimationStats{D, T}(f.num_obs, f.llk, f.aic, f.bic, np,
                                 jarquebera_p_value, estim_results)
end

function eval_coefs_stats(f::Fitted{D, T}) where {D, T}
    np = length(f.unknowns)
    inv_H = inv(f.numerical_hessian)
    vars = diag(inv_H)
    for i in eachindex(vars)
        if vars[i] <= VARIANCE_ZERO
            vars[i] = VARIANCE_ZERO
        end
    end
    std_errors = sqrt.(vars)
    t_stats = f.coefs ./ std_errors

    # Calculate p-values of the the t statistics
    t_dist = TDist(np)
    p_values = 1 .- 2*abs.(cdf.(t_dist, t_stats) .- 0.5)

    return CoefsStats{T}(f.unknowns, f.coefs, std_errors, t_stats, p_values)
end

mutable struct AuxEstimation{T <: AbstractFloat}
    psi::Vector{Vector{T}}
    numerical_hessian::Vector{Matrix{T}}
    loglikelihood::Vector{T}
    opt_result::Vector{Optim.OptimizationResults}

    function AuxEstimation{T}() where T
        return new(
            Vector{Vector{T}}(undef, 0), #psi
            Vector{Matrix{T}}(undef, 0),
            Vector{T}(undef, 0), # loglikelihood
            Vector{Optim.OptimizationResults}(undef, 0) # opt_result
            )
    end
end

function AIC(n_unknowns::Int, log_lik::T) where T
    return T(2 * n_unknowns - 2 * log_lik)
end

function BIC(n::Int, n_unknowns::Int, log_lik::T) where T
    return T(log(n) * n_unknowns - 2 * log_lik)
end

function update_aux_estimation!(aux_est::AuxEstimation{T}, func::Optim.TwiceDifferentiable,
                                opt_result::Optim.OptimizationResults) where T

    push!(aux_est.numerical_hessian, Optim.hessian!(func, opt_result.minimizer))
    push!(aux_est.psi, opt_result.minimizer)
    push!(aux_est.loglikelihood, -opt_result.minimum)
    push!(aux_est.opt_result, opt_result)
    return
end

function fit!(gas::Model{D, T}, y::Vector{T};
             initial_params::Matrix{T} = DEFAULT_INITIAL_PARAM,
             opt_method::AbstractOptimizationMethod = NelderMead(gas, DEFAULT_NUM_SEEDS),
             verbose::Int = DEFAULT_VERBOSE,
             throw_errors::Bool = false,
             time_limit_sec::Int = 10^8) where {D, T}

    unknowns = find_unknowns(gas)
    # Check if the model has no unknowns
    n_unknowns = length(unknowns)
    check_model_estimated(n_unknowns) && return gas

    verbose in [0, 1, 2, 3] || throw(ErrorException, "verbose argument must be in [0, 1, 2, 3]")
    # Number of initial_points and number of params to estimate
    n_initial_points = length(opt_method.initial_points)
    n = length(y)

    unknowns = find_unknowns(gas)
    n_unknowns = length(unknowns)

    # Check if the model has no unknowns
    check_model_estimated(n_unknowns) && return gas

    # Create a copy of the model to estimate
    gas_fit = deepcopy(gas)

    # optimize for each initial_point
    aux_est = AuxEstimation{T}()

    for i = 1:n_initial_points
        try
            func = TwiceDifferentiable(psi_tilde -> log_lik(psi_tilde, y, gas_fit,
                                                        initial_params, unknowns, n),
                                                        opt_method.initial_points[i])
            opt_result = optimize(func, opt_method, verbose, i, time_limit_sec)
            update_aux_estimation!(aux_est, func, opt_result)
            verbose >= 1 && println("Round $i of $n_initial_points - Log-likelihood: $(-opt_result.minimum * length(y))")
        catch err
            println(err)
            if throw_errors
                throw(err)
            end
            verbose >= 1 && println("Round $i diverged")
        end
    end

    if isempty(aux_est.loglikelihood)
        verbose >= 1 && println("No initial point converged.")
        return
    end

    best_llk, best_seed = findmax(aux_est.loglikelihood)
    # We optimize the average loglikelihood inside log_lik
    best_llk = best_llk * length(y)
    num_hessian = aux_est.numerical_hessian[best_seed]
    coefs = aux_est.psi[best_seed]
    aic = AIC(n_unknowns, best_llk)
    bic = BIC(n, n_unknowns, best_llk)

    if verbose >= 2
        println("\nBest optimization result:")
        println(aux_est.opt_result[best_seed])
    end

    fill_psitilde!(gas, coefs, unknowns)

    # Calculate pearson residuals
    pearson_res = isnan(initial_params[1]) ?
                quantile_residuals(y, gas) :
                quantile_residuals(y, gas; initial_params = initial_params)

    return Fitted{D, T}(n, unknowns, aic, bic, best_llk, coefs, num_hessian, pearson_res)
end


text:
init_sieve(sieve_filename_::Nothing) = DataFrame()
init_sieve(sieve_filename_) =  DataFrame(
    SIEVE_STDERR = Union{Float64, Missing}[],
    SIEVE_PVAL = Union{Float64, Missing}[],
    SIEVE_LWB = Union{Float64, Missing}[],
    SIEVE_UPB = Union{Float64, Missing}[]
)

init_results(sieve_filename_) = hcat(DataFrame(
    PHENOTYPE = String[],
    PHENOTYPE_TYPE = String[],
    QUERYNAME = String[],
    FILENAME_ORIGIN = String[],
    REPORT_KEY = String[],
    INITIAL_ESTIMATE = Float64[],
    ESTIMATE = Float64[],
    STDERR = Float64[],
    PVAL = Float64[],
    LWB = Float64[],
    UPB = Float64[],
    ),
    init_sieve(sieve_filename_)
)

sieve_results_(estimate, sieve_stderror::Nothing) =
    [missing, missing, missing, missing]

function sieve_results_(estimate, sieve_stderror)
    sieve_test = OneSampleZTest(estimate, sieve_stderror, 1)
    sieve_pval = pvalue(sieve_test)
    sieve_lwb, sieve_upb = confint(sieve_test)
    return [sieve_stderror, sieve_pval, sieve_lwb, sieve_upb]
end

sieve_results(estimate, filename, key, pair_to_var_id::Nothing, sieve_stderrors::Nothing) = []
function sieve_results(estimate, filename, key, pair_to_var_id, sieve_stderrors)
    sieve_stderror = haskey(pair_to_var_id, (filename, key)) ?
                            sieve_stderrors[pair_to_var_id[(filename, key)]] :
                            nothing
    return sieve_results_(estimate, sieve_stderror)
end

function update_results!(results, tmlereport, phenotype_type, filename, key, pair_to_var_id, sieve_stderrors)
    s = summarize(tmlereport)
    phenotype = string(tmlereport.target_name)
    lwb, upb = s.confint

    summary = vcat(
        [phenotype, phenotype_type, s.query.name, filename, key,
        s.initial_estimate, s.estimate,
        s.stderror, s.pvalue, lwb, upb],
        sieve_results(s.estimate, filename, key, pair_to_var_id, sieve_stderrors)
    )

    push!(results, summary)
end

get_sieve_info(dirname_, sieve_filename_::Nothing) = (nothing, nothing)

function get_sieve_info(dirname_, sieve_filename_)
    sieve_file = jldopen(joinpath(dirname_, sieve_filename_))
    pair_to_var_id = Dict((f, r) => i for (i, (f, r)) in enumerate(sieve_file["SOURCEFILE_REPORTID_PAIRS"]))
    sieve_stderrors = sieve_file["STDERRORS"]
    close(sieve_file)
    return pair_to_var_id, sieve_stderrors
end

function build_summary(parsed_args)
    dirname_, prefix_ = splitdir(parsed_args["prefix"])
    dirname__ = dirname_ == "" ? "." : dirname_
    allhdf5files = filter(
            x -> startswith(x, prefix_) && endswith(x, ".hdf5"),
            readdir(dirname__)
    )

    if parsed_args["sieve"]
        estimate_filenames = filter(x -> !occursin("sieve_variance", x) , allhdf5files)
        sieve_filename_ = filter(x -> occursin("sieve_variance", x) , allhdf5files)[1]
    else
        estimate_filenames = allhdf5files
        sieve_filename_ = nothing
    end

    pair_to_var_id, sieve_stderrors = get_sieve_info(dirname_, sieve_filename_)

    results = init_results(sieve_filename_)
    for filename in estimate_filenames
        phenotype_type = occursin("Bool", filename) ? "BINARY" : "CONTINUOUS"
        jldopen(joinpath(dirname_, filename)) do io
            tmlereports = io["TMLEREPORTS"]
            for key in keys(tmlereports)
                tmlereport = tmlereports[key]
                update_results!(results, tmlereport, phenotype_type, filename, key, pair_to_var_id, sieve_stderrors)
            end
        end
    end

    CSV.write(parsed_args["out"], results)
end


text:
function circleShape(x, y, r)
	θ = LinRange(0, 2*π, 500)
	x .+ r*sin.(θ), y .+ r*cos.(θ)
end

max_stars_repo_name: ScottJordan/EvaluationOfRLAlgs

max_issues_repo_name: ScottJordan/EvaluationOfRLAlgs

max_forks_repo_name: ScottJordan/EvaluationOfRLAlgs


text:
export OneBody

# the type definition of OneBody is in JuLIP to enable its use from
# IPFitting without having to load NBodyIPs. Here we just extend
# the functionality so that it can work effectively with NBodyIPs

using JuLIP: Atoms
import JuLIP.Potentials: OneBody
import JuLIP: energy, forces, virial
import Base: hash
import NBodyIPs: fast, combinebasis

combinebasis(Vs::AbstractVector{<: OneBody}, Cs::AbstractVector{<: Real}) =
             OneBody( sum( V.E0 * c for (V, c) in zip(Vs, Cs) ) )

fast(V::OneBody) = V

hash(::BASIS, ::OneBody) = hash(OneBody)

bodyorder(V::OneBody) = 1

energy(B::AbstractVector{TB}, at::Atoms{T}) where {TB <: OneBody, T} =
   [ energy(b, at) for b in B ]

forces(B::AbstractVector{TB}, at::Atoms{T}) where {TB <: OneBody, T} =
   [ forces(b, at) for b in B ]

virial(B::AbstractVector{TB}, at::Atoms{T}) where {TB <: OneBody, T} =
   [ virial(b, at) for b in B ]

# TODO: maybe the above should be moved to TB <: AbstractCalculator?

max_stars_repo_name: cortner/ManyBodyIPs

max_issues_repo_name: cortner/ManyBodyIPs

max_forks_repo_name: cortner/ManyBodyIPs


text:
import CLBLAS
import OpenCL.cl

    const clblas = CLBLAS
    clblas.setup()

    device, ctx, queue = clblas.get_next_compute_context()

    N = unsigned(5)
    data = Array(Float32, 5)
    data[1] = 1.1
    data[2] = 2.22
    data[3] = 3.333
    data[4] = 4.4444
    data[5] = 5.55555

    dat = Array(Float32, 5)
    dat[1] = 6.1
    dat[2] = 7.22
    dat[3] = 8.333
    dat[4] = 9.4444
    dat[5] = 10.55555

    bufX = cl.Buffer(Float32, ctx, (:r, :copy),hostbuf=data)
    bufY = cl.Buffer(Float32, ctx, (:r, :copy),hostbuf=dat)

    bufDotP = cl.Buffer(Float32, ctx, :w, 1)
    scratchBuff = cl.Buffer(Float32, ctx, :rw, length(data))

    offDP = offy = offx = unsigned(0)
    incx = cl.cl_int(1)
    incy = cl.cl_int(1)
    ncq = cl.cl_uint(1)
    clq = queue.id
    newl = cl.cl_uint(0)

    event = cl.UserEvent(ctx, retain=true)
    ptrEvent = [event.id]

    clblas.clblasSdot( N, bufDotP.id, offDP, bufX.id, offx, incx, bufY.id, offy, incy, scratchBuff.id, ncq, [clq], newl, C_NULL, ptrEvent);

    cl.api.clWaitForEvents(cl.cl_uint(1), ptrEvent)

    result = Array(Float32, length(bufDotP))
    cl.enqueue_read_buffer(queue, bufDotP, result, unsigned(0), nothing, true)

    expected = dot(data, dat)
    println(result)
    println("------------")
    println(expected)

    try
        # locally passes without atol
        @assert(isapprox(norm(expected - result[1]), zero(Float32), atol=0.00002))
        info("success!")
    finally
        clblas.teardown()
    end


text:
#### Document type

immutable _XMLDocStruct  # use the same layout as C
    # common part
    _private::Ptr{Void}
    nodetype::Cint
    name::Ptr{Cchar}
    children::Xptr
    last::Xptr
    parent::Xptr
    next::Xptr
    prev::Xptr
    doc::Xptr

    # specific part
    compression::Cint
    standalone::Cint
    intsubset::Xptr
    extsubset::Xptr
    oldns::Xptr
    version::Xstr
    encoding::Xstr

    ids::Ptr{Void}
    refs::Ptr{Void}
    url::Xstr
    charset::Cint
    dict::Xstr
    psvi::Ptr{Void}
    parseflags::Cint
    properties::Cint
end

type XMLDocument
    ptr::Xptr
    _struct::_XMLDocStruct

    function XMLDocument(ptr::Xptr)
        s::_XMLDocStruct = unsafe_load(convert(Ptr{_XMLDocStruct}, ptr))

        # validate integrity
        @assert s.nodetype == XML_DOCUMENT_NODE
        @assert s.doc == ptr

        new(ptr, s)
    end

    function XMLDocument()
        # create an empty document
        ptr = ccall(xmlNewDoc, Xptr, (Ptr{Cchar},), "1.0")
        XMLDocument(ptr)
    end
end

version(xdoc::XMLDocument) = bytestring(xdoc._struct.version)
encoding(xdoc::XMLDocument) = bytestring(xdoc._struct.encoding)
compression(xdoc::XMLDocument) = int(xdoc._struct.compression)
standalone(xdoc::XMLDocument) = int(xdoc._struct.standalone)

function root(xdoc::XMLDocument)
    pr = ccall(xmlDocGetRootElement, Ptr{Void}, (Ptr{Void},), xdoc.ptr)
    pr != nullptr || throw(XMLNoRootError())
    XMLElement(pr)
end

#### construction & free

function free(xdoc::XMLDocument)
    ccall(xmlFreeDoc, Void, (Ptr{Void},), xdoc.ptr)
    xdoc.ptr = nullptr
end

function set_root(xdoc::XMLDocument, xroot::XMLElement)
    ccall(xmlDocSetRootElement, Xptr, (Xptr, Xptr), xdoc.ptr, xroot.node.ptr)
end

function create_root(xdoc::XMLDocument, name::String)
    xroot = new_element(name)
    set_root(xdoc, xroot)
    return xroot
end

#### parse and free

function parse_file(filename::String)
    p = ccall(xmlParseFile, Xptr, (Ptr{Cchar},), filename)
    p != nullptr || throw(XMLParseError("Failure in parsing an XML file."))
    XMLDocument(p)
end

function parse_string(s::String)
    p = ccall(xmlParseMemory, Xptr, (Ptr{Cchar}, Cint), s, sizeof(s) + 1)
    p != nullptr || throw(XMLParseError("Failure in parsing an XML string."))
    XMLDocument(p)
end

#### output

function save_file(xdoc::XMLDocument, filename::String; encoding::String="utf-8")
    ret = ccall(xmlSaveFormatFileEnc, Cint, (Ptr{Cchar}, Xptr, Ptr{Cchar}, Cint),
        filename, xdoc.ptr, encoding, 1)
    if ret < 0
        throw(XMLWriteError("Failed to save XML to file $filename"))
    end
    return int(ret)  # number of bytes written
end

function Base.string(xdoc::XMLDocument; encoding::String="utf-8")
    buf_out = Array(Xstr, 1)
    len_out = Array(Cint, 1)
    ccall(xmlDocDumpFormatMemoryEnc, Void, (Xptr, Ptr{Xstr}, Ptr{Cint}, Ptr{Cchar}, Cint),
        xdoc.ptr, buf_out, len_out, encoding, 1)
    _xcopystr(buf_out[1])
end

Base.show(io::IO, xdoc::XMLDocument) = println(io, string(xdoc))


text:
if VERSION < v"1.2"
    Base.getproperty(x::Tuple, f::Int) = getfield(x, f)
end


text:
module MPI

const tag_name = "mpi"

const augment = raw"""
    # Can't use Preferences since we might be running this very early with a non-existing Manifest
    MPIPreferences_UUID = Base.UUID("3da0fdf6-3ccc-4f1b-acd9-58baa6c99267")
    const preferences = Base.get_preferences(MPIPreferences_UUID)

    # Keep logic in sync with MPIPreferences.jl
    function augment_mpi!(platform)
        # Doesn't need to be `const` since we depend on MPIPreferences so we
        # invalidate the cache when it changes.
        binary = get(preferences, "binary", Sys.iswindows() ? "MicrosoftMPI_jll" : "MPICH_jll")

        abi = if binary == "system"
            get(preferences, "abi")
        elseif binary == "MicrosoftMPI_jll"
            "MicrosoftMPI"
        elseif binary == "MPICH_jll"
            "MPICH"
        elseif binary == "OpenMPI_jll"
            "OpenMPI"
        elseif binary == "MPItrampoline_jll"
            "MPItrampoline"
        else
            error("Unknown binary: $binary")
        end

        if !haskey(platform, "mpi")
            platform["mpi"] = abi
        end
        return platform
    end
"""

using BinaryBuilder, Pkg
using Base.BinaryPlatforms

mpi_abis = (
    ("MPICH", PackageSpec(name="MPICH_jll"), "", !Sys.iswindows) ,
    ("OpenMPI", PackageSpec(name="OpenMPI_jll"), "", !Sys.iswindows),
    ("MicrosoftMPI", PackageSpec(name="MicrosoftMPI_jll"), "", Sys.iswindows),
    ("MPItrampoline", PackageSpec(name="MPItrampoline_jll"), "", !Sys.iswindows)
)

function augment_platforms(platforms)
    all_platforms = AbstractPlatform[]
    dependencies = []
    for (abi, pkg, compat, f) in mpi_abis
        pkg_platforms = deepcopy(filter(f, platforms))
        foreach(pkg_platforms) do p
            p[tag_name] = abi
        end
        append!(all_platforms, pkg_platforms)
        push!(dependencies, Dependency(pkg; compat, platforms=pkg_platforms))
    end
    # NOTE: packages using this platform tag, must depend on MPIPreferences otherwise
    #       they will not be invalidated when the Preference changes.
    push!(dependencies, Dependency(PackageSpec(name="MPIPreferences", uuid="3da0fdf6-3ccc-4f1b-acd9-58baa6c99267"); compat="0.1"))
    return all_platforms, dependencies
end

end

max_stars_repo_name: MichelJuillard/Yggdrasil

max_issues_repo_name: MichelJuillard/Yggdrasil

max_forks_repo_name: MichelJuillard/Yggdrasil


text:
"""
# Module Perf

Leverage the 'perf' capabilities of the Linux kernel from within Julia.
"""
module Perf

using CpuId: cpufeature

include("kernel.jl")     # Kernel API

#
# Howto create a group leader: http://stackoverflow.com/a/42092180/5294141
#

#=

Initialize group leader:

struct perf_event_attr pea;
int fd1, fd2;
uiwt64_t id1, id2;

memset(&pea, 0, sizeof(struct perf_event_attr));
pea.type = PERF_TYPE_HARDWARE;
pea.size = sizeof(struct perf_event_attr);
pea.config = PERF_COUNT_HW_CPU_CYCLES;
pea.disabled = 1;
pea.exclude_kernel = 1;
pea.exclude_hv = 1;
pea.read_format = PERF_FORMAT_GROUP | PERF_FORMAT_ID;
fd1 = syscall(__NR_perf_event_open, &pea, 0, -1, -1, 0);

Get identifier of first counter

ioctl(fd1, PERF_EVENT_IOC_ID, &id1);

Initialize group members:

memset(&pea, 0, sizeof(struct perf_event_attr));
pea.type = PERF_TYPE_SOFTWARE;
pea.size = sizeof(struct perf_event_attr);
pea.config = PERF_COUNT_SW_PAGE_FAULTS;
pea.disabled = 1;
pea.exclude_kernel = 1;
pea.exclude_hv = 1;
pea.read_format = PERF_FORMAT_GROUP | PERF_FORMAT_ID;
fd2 = syscall(__NR_perf_event_open, &pea, 0, -1, fd1, 0);
                                            // ^^^^^^
ioctl(fd2, PERF_EVENT_IOC_ID, &id2);

Measure:

ioctl(fd1, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP);
ioctl(fd1, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP);
do_something();
ioctl(fd1, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP);

# Read results:

struct read_format {
    uint64_t nr;
    struct {
        uint64_t value;
        uint64_t id;
    } values[/*2*/];
};

char buf[4096];
struct read_format* rf = (struct read_format*) buf;
uint64_t val1, val2;

read(fd1, buf, sizeof(buf));
for (i = 0; i < rf->nr; i++) {
  if (rf->values[i].id == id1) {
    val1 = rf->values[i].value;
  } else if (rf->values[i].id == id2) {
    val2 = rf->values[i].value;
  }
}
printf("cpu cycles: %"PRIu64"\n", val1);
printf("page faults: %"PRIu64"\n", val2);

=#

"""
    perf_event_paranoia() ::Int

Determine the kernel's paranoia level from
`/proc/sys/kernel/perf_event_paranoia`.  If the file doesn't exist, and, thus,
the kernel doesn't provide performance events, then a `SystemError` is thrown.
"""
perf_event_paranoia() ::Int =
            open( x-> readstring(x) |> y->parse(Int,y),
                 "/proc/sys/kernel/perf_event_paranoid")

"""
    perf_permits_rdpmc() ::Bool

Determine whether the kernel direct access to performance counters via a
`rdpmc` instruction by reading `/sys/bus/event_source/devices/cpu/rdpmc`.
This file must have a value larger zero to indicate reading being permitted.

See `man 2 perf_event_open` for details.
"""
perf_permits_rdpmc() ::Bool =
        0 < open( x-> readstring(x) |> y->parse(Int,y),
                 "/sys/bus/event_source/devices/cpu/rdpmc")

"""
Module initialisation and requirement checking.
"""
function __init__()

    #
    # Here we try to initialize performance counters, and provide
    # instructions if something doesn't seem correct.
    #

    println("Checking availability of performance monitoring counters...")

    # Does the CPU indicate availability of PMC?
    !cpufeature(:PDCM) || warning("CPU does not support PMC.")

    # Is /proc/sys/kernel/perf_event_paranoid present?
    # And does it permit access by containing a value of 2 or less?

    # Did we already build the kernel constant extraction file?

    # Does the current kernel match the extraction file?

    # Create the default hardware counters, but leave them disabled.
    # We place these defaults into one single counter group.

end

end # module Perf


text:
using Statistics
using Distributions
using DrWatson
@quickactivate
include("crit_diag_functions.jl")

# R = mean ranks, n = number of datasets, k = number of models
friedman_test_statistic(R::Vector,n::Int,k::Int) = 12*n/(k*(k+1))*(sum(R.^2) - k*(k+1)^2/4)
# k = number of models
crit_chisq(α::Real, df::Int) = quantile(Chisq(df), 1-α)
friedman_critval(α::Real, k::Int) = crit_chisq(α/2, k-1)

# k = number of groups, df = (N - k) = (total samples - k)
crit_srd(α::Real, k::Real, df::Real) =
	(isnan(k) | isnan(df)) ? NaN : quantile(StudentizedRange(df, k), 1-α)
nemenyi_cd(k::Int, n::Int, α::Real) = sqrt(k*(k+1)/(6*n))*crit_srd(α, k, Inf)/sqrt(2)

# this checks out with our kdd18 paper
nd = 35
nm = 6
α = 0.05
nemenyi_cd(nm, nd, α) # = 1.275

# this is for our new paper
nd = 40
nm = 24
α = 0.05
ncv5 = nemenyi_cd(nm, nd, α) # = 5.75
α = 0.1
ncv10 = nemenyi_cd(nm, nd, α) # = 5.40

nd = 40
#
model_names = ["aae", "avae", "gano", "vae", "wae", "abod", "hbos", "if", "knn", "loda", "lof", "osvm",
"pidf", "maf", "rnvp", "sptn", "fmgn", "gan", "mgal", "dagm", "dsvd", "rpn", "vaek", "vaeo"]
model_names_gray = ["aae", "avae", "gano", "vae", "wae", "\\textcolor{gray}{abod}",
"\\textcolor{gray}{hbos}", "\\textcolor{gray}{if}", "\\textcolor{gray}{knn}", "\\textcolor{gray}{loda}",
"\\textcolor{gray}{lof}", "\\textcolor{gray}{osvm}", "\\textcolor{gray}{pidf}",
"maf", "rnvp", "sptn", "fmgn", "gan", "mgal", "dagm", "dsvd", "rpn", "vaek", "vaeo"]
model_ranks = [6.9, 13.4, 10.3, 6.5, 7.0, 11.4, 14.9, 14.5, 8.4, 16.1, 11.4, 2.9, 14.3, 8.9, 9.0, 10.7,
	11.4, 12.0, 22.3, 19.8, 16.2, 12.1, 11.0, 6.8]

plot5 = ranks2tikzcd(model_ranks, model_names_gray,
	nemenyi_cd(length(model_ranks), nd, 0.05);
	scale=0.45, vpad=0.6, levelwidth=0.03, vdist=0.15)
string2file(datadir("paper_results/tabular_auc_auc_cdd5.tex"), plot5)

plot10 = ranks2tikzcd(model_ranks, model_names_gray,
	nemenyi_cd(length(model_ranks), nd, 0.1);
	scale=0.45, vpad=0.6, levelwidth=0.03, vdist=0.15)
string2file(datadir("paper_results/tabular_auc_auc_cdd10.tex"), plot10)

# clean tabular
model_ranks = [12.5, 16.2, 11.6, 10.4, 9.0, 8.6, 11.3, 8.6, 6.0, 18.3, 8.0, 6.0, 12.5, 7.2, 7.1, 7.4,
14.8, 15.4, 20.6, 19.7, 16.6, 19.5, 10.7, 11.6]
plot10 = ranks2tikzcd(model_ranks, model_names_gray,
	nemenyi_cd(length(model_ranks), nd, 0.1);
	scale=0.65, vpad=0.4, levelwidth=0.03, vdist=0.13)
string2file(datadir("paper_results/tabular_clean_auc_auc_cdd10.tex"), plot10)

# image data - statistic
img_model_names = ["aae", "gano", "skip", "vae", "wae", "\\textcolor{gray}{knn}",
"\\textcolor{gray}{osvm}", "fano", "fmgn", "dsvd", "vaek", "vaeo"]
img_model_ranks = [4.0, 9.5, 10.5, 2.7, 2.5, 5.3, 6.0, 3.5, 4.1, 6.9, 4.8, 4.1]

nd = 19
nm = length(img_model_names)
plot5img = ranks2tikzcd(img_model_ranks,img_model_names,
	nemenyi_cd(length(img_model_ranks), nd, 0.05))
string2file(datadir("paper_results/images_auc_auc_cdd5.tex"), plot5img)

plot10img = ranks2tikzcd(img_model_ranks,img_model_names,
	nemenyi_cd(length(img_model_ranks), nd, 0.1))
string2file(datadir("paper_results/images_auc_auc_cdd10.tex"), plot10img)

ncv10 = nemenyi_cd(nm, nd, 0.1) # = 3.54

nd = 37
img_model_ranks = [3.6, 5.1, 9.1, 3.1, 2.9, 5.4, 6.4, 3.4, 7.5, 4.0, 5.7, 4.9]
nm = length(img_model_names)
ncv10 = nemenyi_cd(nm, nd, 0.1) # = 2.54
plot10img = ranks2tikzcd(img_model_ranks,img_model_names, ncv10)
string2file(datadir("paper_results/images_auc_auc_cdd10_granular.tex"), plot10img)

# statistic - clean
img_model_ranks = [3.2, 5.2, 9.1, 3.6, 4.1, 3.8, 5.7, 6.0, 10.1, 7.5, 4.7, 5.2]
plot10img = ranks2tikzcd(img_model_ranks,img_model_names,
	nemenyi_cd(length(img_model_ranks), nd, 0.1))
string2file(datadir("paper_results/images_clean_auc_auc_cdd10_granular.tex"), plot10img)

# image data - semantic
img_model_names = ["aae", "gano", "skip", "vae", "wae", "\\textcolor{gray}{knn}",
"\\textcolor{gray}{osvm}", "fano", "fmgn", "dsvd", "vaek", "vaeo"]
img_model_ranks = [6.5, 10.0, 12.0, 4.0, 5.0, 9.5, 8.0, 4.5, 1.0, 6.0, 4.0, 5.5]

nd = 2
plot5img = ranks2tikzcd(img_model_ranks,img_model_names,
	nemenyi_cd(length(img_model_ranks), nd, 0.05))
string2file(datadir("paper_results/images_auc_auc_cdd5_semantic.tex"), plot5img)

plot10img = ranks2tikzcd(img_model_ranks,img_model_names,
	nemenyi_cd(length(img_model_ranks), nd, 0.1))
string2file(datadir("paper_results/images_auc_auc_cdd10_semantic.tex"), plot10img)

ncv10 = nemenyi_cd(12, 2, 0.1) # = 10.92

img_model_ranks = [5.0, 8.1, 7.8, 3.8, 4.9, 7.8, 8.3, 4.8, 2.4, 6.2, 5.6, 6.6]
nd = 20
nm = length(img_model_ranks)
ncv10 = nemenyi_cd(nm, nd, 0.1) # = 3.45
plot10img = ranks2tikzcd(img_model_ranks,img_model_names, ncv10)
string2file(datadir("paper_results/images_auc_auc_cdd10_semantic_granular.tex"), plot10img)

# semantic - clean
img_model_ranks = [5.4, 5.2, 8.8, 7.0, 7.2, 4.8, 4.6, 6.6, 9.4, 3.4, 4.7, 4.8]
nd = 20
nm = length(img_model_ranks)
ncv10 = nemenyi_cd(nm, nd, 0.1) # = 3.45
plot10img = ranks2tikzcd(img_model_ranks,img_model_names, ncv10)
string2file(datadir("paper_results/images_clean_auc_auc_cdd10_semantic_granular.tex"), plot10img)

# diff oocsvm - vae
difff = 6.5 - 2.9 # 3.6
nemenyi_cd(24, 82, 0.1)

# this is all outdated
model_ranks_tpr5 = [10.5, 12.5, 9.9, 7.6, 8.2, 12.0, 14.7, 15.9, 9.8, 17.9, 10.9, 10.2,
	3.0, 13.9, 10.0, 9.8, 11.8, 9.6, 10.0, 18.9, 15.9, 11.2, 6.4]
plot5tpr = ranks2tikzcd(model_ranks_tpr5, model_names_gray,
	nemenyi_cd(length(model_ranks), nd, 0.05);
	scale=0.55, vpad=0.5, levelwidth=0.03, vdist=0.13)
string2file("tabular_tpr5_tpr5_cdd5.tex", plot5tpr)

plot10tpr = ranks2tikzcd(model_ranks_tpr5, model_names_gray,
	nemenyi_cd(length(model_ranks), nd, 0.1);
	scale=0.55, vpad=0.5, levelwidth=0.03, vdist=0.13)
string2file("tabular_tpr5_tpr5_cdd10.tex", plot10tpr)

# TODO also for more?
plot10img = ranks2tikzcd(img_model_ranks,img_model_names,
	nemenyi_cd(length(img_model_ranks), 4, 0.1))
string2file("images_auc_auc_cdd10_4_datasets.tex", plot10img)

# img data per class
include("crit_diag_functions.jl")
nd = 40
img_model_names = ["aae", "gano", "skip", "vae", "wae", "\\textcolor{gray}{knn}",
"\\textcolor{gray}{osvm}", "fano", "fmgn", "dsvd", "vaek", "vaeo"]
img_model_ranks_per_class = [5.0, 6.8, 10.6, 5.8, 6.2, 6.3, 7.4, 6.4,
	4.6, 6.5, 3.9, 3.2]
plot10img = ranks2tikzcd(img_model_ranks_per_class,img_model_names,
	nemenyi_cd(length(img_model_ranks_per_class), nd, 0.1))
string2file("images_auc_auc_cdd10_per_class.tex", plot10img)


text:
using ChainRulesCore
using ChainRulesTestUtils
using FiniteDifferences
using ITensors
using Random

#
# For ITensor compatibility with FiniteDifferences
#

function FiniteDifferences.to_vec(A::ITensor)
  # TODO: generalize to sparse tensors
  # TODO: define `itensor([1.0])` as well
  # as `itensor([1.0], ())` to help with generic code.
  function vec_to_ITensor(x)
    return isempty(inds(A)) ? ITensor(x[]) : itensor(x, inds(A))
  end
  return vec(array(A)), vec_to_ITensor
end

function FiniteDifferences.to_vec(x::Index)
  return (Bool[], _ -> x)
end

function FiniteDifferences.to_vec(x::Tuple{Vararg{Index}})
  return (Bool[], _ -> x)
end

function FiniteDifferences.to_vec(x::Vector{<:Index})
  return (Bool[], _ -> x)
end

function FiniteDifferences.to_vec(x::Pair{<:Tuple{Vararg{Index}},<:Tuple{Vararg{Index}}})
  return (Bool[], _ -> x)
end

function ChainRulesTestUtils.rand_tangent(rng::AbstractRNG, A::ITensor)
  # TODO: generalize to sparse tensors
  return isempty(inds(A)) ? ITensor(randn(eltype(A))) : randomITensor(eltype(A), inds(A))
end

function ChainRulesTestUtils.rand_tangent(rng::AbstractRNG, x::Index)
  return NoTangent()
end

function ChainRulesTestUtils.rand_tangent(rng::AbstractRNG, x::Tuple{Vararg{Index}})
  return NoTangent()
end

function ChainRulesTestUtils.rand_tangent(
  rng::AbstractRNG, x::Pair{<:Tuple{Vararg{Index}},<:Tuple{Vararg{Index}}}
)
  return NoTangent()
end

function ChainRulesTestUtils.test_approx(::AbstractZero, x::Vector{<:Index}, msg; kwargs...)
  return ChainRulesTestUtils.@test_msg msg true
end

# The fallback version would convert to an Array with `collect`,
# which would be incorrect if the indices had different orderings
function ChainRulesTestUtils.test_approx(
  actual::ITensor, expected::ITensor, msg=""; kwargs...
)
  ChainRulesTestUtils.@test_msg msg isapprox(actual, expected; kwargs...)
end


text:
# cd $PBS_O_WORKDIR
# export PATH=$PATH:/home/viniciusp/programas/julia-1.5.0/bin
# source /etc/profile.d/modules.sh
# echo "hostname " `hostname`
# cd $run
#
#
# julia -t 8 /home/viniciusp/doutorado/sub_mddf/mddf_kbi_BF4NO3.jl
#
# parte que eu preciso submeter para rodar o programa
#

#  # Function to calculate mean results taking all the simulations performed
#  function final_results(trajs,results::Vector{CM.Result},solute,solvent,options)
#    for i in 1:length(trajs)
#      trajectory = CM.Trajectory(trajs[i],solute,solvent)
#      results[i] = CM.mddf(trajectory,options)
#    end
#    return CM.merge(results)
#  end
#
## data
#  ils   = [ "EMIMBF4NO3"]     # , "EMIMDCANO3", "EMIMBF4NO3"]
#  concs = ["1.00", "1.50" , "2.00", "2.50", "3.00"]
#  trajs = ["./00/processed.xtc", "./01/processed.xtc", "./02/processed.xtc","./03/processed.xtc", "./04/processed.xtc", "./05/processed.xtc","./06/processed.xtc", "./07/processed.xtc", "./08/processed.xtc","./09/processed.xtc", "./10/processed.xtc", "./11/processed.xtc","./12/processed.xtc", "./13/processed.xtc", "./14/processed.xtc","./15/processed.xtc", "./16/processed.xtc", "./17/processed.xtc","./18/processed.xtc", "./19/processed.xtc", "./20/processed.xtc"]
#
#
#for il in ils # loop through the ionic liquids
#
#    for c in concs # loop through the concentrations simulated
#
#      cd("/home/viniciusp/doutorado/$il/$c")
#
#      # solute - protein
#      atoms   = PDBTools.readPDB("./00/processed.pdb")
#      protein = PDBTools.select(atoms,"not resname SOL and not resname EMI and not resname BF4 and not resname NO3")
#      solute  = CM.Selection(protein,nmols=1)
#
#      options = CM.Options(dbulk=20.,GC=true,GC_threshold=0.5)
#
#      # vectors
#      resultts_wat = Vector{CM.Result}(undef,21)
#      resultts_cat = Vector{CM.Result}(undef,21)
#      resultts_an1 = Vector{CM.Result}(undef,21)
#      resultts_an2 = Vector{CM.Result}(undef,21)
#      resultts_ans = Vector{CM.Result}(undef,21)
#
# # water
#      if isfile("gmd_sol.json")==false
#        water_sel =  PDBTools.select(atoms,"resname SOL")
#        water     =  CM.Selection(water_sel,natomspermol=3)
#
#        results_water = final_results(trajs,resultts_wat,solute,water,options)
#        CM.save(results_water, "gmd_sol.json")
#        CM.write(results_water,"gmd_sol_$c.dat")
#      en
#
#      # cation
#      if isfile("gmd_$(il[1:3]).json")==false
#        cation_sel =  PDBTools.select(atoms,"resname $(il[1:3])")
#        cation     =  CM.Selection(cation_sel,natomspermol=20)
#
#        results_cation = final_results(trajs, resultts_cat, solute, cation, options)
#        CM.save(results_cation,  "gmd_$(il[1:3]).json")
#        CM.write(results_cation, "gmd_$(il[1:3])_$c.dat")
#      end
#
#      # anion 1
#      if isfile("gmd_$(il[5:7]).json")==false
#        anion1_sel =  PDBTools.select(atoms,"resname $(il[5:7])")
#        anion1     =  CM.Selection(anion1_sel,natomspermol=5)
#
#        results_an1 = final_results(trajs,resultts_an1,solute,anion1,options)
#        CM.save(results_an1, "gmd_$(il[5:7]).json")
#        CM.write(results_an1,"gmd_$(il[5:7])_$c.dat")
#      else
#   # anion 2
#      if isfile("gmd_NO3.json")==false
#        anion2_sel =  PDBTools.select(atoms,"resname NO3")
#        anion2     =  CM.Selection(anion2_sel,natomspermol=4)
#
#
#        results_an2 = final_results(trajs,resultts_an2,solute,anion2,options)
#        CM.save(results_an2, "gmd_NO3.json")
#        CM.write(results_an2,"gmd_NO3_$c.dat")
#      end
#
#      # anions as one component
#      if isfile("gmd_$(il[5:7])_$(il[8:10]).json")==false
#        anions_sel =  PDBTools.select(atoms,"resname $(il[5:7]) or resname $(il[8:10])")
#        anions     =  CM.Selection(anions_sel,natomspermol=9)
#
#        results_ans = final_results(trajs,resultts_ans,solute,anions,options)
#        CM.save(results_ans, "gmd_$(il[5:7])_$(il[8:10]).json")
#        CM.write(results_ans,"gmd_$(il[5:7])_$(il[8:10])_$c.dat")
#      end
#    end
#
#  end
#end

# PRECISO FAZER UM FUNÇÃO QUE PEGA MAKE_CMRUN E EXECUTA A PARTIR DOS DADOS
# PRECISO DE UMA FUNCAÇÃO QUE CALCULA O NÚMERO DE ÁTOMOS DE CADA COMPONENTE
#
# using ComplexMixtures, PDBTools; const CM=ComplexMixtures

function make_CMrun(data, systemPDB, natoms::Vector)
  io = open("analysis.jl","w")
  an = data.anion
  cat = data.cation
  println(io,"""
  using PDBTools, ComplexMixtures

  # basic selections
  atoms   = readPDB("$systemPDB")
  prot    = select(atoms, "protein")
  solute  = ComplexMixtures.Selection(prot,nmols=1)

  # selections for solvent components
  solv    = select(atoms,"resname $(cat[1:3])")
  cation = ComplexMixtures.Selection(solv, natomspermol=$(natoms[1]))

  solv    = select(atoms,"resname $(an)")
  anion  = ComplexMixtures.Selection(solv, natomspermol=$(natoms[2]))

  solv    = select(atoms, "resname SOL")
  water  = ComplexMixtures.Selection(solv, natomspermol=3)

  # options for the calculation
  options = ComplexMixtures.Options(dbulk=20.,GC=true,GC_threshold=0.5)

  # cation calculation
  trajectory = ComplexMixtures.Trajectory("processed.xtc",solute,cation)
  results = ComplexMixtures.mddf(trajectory, options)
  ComplexMixtures.save(results,"$(cat[1:3]).json")
  ComplexMixtures.write(results,"gmd_$(cat[1:3]).dat")

  # anion calculation
  trajectory = ComplexMixtures.Trajectory("processed.xtc",solute,anion)
  results = ComplexMixtures.mddf(trajectory, options)
  ComplexMixtures.save(results,"$(an).json")
  ComplexMixtures.write(results,"gmd_$(an).dat")

  # water calculation
  trajectory = ComplexMixtures.Trajectory("processed.xtc",solute,water)
  results = ComplexMixtures.mddf(trajectory, options)
  ComplexMixtures.save(results,"SOL.json")
  ComplexMixtures.write(results,"gmd_SOL.dat")

""")

end

function make_CMrun2(data, systemPDB, natoms::Vector)
  io = open("analysis.jl","w")
  an1 = data.anion1
  an2 = data.anion2
  cat = data.cation

  # DCA is not the correct name for the selection of dycianamide.
  if an1 == "DCA"
    an1 = "NC"
  elseif an2 == "DCA"
    an2 = "NC"
  end

  println(io,"""
  using PDBTools, ComplexMixtures

  # basic selections
  atoms   = readPDB("$systemPDB")
  prot    = select(atoms, "protein")
  solute  = ComplexMixtures.Selection(prot,nmols=1)

  # selections for solvent components
  solv    = select(atoms,"resname $(cat[1:3])")
  cation  =  ComplexMixtures.Selection(solv, natomspermol=$(natoms[1]))

  solv    = select(atoms,"resname $(an1)")
  anion1  = ComplexMixtures.Selection(solv, natomspermol=$(natoms[2]))

  solv    = select(atoms,"resname $(an2)")
  anion2  = ComplexMixtures.Selection(solv, natomspermol=$(natoms[3]))

  solv    = select(atoms, "resname SOL")
  water  =  ComplexMixtures.Selection(solv, natomspermol=3)

  # options for the calculation
  options = ComplexMixtures.Options(dbulk=20.,GC=true,GC_threshold=0.5)

  # cation calculation
  trajectory = ComplexMixtures.Trajectory("processed.xtc",solute,cation)
  results = ComplexMixtures.mddf(trajectory, options)
  ComplexMixtures.save(results,"$(cat[1:3]).json")
  ComplexMixtures.write(results,"gmd_$(cat[1:3]).dat")

  # anion 1 calculation
  trajectory = ComplexMixtures.Trajectory("processed.xtc",solute,anion1)
  results = ComplexMixtures.mddf(trajectory, options)
  ComplexMixtures.save(results,"$(an1).json")
  ComplexMixtures.write(results,"gmd_$(an1).dat")

  # anion 2 calculation
  trajectory = ComplexMixtures.Trajectory("processed.xtc",solute,anion2)
  results = ComplexMixtures.mddf(trajectory, options)
  ComplexMixtures.save(results,"$(an2).json")
  ComplexMixtures.write(results,"gmd_$(an2).dat")

  # water calculation
  trajectory = ComplexMixtures.Trajectory("processed.xtc",solute,water)
  results = ComplexMixtures.mddf(trajectory, options)
  ComplexMixtures.save(results,"SOL.json")
  ComplexMixtures.write(results,"gmd_SOL.dat")

""")

end

# function that will aplly make_CMrun with data and the number of atoms of each solvent molecule
function analyzeIN(pdb_dir, data)

  cationPDB = "$(data.cation)_VSIL.pdb"
  anionPDB  = "$(data.anion)_VSIL.pdb"

  ncation = length(PDBTools.readPDB("$pdb_dir/$cationPDB"))
  nanion  = length(PDBTools.readPDB("$pdb_dir/$anionPDB"))

  protein = data.protein #"processed.pdb"
  systemPDB = "processed.pdb"     #"$(pdb_dir)/$protein"
  make_CMrun(data,systemPDB,[ncation, nanion])

end

# function that will aplly make_CMrun with data and the number of atoms of each solvent molecule
function analyzeIN2(pdb_dir, data)

  cationPDB = "$(data.cation)_VSIL.pdb"
  anion1PDB = "$(data.anion1)_VSIL.pdb"
  anion2PDB = "$(data.anion2)_VSIL.pdb"

  ncation = length(PDBTools.readPDB("$pdb_dir/$cationPDB"))
  nanion1 = length(PDBTools.readPDB("$pdb_dir/$anion1PDB"))
  nanion2  = length(PDBTools.readPDB("$pdb_dir/$anion2PDB"))

  protein = data.protein
  systemPDB = "processed.pdb"             #"$(pdb_dir)/$protein"
  make_CMrun2(data,systemPDB,[ncation, nanion1, nanion2])

end


text:
# This is executed inside GitLab, so all the variables from there should be available here.

# Download artifacts
# Unzip and make into a gist
# Send response

# Just for testing
const ENV["CI_JOB_ID"]="359540097"
const ENV["CI_JOB_NAME"]="julia:1.1"

function respond(repo_name = "Flux.jl")
  dict = try
    artifacts_name = "artifacts_$(ENV["CI_JOB_ID"]).zip"
    download("https://gitlab.com/JuliaGPU/$repo_name/-/jobs/artifacts/master/download?job=$(ENV["CI_JOB_NAME"])", artifacts_name)
    run(`unzip $artifacts_name`)
    files = glob("*.ipynb", "notebooks")

    gist_params = Dict("description" => "Build Results for $(ENV["CI_JOB_NAME"])",
                        "public" => true,
                        "files" => Dict(last(split(f,"/")) => Dict("content" => read(f,String),) for f in files))

    g = GitHub.create_gist(auth = myauth, params = gist_params)

    dict = Dict("body" => "Find the artifacts for `$(ENV["CI_JOB_NAME"])` at $(g.html_url)")
  catch ex
    dict = Dict("body" => "Pipeline Failed. Check the results at $(ENV["CI_PIPELINE_URL"])")
  end
  # GitHub.create_comment(GitHub.Repo(ENV["REPO_NAME"]), ENV["PRID"], :issue, auth = myauth, params = dict)
  GitHub.create_comment(GitHub.Repo("dhairyagandhi96/maskrcnn"), 3, :issue, auth = myauth, params = dict)
end


text:
"""
    high_cutoff(x) -> Hz
    high_cutoff!(x, val)

Frequencies used for the high-pass filter applied to the channel in Hz.
"""
@defprop HighCutOff{:high_cutoff}::F64Hertz

"""
    low_cutoff(x) -> Hz
    low_cutoff!(x, val)

Frequencies used for the low-pass filter applied to the channel in Hz.
If no low-pass filter applied, use n/a. Note that hardware anti-aliasing in A/D
conversion of all MEG/EEG electronics applies a low-pass filter; specify its
frequency here if applicable.
"""
@defprop LowCutOff{:low_cutoff}::F64Hertz

"""
    notch_filter(x) -> Hz
    notch_filter!(x, val)

Frequencies used for the notch filter applied to the channel, in Hz.
"""
@defprop NotchFilter{:notch_filter}::F64Hertz

"""
    software_filters(x)
    software_filters!(x, val)

Temporal software filters applied.
"""
@defprop SoftwareFilters{:software_filters}

"""
    electrode_groups(x)
    electrode_groups!(x, val)

Field to describe the way electrodes are grouped into strips, grids or depth probes
e.g., `Dict(:grid1 => "10x8 grid on left temporal pole", :strip2 => "1x8 electrode strip on xxx")`.
"""
@defprop ElectrodeGroups{:electrode_groups}

"""
    ground_electrode(x)
    ground_electrode!(x, val)

Description of the location of the ground electrode (\"placed on right mastoid (M2)\").
"""
@defprop GroundElectrode{:ground_electrode}

"""
    placement_scheme(x)
    placement_scheme!(x, val)

Freeform description of the placement of the iEEG electrodes.
Left/right/bilateral/depth/surface (e.g., "left frontal grid and bilateral
hippocampal depth" or "surface strip and STN depth" or "clinical indication
bitemporal, bilateral temporal strips and left grid").
"""
@defprop PlacementScheme{:placement_scheme}

"""
    power_line_frequency(x)
    power_line_frequency!(x, val)

`power_line_frequency::F64Hz`: Frequency (in Hz) of the power grid at the geographical location of the EEG instrument (i.e., 50 or 60).
"""
@defprop PowerlineFrequency{:power_line_frequency}::F64Hertz

"""
    dewar_position(x)
    dewar_position!(x, val)

Position of the dewar during the MEG scan: upright, supine or degrees of angle
from vertical: for example on CTF systems, upright=15°, supine = 90°.
"""
@defprop DewarPosition{:dewar_position}::IntDegree

#### iEEG
"""
    electrical_stimulation(x)
    electrical_stimulation!(x, val)

Boolean field to specify if electrical stimulation was done during the recording
(options are `true` or `false`). Parameters for event-like stimulation should be
specified in the _events.tsv file.
"""
@defprop ElectricalStimulation{:electrical_stimulation}::Bool

"""
    electrical_stimulation_parameters(x)
    electrical_stimulation_parameters!(x, val)

Free form description of stimulation parameters, such as frequency, shape etc.
Specific onsets can be specified in the _events.tsv file. Specific shapes can be
described here in freeform text.
"""
@defprop ElectricalStimulationParameters{:electrical_stimulation_parameters}::String

"""
    epoch_length(x)
    epoch_length!(x, val)

Duration of individual epochs in seconds (e.g., 1) in case of epoched data. If
recording was continuous or discontinuous, leave out the field.
"""
@defprop EpochLength{:epoch_length}::F64Second


text:
include("scp/scp_gusto.jl")
include("scp/scp_trajopt.jl")
# include("scp/scp_mao.jl")

function add_constraint_category!(SCPCCat, func, dimtype::Symbol, ind_time, ind_other...)
	func_name = Symbol(func)
	cc = ConstraintCategory(func, dimtype, ind_time, ind_other)
	haskey(SCPCCat, func_name) ? push!(SCPCCat[func_name], cc) : SCPCCat[func_name] = [cc]
end

function add_constraint_category!(SCPCCat, func, goal::G, dimtype::Symbol, ind_other...) where G <: Goal{T} where T
	func_name = Symbol(func)
	cc = ConstraintCategory(func, dimtype, goal.ind_coordinates, ind_other, goal)
	haskey(SCPCCat, func_name) ? push!(SCPCCat[func_name], cc) : SCPCCat[func_name] = [cc]
end


text:
# Autogenerated wrapper script for SentencePiece_jll for x86_64-linux-musl-cxx03
export libsentencepiece, libsentencepiece_train, spm_decode, spm_encode, spm_export_vocab, spm_normalize, spm_train

using gperftools_jll
JLLWrappers.@generate_wrapper_header("SentencePiece")
JLLWrappers.@declare_library_product(libsentencepiece, "libsentencepiece.so.0")
JLLWrappers.@declare_library_product(libsentencepiece_train, "libsentencepiece_train.so.0")
JLLWrappers.@declare_executable_product(spm_decode)
JLLWrappers.@declare_executable_product(spm_encode)
JLLWrappers.@declare_executable_product(spm_export_vocab)
JLLWrappers.@declare_executable_product(spm_normalize)
JLLWrappers.@declare_executable_product(spm_train)
function __init__()
    JLLWrappers.@generate_init_header(gperftools_jll)
    JLLWrappers.@init_library_product(
        libsentencepiece,
        "lib/libsentencepiece.so",
        RTLD_LAZY | RTLD_DEEPBIND,
    )

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

    JLLWrappers.@init_executable_product(
        spm_decode,
        "bin/spm_decode",
    )

    JLLWrappers.@init_executable_product(
        spm_encode,
        "bin/spm_encode",
    )

    JLLWrappers.@init_executable_product(
        spm_export_vocab,
        "bin/spm_export_vocab",
    )

    JLLWrappers.@init_executable_product(
        spm_normalize,
        "bin/spm_normalize",
    )

    JLLWrappers.@init_executable_product(
        spm_train,
        "bin/spm_train",
    )

    JLLWrappers.@generate_init_footer()
end  # __init__()


text:
# TODO MOVE to StatsDiscretizations

function _midpoints(discr::BoundedIntervalDiscretizer)
    StatsBase.midpoints(discr.grid)
end

function Base.count(discr::BoundedIntervalDiscretizer, zs::AbstractArray)
    cnts = zeros(Int, length(discr))
    for z in zs
        idx = findfirst(discr, z)
        cnts[idx] += 1
    end
    cnts
end

struct McCraryFanGijbels <: BandwidthSelector end

"""
    McCraryTest(bin::Union{Real,Nothing} = nothing, bw::Union{Real,Nothing} = nothing)

The McCrary (2008) test for manipulation in the running variable for a sharp
regression discontinuity estimator.

The `binwidth` object is the width of each bin. The default is 2*sd(runvar)*length(runvar)^(-0.5), following McCrary 2008.

The `bandwidth` object is the bandwidth. The default uses the calculation from McCrary (2008), pg. 705.

## Reference
McCrary, J. (2008).
Manipulation of the running variable in the regression discontinuity design: A density test.
Journal of econometrics, 142(2), 698-714.
"""
Base.@kwdef struct McCraryTest{K,H,W}
    binwidth::W = McCraryBinwidth()
    bandwidth::H = McCraryFanGijbels()
    kernel::K = SymTriangularDist()
end

# TODO: General kernels
# 3.20 and 3.22 of Fan and Gijbels.
#bandwidth(h::Number, kernel, rddata)
function _fan_gijbels_kernel_constant(kernel::SymTriangularDist)
    3.348
end

function bandwidth(
    method::McCraryTest{K, McCraryFanGijbels},
    left_tbl,
    right_tbl
    ) where {K}

    κ = _fan_gijbels_kernel_constant(method.kernel)
    c = left_tbl.cutoff

    #left part
    left_ols = lm(@formula(Ys ~ Zs + Zs^2 + Zs^3 + Zs^4 ), left_tbl)
    left_coef = coef(left_ols)
    left_mse =  deviance(left_ols) / dof_residual(left_ols)

    lf = 2 * left_coef[3] .+ 6 * left_coef[4] * left_tbl.Zs + 12 * left_coef[5] * left_tbl.Zs.^2
    lh = κ * (left_mse * (c - left_tbl.Zs[1]) / sum(abs2, lf))^(1/5)

    # Right part
    right_ols = lm(@formula(Ys ~ Zs + Zs^2 + Zs^3 + Zs^4 ), right_tbl)
    right_coef = coef(right_ols)
    right_mse =  deviance(right_ols) / dof_residual(right_ols)

    rf = 2 * right_coef[3] .+ 6 * right_coef[4] * right_tbl.Zs + 12 * right_coef[5] * right_tbl.Zs.^2
    rh = κ * (right_mse * (right_tbl.Zs[end] - c) / sum(abs2, rf))^(1/5)

    # Taking the average
    h = (rh + lh)/2
end

Base.@kwdef struct FittedMcCraryTest{T,V,C,M}
    b::T
    h::T
    θhat::T
    σθ::T
    pval::T
    fitted_res::V
    coeftable::C
    method::M
end

"""
    fit(method::McCraryTest, data::RunningVariable)

Test for manipulation in the running variable following McCrary (2008).

# Arguments

- `data::RunningVariable`: The running variable.

# Returns
Returns  a FittedMcCraryTest object with fields `b` and `h` which are the bin size
and the bandwidth used, respectively. Also, it returns `θhat`, `σθ`, `pval`, `Js`,
and `fitted_res` which are the estimated difference in the intercepts,
the standard error of the estimate, the pval of the test H0: θhat = 0,
the number of bins used, and the fitted values for the support of the running variable
(as a DataFrame). Finally, `coeftable` provides an object of type CoefTable for the estimator.
"""
function fit(method::McCraryTest, Zs::RunningVariable)

    cutoff = Zs.cutoff
    treated_symbol = Zs.treated

    n = length(Zs)

    kernel(t) = pdf(method.kernel, t)

    b = binwidth(method.binwidth, Zs)
    discr = StatsDiscretizations.samplehull_discretizer(Zs, b)

    Zs_untreated = Zs[Untreated()]
    Zs_treated = Zs[Treated()]

    wts_untreated = count(discr.untreated, Zs_untreated)
    wts_treated = count(discr.treated, Zs_treated)

    untreated_tbl = RDData(
        wts_untreated ./ (n*b),
        RunningVariable(_midpoints(discr.untreated), cutoff, treated_symbol)
    )

    treated_tbl = RDData(
        wts_treated ./ (n*b),
        RunningVariable(_midpoints(discr.treated), cutoff, treated_symbol)
    )

    if Zs.treated ∈ (:≥, :>)
        h = bandwidth(method, untreated_tbl, treated_tbl)
    elseif Zs.treated ∈ (:<, :≤)
        h = bandwidth(method, treated_tbl, untreated_tbl)
    end

    kernel_wts_untreated = kernel.( untreated_tbl.ZsC ./ h)
    kernel_wts_treated = kernel.( treated_tbl.ZsC ./ h)

    fitted_lm_untreated = fit(LinearModel, @formula(Ys ~ ZsC), untreated_tbl, wts = kernel_wts_untreated)
    fitted_lm_treated = fit(LinearModel, @formula(Ys ~ ZsC), treated_tbl, wts = kernel_wts_treated)

    untreated_f_hat = coef(fitted_lm_untreated)[1]
    treated_f_hat = coef(fitted_lm_treated)[1]

    # The estimates:
    θhat = log(treated_f_hat) - log(untreated_f_hat)
    σθ = sqrt((1 / (n * h)) * (24 / 5) * ( (1 / untreated_f_hat) + (1 / treated_f_hat)))
    z = θhat / σθ
    pval = 2(1 -  cdf(Normal(), abs(z)))

    # Creating the table
    res = [θhat σθ z pval]
    colnms = ["θ̂"; "σ̂"; "z"; "p-val"]
    rownms = ["McCrary Test"]
    coeftbl = CoefTable(res, colnms, rownms, 4, 3)

FittedMcCraryTest(
        b=b,
        h=h,
        θhat=θhat,
        σθ=σθ,
        pval=pval,
        fitted_res=nothing,
        coeftable=coeftbl,
        method=method
    )

end

function Base.show(io::IO, rdd_fit::FittedMcCraryTest)
    println(io, "The McCrary (2008) test for manipulation in the")
    println(io, "running variable for RDD.")
    println(io, "          ⋅⋅⋅⋅ " * "Bin size: ", string(round(rdd_fit.b, digits=4)))
    println(io, "          ⋅⋅⋅⋅ " * "Bandwidth size: ", string(round(rdd_fit.h, digits=4)))
    Base.show(io, rdd_fit.coeftable)
end


text:
@testset "ArviZ.Dataset" begin
    data = load_arviz_data("centered_eight")
    dataset = data.posterior
    @test dataset isa ArviZ.Dataset

    @testset "construction" begin
        pydataset = PyObject(dataset)
        @test ArviZ.Dataset(pydataset) isa ArviZ.Dataset
        @test PyObject(ArviZ.Dataset(pydataset)) === pydataset
        @test ArviZ.Dataset(dataset) === dataset
    end

    @testset "properties" begin
        @test length(propertynames(dataset)) > 1
    end

    @testset "conversion" begin
        @test pyisinstance(PyObject(dataset), ArviZ.xarray.Dataset)
        dataset4 = convert(ArviZ.Dataset, PyObject(dataset))
        @test dataset4 isa ArviZ.Dataset
        @test PyObject(dataset4) === PyObject(dataset)
    end
end

@testset "ArviZ.convert_to_dataset" begin
    rng = MersenneTwister(42)

    @testset "ArviZ.convert_to_dataset(::ArviZ.Dataset; kwargs...)" begin
        data = load_arviz_data("centered_eight")
        dataset = data.posterior
        @test ArviZ.convert_to_dataset(dataset) isa ArviZ.Dataset
        @test ArviZ.convert_to_dataset(dataset) === dataset
    end

    @testset "ArviZ.convert_to_dataset(::InferenceData; kwargs...)" begin
        A = Dict("A" => randn(rng, 2, 10, 2))
        B = Dict("B" => randn(rng, 2, 10, 2))
        dataA = ArviZ.convert_to_dataset(A)
        dataB = ArviZ.convert_to_dataset(B)
        idata = InferenceData(posterior = dataA, prior = dataB)

        ds1 = ArviZ.convert_to_dataset(idata)
        @test ds1 isa ArviZ.Dataset
        @test "A" ∈ [ds1.keys()...]

        ds2 = ArviZ.convert_to_dataset(idata; group = :prior)
        @test ds2 isa ArviZ.Dataset
        @test "B" ∈ [ds2.keys()...]
    end

    @testset "ArviZ.convert_to_dataset(::Dict; kwargs...)" begin
        data = Dict("x" => randn(rng, 4, 100), "y" => randn(rng, 4, 100))
        dataset = ArviZ.dict_to_dataset(data)
        @test dataset isa ArviZ.Dataset
        @test "x" ∈ [dataset.keys()...]
        @test "y" ∈ [dataset.keys()...]
    end
end

@testset "dict to dataset roundtrip" begin
    rng = MersenneTwister(42)
    J = 8
    K = 6
    nchains = 4
    ndraws = 500
    vars = Dict(
        "a" => randn(rng, nchains, ndraws),
        "b" => randn(rng, nchains, ndraws, J, K),
    )
    coords = Dict("bi" => 1:J, "bj" => 1:K)
    dims = Dict("b" => ["bi", "bj"])
    attrs = Dict("mykey" => 5)

    ds = ArviZ.dict_to_dataset(vars; library = :MyLib, coords = coords, dims = dims, attrs = attrs)
    @test ds isa ArviZ.Dataset
    vars2, kwargs = ArviZ.dataset_to_dict(ds)
    for (k, v) in vars
        @test k ∈ keys(vars2)
        @test vars2[k] ≈ v
    end
    @test kwargs.coords == coords
    @test kwargs.dims == dims
    for (k, v) in attrs
        @test k ∈ keys(kwargs.attrs)
        @test kwargs.attrs[k] == v
    end
    @test "inference_library" ∈ keys(kwargs.attrs)
    @test kwargs.attrs["inference_library"] == "MyLib"
end


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

using Markdown
using InteractiveUtils

# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
    quote
        local el = $(esc(element))
        global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing
        el
    end
end

# ╔═╡ 78e82534-3101-11eb-2eda-43b2de63543b
using Pkg, DrWatson

# ╔═╡ 7fcdc6c2-3101-11eb-306d-b32a138fa475
begin
	#@quickactivate "SR2StanPluto"
	using StatisticalRethinking
	using PlutoUI
end

# ╔═╡ 69c4c56a-3101-11eb-0c7c-8372134bc6a3
md"## Intro-pluto-10.jl"

# ╔═╡ ad970090-3102-11eb-2aa8-6122b50d6069
@bind x Slider(1:20, default=3)

# ╔═╡ 964f8476-3101-11eb-168e-438a10661092
s1 = s2 = [sqrt(i) for i in 1:x]

# ╔═╡ 0aaeaef0-318e-11eb-289b-978a9a86418b
md"CLICK ME

#### yes, even hidden code blocks!

```julia
Text('hello world!')
```

"

# ╔═╡ 48625f5c-3103-11eb-3d51-4b70ff04ba9b
md"## End of intro-pluto-10.jl"

# ╔═╡ Cell order:
# ╟─69c4c56a-3101-11eb-0c7c-8372134bc6a3
# ╠═78e82534-3101-11eb-2eda-43b2de63543b
# ╠═7fcdc6c2-3101-11eb-306d-b32a138fa475
# ╠═ad970090-3102-11eb-2aa8-6122b50d6069
# ╠═964f8476-3101-11eb-168e-438a10661092
# ╠═0aaeaef0-318e-11eb-289b-978a9a86418b
# ╟─48625f5c-3103-11eb-3d51-4b70ff04ba9b