import numpy as np
import pandas as pd
import os
import time
import csv
from PIL import Image
from paths import *
from sklearn.metrics import roc_auc_score, f1_score

import torch
import torch.nn as nn
from torch import optim
from torchvision import models
from timeit import default_timer as timer

# the train function below is borrowed heavily (with minor modifications)
# from WillKoehrsen, jupyter notebook here https://github.com/WillKoehrsen/pytorch_challenge/blob/master/Transfer%20Learning%20in%20PyTorch.ipynb
# model train/eval function
def train(model,
          criterion,
          optimizer,
          train_loader,
          valid_loader,
          save_file_path,
          final_file_path,
          mod,
          max_epochs_stop=3,
          n_epochs=20,
          print_every=2,
          cuda_device=0):
    """Train a PyTorch Model

    Params
    --------
        model (PyTorch model): cnn to train
        criterion (PyTorch loss): objective to minimize
        optimizer (PyTorch optimizier): optimizer to compute gradients of model parameters
        train_loader (PyTorch dataloader): training dataloader to iterate through
        valid_loader (PyTorch dataloader): validation dataloader used for early stopping
        save_file_path (str ending in '.pt'): file path to save the model info
        final_file_path (str): file path for optimum model state dict
        max_epochs_stop (int): maximum number of epochs with no improvement in validation loss for early stopping
        n_epochs (int): maximum number of training epochs
        print_every (int): frequency of epochs to print training stats
        cuda_device (int): if more than one gpu available, select gpu to run training on
        mod (str): string of cnn model

    Returns
    --------
        model (PyTorch model): trained cnn with best weights
        history (DataFrame): history of train and validation loss and accuracy
    """

    # Early stopping intialization
    epochs_no_improve = 0
    valid_loss_min = np.Inf

    valid_max_acc = 0
    history = []

    # Number of epochs already trained (if using loaded in model weights)
    try:
        print(f'Model has been trained for: {model.epochs} epochs.\n')
    except:
        model.epochs = 0
        print(f'Starting Training from Scratch.\n')

    overall_start = timer()

    start_epoch = model.epochs

    # Main loop
    for epoch in range(start_epoch, n_epochs):

        # keep track of training and validation loss each epoch
        train_loss = 0.0
        valid_loss = 0.0

        train_acc = 0
        valid_acc = 0

        # f1 score train/valid
        y_true_train = np.array([])
        y_score_train = np.array([])
        y_true_valid = np.array([])
        y_score_valid = np.array([])

        # Set to training
        model.train()
        start = timer()

        # Training loop
        for ii, (data, target) in enumerate(train_loader):
            # Tensors to gpu
            data, target = data.cuda(cuda_device), target.cuda(cuda_device)

            # Clear gradients
            optimizer.zero_grad()
            # Predicted outputs are log probabilities
            output = model(data)

            # Loss and backpropagation of gradients
            loss = criterion(output, target)
            loss.backward()

            # Update the parameters
            optimizer.step()

            # Track train loss by multiplying average loss by number of examples in batch
            train_loss += loss.item() * data.size(0)

            # predictions
            _, pred = torch.max(output, dim=1)

            # append label/predicted values for f1 scoring later
            y_true = target.cpu().numpy()
            y_score = pred.cpu().numpy()

            y_true_train = np.append(y_true_train, y_true)
            y_score_train = np.append(y_score_train, y_score)

            # accuracy
            correct_tensor = pred.eq(target.data.view_as(pred))
            # Need to convert correct tensor from int to float to average
            accuracy = torch.mean(correct_tensor.type(torch.FloatTensor))
            # Multiply average accuracy times the number of examples in batch
            train_acc += accuracy.item() * data.size(0)

            # Track training progress
            print(
                f'Epoch: {epoch}\t{100 * (ii + 1) / len(train_loader):.2f}% complete. {timer() - start:.2f} seconds elapsed in epoch.',
                end='\r')

        # After training loops ends, start validation
        else:
            model.epochs += 1

            # Don't need to keep track of gradients
            with torch.no_grad():
                # Set to evaluation mode
                model.eval()

                # Validation loop
                for data, target in valid_loader:
                    # Tensors to gpu
                    data, target = data.cuda(cuda_device), target.cuda(cuda_device)

                    # Forward pass
                    output = model(data)

                    # Validation loss
                    loss = criterion(output, target)
                    # Multiply average loss times the number of examples in batch
                    valid_loss += loss.item() * data.size(0)

                    # predictions
                    _, pred = torch.max(output, dim=1)

                    y_true = target.cpu().numpy()
                    y_score = pred.cpu().numpy()

                    y_true_valid = np.append(y_true_valid, y_true)
                    y_score_valid = np.append(y_score_valid, y_score)

                    # Calculate validation accuracy
                    correct_tensor = pred.eq(target.data.view_as(pred))
                    accuracy = torch.mean(
                        correct_tensor.type(torch.FloatTensor))
                    # Multiply average accuracy times the number of examples
                    valid_acc += accuracy.item() * data.size(0)

                # f1 scores
                f1_train = f1_score(y_true_train, y_score_train, average='weighted')
                f1_valid = f1_score(y_true_valid, y_score_valid, average='weighted')

                # Calculate average losses
                train_loss = train_loss / len(train_loader.dataset)
                valid_loss = valid_loss / len(valid_loader.dataset)

                # Calculate average accuracy
                train_acc = train_acc / len(train_loader.dataset)
                valid_acc = valid_acc / len(valid_loader.dataset)

                history.append([train_loss, valid_loss, train_acc, valid_acc, f1_train, f1_valid])

                # Save Model in case script needs to be run later
                torch.save({
                            'epoch': epoch,
                            'model_state_dict': model.state_dict(),
                            'optimizer_state_dict': optimizer.state_dict(),
                            'loss': loss,
                            }, save_file_path)

                # Print training and validation results
                if (epoch + 1) % print_every == 0:
                    print(
                        f'\nEpoch: {epoch} \tTraining Loss: {train_loss:.4f} \tValidation Loss: {valid_loss:.4f}'
                    )
                    print(
                        f'\t\tTraining Accuracy: {100 * train_acc:.2f}%\t Validation Accuracy: {100 * valid_acc:.2f}%'
                    )
                    print(
                        f'\t\tTraining f1 score: {f1_train:.4f} \t Validation f1 score: {f1_valid:.4f}'
                    )

                # Save the model if validation loss decreases
                if valid_loss < valid_loss_min:
                    # # Save model

                    torch.save({
                                'epoch': epoch,
                                'model_state_dict': model.state_dict(),
                                'optimizer_state_dict': optimizer.state_dict(),
                                'loss': loss,
                                }, final_file_path)

                    # Track improvement
                    epochs_no_improve = 0
                    valid_loss_min = valid_loss
                    valid_best_acc = valid_acc
                    best_epoch = epoch

                # Otherwise increment count of epochs with no improvement
                else:
                    epochs_no_improve += 1

                    # Trigger early stopping
                    if epochs_no_improve >= max_epochs_stop:
                        print(
                            f'\nEarly Stopping! Total epochs: {epoch}. Best epoch: {best_epoch} with loss: {valid_loss_min:.2f} and acc: {100 * valid_best_acc:.2f}%'
                        )
                        total_time = timer() - overall_start
                        print(
                            f'{total_time:.2f} total seconds elapsed. {total_time / (epoch+1):.2f} seconds per epoch.'
                        )

                        # Load the best state dict
                        torch.save({
                            'epoch': epoch,
                            'model_state_dict': model.state_dict(),
                            'optimizer_state_dict': optimizer.state_dict(),
                            'loss': loss,
                            }, save_file_path)
                        # Attach the optimizer
                        model.optimizer = optimizer

                        # Format history
                        history = pd.DataFrame(
                            history,
                            columns=['train_loss', 'valid_loss', 'train_acc','valid_acc', 'f1_train', 'f1_valid'])
                        history['model'] = mod

                        return model, history

    # Attach the optimizer
    model.optimizer = optimizer
    # Record overall time and print out stats
    total_time = timer() - overall_start

    history = pd.DataFrame(
        history,
        columns=['train_loss', 'valid_loss', 'train_acc', 'valid_acc', 'f1_train', 'f1_valid'])
    history['model'] = mod

    return model, history

