text:
module aws_route_53
include("AWSCorePrototypeServices.jl")
using .AWSCorePrototypeServices: route_53

"""
Gets a list of the VPCs that were created by other accounts and that can be associated with a specified hosted zone because you've submitted one or more CreateVPCAssociationAuthorization requests.  The response includes a VPCs element with a VPC child element for each VPC that can be associated with the hosted zone.
"""
ListVPCAssociationAuthorizations(HostedZoneId) = route_53("GET", "/2013-04-01/hostedzone/$Id/authorizevpcassociation")

"""
Gets the maximum number of hosted zones that you can associate with the specified reusable delegation set. For the default limit, see Limits in the Amazon Route 53 Developer Guide. To request a higher limit, open a case.
"""
GetReusableDelegationSetLimit(Type, DelegationSetId) = route_53("GET", "/2013-04-01/reusabledelegationsetlimit/$Id/$Type")

"""
Creates a new version of an existing traffic policy. When you create a new version of a traffic policy, you specify the ID of the traffic policy that you want to update and a JSON-formatted document that describes the new version. You use traffic policies to create multiple DNS resource record sets for one domain name (such as example.com) or one subdomain name (such as www.example.com). You can create a maximum of 1000 versions of a traffic policy. If you reach the limit and need to create another version, you'll need to start a new traffic policy.
"""
CreateTrafficPolicyVersion(Id, Document) = route_53("POST", "/2013-04-01/trafficpolicy/$Id")

"""
Gets status of a specified health check.
"""
GetHealthCheckStatus(HealthCheckId) = route_53("GET", "/2013-04-01/healthcheck/$HealthCheckId/status")

"""
Removes authorization to submit an AssociateVPCWithHostedZone request to associate a specified VPC with a hosted zone that was created by a different account. You must use the account that created the hosted zone to submit a DeleteVPCAssociationAuthorization request.  Sending this request only prevents the AWS account that created the VPC from associating the VPC with the Amazon Route 53 hosted zone in the future. If the VPC is already associated with the hosted zone, DeleteVPCAssociationAuthorization won't disassociate the VPC from the hosted zone. If you want to delete an existing association, use DisassociateVPCFromHostedZone.
"""
DeleteVPCAssociationAuthorization(HostedZoneId, VPC) = route_53("POST", "/2013-04-01/hostedzone/$Id/deauthorizevpcassociation")

"""
Retrieve a list of the health checks that are associated with the current AWS account.
"""
ListHealthChecks() = route_53("GET", "/2013-04-01/healthcheck")

"""
Retrieves information about a specified reusable delegation set, including the four name servers that are assigned to the delegation set.
"""
GetReusableDelegationSet(Id) = route_53("GET", "/2013-04-01/delegationset/$Id")

"""
Adds, edits, or deletes tags for a health check or a hosted zone. For information about using tags for cost allocation, see Using Cost Allocation Tags in the AWS Billing and Cost Management User Guide.
"""
ChangeTagsForResource(ResourceType, ResourceId) = route_53("POST", "/2013-04-01/tags/$ResourceType/$ResourceId")

"""
Gets information about all of the versions for a specified traffic policy. Traffic policy versions are listed in numerical order by VersionNumber.
"""
ListTrafficPolicyVersions(Id) = route_53("GET", "/2013-04-01/trafficpolicies/$Id/versions")

"""
Deletes a hosted zone. If the hosted zone was created by another service, such as AWS Cloud Map, see Deleting Public Hosted Zones That Were Created by Another Service in the Amazon Route 53 Developer Guide for information about how to delete it. (The process is the same for public and private hosted zones that were created by another service.) If you want to keep your domain registration but you want to stop routing internet traffic to your website or web application, we recommend that you delete resource record sets in the hosted zone instead of deleting the hosted zone.  If you delete a hosted zone, you can't undelete it. You must create a new hosted zone and update the name servers for your domain registration, which can require up to 48 hours to take effect. (If you delegated responsibility for a subdomain to a hosted zone and you delete the child hosted zone, you must update the name servers in the parent hosted zone.) In addition, if you delete a hosted zone, someone could hijack the domain and route traffic to their own resources using your domain name.  If you want to avoid the monthly charge for the hosted zone, you can transfer DNS service for the domain to a free DNS service. When you transfer DNS service, you have to update the name servers for the domain registration. If the domain is registered with Route 53, see UpdateDomainNameservers for information about how to replace Route 53 name servers with name servers for the new DNS service. If the domain is registered with another registrar, use the method provided by the registrar to update name servers for the domain registration. For more information, perform an internet search on "free DNS service." You can delete a hosted zone only if it contains only the default SOA record and NS resource record sets. If the hosted zone contains other resource record sets, you must delete them before you can delete the hosted zone. If you try to delete a hosted zone that contains other resource record sets, the request fails, and Route 53 returns a HostedZoneNotEmpty error. For information about deleting records from your hosted zone, see ChangeResourceRecordSets. To verify that the hosted zone has been deleted, do one of the following:   Use the GetHostedZone action to request information about the hosted zone.   Use the ListHostedZones action to get a list of the hosted zones associated with the current AWS account.
"""
DeleteHostedZone(Id) = route_53("DELETE", "/2013-04-01/hostedzone/$Id")

"""
Gets information about the latest version for every traffic policy that is associated with the current AWS account. Policies are listed in the order that they were created in.
"""
ListTrafficPolicies() = route_53("GET", "/2013-04-01/trafficpolicies")

"""
Gets information about a specified configuration for DNS query logging. For more information about DNS query logs, see CreateQueryLoggingConfig and Logging DNS Queries.
"""
GetQueryLoggingConfig(Id) = route_53("GET", "/2013-04-01/queryloggingconfig/$Id")

"""
Lists the resource record sets in a specified hosted zone.  ListResourceRecordSets returns up to 100 resource record sets at a time in ASCII order, beginning at a position specified by the name and type elements.  Sort order   ListResourceRecordSets sorts results first by DNS name with the labels reversed, for example:  com.example.www.  Note the trailing dot, which can change the sort order when the record name contains characters that appear before . (decimal 46) in the ASCII table. These characters include the following: ! " # $ % & ' ( ) * + , -  When multiple records have the same DNS name, ListResourceRecordSets sorts results by the record type.  Specifying where to start listing records  You can use the name and type elements to specify the resource record set that the list begins with:  If you do not specify Name or Type  The results begin with the first resource record set that the hosted zone contains.  If you specify Name but not Type  The results begin with the first resource record set in the list whose name is greater than or equal to Name.  If you specify Type but not Name  Amazon Route 53 returns the InvalidInput error.  If you specify both Name and Type  The results begin with the first resource record set in the list whose name is greater than or equal to Name, and whose type is greater than or equal to Type.    Resource record sets that are PENDING  This action returns the most current version of the records. This includes records that are PENDING, and that are not yet available on all Route 53 DNS servers.  Changing resource record sets  To ensure that you get an accurate listing of the resource record sets for a hosted zone at a point in time, do not submit a ChangeResourceRecordSets request while you're paging through the results of a ListResourceRecordSets request. If you do, some pages may display results without the latest changes while other pages display results with the latest changes.  Displaying the next page of results  If a ListResourceRecordSets command returns more than one page of results, the value of IsTruncated is true. To display the next page of results, get the values of NextRecordName, NextRecordType, and NextRecordIdentifier (if any) from the response. Then submit another ListResourceRecordSets request, and specify those values for StartRecordName, StartRecordType, and StartRecordIdentifier.
"""
ListResourceRecordSets(HostedZoneId) = route_53("GET", "/2013-04-01/hostedzone/$Id/rrset")

"""
Deletes a traffic policy.
"""
DeleteTrafficPolicy(Id, Version) = route_53("DELETE", "/2013-04-01/trafficpolicy/$Id/$Version")

"""
Deletes a traffic policy instance and all of the resource record sets that Amazon Route 53 created when you created the instance.  In the Route 53 console, traffic policy instances are known as policy records.
"""
DeleteTrafficPolicyInstance(Id) = route_53("DELETE", "/2013-04-01/trafficpolicyinstance/$Id")

"""
Retrieves the number of hosted zones that are associated with the current AWS account.
"""
GetHostedZoneCount() = route_53("GET", "/2013-04-01/hostedzonecount")

"""
Creates resource record sets in a specified hosted zone based on the settings in a specified traffic policy version. In addition, CreateTrafficPolicyInstance associates the resource record sets with a specified domain name (such as example.com) or subdomain name (such as www.example.com). Amazon Route 53 responds to DNS queries for the domain or subdomain name by using the resource record sets that CreateTrafficPolicyInstance created.
"""
CreateTrafficPolicyInstance(HostedZoneId, Name, TTL, TrafficPolicyId, TrafficPolicyVersion) = route_53("POST", "/2013-04-01/trafficpolicyinstance")

"""
Gets information about the traffic policy instances that you created by using a specify traffic policy version.  After you submit a CreateTrafficPolicyInstance or an UpdateTrafficPolicyInstance request, there's a brief delay while Amazon Route 53 creates the resource record sets that are specified in the traffic policy definition. For more information, see the State response element.  Route 53 returns a maximum of 100 items in each response. If you have a lot of traffic policy instances, you can use the MaxItems parameter to list them in groups of up to 100.
"""
ListTrafficPolicyInstancesByPolicy(TrafficPolicyId, TrafficPolicyVersion) = route_53("GET", "/2013-04-01/trafficpolicyinstances/trafficpolicy")

"""
Retrieves the number of health checks that are associated with the current AWS account.
"""
GetHealthCheckCount() = route_53("GET", "/2013-04-01/healthcheckcount")

"""
Deletes a configuration for DNS query logging. If you delete a configuration, Amazon Route 53 stops sending query logs to CloudWatch Logs. Route 53 doesn't delete any logs that are already in CloudWatch Logs. For more information about DNS query logs, see CreateQueryLoggingConfig.
"""
DeleteQueryLoggingConfig(Id) = route_53("DELETE", "/2013-04-01/queryloggingconfig/$Id")

"""
Gets the value that Amazon Route 53 returns in response to a DNS request for a specified record name and type. You can optionally specify the IP address of a DNS resolver, an EDNS0 client subnet IP address, and a subnet mask.
"""
TestDNSAnswer(HostedZoneId, RecordName, RecordType) = route_53("GET", "/2013-04-01/testdnsanswer")

"""
Retrieves a list of your hosted zones in lexicographic order. The response includes a HostedZones child element for each hosted zone created by the current AWS account.   ListHostedZonesByName sorts hosted zones by name with the labels reversed. For example:  com.example.www.  Note the trailing dot, which can change the sort order in some circumstances. If the domain name includes escape characters or Punycode, ListHostedZonesByName alphabetizes the domain name using the escaped or Punycoded value, which is the format that Amazon Route 53 saves in its database. For example, to create a hosted zone for exämple.com, you specify ex\344mple.com for the domain name. ListHostedZonesByName alphabetizes it as:  com.ex\344mple.  The labels are reversed and alphabetized using the escaped value. For more information about valid domain name formats, including internationalized domain names, see DNS Domain Name Format in the Amazon Route 53 Developer Guide. Route 53 returns up to 100 items in each response. If you have a lot of hosted zones, use the MaxItems parameter to list them in groups of up to 100. The response includes values that help navigate from one group of MaxItems hosted zones to the next:   The DNSName and HostedZoneId elements in the response contain the values, if any, specified for the dnsname and hostedzoneid parameters in the request that produced the current response.   The MaxItems element in the response contains the value, if any, that you specified for the maxitems parameter in the request that produced the current response.   If the value of IsTruncated in the response is true, there are more hosted zones associated with the current AWS account.  If IsTruncated is false, this response includes the last hosted zone that is associated with the current account. The NextDNSName element and NextHostedZoneId elements are omitted from the response.   The NextDNSName and NextHostedZoneId elements in the response contain the domain name and the hosted zone ID of the next hosted zone that is associated with the current AWS account. If you want to list more hosted zones, make another call to ListHostedZonesByName, and specify the value of NextDNSName and NextHostedZoneId in the dnsname and hostedzoneid parameters, respectively.
"""
ListHostedZonesByName() = route_53("GET", "/2013-04-01/hostedzonesbyname")

"""
Retrieves a list of the public and private hosted zones that are associated with the current AWS account. The response includes a HostedZones child element for each hosted zone. Amazon Route 53 returns a maximum of 100 items in each response. If you have a lot of hosted zones, you can use the maxitems parameter to list them in groups of up to 100.
"""
ListHostedZones() = route_53("GET", "/2013-04-01/hostedzone")

