import torch
from torch import nn
from torch.nn import functional as F
from torch_geometric.nn import MessagePassing, global_sort_pool
from torch_geometric.utils import add_self_loops, degree
from sklearn.metrics import roc_auc_score
from ThisDataset import ThisDataset
import random
import yaml
random.seed(42)
def warn(*args, **kwargs):
    pass
import warnings
warnings.warn = warn
class DGCNN(nn.Module):
    """
    Uses fixed architecture
    """

    def __init__(self, dim_features, dim_target, config):
        super(DGCNN, self).__init__()

        self.ks = {'NCI1': { '0.6': 30, '0.9': 46 },
                   'PROTEINS_full': { '0.6': 32, '0.9': 81 },
                   'DD': {'0.6': 291, '0.9': 503 },
                   'ENZYMES': { '0.6': 36, '0.9': 48 },
                   'IMDB-BINARY': { '0.6': 18, '0.9': 31 },
                   'IMDB-MULTI': { '0.6': 11, '0.9': 22 },
                   'REDDIT-BINARY': { '0.6': 370, '0.9': 1002 },
                   'REDDIT-MULTI-5K': { '0.6': 469, '0.9': 1081 },
                   'COLLAB': { '0.6': 61, '0.9': 130 },
                   }

        self.k = 30#self.ks[config.dataset.name][str(config['k'])]
        self.embedding_dim = config['embedding_dim'][0]
        self.num_layers = config['num_layers'][0]

        self.convs = []
        for layer in range(self.num_layers):
            input_dim = dim_features if layer == 0 else self.embedding_dim
            self.convs.append(DGCNNConv(input_dim, self.embedding_dim))
        self.total_latent_dim = self.num_layers * self.embedding_dim

        # Add last embedding
        self.convs.append(DGCNNConv(self.embedding_dim, 1))
        self.total_latent_dim += 1

        self.convs = nn.ModuleList(self.convs)

        # should we leave this fixed?
        self.conv1d_params1 = nn.Conv1d(1, 16, self.total_latent_dim, self.total_latent_dim)
        self.maxpool1d = nn.MaxPool1d(2, 2)
        self.conv1d_params2 = nn.Conv1d(16, 32, 5, 1)

        dense_dim = int((self.k - 2) / 2 + 1)
        self.input_dense_dim = (dense_dim - 5 + 1) * 32

        self.hidden_dense_dim = config['dense_dim'][0]
        self.dense_layer = nn.Sequential(nn.Linear(self.input_dense_dim, self.hidden_dense_dim),
                                         nn.ReLU(),
                                         nn.Dropout(p=0.5),
                                         nn.Linear(self.hidden_dense_dim, dim_target))

    def forward(self, data):
        # Implement Equation 4.2 of the paper i.e. concat all layers' graph representations and apply linear model
        # note: this can be decomposed in one smaller linear model per layer
        x, edge_index, batch = data.x, data.edge_index, data.batch

        hidden_repres = []

        for conv in self.convs:
            x = torch.tanh(conv(x, edge_index))
            hidden_repres.append(x)

        # apply sortpool
        x_to_sortpool = torch.cat(hidden_repres, dim=1)
        x_1d = global_sort_pool(x_to_sortpool, batch, self.k)  # in the code the authors sort the last channel only

        # apply 1D convolutional layers
        x_1d = torch.unsqueeze(x_1d, dim=1)
        conv1d_res = F.relu(self.conv1d_params1(x_1d))
        conv1d_res = self.maxpool1d(conv1d_res)
        conv1d_res = F.relu(self.conv1d_params2(conv1d_res))
        conv1d_res = conv1d_res.reshape(conv1d_res.shape[0], -1)

        # apply dense layer
        out_dense = self.dense_layer(conv1d_res)
        return out_dense

class DGCNNConv(MessagePassing):
    """
    Extended from tuorial on GCNs of Pytorch Geometrics
    """

    def __init__(self, in_channels, out_channels):
        super(DGCNNConv, self).__init__(aggr='add')  # "Add" aggregation.
        self.lin = nn.Linear(in_channels, out_channels)

    def forward(self, x, edge_index):
        # x has shape [N, in_channels]
        # edge_index has shape [2, E]

        # Step 1: Add self-loops to the adjacency matrix.
        edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0))

        # Step 2: Linearly transform node feature matrix.
        x = self.lin(x)

        # Step 3-5: Start propagating messages.
        return self.propagate(edge_index, size=(x.size(0), x.size(0)), x=x)

    def message(self, x_j, edge_index, size):
        # x_j has shape [E, out_channels]

        # Step 3: Normalize node features.
        src, dst = edge_index  # we assume source_to_target message passing
        deg = degree(src, size[0], dtype=x_j.dtype)
        deg = deg.pow(-1)
        norm = deg[dst]

        return norm.view(-1, 1) * x_j  # broadcasting the normalization term to all out_channels === hidden features

    def update(self, aggr_out):
        # aggr_out has shape [N, out_channels]

        # Step 5: Return new node embeddings.
        return aggr_out

    def __repr__(self):
        return '{}({}, {})'.format(self.__class__.__name__, self.in_channels,
                                   self.out_channels)

with open("config_DGCNN.yml", "r") as f:
    config = yaml.load(f)
addFile = "/home/bcypher/IdeaProjects/Orbit/articleOrbits.csv"
dataset= ThisDataset(root="data/", n_graphs=5000,sampleWhite= 50, filepath="/home/bcypher/PycharmProjects/PytorchTutorial/data/Orbit/graphs/", addressFile=addFile)
dataset = dataset.shuffle()
from torch_geometric.loader import DataLoader
print(f'Dataset num node features: {dataset.num_node_features:.2f}')
print(f'Dataset num classes: {dataset.num_classes:.2f}')
print(f'Dataset num graphs: {len(dataset)}')
#print(f'Dataset classes: {dataset.vals}')

#train_dataset = dataset[len(dataset) // 10:]
#train_loader = DataLoader(train_dataset, 512, shuffle=True)

#test_dataset = dataset[:len(dataset) // 10]
#test_loader = DataLoader(test_dataset, 512)

def train(train_loader,model,criterion,optimizer):
    model.train()

    for data in train_loader:  # Iterate in batches over the training dataset.
         out = model(data)  # Perform a single forward pass.
         loss = criterion(out, data.y)  # Compute the loss.
         loss.backward()  # Derive gradients.
         optimizer.step()  # Update parameters based on gradients.
         optimizer.zero_grad()  # Clear gradients.

def test(test_loader, model):
    model.eval()

    correct = 0
    auc_score = 0
    total_samples = 0

    for data in test_loader:  # Iterate in batches over the training/test dataset.
        out = model(data)
        pred = out.argmax(dim=1)  # Use the class with the highest probability.

        correct += int((pred == data.y).sum().item())  # Check against ground-truth labels.
        total_samples += data.y.size(0)

        arr2 = out[:, 1].detach().numpy()
        arr1 = data.y.detach().numpy()
        auc_score += roc_auc_score(y_true=arr1, y_score=arr2, multi_class='ovr', average='weighted')

    accuracy = correct / total_samples
    auc_score /= len(test_loader)

    #print(f"Accuracy: {accuracy:.4f}, AUC Score: {auc_score:.4f}")

    return accuracy, auc_score

for duplication in range(0,5):
    train_size = int(0.8 * len(dataset))
    test_size = len(dataset) - train_size
    train_dataset, test_dataset = torch.utils.data.random_split(dataset, [train_size, test_size])
    train_loader = DataLoader(train_dataset, 128, shuffle=True)
    test_loader = DataLoader(test_dataset, 128)
    model = DGCNN(dim_features=dataset.num_features, dim_target=dataset.num_classes, config=config)
    optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
    criterion = torch.nn.CrossEntropyLoss()

    for epoch in range(0, 1001):
        train(train_loader,model,criterion,optimizer)
        scores_tr = test(train_loader,model)
        train_acc = scores_tr[0]
        train_auc = scores_tr[1]
        scores_te = test(test_loader,model)
        test_acc = scores_te[0]
        test_auc = scores_te[1]
        if epoch % 10 == 0:
            print(
                f"Duplicate\t{duplication}\tEpoch\t {epoch}\t Train Accuracy\t {train_acc:.4f}\t Train AUC Score\t {train_auc:.4f}\t Test Accuracy: {test_acc:.4f}\t Test AUC Score\t {test_auc:.4f}")


import random

class VNFGenerator(object):
    Flavor = (large, medium, small) = ('large', 'medium', 'small')

    def __init__(self, number_of_vnf, flavor):
        self.number_of_vnf = number_of_vnf
        self.flavor = flavor

    def gen_vnf(self):
        vnf_resource_list = list()
        for i in range(self.number_of_vnf):
            vnf_resource = [self.gen_cpu(), self.gen_mem(), 10]
            vnf_resource_list.append(vnf_resource)
        return vnf_resource_list

    def gen_cpu(self):
        if self.flavor == self.large:
            cpu = 10
        elif self.flavor == self.medium:
            cpu = random.randint(6, 9)
        else:
            cpu = random.randint(1, 5)
        return cpu

    def gen_mem(self):
        if self.flavor == self.large:
            mem = 20 if random.randint(1, 2) == 1 else 10
        elif self.flavor == self.medium:
            mem = random.randint(6, 9)
        else:
            mem = random.randint(1, 5)
        return mem

if __name__ == '__main__':
    vnf_generator = VNFGenerator(30, 'large')
    a = vnf_generator.gen_vnf()
    print(a)


class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
#        if len(nums)<2: return False
#        k=min(k,len(nums)-1)
#        for i in range(len(nums)-k):
#            for j in range(1,k+1):
#                if nums[i]==nums[i+j]:
#                    return True
#        for i in range(len(nums)-k-1,len(nums)):
#            for j in range(i+1,len(nums)):
#                if nums[i]==nums[j]:
#                    return True
#        return False

#        if len(nums)<2: return False
#        for i in range(1,k+1):
#            for j in range(len(nums)-i):
#                if nums[j]==nums[j+i]: return True
#        return False

        if len(nums)<2: return False
        nums2=[]
        for i in range(len(nums)):
            nums2.append([nums[i],i])
        nums2.sort()
        for i in range(len(nums)-1):
            if nums2[i][0]==nums2[i+1][0] and nums2[i+1][1]-nums2[i][1]<=k:
                return True
        return False


import json
import os

class JsonFileHandler():
    def __init__(self):
        print("hello W")
        self.filename = None
        self.data = None

    def open_json_file(self, filename):
        self.filename = filename
        if os.path.exists(self.filename):
            with open(self.filename, "r") as file:
                self.data = json.load(file)
                self.user_request_index = len(self.data)
        else:
            self.data = []
            self.user_request_index = 0
        print("Opened file")

    def get_json_data(self):
        return self.data

    def save_json_file(self):
        with open(self.filename, "w") as file:
            json.dump(self.data, file)
        print("Saved file")

    def add_json_data(self, data):
        self.data.append(data)


import logging
import pandas as pd

from zenml import step
import os
from components.component_data_cleaning import DataCleaning
from typing import Tuple, Annotated
import pandas as pd

FILE_NAME = os.path.basename(__file__)