def run_training(mod,
                 cuda_device,
                 size,
                 train_loader,
                 test_loader,
                 n_epochs):

    """Setup model parameters based on inputs
       and runs train function for various pre-trained models

    Params
    --------
        mod (str): pre-trained model
        cuda_device (int): if more than one gpu available, select gpu to run training on
        size (int): used to resize images. input of 225 will resize images to 225x225 pixels.
        train_loader (PyTorch dataloader): dataloader for training dataset with augmentations applied
        test_loader (PyTorch dataloader): dataloader for testing dataset
        n_epochs (int): Number of epochs to train model

    Returns
    --------
        N/A
    """

    # read in pre-trained models
    resnet18 = models.resnet18(pretrained=True)
    alexnet = models.alexnet(pretrained=True)
    vgg16 = models.vgg16(pretrained=True)
    densenet = models.densenet161(pretrained=True)
    googlenet = models.googlenet(pretrained=True)
    shufflenet = models.shufflenet_v2_x1_0(pretrained=True)
    resnext50_32x4d = models.resnext50_32x4d(pretrained=True)
    wide_resnet50_2 = models.wide_resnet50_2(pretrained=True)

    # set model to mod
    model = eval(mod)

    # Freeze model weights
    for param in model.parameters():
        param.requires_grad = False

    # define linear/classifier layers.
    # refer to jup_notebook folder for details. This was largely manual.
    if mod == 'resnet18':
        model.fc = nn.Linear(in_features=512, out_features=2, bias=True)
    if mod == 'alexnet':
        model.classifier[6] = nn.Sequential(
                            nn.Linear(4096, 256),
                            nn.ReLU(),
                            nn.Dropout(0.4),
                            nn.Linear(256, 2),
                            nn.LogSoftmax(dim=1))
    if mod == 'vgg16':
        model.classifier[6] =  nn.Sequential(
                            nn.Linear(4096, 256),
                            nn.ReLU(),
                            nn.Dropout(0.4),
                            nn.Linear(256, 2),
                            nn.LogSoftmax(dim=1))
    if mod == 'densenet':
        model.classifier = nn.Linear(in_features=2208, out_features=2, bias=True)
    if mod == 'googlenet':
        model.fc = nn.Linear(in_features=1024, out_features=2, bias=True)
    if mod == 'shufflenet':
        model.fc = nn.Linear(in_features=1024, out_features=2, bias=True)
    if mod == 'resnext50_32x4d':
        model.fc = nn.Linear(in_features=2048, out_features=2, bias=True)
    if mod == 'wide_resnet50_2':
        model.fc = nn.Linear(in_features=2048, out_features=2, bias=True)

    # set gpu
    cuda_device=cuda_device

    # Move to gpu
    model = model.cuda(cuda_device)

    # Loss and optimizer
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters())

    # file names
    save_file_name = mod + '_' + str(size) + '.pt'
    save_file_path = model_path/save_file_name
    final_file_path = model_path/str('final_' + save_file_name)
    # train_on_gpu = torch.cuda.is_available()

    # if .pt file already exists, load
    if os.path.isfile(save_file_path):
        checkpoint = torch.load(save_file_path)
        model.load_state_dict(checkpoint['model_state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
        epoch = checkpoint['epoch'] + 1
        loss = checkpoint['loss']

        model.epochs = epoch
    else:
        pass

    model, history = train(
        model,
        criterion,
        optimizer,
        train_loader,
        test_loader,
        save_file_path=save_file_path,
        final_file_path= final_file_path,
        max_epochs_stop=3,
        n_epochs=n_epochs,
        print_every=1,
        cuda_device=cuda_device,
        mod=mod)

    with open(model_path/str('model_eval_'+ str(size) + '.csv'), 'a') as f:
        history.to_csv(f, header=f.tell()==0, index=False)

    with open(model_path/str('model_info_'+ str(size) + '.csv'), 'a') as fd:
        wr = csv.writer(fd)
        wr.writerow([str(model)])


import pandas as pd
import streamlit as st
import plotly.express as px
from database import view_all_data
def read():
    result = view_all_data()
    df = pd.DataFrame(result, columns=['Order ID', 'Customer Name', 'Plant Name','Pot Size', 'Price'])
    st.dataframe(df)
    with st.expander("Plants:"):
        plant_df = df['Plant Name'].value_counts().to_frame()
        plant_df = plant_df.reset_index()
        st.dataframe(plant_df)
        p1 = px.pie(plant_df, names='index', values='Plant Name')
        st.plotly_chart(p1)


from hathor import settings
from hathor.cli import audio as cli
from hathor.exc import CLIException
from tests import utils as test_utils

class TestGlobalArgs(test_utils.TestHelper):
    def setUp(self):
        # common function to test globals
        self.func = ['tags', 'show', 'foo']

    def test_defaults(self):
        args = cli.parse_args(self.func)
        self.assertEqual(args.pop('module'), 'tags')
        self.assertEqual(args.pop('command'), 'show')
        self.assertEqual(args.pop('column_limit'), settings.COLUMN_LIMIT_DEFAULT)
        self.assertEqual(args.pop('reverse_sort'), False)
        args.pop('input_file', None)
        for key in ['keys', 'sort_key']:
            self.assertEqual(args.pop(key), None)
        self.assert_length(args.keys(), 0)

    def test_column_limit(self):
        with self.assertRaises(CLIException) as error:
            cli.parse_args(['-c', 'foo'] + self.func)
        self.check_error_message("argument -c/--column-limit:"
                                 " invalid int value: 'foo'", error)

        args = cli.parse_args(['-c', '20'] + self.func)
        self.assertEqual(args['column_limit'], 20)
        args = cli.parse_args(['--column-limit', '20'] + self.func)
        self.assertEqual(args['column_limit'], 20)

    def test_keys(self):
        args = cli.parse_args(['-k', 'foo'] + self.func)
        self.assertEqual(args['keys'], 'foo')
        args = cli.parse_args(['--keys', 'foo'] + self.func)
        self.assertEqual(args['keys'], 'foo')

    def test_sort_key(self):
        args = cli.parse_args(['-sk', 'foo'] + self.func)
        self.assertEqual(args['sort_key'], 'foo')
        args = cli.parse_args(['--sort-key', 'foo'] + self.func)
        self.assertEqual(args['sort_key'], 'foo')

class TestTagArgs(test_utils.TestHelper):
    def test_tags_show(self):
        expected = {
            'module' : 'tags',
            'command' : 'show',
            'input_file' : 'foo',
        }
        args = cli.parse_args(['tags', 'show', 'foo'])
        for key, value in expected.items():
            self.assertEqual(args[key], value)

    def test_tags_delete(self):
        expected = {
            'module' : 'tags',
            'command' : 'delete',
            'input_file' : 'foo',
            'tag_list' : ['bar', 'derp'],
        }
        args = cli.parse_args(['tags', 'delete', 'foo', 'bar', 'derp'])
        for key, value in expected.items():
            self.assertEqual(args[key], value)

    def test_tags_update(self):
        expected = {
            'module' : 'tags',
            'command' : 'update',
            'input_file' : 'foo',
            'key_values' : {
                'bar' : 'derp',
                'herp' : 'sherp',
            }
        }
        args = cli.parse_args(['tags', 'update', 'foo', '{"bar":"derp","herp":"sherp"}'])
        for key, value in expected.items():
            self.assertEqual(args[key], value)

        with self.assertRaises(CLIException) as error:
            cli.parse_args(['tags', 'update', 'foo', 'bar'])
        self.check_error_message("argument key_values:"
                                 " invalid loads value: 'bar'", error)

class TestPictureArgs(test_utils.TestHelper):
    def test_picture_extract(self):
        expected = {
            'module' : 'picture',
            'command' : 'extract',
            'input_file' : 'foo',
            'output_file' : 'bar',
        }
        args = cli.parse_args(['picture', 'extract', 'foo', 'bar'])
        for key, value in expected.items():
            self.assertEqual(args[key], value)

    def test_picture_update(self):
        expected = {
            'module' : 'picture',
            'command' : 'update',
            'audio_file' : 'foo',
            'picture_file' : 'bar',
        }
        args = cli.parse_args(['picture', 'update', 'foo', 'bar'])
        for key, value in expected.items():
            self.assertEqual(args[key], value)

        common_args = ['picture', 'update', 'foo', 'bar']

        with self.assertRaises(CLIException) as error:
            cli.parse_args(common_args + ['--encoding', 'foo'])
        self.check_error_message("argument --encoding:"
                                 " invalid int value: 'foo'",
                                 error)
        with self.assertRaises(CLIException) as error:
            cli.parse_args(common_args + ['--picture-type', 'foo'])
        self.check_error_message("argument --picture-type:"
                                 " invalid int value: 'foo'",
                                 error)

        args = cli.parse_args(common_args + ['--encoding', '3'])
        self.assertEqual(args['encoding'], 3)

        args = cli.parse_args(common_args + ['--picture-type', '4'])
        self.assertEqual(args['picture_type'], 4)

        args = cli.parse_args(common_args + ['--description', 'derp'])
        self.assertEqual(args['description'], 'derp')


import sys

# * ========================================================
# * @Title       : 1504
# * @Path        : python\02_Solved\boj\g.최단경로\02_1504.py
# * @Link        : https://www.acmicpc.net/problem/1504
# * @Description : 특정한 최단 경로
# * @Note        : 비둘기집의 원리...?, https://www.acmicpc.net/board/view/59341 <- java
# * @Date        : 2021-08-10 08:54:25
# * --------------------------------------------------------
# * @Author      : Inseong-so(https://github.com/inseong-so)
# * ========================================================

# Solve 함수
def solution(vertex, edge, graph, require_path):
    from heapq import heappop, heappush
    INF = sys.maxsize
    v1, v2 = require_path  # 거쳐야 하는 정점

    def dijkstra(start):
        distance = [INF for _ in range(vertex + 1)]
        distance[start] = 0
        heap = [(0, start)]
        while heap:
            lw, lv = heappop(heap)
            if distance[lv] < lw:
                continue
            for nv, w in graph[lv]:
                nw = lw + w
                if distance[nv] > nw:
                    distance[nv] = nw
                    heappush(heap, (nw, nv))
        return distance

    # 1번 정점에서 N번 정점으로 이동할 때, 주어진 두 정점을 반드시 거치려면?
    # 1 > v1 > v2 > N
    # 1 > v2 > v1 > N
    # 총 두 가지 케이스
    # 중복으로 발생하는 경우에 대한 처리가 되어있지 않음

    start_1 = dijkstra(1)       # 1 to vertex에 대한 가중치
    start_v1 = dijkstra(v1)     # v1 to vertex에 대한 가중치
    start_v2 = dijkstra(v2)     # v2 to vertex에 대한 가중치
    # 위의 케이스를 점화식 -> 코드로 구현
    # 1 > v1 > v2 > N
    w1 = start_1[v1] + start_v1[v2] + start_v2[vertex]
    # 1 > v2 > v1 > N
    w2 = start_1[v2] + start_v2[v1] + start_v1[vertex]
    if w1 >= INF or w2 >= INF:
        print(-1)
    else:
        print(min(w1, w2))

# 입력
vertex, edge = list(map(int, sys.stdin.readline().split()))
graph = [[] for _ in range(vertex + 1)]
for _ in range(edge):
    u, v, w = list(map(int, sys.stdin.readline().split()))
    graph[u].append((v, w))
    graph[v].append((u, w))
require_path = list(map(int, sys.stdin.readline().split()))

solution(vertex, edge, graph, require_path)

def test_01(capfd):
    vertex = 4
    edge = 6
    pairs = [
        [1, 2, 3],
        [2, 3, 3],
        [3, 4, 1],
        [1, 3, 5],
        [2, 4, 5],
        [1, 4, 4],
    ]
    graph = [[] for _ in range(vertex + 1)]
    for p in pairs:
        u, v, w = p
        graph[u].append((v, w))
        graph[v].append((u, w))
    require_path = [2, 3]
    solution(vertex, edge, graph, require_path)
    out, err = capfd.readouterr()
    result = '7\n'
    assert out == result

def test_02(capfd):
    vertex = 4
    edge = 3
    pairs = [
        [1, 4, 1],
        [2, 4, 2],
        [3, 4, 3],
    ]
    graph = [[] for _ in range(vertex + 1)]
    for p in pairs:
        u, v, w = p
        graph[u].append((v, w))
        graph[v].append((u, w))
    require_path = [2, 3]
    solution(vertex, edge, graph, require_path)
    out, err = capfd.readouterr()
    result = '11\n'
    assert out == result

def test_03(capfd):
    vertex = 3
    edge = 3
    pairs = [
        [1, 2, 1],
        [1, 3, 3],
        [2, 3, 1],
    ]
    graph = [[] for _ in range(vertex + 1)]
    for p in pairs:
        u, v, w = p
        graph[u].append((v, w))
        graph[v].append((u, w))
    require_path = [1, 3]
    solution(vertex, edge, graph, require_path)
    out, err = capfd.readouterr()
    result = '2\n'
    assert out == result

def test_04(capfd):
    vertex = 4
    edge = 5
    pairs = [
        [1, 3, 4],
        [1, 4, 6],
        [3, 4, 1],
        [4, 2, 2],
        [2, 3, 3],
    ]
    graph = [[] for _ in range(vertex + 1)]
    for p in pairs:
        u, v, w = p
        graph[u].append((v, w))
        graph[v].append((u, w))
    require_path = [1, 3]
    solution(vertex, edge, graph, require_path)
    out, err = capfd.readouterr()
    result = '5\n'
    assert out == result

def test_05(capfd):
    vertex = 4
    edge = 6
    pairs = [
        [1, 4, 1],
        [4, 2, 1],
        [2, 3, 1],
        [2, 1, 1],
        [3, 2, 1],
    ]
    graph = [[] for _ in range(vertex + 1)]
    for p in pairs:
        u, v, w = p
        graph[u].append((v, w))
        graph[v].append((u, w))
    require_path = [1, 3]
    solution(vertex, edge, graph, require_path)
    out, err = capfd.readouterr()
    result = '4\n'
    assert out == result

def test_06(capfd):
    vertex = 5
    edge = 4
    pairs = [
        [1, 2, 3],
        [2, 5, 3],
        [3, 4, 1],
        [1, 5, 5],
    ]
    graph = [[] for _ in range(vertex + 1)]
    for p in pairs:
        u, v, w = p
        graph[u].append((v, w))
        graph[v].append((u, w))
    require_path = [2, 3]
    solution(vertex, edge, graph, require_path)
    out, err = capfd.readouterr()
    result = '-1\n'
    assert out == result


import pygame
import time
import math
import random
import numpy
from numpy import genfromtxt

#slider code imported from https://www.dreamincode.net/forums/topic/401541-buttons-and-sliders-in-pygame/

#defined stuff
col_white = pygame.Color(255,255,255)
col_grey = pygame.Color(79 ,79,79)
col_blue1 = pygame.Color(116,172,199)
col_blue2 = pygame.Color(65,122,146)
col_yellow = pygame.Color(254,177,67)
col_red = pygame.Color(178,69,61)

#graphical sizes and such
MARGIN_Y = 20
MARGIN_X = 30
NODE_WIDTH = 75
NODE_HEIGHT = 300
NODE_SPACE_SIDES = 20
CORNER_DEPTH = 50
NODE_SPACE_MIDDLE = 10

#test array (AB,AC,BA,BC,CA,CB)
flow = [200,300,300,500,200,400]

class Slider():
    def __init__(self, maxi, mini):
        self.maxi = maxi  # maximum at slider position right
        self.mini = mini  # minimum at slider position left
        self.valA = self.mini  # first boundary
        self.valB = self.maxi  # first boundary
        self.hitA = False  # first boundary
        self.hitB = False  # first boundary

        self.high = self.maxi
        self.low = self.mini

        global slidescreen

        self.maxwidth = slidescreen.get_width()-MARGIN_X-MARGIN_X
        self.scalar = self.maxwidth/(self.maxi-self.mini)

        self.rect_sliderA = pygame.Rect((self.valA, 0), (20, 40))
        self.rect_sliderB = pygame.Rect((self.valB, 0), (20, 40))
        pygame.draw.rect(slidescreen, col_blue1, self.rect_sliderA)
        pygame.draw.rect(slidescreen, col_blue1, self.rect_sliderB)

    def draw(self):
        #update position
        self.rect_sliderA = pygame.Rect((MARGIN_X-10+(self.valA-self.mini)*self.scalar, 0), (20, 40))
        self.rect_sliderB = pygame.Rect((MARGIN_X-10+(self.valB-self.mini)*self.scalar, 0), (20, 40))

        #graph background
        slidescreen.fill(col_white)
        pygame.draw.rect(slidescreen, col_grey, pygame.Rect((MARGIN_X,15),(self.maxwidth,10)))
        pygame.draw.rect(slidescreen, col_blue2, pygame.Rect((self.low-self.mini)*self.scalar+MARGIN_X,15,(self.high-self.low)*self.scalar,10))

        #draw them
        pygame.draw.rect(slidescreen, col_blue1, self.rect_sliderA)
        pygame.draw.rect(slidescreen, col_blue1, self.rect_sliderB)

    def moveA(self):
        self.valA = ((pygame.mouse.get_pos()[0]-MARGIN_X)/self.scalar)+self.mini
        if self.valA < self.mini:
            self.valA = self.mini
        if self.valA > self.maxi:
            self.valA = self.maxi
        self.updatehighlow()

    def moveB(self):
        self.valB = ((pygame.mouse.get_pos()[0]-MARGIN_X)/self.scalar)+self.mini
        if self.valB < self.mini:
            self.valB = self.mini
        if self.valB > self.maxi:
            self.valB = self.maxi
        self.updatehighlow()

    def updatehighlow(self):
        if self.valB > self.valA:
            self.high = self.valB
            self.low  = self.valA
        else:
            self.low  = self.valB
            self.high = self.valA

def drawGUI():
    global screen
    global flow

    screen.fill(col_white)

    maxx = screen.get_width()
    maxy = screen.get_height()

    #get some rulers for drawing
    ruler_A = MARGIN_X + NODE_WIDTH          #the amount of pixels from the left side of a to start drawing
    ruler_B = maxx - MARGIN_X - NODE_WIDTH   #the amount of pixels from the right side of a to start drawing
    ruler_C = maxy - MARGIN_Y - NODE_WIDTH   #the amount of pixels from the bottom side of a to start drawing
    ruler_halfx = maxx/2

    #extra helping equations
    dist_AB = ruler_B-ruler_A
    dist_AC = ruler_C-MARGIN_Y-NODE_SPACE_SIDES

    #find the relation of crowflow to pixels
    temp_trafficsum = [0,0,0]
    temp_trafficsum[0] = flow[0]+flow[1]+flow[2]+flow[4] #sum of all traffic over A
    temp_trafficsum[1] = flow[0]+flow[2]+flow[3]+flow[5] #sum of all traffic over B
    temp_trafficsum[2] = flow[1]+flow[3]+flow[4]+flow[5] #sum of all traffic over C
    px_crowd_scalar = (NODE_HEIGHT-(2*NODE_SPACE_SIDES)-NODE_SPACE_MIDDLE)/max(temp_trafficsum)
    del temp_trafficsum

    #calculate the size of the bars - TODO makeit nice
    ab_size = flow[0]*px_crowd_scalar
    ac_size = flow[1]*px_crowd_scalar
    ba_size = flow[2]*px_crowd_scalar
    bc_size = flow[3]*px_crowd_scalar
    ca_size = flow[4]*px_crowd_scalar
    cb_size = flow[5]*px_crowd_scalar

    #basic node rectangles
    rect_A = pygame.Rect(MARGIN_X,MARGIN_Y,NODE_WIDTH,NODE_HEIGHT)
    rect_B = pygame.Rect(ruler_B,MARGIN_Y,NODE_WIDTH,NODE_HEIGHT)
    rect_C = pygame.Rect((ruler_halfx-(NODE_HEIGHT/2)),ruler_C,NODE_HEIGHT,NODE_WIDTH)
    pygame.draw.rect(screen,col_grey,rect_A)
    pygame.draw.rect(screen,col_grey,rect_B)
    pygame.draw.rect(screen,col_grey,rect_C)

    #polygons that show the relation
    polygoncanvas = pygame.Surface((dist_AB,dist_AC), pygame.SRCALPHA, 32)
    polygoncanvas = polygoncanvas.convert_alpha()

    poly_AB = pygame.draw.polygon(polygoncanvas, col_blue2, ([0, 0], [0, ab_size], [(dist_AB-(ab_size/2)), ab_size], [dist_AB, ab_size/2], [(dist_AB-(ab_size/2)), 0]))
    poly_BA = pygame.draw.polygon(polygoncanvas, col_red, ([(ba_size/2), ab_size], [0, (ba_size/2)+ab_size], [(ba_size/2), ab_size+ba_size],[dist_AB,ab_size+ba_size],[dist_AB,ab_size]))

    tmp1 = NODE_HEIGHT-NODE_SPACE_SIDES-NODE_SPACE_SIDES-ac_size-ca_size    #startin y point for the bars ac and ca
    tmp2 = (ruler_halfx-(NODE_HEIGHT/2))+NODE_SPACE_SIDES-ruler_A           #starting x point for the bars in node C
    tanx = 0.414213 #shorthand to not use the actual tngent = tan(45/2)
    tanac = tanx*ac_size
    tanca = tanx*ca_size
    tanbc = tanx*bc_size
    tancb = tanx*cb_size

    poly_AC = pygame.draw.polygon( polygoncanvas, col_blue2, (
        [0, tmp1],#topleft
        [0, tmp1+ac_size],#bottomleft
        [tmp2-CORNER_DEPTH+tanca, tmp1+ac_size],
        [tmp2+ca_size, tmp1+ac_size+ca_size+CORNER_DEPTH-tanca],
        [tmp2+ca_size, dist_AC-(ac_size/2)],#floor
        [tmp2+ca_size+(ac_size/2), dist_AC],#point
        [tmp2+ca_size+ac_size, dist_AC-(ac_size/2)],
        [tmp2+ca_size+ac_size, tmp1+ac_size+ca_size+CORNER_DEPTH-tanca-tanac],
        [tmp2-CORNER_DEPTH+tanca+tanac, tmp1]

        ))

    poly_CA = pygame.draw.polygon( polygoncanvas, col_yellow, (
        [ca_size/2, tmp1+ac_size],
        [0, tmp1+ac_size+(ca_size/2)],
        [ca_size/2, tmp1+ac_size+ca_size],
        [tmp2-CORNER_DEPTH, tmp1+ac_size+ca_size],
        [tmp2, tmp1+ac_size+ca_size+CORNER_DEPTH],
        [tmp2, dist_AC],
        [tmp2+ca_size, dist_AC],
        [tmp2+ca_size, tmp1+ac_size+ca_size+CORNER_DEPTH-tanca],
        [tmp2-CORNER_DEPTH+tanca, tmp1+ac_size]
        ))

    tmp3 = NODE_HEIGHT-NODE_SPACE_SIDES-NODE_SPACE_SIDES-bc_size-cb_size            #starting y point for bc and cb
    tmp4 = (ruler_halfx+(NODE_HEIGHT/2))-NODE_SPACE_SIDES-ruler_A-cb_size-bc_size   #starting x point for bc and cb in node C

    poly_BC = pygame.draw.polygon(polygoncanvas, col_red, (
        [dist_AB,tmp3],
        [dist_AB,tmp3+bc_size],
        [tmp4+bc_size+cb_size-tancb+CORNER_DEPTH, tmp3+bc_size],
        [tmp4+bc_size, tmp3+bc_size+cb_size-tancb+CORNER_DEPTH], #outer corner
        [tmp4+bc_size,dist_AC-(bc_size/2)],
        [tmp4+(bc_size/2),dist_AC],#point
        [tmp4,dist_AC-(bc_size/2)],
        [tmp4,tmp3+bc_size+cb_size-tancb-tanbc+CORNER_DEPTH],
        [tmp4+bc_size+cb_size-tancb-tanbc+CORNER_DEPTH ,tmp3]
        ))

    poly_CB = pygame.draw.polygon(polygoncanvas, col_yellow, (
        [dist_AB-(cb_size/2), tmp3+bc_size],
        [dist_AB, tmp3+bc_size+(cb_size/2)],
        [dist_AB-(cb_size/2), tmp3+bc_size+cb_size],
        [tmp4+bc_size+cb_size +CORNER_DEPTH, tmp3+bc_size+cb_size], #inner corner
        [tmp4+bc_size+cb_size, tmp3+bc_size+cb_size+CORNER_DEPTH],
        [tmp4+bc_size+cb_size, dist_AC],
        [tmp4+bc_size,dist_AC],
        [tmp4+bc_size, tmp3+bc_size+cb_size-tancb+CORNER_DEPTH], #outer corner
        [tmp4+bc_size+cb_size-tancb+CORNER_DEPTH, tmp3+bc_size]
         ))

    #draw the geometr on the canvas
    screen.blit(polygoncanvas,(ruler_A,MARGIN_Y+NODE_SPACE_SIDES))
    del tanac,tanbc,tanca,tancb
    del tmp1,tmp2,tmp3,tmp4

    #draw the text
    #make the Tet in the appropriate size
    pygame.font.init()
    font = pygame.font.SysFont('arial',60,True) #bold arial font
    #A
    text_A = font.render('A',True,col_white)
    screen.blit(text_A,(MARGIN_X+(NODE_WIDTH/2.0)-(text_A.get_width()/2.0),MARGIN_Y+(NODE_HEIGHT/2.0)-(text_A.get_height()/2.0)))
    #B
    text_B = font.render('B',True,col_white)
    screen.blit(text_B,(ruler_B+(NODE_WIDTH/2.0)-(text_B.get_width()/2.0),MARGIN_Y+(NODE_HEIGHT/2.0)-(text_B.get_height()/2.0)))
    #C
    text_C = font.render('C',True,col_white)
    screen.blit(text_C,(ruler_halfx-(text_C.get_width()/2.0),ruler_C+(NODE_WIDTH/2.0)-(text_C.get_height()/2.0)))

    ax_offset = ruler_A + 35
    bx_offset = ruler_B - 35
    cy_offset = ruler_C - 25

    #test array (AB,AC,BA,BC,CA,CB)
    #AB
    font = pygame.font.SysFont('arial',math.floor(ab_size),True) #bold arial font
    text_AB = font.render(str(flow[0]),True,col_white)
    screen.blit(text_AB,(bx_offset-text_AB.get_width(),MARGIN_Y+NODE_SPACE_SIDES+ab_size/2-text_AB.get_height()/2))
    #AC
    font = pygame.font.SysFont('arial',math.floor(ac_size*0.6),True) #bold arial font
    text_AC = font.render(str(flow[1]),True,col_white)
    screen.blit(text_AC,(ruler_halfx-NODE_HEIGHT/2+NODE_SPACE_SIDES+ca_size+ac_size/2-text_AC.get_width()/2,cy_offset-text_AC.get_height()))
    #BA
    font = pygame.font.SysFont('arial',math.floor(ba_size),True) #bold arial font
    text_BA = font.render(str(flow[2]),True,col_white)
    screen.blit(text_BA,(ax_offset,MARGIN_Y+NODE_SPACE_SIDES+ab_size+ba_size/2-text_BA.get_height()/2))
    #BC
    font = pygame.font.SysFont('arial',math.floor(bc_size*0.6),True) #bold arial font
    text_BC = font.render(str(flow[3]),True,col_white)
    screen.blit(text_BC,(ruler_halfx+NODE_HEIGHT/2-NODE_SPACE_SIDES-cb_size-bc_size/2-text_BC.get_width()/2,cy_offset-text_BC.get_height()))
    #CA
    font = pygame.font.SysFont('arial',math.floor(ca_size),True) #bold arial font
    text_CA = font.render(str(flow[4]),True,col_white)
    screen.blit(text_CA,(ax_offset,MARGIN_Y-NODE_SPACE_SIDES+NODE_HEIGHT-ca_size/2-text_CA.get_height()/2))
    #CB
    font = pygame.font.SysFont('arial',math.floor(cb_size),True) #bold arial font
    text_CB = font.render(str(flow[5]),True,col_white)
    screen.blit(text_CB,(bx_offset-text_CB.get_width(),MARGIN_Y-NODE_SPACE_SIDES+NODE_HEIGHT-cb_size/2-text_CB.get_height()/2))

def updatevalues():
    global flow
    global data

    #filter and calculate cumulative values for set boundaries
    for i in range(len(data)):
        tmpA = ([i[0] for i in data[i]] <= slider.high)
        tmpB = ([i[1] for i in data[i]] >= slider.low)
        tmpC = numpy.logical_and(tmpA,tmpB)
        #update flow values
        flow[i] = tmpC.sum()
        del tmpA,tmpB,tmpC

#programm starts here
#window = pygame.display.set_mode((800, 600))
window = pygame.display.set_mode((800, 600),pygame.FULLSCREEN)
window.fill(col_white)

screen = pygame.Surface((800,500), pygame.SRCALPHA, 32)
screen.fill(col_white)

slidescreen = pygame.Surface((800,100), pygame.SRCALPHA, 32)
slidescreen.fill(col_white)
pygame.display.set_caption('IoT_datavisualizer')

#test array (AB,AC,BA,BC,CA,CB)
#open csv files
data = [[],[],[],[],[],[]]
maxstuff = [0,0,0,0,0,0]
minstuff = [0,0,0,0,0,0]
Files =  ['AB.csv','AC.csv','BA.csv','BC.csv','CA.csv','CB.csv']

for i , file in enumerate(Files):
    tmpdata = genfromtxt(file, delimiter=',')
    data[i] = numpy.delete(tmpdata,[0,3,4],1)
    maxstuff[i] = numpy.amax(data[i])
    minstuff[i] = numpy.amin(data[i])
    del tmpdata

print(maxstuff , "  <>  ", minstuff)
#maxtime = numpy.amax(data)
maxtime = numpy.amax(maxstuff)
print("max time = ", maxtime)
mintime = numpy.amin(minstuff)
print("min time = ", mintime)

del maxstuff,minstuff,Files

#set maximum and minimum bounds of slider
slider = Slider(maxtime,mintime)
updatevalues()

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            if slider.rect_sliderA.collidepoint(pos[0],pos[1]-500):  slider.hitA = True
            elif slider.rect_sliderB.collidepoint(pos[0],pos[1]-500):  slider.hitB = True
            print(slider.maxi, "  ",slider.mini,"  ",slider.high,"  ",slider.low)
            updatevalues()
        elif event.type == pygame.MOUSEBUTTONUP:
            slider.hitA = False
            slider.hitB = False
            updatevalues()

    drawGUI()
    window.blit(screen,(0,0))

    if slider.hitA:slider.moveA()
    if slider.hitB:slider.moveB()
    slider.draw()
    window.blit(slidescreen,(0,520))

    pygame.display.flip()
    time.sleep(0.05)

pygame.display.quit()
pygame.quit()
exit()


import random

class State:
    def __init__(self, w, h, no_mines):

        self.no_mines = no_mines
        self.w = w
        self.h = h
        self.start_game()

    def start_game(self):
        self.generate_board()

        self.visible_board = [[False for col in range(self.w)] for row in range(self.h)]

        self.flags = []

        self.game_over = False

    def generate_board(self):
        self.board = [[0 for col in range(self.w)] for row in range(self.h)]

        for mine in range(self.no_mines):

            while True:
                row = random.randint(0, self.h-1)
                col = random.randint(0, self.w-1)
                if self.board[row][col] != -1:
                    self.board[row][col] = -1
                    break

        for row in range(self.h):
            for col in range(self.w):
                if self.board[row][col] != -1:
                    self.board[row][col] = self.count_mines(row,col)

    def no_mine(self, row, col):
        cell = self.board[row][col]

        if [row,col] in self.flags:
            return

        if cell == -1:
            print("UH OH!")
            return

        self.spread_visibility(row,col)

    def flag_cell(self, row, col):
        if self.visible_board[row][col]:
            return
        if [row,col] in self.flags:
            self.flags.remove([row,col])
        else:
            self.flags.append([row, col])

    def check_for_win(self):
        if self.w * self.h - sum([sum(row) for row in self.visible_board]) == self.no_mines:
            print("you won!")
            return True
        return False

    def spread_visibility(self, row, col):
        if [row,col] in self.flags:
            return
        self.visible_board[row][col] = True
        if self.board[row][col] == 0:

            #Check left hand side
            if row > 0:
                if self.visible_board[row-1][col] == False:
                    self.spread_visibility(row-1, col)
                if col > 0:
                    if self.visible_board[row-1][col-1] == False:
                        self.spread_visibility(row-1, col-1)
                if col < self.w-1:
                    if self.visible_board[row-1][col+1] == False:
                        self.spread_visibility(row-1, col+1)

            #Check right hand side
            if row < self.h-1:
                if self.visible_board[row+1][col] == False:
                        self.spread_visibility(row+1, col)
                if col > 0:
                    if self.visible_board[row+1][col-1] == False:
                        self.spread_visibility(row+1, col)
                if col < self.w-1:
                    if self.visible_board[row+1][col+1] == False:
                        self.spread_visibility(row+1, col+1)

            #check top
            if col > 0:
                if self.visible_board[row][col-1] == False:
                        self.spread_visibility(row, col-1)

            #check bottom
            if col < self.w -1:
                if self.visible_board[row][col+1] == False:
                        self.spread_visibility(row, col+1)

    def count_mines(self, row, col):
        neighbouring_mines = 0
        #Check left hand side
        if row > 0:
            if self.board[row-1][col] == -1:
                neighbouring_mines += 1
            if col > 0:
                if self.board[row-1][col-1] == -1:
                    neighbouring_mines += 1
            if col < self.w-1:
                if self.board[row-1][col+1] == -1:
                    neighbouring_mines += 1

        #Check right hand side
        if row < self.h-1:
            if self.board[row+1][col] == -1:
                neighbouring_mines += 1
            if col > 0:
                if self.board[row+1][col-1] == -1:
                    neighbouring_mines += 1
            if col < self.w-1:
                if self.board[row+1][col+1] == -1:
                    neighbouring_mines += 1

        #check top
        if col > 0:
            if self.board[row][col-1] == -1:
                neighbouring_mines += 1

        #check bottom
        if col < self.w -1:
            if self.board[row][col+1] == -1:
                neighbouring_mines += 1

        return neighbouring_mines


'''
import numpy as np

import torch.nn.functional as F
from torchgeometry.losses.one_hot import one_hot

import torch
import cv2

a = np.array([[[[1.2, 1.4, 1.9],[1.5, 1.9, 0.4],[0.2, 0.1, 12]],[[1.2, 1.4, 16],[1.5, 4, 0.4],[0.2, 0.1, 0.12]],[[12, 1.4, 1.6],[1.5, 1.7, 0.4],[0.2, 0.1, 12]]]])
#a = np.uint8(255*np.array([[[[0, 1, 0],[0, 1, 0],[0, 1, 0]],[[1, 0, 0],[1, 0, 0],[1, 0, 0]],[[0, 0, 1],[0, 0, 1],[0, 0, 1]]]]))
##print(a)
#cv2.imshow('a', a[0,:,:,:])
#cv2.waitKey()
#cv2.destroyAllWindows()

#a = np.array([[[0.0, 1.0, 2.0]]])
t = torch.from_numpy(a)
t = F.softmax(t, dim=3)

#print(t)
#print(t.shape)

#print(t.shape)

t = one_hot(t, 3)

#print(t)
'''

import numpy as np
import matplotlib.pyplot as plt

# define a range of probabilities
p = np.linspace(0.01, 0.99, 100)

# calculate cross entropy loss when the true class is 1
loss_positive = -np.log(p)

# calculate cross entropy loss when the true class is 0
loss_negative = -np.log(1 - p)

# plot both scenarios
plt.figure(figsize=(8,6))
plt.plot(p, loss_positive, label='Clasa corectă: 1')
plt.plot(p, loss_negative, label='Clasa corectă: 0')
plt.title('Cross Entropy Loss for different scenarios')
plt.xlabel('Probabilitatea de apartenență la clasă (prezisă)', fontsize=16)
plt.ylabel('Valoarea entropiei încrucișate', fontsize=16)
plt.legend()
plt.show()


# discord imports
import discord
from discord.ext import commands
from discord.utils import get

# timing imports
import time
from datetime import datetime
import asyncio
import async_timeout

class GiveRole(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot

    '''
    Garmoth
    Karanda
    Kutum
    Kzarka
    Muraka
    Nouver
    Offin
    Quint
    Vell
    '''

    @commands.command()
    async def addroles(self, ctx: commands.Context):
        if get(ctx.guild.roles, name='Garmoth'):
            print('Garmoth role already exists.')
        else:
            await ctx.guild.create_role(name='muted', color=discord.Color('0xff0000'))
            print('muted role created')

class Alert(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot

    @commands.command(name='time')
    async def tell_time(self, ctx: commands.Context):
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        await ctx.send(current_time)


# -*- coding: utf-8 -*-
"""
Created on Mon Jun 15 18:47:42 2020

@author: Ankita Gogawale
"""

from  prettytable  import PrettyTable
import numpy as np
from scipy import stats
t = PrettyTable(['Marital Status','Middle school', 'High School','Bachelor','Masters','PhD'])
t.add_row(['Single',18,36,21,9,6])
t.add_row(['Married',12,36,45,36,21])
t.add_row(['Divorced',6,9,9,3,3])
t.add_row(['Widowed',3,9,9,6,3])
# print (t)

arr=np.array([[18,36,21,9,6],[12,36,45,36,21],[6,9,9,3,3],[3,9,9,6,3]])
# print(arr)

stat,p_value,dof,expected = stats.chi2_contingency(arr)
p=0.05
print(stat)
print(dof)
print(p_value)

if p_value < p:
    print("Reject the Null Hypothesis")
else:
    print("Failed to reject the Null Hypothesis")


#!/usr/bin/env python
__author__ = "Simon Swolfs en Robbe de Budt"
__email__ = "simon.swolfs@student.kdg.be"
__version__ = "1.0"

# LIBRARIES
import time
import gpiozero
from gpiozero import Button
from gpiozero import LED

# VARIABLES
systemwork = True
howworks = {
    Button(26): LED(6),
    Button(16): LED(5),
    Button(25): LED(23),
    Button(17): LED(24)
}

while systemwork:
    for key, value in howworks.items():
        if key.value == 1:
            print('The button was pressed!')
            if value.value == 1:
                value.off()
            else:
                value.on()

        time.sleep(0.50)


import logging
import os
import time

# 定义历史数据库
# 目的是判重，不下载重复资源
class _historyDB :
    # 类初始化
    def __init__(self, RootPath=""):
        # self.contentFolder = u"抓取内容"
        self.RootPath = RootPath
        '''
        self.dbName = os.path.join(RootPath, "url.db")              # 1、定义数据库文件名
        # self.createFolder(self.contentFolder)    # 2、创建内容存储目录
        self.urlDB = set()                 # 3、创建内存数据库
        self.pathDB = set()                 # 4、创建路径数据库（适合资源有明确的文件名的情况）
        '''

     # 读取URL数据库以获取爬过的网页地址
    # def loadURLDatabase(self):
    #      isExists = os.path.exists(self.dbName)    # 4、首先判断是否是首次运行，如果数据库文件不存在则创建一下
    #      if not isExists:
    #          logging.info(u"创建URL数据库文件：'" + self.dbName + u"'")
    #          f = open(self.dbName, 'w+')
    #          f.write("#Create time: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) + '\n')
    #          f.close()
    #          return
    #      db = open(self.dbName, 'r')          # 5、从磁盘中加载数据库
    #      for line in db.readlines():
    #          if not line.startswith('#'):    # 注释性的信息
    #              self.urlDB.add(line.strip('\n'))    # 读取文件中的URL, 保存进数据库
    #      db.close()
    #      logging.info(u"URL数据库加载完成！")

     # 获取所有已经获取爬过的网页图片的路径
    # def loadPathDatabase(self):
    #     for parent, dirnames, filenames in os.walk(self.RootPath):  # 三个参数：分别返回1.父目录 2.所有文件夹名字（不含路径） 3.所有文件名字
    #         for dirname in dirnames :   # 遍历每个相册
    #             for _parent, _dirnames, _filenames in os.walk(os.path.join(self.RootPath, dirname)) :  # 三个参数：分别返回1.父目录 2.所有文件夹名字（不含路径） 3.所有文件名字
    #                 for filename in _filenames: # 遍历相册里每个资源
    #                     # 将路径信息, 保存进数据库
    #                     self.pathDB.add(os.path.join(self.RootPath, dirname, filename))
    #
    #     logging.info(u"路径数据库加载完成！")

     # 追加URL数据, 写入URL数据库 和 当前内存中的URL数据库
    # def writeToDatabase(self, url):         # 6、在系统运行过程中，如需记录日志，追加日志内容即可
    #      # logging.info('有新追加的URL..')
    #      db = open(self.dbName, 'a', encoding='utf-8')
    #      # 如果是list ，那么就是pic的List
    #      if isinstance(url, list) :
    #          for urlData in url :
    #              db.write(urlData.URL + '\n')
    #              self.urlDB.add(urlData.URL)
    #              # 更新path数据库
    #              self.pathDB.add(os.path.join(urlData.album, urlData.name))

        # 单个的只是URL
        #  else :
        #     db.write(url + '\n')
        #     self.urlDB.add(url)
        #  db.close()

     # 判断资源是否已存在
    def IsExists (self, pic) :
         isExists = False

         # 先判断是否在路径数据库中（实体已经存在文件夹中）
         isExists = os.path.exists( os.path.join(pic.album, pic.name) )
         return isExists

         # if os.path.join(pic.album, pic.name) in self.pathDB :
         #     pass
             # 判断文件是否过小
             # fileSize = os.path.getsize(os.path.join(self.RootPath, pic.album, pic.name)) / 1024
             # logging.info('the file size is : %.3f KB' % (fileSize))
             # if( fileSize < 2 ) :
             #     isExists = False
             # # 文件大小正常
             # else :
             # isExists = True
             # # 在路径数据库中存在（实体已经存在文件夹中），但是没有存入路径的情况
             # if pic.URL not in self.urlDB :
             #     self.writeToDatabase(pic.URL)

         # 资源不存在的情况
         # elif pic.URL in self.urlDB :
             # logging.DEBUG("??? 应该是不会出现的情况：存在URL数据库中，但是实体文件不存在")
             # isExists = True
         #     isExists = False
         # else :
         #     isExists = False

         # 返回结果
         return isExists

    # 外界API
    def removeDuplicate(self, picList) :
        # 待删除的列表（这个删除方式感觉不太方便）
        delList = []

        raw_len = len(picList)
        cnt = 0
        for ix, pic in enumerate(picList):
            # 图片过滤（过滤重复）
            if self.IsExists(pic):
                delList.append(pic)
                cnt += 1
        logging.info('判重操作完毕.. 需要的'+str(raw_len)+'张图片中,有'+str(cnt)+'张图片已存在..')

        # 删掉列表中的元素
        for pic in delList :
            picList.remove(pic)

        return picList


from Barebones.models import Product, Cart
from Barebones.serializers import ProductSerializer, CheckoutSerializer
from rest_framework import generics, status
from rest_framework.decorators import api_view
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.reverse import reverse
from django.http import Http404
from rest_framework import permissions
from Barebones.permission import IsOwnerOrReadOnly

@api_view(['GET'])
def api_root(request, format=None):
    return Response({
        'products': reverse('product-list', request=request, format=format),
        'Cart': reverse('cart-list', request=request, format=format)
    })

class ProductList(generics.ListCreateAPIView):
    """
    Retrieve all products, or create a new product.
    """
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)
    queryset = Product.objects.all()
    serializer_class = ProductSerializer

class ProductDetail(generics.RetrieveAPIView):
    """
    Retrieve a single product.
    """
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)
    queryset = Product.objects.all()
    serializer_class = ProductSerializer

class ProductAvlList(generics.ListAPIView):
    """
    List all available products.
    """
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
    queryset = Product.objects.filter(inventory_count__gt=0)
    serializer_class = ProductSerializer

class ProductPurchase(APIView):
    """
    Purchase a product. Inventory count is reduced.
    """
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)

    def get_object(self, pk):
        try:
            return Product.objects.get(pk=pk)
        except Product.DoesNotExist:
            raise Http404

    def post(self, request, pk):
        product = self.get_object(pk)
        if product.inventory_count > 0:
            data = {"inventory_count": product.inventory_count - 1}
        else:
            raise Http404
        serializer = ProductSerializer(product, data=data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response({"success": True}, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class AddCart(APIView):
    """
    Add a product to Cart.
    """
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)

    def get_object(self, pk):
        try:
            return Cart.objects.get(product=pk)
        except Product.DoesNotExist:
            raise Http404

    def post(self, request, pk, format=None):

        product = Product.objects.get(id=pk)

        product_name = request.data.get('product')
        product_price = request.data.get('price')
        product_count = request.data.get('product_cnt')
        checked_out = request.data.get('checked_out')
        owner = request.data.get('owner')
        try:
            shop = Cart.objects.get(product=product.title)
            flag = 1
        except:
            flag = 0

        #Below code verifies the incoming data
        #If the incoming data does not match the data in database
        #then error is thrown
        if product.inventory_count < 1:
            raise Http404
        elif product.title != product_name:
            return Response({"Error": "Invalid Product Name"}, status=status.HTTP_400_BAD_REQUEST)
        elif product.price != product_price:
            return Response({"Error": "Invalid Product Price"}, status=status.HTTP_400_BAD_REQUEST)
        elif product_count > product.inventory_count:
            return Response({"Error": "Insufficient Inventory"}, status=status.HTTP_400_BAD_REQUEST)
        elif checked_out == True:
            return Response({"Error": "Cannot checkout. Set to False for adding to cart"},
                            status=status.HTTP_400_BAD_REQUEST)
        elif owner != request.data.get('owner'):
            return Response({"Error": "Incorrect Owner"}, status=status.HTTP_400_BAD_REQUEST)
        elif flag == 1:

            data = {"product_cnt": shop.product_cnt + product_count}
            serializer = CheckoutSerializer(shop, data=data, partial=True)
            if serializer.is_valid():
                serializer.save()
                return Response({"success": True}, status=status.HTTP_201_CREATED)
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

        serializer = CheckoutSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({"success": True})
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class ViewCart(APIView):
    """
    View Cart
    """

    permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)

    def get(self, request, format=None):
        queryset = Cart.objects.all()
        products_included = {}
        total_amount = 0

        for item in queryset:
            total_amount = total_amount + (item.product_cnt * item.price)
            products_included[item.product] = item.product_cnt

        return Response({"Products": products_included, "Total Amount": total_amount}, status=status.HTTP_200_OK)

class CheckoutCart(APIView):
    """
    Checkout Cart
    """
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)

    def post(self, request, format=None):
        cart_items = Cart.objects.all()

        for items in cart_items:
            products = Product.objects.get(title=items.product)
            products.inventory_count = products.inventory_count - items.product_cnt
            products.save()
            items.delete()

        return Response({"Success": "Cart Checkout Successful"}, status=status.HTTP_200_OK)