"""
Associates an Amazon VPC with a private hosted zone.   To perform the association, the VPC and the private hosted zone must already exist. You can't convert a public hosted zone into a private hosted zone.   If you want to associate a VPC that was created by using one AWS account with a private hosted zone that was created by using a different account, the AWS account that created the private hosted zone must first submit a CreateVPCAssociationAuthorization request. Then the account that created the VPC must submit an AssociateVPCWithHostedZone request.
"""
AssociateVPCWithHostedZone(HostedZoneId, VPC) = route_53("POST", "/2013-04-01/hostedzone/$Id/associatevpc")

"""
Disassociates a VPC from a Amazon Route 53 private hosted zone. Note the following:   You can't disassociate the last VPC from a private hosted zone.   You can't convert a private hosted zone into a public hosted zone.   You can submit a DisassociateVPCFromHostedZone request using either the account that created the hosted zone or the account that created the VPC.
"""
DisassociateVPCFromHostedZone(HostedZoneId, VPC) = route_53("POST", "/2013-04-01/hostedzone/$Id/disassociatevpc")

"""
Updates the comment for a specified traffic policy version.
"""
UpdateTrafficPolicyComment(Id, Version, Comment) = route_53("POST", "/2013-04-01/trafficpolicy/$Id/$Version")

"""
Authorizes the AWS account that created a specified VPC to submit an AssociateVPCWithHostedZone request to associate the VPC with a specified hosted zone that was created by a different account. To submit a CreateVPCAssociationAuthorization request, you must use the account that created the hosted zone. After you authorize the association, use the account that created the VPC to submit an AssociateVPCWithHostedZone request.  If you want to associate multiple VPCs that you created by using one account with a hosted zone that you created by using a different account, you must submit one authorization request for each VPC.
"""
CreateVPCAssociationAuthorization(HostedZoneId, VPC) = route_53("POST", "/2013-04-01/hostedzone/$Id/authorizevpcassociation")

"""
Lists tags for up to 10 health checks or hosted zones. For information about using tags for cost allocation, see Using Cost Allocation Tags in the AWS Billing and Cost Management User Guide.
"""
ListTagsForResources(ResourceType, ResourceIds) = route_53("POST", "/2013-04-01/tags/$ResourceType")

"""
Retrieves a list of the reusable delegation sets that are associated with the current AWS account.
"""
ListReusableDelegationSets() = route_53("GET", "/2013-04-01/delegationset")

"""
Gets information about the traffic policy instances that you created by using the current AWS account.  After you submit an UpdateTrafficPolicyInstance request, there's a brief delay while Amazon Route 53 creates the resource record sets that are specified in the traffic policy definition. For more information, see the State response element.  Route 53 returns a maximum of 100 items in each response. If you have a lot of traffic policy instances, you can use the MaxItems parameter to list them in groups of up to 100.
"""
ListTrafficPolicyInstances() = route_53("GET", "/2013-04-01/trafficpolicyinstances")

"""
Gets information about a specified hosted zone including the four name servers assigned to the hosted zone.
"""
GetHostedZone(Id) = route_53("GET", "/2013-04-01/hostedzone/$Id")

"""
Lists tags for one health check or hosted zone.  For information about using tags for cost allocation, see Using Cost Allocation Tags in the AWS Billing and Cost Management User Guide.
"""
ListTagsForResource(ResourceType, ResourceId) = route_53("GET", "/2013-04-01/tags/$ResourceType/$ResourceId")

"""
Gets information about whether a specified geographic location is supported for Amazon Route 53 geolocation resource record sets. Use the following syntax to determine whether a continent is supported for geolocation:  GET /2013-04-01/geolocation?continentcode=two-letter abbreviation for a continent   Use the following syntax to determine whether a country is supported for geolocation:  GET /2013-04-01/geolocation?countrycode=two-character country code   Use the following syntax to determine whether a subdivision of a country is supported for geolocation:  GET /2013-04-01/geolocation?countrycode=two-character country code&subdivisioncode=subdivision code
"""
GetGeoLocation() = route_53("GET", "/2013-04-01/geolocation")

"""
Updates an existing health check. Note that some values can't be updated.  For more information about updating health checks, see Creating, Updating, and Deleting Health Checks in the Amazon Route 53 Developer Guide.
"""
UpdateHealthCheck(HealthCheckId) = route_53("POST", "/2013-04-01/healthcheck/$HealthCheckId")

"""
Deletes a reusable delegation set.  You can delete a reusable delegation set only if it isn't associated with any hosted zones.  To verify that the reusable delegation set is not associated with any hosted zones, submit a GetReusableDelegationSet request and specify the ID of the reusable delegation set that you want to delete.
"""
DeleteReusableDelegationSet(Id) = route_53("DELETE", "/2013-04-01/delegationset/$Id")

"""
Creates a new health check. For information about adding health checks to resource record sets, see HealthCheckId in ChangeResourceRecordSets.   ELB Load Balancers  If you're registering EC2 instances with an Elastic Load Balancing (ELB) load balancer, do not create Amazon Route 53 health checks for the EC2 instances. When you register an EC2 instance with a load balancer, you configure settings for an ELB health check, which performs a similar function to a Route 53 health check.  Private Hosted Zones  You can associate health checks with failover resource record sets in a private hosted zone. Note the following:   Route 53 health checkers are outside the VPC. To check the health of an endpoint within a VPC by IP address, you must assign a public IP address to the instance in the VPC.   You can configure a health checker to check the health of an external resource that the instance relies on, such as a database server.   You can create a CloudWatch metric, associate an alarm with the metric, and then create a health check that is based on the state of the alarm. For example, you might create a CloudWatch metric that checks the status of the Amazon EC2 StatusCheckFailed metric, add an alarm to the metric, and then create a health check that is based on the state of the alarm. For information about creating CloudWatch metrics and alarms by using the CloudWatch console, see the Amazon CloudWatch User Guide.
"""
CreateHealthCheck(CallerReference, HealthCheckConfig) = route_53("POST", "/2013-04-01/healthcheck")

"""
Creates a traffic policy, which you use to create multiple DNS resource record sets for one domain name (such as example.com) or one subdomain name (such as www.example.com).
"""
CreateTrafficPolicy(Name, Document) = route_53("POST", "/2013-04-01/trafficpolicy")

"""
Creates a delegation set (a group of four name servers) that can be reused by multiple hosted zones. If a hosted zoned ID is specified, CreateReusableDelegationSet marks the delegation set associated with that zone as reusable.  You can't associate a reusable delegation set with a private hosted zone.  For information about using a reusable delegation set to configure white label name servers, see Configuring White Label Name Servers. The process for migrating existing hosted zones to use a reusable delegation set is comparable to the process for configuring white label name servers. You need to perform the following steps:   Create a reusable delegation set.   Recreate hosted zones, and reduce the TTL to 60 seconds or less.   Recreate resource record sets in the new hosted zones.   Change the registrar's name servers to use the name servers for the new hosted zones.   Monitor traffic for the website or application.   Change TTLs back to their original values.   If you want to migrate existing hosted zones to use a reusable delegation set, the existing hosted zones can't use any of the name servers that are assigned to the reusable delegation set. If one or more hosted zones do use one or more name servers that are assigned to the reusable delegation set, you can do one of the following:   For small numbers of hosted zones—up to a few hundred—it's relatively easy to create reusable delegation sets until you get one that has four name servers that don't overlap with any of the name servers in your hosted zones.   For larger numbers of hosted zones, the easiest solution is to use more than one reusable delegation set.   For larger numbers of hosted zones, you can also migrate hosted zones that have overlapping name servers to hosted zones that don't have overlapping name servers, then migrate the hosted zones again to use the reusable delegation set.
"""
CreateReusableDelegationSet(CallerReference) = route_53("POST", "/2013-04-01/delegationset")

"""
  GetCheckerIpRanges still works, but we recommend that you download ip-ranges.json, which includes IP address ranges for all AWS services. For more information, see IP Address Ranges of Amazon Route 53 Servers in the Amazon Route 53 Developer Guide.
"""
GetCheckerIpRanges() = route_53("GET", "/2013-04-01/checkeripranges")

"""
Updates the comment for a specified hosted zone.
"""
UpdateHostedZoneComment(Id) = route_53("POST", "/2013-04-01/hostedzone/$Id")

"""
Creates a configuration for DNS query logging. After you create a query logging configuration, Amazon Route 53 begins to publish log data to an Amazon CloudWatch Logs log group. DNS query logs contain information about the queries that Route 53 receives for a specified public hosted zone, such as the following:   Route 53 edge location that responded to the DNS query   Domain or subdomain that was requested   DNS record type, such as A or AAAA   DNS response code, such as NoError or ServFail     Log Group and Resource Policy  Before you create a query logging configuration, perform the following operations.  If you create a query logging configuration using the Route 53 console, Route 53 performs these operations automatically.    Create a CloudWatch Logs log group, and make note of the ARN, which you specify when you create a query logging configuration. Note the following:   You must create the log group in the us-east-1 region.   You must use the same AWS account to create the log group and the hosted zone that you want to configure query logging for.   When you create log groups for query logging, we recommend that you use a consistent prefix, for example:  /aws/route53/hosted zone name   In the next step, you'll create a resource policy, which controls access to one or more log groups and the associated AWS resources, such as Route 53 hosted zones. There's a limit on the number of resource policies that you can create, so we recommend that you use a consistent prefix so you can use the same resource policy for all the log groups that you create for query logging.     Create a CloudWatch Logs resource policy, and give it the permissions that Route 53 needs to create log streams and to send query logs to log streams. For the value of Resource, specify the ARN for the log group that you created in the previous step. To use the same resource policy for all the CloudWatch Logs log groups that you created for query logging configurations, replace the hosted zone name with *, for example:  arn:aws:logs:us-east-1:123412341234:log-group:/aws/route53/*   You can't use the CloudWatch console to create or edit a resource policy. You must use the CloudWatch API, one of the AWS SDKs, or the AWS CLI.     Log Streams and Edge Locations  When Route 53 finishes creating the configuration for DNS query logging, it does the following:   Creates a log stream for an edge location the first time that the edge location responds to DNS queries for the specified hosted zone. That log stream is used to log all queries that Route 53 responds to for that edge location.   Begins to send query logs to the applicable log stream.   The name of each log stream is in the following format:   hosted zone ID/edge location code   The edge location code is a three-letter code and an arbitrarily assigned number, for example, DFW3. The three-letter code typically corresponds with the International Air Transport Association airport code for an airport near the edge location. (These abbreviations might change in the future.) For a list of edge locations, see "The Route 53 Global Network" on the Route 53 Product Details page.  Queries That Are Logged  Query logs contain only the queries that DNS resolvers forward to Route 53. If a DNS resolver has already cached the response to a query (such as the IP address for a load balancer for example.com), the resolver will continue to return the cached response. It doesn't forward another query to Route 53 until the TTL for the corresponding resource record set expires. Depending on how many DNS queries are submitted for a resource record set, and depending on the TTL for that resource record set, query logs might contain information about only one query out of every several thousand queries that are submitted to DNS. For more information about how DNS works, see Routing Internet Traffic to Your Website or Web Application in the Amazon Route 53 Developer Guide.  Log File Format  For a list of the values in each query log and the format of each value, see Logging DNS Queries in the Amazon Route 53 Developer Guide.  Pricing  For information about charges for query logs, see Amazon CloudWatch Pricing.  How to Stop Logging  If you want Route 53 to stop sending query logs to CloudWatch Logs, delete the query logging configuration. For more information, see DeleteQueryLoggingConfig.
"""
CreateQueryLoggingConfig(HostedZoneId, CloudWatchLogsLogGroupArn) = route_53("POST", "/2013-04-01/queryloggingconfig")

"""
Gets information about a specified traffic policy instance.  After you submit a CreateTrafficPolicyInstance or an UpdateTrafficPolicyInstance request, there's a brief delay while Amazon Route 53 creates the resource record sets that are specified in the traffic policy definition. For more information, see the State response element.   In the Route 53 console, traffic policy instances are known as policy records.
"""
GetTrafficPolicyInstance(Id) = route_53("GET", "/2013-04-01/trafficpolicyinstance/$Id")

