## -> JSON(JavaScript Object Notation)
## -> It is mainly used for storing and transferring data between the browser and the server.
## -> JSON is test, written with JavaScript Object Notation
## -> Python to Supports JSON with a built-in package called json.

## JSON_Supports_maily_6_datatypes:
    # String
    # Number
    # Boolean
    # Null
    # Object
    # Array

import json
p = "{'name':'WS', 'lang':['Python', 'Java']}"
blog = {"URL": 'www.wscubetech', 'name':'wscubetech'}
to_json = json.dumps(blog)

d = {"cousre_name":'Python', "Fees":12000}
f = json.dumps(d)
print(f)
print(type(f))


import numpy as np
from tqdm import tqdm

import Dynamics.dynamics as dyn
import utils.convert as convert
import utils.quaternion as quat
import utils.horizons as horizons
import Dynamics.body as body

class Simulator():

    def __init__(self, bodies, start, end, dt, horizons_dt):
        self.bodies = bodies
        t_array = np.arange(start, end, convert.convertSecToDays(dt))
        self.t_array = t_array
        self.time_since_j2000 = t_array[0]
        self.last_time = 2*t_array[0] - t_array[1]
        self.spacecrafts = []
        self.prev_true = self.time_since_j2000
        self.horizons_dt = horizons_dt
        for i, body in enumerate(self.bodies):
            if body.name == "Earth":
                self.idx_earth = i

    def init_bodies(bodies, start_time, end_time, dt):
        objs = []
        for obj in bodies:
            states, dt = horizons.get_state_from_horizons(obj, start_time, end_time, dt)
            obj = body.Body(obj, states)
            objs.append(obj)
        return objs, dt

    def add_spacecrafts(self, spacecraft):
        self.spacecrafts = spacecraft

    def check_impact(self, sat, body):
        if np.linalg.norm(sat.state - body.state) < body.radius:
            print(f"{sat.get_name()} impacted {body.name}")
            if body.name == "Earth":
                sat.set_crashed(True, self.time_since_j2000)
            else:
                sat.set_crashed(False)

    def tic(self, dt):
        self.time_since_j2000 += dt
        next_true = self.prev_true + self.horizons_dt
        if self.time_since_j2000 == next_true:
            self.prev_true = self.time_since_j2000

        for sat in self.spacecrafts:
            new_state, new_qEci2Body, ecef_state, ll_state = np.zeros(6), np.zeros(4), np.zeros(6), np.zeros(2)
            if not sat.is_crashed():
                new_state, new_qEci2Body = dyn.orbit_prop(sat, dt, self.bodies)
                bci_state = new_state - self.bodies[0].state
            else:
                if sat.ecef_crash is not None:
                    ecef_state = sat.ecef_crash
                    new_state = self.bodies[self.idx_earth].state + convert.ecef2eci(ecef_state, convert.convertSecToDays(self.time_since_j2000))
                    bci_state = new_state - self.bodies[0].state
                    new_qEci2Body = quat.init_quat(bci_state, "lvlh")
            for body in self.bodies:
                self.check_impact(sat, body)
            if self.bodies[0].name == "Earth":
                ecef_state = convert.eci2ecef(bci_state, convert.convertSecToDays(self.time_since_j2000))
                ll_state = convert.ecef2lla(ecef_state)
            sat.update_state(self.time_since_j2000, new_state, new_qEci2Body, bci_state, ecef_state, ll_state)
        for body in self.bodies:
            body.update_state(self.bodies, dt, self.time_since_j2000 == next_true)

    def run(self):
        for t in tqdm(self.t_array):
            self.tic(convert.convertDaysToSec(t-self.last_time))
            self.last_time = t
        return self.t_array, self.spacecrafts


from goose3 import Goose
import pandas as pd
import time
from newspaper import Article
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
import random
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from deep_translator import GoogleTranslator
from googletrans import Translator

path_to_csv = r"C:\Users\ujwal\Downloads\PREIPO_Final_Report_2023-07-01.csv"

def get_text(path_to_csv):

    def translatedeep(text):
        translation = GoogleTranslator(source='auto', target='en').translate(text)
        return translation

    def translate(text):
        translator = Translator()
        translation = translator.translate(text, dest='en')
        return translation.text

    df = pd.read_csv(path_to_csv)
    g = Goose({
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0",
                    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
                    'sec-fetch-site': 'none',
                    'sec-fetch-mode': 'navigate',
                    'sec-fetch-user': '?1',
                    'sec-fetch-dest': 'document',
                    'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
                })
    errors_404 = []

    options = webdriver.ChromeOptions()
    options.headless = True
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument("--window-size=1920,1080")
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    service = ChromeService(executable_path=ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=options)

    def reset_driver():
        url = "https://www.ibm.com/demos/live/natural-language-understanding/self-service/home"
        driver.get(url)
        url_parts = driver.find_elements(By.CLASS_NAME, "story-button")
        for url in url_parts:
            if "URL" in str(url.text):
                url.click()
                break
        time.sleep(round(random.uniform(0.5, 1), 2))

    reset_driver()

    def get_from_goose(link):
        """ Fastest way to extract article """
        return g.extract(url=link).cleaned_text

    def get_from_newspaper(link):
        " Mediocre fast "
        article = Article(link)
        article.download()
        article.parse()
        return article.text

    def get_from_ibm(link):
        """ Very Slow """
        try:
            textarea = driver.find_element(By.ID, "nlu-analyze-textbox-1")
            textarea.clear()
            textarea.send_keys(link)
            time.sleep(round(random.uniform(1, 2), 2))
            button = driver.find_element(By.CLASS_NAME, "bx--btn--tertiary")
            button.click()
            text = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'results-text'))).text
            if not text:
                print(f"\n Error: {link} \n")
            time.sleep(round(random.uniform(1, 2), 2))
            button = driver.find_element(By.CLASS_NAME, "bx--btn--tertiary")
            button.click()
            time.sleep(round(random.uniform(0.6, 1), 2))
            return text
        except Exception as e:
            print(f"\n Exception: {e} \n Error: {link} \n")
            reset_driver()
            return None

    length = len(df)

    for i, row in df.iterrows():
        text=""
        flag=0
        link = row["link"]
        try:
            text = get_from_goose(link)
            if text:
                df.at[i, "text"] = text
            else:
                text = get_from_newspaper(link)
                if text:
                    df.at[i, "text"] = text
                else:
                    flag = 1
                    text = get_from_ibm(link)
                    if text:
                        df.at[i, "text"] = text
        except Exception as error:
            if "404" or "500" in str(error):
                length-=1
                print(length)
                errors_404.append(link)
                continue
            if flag==0:
                try:
                    text = get_from_newspaper(link)
                    if text:
                        df.at[i, "text"] = text
                    else:
                        text = get_from_ibm(link)
                        if text:
                            df.at[i, "text"] = text
                except:
                    text = get_from_ibm(link)
                    if text:
                        df.at[i, "text"] = text
            else:
                text = get_from_ibm(link)
                if text:
                    df.at[i, "text"] = text

        length-=1
        print(length)

    driver.quit()

    df.to_csv(path_to_csv)

if __name__ == "__main__":
    get_text(path_to_csv)


from shop.models import *
from django.shortcuts import *
from django.db.models import Sum
from django.core.exceptions import ObjectDoesNotExist

def brand(request, name):
	rtx = {}
	rtx['isUser'] = request.session.get('type', '') == 'user'
	rtx['user_id'] = request.session.get('id', '')

	rtx['brand'] = Brand.objects.get(pk=name)
	rtx['turnover'] = Buy.objects.filter(computer_id__brand=rtx['brand']).aggregate(Sum('price'))[r'price__sum']
	rtx['likeNumber'] = like.objects.filter(brand_name=rtx['brand']).count()
	rtx['followers'] = like.objects.filter(brand_name=rtx['brand'])

	rtx['like'] = '取消关注' if like.objects.filter(brand_name__name=name, user_id__user_id=rtx['user_id']).count() > 0 else '关注'

	rtx['computers'] = []
	for c in Computer.objects.filter(brand__name=name):
		rtx['computers'].append((c.computer_id, c.cpu, c.memory, Buy.objects.filter(computer_id__brand__name=name).count()))

	rtx['net'] = rtx['brand'].net
	if rtx['net'][:4] != 'http':
		rtx['net'] = 'http://' + rtx['net']

	return render(request, 'Dashio/brand.html', rtx)

def makeLike(request, name, user_id):
  try:
    m = like.objects.get(brand_name__name=name, user_id__user_id=user_id)
    m.delete()
  except ObjectDoesNotExist:
    brand = get_object_or_404(Brand, pk=name)
    user = get_object_or_404(User, pk=user_id)
    like(brand_name=brand, user_id=user).save()

  return HttpResponseRedirect(reverse('shop:brand', args=(name, )))

def allBrands(request):
	brands = Brand.objects.all()
	rtx = {'brands': []}
	for b in brands:
		rtx['brands'].append([b.name, b.country, b.found_date, Computer.objects.filter(brand=b).count(), Buy.objects.filter(computer_id__brand=b).aggregate(Sum('price'))[r'price__sum']])
		if rtx['brands'][-1][4] is None:
			rtx['brands'][-1][4] = 0.0

	if request.method == 'POST':
		sortKey = {'name': 0, 'country': 1, 'found_date': 2, 'computerNumber': 3, 'turnover': 4}
		rtx['brands'].sort(key=lambda x: x[sortKey[request.POST['sort']]], reverse=(request.POST['sortType'] == '-'))

	return render(request, 'Dashio/allbrand.html', rtx)


# This more efficient program counts up or counts down depending on which button is clicked

from tkinter import *
from functools import partial

root =Tk()
root.title("Counting")

def change_number(change):
    """ This function changes the number and prints it. """

    global number
    number += change
    print(number)

# setting the starting value
number = 0

values = [1, -1]

# add butons
for value in values:
    textdisplay = "Change by "+str(value)
    button = Button(root, text=textdisplay, command=partial(change_number,value)).grid()

root.mainloop()


import datetime
import os
from math import inf, lcm, log10, sqrt
import Eratosthenes
import random
time = datetime.datetime.now()

board = {0:'GO',1:'A1',2:'CC1',3:'A2',4:'T1',5:'R1',6:'B1',7:'CH1',8:'B2',9:'B3',10:'JAIL',11:'C1',12:'U1',13:'C2',14:'C3',15:'R2',16:'D1',17:'CC2',18:'D2',19:'D3',20:'FP',21:'E1',22:'CH2',23:'E2',24:'E3',25:'R3',26:'F1',27:'F2',28:'U2',29:'F3',30:'G2J',31:'G1',32:'G2 ',33:'CC3',34:'G3',35:'R4',36:'CH3',37:'H1',38:'T2',39:'H2'
}
communityChest = {1:'Advance to GO', 2:'Go to JAIL'}
communityChestOrder = list(range(16))
chance = {1:'Advance to GO',2:'Go to JAIL',3:'Go to C1',4:'Go to E3',5:'Go to H2',6:'Go to R1',7:'Go to next R',8:'Go to next R',9:'Go to next U',10:'Go back 3 squares'}
chanceOrder = list(range(16))