from flask import (Flask, jsonify, request)

drivers = [
    {
        'id': 0,
        'name': 'Fernando',
        'last_name': 'Gutierrez',
        'license_number': '144589',
        'truck_id': 1
    },
    {
        'id': 1,
        'name': 'Javier',
        'last_name': 'Caceres',
        'license_number': '589636',
        'truck_id': 2
    },

]

trucks = [
    {
        'id': 0,
        'plate': 'TRUCKB',
        'type': 'Crane Truck',
        'color': 'Red',
        'height': '2.45 m',
        'volume': '86 cubic meters'
    },
    {
        'id': 1,
        'plate': 'ROO 8305',
        'type': 'Crane Truck',
        'color': 'Blue',
        'height': '2.20 m',
        'volume': '90 cubic meters'
    },
    {
        'id': 2,
        'plate': 'TRUCKB',
        'type': 'KA 14A 1478',
        'color': 'Orange',
        'height': '2.10 m',
        'volume': '92 cubic meters'
    }
]
app = Flask(__name__)
API_NAME = "/api/v1"

@app.route(f"{API_NAME}/trucks", methods=["GET"])
def truck_list():
    """
    truck_list --> Get the list of all trucks available
    Return:
        trucks(dict): list of trucks
    """
    return jsonify(trucks)

