from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['preview'],
                    'supported_by': 'community'}

DOCUMENTATION = '''
---
module: azure_rm_cdnendpoint
version_added: "2.8"
short_description: Manage Azure Endpoint instance.
description:
    - Create, update and delete instance of Azure Endpoint.

options:
    resource_group:
        description:
            - Name of the Resource group within the Azure subscription.
        required: True
    profile_name:
        description:
            - Name of the CDN profile which is unique within the resource group.
        required: True
    name:
        description:
            - Name of the endpoint under the profile which is unique globally.
        required: True
    location:
        description:
            - Resource location.
            - Required when C(state) is I(present).
    origin_host_header:
        description:
            - "The host header value sent to the origin with each request. If you leave this blank, the request hostname determines this value. Azure CDN
               I(origins), such as Web Apps, Blob Storage, and Cloud Services require this host header value to match the origin hostname by default."
    origin_path:
        description:
            - A directory path on the origin that CDN can use to retreive content from, e.g. contoso.cloudapp.net/originpath.
    content_types_to_compress:
        description:
            - List of content types on which compression applies. The value should be a valid MIME type.
        type: list
    is_compression_enabled:
        description:
            - "Indicates whether content compression is enabled on CDN. Default value is false. If compression is enabled, content will be served as
               compressed if user requests for a compressed version. Content won't be compressed on CDN when requested content is smaller than 1 byte or
               larger than 1 MB."
    is_http_allowed:
        description:
            - Indicates whether HTTP traffic is allowed on the endpoint. Default value is true. At least one protocol (HTTP or HTTPS) must be allowed.
    is_https_allowed:
        description:
            - Indicates whether HTTPS traffic is allowed on the endpoint. Default value is true. At least one protocol (HTTP or HTTPS) must be allowed.
    query_string_caching_behavior:
        description:
            - "Defines how CDN caches requests that include query strings. You can ignore any query strings when caching, bypass caching to prevent requests
               that contain query strings from being cached, or cache every request with a unique URL."
        choices:
            - 'ignore_query_string'
            - 'bypass_caching'
            - 'use_query_string'
            - 'not_set'
    optimization_type:
        description:
            - "Specifies what scenario the customer wants this CDN endpoint to optimize for, e.g. Download, Media services. With this information, CDN can
               apply scenario driven optimization."
        choices:
            - 'general_web_delivery'
            - 'general_media_streaming'
            - 'video_on_demand_media_streaming'
            - 'large_file_download'
            - 'dynamic_site_acceleration'
    probe_path:
        description:
            - "Path to a file hosted on the origin which helps accelerate delivery of the dynamic content and calculate the most optimal routes for the CDN.
               This is relative to the origin path."
    geo_filters:
        description:
            - "List of rules defining the user's geo access within a CDN endpoint. Each geo filter defines an acess rule to a specified path or content,
               e.g. block APAC for path /pictures/"
        type: list
        suboptions:
            relative_path:
                description:
                    - "Relative path applicable to geo filter. (e.g. '/mypictures', '/mypicture/kitty.jpg', and etc.)"
                    - Required when C(state) is I(present).
            action:
                description:
                    - Action of the geo filter, i.e. C(allow) or C(block) access.
                    - Required when C(state) is I(present).
                choices:
                    - 'block'
                    - 'allow'
            country_codes:
                description:
                    - Two letter country codes defining user country access in a geo filter, e.g. AU, MX, US.
                    - Required when C(state) is I(present).
                type: list
    delivery_policy:
        description:
            - A policy that specifies the delivery rules to be used for an endpoint.
        suboptions:
            description:
                description:
                    - User-friendly description of the policy.
            rules:
                description:
                    - A list of the delivery rules.
                type: list
                suboptions:
                    order:
                        description:
                            - "The order in which the rules are applied for the endpoint. Possible values {0,1,2,3,………}. A rule with a lesser order will be
                               applied before a rule with a greater order. Rule with order 0 is a special rule. It does not require any condition and
                               I(actions) listed in it will always be applied."
                            - Required when C(state) is I(present).
                    actions:
                        description:
                            - A list of actions that are executed when all the I(conditions) of a rule are satisfied.
                            - Required when C(state) is I(present).
                        type: list
                        suboptions:
                            name:
                                description:
                                    - Constant filled by server.
                                    - Required when C(state) is I(present).
                    conditions:
                        description:
                            - A list of conditions that must be matched for the I(actions) to be executed
                        type: list
                        suboptions:
                            name:
                                description:
                                    - Constant filled by server.
                                    - Required when C(state) is I(present).
    origins:
        description:
            - The source of the content being delivered via CDN.
            - Required when C(state) is I(present).
        type: list
        suboptions:
            name:
                description:
                    - Origin name
                    - Required when C(state) is I(present).
            host_name:
                description:
                    - The address of the origin. It can be a domain name, IPv4 address, or IPv6 address.
                    - Required when C(state) is I(present).
            http_port:
                description:
                    - The value of the HTTP port. Must be between 1 and 65535
            https_port:
                description:
                    - The value of the HTTPS port. Must be between 1 and 65535
    state:
      description:
        - Assert the state of the Endpoint.
        - Use 'present' to create or update an Endpoint and 'absent' to delete it.
      default: present
      choices:
        - absent
        - present

extends_documentation_fragment:
    - azure
    - azure_tags

author:
    - "Zim Kalinowski (@zikalino)"

'''

EXAMPLES = '''
  - name: Create (or update) Endpoint
    azure_rm_cdnendpoint:
      resource_group: RG
      profile_name: profile1
      name: endpoint1
      location: WestCentralUs
      origins:
        - name: www-bing-com
          host_name: www.bing.com
          http_port: 80
          https_port: 443
'''

RETURN = '''
id:
    description:
        - Resource ID.
    returned: always
    type: str
    sample: /subscriptions/subid/resourcegroups/RG/providers/Microsoft.Cdn/profiles/profile1/endpoints/endpoint1
'''

import time
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
from ansible.module_utils.common.dict_transformations import _snake_to_camel

try:
    from msrestazure.azure_exceptions import CloudError
    from msrest.polling import LROPoller
    from msrestazure.azure_operation import AzureOperationPoller
    from azure.mgmt.cdn import CdnManagementClient
    from msrest.serialization import Model
except ImportError:
    # This is handled in azure_rm_common
    pass

class Actions:
    NoAction, Create, Update, Delete = range(4)