def nextMove(currentPos, instruction):
    if instruction == 'Advance to GO':
        return 1
    elif instruction == 'Go to JAIL':
        return 11
    elif instruction == 'Go to E3':
        return 25
    elif instruction == 'Go to H2':
        return 39
    elif instruction == 'Go to R1':
        return 6
    elif instruction == 'Go to next R':
        return (((currentPos+5)//10)*10+5)%40
    elif instruction == 'Go to next U':
        if currentPos > 12 and currentPos < 28:
            return 28
        else:
            return 12
    elif instruction == 'Go back 3 squares':
        return (currentPos-3)%40

print(chanceOrder)
random.shuffle(chanceOrder)
random.shuffle(communityChestOrder)
print (communityChestOrder)

runtime  = datetime.datetime.now() - time
print (runtime)


#!/usr/bin/env python
# coding: utf-8

# In[1]:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F

# In[2]:

train_set = pd.read_csv('/home/kandpal/Downloads/mnist_train.csv', header=None, dtype='float64')
test_set = pd.read_csv('/home/kandpal/Downloads/mnist_test.csv', header=None, dtype='float64')

train_labels = train_set[0]
test_label = test_set[0]

train_set.drop(columns=0, inplace=True)
test_set.drop(columns=0, inplace=True)

## Add dataLoader part below ##

# In[3]:

class Model(nn.Module):

    def __init__(self):
        super(Model, self).__init__()

        self.conv1 = nn.Conv2d(1, 10, 5)
        self.conv2 = nn.Conv2d(10, 20, 5)
        self.linear1 = nn.Linear(8000, 4000)
        self.linear2 = nn.Linear(4000, 25)
        self.convtrans1 = nn.ConvTranspose2d(1, 10, 1)
        self.convtrans2 = nn.ConvTranspose2d(10, 30, 3)
        self.linear3 = nn.Linear(1470, 800)
        self.reconst = nn.Linear(800, 784)

    def encoder(self, image):

        image = torch.unsqueeze(torch.unsqueeze(image, 0), 0)

        conv1 = F.relu( self.conv1(image) )
        conv2 = F.relu( self.conv2(conv1) )
        linear = F.relu( self.linear1( conv2.reshape(-1) ) )
        bottleneck = F.relu( self.linear2( linear ) )

        return bottleneck

    def decoder(self, bottleneck):

        bottleneck = bottleneck.reshape(5, 5)
        bottleneck = torch.unsqueeze(torch.unsqueeze(bottleneck, 0), 0)

        deconv1 = F.relu( self.convtrans1( bottleneck ) )
        deconv2 = F.relu( self.convtrans2( deconv1 ))
        reconstr = F.relu( self.linear3( deconv2.reshape(-1) ) )
        reconstr = F.relu( self.reconst( reconstr ) )

        return reconstr

# In[4]:

net = Model().cuda()

loss_fn = nn.MSELoss()
optimizer = torch.optim.AdamW(net.parameters())
epochs = 10

# In[5]:

costs = []

for iterations in range(epochs):

    for image_number in range(len(train_set.index)):

        image = torch.from_numpy(np.array(list(train_set.iloc[1])).reshape(28,28))
        image = image.float()

        gaussian_noise = torch.randn(image.shape)
        noisy_image = image + 100*gaussian_noise

        latent_space = net.encoder(noisy_image.cuda())
        reconstruction = net.decoder(latent_space.cuda())

        loss = loss_fn(image.flatten().cuda(), reconstruction.cuda())
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        costs.append(loss.item())
    print(f'average loss {np.mean(costs)}')
    plt.figure(1)
    plt.subplot(131)
    plt.imshow(image.cpu().detach().reshape(28,28).numpy())

    plt.subplot(132)
    plt.imshow(noisy_image.reshape(28,28))

    plt.subplot(133)
    plt.imshow(reconstruction.cpu().detach().reshape(28,28).numpy())

# In[ ]:

from scipy.ndimage import gaussian_filter
plt.plot(costs)
plt.plot(gaussian_filter(costs, 10))
plt.show()


import argparse
import os
import os.path as osp

import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import network
import loss
import pre_process as prep
import torch.utils.data as util_data
import lr_schedule
import data_list
from data_list import ImageList
from torch.autograd import Variable

optim_dict = {"SGD": optim.SGD}

def train(config):
    ## set pre-process
    prep_dict = {}
    prep_config = config["prep"]
    prep_dict["train_set1"] = prep.image_train( \
                            resize_size=prep_config["resize_size"], \
                            crop_size=prep_config["crop_size"])
    prep_dict["train_set2"] = prep.image_train( \
                            resize_size=prep_config["resize_size"], \
                            crop_size=prep_config["crop_size"])

    ## prepare data
    dsets = {}
    dset_loaders = {}
    data_config = config["data"]
    dsets["train_set1"] = ImageList(open(data_config["train_set1"]["list_path"]).readlines(), \
                                transform=prep_dict["train_set1"])
    dset_loaders["train_set1"] = util_data.DataLoader(dsets["train_set1"], \
            batch_size=data_config["train_set1"]["batch_size"], \
            shuffle=True, num_workers=4)
    dsets["train_set2"] = ImageList(open(data_config["train_set2"]["list_path"]).readlines(), \
                                transform=prep_dict["train_set2"])
    dset_loaders["train_set2"] = util_data.DataLoader(dsets["train_set2"], \
            batch_size=data_config["train_set2"]["batch_size"], \
            shuffle=True, num_workers=4)

    hash_bit = config["hash_bit"]

    ## set base network
    net_config = config["network"]
    base_network = net_config["type"](**net_config["params"])

    use_gpu = torch.cuda.is_available()
    if use_gpu:
        base_network = base_network.cuda()

    ## collect parameters
    parameter_list = [{"params":base_network.feature_layers.parameters(), "lr":1}, \
                      {"params":base_network.hash_layer.parameters(), "lr":10}]

    ## set optimizer
    optimizer_config = config["optimizer"]
    optimizer = optim_dict[optimizer_config["type"]](parameter_list, \
                    **(optimizer_config["optim_params"]))
    param_lr = []
    for param_group in optimizer.param_groups:
        param_lr.append(param_group["lr"])
    schedule_param = optimizer_config["lr_param"]
    lr_scheduler = lr_schedule.schedule_dict[optimizer_config["lr_type"]]

    ## train
    len_train1 = len(dset_loaders["train_set1"]) - 1
    len_train2 = len(dset_loaders["train_set2"]) - 1
    transfer_loss_value = classifier_loss_value = total_loss_value = 0.0
    best_acc = 0.0
    for i in range(config["num_iterations"]):
        if i % config["snapshot_interval"] == 0:
            torch.save(nn.Sequential(base_network), osp.join(config["output_path"], \
                "iter_{:05d}_model.pth.tar".format(i)))

        ## train one iter
        base_network.train(True)
        optimizer = lr_scheduler(param_lr, optimizer, i, **schedule_param)
        optimizer.zero_grad()
        if i % len_train1 == 0:
            iter1 = iter(dset_loaders["train_set1"])
        if i % len_train2 == 0:
            iter2 = iter(dset_loaders["train_set2"])
        inputs1, labels1 = iter1.next()
        inputs2, labels2 = iter2.next()
        if use_gpu:
            inputs1, inputs2, labels1, labels2 = \
                Variable(inputs1).cuda(), Variable(inputs2).cuda(), \
                Variable(labels1).cuda(), Variable(labels2).cuda()
        else:
            inputs1, inputs2, labels1, labels2 = Variable(inputs1), \
                Variable(inputs2), Variable(labels1), Variable(labels2)

        inputs = torch.cat((inputs1, inputs2), dim=0)
        outputs = base_network(inputs)
        similarity_loss = loss.pairwise_loss(outputs.narrow(0,0,inputs1.size(0)), \
                                 outputs.narrow(0,inputs1.size(0),inputs2.size(0)), \
                                 labels1, labels2, \
                                 hashbit = hash_bit,\
                                 gamma=config["loss"]["gamma"], \
                                 normed=config["loss"]["normed"], \
                                 q_lambda=config["loss"]["q_lambda"])
        total_loss_value = total_loss_value + similarity_loss.float().data[0]
        similarity_loss.backward()
        if (i+1) % len_train1 == 0:
            print("Epoch: {:05d}, loss: {:.3f}".format(i//len_train1, total_loss_value))
            total_loss_value = 0.0
        optimizer.step()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='DCH')
    parser.add_argument('--gpu_id', type=str, default='0', help="device id to run")
    parser.add_argument('--dataset', type=str, default='coco', help="dataset name")
    parser.add_argument('--hash_bit', type=int, default=48, help="number of hash code bits")
    parser.add_argument('--net', type=str, default='ResNet50', help="base network type")
    parser.add_argument('--prefix', type=str, help="save path prefix")
    parser.add_argument('--lr', type=float, help="learning rate")
    parser.add_argument('--q_lambda', type=float, help="parameter weight")
    args = parser.parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id

    # train config
    config = {}
    config["num_iterations"] = 10000
    config["snapshot_interval"] = 3000
    config["dataset"] = args.dataset
    config["hash_bit"] = args.hash_bit
    config["output_path"] = "../snapshot/"+config["dataset"]+"_"+ \
                            str(config["hash_bit"])+"bit_"+args.prefix
    if not osp.exists(config["output_path"]):
        os.mkdir(config["output_path"])
    config["out_file"] = open(osp.join(config["output_path"], "log.txt"), "w")

    if not osp.exists(config["output_path"]):
        os.mkdir(config["output_path"])
    config["network"] = {}
    if "ResNet" in args.net:
        config["network"]["type"] = network.ResNetFc
        config["network"]["params"] = {"name":args.net, "hash_bit":config["hash_bit"]}
    elif "VGG" in args.net:
        config["network"]["type"] = network.VGGFc
        config["network"]["params"] = {"name":args.net, "hash_bit":config["hash_bit"]}
    elif "AlexNet" in args.net:
        config["network"]["type"] = network.AlexNetFc
        config["network"]["params"] = {"hash_bit":config["hash_bit"]}
    config["prep"] = {"test_10crop":True, "resize_size":256, "crop_size":224}
    config["optimizer"] = {"type":"SGD", "optim_params":{"lr":1.0, "momentum":0.9, \
                           "weight_decay":0.0005, "nesterov":True}, "lr_type":"step", \
                           "lr_param":{"init_lr":args.lr, "gamma":0.5, "step":3000} }

    config["loss"] = {"gamma":15.0, "normed":True, "q_lambda":args.q_lambda}

    if config["dataset"] == "imagenet":
        config["data"] = {"train_set1":{"list_path":"../data/imagenet/train.txt", "batch_size":36}, \
                          "train_set2":{"list_path":"../data/imagenet/train.txt", "batch_size":36}}
    elif config["dataset"] == "nus_wide":
        config["data"] = {"train_set1":{"list_path":"../data/nus_wide/train.txt", "batch_size":36}, \
                          "train_set2":{"list_path":"../data/nus_wide/train.txt", "batch_size":36}}
    elif config["dataset"] == "coco":
        config["data"] = {"train_set1":{"list_path":"../data/coco/train.txt", "batch_size":36}, \
                          "train_set2":{"list_path":"../data/coco/train.txt", "batch_size":36}}
    print(config["loss"])
    train(config)


from pmlb import fetch_data

import numpy as np

from standardgp import GP

def get_data(dataset_name) -> np.ndarray:
    # data from https://github.com/EpistasisLab/pmlb
    data = fetch_data(dataset_name, local_cache_dir="./datasets")
    y = data["target"].to_numpy()
    x = []
    for i, key in enumerate(data):
        if key == "target":
            continue
        x.append(data[key].to_numpy())
    return np.array(x).T, y

if __name__ == "__main__":
    GP.seed(149)
    # example for regression tasks
    # see all regression datasets available at:
    # https://epistasislab.github.io/pmlb/
    x, y = get_data("579_fri_c0_250_5")
    gp = GP(x[::2, :], y[::2])  # half of the data to train
    error, model = gp.run(show=True, threads=8)
    print("Error: {}, Model: {}".format(error, model))
    print("RMSE train:", np.sqrt(np.mean(np.square(gp.predict(x) - y))))
    x, y = x[1::2, :], y[1::2]  # rest of the data to test on unknown data
    print("RMSE test:", np.sqrt(np.mean(np.square(gp.predict(x) - y))))


import json
import unittest
from io import StringIO
from unittest.mock import MagicMock
from unittest.mock import patch

from tr.parsers.csv_parser import CSVParser

class TestXMLParser(unittest.TestCase):

    @patch('os.path.exists')
    @patch('tr.parsers.csv_parser.open')
    def test_read(self, mock_open: MagicMock, mock_exists: MagicMock):
        mock_exists.return_value = True
        mock_open.return_value.__enter__.side_effect = [StringIO("""
"id","name","address","phone","date"
"ID5410","Melissa T Miller","2837  Fidler Drive","210-624-7306","31/01/2020"
"ID9857","Daniel I Walker","3853  Hilltop Street","413-655-7397","25/04/2020"
""".strip()),
StringIO(r"""
"id","make","vin_number","owner_id"
"V3015","Chevrolet","1HGFA16548L016469","ID9857"
"V2014","Honda","1G6KD57Y46U180996","ID9857"
"V1475","Ford","2HKYF18575H574967","ID5410"
""".strip())]
        parser = CSVParser()
        parser.read(('csv/customers.csv', 'csv/vehicles.csv'))
        expected_data = [{
            "file_name": "csv/customers.csv",
            "transaction": {
                "date": "31/01/2020",
                "customer": {
                    "id": "ID5410",
                    "name": "Melissa T Miller",
                    "address": "2837  Fidler Drive",
                    "phone": "210-624-7306"
                },
                "vehicles": [
                    {
                        "id": "V1475",
                        "make": "Ford",
                        "vin_number": "2HKYF18575H574967"
                    }]
            }
        }, {
            "file_name": "csv/customers.csv",
            "transaction": {
                "date": "25/04/2020",
                "customer": {
                    "id": "ID9857",
                    "name": "Daniel I Walker",
                    "address": "3853  Hilltop Street",
                    "phone": "413-655-7397"
                },
                "vehicles": [
                    {
                        "id": "V3015",
                        "make": "Chevrolet",
                        "vin_number": "1HGFA16548L016469"
                    },
                    {
                        "id": "V2014",
                        "make": "Honda",
                        "vin_number": "1G6KD57Y46U180996"
                    }
                ]
            }
        }
        ]

        actual_data = parser.data
        self.assertEqual(json.dumps(expected_data), json.dumps(actual_data))


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('f_info', '0001_initial'),
        ('f_user', '__first__'),
        ('f_order', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Evaluate',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
                ('value', models.CharField(max_length=300)),
                ('date', models.DateTimeField(auto_now=True)),
                ('efruit', models.ForeignKey(to='f_info.FruInfo')),
                ('euser', models.ForeignKey(to='f_user.UserInfo')),
            ],
        ),
    ]


from flask import Flask, request,jsonify #import main Flask class and request object
import subprocess
import hid
import time
from commands import *
from usb_comm import *
#import Tkinter as tk
import pandas as pd
import math

app = Flask(__name__) #create the Flask app

@app.route('/postData',methods=['POST','GET'])
def sensor():

    set_active_config(0)
    get_scan_config_id()

    start_scan(0) # donot store in sd card

    results = get_results() # get scan results
    ref_scan = get_ref_data() # get reference values

    # Convert the results into a dataframe

    values = {"wavelength":results["wavelength"],"intensity":results["intensity"],"reference":ref_scan["intensity"]}
    df = pd.DataFrame(values)
    df = df[(df[['wavelength','intensity']] > 0).all(axis=1)].reset_index() # drop values of 0 or less
    df['reflectance'] = df['intensity']/df['reference'] #reflectance = sample/reference
    df['absorption'] = -(np.log10(df['reflectance']))#absorption = -log(reflectance)

    final_out=df.to_json(orient='records')
    return final_out

if __name__ == '__main__':
    a= 0x0451
    b = 0x4200

    setup(a,b)
    time.sleep(1)
    app.run(port=5000)


import numpy as np
import tensorflow

import matplotlib.pyplot as plt
from tensorflow import keras
print(tensorflow.__version__)
print(keras.__version__)
from tensorflow.keras.datasets import cifar10
(x_train,y_train), (x_test,y_test) = cifar10.load_data()
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.layers import Convolution2D, MaxPool2D, Flatten, Dense, Dropout, BatchNormalization
from tensorflow.keras import regularizers
from tensorflow.keras.utils import to_categorical

gpus = tensorflow.config.experimental.list_physical_devices('GPU')
if gpus:
  try:
    for gpu in gpus:
      tensorflow.config.experimental.set_memory_growth(gpu, True)
  except RuntimeError as e:
    print(e)

model = Sequential([
   Convolution2D(filters=128, kernel_size=(5,5), input_shape=(32,32,3), activation='relu', padding='same'),
   BatchNormalization(),
   Convolution2D(filters=128, kernel_size=(5,5), activation='relu', padding='same'),
   BatchNormalization(),
   MaxPool2D((2,2)),
   Convolution2D(filters=64, kernel_size=(5,5), activation='relu', padding='same'),
   BatchNormalization(),
   Convolution2D(filters=64, kernel_size=(5,5), activation='relu', padding='same'),
   BatchNormalization(),
   MaxPool2D((2,2)),
   Convolution2D(filters=32, kernel_size=(5,5), activation='relu', padding='same'),
   BatchNormalization(),
   Convolution2D(filters=32, kernel_size=(5,5), activation='relu', padding='same'),
   BatchNormalization(),
   MaxPool2D((2,2)),
   Convolution2D(filters=16, kernel_size=(3,3), activation='relu', padding='same'),
   BatchNormalization(),
   Convolution2D(filters=16, kernel_size=(3,3), activation='relu', padding='same'),
   BatchNormalization(),
   Flatten(),
   Dense(units=32, activation="relu"),
   Dropout(0.15),
   Dense(units=16, activation="relu"),
   Dropout(0.05),
   Dense(units=10, activation="softmax")
])
optim = RMSprop(lr=0.001)
model.compile(optimizer=optim, loss='categorical_crossentropy', metrics=['accuracy'])

#model = keras.models.load_model(r'C:\Users\Student240914\Desktop\PycharmProjects\keylogger\Praca')
from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
    rotation_range=10,
    horizontal_flip=True,
    vertical_flip = False,
    width_shift_range=0.1,
    height_shift_range=0.1,
    rescale = 1. / 255,
    shear_range=0.05,
    zoom_range=0.05,
)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