@app.route(f"{API_NAME}/trucks/<truck_id>", methods=["GET"])
def get_truck_by_id(truck_id):
    """
    get_truck_by_id --> Get a truck given an id
    Args:
        truck_id(int): The truck id to look for
    Return:
        truck(dict): A truck
    """

    for truck in trucks:
        if truck.get("id") == int(truck_id):
            return jsonify(truck)
    else:
        return {
            'error': 'Truck not found.',
            'status': 404
        }

@app.route(f"{API_NAME}/trucks", methods=["POST"])
def save_truck():
    """
    save_truck --> Save a given truck into a truck list.
    Return:
        message(dict): A dict that indicates that the truck was saved.
    """
    truck_json = request.json
    trucks.append(truck_json)
    return jsonify({
        'message': "The truck was saved.",
        'status': 200
    })

@app.route(f"{API_NAME}/trucks/<truck_id>", methods=["DELETE"])
def remove_truck(truck_id):
    """
    truck_id --> Remove a truck given an id.
    Args:
        truck_id(int): The truck id to remove.
    Return:
        result(dict): A dictionary that indicates if the truck was removed.
    """
    global trucks
    trucks = [truck for truck in trucks if truck.get("id") != int(truck_id)]
    return jsonify({
        'message': f"Truck with id {truck_id} was removed.",
        'status': 200
    })