class AzureRMEndpoint(AzureRMModuleBase):
    """Configuration class for an Azure RM Endpoint resource"""

    def __init__(self):
        self.module_arg_spec = dict(
            resource_group=dict(
                type='str',
                required=True
            ),
            profile_name=dict(
                type='str',
                required=True
            ),
            name=dict(
                type='str',
                required=True
            ),
            location=dict(
                type='str'
            ),
            origin_host_header=dict(
                type='str'
            ),
            origin_path=dict(
                type='str'
            ),
            content_types_to_compress=dict(
                type='list'
            ),
            is_compression_enabled=dict(
                type='str'
            ),
            is_http_allowed=dict(
                type='str'
            ),
            is_https_allowed=dict(
                type='str'
            ),
            query_string_caching_behavior=dict(
                type='str',
                choices=['ignore_query_string',
                         'bypass_caching',
                         'use_query_string',
                         'not_set']
            ),
            optimization_type=dict(
                type='str',
                choices=['general_web_delivery',
                         'general_media_streaming',
                         'video_on_demand_media_streaming',
                         'large_file_download',
                         'dynamic_site_acceleration']
            ),
            probe_path=dict(
                type='str'
            ),
            geo_filters=dict(
                type='list',
                options=dict(
                    relative_path=dict(
                        type='str'
                    ),
                    action=dict(
                        type='str',
                        choices=['block',
                                 'allow']
                    ),
                    country_codes=dict(
                        type='list'
                    )
                )
            ),
            delivery_policy=dict(
                type='dict',
                options=dict(
                    description=dict(
                        type='str'
                    ),
                    rules=dict(
                        type='list',
                        options=dict(
                            order=dict(
                                type='int'
                            ),
                            actions=dict(
                                type='list',
                                options=dict(
                                    name=dict(
                                        type='str'
                                    )
                                )
                            ),
                            conditions=dict(
                                type='list',
                                options=dict(
                                    name=dict(
                                        type='str'
                                    )
                                )
                            )
                        )
                    )
                )
            ),
            origins=dict(
                type='list',
                options=dict(
                    name=dict(
                        type='str'
                    ),
                    host_name=dict(
                        type='str'
                    ),
                    http_port=dict(
                        type='int'
                    ),
                    https_port=dict(
                        type='int'
                    )
                )
            ),
            state=dict(
                type='str',
                default='present',
                choices=['present', 'absent']
            )
        )

        self.resource_group = None
        self.profile_name = None
        self.name = None
        self.endpoint = dict()

        self.results = dict(changed=False)
        self.mgmt_client = None
        self.state = None
        self.to_do = Actions.NoAction

        super(AzureRMEndpoint, self).__init__(derived_arg_spec=self.module_arg_spec,
                                              supports_check_mode=True,
                                              supports_tags=True)

    def exec_module(self, **kwargs):
        """Main module execution method"""

        for key in list(self.module_arg_spec.keys()) + ['tags']:
            if hasattr(self, key):
                setattr(self, key, kwargs[key])
            elif kwargs[key] is not None:
                self.endpoint[key] = kwargs[key]

        dict_camelize(self.endpoint, ['query_string_caching_behavior'], True)
        dict_camelize(self.endpoint, ['optimization_type'], True)
        dict_camelize(self.endpoint, ['geo_filters', 'action'], True)

        response = None

        self.mgmt_client = self.get_mgmt_svc_client(CdnManagementClient,
                                                    base_url=self._cloud_environment.endpoints.resource_manager)

        resource_group = self.get_resource_group(self.resource_group)

        old_response = self.get_endpoint()

        if not old_response:
            self.log("Endpoint instance doesn't exist")
            if self.state == 'absent':
                self.log("Old instance didn't exist")
            else:
                self.to_do = Actions.Create
        else:
            self.log("Endpoint instance already exists")
            if self.state == 'absent':
                self.to_do = Actions.Delete
            elif self.state == 'present':
                if (not default_compare(self.endpoint, old_response, '', self.results)):
                    self.to_do = Actions.Update

        if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
            self.log("Need to Create / Update the Endpoint instance")

            if self.check_mode:
                self.results['changed'] = True
                return self.results

            response = self.create_update_endpoint()

            self.results['changed'] = True
            self.log("Creation / Update done")
        elif self.to_do == Actions.Delete:
            self.log("Endpoint instance deleted")
            self.results['changed'] = True

            if self.check_mode:
                return self.results

            self.delete_endpoint()
            # This currently doesnt' work as there is a bug in SDK / Service
            if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
                response = self.get_poller_result(response)
        else:
            self.log("Endpoint instance unchanged")
            self.results['changed'] = False
            response = old_response

        if self.state == 'present':
            self.results.update({
                'id': response.get('id', None)
                })
        return self.results

    def create_update_endpoint(self):
        '''
        Creates or updates Endpoint with the specified configuration.

        :return: deserialized Endpoint instance state dictionary
        '''
        self.log("Creating / Updating the Endpoint instance {0}".format(self.name))

        try:
            if self.to_do == Actions.Create:
                response = self.mgmt_client.endpoints.create(resource_group_name=self.resource_group,
                                                             profile_name=self.profile_name,
                                                             endpoint_name=self.name,
                                                             endpoint=self.endpoint)
            else:
                response = self.mgmt_client.endpoints.update(resource_group_name=self.resource_group,
                                                             profile_name=self.profile_name,
                                                             endpoint_name=self.name,
                                                             endpoint_update_properties=self.endpoint_update_properties)
            if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
                response = self.get_poller_result(response)

        except CloudError as exc:
            self.log('Error attempting to create the Endpoint instance.')
            self.fail("Error creating the Endpoint instance: {0}".format(str(exc)))
        return response.as_dict()

    def delete_endpoint(self):
        '''
        Deletes specified Endpoint instance in the specified subscription and resource group.

        :return: True
        '''
        self.log("Deleting the Endpoint instance {0}".format(self.name))
        try:
            response = self.mgmt_client.endpoints.delete(resource_group_name=self.resource_group,
                                                         profile_name=self.profile_name,
                                                         endpoint_name=self.name)
        except CloudError as e:
            self.log('Error attempting to delete the Endpoint instance.')
            self.fail("Error deleting the Endpoint instance: {0}".format(str(e)))

        return True

    def get_endpoint(self):
        '''
        Gets the properties of the specified Endpoint.

        :return: deserialized Endpoint instance state dictionary
        '''
        self.log("Checking if the Endpoint instance {0} is present".format(self.name))
        found = False
        try:
            response = self.mgmt_client.endpoints.get(resource_group_name=self.resource_group,
                                                      profile_name=self.profile_name,
                                                      endpoint_name=self.name)
            found = True
            self.log("Response : {0}".format(response))
            self.log("Endpoint instance : {0} found".format(response.name))
        except CloudError as e:
            self.log('Did not find the Endpoint instance.')
        if found is True:
            return response.as_dict()

        return False

def default_compare(new, old, path, result):
    if new is None:
        return True
    elif isinstance(new, dict):
        if not isinstance(old, dict):
            result['compare'] = 'changed [' + path + '] old dict is null'
            return False
        for k in new.keys():
            if not default_compare(new.get(k), old.get(k, None), path + '/' + k, result):
                return False
        return True
    elif isinstance(new, list):
        if not isinstance(old, list) or len(new) != len(old):
            result['compare'] = 'changed [' + path + '] length is different or null'
            return False
        if isinstance(old[0], dict):
            key = None
            if 'id' in old[0] and 'id' in new[0]:
                key = 'id'
            elif 'name' in old[0] and 'name' in new[0]:
                key = 'name'
            else:
                key = list(old[0])[0]
            new = sorted(new, key=lambda x: x.get(key, None))
            old = sorted(old, key=lambda x: x.get(key, None))
        else:
            new = sorted(new)
            old = sorted(old)
        for i in range(len(new)):
            if not default_compare(new[i], old[i], path + '/*', result):
                return False
        return True
    else:
        if path == '/location':
            new = new.replace(' ', '').lower()
            old = new.replace(' ', '').lower()
        if new == old:
            return True
        else:
            result['compare'] = 'changed [' + path + '] ' + str(new) + ' != ' + str(old)
            return False

def dict_camelize(d, path, camelize_first):
    if isinstance(d, list):
        for i in range(len(d)):
            dict_camelize(d[i], path, camelize_first)
    elif isinstance(d, dict):
        if len(path) == 1:
            old_value = d.get(path[0], None)
            if old_value is not None:
                d[path[0]] = _snake_to_camel(old_value, camelize_first)
        else:
            sd = d.get(path[0], None)
            if sd is not None:
                dict_camelize(sd, path[1:], camelize_first)

def main():
    """Main execution"""
    AzureRMEndpoint()

if __name__ == '__main__':
    main()


import torch
import numpy as np
import pickle
from tqdm import tqdm
import pandas as pd
import math
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, log_loss
from sentence_transformers import SentenceTransformer, models
from transformers import AutoModel, AutoTokenizer
import os

os.environ['CUDA_VISIBLE_DEVICES'] = '6,5'

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#device = 'cpu'
print("Device", device)

bert_model = SentenceTransformer("/mount/arbeitsdaten43/projekte/thesis-dp-1/banerjak/msmarco-MiniLM-L-6-v3/")
if torch.cuda.is_available():
    bert_model.cuda()