"""
Deletes a health check.  Amazon Route 53 does not prevent you from deleting a health check even if the health check is associated with one or more resource record sets. If you delete a health check and you don't update the associated resource record sets, the future status of the health check can't be predicted and may change. This will affect the routing of DNS queries for your DNS failover configuration. For more information, see Replacing and Deleting Health Checks in the Amazon Route 53 Developer Guide.
"""
DeleteHealthCheck(HealthCheckId) = route_53("DELETE", "/2013-04-01/healthcheck/$HealthCheckId")

"""
Gets the specified limit for a specified hosted zone, for example, the maximum number of records that you can create in the hosted zone.  For the default limit, see Limits in the Amazon Route 53 Developer Guide. To request a higher limit, open a case.
"""
GetHostedZoneLimit(Type, HostedZoneId) = route_53("GET", "/2013-04-01/hostedzonelimit/$Id/$Type")

"""
Updates the resource record sets in a specified hosted zone that were created based on the settings in a specified traffic policy version. When you update a traffic policy instance, Amazon Route 53 continues to respond to DNS queries for the root resource record set name (such as example.com) while it replaces one group of resource record sets with another. Route 53 performs the following operations:   Route 53 creates a new group of resource record sets based on the specified traffic policy. This is true regardless of how significant the differences are between the existing resource record sets and the new resource record sets.    When all of the new resource record sets have been created, Route 53 starts to respond to DNS queries for the root resource record set name (such as example.com) by using the new resource record sets.   Route 53 deletes the old group of resource record sets that are associated with the root resource record set name.
"""
UpdateTrafficPolicyInstance(Id, TTL, TrafficPolicyId, TrafficPolicyVersion) = route_53("POST", "/2013-04-01/trafficpolicyinstance/$Id")

"""
Gets information about a specific traffic policy version.
"""
GetTrafficPolicy(Id, Version) = route_53("GET", "/2013-04-01/trafficpolicy/$Id/$Version")

"""
Retrieves a list of supported geographic locations. Countries are listed first, and continents are listed last. If Amazon Route 53 supports subdivisions for a country (for example, states or provinces), the subdivisions for that country are listed in alphabetical order immediately after the corresponding country.
"""
ListGeoLocations() = route_53("GET", "/2013-04-01/geolocations")

"""
Gets the reason that a specified health check failed most recently.
"""
GetHealthCheckLastFailureReason(HealthCheckId) = route_53("GET", "/2013-04-01/healthcheck/$HealthCheckId/lastfailurereason")

"""
Gets information about the traffic policy instances that you created in a specified hosted zone.  After you submit a CreateTrafficPolicyInstance or an UpdateTrafficPolicyInstance request, there's a brief delay while Amazon Route 53 creates the resource record sets that are specified in the traffic policy definition. For more information, see the State response element.  Route 53 returns a maximum of 100 items in each response. If you have a lot of traffic policy instances, you can use the MaxItems parameter to list them in groups of up to 100.
"""
ListTrafficPolicyInstancesByHostedZone(HostedZoneId) = route_53("GET", "/2013-04-01/trafficpolicyinstances/hostedzone")

"""
Creates, changes, or deletes a resource record set, which contains authoritative DNS information for a specified domain name or subdomain name. For example, you can use ChangeResourceRecordSets to create a resource record set that routes traffic for test.example.com to a web server that has an IP address of 192.0.2.44.  Change Batches and Transactional Changes  The request body must include a document with a ChangeResourceRecordSetsRequest element. The request body contains a list of change items, known as a change batch. Change batches are considered transactional changes. When using the Amazon Route 53 API to change resource record sets, Route 53 either makes all or none of the changes in a change batch request. This ensures that Route 53 never partially implements the intended changes to the resource record sets in a hosted zone.  For example, a change batch request that deletes the CNAME record for www.example.com and creates an alias resource record set for www.example.com. Route 53 deletes the first resource record set and creates the second resource record set in a single operation. If either the DELETE or the CREATE action fails, then both changes (plus any other changes in the batch) fail, and the original CNAME record continues to exist.  Due to the nature of transactional changes, you can't delete the same resource record set more than once in a single change batch. If you attempt to delete the same change batch more than once, Route 53 returns an InvalidChangeBatch error.   Traffic Flow  To create resource record sets for complex routing configurations, use either the traffic flow visual editor in the Route 53 console or the API actions for traffic policies and traffic policy instances. Save the configuration as a traffic policy, then associate the traffic policy with one or more domain names (such as example.com) or subdomain names (such as www.example.com), in the same hosted zone or in multiple hosted zones. You can roll back the updates if the new configuration isn't performing as expected. For more information, see Using Traffic Flow to Route DNS Traffic in the Amazon Route 53 Developer Guide.  Create, Delete, and Upsert  Use ChangeResourceRecordsSetsRequest to perform the following actions:    CREATE: Creates a resource record set that has the specified values.    DELETE: Deletes an existing resource record set that has the specified values.    UPSERT: If a resource record set does not already exist, AWS creates it. If a resource set does exist, Route 53 updates it with the values in the request.     Syntaxes for Creating, Updating, and Deleting Resource Record Sets  The syntax for a request depends on the type of resource record set that you want to create, delete, or update, such as weighted, alias, or failover. The XML elements in your request must appear in the order listed in the syntax.  For an example for each type of resource record set, see "Examples." Don't refer to the syntax in the "Parameter Syntax" section, which includes all of the elements for every kind of resource record set that you can create, delete, or update by using ChangeResourceRecordSets.   Change Propagation to Route 53 DNS Servers  When you submit a ChangeResourceRecordSets request, Route 53 propagates your changes to all of the Route 53 authoritative DNS servers. While your changes are propagating, GetChange returns a status of PENDING. When propagation is complete, GetChange returns a status of INSYNC. Changes generally propagate to all Route 53 name servers within 60 seconds. For more information, see GetChange.  Limits on ChangeResourceRecordSets Requests  For information about the limits on a ChangeResourceRecordSets request, see Limits in the Amazon Route 53 Developer Guide.
"""
ChangeResourceRecordSets(HostedZoneId, ChangeBatch) = route_53("POST", "/2013-04-01/hostedzone/$Id/rrset/")

"""
Gets the number of traffic policy instances that are associated with the current AWS account.
"""
GetTrafficPolicyInstanceCount() = route_53("GET", "/2013-04-01/trafficpolicyinstancecount")

"""
Creates a new public or private hosted zone. You create records in a public hosted zone to define how you want to route traffic on the internet for a domain, such as example.com, and its subdomains (apex.example.com, acme.example.com). You create records in a private hosted zone to define how you want to route traffic for a domain and its subdomains within one or more Amazon Virtual Private Clouds (Amazon VPCs).   You can't convert a public hosted zone to a private hosted zone or vice versa. Instead, you must create a new hosted zone with the same name and create new resource record sets.  For more information about charges for hosted zones, see Amazon Route 53 Pricing. Note the following:   You can't create a hosted zone for a top-level domain (TLD) such as .com.   For public hosted zones, Amazon Route 53 automatically creates a default SOA record and four NS records for the zone. For more information about SOA and NS records, see NS and SOA Records that Route 53 Creates for a Hosted Zone in the Amazon Route 53 Developer Guide. If you want to use the same name servers for multiple public hosted zones, you can optionally associate a reusable delegation set with the hosted zone. See the DelegationSetId element.   If your domain is registered with a registrar other than Route 53, you must update the name servers with your registrar to make Route 53 the DNS service for the domain. For more information, see Migrating DNS Service for an Existing Domain to Amazon Route 53 in the Amazon Route 53 Developer Guide.    When you submit a CreateHostedZone request, the initial status of the hosted zone is PENDING. For public hosted zones, this means that the NS and SOA records are not yet available on all Route 53 DNS servers. When the NS and SOA records are available, the status of the zone changes to INSYNC.
"""
CreateHostedZone(Name, CallerReference) = route_53("POST", "/2013-04-01/hostedzone")

"""
Lists the configurations for DNS query logging that are associated with the current AWS account or the configuration that is associated with a specified hosted zone. For more information about DNS query logs, see CreateQueryLoggingConfig. Additional information, including the format of DNS query logs, appears in Logging DNS Queries in the Amazon Route 53 Developer Guide.
"""
ListQueryLoggingConfigs() = route_53("GET", "/2013-04-01/queryloggingconfig")

"""
Returns the current status of a change batch request. The status is one of the following values:    PENDING indicates that the changes in this request have not propagated to all Amazon Route 53 DNS servers. This is the initial status of all change batch requests.    INSYNC indicates that the changes have propagated to all Route 53 DNS servers.
"""
GetChange(Id) = route_53("GET", "/2013-04-01/change/$Id")

"""
Gets the specified limit for the current account, for example, the maximum number of health checks that you can create using the account. For the default limit, see Limits in the Amazon Route 53 Developer Guide. To request a higher limit, open a case.  You can also view account limits in AWS Trusted Advisor. Sign in to the AWS Management Console and open the Trusted Advisor console at https://console.aws.amazon.com/trustedadvisor/. Then choose Service limits in the navigation pane.
"""
GetAccountLimit(Type) = route_53("GET", "/2013-04-01/accountlimit/$Type")

"""
Gets information about a specified health check.
"""
GetHealthCheck(HealthCheckId) = route_53("GET", "/2013-04-01/healthcheck/$HealthCheckId")
end

max_stars_repo_name: nicoleepp/JuliaCloud-AWS-State

max_issues_repo_name: nicoleepp/JuliaCloud-AWS-State

max_forks_repo_name: nicoleepp/JuliaCloud-AWS-State


text:
module JlTuner
    type ExprState
        stack::Array{Tuple{Expr, Int64}}
    end
    Base.copy(e::ExprState) = ExprState([i for i in e.stack])
    Base.start(e::Expr)::ExprState = ExprState([(e, 0)])
    Base.done(it::Expr, state::ExprState)::Bool = state.stack|>length == 0 || (state.stack|>length == 1 && state.stack[end][2] > state.stack[end][1].args|>length)
    function Base.next(it::Expr, state::ExprState)::Tuple{Any, ExprState}
        function promoteStack!(state::ExprState)
            if state.stack |> length > 0
                state.stack[end] = (state.stack[end][1], state.stack[end][2] + 1)
            end
        end
        function windStack!(state::ExprState)
            while state.stack |> length > 0 && state.stack[end][1].args |> length <= state.stack[end][2]
                pop!(state.stack)
            end
            promoteStack!(state)
        end

        s = copy(state)
        r = ()
        while true
            if s.stack |> isempty
                throw("expr stack must not be empty")
            end
            (e, i) = s.stack[end]

            if e |> typeof != Expr
                throw("non-expr in expr stack")
            end

            if i == 0
                promoteStack!(s)
                return (e, s)
            else
                if e.args[i] |> typeof == Expr
                    push!(s.stack, (e.args[i], 0))
                    continue
                else
                    windStack!(s)
                    return (e.args[i], s)
                end
            end
        end
    end
#=
:(  )
=#
    function interpretMetaInstruction(src::Any, ptn::Expr, env::Dict{Symbol, Expr})::Bool

    end
    function match(src::Expr, ptn::Expr, env::Dict{Symbol, Expr})::Bool
        sitr = start(src)
        pitr = start(ptn)
        while true
            if done(ptn, pitr)
                return true
            end
            if done(src, sitr)
                return false
            end
            (p, pitr) = next(ptn, pitr)
            (s, sitr) = next(src, sitr)

            if p |>typeof != Expr
                if p != s
                    return false
                end
                continue
            end
            if p.head == :macrocall
                # meta instructions
                #if
            else
                if p.head != s.head
                    return false
                end
                continue
            end
        end
        return true
    end
end

ext: jl


text:
module TestBuilderCLI

using Test
using Comonicon.Options
using Comonicon.Builder: print_builder_help, command_main
using FromFile

@from "../utils.jl" import with_args

module TestCLI end

@testset "CLI help info" begin
    options = Options.Comonicon(name = "test")
    @test with_args(["-h"]) do
        command_main(TestCLI, options)
    end === 0

    @test with_args(["aaahelp"]) do
        command_main(TestCLI, options)
    end === 1
end

end


text:
workspace()
using DoubleArrayTrie

homedir()
pwd()
lines = open(readlines, joinpath(pwd(),"test/words.txt"))
sort!(lines)

words = map(chomp, lines)
keys = map(w -> [Int(c) for c in w], words)
trie = DATrie(keys, words)

for i = 1:length(words)
  v = get(trie, keys[i], "")
  @assert v == words[i]
end


text:
include("./tree.jl")

type BBFMM
    tree::Tree

    chargeTree
    kernel
    rank

    R # translation from cluster to parent

    nCNode
    cNode # std cnode
    TNode # chey poly at cnodes

    BBFMM() = new(Tree(), [], Nil, 0, [], [],[],[])