@app.route(f"{API_NAME}/drivers", methods=["GET"])
def driver_list():
    """
    driver_list --> Get the list of all drivers available
    Return:
        drivers(dict): list of drivers
    """
    return jsonify(drivers)

@app.route(f"{API_NAME}/drivers/<driver_id>", methods=["GET"])
def get_driver_by_id(driver_id):
    """
    get_driver_by_id --> Get a driver given an id.
    Args:
        driver_id(int): The driver id to look for.
    Return:
        driver(dict): A driver.
    """
    for driver in drivers:
        if driver.get("id") == int(driver_id):
            return jsonify(driver)
    else:
        return {
            'error': 'Driver not found.',
            'status': 404
        }

@app.route(f"{API_NAME}/drivers", methods=["POST"])
def save_driver():
    """
    save_driver --> Save a given driver into the drivers list
    Return:
        truck(dict): A dict that indicates the driver was saved.
    """
    driver_json = request.json
    drivers.append(driver_json)
    return jsonify({
        'message': "The driver was saved.",
        'status': 200
    })

@app.route(f"{API_NAME}/drivers/<driver_id>", methods=["DELETE"])
def remove_driver(driver_id):
    """
    remove_driver --> Remove a driver given an id.
    Args:
        truck_id(int): The driver id to remove.
    Return:
        result(dict): A dictionary that indicates if the driver was removed.
    """
    global drivers
    drivers = [driver for driver in drivers if driver.get("id") != int(driver_id)]
    return jsonify({
        'message': f"Driver with id {driver_id} was removed.",
        'status': 200
    })