batch_size = 32
train_generator = datagen.flow(x_train, y_train, batch_size=batch_size)
datagen_valid = ImageDataGenerator(
   rescale = 1. / 255,)

x_valid = x_train[:100*batch_size]
y_valid = y_train[:100*batch_size]

valid_steps = x_valid.shape[0] // batch_size
validation_generator = datagen_valid.flow(x_valid, y_valid, batch_size=batch_size)

history = model.fit(
   train_generator,
   steps_per_epoch=len(x_train) // batch_size,
   epochs=50,
   validation_data=validation_generator,
   validation_freq=1,
   validation_steps=valid_steps,
   verbose=2
)
model.save(r'C:\Users\Student240914\Desktop\PycharmProjects\keylogger\Praca\Model_cifar_10')

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Valid'], loc='upper left')
plt.show()

x_final_test = x_test / 255.0
eval = model.evaluate(x_final_test, y_test)


from argparse import ArgumentError
import logging
from pathlib import Path
from typing import Union
import zarr
import numpy as np
from db.file_system.models import FileSystemVolumeMedatada
from db.file_system.constants import ANNOTATION_METADATA_FILENAME, GRID_METADATA_FILENAME
from preprocessor.src.preprocessors.i_data_preprocessor import IDataPreprocessor
from preprocessor.src.preprocessors.implementations.sff.preprocessor._zarr_methods import get_volume_downsampling_from_zarr, get_segmentation_downsampling_from_zarr
from preprocessor.src.preprocessors.implementations.sff.preprocessor.constants import MESH_SIMPLIFICATION_CURVE_LINEAR, MESH_SIMPLIFICATION_N_LEVELS, MESH_SIMPLIFICATION_LEVELS_PER_ORDER, TEMP_ZARR_HIERARCHY_STORAGE_PATH
from preprocessor.src.tools.magic_kernel_downsampling_3d.magic_kernel_downsampling_3d import MagicKernel3dDownsampler
import mrcfile
import dask.array as da
import math

def open_zarr_structure_from_path(path: Path) -> zarr.hierarchy.Group:
    store: zarr.storage.DirectoryStore = zarr.DirectoryStore(str(path))
    # Re-create zarr hierarchy from opened store
    root: zarr.hierarchy.group = zarr.group(store=store)
    return root