end

function initialize!(b::BBFMM,  nCNode, source, target, charge, nSource, nTarget, rank, maxLevel, kernel::Function)
    b.tree = Tree()
    t = b.tree
    populate!(t, source, target, nSource, nTarget, rank, maxLevel)

    b.nCNode = nCNode
    b.chargeTree = charge
    b.kernel = kernel
    b.cNode = getStandardCNodes(nCNode)
    b.TNode = getStandardCPoly(nCNode, nCNode, b.cNode)
    b.R = getTransfer(nCNode, b.cNode, b.TNode)
    b.rank = nCNode^2

end

function FMM!(b::BBFMM)
    upPass!(b, 1)
    potential = zeros(b.tree.nTarget)
    downPass!(b, 1, potential)
    return potential
end

function upPass!(b::BBFMM, rootId)
    t = b.tree
    d = t.dict
    node = d[rootId]

    """
    allocate space for each node's properties.

    scaledCnode # cnodes
    cnodeCharge # charge on cnodes
    cnodePotential # potential on cnodes
    """
    node.cnodePotential = zeros(b.rank)
    node.cnodeCharge = zeros(b.rank)

    node.scaledCnode = []
    getScaledCNodes(b.nCNode, b.cNode, node.center, node.radius,node.scaledCnode)

    if (node.isLeaf)
        getCharge!(b, rootId)
        node.R = getTransferP2C(b.nCNode, node.source, node.center, node.radius, b.cNode, b.TNode)
        node.L = getTransferP2C(b.nCNode, node.target, node.center, node.radius, b.cNode, b.TNode)

        """
        interpolation
        """
        node.cnodeCharge += node.R' * node.charge

    else
        for l = 1:4
            upPass!(b, node.child[l])
            if (!d[node.child[l]].isEmpty)
                node.cnodeCharge += (b.R[l])' * d[node.child[l]].cnodeCharge
            end
        end
    end
end

function downPass!(b::BBFMM, rootId, potential)
    t = b.tree
    d = t.dict
    node = d[rootId]

    if (node.parent != -1)
        """
        Vlist
        """
        for l = 1:node.nVList
            if (!d[node.vList[l]].isEmpty)
                node.cnodePotential += getAccumulationCheb(b, b.nCNode, d[node.vList[l]].scaledCnode, b.nCNode, node.scaledCnode ,d[node.vList[l]].cnodeCharge)
            end
        end
        """
        XList
        """

        for l = 1:node.nXList
            if (!d[node.xList[l]].isEmpty)
                node.cnodePotential += getAccumulationCheb(b, b.nCNode, d[node.xList[l]].scaledCnode, b.nCNode, node.scaledCnode,d[node.xList[l]].cnodeCharge)
            end
        end
        """
        L2L
        """
        parentNode = d[node.parent]
        node.cnodePotential += b.R[node.nodeIndex]*parentNode.cnodePotential

    end

    if (node.isLeaf && node.nTarget!=0)
        node.potential = zeros(node.nTarget)

        """
        UList
        """
        for l = 1:node.nUList
            if(!d[node.uList[l]].isEmpty)
                getCharge!(b, node.uList[l])
                node.potential += getAccumulation(b, d[node.uList[l]].source, node.target, d[node.uList[l]].charge)
            end
        end

        """
        WList, should not be direct
        """

        for l = 1:node.nWList
            if(!d[node.wList[l]].isEmpty)
                getCharge!(b, node.wList[l])
                node.potential += getAccumulation(b, d[node.wList[l]].source, node.target, d[node.wList[l]].charge)
            end
        end

        """
        from potential on cheb nodes
        """
        node.potential += node.L * node.cnodePotential

        """
        finalize
        """
        for l = 1:node.nTarget
            potential[node.targetIndex[l]] += node.potential[l]
        end
    end

    if !node.isLeaf
        for l = 1:4
            downPass!(b, node.child[l], potential)
        end
    end
end

function downPass2!(b::BBFMM, rootId, potential)
    t = b.tree
    d = t.dict
    node = d[rootId]
    if (!node.isEmpty)
        if (node.isLeaf)
            #ULIST
            node.potential = zeros(node.nTarget)
            for l = 1:node.nUList
                if(!d[node.uList[l]].isEmpty)
                    getCharge!(b, node.uList[l])
                    node.potential += getAccumulation(b, d[node.uList[l]].source, node.target, d[node.uList[l]].charge)
                end
            end
            # w empty

            node.potential += node.R*node.cnodePotential
            for l = 1:node.nTarget
                potential[node.targetIndex[l]] += node.potential[l]
            end
        else

            for k = 1:4
                childId = node.child[k]
                childnode = d[childId]
                if (!childnode.isEmpty)
                    for l = 1:childnode.nVList
                        childnode.cnodePotential += getAccumulationCheb(b, b.nCNode, d[childnode.vList[l]].scaledCnode, b.nCNode, childnode.scaledCnode ,d[childnode.vList[l]].cnodeCharge)

                    end
                end
            end

            for k = 1:4
                childId = node.child[k]
                childnode = d[childId]
                if (!childnode.isEmpty)
                    childnode.cnodePotential += b.R[k] * node.cnodePotential
                end
            end

            for k = 1:4
                downPass2!(b, node.child[k], potential)
            end
        end
    end
end

function getCharge!(b::BBFMM, id)
    # lazy evaluation
    node = b.tree.dict[id]
    if (node.chargeComputed == true)
        # pass
    else
        node.chargeComputed = true
        node.charge = zeros(node.nSource)
        for l = 1:node.nSource
            node.charge[l] = b.chargeTree[node.sourceIndex[l]]
        end
    end
end

function getScaledCNodes(nCNodes, cNode, center, radius, cheNode)
    for k = 1:nCNodes
        push!(cheNode, center + cNode[k]*radius)
    end
end

function getStandardCNodes(nCNodes)
    cNode = zeros(nCNodes)
    for k = 1:nCNodes
        cNode[k] = -cos((k - 0.5)*π/nCNodes)
    end
    return cNode
end

function getStandardCPoly(nPoly, N, x)
    Tnode = zeros(N, nPoly)
    Tnode[:, 1] = ones(N)

    if (nPoly > 1)
        Tnode[:, 2] = x
        for k = 3:nPoly
            Tnode[:, k] = 2.0 * x.*Tnode[:, k-1] - Tnode[:, k-2]
        end
    end

    return Tnode
end

function getTransfer(nCNode, cNode, TNode)
    S = getTransferP2C_CNode(nCNode, cNode, TNode)
    Transfer0 = S[1:nCNode, 1:nCNode]
    Transfer1 = S[(nCNode+1):2*nCNode, 1:nCNode]
    rank = nCNode * nCNode
    R1 = zeros(rank, rank)
    R2 = zeros(rank, rank)
    R3 = zeros(rank, rank)
    R4 = zeros(rank, rank)
    for i = 1:nCNode
        for j = 1:nCNode
            for k = 1:nCNode
                for l=1:nCNode
                    R1[(i-1)*nCNode+j, (k-1)*nCNode+l] = Transfer0[i,k]*Transfer0[j,l]
                    R2[(i-1)*nCNode+j, (k-1)*nCNode+l] = Transfer0[i,k]*Transfer1[j,l]
                    R3[(i-1)*nCNode+j, (k-1)*nCNode+l] = Transfer1[i,k]*Transfer0[j,l]
                    R4[(i-1)*nCNode+j, (k-1)*nCNode+l] = Transfer1[i,k]*Transfer1[j,l]
                end
            end
        end
    end
    return [R1, R2, R3, R4]
end