print("Initializing initial query encoder...")
bert_model.max_seq_length = 1000
torch.manual_seed(10)
torch.autograd.set_detect_anomaly(True)

embed_size = 256
init_query_embed_size = 384
num_docs = 20
batch_size = 10
epochs = 20
max_len = 256
epsilon = 0.0001

tokenizer = AutoTokenizer.from_pretrained('google/electra-small-discriminator', truncation = True)

print("Initializing the model...")
class SBERT(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.roberta = AutoModel.from_pretrained('google/electra-small-discriminator').train()
        self.pooler = torch.nn.AvgPool1d(embed_size)
    def forward(self, ids, attention_masks, token_type_ids):
        embeds = [self.roberta(id, attention_mask, token_type_id).last_hidden_state for (id, attention_mask, token_type_id) in zip(ids, attention_masks, token_type_ids)]
        embeds = [self.pooler(embed) for embed in embeds]
        embeds = [torch.squeeze(embed, dim = 2) for embed in embeds]
        ln = len(embeds)
        embeds = torch.cat(embeds, dim = 0).reshape((ln, num_docs, embed_size))
        return embeds

class GCN(torch.nn.Module):
    def __init__(self, n_h, input_features):
        super().__init__()
        W = torch.FloatTensor(input_features, n_h)
        self.weights = torch.nn.Parameter(W)
        self.relu = torch.nn.ReLU()
        stdv = 1. / math.sqrt(self.weights.size(1))
        self.weights.data.uniform_(-stdv, stdv)
        bs = torch.FloatTensor(n_h)
        self.bias = torch.nn.Parameter(bs)
        self.bias.data.uniform_(-stdv, stdv)

    def forward(self, nfs, A):
        out = torch.matmul(nfs, self.weights)
        out = torch.matmul(A, out)
        out = out + self.bias
        out = self.relu(out)
        return out

class AmbiguityNetwork(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.sbert = SBERT()
        self.gcn1 = GCN(20, embed_size)
        self.gcn2 = GCN(10, 20)
        self.gcn3 = GCN(10,10)
        self.dp = torch.nn.Dropout(p = 0.5)
        self.dense1 = torch.nn.Linear(in_features = num_docs*10 + init_query_embed_size, out_features = 64)
        self.dense2 = torch.nn.Linear(in_features = 64, out_features = 4)
        self.relu = torch.nn.ReLU()

    def forward(self, ids, attention_masks, token_type_ids, init_query):
        embeds = self.sbert(ids, attention_masks, token_type_ids)
        emb_norm = torch.nn.functional.normalize(embeds, dim = 2)
        emb_sum = torch.sum(torch.square(emb_norm), axis = 2)
        csml = torch.matmul(emb_norm, emb_norm.transpose(1,2))
        A = csml
        print(A[0])
        S = torch.sum(A, axis = 2)
        S = S + epsilon
        S = torch.pow(S, -0.5)
        #D = torch.diag_embed(torch.sum(A, axis = 2))
        #D_inv = torch.linalg.inv(D)
        D_ = torch.diag_embed(S)
        #print("D_", D_)
        #D_ = torch.pow(D_inv, 0.5)
        A_ = torch.matmul(A, torch.transpose(D_, 1,2))
        A_ = torch.matmul(D_, A_)
        A = A_ + torch.eye(embeds.shape[1]).to(device)
        out = self.gcn1(embeds, A)
        out = self.dp(out)
        out = self.gcn2(out, A)
        out = self.dp(out)
        out = self.gcn3(out, A)
        out = self.dp(out)
        out = torch.flatten(out, 1)
        out = torch.cat((out, init_query.to(device)), dim = 1)
        out = self.dense1(out)
        out = self.relu(out)
        out = self.dense2(out)
        return out

class InputDataset(torch.utils.data.Dataset):
    def __init__(self, ids, masks, token_type_ids, labels, init_qrs):
        self.ids = ids
        self.masks = masks
        self.token_type_ids = token_type_ids
        self.labels = torch.LongTensor(labels.long())
        self.init_qrs = init_qrs

    def __len__(self):
        return len(self.ids)

    def __getitem__(self, idx):
        return self.ids[idx], self.masks[idx], self.token_type_ids[idx], self.labels[idx], self.init_qrs[idx]

def prep_data():
    print("Preparing the data...")

    requests = pd.read_table('/mount/arbeitsdaten43/projekte/thesis-dp-1/banerjak/data/dev.tsv', sep = '\t', header = 0)

    df = requests

    f = open('/mount/arbeitsdaten43/projekte/thesis-dp-1/banerjak/doclists_dev.pkl', 'rb')
    docs = {}
    queries = {}
    query_list = []
    while True:
        try:
            data = pickle.load(f)
            tid = int(data['topic_id'])
            docs[tid] = data['docs']
            queries[tid] = data['queries']
        except EOFError:
            break
    f.close()

    tids = list(docs.keys())

    inputs = []
    for tid in tids:
        inputs.append(docs[tid][:num_docs])

    init_qrs = torch.zeros((len(tids), init_query_embed_size))
    ids = torch.LongTensor(len(tids), num_docs, max_len)
    masks = torch.LongTensor(len(tids), num_docs, max_len)
    token_type_ids = torch.LongTensor(len(tids), num_docs, max_len)

    print("Preparing training data...")

    for i in tqdm(range(len(tids))):
        tid = tids[i]
        documents = inputs[i]
        tokens = [tokenizer.encode_plus(doc, None, add_special_tokens = True, max_length = max_len, padding = 'max_length', return_token_type_ids = True, truncation = True) for doc in documents]
        idlist = [torch.tensor(token['input_ids'], dtype = torch.long) for token in tokens]
        torch.cat(idlist, out = ids[i])
        masklist = [torch.tensor(token['attention_mask'], dtype = torch.long) for token in tokens]
        torch.cat(masklist, out = masks[i])
        tokentypelist = [torch.tensor(token['token_type_ids'], dtype = torch.long) for token in tokens]
        torch.cat(tokentypelist, out = token_type_ids[i])
        init_qrs[i] = torch.Tensor(bert_model.encode(queries[tid]))

    return tids, ids, masks, token_type_ids, init_qrs

print("Testing")
def test():
    tids, ids, masks, token_type_ids, init_qrs = prep_data()
    model = torch.nn.DataParallel(AmbiguityNetwork())
    model.load_state_dict(torch.load('roberta_finetune_aug_model_for_test.pt'))
    model.cuda(device)
    model.eval()
    print("Testing...")
    ids = ids.to(device)
    masks = masks.to(device)
    token_type_ids = token_type_ids.to(device)
    with torch.no_grad():
        val_preds = model(ids, masks, token_type_ids, init_qrs)
    m = torch.nn.Softmax()
    val_npreds = m(val_preds).cpu().numpy()
    class_preds = np.argmax(val_npreds, axis = 1)
    class_preds = class_preds + 1
    outs = [(tids[i], class_preds[i]) for i in range(len(class_preds))]
    outs = np.array(outs)
    np.savetxt('preds_model_1_dev.txt', outs, fmt="%s %s")
    np.save('classpreds_model_1_dev.npy', class_preds)

test()


"""
Need to consider the switching on and off of physics schemes with reduced
precision. Show the active/deactive points as a function of precision for
selected physics schemes (Vertical Diffusion/Surface Fluxes/Convection)
"""

import numpy as np
import matplotlib.pyplot as plt
import iris.plot as iplt

from myscripts.statistics import count
from myscripts.models import speedy
from myscripts.projects.ithaca.tendencies import load_tendency

def main():
    variable = 'Temperature'
    sigma = speedy.sigma_levels[0]
    precisions = range(5, 24)

    # Create a two by two grid with shared x and y axes along rows and columns
    fig, axes = plt.subplots(nrows=2, ncols=2, sharex='col', figsize=[16, 10])

    plt.axes(axes[0, 0])
    scheme = 'Convection'
    make_plot(variable, scheme, sigma, precisions)
    plt.legend(title='Number of gridpoints')

    plt.axes(axes[0, 1])
    scheme = 'Surface Fluxes'
    make_plot(variable, scheme, sigma, precisions)

    plt.axes(axes[1, 0])
    scheme = 'Vertical Diffusion'
    make_plot(variable, scheme, sigma, precisions)

    plt.axes(axes[1, 1])
    variable = 'Specific Humidity'
    make_plot(variable, scheme, sigma, precisions)

    plt.xlabel('Precision [sbits]')
    fig.text(0.01, 0.5, 'Number of Gridpoints',
             rotation='vertical', va='center')

    plt.show()

    return

def make_plot(variable, scheme, sigma, precisions):
    rp = load_tendency(
        variable=variable, scheme=scheme,
        rp_scheme=scheme.replace(' ', '_').replace('-', '_').lower(),
        sigma=sigma, precision=precisions)

    fp = load_tendency(
        variable=variable, scheme=scheme,
        rp_scheme='all_parametrizations',
        sigma=sigma, precision=52)
    plt.title(scheme)
    plot_active(rp, fp)

    return

def plot_active(rp, fp, **kwargs):
    # Count the number of gridboxes with nonzero tendencies
    n_active, n_activated, n_deactivated, n_zeros = \
        count_active_deactive(rp, fp)

    for linestyle, label, data in [('-k', 'Active', n_active),
                                   ('--k', 'Activated', n_activated),
                                   (':k', 'Deactivated', n_deactivated)]:
        iplt.plot(data, linestyle, label=label, **kwargs)

    return

def count_active_deactive(rp, fp):
    # Ignore gridboxes where reduced precision has activated or deactivated the
    # physics scheme. Will give e=infinity or e=-1 respectively

    # Create a dummy cube to use the collapsed function
    cube = rp.copy()

    # Number of active gridpoints
    n_active = count(rp)

    # Number of activated gridpoints
    activated = np.logical_and(rp.data != 0, fp.data == 0)
    cube.data = activated
    n_activated = count(cube)

    # Number of deactivated gridpoints
    deactivated = np.logical_and(rp.data == 0, fp.data != 0)
    cube.data = deactivated
    n_deactivated = count(cube)

    # Number of zeros
    n_zeros = count(cube, func=lambda x: x == 0)

    return n_active, n_activated, n_deactivated, n_zeros

if __name__ == '__main__':
    main()


import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Load the cleaned data
data = pd.read_csv("C:\\Users\\wamm1\\Desktop\\midterm\\clean_pop.csv")

# List of countries for visualization
countries = ['Canada', 'India', 'Kenya', 'Brazil', 'Ukraine']

# Filter data for these countries
selected_countries = data[data['country'].isin(countries)]

# Plot
plt.figure(figsize=(12, 7))
sns.lineplot(data=selected_countries, x='year', y='Infant Mortality Rate', hue='country')
plt.title('Infant Mortality Rate Over the Years')
plt.ylabel('Infant Mortality Rate')
plt.xlabel('Year')
plt.legend(title='Country')
plt.show()


from pathlib import Path
import numpy as np
import copy
from itertools import permutations
import re

data_folder = Path(__file__).parent.resolve()
reg = re.compile(
    r"(\w+) would (lose|gain) (\d+) happiness units by sitting next to (\w+)."
)

class Table:
    def __init__(self, data):
        happiness = []
        people = set()
        for line in data.split("\n"):
            m = reg.match(line)
            assert m is not None
            new_data = list(m.groups())
            new_data[2] = int(new_data[2])
            if new_data[1] == "lose":
                new_data[2] *= -1
            new_data.pop(1)
            happiness.append(new_data)
            for i in [0, 2]:
                people.add(new_data[i])
        self.n_people = len(people)
        self.people = dict(zip(people, np.arange(self.n_people)))
        self.happiness = self.compute_happiness_matrix(happiness)

    def compute_happiness_matrix(self, happiness):
        n = self.n_people
        happiness_matrix = np.zeros((n, n), dtype=int)
        for h in happiness:
            i = self.people[h[0]]
            j = self.people[h[2]]
            happiness_matrix[i, j] = h[1]
        return happiness_matrix

    def find_max_happiness(self,include_self=False):
        n = self.n_people
        if include_self:
            n += 1
        s = np.arange(n)
        max_happiness = 0
        for order in permutations(s):
            happiness = 0
            for j in range(n):
                i = (j + 1) % n

                if (order[i]==self.n_people) or (order[j]==self.n_people):
                    continue

                happiness += self.happiness[order[j], order[i]]
                happiness += self.happiness[order[i], order[j]]

            if happiness > max_happiness:
                max_happiness = happiness

        return max_happiness

def main():
    data_folder = Path(".").resolve()
    data = data_folder.joinpath("input.txt").read_text()
    t = Table(data)

    print("Part 1")
    print(
        "The total change in happiness for the optimal "
        + f"seating arrangement is {t.find_max_happiness()}"
    )
    print()

    print("Part 2")
    print(
        "The total change in happiness for the optimal "
        + f"seating arrangement including you is {t.find_max_happiness(True)}"
    )

if __name__ == "__main__":
    main()


"""
plot_model.py
Code to plot an HII region model fit and data
Based on IDL code by A.A. Kepley

Trey Wenger June 2016

dsb 21Jun2016 - Use linecolor to distinguish models instead of linestyle
dsb 02Sep2016 - Modify units labels in plots
dsb 14Sep2016 - Add spontaneous emission model; GBT data point
dsb 26Sep2016 - Create separate plots for different models
dsb 16Nov2016 - Need divide HII region size by 2 to get radius
dsb 07Dec2016 - Add obsDV and smDens to inputs.
"""
import numpy as np
import math
import matplotlib
matplotlib.rc_file('matplotlibrc')
import matplotlib.pyplot as plt
import astropy.constants as c
import astropy.units as u
from scipy.optimize import curve_fit
import utils
import argparse

import pdb
#pdb.set_trace()

# Define power law function
def powFunction(x, a, b):
    return a*np.array(x)**b

def plot_model(filenames,labels=None,
               freqrange=[1.,120.],
               linerange=[1.e-25,1.e-21],
               contrange=[1.e-29,1.e-26],
               obsDV = 75.0,
               smDen=[10.0, 25.0, 100.0]):
    """
    Plot model fits on top of the data

    Inputs:
      filenames = list of HII region model pickle files to plot
      labels    = list of labels for legend
      linerange = range for y-axis of line plot (W/m2)
      contrange = range for y-axis of cont plot (W/m2/Hz)
      obsDV     = observed RRL FWHM line width (km/s)
      smDen     = densities to use for simple model (cm-3)
    """

    # --------------------------------
    # Model: Collection of HII regions
    # --------------------------------
    #
    if len(filenames) > 4:
        raise ValueError("Can plot up to 4 models at once")
    if labels is None:
        model_labels = [None] * len(filenames)
    linecolors = ['k','r','g','b']
    #
    # Calculate best fit line and continuum flux models for a range
    # of RRL frequencies
    #
    models = [np.load(filename) for filename in filenames]
    # range of n to plot
    fit_lines = np.arange(110)+40.
    for model in models:
        model.fit_lines = fit_lines
        model.calc_HII_region_properties()
    #
    # Plot RRL
    #
    fig = plt.figure()
    # Models
    for model,linecolor,label in zip(models,linecolors,model_labels):
        plt.plot(model.rrl_freq,model.line_flux*model.num_HII_regions,
                 linecolor,linestyle='-',label=label)
    # VLA data
    plt.errorbar(model.linedata['freq_GHz'],model.linedata['intflux_Wm2'],
                 yerr=model.linedata['intflux_Wm2_err'],linestyle='',
                 color='k',marker='o')
    # GBT data
    # (H52+H53) integrated intensity = 147.1 (5.7) mJy km/s
    # H52alpha freq = 45.453 GHz
    freqGBT = 45.45373
    sInt_mks = 147.1*1.e-3*1.e-26*1.e3*freqGBT*1.e9/c.c.value
    sIntErr_mks = 5.7*1.e-3*1.e-26*1.e3*freqGBT*1.e9/c.c.value
    #plt.errorbar([freqGBT], [sInt_mks], yerr=[sIntErr_mks], color='g', marker='o')
    # labels, etc.
    plt.xlabel("Frequency (GHz)", fontsize=20)
    plt.ylabel(r"Integrated Line Flux (W$\,$m$^{-2}$)", fontsize=20)
    plt.xscale('log')
    plt.yscale('log')
    plt.xlim(freqrange)
    plt.ylim(linerange)
    plt.text(freqrange[0]*(1.2), linerange[1]*(1-0.5), 'Multiple Compact HII Region Model', fontsize=20)
    fooNe = "%.2g" % (model.electron_dens)
    fooTe = "%4.0f" % (model.electron_temp)
    plt.text(freqrange[0]*(1.2), linerange[1]*(1-0.75), r'(n$_{\rm e}$, T$_{\rm e}$)  = (' + fooNe + ' cm$^{-3}$, '  + fooTe + r' K)', fontsize=20)

    if label is not None:
        plt.legend(fontsize=12,loc='best')
    plt.tight_layout()
    plt.savefig('linefit.eps')
    #
    # Plot radio continuum
    #
    fig = plt.figure()
    for model,linecolor,label in zip(models,linecolors,model_labels):
        if label is None:
            mylabel = ''
        else:
            mylabel = label+', '
        plt.plot(model.rrl_freq,model.thermal_fluxden_rrl+model.nonthermal_fluxden_rrl,
                 linecolor,linestyle='-',label=mylabel+'Total')
        plt.plot(model.rrl_freq,model.thermal_fluxden_rrl,
                 linecolor,linestyle=':',label=mylabel+'Thermal')
        plt.plot(model.rrl_freq,model.nonthermal_fluxden_rrl,
                 linecolor,linestyle='--',label=mylabel+'Non-thermal')
    plt.errorbar(model.contdata['freq_GHz'],model.contdata['S_C_mJy']*1.e-29,
                 yerr=model.contdata['S_C_mJy_err']*1.e-29,linestyle='',
                 color='k',marker='o')
    plt.xlabel("Frequency (GHz)", fontsize=20)
    plt.ylabel(r"Continuum Flux Density (W$\,$m$^{-2}\,$Hz$^{-1}$)", fontsize=20)
    plt.xscale('log')
    plt.yscale('log')
    plt.xlim(freqrange)
    plt.ylim(contrange)
    plt.text(freqrange[0]*(1.2), contrange[1]*(1-0.5), 'Multiple Compact HII Region Model', fontsize=20)
    plt.legend(fontsize=16,loc='best')
    plt.tight_layout()
    plt.savefig('contfit.eps')

    # --------------------------------
    # Model: Spontaneous emission
    # --------------------------------
    #
    # Calculate simple model (sm): spontaneous, optically thin emission in LTE
    #  Use equations 4-9 from Puxley et al. (1991, MNRAS, 248, 585)
    #
    smTe = model.electron_temp              # K
    smDist = model.distance                 # Mpc
    smFreq = model.linedata['freq_GHz'][0]  # GHz use Ka-band freq
    smOmega = model.omega_region            # steradians
    smAngSize = math.sqrt(smOmega/math.pi)  # radians
    smLinSize = smDist*1.e6*smAngSize       # pc
    # line width (Hz)
    smDV = (obsDV*1.e3)*(smFreq*1.e9)/c.c.value
    # Planck Function (mks)
    smB = (2.0*c.h.value*(smFreq*1.e9)**3/c.c.value**2)*(1.0/(math.exp((c.h.value*smFreq*1.e9)/(c.k_B.value*smTe)) - 1.0))
    # set density list to an np array
    smDen = np.array(smDen)
    # EM cm-6 pc
    smEM = smDen**2*smLinSize
    # Continuum optical depth (Eq. 8)
    smTauC = 8.2e-2*smTe**(-1.35)*smFreq**(-2.1)*smEM
    # LTE line optical depth (Eq. 7)
    smTauL = 1.7e3*(smDV*1.e-3)**(-1)*smTe**(-2.5)*smEM
    # thermal continuum flux density (Eq. 5)
    smContFlux = smOmega*smB*(1.0 - np.exp(-smTauC))
    # spontaneous line emission (Eq. 4)
    smLineEmission = smOmega*smB*np.exp(-smTauC)*(1.0 - np.exp(-smTauL))
    # integrated line emission (mks)
    smIntLine = 1.064*smLineEmission*smDV
    # Line flux (optically thin) = a * freq^2
    aLine = smIntLine/smFreq**2
    # Cont flux (optically thin) = a * freq^(-0.1)
    aCont = smContFlux/smFreq**(-0.1)
    # number of H-ionizing photons (assume a sphere)
    #  Assume case B recombination rate (cm^3 s-1)
    #  Use Hui & Gnedin (1997) approximation
    lambda_hi = 315614.0/smTe
    alpha_B = (2.753e-14*(lambda_hi**1.5/(1.0 + (lambda_hi/2.740)**0.407)**2.242))
    smRadius = (smLinSize*u.pc).to(u.cm).value/2.0
    smNL = (4.0/3.0)*math.pi*smRadius**3*smDen**2*alpha_B
    # SFR from Murphy et al. (2011)
    smSFR = 7.29e-54*smNL

    # Plot RRL
    #
    fig = plt.figure()
    # Simple model
    xx = np.array([model.rrl_freq[len(model.rrl_freq)-1], model.rrl_freq[0]])
    for i in range(len(aLine)):
        yy = aLine[i]*xx**2
        if smTauC[i] < 1.0 and smTauL[i] < 1.0:
            fooNe = "%3.0f" % (smDen[i])
            fooTe = "%4.0f" % (smTe)
            plt.plot(xx, yy, '--', label=r'(n$_{\rm e}$, T$_{\rm e}$)  = (' + fooNe + ' cm$^{-3}$, '  + fooTe + r' K)')
    # VLA data
    plt.errorbar(model.linedata['freq_GHz'],model.linedata['intflux_Wm2'],
                 yerr=model.linedata['intflux_Wm2_err'],linestyle='',
                 color='k',marker='o')
    # GBT data
    # (H52+H53) integrated intensity = 147.1 (5.7) mJy km/s
    # H52alpha freq = 45.453 GHz
    freqGBT = 45.45373
    sInt_mks = 147.1*1.e-3*1.e-26*1.e3*freqGBT*1.e9/c.c.value
    sIntErr_mks = 5.7*1.e-3*1.e-26*1.e3*freqGBT*1.e9/c.c.value
    #plt.errorbar([freqGBT], [sInt_mks], yerr=[sIntErr_mks], color='g', marker='o')
    # labels, etc.
    plt.xlabel("Frequency (GHz)", fontsize=20)
    plt.ylabel(r"Integrated Line Flux (W$\,$m$^{-2}$)", fontsize=20)
    plt.xscale('log')
    plt.yscale('log')
    plt.xlim(freqrange)
    plt.ylim(linerange)
    plt.text(freqrange[0]*(1.2), linerange[1]*(1-0.5), 'Spontaneous Emission Model', fontsize=20)
    plt.legend(fontsize=12, loc=4)
    plt.tight_layout()
    plt.savefig('linefitSimple.eps')
    #
    # Plot radio continuum (use second density model only)
    #
    fig = plt.figure()
    # non-thermal = total observed - thermal
    xObs = model.contdata['freq_GHz']
    yObs = model.contdata['S_C_mJy']*1.e-29
    yThermal = aCont[1]*xObs**(-0.1)
    yNonthermal = yObs - yThermal
    # Fit Power law
    p0 = [yObs.mean(), -0.7]
    popt, pcov = curve_fit(powFunction, xObs, yNonthermal, p0)
    # Calculate various contributions over extended frequency range
    xx = np.array([model.rrl_freq[len(model.rrl_freq)-1], model.rrl_freq[0]])
    # Thermal
    yyThermal = aCont[1]*xx**(-0.1)
    # Non-thermal
    yyNonthermal = popt[0]*xx**(popt[1])
    # Total
    yyTotal = yyThermal + yyNonthermal
    # plot model
    plt.plot(xx, yyTotal, '-k', label='Total')
    plt.plot(xx, yyThermal, ':k', label='Thermal')
    plt.plot(xx, yyNonthermal, '--k', label='Non-thermal')
    # plot data
    plt.errorbar(model.contdata['freq_GHz'],model.contdata['S_C_mJy']*1.e-29,
                 yerr=model.contdata['S_C_mJy_err']*1.e-29,linestyle='',
                 color='k',marker='o')
    plt.xlabel("Frequency (GHz)", fontsize=20)
    plt.ylabel(r"Continuum Flux Density (W$\,$m$^{-2}\,$Hz$^{-1}$)", fontsize=20)
    plt.xscale('log')
    plt.yscale('log')
    plt.xlim(freqrange)
    plt.ylim(contrange)
    plt.text(freqrange[0]*(1.2), contrange[1]*(1-0.5), 'Spontaneous Emission Model', fontsize=20)
    plt.legend(fontsize=16,loc='best')
    plt.tight_layout()
    plt.savefig('contfitSimple.eps')

    # print results (second density model only)
    print("=============================")
    print("Spontaneous Emissionn Model")
    print("=============================")
    print("Electron temperature = {:g} K".format(smTe))
    print("Electron density = {:g} cm-3".format(smDen[1]))
    print("HII region size = {:g} pc".format(smLinSize))
    print("Non-thermal spectral index = {:g}".format(popt[1]))
    #print("Mass of ionized gas = {:g} Msun".format(best_model.mass_ion))
    print("Number of H-ionizing photons = {:g} s-1".format(smNL[1]))
    print("Star Formation Rate = {:g} Msun/yr".format(smSFR[1]))
    print("=============================")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Plot HII region model(s) and data",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('filenames',nargs="+",help='filenames of model pickle files')
    parser.add_argument('--labels',nargs="+",default=None,help='labels for each model')
    parser.add_argument('--freqrange',nargs=2,default=[1.,120.],type=float,help='x-axis range (min max) (GHz)')
    parser.add_argument('--linerange',nargs=2,default=[1.e-25,1.e-21],type=float,help='y-axis range (min max) for line plots (W/m2)')
    parser.add_argument('--contrange',nargs=2,default=[1.e-29,1.e-26],type=float,help='y-axis range (min max) for cont plots (W/m2/Hz)')
    parser.add_argument('--obsDV',nargs=1,default=75.0,type=float,help='observed RRL FWHM line width (km/s)')
    parser.add_argument('--smDen',nargs=3,default=[10., 25.0, 100.0],type=float,help='densities to use for simple model (cm-3)')
    args = parser.parse_args()
    plot_model(args.filenames,labels=args.labels,
               linerange=args.linerange,contrange=args.contrange,
               obsDV=args.obsDV,smDen=args.smDen)


import os
import sys
import requests
from pymongo import MongoClient
from apscheduler.schedulers.blocking import BlockingScheduler

# For simplicity, we are hardcoding the GitHub URL here.
GITHUB_URL = "https://github.com/akto-api-security/pii-types/blob/master/general.json"
# We can also pass the GitHub URL as an environment variable or as a command line argument.
# GITHUB_URL = os.environ.get("GITHUB_URL") if os.environ.get("GITHUB_URL") else sys.argv[1] if len(sys.argv) > 1 else print("Please provide a GitHub URL.")

MONGO_URI = os.environ.get("MONGO_URI", "mongodb://localhost:27017")
MONGO_DB_NAME = os.environ.get("MONGO_DB_NAME", "pii_data")
MONGO_COLLECTION_NAME = os.environ.get("MONGO_COLLECTION_NAME", "patterns")
CRON_MINUTES = os.environ.get("CRON_MINUTES", 60)

client = MongoClient(MONGO_URI)
db = client[MONGO_DB_NAME]
collection = db[MONGO_COLLECTION_NAME]

def fetch_data(github_url: str):
    """
    Fetches data from the given GitHub URL and updates the MongoDB collection.
    """
    response = requests.get(github_url)
    if response.ok:
        data = response.json()
        if isinstance(data, dict):
            data = [data]
        collection.delete_many({})
        collection.insert_many(data)
        print("Data fetched and updated in MongoDB.")
    else:
        print("Error fetching data from GitHub.")

def trigger(minutes=int(CRON_MINUTES), github_file_link=GITHUB_URL):
    """
    Triggers the fetch_data function every given number of minutes (default: 60 minutes).
    """
    scheduler = BlockingScheduler()

    # For a single URL, you can use the following code:
    scheduler.add_job(fetch_data, 'interval', minutes=minutes, args=[github_file_link])

    # For multiple URLs, you can use the following code:
    # GITHUB_URLS = ["https://raw.githubusercontent.com/rabilrbl/akto/master/patterns.json", "https://raw.githubusercontent.com/rabilrbl/akto2/master/patterns.json"]
    # scheduler.add_job(fetch_data, 'interval', minutes=60, args=[GITHUB_URLS])

    scheduler.start()

if __name__ == "__main__":
    trigger()


from pathlib import Path
import numpy as np

data_folder = Path(".")

class IntCodeProgram:
    """A Class for the state of an IntCode program"""

    def __init__(self, instr):
        self.instructions = dict(zip(list(range(len(instrs))), instrs))
        self.rel_base = 0
        self.instr_ptr = 0
        self.input = None
        self.output = None

    def get(self, ptr, mode):
        loc = self._find_loc(ptr, mode)
        if loc in self.instructions:
            return self.instructions[loc]
        else:
            return 0

    def set(self, ptr, mode, value):
        loc = self._find_loc(ptr, mode)
        self.instructions[loc] = value

    def _find_loc(self, ptr, mode):
        if mode == 1:
            return ptr
        elif mode == 0:
            return self.get(ptr, 1)
        elif mode == 2:
            return self.get(ptr, 1) + self.rel_base

class Robot:
    """A class for a hull painting robot"""

    def __init__(self, prog, initial_panel_color=0):
        self.location = (0, 0)
        self.panel_colors = {(0, 0): initial_panel_color}
        self.times_painted = {(0, 0): 0}
        self.orientation = 0
        self.prog = IntCodeProgram(prog)

    def paint_panel(self, color_int):
        self.panel_colors[self.location] = color_int
        self.times_painted[self.location] += 1

    def move(self, right):
        if right:
            self.orientation = (self.orientation + 90) % 360
        else:
            self.orientation = (self.orientation - 90) % 360

        if self.orientation == 0:
            self.location = (self.location[0] - 1, self.location[1])
        elif self.orientation == 90:
            self.location = (self.location[0], self.location[1] + 1)
        elif self.orientation == 180:
            self.location = (self.location[0] + 1, self.location[1])
        elif self.orientation == 270:
            self.location = (self.location[0], self.location[1] - 1)

        if self.location not in self.panel_colors:
            self.panel_colors[self.location] = 0
            self.times_painted[self.location] = 0

    def get_n_tiles_painted(self):
        tiles_painted = 0
        for tile in self.times_painted:
            if self.times_painted[tile] > 0:
                tiles_painted += 1
        return tiles_painted

    def print_hull(self):
        row_dim = [0, 0]
        column_dim = [0, 0]
        tile_rows = []
        tile_columns = []
        colors = []
        for tile in self.panel_colors:
            if tile[0] > row_dim[1]:
                row_dim[1] = tile[0]
            elif tile[0] < row_dim[0]:
                row_dim[0] = tile[0]
            if tile[1] > column_dim[1]:
                column_dim[1] = tile[1]
            elif tile[1] < column_dim[0]:
                column_dim[0] = tile[1]
            tile_rows.append(tile[0])
            tile_columns.append(tile[1])
            colors.append(self.panel_colors[tile])

        tile_rows = np.array(tile_rows)
        tile_columns = np.array(tile_columns)
        hull = np.zeros(
            (row_dim[1] - row_dim[0] + 1, column_dim[1] - column_dim[0] + 1), dtype=int
        )
        hull[tile_rows - row_dim[0], tile_columns - column_dim[0]] = colors

        # Assumes text color is white on the system
        print(
            "\n".join(
                [
                    "".join([str(d) for d in row])
                    .replace("0", " ")
                    .replace("1", u"\u2588")
                    for row in hull
                ]
            )
        )

def add(prog, modes):
    n_params = 3
    modes = modes + [0] * (n_params - len(modes))
    prog.set(
        prog.instr_ptr + 2,
        modes[2],
        prog.get(prog.instr_ptr, modes[0]) + prog.get(prog.instr_ptr + 1, modes[1]),
    )
    prog.instr_ptr += n_params
    return None

def mult(prog, modes):
    n_params = 3
    modes = modes + [0] * (n_params - len(modes))
    prog.set(
        prog.instr_ptr + 2,
        modes[2],
        prog.get(prog.instr_ptr, modes[0]) * prog.get(prog.instr_ptr + 1, modes[1]),
    )
    prog.instr_ptr += n_params

def inp(prog, modes):
    n_params = 1
    modes = modes + [0] * (n_params - len(modes))
    prog.set(prog.instr_ptr, modes[0], prog.input)
    prog.instr_ptr += n_params

def outp(prog, modes):
    n_params = 1
    modes = modes + [0] * (n_params - len(modes))
    prog.output = prog.get(prog.instr_ptr, modes[0])
    prog.instr_ptr += n_params

def jump_if_true(prog, modes):
    n_params = 2
    modes = modes + [0] * (n_params - len(modes))

    if prog.get(prog.instr_ptr, modes[0]) > 0:
        prog.instr_ptr = prog.get(prog.instr_ptr + 1, modes[1])
    else:
        prog.instr_ptr += n_params

def jump_if_false(prog, modes):
    n_params = 2
    modes = modes + [0] * (n_params - len(modes))

    if prog.get(prog.instr_ptr, modes[0]) == 0:
        prog.instr_ptr = prog.get(prog.instr_ptr + 1, modes[1])
    else:
        prog.instr_ptr += n_params

def less_than(prog, modes):
    n_params = 3
    modes = modes + [0] * (n_params - len(modes))
    if prog.get(prog.instr_ptr, modes[0]) < prog.get(prog.instr_ptr + 1, modes[1]):
        prog.set(prog.instr_ptr + 2, modes[2], 1)
    else:
        prog.set(prog.instr_ptr + 2, modes[2], 0)
    prog.instr_ptr += n_params

def equals(prog, modes):
    n_params = 3
    modes = modes + [0] * (n_params - len(modes))
    if prog.get(prog.instr_ptr, modes[0]) == prog.get(prog.instr_ptr + 1, modes[1]):
        prog.set(prog.instr_ptr + 2, modes[2], 1)
    else:
        prog.set(prog.instr_ptr + 2, modes[2], 0)
    prog.instr_ptr += n_params

def adj_rel_base(prog, modes):
    n_params = 1
    modes = modes + [0] * (n_params - len(modes))
    prog.rel_base += prog.get(prog.instr_ptr, modes[0])
    prog.instr_ptr += n_params

operations = {
    1: add,
    2: mult,
    3: inp,
    4: outp,
    5: jump_if_true,
    6: jump_if_false,
    7: less_than,
    8: equals,
    9: adj_rel_base,
}

def run_robot(robot):
    end_of_program = False
    output_type = "color"
    while not end_of_program:
        digits = [int(d) for d in str(robot.prog.get(robot.prog.instr_ptr, 1))]
        if len(digits) == 1:
            op_mode = digits[-1]
        else:
            op_mode = digits[-2] * 10 + digits[-1]
        if op_mode == 99:
            return
        else:
            modes = digits[-3::-1]
            robot.prog.instr_ptr += 1
            if op_mode == 3:
                robot.prog.input = robot.panel_colors[robot.location]
            operations[op_mode](robot.prog, modes)

            if op_mode == 4:
                if output_type == "color":
                    robot.paint_panel(robot.prog.output)
                    output_type = "direction"
                elif output_type == "direction":
                    robot.move(robot.prog.output)
                    output_type = "color"

file = data_folder / "input.txt"
instrs = [int(instr) for instr in file.read_text().split(",")]

def main():
    # Part 1
    robot = Robot(instrs, 0)
    run_robot(robot)
    print("Part 1:")
    print(f"{robot.get_n_tiles_painted()} tiles were painted at least once.")
    print()

    # Part 2
    robot = Robot(instrs, 1)
    run_robot(robot)
    print("Part 2:")
    print(f"The completed hull looks like:")
    robot.print_hull()

if __name__ == "__main__":
    main()


# The Polymorphism is the provision of a single interface for all class which was inherited it
# Like the example in previous session
# So, I'll create the base class with the base method (but cannot implement)
# When the child class inherits this class, they will implement this methods
# Depend on the child class, the action in this method will be created
# So, in the child class, we don't need define a new method, we only access to this method and implement it
class Vehicle:
  def __init__(self, brand, model):
    self.brand = brand
    self.model = model

  def move(self):
    print("Move!")

class Car(Vehicle):
  pass

class Boat(Vehicle):
  def move(self):
    print("Sail!")

class Plane(Vehicle):
  def move(self):
    print("Fly!")

car1 = Car("Ford", "Mustang") #Create a Car object
boat1 = Boat("Ibiza", "Touring 20") #Create a Boat object
plane1 = Plane("Boeing", "747") #Create a Plane object

for x in (car1, boat1, plane1):
  print(x.brand)
  print(x.model)
  x.move()


import torch
import torch.nn
from collections import OrderedDict

from deep_learning.architectures.Resnet3D.model import get_pretrained_resnet
from deep_learning.architectures.ClinicalNet import ClinicalNet

def load_trained_model(model, weights_path):
    print('loading pretrained model {}'.format(weights_path))
    pretrain = torch.load(weights_path, map_location='cpu')
    new_state_dict = OrderedDict()
    for k, v in pretrain['state_dict'].items():
        name = k[6:] # remove `model.`
        new_state_dict[name] = v

    model.load_state_dict(new_state_dict)
    return model

class Ensemble(torch.nn.Module):
    def __init__(self, indices: list, fold: int, combined_clinical=False):
        super().__init__()
        self.imaging_models = torch.nn.ModuleList()
        self.clinical_model = None
        if combined_clinical:
            self.clinical_model = ClinicalNet()
        # ["T1_COR_agent_None", "T1_FS_COR_agent_GD", "T2_FS_COR_agent_None", "T1_FS_AX_agent_GD", "T2_FS_AX_agent_None"]
        # Fold 0
        if fold == 0:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
                 # Add path to trained clinical model...
            ]
        # Fold 1
        if fold == 1:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
            ]
        # Fold 2
        if fold == 2:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
            ]
        # Fold 3
        if fold == 3:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
            ]
        # Fold 4
        if fold == 4:
             trained_paths = [
                 # Specify path to weights
                 "/path/to/weights/T1_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_COR_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_COR_agent_None.ckpt",
                 "/path/to/weights/T1_FS_AX_agent_GD.ckpt",
                 "/path/to/weights/T2_FS_AX_agent_None.ckpt",
            ]
        trained_paths = [path for i, path in enumerate(trained_paths) if i in indices]
        for path in trained_paths:
            model = get_pretrained_resnet(34)
            model = load_trained_model(model, path)
            # Freeze all models
            for parameter in model.parameters():
                parameter.requires_grad = False
            self.imaging_models.append(model)

    def forward(self, x, clinical_data=None):
        predictions = []
        for x_, model in zip(x, self.imaging_models):
            pred = model(x_)
            predictions.append(pred)
        if self.clinical_model:
            predictions.append(self.clinical_model(clinical_data))
        ensemble_prediction = torch.mean(torch.stack(predictions, dim=0), 0)
        return ensemble_prediction