@app.route(f"{API_NAME}/driver/<driver_name_or_id>/truck", methods=["GET"])
def get_truck_of_driver(driver_name_or_id):
    """
    get_truck_of_driver --> Get a truck given a driver id or name.
    Args:
        driver_name_or_id(int): The driver name or id to look for.
    Return:
        driver(dict): A driver.
    """
    for driver in drivers:
        if str(driver.get("id")) == driver_name_or_id or driver.get("name") == driver_name_or_id:
            return get_truck_by_id(driver.get("truck_id"))

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True, port=5001, use_reloader=True)


#!/usr/bin/env python

"""
A setuptools based setup module.

See:
https://packaging.python.org/en/latest/distributing.html
"""

import io
from os import path
from setuptools import setup, find_packages

if __name__ == "__main__":
    HERE = path.abspath(path.dirname(__file__))
    with io.open(path.join(HERE, "README.rst"), encoding="utf-8") as readme:
        LONG_DESCRIPTION = readme.read()
    setup(
        name="sievelib",
        packages=find_packages(),
        include_package_data=True,
        description="Client-side SIEVE library",
        author="Antoine Nguyen",
        author_email="tonio@ngyn.org",
        url="https://github.com/tonioo/sievelib",
        license="MIT",
        keywords=["sieve", "managesieve", "parser", "client"],
        install_requires=[],
        setup_requires=["setuptools_scm"],
        use_scm_version=True,
        classifiers=[
            "Programming Language :: Python",
            "Development Status :: 4 - Beta",
            "Intended Audience :: Developers",
            "License :: OSI Approved :: MIT License",
            "Operating System :: OS Independent",
            "Topic :: Software Development :: Libraries :: Python Modules",
            "Topic :: Communications :: Email :: Filters"
        ],
        long_description=LONG_DESCRIPTION
    )


import sys
from src.log_handler import logging

def get_error_message(error, error_detail: sys):
    _, _, exc_tb = error_detail.exc_info()
    file_name = exc_tb.tb_frame.f_code.co_filename
    error_line = exc_tb.tb_lineno
    error_message = f"Error occured at [{file_name}] at line number [{error_line}]  error message: [{error}] "  # customized error message

    return error_message

class CustomException(Exception):
    def __init__(self, error, error_detail):
        super().__init__(error)
        self.error_message = get_error_message(error, error_detail)
        # print("CUSTOM EXCEPTION ACTIVATED")

    def __str__(self):
        return self.error_message

def handle_exceptions(func):
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)

        except Exception as e:
            exc = CustomException(e, sys)
            args[0].log_writer.handle_logging(exc, level=logging.ERROR)
            raise exc

    return wrapper

if __name__ == "__main__":
    try:
        print(1 / 0)

    except Exception as e:
        new_exception = CustomException(e, sys)
        logging.error(new_exception)
        raise new_exception


import pandas as pd
import numpy as np
import time
import os
import joblib

from run_pred.functions.a_cleaning import clean_data
from run_pred.functions.b_splitting import split_data
from run_pred.functions.c_feature_engineering import engineer_features
from run_pred.functions.d_balancing import balance_data
from run_pred.functions.e_scaling_encoding import scale_encode_data

from sklearn.metrics import mean_absolute_percentage_error
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, StackingRegressor
from xgboost import XGBRegressor

from google.cloud import storage

# Note: the best model has been searched and engineered into individual notebooks,
# by each member of the team. Only the best model is train and stored here, see below.

def train_best_model(
    X_train: pd.DataFrame,
    y_train: pd.Series,
    X_test: pd.DataFrame,
    y_test: pd.Series) -> object:

    """
    Train a stacking regression model on the input training dataset `X_train` and `y_train`,
    and evaluate its performance on the test dataset `X_test` and `y_test`.
    The function defines the hyperparameters and models for three base regression models:
    Random Forest Regressor, XGBoost Regressor, and Gradient Boosting Regressor.

    It then trains a meta-regressor, which is a Linear Regression model,
    using the predicted outputs of the three base models as its input.

    Finally, it constructs a Stacking Regressor, which combines the three base models
    and the meta-regressor, and trains it on the input training dataset.

    Args:
    - X_train (array-like): Training data features.
    - y_train (array-like): Training data target.
    - X_test (array-like): Test data features.
    - y_test (array-like): Test data target.

    Returns:
    - model (object): Trained StackingRegressor model.
    """

    # Defining the parameters for each sub-model
    xgb_params = {'gamma': 0, 'learning_rate': 0.1, 'max_depth': 25, 'min_child_weight': 1, 'n_estimators': 1200}
    gb_params = {'loss': 'huber', 'max_depth': 15, 'max_features': 1.0, 'min_samples_split': 6, 'n_estimators': 500}

    # Defining the models
    rf_model = RandomForestRegressor()
    xgb_model = XGBRegressor(**xgb_params)
    gb_model = GradientBoostingRegressor(**gb_params)

    # Defining the meta-regressor
    meta_regressor = LinearRegression()

    # Defining the StackingRegressor by combining the sub_models defined above
    model = StackingRegressor(
        estimators=[ ('gb', gb_model),('xgb', xgb_model),('rf', rf_model)],
        final_estimator=meta_regressor)

    # Training the model
    model.fit(X_train,y_train)

    # Checking the model results
    print(f'R² = {model.score(X_test,y_test).round(3)}')
    print(f'MAPE = {mean_absolute_percentage_error(y_test, model.predict(X_test)).round(3)}')

    return model

def save_model(model: object) -> None:
    """
    Saves the trained model on hard drive at f"{models/{model_name}.joblib"
    """
    if not os.path.isdir('models'): os.mkdir('models')

    model_name = type(model).__name__
    print(f'Saving {model_name} model...')

    # Saving model locally
    model_path = os.path.join("models", f"{model_name}.joblib")
    joblib.dump(model, open(model_path, 'wb'))
    print(f'{model_name} model saved')

def load_model(model: str) -> object:
    """
    This function loads a given model, which can be one of these:
        - StandardScaler
        - OneHotEncoder
        - StackingRegressor
    """
    if not os.path.isdir('models'): os.mkdir('models')

    model_path = os.path.join("models", f"{model}.joblib")

    if not os.path.isfile(model_path):
        print(f'Downloading {model} model from GCS...')
        # ID of the GCS bucket
        bucket_name = "run_pred_model"

        # ID of the GCS object
        source_blob_name = model_path

        # Path to which the file should be downloaded
        destination_file_name = model_path

        storage_client = storage.Client.from_service_account_json('gcs_credentials.json')
        bucket = storage_client.bucket(bucket_name)
        blob = bucket.blob(source_blob_name)
        blob.download_to_filename(destination_file_name)
        print(f'{model} model downloaded from GCS')

    model = joblib.load(model_path)
    return model

if __name__ == '__main__' :

    # Testing the data whole preprocessing
    ## dataset = clean_data('raw_data/raw-data-kaggle.csv')
    ## X_train_raw, X_test_raw, y_train, y_test = split_data(dataset)
    ## X_train_feat = engineer_features(X_train_raw, y_train)
    ## X_test_feat = engineer_features(X_test_raw, y_test)
    ## X_train_balanced, y_train_balanced = balance_data(X_train_feat=X_train_feat, y_train=y_train)
    ## X_train_balanced_scaled_encoded, X_test_scaled_encoded = scale_encode_data(X_train_balanced, X_test_feat)

    # Testing the load_model and save_model functions
    ## model = load_model('StandardScaler')
    ## assert(model is not None)
    ## print(type(model))
    ## save_model(model)

    # Training the best model on the train dataset (28k rows), evaluating its performance, and saving the model
    ## dataset = clean_data('raw_data/raw-data-kaggle.csv')
    ## X_train_raw, X_test_raw, y_train, y_test = split_data(dataset)
    ## X_train_feat = engineer_features(X_train_raw, y_train)
    ## X_test_feat = engineer_features(X_test_raw, y_test)
    ## X_train_balanced, y_train_balanced = balance_data(X_train_feat=X_train_feat, y_train=y_train)
    ## X_train_balanced_scaled_encoded, X_test_scaled_encoded = scale_encode_data(X_train_balanced, X_test_feat)
    ## model = train_best_model(X_train_balanced_scaled_encoded, y_train_balanced, X_test_scaled_encoded, y_test)
    ## save_model(model)

    # Training the validated best model on the FULL dataset (32k rows) and saving it
    dataset = clean_data('raw_data/raw-data-kaggle.csv')
    print(dataset.head())
    X = dataset.drop(columns = ['time'])
    y = dataset.time
    X_feat = engineer_features(X, y)
    X_balanced, y_balanced = balance_data(X_train_feat=X_feat, y_train=y)
    X_balanced_scaled_encoded = scale_encode_data(X_balanced, X_balanced, save=True)[0]
    model = train_best_model(X_balanced_scaled_encoded, y_balanced, X_balanced_scaled_encoded, y_balanced)
    save_model(model)


import torch
from network import Network
from metric import valid
from torch.utils.data import Dataset
import numpy as np
import argparse
from loss import Loss
from dataloader import load_data
import os
import time

os.environ["CUDA_VISIBLE_DEVICES"] = "0"

# Dataname = 'DHA'
# Dataname = 'CCV'
# Dataname = 'NUSWIDE'
Dataname = 'Caltech'
# Dataname = 'YoutubeVideo'

CL_Loss = ['InfoNCE', 'PSCL', 'RINCE']  # three kinds of contrastive losses
Measure_M_N = ['CMI', 'JSD', 'MMD']     # Class Mutual Information (CMI), Jensen–Shannon Divergence (JSD), Maximum Mean Discrepancy (MMD)
sample_mmd = 2000                       # select partial samples to compute MMD as it has high complexity, otherwise might be out-of-memory
Reconstruction = ['AE', 'DAE', 'MAE']   # autoencoder (AE), denoising autoencoder (DAE), masked autoencoder (MAE)
per = 0.3                               # the ratio of masked samples to perform masked AE, e.g., 30%

parser = argparse.ArgumentParser(description='train')
parser.add_argument('--dataset', default=Dataname)
parser.add_argument('--batch_size', default=256, type=int)     # 256
parser.add_argument("--temperature_f", default=1.0)            # 1.0
parser.add_argument("--contrastive_loss", default=CL_Loss[0])  # 0, 1, 2
parser.add_argument("--measurement", default=Measure_M_N[0])   # 0, 1, 2
parser.add_argument("--Recon", default=Reconstruction[0])      # 0, 1, 2
parser.add_argument("--bi_level_iteration", default=4)         # 4
parser.add_argument("--times_for_K", default=1)                # 0.5 1 2 4
parser.add_argument("--Lambda", default=1)                     # 0.001 0.01 0.1 1 10 100 1000
parser.add_argument("--learning_rate", default=0.0003)         # 0.0003
parser.add_argument("--weight_decay", default=0.)              # 0.
parser.add_argument("--workers", default=8)                    # 8
parser.add_argument("--mse_epochs", default=100)               # 100
parser.add_argument("--con_epochs", default=100)               # 100
parser.add_argument("--feature_dim", default=512)              # 512
parser.add_argument("--high_feature_dim", default=128)         # 128
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