@step(enable_cache=False)
def process_clean_data(data: pd.DataFrame) -> Tuple[Annotated[pd.DataFrame, "X_train"], Annotated[pd.DataFrame,
                                                                                "X_test"], Annotated[pd.Series,
                                                                                "y_train"], Annotated[pd.Series, "y_test"]]:
    try:
        logging.info(f"Start cleaning {FILE_NAME}")

        cleaner = DataCleaning(data)
        cleaned_data = cleaner.clean_data()
        logging.info(f"Enf of cleaning {FILE_NAME}")

        logging.info(f"Start splitting {FILE_NAME}")
        X_train, X_test, y_train, y_test = cleaner.divide_data(cleaned_data)
        logging.info(f"Enf of splitting {FILE_NAME}")

        return X_train, X_test, y_train, y_test

    except Exception as e:
        logging.error(f"Error for {FILE_NAME}: {e}")
        raise e


sales_prices = [
    100,
    83,
    220,
    40,
    100,
    400,
    10,
    1,
    3
]

print(sales_prices)

sorted_sales_prices = sorted(sales_prices)

print(sorted_sales_prices)

total_num_sales = len(sorted_sales_prices)

print(total_num_sales) # 9 total sales´s numbers

sales_prices_left = sorted_sales_prices[:4]

sales_prices_right = sorted_sales_prices[5:]

print(sales_prices_left)
print(sales_prices_right)

central_total_prices_sales = sorted_sales_prices[4]

print(central_total_prices_sales)

print('-----other way to know the final solution-----')

# other way to know the final solution

import math

"""
Tools:
- math library
- sorted function
- list slicing
- computations
"""

sale_prices1 = [
  100,
  83,
  220,
  40,
  100,
  400,
  10,
  1,
  3
]

sorted_list = sorted(sale_prices1)
num_of_sales = len(sorted_list)
half_slice = math.floor(num_of_sales/2)
first_sales_items = sorted_list[:half_slice]
last_sales_items = sorted_list[-(half_slice):]
median = sorted_list[half_slice:(half_slice + 1)]

print(sorted_list)
print(num_of_sales)
print(first_sales_items)
print(last_sales_items)
print(median)


menu = """
Bienvenido al conversor de monedas 馃憣

1 - Pesos colombianos
2 - Pesos argentinos
3 - Pesos mexicanos

Elige una opci贸n: """

opcion = int(input(menu))

if opcion == 1:
    pesos = input("驴Cuantos pesos colombianos tienes?: ")
    esos = float(pesos)
    valor_dolar = 3875
    dolares = pesos / valor_dolar
    dolares = round(dolares, 2)
    dolares = str(dolares)
    print("Tienes $"+dolares+ " d贸lares")
elif opcion == 2:
    pesos = input("Ingrese su cantidad de dinero en pesos argentinos: ")
    pesos = float(pesos)
    dolares = pesos/65
    dolares = round(dolares, 2)
    dolares = str(dolares)
    print("La cantidad es: " + dolares + " d贸lares")
elif opcion == 3:
    pesos = input("Ingrese su cantidad de dinero en pesos mexicanos: ")
    pesos = float(pesos)
    dolares = pesos/20
    dolares = round(dolares, 2)
    dolares = str(dolares)
    print("La cantidad es: " + dolares + " d贸lares")
else:
    print("Elige una opci贸n correcta por favor")


import pyowm

owm = pyowm.OWM('5b626cfbc81840ed25df6235c8313d7d')  # You MUST provide a valid API key

# Have a pro subscription? Then use:
# owm = pyowm.OWM(API_key='your-API-key', subscription_type='pro')

# Search for current weather in London (Great Britain)
user_city = input("Введіть місто у якому потрібно дізнатись погоду! ")
observation = owm.weather_at_place(user_city)
w = observation.get_weather()
#print(w)                      # <Weather - reference time=2013-12-18 09:20,
                              # status=Clouds>

# Weather details
w.get_wind()                  # {'speed': 4.6, 'deg': 330}
w.get_humidity()              # 87
w.get_temperature('celsius')  # {'temp_max': 10.5, 'temp': 9.7, 'temp_min': 9.0}
print(f"Speed wind:{w.get_wind()['speed']} km/hours")
print(f"huminidy: {w.get_humidity()} humidity")
print(f"temperature: {w.get_temperature('celsius')['temp']} celsius")

# Search current weather observations in the surroundings of
# lat=22.57W, lon=43.12S (Rio de Janeiro, BR)
observation_list = owm.weather_around_coords(-22.57, -43.12)


# -*- coding: utf-8 -*-
import sys
from maximal import XMLExport

x = XMLExport.XMLExporter(XMLExport.XMLCompressor(sys.stdout))

with x.namespace('http://taverna.org/2013/') as tav:
    with tav.workflow(author='Jon Giddy'):
        tav.input.routine(u'Hλ1ello', {'complex.name': u'hλ1h'}, name='fido', x=2)
        with tav.output as foo:
            foo.hd("World!")

# x.namespace returns a namespace linked to the exporter
# a namespace getattr returns a tag with the same namespace
# e.g. namespace.foo returns a tag foo <foo>
# tag getattr returns an extended tag
# e.g. namespace.foo.bar returns a tag <foo.bar>
# tag __call__(text, )


import pandas as pd
import numpy as np
from .utils import utils

class monte_carlo():
    def __init__(self, size, cov_matrix, mean_returns):
        self.size = size
        self.cov_matrix = cov_matrix
        self.mean_returns = mean_returns

    def get_equal_weights(self, size):
        n = 1.0 / size
        return n

    def get_random_weights(self, size):
        weights = np.random.rand(size)
        weights /= np.sum(weights)
        return weights

    def generate_portfolios(self, df):
        companies = df.columns[1:]
        ports = pd.DataFrame({
            'company': pd.Series(companies)
        })
        ports['mean_return'] = self.mean_returns['mean_return']
        nr_companies = len(companies)
        port_0 = np.empty(nr_companies)
        port_0.fill(self.get_equal_weights(nr_companies))
        ports['port_0'] = pd.Series(port_0)

        random_weights = {}
        ports_columns = list(ports.columns)

        for i in range(self.size):
            ports_columns.append(f'port_{i+1}')
            random_weights[f'w_{i+1}'] = self.get_random_weights(nr_companies)

        weights_df = pd.DataFrame.from_dict(random_weights)
        ports = pd.concat([ports, weights_df], axis=1)
        ports.columns = ports_columns
        return ports

    def evaluate_portfolios(self, ports, riskfree_rate=0):
        return_array = np.array(ports['mean_return'])
        port_return_list = []
        port_risk_list = []
        k = 2
        port_return_dict = {}
        port_risk_dict = {}
        for name,val in ports.items():
            if (k>0):
                k -= 1
                continue
            port_return_dict[f're_{name}'] = utils().calculate_portfolio_returns(return_array,val)
            port_risk_dict[f'ri_{name}'] = utils().calculate_portfolio_volatility(val,
                                                                np.asarray(self.cov_matrix))

        port_expected_returns = pd.DataFrame({
            'return':port_return_dict
        }).reset_index(drop=True)
        port_volatility = pd.DataFrame({
            'risk': port_risk_dict,
        }).reset_index(drop=True)
        port_sharpe_ratios = utils().calculate_sharpe_ratio(
            port_expected_returns['return'],
            port_volatility['risk'],
            riskfree_rate
        ).reset_index(drop=True)

        ports_summary = pd.DataFrame({
            'portfolio': ports.columns[2:]
        })
        ports_summary = pd.concat([ports_summary, port_expected_returns, port_volatility], axis=1)
        ports_summary['sharpe_ratio'] = port_sharpe_ratios
        return ports_summary


"""
다이얼 문제
Author : DongHak Park
Contact: donghark03@naver.com
"""

arr = [3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,8,9,9,9,10,10,10,10]

S = input()
answer = 0
for char in S:
    answer += arr[ord(char) - 65]
print(answer)


from __future__ import division
from future.utils import raise_from

import errno
import json
import os
import platform
import socket
import subprocess
import sys
from collections import OrderedDict
from time import sleep
from warnings import warn

import semver

from pennsieve.log import get_log_level, get_logger
from pennsieve.models import Collection, DataPackage, Dataset

logger = get_logger("pennsieve.agent")

try:
    from websocket import create_connection
except ModuleNotFoundError:
    logger.warn(
        "websocket-client is not installed - uploading with the Agent will not work"
    )

MINIMUM_AGENT_VERSION = semver.VersionInfo.parse("0.3.4")
DEFAULT_LISTEN_PORT = 11235

class AgentError(Exception):
    pass

def agent_cmd():
    if sys.platform == "darwin":
        return "/usr/local/opt/pennsieve/bin/pennsieve"

    elif sys.platform.startswith("linux"):
        return "/opt/pennsieve/bin/pennsieve"

    elif sys.platform in ["win32", "cygwin"]:
        return "C:/Program Files/Pennsieve/pennsieve.exe"

    raise AgentError("Platform {} is not supported".format(sys.platform))

def validate_agent_installation(settings):
    """
    Check whether the agent is installed and at least the minimum version.
    """
    try:
        env = agent_env(settings)
        env["PENNSIEVE_LOG_LEVEL"] = "ERROR"  # Avoid spurious output with the version
        version = subprocess.check_output([agent_cmd(), "version"], env=env)
    except (AgentError, subprocess.CalledProcessError, EnvironmentError) as e:
        raise AgentError(
            "Agent not installed. Visit https://developer.pennsieve.io/agent for installation directions."
        )

    try:
        agent_version = semver.VersionInfo.parse(version.decode().strip())
    except ValueError as e:
        raise_from(AgentError("Invalid version string"), e)

    if agent_version < MINIMUM_AGENT_VERSION:
        raise AgentError(
            "Agent not compatible: found version {}, need version {}".format(
                agent_version, MINIMUM_AGENT_VERSION
            )
        )

    logger.info("Agent version %s found", agent_version)

def agent_env(settings):
    """
    Configure the agent environment to mirror the Python client
    The "local" environment looks for the host in PENNSIEVE_API_LOC
    (this is configured down in pennsieve-rust)
    """
    env = {
        "PENNSIEVE_API_ENVIRONMENT": "local",
        "PENNSIEVE_API_LOC": settings.api_host,
        "PENNSIEVE_API_TOKEN": settings.api_token,
        "PENNSIEVE_API_SECRET": settings.api_secret,
        "PENNSIEVE_LOG_LEVEL": get_log_level(),
    }
    if sys.platform in ["win32", "cygwin"]:
        env["SYSTEMROOT"] = os.getenv("SYSTEMROOT")
    # On Windows, the SYSTEMROOT environment variable must be preserved for DLLs to correctly load.
    # ref: https://travis-ci.community/t/socket-the-requested-service-provider-could-not-be-loaded-or-initialized/1127

    logger.debug("Agent environment: %s", env)

    return env

class AgentListener(object):
    """
    Context manager that starts the agent in listen server mode.
    """

    def __init__(self, settings, port):
        self.settings = settings
        self.port = port
        self.proc = None
        self.devnull = None
        warn(
            f"Pennsieve is transitioning to the new agent. This class '{self.__class__.__name__}' will be deprecated; version=7.0.0; date=2022-11-01.",
            DeprecationWarning,
            stacklevel=2,
        )

    def __enter__(self):
        check_port(self.port)
        command = [agent_cmd(), "upload-status", "--listen", "--port", str(self.port)]

        self.devnull = open(os.devnull, "w")

        self.proc = subprocess.Popen(
            command,
            env=agent_env(self.settings),
            stdout=sys.stdout if get_log_level() == "DEBUG" else self.devnull,
            stderr=sys.stderr if get_log_level() == "DEBUG" else self.devnull,
        )
        return self.proc

    def __exit__(self, *exc):
        self.proc.kill()
        self.devnull.close()