class SFFPreprocessor(IDataPreprocessor):
    from ._hdf5_to_zarr import hdf5_to_zarr
    from ._volume_map_methods import normalize_axis_order_mrcfile
    from ._process_X_data_methods import process_volume_data, process_segmentation_data
    from ._metadata_methods import temp_save_metadata, extract_annotations, extract_metadata

    def __init__(self, temp_zarr_hierarchy_storage_path: Path):
        if not temp_zarr_hierarchy_storage_path:
            raise ArgumentError('No temp_zarr_hierarchy_storage_path is provided')
        # path to root of temporary storage for zarr hierarchy
        self.temp_root_path = temp_zarr_hierarchy_storage_path
        self.magic_kernel = MagicKernel3dDownsampler()
        self.temp_zarr_structure_path = None

    def preprocess(self, segm_file_path: Path, volume_file_path: Path, params_for_storing: dict, volume_force_dtype: Union[np.dtype, None], entry_id: str,
        source_db_id: str,
        source_db_name: str
        ):
        '''
        Returns path to temporary zarr structure that will be stored permanently using db.store
        '''
        try:
            if segm_file_path is not None:
                self.temp_zarr_structure_path = SFFPreprocessor.hdf5_to_zarr(self.temp_root_path, segm_file_path, entry_id)
            else:
                self.__init_empty_zarr_structure(volume_file_path)
            # Re-create zarr hierarchy
            zarr_structure: zarr.hierarchy.group = open_zarr_structure_from_path(
                self.temp_zarr_structure_path)

            # read map
            with mrcfile.mmap(str(volume_file_path.resolve())) as mrc_original:
                data: np.memmap = mrc_original.data
                if volume_force_dtype is not None:
                    data = data.astype(volume_force_dtype)
                else:
                    volume_force_dtype = data.dtype

                header = mrc_original.header

            if ('quantize_dtype_str' in params_for_storing) and \
                (
                    (volume_force_dtype in (np.uint8, np.int8)) or \
                    ((volume_force_dtype in (np.uint16, np.int16)) and (params_for_storing['quantize_dtype_str'] in ['u2', '|u2', '>u2', '<u2'] ))
                ):
                print(f'Quantization is skipped because input volume dtype is {volume_force_dtype} and requested quantization dtype is {params_for_storing["quantize_dtype_str"]}')
                del params_for_storing['quantize_dtype_str']

            print(f"Processing volume file {volume_file_path}")
            dask_arr = da.from_array(data)
            # print('shape', dask_arr.shape)
            # print('crs', int(header.mapc) - 1, int(header.mapr) - 1, int(header.maps) - 1)
            # print('n', int(header.nx), int(header.ny), int(header.nz))

            dask_arr = SFFPreprocessor.normalize_axis_order_mrcfile(dask_arr=dask_arr, mrc_header=header)
            # print('after', dask_arr.shape)

            print(f"Processing segmentation file {segm_file_path}")
            # mesh_simplification_curve = MESH_SIMPLIFICATION_CURVE_LINEAR
            mesh_simplification_curve = make_simplification_curve(MESH_SIMPLIFICATION_N_LEVELS, MESH_SIMPLIFICATION_LEVELS_PER_ORDER)
            if segm_file_path is not None:
                SFFPreprocessor.process_segmentation_data(self.magic_kernel, zarr_structure, mesh_simplification_curve, params_for_storing=params_for_storing)

            SFFPreprocessor.process_volume_data(zarr_structure=zarr_structure, dask_arr=dask_arr, params_for_storing=params_for_storing, force_dtype=volume_force_dtype)

            grid_metadata = SFFPreprocessor.extract_metadata(
                zarr_structure,
                mrc_header=header,
                mesh_simplification_curve=mesh_simplification_curve,
                volume_force_dtype=volume_force_dtype,
                source_db_id=source_db_id,
                source_db_name=source_db_name
            )

            # grid_dimensions: list = list(FileSystemVolumeMedatada(grid_metadata).grid_dimensions())
            # zarr_volume_arr_shape: list = list(get_volume_downsampling_from_zarr(1, zarr_structure).shape)
            # if segm_file_path is not None and zarr_structure.primary_descriptor[0] == b'three_d_volume':
            #     zarr_segm_arr_shape: list = list(get_segmentation_downsampling_from_zarr(1, zarr_structure, 0).shape)
            #     assert grid_dimensions == zarr_segm_arr_shape, \
            #     f'grid dimensions from metadata {grid_dimensions} are not equal to segmentation arr shape {zarr_segm_arr_shape}'
            # assert grid_dimensions == zarr_volume_arr_shape, \
            #         f'grid dimensions from metadata {grid_dimensions} are not equal to volume arr shape {zarr_volume_arr_shape}'

            SFFPreprocessor.temp_save_metadata(grid_metadata, GRID_METADATA_FILENAME, self.temp_zarr_structure_path)

            if segm_file_path is not None:
                annotation_metadata = SFFPreprocessor.extract_annotations(segm_file_path)
                SFFPreprocessor.temp_save_metadata(annotation_metadata, ANNOTATION_METADATA_FILENAME, self.temp_zarr_structure_path)
        except Exception as e:
            logging.error(e, stack_info=True, exc_info=True)
            raise e

        return self.temp_zarr_structure_path

    def __init_empty_zarr_structure(self, volume_file_path: Path):
        '''
        Creates EMPTY temp zarr structure for the case when just volume file is provided
        '''
        self.temp_zarr_structure_path = self.temp_root_path / volume_file_path.stem
        try:
            assert self.temp_zarr_structure_path.exists() == False, \
                f'temp_zarr_structure_path: {self.temp_zarr_structure_path} already exists'
            store: zarr.storage.DirectoryStore = zarr.DirectoryStore(str(self.temp_zarr_structure_path))
        except Exception as e:
            logging.error(e, stack_info=True, exc_info=True)
            raise e

def make_simplification_curve(n_levels: int, levels_per_order: int) -> dict[int, float]:
    result = {}
    for i in range(n_levels):
        ratio = 10 ** (-i / levels_per_order)
        result[i + 1] = _round_to_significant_digits(ratio, 2)
    return result

def _round_to_significant_digits(number: float, digits: int) -> float:
    first_digit = -math.floor(math.log10(number))
    return round(number, first_digit + digits - 1)

{'hdf5_to_zarr': '_hdf5_to_zarr.hdf5_to_zarr', 'normalize_axis_order_mrcfile': '_volume_map_methods.normalize_axis_order_mrcfile', 'process_volume_data': '_process_X_data_methods.process_volume_data', 'process_segmentation_data': '_process_X_data_methods.process_segmentation_data', 'temp_save_metadata': '_metadata_methods.temp_save_metadata', 'extract_annotations': '_metadata_methods.extract_annotations', 'extract_metadata': '_metadata_methods.extract_metadata'}.hdf5_to_zarr

{'hdf5_to_zarr': '_hdf5_to_zarr.hdf5_to_zarr', 'normalize_axis_order_mrcfile': '_volume_map_methods.normalize_axis_order_mrcfile', 'process_volume_data': '_process_X_data_methods.process_volume_data', 'process_segmentation_data': '_process_X_data_methods.process_segmentation_data', 'temp_save_metadata': '_metadata_methods.temp_save_metadata', 'extract_annotations': '_metadata_methods.extract_annotations', 'extract_metadata': '_metadata_methods.extract_metadata'}.normalize_axis_order_mrcfile

{'hdf5_to_zarr': '_hdf5_to_zarr.hdf5_to_zarr', 'normalize_axis_order_mrcfile': '_volume_map_methods.normalize_axis_order_mrcfile', 'process_volume_data': '_process_X_data_methods.process_volume_data', 'process_segmentation_data': '_process_X_data_methods.process_segmentation_data', 'temp_save_metadata': '_metadata_methods.temp_save_metadata', 'extract_annotations': '_metadata_methods.extract_annotations', 'extract_metadata': '_metadata_methods.extract_metadata'}.process_segmentation_data

{'hdf5_to_zarr': '_hdf5_to_zarr.hdf5_to_zarr', 'normalize_axis_order_mrcfile': '_volume_map_methods.normalize_axis_order_mrcfile', 'process_volume_data': '_process_X_data_methods.process_volume_data', 'process_segmentation_data': '_process_X_data_methods.process_segmentation_data', 'temp_save_metadata': '_metadata_methods.temp_save_metadata', 'extract_annotations': '_metadata_methods.extract_annotations', 'extract_metadata': '_metadata_methods.extract_metadata'}.process_volume_data

{'hdf5_to_zarr': '_hdf5_to_zarr.hdf5_to_zarr', 'normalize_axis_order_mrcfile': '_volume_map_methods.normalize_axis_order_mrcfile', 'process_volume_data': '_process_X_data_methods.process_volume_data', 'process_segmentation_data': '_process_X_data_methods.process_segmentation_data', 'temp_save_metadata': '_metadata_methods.temp_save_metadata', 'extract_annotations': '_metadata_methods.extract_annotations', 'extract_metadata': '_metadata_methods.extract_metadata'}.extract_metadata

{'hdf5_to_zarr': '_hdf5_to_zarr.hdf5_to_zarr', 'normalize_axis_order_mrcfile': '_volume_map_methods.normalize_axis_order_mrcfile', 'process_volume_data': '_process_X_data_methods.process_volume_data', 'process_segmentation_data': '_process_X_data_methods.process_segmentation_data', 'temp_save_metadata': '_metadata_methods.temp_save_metadata', 'extract_annotations': '_metadata_methods.extract_annotations', 'extract_metadata': '_metadata_methods.extract_metadata'}.temp_save_metadata

{'hdf5_to_zarr': '_hdf5_to_zarr.hdf5_to_zarr', 'normalize_axis_order_mrcfile': '_volume_map_methods.normalize_axis_order_mrcfile', 'process_volume_data': '_process_X_data_methods.process_volume_data', 'process_segmentation_data': '_process_X_data_methods.process_segmentation_data', 'temp_save_metadata': '_metadata_methods.temp_save_metadata', 'extract_annotations': '_metadata_methods.extract_annotations', 'extract_metadata': '_metadata_methods.extract_metadata'}.extract_annotations


import itertools
import operator
import sys

def _is_int(string):
    try:
        int(string)
        return True
    except ValueError:
        return False