print('SEM + ' + args.contrastive_loss + ' + ' + args.measurement + ' + ' + args.Recon)

if args.dataset == "DHA":
    args.con_epochs = 300
    args.bi_level_iteration = 1

if args.dataset == "CCV":
    args.con_epochs = 50
    args.bi_level_iteration = 4

if args.dataset == "YoutubeVideo":
    args.con_epochs = 25
    args.bi_level_iteration = 1

if args.dataset == "NUSWIDE":
    args.con_epochs = 25
    args.bi_level_iteration = 4

if args.dataset == "Caltech":
    args.con_epochs = 100
    args.bi_level_iteration = 4
    # or
    args.bi_level_iteration = 3

Total_con_epochs = args.con_epochs * args.bi_level_iteration

def setup_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    # np.random.seed(seed)
    # random.seed(seed)
    torch.backends.cudnn.deterministic = True

accs = []
nmis = []
aris = []
purs = []
ACC_tmp = 0

for Runs in range(1):   # 10
    print("ROUND:{}".format(Runs+1))

    t1 = time.time()
    # setup_seed(5)   # if we find that the initialization of networks is sensitive, we can set a seed for stable performance.
    dataset, dims, view, data_size, class_num = load_data(args.dataset)

    data_loader = torch.utils.data.DataLoader(
            dataset,
            batch_size=args.batch_size,
            shuffle=True,
            # drop_last=True,
            drop_last=False,
        )

    def Low_level_rec_train(epoch, rec='AE', p=0.3, mask_ones_full=[], mask_ones_not_full=[]):
        tot_loss = 0.
        criterion = torch.nn.MSELoss()
        Vones_full = []
        Vones_not_full = []
        flag_full = 0
        flag_not_full = 0
        for batch_idx, (xs, _, _) in enumerate(data_loader):
            for v in range(view):
                xs[v] = xs[v].to(device)

            xnum = xs[0].shape[0]

            if rec == 'AE':
                optimizer.zero_grad()
                _, _, xrs, _, _ = model(xs)
            if rec == 'DAE':
                noise_x = []
                for v in range(view):
                    # print(xs[v])
                    noise = torch.randn(xs[v].shape).to(device)
                    # print(noise)
                    noise = noise + xs[v]
                    # print(noise)
                    noise_x.append(noise)
                optimizer.zero_grad()
                _, _, xrs, _, _ = model(noise_x)
            if rec == 'MAE':
                noise_x = []
                for v in range(view):

                    if xnum == args.batch_size and flag_full == 0 and epoch == 1:
                        # print(1)
                        num = xs[v].shape[0] * xs[v].shape[1]
                        ones = torch.ones([1, num]).to(device)
                        zeros_num = int(num * p)
                        for i in range(zeros_num):
                            ones[0, i] = 0
                        Vones_full.append(ones)
                    if xnum is not args.batch_size and flag_not_full == 0 and epoch == 1:
                        # print(1)
                        num = xs[v].shape[0] * xs[v].shape[1]
                        ones = torch.ones([1, num]).to(device)
                        zeros_num = int(num * p)
                        for i in range(zeros_num):
                            ones[0, i] = 0
                        Vones_not_full.append(ones)

                    if xnum == args.batch_size and epoch == 1:
                        noise = Vones_full[v][:, torch.randperm(Vones_full[v].size(1))]
                    if xnum is not args.batch_size and epoch == 1:
                        noise = Vones_not_full[v][:, torch.randperm(Vones_not_full[v].size(1))]

                    if xnum == args.batch_size and epoch is not 1:
                        noise = mask_ones_full[v][:, torch.randperm(mask_ones_full[v].size(1))]
                    if xnum is not args.batch_size and epoch is not 1:
                        noise = mask_ones_not_full[v][:, torch.randperm(mask_ones_not_full[v].size(1))]
                    noise = torch.reshape(noise, xs[v].shape)
                    noise = noise * xs[v]
                    noise_x.append(noise)

                if xnum == args.batch_size:
                    flag_full = 1
                else:
                    flag_not_full = 1

                optimizer.zero_grad()
                _, _, xrs, _, _ = model(noise_x)

            loss_list = []
            for v in range(view):
                loss_list.append(criterion(xs[v], xrs[v]))
            loss = sum(loss_list)
            loss.backward()
            optimizer.step()
            tot_loss += loss.item()
        # print('Epoch {}'.format(epoch), 'Loss:{:.6f}'.format(tot_loss / len(data_loader)))
        return Vones_full, Vones_not_full

    def High_level_contrastive_train(epoch, nmi_matrix, Lambda=1.0, rec='AE', p=0.3, mask_ones_full=[], mask_ones_not_full=[]):
        tot_loss = 0.
        mes = torch.nn.MSELoss()
        record_loss_con = []
        Vones_full = []
        Vones_not_full = []
        flag_full = 0
        flag_not_full = 0

        for v in range(view):
            record_loss_con.append([])
            for w in range(view):
                record_loss_con[v].append([])

        # Sim = 0
        # cos = torch.nn.CosineSimilarity(dim=0)

        for batch_idx, (xs, _, _) in enumerate(data_loader):
            for v in range(view):
                xs[v] = xs[v].to(device)

            optimizer.zero_grad()
            zs, qs, xrs, hs, re_h = model(xs)
            loss_list = []

            xnum = xs[0].shape[0]
            #------------------------
            # P = zs[0]
            # Q = zs[1]
            # for i in range(xnum):
            #     # print(cos(P[i], Q[i]))
            #     Sim += cos(P[i], Q[i]).item()
            #-------------------------
            if rec == 'DAE':
                noise_x = []
                for v in range(view):
                    # print(xs[v])
                    noise = torch.randn(xs[v].shape).to(device)
                    # print(noise)
                    noise = noise + xs[v]
                    # print(noise)
                    noise_x.append(noise)
                optimizer.zero_grad()
                _, _, xrs, _, _ = model(noise_x)
            if rec == 'MAE':
                noise_x = []
                for v in range(view):

                    if xnum == args.batch_size and flag_full == 0 and epoch == 1:
                        # print(1)
                        num = xs[v].shape[0] * xs[v].shape[1]
                        ones = torch.ones([1, num]).to(device)
                        zeros_num = int(num * p)
                        for i in range(zeros_num):
                            ones[0, i] = 0
                        Vones_full.append(ones)
                    if xnum is not args.batch_size and flag_not_full == 0 and epoch == 1:
                        # print(1)
                        num = xs[v].shape[0] * xs[v].shape[1]
                        ones = torch.ones([1, num]).to(device)
                        zeros_num = int(num * p)
                        for i in range(zeros_num):
                            ones[0, i] = 0
                        Vones_not_full.append(ones)

                    if xnum == args.batch_size and epoch == 1:
                        noise = Vones_full[v][:, torch.randperm(Vones_full[v].size(1))]
                    if xnum is not args.batch_size and epoch == 1:
                        noise = Vones_not_full[v][:, torch.randperm(Vones_not_full[v].size(1))]

                    if xnum == args.batch_size and epoch is not 1:
                        noise = mask_ones_full[v][:, torch.randperm(mask_ones_full[v].size(1))]
                    if xnum is not args.batch_size and epoch is not 1:
                        noise = mask_ones_not_full[v][:, torch.randperm(mask_ones_not_full[v].size(1))]

                    noise = torch.reshape(noise, xs[v].shape)
                    noise = noise * xs[v]
                    noise_x.append(noise)

                if xnum == args.batch_size:
                    flag_full = 1
                else:
                    flag_not_full = 1

                optimizer.zero_grad()
                _, _, xrs, _, _ = model(noise_x)

            for v in range(view):
                # for w in range(v + 1, view):
                for w in range(view):
                    # if v == w:
                    #     continue
                    if args.contrastive_loss == 'InfoNCE':
                        tmp = criterion.forward_feature_InfoNCE(zs[v], zs[w], batch_size=xnum)
                    if args.contrastive_loss == 'PSCL':
                        tmp = criterion.forward_feature_PSCL(zs[v], zs[w])
                    if args.contrastive_loss == 'RINCE':
                        tmp = criterion.forward_feature_RINCE(zs[v], zs[w], batch_size=xnum)

                    # loss_list.append(tmp)
                    loss_list.append(tmp * nmi_matrix[v][w])
                    record_loss_con[v][w].append(tmp)

                loss_list.append(Lambda * mes(xs[v], xrs[v]))
            loss = sum(loss_list)
            loss.backward()
            optimizer.step()
            tot_loss += loss.item()

        # print(Sim / 1400)  # 1400 is the data size of Caltech

        for v in range(view):
            for w in range(view):
                record_loss_con[v][w] = sum(record_loss_con[v][w])
                record_loss_con[v][w] = record_loss_con[v][w].item() / len(data_loader)

        return Vones_full, Vones_not_full, record_loss_con, _

    if not os.path.exists('./models'):
        os.makedirs('./models')

    model = Network(view, dims, args.feature_dim, args.high_feature_dim, class_num, device)
    # print(model)
    model = model.to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate, weight_decay=args.weight_decay)
    criterion = Loss(args.batch_size, class_num, args.temperature_f, device).to(device)

    print("Initialization......")
    epoch = 0
    while epoch < args.mse_epochs:
        epoch += 1
        if epoch == 1:
            mask_ones_full, mask_ones_not_full = Low_level_rec_train(epoch,
                                                                     rec=args.Recon,
                                                                     p=per,
                                                                     )
        else:
            Low_level_rec_train(epoch,
                                rec=args.Recon,
                                p=per,
                                mask_ones_full=mask_ones_full,
                                mask_ones_not_full=mask_ones_not_full,
                                )

    acc, nmi, ari, pur, nmi_matrix_1, _ = valid(model, device, dataset, view, data_size, class_num,
                                                eval_h=True, eval_z=False, times_for_K=args.times_for_K,
                                                Measure=args.measurement, test=False, sample_num=sample_mmd)

    print("Self-Weighted Multi-view Contrastive Learning with Reconstruction Regularization...")
    Iteration = 1
    print("Iteration " + str(Iteration) + ":")
    epoch = 0
    record_loss_con = []
    record_cos = []
    while epoch < Total_con_epochs:
        epoch += 1
        if epoch == 1:
            mask_ones_full, mask_ones_not_full, record_loss_con_, record_cos_ = High_level_contrastive_train(epoch,
                                                                                nmi_matrix_1,
                                                                                args.Lambda,
                                                                                rec=args.Recon,
                                                                                p=per)
        else:
            _, _, record_loss_con_, record_cos_ = High_level_contrastive_train(epoch,
                                                                  nmi_matrix_1,
                                                                  args.Lambda,
                                                                  rec=args.Recon,
                                                                  p=per,
                                                                  mask_ones_full=mask_ones_full,
                                                                  mask_ones_not_full=mask_ones_not_full,
                                                                  )

        record_loss_con.append(record_loss_con_)
        record_cos.append(record_cos_)
        if epoch % args.con_epochs == 0:
            if epoch == args.mse_epochs + Total_con_epochs:
                break

            # print(nmi_matrix_1)

            acc, nmi, ari, pur, _, nmi_matrix_2 = valid(model, device, dataset, view, data_size, class_num,
                                                        eval_h=False, eval_z=True, times_for_K=args.times_for_K,
                                                        Measure=args.measurement, test=False, sample_num=sample_mmd)
            nmi_matrix_1 = nmi_matrix_2
            if epoch < Total_con_epochs:
                Iteration += 1
                print("Iteration " + str(Iteration) + ":")

        pg = [p for p in model.parameters() if p.requires_grad]
        #  this code matters, to re-initialize the optimizers
        optimizer = torch.optim.Adam(pg, lr=args.learning_rate, weight_decay=args.weight_decay)

    accs.append(acc)
    nmis.append(nmi)
    aris.append(ari)
    purs.append(pur)

    # if acc > ACC_tmp:
    #     ACC_tmp = acc
    #     state = model.state_dict()
    #     torch.save(state, './models/' + args.dataset + '.pth')

    t2 = time.time()
    print("Time cost: " + str(t2 - t1))
    print('End......')