def check_port(port):
    """
    Refuse to start up if the agent is already running in listen mode.
    This can cause problems with relative files paths and session credentials.
    """
    try:
        logger.debug("Checking port %s", port)
        create_connection(socket_address(port)).close()
    except socket.error as e:
        if e.errno == errno.ECONNREFUSED:  # ConnectionRefusedError for Python 3
            logger.debug("No agent found, port %s OK", port)
            return True
        else:
            raise
    else:
        raise AgentError(
            "The agent is already running. Please stop any running processes and try again"
        )

def socket_address(port):
    if platform.system() == "Windows":
        return "ws://127.0.0.1:{}".format(port)
    return "ws://0.0.0.0:{}".format(port)

def create_agent_socket(port):
    """
    Open a websocket connection to the agent

    If the agent is not available, wait using exponential backoff for it to
    come up and start responding to messages
    """
    for i in range(-2, 4):
        try:
            return create_connection(socket_address(port))
        except socket.error as e:
            if e.errno == errno.ECONNREFUSED:  # ConnectionRefusedError for Python 3
                sleep_time = 2**i
                logger.debug("Connection refused - sleeping for %s seconds", sleep_time)
                sleep(sleep_time)
            else:
                raise

    raise AgentError("Could not connect to Agent")

def agent_upload(
    destination, files, dataset, append, recursive, display_progress, settings
):
    """
    Push an upload through the agent.
    """
    directory_upload = any(os.path.isdir(f) for f in files)

    if directory_upload and len(files) > 1:
        raise AgentError(
            "Can only upload a single directory.\n"
            'Please pass a single directory argument: `pkg.upload("/experiment/dir")`'
        )

    if recursive and not directory_upload:
        raise AgentError(
            "Recursive uploads are only allowed with directories.\n"
            "Upload a directory or pass `recursive=False`."
        )

    if recursive and append:
        raise AgentError("Cannot use `recursive=True` when appending`")

    # Figure out what files the agent is going to upload.
    # We cannot count on the agent to send "upload queued" messages for
    # all files before it starts uploading, so we generate the files we
    # plan to wait for.
    if directory_upload:
        directory = files[0]
        if recursive:
            expected_files = []
            for dirpath, _, filenames in os.walk(directory):
                for f in filenames:
                    expected_files.append(os.path.join(dirpath, f))
        else:
            expected_files = []
            for f in os.listdir(directory):
                path = os.path.join(directory, f)
                if os.path.isfile(path):
                    expected_files.append(path)
    else:
        expected_files = files

    # Agent uses absolute paths
    expected_files = [os.path.abspath(f) for f in expected_files]

    if isinstance(destination, Dataset):
        dataset_id = destination.id
        package_id = None
    elif isinstance(destination, (Collection, DataPackage)):
        dataset_id = dataset.id
        package_id = destination.id
    else:
        raise ValueError("Can only upload to a Dataset, Package, or Collection")

    with AgentListener(settings, DEFAULT_LISTEN_PORT):
        try:
            ws = create_agent_socket(DEFAULT_LISTEN_PORT)

            ws.send(
                json.dumps(
                    {
                        "message": "queue_upload",
                        "body": {
                            "dataset": dataset_id,
                            "package": package_id,
                            "files": files,
                            "append": append,
                            "recursive": recursive,
                        },
                    }
                )
            )

            upload_manager = UploadManager(expected_files, display_progress)
            upload_manager.print_progress()

            for msg in ws:
                msg = json.loads(msg)

                if msg["message"] == "file_queued_for_upload":
                    upload_manager.set_queued(msg["path"], msg["import_id"])

                elif msg["message"] == "upload_progress":
                    upload_manager.set_progress(
                        msg["path"], msg["import_id"], msg["percent_done"], msg["done"]
                    )

                elif msg["message"] == "upload_complete":
                    upload_manager.set_complete(msg["import_id"])

                elif msg["message"] == "upload_error":
                    logger.error(msg["context"])
                    upload_manager.set_error(msg["import_id"])

                elif msg["message"] == "error":
                    raise AgentError(msg["context"])

                else:
                    logger.debug("Unknown message", msg)

                upload_manager.print_progress()
                if upload_manager.done:
                    break

        finally:
            try:
                ws.close()
            except UnboundLocalError:
                pass

def remove_prefix(text, prefix):
    return text[text.startswith(prefix) and len(prefix) :]

class UploadManager(object):
    """
    Manager for file status and messages.

    This is complicated by that fact that the agent sends status information
    for files that are already in the queue or started by other processes.
    This makes it possible for the same file to be queued twice, so we
    have to track both the filename and import id. We only want to wait for
    all "our" files to upload.
    """

    def __init__(self, files, display_progress):
        # Should we show progress bars?
        self.display_progress = display_progress

        # map of filepath -> list(FileProgress)
        self.uploads = OrderedDict()

        for file in files:
            self.track_file(file, import_id=None, ours=True)

        # Keep track of whether progress bars have already been rendered so
        # we know if/what to erase when re-drawing
        self.lines_on_screen = 0
        warn(
            f"Pennsieve is transitioning to the new agent. This class '{self.__class__.__name__}' will be deprecated; version=7.0.0; date=2022-11-01.",
            DeprecationWarning,
            stacklevel=2,
        )

    def track_file(self, file, import_id, ours):
        progress = FileProgress(file, import_id, ours)
        if file in self.uploads:
            self.uploads[file].append(progress)
        else:
            self.uploads[file] = [progress]
        return progress

    def get_tracked_file(self, file, import_id):
        if sys.platform in ["win32", "cygwin"]:
            file = remove_prefix(
                file, "\\\\?\\"
            )  # windows OS sometimes prefix the filepath with \\?\, so we remove it if we see if
        if file in self.uploads:
            for p in self.uploads[file]:
                if p.import_id == import_id:
                    return p

    def all_tracked_files(self):
        for filegroup in self.uploads.values():
            for progress in filegroup:
                yield progress

    def set_queued(self, file, import_id):
        # Update the unqueued version of the file with an import id
        progress = self.get_tracked_file(file, None)

        # This import is queued from a different process - we don't care
        if progress is None:
            return

        progress.queued = True
        progress.import_id = import_id

    def set_progress(self, file, import_id, percent_done, done):
        # Absorb any files that are in the DB/already queued
        if self.get_tracked_file(file, import_id) is None:
            self.track_file(file, import_id, ours=False)

        progress = self.get_tracked_file(file, import_id)
        progress.percent_done = percent_done

    def set_complete(self, import_id):
        for progress in self.all_tracked_files():
            if progress.import_id == import_id:
                progress.done = True

    def set_error(self, import_id):
        for progress in self.all_tracked_files():
            if progress.import_id == import_id:
                progress.errored = True

    @property
    def done(self):
        return all([fstat.done for fstat in self.all_tracked_files() if fstat.ours])

    def print_progress(self, width=24):
        if not self.display_progress:
            return

        # move cursor to relative beginning
        sys.stdout.write("\033[F" * self.lines_on_screen)

        for fstat in self.all_tracked_files():
            if fstat.done:
                state = "DONE"
            elif fstat.errored:
                state = "ERRORED"
            elif fstat.queued:
                state = "UPLOADING"
            else:
                state = "WAITING"

            text = " [ {bars}{dashes} ] {state:12s} {percent:05.1f}% {name}\n".format(
                bars="#" * int(fstat.progress * width),
                dashes="-" * (width - int(fstat.progress * width)),
                percent=fstat.percent_done,
                name=fstat.name,
                state=state,
            )

            sys.stdout.write("{}\r".format(text))
            sys.stdout.flush()

        self.lines_on_screen = len(list(self.all_tracked_files()))

class FileProgress(object):
    def __init__(self, filename, import_id, ours):
        # We only care about the state of uploads started by this process
        self.ours = ours
        self.filename = filename
        self.import_id = import_id
        self.name = os.path.basename(filename)
        self._percent_done = 0
        self.done = False
        self.errored = False
        self.queued = False
        warn(
            f"Pennsieve is transitioning to the new agent. This class '{self.__class__.__name__}' will be deprecated; version=7.0.0; date=2022-11-01.",
            DeprecationWarning,
            stacklevel=2,
        )

    @property
    def percent_done(self):
        if self.done:
            return 100
        return self._percent_done

    @percent_done.setter
    def percent_done(self, value):
        # Only increment progress (in case messages come out of order)
        if value > self._percent_done:
            self._percent_done = value

    @property
    def progress(self):
        return self.percent_done / 100