class SemanticVersion(object):
    """A pure semantic version independent of serialisation.

    See the pbr doc 'semver' for details on the semantics.
    """

    def __init__(
            self, major, minor=0, patch=0, prerelease_type=None,
            prerelease=None, dev_count=None):
        """Create a SemanticVersion.

        :param major: Major component of the version.
        :param minor: Minor component of the version. Defaults to 0.
        :param patch: Patch level component. Defaults to 0.
        :param prerelease_type: What sort of prerelease version this is -
            one of a(alpha), b(beta) or rc(release candidate).
        :param prerelease: For prerelease versions, what number prerelease.
            Defaults to 0.
        :param dev_count: How many commits since the last release.
        """
        self._major = major
        self._minor = minor
        self._patch = patch
        self._prerelease_type = prerelease_type
        self._prerelease = prerelease
        if self._prerelease_type and not self._prerelease:
            self._prerelease = 0
        self._dev_count = dev_count or 0  # Normalise 0 to None.

    def __eq__(self, other):
        if not isinstance(other, SemanticVersion):
            return False
        return self.__dict__ == other.__dict__

    def __hash__(self):
        return sum(map(hash, self.__dict__.values()))

    def _sort_key(self):
        """Return a key for sorting SemanticVersion's on."""
        # key things:
        # - final is after rc's, so we make that a/b/rc/z
        # - dev==None is after all other devs, so we use sys.maxsize there.
        # - unqualified dev releases come before any pre-releases.
        # So we do:
        # (major, minor, patch) - gets the major grouping.
        # (0|1) unqualified dev flag
        # (a/b/rc/z) - release segment grouping
        # pre-release level
        # dev count, maxsize for releases.
        rc_lookup = {'a': 'a', 'b': 'b', 'rc': 'rc', None: 'z'}
        if self._dev_count and not self._prerelease_type:
            uq_dev = 0
        else:
            uq_dev = 1
        return (
            self._major, self._minor, self._patch,
            uq_dev,
            rc_lookup[self._prerelease_type], self._prerelease,
            self._dev_count or sys.maxsize)

    def __lt__(self, other):
        """Compare self and other, another Semantic Version."""
        # NB(lifeless) this could perhaps be rewritten as
        # lt (tuple_of_one, tuple_of_other) with a single check for
        # the typeerror corner cases - that would likely be faster
        # if this ever becomes performance sensitive.
        if not isinstance(other, SemanticVersion):
            raise TypeError("ordering to non-SemanticVersion is undefined")
        return self._sort_key() < other._sort_key()

    def __le__(self, other):
        return self == other or self < other

    def __ge__(self, other):
        return not self < other

    def __gt__(self, other):
        return not self <= other

    def __ne__(self, other):
        return not self == other

    def __repr__(self):
        return "pbr.version.SemanticVersion(%s)" % self.release_string()

    @classmethod
    def from_pip_string(klass, version_string):
        """Create a SemanticVersion from a pip version string.

        This method will parse a version like 1.3.0 into a SemanticVersion.

        This method is responsible for accepting any version string that any
        older version of pbr ever created.

        Therefore: versions like 1.3.0a1 versions are handled, parsed into a
        canonical form and then output - resulting in 1.3.0.0a1.
        Pre pbr-semver dev versions like 0.10.1.3.g83bef74 will be parsed but
        output as 0.10.1.dev3.g83bef74.

        :raises ValueError: Never tagged versions sdisted by old pbr result in
            just the git hash, e.g. '1234567' which poses a substantial problem
            since they collide with the semver versions when all the digits are
            numerals. Such versions will result in a ValueError being thrown if
            any non-numeric digits are present. They are an exception to the
            general case of accepting anything we ever output, since they were
            never intended and would permanently mess up versions on PyPI if
            ever released - we're treating that as a critical bug that we ever
            made them and have stopped doing that.
        """

        try:
            return klass._from_pip_string_unsafe(version_string)
        except IndexError:
            raise ValueError("Invalid version %r" % version_string)

    @classmethod
    def _from_pip_string_unsafe(klass, version_string):
        # Versions need to start numerically, ignore if not
        version_string = version_string.lstrip('vV')
        if not version_string[:1].isdigit():
            raise ValueError("Invalid version %r" % version_string)
        input_components = version_string.split('.')
        # decimals first (keep pre-release and dev/hashes to the right)
        components = [c for c in input_components if c.isdigit()]
        digit_len = len(components)
        if digit_len == 0:
            raise ValueError("Invalid version %r" % version_string)
        elif digit_len < 3:
            if (digit_len < len(input_components) and
                    input_components[digit_len][0].isdigit()):
                # Handle X.YaZ - Y is a digit not a leadin to pre-release.
                mixed_component = input_components[digit_len]
                last_component = ''.join(itertools.takewhile(
                    lambda x: x.isdigit(), mixed_component))
                components.append(last_component)
                input_components[digit_len:digit_len + 1] = [
                    last_component, mixed_component[len(last_component):]]
                digit_len += 1
            components.extend([0] * (3 - digit_len))
        components.extend(input_components[digit_len:])
        major = int(components[0])
        minor = int(components[1])
        dev_count = None
        post_count = None
        prerelease_type = None
        prerelease = None

        def _parse_type(segment):
            # Discard leading digits (the 0 in 0a1)
            isdigit = operator.methodcaller('isdigit')
            segment = ''.join(itertools.dropwhile(isdigit, segment))
            isalpha = operator.methodcaller('isalpha')
            prerelease_type = ''.join(itertools.takewhile(isalpha, segment))
            prerelease = segment[len(prerelease_type)::]
            return prerelease_type, int(prerelease)
        if _is_int(components[2]):
            patch = int(components[2])
        else:
            # legacy version e.g. 1.2.0a1 (canonical is 1.2.0.0a1)
            # or 1.2.dev4.g1234 or 1.2.b4
            patch = 0
            components[2:2] = [0]
        remainder = components[3:]
        remainder_starts_with_int = False
        try:
            if remainder and int(remainder[0]):
                remainder_starts_with_int = True
        except ValueError:
            pass
        if remainder_starts_with_int:
            # old dev format - 0.1.2.3.g1234
            dev_count = int(remainder[0])
        else:
            if remainder and (remainder[0][0] == '0' or
                              remainder[0][0] in ('a', 'b', 'r')):
                # Current RC/beta layout
                prerelease_type, prerelease = _parse_type(remainder[0])
                remainder = remainder[1:]
            while remainder:
                component = remainder[0]
                if component.startswith('dev'):
                    dev_count = int(component[3:])
                elif component.startswith('post'):
                    dev_count = None
                    post_count = int(component[4:])
                else:
                    raise ValueError(
                        'Unknown remainder %r in %r'
                        % (remainder, version_string))
                remainder = remainder[1:]
        result = SemanticVersion(
            major, minor, patch, prerelease_type=prerelease_type,
            prerelease=prerelease, dev_count=dev_count)
        if post_count:
            if dev_count:
                raise ValueError(
                    'Cannot combine postN and devN - no mapping in %r'
                    % (version_string,))
            result = result.increment().to_dev(post_count)
        return result

    def brief_string(self):
        """Return the short version minus any alpha/beta tags."""
        return "%s.%s.%s" % (self._major, self._minor, self._patch)

    def debian_string(self):
        """Return the version number to use when building a debian package.

        This translates the PEP440/semver precedence rules into Debian version
        sorting operators.
        """
        return self._long_version("~")

    def decrement(self):
        """Return a decremented SemanticVersion.

        Decrementing versions doesn't make a lot of sense - this method only
        exists to support rendering of pre-release versions strings into
        serialisations (such as rpm) with no sort-before operator.

        The 9999 magic version component is from the spec on this - pbr-semver.

        :return: A new SemanticVersion object.
        """
        if self._patch:
            new_patch = self._patch - 1
            new_minor = self._minor
            new_major = self._major
        else:
            new_patch = 9999
            if self._minor:
                new_minor = self._minor - 1
                new_major = self._major
            else:
                new_minor = 9999
                if self._major:
                    new_major = self._major - 1
                else:
                    new_major = 0
        return SemanticVersion(
            new_major, new_minor, new_patch)

    def increment(self, minor=False, major=False):
        """Return an incremented SemanticVersion.

        The default behaviour is to perform a patch level increment. When
        incrementing a prerelease version, the patch level is not changed
        - the prerelease serial is changed (e.g. beta 0 -> beta 1).

        Incrementing non-pre-release versions will not introduce pre-release
        versions - except when doing a patch incremental to a pre-release
        version the new version will only consist of major/minor/patch.

        :param minor: Increment the minor version.
        :param major: Increment the major version.
        :return: A new SemanticVersion object.
        """
        if self._prerelease_type:
            new_prerelease_type = self._prerelease_type
            new_prerelease = self._prerelease + 1
            new_patch = self._patch
        else:
            new_prerelease_type = None
            new_prerelease = None
            new_patch = self._patch + 1
        if minor:
            new_minor = self._minor + 1
            new_patch = 0
            new_prerelease_type = None
            new_prerelease = None
        else:
            new_minor = self._minor
        if major:
            new_major = self._major + 1
            new_minor = 0
            new_patch = 0
            new_prerelease_type = None
            new_prerelease = None
        else:
            new_major = self._major
        return SemanticVersion(
            new_major, new_minor, new_patch,
            new_prerelease_type, new_prerelease)

    def _long_version(self, pre_separator, rc_marker=""):
        """Construct a long string version of this semver.

        :param pre_separator: What separator to use between components
            that sort before rather than after. If None, use . and lower the
            version number of the component to preserve sorting. (Used for
            rpm support)
        """
        if ((self._prerelease_type or self._dev_count)
                and pre_separator is None):
            segments = [self.decrement().brief_string()]
            pre_separator = "."
        else:
            segments = [self.brief_string()]
        if self._prerelease_type:
            segments.append(
                "%s%s%s%s" % (pre_separator, rc_marker, self._prerelease_type,
                              self._prerelease))
        if self._dev_count:
            if not self._prerelease_type:
                segments.append(pre_separator)
            else:
                segments.append('.')
            segments.append('dev')
            segments.append(self._dev_count)
        return "".join(str(s) for s in segments)

    def release_string(self):
        """Return the full version of the package.

        This including suffixes indicating VCS status.
        """
        return self._long_version(".", "0")

    def rpm_string(self):
        """Return the version number to use when building an RPM package.

        This translates the PEP440/semver precedence rules into RPM version
        sorting operators. Because RPM has no sort-before operator (such as the
        ~ operator in dpkg),  we show all prerelease versions as being versions
        of the release before.
        """
        return self._long_version(None)

    def to_dev(self, dev_count):
        """Return a development version of this semver.

        :param dev_count: The number of commits since the last release.
        """
        return SemanticVersion(
            self._major, self._minor, self._patch, self._prerelease_type,
            self._prerelease, dev_count=dev_count)

    def version_tuple(self):
        """Present the version as a version_info tuple.

        For documentation on version_info tuples see the Python
        documentation for sys.version_info.

        Since semver and PEP-440 represent overlapping but not subsets of
        versions, we have to have some heuristic / mapping rules, and have
        extended the releaselevel field to have alphadev, betadev and
        candidatedev values. When they are present the dev count is used
        to provide the serial.
        - a/b/rc take precedence.
        - if there is no pre-release version the dev version is used.
        - serial is taken from the dev/a/b/c component.
        - final non-dev versions never get serials.
        """
        segments = [self._major, self._minor, self._patch]
        if self._prerelease_type:
            type_map = {('a', False): 'alpha',
                        ('b', False): 'beta',
                        ('rc', False): 'candidate',
                        ('a', True): 'alphadev',
                        ('b', True): 'betadev',
                        ('rc', True): 'candidatedev',
                        }
            segments.append(
                type_map[(self._prerelease_type, bool(self._dev_count))])
            segments.append(self._dev_count or self._prerelease)
        elif self._dev_count:
            segments.append('dev')
            segments.append(self._dev_count - 1)
        else:
            segments.append('final')
            segments.append(0)
        return tuple(segments)

class VersionInfo(object):

    def __init__(self, package):
        """Object that understands versioning for a package

        :param package: name of the python package, such as glance, or
                        python-glanceclient
        """
        self.package = package
        self.version = None
        self._cached_version = None
        self._semantic = None

    def __str__(self):
        """Make the VersionInfo object behave like a string."""
        return self.version_string()

    def __repr__(self):
        """Include the name."""
        return "pbr.version.VersionInfo(%s:%s)" % (
            self.package, self.version_string())

    def _get_version_from_pkg_resources(self):
        """Obtain a version from pkg_resources or setup-time logic if missing.

        This will try to get the version of the package from the pkg_resources
        record associated with the package, and if there is no such record
        falls back to the logic sdist would use.
        """
        # Lazy import because pkg_resources is costly to import so defer until
        # we absolutely need it.
        import pkg_resources
        try:
            requirement = pkg_resources.Requirement.parse(self.package)
            provider = pkg_resources.get_provider(requirement)
            result_string = provider.version
        except pkg_resources.DistributionNotFound:
            # The most likely cause for this is running tests in a tree
            # produced from a tarball where the package itself has not been
            # installed into anything. Revert to setup-time logic.
            from pbr import packaging
            result_string = packaging.get_version(self.package)
        return SemanticVersion.from_pip_string(result_string)

    def release_string(self):
        """Return the full version of the package.

        This including suffixes indicating VCS status.
        """
        return self.semantic_version().release_string()

    def semantic_version(self):
        """Return the SemanticVersion object for this version."""
        if self._semantic is None:
            self._semantic = self._get_version_from_pkg_resources()
        return self._semantic

    def version_string(self):
        """Return the short version minus any alpha/beta tags."""
        return self.semantic_version().brief_string()

    # Compatibility functions
    canonical_version_string = version_string
    version_string_with_vcs = release_string

    def cached_version_string(self, prefix=""):
        """Return a cached version string.

        This will return a cached version string if one is already cached,
        irrespective of prefix. If none is cached, one will be created with
        prefix and then cached and returned.
        """
        if not self._cached_version:
            self._cached_version = "%s%s" % (prefix,
                                             self.version_string())
        return self._cached_version


#!/usr/bin/env python
#DOC: https://www.rabbitmq.com/tutorials/tutorial-six-python.html

import pika
from time import sleep
from random import randint

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))

channel = connection.channel()

#por aqui te entran los mensajes
channel.queue_declare(queue='rpc_queue')

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

#props son las pripiedades. Aqui tiene de nuevo:
#   correlation_id (lo que identifica al mensaje)
#   routing_key (la cola donde mandas de vuelta la respuesta)
#   ack: pa decir cuando haya llegado

