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

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

Accepts an invitation to a resource share from another Amazon Web Services account.

# Arguments
- `resource_share_invitation_arn`: The Amazon Resource Name (ARN) of the invitation.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A unique, case-sensitive identifier that you provide to ensure the
  idempotency of the request.
"""
function accept_resource_share_invitation(
    resourceShareInvitationArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/acceptresourceshareinvitation",
        Dict{String,Any}("resourceShareInvitationArn" => resourceShareInvitationArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function accept_resource_share_invitation(
    resourceShareInvitationArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/acceptresourceshareinvitation",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "resourceShareInvitationArn" => resourceShareInvitationArn
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Associates the specified resource share with the specified principals and resources.

# Arguments
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A unique, case-sensitive identifier that you provide to ensure the
  idempotency of the request.
- `"principals"`: The principals to associate with the resource share. The possible values
  are:   An Amazon Web Services account ID   An Amazon Resource Name (ARN) of an organization
  in Organizations   An ARN of an organizational unit (OU) in Organizations   An ARN of an
  IAM role   An ARN of an IAM user    Not all resource types can be shared with IAM roles and
  IAM users. For more information, see Sharing with IAM roles and IAM users in the Resource
  Access Manager User Guide.
- `"resourceArns"`: The Amazon Resource Names (ARNs) of the resources.
"""
function associate_resource_share(
    resourceShareArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/associateresourceshare",
        Dict{String,Any}("resourceShareArn" => resourceShareArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function associate_resource_share(
    resourceShareArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/associateresourceshare",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("resourceShareArn" => resourceShareArn), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    associate_resource_share_permission(permission_arn, resource_share_arn)
    associate_resource_share_permission(permission_arn, resource_share_arn, params::Dict{String,<:Any})

Associates a permission with a resource share.

# Arguments
- `permission_arn`: The Amazon Resource Name (ARN) of the RAM permission to associate with
  the resource share.
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A unique, case-sensitive identifier that you provide to ensure the
  idempotency of the request.
- `"permissionVersion"`: The version of the RAM permissions to associate with the resource
  share.
- `"replace"`: Indicates whether the permission should replace the permissions that are
  currently associated with the resource share. Use true to replace the current permissions.
  Use false to add the permission to the current permission.
"""
function associate_resource_share_permission(
    permissionArn, resourceShareArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/associateresourcesharepermission",
        Dict{String,Any}(
            "permissionArn" => permissionArn, "resourceShareArn" => resourceShareArn
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function associate_resource_share_permission(
    permissionArn,
    resourceShareArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/associateresourcesharepermission",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "permissionArn" => permissionArn, "resourceShareArn" => resourceShareArn
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Creates a resource share. You must provide a list of the Amazon Resource Names (ARNs) for
the resources you want to share. You must also specify who you want to share the resources
with, and the permissions that you grant them.  Sharing a resource makes it available for
use by principals outside of the Amazon Web Services account that created the resource.
Sharing doesn't change any permissions or quotas that apply to the resource in the account
that created it.

# Arguments
- `name`: The name of the resource share.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"allowExternalPrincipals"`: Indicates whether principals outside your organization in
  Organizations can be associated with a resource share.
- `"clientToken"`: A unique, case-sensitive identifier that you provide to ensure the
  idempotency of the request.
- `"permissionArns"`: The Amazon Resource Names (ARNs) of the permissions to associate with
  the resource share. If you do not specify an ARN for the permission, RAM automatically
  attaches the default version of the permission for each resource type. Only one permission
  can be associated with each resource type in a resource share.
- `"principals"`: The principals to associate with the resource share. The possible values
  are:   An Amazon Web Services account ID   An Amazon Resource Name (ARN) of an organization
  in Organizations   An ARN of an organizational unit (OU) in Organizations   An ARN of an
  IAM role   An ARN of an IAM user    Not all resource types can be shared with IAM roles and
  IAM users. For more information, see Sharing with IAM roles and IAM users in the Resource
  Access Manager User Guide.
- `"resourceArns"`: The ARNs of the resources to associate with the resource share.
- `"tags"`: One or more tags.
"""
function create_resource_share(name; aws_config::AbstractAWSConfig=global_aws_config())
    return ram(
        "POST",
        "/createresourceshare",
        Dict{String,Any}("name" => name);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function create_resource_share(
    name, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/createresourceshare",
        Dict{String,Any}(mergewith(_merge, Dict{String,Any}("name" => name), params));
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Deletes the specified resource share.

# Arguments
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A unique, case-sensitive identifier that you provide to ensure the
  idempotency of the request.
"""
function delete_resource_share(
    resourceShareArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "DELETE",
        "/deleteresourceshare",
        Dict{String,Any}("resourceShareArn" => resourceShareArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function delete_resource_share(
    resourceShareArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "DELETE",
        "/deleteresourceshare",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("resourceShareArn" => resourceShareArn), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Disassociates the specified principals or resources from the specified resource share.

# Arguments
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A unique, case-sensitive identifier that you provide to ensure the
  idempotency of the request.
- `"principals"`: The principals.
- `"resourceArns"`: The Amazon Resource Names (ARNs) of the resources.
"""
function disassociate_resource_share(
    resourceShareArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/disassociateresourceshare",
        Dict{String,Any}("resourceShareArn" => resourceShareArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function disassociate_resource_share(
    resourceShareArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/disassociateresourceshare",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("resourceShareArn" => resourceShareArn), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    disassociate_resource_share_permission(permission_arn, resource_share_arn)
    disassociate_resource_share_permission(permission_arn, resource_share_arn, params::Dict{String,<:Any})

Disassociates an RAM permission from a resource share.

# Arguments
- `permission_arn`: The Amazon Resource Name (ARN) of the permission to disassociate from
  the resource share.
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A unique, case-sensitive identifier that you provide to ensure the
  idempotency of the request.
"""
function disassociate_resource_share_permission(
    permissionArn, resourceShareArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/disassociateresourcesharepermission",
        Dict{String,Any}(
            "permissionArn" => permissionArn, "resourceShareArn" => resourceShareArn
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function disassociate_resource_share_permission(
    permissionArn,
    resourceShareArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/disassociateresourcesharepermission",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "permissionArn" => permissionArn, "resourceShareArn" => resourceShareArn
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Enables resource sharing within your organization in Organizations. The caller must be the
master account for the organization.

"""
function enable_sharing_with_aws_organization(;
    aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/enablesharingwithawsorganization";
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function enable_sharing_with_aws_organization(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/enablesharingwithawsorganization",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Gets the contents of an RAM permission in JSON format.

# Arguments
- `permission_arn`: The Amazon Resource Name (ARN) of the permission.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"permissionVersion"`: The identifier for the version of the permission.
"""
function get_permission(permissionArn; aws_config::AbstractAWSConfig=global_aws_config())
    return ram(
        "POST",
        "/getpermission",
        Dict{String,Any}("permissionArn" => permissionArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_permission(
    permissionArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/getpermission",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("permissionArn" => permissionArn), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Gets the policies for the specified resources that you own and have shared.

# Arguments
- `resource_arns`: The Amazon Resource Names (ARNs) of the resources.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"nextToken"`: The token for the next page of results.
- `"principal"`: The principal.
"""
function get_resource_policies(
    resourceArns; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/getresourcepolicies",
        Dict{String,Any}("resourceArns" => resourceArns);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_resource_policies(
    resourceArns,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/getresourcepolicies",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("resourceArns" => resourceArns), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Gets the resources or principals for the resource shares that you own.

# Arguments
- `association_type`: The association type. Specify PRINCIPAL to list the principals that
  are associated with the specified resource share. Specify RESOURCE to list the resources
  that are associated with the specified resource share.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"associationStatus"`: The association status.
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"nextToken"`: The token for the next page of results.
- `"principal"`: The principal. You cannot specify this parameter if the association type
  is RESOURCE.
- `"resourceArn"`: The Amazon Resource Name (ARN) of the resource. You cannot specify this
  parameter if the association type is PRINCIPAL.
- `"resourceShareArns"`: The Amazon Resource Names (ARN) of the resource shares.
"""
function get_resource_share_associations(
    associationType; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/getresourceshareassociations",
        Dict{String,Any}("associationType" => associationType);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_resource_share_associations(
    associationType,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/getresourceshareassociations",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("associationType" => associationType), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Gets the invitations that you have received for resource shares.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"nextToken"`: The token for the next page of results.
- `"resourceShareArns"`: The Amazon Resource Names (ARN) of the resource shares.
- `"resourceShareInvitationArns"`: The Amazon Resource Names (ARN) of the invitations.
"""
function get_resource_share_invitations(; aws_config::AbstractAWSConfig=global_aws_config())
    return ram(
        "POST",
        "/getresourceshareinvitations";
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_resource_share_invitations(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/getresourceshareinvitations",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Gets the resource shares that you own or the resource shares that are shared with you.

# Arguments
- `resource_owner`: The type of owner.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"name"`: The name of the resource share.
- `"nextToken"`: The token for the next page of results.
- `"permissionArn"`: The Amazon Resource Name (ARN) of the RAM permission that is
  associated with the resource share.
- `"resourceShareArns"`: The Amazon Resource Names (ARNs) of the resource shares.
- `"resourceShareStatus"`: The status of the resource share.
- `"tagFilters"`: One or more tag filters.
"""
function get_resource_shares(
    resourceOwner; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/getresourceshares",
        Dict{String,Any}("resourceOwner" => resourceOwner);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function get_resource_shares(
    resourceOwner,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/getresourceshares",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("resourceOwner" => resourceOwner), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Lists the resources in a resource share that is shared with you but that the invitation is
still pending for.

# Arguments
- `resource_share_invitation_arn`: The Amazon Resource Name (ARN) of the invitation.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"nextToken"`: The token for the next page of results.
"""
function list_pending_invitation_resources(
    resourceShareInvitationArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/listpendinginvitationresources",
        Dict{String,Any}("resourceShareInvitationArn" => resourceShareInvitationArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_pending_invitation_resources(
    resourceShareInvitationArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/listpendinginvitationresources",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "resourceShareInvitationArn" => resourceShareInvitationArn
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Lists the RAM permissions.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"nextToken"`: The token for the next page of results.
- `"resourceType"`: Specifies the resource type for which to list permissions. For example,
  to list only permissions that apply to EC2 subnets, specify ec2:Subnet.
"""
function list_permissions(; aws_config::AbstractAWSConfig=global_aws_config())
    return ram(
        "POST", "/listpermissions"; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end
function list_permissions(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/listpermissions",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Lists the principals that you have shared resources with or that have shared resources with
you.

# Arguments
- `resource_owner`: The type of owner.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"nextToken"`: The token for the next page of results.
- `"principals"`: The principals.
- `"resourceArn"`: The Amazon Resource Name (ARN) of the resource.
- `"resourceShareArns"`: The Amazon Resource Names (ARN) of the resource shares.
- `"resourceType"`: The resource type. Valid values: acm-pca:CertificateAuthority |
  appmesh:Mesh | codebuild:Project | codebuild:ReportGroup | ec2:CapacityReservation |
  ec2:DedicatedHost | ec2:LocalGatewayRouteTable | ec2:PrefixList | ec2:Subnet |
  ec2:TrafficMirrorTarget | ec2:TransitGateway | imagebuilder:Component | imagebuilder:Image
  | imagebuilder:ImageRecipe | imagebuilder:ContainerRecipe | glue:Catalog | glue:Database |
  glue:Table | license-manager:LicenseConfiguration I network-firewall:FirewallPolicy |
  network-firewall:StatefulRuleGroup | network-firewall:StatelessRuleGroup | outposts:Outpost
  | resource-groups:Group | rds:Cluster | route53resolver:FirewallRuleGroup
  |route53resolver:ResolverQueryLogConfig | route53resolver:ResolverRule |
  s3-outposts:Outpost | ssm-contacts:Contact | ssm-incidents:ResponsePlan
"""
function list_principals(resourceOwner; aws_config::AbstractAWSConfig=global_aws_config())
    return ram(
        "POST",
        "/listprincipals",
        Dict{String,Any}("resourceOwner" => resourceOwner);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_principals(
    resourceOwner,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/listprincipals",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("resourceOwner" => resourceOwner), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Lists the RAM permissions that are associated with a resource share.

# Arguments
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"nextToken"`: The token for the next page of results.
"""
function list_resource_share_permissions(
    resourceShareArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/listresourcesharepermissions",
        Dict{String,Any}("resourceShareArn" => resourceShareArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_resource_share_permissions(
    resourceShareArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/listresourcesharepermissions",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("resourceShareArn" => resourceShareArn), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Lists the shareable resource types supported by RAM.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"nextToken"`: The token for the next page of results.
"""
function list_resource_types(; aws_config::AbstractAWSConfig=global_aws_config())
    return ram(
        "POST", "/listresourcetypes"; aws_config=aws_config, feature_set=SERVICE_FEATURE_SET
    )
end
function list_resource_types(
    params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/listresourcetypes",
        params;
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Lists the resources that you added to a resource shares or the resources that are shared
with you.

# Arguments
- `resource_owner`: The type of owner.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"maxResults"`: The maximum number of results to return with a single call. To retrieve
  the remaining results, make another call with the returned nextToken value.
- `"nextToken"`: The token for the next page of results.
- `"principal"`: The principal.
- `"resourceArns"`: The Amazon Resource Names (ARNs) of the resources.
- `"resourceShareArns"`: The Amazon Resource Names (ARN) of the resource shares.
- `"resourceType"`: The resource type. Valid values: acm-pca:CertificateAuthority |
  appmesh:Mesh | codebuild:Project | codebuild:ReportGroup | ec2:CapacityReservation |
  ec2:DedicatedHost | ec2:LocalGatewayRouteTable | ec2:PrefixList | ec2:Subnet |
  ec2:TrafficMirrorTarget | ec2:TransitGateway | imagebuilder:Component | imagebuilder:Image
  | imagebuilder:ImageRecipe | imagebuilder:ContainerRecipe | glue:Catalog | glue:Database |
  glue:Table | license-manager:LicenseConfiguration I network-firewall:FirewallPolicy |
  network-firewall:StatefulRuleGroup | network-firewall:StatelessRuleGroup | outposts:Outpost
  | resource-groups:Group | rds:Cluster | route53resolver:FirewallRuleGroup
  |route53resolver:ResolverQueryLogConfig | route53resolver:ResolverRule |
  s3-outposts:Outpost | ssm-contacts:Contact | ssm-incidents:ResponsePlan
"""
function list_resources(resourceOwner; aws_config::AbstractAWSConfig=global_aws_config())
    return ram(
        "POST",
        "/listresources",
        Dict{String,Any}("resourceOwner" => resourceOwner);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function list_resources(
    resourceOwner,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/listresources",
        Dict{String,Any}(
            mergewith(_merge, Dict{String,Any}("resourceOwner" => resourceOwner), params)
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Resource shares that were created by attaching a policy to a resource are visible only to
the resource share owner, and the resource share cannot be modified in RAM. Use this API
action to promote the resource share. When you promote the resource share, it becomes:
Visible to all principals that it is shared with.   Modifiable in RAM.

# Arguments
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share to promote.

"""
function promote_resource_share_created_from_policy(
    resourceShareArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/promoteresourcesharecreatedfrompolicy",
        Dict{String,Any}("resourceShareArn" => resourceShareArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function promote_resource_share_created_from_policy(
    resourceShareArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/promoteresourcesharecreatedfrompolicy",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("resourceShareArn" => resourceShareArn), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Rejects an invitation to a resource share from another Amazon Web Services account.

# Arguments
- `resource_share_invitation_arn`: The Amazon Resource Name (ARN) of the invitation.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"clientToken"`: A unique, case-sensitive identifier that you provide to ensure the
  idempotency of the request.
"""
function reject_resource_share_invitation(
    resourceShareInvitationArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/rejectresourceshareinvitation",
        Dict{String,Any}("resourceShareInvitationArn" => resourceShareInvitationArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function reject_resource_share_invitation(
    resourceShareInvitationArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/rejectresourceshareinvitation",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "resourceShareInvitationArn" => resourceShareInvitationArn
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    tag_resource(resource_share_arn, tags)
    tag_resource(resource_share_arn, tags, params::Dict{String,<:Any})

Adds the specified tags to the specified resource share that you own.

# Arguments
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share.
- `tags`: One or more tags.

"""
function tag_resource(
    resourceShareArn, tags; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/tagresource",
        Dict{String,Any}("resourceShareArn" => resourceShareArn, "tags" => tags);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function tag_resource(
    resourceShareArn,
    tags,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/tagresource",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}("resourceShareArn" => resourceShareArn, "tags" => tags),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

"""
    untag_resource(resource_share_arn, tag_keys)
    untag_resource(resource_share_arn, tag_keys, params::Dict{String,<:Any})

Removes the specified tags from the specified resource share that you own.

# Arguments
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share.
- `tag_keys`: The tag keys of the tags to remove.

"""
function untag_resource(
    resourceShareArn, tagKeys; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/untagresource",
        Dict{String,Any}("resourceShareArn" => resourceShareArn, "tagKeys" => tagKeys);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function untag_resource(
    resourceShareArn,
    tagKeys,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/untagresource",
        Dict{String,Any}(
            mergewith(
                _merge,
                Dict{String,Any}(
                    "resourceShareArn" => resourceShareArn, "tagKeys" => tagKeys
                ),
                params,
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end

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

Updates the specified resource share that you own.

# Arguments
- `resource_share_arn`: The Amazon Resource Name (ARN) of the resource share.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"allowExternalPrincipals"`: Indicates whether principals outside your organization in
  Organizations can be associated with a resource share.
- `"clientToken"`: A unique, case-sensitive identifier that you provide to ensure the
  idempotency of the request.
- `"name"`: The name of the resource share.
"""
function update_resource_share(
    resourceShareArn; aws_config::AbstractAWSConfig=global_aws_config()
)
    return ram(
        "POST",
        "/updateresourceshare",
        Dict{String,Any}("resourceShareArn" => resourceShareArn);
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end
function update_resource_share(
    resourceShareArn,
    params::AbstractDict{String};
    aws_config::AbstractAWSConfig=global_aws_config(),
)
    return ram(
        "POST",
        "/updateresourceshare",
        Dict{String,Any}(
            mergewith(
                _merge, Dict{String,Any}("resourceShareArn" => resourceShareArn), params
            ),
        );
        aws_config=aws_config,
        feature_set=SERVICE_FEATURE_SET,
    )
end


text:
##  Copyright 2022 Myrl Marmarelis
##
##  Licensed under the Apache License, Version 2.0 (the "License");
##  you may not use this file except in compliance with the License.
##  You may obtain a copy of the License at
##
##    http://www.apache.org/licenses/LICENSE-2.0
##
##  Unless required by applicable law or agreed to in writing, software
##  distributed under the License is distributed on an "AS IS" BASIS,
##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##  See the License for the specific language governing permissions and
##  limitations under the License.

using StaticArrays, Setfield

"""
  `input` features should be scaled between zero and one,
  preferably with a mapping defined a priori (not an empirical
  CDF or standardization,) so that new `Instance`s may come
  online without having to alter the old ones.
  Note: should I store it log-transformed, then?
  Some features need not be scaled like that if they will
  never be treated.
""" # if they're already Z-scored, should I apply a normal CDF
# or an empirical CDF that guarantees the histogram to be uniform?
struct Instance{ In, Out, T<:Real }
  id :: Int
  input :: SVector{In, T}
  output :: SVector{Out, T}
end

# some differential operator on `input` must match `output`,
# the latter of which takes whatever dimensionality of the operator.
# `output` basically can be used to store intermediate computations
struct ConstraintInstance{ In, Out, T<:Real }
  input :: SVector{In, T}
  output :: SVector{Out, T}
end

wrap_treatment_index(index, In) = index < 0 ? (index + In+1) : index

# may not be the route we always follow in structuring treatment-effect models
function treat_instance(instance::Instance{In,Out,T},
    index::Int, treatment::T; perturb::Bool=false
    )::Instance{In,Out,T} where {In,Out,T}
  @assert index != 0
  index = wrap_treatment_index(index, In)
  if perturb
    @set instance.input[index] += treatment
  else
    @set instance.input[index] = treatment
  end
end

function make_propensity_instance(instance::Instance{In,Out,T},
    index::Int)::Instance{In-1,1,T} where {In,Out,T}
  index = wrap_treatment_index(index, In)
  propensity_input = SVector{In-1}(
    instance.input[i < index ? i : (i+1)] for i in 1:(In-1) )
  propensity_output = @SVector [ instance.input[index] ]
  Instance(instance.id, propensity_input, propensity_output)
end

ext: jl

max_stars_repo_name: marmarelis/TreatmentCurves

max_issues_repo_name: marmarelis/TreatmentCurves

max_forks_repo_name: marmarelis/TreatmentCurves


text:
using DataFrames
using StatsModels
using Clustering
using Statistics
using LinearAlgebra
using ATools
using HDF5

# Selection

"""
    primary_in_lxe(verticesdf::DataFrame)

Select primary photons in LXe.

Row │ event_id track_id parent_id x y z t moved pre_KE post_KE deposited process_id  volume_id
LXe           : volume_id = 0
Primary       : parent_id = 0
"""
function primary_in_lxe(verticesdf::DataFrame)
    vlxe      = select_by_column_value(verticesdf, "volume_id", 0)
    vlxephepr = select_by_column_value(vlxe, "parent_id", 0)
    return vlxephepr
end

"""
	xyz_dot(hitdf::DataFrame, simax::Hit)

Return the dot product between each SiPM in the event and the SiPM of max charge
"""
function xyz_dot(hitdf::DataFrame, simax::Hit)
	xyzmax = [simax.x, 	simax.y, simax.z]
	xyzm_dot = dot(xyzmax, xyzmax)
	return [dot(Array(hitdf[i,1:3]), xyzmax) /xyzm_dot  for i in 1:nrow(hitdf)]
end

"""
	sipm_pos(sxyz::DataFrame, index::Integer)

Return the position of the SiPMs in the sipm_xyz database for a given index

"""
function sipm_pos(sxyz::DataFrame, index::Integer)
	return Array(select_by_index(sxyz, "sensor_id", index)[1,2:end])
end

"""
	find_xyz_sipm_qmax(hitdf::DataFrame)
Return the coordinates of the SiPM with maximum charge

"""
function find_xyz_sipm_qmax(hitdf::DataFrame)
	indx, xmax, qxmax = find_max_xy(hitdf, "x", "q")
	return Hit(xmax, hitdf.y[indx], hitdf.z[indx], qxmax)
end

"""
	sipmsel(hdf::DataFrame)

Return two data frames, separating the SiPMs in two groups depending on
the sign of the angle with the SiPM of max charge
"""
function sipmsel(hdf::DataFrame)
	simax = find_xyz_sipm_qmax(hdf)
	npr   = xyz_dot(hdf, simax)
	return hdf[(npr.>0), :], hdf[(npr.<0), :]
end

"""
	ksipmsel(hdf::DataFrame, ka::Vector{Int64})

Return two data frames, separating the SiPMs in two groups depending on
the value of vector ka (1 or 2)
"""
function ksipmsel(hdf::DataFrame, ka::Vector{Int64})
	return hdf[(ka.==2), :], hdf[(ka.==1), :]
end

# kmeans algorithm
"""
	get_hits_as_matrix(hitdf::DataFrame)

Given the dataframe hitdf (with fields x,y,z), return the
underlying matrix
"""
function get_hits_as_matrix(hitdf::DataFrame)
	f = @formula(0 ~ x + y + z)
	f2 = apply_schema(f, schema(f, hitdf))
	resp, pred = modelcols(f2, hitdf)
	return transpose(pred)
end

# lors

"""
	lor_maxq(hitdf::DataFrame)

Compute lors using the SiPM of max charge (maxq algo) to divide the event
in two hemispheres and estimating the lor vertex from barycenter
"""
function lor_maxq(hitdf::DataFrame)
	hq2df, hq1df = sipmsel(hitdf)   # select using maxq
	b1 = baricenter(hq1df)          # baricenters
	b2 = baricenter(hq2df)
	return b1, b2, hq1df, hq2df
end

"""
	lor_kmeans(hitdf::DataFrame)

Compute lors using kmeans clustering.
Returns two estimations of vertices. One based in pure kmeans, the
other in barycenter.
"""
function lor_kmeans(hitdf::DataFrame)
	Mhits = get_hits_as_matrix(hitdf)  # take the underlying matrix
	kr = kmeans(Mhits, 2)              # apply kmeans
	ka = assignments(kr) # get the assignments of points to clusters

	hq2df, hq1df = ksipmsel(hitdf, ka)   # select using kmeans list
	b1 = baricenter(hq1df)     # baricenters
	b2 = baricenter(hq2df)
	return b1, b2, hq1df, hq2df
end

"""
	baricenter(hdf::DataFrame)
	returns the barycenter of a cluster of hits
"""
function baricenter(hdf::DataFrame)
	function xq(hdf::DataFrame, pos::String)
		return sum(hdf[!,pos] .* hdf.q) / qt
	end
	qt = sum(hdf.q)
	return Hit(xq(hdf, "x"), xq(hdf, "y"), xq(hdf, "z"), qt)
end

"""
	radial_correction(b::Hit, r::Float32, rsipm::Float32)

Take the estimated radius of interaction (r), and the radius of the sipm
and return the corrected positions
"""
function radial_correction(b::Hit, r::Float32)

	ϕ = atan(b.y,b.x)
    x = r .* cos.(ϕ)
    y = r .* sin.(ϕ)
    return x,y,b.z
end

"""
	phistd(hitdf::DataFrame)

Compute the std deviation in phi weighted by charge, e.g:
Sqrt(1/Q Sum_i (phi_i - phi_mean) * qi )
"""
function phistd(hitdf::DataFrame)
	phi = fphi(hitdf)
	return wstd(phi, hitdf.q)
end

"""
	xyzstd(hitdf::DataFrame)

Compute the std deviation in x weighted by charge, e.g:
Sqrt(1/Q Sum_i (phi_i - phi_mean) * qi )
"""
function xyzstd(hitdf::DataFrame, column::String="x")
	x = hitdf[!, column]
	return wstd(x, hitdf.q)
end

"""
    filter_energies
"""
function filter_energies(df::DataFrame, qmin::Float32, qmax::Float32)
    interval = ATools.range_bound(qmin, qmax, ATools.OpenBound)
    ndfq     = filter(x -> interval.(x.q1) .& interval.(x.q2), df)
end

"""
    calibration_function
"""
function calibration_function(calibFunc::NReco.CalFunction, rmin::Real, rmax::Real)
    line_pars = h5open(calibFunc.cal_file) do h5cal
        bias   = read_attribute(h5cal[calibFunc.cal_grp], calibFunc.cal_std * "-bias" )
        lconst = read_attribute(h5cal[calibFunc.cal_grp], calibFunc.cal_std * "-const")
        llin   = read_attribute(h5cal[calibFunc.cal_grp], calibFunc.cal_std * "-lin"  )
        return bias, lconst, llin
    end
    cal_func = ATools.predict_interaction_radius(ATools.gpol1(line_pars[2:end]),
        rmin, rmax, line_pars[1])
    return cal_func
end

"""
    calculate_interaction_radius!

"""
function calculate_interaction_radius!(df       ::DataFrame,
                                       predictor::Function,
                                       variable ::String,
                                       rmax     ::Union{Real, Nothing}=nothing)
    if variable == "cstd" && !(:cstd in propertynames(df))
        # We want to use the combined std which isn't necessarily saved in the H5
        comb_std(z, phi) = sqrt(z^2 + (rmax * phi)^2)
        transform!(df, [:zstd1, :phistd1] => ByRow(comb_std) => :cstd1,
            [:zstd2, :phistd2] => ByRow(comb_std) => :cstd2)
    end
    sym1 = Symbol(variable * "1")
    sym2 = Symbol(variable * "2")
    transform!(df, sym1 => predictor => :r1x, sym2 => predictor => :r2x)
    nothing
end

max_stars_repo_name: andLaing/NReco

max_issues_repo_name: andLaing/NReco

max_forks_repo_name: andLaing/NReco


text:
@testset "Basic Operations" begin
    seq = ReferenceSequence(dna"")
    @test length(seq) === lastindex(seq) === 0
    @test isempty(seq)
    @test ReferenceSequence(dna"") == seq
    @test_throws BoundsError seq[0]
    @test_throws BoundsError seq[1]
    @test_throws BoundsError seq[2:3]
    @test collect(seq) == DNA[]

    seq = ReferenceSequence(dna"ACGTN")
    @test length(seq) === lastindex(seq) === 5
    @test !isempty(seq)
    @test seq[1] === DNA_A
    @test seq[2] === DNA_C
    @test seq[3] === DNA_G
    @test seq[4] === DNA_T
    @test seq[5] === DNA_N
    @test seq[2:3] == dna"CG"
    @test seq[3:5] == dna"GTN"
    @test seq[5:4] == dna""
    @test_throws BoundsError seq[0]
    @test_throws BoundsError seq[6]
    @test_throws BoundsError seq[0:2]
    @test_throws BoundsError seq[5:6]
    @test collect(seq) == [DNA_A, DNA_C, DNA_G, DNA_T, DNA_N]
end


text:
__precompile__()

module LispSyntax
include("parser.jl")
export sx, desx, codegen, @lisp, @lisp_str, assign_reader_dispatch

# Internal types
type s_expr
  vector
end

sx(x...) = s_expr([x...])
==(a :: s_expr, b :: s_expr) = a.vector == b.vector

function desx(s)
  if typeof(s) == s_expr
    return map(desx, s.vector)
  elseif isa(s, Dict)
    return Dict(map(x -> desx(x[1]) => desx(x[2]), s)...)
  elseif isa(s, Set)
    return Set(map(desx, s))
  else
    return s
  end
end

function lispify(s)
  if isa(s, s_expr)
    return "(" * join(map(lispify, s.vector), " ") * ")"
  else
    return "$s"
  end
end

function construct_sexpr(items...) # convert the input tuple to an array
  ret = Array(Any, length(items))
  for i = 1:length(items)
    ret[i] = items[i]
  end
  ret
end

function assign_reader_dispatch(sym, fn)
  reader_table[sym] = fn
end

function quasiquote(s, escape_exceptions)
  if isa(s, Array) && length(s) == 2 && s[1] == :splice
    codegen(s[2], escape_exceptions = escape_exceptions)
  elseif isa(s, Array) && length(s) == 2 && s[1] == :splice_seq
    Expr(:..., codegen(s[2], escape_exceptions = escape_exceptions))
  elseif isa(s, Array)
    Expr(:call, :construct_sexpr, map(s -> quasiquote(s, escape_exceptions), s)...)
  elseif isa(s, Symbol)
    Expr(:quote, s)
  else
    s
  end
end

function quote_it(s)
  if isa(s, Array)
    Expr(:call, :construct_sexpr, map(s -> quote_it(s), s)...)
  elseif isa(s, Symbol)
   QuoteNode(s)
  else
    s
  end
end

function codegen(s; escape_exceptions = Set{Symbol}())
  if isa(s, Symbol)
    if s in escape_exceptions
      s
    else
      esc(s)
    end
  elseif isa(s, Dict)
    coded_s = map(x -> Expr(symbol("=>"),
                            codegen(x[1], escape_exceptions = escape_exceptions),
                            codegen(x[2], escape_exceptions = escape_exceptions)), s)
    Expr(:call, :Dict, coded_s...)
  elseif isa(s, Set)
    coded_s = map(x -> codegen(x, escape_exceptions = escape_exceptions), s)
    Expr(:call, :Set, Expr(:vect, coded_s...))
  elseif !isa(s, Array) # constant
    s
  elseif length(s) == 0 # empty array
    s
  elseif s[1] == :if
    if length(s) == 3
      :($(codegen(s[2], escape_exceptions = escape_exceptions)) && $(codegen(s[3], escape_exceptions = escape_exceptions)))
    elseif length(s) == 4
      :($(codegen(s[2], escape_exceptions = escape_exceptions)) ? $(codegen(s[3], escape_exceptions = escape_exceptions)) : $(codegen(s[4],  escape_exceptions = escape_exceptions)))
    else
      throw("illegal if statement $s")
    end
  elseif s[1] == :def
    assert(length(s) == 3)
    :($(esc(s[2])) = $(codegen(s[3], escape_exceptions = escape_exceptions)))
  elseif s[1] == :let
    syms     = Set([ s[2][i] for i = 1:2:length(s[2]) ])
    bindings = [ :($(s[2][i]) = $(codegen(s[2][i+1], escape_exceptions = escape_exceptions ∪ syms))) for i = 1:2:length(s[2]) ]
    coded_s  = map(x -> codegen(x, escape_exceptions = escape_exceptions ∪ syms), s[3:end])
    Expr(:let, Expr(:block, coded_s...), bindings...)
  elseif s[1] == :while
    coded_s = map(x -> codegen(x, escape_exceptions = escape_exceptions), s[2:end])
    Expr(:while, coded_s[1], Expr(:block, coded_s[2:end]...))
  elseif s[1] == :for
    syms     = Set([ s[2][i] for i = 1:2:length(s[2]) ])
    bindings = [ :($(s[2][i]) = $(codegen(s[2][i+1], escape_exceptions = escape_exceptions ∪ syms))) for i = 1:2:length(s[2]) ]
    coded_s  = map(x -> codegen(x, escape_exceptions = escape_exceptions ∪ syms), s[3:end])
    Expr(:for, Expr(:block, bindings...), Expr(:block, coded_s...))
  elseif s[1] == :do
    Expr(:block, map(x -> codegen(x, escape_exceptions = escape_exceptions), s[2:end])...)
  elseif s[1] == :global
    Expr(:global, map(x -> esc(x), s[2:end])...)
  elseif s[1] == :quote
    quote_it(s[2])
  elseif s[1] == :import
     Expr(:using, map(x -> esc(x), s[2:end])...)
  elseif s[1] == :splice
    throw("missplaced ~ (splice)")
  elseif s[1] == :splice_seq
    throw("missplaced ~@ (splice_seq)")
  elseif s[1] == :quasi
    quasiquote(s[2], escape_exceptions)
  elseif s[1] == :lambda || s[1] == :fn
    assert(length(s) >= 3)
    coded_s = map(x -> codegen(x, escape_exceptions = escape_exceptions ∪ Set(s[2])), s[3:end])
    Expr(:function, Expr(:tuple, s[2]...), Expr(:block, coded_s...))
  elseif s[1] == :defn
    # Note: julia's lambdas are not optimized yet, so we don't define defn as a macro.
    #       this should be revisited later.
    coded_s = map(x -> codegen(x, escape_exceptions = escape_exceptions ∪ Set(s[3])), s[4:end])
    Expr(:function, Expr(:call, esc(s[2]), s[3]...), Expr(:block, coded_s...))
  elseif s[1] == :defmacro
     Expr(:macro, Expr(:call, esc(s[2]), s[3]...),
          begin
            coded_s = map(x -> codegen(x, escape_exceptions = escape_exceptions ∪ Set(s[3])), s[4:end])
            sexpr = Expr(:block, coded_s...) #codegen(s[4], escape_exceptions = escape_exceptions ∪ Set(s[3]))
            :(codegen($sexpr, escape_exceptions = $escape_exceptions ∪ Set($(s[3]))))
          end)
  elseif s[1] == :defmethod
    # TODO
  else
    coded_s = map(x -> codegen(x, escape_exceptions = escape_exceptions), s)
    if (typeof(coded_s[1]) == Symbol && ismatch(r"^@.*$", string(coded_s[1]))) ||
       (typeof(coded_s[1]) == Expr && ismatch(r"^@.*$", string(coded_s[1].args[1])))
      Expr(:macrocall, coded_s[1], coded_s[2:end]...)
    else
      Expr(:call, coded_s[1], coded_s[2:end]...)
    end
  end
end

"This is an internal helper function, do not call outside of package"
function lisp_eval_helper(str :: AbstractString)
  s = desx(LispSyntax.read(str))
  return codegen(s)
end

macro lisp(str)
  return lisp_eval_helper(str)
end

macro lisp_str(str)
  return lisp_eval_helper(str)
end

end # module


text:
struct Adult <: Tabular end
url(::Adult) = ["https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data", "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test"]
checksum(::Adult) = ["5b00264637dbfec36bdeaab5676b0b309ff9eb788d63554ca0a249491c86603d", "a2a9044bc167a35b2361efbabec64e89d69ce82d9790d2980119aac5fd7e9c05"]
prep(::Adult) = [path -> preprocess(path, Adult(); delim=", "), path -> preprocess(path, Adult(); delim=", ", skipto=2)]
target(::Adult) = 15
categorical(::Adult) = [2; 4; 6:10; 14]
size(::Adult) = (32561, 0, 16281)
problem(::Adult) = Classification

max_stars_repo_name: GruAna/PreprocessData

max_issues_repo_name: GruAna/PreprocessData

max_forks_repo_name: GruAna/PreprocessData


text:
export Wishart

"""
Description:

    A Wishart node:

    f(out,v,nu) = W(out|v, nu) = B(v, nu) |out|^{(nu - D - 1)/2} exp(-1/2 tr(v^{-1} out))

Interfaces:

    1. out
    2. v (scale matrix)
    3. nu (degrees of freedom)

Construction:

    Wishart(out, v, nu, id=:some_id)
"""
mutable struct Wishart <: SoftFactor
    id::Symbol
    interfaces::Vector{Interface}
    i::Dict{Symbol,Interface}

    function Wishart(out, v, nu; id=generateId(Wishart))
        @ensureVariables(out, v, nu)
        self = new(id, Array{Interface}(undef, 3), Dict{Symbol,Interface}())
        addNode!(currentGraph(), self)
        self.i[:out] = self.interfaces[1] = associate!(Interface(self), out)
        self.i[:v] = self.interfaces[2] = associate!(Interface(self), v)
        self.i[:nu] = self.interfaces[3] = associate!(Interface(self), nu)

        return self
    end
end

slug(::Type{Wishart}) = "W"

format(dist::ProbabilityDistribution{MatrixVariate, Wishart}) = "$(slug(Wishart))(v=$(format(dist.params[:v])), nu=$(format(dist.params[:nu])))"

ProbabilityDistribution(::Type{MatrixVariate}, ::Type{Wishart}; v=mat(1.0), nu=1.0) = ProbabilityDistribution{MatrixVariate, Wishart}(Dict(:v=>v, :nu=>nu))
ProbabilityDistribution(::Type{Wishart}; v=mat(1.0), nu=1.0) = ProbabilityDistribution{MatrixVariate, Wishart}(Dict(:v=>v, :nu=>nu))

dims(dist::ProbabilityDistribution{MatrixVariate, Wishart}) = size(dist.params[:v])

vague(::Type{Wishart}, dims::Int64) = ProbabilityDistribution(MatrixVariate, Wishart, v=huge*diageye(dims), nu=Float64(dims)) # Flat prior
vague(::Type{Wishart}, dims::Tuple{Int64, Int64}) = ProbabilityDistribution(MatrixVariate, Wishart, v=huge*diageye(dims[1]), nu=Float64(dims[1]))

vague(::Type{Union{Gamma, Wishart}}, dims::Int64) = vague(Wishart, dims)
vague(::Type{Union{Gamma, Wishart}}, dims::Tuple{Int64, Int64}) = vague(Wishart, dims)
vague(::Type{Union{Gamma, Wishart}}, dims::Tuple) = vague(Gamma) # Univariate fallback

unsafeMean(dist::ProbabilityDistribution{MatrixVariate, Wishart}) = dist.params[:nu]*dist.params[:v] # unsafe mean

function unsafeDetLogMean(dist::ProbabilityDistribution{MatrixVariate, Wishart})
    d = dims(dist)[1]
    sum([digamma.(0.5*(dist.params[:nu] + 1 - i)) for i = 1:d]) +
    d*log(2) +
    logdet(dist.params[:v])
end

function unsafeVar(dist::ProbabilityDistribution{MatrixVariate, Wishart}) # unsafe variance
    d = dims(dist)[1]
    M = fill!(similar(Matrix(dist.params[:v])), NaN)
    for i = 1:d
        for j = 1:d
            M[i, j] = dist.params[:nu]*(dist.params[:v][i, j]^2 + dist.params[:v][i, i]*dist.params[:v][j, j])
        end
    end
    return M
end

function logPdf(dist::ProbabilityDistribution{MatrixVariate, Wishart},x)
    d = dims(dist)[1]
    0.5*((dist.params[:nu]-d-1)*logdet(x) - tr(inv(dist.params[:v])*x) - dist.params[:nu]*d*log(2) - dist.params[:nu]*logdet(dist.params[:v])) - logmvgamma(d,0.5*dist.params[:nu])
end

function isProper(dist::ProbabilityDistribution{MatrixVariate, Wishart})
    (size(dist.params[:v], 1) == size(dist.params[:v], 2)) || return false
    (dist.params[:nu] > size(dist.params[:v], 1) - 1) || return false
    isRoundedPosDef(dist.params[:v]) || return false
    return true
end

function prod!( x::ProbabilityDistribution{MatrixVariate, Wishart},
                y::ProbabilityDistribution{MatrixVariate, Wishart},
                z::ProbabilityDistribution{MatrixVariate, Wishart}=ProbabilityDistribution(MatrixVariate, Wishart, v=mat(1.0), nu=1.0))

    d = dims(x)[1]
    z.params[:v] = x.params[:v] * cholinv(x.params[:v] + y.params[:v]) * y.params[:v]
    z.params[:nu] = x.params[:nu] + y.params[:nu] - d - 1.0

    return z
end

@symmetrical function prod!(x::ProbabilityDistribution{MatrixVariate, Wishart},
                            y::ProbabilityDistribution{MatrixVariate, PointMass},
                            z::ProbabilityDistribution{MatrixVariate, PointMass}=ProbabilityDistribution(MatrixVariate, PointMass, m=mat(NaN)))

    isRoundedPosDef(y.params[:m]) || error("PointMass location $(y.params[:m]) should be positive definite")
    z.params[:m] = deepcopy(y.params[:m])

    return z
end

# Entropy functional
function differentialEntropy(dist::ProbabilityDistribution{MatrixVariate, Wishart})
    d = dims(dist)[1]
    0.5*(d + 1.0)*logdet(dist.params[:v]) +
    0.5*d*(d + 1.0)*log(2) +
    0.25*d*(d - 1.0)*log(pi) +
    sum([labsgamma(0.5*(dist.params[:nu] + 1.0 - i)) for i=1:d]) -
    0.5*(dist.params[:nu] - d - 1.0) * sum([digamma.(0.5*(dist.params[:nu] + 1.0 - i)) for i=1:d]) +
    0.5*dist.params[:nu]*d
end

# Average energy functional
function averageEnergy(::Type{Wishart}, marg_out::ProbabilityDistribution{MatrixVariate}, marg_v::ProbabilityDistribution{MatrixVariate}, marg_nu::ProbabilityDistribution{Univariate, PointMass})
    d = dims(marg_out)[1]
    0.5*marg_nu.params[:m]*unsafeDetLogMean(marg_v) +
    0.5*marg_nu.params[:m]*d*log(2) +
    0.25*d*(d - 1.0)*log(pi) +
    sum([labsgamma(0.5*(marg_nu.params[:m] + 1.0 - i)) for i=1:d]) -
    0.5*(marg_nu.params[:m] - d - 1.0)*unsafeDetLogMean(marg_out) +
    0.5*tr(unsafeInverseMean(marg_v)*unsafeMean(marg_out))
end


text:
using NPZ
using JSON
using Random
using LinearAlgebra
using FermiCG

@testset "hamiltonian stuff" begin
    atoms = []
    push!(atoms,Atom(1,"H",[0,0,0]))
    push!(atoms,Atom(2,"H",[0,0,1]))
    push!(atoms,Atom(3,"H",[0,0,2]))
    push!(atoms,Atom(4,"H",[0,0,3]))

    basis = "6-31g"
    mol     = Molecule(0,1,atoms,basis)
    mf = FermiCG.pyscf_do_scf(mol)
    nbas = size(mf.mo_coeff)[1]
    @test isapprox(mf.e_tot, -2.16024391299511, atol=1e-10)
    ints = FermiCG.pyscf_build_ints(mol,mf.mo_coeff,zeros(nbas,nbas));
    e,d,dim = FermiCG.pyscf_fci(ints,2,2)
    @test isapprox(e+ints.h0, -2.2251145788392828, atol=1e-10)
end

max_stars_repo_name: nmayhall-vt/FermiCG

max_issues_repo_name: nmayhall-vt/FermiCG

max_forks_repo_name: nmayhall-vt/FermiCG


text:
# using Pkg;
# Pkg.add("JuliaFormatter");
using JuliaFormatter

function format_files(dir)
    JuliaFormatter.format(dir)
end

function receive_files()
    file_name = append!([PROGRAM_FILE], ARGS)[2]
    format_files(file_name)
end

receive_files()


text:
abstract type SuperRes <: BoxConstrainedDifferentiableModel end

function to_static(thetas, t)
    d = div(size(thetas, 1),2)
    pts = thetas[1:d,:]
    velocities = thetas[d+1:2*d,:]
    pts_t = pts + velocities * t
    return pts_t
end

function is_in_bounds(model::SuperRes, thetas)
    bounds = parameterBounds(model)
    for i = 1:size(thetas,1)
        for j = 1:size(thetas,2)
            if thetas[i,j] < bounds[1][i] || thetas[i,j] > bounds[2][i]
                return false
            end
        end
    end
    return true
end


text:
# This file is a part of UltraNest.jl, licensed under the MIT License (MIT).

import Test
Test.@testset "Package UltraNest" begin

include("test_ultra_nest.jl")

end # testset


text:
# using Squareness
using Test

tests = ("test",)
# tests = ("test", "benchmark")

for test in tests
    @testset "$test" begin
        include(joinpath(@__DIR__, "$(test).jl"))
    end
end


text:
function ImageClassificationSingle(
		blocks::Tuple{<:Image{N},<:Label},
        data=nothing;
		size=ntuple(i -> 128, N),
		aug_projections=DataAugmentation.Identity(),
		aug_image=DataAugmentation.Identity(),
        C=RGB{N0f8},
        computestats=false,
	) where N
	return SupervisedTask(
		blocks,
		(
			ProjectiveTransforms(size; augmentations=aug_projections),
			getimagepreprocessing(data, computestats; C=C, augmentations=aug_image),
        	OneHot()
		)
	)
end

"""
    ImageClassificationSingle(size, classes; kwargs...)
    ImageClassificationSingle(blocks[, data]; kwargs...)

Learning task for single-label image classification. Images are
resized to `size` and classified into one of `classes`.

Use [`ImageClassificationMulti`](#) for the multi-class setting.

## Keyword arguments

- `computestats = false`: Whether to compute image statistics on dataset `data` or use
    default ImageNet stats.
- `aug_projections = `[`DataAugmentation.Identity`](#): augmentation to apply during
  [`ProjectiveTransforms`](#) (resizing and cropping)
- `aug_image = `[`DataAugmentation.Identity`](#): pixel-level augmentation to apply during
  [`ImagePreprocessing`](#)
- `C = RGB{N0f8}`: Color type images are converted to before further processing. Use `Gray{N0f8}`
    for grayscale images.
"""
function ImageClassificationSingle(size::NTuple{N,Int}, classes::AbstractVector; kwargs...) where N
    blocks = (Image{N}(), Label(classes))
    return ImageClassificationSingle(blocks, size=size)
end

registerlearningtask!(FASTAI_METHOD_REGISTRY, ImageClassificationSingle, (Image, Label))

# ---

function ImageClassificationMulti(
		blocks::Tuple{<:Image{N},<:LabelMulti},
        data = nothing;
		size=ntuple(i -> 128, N),
		aug_projections=DataAugmentation.Identity(),
		aug_image=DataAugmentation.Identity(),
        C=RGB{N0f8},
        computestats=false,
	) where N
	return SupervisedTask(
		blocks,
		(
			ProjectiveTransforms(size; augmentations=aug_projections),
			getimagepreprocessing(data, computestats; C=C, augmentations=aug_image),
        	OneHot()
		)
	)
end

"""
    ImageClassificationMulti(size, classes; kwargs...)

Learning task for multi-label image classification. Images are
resized to `size` and classified into multiple of `classes`.

Use [`ImageClassificationSingle`](#) for the single-class setting.

## Keyword arguments

- `computestats = false`: Whether to compute image statistics on dataset `data` or use
    default ImageNet stats.
- `aug_projections = `[`DataAugmentation.Identity`](#): augmentation to apply during
  [`ProjectiveTransforms`](#) (resizing and cropping)
- `aug_image = `[`DataAugmentation.Identity`](#): pixel-level augmentation to apply during
  [`ImagePreprocessing`](#)
- `C = RGB{N0f8}`: Color type images are converted to before further processing. Use `Gray{N0f8}`
    for grayscale images.
"""
function ImageClassificationMulti(size::NTuple{N,Int}, classes::AbstractVector; kwargs...) where N
    blocks = (Image{N}(), LabelMulti(classes))
    return ImageClassificationMulti(blocks; size=size, kwargs...)
end

registerlearningtask!(FASTAI_METHOD_REGISTRY, ImageClassificationMulti, (Image, LabelMulti))

# ## Tests

@testset "ImageClassificationSingle [task]" begin
    task = ImageClassificationSingle((16, 16), [1, 2])
    testencoding(getencodings(task), getblocks(task).sample)
    FastAI.checktask_core(task)
    @test_nowarn tasklossfn(task)
    @test_nowarn taskmodel(task, Models.xresnet18())

    @testset "`encodeinput`" begin
        image = rand(RGB, 32, 48)

        xtrain = encodeinput(task, Training(), image)
        @test size(xtrain) == (16, 16, 3)
        @test eltype(xtrain) == Float32

        xinference = encodeinput(task, Inference(), image)
        @test size(xinference) == (16, 24, 3)
        @test eltype(xinference) == Float32
    end
    @testset "`encodetarget`" begin
        category = 1
        y = encodetarget(task, Training(), category)
        @test y ≈ [1, 0]
        # depends on buffered interface for `BlockTask`s and `Encoding`s
        #encodetarget!(y, task, Training(), 2)
        #@test y ≈ [0, 1]
    end
    @testset "Show backends" begin
        @testset "ShowText" begin
            #@test_broken FastAI.test_task_show(task, ShowText(Base.DevNull()))
        end
    end

    @testset "blockmodel" begin
        task = ImageClassificationSingle((Image{2}(), Label(1:2)))
        @test_nowarn taskmodel(task)
    end
end

@testset "ImageClassificationMulti [task]" begin

    task = ImageClassificationMulti((16, 16), [1, 2])

    testencoding(getencodings(task), getblocks(task).sample)
    FastAI.checktask_core(task)
    @test_nowarn tasklossfn(task)
    @test_nowarn taskmodel(task, Models.xresnet18())
    @testset "Show backends" begin
        @testset "ShowText" begin
            FastAI.test_task_show(task, ShowText(Base.DevNull()))
        end
    end
end


text:
function register_rights_error(evt, user)
    if is_owned_by_organization(evt)
        org = evt.repository.owner.login
        return "**Register Failed**\n@$(user), it looks like you are not a publicly listed member/owner in the parent organization ($(org)).\nIf you are a member/owner, you will need to change your membership to public. See [GitHub Help](https://help.github.com/en/articles/publicizing-or-hiding-organization-membership)"
    else
        return "**Register Failed**\n@$(user), it looks like you don't have collaborator status on this repository."
    end
end

get_access_token(event) = create_access_token(Installation(event.payload["installation"]), get_jwt_auth())
get_user_auth() = GitHub.authenticate(CONFIG["github"]["token"])
get_jwt_auth() = GitHub.JWTAuth(CONFIG["github"]["app_id"], CONFIG["github"]["priv_pem"])

function get_sha_from_branch(reponame, brn; auth = GitHub.AnonymousAuth())
    try
        b = branch(reponame, Branch(brn); auth=auth)
        sha = b.sha !== nothing ? b.sha : b.commit.sha
        return sha, nothing
    catch ex
        d = parse_github_exception(ex)
        if d["Status Code"] == "404" && d["Message"] == "Branch not found"
            return nothing, "Branch `$brn` not found"
        else
            rethrow(ex)
        end
    end

    return nothing, nothing
end

is_owned_by_organization(event) = event.repository.owner.typ == "Organization"

function is_comment_by_collaborator(event)
    @debug("Checking if comment is by collaborator")
    user = get_user_login(event.payload)
    return iscollaborator(event.repository, user; auth=get_access_token(event))
end

function is_comment_by_org_owner_or_member(event)
    @debug("Checking if comment is by repository parent organization owner or member")
    org = event.repository.owner.login
    user = get_user_login(event.payload)
    if get(CONFIG, "check_private_membership", false)
        return GitHub.check_membership(org, user; auth=get_user_auth())
    else
        return GitHub.check_membership(org, user; public_only=true)
    end
end

has_register_rights(event) = is_comment_by_collaborator(event) || is_owned_by_organization(event) && is_comment_by_org_owner_or_member(event)

is_pull_request(payload::Dict{<:AbstractString}) = haskey(payload, "pull_request") || haskey(payload, "issue") && haskey(payload["issue"], "pull_request")
is_commit_comment(payload::Dict{<:AbstractString}) = haskey(payload, "comment") && !haskey(payload, "issue")

function get_prid(payload::Dict{<:AbstractString})
    if haskey(payload, "pull_request")
        return payload["pull_request"]["number"]
    elseif haskey(payload, "issue")
        return payload["issue"]["number"]
    else
        error("Don't know how to get pull request number")
    end
end

get_comment_commit_id(event) = event.payload["comment"]["commit_id"]
get_clone_url(event) = event.payload["repository"]["clone_url"]

function make_comment(evt::WebhookEvent, body::AbstractString)
    CONFIG["reply_comment"] || return
    @debug("Posting comment to PR/issue")
    headers = Dict("private_token" => CONFIG["github"]["token"])
    params = Dict("body" => body)
    repo = evt.repository
    auth = get_user_auth()
    if is_commit_comment(evt.payload)
        GitHub.create_comment(repo, get_comment_commit_id(evt),
                              :commit; headers=headers,
                              params=params, auth=auth)
    else
        GitHub.create_comment(repo, get_prid(evt.payload),
                              :issue; headers=headers,
                              params=params, auth=auth)
    end
end

function get_html_url(payload::Dict{<:AbstractString})
    if haskey(payload, "pull_request")
        return payload["pull_request"]["html_url"]
    elseif haskey(payload, "issue")
        if haskey(payload, "comment")
            return payload["comment"]["html_url"]
        else
            return payload["issue"]["html_url"]
        end
    elseif haskey(payload, "comment")
        return payload["comment"]["html_url"]
    else
        error("Don't know how to get html_url")
    end
end

"""
    raise_issue(event::WebhookEvent, phrase::Regex, bt::String)
Open an issue in the configured Registrator repository. The issue
body will contain the trigger comment `phrase` and the backtrace
in `bt`. A link to the opened issue will be posted on the source
issue, PR or commit from which the `event` comes from.

This will also post the backtrace on the slack channel if
configured.
"""
function raise_issue(event::WebhookEvent, phrase::Regex, bt::String)
    repo = event.repository.full_name
    lab = is_commit_comment(event.payload) ? get_comment_commit_id(event) : get_prid(event.payload)
    title = "Error registering $repo#$lab"
    input_phrase = "`[" * phrase.match[2:end-1] * "]`"
    body = """
        Repository: $repo
        Issue/PR: [$lab]($(get_html_url(event.payload)))
        Command: $(input_phrase)
        Stacktrace:
        ```
        $bt
        ```
        """

    slack_config = get(CONFIG, "slack", nothing)
    if (slack_config !== nothing) && get(slack_config, "alert", false)
        post_on_slack_channel(body, slack_config["token"], slack_config["channel"])
    end

    if CONFIG["report_issue"]
        params = Dict("title"=>title, "body"=>body)
        regrepo = CONFIG["issue_repo"]
        iss = create_issue(regrepo; params=params, auth=get_user_auth())
        msg = "Unexpected error occured during registration, see issue: [$(regrepo)#$(iss.number)]($(iss.html_url))"
        @debug(msg)
        make_comment(event, msg)
    else
        msg = "An unexpected error occured during registration."
        @debug(msg)
        make_comment(event, msg)
    end
end

function set_status(rp, state, desc)
     CONFIG["set_status"] || return
     repo = rp.reponame
     kind = rp.evt.kind
     payload = rp.evt.payload
     if kind == "pull_request"
         commit = payload["pull_request"]["head"]["sha"]
         params = Dict("state" => state,
                       "context" => CONFIG["github"]["user"],
                       "description" => desc)
         GitHub.create_status(repo, commit;
                              auth=get_access_token(rp.evt),
                              params=params)
     end
end

set_pending_status(rp) = set_status(rp, "pending", "Processing request...")
set_error_status(rp) = set_status(rp, "error", "Failed to register")
set_success_status(rp) = set_status(rp, "success", "Done")

function parse_github_exception(ex::ErrorException)
    msgs = map(strip, split(ex.msg, '\n'))
    d = Dict()
    for m in msgs
        a, b = split(m, ":"; limit=2)
        d[a] = strip(b)
    end
    return d
end

function is_pr_exists_exception(ex)
    d = parse_github_exception(ex)

    if d["Status Code"] == "422" &&
       match(r"A pull request already exists", d["Errors"]) !== nothing
        return true
    end

    return false
end

function get_user_login(payload::Dict{<:AbstractString})
    if haskey(payload, "comment")
        return payload["comment"]["user"]["login"]
    elseif haskey(payload, "issue")
        return payload["issue"]["user"]["login"]
    elseif haskey(payload, "pull_request")
        return payload["pull_request"]["user"]["login"]
    else
        error("Don't know how to get user login")
    end
end

function get_body(payload::Dict{<:AbstractString})
    if haskey(payload, "comment")
        return payload["comment"]["body"]
    elseif haskey(payload, "issue")
        return payload["issue"]["body"]
    elseif haskey(payload, "pull_request")
        return payload["pull_request"]["body"]
    else
        error("Don't know how to get body")
    end
end

function create_or_find_pull_request(repo::AbstractString,
                                     params::Dict{<:AbstractString, Any},
                                     rbrn::RegBranch)
    pr = nothing
    msg = ""
    auth = get_user_auth()
    try
        pr = create_pull_request(repo; auth=auth, params=params)
        msg = "created"
        @debug("Pull request created")
        try # add labels
            if get(rbrn.metadata, "labels", nothing) !== nothing
                edit_issue(repo, pr; auth = auth,
                    params = Dict("labels"=>rbrn.metadata["labels"]))
            end
        catch
            @debug "Failed to add labels, ignoring."
        end
    catch ex
        if is_pr_exists_exception(ex)
            @debug("Pull request already exists, not creating")
            msg = "updated"
        else
            rethrow(ex)
        end
    end

    if pr === nothing
        prs, _ = pull_requests(repo; auth=auth, params=Dict(
            "state" => "open",
            "base" => params["base"],
            "head" => string(split(repo, "/")[1], ":", params["head"]),
        ))
        if !isempty(prs)
            @assert length(prs) == 1 "PR lookup should only contain one result"
            @debug("PR found")
            pr = prs[1]
        end

        if pr === nothing
            error("Registration PR already exists but unable to find it")
        else
            update_pull_request(repo, pr.number; auth=auth, params=Dict("body" => params["body"], "title" => params["title"]))
            try # update labels
                if get(rbrn.metadata, "labels", nothing) !== nothing
                    edit_issue(repo, pr; auth = auth,
                        params = Dict("labels"=>rbrn.metadata["labels"]))
                end
            catch
                @debug "Failed to update labels, ignoring."
            end
        end
    end

    return pr, msg
end


text:
module TestJoin
    using Base.Test
    using DataFrames

    name = DataFrame(ID = [1, 2, 3], Name = ["John Doe", "Jane Doe", "Joe Blogs"])
    job = DataFrame(ID = [1, 2, 2, 4], Job = ["Lawyer", "Doctor", "Florist", "Farmer"])

    # Join on symbols or vectors of symbols
    join(name, job, on = :ID)
    join(name, job, on = [:ID])

    # Soon we won't allow natural joins
    #@test_throws join(name, job)

    # Test output of various join types
    outer = DataFrame(ID = [1, 2, 2, 3, 4],
                      Name = NullableArray(Nullable{String}["John Doe", "Jane Doe", "Jane Doe", "Joe Blogs", Nullable()]),
                      Job = NullableArray(Nullable{String}["Lawyer", "Doctor", "Florist", Nullable(), "Farmer"]))

    # (Tests use current column ordering but don't promote it)
    right = outer[Bool[!isnull(x) for x in outer[:Job]], [:Name, :ID, :Job]]
    left = outer[Bool[!isnull(x) for x in outer[:Name]], :]
    inner = left[Bool[!isnull(x) for x in left[:Job]], :]
    semi = unique(inner[:, [:ID, :Name]])
    anti = left[Bool[isnull(x) for x in left[:Job]], [:ID, :Name]]

    @test isequal(join(name, job, on = :ID), inner)
    @test isequal(join(name, job, on = :ID, kind = :inner), inner)
    @test isequal(join(name, job, on = :ID, kind = :outer), outer)
    @test isequal(join(name, job, on = :ID, kind = :left), left)
    @test isequal(join(name, job, on = :ID, kind = :right), right)
    @test isequal(join(name, job, on = :ID, kind = :semi), semi)
    @test isequal(join(name, job, on = :ID, kind = :anti), anti)

    # Join with no non-key columns
    on = [:ID]
    nameid = name[on]
    jobid = job[on]

    @test isequal(join(nameid, jobid, on = :ID), inner[on])
    @test isequal(join(nameid, jobid, on = :ID, kind = :inner), inner[on])
    @test isequal(join(nameid, jobid, on = :ID, kind = :outer), outer[on])
    @test isequal(join(nameid, jobid, on = :ID, kind = :left), left[on])
    @test isequal(join(nameid, jobid, on = :ID, kind = :right), right[on])
    @test isequal(join(nameid, jobid, on = :ID, kind = :semi), semi[on])
    @test isequal(join(nameid, jobid, on = :ID, kind = :anti), anti[on])

    # Join on multiple keys
    df1 = DataFrame(A = 1, B = 2, C = 3)
    df2 = DataFrame(A = 1, B = 2, D = 4)

    join(df1, df2, on = [:A, :B])

    # Test output of cross joins
    df1 = DataFrame(A = 1:2, B = 'a':'b')
    df2 = DataFrame(A = 1:3, C = 3:5)

    cross = DataFrame(A = [1, 1, 1, 2, 2, 2],
                      B = ['a', 'a', 'a', 'b', 'b', 'b'],
                      C = [3, 4, 5, 3, 4, 5])

    @test isequal(join(df1, df2[[:C]], kind = :cross), cross)

    # Cross joins handle naming collisions
    @test size(join(df1, df1, kind = :cross)) == (4, 4)

    # Cross joins don't take keys
    @test_throws ArgumentError join(df1, df2, on = :A, kind = :cross)

    # test empty inputs
    simple_df(len::Int, col=:A) = (df = DataFrame(); df[col]=collect(1:len); df)
    @test isequal(join(simple_df(0), simple_df(0), on = :A, kind = :left),  simple_df(0))
    @test isequal(join(simple_df(2), simple_df(0), on = :A, kind = :left),  simple_df(2))
    @test isequal(join(simple_df(0), simple_df(2), on = :A, kind = :left),  simple_df(0))
    @test isequal(join(simple_df(0), simple_df(0), on = :A, kind = :right), simple_df(0))
    @test isequal(join(simple_df(0), simple_df(2), on = :A, kind = :right), simple_df(2))
    @test isequal(join(simple_df(2), simple_df(0), on = :A, kind = :right), simple_df(0))
    @test isequal(join(simple_df(0), simple_df(0), on = :A, kind = :inner), simple_df(0))
    @test isequal(join(simple_df(0), simple_df(2), on = :A, kind = :inner), simple_df(0))
    @test isequal(join(simple_df(2), simple_df(0), on = :A, kind = :inner), simple_df(0))
    @test isequal(join(simple_df(0), simple_df(0), on = :A, kind = :outer), simple_df(0))
    @test isequal(join(simple_df(0), simple_df(2), on = :A, kind = :outer), simple_df(2))
    @test isequal(join(simple_df(2), simple_df(0), on = :A, kind = :outer), simple_df(2))
    @test isequal(join(simple_df(0), simple_df(0), on = :A, kind = :semi),  simple_df(0))
    @test isequal(join(simple_df(2), simple_df(0), on = :A, kind = :semi),  simple_df(0))
    @test isequal(join(simple_df(0), simple_df(2), on = :A, kind = :semi),  simple_df(0))
    @test isequal(join(simple_df(0), simple_df(0), on = :A, kind = :anti),  simple_df(0))
    @test isequal(join(simple_df(2), simple_df(0), on = :A, kind = :anti),  simple_df(2))
    @test isequal(join(simple_df(0), simple_df(2), on = :A, kind = :anti),  simple_df(0))
    @test isequal(join(simple_df(0), simple_df(0, :B), kind = :cross), DataFrame(A=Int[], B=Int[]))
    @test isequal(join(simple_df(0), simple_df(2, :B), kind = :cross), DataFrame(A=Int[], B=Int[]))
    @test isequal(join(simple_df(2), simple_df(0, :B), kind = :cross), DataFrame(A=Int[], B=Int[]))

    # issue #960
    df1 = DataFrame(A = 1:50,
                    B = 1:50,
                    C = 1)
    categorical!(df1, :A)
    categorical!(df1, :B)
    join(df1, df1, on = [:A, :B], kind = :inner)

    # Test that Array{Nullable} works when combined with NullableArray (#1088)
    df = DataFrame(Name = Nullable{String}["A", "B", "C"],
                   Mass = [1.5, 2.2, 1.1])
    df2 = DataFrame(Name = ["A", "B", "C", "A"],
                    Quantity = [3, 3, 2, 4])
    @test join(df2, df, on=:Name, kind=:left) == DataFrame(Name = ["A", "A", "B", "C"],
                                                           Quantity = [3, 4, 3, 2],
                                                           Mass = [1.5, 1.5, 2.2, 1.1])
end


text:
@testset "Ensemble assembly and nested sampling functions" begin
    ensembledir = randstring()
    spensembledir = randstring()
    distdir = randstring()

    source_pwm = [.7 .1 .1 .1
    .1 .1 .1 .7
    .1 .1 .7 .1]

    source_pwm_2 = [.6 .1 .1 .2
    .2 .1 .1 .6
    .1 .2 .6 .1]

    src_length_limits=2:12
    no_sources=3

    source_priors = assemble_source_priors(no_sources, [source_pwm, source_pwm_2])
    mix_prior=.5

    bg_scores = log.(fill(.1, (30,4)))
    obs=[BioSequences.LongSequence{DNAAlphabet{2}}("CCGTTGACGATGTGATGAATAATGAAAGAA")
    BioSequences.LongSequence{DNAAlphabet{2}}("CCCCGATGATGACCGTTGACCAGATGGATG")
    BioSequences.LongSequence{DNAAlphabet{2}}("CCCCGATGATGACCCCGATTTTGAAAAAAA")
    BioSequences.LongSequence{DNAAlphabet{2}}("TCATCATGCTGATGATGAATCAGATGAAAG")
    ]

    order_seqs = BioBackgroundModels.get_order_n_seqs(obs, 0)
    coded_seqs = BioBackgroundModels.code_seqs(order_seqs)
    obs=Array(transpose(coded_seqs))

    ensemble = IPM_Ensemble(ensembledir, 150, source_priors, (falses(0,0),mix_prior), bg_scores, obs, src_length_limits)
    ensemble = IPM_Ensemble(ensembledir, 200, source_priors, (falses(0,0),mix_prior), bg_scores, obs, src_length_limits) #test resumption

    sp_ensemble = IPM_Ensemble(spensembledir, 200, source_priors, (falses(0,0),mix_prior), bg_scores, obs, src_length_limits, posterior_switch=true)

    @test length(ensemble.models) == 200
    for model in ensemble.models
        @test -350 < model.log_Li < -150
    end

    @test length(sp_ensemble.models) == 200
    for model in sp_ensemble.models
        @test -350 < model.log_Li < -150
    end

    assembler=addprocs(1)

    @everywhere using BioMotifInference

    dist_ensemble=IPM_Ensemble(assembler, distdir, 150, source_priors, (falses(0,0),mix_prior), bg_scores, obs, src_length_limits)
    dist_ensemble=IPM_Ensemble(assembler, distdir, 200, source_priors, (falses(0,0),mix_prior), bg_scores, obs, src_length_limits) #test resumption

    @test length(dist_ensemble.models) == 200
    for model in ensemble.models
        @test -350 < model.log_Li < -150
    end

    rmprocs(assembler)
    rm(distdir, recursive=true)

    models_to_permute = 600
    funclimit=200
    funcvec=full_perm_funcvec

    instruct = Permute_Instruct(funcvec, ones(length(funcvec))./length(funcvec),models_to_permute,200, min_clmps=fill(.02,length(funcvec)))

    @info "Testing convergence displays..."
    sp_logZ = converge_ensemble!(sp_ensemble, instruct, converge_factor=500.,  wk_disp=true, tuning_disp=true, ens_disp=true, conv_plot=true, src_disp=true, lh_disp=true, liwi_disp=true, max_iterates=50)

    sp_ensemble=reset_ensemble!(sp_ensemble)

    @info "Testing threaded convergence..."
    sp_logZ = converge_ensemble!(sp_ensemble, instruct,  converge_factor=500.,wk_disp=false, tuning_disp=false, ens_disp=false, conv_plot=false, src_disp=false, lh_disp=false, liwi_disp=false, backup=(true, 150), max_iterates=300)

    @info "Testing resumption..."
    sp_logZ = converge_ensemble!(sp_ensemble, instruct,  converge_factor=500.,wk_disp=false, tuning_disp=false, ens_disp=false, conv_plot=false, src_disp=false, lh_disp=false, liwi_disp=false, backup=(true, 150))
    @test length(sp_ensemble.models) == 200
    @test length(sp_ensemble.log_Li) == length(sp_ensemble.log_Xi) == length(sp_ensemble.log_wi) == length(sp_ensemble.log_Liwi) == length(sp_ensemble.log_Zi) == length(sp_ensemble.Hi) == sp_ensemble.model_counter-200
    for i in 1:length(sp_ensemble.log_Li)-1
        @test sp_ensemble.log_Li[i] <= sp_ensemble.log_Li[i+1]
    end
    for i in 1:length(sp_ensemble.log_Zi)-1
        @test sp_ensemble.log_Zi[i] <= sp_ensemble.log_Zi[i+1]
    end
    @test sp_logZ > -1500.0

    @info "Testing multiprocess convergence..."
    @info "Spawning worker pool..."
    worker_pool=addprocs(2, topology=:master_worker)
    @everywhere using BioMotifInference

    ####CONVERGE############
    final_logZ = converge_ensemble!(ensemble, instruct, worker_pool,  converge_factor=500., wk_disp=false, tuning_disp=false, ens_disp=false, conv_plot=false, src_disp=false, lh_disp=false, liwi_disp=false, backup=(true,500), clean=(true, 500, 1000))

    convits=length(ensemble.log_Li)

    #test converging already converged, wih different converge criterion
    final_logZ = converge_ensemble!(ensemble, instruct, worker_pool,  converge_factor=150., converge_criterion="compression", wk_disp=false, tuning_disp=false, ens_disp=false, conv_plot=false, src_disp=false, lh_disp=false, liwi_disp=false, backup=(true,500), clean=(true, 500, 1000))

    length(ensemble.log_Li)==convits

    rmprocs(worker_pool)

    @test length(ensemble.models) == 200
    @test length(ensemble.log_Li) == length(ensemble.log_Xi) == length(ensemble.log_wi) == length(ensemble.log_Liwi) == length(ensemble.log_Zi) == length(ensemble.Hi) == ensemble.model_counter-200
    for i in 1:length(ensemble.log_Li)-1
        @test ensemble.log_Li[i] <= ensemble.log_Li[i+1]
    end
    for i in 1:length(ensemble.log_Zi)-1
        @test ensemble.log_Zi[i] <= ensemble.log_Zi[i+1]
    end
    @test typeof(final_logZ) == Float64
    @test final_logZ > -1500.0

    rm(ensembledir, recursive=true)
    rm(spensembledir, recursive=true)
end


text:
module TestCv

using Test
using LightGBM

train_matrix = rand(1000,70) # create random dataset
train_labels = rand([0, 1], 1000)
splits = (
    collect(1:200), collect(201:400),
    collect(401:600), collect(601:800),
    collect(801:1000)
)

estimator = LightGBM.LGBMClassification(
    objective = "binary",
    num_class = 1,
    is_training_metric = true,
    metric = ["auc"]
)

@testset "test cv with X y input -- binary" begin
    # Act
    output = LightGBM.cv(estimator, train_matrix, train_labels, splits, verbosity = -1);

    # Assert
    @test output isa Dict{String,Dict{String,Vector{Float64}}}
    @test length(output["validation"]["auc"]) == 5
    @test length(output["training"]["auc"]) == 5
end

@testset "test cv with Dataset input -- binary" begin
    # Arrange
    train_dataset = LightGBM.LGBM_DatasetCreateFromMat(train_matrix, "")
    LightGBM.LGBM_DatasetSetField(train_dataset, "label", train_labels)

    # Act
    output = LightGBM.cv(estimator, train_dataset, splits, verbosity = -1);

    # Assert
    @test output isa Dict{String,Dict{String,Vector{Float64}}}
    @test length(output["validation"]["auc"]) == 5
    @test length(output["training"]["auc"]) == 5
end

end # module


text:
@testset "model2fit_sum.jl" begin

    @testset "Empty left" begin

        model = Model2Fit_Empty()
        model = model + Gaussian_Peak()

        @test parameter_size(model) == 3

        θ=Float64[1, 0, 1]

        @test eval_y(model,[0.0],θ) ≈ [1.0]

        X = rand(10)
        Y = alloc_y(model,X,θ)

        @test (@benchmark accumulate_y!($model, $Y, $X,$θ)).allocs == 0
    end

    @testset "Empty right" begin

        model = Model2Fit_Empty()
        model = Gaussian_Peak() + model

        @test parameter_size(model) == 3

        θ=Float64[1, 0, 1]

        @test eval_y(model,[0.0],θ) ≈ [1.0]

        X = rand(10)
        Y = alloc_y(model,X,θ)

        @test (@benchmark accumulate_y!($model, $Y, $X,$θ)).allocs == 0

    end

    @testset "2 terms sum" begin

        model = Gaussian_Peak() + Gaussian_Peak()

        @test parameter_size(model) == 6

        θ=Float64[1, 0, 1, 1, 0, 4]

        @test eval_y(model,[0.0],θ) ≈ [2.0]

        X = rand(10)
        Y = alloc_y(model,X,θ)

        @test (@benchmark accumulate_y!($model, $Y, $X,$θ)).allocs == 0

    end

    @testset "3 terms sum" begin

        model = Gaussian_Peak() + Gaussian_Peak() + Gaussian_Peak()

        @test parameter_size(model) == 9

        θ=Float64[1, 0, 1, 1, 0, 4, 1, 0, 4]

        @test eval_y(model,[0.0],θ) ≈ [3.0]

        X = rand(10)
        Y = alloc_y(model,X,θ)

        @test (@benchmark accumulate_y!($model, $Y, $X,$θ)).allocs == 0

    end

end


text:
module Simulate_ControllerDamper

using  Modia3D
import Modia3D.ModiaMath

# Damper
@forceElement Damper(; d=1.0) begin
    pot   = ModiaMath.RealScalar("pot",   causality=ModiaMath.Input,  numericType=ModiaMath.WR)
    flow = ModiaMath.RealScalar("flow", causality=ModiaMath.Output, numericType=ModiaMath.WR)
end
function Modia3D.computeTorque(damper::Damper, sim::ModiaMath.SimulationState)
  damper.flow.value = -damper.d*damper.pot.value
end

# Controller
@forceElement Controller(; k1=10.0, k2=10.0, T2=0.01, freqHz=0.5, A=1.0) begin
    PI_x    = ModiaMath.RealScalar("PI_x"   , start=0.0, info="State of PI controller", numericType=ModiaMath.XD_EXP)
    PI_derx = ModiaMath.RealScalar("PI_derx", start=0.0, info="= der(PI_x)"           , numericType=ModiaMath.DER_XD_EXP, integral=PI_x)
    sine_y  = ModiaMath.RealScalar("sine_y" , start=0.0, info="= sin(2*pi*f*time)"    , numericType=ModiaMath.WR)
    phi = ModiaMath.RealScalar("phi", causality=ModiaMath.Input,  numericType=ModiaMath.WR)
    w   = ModiaMath.RealScalar("w",   causality=ModiaMath.Input,  numericType=ModiaMath.WR)
    tau = ModiaMath.RealScalar("tau", causality=ModiaMath.Output, numericType=ModiaMath.WR)
end
function Modia3D.computeTorque(c::Controller, sim::ModiaMath.SimulationState)
    c.sine_y.value  = c.A*sin(2*pi*c.freqHz*sim.time)
    gain_y          = c.k1*(c.sine_y.value - c.phi.value)
    PI_u            = gain_y - c.w.value
    c.PI_derx.value = PI_u/c.T2
    c.tau.value = c.k2*(c.PI_x.value + PI_u)
end

# Pendulum
vmat1 = Modia3D.Material(color="LightBlue" , transparency=0.5)
vmat2 = Modia3D.Material(color="Red")

@assembly PendulumWithController(;Lx = 1.0, Ly=0.2*Lx, Lz=0.2*Lx, m=1.0) begin
   world  = Modia3D.Object3D(Modia3D.CoordinateSystem(0.5*Lx))

   # Pendulum
   body   = Modia3D.Object3D(Modia3D.Solid(Modia3D.SolidBeam(Lx,Ly,Lz), Modia3D.MassProperties(m=m), vmat1))
   frame1 = Modia3D.Object3D(body; r=[-Lx/2, 0.0, 0.0])
   cyl    = Modia3D.Object3D(frame1,Modia3D.Cylinder(Ly/2,1.2*Ly; material=vmat2))

#=
   body2   = Modia3D.Object3D(Modia3D.Solid(Modia3D.SolidBeam(Lx,Ly,Lz), Modia3D.MassProperties(m=m), vmat1))
   frame2 = Modia3D.Object3D(body2; r=[-Lx/2, 0.0, 0.0])
   cyl2    = Modia3D.Object3D(frame2,Modia3D.Cylinder(Ly/2,1.2*Ly; material=vmat2))
=#

   # Connect pendulum to world
   rev        = Modia3D.Revolute(world, frame1)
   #rev2        = Modia3D.Revolute(world, frame2)

   d     = Damper(d=0.2)
   damper = Modia3D.AdaptorForceElementToFlange(w=d.pot, tau=d.flow)
   Modia3D.connect(damper, rev)

   c = Controller()
   controller = Modia3D.AdaptorForceElementToFlange(phi=c.phi, w=c.w, tau=c.tau)
   Modia3D.connect(controller, rev)

end
pendulum = PendulumWithController(Lx=1.6, m=0.5, sceneOptions = Modia3D.SceneOptions(visualizeFrames=true, defaultFrameLength=0.3))
model    = Modia3D.SimulationModel( pendulum )
#ModiaMath.print_ModelVariables(model)
result   = ModiaMath.simulate!(model, stopTime=5.0, log=false, tolerance=1e-4)
ModiaMath.plot(result, [("rev.phi","c.sine_y"), "rev.w", "rev.a", "rev.tau"])

println("... success of Simulate_ControllerDamper.jl!")
end