# -*- coding: UTF-8 -*-
# https://leetcode-cn.com/problems/kth-largest-element-in-an-array/description/?utm_source=LCUS&utm_medium=ip_redirect&utm_campaign=transfer2china
import random
def findKthLargest(nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: int
    """
    k = len(nums)-k
    kth_index = quick_sort(nums,k,0,len(nums)-1)
    return nums[kth_index]

def quick_sort(nums,k,left,right):
    j = partition(nums,left,right)
    if j==k:
        return j
    elif j<k:
        return quick_sort(nums,k,j+1,right)
    elif j>k:
        return quick_sort(nums,k,left,j-1)

def partition(nums,left,right):
    random_index = random.randint(left,right)
    nums[left],nums[random_index] = nums[random_index],nums[left]
    pivot = nums[left]
    while left<right:
        while pivot<nums[right] and left<right:
            right-=1
        nums[left], nums[right] = nums[right], nums[left]
        while pivot>=nums[left] and left<right:
            left+=1
        nums[left],nums[right] = nums[right],nums[left]

    return left

nums = [3,2,1,5,6,4]
k = 2
print(findKthLargest(nums, k))


import tkinter as tk
import tkinter.messagebox as mb
import tkinter.ttk as ttk
from controller.divisions_controller import DivisionsController
from entity.divisions import Division
from docx import Document
import openpyxl

class DivisionApp(ttk.Frame):

    def __init__(self):
        super().__init__()
        self.div_ctrl = DivisionsController()
        self.lblTitle = tk.Label(self, text="Divisions",
                                 font=("Helvetica", 16), bg="yellow",
                                 fg="green")
        self.lblName = tk.Label(self, text="Enter Name:",
                                font=("Helvetica", 10),
                                bg="blue", fg="yellow")
        self.lblPersent = tk.Label(self, text="Enter persentage per irregular day:",
                                   font=("Helvetica", 10), bg="blue",
                                   fg="yellow")
        self.lblType = tk.Label(self, text="Enter type:",
                                font=("Helvetica", 10),
                                bg="blue", fg="yellow")
        self.lblSelect = tk.Label(self, text="Please select one record below to update or delete",
                                  font=("Helvetica", 10),
                                  bg="blue", fg="yellow")
        self.lblSearch = tk.Label(self, text="Please Enter Roll No:",
                                  font=("Helvetica", 10),
                                  bg="blue", fg="yellow")

        self.entName = tk.Entry(self)
        self.entPersent = tk.Entry(self)
        self.entType = tk.Entry(self)
        self.entSearch = tk.Entry(self)

        self.btn_xlsx = tk.Button(self, text="xlsx", font=("Helvetica", 11), bg="yellow", fg="blue",
                                  command=self.xlsx_export)
        self.btn_docx = tk.Button(self, text="docx", font=("Helvetica", 11), bg="yellow", fg="blue",
                                  command=self.docx_export)
        self.btn_register = tk.Button(self, text="Register", font=("Helvetica", 11), bg="yellow", fg="blue",
                                      command=self.register_division)
        self.btn_update = tk.Button(self, text="Update", font=("Helvetica", 11), bg="yellow", fg="blue",
                                    command=self.update_division)
        self.btn_delete = tk.Button(self, text="Delete", font=("Helvetica", 11), bg="yellow", fg="blue",
                                    command=self.delete_division)
        self.btn_clear = tk.Button(self, text="Clear", font=("Helvetica", 11), bg="yellow", fg="blue",
                                   command=self.clear_form)
        self.btn_show_all = tk.Button(self, text="Show All", font=("Helvetica", 11), bg="yellow", fg="blue",
                                      command=self.load_divisions)
        self.btn_search = tk.Button(self, text="Search", font=("Helvetica", 11), bg="yellow", fg="blue",
                                    command=self.show_search_record)
        self.btn_exit = tk.Button(self, text="Exit", font=("Helvetica", 16), bg="yellow", fg="blue", command=self.exit)

        columns = ("#1", "#2", "#3", "#4")
        self.tvDivision = ttk.Treeview(self, show="headings", height="5", columns=columns)
        self.tvDivision.heading('#1', text='RollNo', anchor='center')
        self.tvDivision.column('#1', width=60, anchor='center', stretch=False)
        self.tvDivision.heading('#2', text='Persentage', anchor='center')
        self.tvDivision.column('#2', width=10, anchor='center', stretch=True)
        self.tvDivision.heading('#3', text='Type', anchor='center')
        self.tvDivision.column('#3', width=10, anchor='center', stretch=True)
        self.tvDivision.heading('#4', text='Name', anchor='center')
        self.tvDivision.column('#4', width=10, anchor='center', stretch=False)

        vsb = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.tvDivision.yview)
        vsb.place(x=40 + 640 + 1, y=310, height=180 + 20)
        self.tvDivision.configure(yscroll=vsb.set)
        hsb = ttk.Scrollbar(self, orient=tk.HORIZONTAL, command=self.tvDivision.xview)
        hsb.place(x=40, y=310 + 200 + 1, width=620 + 20)
        self.tvDivision.configure(xscroll=hsb.set)
        self.tvDivision.bind("<<TreeviewSelect>>", self.show_selected_record)

        self.lblTitle.place(x=280, y=30, height=27, width=300)
        self.lblName.place(x=175, y=70, height=23, width=100)
        self.lblPersent.place(x=20, y=100, height=23, width=250)
        self.lblType.place(x=171, y=129, height=23, width=104)
        self.lblSelect.place(x=150, y=280, height=23, width=400)
        self.lblSearch.place(x=174, y=560, height=23, width=134)
        self.entName.place(x=277, y=72, height=21, width=186)
        self.entPersent.place(x=277, y=100, height=21, width=186)
        self.entType.place(x=277, y=129, height=21, width=186)
        self.entSearch.place(x=310, y=560, height=21, width=186)
        self.btn_xlsx.place(x=130, y=245, height=25, width=76)
        self.btn_docx.place(x=210, y=245, height=25, width=76)
        self.btn_register.place(x=290, y=245, height=25, width=76)
        self.btn_update.place(x=370, y=245, height=25, width=76)
        self.btn_delete.place(x=460, y=245, height=25, width=76)
        self.btn_clear.place(x=548, y=245, height=25, width=76)
        self.btn_show_all.place(x=630, y=245, height=25, width=76)
        self.btn_search.place(x=498, y=558, height=26, width=60)
        self.btn_exit.place(x=320, y=610, height=31, width=60)
        self.tvDivision.place(x=40, y=310, height=200, width=640)
        self.load_divisions()

    def clear_form(self):
        self.entName.delete(0, tk.END)
        self.entPersent.delete(0, tk.END)
        self.entType.delete(0, tk.END)

    def exit(self):
        MsgBox = mb.askquestion('Exit Application',
                                'Are you sure you want to exit the application',
                                icon='warning')
        if MsgBox == 'yes':
            self.destroy()

    def delete_division(self):
        MsgBox = mb.askquestion('Delete Record', 'Are you sure! you want to delete selected record',
                                icon='warning')

        if MsgBox == 'yes':
            self.div_ctrl.delete(roll_no)
            mb.showinfo("Information", "Record Deleted Succssfully")
            self.load_divisions()
            self.clear_form()

    def register_division(self):

        name = self.entName.get()  # Retrieving entered first name
        pers = self.entPersent.get()  # Retrieving entered last name
        type = self.entType.get()  # Retrieving entered contact number

        # validating Entry Widgets
        if name == "":
            mb.showinfo('Information', "Please Enter name")
            self.entName.focus_set()
            return
        if pers == "":
            mb.showinfo('Information', "Please Enter persent")
            self.entPersent.focus_set()
            return
        if type == "":
            mb.showinfo('Information', "Please Enter type")
            self.entType.focus_set()
            return
        # Inserting record
        try:
            self.div_ctrl.create(Division(name, pers, type))
            self.load_divisions()
        except Exception as err:
            print(err)

    def show_search_record(self):
        s_roll_no = self.entSearch.get()  # Retrieving entered first name
        print(s_roll_no)
        if s_roll_no == "":
            mb.showinfo('Information', "Please Enter Roll")
            self.entSearch.focus_set()
            return

        self.tvDivision.delete(*self.tvDivision.get_children())
        division = self.div_ctrl.get_by_id(s_roll_no)
        self.tvDivision.insert("", 'end', text="Division",
                               values=(division.id, division.name, division.persentage_one, division.type))

    def show_selected_record(self, event):
        self.clear_form()
        for selection in self.tvDivision.selection():
            item = self.tvDivision.item(selection)
        global roll_no
        roll_no, name, pers, type = item["values"][0:4]
        self.entName.insert(0, name)
        self.entPersent.insert(0, pers)
        self.entType.insert(0, type)
        return roll_no

    def update_division(self):
        name = self.entName.get()
        pers = self.entPersent.get()
        type = self.entType.get()
        print(roll_no)
        self.div_ctrl.update(roll_no, Division(name, pers, type))
        mb.showinfo("Info", "Selected  Record Updated Successfully ")
        self.load_divisions()

    def load_divisions(self):
        self.tvDivision.delete(*self.tvDivision.get_children())
        divisions = self.div_ctrl.all_divisions()
        for div in divisions:
            self.tvDivision.insert("", 'end', text="Divisions",
                                   values=(div.id, div.persentage_one, div.type, div.name))

    def xlsx_export(self):
        wb = openpyxl.load_workbook('C:\\Users\\karina\\PycharmProjects\\staffing\\reports\\All.xlsx')
        if 'Divisions' not in wb.sheetnames:
            wb.create_sheet('Divisions')
        ws = wb.get_sheet_by_name('Divisions')
        ws.delete_cols(1, 4)
        ws.delete_rows(1, 100)
        i = 1
        ws.cell(row=i, column=1).value = "ID"
        ws.cell(row=i, column=2).value = "Persent1"
        ws.cell(row=i, column=3).value = "Type"
        ws.cell(row=i, column=4).value = "Name"
        divisions = self.div_ctrl.all_divisions()
        for div in divisions:
            ws.cell(row=i + 1, column=1).value = div.id
            ws.cell(row=i + 1, column=2).value = div.persentage_one
            ws.cell(row=i + 1, column=3).value = div.type
            ws.cell(row=i + 1, column=4).value = div.name
            i += 1
        wb.save('C:\\Users\\karina\\PycharmProjects\\staffing\\reports\\All.xlsx')
        print("xlsx successful")

    def docx_export(self):
        document = Document()
        document.add_heading("Divisions", 0)
        table = document.add_table(rows=1, cols=4)
        hdr_cells = table.rows[0].cells
        hdr_cells[0].text = 'ID'
        hdr_cells[1].text = 'Name'
        hdr_cells[2].text = 'Persentage per irregular day'
        hdr_cells[3].text = 'Type'
        divisions = self.div_ctrl.all_divisions()
        for div in divisions:
            row_cells = table.add_row().cells
            row_cells[0].text = str(div.id)
            row_cells[1].text = div.name
            row_cells[2].text = str(div.persentage_one)
            row_cells[3].text = div.type
        document.save('C:\\Users\\karina\\PycharmProjects\\staffing\\reports\\Divisions.docx')
        print("docx successful")


if __name__=="__main__":
    import io
    import sys
    import os
    def fflip(line):
        if line.find(":")!=-1 : return True;
        if line.find("={")!=-1 : return True;
        if line.find("=")!=-1 and line.find("function")!=-1 : return True;
        if line.find("=")!=-1 and line.find("this")!=-1 : return True;
        return False

    if len(sys.argv)>0 :
        src= "basic_text.js"
    fi= open(src,"r")
    lines= fi.readlines();
    fi.close()
    defs= ["this file is make from ../get_js_decl.py \n"]
    defs= defs+["%04d"%(line_no+1)+"|"+i for line_no,i in zip(range(len(lines)),lines) if fflip(i)]
    os.makedirs("midfile",exist_ok=True)
    fo= open("midfile/decl."+src, "w")
    fo.writelines(defs)
    fo.close()
    #os.system("pause")


from network.base_model import ModelBase_ResNet18
from network.heads import (
    MoCoProjectionHead
)
import torch.nn.functional as F
import copy
from util.MemoryBankModule import MemoryBankModule
from util.utils import *
class SCE(nn.Module):
    def __init__(self, dim=256, K=4096, momentum=-1, tem=0.05, dataset='cifar10', bn_splits=8, symmetric=False):
        super(SCE, self).__init__()
        self.K = K
        self.momentum = momentum
        self.tem = tem
        self.coeff = 0.5
        self.symmetric = symmetric
        # create the encoders
        self.net               = ModelBase_ResNet18(dataset=dataset, bn_splits=bn_splits)
        self.backbone_momentum = copy.deepcopy(self.net)

        self.projection_head = MoCoProjectionHead(input_dim=512, hidden_dim=2048, output_dim=dim)
        self.projection_head_momentum = copy.deepcopy(self.projection_head)

        self.memory_bank = MemoryBankModule(size=self.K).cuda()

        deactivate_requires_grad(self.backbone_momentum)
        deactivate_requires_grad(self.projection_head_momentum)
        # self.max_entropy = np.log(self.K)

    def contrastive_loss(self, im_q, im_k, true_labels, update=False):

        # compute query features
        z_q = self.projection_head(self.net(im_q))  # queries: NxC

        with torch.no_grad():  # no gradient to keys
            # shuffle
            im_k_, shuffle = batch_shuffle(im_k)
            z_k = self.projection_head_momentum(self.backbone_momentum(im_k_))  # keys: NxC
            # undo shuffle
            z_k = batch_unshuffle(z_k, shuffle)

        # Nearest Neighbour,    queue: [feature_dim, self.K]
        _, bank, _ = self.memory_bank(output=z_k, labels=true_labels, update=update)
        # ================normalized==================
        z_q = nn.functional.normalize(z_q, dim=1)
        z_k = nn.functional.normalize(z_k, dim=1)
        bank = nn.functional.normalize(bank.t(), dim=1)
        # ================SCE==================
        batch_size = z_q.shape[0]

        # ================target similarity distribution==================
        one_hot_labels = torch.zeros(batch_size, dtype=torch.long).cuda()
        # [batch_size, 1+bank.shape[0]], except for the first column, which is 1, the rest is 0
        one_hot_labels = nn.functional.one_hot(one_hot_labels, 1 + bank.shape[0])

        sim_k_ktarget = torch.zeros(batch_size).unsqueeze(-1).cuda()
        sim_k_queue = torch.einsum("nc,mc->nm", z_k, bank).cuda()
        sim_k = torch.cat([sim_k_ktarget, sim_k_queue], dim=1).cuda()
        logits_k = sim_k / self.tem
        prob_k = nn.functional.softmax(logits_k, dim=1)
        w = nn.functional.normalize(self.coeff * one_hot_labels + (1 - self.coeff) * prob_k, p=1, dim=1)

        sim_q_ktarget = torch.einsum('nc,nc->n', [z_q, z_k]).unsqueeze(-1).cuda()
        sim_q_queue = torch.einsum("nc,mc->nm", z_q, bank).cuda()
        sim_q = torch.cat([sim_q_ktarget, sim_q_queue], dim=1)
        logits_q = sim_q / 0.1

        loss = -torch.sum(w * F.log_softmax(logits_q, dim=1), dim=1).mean(dim=0).cuda()
        return loss

    def forward(self, im1, im2, labels):
        """
        Input:
            im_q: a batch of query images
            im_k: a batch of key images
        Output:
            loss
        """
        # Updates parameters of `model_ema` with Exponential Moving Average of `model`
        update_momentum(model=self.net, model_ema=self.backbone_momentum, m=self.momentum)
        update_momentum(model=self.projection_head, model_ema=self.projection_head_momentum, m=self.momentum)

        if self.symmetric:  # symmetric loss
            loss_21 = self.contrastive_loss(im2, im1, update=False, true_labels=labels)

        loss_12 = self.contrastive_loss(im1, im2, update=True, true_labels=labels)

        loss = loss_12
        # compute loss
        if self.symmetric:  # symmetric loss
            # loss_21 = self.contrastive_loss(im2, im1, update=False, true_labels=labels)
            loss = (loss_12 + loss_21) * 1.0 / 2

        return loss


import requests

class Swapi():

    def __init__(self, url_swapi, redis_db_swapi, expire):
        self.url_swapi = url_swapi
        self.redis_db_swapi = redis_db_swapi
        self.expire = expire

    def get_qtd_planet_by_name(self):
        qtd_planet_film = self.check_cache_swapi(self.url_swapi)
        if qtd_planet_film:
            return qtd_planet_film
        else:
            swapi_request = requests.get(self.url_swapi, timeout=3)
            swapi_request.raise_for_status()
            swapi_response = swapi_request.json()
            qtd_planet_film = len(swapi_response['results'][0]['films'])

            # Save in redis
            self.redis_db_swapi.setex(
                self.url_swapi,
                self.expire,
                int(qtd_planet_film)
            )
            return qtd_planet_film

    def check_cache_swapi(self, url):
        get_redis_swapi = self.redis_db_swapi.get(url)
        if get_redis_swapi:
            swapi_load = eval(get_redis_swapi.decode(),
                              {'false': False, 'true': True, 'Null': None,
                               'null': None, '__builtins__': {}})
            return swapi_load
        return None


import os
import cubical
from setuptools import setup, find_packages

with open('README.md') as f:
    long_description = f.read()

try:
    import six
except ImportError:
    raise ImportError("Please install six before running install. If you're using pip 19 to install this package you should not be seeing this message.")

try:
    import numpy
except ImportError:
    raise ImportError("Please install numpy before running install. If you're using pip 19 to install this package you should not be seeing this message.")

# Check for readthedocs environment variable.

on_rtd = os.environ.get('READTHEDOCS') == 'True'

if on_rtd:
    requirements = ['numpy',
                    'matplotlib',
                    'scipy']
else:
    requirements = ['future',
                    'numpy',
                    'numba',
                    'python-casacore',
                    'sharedarray >= 3.2.1',
                    'matplotlib',
                    'scipy',
                    'astro-tigger-lsm',
                    'six',
                    'astropy>=3.0',
                    'psutil'
                    ]

setup(name='cubical',
      version=cubical.VERSION,
      description='Fast calibration implementation exploiting complex optimisation.',
      url='https://github.com/ratt-ru/CubiCal',
      classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Intended Audience :: Science/Research",
        "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
        "Operating System :: POSIX :: Linux",
        "Programming Language :: Python",
        "Topic :: Scientific/Engineering :: Astronomy"],
      author='Jonathan Kenyon',
      author_email='jonosken@gmail.com',
      license='GNU GPL v3',
      long_description=long_description,
      long_description_content_type='text/markdown',
      packages=find_packages(),
      python_requires=">=3.6",
      install_requires=requirements,
      include_package_data=True,
      zip_safe=False,
      scripts=['cubical/bin/print-cubical-stats',
               'cubical/bin/plot-leakage-solutions',
               'cubical/bin/plot-gain-solutions'],
      entry_points={'console_scripts': ['gocubical = cubical.main:main']},
      extras_require={
          'lsm-support': ['montblanc >= 0.6.4'],
          'degridder-support': ['ddfacet >= 0.6.1',
                                'regions < 0.5', # bug in new DS9 parser
                                'meqtrees-cattery >= 1.7.7']
      }
)


from Tkinter import *
import pandas as pd
from scipy.stats import mode
import tkFileDialog
import tkMessageBox
from decimal import *
from sklearn import metrics

class Analyser:
    train = None
    sructureDic = None
    numOfBins = None
    initDictionaryStruct = {}
    countRecotdsClass = {}
    struct = {}
    p = 0.0
    m = 2

#The function calculates the average and put the value at missind data
    def average(self, key):
        self.train[key].fillna(self.train[key].mean(), inplace=True)

#The function found the most common value and put the value at missind data
    def mostCommon(self, key):
        self.train[key].fillna(mode(self.train[key]).mode[0], inplace=True)

#The function handles missing numeric and category data
    def handleMissingData(self):
        for key, value in self.sructureDic.iteritems():
            if(value == "class"):
                continue
            if value == "NUMERIC":
                self.average(key)
            else:
                self.mostCommon(key)

#The function calculates equal width
    def EqualWidth(self, maxval, minval, cut_points):
        tempWidth = float(maxval) - float(minval)
        width =  float(tempWidth/ float(cut_points))
        i = 1
        binsArr = []
        while i < cut_points:
            binsArr.append(minval + (width * i))
            i += 1
        return binsArr

#The function devides to equal width
    def binning(self, col, cut_points, key):
        minval = col.min()
        maxval = col.max()
        binsArr = self.EqualWidth(maxval, minval, cut_points) #calculate bins
        labels = range(1, cut_points + 1)
        break_points = [minval] + binsArr + [maxval]
        self.initDictionaryStruct[key] = labels
        self.struct[key] = break_points
        colBin = pd.cut(col, bins=break_points, labels=labels, include_lowest=True)
        return colBin

#The function does discretization to the data set
    def Discretization(self):
        try:
            for key, value in self.sructureDic.iteritems(): #devide if numeric or categoty
                if value == "NUMERIC":
                    # print (key)
                    self.train[key] = self.binning(self.train[key], self.numOfBins, key)
                else:
                    valueList = str(value).replace("}", "").replace("{", "").split(",")
                    self.initDictionaryStruct[key] = valueList
                    self.struct[key] = valueList
        except:
            tkMessageBox.showerror("Naive Bayes Classifier", "the bins are not valid")

#The function calculates the probability of the attributes in category by the class
    def calculateProb(self, classValue, attribute, category):
        n = self.countRecotdsClass[classValue]
        countAtt = self.train[category] == attribute
        countClass = self.train['class'] == classValue
        count = self.train[countAtt & countClass]
        nc = len(count)
        result = (nc + (self.m * self.p)) / (n + self.m)
        return result

#The function calculates the probability of 'class' attribute
    def probClass(self):
        self.countRecotdsClass = self.train["class"].value_counts()

#The function creates class probability table for each class value
    def createClassProbTable(self, attribute, category):
        probDictionary = {}
        for classValue in self.initDictionaryStruct["class"]:
            prob = self.calculateProb(classValue, attribute, category)
            probDictionary[classValue] = prob
        return probDictionary

#The function create table category for each attribute
    def createTableCategory(self, attributesCategory, category):
        categoryTable = {}
        for attribute in attributesCategory:
            classProb = self.createClassProbTable(attribute, category)
            categoryTable[attribute] = classProb
        return categoryTable

#The function bulids table probability
    def buildTablesProb(self):
        bigTable = {}
        leng = len(str(self.sructureDic["class"]).replace("}", "").replace("{", "").split(","))
        self.p = float(float(1) / float(leng))
        for category, value in self.sructureDic.iteritems():
            if category == "class":
                continue
            attributesCategory = self.initDictionaryStruct[category] #take the attribue category
            categoryTable = self.createTableCategory(attributesCategory, category)
            bigTable[category] = categoryTable

        return bigTable

#The function builds the model
    def buildModel(self, sructureDic, train, numOfBins):
        self.train = train
        self.sructureDic = sructureDic
        self.numOfBins = numOfBins
        self.handleMissingData()
        self.Discretization()
        self.probClass()
        probTables = self.buildTablesProb()
        return probTables

class Classifier:
    test = None
    structIntervals = None
    structDic = None
    structLables = None

# The function does discretization to the test set
    def Discretization(self):
        for key, value in self.structDic.iteritems():
            if key == "class":
                continue
            if value == "NUMERIC":
                intervals = self.structIntervals[key]
                labels = self.structLables[key]
                self.test[key] = pd.cut(self.test[key], bins=intervals, labels=labels, include_lowest=True)

#The function writes to file the results
    def writeToFile(self, dirpath, classification):
        f = open(dirpath + "\\output.txt", "w")
        i = int(1)
        while i < len(classification):
            f.write(str(i) + " " + str(classification[int(i)]) + "\n") #write the number and the classification
            i = int(i) + int(1)
        f.close()

#The function calculates nult between probs
    def getMult(self, listProb):
        mult = Decimal(1)
        for prob in listProb:
            mult = Decimal(mult) * Decimal(prob)
        return mult

#The function gets the probability from the table
    def getProb(self, model, category, attribute, classAttribute):
        dictionary = model[category]
        classProb = dictionary[attribute]
        prob = classProb[classAttribute]
        return prob

#The function gets the classification of the row
    def getClassificationRow(self,probByVal):
        # print (probByVal)
        classification = None
        maxProb = 0
        for classVal, prob in probByVal.iteritems(): # for on probByVal
            if prob>maxProb:
                maxProb = prob
                classification = classVal
        return classification

#The function get the classification for the test
    def getClassification(self,model):
        classification = []
        for index, row in self.test.iterrows():
            probByVal = {}
            for classValue in self.structLables["class"]: #for on each class
                listProb  = []
                for category, value in self.structDic.iteritems():
                    if category =="class":
                         continue
                    attribute = row[category]
                    prob = self.getProb(model,category,attribute,classValue)
                    listProb.append(prob)
                probClassVal = self.getMult(listProb)
                probByVal[classValue] = probClassVal
            classificationRow = self.getClassificationRow(probByVal)
            classification.append(classificationRow)
        return classification

#The function classify the test by the model
    def classify(self, test, structIntervals, structDic, structLables, dirpath,model):
        self.test = test
        self.structIntervals = structIntervals
        self.structDic = structDic
        self.structLables = structLables
        self.Discretization()
        classification = self.getClassification(model)
        self.writeToFile(dirpath,classification)
        accurancy = metrics.accuracy_score(classification,test["class"])
        # print "Accuracy : %s" % "{0:.3%}".format(accurancy)
        tkMessageBox.showinfo("Naive Bayes Classifier", "classify is done!")
        exit()

class NaiveBayes:
    analyser = Analyser()
    classifier = Classifier()
    model = None

#The function give the user the search for the path
    def browse(self):
        dir = tkFileDialog.askdirectory()
        self.var.set(dir)

#The function load csv file
    def load_csv(self, filename):
        try:
            df = pd.read_csv(filename)
            return df
        except:
            tkMessageBox.showerror("Naive Bayes Classifier", "the path " + filename + " is not exist")

#The function load text file
    def load_txt(self, filename):
        try:
            d = {}
            with open(filename) as f:
                content = f.read().splitlines()
                for line in content:
                    arr = line.split(' ')
                    key = arr[1]
                    value = arr[2]
                    d[key] = value
            return d
        except:
            tkMessageBox.showerror("Naive Bayes Classifier", "the path " + filename + " is not exist") #print if the file is not exist

#The function build the model
    def build(self):
        dirPath = self.var.get()
        filename = dirPath + "\\Structure.txt"
        sructureDic = self.load_txt(filename)
        filename = dirPath + "\\train.csv"
        train = self.load_csv(filename)
        numOfBins = int(self.varBins.get())
        self.model = self.analyser.buildModel(sructureDic, train, numOfBins)
        tkMessageBox.showinfo("Naive Bayes Classifier", "Building classifier using train-set is done!")

#The function classify the test by the model
    def classify(self):
        dirPath = self.var.get()
        filename = dirPath + "\\test.csv"
        test = self.load_csv(filename)
        structIntervals = self.analyser.struct
        structDic = self.analyser.sructureDic
        structLabes = self.analyser.initDictionaryStruct
        self.classifier.classify(test, structIntervals, structDic, structLabes, dirPath,self.model)

#The function init the gui
    def __init__(self, master):
        self.master = master
        self.master.title("Naive Bayes Classifier")
        self.labelDir = Label(master, text="Directory Path")
        self.labelDisc = Label(text="Discretization Bins:")
        self.var = StringVar()
        dirname = Entry(master, textvariable=self.var)
        self.varBins = StringVar()
        bins = Entry(master, textvariable=self.varBins)
        self.browse_button = Button(master, text="Browse", command=lambda: self.browse()) #button browse
        self.build_button = Button(master, text="Build", command=lambda: self.build()) #button build
        self.classify_button = Button(master, text="Classify", command=lambda: self.classify())
        self.labelDir.grid(row=0, column=0, sticky=W)
        self.labelDisc.grid(row=1, column=0, sticky=E)
        dirname.grid(row=0, column=1, columnspan=2, sticky=W + E)
        bins.grid(row=1, column=1, columnspan=2, sticky=W + E)
        self.browse_button.grid(row=0, column=3)
        self.build_button.grid(row=2, column=1)
        self.classify_button.grid(row=3, column=1)

root = Tk()
my_gui = NaiveBayes(root)
root.mainloop()


from django.contrib import admin
from django.urls.base import reverse
from django.urls.exceptions import NoReverseMatch
from django_audit_fields.admin import audit_fieldset_tuple
from edc_model_admin.dashboard import ModelAdminSubjectDashboardMixin
from edc_model_admin.model_admin_simple_history import SimpleHistoryAdmin

from ..admin_site import inte_screening_admin
from ..forms import SubjectRefusalForm
from ..models import SubjectRefusal

@admin.register(SubjectRefusal, site=inte_screening_admin)
class SubjectRefusalAdmin(ModelAdminSubjectDashboardMixin, SimpleHistoryAdmin):
    form = SubjectRefusalForm

    post_url_on_delete_name = "screening_listboard_url"
    subject_listboard_url_name = "screening_listboard_url"
    subject_dashboard_url_name = "screening_listboard_url"

    fieldsets = (
        [
            None,
            {
                "fields": (
                    "screening_identifier",
                    "report_datetime",
                    "reason",
                    "other_reason",
                    "comment",
                )
            },
        ],
        audit_fieldset_tuple,
    )

    list_display = (
        "screening_identifier",
        "report_datetime",
        "reason",
        "user_created",
        "created",
    )

    list_filter = ("report_datetime", "reason")

    search_fields = ("screening_identifier",)

    radio_fields = {"reason": admin.VERTICAL}

    def get_subject_dashboard_url_kwargs(self, obj):
        return dict(screening_identifier=obj.screening_identifier)

    def view_on_site(self, obj):
        try:
            url = reverse(
                self.get_subject_dashboard_url_name(),
                kwargs=self.get_subject_dashboard_url_kwargs(obj),
            )
        except NoReverseMatch as e:
            if callable(super().view_on_site):
                url = super().view_on_site(obj)
            else:
                raise NoReverseMatch(f"{e}. See subject_dashboard_url_name for {repr(self)}.")
        return url


from sklearn.linear_model import Ridge
from sklearn.linear_model import LinearRegression
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
import torch
class Poly_linear_regression_grad():
    '''
    import matplotlib.pyplot as plt
    from sklearn.linear_model import Ridge
    from model.Ridge_shift_regression import Ridge_Shift
    from model.model import polynomial_regression
    from model.linear_regression_grad import Poly_linear_regression_grad
    import numpy as np
    scale = 1
    r_scale = 2
    shift = 0.2
    num = 5
    xs = [ele / scale - shift for ele in range(num)]
    ys = [np.exp(-ele ) + np.random.random() / r_scale for ele in xs]

    model = Poly_linear_regression_grad(n_order= 4 ,lr = 0.00001,  momentum = 0.99, epoch= 100000, verbal= False, lambda_ = 0.1)
    model.fit(np.array(xs).reshape(-1, 1), np.array(ys), range_ = [-0.1, 4.2] )
    pred_x = np.arange(-0.1, 4.2, 0.1)
    pred_y = model.predict(pred_x.reshape(-1, 1))
    plt.scatter(xs, ys)
    plt.plot(pred_x, pred_y)
    plt.show()
    '''
    def  __init__(self, n_order, lr = 0.0001, momentum = 0.8, epoch = 1000, verbal = False, lambda_ = 0.01 ):
        """
        Example code
        n_samples, n_features = 3, 2
        """
        self.n_order = n_order
        self.lr = lr
        self.momentum = momentum
        self.epoch = epoch
        self.verbal = verbal
        self.lambda_ = lambda_
    def fit(self, x, y, range_ = None, direction = 'decreasing'):
        '''
        x : numpy : N_len x m_feature,
        y : numpy : N_len

        '''
        self._range = range_
        self._direction = direction
        self.poly = PolynomialFeatures(degree = self.n_order)
        x_trans = self.poly.fit_transform(x)
        self.n_features = x_trans.shape[1]
        LR =  LinearRegression(fit_intercept=False)
        LR.fit(x_trans, y)

        coef = np.array( LR.coef_.tolist()) / 1000#+ 1
        self.coef_ = torch.from_numpy(coef)
        self.coef_.requires_grad = True
        self.x = torch.from_numpy(x_trans)
        self.y = torch.from_numpy(y)

        # preparing for monotonic loss
        if self._range != None:
            left, right, step = self._range[0], self._range[1], (self._range[1] - self._range[0] ) / 100
            x_range = np.arange(left, right, step).reshape(-1, 1)
            poly__ = PolynomialFeatures(degree = self.n_order - 1)
            self.x_trans__ = poly__.fit_transform(x_range)
            self.x__ = torch.from_numpy(self.x_trans__)

        self.op = torch.optim.SGD([self.coef_], lr=self.lr, momentum = self.momentum)
        self.train(self.epoch)

    def train(self, n_epoch = 1000):
        for _ in range(n_epoch):
            # acc loss
            predict_y = self.x @ self.coef_.T
            loss1 =  torch.sum((predict_y - self.y) ** 2)

            if self._range != None:
                # breakpoint()
                f_derivative = self.x__ @ ( self.coef_[1:] * ( torch.arange(self.n_features - 1 ) + 1 ) )
                if self._direction == 'decreasing':
                    loss2 = torch.sum(f_derivative[f_derivative > 0 ] ** 2)
                else:
                    loss2 = torch.sum(-f_derivative[f_derivative < 0 ]** 2)
                # print('f_derivative', f_derivative)
            else:
                loss2 = 0
            # loss2 = loss1 * 0
            loss = loss1 + self.lambda_ *loss2
            self.op.zero_grad()
            loss.backward()
            # loss1.backward()
            self.op.step()

            if self.verbal and _%10 == 0 :
                print('loss: ', loss.item(), ' loss1: ', loss1.item(), ' loss2: ', loss2.item(), 'self.coef_', self.coef_.detach().numpy())
                # print('predict_y', predict_y)
                # print('self.y', self.y)
            # breakpoint()
        # loss2 =
        print('final training loss', loss1)
        # print('predict_y', predict_y)
        # print('self.y', self.y)
        # breakpoint()
    def predict(self, x):
        x_trans = self.poly.fit_transform(x)
        pred = x_trans @ self.coef_.detach().numpy()
        return pred

if __name__ == '__main__':
    PLRG = Poly_linear_regression_grad(3)
    rng = np.random.RandomState(0)
    n_samples = 3
    n_features = 1
    y = rng.randn(n_samples)
    X = rng.randn(n_samples, n_features)
    PLRG.fit(X,y)
    print(' PLRG.pred(X)',  PLRG.predict(X))
    breakpoint()


T = int(input())
for test_case in range(1,T + 1):
    N = int(input())
    sum = 0
    for i in range(1,N+1):
        if i%2 == 0:
            sum -= i
        elif i%2 == 1:
            sum += i
    print("#{} {}".format(test_case, sum))


from torch.utils.data.dataloader import DataLoader
from dataset import DataSet
from network import NetA, SubCNNA, NetA4
from torch import optim
from torch import nn, save, load, Tensor
from collections import Counter
from util import calculate_mcc
import time

def train(epoch, batch_size=128, lr=0.00001):
    model.cuda()
    model.train()
    optimizer = optim.AdamW(model.parameters(), lr=lr)
    lossfn = nn.MSELoss()
    print("=========Epoch-{}==========[lr:{}]".format(epoch, lr))
    dataset = DataSet("train_data_a.json", True)
    train_data = DataLoader(dataset, batch_size=batch_size, shuffle=True)
    tloss = 0
    avg_loss = 0
    start_time = time.time()
    for i, (left, right, label, angle) in enumerate(train_data):
        optimizer.zero_grad()
        left = left.cuda()
        right = right.cuda()
        label = label.cuda()
        angle = angle.cuda()
        preds = model(left, right, angle)
        loss = lossfn(preds, label)
        loss.backward()
        optimizer.step()
        tloss += loss
        avg_loss = round(float(tloss / i), 3)
        curtime = time.time()
        eta = (curtime - start_time) / (i + 1) * (len(train_data) - i)
        print('\r', "{}/{} current loss {}, avg loss {}, ETA {}s."
              .format(i * batch_size, len(dataset), loss, avg_loss, round(eta, 2)), end="")
    print("\nEpoch {} has finish. Epoch loss is {}".format(epoch, avg_loss))

def testCHK(**kwargs):
    chkname = kwargs.get("chkname")
    epoch = kwargs.get("epoch")
    model.eval()
    model.cuda()
    if chkname is not None:
        fchk = open(chkname, 'rb')
        model.load_state_dict(load(fchk))
        print("=========Test-{}==========".format(chkname))
    else:
        print("=========Test-{}==========".format(epoch))
    test_data = DataLoader(DataSet("test_data_a.json"), batch_size=128, shuffle=False)
    res = []
    corr = []
    for i, (left, right, label, angle) in enumerate(test_data):
        left = left.cuda()
        right = right.cuda()
        label = label.cuda()
        angle = angle.cuda()
        pred: Tensor = model(left, right, angle)
        res.extend(pred.tolist())
        corr.extend(label.tolist())
    print(res[2])
    res = list(map(softmax_convert, res))
    corr = list(map(softmax_convert, corr))
    tp, tn, fp, fn = 0, 0, 0, 0
    for x in range(len(res)):
        if res[x] == corr[x]:
            if corr[x] == [0, 1]:
                tp += 1
            if corr[x] == [1, 0]:
                tn += 1
        elif res[x] != corr[x]:
            if corr[x] == [0, 1]:
                fn += 1
            if corr[x] == [1, 0]:
                fp += 1
    print("labels: " + str(corr[:10]))
    print("result: {}".format(res[:10]))
    print(Counter(map(tuple, corr)))
    print(Counter(map(tuple, res)))
    print("tp:{},tn:{},fp:{},fn:{}".format(tp, tn, fp, fn))
    print("Accuracy:{}/{}".format(tp + tn, tp + tn + fn + fp))
    print("Precision:{}/{}".format(tp, tp + fp))
    print("Recall:{}/{}".format(tp, tp + fn))
    print("MCC:{}".format(calculate_mcc(tp, fp, fn, tn)))

def softmax_convert(tar):
    if tar[0] == max(tar):
        return [1, 0]
    else:
        return [0, 1]

def sigmod_convent(tar):
    if tar[0] >= 0:
        return 1
    else:
        return 0

if __name__ == '__main__':
    model = NetA(SubCNNA)
    model.cuda()
    init_lr = 0.0001
    # for e in range(1, 82):
    #     if e > 1 and (e - 1) % 20 == 0:
    #         init_lr *= 0.5
    #     train(e, lr=init_lr, batch_size=256)
    #     save(model.state_dict(), open("{}a_epcoh-checkpoint.chk".format(e), 'wb'))
    #     testCHK(epoch=e)
    testCHK(chkname="62a_epcoh-checkpoint.chk")


# -*- coding: utf-8 -*-
# Función para generar las keys de cada iteración, generadas por transposición.

# Genera la función de transposicion, segun la key ingresada.
# IN -> key: es la llave ingresada por el usuario.
# OUT -> swapKey: función de transposicion.
def keySeed(key):
	orderKey = sorted(key)
	swapKey = []

	for k in key:
		ind = orderKey.index(k)
		swapKey.append(ind)
		orderKey[ind] = None

	return swapKey

# Realiza la transposición de cada keyself.
# IN -> actKey: key actual a transponer.
# IN -> swapKey: función de transposición.
# OUT -> newKey: la nueva key despues de la transposición.
def swap(actKey, swapKey):
	newKey = ""
	for i in swapKey:
		newKey += actKey[i]

	return newKey

# Genera las llaves de cada iteración
# IN -> key: llave ingresada por el usuario
# IN -> it: cantidad de iteraciones, keys a generar.
# OUT -> keys: lista de keys.
def keyGenerator(key, it):
	keys = []
	swapKey = keySeed(key)
	# print(swapKey)

	for i in range(it):
		keys.append(key)
		key = swap(key, swapKey)

	return keys

# MAIN
# msg = "camilo"
# key = "camilo"
#
# keyGen = keyGenerator(key, 4)
# print(keyGen)
#
# # Ejemplo de como evaluar cada caso
# for i in range(1):
# 	for m, k in zip(msg, keyGen[i]):
# 		vm = ord(m) # Valor ASCII de la letre en el mensaje
# 		vk = ord(k) # Valor ASCII de la letre en la llave
# 		nv = (vm + vk) % 256
# 		nc = chr(nv) # Nuevo caracter ASCII, de reemplazo
# 		print('{0:1} {1:1} {2:3d} {3:3d} {4:3d} {5:1}'.format(m, k, vm, vk, nv, nc))


def issosu(n):
    if n<2:
        return False
    if not n%2:
        return False
    for i in range(3, n//2+1):
        if not n%i:
            return False
    return True
def sosulist(n):
    return [a for a in range(2, n+1) if issosu(a)]
def divisor(n):
    rtn = []
    for a in range(1, n//2+1):
        if not n%a:
            rtn.append(a)
    rtn.append(n)
    return rtn


import time
import re
from cumulusci.robotframework.utils import capture_screenshot_on_error
from cumulusci.robotframework.pageobjects import BasePage
from cumulusci.robotframework.pageobjects import pageobject
from BaseObjects import BaseNPSPPage
from NPSP import npsp_lex_locators
from logging import exception

OID_REGEX = r"^(%2F)?([a-zA-Z0-9]{15,18})$"
@pageobject("Custom", "GE_Gift_Entry")
class GiftEntryPage(BaseNPSPPage, BasePage):

    def _go_to_page(self):
        """To go to Gift Entry page"""
        url_template = "{root}/lightning/n/{object}"
        name = self._object_name
        object_name = "{}{}".format(self.cumulusci.get_namespace_prefix(), name)
        url = url_template.format(root=self.cumulusci.org.lightning_base_url, object=object_name)
        self.selenium.go_to(url)
        self.salesforce.wait_until_loading_is_complete()
        self.selenium.wait_until_page_contains("Templates")

    def _is_current_page(self):
        """
        Verifies that current page is Gift Entry landing page
        """
        self.selenium.wait_until_location_contains("GE_Gift_Entry", timeout=60,
                                                   message="Current page is not Gift Entry landing page")
        self.selenium.wait_until_page_contains("Default Gift Entry Template")

    def click_gift_entry_button(self,title):
        """clicks on Gift Entry button identified with title"""
        locator=npsp_lex_locators["gift_entry"]["button"].format(title)
        self.selenium.wait_until_page_contains_element(locator)
        self.selenium.click_element(locator)

    def enter_value_in_field(self,**kwargs):
        """Enter value in specified field"""
        for key,value in kwargs.items():
            if key=='Description':
                locator=npsp_lex_locators["gift_entry"]["field_input"].format(key,"textarea")
                self.selenium.wait_until_page_contains_element(locator)
                self.salesforce._populate_field(locator, value)
            else:
                locator=npsp_lex_locators["gift_entry"]["field_input"].format(key,"input")
                self.selenium.wait_until_page_contains_element(locator)
                self.salesforce._populate_field(locator, value)

    def select_template_action(self,name,action):
        """From the template table, select template with name and select an action from the dropdown"""
        locator=npsp_lex_locators["gift_entry"]["actions_dropdown"].format(name)
        self.selenium.click_element(locator)
        element=self.selenium.get_webelement(locator)
        status=element.get_attribute("aria-expanded")
        if status=="false":
            self.selenium.wait_until_page_contains("Clone")
        self.selenium.click_link(action)
        if action=="Edit" or action=="Clone":
            self.selenium.wait_until_page_contains("Gift Entry Template Information")
        elif action=="Delete":
            self.selenium.wait_until_page_does_not_contain(name)

    def select_object_group_field(self,object_group,field):
        """Select the specified field under specified object group
           to add the field to gift entry form and verify field is added"""
        locator=npsp_lex_locators["gift_entry"]["form_object_dropdown"].format(object_group)
        self.selenium.scroll_element_into_view(locator)
        self.selenium.click_element(locator)
        element=self.selenium.get_webelement(locator)
        status=element.get_attribute("aria-expanded")
        if status=="false":
            time.sleep(2)
        field_checkbox=npsp_lex_locators["gift_entry"]["object_field_checkbox"].format(field)
        self.selenium.scroll_element_into_view(field_checkbox)
        self.selenium.click_element(field_checkbox)
        field_label=object_group+': '+field
        self.selenium.wait_until_page_contains(field_label)

    def verify_template_is_not_available(self,template):
        """Verify that a gift template is not available for selection while creating a new batch"""
        field=npsp_lex_locators["adv_mappings"]["field_mapping"].format("Template")
        self.selenium.click_element(field)
        element=self.selenium.get_webelement(field)
        status=element.get_attribute("aria-activedescendant")
        if status is not None:
            self.selenium.page_should_not_contain(template)
        else:
            self.selenium.wait_until_page_contains("Default Gift Entry Template")
            self.selenium.page_should_not_contain(template)
        self.selenium.click_button("Cancel")

    def get_template_record_id(self,template):
        """ Parses the current url to get the object id of the current record.
            Expects url format like: [a-zA-Z0-9]{15,18}
        """
        locator=npsp_lex_locators["link-text"].format(template)
        element = self.selenium.get_webelement(locator)
        e=element.get_attribute("href")
        print(f"url is {e}")
        for part in e.split("="):
            oid_match = re.match(OID_REGEX, part)
            if oid_match is not None:
                return oid_match.group(2)
        raise AssertionError("Could not parse record id from url: {}".format(e))

    def store_template_record_id(self,template):
        """ Parses the template href to get the object id of the current record.
            Expects url format like: [a-zA-Z0-9]{15,18}
        """
        id=self.get_template_record_id(template)
        self.salesforce.store_session_record("Form_Template__c",id)


import pickle
import matplotlib.pyplot as pyplot
import seaborn
from matplotlib.path import Path
import matplotlib.patches as patches
from random import shuffle
from random import randrange
from matplotlib import gridspec
import pandas

def get_verts( origin, target, test=False ):
    origin = float( origin )
    target = float( target )
    if test:
        return  [
            ( 0.0, origin ), # MOVETO
            ( 0.3, origin ), # LINETO
            ( 0.4, origin ), # CURVE3
            ( 0.5, origin + (target-origin)/2 ), # MOVETO
            ( 0.6, target ), # CURVE3
            ( 0.7, target ), # LINETO
            ( 1.0, target )  # LINETO
        ]
    else:
        return  [
            ( 0.0, origin ), # MOVETO
            ( 0.1, origin ), # LINETO
            ( 0.2, origin ), # CURVE3
            ( 0.5, origin + (target-origin)/2 ), # MOVETO
            ( 0.8, target ), # CURVE3
            ( 0.9, target ), # LINETO
            ( 1.0, target )  # LINETO
        ]

def plot_ml_vs_idx( save_output=True, test=False ):

    with open( 'results/tmp-datafram-b.pickle', 'rb' ) as pickle_file:
         data_frame = pickle.load( pickle_file )

    # We want to create a graph that contains a comparison of predictions
    # by machine learning (ml) and ik-index. Two outer 'panes' will have
    # the same comparison of ml prediction vs ik-index but in different orders,
    # one sorted by ml prediction and one sorted by ik-index.
    # The middle pane will hold the text labels, twice in those different orders.
    # bezier curves will connect the labels with the same text names.

    # The actual labels, data, and colors for the left pane (0)
    # and the right pane (2) that we'll be working with
    data_frame[ 'bezier_index' ] = range( len( data_frame ) )
    data_frame2 = data_frame.sort_values( [ 'ik-index' ], ascending=False )
    X = data_frame.index.values
    Y_ik_index = data_frame[ 'ik-index' ]
    Y_ml_index = data_frame[ 'ml-index' ]
    ik_color = data_frame[ 'ik-color' ]
    ml_color = data_frame[ 'ml-color' ]
    X2 = data_frame2.index.values
    Y_ik_index2 = data_frame2[ 'ik-index' ]
    Y_ml_index2 = data_frame2[ 'ml-index' ]
    ik_color2 = data_frame2[ 'ik-color' ]
    ml_color2 = data_frame2[ 'ml-color' ]
    if test:
        X = X[300:330]
        Y_ik_index = Y_ik_index[300:330]
        Y_ml_index = Y_ml_index[300:330]
        ik_color = data_frame[ 'ik-color' ][300:330]
        ml_color = data_frame[ 'ml-color' ][300:330]
        X2 = X.copy()
        Y_ik_index2 = Y_ik_index.copy()
        Y_ml_index2 = Y_ml_index.copy()
        ik_color2 = ik_color.copy()
        ml_color2 = ml_color.copy()
        shuffle( Y_ik_index2 )
        shuffle( Y_ml_index2 )
        shuffle( ik_color2 )
        shuffle( ml_color2 )

    # Accept some pretty styling out of the box
    seaborn.set()
    seaborn.set_palette( seaborn.color_palette( 'coolwarm' ) )

    # Create the figure as a whole, set dimensions
    fig_height = 500
    fig_width = 100
    ratios = [ 0.2, 0.6, 0.2 ]
    if test:
        fig_height = 10
        fig_width = 40
        ratios = [ 0.3, 0.4, 0.3 ]
    figure = pyplot.figure( figsize=( fig_width, fig_height ) )

    # Set up a grid, we want a wider middle 'pane' to display the text labels
    # and the bezier connectors
    grid_spec = gridspec.GridSpec( 1, 3, width_ratios=ratios )

    # We don't need space between the subplots
    pyplot.subplots_adjust( wspace=0 )

    # # These are just temporary aids to see if suboplots align exactly
    # pyplot.rcParams["axes.edgecolor"] = '0.15'
    # pyplot.rcParams["axes.linewidth"] = 1.25

    # a1 is the middle pane with text labels and connectors
    a1 = pyplot.subplot( grid_spec[1] )
    # we hadrly need a gridded background or color for the middle pane,
    # but still a hint of color is nice, so…
    a1.patch.set_alpha( 0.4 )
    a1.barh( X, [0] * len( X ), color=data_frame['ik-color'], height=0.5 )
    a1.set_xlim( left=0.0, right=1.0 )
    if not test:
        a1.set_ylim( bottom=-1.0, top=1002 )
    # we need no ticks, major or minor, nor labels in the mid pane
    a1.tick_params( axis='x', which='both', bottom=False, labelbottom=False )

    # Add in Bezier magics
    origins = data_frame[ 'bezier_index' ].tolist()
    targets = data_frame2[ 'bezier_index' ].tolist()
    codes = [
        Path.MOVETO,
        Path.LINETO,
        Path.CURVE3,
        Path.MOVETO,
        Path.CURVE3,
        Path.LINETO,
        Path.LINETO
    ]
    if test:
        origins = list( range( 30 ) )
        targets = list( range( 30 ) )
        # a bit of shifting to create interesting beziers
        for i in range(10):
            targets.append( targets.pop( randrange(30) ) )
            beziers = list( zip( origins, targets ) )
        for bezier in beziers:
            path = Path( get_verts( bezier[0], bezier[1], test ), codes )
            patch = patches.PathPatch( path, facecolor='none', lw=14, edgecolor='midnightblue', alpha=0.1 )
            a1.add_patch( patch )
    else:
        for origin in origins:
            target = targets.index( origin )
            path = Path( get_verts( origin, target ), codes )
            patch = patches.PathPatch( path, facecolor='none', lw=2, edgecolor='midnightblue', alpha=0.2 )
            a1.add_patch( patch )

    # a0 is the left pane
    a0 = pyplot.subplot( grid_spec[0] )
    # make sure this thing is printed on top of the bezier connectors stuff
    a0.set_zorder( 100 )
    a0.barh( X, Y_ik_index, color=ik_color, height=0.5 )
    a0.barh( X, -Y_ml_index, color=ml_color, height=0.5 )
    # a0.tick_params( labelleft=False )
    # Make the x axis show ticks from 1.0 to 0.0 to 1.0
    # (for the mirrored hbar chart).
    a0.set_xlim( right=1.05, left=-1.05 )
    if not test:
        a0.set_ylim( bottom=-1.0, top=1002 )
    ticks = a0.get_xticks()
    a0.set_xticklabels( [ abs( tick ) for tick in ticks ] )
    # We have to mess around with labels here, as they need to be on the (non
    # default) right side..
    a0.yaxis.set_label_position( 'right' )
    a0.yaxis.tick_right()
    a0.tick_params( axis='y', which='both', right=False )
    a0.axvline( 0, linewidth=1, color='darkorchid' )

    # a2 is the right pane
    a2 = pyplot.subplot( grid_spec[2] )
    # make sure this thing is printed on top of the bezier connectors stuff
    a2.set_zorder( 101 )
    a2.barh( X2, -Y_ik_index2, color=ik_color2, height=0.5 )
    a2.barh( X2, Y_ml_index2, color=ml_color2, height=0.5 )
    # axes.set_ylim( bottom=-1.0, top=1002 )
    # Make the x axis show ticks from 1.0 to 0.0 to 1.0
    # (for th mirrored hbar chart).
    a2.set_xlim( right=1.05, left=-1.05 )
    if not test:
        a2.set_ylim( bottom=-1.0, top=1002 )
    ticks = a2.get_xticks()
    a2.set_xticklabels( [ abs( tick ) for tick in ticks ] )
    a2.axvline( 0, linewidth=1, color='darkorchid' )
    # We don't have to mess around with labels here, as it comes with the right
    # labels and positioning on the left out of the box (obviously).

    pyplot.figure(1).savefig( 'results/ml_vs_index_spike.png', dpi=95, bbox_inches='tight' )
    # pyplot.show()

# main

plot_ml_vs_idx( save_output=False, test=True )


import torch

# Training configuration
MODEL = ""

BATCH_SIZE = 128

LRATE = 1e-4

NUM_EPOCHS = 50

WEIGHT_DECAY = 0

TRAIN_SIZE = 33600

VAL_SIZE = 8400

NUM_FEATURES = 30
# Classifier configuration
TRAIN_PROPORTION = 0.9999

NUM_NEIGHBORS = 3

NUM_ESTIMATORS = 101

# General configuration
TRAIN_FILE = "../data/train.csv"

TEST_FILE = "../data/test.csv"

SUBMISSION_FILE = "result/submission.csv"

EVAL_FILE = "../eval.xlsx"

MODEL_STATE_DICT_FILE = "result/model.pt"

DTYPE = torch.float32

# Best
# n_neighbors = 5, num_features = 200


import gym
gym.logger.set_level(40)  # nopep8
from gym.envs.registration import registry
from eagent.gym_evolving_locomotion_envs import EvolvingWalkerEnv  # noqa
from eagent.gym_evolving_manipulate_envs import EvolvingHandEnv  # noqa

def register(id, *args, **kvargs):
    if id in registry.env_specs:
        return
    else:
        return gym.envs.registration.register(id, *args, **kvargs)

# ----------------------------------
# Walking task
register(
    id="EvolvingWalkerEnv-v2",
    entry_point="eagent:EvolvingWalkerEnv",
    max_episode_steps=1000,  # Should be matched with num_steps_in_eval
    reward_threshold=99999.0,  # This might not be needed
)

# ----------------------------------
# EvolvingBlock
register(
    id="EvolvingHandBlockDense-v1",
    entry_point="eagent:EvolvingHandEnv",
    max_episode_steps=100,
    kwargs={
        "target_position": "fixed_random",
        "target_rotation": "xyz",
        "reward_type": "dense",
        "object_type": "block",
    },
)
register(
    id="EvolvingHandBlockSparse-v1",
    entry_point="eagent:EvolvingHandEnv",
    max_episode_steps=100,
    kwargs={
        "target_position": "fixed_random",
        "target_rotation": "xyz",
        "reward_type": "sparse",
        "object_type": "block",
    },
)
register(
    id="EvolvingHandBlockRotateZDense-v1",
    entry_point="eagent:EvolvingHandEnv",
    max_episode_steps=100,
    kwargs={
        "target_position": "ignore",
        "target_rotation": "z",
        "reward_type": "dense",
        "object_type": "block",
    },
)
register(
    id="EvolvingHandBlockRotateZSparse-v1",
    entry_point="eagent:EvolvingHandEnv",
    max_episode_steps=100,
    kwargs={
        "target_position": "ignore",
        "target_rotation": "z",
        "reward_type": "sparse",
        "object_type": "block",
    },
)

# ----------------------------------
# EvolvingEgg
register(
    id="EvolvingHandEggRotateZSparse-v1",
    entry_point="eagent:EvolvingHandEnv",
    max_episode_steps=100,
    kwargs={
        "target_position": "ignore",
        "target_rotation": "z",
        "reward_type": "sparse",
        "object_type": "egg",
    },
)
register(
    id="EvolvingHandEggRotateSparse-v1",
    entry_point="eagent:EvolvingHandEnv",
    max_episode_steps=100,
    kwargs={
        "target_position": "ignore",
        "target_rotation": "xyz",
        "reward_type": "sparse",
        "object_type": "egg",
    },
)


# importing the required libraries
import csv
import json

# defining the function to convert CSV file to JSON file
def convjson(csvFilename, jsonFilename):

    # creating a dictionary
    mydata = {}

    # reading the data from CSV file
    with open(csvFilename, encoding = 'utf-8') as csvfile:
        csvRead = csv.reader(csvfile, delimiter='~')

        # Converting rows into dictionary and adding it to data
        for rows in csvRead:
            print(rows)

    # dumping the data
    with open(jsonFilename, 'w', encoding = 'utf-8') as jsonfile:
        jsonfile.write(json.dumps(mydata, indent = 4))

# filenames
csvFilename = r'dataframe1.csv'
jsonFilename = r'dataframe1.json'

# Calling the convjson function
convjson(csvFilename, jsonFilename)

Диплом/Дипломный проект/Генерация базы для модели/Convert_CSV_to_json.py


"""
    select all Texture Nodes in the scene

"""

import ix

item_List = ix.api.OfObjectArray() # Make empty object list

ix.application.get_factory().get_all_objects("ProjectItem", item_List) # assign all scene nodes to item_List

selection = []

for i in range(item_List.get_count()):

    """
        filter all items in the scene , add just "TextureStreamedMapFile,TextureMapFile,TextureMap" to selection list

    """

    if item_List[i].is_kindof("TextureStreamedMapFile") == True or item_List[i].is_kindof("TextureMapFile") or item_List[i].is_kindof("TextureMap"):

        """
            Check if the node is Texture Node

        """

        selection.append(item_List[i]) # append to selection list

ix.application.get_selection().set_selection('global','Global',selection[0] ) # select first item in selection list

for sel in range(len(selection)-1):

    """
    range(len(selection)-1) to extract first item in the selection list that already selected and append the rest of items to this one

    """

    ix.application.get_selection().add_item('global',selection[sel+1],'Global') # select all items


# coding: utf8
# try something like
def index():

    form = SQLFORM.factory(
        Field("descripcion", "string", default="Barrilete multicolor"),
        Field("cantidad", "integer", default=1),
        Field("precio", "float", default=1.00),
        )

    if form.accepts(request.vars, session):
        preference_data = {
            "items": [
               {
                   "title": form.vars.descripcion,
                   "quantity": int(form.vars.cantidad),
                   "currency_id": "ARS",
                   "unit_price": float(form.vars.precio),
               }
            ]
        }

        preference = mp.create_preference(preference_data)

        #return str(preference)
        return """<!DOCTYPE html>

            <title>Pagar</title>

            Pagar

    """ % preference['response']['init_point']
    else:
        response.view = "generic.html"
        return {'form': form}