print(accs, np.mean(accs)/0.01, np.std(accs)/0.01)
print(nmis, np.mean(nmis)/0.01, np.std(nmis)/0.01)
# print(aris, np.mean(aris)/0.01, np.std(aris)/0.01)
# print(purs, np.mean(purs)/0.01, np.std(purs)/0.01)

def PLOT_LOSS(record_loss_con=[]):
    import matplotlib.pyplot as plt
    from matplotlib import pyplot
    plt.style.use('seaborn-whitegrid')
    palette = pyplot.get_cmap('Set1')
    font1 = {'family': 'Times New Roman',
             'weight': 'normal',
             'size': 50,
             }
    fontsize = 60
    fig = plt.figure()
    ax = fig.add_subplot()
    length = 133      # 400 / 3
    # print(len(record_loss_con))
    iters = np.linspace(0, length - 1, length, dtype=int)

    loss = []
    v1 = 0
    v2 = 3
    for i in range(length):
        loss.append(record_loss_con[3 * i + 1][v1][v2])
    ax.plot(iters, loss, color=palette(1), linestyle='-', label='InfoNCE loss (View 1; View 4)', linewidth=4.0)

    loss = []
    v1 = 3
    v2 = 4
    for i in range(length):
        loss.append(record_loss_con[3 * i + 1][v1][v2])
    ax.plot(iters, loss, color=palette(2), linestyle='-', label='InfoNCE loss (View 4; View 5)', linewidth=4.0)

    ax.legend(prop=font1, frameon=1, fancybox=0, framealpha=1)
    ax.set_xlabel('Training epochs', fontsize=fontsize)
    ax.set_ylabel('Loss values', fontsize=fontsize)
    plt.xticks([0, 33, 66, 99, 132], [0, 100, 200, 300, 400], rotation=0, fontsize=fontsize)
    plt.yticks(rotation=0, fontsize=fontsize)
    plt.show()

if Dataname == 'Caltech' and args.contrastive_loss == 'InfoNCE' and args.bi_level_iteration == 4:
    PLOT_LOSS(record_loss_con)


from classes import address_book
from notes import user_notes
from parse_utils import parse_input

def main():
    try:
        while True:
            command_input = input("Enter command: ")
            result = parse_input(command_input)
            print(result)
            if result == 'good bye':
                break
    finally:
        address_book.save_contacts_to_file()
        user_notes.save_notes_to_file()

if __name__ == "__main__":
    main()


import json
from functools import partial
from multiprocessing import Pool
from typing import Dict, Optional

import numpy as np
import pandas as pd
import requests
from pkg_resources import resource_filename

def get_raw_data(proxies: Optional[Dict[str, str]] = None):
    concelhos_path = resource_filename('covid.data.pt', 'concelhos.csv')
    concelhos = pd.read_csv(concelhos_path, usecols=['Concelho']).Concelho.str.upper().to_list()
    with Pool(10) as p:
        f = partial(_get_concelho, proxies=proxies)
        dfs = p.map(f, concelhos)
    dados_por_concelho = pd.concat(dfs).rename(
        columns={'Concelho': 'CONCELHO'}).drop_duplicates(['Data', 'CONCELHO'])
    dados_por_concelho.Data = pd.to_datetime(dados_por_concelho.Data, unit='ms')
    return dados_por_concelho

def _get_concelho(concelho, proxies):
    query_template = ('https://services.arcgis.com/CCZiGSEQbAxxFVh3/ArcGIS/rest/services/'
                      'COVID_Concelhos_DadosDiariosConcelho_VIEW2/FeatureServer/0/query?'
                      'where=Concelho+like+%27{concelho}%27&objectIds=&time=&geometry='
                      '&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects'
                      '&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false'
                      '&outFields=*&returnGeometry=false&featureEncoding=esriDefault'
                      '&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision='
                      '&outSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false'
                      '&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false'
                      '&returnQueryGeometry=false&returnDistinctValues=false&cacheHint=false&orderByFields='
                      '&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=32000'
                      '&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters='
                      '&sqlFormat=none&f=pjson&token=')
    query = query_template.format(concelho=concelho)
    json_data = json.loads(requests.get(query, proxies=proxies).text)
    df = pd.DataFrame([f['attributes'] for f in json_data['features']])
    return df

def get_data(proxies: Optional[Dict[str, str]] = None) -> pd.DataFrame:
    dados_por_concelho = get_raw_data(proxies)
    colunas_de_interesse = ['CONCELHO', 'ConfirmadosAcumulado', 'ConfirmadosNovos', 'Data']
    dados_por_concelho = dados_por_concelho[colunas_de_interesse]

    tabela = dados_por_concelho.pivot('Data', 'CONCELHO').ffill().fillna(0)
    tabela[
        (tabela > tabela.sort_index(ascending=False).cummin().sort_index())] = np.nan
    tabela = tabela.interpolate()
    dados_por_concelho = tabela.stack().reset_index()

    concelhos_path = resource_filename('covid.data.pt', 'concelhos.csv')
    concelhos = pd.read_csv(concelhos_path)
    concelhos = concelhos[~concelhos['Nível I'].isna()]
    resultado = pd.merge(left=dados_por_concelho,
                         right=concelhos,
                         left_on='CONCELHO',
                         right_on=concelhos.Concelho.str.upper()).drop(columns='CONCELHO')
    return resultado


import torch
from tensorboardX import SummaryWriter
from torch import nn
from torch.utils.data import DataLoader
import numpy as np
import matplotlib.pyplot as plt

def compute_val_loss(net: nn.Module, val_loader: DataLoader, criterion,
                     masked_flag: int, missing_value: float, sw: SummaryWriter,
                     epoch: int, limit=None):
    """

    :param net: 模型
    :param val_loader: Validation 加载器
    :param criterion: 损失函数
    :param masked_flag: 是否 Masked
    :param missing_value: 缺失值
    :param sw: 日志工具
    :param epoch: 训练步数
    :param limit: None
    :return: Loss Value
    """
    net.train(False)  # ensure dropout layers are in evaluation mode
    with torch.no_grad():

        tmp = []  # 记录了所有batch的loss
        prediction = []
        target = []

        for batch_index, batch_data in enumerate(val_loader):
            encoder_inputs, labels = batch_data
            outputs = net(encoder_inputs)
            prediction.append(outputs.detach().cpu().numpy())
            target.append(labels.detach().cpu().numpy())
            if masked_flag:
                loss = criterion(outputs, labels, missing_value)
            else:
                loss = criterion(outputs, labels)

            tmp.append(loss.item())
            if (limit is not None) and batch_index >= limit:
                break
        prediction = np.concatenate(prediction, axis=0)
        target = np.concatenate(target, axis=0)

        if epoch % 5 == 0:
            for i in range(10):
                fig = plt.figure()
                plt.title('Node {} Validation Result'.format(i))
                plot_length = int(0.2 * len(prediction))
                y_predict = prediction[:plot_length, i, -1]
                y_label = target[:plot_length, i, -1]
                plt.plot(range(plot_length), y_label, label='real')
                plt.plot(range(plot_length), y_predict, label='predict')
                plt.xlabel('time step')
                plt.ylabel('flow value')
                plt.legend()
                sw.add_figure(
                    'Node {} Validation Result Figure'.format(i),
                    fig,
                    global_step=epoch,
                    close=True
                )

        validation_loss = sum(tmp) / len(tmp)
        sw.add_scalar('validation_loss', validation_loss, epoch)
    return validation_loss


import parsefunc
import convertfunc

feet_inches = input("Enter feet and inches: ")

parsed = parsefunc.parse(feet_inches)

result = convertfunc.convert(parsed['feet'], parsed['inches'])

print(f"{parsed['feet']} feet and {parsed['inches']} is equal to {result}")

if result < 1:
    print("Sorry you are too small to enter te ride.")
else:
    print("Come on in you can enter the ride.")


"""
Endpoint routers for users.

Eventually might need to handle auth here as well, so write code as if
that was an upcoming feature.
"""
from fastapi import APIRouter, Depends

import util.users as utils
import util.auth as auth_utils
from docs import users as docs
from models import users as models
import models.commons as common_models

router = APIRouter()

@router.post(
    "/users/register",
    response_model=models.UserAuthenticationResponse,
    description=docs.registration_desc,
    summary=docs.registration_summ,
    tags=["Users"],
    status_code=201,
)
async def register_user(form: models.UserRegistrationForm):
    # send the form data and DB instance to util.users.register_user
    user_id = await utils.register_user(form)

    auth_token_str = await utils.get_auth_token_from_user_id(user_id)

    # return response in response model
    return models.UserAuthenticationResponse(jwt=auth_token_str)

@router.delete(
    "/users/delete",
    description=docs.delete_user_desc,
    summary=docs.delete_user_summ,
    tags=["Users"],
    status_code=204,
)
async def delete_user(
    identifier: models.UserIdentifier,
    user_id_from_token: str = Depends(
        auth_utils.get_user_id_from_header_and_check_existence)):
    identifier.check_user_id_matches_or_error(user_id_from_token)
    await utils.delete_user(identifier)

@router.post("/users/find",
             response_model=models.UserInfoQueryResponse,
             description=docs.find_user_by_identifier_desc,
             summary=docs.find_user_by_identifier_summ,
             tags=["Users"],
             status_code=200)
async def get_user(identifier: models.UserIdentifier):
    user_data = await utils.get_user_info_by_identifier(identifier)
    return models.UserInfoQueryResponse(**user_data.dict())

@router.post("/users/login",
             response_model=models.UserAuthenticationResponse,
             description=docs.login_user_desc,
             summary=docs.login_user_summ,
             tags=["Users"],
             status_code=200)
async def login_user(login_form: models.UserLoginForm):
    return await utils.login_user(login_form)

@router.put("/users/add_event",
            response_model=models.UserAddEventResponse,
            description=docs.user_add_event_desc,
            summary=docs.user_add_event_summ,
            tags=["Users"],
            status_code=201)
async def add_event_to_user(
    add_event_form: models.UserAddEventForm,
    user_id: common_models.UserId = Depends(
        auth_utils.get_user_id_from_header_and_check_existence)):
    return await utils.user_add_event(add_event_form, user_id)

@router.patch("/users/update",
              response_model=models.UserUpdateResponse,
              description=docs.update_user_desc,
              summary=docs.update_user_summ,
              tags=["Users"],
              status_code=200)
async def update_user(
    update_form: models.UserUpdateForm,
    user_id_from_token: str = Depends(
        auth_utils.get_user_id_from_header_and_check_existence)):
    identifier = update_form.identifier
    identifier.check_user_id_matches_or_error(user_id_from_token)

    return await utils.update_user(update_form)


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="TickerStore",
    version="0.0.8",
    author="Apoorva Singh",
    author_email="apoorva.singh157@gmail.com",
    description="Historical data of financial instruments from NSE",
    long_description=long_description,
    url="https://github.com/meticulousCraftman/tickerstore",
    license="MIT",
    install_requires=[
        "upstox",
        "python-dotenv",
        "Flask",
        "influxdb",
        "crayons",
        "nsepy",
        "pandas",
        "requests",
        "lxml",
        "loguru",
    ],
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)