def solution(babbling):
    answer = 0

    cap = ["aya", "ye", "woo", "ma"]
    d_cap = ['ayaaya','yeye','woowoo','mama']
    b = []
    b2 = []

    for i in babbling:
        if i in cap:
            answer += 1
        else:
            b.append(i)

    for i in b:
        count = 0
        for e in d_cap:
            if e in i:
                count +=1
        if count == 0:
            b2.append(i)

    for i in range(len(b2)):
        j = 0
        while j < 16:
            for e in cap:
                if e in b2[i]:
                    b2[i] = b2[i].replace(e,'')
                    if len(b2[i]) == 0:
                        answer +=1
                        break
            j+=1
    return answer

c = ["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
print(solution(c))


"""This is the utils module.

Module for storing information to the file between program launches.
"""

from os import path
import json

# '/your_absolute_path_here/notes.json'
JSON_FILE = path.abspath('notes.json')

def save_to_file(notes: list) -> None:
    """
    Save the list with notes to a file.

    :param notes: list with notes.
    :return: None
    """
    with open(JSON_FILE, 'w', encoding='utf-8') as outfile:
        json.dump(notes,
                  outfile,
                  ensure_ascii=False,
                  indent=4,
                  default=str,
                  separators=(',', ': '))

def load_from_json_file() -> list:
    """
    Load notes from file if file exists otherwise create empty JSON file.

    :return: list with notes
    """
    if not path.isfile(JSON_FILE):
        save_to_file([])

    with open(JSON_FILE, 'r', encoding='utf-8') as f:
        notes = json.load(f)
    return notes


from django.shortcuts import get_object_or_404
from services.models import Services
from decimal import Decimal

def get_cart_items_and_total(cart):
    cart_items = []
    total = 0
    for item_id, item_quantity in cart.items():
        this_service = get_object_or_404(Services, pk=item_id)
        this_total = this_service.price * Decimal(item_quantity)
        total += this_total
        this_item = {
            'service_id': item_id,
            'name': this_service.name,
            'quantity': item_quantity,
            'price': this_service.price,
            'total': this_total,
        }
        cart_items.append(this_item)

    return { 'cart_items': cart_items, 'total': total }


class Sort:

	def bubble_sort(self,list):

		for i in range(0,len(list)-1):

			for j in range(0,len(list)-1-i):
				if list[j] > list[j+1]:
					list[j] , list[j+1] = list[j+1] , list[j]

				else:
					list[j]
		return list

	def insertionSort(self,arr):

		for i in range(1, len(arr)):

			key = arr[i]
			j = i-1

			while j >=0 and key < arr[j] :
				arr[j+1] = arr[j]
				j -= 1
			arr[j+1] = key
		return arr

	def selectionSort(self,arr):
		size = len(arr)
		for i in range(size-1):
			value = arr[i]
			j =i-1

		while j>=0 and value<arr[j]:
			arr[j+1]=arr[j]
			j -=1
		arr[j+1] = value
		return arr

obj = Sort()
list=[23,6,54,9,85]
print(list)

choice=input("enter type of search B for bubble sort ,I for insertion sort and S for selection sort")
if choice=="B":
	print(obj.bubble_sort(list))

elif choice=="I":
	print(obj.insertionSort(list))

elif choice=="S":
	print(obj.selectionSort(list))

else:
	print("make correct choice B,I,S")


import logging
from smtplib import SMTPException

from django.contrib import messages
from django.core.mail import EmailMultiAlternatives
from django.http import HttpRequest
from django.template.loader import render_to_string
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _

def try_send_email_add_warning_if_failed(
        request: HttpRequest, email: str, subject: str, message: str
):
    html = render_to_string(
        'email/regular_email.html',
        context={
            'header': subject,
            'text':   message
        }
    )
    if not send_html_email(email, subject, html):
        messages.warning(request, _(f'Не вдалося надіслати повідомлення на пошту {email}'))

def send_html_email(email: str, subject: str, html: str) -> bool:
    msg = EmailMultiAlternatives(
        subject=subject,
        body=mark_safe(html),
        # django requires from_email to be explicitly set to None
        # to use settings.DEFAULT_FROM_EMAIL
        from_email=None,
        to=[email]
    )
    msg.content_subtype = 'html'
    try:
        msg.send()
    except SMTPException as e:
        logging.error(
            'failed to send email to %s with error: %s',
            email,  # pylint: disable=C0209
            e
        )
        return False
    return True


from collections import OrderedDict
from six.moves.urllib.parse import urlencode

def joined_or_null(arr):
    return "null" if len(arr) == 0 else ','.join(arr)

def build_url(path, includes=None, fields=None):
    connector = '&' if '?' in path else '?'
    params = {}

    if includes:
        params.update({'include': joined_or_null(includes)})

    if fields:
        params.update(
            {
                "fields[{resource_type}]".format(resource_type=resource_type):
                joined_or_null(attributes)
                for resource_type, attributes in fields.items()
            }
        )

    if not params:
        return path

    sorted_params = OrderedDict(sorted(params.items(), key=lambda t: t[0]))
    return path + connector + urlencode(sorted_params)


# import matplotlib.pyplot as plt
# import numpy as np
#
# # Sample data for clustered columns
# categories = ['Category A', 'Category B', 'Category C']
# values1 = [15, 25, 30]
# values2 = [10, 20, 25]
#
# # Create an array for the x-axis positions
# x = np.arange(len(categories))
#
# # Set the width of each bar
# bar_width = 0.35
#
# # Create the clustered columns
# plt.bar(x - bar_width/2, values1, bar_width, label='Value 1', color='b', align='center')
# plt.bar(x + bar_width/2, values2, bar_width, label='Value 2', color='g', align='center')
#
# # Add labels and legend
# plt.xlabel('Categories')
# plt.ylabel('Values')
# plt.title('Clustered Column Bar Chart')
# plt.xticks(x, categories)
# plt.legend()
#
# # Show the plot
# plt.tight_layout()
# plt.show()

import matplotlib.pyplot as plt
import numpy as np

# Sample data (replace with your actual data)
time = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
memory_overhead = [100, 105, 98, 110, 115, 112, 120, 130, 128, 132]

# Window size for the moving average (adjust as needed)
window_size = 4

# Calculate the moving average
smoothed_memory_overhead = np.convolve(memory_overhead, np.ones(window_size)/window_size, mode='same')

# Create a figure and axis
fig, ax = plt.subplots()

# Plot the original data
ax.plot(time, memory_overhead, label='Memory Overhead', marker='o')

# Plot the smoothed data
ax.plot(time, smoothed_memory_overhead, label='Smoothed Memory Overhead', linestyle='--')

# Set labels and legend
ax.set_xlabel('Time')
ax.set_ylabel('Memory Overhead')
ax.legend()

# Show the plot
plt.show()