function getTransferP2C_CNode(nCNode, cNode, TNode)
    childcNode = zeros(2 * nCNode)
    childcNode[1:nCNode] = 0.5 * (cNode - ones(nCNode))
    childcNode[(nCNode + 1): 2*nCNode] = 0.5 * (cNode + ones(nCNode))
    Transfer = getStandardCPoly(nCNode, 2 * nCNode, childcNode)
    return (2.0 * Transfer * TNode' - ones(2*nCNode, nCNode))/nCNode
end

function getTransferP2C(nCNode, source, center, radius, cNode, TNode)
    N = length(source)
    loc0 = zeros(N)
    loc1 = zeros(N)
    for i = 1:N
        loc0[i] = (source[i][1] - center[1])/radius[1]
        loc1[i] = (source[i][2] - center[2])/radius[2]
    end

    T0 = getStandardCPoly(nCNode, N, loc0)
    T1 = getStandardCPoly(nCNode, N, loc1)

    T0 = (2.0 * T0 * TNode' - ones(N, nCNode))/nCNode
    T1 = (2.0 * T1 * TNode' - ones(N, nCNode))/nCNode

    rank = nCNode^2
    R = zeros(N, rank)
    for k = 1:N
        for i = 1:nCNode
            for j = 1:nCNode
                R[k, (j-1)*nCNode + i] = T0[k,i]*T1[k,j]
            end
        end
    end
    return R
end

function kernelEval(source, target, f)
    sourceSize = length(source)
    targetSize = length(target)
    K = zeros(targetSize, sourceSize)
    for s = 1:sourceSize
        for t = 1: targetSize
            K[t, s] = f(source[s], target[t])
        end
    end
    return K
end

function getAccumulation(b::BBFMM, source, target, charge)
    K = kernelEval(source, target, b.kernel)
    # K(target, source) * charge
    # density of length(target)
    # charge of lenth(source)
    return K * charge
end

function getAccumulationCheb(b::BBFMM, M, s, N, t, charge)
    sourceSize = length(s)^2
    targetSize = length(t)^2
    source = []
    target = []
    for j = 1:M
        for i = 1:M
            push!(source, [s[i][1], s[j][2]])
        end
    end

    for j = 1:N
        for i=1:N
            push!(target, [t[i][1], t[j][2]])
        end
    end

    K = kernelEval(source, target, b.kernel)
    return K*charge

end


text:
@testset "204.count-primes.jl" begin
    @test count_primes(1) == 0
    @test count_primes(10) == count_primes(11) == 4
    @test count_primes(1000) == 168
    @test count_primes(10000) == 1229
    @test count_primes(100000) == 9592
end


text:
using Pkg
Pkg.activate(".")

using ITensors
using LinearAlgebra
using DelimitedFiles

include("options.jl")

println()
print("Using $blas_num_threads BLAS thread")
blas_num_threads > 1 ? println("s") : println()

println()
println("Benchmarking $benchmarks")
println()

println("Bond dimensions set to:")
display(maxdims)
println()

warning_seperator = "X"^70

println()
println(warning_seperator)
if write_results
  println("XXX WARNING: benchmark results are set to be written to disk, may overwrite previous results")
else
  println("XXX WARNING: benchmark results are not set to be written to disk.")
end
println(warning_seperator)
println()

seperator = "#"^70

outputlevel = 1

for benchmark in benchmarks
  println("Running benchmark $benchmark for bond dimensions $(maxdims[benchmark])")
  println()
  for maxdim in maxdims[benchmark]
    if isnothing(which_version) || which_version == "julia"
      julia_dir = joinpath(@__DIR__, "bench_$benchmark", "julia")

      println(seperator)
      println("Run Julia benchmark $benchmark.")
      println()
      println("Benchmark located in path $julia_dir")
      println()
      println("Maximum bond dimension set to $maxdim.")
      println()

      BLAS.set_num_threads(blas_num_threads)

      include(joinpath(julia_dir, "run.jl"))

      # Trigger compilation
      run(maxdim = 10, nsweeps = 2, outputlevel = 0)

      time = @elapsed maxdim_ = run(maxdim = maxdim,
                                    outputlevel = outputlevel)

      println()
      println("Maximum dimension = $maxdim_")
      println("Total runtime = $time seconds")
      println()

      if write_results
        # TODO: add version number to data file name
        # v = Pkg.dependencies()[Base.UUID("9136182c-28ba-11e9-034c-db9fb085ebd5")].version
        # "$(v.major).$(v.minor).$(v.patch)"

        filename = "data_blas_num_threads_$(blas_num_threads)"
        filename *= "_maxdim_$(maxdim_).txt"
        filepath = joinpath(julia_dir, "data", filename)
        println("Writing results to $filepath")
        println()
        mkpath(dirname(filepath))
        writedlm(filepath, time)
      end
    end
    if isnothing(which_version) || which_version == "c++"
      cpp_dir = joinpath(@__DIR__, "bench_$benchmark", "c++")

      println(seperator)
      println("Run C++ benchmark $benchmark.")
      println()
      println("Benchmark located in path $cpp_dir")
      println()
      println("Maximum bond dimension set to $maxdim.")
      println()

      open("run.h", "w") do io
        write(io, "#include \"$(joinpath(cpp_dir, "run.h"))\"")
      end
      # Trigger rebuild
      touch("run.cc")
      println("Compile the benchmark")
      Base.run(`make`)
      println()
      rm("run.h")
      open("run.sh", "w") do io
        write(io, """#!/bin/bash
                     export MKL_NUM_THREADS=$blas_num_threads
                     export OPENBLAS_NUM_THREADS=$blas_num_threads
                     export OMP_NUM_THREADS=$omp_num_threads
                     ./run $maxdim $outputlevel""")
      end
      chmod("run.sh", 0o777)
      time = @elapsed Base.run(`./run.sh`)
      rm("run.sh")

      println()
      println("Maximum dimension = $maxdim")
      println("Total runtime = $time seconds")
      println()

      if write_results
        # TODO: add version number to data file name

        filename = "data_blas_num_threads_$(blas_num_threads)"
        filename *= "_maxdim_$(maxdim).txt"
        filepath = joinpath(cpp_dir, "data", filename)
        println("Writing results to $filepath")
        println()
        mkpath(dirname(filepath))
        writedlm(filepath, time)
      end
    end
  end
end


text:
const DimArrayOrStack = Union{AbstractDimArray,AbstractDimStack}

# getindex/view/setindex! ======================================================

#### Array getindex/view ####
# Integer returns a single value, but not for view
@propagate_inbounds Base.getindex(A::AbstractDimArray, i1::Integer, i2::Integer, I::Integer...) =
    Base.getindex(parent(A), i1, i2, I...)
# No indices. These just prevent stack overflows
@propagate_inbounds Base.getindex(A::AbstractDimArray) = Base.getindex(parent(A))
@propagate_inbounds Base.view(A::AbstractDimArray) = rebuild(A, Base.view(parent(A)), ())
#### Stack getindex ####
# Symbol key
@propagate_inbounds Base.getindex(s::AbstractDimStack, key::Symbol) =
    DimArray(data(s)[key], dims(s, layerdims(s, key)), refdims(s), key, layermetadata(s, key))
@propagate_inbounds Base.getindex(s::AbstractDimStack, i::Int, I::Int...) =
    map(A -> Base.getindex(A, i, I...), data(s))

for f in (:getindex, :view, :dotview)
    @eval begin
        #### Array getindex/view ###
        @propagate_inbounds Base.$f(A::AbstractDimArray, i::Integer) = Base.$f(parent(A), i)
        @propagate_inbounds Base.$f(A::AbstractDimArray, I...) =
            Base.$f(A, dims2indices(A, I)...)
        # Linear indexing returns parent type
        @propagate_inbounds Base.$f(A::AbstractDimArray, i::Union{Colon,AbstractVector{<:Integer}}) =
            Base.$f(parent(A), i)
        @propagate_inbounds Base.$f(A::AbstractDimArray, i::AbstractArray{<:Bool}) =
            Base.$f(parent(A), i)
        # Except 1D DimArrays
        @propagate_inbounds Base.$f(A::AbstractDimArray{<:Any, 1}, i::Union{Colon,AbstractVector{<:Integer}}) =
            rebuildsliced(Base.$f, A, Base.$f(parent(A), i), (i,))
        # Dimension indexing. Allows indexing with A[somedim=25.0] for Dim{:somedim}
        @propagate_inbounds Base.$f(A::AbstractDimArray, args::Dimension...; kw...) =
            Base.$f(A, dims2indices(A, (args..., _kwdims(kw.data)...))...)
        # Standard indices
        @propagate_inbounds Base.$f(A::AbstractDimArray, i1::StandardIndices, i2::StandardIndices, I::StandardIndices...) =
            rebuildsliced(Base.$f, A, Base.$f(parent(A), i1, i2, I...), (i1, i2, I...))
        @propagate_inbounds Base.$f(A::AbstractDimArray, I::CartesianIndex) = Base.$f(parent(A), I)

        #### Stack ###
        @propagate_inbounds function Base.$f(s::AbstractDimStack, I...; kw...)
            vals = map(A -> Base.$f(A, I...; kw...), dimarrays(s))
            if all(map(v -> v isa AbstractDimArray, vals))
                rebuildsliced(Base.$f, s, vals, (dims2indices(dims(s), I)))
            else
                vals
            end
        end
    end
end

#### Array setindex ####
@propagate_inbounds Base.setindex!(A::AbstractDimArray, x) = setindex!(parent(A), x)
@propagate_inbounds Base.setindex!(A::AbstractDimArray, x, args::Dimension...; kw...) =
    setindex!(A, x, dims2indices(A, (args..., _kwdims(kw.data)...))...)
@propagate_inbounds Base.setindex!(A::AbstractDimArray, x, i, I...) =
    setindex!(A, x, dims2indices(A, maybeselector(i, I...))...)
@propagate_inbounds Base.setindex!(A::AbstractDimArray, x, i1::StandardIndices, I::StandardIndices...) =
    setindex!(parent(A), x, i1, I...)
@propagate_inbounds Base.setindex!(A::AbstractDimArray, x, I::CartesianIndex) =
    setindex!(parent(A), x, I)
#### Stack setindex ####
@propagate_inbounds Base.setindex!(s::AbstractDimStack, xs, I...; kw...) =
    map((A, x) -> setindex!(A, x, I...; kw...), dimarrays(s), xs)
@propagate_inbounds function Base.setindex!(
    s::AbstractDimStack{<:NamedTuple{K1}}, xs::NamedTuple{K2}, I...; kw...
) where {K1,K2}
    K1 == K2 || _keysmismatch(K1, K2)
    map((A, x) -> setindex!(A, x, I...; kw...), dimarrays(s), xs)
end

#### dims2indices ####

"""
    dims2indices(dim::Dimension, lookup) => NTuple{Union{Colon,AbstractArray,Int}}

Convert a `Dimension` or `Selector` lookup to indices of Int, AbstractArray or Colon.
"""
@inline dims2indices(dim::Dimension, lookup::StandardIndices) = lookup
@inline dims2indices(dim::Dimension, lookup) = _dims2indices(dim, lookup)

@inline dims2indices(x, lookup) = dims2indices(dims(x), lookup)
@inline dims2indices(::Nothing, lookup) = _dimsnotdefinederror()
@inline dims2indices(dims::DimTuple, lookup) = dims2indices(dims, (lookup,))
# Standard array indices are simply returned
@inline dims2indices(dims::DimTuple, lookup::Tuple{Vararg{<:StandardIndices}}) = lookup
@inline dims2indices(dims::DimTuple, lookup::Tuple{<:CartesianIndex}) = lookup
@inline dims2indices(dims::DimTuple, lookup::Tuple) = sel2indices(dims, lookup)
@inline dims2indices(dims::DimTuple, lookup::Tuple{}) = ()
# Otherwise attempt to convert dims to indices
@inline function dims2indices(dims::DimTuple, lookup::DimTuple)
    extradims = otherdims(lookup, dims)
    length(extradims) > 0 && _warnextradims(extradims)
    _dims2indices(map(mode, dims), dims, sortdims(lookup, dims))
end

# Handle tuples with @generated
@inline _dims2indices(modes::Tuple{}, dims::Tuple{}, lookup::Tuple{}) = ()
@generated function _dims2indices(modes::Tuple, dims::Tuple, lookup::Tuple)
    unalligned = Expr(:tuple)
    ualookups = Expr(:tuple)
    alligned = Expr(:tuple)
    dimmerge = Expr(:tuple)
    a_count = ua_count = 0
    for (i, mp) in enumerate(modes.parameters)
        if mp <: Unaligned
            ua_count += 1
            push!(unalligned.args, :(dims[$i]))
            push!(ualookups.args, :(lookup[$i]))
            push!(dimmerge.args, :(uadims[$ua_count]))
        else
            a_count += 1
            push!(alligned.args, :(_dims2indices(dims[$i], lookup[$i])))
            # Update  the merged tuple
            push!(dimmerge.args, :(adims[$a_count]))
        end
    end

    if length(unalligned.args) > 1
        # Output the dimmerge, that will combine uadims and adims in the right order
        quote
             adims = $alligned
             # Unaligned dims have to be run together as a set
             uadims = unalligned2indices($unalligned, $ualookups)
             $dimmerge
        end
    else
        alligned
    end
end
# Single dim methods
# A Dimension type always means Colon(), as if it was constructed with the default value.
@inline _dims2indices(dim::Dimension, lookup::Type{<:Dimension}) = Colon()
# Nothing means nothing was passed for this dimension
@inline _dims2indices(dim::Dimension, lookup::Nothing) = Colon()
# Simply unwrap dimensions
@inline _dims2indices(dim::Dimension, lookup::Dimension) = sel2indices(dim, val(lookup))

@noinline _keysmismatch(K1, K2) = throw(ArgumentError("NamedTuple keys $K2 do not mach stack keys $K1"))


text:
using Optim, SMM, HTTP, GLM, LinearAlgebra, Random, Statistics, DataFrames, DataFramesMeta, CSV
#Worked with Ahmed and Aleeze
function ps7()
#Q1
url = "https://raw.githubusercontent.com/OU-PhD-Econometrics/fall-2020/master/ProblemSets/PS1-julia-intro/nlsw88.csv"
df = CSV.read(HTTP.get(url).body, DataFrame)
X = [ones(size(df,1),1) df.age df.race.==1 df.collgrad.==1]
y = df.married.==1

function ols_gmm(alpha, X,y)
        g = y .- X*alpha
        J = g'*I*g
        return J
end
alphhat_optim = optimize(a -> ols_gmm(a, X, y), rand(size(X,2)), LBFGS(), Optim.Options(g_tol=1e-8, iterations=100_000))
println(alphhat_optim.minimizer)
# Checking work
bols = inv(X'*X)*X'*y

#2a

df = dropmissing(df, :occupation)
df[df.occupation.==8 ,:occupation] .= 7
df[df.occupation.==9 ,:occupation] .= 7
df[df.occupation.==10,:occupation] .= 7
df[df.occupation.==11,:occupation] .= 7
df[df.occupation.==12,:occupation] .= 7
df[df.occupation.==13,:occupation] .= 7

X = [ones(size(df,1),1) df.age df.race.==1 df.collgrad.==1]
y = df.occupation

function mlogit(alpha, X, y)

    K = size(X,2)
    J = length(unique(y))
    N = length(y)
    bigY = zeros(N,J)
    for j=1:J
        bigY[:,j] = y.==j
    end
    bigAlpha = [reshape(alpha,K,J-1) zeros(K)]

    num = zeros(N,J)
    dem = zeros(N)
    for j=1:J
        num[:,j] = exp.(X*bigAlpha[:,j])
        dem .+= num[:,j]
    end

    P = num./repeat(dem,1,J)

    loglike = -sum( bigY.*log.(P) )

    return loglike
end

alpha_zero = zeros(6*size(X,2))
alpha_rand = rand(6*size(X,2))
alpha_true = [.1910213,-.0335262,.5963968,.4165052,-.1698368,-.0359784,1.30684,-.430997,.6894727,-.0104578,.5231634,-1.492475,-2.26748,-.0053001,1.391402,-.9849661,-1.398468,-.0142969,-.0176531,-1.495123,.2454891,-.0067267,-.5382892,-3.78975]
alpha_start = alpha_true.*rand(size(alpha_true))
println(size(alpha_true))

#2b

alpha_hat_optim = optimize(a -> mlogit(a, X, y), alpha_start, LBFGS(), Optim.Options(g_tol = 1e-5, iterations=100_000, show_trace=true, show_every=50))
alpha_hat_mle = alpha_hat_optim.minimizer
println(alpha_hat_mle)

#2C

function logit_gmm(alpha, X, y)
    K = size(X,2)
    J = length(unique(y))
    N = length(y)
    bigY = zeros(N,J)
    for j=1:J
        bigY[:,j] = y.==j
    end
    bigAlpha = [reshape(alpha,K,J-1) zeros(K)]

    num = zeros(N,J)
    dem = zeros(N)
    for j=1:J
        num[:,j] = exp.(X*bigAlpha[:,j])
        dem .+= num[:,j]
    end

    P = num./repeat(dem,1,J)

    loglike = -sum( bigY.*log.(P) )

    return loglike
end

alpha_hat_optim = optimize(a -> logit_gmm(a, X, y), alpha_true, LBFGS(), Optim.Options(g_tol = 1e-5, iterations=100_000, show_trace=true, show_every=50))
println(alpha_hat_optim)

alpha_hat_optim = optimize(a -> logit_gmm(a, X, y), alpha_rand, LBFGS(), Optim.Options(g_tol = 1e-5, iterations=100_000, show_trace=true, show_every=50))
println(alpha_hat_optim)

#The objective function looks globally concave.

#3a
XR = rand(1000,2)
#3b
beta = [[(.4),(.1),(.5)] [(.5),(.2),(.4)]]]
#3c
p = XR*beta
#3d
eps = random(GeneralizedExtremeValue(0, 1, 0), 1000, 3)

GenY = zeros(1000,1)
for i in 1:size((XR),1)
    GenY[i,1] = argmax(p[i,:] + eps[i,:])
end

#4

MA = SMM.parallelNormal()
dc = SMM.history(MA.chains[1])
dc = dc[dc[:accepted].==true, :]
println(describe(dc))

#5, I'm sorry but I was very confused on this question. I tried mixing and matching with the code you gave in
#the slides but I'm sure I'm missing something here.
function gmm_smm(θ, X, y, D)
    K = size(X,2)
    N = size(y,1)
    β = θ[1:end-1]
    σ = θ[end]
    if length(β)==1
        β = β[1]
    end
    # N+1 moments in both model and data
    gmodel = zeros(N+1,D)
    # data moments are just the y vector itself
    # and the variance of the y vector
    gdata  = vcat(y,var(y))
    #### !!!!!!!!!!!!!!!!!!!!!!!!!!!!! ####
    # This is critical!                   #
    Random.seed!(1234)                    #
    # You must always use the same ε draw #
    # for every guess of θ!               #
    #### !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ###
    # simulated model moments
    for d=1:D
        ε = σ*randn(N)
        ỹ = X*β .+ ε
        gmodel[1:end-1,d] = ỹ
        gmodel[  end  ,d] = var(ỹ)
    end
    # criterion function
    err = vec(gdata .- mean(gmodel; dims=2))
    # weighting matrix is the identity matrix
    # minimize weighted difference between data and moments
    J = err'*I*err
    return J
end

alpha_hat_smm = optimize(a -> gmm_smm(a, X, y, 1000), alpha_true, LBFGS(), Optim.Options(g_tol = 1e-5, iterations=100_000, show_trace=true, show_every=50))
println(alpha_hat_smm)
end

ps7()


text:
function split_box(box, numsplits)
    return mince(box, numsplits)
end

function min_diam(box)
    return minimum(diam.(box))
end

function interval_arithmetic_sign_search(
    func,
    initialbox,
    tol,
    perturbation,
    numsplits,
)

    rtol = tol * diam(initialbox)
    numiter = 0

    foundpos = foundneg = breachedtol = false
    queue = [initialbox]

    while !isempty(queue)
        if (foundpos && foundneg)
            break
        else
            box = popfirst!(queue)
            if min_diam(box) < rtol
                breachedtol = true
                break
            end
            funcrange = func(box)
            if inf(funcrange) > -perturbation
                foundpos = true
            elseif sup(funcrange) < perturbation
                foundneg = true
            else
                newboxes = split_box(box, numsplits)
                push!(queue, newboxes...)
            end
        end
        numiter += 1
    end

    returnval = 2
    if breachedtol
        returnval = 2
    elseif foundpos && foundneg
        returnval = 0
    elseif foundpos && !foundneg
        returnval = +1
    elseif !foundpos && foundneg
        returnval = -1
    else
        error("Unexpected scenario")
    end

    return returnval, numiter
end


text:
#
# Copyright (c) 2021 Tobias Thummerer, Lars Mikelsons
# Licensed under the MIT license. See LICENSE file in the project root for details.
#

using Documenter, FMIFlux

makedocs(sitename="FMIFlux.jl",
         pages=Any["Home" => "index.md"])

deploydocs(repo = "github.com/ThummeTo/FMIFlux.jl.git", devbranch = "main")


text:
__precompile__(true)

module AffineTransforms

import Base: *, \, eltype, ndims, show
if isdefined(Base, :scale)
    import Base.scale
else
    export scale
end

using Interpolations, Requires, Base.Cartesian

export
    # types
    AffineTransform,
    TransformedArray,
    # functions
    rotation2,
    rotation3,
    rotationmatrix,
    rotationparameters,
    tformeye,
    tformfwd,
    tformfwd!,
    tforminv,
    tforminv!,
    tformfwd3rd,
    tformrigid,
    tformrotate,
    tformscale,
    tformtranslate,
    transform,
    transform!

include("transform.jl")
include("tformedarrays.jl")

end


text:
function hfun_bar(vname)
  val = Meta.parse(vname[1])
  return round(sqrt(val), digits=2)
end

function hfun_m1fill(vname)
  var = vname[1]
  return pagevar("index", var)
end

function lx_baz(com, _)
  # keep this first line
  brace_content = Franklin.content(com.braces[1]) # input string
  # do whatever you want here
  return uppercase(brace_content)
end

function lx_defaultoutput(com, _)
    raw"""
    ```julia:write_helper
    # hideall
    output_dir = @OUTPUT
    function write_svg(name, p)
      fig_path = joinpath(output_dir, "$name.svg")
      StatsPlots.savefig(fig_path)
    end;
    ```
    \output{write_helper}

    ```julia:plot
    using StatsPlots

    write_svg("chns", # hide
    StatsPlots.plot(chns)
    ) # hide
    ```
    \output{plot}
    \fig{chns.svg}
    """
end


text:
const Terminal = Union{Symbol, Real, Function}

"""
Implementation of Koza-type (tree-based) Genetic Programming

The constructor takes following keyword arguments:

- `populationSize`: The size of the population
- `terminals`: A dictionary of terminals with their their corresponding dimensionality
    - This dictionary contains (`Terminal`, `Int`) pairs
    - The terminals can be any symbols (variables), constat values, or 0-arity functions.
- `functions`: A collection of functions with their corresponding arity.
    - This dictionary contains (`Function`, `Int`) pairs
- `initialization`: A strategy for population initialization (default: `:grow`)
    - Possible values: `:grow` and `:full`
- `mindepth`: Minimal depth of the expression (default: `0`)
- `maxdepth`: Maximal depth of the expression (default: `3`)
- `simplify`: An expression simplification function (default: `:nothing`)
- `optimizer`: An evolutionary optimizer used for evolving the expressions (default: [`GA`](@ref))
    - Use `mutation` and `crossover` parameters to specify GP-related mutation operation.
    - Use `selection` parameter to specify the offspring selection procedure
"""
@kwdef struct TreeGP <: AbstractOptimizer
    populationSize::Integer = 50
    terminals::Dict{Terminal, Int} = Dict(:x=>1, rand=>1)
    functions::Dict{Function, Int} = Dict( f=>2 for f in [+,-,*,pdiv] )
    mindepth::Int = 0
    maxdepth::Int = 3
    initialization::Symbol = :grow
    simplify::Union{Nothing, Function} = nothing
    optimizer::AbstractOptimizer = GA()
end
function TreeGP(pop::Integer, term::Vector{Terminal}, func::Vector{Function}; kwargs...)
    terminals = Dict(t=>1 for t in term)
    functions = Dict(f=>2 for f in func)
    TreeGP(;populationSize=pop, terminals=terminals, functions=functions, kwargs...)
end

population_size(method::TreeGP) = method.populationSize
default_options(method::TreeGP) = (iterations=1000, abstol=1e-15)
terminals(m::TreeGP) = Symbol[t for t in keys(m.terminals) if isa(t,Symbol)] |> sort!
function summary(m::TreeGP)
    par = join(terminals(m),",")
    "TreeGP[P=$(m.populationSize),Parameter[$(par)],$(keys(m.functions))]"
end
show(io::IO,m::TreeGP) = print(io, summary(m))

"""
    randterm(t::TreeGP)

Returns a random terminal given the specification from the `TreeGP` object `t`.
"""
function randterm(t::TreeGP)
    term = rand(keys(t.terminals))
    if isa(term, Symbol)
        term
    else
        dim = t.terminals[term]
        dim == 1 ? rand() : rand(dim)
    end
end

"""
    rand(t::TreeGP, maxdepth=2; mindepth=maxdepth-1)::Expr

Create a ranodm expression tree given the specification from the `TreeGP` object `t`.
"""
function rand(t::TreeGP, maxdepth::Int=2; mindepth::Int=maxdepth-1)
    @assert maxdepth > mindepth "`maxdepth` must be larger then `mindepth`"
    tl = length(t.terminals)
    fl = length(t.functions)
    root = if (maxdepth == 0  || ( t.initialization == :grow && rand() < tl/(tl+fl) ) ) && mindepth <= 0
        randterm(t)
    else
        rand(keys(t.functions))
    end
    if isa(root, Function)
        args = Any[]
        for i in 1:t.functions[root]
            arg = rand(t, maxdepth-1, mindepth=mindepth-1)
            push!(args, arg)
        end
        Expr(:call, root, args...)
    else
        return root
    end
end

"""
    initial_population(m::TreeGP, expr::{Expr,Nothing}=nothing)

Initialize a random population of expressions derived from `expr`.
"""
function initial_population(m::TreeGP, expr::Union{Expr,Nothing}=nothing)
    n = population_size(m)
    return [
        expr === nothing ? rand(m, m.maxdepth, mindepth=m.mindepth) : deepcopy(expr)
        for i in 1:n
    ]
end

mutable struct GPState{T,IT} <: Evolutionary.AbstractOptimizerState
    ga::GAState{T,IT}
end
value(s::GPState) = s.ga.fitness
minimizer(s::GPState) = s.ga.fittest

"""Initialization of GP algorithm state"""
function initial_state(method::TreeGP, options, objfun, population)
    return GPState(initial_state(method.optimizer, options, objfun, population))
end

function update_state!(objfun, constraints, state::GPState, population::AbstractVector{IT}, method, options, itr) where {IT}
    # perform GA step
    res = update_state!(objfun, constraints, state.ga, population, method.optimizer, options, itr)
    # simplify expressions
    if method.simplify !== nothing
        for i in 1:length(population)
            method.simplify(population[i])
        end
    end
    return res
end

# Custom optimization call
function optimize(f, method::TreeGP, options::Options = Options(;default_options(method)...))
    method.optimizer.mutation =  subtree(method)
    method.optimizer.crossover = crosstree
    optimize(f, NoConstraints(), nothing, method, options)
end


text:
"""
$(SIGNATURES)

Returns a partial LMM model (non-functional due to lacking data) to be used in likelihoodratiotests.
`k` to selcet which of the modelfit's to fake
"""
function fake_lmm(m::UnfoldLinearMixedModel,k::Int)
    (feterm,reterm) = Unfold.designmatrix(m).Xs
    fakeY = zeros(size(feterm,1))
    lmm = Unfold.LinearMixedModel_wrapper(Unfold.formula(m),fakeY,designmatrix(m).Xs)

    fcoll = Unfold.modelfit(m)
    #lmm.objective .= fcoll.fits[1].objective
    #lmm.optsum.feval .= 1
    #lmm.optsum.fmin .= 1
    lmm.optsum.sigma = fcoll.fits[k].σ
    lmm.optsum.optimizer = :unfold
    lmm.optsum.returnvalue = :success
    setθ!(lmm,fcoll.fits[k].θ)
    return lmm
end
    fake_lmm(m::UnfoldLinearMixedModel) = fake_lmm.(m,1:length(modelfit(m).fits))

    """
    $(SIGNATURES)

    Calculate likelihoodratiotest
    """
	function MixedModels.likelihoodratiotest(m::UnfoldLinearMixedModel...)
		#@info lrtest(fake_lmm.(m,1)...)
		n = length(Unfold.modelfit(m[1]).fits)
		lrt = Array{MixedModels.LikelihoodRatioTest}(undef,n)
		sizehint!(lrt,n)
		for k = 1:n
			ms = fake_lmm.(m,k)
			@info objective.(ms)
			lrt[k] = MixedModels.likelihoodratiotest(ms...)
		end
		return lrt
	end

    """
    $(SIGNATURES)
    Unfold-Method: return pvalues of likelihoodratiotests, typically calculated:

    # Examples
    julia> pvalues(likelihoodratiotest(m1,m2))

    where m1/m2 are UnfoldLinearMixedModel's

    Tipp: if you only compare two models you can easily get a vector of p-values:

    julia> vcat(pvalues(likelihoodratiotest(m1,m2))...)

    Multiple channels are returned linearized at the moment, as we do not have access to the amount of channels after the LRT, you can do:

    julia> reshape(vcat(pvalues(likelihoodratiotest(m1,m2))...),ntimes,nchan)'

    """
	function pvalues(lrtvec::Vector{MixedModels.LikelihoodRatioTest})
		[lrt.pvalues for lrt in lrtvec]
	end

max_stars_repo_name: unfoldtoolbox/jlunfold

max_issues_repo_name: unfoldtoolbox/jlunfold

max_forks_repo_name: unfoldtoolbox/jlunfold


text:
using MLJBase
using PersistenceDiagrams
using Suppressor
using Tables
using Test

diagrams = (
    dim_0=[
        PersistenceDiagram([(0.0, Inf), (0.0, 1.0)]),
        PersistenceDiagram([(0.0, Inf), (0.0, 1.5)]),
    ],
    dim_1=[
        PersistenceDiagram([(0.0, 1.0), (1.2, 2.0)]),
        PersistenceDiagram([(1.0, 1.1), (1.3, 1.9)]),
    ],
)
table = MLJBase.table(diagrams)

n_rows(x) = length(Tables.rows(x))
n_cols(x) = length(Tables.columnnames(x))

"""
    test_clean(model_type, kwargs; expect_warning=true)

Tests that for given model and kwargs:

* cleaning shows warning if `expect_warning=true`.
* cleaning the second time does not change anything and does not give warnings.
"""
function test_clean(model_type, kwargs; expect_warning=true)
    @testset "`clean!` with $kwargs" begin
        # Does clean warn?
        local model
        warning = @capture_err begin
            model = model_type(; kwargs...)
        end
        if expect_warning
            @test warning ≠ ""
        else
            @test warning == ""
        end

        # Cleaning again should not warn and should not change anything.
        prev_model = deepcopy(model)
        warning = clean!(model)
        @test warning == ""
        for field in fieldnames(model_type)
            @test getfield(model, field) == getfield(prev_model, field)
        end
    end
end

@testset "PersistenceImageVectorizer" begin
    for kwargs in (NamedTuple(), (sigma=0.1, slope_end=0.5, height=7), (sigma=0.1, width=6))
        model = PersistenceImageVectorizer(; kwargs...)
        mach = machine(model, table)
        fit!(mach; verbosity=0)
        res = transform(mach, table)

        @test n_rows(res) == 2
        @test n_cols(res) == model.width * model.height + model.height
    end

    for faulty_kwargs in (
        (; width=0),
        (; height=0),
        (; distribution="a"),
        (; weight=1),
        (; sigma=0),
        (; slope_end=2),
        (; margin=-1),
        (; sigma=1, distribution=(+)),
        (; weight=(_, _) -> 0, slope_end=0.5),
        (; distribution=(_, _) -> 0, sigma=1),
    )
        test_clean(PersistenceImageVectorizer, faulty_kwargs; expect_warning=true)
    end
    for good_kwargs in (
        NamedTuple(),
        (; width=5, height=10),
        (; sigma=1, slope_end=0.2),
        (; distribution=(_, _) -> 0, slope_end=0.001),
        (; weight=(_, _) -> 0, sigma=0.9, width=13),
        (; zero_start=true),
        (; margin=0),
    )
        test_clean(PersistenceImageVectorizer, good_kwargs; expect_warning=false)
    end
end

@testset "PersistenceCurveVectorizer" begin
    for kwargs in (
        NamedTuple(),
        (length=15, normalize=true),
        (integrate=false, curve=:midlife),
        (length=3, curve=:midlife_entropy),
    )
        model = PersistenceCurveVectorizer(; kwargs...)
        mach = machine(model, table)
        fit!(mach; verbosity=0)
        res = transform(mach, table)

        @test n_rows(res) == 2
        @test n_cols(res) == model.length * 2
    end
    birth_fun(i, _, _) = birth(i)
    for faulty_kwargs in (
        (; curve=:betti, fun=birth_fun),
        (; curve=:life, stat=mean),
        (; curve=:midlife, fun=birth_fun, stat=mean),
        (; curve=:life_entropy, fun=birth_fun),
        (; curve=:midlife_entropy, stat=mean),
        (; curve=:pd_thresholding, normalize=true),
        (; curve=:silhuette, normalize=true),
        (; curve=:something),
        (; length=0),
        (; length=-1),
    )
        test_clean(PersistenceCurveVectorizer, faulty_kwargs; expect_warning=true)
    end
    for good_kwargs in (
        NamedTuple(),
        (; curve=:custom),
        (; curve=:betti, length=100),
        (; curve=:life),
        (; curve=:midlife, normalize=false),
        (; curve=:life_entropy, normalize=true),
        (; curve=:midlife_entropy, integrate=false),
        (; curve=:pd_thresholding, integrate=true),
        (; curve=:silhuette, length=10),
        (; fun=birth_fun),
        (; stat=mean),
        (; fun=birth_fun, stat=mean, curve=:custom),
    )
        test_clean(PersistenceCurveVectorizer, good_kwargs; expect_warning=false)
    end
end

@testset "PersistenceLandscapeVectorizer" begin
    for kwargs in (NamedTuple(), (length=15, n_landscapes=5), (; n_landscapes=3))
        model = PersistenceLandscapeVectorizer(; kwargs...)
        mach = machine(model, table)
        fit!(mach; verbosity=0)
        res = transform(mach, table)

        @test n_rows(res) == 2
        @test n_cols(res) == model.length * model.n_landscapes * 2
    end
    for faulty_kwargs in ((; length=-1), (; n_landscapes=0), (; length=0, n_landscapes=-1))
        test_clean(PersistenceLandscapeVectorizer, faulty_kwargs; expect_warning=true)
    end
    for good_kwargs in
        (NamedTuple(), (; length=100), (; n_landscapes=4), (; n_landscapes=3, length=15))
        test_clean(PersistenceLandscapeVectorizer, good_kwargs; expect_warning=false)
    end
end


text:
# ---
# title: 833. Find And Replace in String
# id: problem833
# author: Tian Jun
# date: 2020-10-31
# difficulty: Medium
# categories: String
# link: <https://leetcode.com/problems/find-and-replace-in-string/description/>
# hidden: true
# ---
#
# To some string `S`, we will perform some replacement operations that replace
# groups of letters with new ones (not necessarily the same size).
#
# Each replacement operation has `3` parameters: a starting index `i`, a source
# word `x` and a target word `y`.  The rule is that if `x` starts at position
# `i` in the **original** **string** **`S`** , then we will replace that
# occurrence of `x` with `y`.  If not, we do nothing.
#
# For example, if we have `S = "abcd"` and we have some replacement operation `i
# = 2, x = "cd", y = "ffff"`, then because `"cd"` starts at position `2` in the
# original string `S`, we will replace it with `"ffff"`.
#
# Using another example on `S = "abcd"`, if we have both the replacement
# operation `i = 0, x = "ab", y = "eee"`, as well as another replacement
# operation `i = 2, x = "ec", y = "ffff"`, this second operation does nothing
# because in the original string `S[2] = 'c'`, which doesn't match `x[0] = 'e'`.
#
# All these operations occur simultaneously.  It's guaranteed that there won't
# be any overlap in replacement: for example, `S = "abc", indexes = [0, 1],
# sources = ["ab","bc"]` is not a valid test case.
#
#
#
# **Example 1:**
#
#
#
#     Input: S = "abcd", indexes = [0, 2], sources = ["a", "cd"], targets = ["eee", "ffff"]
#     Output: "eeebffff"
#     Explanation:
#     "a" starts at index 0 in S, so it's replaced by "eee".
#     "cd" starts at index 2 in S, so it's replaced by "ffff".
#
#
# **Example 2:**
#
#
#
#     Input: S = "abcd", indexes = [0, 2], sources = ["ab","ec"], targets = ["eee","ffff"]
#     Output: "eeecd"
#     Explanation:
#     "ab" starts at index 0 in S, so it's replaced by "eee".
#     "ec" doesn't starts at index 2 in the **original** S, so we do nothing.
#
#
#
#
# **Constraints:**
#
#   * `0 <= S.length <= 1000`
#   * `S` consists of only lowercase English letters.
#   * `0 <= indexes.length <= 100`
#   * `0 <= indexes[i] < S.length`
#   * `sources.length == indexes.length`
#   * `targets.length == indexes.length`
#   * `1 <= sources[i].length, targets[i].length <= 50`
#   * `sources[i]` and `targets[i]` consist of only lowercase English letters.
#
#
## @lc code=start
using LeetCode

## add your code here:
## @lc code=end


text:
using PyPlot
using NCDatasets
using Formatting
plt = PyPlot

schemes = ["AGA2020", "KSC2018", "CO2012"]

d = Dict()

for scheme in schemes
    global d, ϵ

    ds = Dataset("epsilon_logpost_$(scheme).nc", "r")

    ϵ    = nomissing(ds["epsilon"][:], NaN)
    post = nomissing(ds["post"][:], NaN)

    d[scheme] = post
end

ϵ_vec = collect(Float64, range(0.0, 2.0, length=11)) # day^-1
#day_vec = collect(Float64, range(0.6, 2.0, length=7))
#day_on_ϵ_vec = 1 ./ (day_vec * 86400.0)

fig, ax = plt.subplots(1, 1, figsize=(6,4))

for scheme in schemes
    ax.plot(ϵ*86400.0, d[scheme], label=scheme)
end

ax.legend()

ax.set_xticks(ϵ_vec)
#ax.set_xticks(day_on_ϵ_vec)
#ax.set_xticklabels([ format("{:.2f}", day) for day in day_vec])

#ax.invert_xaxis()

plt.show()

max_stars_repo_name: meteorologytoday/IOM

max_issues_repo_name: meteorologytoday/IOM

max_forks_repo_name: meteorologytoday/IOM


text:
#=

  https://dtai.cs.kuleuven.be/problog/tutorial/basic/05_smokers.html
  """
  Social networks (Friends & Smokers)

  The ProbLog program below encodes a variant of the famous Friends & Smokers problem. The
  first two rules state that there are two possible causes for a person X to smoke,
  namely X having stress, and X having a friend Y who smokes himself and influences X.
  Note that, according to our this program, the presence of stress and of (possibly multiple)
  smoking friends all contribute to the probability of the person X smoking, namely in a
  noisy-or way (recall the noisy-or in the coin tossing example). Furthermore, the program
  encodes that if X smokes, (s)he has asthma with probability 0.4.

  The program below considers a scenario with 4 people, having a total of 5 friendship relations.
  """

  Cf ~/blog/smokers.blog
     ~/webppl/smokers.wppl

=#

using Turing, StatsPlots, DataFrames
include("jl_utils.jl")

@model function smokers(mode,pp1=1,pp2=1)
    people = [1,2,3,4]
    n = length(people)

    stress = tzeros(n)
    for p in 1:n
        stress[p] ~ flip(0.3)
    end

    influences = tzeros(n,n)
    for p1 in 1:n, p2 in 1:n
        if p1 != p2
            influences[p1,p2] ~ flip(0.2)
        else
            influences[p1,p2] ~ flip(0.0)
        end
    end

    friend_table = Dict(
        [1,2]=>1,
        [2,1]=>1,
        [2,4]=>1,
        [3,2]=>1,
        [4,2]=>1
    )
    friend = tzeros(n,n)
    for p1 in 1:n, p2 in 1:n
        friend[p1,p2] ~ get(friend_table,[[p1,p2]],0) == 1 ? flip(1.0) : flip(0.0)
    end

    smokes = tzeros(n)
    for p1 in 1:n
        smokes[p1] ~ flip(0.5)
    end

    for p1 in 1:n
        if stress[p1] == true
            smokes[p1] ~ flip(1.0)
        else
            if any(p2 -> p2 != p1 && friend[p1,p2]==1 && influences[p2,p1]==1 && smokes[p2]==1, people)
                smokes[p1] ~ flip(1.0)
            else
                smokes[p1] ~ flip(0.0)
            end
        end
    end

    asthma = tzeros(n)
    for p1 in 1:n
        if smokes[p1]==1
            asthma[p1] ~ flip(0.4)
        else
            asthma[p1] ~ flip(0.0)
        end
    end

    # Observations
    true ~ Dirac(smokes[2]==true)
    true ~ Dirac(influences[4,2]==true)

    if mode == "smokes"
        prob ~ Dirac(smokes[pp1])
        return prob
    elseif mode == "asthma"
        prob ~ Dirac(asthma[pp1])
        return prob
    elseif mode == "stress"
        prob ~ Dirac(stress[pp1])
        return prob
    elseif mode == "influences"
        prob ~ Dirac(influences[pp1,pp2])
        return prob
    else
        true
    end

end

function run_smokers(mode,pp1,pp2)
    model = smokers(mode,pp1,pp2)
    num_chns = 4

    # chns = sample(model, Prior(), 10_000)

    # chns = sample(model, MH(), 10_000)
    # chns = sample(model, PG(15), 1_000)
    # chns = sample(model, SMC(1000), 10_000)
    chns = sample(model, SMC(1000), 1_000)

    # chns = sample(model, IS(), 10_000)

    #
    # display(chns)
    # show_var_dist_pct(chns,:len,1000)

    # println("prob return value t[$a,$b]:")
    # genq = generated_quantities(model, chns)
    # show_var_dist_pct(genq,1000)
    chns_params = Turing.MCMCChains.get_sections(chns, :parameters)
    genq = generated_quantities(model, chns_params)

    show_var_dist_pct(chns,:prob)

    println("mean $(mean(genq))")
    return mean(genq)
end

println("SMOKES: ")
for p in 1:4
    println("\nsmokes($p)")
    run_smokers("smokes",p,p)
end

println("\nSTRESS: ")
for p in 1:4
    println("\nstress($p)")
    run_smokers("stress",p,p)
end

println("\nASTHMA: ")
for p in 1:4
    println("\nasthma($p)")
    run_smokers("asthma",p,p)
end

println("\nINFLUENCES: ")
influences = tzeros(4,4)
for p1 in 1:4, p2 in 1:4
    println("\ninfluences($p1,$p2)")
    influences[p1,p2] = run_smokers("influences",p1,p2)
end

display(influences)

max_stars_repo_name: tias/hakank

max_issues_repo_name: tias/hakank

max_forks_repo_name: tias/hakank


text:
fibs = [0, 1]

while fibs[length(fibs)] + fibs[length(fibs) - 1] < 4000000
    append!(fibs, [fibs[length(fibs)] + fibs[length(fibs) - 1]])
end

println(sum(filter(x -> mod(x, 2) == 0, fibs)))

max_stars_repo_name: seaneshbaugh/rosetta-euler

max_issues_repo_name: seaneshbaugh/rosetta-euler

max_forks_repo_name: seaneshbaugh/rosetta-euler


text:
module NeuralNetDiffEq

using Reexport, Statistics
@reexport using DiffEqBase
using Flux

abstract type NeuralNetDiffEqAlgorithm <: DiffEqBase.AbstractODEAlgorithm end

struct TerminalPDEProblem{G,F,Mu,Sigma,X,T,P} <: DiffEqBase.DEProblem
    g::G
    f::F
    μ::Mu
    σ::Sigma
    X0::X
    tspan::Tuple{T,T}
    p::P
    TerminalPDEProblem(g,f,μ,σ,X0,tspan,p=nothing) = new{typeof(g),typeof(f),
                                                         typeof(μ),typeof(σ),
                                                         typeof(X0),eltype(tspan),
                                                         typeof(p)}(
                                                         g,f,μ,σ,X0,tspan,p)
end

Base.summary(prob::TerminalPDEProblem) = string(nameof(typeof(prob)))

function Base.show(io::IO, A::TerminalPDEProblem)
  println(io,summary(A))
  print(io,"timespan: ")
  show(io,A.tspan)
end

include("ode_solve.jl")
include("pde_solve.jl")
include("pde_solve_ns.jl")
include("rode_solve.jl")

export NNODE, TerminalPDEProblem, NNPDEHan, NNPDENS, NNRODE

end # module


text:
module problem107

description = """
Minimal network
Problem 107

The following undirected network consists of seven vertices and twelve edges with a total weight of 243.

The same network can be represented by the matrix below.
    	A	B	C	D	E	F	G
A	-	16	12	21	-	-	-
B	16	-	-	17	20	-	-
C	12	-	-	28	-	31	-
D	21	17	28	-	18	19	23
E	-	20	-	18	-	-	11
F	-	-	31	19	-	-	27
G	-	-	-	23	11	27	-

However, it is possible to optimise the network by removing some edges and still ensure that all points on the network remain connected. The network which achieves the maximum saving is shown below. It has a weight of 93, representing a saving of 243 − 93 = 150 from the original network.

Using network.txt (right click and 'Save Link/Target As...'), a 6K text file containing a network with forty vertices, and given in matrix form, find the maximum saving which can be achieved by removing redundant edges whilst ensuring that the network remains connected.
"""

immutable Edge
  from :: Int
  to :: Int
  cost :: Int
end

immutable Tree
  edges :: Vector{Edge}
end

immutable Forest
  trees :: Vector{Tree}
end

# Find the index of the tree containing node in the forest, or -1 if not found
function tree_index(node :: Int, f :: Forest)
  for i = 1:length(f.trees)
    for j = 1:length(f.trees[i].edges)
      if (f.trees[i].edges[j].from == node || f.trees[i].edges[j].to == node)
        return i
      end
    end
  end
  return -1
end

# Tests if adding Edge e to Forest would connect two previously disconnected trees
function makes_new_connection(e :: Edge, f :: Forest)
  t1 = tree_index(e.from, f)
  t2 = tree_index(e.to, f)
  return t1 < 0 || t2 < 0 || t1 != t2
end

# Creates Add Edge to Forest, joining any trees that become linked
function add_edge(e :: Edge, f :: Forest)
  t1 = tree_index(e.from, f)
  t2 = tree_index(e.to, f)

  # Simple case, neither from/to existed previously, add a new tree
  if t1 < 0 && t2 < 0
    return Forest([Tree([e]); f.trees])
  end

  # One of the trees exists - make a copy and update
  newtrees = copy(f.trees)
  if t1 > 0 && t2 < 0
    newtrees[t1] = Tree([e; f.trees[t1].edges])
  elseif t1 < 0 && t2 > 0
    newtrees[t2] = Tree([e; f.trees[t2].edges])
  else
    newtrees[t1] = Tree([e; f.trees[t1].edges; f.trees[t2].edges])
    splice!(newtrees, t2, Tree[])
  end
  return Forest(newtrees)
end

# Convert from matrix representation to Edge vector
function matrix_to_edges(connections :: Matrix{Int})
  edges = Edge[]
  for i = 1:size(connections, 1)
    for j = i+1:size(connections, 2)
      if connections[i, j] > 0
        push!(edges, Edge(i, j, connections[i,j]))
      end
    end
  end
  return edges
end

# Calculate the minimum spanning tree by adding the minimum cost edge to a forest if it links up previously disconnected trees
function minimum_spanning_tree(connections :: Matrix{Int})
  edges = matrix_to_edges(connections)
  sort!(edges, by = x -> x.cost)
  f = Forest(Tree[])
  for e in edges
    if makes_new_connection(e, f)
      f = add_edge(e, f)
    end
  end
  return f.trees[1]
end

function read_network()
  m = readcsv("network.txt")
  m[m .== "-"] = -1
  connections = int(m)
  return connections
end

function solve107()
  network = read_network()
  t = minimum_spanning_tree(network)
  initial_cost = sum([e.cost for e in matrix_to_edges(network)])
  final_cost = sum([e.cost for e in t.edges])
  println("$initial_cost - $final_cost = $(initial_cost - final_cost)")
end

using Base.Test

testcase = [
-1	16	12	21	-1	-1	-1
16	-1	-1	17	20	-1	-1
12	-1	-1	28	-1	31	-1
21	17	28	-1	18	19	23
-1	20	-1	18	-1	-1	11
-1	-1	31	19	-1	-1	27
-1	-1	-1	23	11	27	-1]

@test sum([e.cost for e in minimum_spanning_tree(testcase).edges]) == 93

end

max_stars_repo_name: mbuhot/mbuhot-euler-solutions

max_issues_repo_name: mbuhot/mbuhot-euler-solutions

max_forks_repo_name: mbuhot/mbuhot-euler-solutions


text:
"""

Handle model specific keywords in kwargs argument in stan_run(model; kwargs...).

$(SIGNATURES)

"""
function handle_keywords!(m::T, kwrds) where { T <: CmdStanModels}

    model_keywords = fieldnames(typeof(m))
    excluded_model_keywords = [
        :name, :model, :data, :init, :output_base, :tmpdir, :exec_path,
        :data_file, :init_file, :cmds, :sample_file, :log_file,
        :diagnostic_file, :cmdstan_home
    ]
    accepted_keywords = setdiff(model_keywords, excluded_model_keywords)
    #println(accepted_keywords)

    for kwrd in keys(kwrds)
        if !(kwrd in [:data, :init])
            if kwrd in accepted_keywords
                setfield!(m, kwrd, kwrds[kwrd])
            else
                println("\nAllowed keywords: $(accepted_keywords)")
                if kwrd in excluded_model_keywords
                    println("\nKeyword \"$(Symbol(kwrd))\" not allowed. Ignored.")
                else
                    println("\nKeyword \"$(Symbol(kwrd))\" not in model. Ignored.")
                end
            end
        end
    end
end


text:
# ---
# title: 911. Online Election
# id: problem911
# author: Tian Jun
# date: 2020-10-31
# difficulty: Medium
# categories: Binary Search
# link: <https://leetcode.com/problems/online-election/description/>
# hidden: true
# ---
#
# In an election, the `i`-th vote was cast for `persons[i]` at time `times[i]`.
#
# Now, we would like to implement the following query function:
# `TopVotedCandidate.q(int t)` will return the number of the person that was
# leading the election at time `t`.
#
# Votes cast at time `t` will count towards our query.  In the case of a tie,
# the most recent vote (among tied candidates) wins.
#
#
#
# **Example 1:**
#
#
#
#     Input: ["TopVotedCandidate","q","q","q","q","q","q"], [[[0,1,1,0,0,1,0],[0,5,10,15,20,25,30]],[3],[12],[25],[15],[24],[8]]
#     Output: [null,0,1,1,0,0,1]
#     Explanation:
#     At time 3, the votes are [0], and 0 is leading.
#     At time 12, the votes are [0,1,1], and 1 is leading.
#     At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
#     This continues for 3 more queries at time 15, 24, and 8.
#
#
#
#
# **Note:**
#
#   1. `1 <= persons.length = times.length <= 5000`
#   2. `0 <= persons[i] <= persons.length`
#   3. `times` is a strictly increasing array with all elements in `[0, 10^9]`.
#   4. `TopVotedCandidate.q` is called at most `10000` times per test case.
#   5. `TopVotedCandidate.q(int t)` is always called with `t >= times[0]`.
#
#
## @lc code=start
using LeetCode

## add your code here:
## @lc code=end


text:
struct DofMap
    cell_dofs::Array{Int64, 2}
    dim::Int64
    mesh::Mesh
    element
end

function build_dofmap(element, mesh::Mesh)
    fiat_element = ffc.fiatinterface.create_element(element)
    tdim = mesh.reference_cell.get_dimension()::Int64

    cell_dofs = zeros(Int64, num_entities(mesh, tdim), fiat_element.space_dimension())

    # Explicit conversion
    fiat_entity_dofs = convert(Dict{Int64, Dict{Int64, Array{Int64, 1}}}, fiat_element.entity_dofs())

    offset = 0
    for (dim, local_dofs) in fiat_entity_dofs
        # For each set of entities (and its local dofmaps) of dimension dim
        dofs_per_entity = length(local_dofs[0])

        # Take a handle to connectivity array
        if tdim == dim
            connectivity = 1:num_entities(mesh, tdim)
        else
            connectivity = mesh.topology[(tdim, dim)]
        end

        for k in 1:dofs_per_entity
            for (entity, entity_dofs) in local_dofs
                cell_dofs[:, entity_dofs[k] + 1] = dofs_per_entity * (connectivity[:, entity + 1] .- 1) .+ (offset + k)
            end
        end
        offset += dofs_per_entity * num_entities(mesh, dim)
    end
    return DofMap(cell_dofs, maximum(cell_dofs), mesh, element)
end


text:
theta = 0.9
mu    = 0.0

data = Matrix{Float64}(1,100)

old_eps = 0.0
for i = 1:length(data)
    r = randn()
    data[i] = old_eps + theta * r
    old_eps = r
end

writecsv("data_ma1.csv", data)


text: include("MinimizerTable.jl")