def on_request(ch, method, props, body):
    n = int(body)

    print(" [.] fib(%s)" % n)
    sleep (randint(1,5))
    response = fib(n)

    ch.basic_publish(exchange='',
                     routing_key=props.reply_to,
                     properties=pika.BasicProperties(correlation_id = \
                                                         props.correlation_id),
                     body=str(response))
    ch.basic_ack(delivery_tag = method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(on_request, queue='rpc_queue')

print(" [x] Awaiting RPC requests")
channel.start_consuming()


import sys
from PIL import Image
import argparse
import math

def image_dimensions(x):
    output = []
    for i in range(1, x + 1):
        if x % i == 0:
            output.append(i)
    center = output[len(output)//2]
    return (x//center, center)

def convertImageToBinary(args):
    image = Image.open(args.image)
    imageData = image.tobytes()
    return imageData

def convertBinToImage(args):
    with open(args.binary, "rb") as f:
        binaryCollections = f.read()
    if not args.fullColour:
        print("Using single colour")
        dimensions =  image_dimensions(len(binaryCollections))
        image = Image.frombytes('L',dimensions,binaryCollections)
    else:
        print("Using colour")
        dimensions =  image_dimensions(int(len(binaryCollections)/3))
        print(dimensions)
        image = Image.frombytes('RGB',dimensions,binaryCollections)
    return image

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("binary", help="The path to the binary/data")
    parser.add_argument("image", help="The path to the image")
    parser.add_argument("output", help="The direction to convert the data (Either: 'image' or 'binary')")
    parser.add_argument("-rgb","--fullColour",action="store_true",required=False,help="Store the data in the colour bands, not just greyscale",dest="fullColour")
    args = parser.parse_args()

    if args.output == "image":
        output = convertBinToImage(args)
        output.save(args.image)
    elif args.output == "binary":
        output = convertImageToBinary(args)
        with open(args.binary,"wb+") as f:
            f.write(output)

    else:
        print("Unknown arg for output: ",args.output)

if __name__ == '__main__':
    main()


#!/usr/bin/python
# coding: utf8

'''
This script convert an image to black and white
python3 -m pip install --upgrade pip
python3 -m pip install --upgrade Pillow
python convert_image_to_black_and_white.py
'''

from PIL import Image

print("Converting...")

img = Image.open("kub.png")
result = img.convert("L")
result.save("kub-bw.png")
result.show()


import itertools
import sys
import time
import random
import math
import pygame

from pygame.locals import *
from MyLibrary import *

def calc_velocity(direction, vel=1.0):
    velocity = Point(0,0)
    if direction == 0: #north
        velocity.y = -vel
    elif direction == 2: #east
        velocity.x = vel
    elif direction == 4: #south
        velocity.y = vel
    elif direction == 6: # west
        velocity.x = -vel
    return velocity

def be_bound():
    if player.X < 0:
        player.X = 0
    elif player.X > 700:
        player.X = 700
    if player.Y < 0:
        player.Y = 0
    elif player.Y > 500:
        player.Y = 500

def move_player():
    player.X += player.velocity.x
    player.Y += player.velocity.y

def reverse_direction(sprite):
    if sprite.direction == 0:
        sprite.direction = 4
    elif sprite.direction == 2:
        sprite.direction = 6
    elif sprite.direction == 4:
        sprite.direction = 0
    elif sprite.direction == 6:
        sprite.direction = 2

def draw_energy_bar():
    pygame.draw.rect(screen, (50,150,50), Rect(300,570,player_health*2,25))
    pygame.draw.rect(screen, (100,200,100), Rect(300,570,200,25), 2)

def update_player():
    player.first_frame = player.direction * player.colums
    player.last_frame = player.first_frame + player.colums - 1
    if player.frame < player.first_frame:
        player.frame = player.first_frame
        pass
    if not player_moving:
        #stop animating when player is not pressing a key
        player.frame = player.first_frame = player.last_frame
    else:
        #move player in direction
        player.velocity = calc_velocity(player.direction, 1.5)
        player.velocity.x *= 1.5
        player.velocity.y *= 1.5

    #manually move the player
    if player_moving:
        move_player()
        be_bound()
    player_group.update(ticks, 50)

def add_health():
    global health_group
    health = MySprite()
    health.load("health.png", 32, 32, 1)
    health.position = random.randint(0,700),random.randint(0,500)
    health_group.add(health)

def add_zombie_to_group():
    global zombie_group
    zombie = MySprite()
    zombie.load("zombie walk.png", 96, 96, 8)
    zombie.position = random.randint(0,700), random.randint(0,500)
    zombie.direction = random.randint(0,3) * 2
    zombie_group.add(zombie)

def add_fire():
    global fire_group
    arrow_image = pygame.image.load("flame.png").convert_alpha()
    width,height = arrow_image.get_size()
    arrow_image = pygame.transform.smoothscale(arrow_image, (width//2,height//2))

    vel = 5
    if player.direction == 0:
        arrow_image = pygame.transform.rotate(arrow_image, 270)
        fire_velocity = Point(0,-vel)
    elif player.direction == 2:
        arrow_image = pygame.transform.rotate(arrow_image, 180)
        fire_velocity = Point(vel,0)
    elif player.direction == 4:
        arrow_image = pygame.transform.rotate(arrow_image, 90)
        fire_velocity = Point(0,vel)
    elif player.direction == 6:
        fire_velocity = Point(-vel,0)

    width,height = arrow_image.get_size()

    arrow = MySprite()
    arrow.load_image(arrow_image, width, height, 1)
    arrow.fire_velocity = fire_velocity
    #arrow.load("flame.png", 40, 16, 1)
    arrow.X = player.X + player.frame_width//2
    arrow.Y = player.Y + player.frame_height//2
    fire_group.add(arrow)

def update_zombie():
    global start_time
    now_time = time.clock()
    seconds = now_time - start_time
    if seconds > 1.0:
        add_zombie_to_group()
        start_time = now_time
    #manually iterate through all the zombies
    for z in zombie_group:
        #set the zombie's animation range
        z.first_frame = z.direction * z.colums
        z.last_frame = z.first_frame + z.colums - 1
        if z.frame < z.first_frame:
            z.frame = z.first_frame

        z.velocity = calc_velocity(z.direction)
        #keep the zombie on the screen
        z.X += z.velocity.x
        z.Y += z.velocity.y
        if z.X < 0 or z.X > 700 or z.Y < 0 or z.Y > 500:
            reverse_direction(z)

    zombie_group.update(ticks, 50)

def check_collistion_zombie():
    #check for collistion with zombies
    attacker = None
    attacker = pygame.sprite.spritecollideany(player, zombie_group)
    global player_health
    if attacker != None:
        #we got a hit, now do a more precise check
        if pygame.sprite.collide_rect_ratio(0.5)(player, attacker):
            player_health -= 10
            if attacker.X < player.X:
                attacker.X -= 10
            elif attacker.X > player.X:
                attacker.X += 10
        else:
            attacker = None

def check_collistion_fire():
    pygame.sprite.groupcollide(fire_group, zombie_group, True, True)

def update_health():
    health_group.update(ticks, 50)
    #check for collistion with health
    global player_health
    healther = None
    healther = pygame.sprite.spritecollideany(player, health_group)
    if healther != None:
        if pygame.sprite.collide_rect_ratio(0.5)(player, healther):
            player_health += 10
            if player_health > 100 : player_health = 100
            healther.X = random.randint(0,700)
            healther.Y = random.randint(0,500)

def update_fire():
    fire_group.update(ticks, 50)

    for fire in fire_group:
        #fire.velocity = calc_velocity(fire.direction,0.5)
        fire.X += fire.fire_velocity.x
        fire.Y += fire.fire_velocity.y
        if fire.X < 0 or fire.X > 700 or fire.Y < 0 or fire.Y > 500:
            fire_group.remove(fire)

#main program begins
pygame.init()
pygame.display.set_caption("Zombie Mob Game")
screen = pygame.display.set_mode((800,600))
font = pygame.font.Font(None, 36)
timer = pygame.time.Clock()

#create sprite groups
player_group = pygame.sprite.Group()
zombie_group = pygame.sprite.Group()
health_group = pygame.sprite.Group()
fire_group = pygame.sprite.Group()

#create player sprite
player = MySprite()
player.load("farmer walk.png", 96, 96, 8)
player.position = 80,80
player.direction = 4
player_group.add(player)

#create health sprite
for n in range(0,3):
    add_health()

player_moving = False
game_over = False
player_health = 100
start_time = time.clock()

#create zombie sprite
zombie_image = pygame.image.load("zombie walk.png").convert_alpha()
for n in range(0, 10):
    add_zombie_to_group()

while True:
    timer.tick(30)
    ticks = pygame.time.get_ticks()

    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYUP:
            if event.key == K_SPACE:
                if not game_over:
                    add_fire()
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()
    elif keys[K_UP] or keys[K_w]:
        player.direction = 0
        player_moving = True
    elif keys[K_RIGHT] or keys[K_d]:
        player.direction = 2
        player_moving = True
    elif keys[K_DOWN] or keys[K_s]:
        player.direction = 4
        player_moving = True
    elif keys[K_LEFT] or keys[K_a]:
        player.direction = 6
        player_moving = True
    else:
        player_moving = False

    if not game_over:
        update_player()
        update_zombie()
        check_collistion_zombie()
        update_health()
        update_fire()
        check_collistion_fire()

    if player_health <= 0:
        game_over = True

    screen.fill((50,50,100))

    player_group.draw(screen)
    zombie_group.draw(screen)
    health_group.draw(screen)
    fire_group.draw(screen)

    #draw energy bar
    draw_energy_bar()

    if game_over:
        print_text(font, 300, 100, "G A M E  O V E R")

    pygame.display.update()


from datetime import date
import json

from kombu.serialization import register
from celery import Celery

from example.models import CashFlowStatement

class Encoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, date):
            return {"__type__": "__date__", "epoch": o.isoformat()}

        if isinstance(o, CashFlowStatement):
            return {"__type__": "cfstmt", "kwargs": o.to_dict()}

        return json.JSONEncoder.default(self, o)

def decoder(o):
    if "__type__" in o:
        if o["__type__"] == "__date__":
            return date.fromisoformat(o["epoch"])
        if o["__type__"] == "cfstmt":
            return CashFlowStatement(**o["kwargs"])
    return o

def dumps(o):
    return json.dumps(o, cls=Encoder)

def loads(s):
    return json.loads(s, object_hook=decoder)

register(
    "fiberjson",
    dumps,
    loads,
    content_type="application/x-fiberjson",
    content_encoding="utf-8",
)

app = Celery(
    "tasks", backend="amqp://redis", broker="amqp://rabbitmq", include="example.tasks"
)

app.conf.update(
    accept_content=["fiberjson"],
    task_serializer="fiberjson",
    result_serializer="fiberjson",
)


import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_Sample_Input_1(self):
        input = """5
WEEWW"""
        output = """1"""
        self.assertIO(input, output)

    def test_Sample_Input_2(self):
        input = """12
WEWEWEEEWWWE"""
        output = """4"""
        self.assertIO(input, output)

    def test_Sample_Input_3(self):
        input = """8
WWWWWEEE"""
        output = """3"""
        self.assertIO(input, output)

    def test_Sample_Input_4(self):
        input = """1
E"""
        output = """0"""
        self.assertIO(input, output)

def resolve():
  # E と W で区間和をとると、i 番目の人をリーダーにした時の振り向く人数が O(1) で出せる。
  # 全体で O(N)
  N = int(input())
  S = list(input())
  E = [0]*(N+1)
  W = [0]*(N+1)
  for i in range(N):
    if S[i]=="E":
      E[i+1] = E[i]+1
      W[i+1] = W[i]
    else:
      E[i+1] = E[i]
      W[i+1] = W[i]+1
  print(min(E[-1] - E[i+1] + W[i] for i in range(N)))

resolve()

if __name__ == "__main__":
    unittest.main()


import os
from pathlib import Path

path2 = './photos'
for i in range(4):
    file_name = "photo{}.jpg"
    open(os.path.join(path2, file_name.format(i)), 'a').close()
files = os.listdir(path2)
print(files)
for file in files:
    fPath = Path(os.path.join(path2, file))
    fPath.rename(fPath.with_suffix('.png'))
files = os.listdir(path2)
print(files)


#! /usr/bin/env python3

import logging
import os
import re
import sys

LOG = logging.getLogger( 'scad' )

LIBRARIES = []

LINE_LENGTH = 120
INCLUDE = re.compile( '^include <(.*)>;$' )
BARS = '/'*LINE_LENGTH + '\n'

def section_comment( string ):
    line = "// ===== %s %s" % ( string, '='*LINE_LENGTH)
    return line[0:LINE_LENGTH]+'\n'

def expand_path( src_path, dst_file ):
    canonical = os.path.abspath( src_path )
    if canonical not in LIBRARIES:
        LOG.info( "Expanding %s", canonical )
        LIBRARIES.append( canonical )
        with open( canonical, 'r' ) as src_file:
            dst_file.write( section_comment( 'Start ' + src_path ) )

            here = os.path.curdir
            os.chdir( os.path.dirname( canonical ) )

            for dummy_index, line in enumerate( src_file ):
                match = INCLUDE.match( line )
                if match:
                    filename = match.group( 1 )
                    expand_path( filename, dst_file )
                else:
                    dst_file.write( line )

            os.chdir( here )

            dst_file.write( section_comment( 'End ' + src_path ) )

if __name__ == '__main__':
    logging.basicConfig( level=logging.WARNING, format='%(asctime)s %(message)s', datefmt='%H:%M:%S' )

    if len( sys.argv ) < 3:
        LOG.error( "usage: %s <source> <destination>", sys.argv[0] )
        sys.exit( 1 )

    src = sys.argv[1]
    dst = sys.argv[2]

    if not os.path.exists( src ):
        LOG.error( "Unable to locate source: %s", src )
        sys.exit( 2 )

    with open( dst, 'w' ) as output:
        expand_path( src, output )


# Standard library imports
import os
import tempfile
from typing import Dict
import unittest

# Third party imports
import numpy as np
import pandas as pd

# Local imports
from mg_nmf.nmf import selection

# Define a test dataset
TEST_DATA: pd.DataFrame = pd.read_csv('data/k2_testdata.csv', index_col=0)

class TestNMFConsensusSelection(unittest.TestCase):
    """Test consensus based selection methods. NMFModelSelection is abstract, so testing the base functions here."""

    sel: selection.NMFConsensusSelection
    data: pd.DataFrame

    @classmethod
    def setUpClass(cls) -> None:
        """Initialise a selection object for our test class to work with, and load some synthetic data."""

        cls.sel = selection.NMFConsensusSelection(
            data=TEST_DATA, k_min=2, k_max=3, solver='mu', beta_loss='frobenius', iterations=10, nmf_max_iter=1000
        )

    def test_run(self):
        """Perform an NMF run on the simple data, check we get the expected outputs."""

        # Run the selection object we set up in the classSetUp
        coph, disp = self.sel.run()
        # Assert that we get two NMFResults objects
        self.assertIsInstance(coph, selection.NMFResults)
        self.assertIsInstance(disp, selection.NMFResults)

    def test_properties(self):
        """Set properties, and check they get changed or throw exceptions as expected. These are base class properties,
        don't need to be tested in other subclasses."""

        # Create a new object to mess with it's properties
        sel: selection.NMFConsensusSelection = selection.NMFConsensusSelection(
            data=TEST_DATA, k_min=2, k_max=3, solver='mu', beta_loss='frobenius', iterations=10, nmf_max_iter=1000
        )

        # Metrics
        with self.assertRaises(Exception, msg='Setting metric to invalid value should raise exception'):
            sel.metric = 'coph'
        sel.metric = 'cophenetic'
        self.assertEqual(sel.metric, 'cophenetic')

        # Solvers
        with self.assertRaises(Exception, msg='Setting solver to invalid value should raise exception'):
            sel.solver = 'magic'
        sel.solver = 'cd'
        self.assertEqual(sel.solver, 'cd')

        # Beta-loss
        with self.assertRaises(Exception, msg='Setting beta-loss to invalid value should raise exception'):
            sel.beta_loss = 'magic'
        sel.beta_loss = 'kullback-leibler'
        self.assertEqual(sel.beta_loss, 'kullback-leibler')

        # k min/max
        # Should refuse values which are too low
        prev_kmin: int = sel.k_min
        sel.k_min = 1
        self.assertEqual(sel.k_min, prev_kmin)
        sel.k_min = -3
        self.assertEqual(sel.k_min, prev_kmin)
        # Max canot be lower than min
        sel.k_min = 4
        sel.k_max = 3
        # Max cannot be changed to lower than min
        self.assertEqual(sel.k_max, sel.k_min)
        sel.k_max = 10
        self.assertEqual(sel.k_max, 10)
        # Ensure iterations cannot be below 1
        sel.iterations = -100
        self.assertEqual(sel.iterations, 1)
        # Ensure nmf_max_iter cannot be below 0, should reset to default
        sel.nmf_max_iter = 0
        self.assertEqual(sel.nmf_max_iter, sel.DEF_NMF_MAX_ITER)

class TestNMFConcordanceSelection(unittest.TestCase):
    """Test Jiang stability based selection methods."""

    sel: selection.NMFConcordanceSelection
    data: pd.DataFrame

    @classmethod
    def setUpClass(cls) -> None:
        """Initialise a selection object for our test class to work with, and load some synthetic data."""

        cls.sel = selection.NMFConcordanceSelection(
            data=TEST_DATA, k_min=2, k_max=3, solver='mu', beta_loss='frobenius', iterations=10, nmf_max_iter=1000
        )

    def test_run(self):
        """Perform an NMF run on the simple data, check we get the expected outputs."""

        # Run the selection object we set up in the classSetUp
        jiang = self.sel.run()
        # Assert that we get two NMFResults objects
        self.assertIsInstance(jiang, selection.NMFResults)

class TestNMFSplitHalfSelection(unittest.TestCase):
    """Test split half based selection methods."""

    sel: selection.NMFSplitHalfSelection
    data: pd.DataFrame

    @classmethod
    def setUpClass(cls) -> None:
        """Initialise a selection object for our test class to work with, and load some synthetic data."""

        cls.sel = selection.NMFSplitHalfSelection(
            data=TEST_DATA, k_min=2, k_max=3, solver='mu', beta_loss='frobenius', iterations=10, nmf_max_iter=1000
        )

    def test_run(self):
        """Perform an NMF run on the simple data, check we get the expected outputs."""

        # Run the selection object we set up in the classSetUp
        ari = self.sel.run()
        # Assert that we get two NMFResults objects
        self.assertIsInstance(ari, selection.NMFResults)

class TestNMFPermutationSelection(unittest.TestCase):
    """Test permutation based selection methods."""

    sel: selection.NMFPermutationSelection
    data: pd.DataFrame

    @classmethod
    def setUpClass(cls) -> None:
        """Initialise a selection object for our test class to work with, and load some synthetic data."""

        cls.sel = selection.NMFPermutationSelection(
            data=TEST_DATA, k_min=2, k_max=3, solver='mu', beta_loss='frobenius', iterations=10, nmf_max_iter=1000
        )

    def test_run(self):
        """Perform an NMF run on the simple data, check we get the expected outputs."""

        # Run the selection object we set up in the classSetUp
        kprop = self.sel.run()
        # Assert that we get two NMFResults objects
        self.assertIsInstance(kprop, selection.NMFResults)

class TestNMFImputationSelection(unittest.TestCase):
    """Test Jiang stability based selection methods."""

    sel: selection.NMFImputationSelection
    data: pd.DataFrame

    @classmethod
    def setUpClass(cls) -> None:
        """Initialise a selection object for our test class to work with, and load some synthetic data."""

        cls.sel = selection.NMFImputationSelection(
            data=TEST_DATA, k_min=2, k_max=3, solver='mu', beta_loss='frobenius', iterations=10, nmf_max_iter=1000,
            p_holdout=0.1, metric='both'
        )

    def test_run(self):
        """Perform an NMF run on the simple data, check we get the expected outputs."""

        # Run the selection object we set up in the classSetUp
        mse, mad = self.sel.run()
        # Assert that we get two NMFResults objects
        self.assertIsInstance(mse, selection.NMFResults)
        self.assertIsInstance(mad, selection.NMFResults)

class TestNMFMultiSelect(unittest.TestCase):
    """Tests for class which allows running of multiple methods of model selection and returns their results."""

    multi: selection.NMFMultiSelect

    @classmethod
    def setUp(cls) -> None:
        """Intialise a model selection object, to use multiple model selection methods."""

        # TEMP - Testing threading
        import os
        os.environ["OMP_NUM_THREADS"] = "1"  # export OMP_NUM_THREADS=4
        os.environ["OPENBLAS_NUM_THREADS"] = "1"  # export OPENBLAS_NUM_THREADS=4
        os.environ["MKL_NUM_THREADS"] = "1"  # export MKL_NUM_THREADS=6
        os.environ["VECLIB_MAXIMUM_THREADS"] = "1"  # export VECLIB_MAXIMUM_THREADS=4
        os.environ["NUMEXPR_NUM_THREADS"] = "1"  # export NUMEXPR_NUM_THREADS=6
        cls.multi = selection.NMFMultiSelect(
            ranks=(2, 6), iterations=100, nmf_max_iter=1000, solver='mu', beta_loss='frobenius'
        )

    def test_run(self) -> None:
        """Perform model selection using different methods."""

        # Temp - used large test data
        import mg_nmf.nmf.synthdata as synthdata
        large_td = synthdata.sparse_overlap_even((50, 150), 6, 0.1, 0.1, 0.1, (0, 1))
        res: Dict[str, selection.NMFResults] = self.multi.run(large_td, 1)

        # Ensure all methods were run, and returned correct type
        for method in selection.NMFMultiSelect.PERMITTED_METHODS:
            self.assertIn(method, res)
            self.assertIsInstance(res[method], selection.NMFResults)

    def test_properties(self) -> None:
        """Test property changes."""

        # Iterations should throw exception if  < 1
        with self.assertRaises(Exception):
            self.multi.iterations = 0
        with self.assertRaises(Exception):
            self.multi.iterations = -1
        self.multi.iterations = 6
        self.assertEqual(self.multi.iterations, 6)

        # Methods should throw an error if asking for method not in list
        with self.assertRaises(Exception):
            self.multi.methods = ['coph', 'error']
        self.multi.methods = ['coph', 'disp']
        self.assertEqual(self.multi.methods, ['coph', 'disp'])

        # Solver should throw an error if asking for solver not in list
        with self.assertRaises(Exception):
            self.multi.solver = 'error'
        self.multi.solver = 'cd'
        self.assertEqual(self.multi.solver, 'cd')

        # Beta loss should throw an error if asking for function not in list
        with self.assertRaises(Exception):
            self.multi.beta_loss = 'error'
        self.multi.beta_loss = 'kullback-leibler'
        self.assertEqual(self.multi.beta_loss, 'kullback-leibler')

class TestNMFResults(unittest.TestCase):
    """Tests for class which holds results for model selection runs for multiple values of k."""

    sel: selection.NMFConcordanceSelection
    res: selection.NMFResults
    K_FROM: int = 2
    K_TO: int = 4
    ITER: int = 5

    @classmethod
    def setUp(cls) -> None:
        """Run a simple model selection method, to obtain an NMFResults object to work with."""

        cls.sel = selection.NMFConcordanceSelection(
            k_min=cls.K_FROM, k_max=cls.K_TO, iterations=cls.ITER, nmf_max_iter=1000, solver='mu',
            beta_loss='frobenius', data=TEST_DATA
        )
        cls.res = cls.sel.run()

    def test_properties(self) -> None:
        """Ensure properties behave as expected."""

        # Correct number of results returned
        self.assertEqual(len(self.res.results), self.K_TO - self.K_FROM + 1)
        # Check that the selected result does actually have optimal value for selection criteria
        top_metric: float = -np.inf
        top_res: selection.NMFModelSelectionResults
        for res in self.res.results:
            if res.metric >= top_metric:
                top_metric = res.metric
                top_k = res
        self.assertIs(self.res.selected, top_k)

    def test_cophenetic_table(self) -> None:
        """Make sure the table of cophenetic correlation comes out as expected."""

        c_tbl: pd.DataFrame = self.res.measure(None, None)
        # Test has expected length
        self.assertEqual(len(c_tbl.columns), len(self.res.results))
        # Test each value in the table, check matches it's table entry
        for i in range(0, len(self.res.results)):
            res_val: float = self.res.results[i].metric
            tbl_val: float = float(c_tbl.iloc[1,i])
            self.assertAlmostEqual(res_val, tbl_val, places=7)

        # Test both plot and file can be written
        with tempfile.TemporaryDirectory() as tmpdirname:
            plot_f, table_f = os.path.join(tmpdirname, 'tmp_plot.png'), os.path.join(tmpdirname, 'tmp_tbl.csv')
            self.res.measure(plot_f, table_f)
            self.assertTrue(os.path.exists(plot_f))
            self.assertTrue(os.path.exists(table_f))

# TODO: NMFModelSelectionResults, Connectivity Matrix

if __name__ == '__main__':
    unittest.main()


import pika

from pydantic import BaseModel

from nasajon.settings import logger, RABBITMQ_HOST, RABBITMQ_PORT, RABBITMQ_VHOST

from nsj_gcf_utils.json_util import json_dumps

class AsynServiceBase:

    def __init__(self, async_queue_name: str, queue_ttl=86400, queue_delay=900):
        super().__init__()
        self._async_queue_name = async_queue_name
        self._async_queue_name_delay = f"{async_queue_name}_delay"
        self._queue_ttl = queue_ttl * 1000
        self._queue_delay = queue_delay * 1000

    def handle_message_befor_enqueue(self, msg: BaseModel) -> BaseModel:
        return msg

    def enqueue(self, msg: BaseModel):

        msg = self.handle_message_befor_enqueue(msg)

        with pika.BlockingConnection(
            pika.ConnectionParameters(
                host=RABBITMQ_HOST,
                virtual_host=RABBITMQ_VHOST,
                port=RABBITMQ_PORT)
        ) as connection:
            channel = connection.channel()

            # Criando a fila para processamento das mensagens
            channel.queue_declare(
                queue=self._async_queue_name,
                durable=True,
                arguments={
                    "x-message-ttl": self._queue_ttl,
                    'x-dead-letter-exchange': '',
                    'x-dead-letter-routing-key': self._async_queue_name_delay
                }
            )

            # Criando a fila de mortos (para atraso dos erros)
            channel.queue_declare(
                queue=self._async_queue_name_delay,
                durable=True,
                arguments={
                    "x-message-ttl": self._queue_delay,
                    'x-dead-letter-exchange': '',
                    'x-dead-letter-routing-key': self._async_queue_name
                }
            )

            # Publicando a mensagem (e garantindo o roteamento para a fila de mortos)
            channel.basic_publish(
                exchange='',
                routing_key=self._async_queue_name,
                body=json_dumps(msg),
                properties=pika.BasicProperties(
                    delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE
                )
            )
            logger.info("Nova mensagem enfileirada.")
            logger.debug(f"Dados da mensagem enfileirada: {json_dumps(msg)}")


from typing import Any, List, Optional

from llama_index.bridge.pydantic import Field, PrivateAttr
from llama_index.callbacks import CallbackManager
from llama_index.embeddings.base import DEFAULT_EMBED_BATCH_SIZE, BaseEmbedding
from llama_index.embeddings.huggingface_utils import (
    DEFAULT_INSTRUCT_MODEL,
    get_query_instruct_for_model_name,
    get_text_instruct_for_model_name,
)

class InstructorEmbedding(BaseEmbedding):
    query_instruction: Optional[str] = Field(
        description="Instruction to prepend to query text."
    )
    text_instruction: Optional[str] = Field(
        description="Instruction to prepend to text."
    )
    cache_folder: Optional[str] = Field(
        description="Cache folder for huggingface files."
    )

    _model: Any = PrivateAttr()

    def __init__(
        self,
        model_name: str = DEFAULT_INSTRUCT_MODEL,
        query_instruction: Optional[str] = None,
        text_instruction: Optional[str] = None,
        embed_batch_size: int = DEFAULT_EMBED_BATCH_SIZE,
        cache_folder: Optional[str] = None,
        device: Optional[str] = None,
        callback_manager: Optional[CallbackManager] = None,
    ):
        try:
            from InstructorEmbedding import INSTRUCTOR
        except ImportError:
            raise ImportError(
                "InstructorEmbedding requires instructor to be installed.\n"
                "Please install transformers with `pip install InstructorEmbedding`."
            )
        self._model = INSTRUCTOR(model_name, cache_folder=cache_folder, device=device)

        super().__init__(
            embed_batch_size=embed_batch_size,
            callback_manager=callback_manager,
            model_name=model_name,
            query_instruction=query_instruction,
            text_instruction=text_instruction,
            cache_folder=cache_folder,
        )

    @classmethod
    def class_name(cls) -> str:
        return "InstructorEmbedding"

    def _format_query_text(self, query_text: str) -> List[str]:
        """Format query text."""
        instruction = self.text_instruction

        if instruction is None:
            instruction = get_query_instruct_for_model_name(self.model_name)

        return [instruction, query_text]

    def _format_text(self, text: str) -> List[str]:
        """Format text."""
        instruction = self.text_instruction

        if instruction is None:
            instruction = get_text_instruct_for_model_name(self.model_name)

        return [instruction, text]

    def _embed(self, instruct_sentence_pairs: List[List[str]]) -> List[List[float]]:
        """Embed sentences."""
        return self._model.encode(instruct_sentence_pairs).tolist()

    def _get_query_embedding(self, query: str) -> List[float]:
        """Get query embedding."""
        query_pair = self._format_query_text(query)
        return self._embed([query_pair])[0]

    async def _aget_query_embedding(self, query: str) -> List[float]:
        """Get query embedding async."""
        return self._get_query_embedding(query)

    async def _aget_text_embedding(self, text: str) -> List[float]:
        """Get text embedding async."""
        return self._get_text_embedding(text)

    def _get_text_embedding(self, text: str) -> List[float]:
        """Get text embedding."""
        text_pair = self._format_text(text)
        return self._embed([text_pair])[0]

    def _get_text_embeddings(self, texts: List[str]) -> List[List[float]]:
        """Get text embeddings."""
        text_pairs = [self._format_text(text) for text in texts]
        return self._embed(text_pairs)


import random

from type import Type
import pygame

class Model:
    def __init__(self, x_coordinate, y_coordinate):
        self.type = random.choice(list(Type))
        self.x_coordinate = x_coordinate
        self.y_coordinate = y_coordinate
        self.x_speed, self.y_speed = 1, 1
        self.WIDTH, self.HEIGHT = 80, 60
        self.scaled_image = pygame.transform.scale(self.type.value, (self.WIDTH, self.HEIGHT))
        self.rect = pygame.Rect(self.x_coordinate, self.y_coordinate, self.WIDTH, self.HEIGHT)

    # def collision(self, second:Model):
    #     if abs(second.rect.top - self.rect.bottom) < 10 and self.y_speed > 0:
    #         self.y_speed *= -1
    #     if abs(second.rect.bottom - self.rect.top) < 10 and self.y_speed < 0:
    #         self.y_speed *= -1
    #     if abs(second.rect.right - self.rect.left) < 10 and self.x_speed < 0:
    #         self.x_speed *= -1
    #     if abs(second.rect.left - self.rect.right) < 10 and self.x_speed > 0:
    #         self.x_speed *= -1


# -*- coding: utf-8 -*-
"""
Created on Wed Mar 31 10:32:17 2021

@author: Edgar Sanchez
"""
import os
import matplotlib.pyplot as plt

def count(func):
    def wrapper(*args, **kwargs):
        wrapper.counter += 1    # executed every time the wrapped function is called
        return func(*args, **kwargs)
    wrapper.counter = 0         # executed only once in decorator definition time
    return wrapper

def save_train_traj(path, df_model_training, valid=True):
    if not os.path.exists(path):
        os.makedirs(path)
    df_model_training.to_csv(path+'/Training.csv')

    # Save training curve
    train_loss = df_model_training['Train_loss'].to_list()
    if valid:
        valid_loss = df_model_training['Valid_loss'].to_list()

    fig = plt.figure()
    plt.plot(train_loss, label='Training')
    if valid:
        plt.plot(valid_loss, label='Validation')
    plt.xlabel('Epoch')
    plt.ylabel('Loss function')
    plt.yscale('log')
    plt.legend()
    plt.close(fig)
    fig.savefig(path+'/Training.png')


import numpy as np
from abc import ABC, abstractmethod
from .geometry import *
from .tools import distance, istypes, unsupported

__all__ = ["AbstractConstraint", "SamePointConstraint", "SameLengthConstraint",
           "IntersectsConstraint"]

class AbstractConstraint(ABC):
    def __init__(self, *objects):
        self.objects = objects

    # error should always be non-negative, trying to minimize it
    def error(self, objects):
        objects = [object.compute(objects) for object in self.objects]
        # squaring keeps non-negative and improves tendency to satisfy
        # all constraints well rather than a few poorly and others greatly
        err = self._error(objects)
        if err is not None:
            return err**2
        else:
            unsupported(self, objects)

    @abstractmethod
    def _error(self, points):
        pass

class SamePointConstraint(AbstractConstraint):
    def _error(self, objects):
        return distance(objects[0], objects[1])

class SameLengthConstraint(AbstractConstraint):
    def _error(self, objects):
        return objects[0].length() - objects[1].length()

class IntersectsConstraint(AbstractConstraint):
    def _error(self, objects):
        if istypes(objects, [Circle, Point]):
            return distance(objects[0].center, objects[1]) - objects[0].radius
        elif istypes(objects, [Point, Circle]):
            return self._error(objects[::-1])


import random
from stockfish import Stockfish
import chess

class Player:
    def __init__(self, controller="random", customFunction=None):
        # "random", "gp", "alphazero", "*anyother we're planning to implement"
        self.controller = controller
        self.customFunction = customFunction
        # add any other variables as needed
        self.stockfish = Stockfish('./stockfish-10-win/Windows/stockfish_10_x64.exe')

    def choose_move(self, board, moves):
        if self.controller == "random":
            chosenMoveIndex = random.randint(0, len(moves)-1)
            chosenMove = moves[chosenMoveIndex]
            return chosenMove
        elif self.controller == "custom":
            return self.customFunction(board, moves)
        elif self.controller == "stockfish":
            self.stockfish.set_fen_position(board.fen())
            uciStr = self.stockfish.get_best_move()
            move = chess.Move.from_uci(uciStr)
            return move
        elif self.controller == "player":
            while True:
                print(board)
                move = input("Input a move, or input m for a list of possible moves: ")
                if move == 'm':
                    for move in moves:
                        print(move)
                    continue
                try:
                    move = chess.Move.from_uci(move)
                    if move in moves:
                        return move
                    else:
                        print("Invalid move")
                except:
                    print("Invalid move")
                    continue
        else:
            # implement controller's movement choice
            pass